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();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章