[BeiJing2009 WinterCamp]取石子游戏 Nim SG 函数

Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 1003
int arr[13],step[13],SG[maxn];
bool vis[maxn];
int main(){
    //freopen("input.in","r",stdin);
    int n,m,MAX=0,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d",&arr[i]), MAX=max(MAX,arr[i]);
    scanf("%d",&m);
    for(int i=1;i<=m;++i) scanf("%d",&step[i]);
    SG[0]=0;
    for(int i=1;i<=1000;++i) {
        memset(vis,0,sizeof(vis));
        for(int j=1;j<=m&&step[j]<=i;++j) vis[SG[i-step[j]]]=1;      
        for(int j=0;;++j)
            if(!vis[j]){
                SG[i]=j;
                break;
            }
    }
    for(int i=1;i<=n;++i) ans^=SG[arr[i]];
    if(ans==0)printf("NO");
    else printf("YES\n");
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m&&step[j]<=arr[i];++j)
            if((ans^SG[arr[i]])==SG[arr[i]-step[j]]) {
                printf("%d %d",i,step[j]);
                return 0;
            }
    }
    return 0;
}

  

posted @ 2018-11-06 01:38  EM-LGH  阅读(136)  评论(0编辑  收藏  举报