Topic 1: Number theory six • time limit for modular linear equations: 10000ms single point time limit: 1000ms memory limit: 256MB description
Little ho: Today I heard a very interesting story!
Little hi: What story?
Small ho: said the Qin Dynasty, Liu Bang's general Han Xin led 1500 soldiers through a battle, killed more than 400 people. Han Xin in order to count the number of soldiers stood three people in a row, more than two people, stand five people in a row, more than four people, stand seven people in a row, more than six people. Han Xin immediately knew that the remaining number was 1049 people.
Little hi: Han xin Soldiers of, this story is very famous.
Little ho: I think there must be some clever way of calculating this! Otherwise, Han Xin can't calculate it so quickly.
Little hi: So we might as well take a look at the mathematical model of this story?
Little ho: Good!
< Little Ho thought a little bit about >
Small ho: Han Xin is to calculate the number of soldiers, then we set this number as X. Three men in rows, five in rows, seven in rows, x mod 3, x mod 5, x mod 7. That means we can list a set of equations:
X mod 3 = 2x mod 5 = 4x MoD 7 = 6
Han Xin is based on this equation set, the value of x is solved.
Little hi: Well, that's it! We generalize this equation group to the general form: given n group Divisor m[i] and remainder r[i], the N group (M[i],r[i]) solves an X, making x mod m[i] = r[i].
Little ho: How do I feel that this equation set has a fixed solution?
Small hi: This equation group is called the modular linear equations. It does have a fixed workaround. But before I tell you the solution, why don't you think about how to solve it yourself?
Little ho: Well, let me try it first!
Hint: Modular linear equation Group
Input
Line 1th: 1 positive integers, n,2≤n≤1,000.
2nd.. N+1:2 positive integers, line i+1 represents group I m,r,2≤m≤20,000,000,0≤r<m.
Use 64-bit integers as much as possible during the calculation.
Output
Line 1th: An integer representing the minimum x that satisfies the requirement, if no solution output-1. The answer range is within the 64-bit integer type.
-
-
Sample input
-
-
33 25 37 2
-
-
Sample output
-
23
Bare Chinese remainder theorem, the data is strong, the original written can not, estimated multiplied by 64;
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell Long Long#defineMoD 1000000007#defineINF 999999999//#pragma COMMENT (linker, "/stack:102400000,102400000")intScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;} ll a[100010];ll b[100010];ll gcd (ll x,ll y) {if(y==0) returnx; Else returnGCD (y,x%y);}voidEXGCD (ll A, ll B, ll &x, LL &y) { if(b = =0) {x=1; Y=0; return; } EXGCD (b, a%b, x, y); LL TMP=x; X=y; Y= tmp-(A/b) *y;}intMain () {ll x,y,z,i,t; while(SCANF ("%lld", &z)! =EOF) { for(i=0; i<z;i++) scanf ("%lld%lld",&b[i],&A[i]); LL A1=a[0],b1=b[0]; ll Jie=1; for(i=1; i<z;i++) {ll A2=a[i],b2=B[i]; ll Xx,yy; ll Gys=gcd (B1,B2); if((A2-A1)%Gys) {Jie=0; Break; } EXGCD (B1/gys,b2/gys,xx,yy); XX= ((a2-a1)/gys*xx)% (b[i]/gys); A1= a1+xx*B1; B1= b1/gys*B[i]; A1= ((A1%B1) +b1)%B1; } if(!Jie) printf ("-1\n"); Elseprintf ("%lld\n", A1); } return 0;}
Hihocode 97-week Chinese remainder theorem