Title: http://poj.org/problem?id=2115
The first two days were solved by a linear equation of two yuan, and the optimal solution was obtained by using the extended Euclidean original aim.
Analysis:
Number theory is not too much to understand, the solution of the time, met with no small trouble.
Set the answer to X,n = (1<<k), then (a+c*x)% n = = B
IE (a+c*x) ≡b (mod n)//-----Results are obvious on both sides (A+CX)%n==b<n
Degenerate C*x≡ (b-a) (mod n)//----with Comodule nature a-c==b-c (mod n) on a==b (mod n)
oneself dizzy, or master of bad, and before the code, just the method of derivation more one.
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;Long Longa,b,c,k;Long Longx1,x2;Long LonggcdLong LongALong Longb) { returnb==0? A:GCD (b,a%b);}voidExtendLong LongALong LongBLong Long&X1,Long Long&y1) { if(b==0) {x1=1; Y1=0; return ; } Extend (B,a%b,x1,y1); Long Longt=X1; X1=Y1; Y1=t-(A/b) *Y1;}intMain () { while(SCANF ("%lld%lld%lld%lld", &a,&b,&c,&k)! =EOF) { if(a==0&&b==0&&c==0&&k==0) Break; Long LongA=C; Long LongB=pow (2, K); Long Longc=b-A; Long Longtemp=gcd (A, b); if(c%temp) {printf ("forever\n"); Continue; } A/=temp; B/=temp; C/=temp; Extend (A,B,X1,X2); Long Longt= (c*x1%b+b)%C; printf ("%lld\n", T); } return 0;}
Poj2115:c looooops (unary linear with congruence)