zoukankan      html  css  js  c++  java
  • AtCoder Grand Contest 018 A

    A - Getting Difference


    Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB

    配点 : 300

    問題文

    箱に N 個のボールが入っていて、i 番目のボールには整数 Ai が書かれています。 すぬけ君は、次の操作を好きな回数だけ行うことができます。

    • 箱から二つのボールを取り出し、その二つのボールに書かれている数の差の絶対値を書いた新しいボールと一緒に箱に戻す。

    すぬけ君が、整数 K の書かれたボールが箱の中に入っている状態にできるかどうか判定してください。

    制約

    • 1≤N≤105
    • 1≤Ai≤109
    • 1≤K≤109
    • 入力はすべて整数である。

    入力

    入力は以下の形式で標準入力から与えられる。

    N K
    A1 A2  AN
    

    出力

    すぬけ君が、整数 K がかかれたボールが箱の中に入っている状態にできる場合には POSSIBLE、 できない場合には IMPOSSIBLE と出力せよ。


    入力例 1

    Copy
    3 7
    9 3 4
    

    出力例 1

    Copy
    POSSIBLE
    

    まず、9 と書かれたボールと 4 と書かれたボールを取り出し、abs(9−4)=5 なので、5 と書かれた新しいボールと一緒に箱に戻します。 次に、3 と書かれたボールと 5 と書かれたボールを取り出し、abs(3−5)=2 なので、2 と書かれた新しいボールと一緒に箱に戻します。 最後に、9 と書かれたボールと 2 と書かれたボールを取り出し、abs(9−2)=7 なので、7 と書かれた新しいボールと一緒に箱に戻します。 7 と書かれたボールを箱に入れることができたので、この例の答えは POSSIBLE になります。


    入力例 2

    Copy
    3 5
    6 9 3
    

    出力例 2

    Copy
    IMPOSSIBLE
    

    どれだけ操作を行っても、5 の書かれたボールを箱の中に入れることはできません。 よってこの例の答えは、IMPOSSIBLE になります。


    入力例 3

    Copy
    4 11
    11 3 7 15
    

    出力例 3

    Copy
    POSSIBLE
    

    操作を行うまでもなく、箱の中には 11 の書かれたボールが入っています。 よってこの例の答えは、POSSIBLE になります。


    入力例 4

    Copy
    5 12
    10 2 8 6 4
    

    出力例 4

    Copy
    IMPOSSIBLE
    

    Score : 300 points

    Problem Statement

    There is a box containing N balls. The i-th ball has the integer Ai written on it. Snuke can perform the following operation any number of times:

    • Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.

    Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

    Constraints

    • 1≤N≤105
    • 1≤Ai≤109
    • 1≤K≤109
    • All input values are integers.

    Input

    Input is given from Standard Input in the following format:

    N K
    A1 A2  AN
    

    Output

    If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.


    Sample Input 1

    Copy
    3 7
    9 3 4
    

    Sample Output 1

    Copy
    POSSIBLE
    

    First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9−4)=5. Next, take out 3 and 5, and return them back along with abs(3−5)=2. Finally, take out 9 and 2, and return them back along with abs(9−2)=7. Now we have 7 in the box, and the answer is therefore POSSIBLE.


    Sample Input 2

    Copy
    3 5
    6 9 3
    

    Sample Output 2

    Copy
    IMPOSSIBLE
    

    No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.


    Sample Input 3

    Copy
    4 11
    11 3 7 15
    

    Sample Output 3

    Copy
    POSSIBLE
    

    The box already contains 11 before we do anything. The answer is therefore POSSIBLE.


    Sample Input 4

    Copy
    5 12
    10 2 8 6 4
    

    Sample Output 4

    Copy
    IMPOSSIBLE
    

     不知天高地厚的去尝试了一波Atcoder 果不其然 只写出来一道题 还是大爷提醒一波才会的 所以我们就来讲讲这第一题吧

     一句话题意就是 给你n个数以及一个k 我们可以从n个数中拿出两个数 相减得到一个新的数(也就是多了这一个新的数,新的数也可以参与操作) 问能否得到k

     分析一波易得 我们只考虑两个数 那个根据更相减损术 我们可以利用相减得到一波gcd(也就是最大公约数)

    那么两个数 x y 都是gcd的倍数 他们无论怎么相减都会是gcd的倍数

    把这个结论推广到n'个数 那么答案就是n个数的gcd啦 2333

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define LL long long
    using namespace std;
    const int M=1e6+7;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f; 
    }
    int n,k,num[M],ans,mx;
    bool f;
    int gcd(int x,int y){
        while(y){
            int p=x%y;
            x=y;
            y=p;
        }
        return x;
    }
    int main()
    {
        n=read(); k=read();
        for(int i=1;i<=n;i++){
            num[i]=read();
            mx=max(mx,num[i]);
            if(num[i]==k) f=1;
        }
        if(f){printf("POSSIBLE
    "); return 0;}
        if(mx<k){printf("IMPOSSIBLE
    "); return 0;}
        ans=gcd(num[1],num[2]);
        for(int i=3;i<=n;i++) ans=gcd(ans,num[i]);
        if(k%ans==0) printf("POSSIBLE
    ");
        else printf("IMPOSSIBLE
    ");
        return 0;
    }
    View Code
  • 相关阅读:
    跨平台这件事儿
    BTrace : Java 线上问题排查神器
    Spring 实现自定义 bean 的扩展
    运用计划缓冲的建议
    查询计划Hash和查询Hash
    执行计划的重用
    执行计划组件、组件、老化
    执行计划的生成
    SQL Server索引 (原理、存储)聚集索引、非聚集索引、堆 <第一篇>
    SQL Server执行计划的理解
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7226403.html
Copyright © 2011-2022 走看看