HDU 1573 x problem (unary linear congruence equation set)

Source: Internet
Author: User


X problem Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3850 Accepted Submission (s): 1228


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 Input
310 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

Sample Output
103





Idea: or the M-congruence equation 22 combined with EXTGCD in turn to solve

249MS 1400K 1068 B g++ #include <cstdio> #include <iostream> #include <cstring> #include <algorithm >using namespace Std;typedef Long long ll;ll a[15],b[15];ll n,m;ll x,y;ll e_gcd (ll a,ll b,ll&x,ll&y) {ll ans    ;    if (b==0) ans=a,x=1,y=0;    else ANS=E_GCD (b,a%b,y,x), y-= (A/b) *x; return ans;}    BOOL Flag;int Main () {int T;    scanf ("%d", &t);        while (t--) {flag=0;        scanf ("%i64d%i64d", &n,&m);        for (int i=1;i<=m;i++) scanf ("%i64d", &a[i]);        for (int i=1;i<=m;i++) scanf ("%i64d", &b[i]);            for (int i=2;i<=m;i++) {ll a=a[i-1],b=a[i],r=b[i]-b[i-1];            ll D=E_GCD (a,b,x,y);            if (r%d) flag=1;            ll t=b/d;            x= (x*r/d%t+t)%t;        a[i]=a[i-1]*a[i]/d;//merged into a new congruence equation b[i]=x*a[i-1]+b[i-1];//merged into a new congruence equation} if (flag) puts ("0");            else {ll ans=0,lcm=a[m]; b[m]= (b[m]%lcm+lcm-1)%lcm+1;//the solution in 1~lcm[a1,a2,a3...an] Within, since the title requirement is positive not including 0 while (b[m]<=n) ANS++,B[M]+=LCM;        printf ("%i64d\n", ans); }} return 0;}


HDU 1573 x problem (unary linear congruence equation set)

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.