Doubloon Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 578 Accepted Submission(s): 324
Problem Description
Being a pirate means spending a lot of time at sea. Sometimes, when there is not much wind, days can pass by without any activity. To pass the time between chores, pirates like to play games with coins.
An old favorite of the pirates is a game for two players featuring one stack of coins. In turn, each player takes a number of coins from the stack. The number of coins that a player takes must be a power of a given integer K (1, K, K^2, etcetera). The winner is the player to take the last coin(s).
Can you help the pirates gure out how the player to move can win in a given game situation?
Input
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
One line with two integers S and K, satisfying 1 <= S <= 10^9 and 1 <= K <= 100: the size of the stack and the parameter K, respectively.
Output
For every test case in the input, the output should contain one integer on a single line: the smallest number of coins that the player to move can take in order to secure the win. If there is no winning move, the output should be 0.
Sample Input
5
5 1
3 2
8 2
50 3
100 10
Sample Output
1
0
2
0
1
题意:给你
S
个石子和一个数字
思路:如果数据量小的话,可以直接求其
sg
值,但是现在数据范围为
1e9
,那么就不能那么暴力的写了,我们可以通过打表来找到规律,我们可以根据
k
的值来进行打表,会发现:
当
当
k
为偶数的时候,我们列举一下循环节:
k=4:1,0,1,2,0
k=6:1,0,1,0,1,2,0
…
就可以发现规律了,然后就可以判断当前局面能否到达必败面,如果可以,那么我们可以枚举选取的石子数量,时间复杂度是允许的.
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-10-10.39 Wednesday
File Name : D:\MyCode\2016-8月\2016-8-10.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int sg[1010],vis[1010];
void getSG()
{
sg[0]=0;
for(int i=1;i<=1000;i++)
{
mem(vis,0);
for(int j=1;j<=i;j*=6)
vis[sg[i-j]]=1;
for(int j=0;;j++)
if(!vis[j])
{
sg[i]=j;
break;
}
}
}
int main()
{
int t;cin>>t;
while(t--)
{
int n,k;cin>>n>>k;
if(k%2)
{
if(n%2) printf("1\n");
else printf("0\n");
}
else
{
int c=n%(k+1);
if(c<=k-1)
{
if(c%2) printf("1\n");
else printf("0\n");
}
else
{
int flag=0;
for(int j=k;j<=n;j*=k)
{
int x=n-j;
c=x%(k+1);
if(c%2==0)
{
flag=j;
break;
}
}
if(flag) printf("%d\n",flag);
else printf("0\n");
}
}
}
return 0;
}