Topic: Give a table G of n*m and an M sequence of k integers. (1 < n,;1012; 1
The value of each element in the table is G[i][j]=GCD(i,j). Determine whether there is a row of elements starting from a column in table G and the given sequence coincides.
Links: http://codeforces.com/problemset/problem/338/D
First, if it exists, it must appear in I=lcm(M1,M2,...). The proof on cf is not very clear. If I > n can judge NO directly.
For j:
j=0 mod m1
j+1=0 mod m2
.
.
j+k-1=0 mod mk
=>j=1-i mod mi
The Chinese Remainder Theorem understands that the smallest J is substituted for the original condition judgment, so that LCM=lcm(m1,m2,... In theory, the general solution is J = j + LCM * x (x > 0), GCD (i, j) = GCD (i, j + LCM * x) = GCD (i, j + I * x) = GCD (i, j + I * x) = GCD (i, j). So judging J is enough.
#include<cstdio> // j=0 mod m1 => J=m1*x0+a1 mod lcm(m1,m2)
#include<queue> // j=-1mod m2
#include<iostream>//j=-2mod m3
#include<vector>
#include<map>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<algorithm>
#define cle(a) memset(a,0,sizeof(a))
#define inf(a) memset(a,ox3f,sizeof(a))
#define ll long long
#define Rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
const int INF = ( 2e9 ) + 2;
const int maxn = 1e4+10;
ll A[maxn],M[maxn];
ll n,m,k;
ll exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b){x=1;y=0;d=a;}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
ll Chinese_remainder(ll *A,ll *M) // Extended Chinese Remainder Theorem: General Solution: LA+k*LM (k >=0)
{
ll d,x,y,LM=M[1],LA=A[1];
for(int i=2;i<=k;i++)
{
ll a=LM,b=M[i],c=A[i]-LA;
exgcd(a,b,d,x,y);
if(c%d)return -1;
ll mod=b/d;
x=x*c/d;
x=(x%mod+mod)%mod;
LA = LM*x+LA;
LM = LM*b/d;
}
return LA; // Minimal solution
}
bool check()
{
ll lcm=1;
for(int i=1;i<=k;i++)lcm=lcm/__gcd(lcm,M[i])*M[i];
if(lcm>n)return false; // At least on the lcm line
for(int i=1;i<=k;i++)A[i]=(1-i);
ll J=Chinese_remainder(A,M); //The Minimum Understanding of Chinese Remainder Theorem
if(J==0)J=lcm; // The solution is 0?
if(J>m-k+1||J<=0)
{
return false;
}
for(int i=0;i<k;i++)
if(__gcd(lcm,(ll)J+i)!=M[i+1])return false;
return true;
}
int main()
{
// freopen("in.txt","r",stdin);//
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(int i=1;i<=k;i++)scanf("%I64d",&M[i]);
if(check())printf("YES\n");
else printf("NO\n");
}