URAL 1180 Stone Game 博弈 找规律 大数取模 除3取模

来源:互联网 发布:网络用语哔哔什么意思 编辑:程序博客网 时间:2024/05/17 00:00

题意:两人依次从一堆石子中取2的幂次个数的石子,如1 2 4 8 16…,能取到最后一颗石子的获胜。输入n,若第一人获胜则输出1和他第一次至少取多少个石子,若第二人获胜输出2。(n很大,10的250次幂)

结论:n%3=0则第二人获胜,否则第一人获胜且第一次至少取n%3个

心得:博弈题没有思路的时候就先找规律,足够敏感:取模,2的幂次,斐波那契等等!纸上演算或者写个程序跑打表观察!

 http://acm.timus.ru/problem.aspx?space=1&num=1180

1180. Stone Game

Time limit: 1.0 second
Memory limit: 64 MB

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

inputoutput
8
12

 

   char s[300];    intsum=0;   scanf("%s",s);   for(int i=0; i<strlen(s); i++)       sum+=(s[i]-'0');   if(sum%3==0) printf("2\n");    elseif (sum%3==1) printf("1\n1\n");    elseprintf("1\n2\n");   return 0;

 

0 0