Matrix Fast power ... + fast multiply on OK
--------------------------------------------------------------------------------------
#include <bits/stdc++.h>using namespace std;typedef long Long ll;ll MOD, A, C, X, N, G;ll MUL (ll A, ll b) {ll ans = 0;for (; b; b >>= 1) {if (b & 1) ans + = A;if (ans >= mod) ans-= mod;a <<= 1;if (a >= mod) A-= mod;}return ans;}struct Matrix {ll x[2][2];Matrix () {memset (x, 0, sizeof x);}inline void Unit () {x[0][0] = x[1][1] = 1;x[0][1] = x[1][0] = 0;}Matrix operator * (const matrix &o) {matrix ans;for (int i = 0; i < 2; i++)For (int j = 0; J < 2; j + +)for (int k = 0; k < 2; k++)(Ans.x[i][j] + = MUL (X[i][k], o.x[k][j]))%= MOD;return ans;}Matrix operator = (const matrix &o) {memcpy (x, o.x, sizeof x);return *this;}matrix operator ^ (ll k) {matrix ans, p = *this; Ans.unit ();For (; k; k >>= 1) {if (K & 1) ans = ans * p;P = p * p;}return ans;}} Q; int main () {cin >> MOD >> a >> c >> x >> n >> G;q.x[0][0] = A; Q.X[0][1] = 0; Q.x[1][0] = c; Q.X[1][1] = 1;Matrix ans = Q ^ n;cout << (MUL (x, ans.x[0][0]) + ans.x[1][0])% MOD% g << "\ n";return 0;}
--------------------------------------------------------------------------------------
2875: [Noi2012] random number generator time limit: ten Sec Memory Limit: MB
Submit: 1289 Solved: 731
[Submit] [Status] [Discuss] Descriptioninput
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 fast power)