HDU——T 1573 X问题

http://acm.hdu.edu.cn/showproblem.php?pid=1573

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6718    Accepted Submission(s): 2342


Problem Description
求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。
 
Input
输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。
 
Output
对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。
 
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
 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 const int N(1000000001);
 7 int n,m,a[11],b[11],tot;
 8 
 9 int exgcd(int a,int b,int &x,int &y)
10 {
11     if(!b)
12     {
13         x=1; y=0;
14         return a;
15     }
16     int ret=exgcd(b,a%b,x,y),tmp=x;
17     x=y; y=tmp-a/b*y;
18     return ret;
19 }
20 int CRT()
21 {
22     int ret=b[1]; tot=a[1];
23     for(int i=2;i<=m;i++)
24     {
25         int x,y,tmp;
26         int c=b[i]-ret;
27         int gcd=exgcd(tot,a[i],x,y);
28         if(c%gcd) return N;
29         x=x*c/gcd;
30         int mod=a[i]/gcd;
31         x=(x%mod+mod)%mod;
32         ret+=tot*x; tot*=mod;
33     }
34     if(!ret) ret+=tot;
35     return ret;
36 }
37 
38 int main()
39 {
40     int t; scanf("%d",&t);
41     for(int ans=0;t--;ans=0)
42     {
43         scanf("%d%d",&n,&m);
44         for(int i=1;i<=m;i++) scanf("%d",a+i);
45         for(int i=1;i<=m;i++) scanf("%d",b+i);
46         int tmp=CRT();
47         for(;tmp<=n;tmp+=tot) ans++;
48         printf("%d
",ans);
49     }
50     return 0;
51 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7352287.html