转载请注明出处,谢谢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;
}

CF 338 D GCD Table(CRT)的更多相关文章

  1. Codeforces 338 D. GCD Table

    http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...

  2. 【CF#338D】GCD Table

    [题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...

  3. Codeforces Round #323 (Div. 2) C.GCD Table

    C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...

  4. Codeforces Round #323 (Div. 1) A. GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #323 (Div. 2) C. GCD Table 暴力

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

  6. SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)

    4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...

  7. Codeforces Round #323 (Div. 2) C. GCD Table map

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  8. CF582A GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1&lt;=a&lt;=n,1&lt;=b&lt;=m))加强版

    SPOJ4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the result ...

随机推荐

  1. python标准库 platform模块

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #platform #作用:检查底层平台硬件,操作系统和解释器版本信 ...

  2. python学习之路-11 多线程、多进程、协程

    python内置队列模块 queue queue的四种队列 q = queue.Queue() # 先进先出队列 q = queue.LifoQueue() # 后进先出队列 q = queue.Pr ...

  3. Spring AOP应用实例demo

    AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...

  4. 调起qq临时通话

    1.在qq推广的网站上的推广工具中将个人或者公司的qq确认为推广qq 2. 选择好显示的图片,将代码拷贝到页面上就可以了

  5. MVC 错误处理1

    实例1. /// <summary> /// 错误处理 /// 404 处理 /// </summary> protected void Application_Error(o ...

  6. SQL Server中带事务的存储过程简单举例

    先来看一个概念: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完整地执行,要么完全地不执行.那么在存储过程里添加事务,则可以保证该事务里的所 ...

  7. iOS-OC-基础-NSPredicate常用方法

    NSpredicate 常用方法 // 谓词的条件查询 > .< .==.!= NSPredicate *predicate1 = [NSPredicate predicateWithFo ...

  8. FZU1327 优先队列

    Problem 1327 Blocks of Stones II Accept: 318    Submit: 881Time Limit: 1000 mSec    Memory Limit : 3 ...

  9. C++程序设计实践指导1.10二维数组元素换位改写要求实现

    改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数Double ...

  10. VIM编辑器操作指令

    VIM有三种操作模式: 1,命令模式--command mode 2,输入模式--insert mode 3,底行模式--last line mode [在命令模式的时候,按Shift +  :出现的 ...