当前位置:   article > 正文

A. Nezzar and Board - 裴蜀定理 -数论 -gcd_gcd=__gcd(gcd,abs(a[i]-a[i-1]));

gcd=__gcd(gcd,abs(a[i]-a[i-1]));

Problem - A - Codeforces

A. Nezzar and Board

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

nn distinct integers x1,x2,…,xnx1,x2,…,xn are written on the board. Nezzar can perform the following operation multiple times.

  • Select two integers x,yx,y (not necessarily distinct) on the board, and write down 2x−y2x−y. Note that you don't remove selected numbers.

Now, Nezzar wonders if it is possible to have his favorite number kk on the board after applying above operation multiple times.

Input

The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases.

The first line of each test case contains two integers n,kn,k (2≤n≤2⋅1052≤n≤2⋅105, −1018≤k≤1018−1018≤k≤1018).

The second line of each test case contains nn distinct integers x1,x2,…,xnx1,x2,…,xn (−1018≤xi≤1018−1018≤xi≤1018).

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print "YES" on a single line if it is possible to have kk on the board. Otherwise, print "NO".

You can print each letter in any case (upper or lower).

Example

input

Copy

6
2 1
1 2
3 0
2 3 7
2 -1
31415926 27182818
2 1000000000000000000
1 1000000000000000000
2 -1000000000000000000
-1000000000000000000 123
6 80
-5 -20 13 -14 -2 -11

output

Copy

YES
YES
NO
YES
YES
NO

Note

In the first test case, the number 11 is already on the board.

In the second test case, Nezzar could perform the following operations to write down k=0k=0 on the board:

  • Select x=3x=3 and y=2y=2 and write down 44 on the board.
  • Select x=4x=4 and y=7y=7 and write down 11 on the board.
  • Select x=1x=1 and y=2y=2 and write down 00 on the board.

In the third test case, it is impossible to have the number k=−1k=−1 on the board.

=========================================================================

网上不乏各种caodan题解,讲的稀里糊涂,其实就是没做出来看别的题解一知半解之后瞎糊弄。

这里先借助 2x-y  这一条件 先手玩一下样例 

a1 a2 a3 a4 a5

得a6= 2a1-a2 = a1+ (a1-a2)

得a7= 2a6-a3 = 2(2a1-a2)-a3 = 4a1-2a2-a3 = a1 + 2(a1-a2) - (a1-a3)

得a8 = 2a5- a7 = 2a5-a1-2(a1-a2)+(a1-a3)  =a5 +(a5-a1) -2(a1-a2) +(a1-a3)

.....

手玩几次不难发现,我们最终新产生的,就是原数组的ai  + 若干 (aj-ak),  jk为原数组的下标

对于ai 来说,我们有n中   aj-ak就有n^2种,那么最终答案一定是其中n个之一

 a1+  k1 (a1-a1)+k2(a1-a2)+..... +kn*n (an-an)    

 a2+ k1 (a1-a1)+k2(a1-a2)+..... +kn*n (an-an)   

....

an+k1 (a1-a1)+k2(a1-a2)+..... +kn*n (an-an)    

我们先不考虑化简什么的,至少目前获得了一个巨暴力的方法,那就是对K-ai ,看能不能被 k1 (a1-a1)+k2(a1-a2)+..... +kn*n (an-an)   表示, 这时用到裴蜀定理,也即是K-ai 是,这n^2个数字gcd的倍数即可。

接下来才是本题关键,如何化简?

将数组进行排序

 我们只需要 a2-a1  a3-a2  a4-a3  a6-a4 a7-a6就能表示出来全部六个数字两两相减的结果!

那么就好办咯,既然n^2的结果等价于n-1个的结果,那就排个序,再跑一个暴力就是了

不需要任何复杂的推理,至于网上那种纯数学推理,一个是非常不严谨,二个是几乎很难考证正确性。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4. ll a[200000+10];
  5. int main()
  6. {
  7. int t;
  8. cin>>t;
  9. while(t--)
  10. {
  11. ll n,k;
  12. cin>>n>>k;
  13. for(int i=1;i<=n;i++)
  14. {
  15. cin>>a[i];
  16. }
  17. sort(a+1,a+1+n);
  18. ll gcd=0;
  19. for(int i=2;i<=n;i++)
  20. {
  21. gcd=__gcd(gcd,a[i]-a[i-1]);
  22. }
  23. int ans=0;
  24. for(int i=1;i<=n;i++)
  25. {
  26. if((k-a[i])%gcd==0)
  27. {
  28. ans=1;
  29. }
  30. }
  31. if(ans)
  32. {
  33. cout<<"YES"<<endl;
  34. }
  35. else
  36. {
  37. cout<<"No"<<endl;
  38. }
  39. }
  40. return 0;
  41. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/75416
推荐阅读
相关标签
  

闽ICP备14008679号