HDU6030 Happy Necklace (Fast Power matrix derived +)

HDU6030 Happy Necklace

Or may be derived to find the law have the formula: \ (F [n-] = F [n--. 1] + F [. 3-n-] \) .

Configuration matrix multiplication:
\ [\} F_i pmatrix the begin {F_ {\\ \\}. 1 I-I-F_ {2} \} End {pmatrix = \ pmatrix the begin {}. 1. 1 & & \\ 0 & 0. 1 & 0 \\ 0 & 1 & 0 \ end {pmatrix} \ begin {pmatrix} f_ {i-1} \\ f_ {i-2} \\ f_ {i-3} \ end {pmatrix} \]

#include<bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
int t;
long long n;
struct Matrix{
    long long a[5][5];
};

Matrix mul(Matrix M1, Matrix M2)
{
    Matrix ret;
    memset(ret.a, 0, sizeof(ret.a));
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            for(int k = 0; k < 3; k++){
                ret.a[i][j] = (M1.a[i][k] * M2.a[k][j] + ret.a[i][j]) % mod;
            }
        }
    }
    return ret;
}
void matrix_pow(long long x)
{
    Matrix ret;
    memset(ret.a, 0, sizeof(ret.a));
    for(int i = 0; i < 3; i++) ret.a[i][i] = 1;
    Matrix tmp;
    memset(tmp.a, 0, sizeof(tmp.a));
    tmp.a[0][0] = tmp.a[0][2] = tmp.a[1][0] = tmp.a[2][1] = 1;
    while(x){
        if(x & 1LL) ret = mul(ret, tmp);
        tmp = mul(tmp, tmp);
        x >>= 1LL;
    }
    long long ans = (ret.a[0][0] * 4 + ret.a[0][2] * 2 + ret.a[0][1] * 3) % mod;
    cout << ans << endl;
}
int main()
{
    for(scanf("%d", &t); t--; ){
        scanf("%lld", &n);
        if(n == 1) {puts("2"); continue;}
        else if(n == 2) {puts("3"); continue;}
        else if(n == 3) {puts("4"); continue;}
        else{
            matrix_pow(n - 3);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/solvit/p/11445300.html