Time Limit: 0.5 s
Space limit: 4 m
Question:
Make X0 =,
Xi = (A * Xi-1 ^ 2, B * Xi-1 + C) % m;
Calculate XK, (0 <= k <= 109), (0 <= A, B <= 100), (1 <m <1000 );
Solution:
The key to the question lies in the scope of M. The range of 1000 is obviously cyclical after the remainder is obtained.
You only need to get the length of the loop sequence and each of its numbers, and the starting position of any loop. You can calculate the XK value.
Note that x0 does not return M remainder, and when x0 is large, the intermediate amount of computing X1 may exceed the int range.
Code:
# Include <iostream> # include <string> # include <cstring> # include <vector> using namespace STD; const int INF = 111111; vector <int> ans; int f [INF]; long X, A, B, C, M, K, T; inline long GET (long X) {return (A * x + B * x + C) % m;} int main () {CIN> x> A> B> C> m> K; // locate the end of the first loop, that is, for (t = 0 ;! F [X]; X = get (x), t ++) {f [x] = 1; if (t = k) {cout <X; return 0 ;}} K-= (t-1); // the new position of K from the starting point of the loop // get the cyclic array memset (F, 0, sizeof F ); for (t = 0 ;! F [X];) {f [x] = 1; ans. push_back (x); X = get (x), t ++;} // locate K in the loop section and output k = K % t-1; if (k <0) k = t-1; cout <ans [k]; return 0 ;}