1. Title:
1180 Stone GameTime limit:1.0 Second
Memory limit:64 mbtwo nikifors play a funny game. There is a heap of
NStones 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 is to write a program, that determines winner assuming Nikifor does it best. Inputan input contains the only positive integer number
N(condition
N≤10^250 holds). Outputthe 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
2. Solving Ideas
This kind of game problem, all from the simplest case consideration, recursion to the complex situation. But there are some interesting tricks to this problem ~
Basic recursion:
N win?
1 y
2 y
3 N
Well. Analysis here, the idea is probably this:
BOOL Suc[maxn+1];
SUC[1] = suc[2] = true;
for (i = 3; I <= n; i++) {
for (b = 1; b < n; b *= 2) {
if (suc[i-b] = = False) {
Suc[i] = true;
Break
}
}
}
However, looking back at the size of the topic, found that the value of n range is n <= 10^250. This is a high-precision rhythm.
No hurry, go back and see if there is any optimization method. By finding patterns,
Found: n = 3*n, suc[n] = n
N = 3*n + 1 | | N = 3*n + 2, suc[n] = y
This problem is converted to n%3.
and a decimal number n = SUM (ai*10^i) = SUM (AI) + sum (ai* (10^i-1)) (i>=0)
and 10^i-1 = 0 (mod3)
So n = SUM (AI) (mod3)
3. Code:
#include <iostream>using namespacestd;intMain () {ints =0; Chartmp; while(CIN >> tmp) s + = tmp-'0'; if(s%3==0) cout <<2; Elsecout <<1<<'\ n'<< S%3; //return 0;}
Timus 1180. Stone Game Problem Solving report