Good Luck in CET-4 Everybody!
Problem Description
CET-4 is coming. Are you studying nervously? Maybe I'm so nervous that I don't even have time to practice ACM for a short semester. Anyway, I know Kiki and ICI are the same. Of course, as a contemporary college student who has infiltrated the examination hall for more than ten years, Kiki and Ci know more about the relaxation before the examination. That is what the so-called "relaxation and morality" means. No, Kiki and Ci play poker for a while every night before they rest to relax their nerves.
"Upgrade"? "Double buckle"? "Red Five"? Or "fight against landlords"?
Of course not! How vulgar that is~
As students of Computer College, Kiki and Ci play cards without forgetting their major. Their rules of playing cards are as follows:
1. Total n cards;
2. Both sides take turns to grab cards.
3. The number of cards per person can only be a power of 2 (i.e. 1, 2, 4, 8, 16...)
4. When the cards are finished, the result of winning or losing comes out: the man who finishes the cards is the winner;
Assuming that Kiki and Ci are smart enough (no need to assume that there are no smart students ~), and that each time Kiki grabs the cards first, who can win?
Of course, no matter who wins, it's not a big problem. The important thing is that the coming CET-4 will be in good condition.
Good luck in CET-4 everybody!
Input
The input data contains multiple test cases, each of which takes up one line and contains an integer n (1<=n<=1000).
Output
If Kiki wins, output "Kiki" or "Cici" with one line for each instance.
Sample Input
1
3
Sample Output
Kiki
Cici
Ideas:
1. Proof of Pushing Small Data by Hand Guess
It can be deduced that when n%3 is 0, the first hand will fail.
Code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n%3)
printf("Kiki\n");
else
printf("Cici\n");
}
return 0;
}
2. Direct use of SG functions
Typographic edition:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e3+10;
int f[maxn],sg[maxn];
bool vis[maxn];
int ps;
void Get_SG(int n)
{
memset(sg,0,sizeof(sg));
for(int i=1; i<=n; ++i)
{
memset(vis,false,sizeof(vis));
for(int j=1; f[j]<=i&&j<=ps; ++j)
vis[sg[i-f[j]]]=true;
for(int j=0;; ++j)
if(!vis[j])
{
sg[i]=j;
break;
}
}
}
int main()
{
ps=0;
for(int i=1;i<=1000;i*=2)
f[++ps]=i;
Get_SG(1000);
int x;
while(~scanf("%d",&x))
{
if(sg[x])
printf("Kiki\n");
else
printf("Cici\n");
}
return 0;
}
DFS version:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e3+10;
int s[maxn],sg[maxn],n;
int SG_dfs(int x)
{
if(sg[x]!=-1)
return sg[x];
bool vis[maxn];
memset(vis,false,sizeof(vis));
for(int i=1; i<=n; ++i)
{
if(x>=s[i])
{
SG_dfs(x-s[i]);
vis[sg[x-s[i]]]=true;
}
}
for(int i=0;; ++i)
if(!vis[i])
return sg[x]=i;
}
int main()
{
memset(sg,-1,sizeof(sg));
sg[0]=0;
n=0;
for(int i=1;i<=1000;i*=2)
s[++n]=i;
SG_dfs(1000);
int x;
while(~scanf("%d",&x))
{
if(sg[x])
printf("Kiki\n");
else
printf("Cici\n");
}
return 0;
}
SG Function Reference Blog: Strangedbly