转载请注明出处,谢谢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. 数据库中简单的增删改查(CRUD)

    一切都是基于数据,而对数据的管理都离不开数据库.最近学到数据库的简单操作,所以写下这篇文章,总结一下学习到的知识.浅陋之处,多多见谅. 补充一下:一直弄不清SQL Server,Mysql ,以及Or ...

  2. Spring (二) OOP V.S AOP

    介绍 这是两种不同的编程思想就好比初中数学中学习的横纵坐标,一种是横向的一种是纵向,OOP是代表X轴而AOP代表Y轴,如下图: 数学几乎可以解释生活中所有的现象,无论是物体运动还是静止,也可以通过数学 ...

  3. bower安装使用以及git安装

    bower需要:node 和 git node安装包下载:http://blog.csdn.net/myan/article/details/2028545 Git安装: 选择第二项:Use Git ...

  4. EF搭建可扩展菜单

    EF实现可扩展性菜单 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...

  5. Python3.5入门学习记录-函数

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...

  6. Niagara AX连接MySQL数据库

    步骤如下 1.安装Niagara AX (3.8.38): 2.安装MySQL(5.6.25):将MySQL\Connector.J 5.1\mysql-connector-java-5.1.35-b ...

  7. hadoop官网介绍及如何下载hadoop(2.4)各个版本与查看hadoop API介绍

    1.如何访问hadoop官网?2.如何下载hadoop各个版本?3.如何查看hadoop API? 很多同学开发都没有二手资料,原因很简单觉得不会英语,但是其实作为软件行业,多多少少大家会英语的,但是 ...

  8. Retrieving the COM class factory for component with CLSID XX failed due to the following error: 80070005 拒绝访问。

    环境及异常信息说明 环境说明: Win2008 R2 企业版 x64 .IIS 7.0 功能说明:服务端操作Excel,(上传Excel到服务器,并在服务器端读取Excel中的数据) 异常信息:Ret ...

  9. hibernate update部分更新

    hibernate update Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to ...

  10. php5.5新特性之yield理解

    今天,在阅读别人代码时,其中出现了一个陌生的关键字yield,想一探究竟,于是找到:http://php.net/manual/zh/language.generators.overview.php ...