https://codeforces.ml/contest/1478/problem/D
目錄
- 題意
- 分析
- Code
題意
黑板上有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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/254421.html
標籤:其他
上一篇:qt怎么保存組態檔
