Matrix Structure:
One matrix is full of constants, and the other is from a [I]. Evaluate the values of a [I + 1!
The construction matrix is as follows: Ai * bi AX * bx ax * by ay * bx ay * BY 0 a (I-1) * B (I-1) Ai 0 AX 0 AY 0 a (I-1) bi 0 0 bx by 0 B (I-1) 1 0 0 1 0 1Sum (I) AX * BX AX * BY AY * BX AY * BY 1 sum (I-1) sum (I) indicates the sum of I items, sum (I) = Sum (I-1) + ai * bi; Calculate the result of n times, and directly perform n-1 times on the matrix, the time complexity is 10 * logn ~ using the matrix's rapid power ~ Logn should pay attention to the modulo explosion range and n = 0.
# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <cmath> # include <string> # include <cstring> # include <set> # include <map> # include <list> # include <queue> # include <vector> # define tree int o, int l, int r # define lson o <1, l, mid # define rson o <1 | 1, mid + 1, r # define lo o <1 # define ro o <1 | 1 # define ULL unsigned long # define LL long # define inf 0x7fffffff # define eps 1e-7 # define M 10 00000007 # define N 100009 using namespace std; // int T, m, k, t, maxv; LL a0, b0, ax, bx, ay, by, n; LL ma [5] [5]; LL ans [5] [5]; void multi (LL a [] [5], LL B [] [5]) {LL c [5] [5]; for (int I = 0; I <5; I ++) for (int j = 0; j <5; j ++) {c [I] [j] = 0; for (int k = 0; k <5; k ++) if (a [I] [k] & B [k] [j]) {c [I] [j] = (c [I] [j] + (a [I] [k] * B [k] [j]) % M) % M ;}} memcpy (a, c, sizeof (c) ;}int main () {# ifndef ONLINE_JUDGE freopen ("ex. in "," r ", stdin); # endif while (Scanf ("% I64d", & n) = 1) {scanf ("% I64d % I64d % I64d", & a0, & ax, & ay ); scanf ("% I64d % I64d % I64d", & b0, & bx, & by); if (n = 0) // TLE, timeout if not added! {Puts ("0"); continue;} n --; memset (ma, 0, sizeof (ma); memset (ans, 0, sizeof (ans )); ma [0] [0] = 1; ma [1] [0] = ax * bx; ma [1] [1] = ax * bx; ma [2] [0] = ax * by; ma [2] [1] = ax * by; ma [2] [2] = ax; ma [3] [0] = ay * bx; ma [3] [1] = ay * bx; ma [3] [3] = bx; ma [4] [0] = ay * by; ma [4] [1] = ay * by; ma [4] [2] = ay; ma [4] [3] = by; ma [4] [4] = 1; ans [0] [0] = a0 * b0; ans [0] [1] = a0 * b0; ans [0] [2] = a0; ans [0] [3] = b0; ans [0] [4] = 1; for (int I = 0; I <5; I ++) {for (int j = 0; j <5; j ++) {ma [I] [j] % = M; ans [I] [j] % = M ;}} while (n) {if (n & 1) multi (ans, ma); n >>= 1; multi (ma, ma);} printf ("% I64d \ n", ans [0] [0]);} return 0 ;}