zoukankan      html  css  js  c++  java
  • SG 函数初步 HDU 1536 && HDU 1944

    题目链接:http://acm.hdu.edu.cn/showproblem.php?

    pid=1944

                        http://acm.hdu.edu.cn/showproblem.php?pid=1536

    给定每一次能够取的石头数,给定非常多种情况,每一种情况有若干堆石头,推断先手胜负。

    SG函数打表,然后直接抑或。推断结果是否为0。第一次写SG函数,贴个代码,慢慢理解。

    代码:

    /* ***********************************************
    Author :rabbit
    Created Time :2014/7/4 12:00:18
    File Name :3.cpp
    ************************************************ */
    #pragma comment(linker, "/STACK:102400000,102400000")
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <sstream>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    #include <string>
    #include <time.h>
    #include <math.h>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define eps 1e-8
    #define pi acos(-1.0)
    typedef long long ll;
    int f[110],d[20010],g[110];
    int SG(int p,int k){
    	memset(g,0,sizeof(g));
    	for(int i=0;i<k;i++){
    		int t=p-f[i];
    		if(t<0)break;
    		if(d[t]==-1)d[t]=SG(t,k);
    		g[d[t]]=1;
    	}
    	for(int i=0;;i++)
    		if(!g[i])return i;
    }
    int main()
    {
         //freopen("data.in","r",stdin);
         //freopen("data.out","w",stdout);
         int n;
    	 while(~scanf("%d",&n)){
    		 if(!n)break;
    		 for(int i=0;i<n;i++)scanf("%d",&f[i]);
    		 sort(f,f+n);
    		 memset(d,-1,sizeof(d));
    		 d[0]=0;
    		 for(int i=1;i<=10010;i++)
    			 d[i]=SG(i,n);
    		 int m;
    		 scanf("%d",&m);
    		 while(m--){
    			 int k;
    			 scanf("%d",&k);
    			 int sum=0,v;
    			 while(k--){
    				 scanf("%d",&v);
    				 sum^=d[v];
    			 }
    			 if(!sum)printf("L");
    			 else printf("W");
    		 }
    		 puts("");
    	 }
         return 0;
    }
    


  • 相关阅读:
    POJ 2155 Matrix
    Codeforces 626D Jerry's Protest 「数学组合」「数学概率」
    Codeforces 626E Simple Skewness 「数学」「二分」
    Codeforces 633D
    Codeforces 631C
    二分查找
    CodeForces 617C【序枚举】
    HDU 4405 【概率dp】
    ZOJ 3329 【概率DP】
    POJ 2096 【期望DP】
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7373004.html
Copyright © 2011-2022 走看看