URAL 1180. Stone Game (博弈 + 规律)

1180. Stone Game

Time limit: 1.0 secondMemory limit: 64 MB

Two Nikifors play a funny game. There is a heap ofN 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 numberN (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

inputoutput

812

Problem Author: Dmitry FilimonenkovProblem Source: Third USU personal programming contest, Ekaterinburg, Russia, February 16, 2002

解析:找规律。

考虑如下例子:剩余石子的数量 first Nikifor1 win2 win—- 依次选择1,2. 并且1 和 2是可以选择的。

3 lose—- 因为你选1或2的时候,另外一个人总能一次把剩下的取完。4, 5 win

—- 依次选择1,2,能够获胜。6 lose7, 8 win…. 从上面的规律我们可以看出当 N mod 3 == 0 我们能确认first Nikifor一定能失败;否则的话,,我们能选择的最小石子的数量 = N mod 3,此时first Nikifor获胜。

AC代码:

#include <bits/stdc++.h>using namespace std;int main(){int ans = 0;char c;while(scanf("%c", &c) && c != '\n') ans += c – '0';//求个位数字之和以判断数字是否能被3整除,数太大,直接存不下!if(ans % 3 == 0) puts("2");else printf("%d\n%d", 1, ans % 3);return 0;}.

同时也用对她的怀念来惩罚自己。

URAL 1180. Stone Game (博弈 + 规律)

相关文章:

你感兴趣的文章:

标签云: