codeforces 347C Alice and Bob (mathematics, thinking)

Question: In
a set, there are n numbers and they are not equal. Two people pick two different numbers x and y from the set in turn, so that |xy| is not in the set, and add |xy| to the set , Until someone can no longer operate.
Solution: This
question actually has nothing to do with strategy, it only has to do with how many numbers can be put. At first I thought that all numbers between 1 and the maximum value can be put into the set, so the answer is max-n, but Was, and later I found that the data of 2, 4, 6, 8 is not good. If you think about it carefully, you can find that if the elements in the set are a, 2a, 3a, 4a, 5a,..., ka, then we can’t Add any number, and there are only k numbers at most. Looking at it, the greatest common divisor of these numbers is actually a, then k=max/gcd, which is the maximum number that can be accommodated in the set, then kn is what we can still put If the number of numbers that go in is odd, then the first person wins, otherwise the second person wins.
Although it seems simple, the idea is really hard to think about.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAXN=1e3+5;
int a[MAXN];
int gcd(int a,int b)
{
    
    
    if (b == 0)
        return a;
    else if (a < b)
        return gcd(b, a);
    else
        return gcd(b, a % b);
}
int main()
{
    
    
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    int gg=gcd(a[1],a[2]);
    for(int i=3;i<=n;i++)
    {
    
    
        gg=gcd(gg,a[i]);
    }
    if((a[n]/gg-n)&1)
    {
    
    
        cout<<"Alice"<<endl;
    }
    else
    {
    
    
        cout<<"Bob"<<endl;
    }
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/108033642