2019牛客暑期多校訓練營(第五場) B-generator 1 (十進制矩陣快速冪)

鏈接:https://ac.nowcoder.com/acm/contest/885/B
來源:牛客網
 

題目描述

You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0​,x1​,a,b. And you know xi=a⋅xi−1+b⋅xi−2x_i = a \cdot x_{i-1} + b \cdot x_{i-2}xi​=a⋅xi−1​+b⋅xi−2​ for all i≥2i \ge 2i≥2.

Given two positive integers n, and MOD, please calculate xnx_nxn​ modulo MOD.

Does the problem look simple? Surprise! The value of n may have many many digits!

輸入描述:

The input contains two lines.
The first line contains four integers x0,x1,a,bx_0, x_1, a, bx0​,x1​,a,b (1≤x0,x1,a,b≤1091 \le x_0, x_1, a, b \le 10^91≤x0​,x1​,a,b≤109).
The second line contains two integers n, MOD (1≤n<10(106),109<MOD≤2×1091 \le n < 10^{(10^6)}, 10^9 < MOD \le 2 \times 10^91≤n<10(106),109<MOD≤2×109, n has no leading zero).

輸出描述:

Print one integer representing the answer.

示例1

輸入

複製

1 1 1 1
10 1000000001

輸出

複製

89

說明

The resulting sequence x is Fibonacci sequence. The 11-th item is 89.

示例2

輸入

複製

1315 521 20185 5452831
9999999999999999999999999999999999999 1000000007

輸出

複製

914730061

 解題思路:十進制矩陣快速冪。答案爲矩陣{{a,b}{1,0}}的n-1次方。

十進制快速冪可參考:Here

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e3 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;
const int MOD = 51123987 ;

struct Node {
    ll mat[2][2] ;
}ans, tmp, O, z ;

ll x0, x1, a, b ;
ll mod, len ;
string n ;

void init(){
    ans.mat[0][0] = ans.mat[1][1] = 1 ;
    ans.mat[0][1] = ans.mat[1][0] = 0 ;
    tmp.mat[0][0] = a ;
    tmp.mat[0][1] = b ;
    tmp.mat[1][0] = 1 ;
    tmp.mat[1][1] = 0 ;
}

Node getPow(Node a, Node b){
    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] + a.mat[i][k] * b.mat[k][j]) % mod ;
            }
        }
    }
    return z ;
}

int main (){
    ios_base::sync_with_stdio(false) ;
    cin.tie(0) ;
    cout.tie(0) ;
    cin >> x0 >> x1 >> a >> b ;
    cin >> n >> mod ;
    len = n.size() ;
    len-- ;
    init() ;
    for (int i = len; i >= 0; i--) {
        ll cnt = n[i] - '0' ;
        for (int i = 1; i <= cnt; i++) ans = getPow(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 = getPow(O, tmp) ;
        tmp = O ;
    }
    cout << (x1 * ans.mat[1][0] + x0 * ans.mat[1][1]) % mod << endl ;
    return 0 ;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章