Topic Links:
http://poj.org/problem?id=2115
Main topic:
For loop statements:
for (int i = A; I! = B; i + = C)
Statement 1;
It is known that I, A, B, and C are all K-binary unsigned integer types, giving values of a, B, C, K, and calculating and outputting statements 1
The number of executions, if infinite, then the direct output "FOREVER".
Ideas:
set the algorithm to perform X-steps, then the title becomes solution A + cx≡b (mod M) (m= 2^k). that is a + CX + my≡b.
CX + MY ≡b-a (M = 2^k) is changed in order to find linear congruence equation, simple linear congruence algorithm can be applied.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;__int64 a,b,c,k;__int64 a,b,c,d,x,y;void exgcd (__int64 a,__int64 b,__int64 &d,__int64 &x,__int64 &y) { C0/>if (!b) x = 1, y = 0, d = A; else { exgcd (b,a%b,d,y,x); Y-= x * (A/b); }} int main () {while (cin >> A >> B >> C >> k && (a| | b| | c| | k)) { a = C; c = b-a; b = (__int64) 1 << k; EXGCD (a,b,d,x,y); if (c% d! = 0) cout << "FOREVER" << Endl; else { __int64 ans,temp; Ans = x * C/D; temp = b/d; ans = ans% temp + temp; cout << ans% temp << endl; } } return 0;}
POJ2115 C looooops "Solving linear congruence equation"