CF 338 D GCD Table(CRT)
轉載請註明出處,謝謝http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
給定一個序列,a[1 。。k],問是否存在(i , j)使得 GCD(i , j + r - 1) = a[r] (k>=r >=1),其中 i <= n && j + k - 1 <= m
http://codeforces.com/contest/338/problem/D
首先容易知道row = lcm (a[1……k]),是最小可能存在解的行。
官方題解中有證明,反正亂七八糟的。。。我太弱了,看不懂
之後我們找到最小的col滿足
col % a[1] = 0
(col + 1) % a[2] = 0
……
(col + k - 1) % a[k] = 0
這個東西用CRT求出來。
最後check一下row , col是否滿足,就結束了。。。
顯然j < col 是不可能滿足的,而且col + x * row肯定也是可能滿足的。
當然可能存在gcd (col + r - 1 , row) > a[r]。
所以col縮小是肯定不滿足的,而即使你增大col若干倍,只可能使得偏差更大。
接下來就注意一下各種細節,比如溢位等問題
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lson step << 1
#define rson step << 1 | 1
using namespace std;
typedef long long LL;
const int N = 10005;
LL n , m , a[N] , row = 1LL;
int k;
LL gcd (LL a , LL b) {
return b == 0 ? a : gcd (b , a % b);
}
LL extend_gcd (LL a , LL b , LL &x , LL &y) {
if (b == 0) {
x = 1LL;
y = 0;
return a;
}
LL g = extend_gcd (b , a % b , x , y);
LL t = x;
x = y; y = t - a / b * x;
return g;
}
int main() {
int t;
#ifndef ONLINE_JUDGE
freopen ("input.txt" , "r" , stdin);
// freopen ("output.txt" , "w" , stdout);
#endif
cin >> n >> m >> k;
for (int i = 0 ; i < k ; i ++) {
cin >> a[i];
row = row / gcd (row , a[i]) * a[i];
if (row <= 0 || row > n) {
puts ("NO");
return 0;
}
}
// Z = u + v * x
LL u = 0LL , v = a[0];
for (int i = 1 ; i < k ; i ++) {
// Z = U + V * x1
// Z = - i + a[i] * x2
// v * x - a[i] * y = - u - i
// A * x + B * y = C
LL x , y , A = v , B = -a[i] , C = - u - i;
LL g = extend_gcd (A , B , x , y);
if (C % g) {
puts ("NO");
return 0;
}
if (B % g) puts ("ERROR");
LL t = B / g;
x = x * (C / g);
x = (x % t + t) % t;
if (x < 0) x -= t;
// y = (C - A * x) / B;
u = u + v * x;
v = v / gcd (v , a[i]) * a[i];
}
if (u == 0) u += row;
if (u + k - 1 > m) {
puts ("NO");
return 0;
}
for (int i = 0 ; i < k ; i ++) {
if (gcd (row , u + i) != a[i]) {
puts ("NO");
return 0;
}
}
puts ("YES");
return 0;
}
相關文章
- (桶暴力列舉gcd) CF1627D Not AddingGC
- CF939 D
- CF 1839 D
- CF 1913 D
- CF1948D
- CF242D題解
- CF292D 題解
- 最近做題遇到的坑 cf865d,cf838e,cf317d,cf1394b,poj3322,cf223c,hdu2435,cf47d
- 338
- Codeforces1493D GCD of an Array3DGC
- CF1016D
- CF963-D. Med-imize
- CF1925D Good TripGo
- CF391D2 SupercolliderIDE
- CF1614D2
- CF1981D題解
- CF924D Contact ATC
- CF1977D XORificator
- (構造) CF1758D Range = √Sum
- CF1906D題解
- CF1840D的題解
- CF1352D的題解
- CF445D. Serega and Fun分塊
- [CF1718D] Permutation for Burenka 瞎扯
- [題解]CF55D Beautiful Numbers
- CF547D Mike and Fish 題解
- CF 948 DIV.2 D. XORificator
- CF773D Perishable Roads3D
- CF2031D 題解
- Codeforces 448D Multiplication Table
- CF 274D Lovely Matrix(拓撲排序)排序
- CF708D Incorrect Flow 題解
- CF 1975 D Paint the Tree(*1700) 貪心AI
- (DP) CF1861D Sorting By Multiplication
- 「雜題亂刷」CF1987D
- CF1905D Cyclic MEX 題解
- CF908D-New Year and Arbitrary Arrangement
- CF1458D Flip and Reverse 題解