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

     ==========

     
  • 相关阅读:
    5.win上安装ES
    6.入门案例:电商网站商品管理(一)
    BZOJ 1616 [Usaco2008 Mar]Cow Travelling游荡的奶牛:dp【网格型】
    BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成树)
    BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线:spfa + 二分【路径中最大边长最小】
    BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包
    BZOJ 1609 [Usaco2008 Feb]Eating Together麻烦的聚餐:LIS & LDS (nlogn)
    POJ 2976 Dropping tests:01分数规划【二分】
    BZOJ 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头:统计 + 筛法【调和级数】
    BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机:dp【找转移路径】
  • 原文地址:https://www.cnblogs.com/12fs/p/7723393.html
Copyright © 2011-2022 走看看