HDU4565-So Easy! (Use the constructor + power of the matrix)
Question Link
Question:
Solution
Ideas:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Signature + signature + o6zF5M/Signature + CjxoMz7Bvc/Signature + 3c/e1sbM9bz + signature/Signature + 0rK + signature/QW4/Signature + signature + Q248L25vYnI + release + release + 09 rKxzwvaDM + release/release + release/ydLU08O + 2 NXzv + release + sLro7o8YnI + c1_vcd4kpha + release = "brush: java; "> # include # Include # Include # Include # Include using namespace std; typedef long ll; // typedef _ int64 ll; ll a, B, n, m; struct mat {ll s [2] [2]; mat () {memset (s, 0, sizeof (s);} mat operator * (const mat & c) {mat ans; memset (ans. s, 0, sizeof (ans. s); for (int I = 0; I <2; I ++) for (int j = 0; j <2; j ++) ans. s [I] [j] = (s [I] [0] * c. s [0] [j] + s [I] [1] * c. s [1] [j]) % m; return ans;} void put () {for (int I = 0; I <2; I ++) {for (in T j = 0; j <2; j ++) printf ("% lld", s [I] [j]); printf ("\ n ");}}} c; void init () {c. s [0] [0] = 2 * a; c. s [0] [1] = B-a * a; c. s [1] [0] = 1; c. s [1] [1] = 0;} mat pow_mod (ll k) {if (k = 1) return c; mat a = pow_mod (k/2 ); mat ans = a * a; if (k % 2) ans = ans * c; return ans;} int main () {while (scanf ("% lld", & a, & B, & n, & m )! = EOF) {init (); ll s1, s2; s1 = (a * 2) % m; double t = pow (a + sqrt (double) B ), 2); s2 = (ll) ceil (t) % m; if (n = 1) printf ("% lld \ n", s1 ); else if (n = 2) printf ("% lld \ n", s2); else {mat ans = pow_mod (n-2); ll a = (ans. s [0] [0] * s2 % m) + m) % m + (ans. s [0] [1] * s1 % m) + m) % m); printf ("% lld \ n", a % m) ;}} return 0 ;}