AcWing 891. Nim游戏&&892.台阶-Nim游戏&&893. 集合-Nim游戏

在这里插入图片描述

https://www.acwing.com/video/312/

博弈论是数论里面的,我感觉博弈论好像是要记住每一个情况的模板。视频如上,看了能加深模板的理解。
代码如下

#include<iostream>
using namespace std;
int main(void)
{
    
    
    int res=0;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        int t;
        cin>>t;
        res^=t;
    }
    if(res) cout<<"Yes";
    else
    cout<<"No";
}

老师的代码

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;


int main()
{
    
    
    int n;
    scanf("%d", &n);

    int res = 0;
    while (n -- )
    {
    
    
        int x;
        scanf("%d", &x);
        res ^= x;
    }

    if (res) puts("Yes");
    else puts("No");

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/53517/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

在这里插入图片描述

#include<iostream>
using namespace std;
int main(void)
{
    
    
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        int a,b;
        cin>>a>>b;
        if(a%(b+1)==0)
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
}

老师代码

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int main()
{
    
    
    int n;
    scanf("%d", &n);

    int res = 0;
    for (int i = 1; i <= n; i ++ )
    {
    
    
        int x;
        scanf("%d", &x);
        if (i & 1) res ^= x;
    }

    if (res) puts("Yes");
    else puts("No");

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/53528/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  1. 集合-Nim游戏
    在这里插入图片描述
    在这里插入图片描述
#include<iostream>
#include<algorithm>
#include<cstring>
#include<unordered_set>
using namespace std;
const int N=110,M=10010;
int n,m;
int s[N],g[M],t;
int res=0;
int sg(int x)
{
    
    
    if(g[x]!=-1)
    return g[x];
    unordered_set<int> h; 
    for(int i=1;i<=n;i++)
    if(x>=s[i]) h.insert(sg(x-s[i]));
    for(int i=0;;i++)
    if(!h.count(i))
    return g[x]=i;
}
int main(void)
{
    
    
    memset(g,-1,sizeof(g));
    cin>>n;
    for(int i=1;i<=n;i++) cin>>s[i];
    cin>>m;
    for(int i=1;i<=m;i++)
    {
    
    
        cin>>t;
        res^=sg(t);
    }
    if(res) cout<<"Yes";
    else 
    cout<<"No";
}
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_set>

using namespace std;

const int N = 110, M = 10010;

int n, m;
int s[N], f[M];


int sg(int x)
{
    
    
    if (f[x] != -1) return f[x];

    unordered_set<int> S;
    for (int i = 0; i < m; i ++ )
    {
    
    
        int sum = s[i];
        if (x >= sum) S.insert(sg(x - sum));
    }

    for (int i = 0; ; i ++ )
        if (!S.count(i))
            return f[x] = i;
}


int main()
{
    
    
    cin >> m;
    for (int i = 0; i < m; i ++ ) cin >> s[i];
    cin >> n;

    memset(f, -1, sizeof f);

    int res = 0;
    for (int i = 0; i < n; i ++ )
    {
    
    
        int x;
        cin >> x;
        res ^= sg(x);
    }

    if (res) puts("Yes");
    else puts("No");

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/53562/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/qq_52358098/article/details/113728402
今日推荐