GCD Game 博弈论-Nim-质因数应用-质因数个数预处理

在这里插入图片描述

题意 :

  • 给n个数的序列,每次选择一个ai,定义x为1 <= x < ai,将ai用gcd(ai, x)替换,无法行动者输。

思路 :

  • gcd(ai, x)一定是ai的因数,ai的因数也是由ai的若干个质因数相乘得到,说明每一次替代的过程都是将ai的若干个(必须取大于等于1个,不能不取,可以拿完(变成1))质因数拿走,即Nim,把每个数看成石子堆,每个石子就是这个数的质因数,每次操作可以从任意一堆石子中拿走任意数量的石子(可以拿完,但不能不拿),最后无法操作的人看作失败。
  • 提前用线性筛预处理一下 1 0 7 10^7 107 以内的数的质因子个数。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const double pi = acos(-1);
typedef long long ll;

const int N = 1e7 + 10;

bool st[N];
int primes[N], num[N], cnt = 0;

int main()
{
    IOS;
    int T;
    cin >> T;
    
    for (int i = 2; i <= N; i ++ )
    {
        if (!st[i]) primes[cnt ++ ] = i, num[i] = 1;
        for (int j = 0; primes[j] <= N / i; j ++ )
        {
            st[i * primes[j]] = true;
            // 质因数个数
            num[i * primes[j]] = num[i] + 1;
            if (i % primes[j] == 0) break;
        }
    }
    
    while (T -- )
    {
        int n;
        cin >> n;
        int res = 0;
        for (int i = 0; i < n; i ++ )
        {
            int x;
            cin >> x;
            res ^= num[x];
        }
        if (res) cout << "Alice" << endl;
        else cout << "Bob" << endl;
    }
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值