zoukankan      html  css  js  c++  java
  • 博弈结论记录

    一、巴什博奕:

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

    结论:

    见代码

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <cstring>
     5 #define FRE() freopen("in.txt","r",stdin)
     6 
     7 using namespace std;
     8 int n,m;
     9 
    10 int main()
    11 {
    12     while(~scanf("%d%d",&n,&m))
    13     {
    14         if(n % (m+1) == 0)
    15             printf("先手输
    ");
    16         else
    17             printf("先手赢
    ");
    18     }
    19     return 0;
    20 }

    二、 威佐夫博弈:

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

    结论:

    两堆物品的数量各是a、b,则令k = abs(b - a),t = min(a,b),计算temp = (sqrt(5.0)+1)/ 2.0,如果k * temp = t,则先手输,反之先手赢。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <cstring>
     5 #define FRE() freopen("in.txt","r",stdin)
     6 
     7 using namespace std;
     8 const int maxn = 1e4;
     9 int a,b;
    10 
    11 int main()
    12 {
    13     //FRE();
    14     double temp = (sqrt(5.0) + 1) / 2.0;
    15     while(~scanf("%d%d",&a,&b))
    16     {
    17         if(a > b) swap(a, b);
    18         int k = b - a;
    19         if(a == (int)(k*temp))//注意数据类型的转换
    20             printf("0
    ");
    21         else
    22             printf("1
    ");
    23     }
    24     return 0;
    25 }

    三、尼姆博奕:

    有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取部分或全部物品,最少取一件,取到最后一件物品的人获胜。

    结论:

    把所有堆得物品的个数连续求异或(^运算),如果结果为0,先手输,不为0时,先手赢。

    例题:HDU2176

    当序列的连续异或值为k(k != 0)时,假设其中的某个元素a,当a > a ^ k 时,将a改为a^k,可以使序列的异或值变为0。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <set>
     7 #define INF 0x3f3f3f3f
     8 
     9 using namespace std;
    10 typedef long long ll;
    11 const int maxn = 200000;
    12 int buf[maxn];
    13 
    14 
    15 int main()
    16 {
    17     int n;
    18     while(scanf("%d",&n) && n)
    19     {
    20         int temp = 0,mmin = INF;
    21         for(int i = 0; i < n; i++)
    22         {
    23             scanf("%d",&buf[i]);
    24             mmin = min(mmin, buf[i]);
    25             temp ^= buf[i];
    26         }
    27         if(temp == 0)
    28             printf("No
    ");
    29         else
    30         {
    31             printf("Yes
    ");
    32             for(int i = 0; i < n; i++)
    33             {
    34                 if( buf[i] > (buf[i]^temp) )
    35                     printf("%d %d
    ",buf[i],buf[i]^temp);
    36             }
    37         }
    38     }
    39     return 0;
    40 }

    四、斐波那契博弈:

    有一堆物品,两人轮流取物品,先手最少取一个,至多无上限,但不能把物品取完,之后每次取的物品数不能超过上一次取的物品数的二倍且至少为一件,取走最后一件物品的人获胜。

    结论

    先手胜利当且仅当这堆物品的数目不是斐波那契数列中的数字。(该类问题可以打表解决)

    例题HDU-2516

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <set>
     7 #define INF 0x3f3f3f3f
     8 
     9 using namespace std;
    10 typedef long long ll;
    11 const int maxn = 100000;
    12 
    13 ll buf[maxn];
    14 set<ll> s;
    15 void init()
    16 {
    17     buf[0] = buf[1] = 1;
    18     for(int i = 2; i < maxn; i++)
    19     {
    20         buf[i] = buf[i - 1] + buf[i - 2];
    21         s.insert(buf[i]);
    22     }
    23 }
    24 
    25 
    26 int main()
    27 {
    28     ll x;
    29     init();
    30     while(scanf("%lld",&x) && x)
    31     {
    32         if(s.count(x))
    33             printf("Second win
    ");
    34         else
    35             printf("First win
    ");
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    GlusterFS分布式文件系统部署
    bower解决js的依赖管理
    Linux如何查看当前占用CPU或内存最多的几个进程
    WIN7无法卸载掉中文繁体注音输入法
    kafka消费者报错INVALID_FETCH_SESSION_EPOCH
    Hbase报错:org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet
    Prometheus一条告警是怎么触发的
    prometheus client_golang使用
    prometheus告警插件-alertmanager
    sed替换变量
  • 原文地址:https://www.cnblogs.com/sykline/p/9737840.html
Copyright © 2011-2022 走看看