"Hdu1573-x problem" expands Euclidean-congruence equations

Source: Internet
Author: User

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

The number of X in a positive integer less than or equal to N is satisfied:

X MoD a0 = B0

X MoD A1 = B1

......

X mod ai = Bi

(0<ai<=10)

Input: First behavior a positive integerT, indicating that there areTGroup test Data. First behavior of each set of test data two positive integersN,m (0 < N <= 1000,000,000, 0 < M <=), indicatingXless than or equalN, the arrayaand thebin each hasMan element. In the next two lines, each row hasMa positive integer, respectively, for theaand thebthe element in the.

Output: Corresponds to each set of inputs, outputting a positive integer in a separate row that represents the number of X that satisfies the condition.

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

Problem: The bare topic of the same residual equation group. In particular, it is important to note that a positive integer is required, that is, 0 of the time to special treatment.

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>using namespaceStd;typedefLong LongLL;ConstLL m= -; LL A[m],b[m]; ll EXGCD (ll U,ll v,ll&x,ll &y) {    if(v==0) {x=1; y=0;returnu;} Else{LL tx,ty; LL D=EXGCD (v,u%v,tx,ty); X=Ty; Y=tx-(u/v) *Ty; returnD; }}intMain () {//freopen ("a.in", "R", stdin); //freopen ("A.out", "w", stdout);LL T; scanf ("%i64d",&T);  while(t--) {LL n,m; BOOLbk=1; scanf ("%i64d%i64d",&n,&m);  for(LL i=1; i<=m;i++) scanf ("%i64d",&A[i]);  for(LL i=1; i<=m;i++) scanf ("%i64d",&B[i]);        LL a,b,c,g,x,tx,ty,a1,b1; A1=a[1],b1=b[1];  for(LL i=2; i<=m;i++) {A=A1; B=a[i]; c=b[i]-B1; G=EXGCD (a,b,tx,ty); if(c%g) {bk=0; Break;} X= ((tx*c/g)% (b/g) + (b/g))% (b/g); B1=a1*x+B1; A1=a1/g*A[i]; }        if(BK) {/*after the indefinite equation is a1x+b1=p, the x=0 b1=p is obtained.            Because x can be equal to 0:ans= (N-B1)/a1+1, if p=0, then b1=x=0, does not satisfy positive integers, so subtract. */LL ans=0; if(N&GT;=B1) ans= (N-B1)/a1+1; if(Ans && b1==0) ans--; printf ("%i64d\n", ans); }        Elseprintf"0\n"); }    return 0;}
View Code

"Hdu1573-x problem" expands Euclidean-congruence equations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.