generator 1(十倍快速幂)

题目链接

题意:    

 

思路:只要求出矩阵{{a,b}{1,0}}的n-1次方就能得出答案。学习了网上的十倍快速幂https://blog.csdn.net/To_the_beginning/article/details/88367974

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#define ll long long
using namespace std;
const int N = 1e6 + 10;
struct node {
    ll mat[2][2];
};
ll x0, x1, a, b, mod;
node ans, O, tmp, z;
char n[N];
node cul (node x, node y) {
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 2; j++) {
            z.mat[i][j] = 0;
            for(int k = 0 ; k < 2; k++)
                z.mat[i][j] = (z.mat[i][j] + x.mat[i][k] * y.mat[k][j] ) % mod;
        }
    return z;
}
int main() {
    int op;
    scanf("%lld %lld %lld %lld %s %lld", &x0, &x1, &a, &b, n, &mod);
    int len = strlen(n);
    ans.mat[0][0] = ans.mat[1][1] = 1;
    tmp.mat[0][0] = a;
    tmp.mat[0][1] = b;
    tmp.mat[1][0] = 1;
    for(int i = len - 1; i >= 0; i--) {
        op = n[i] - '0';
        for(int j = 0; j < op; j++) ans = cul(ans, tmp);
        O.mat[0][0] = O.mat[1][1] = 1;
        O.mat[0][1] = O.mat[1][0] = 0;
        for(int j = 0; j < 10; j++) O = cul(O, tmp);
        tmp = O;
    }
    printf("%lld\n", (x1 * ans.mat[1][0] + x0 * ans.mat[1][1] ) % mod) ;
    return 0;
}

 

posted @ 2019-08-02 19:10  Ldler  Views(145)  Comments(0Edit  收藏  举报