zoukankan      html  css  js  c++  java
  • CF-346A Alice and Bob (gcd)

    题意:给出长为N的序列,然后Alice与Bob每次要轮流从中取两个数,如果|x-y|的差值已经存在与序列中
    以为着此次的操作无效,反之则有效。并且把得到的差值加入到序列中
    思路:要使得最后一个人操作无效即,该序列已经成为一个1到N的等差数列
    所以我们就要计算最后成为这种序列的次数。
    而要计算这个序列的次数,就必须知道这个序列的等差为多少
    要知道这个序列的等差为多少,最后即要计算其最大公约数
    两个数相减必定是公约数的倍数,我们知道最小的数一定是这n个数的gcd
    接下来计算奇偶次数,我们知道了等差所以就可以得到最终序列的个数
    减去初始的个数再模2就可以判断最终的值

    完整代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int maxn = 1e6;
    int arr[maxn];
    int gcd(int a,int b){
        return b==0? a:gcd(b,a%b);
    }
    int main(){
        int n;
        while(cin>>n){
            for(int i =0;i < n;i++){
                cin>>arr[i];
            }
            sort(arr,arr+n);
            int g = arr[0];
            for(int i = 1;i<n;i++){
                g = gcd(arr[i],g);
            }
            int ans = arr[n-1]/g - n;
            if(ans&1) puts("Alice");
            else puts("Bob");
        }
    }
  • 相关阅读:
    枚举和字符串之间的转换 [转帖]
    escape,encodeURI,encodeURIComponent函数比较[转帖]
    .net中的Provider模式 [转帖]
    ogg转到mp3
    四季养生(樊正伦教授)
    JavaScript高阶之路
    Python初识
    理解error和exception之间的区别(转)
    一些有用的话
    《爱在雨季》片尾曲
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11319948.html
Copyright © 2011-2022 走看看