Math # extended Euclid POJ 1061&2115&2891

Source: Internet
Author: User

Winter vacation To do the problem, first paste at that time write code.

POJ 1061

#include <iostream>#include<cstdio>typedefLong LongLL;using namespacestd;voidEXTEND_GCD (LL a,ll b,ll &d,ll &x,ll &y) {    if(b==0) {D=A; X=1, y=0; }    Else{EXTEND_GCD (b,a%b,d,y,x); Y-=x* (A/b); }}intMain () {LL x,y,m,n,l;  while(SCANF ("%i64d%i64d%i64d%i64d%i64d", &x,&y,&m,&n,&l)! =EOF)        {LL d,xx,yy; EXTEND_GCD (n-m,l,d,xx,yy); if(x-y)%d==0) {LL P=l/D; XX= (x-y)/d*xx; XX= (xx%p+p)%p; printf ("%i64d\n", XX); }        Elseprintf"impossible\n"); }    return 0;}

POJ 2115

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<cstdlib>#include<vector>#include<stack>#include<map>using namespaceStd;typedefLong LongLl;ll extend_gcd (ll a,ll b,ll&x,ll &y) {  //return D=GCD (a,n);    if(b==0) {x=1, y=0; returnA; }    Else{ll T=EXTEND_GCD (b,a%b,x,y); ll xx=x,yy=y; X=yy; Y=xx-(A/b) *yy; returnT; }}intMain () {ll a,b,c,k,x,y;  while(~SCANF ("%i64d%i64d%i64d%i64d",&a,&b,&c,&k)) {if(a==0&&b==0&&c==0&&k==0) Break; ll a=c,b=b-a,n= (LL)1<<k;//n=2^kll d=EXTEND_GCD (a,n,x,y); if(b%d!=0)//no solution to the equationprintf"forever\n"); Else{x= (x* (b/d))%n;//x is the minimum solution of Equation ax=b (mod n)x= (x (n/d) +n/d)% (N/D);//x is the smallest integer solution of equation ax=b (mod n)printf"%i64d\n", x); }    }    return 0;}

POJ 2891

#include <iostream>#include<cstdio>using namespaceStd;typedefLong Longll;voidEXTEND_GCD (ll a,ll b,ll &d,ll &x,ll &y) {    if(b==0) {D=A; X=1, y=0; }    Else{EXTEND_GCD (b,a%b,d,y,x); Y-=x* (A/b); }}intMain () {intK;    ll A1,r1,a,r;  while(~SCANF ("%d",&k)) {scanf ("%i64d%i64d",&a1,&R1); intflag=1;  for(intI=2; i<=k;i++) {scanf ("%i64d%i64d",&a,&R);            ll D,x,y;            EXTEND_GCD (A1,a,d,x,y); if((R-R1)%d!=0) Flag=0; LL P=a/D; X= (R-R1)/d*x; X= (x%p+p)%p; R1=a1*x+R1; A1=a1* (A/d); }        if(flag) printf ("%i64d\n", R1); Elseprintf"-1\n"); }    return 0;}

Math # extended Euclid POJ 1061&2115&2891

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.