title: How many X are satisfied in a positive integer less than or equal to N: 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).
Solution: the same as the first question with the expansion of Euclidean Germany to find the last equation of the same residual equation x=ax+b, and then adjust X to obtain the number of X solution. For some explanations, see the code below.
Note-The minimum positive integer solution is obtained after each simultaneous equation, which can improve the code speed.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5 using namespacestd;6typedefLong LongLL;7 8 intn,m;9LL aa[ A],bb[ A];Ten Onell MAbs (ll x) {returnx<0? x:-x;} All EXGCD (ll A,ll b,ll& x,ll&y) - { - if(!B) {x=1, y=0;returnA;} the LL d,tx,ty; -D=EXGCD (b,a%b,tx,ty); -X=ty,y=tx-(A/b) *Ty; - returnD; + } - intMain () + { A intT; atscanf"%d",&T); - while(t--) - { -scanf"%d%d",&n,&m); - for(intI=1; i<=m;i++) scanf ("%i64d",&aa[i]); - for(intI=1; i<=m;i++) scanf ("%i64d",&bb[i]); in LL a,b,d,x,y; - BOOLok=false; toa=aa[1],b=bb[1]; + for(intI=2; i<=m;i++) - { theD=EXGCD (A,aa[i],x,y);//Ax-aa[i]y=bb[i]-b * if((bb[i]-b)%d!=0) {ok=true; Break;} $X=x* ((bb[i]-b)/d);Panax Notoginseng -LL T=mabs (aa[i]/d); thex= (x%t+t)%T; + Ab=a*x+b,a=a*aa[i]/d;//LCM (A,aa[i]); the } + if(OK) printf ("0\n"); - Else $ { $LL ans= (b%a+a)%a,cnt=0; //x=ax+b The minimum nonnegative integer solution for the X at the time of ans - if(ans>0&& ans<=n) cnt++; // if ANS is eligible for X-value to be counted in CNT -cnt+= (N-ans)/A; The number of solutions to x other than ans theprintf"%i64d\n", CNT); - }Wuyi } the return 0; -}
"Hdu 1573" x problem (number theory-expanding Euclid to solve the model problem of the congruence equation Group)