小博弈论

太太太ci了我


巴什博弈

有n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取 m 个。最后取光者得胜

  • 如果 n%(m+1)=0 那么先手必胜
  • 否则必输

nim游戏

有 n 堆数,每堆有 s[i] 个,每次可以且仅可以取一堆中的若干个数,求问先手有没有必胜策略。

  • 将每堆的个数异或
  • 如果结果是0 那么先手必输
  • 否则必胜
  • 例:P2197 【模板】nim游戏
  • //P2197 【模板】nim游戏
    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        int t,n;
        cin>>t;
        while(t--){
            cin>>n;
            int ans=0;
            for(int i=1;i<=n;i++){
                int s;
                cin>>s;
                ans^=s;
            }
            if(!ans) puts("No");
            else puts("Yes");
        }
        return 0;
    }
    View Code

威佐夫博奕 

两堆各x,y个物品,两人轮流从其中一堆取至少一件物品,至多不限,或从两堆中同时取相同件物品,规定最后取完者胜利。

  • 若x < y,令a=y-x
  • gr= (√5-1)/2 (golden ratio)
  • 如果x=gr*a 那么先手必输
  • 否则必胜
  • //P2252 取石子游戏 
    //威佐夫博奕     
    #include<bits/stdc++.h>
    using namespace std;
    const double lorry=(sqrt(5.0)+1.0)/2.0;
    int main(){
        int a,b;
        cin>>a>>b;
        if(a>b) swap(a,b);
        int m=b-a;
        if(a==(int)(lorry*(double)m)) puts("0");
        else puts("1");
        return 0;
    }
    View Code

猜你喜欢

转载自www.cnblogs.com/duojiaming/p/11788932.html