题解:CodeForces 346A Alice and Bob[博弈/数论]
CodeForces 346A
A. Alice and Bob
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers \(x\) and \(y\) from the set, such that the set doesn't contain their absolute difference \(|x - y|\). Then this player adds integer \(|x - y|\) to the set (so, the size of the set increases by one).
暑假真是无聊,不是吗?所以爱丽丝和鲍勃发明了一个新游戏来玩。规则如下。首先,他们获得一组不同的整数 \(n\)。然后他们轮流进行以下操作。在每一步中,轮到的玩家(当前的玩家)可以从集合中选择两个不同的整数 \(x\) 和 \(y\) ,使得集合中不包含它们的绝对差 \(|x - y|\) 。然后这个玩家将整数 \(|x - y|\) 添加到集合中(因此,集合的大小增加了 \(1\))。
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
如果当前玩家没有有效的移动,他(或她)将输掉游戏。问题是,如果两个玩家都采取最佳策略,最终谁会赢得游戏。请记住,爱丽丝总是先走。
Input
The first line contains an integer \(n (2 ≤ n ≤ 100)\) — the initial number of elements in the set. The second line contains n distinct space-separated integers \(a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9)\) — the elements of the set.
第一行包含一个整数 \(n(2 ≤ n ≤ 100)\)— 集合中初始元素的数量。第二行包含 \(n\) 个不同的以空格分隔的整数 \(a_1, a_2, ..., a_n(1 ≤ a_i ≤ 10^9)\)— 集合的元素。
Output
Print a single line with the winner's name. If Alice wins print Alice
, otherwise print Bob
(without quotes).
打印一行,输出获胜者的名字。如果爱丽丝获胜,则打印Alice
,否则打印Bob
(不带引号)。
Examples
input
2
2 3
output
Alice
input
2
5 3
output
Alice
input
3
5 6 7
output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose \(2\) and \(3\), then to add \(1\) to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
我的题解
我的解法就是先找到里面所有数的最大公因数
然后用最大的数除以这个最大公因数
就能算出这个数组最多能有多少个数字
进而求出他们能放进来的数字的多少
进而推断出这个游戏的赢家
注意:
求最大公因数的时候
不要写
g = std::min(std::__gcd(a[i],a[j]),g);
一定要
g = std::__gcd(std::__gcd(a[i],a[j]),g);
前者是有问题的! 我已经WA了好多发了
我的代码
#include <bits/stdc++.h>
#define int long long
int t,n,num;
void solve()
{
std::cin >> n;
std::vector<int> a(n);
for(int i = 0 ; i < n ; i ++){
std::cin >> a[i];
}
std::sort(a.begin(),a.end());
int g = std::__gcd(a[0],a[1]);
for(int i = 0 ; i < n ; i ++)
{
for(int j = i ;j < n; j ++)
{
g = std::__gcd(std::__gcd(a[i],a[j]),g);
}
}
if((a[n-1]/g - n)%2 == 0) std::cout << "Bob";
else std::cout << "Alice";
}
signed main()
{
solve();
return 0;
}
有什么出现纰漏的地方还请大家在评论区指出!谢谢!
posted on 2024-07-14 14:37 Jiejiejiang 阅读(10) 评论(0) 编辑 收藏 举报