X problem (Chinese remainder theorem + coprime version application) hdu1573

Source: Internet
Author: User
Tags greatest common divisor

x Problem

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3921 Accepted Submission (s): 1253


Problem description The number of x in a positive integer less than or equal to n is satisfied: 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).

The first behavior of input data is a positive integer t, which indicates that there is a T group of test data. The first behavior of each set of test data is two positive integers n,m (0 < N <= 1000,000,000, 0 < M <= 10), indicating that x is less than or equal to N, and that each has M elements in arrays A and B. The next two lines, each with m positive integers, are the elements in A and B, respectively.

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 Input310 31 2 30 1 2100 73 4 5 6 7 8 91 2 3 4 5 6 710000 101 2 3 4 5 6 7 8 9 100 1 2 3 4 5 6 7 8 9

In the application of Sample Output103, the Chinese remainder theorem (not coprime version) has just been learned; Test instructions will not speak, Chinese. Reprint please indicate source: http://www.cnblogs.com/yuyixingkong/topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1573
#include <stdio.h>#defineLL __int64LL a[ the],b[ the]; LL D,x,y,ans; LL DG;//Greatest Common DivisorvoidEXGCD (LL a,ll b,ll& d,ll& x,ll&y) {    if(!B) {d=a;x=1; y=0;} Else{EXGCD (b,a%b,d,y,x); Y-=x* (A/b); }}ll gcd (LL a,ll b) {if(!B)returnA; Elsegcd (B,a%b);}    LL China (ll N) {ll dm,a,b,d,x,y;    LL C1,c2,c; A=a[0]; C1=b[0];  for(intI=1; i<n;i++) {b=A[i]; C2=B[i];        EXGCD (A,b,d,x,y); DM=b/D; C=c2-C1; if(c%d)return-1; X= ((X*C/D)%DM+DM)%DM; C1=a*x+C1; A=a*b/D; } DG=A; if(c1==0) {C1=1;  for(intI=0; i<n;i++) {C1=c1*a[i]/gcd (C1,a[i]); } DG=C1; }    returnC1;}intMain () {LL t,n,m; scanf ("%i64d",&T);  while(t--) {scanf ("%i64d%i64d",&n,&M);  for(LL i=0; i<m;i++) scanf ("%i64d",&A[i]);  for(LL i=0; i<m;i++) scanf ("%i64d",&B[i]); Ans=China (M);//printf ("ans==%i64d\tdg=%i64d\n", ANS,DG);        if(ans==-1|| Ans>n) printf ("0\n"); Else{printf ("%i64d\n", (N-ans)/dg+1); }    }    return 0;}

X problem (Chinese remainder theorem + coprime version application) hdu1573

Related Article

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.