zoukankan      html  css  js  c++  java
  • Light OJ 1199:Partitioning Game(SG函数模板)

    Alice and Bob are playing a strange game. The rules of the game are:

    1.      Initially there are n piles.

    2.      A pile is formed by some cells.

    3.      Alice starts the game and they alternate turns.

    4.      In each tern a player can pick any pile and divide it into two unequal piles.

    5.      If a player cannot do so, he/she loses the game.

    Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.

    Input

    Input starts with an integer T (≤ 1000), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number of cells in each pile is between 1 and 10000.

    Output

    For each case, print the case number and 'Alice' or 'Bob' depending on the winner of the game.

    Sample Input

    3

    1

    4

    3

    1 2 3

    1

    7

    Sample Output

    Case 1: Bob

    Case 2: Alice

    Case 3: Bob

    题意

    有n堆石子,每一堆分别有ai个石子
    一次操作可以使一堆石子变成两堆数目不相等的石子
    最后不能操作的算输,问先手胜还是后手胜

    思路

    n堆石子相互独立,所以可以应用SG定理,只需要算出一堆石子的SG函数。
    一堆石子(假设有x个)的后继状态可以枚举出来,分别是{1,x-1},{2,x-2},...,{(x-1)/2,x-(x-1)/2},
    一堆石子分成的两堆石子又相互独立,再次应用SG定理。

    所以SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),..., SG((x-1)/2)^SG(x-(x-1)/2) },

    最后的答案是SG(x1)^SG(x2)^...^SG(xn)

    AC代码

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <set>
    #include <string>
    #define ll long long
    #define ull unsigned long long
    #define ms(a) memset(a,0,sizeof(a))
    #define pi acos(-1.0)
    #define INF 0x7f7f7f7f
    #define lson o<<1
    #define rson o<<1|1
    const double E=exp(1);
    const int maxn=1e4+10;
    const int mod=1e9+7;
    using namespace std;
    int sg[maxn];
    int vis[maxn];
    int a[maxn];
    inline void getSG()
    {
    	ms(sg);
    	for(register int i=1;i<=maxn;i++)
    	{
    		ms(vis);
    		for(int j=1;2*j<i;j++)
    			vis[sg[i-j]^sg[j]]=1;
    		for(register int j=0;j<=maxn;j++)
    		{
    			if(!vis[j])
    			{
    				sg[i]=j;
    				break;
    			}
    		}
    	}
    }
    int main(int argc, char const *argv[])
    {
    	ios::sync_with_stdio(false);
    	register int t;
    	getSG();
    	register int n;
    	cin>>t;
    	register int _=0;
    	while(t--)
    	{
    		int sum=0;
    		ms(a);
    		cin>>n;
    		for(register int i=1;i<=n;i++)
    		{
    			cin>>a[i];
    			sum^=sg[a[i]];
    		}
    		cout<<"Case "<<++_<<": ";
    		if(sum)
    			cout<<"Alice"<<endl;
    		else
    			cout<<"Bob"<<endl;
    	}
    	return 0;
    }

    参考博客:https://www.cnblogs.com/Ritchie/p/5396893.html

  • 相关阅读:
    Jquery 学习一
    响应式设计
    微信开发一
    Ajax 技术二
    Ajax 技术一
    SVN版本控制软件
    正则表达式概述
    编写小游戏:贪吃蛇
    POJ 3356 AGTC(DP-最小编辑距离)
    算法模板の字符串处理
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324352.html
Copyright © 2011-2022 走看看