C. Battle 2023 (ICPC) Jiangxi Provincial Contest -- Official Contest

Problem - C - Codeforces

题目大意:有n堆石子,给出一个数p,A先B后,每个人每次只能取p的幂个石子(包括1)问A能不能赢

1<=n<=3e5;1<=p<=1e18

思路:先递归算出sg函数看看,sg(0)=0,sg(x)能转移的位置也就是sg(x-p^{i})

打出表发现如果p是奇数,那么当x为奇数时sg(x)=1,x为偶数时sg(x)=0,如果p是偶数,sg(x)会出现长度为p+1的循环节,x对p+1取模后,如果新的x是奇数,sg(x)=1,如果x=p,sg(x)=2,否则sg(x)=0

因为每堆石子都是相互独立的游戏,根据打表发现的规律求出每一堆石子的sg值然后求异或和即可

//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e18;
map<ll,ll>sg;
ll po[100];
const int N=3e5+5;
int it=1;
ll dfs(ll x)
{//递归计算sg值
	if (sg.find(x)!=sg.end())
	{//记忆化搜索
		return sg[x];
	}
	set<ll>s;
	for (int i = 1; i <= it-1; i++)
	{//能转移到的地方
		ll j = x-po[i];
		if (j < 0)
			continue;
        ll temp=dfs(j);
        sg[j]=temp;
		s.insert(sg[j]);
	}
	int now = 0;
	while (s.count(now))
	{//找MEX
		now++;
	}
	return now;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	sg[0] = 0;
    sg[1]=1;
    ll n,p;
    ll ans=0;
    cin>>n>>p;
    ll now=1;
    //while(now<=INF)
    //{//预处理p的幂
        //po[it++]=now;
        //now*=p;
    //}
    // for(int i=1;i<=1000;i++)
    // {//打表找规律
    //     ll temp=dfs(i);
    //     sg[i]=temp;
    //     cout<<sg[i]<<endl;
    // }
    if(p&1)
    {
        for(int i=1;i<=n;i++)
        {
            ll x;
            cin>>x;
            ll temp=x%2;
            if(temp&1)
            {
                ans^=1;
            }
            //cout<<ans<<endl;
        }
    }
    else
    {
        for(int i=1;i<=n;i++)
        {
            ll x;
            cin>>x;
            ll temp=x%(p+1);
            if(temp&1)
            {
                ans^=1;
            }
            else if(temp==p)
            {
                ans^=2;
            }
            //cout<<ans<<endl;
        }
        
    }
    cout<<(!ans?"BAD":"GOOD")<<endl;
//    for(int i=1;i<=n;i++)
//         {
//             ll x;
//             cin>>x;
//             ll temp=dfs(x);
//             sg[x]=temp;
//             ans^=sg[x];
//             //cout<<ans<<endl;
//         }
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timidcatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值