您的位置:首页 > 其它

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

2016-07-30 10:27 459 查看
题意:两人依次从一堆石子中取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

1
2


 

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;


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: