zoukankan      html  css  js  c++  java
  • 取石子游戏 BZOJ1874 博弈

    小H和小Z正在玩一个取石子游戏。 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子,
    每次取石子的个数有限制,谁不能取石子时就会输掉游戏。 小H先进行操作,他想问你他是否有必胜策略,如果有
    ,第一步如何取石子。

    Sample OutputYES 1 1 Hint 样例中共有四堆石子,石子个数分别为7、6、9、3,每人每次可以从任何一堆石子中取出1个或者2个石子,小H有 必胜策略,事实上只要从第一堆石子中取一个石子即可。


    Input
    输入文件的第一行为石子的堆数N 
    接下来N行,每行一个数Ai,表示每堆石子的个数 接下来一行为每次取石子个数的种类数M 
    接下来M行,每行一个数Bi,表示每次可以取的石子个数,
    输入保证这M个数按照递增顺序排列。
    N≤10 Ai≤1000
    对于全部数据,M≤10,Bi≤10
    Output
    输出文件第一行为“YES”或者“NO”,表示小H是否有必胜策略。 
    若结果为“YES”,则第二行包含两个数,第一个数表示从哪堆石子取,第二个数表示取多少个石子,
    若有多种答案,取第一个数最小的答案,
    若仍有多种答案,取第二个数最小的答案。
    Sample Input4 7 6 9 3 2 1 2
     
    首先算出sg函数;
    数据范围较小,直接计算即可;
    如果所有a[ i ] 的sg函数=0,那么此时就NO;
    否则就为 YES,这时我们只需枚举遍历即可;
    注意一点:运算符优先级问题,^的时候要加();
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#pragma GCC optimize(2)
    //#include<cctype>
    //#pragma GCC optimize("O3")
    using namespace std;
    #define maxn 100005
    #define inf 0x3f3f3f3f
    #define INF 9999999999
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    ll sqr(ll x) { return x * x; }
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    
    
    ll qpow(ll a, ll b, ll c) {
    	ll ans = 1;
    	a = a % c;
    	while (b) {
    		if (b % 2)ans = ans * a%c;
    		b /= 2; a = a * a%c;
    	}
    	return ans;
    }
    
    int n, m;
    int a[maxn], b[maxn];
    bool vis[maxn];
    int sg[maxn];
    
    void SG() {
    	for (int i = 1; i <= 2000; i++) {
    		ms(vis);
    		for (int j = 1; j <= m; j++) {
    			if (i - b[j] >= 0)vis[sg[i - b[j]]] = 1;
    		}
    		for (int j = 0; j <= 10; j++)
    			if (vis[j] == 0) {
    				sg[i] = j; break;
    			}
    	}
    }
    
    int main()
    {
    	//ios::sync_with_stdio(0);
    	rdint(n);
        for (int i = 1; i <= n; i++)rdint(a[i]);
    	rdint(m);
    	for (int i = 1; i <= m; i++)rdint(b[i]);
    	SG();
    	int ans = 0;
    	for (int i = 1; i <= n; i++)ans ^= sg[a[i]];
    	if (ans == 0)cout << "NO" << endl;
    	else {
    		for (int i = 1; i <= n; i++) {
    			for (int j = 1; j <= m; j++) {
    				if (a[i] - b[j] >= 0) {
    					if ((ans ^ (sg[a[i]]) ^ (sg[a[i] - b[j]])) == 0) {
    						cout << "YES" << endl;
    						cout << i << ' ' << b[j] << endl; return 0;
    					}
    				}
    			}
    		}
    	}
        return 0;
    }
    
     
    EPFL - Fighting
  • 相关阅读:
    Number原生类型的扩展
    复杂参数的基本使用方式
    使用Number原生类型
    Function原生类型
    面向对象类型系统
    Error原生类型的扩展
    Date原生类型的扩展
    flex学习网站大全(转)
    如何调试javaScript
    使用JavaScriptConverter实现返回DataTable对象
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10073430.html
Copyright © 2011-2022 走看看