SGU 153 Playing with matches(博弈)

原创 2013年12月03日 19:06:57
题意:取石子游戏升级版,每次能取1,P1,P2,...,Pm 个。



思路:没有后面的条件就很简单了。不过加了这个条件也没复杂多少,大概想法就是标出必胜点必败点,然后就能找出循环节,然后就好弄了,其实找到最大能取的石子的数量的两倍的石子数就能确定循环节了……


代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=100+10;
bool flag[maxn];
int op[10];
bool check(int p)
{
    int mid=p/2;
    for(int i=1;i<=mid;++i)
    {
        if(flag[i]!=flag[mid+i]) return false;
    }
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        op[0]=1;
        for(int i=1;i<=m;++i)
          scanf("%d",&op[i]);
        memset(flag,true,sizeof(flag));
        flag[1]=false;
        bool ff;
        for(int i=2;i<=20;++i)
        {
            ff=false;
            for(int j=0;j<=m;++j)
              if(i-op[j]>0&&!flag[i-op[j]]) ff=true;
            flag[i]=ff;
        }
        int p=50;
        while(true)
        {
            if(check(p)) break;
            p-=2;
        }
        n%=p;
        if(n==0) n=p;
        if(flag[n]) printf("FIRST PLAYER MUST WIN\n");
        else printf("SECOND PLAYER MUST WIN\n");
    }
    return 0;
}

FOJ Playing with Calculator (模运算)

Problem Description Pete is playing a game. He types on a calculator a natural number K and then ...

527A. Playing with Paper【math】

A. Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes i...

Playing with ptrace, Part I

Nov 01, 2002  By Pradeep Padala  in SysAdmin Using ptrace allows you to set up system call ...

programming-challenges Playing with Wheels (110902) 题解

一开始居然没想到bfs。 #include #include #include #include #include #include #include #include #inclu...

Playing with huge information streams: Twitter Storm!

Past Christmas I found the perfect pet project for that season: Twitter Storm. Basically is a e...
  • macyang
  • macyang
  • 2012年02月06日 21:24
  • 964

Playing with Virtual Memory

文章来源:http://www.snailinaturtleneck.com/blog/2011/08/30/playing-with-virtual-memory/  很赞的一篇文章! Whe...
  • macyang
  • macyang
  • 2011年09月01日 22:43
  • 783

Playing with ptrace, Part II

In Part II of his series on ptrace, Pradeep tackles the more advanced topics of setting breakpoints ...

playing with ptrace

Using ptrace allows you to set up system call interception and modification at the user level. Ha...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SGU 153 Playing with matches(博弈)
举报原因:
原因补充:

(最多只允许输入30个字)