"poj2891" congruence equations

Source: Internet
Author: User

Congruence equations

examples 1 : pku2891strange to Express integers

The number of the congruence equations for the remainder theorem in China is 22. However, the subject (the general situation, also includes 22 of the situation, so the Chinese remainder theorem becomes "The tears of The Times") MoD number may not be mutual, so we need to convert to ask.

P=B1 (mod a1); P/A1 ==?~~~~B1

P =b2 (mod a2);

P =b3 (mod A3);

......

P =bn (mod an);

The a1~an,b1~bn is given out.

Solution:

Article One: a1*x+b1= P

Article II: a2*y+b2= P

First strip minus second: A1*x-a2*y = b2-b1

Set A=A1,B=-A2,K=B2-B1, get X (actually call EXGCD when you ignore the minus sign in front of A2)

If k%d!=0, no solution

Otherwise, x=[(x* k/d)% (b/d) + (b/d)]% (b/d)

LCU says least common multiple

P= a1*x+b1+ several times LCU (A1,A2) (or y= (k-ax)/b, p=a2*y+b2+ several times LCU (A1,A2)

So the new b= a1*x+b1, the new a= LCU (A1,A2),

The new B as B1, the new A as A1, and then to A3 and B3 Union, until the end, the last new B is X

1#include <cstdio>2#include <cstring>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <cmath>7 using namespacestd;8 #defineLL Long Long9 Ten LL a1,b1,a2,b2; One  A LL Ax,ay; - ll EXGCD (ll A,ll b) - { the     if(b==0) {ax=1, ay=0;returnA;} -LL G=EXGCD (b,a%b); -LL yy=ay; -ay=ax-a/b*ay;ax=yy; +     returnG; - } +  A intMain () at { -Freopen ("a.in","R", stdin); -Freopen ("a.out","W", stdout); -     intN; -      while(SCANF ("%d", &n)! =EOF) -     { inscanf"%lld%lld",&a1,&B1); -         BOOLok=1; to          for(intI=2; i<=n;i++) +         { -scanf"%lld%lld",&a2,&B2); the             if(!ok)Continue; * LL a,b,c,g; $a=a1,b=a2,c=b2-B1;Panax Notoginsengg=EXGCD (A, b); -             if(c%g!=0) {ok=0;Continue;} the             if(b/g<0) b*=-1; +Ax= ((ax*c/g)% (b/g) + (b/g))% (b/g); Aa=b1+ax*A1; theg=a1*a2/EXGCD (A1,A2); +a1=g;b1=A; -         } $         if(!ok) printf ("-1\n"); $         Elseprintf"%lld\n", B1); -     } -     return 0; the}
"poj2891"

2016-02-02 09:44:06

"poj2891" congruence equations

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.