Alice and Bob
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Problem Description
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
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 ≤ 10^9) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Sample Input
2
2 3
2
5 3
3
5 6 7
Sample Output
Alice
Alice
Bob
Hint
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.
http://codeforces.com/problem...
Accepted Code
// Author : Weihao Long
// Created : 2017/12/16
#include "stdio.h"
int gcd(int m, int n) {
int ans, r = m % n;
if (r == 0)
ans = n;
else
ans = gcd(n, r);
return ans;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int a[100];
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int j = 1; j < n; j++)
for (int k = 0; k < n - j; k++)
if (a[k] > a[k + 1]) {
int tmp = a[k];
a[k] = a[k + 1];
a[k + 1] = tmp;
}
int max = a[n - 1];
int head = a[0];
for (int u = 1; u < n; u++)
head = gcd(a[u], head);
int x = max / head - n;
puts(x % 2 ? "Alice" : "Bob");
}
return 0;
}
Notes
题意:
两人在现有数字中分别挑一个数,计算 |x - y| ,若该数还不存在,则添加上去,如果无论怎样计算 |x - y| ,都在现有数字中能找到,游戏就结束了。如果新增奇数个数字,则 Alice 赢,否则 Bob 赢。
思路:
1.先找规律,就拿短的试试“1,3,5,7”、“2,4,6,8”、“3,6,9,12”、“3,4,5,6”。
2.试了几组数据,答案都是首项等于公差的等差数列 { d, 2d, 3d, 4d ... } 。
3.猜测最终状态都形如此。
4.如果知道了最终数列的首项(公差),又知道数列中最大的数,这个数列最终有多少个元素就可以算出来了。
5.问题转化为求首项,也即求“最终数列里所有数的最大公约数”。
算法:
第一步:将初始的数字按升序排列。
第二步:初始状态,数列的首项就暂时设为最小的 a0 。
第三步:遍历初始数列,求当前的首项与 ai 的最大公约数,并更新首项。(这步最关键,目的是求“最终数列里所有数的最大公约数”)
第四步:计算最终数列元素个数与初始时的差值,即为后来增添的数字个数。
感受:
第一次提交是错的,当时我以为最终状态就是一个从 1 到 max 的序列(由题目的样例数据得来的)。后来多试了几组数据,发现“2,4,6,8”就不是,然后才发现是最终状态都是等差数列,并且首项等于公差。按这个想法试了一下就过了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。