『算法-ACM竞赛-数学-数论』Alice and Bob (CodeForces - 346A )推导

『算法-ACM 竞赛-数学-数论』Alice and Bob (CodeForces - 346A )推导

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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).

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

```cpp
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 a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
````

Output

```cpp
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob

Note

1
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.

游戏结束的标志是无法取出两个数字,他们的差值不在序列中。也就是说,最终状态是一个首项等于公差的等差序列。

求出这个等差数列的项数-n,就是游戏进行的回合。所以我们要先求出首项。设首项为 d,接下来就是 d+d,d+2d….

后面几项都是首项的倍数,所以我们可以用 gcd(a1,a2,a3…an)求出。ans = an / gcd(a1,a2..an) - n;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
return b==0? a:gcd(b,a%b);
}
int a[101];
int main()
{
int n;
int maxn = 0;
scanf("%d", &n);
for(int i=0;i<n;i++)
{
scanf("%d", &a[i]);
maxn = max(maxn, a[i]);
}
int tmp = maxn;

for(int i=0; i<n; i++)
{
tmp = gcd(tmp,a[i]);
}
int x = maxn/tmp - n;
if(x%2==1)
printf("Alice\n");
else
printf("Bob\n");
return 0;
}

『算法-ACM竞赛-数学-数论』Alice and Bob (CodeForces - 346A )推导
https://chiamzhang.github.io/2024/06/29/『算法-ACM竞赛-数学-数论』Alice and Bob (CodeForces - 346A )推导/
Author
Chiam
Posted on
June 29, 2024
Licensed under