# HDU 1573 X问题

Problem Description

Input

Output

Sample Input

3 10 3 1 2 3 0 1 2 100 7 3 4 5 6 7 8 9 1 2 3 4 5 6 7 10000 10 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9

Sample Output

1 0 3

Author

lwg

Source

HDU 2007-1 Programming Contest

Recommend

linle   |   We have carefully selected several similar problems for you:  1788 1695 1452 1060 1299

```#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN=1e6+10;
int N,K,C[MAXN],M[MAXN],x,y;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int exgcd(int a,int b,int &x,int &y)
{
if(b==0){x=1,y=0;return a;}
int r=exgcd(b,a%b,x,y),tmp;
tmp=x;x=y;y=tmp-(a/b)*y;
return r;
}
int inv(int a,int b)
{
int r=exgcd(a,b,x,y);
while(x<0) x+=b;
return x;
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int T;
scanf("%d",&T);
while(T--)
{
memset(M,0,sizeof(M));
memset(C,0,sizeof(C));
scanf("%d%d",&N,&K);
for(int i=1;i<=K;i++) scanf("%d",&M[i]);
for(int i=1;i<=K;i++) scanf("%d",&C[i]),C[i]%=M[i];
bool flag=1;
for(int i=2;i<=K;i++)
{
int M1=M[i-1],M2=M[i],C2=C[i],C1=C[i-1],T=gcd(M1,M2);
if((C2-C1)%T!=0) {flag=0;break;}
M[i]=(M1*M2)/T;
C[i]= ( inv( M1/T , M2/T ) * (C2-C1)/T ) % (M2/T) * M1 + C1;
C[i]=(C[i]%M[i]+M[i])%M[i];
}
if(flag==0) printf("0\n");
else
{
if(N<C[K]) printf("0\n");
else
{
int ans=(N-C[K])/M[K]+1;
//if(C[K]) ans++;
printf("%d\n",ans);
}

}
}
return 0;
}```

1811 篇文章123 人订阅

0 条评论