codeforces Gym - 101147A The game of Osho(sg博弈)

题意:
给出g对数字,每对由bi和ni组成,一次操作可以选择一对,然后将ni减去bi的k次幂,k为任意非负整数,无法操作者失败,问先后手谁能赢。

题解:
打出sg表可以发现,对于b是奇数,sg值=n%2。
对于b是偶数,则存在一个长度为b+1的循环节,如果n%(b+1)==0,则sg=0,如果n%(b+1)==b,则sg=2,其余情况,都是1010交替出现,即sg=(n%(b+1))%2。

附上打表代码+ac代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int>pii;
const int MAXN=1e6+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double pi=acos(-1);
int sg[105][105];
bool vis[105];
void getsg(){
    
    

    memset(sg,0,sizeof sg);
    for(int i=1;i<=20;i++){
    
    //b
        for(int j=1;j<=20;j++){
    
    //n
            memset(vis,0,sizeof vis);
            for(int k=1;k<=j;k=k*i){
    
    
                vis[sg[i][j-k]]=1;
                if(i==1) break;
            }
            for(int k=0;;k++){
    
    
                if(!vis[k]){
    
    
                    sg[i][j]=k;
                    break;
                }
            }
        }
    }
    for(int i=1;i<=20;i++){
    
    
        for(int j=1;j<=20;j++){
    
    
            cout<<sg[i][j]<<" ";
        }
        cout<<endl;
    }
}
int main()
{
    
    
    freopen("powers.in","r",stdin);
    //getsg();
    int t;
    cin>>t;
    while(t--){
    
    
        int b,n,g;
        cin>>g;
        int ans=0;
        while(g--){
    
    
            cin>>b>>n;
            if(b&1){
    
    
                ans^=n%2;
            }
            else{
    
    
                if(n%(b+1)==b) ans^=2;
                else if(n%(b+1)!=0) ans^=(n%(b+1))%2;  
            }
        }
        if(ans) printf("%d\n",1);
        else printf("2\n");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45755679/article/details/113663838