zoukankan      html  css  js  c++  java
  • 【Atcoder Grand Contest 010】D

    D - Decrementing


    Time limit : 2sec / Memory limit : 256MB

    Score : 1000 points

    Problem Statement

    There are N integers written on a blackboard. The i-th integer is Ai, and the greatest common divisor of these integers is 1.

    Takahashi and Aoki will play a game using these integers. In this game, starting from Takahashi the two player alternately perform the following operation:

    • Select one integer on the blackboard that is not less than 2, and subtract 1 from the integer.
    • Then, divide all the integers on the black board by g, where g is the greatest common divisor of the integers written on the blackboard.

    The player who is left with only 1s on the blackboard and thus cannot perform the operation, loses the game. Assuming that both players play optimally, determine the winner of the game.

    Constraints

    • 1≦N≦105
    • 1≦Ai≦109
    • The greatest common divisor of the integers from A1 through AN is 1.

    Input

    The input is given from Standard Input in the following format:

    N
    A1 A2AN
    

    Output

    If Takahashi will win, print First. If Aoki will win, print Second.


    Sample Input 1

    Copy
    3
    3 6 7
    

    Sample Output 1

    Copy
    First
    

    Takahashi, the first player, can win as follows:

    • Takahashi subtracts 1 from 7. Then, the integers become: (1,2,2).
    • Aoki subtracts 1 from 2. Then, the integers become: (1,1,2).
    • Takahashi subtracts 1 from 2. Then, the integers become: (1,1,1).

    Sample Input 2

    Copy
    4
    1 2 4 8
    

    Sample Output 2

    Copy
    First
    

    Sample Input 3

    Copy
    5
    7 8 8 8 8
    

    Sample Output 3

    Second

    ==============

    题目大意:给定n个最大公约数为1的整数,两个人轮流进行操作,每次操作可以选一个大于1的数使其减1,然后所有的数再除以当前的最大公约数(如3 6 10对10操作后得到1 2 3),当其中一个人无法操作时,输掉比赛,求获胜的是先手还是后手。先手输出First,后手输出Second。

    解:见Child-Single(good);

    #include<cstdio>
    #include<cstring>
    const int N=1e5+12;
    int map[N];int n;
    int gcd(int a,int b) 
    {
        if(!b) return a;
        return gcd(b,a%b); 
    }
    int dfs(int x)
    {
        int tot=0,flag=1;
        for(int i=1;i<=n;i++)     
            if(map[i]%2)
            {
                map[i]--;
                break;
            }
        int gc=map[1];
        for(int i=2;i<=n;i++) gc=gcd(gc,map[i]);
        for(int i=1;i<=n;i++)
        {
            map[i]/=gc;
            if(map[i]%2==0) tot++;
            if(map[i]==1) flag=0;
        }
        if(tot%2) return !x;
        if(!flag||n-tot>1) return x;
        dfs(x^1);
        
    }
    int main()
    {    
        scanf("%d",&n);
        int sum=0;
        int sum_flag=0,flag1=1;
        for(int i=1;i<=n;i++) 
        {
            scanf("%d",&map[i]);
            if(map[i]==1) flag1=0;
            if(map[i]%2==0) sum++;
            else sum_flag++;
    //        printf("std:: %d %d %d
    ",sum,sum_flag,flag1);
        }
        if(sum%2==1) printf("First
    ");
        else
        {
            if(!flag1||sum_flag>=2) printf("Second
    ");
            else if(dfs(1)) printf("First
    ");
            else printf("Second
    ");
        }
        return 0;
    }
    View Code

     ==========

     
  • 相关阅读:
    [leetcode] Bulls and Cows
    Win7 系统所有应用颜色调整
    一道题反映Java的类初始化过程
    翻转二叉树(深搜-先序遍历-交换Node)
    在一个数组中,除了两个数外,其余数都是两两成对出现,找出这两个数,要求时间复杂度O(n),空间复杂度O(1)
    一道随机函数题:由rand5()生成rand7()
    求一条直线通过的最大点数
    菜根谭#236
    菜根谭#235
    菜根谭#234
  • 原文地址:https://www.cnblogs.com/12fs/p/7723393.html
Copyright © 2011-2022 走看看