8.12 第八场 GCD Game
8.12 第八场 GCD Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3640 Accepted Submission(s): 730
Problem Description
Alice and Bob are playing a game.
They take turns to operate. There are n numbers, a1 , a2 , … , an. Every time, the player plays in 3 steps.
-
Arbitrarily chooses one number ai.
-
Arbitrarily chooses another number x(1≤x<ai).
-
Replace the number ai with gcd(ai,x). Here, gcd(u,v) refers to the Greatest Common Divisor of u and v.
When a player can not make a single move he/she loses the game. Alice moves the first and she asks you to tell her who will win the game if both player play optimally.
Input
The first line contains a number T(1≤T≤100), the number of testcases.
For each testcase, there are two lines.
The first line contains one number n(1≤n≤106).
The second line contains n numbers a1 , a2 , … , an(1≤ai≤107).
It is guaranteed that for all testcases, ∑n≤106.
Output
For each testcase, output the answer in one line. If Alice will win the game, print “Alice”, otherwise print “Bob”.
Sample Input
2
1
1
1
2
Sample Output
Bob
Alice
大概题意:
两个人轮流进行游戏,可以将集合中的数变为与另一个数的最大公约数,先无法行动的人输。
思路:
用线性筛法求约数个数,然后进行 Nim 博弈即可
代码:
//
// Created by Black on 2021/8/12.
//
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e7 + 5;
int primes[N], cnt; // primes[]存储所有素数
bool st[N]; // st[x]存储x是否被筛掉
int d[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
d[1] = 0;
for (int i = 2; i <= n; i++) {
if (!st[i]) d[i] = 1;
for (int j = 0; primes[j] <= n / i; j++) {
d[primes[j] * i] = d[i] + 1;
if (i % primes[j] == 0) break;
}
}
}
//
//int play(int n) {
// int res = 0;
// for (int i = 2; i <= n / i; ++i) {
// if (n % i == 0) {
// while (n % i == 0) {
// n /= i;
// res++;
// }
// }
// }
// if (n > 1)res++;
// return res;
//}
//int getY(int x) {
// if (x == 1)return 0;
// int res = 1, i;
// for (i = 2; i * i <= x; i++) {
// int r = 0;
// while (x % i == 0) {
// r++;
// x /= i;
// }
// if (r > 0) {
// r++;
// res *= r;
// }
// }
// if (x > 1)
// res *= 2;
// return res == 0 ? 1 : res - 1;
//}
int main() {
get_primes(N);
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int res = 0;
while (n--) {
int x;
scanf("%d", &x);
// int h = play(x);
int h = d[x];
// if (x == 1)h = 0;
// else h = d[x] == 0 ? 1 : d[x] - 1;
// cout << h << endl;
res ^= h;
}
if (res) printf("Alice\n");
else printf("Bob\n");
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效