Bzoj 2875: [Noi2012] random number generator (Matrix fast power)

Source: Internet
Author: User
Tags mul

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.