Codeforces 347C Alice and Bob

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 ≤ 109) — 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).

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.

题意:现给出一个集合,Alice和Bob每人依次拿出两个数,要求这两个数差的绝对值不在这个集合中,并把两个数差的绝对值加入这个集合,谁先取不到这两个数谁就输了,Alice先拿。

思路:根据题意容易看出最后的序列一定是个等差数列,而且数列的第一个数一定为这个数列的公差(我们知道公差一定会在这个数列当中,如果存在比公差小的数那么公差减去这个数一定比公差小,与公差的定义矛盾),最大的数一定是原始集合中最大的一个,只要我们求出最小与最大数之间等差数列的项数减去原始集合中项的个数就是要添加的个数,判断这个数的奇偶性就能知道到底是谁赢了。所以我们要求出这个公差,我们知道原集合中所有的数都是这个公差的倍数,所以这个公差就是原集合的最大公因子。(之所以是最大公因子是因为其他公因子会引进无关项)

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
ll a[10010],b[10010];
ll gcd(ll a,ll b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int main()
{
    int n,m=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
       if(a[i]>m)
        m=a[i];
    }
    sort(a,a+n);
    ll t=a[0];
    for(int i=1;i<n;i++)
    {
        t=gcd(t,a[i]);
    }
    ll h;
    h=m/t-n;
   if(h%2)
    printf("Alice\n");
   else
    printf("Bob\n");
}

额(⊙﹏⊙),一开始想成了所有差的GCD

发布了261 篇原创文章 · 获赞 14 · 访问量 7415

猜你喜欢

转载自blog.csdn.net/weixin_43244265/article/details/104091837
今日推荐