Buttons
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4117 | Accepted: 1137 |
Description
As you surely already know, Yekaterinburg has gotten its right to hold The Summer Olympic Games of the 2032. It is planned that it will be allowed to Russia as a country-organizer to emend a program of the games a bit. So, in order to improve the command result it has been decided to replace the competition in gymnastics by the competition in the new game "Buttons".
The rules of the game are very simple. There‘s a small heap of K buttons before two players. The players in turns take buttons from the heap, moreover, at a time one can take a number of buttons from 1 up to L. The one who takes the last button is the winner.
The rules of the Olympic Games will be a bit harder then usual. The one, who is to make a first step according to a lot, has an opportunity to fix a number K with the following restriction to it: 3 <= K <= 100 000 000 (that is the exact number of buttons that has been prepared for the Olympic tournament). The player who is to make the second step fixes a number L that satisfies the following conditions 2 <= L < K.
A very crucial task is given to your team: you are to write a program that should help the second player to make his choice. In other words, given a number K your program is to find a number L that guaranties a victory to the second player with a proper game of both sides.
So, for instance, there are only three buttons in the heap, the choice L = 2 provides for the victory of the second player. Really, if the first player takes only one button at his turn, the second one wins, taking the two last buttons. On the contrary, if the first one takes two buttons, the second one wins, taking the last button.
The rules of the game are very simple. There‘s a small heap of K buttons before two players. The players in turns take buttons from the heap, moreover, at a time one can take a number of buttons from 1 up to L. The one who takes the last button is the winner.
The rules of the Olympic Games will be a bit harder then usual. The one, who is to make a first step according to a lot, has an opportunity to fix a number K with the following restriction to it: 3 <= K <= 100 000 000 (that is the exact number of buttons that has been prepared for the Olympic tournament). The player who is to make the second step fixes a number L that satisfies the following conditions 2 <= L < K.
A very crucial task is given to your team: you are to write a program that should help the second player to make his choice. In other words, given a number K your program is to find a number L that guaranties a victory to the second player with a proper game of both sides.
So, for instance, there are only three buttons in the heap, the choice L = 2 provides for the victory of the second player. Really, if the first player takes only one button at his turn, the second one wins, taking the two last buttons. On the contrary, if the first one takes two buttons, the second one wins, taking the last button.
Input
The
standard input consists of one line, which contains an only integer
number K — a number of buttons in the heap, that has fixed the first
player at his turn.
Output
To
the standard output you are to write the only number L — the maximal
number of buttons that can be taken at a time which provides for the
victory of the second player. If there are several those numbers L, you
should write the least. If there are no such numbers, you are to write 0
to the standard output.
Sample Input
3
Sample Output
2
分析:题意为一堆K粒纽扣,两个玩家轮流从堆中取纽扣,一次可以取1到L个纽扣,取得最后一个纽扣的人是赢家。
让你找到数字最小的数字L(2<L<K)确保第二个人为赢家,如果不存在则输出0。
很基础的巴什博弈,只需找到K的大于3的最小因子x,那么L=x-1。
#include<cstdio> #include<algorithm> using namespace std; int main() { int K; while(scanf("%d",&K)!=EOF) { int a[100000]; int cnt=0; for(int i=1;i*i<=K;i++) { if(K%i==0) { a[cnt++]=i; a[cnt++]=K/i; } } int flag=1; sort(a,a+cnt); for(int i=0;i<cnt;i++) { if(a[i]>=3) { printf("%d\n",a[i]-1); flag=0; break; } } if(flag) printf("0\n"); } return 0; }