http://www.lydsy.com/JudgeOnline/problem.php?id=2875
Matrix words are easy to see ..... I'm not going to write it. It's too much water.
Then multiply Longlong will overflow ... So we're using a fast ride ... is to turn the multiplication of the fast power into addition ... This is very simple.
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef unsigned long long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i ) #define FOR2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n);-I.) #define FOR4 (I,a,n) for (int i= (a);i> (n),-i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d ", a) #define DBG (x) cout << (#x) <<" = "<< (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }ll m, A, C, x0, N, g;typedef ll mtx[2][2];mtx T;ll Mul (lL A, ll b) {ll ret=0;while (b) {if (b&1) ret= (ret+a)%m;a= (a+a)%m;b>>=1;} return ret;} void Mtxmul (MTX A, mtx B, mtx c, int la, int lb, int lc) {Rep (i, LA) Rep (J, LC) {T[i][j]=0;rep (k, LB) t[i][j]= (t[i][j]+mul (A[i][k], b[k][j])) %m;} Rep (i, LA) Rep (J, LC) C[I][J]=T[I][J];} MTX Ma, MB, mc;int main () {cin >> m >> a >> c >>x0 >> n >> g;ma[0][0]=x0; ma[0][1]=1;m B[0][0]=a; Mb[0][1]=0;mb[1][0]=c; Mb[1][1]=1;mc[0][0]=mc[1][1]=1;while (n) {if (n&1) Mtxmul (MC, MB, MC, 2, 2, 2), N>>=1;mtxmul (MB, MB, MB, 2, 2, 2);} Mtxmul (MA, MC, MA, 2, 2, 2);p rintf ("%llu\n", ma[0][0]%g); return 0;}
Description Input
Contains 6 m,a,c,x0,n and g separated by a space, where a,c,x0 is a non-negative integer and M,n,g is a positive integer.
Output
Output a number, i.e. xn mod g
Sample Input
11 8 7 1 5 3
Sample Output2
HINT Source
"Bzoj" 2875: [Noi2012] random number generator (matrix multiplication + fast multiply)