hdu 1847 Good Luck in CET-4 Everybody! (巴什博奕,找规律||sg函数)

hdu1847题目链接 题意:n个石子,每次只能取2的幂次个。。。问先手是否有必赢策略。。。 思路:画n点p点。。。发现n为3的倍数的时候先手必输。。。否则先手必赢。。。

/* ***********************************************
Author :111qqz
Created Time :2016年07月20日 星期三 18时54分46秒
File Name :code/hdu/1847.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6int n;
 7int main()
 8{
 9	#ifndef  ONLINE_JUDGE 
10	freopen("code/in.txt","r",stdin);
11  #endif
 1	while (scanf("%d",&n)!=EOF)
 2	{
 3	    if (n%3==0)
 4	    {
 5		puts("Cici");
 6	    }
 7	    else
 8	    {
 9		puts("Kiki");
10	    }
11	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}

也可以利用sg函数求解。。。 从这里我们可以看出,sg函数类似于母函数,本身并不是一类问题,而是一种求解问题的工具。

/* ***********************************************
Author :111qqz
Created Time :2016年07月20日 星期三 18时54分46秒
File Name :code/hdu/1847.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E3+7; 
 7int n;
 8int two[12];
 9int sg[N];
10bool vis[N];
void sg_solve()
{
1    two[0] = 1;
2    for ( int i = 1 ; i <=10 ; i++) two[i] = two[i-1]*2;
3    ms(sg,0);
1    for ( int i = 1 ; i < N ; i++)
2    {
3	ms(vis,false);
4	for ( int j = 0 ;  j <=10 ; j++)
5	    if (i-two[j]>=0) vis[sg[i-two[j]]] = true;
6	    //i-two[j]表示能从i到达的状态。。。 这里完全是按照定义在求。。。
1	for ( int j = 0 ; ; j++)
2	    if (!vis[j]) //第一个没有出现的值。。。
3	    {
4		sg[i] = j ;
5//		cout<<"j:"<<j<<endl;
6		break;
7	    }
8    }
1}
2int main()
3{
4	#ifndef  ONLINE_JUDGE 
5	freopen("code/in.txt","r",stdin);
6  #endif
 1	sg_solve();
 2//	for ( int i = 1 ; i <= 100 ; i++) cout<<"i:"<<i<<" sg[i]"<<sg[i]<<endl;
 3	while (scanf("%d",&n)!=EOF)
 4	{
 5	    if (!sg[n])
 6	    {
 7		puts("Cici");
 8	    }
 9	    else
10	    {
11		puts("Kiki");
12	    }
13	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}