Transmission Door
This problem is a good practice to solve the indefinite equation.
The topic is very strange. Also said what K-system, in fact, is to ask for a number a, each add C, ask how many times B to add, all the number of 2k modulo.
That means we can list the following equations: A+xc≡b (mod 2k)
We convert the equations on both sides of the equation, then we can get a form of the indefinite equation.
Old routines, judging there is no solution, otherwise with EXGCD solution.
There is a small hole in the 1<<32, you in the time, even if the variable opened Longlong also not, must be written in 1ll form, otherwise it will WA.
Take a look at the code.
#include <cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<Set>#include<vector>#include<queue>#definePB Push_back#defineRep (i,a,n) for (int i = a;i <= n;i++)#definePer (i,n,a) for (int i = n;i >= a;i--)#defineEnter Putchar (' \ n ')using namespaceStd;typedefLong Longll;Const intM =40005;Const intN =2000005;Const intINF =1000000009;Constll mod =51123987; ll read () {ll ans=0, op =1; CharCH =GetChar (); while(Ch <'0'|| CH >'9') { if(ch = ='-') op =-1; CH=GetChar (); } while(Ch >='0'&& CH <='9') {ans*=Ten; Ans+ = CH-'0'; CH=GetChar (); } returnAns *op;} ll A,b,c,k,x,y,p,q,s;ll gcd (ll a,ll b) {return(!b)? A:GCD (b,a%b);} ll EXGCD (ll A,ll b,ll&x,ll &y) { if(!b) {x=1, y =0; returnA; } ll D= EXGCD (b,a%b,y,x); Y-= A/b *x; returnD;}intMain () { while(1) {a= Read (), B = Read (), C = Read (), k =read (); if(!a &&!b &&!c &&!k) Break; P= C,q = 1LL << k,s = (b-a+q)%Q; ll G=gcd (P,Q); if(s%G) {printf ("forever\n"); Continue; } P/= g,q/= g,s/=G; EXGCD (P,q,x,y); X= (x + q)% q,x *= s,x%=Q; printf ("%lld\n", x); } return 0;}
POJ2115 C-loop