Codeforces Round #698 (Div. 2) D. Nezzar and Board 裴蜀定理

https://codeforces.ml/contest/1478/problem/D

目录

题意

黑板上有n个数,你可以任选两个数x,y,然后将2x-y写到黑板上,问k是否能被写到黑板上

分析

我们将2x-y拆开来看一下,x+x-y相当于x这个数加上他和y的差值,那么我们可以处理出所有的差值。但数据范围是1e5如果用n2 处理肯定超时,但我们发现如果我们求出了a1,a2和a2,a3的差值也就相当于求出了a3,a1的差值(a3-a2+a2-a1=a3-a1)因此我们只要处理出n-1个差值就可以了。然后根据裴蜀定理
在这里插入图片描述
那么我们求的改变值为k-a[i],同时只要这个改变值为最大公约数的整数倍,那么都满足条件,最后枚举一遍a[i]即可。

Code

#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const ll INF = 1e18;
const double eps = 1e-4;
ll d[N];

void solve() {
    
    
    int T; cin >> T;
    while (T--) {
    
    
        ll n, k;
        cin >> n >> k;
        for (int i = 1; i <= n; i++) cin >> d[i];
        ll gcd_ = 0;
        for (int i = 2; i <= n; i++) gcd_ = __gcd(d[i]-d[i-1], gcd_);
        int f = 0;
        for (int i = 1; i <= n; i++) {
    
    
            if ((k - d[i]) % gcd_ == 0) f = 1;
        }
        if (f) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}

signed main() {
    
    
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
}

猜你喜欢

转载自blog.csdn.net/kaka03200/article/details/113384470