码迷,mamicode.com
首页 > 其他好文 > 详细

Timus 1180. Stone Game 游戏题目

时间:2014-04-28 10:17:41      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:timus   1180. stone game   游戏题目   

Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only condition that this number is a nonnegative integer power of 2 (e.g. 1, 2, 4, 8 etc.). Nikifor who takes the last stone wins. You are to write a program that determines winner assuming each Nikifor does its best.

Input

An input contains the only positive integer number N (condition N ≤ 10250 holds).

Output

The first line should contain 1 in the case the first Nikifor wins and 2 in case the second one does. If the first Nikifor wins the second line should contain the minimal number of stones he should take at the first move in order to guarantee his victory.

Sample

input output
8
1
2

这也是个有趣的问题,也很经典的游戏题目的变形了。

不过这道题扩展了成为无限大的数了。

类似的游戏有:没人可以拿掉桌面上的棋子,每次不能超过5个,最后没棋子可以拿的算输


解决这样的题目只能是寻找规律了,不能真的模拟区玩了,否则必定超时。

这道题目的规律就是:

1 如果给出的stone是3的倍数,那么先取者必输

2 如果给出的不是3的倍数,那么先取者就凑成3的倍数就必赢,因为凑3的倍数很容易,去掉1个或者2个必定可以凑出来了

所以最后问题就成了mod3问题了。

我是怎么想出来的?

我是一个列子一个例子去观察,最后得出结论的,然后验证,AC,结论正确。

也挺花时间的。

#include <string>
#include <iostream>
using namespace std;

int StoneGameMod3(string &s)
{
	int carry = 0;
	for (int i = 0; i < s.size(); i++)
	{
		int a = carry * 10 + s[i] - ‘0‘;
		carry = a % 3;
	}
	return carry;
}

void StoneGame1180()
{
	string s;
	cin>>s;
	int mod3 = StoneGameMod3(s);
	if (0 == mod3) cout<<2;
	else cout<<1<<endl<<mod3;
}

int main()
{
	StoneGame1180();
	return 0;
}



Timus 1180. Stone Game 游戏题目

标签:timus   1180. stone game   游戏题目   

原文地址:http://blog.csdn.net/kenden23/article/details/24619077

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!