CodeForces - 850C Arpa and a game with Mojtaba (博弈,sg函数)

题目vj链接

题面:
在这里插入图片描述
题意:
给定一个长度为 n n n 的序列 a a a
每次可以选择一个质数 p p p 和一个正整数 k k k,其中要保证 p k p^k pk 要至少是当前序列 a a a 中某一个数的因子。然后把序列 a a a 中所有 p k p^k pk 的倍数 a i ai ai 替换为 a i p k \frac{ai}{p^k} pkai

两个人轮流操作,都采取最优的策略,谁不能进行操作就输了。

题解:
容易发现,对于序列 a a a 中的所有的数唯一分解之后出现的质数 p p p,不同的 p p p 相互独立。
a n s s g = s g ( p 1 )   x o r   s g ( p 2 )   x o r . . . . x o r   s g ( p k ) anssg=sg(p_1) \space xor \space sg(p_2)\space xor....xor\space sg(p_k) anssg=sg(p1) xor sg(p2) xor....xor sg(pk)

我们只需要处理出对于每一个 p i p_i pi 的 sg 函数然后异或起来即可。

求解 sg 函数时与具体的 p i p_i pi 无关,只与每个数中所包含 p i p_i pi的个数有关。

因为 a i ≤ 1 e 9 a_i\le1e9 ai1e9,唯一分解之后的某个数质因子的指数不会超过32,而且如果有两个数都含有 x x x 个质因子 p p p,那么与一个数含有 x x x 个质因子 p p p 另一个数不含有质因子 p p p 是一致的。所以我们可以状压记录当前状态。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<set>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x)  (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
#define fhead(x) for(int i=head[(x)];i;i=nt[i])
//#define max(x,y) ((x)>(y)?(x):(y))
//#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const int mod=1e9+7;
const double eps=1e-8;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=100100;
const int maxm=100100;
const int maxp=100100;
const int up=100100;

int prime[maxn],cnt=0;
bool ha[maxn];
void Prime(void)
{
    ha[1]=true;
    for(int i=2;i<maxn;i++)
    {
        if(!ha[i])
            prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<maxn;j++)
        {
            ha[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}

int a[200];
int n;
map<int,int>mp;
void only(int x)
{
    for(int i=1;i<=cnt&&prime[i]*prime[i]<=x;i++)
    {
        if(x%prime[i]) continue;
        int pm=0;
        while(x%prime[i]==0)
        {
            pm++;
            x/=prime[i];
        }
        mp[prime[i]]|=(1<<pm);
    }
    if(x>1) mp[x]|=(1<<1);
}

map<int,int>mmp;

int get_sg(int x)
{

    if(x==0||x==1) return 0;//x==1-->p^0
    if(mmp.count(x)) return mmp[x];

    bool ha[55];
    memset(ha,0,sizeof(ha));

    for(int i=1;i<=32;i++)
    {
        if((x>>i)==0) break;
        ha[get_sg((x>>i)|(x&((1<<i)-1)))]=true;

    }
    for(int i=0;;i++)
    {
        if(!ha[i])
            return mmp[x]=i;
    }
}

int main(void)
{
    Prime();
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        only(a[i]);
    }

    int ans=0;
    for(auto it=mp.begin();it!=mp.end();it++)
        ans^=get_sg(it->second);

    if(ans) printf("Mojtaba\n");
    else printf("Arpa\n");

    return 0;
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值