A simple stone game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 359 Accepted Submission(s): 190
The game goes like this: Two players start the game with a pile of n stones. They take stones from the pile in turn and every time they take at least one stone. The one who goes first can take at most n-1 stones for his first move. From then on a player can take at most k times as many stones as his opponent has taken last time. For example, if one player take m stones in his turn, then the other player can take at most k × m stones next time. The player who takes the last stone wins the game. Suppose that those two players always take the best moves and never make mistakes, your job is to find out who will definitely win the game.
Each test case is a line consisting of two integer n and k.(2<=n<=10^8,1<=k<=10^5).
5 16 1 11 1 32 2 34 2 19 3
Case 1: lose Case 2: 1 Case 3: 3 Case 4: lose Case 5: 4
题解
http://blog.csdn.net/acm_cxlove/article/details/7836544
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=4000000+1000;
int a[maxn];
int b[maxn];
int main()
{
int ca=1,t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
a[0]=b[0]=1;
int i,j;
i=j=0;
while(b[i]<n)
{
i++;
a[i]=b[i-1]+1;
while(a[j+1]*k<a[i])
j++;
if(a[j]*k<a[i])
b[i]=a[i]+b[j];
else
b[i]=a[i];
}
printf("Case %d: ",ca++);
if(a[i]==n)
printf("lose\n");
else
{
int ans;
while(n)
{
if(a[i]<=n)
{
n-=a[i];
ans=a[i];
}
i--;
}
printf("%d\n",ans);
}
}
return 0;
}
zoj2599
Alice and Bod decide to play a new stone game. At the beginning Alice puts n(n>1) stones (out of N in all) on the table. Alice and Bob remove the stones in turn. At each step the player should remove some number of stones. The number of stones the player removed should be at least one, and cannot exceed m times of the number of stones the player removed at the last step. The player who removes the last stone wins the game. Alice always plays first and of course at the first turn, she cannot remove all the stones. Alice wants to know how many positive integers n she can choose to win the game if both players play optimally.
Since you're an ace programmer, Alice wants you to help her.
Input
There are multiple test cases. The first line of input contains an integer T (0 < T ≤ 500) indicating the number of test cases. Then T test cases follow.
Each test case is a line of 2 integers m and N(0 < m ≤ 2012, 1 < N < 231)
Output
For each test case output the number of positive integers Alice can choose for n to make her win the game.
Sample Input
3 1 10 2 10 3 10
Sample Output
6 5 4代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn=2000000+100;
long long a[maxn],b[maxn];
using namespace std;
int main()
{
long long n,k;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&k,&n);
int i,j;
i=j=0;
a[0]=b[0]=1;
while(a[i]<n)
{
i++;
a[i]=b[i-1]+1;
while(a[j+1]*k<a[i])
j++;
if(a[j]*k<a[i])
b[i]=b[j]+a[i];
else
b[i]=a[i];
}
long long ans;
if(a[i]==n)
ans=n-i-1;
else
ans=n-i;
printf("%lld\n",ans);
}
return 0;
}