Timus 1180. Stone Game 游戏题目

游戏策略:石头游戏与3的倍数规律
本文探讨了一款经典的石头游戏策略,玩家轮流从一堆石头中取出一定数量的石头,所取数量必须为2的幂次。通过分析3的倍数规则,揭示了如何预测并确保胜利的方法。

Two Nikifors play a funny game. There is a heap ofNstones 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 numberN(conditionN≤ 10250holds).

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;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值