Hdu1573:x problem (solving a linear congruence equation set)

Source: Internet
Author: User

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

Problem analysis; HDU is the pit, is because n,m defined as __int64 on the way, changed to int on a, no words.

The problem is that the number of solutions to a linear congruence equation of one element is less than that of a positive integer n. The solution of the minimum positive integer is x= (x* (C/D)%t+t)%t;
x=a1*x+R1, where X is the special solution of the extended Euclidean solution, the cycle interval for the M-Systems is LCM (a1,a2,a3...am),
So the answer is (n-x)/lcm+1;

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;__int64 a,b,c,d;__int64 x,y;__int64 gcd (__int64 A,__int64 b) {returnb==0? A:GCD (b,a%B);}voidExtend (__int64 a,__int64 b,__int64 &d,__int64 &x1,__int64 &y1) {    if(b==0) {x1=1; Y1=0; D=A; return ; } Extend (B,a%b,d,x1,y1); __int64 Temp=X1; X1=Y1; Y1=temp-(A/b) *Y1; return ;}intMain () {__int64 s[ -],e[ -];    __int64 A1,R1,A2,R2,LCM;    __int64 T; intn,m; scanf ("%i64d",&T);  while(t--) {scanf ("%d%d",&n,&m); LCM=1;  for(intI=1; i<=m; i++) {scanf ("%i64d",&S[i]); LCM=LCM/GCD (Lcm,s[i]) *s[i];//to a certain extent can prevent explosion type (LCM*S[I]/GCD ())        }         for(intI=1; i<=m; i++) {scanf ("%i64d",&E[i]); }        BOOLIfhave=true; A1=s[1],r1=e[1];  for(__int64 i=2; i<=m; i++) {A2=s[i],r2=E[i]; A=A1; b=A2; C=r2-R1;            Extend (a,b,d,x,y); if(c%d) {Ifhave=false;  Break; } __int64 T=b/D; X= (x* (C/D)%t+t)%T; X=a1*x+R1; A1=a1* (a2/d); R1=y; } __int64 ans=0; if(!Ifhave) {printf ("0\n"); Continue; }        if(r1<=n) ans=1+ (N-R1)/Lcm; if(r1==0&&ans) ans--; printf ("%i64d\n", ans); }    return  0;}

Hdu1573:x problem (solving a 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.