当前位置 : 主页 > 编程语言 > c语言 >

Gym - 101341D

来源:互联网 收集:自由互联 发布时间:2023-09-07
A frog lives in a one-dimensional world in the point with the coordinate 0. He needs to get to the point with the coordinate x. For some reason he cannot make jumps of arbitrary length, and can jump only by a1, …, an in any direction. Is

A frog lives in a one-dimensional world in the point with the coordinate 0. He needs to get to the point with the coordinate x. For some reason he cannot make jumps of arbitrary length, and can jump only by a1, …, an in any direction. Is he able to reach x?

Input
The first line contains two integers n and x separated by a space (1 ≤ n ≤ 200000,  - 109 ≤ x ≤ 109) — the number of variants of jump length and the coordinate of the point to reach.

The second line contains n integers ai separated by spaces (1 ≤ ai ≤ 109) — the lengths of jumps the frog can make.

Output
Output «YES» (without quotes), if the frog can reach the point x, otherwise output «NO» (without quotes).

Examples
Input
3 17
3 5 4
Output
YES
Input
4 5
10 20 30 40
Output
NO
思路:
即求解方程组,且必须有整数解;
考虑两个的情况,即 ax+by=c;
此时c % gcd(a,b)才有解;
同理推广到多元即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)



ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

int a[maxn];

int main() {
    int n, k;
    cin >> n >> k;
    int i, j;
    if (n == 1) {
        int x;
        cin >> x;
        if (k%x != 0) {
            cout << "NO" << endl;
        }
        else cout << "YES" << endl;
    }
    else {
        cin >> a[0] >> a[1];
        int Gcd = gcd(a[0], a[1]);
        for (i = 2; i < n; i++) {
            cin >> a[i];
            Gcd = gcd(Gcd, a[i]);
        }
        if (k%Gcd != 0)cout << "NO" << endl;
        else cout << "YES" << endl;
    }
    return 0;
}
上一篇:ZOJ - 3961 模拟题 区间问题
下一篇:没有了
网友评论