http://codeforces.com/problemset/problem/338/D

题意:

有一张n*m的表格,其中第i行第j列的数为gcd(i,j)

给出k个数

问在这张表格中是否 有某一行中连续的某一部分 就是 这k个数

题意转化:

是否存在 一对i,j

满足gcd(i,j)=a1,gcd(i,j+1)=a2,…… gcd(i,j+k-1)=ak

直观上感觉:

i要满足的必要条件是 i |  lcm(a1,a2……ak)

j要满足的必要条件是

j= a1*k1,j+1=a2*k2……,j+k-1=ak*k_k

相当于

j ≡ 0 mod a1

j ≡ -1 mod a2

……

j≡ -(k-1) mod ak

利用扩展中国剩余定理可以求出 满足条件的最小的j

我们令i=lcm(a1,a2……ak)

去检验 是否满足 gcd(i,j+m-1)= a_m  m∈[1,k]

若满足条件输出YES,否则输出NO

为什么用满足必要条件的最小的i和j检验?

证明 i= lcm(a1,a2……ak)是唯一满足要求的i:

若还存在一个 i*x  满足条件,那么

将 i , i*x,j 质因数分解,存在一个 p^k 能整除i*x、j,不能整除i

∵ i= lcm(a1,a2……ak)

∴i的质因数分解的任意一项 必须能整除 a中的某一个

而 p^k 不能整除a 中的任意一个,否则i的质因数分解包含 p^k

证明 满足 j ≡ -(h-1) mod a[h] 的最小的j一定满足要求

若存在一个j*x 满足条件,而j不满足条件

即 存在一个h,满足 gcd(i,j+h-1)< a[h],gcd(i,j*x+h-1)= a[h]

将 j,j*x ,a[h]质因数分解,j*x 中 存在一个p^k2,满足

j 中 为 p^k1,a[h] 中为 p^k2 , 且 k1<k2

∵  j ≡ -(h-1) mod a[h]

即 j= a[h]*s - h+1

∴ j+h-1 = a[h]*s,所以k1>=k2

所以 最小的j一定满足要求

#include<cstdio>
#include<iostream> using namespace std; typedef long long LL; #define N 10001 LL a[N]; LL n1,a1; LL lcm; template<typename T>
void read(T &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} LL gcd(LL a,LL b) { return !b ? a : gcd(b,a%b); } bool judge_lcm(int k,LL n)
{
lcm=;
for(int i=;i<=k;++i)
{
lcm=lcm/gcd(lcm,a[i])*a[i];
if(lcm>n) return false;
}
return true;
} void exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b) { x=; y=; return;}
exgcd(b,a%b,y,x); y-=a/b*x;
} LL mul(LL a,LL b,LL mod)
{
LL res=;
while(b)
{
if(b&) { b--; res+=a; res%=mod; }
a<<=; a%=mod; b>>=;
}
return res;
} LL merge(LL n2,LL a2)
{
LL d=gcd(n1,n2),c=a2-a1;
if(c%d) return -;
LL x,y;
exgcd(n1/d,n2/d,x,y);
LL mod=n2/d;
x=(x%mod+mod)%mod;
LL k=(mul(c/d,x,mod)%mod+mod)%mod;
a1=(a1+n1*k%(mod*n1))%(mod*n1);
n1*=mod;
return a1;
} int main()
{
LL n,m; int k;
read(n); read(m); read(k);
for(int i=;i<=k;++i) read(a[i]);
if(!judge_lcm(k,n))
{
puts("NO");
return ;
}
n1=a[]; a1=;
LL j=a1;
for(int i=;i<=k;++i)
{
j=merge(a[i],a[i]-(i-));
if(j==-) { puts("NO"); return ; }
}
if(!j) j=lcm;
if(j+k->m) { puts("NO"); return ;}
for(int i=;i<=k;++i)
if(gcd(lcm,j+i-)!=a[i]) { puts("NO"); return ; }
puts("YES");
}

Codeforces 338 D. GCD Table的更多相关文章

  1. CF 338 D GCD Table(CRT)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 给定一个序列,a[1 ..k],问是否存在(i , ...

  2. Codeforces 583 DIV2 GCD Table 贪心

    原题链接:http://codeforces.com/problemset/problem/583/C 题意: 大概就是给你个gcd表,让你还原整个序列. 题解: 由$GCD(a,a)=a$,我们知道 ...

  3. 【Codeforces 582A】 GCD Table

    [题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数.          G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...

  4. 【Codeforces 582A】GCD Table

    [链接] 我是链接,点我呀:) [题意] 给你一个数组A[]经过a[i][j] = gcd(A[i],A[j])的规则生成的二维数组 让你求出原数组A [题解] 我们假设原数组是A 然后让A数组满足A ...

  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. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 在Windows商店应用中使用浅色主题

    在开发商店应用时会遇到这样的情况,设计师给我们的设计是浅色背景/深色文本,而商店应用默认是深色背景/浅色文本.那我们需要在每个页面去显式声明背景色和前景色吗,这显然是不理想的.这时就需要设置应用的主题 ...

  2. Azure 基础:Queue Storage

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 File Storage 的基本用 ...

  3. 7. Reverse Integer【Leetcode by java】

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  4. ats 安全

    Controlling Access ats可以配置为仅允许某些客户端使用代理缓存. 1. 为ip_allow.config添加一行,以获取允许访问ats的每个IP地址或IP地址范围; 2. traf ...

  5. 如何解决python连接数据库编码问题(python传数据到mysql乱码)'ascii' codec can't encode _mysql_exceptions.OperationalError: (1366, "Incorrect string value:?

    首先描述下问题:  在使用python计算出结果后将结果插入到mysql过程中,报如下错误.原因很好定位就是编码的问题.那么到底是编码哪里出了问题了呢? 报错如上: 排查顺序: 第一:python的编 ...

  6. LazyBug环境部署

    前言: LazyBug(授权协议:GPL)是一款PHP编写的开源HTTP接口测试管理系统,它集成了接口的测试.管理.维护.自动化回归等一系列工作,以实现对测试效率和管理效率的提高. 本次教程仅支持Wi ...

  7. SQLite与ContentProvider

    http://www.rom007.com/SQLite-yu-ContentProvider.html 在Android中,对于数据的存贮,有几种方式,有文件方式,有文件式数据库方式,Android ...

  8. 11慕课网《进击Node.js基础(一)》Buffer和Stream

    Buffer 用来保存原始数据 (logo.png) 以下代码读取logo.png为buffer类型 然后将buffer转化为string,新建png 可以将字符串配置: data:image/png ...

  9. 第二个spring冲刺第7天

    今天因为停电,所以没什么进展,延迟一天工作,今天当作休息

  10. 我是一个程序猿 ——《不是书评 :<我是一只IT小小鸟>》有感

    读了刘未鹏先生的文章<不是书评 :<我是一只IT小小鸟>>,产生了诸多共鸣,更明白了不少道理. 首先是一个很平常的现象,进度条效应,在操作移动终端上的软件时,如果没有进度条,人 ...