codeforces1478 D. Nezzar and Board (Pei Shu theorem)

Question: Initially there are n numbers, each operation can select two numbers x, y, add 2x-y to the number sequence (x, y are not erased), and ask if the number k can be combined

Ideas:

2x-y = x + (x-y), that is, each operation is equivalent to a linear combination of x + any difference. It can be found that a3-a1 = (a3-a2) + (a2-a1), two non-adjacent The difference between the numbers can be expressed by the difference between the adjacent numbers, so only the difference between two adjacent numbers can be calculated.

Pei Shu theorem of n numbers:

 It can be seen that the number that these n-1 differences can be combined must be a multiple of their gcd, that is, (k-a[i]) must be a multiple of gcd, because any number in the original sequence can be determined by a[1 ] + The difference is expressed, so only a[1] can be judged.

Reference: https://blog.csdn.net/kaka03200/article/details/113384470

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 2e5 + 7;

ll a[N];

ll gcd(ll a, ll b) {
    return b ? gcd(b, a % b) : a;
}

int main() {
    int t, n;
    ll k;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%lld", &n, &k);
        ll gd;
        for(int i = 1; i <= n; ++i) {
            scanf("%lld", &a[i]);
            if(i == 1) continue;
            else if(i == 2) gd = a[i] - a[i - 1];
            else gd = gcd(gd, a[i] - a[i - 1]);
        }
        if((k - a[1]) % gd == 0) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43871207/article/details/113467205