See also the Fibonacci number (+ matrix structure matrix fast power)

Copyright: private individuals do question summed up ~ https://blog.csdn.net/tb_youth/article/details/90941875

// fill the question ~~~
link: https://ac.nowcoder.com/acm/problem/15666
Source: Cattle-off network

Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32768K, other languages 65536k
64bit the IO the Format: LLD%
Here Insert Picture Description
Example 1
Input
Copy

4
1
2
3
100

Output
Copy

1
16
57
558616258

/ *
1 matrix structure ~
2 ~ matrix Fast Power

[f(n-2),f(n-1),n^3,n^2,n,1] * A = [f(n-1),f(n),(n+1)^3 ,(n+1)^2,n+1,1]
A = [
0,1,0,0,0,0,
1,1,0,0,0,0,
0,1,1,0,0,0,
0,1,3,1,0,0,
0,1,3,2,1,0,
0,1,1,1,1,1
]
[f(0),f(1),8,4,2,1] * A = [f(1),f(2),27,9,3,1]
[f(0),f(1),8,4,2,1] * A^n = [f(n),f(n=1),f(n+1)^3,f(n+1)^2,n+1,1]

*/
ac_code:

#include <stdio.h>
#define ll long long
const ll mod = 1e9+7;
struct mat
{
    ll m[10][10];
}a,e;
mat operator*(const mat x,const mat y)
{
    mat ans;
    ll tmp;
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 6; j++)
        {
            tmp = 0;
            for(int k = 0; k < 6; k++)
            {
                tmp = (tmp%mod+(x.m[i][k]%mod*y.m[k][j]%mod)%mod)%mod;
            }
            ans.m[i][j] = tmp;
        }
    }
    return ans;
}
mat quickPow(mat a,ll b)
{
    mat res = e;
    while(b)
    {
        if(b&1)
            res = res*a;
        a = a*a;
        b >>= 1;
    }
    return res;
}
int main()
{
    for(int i = 0; i < 6; i++)
    {
        e.m[i][i] = 1;
        a.m[i][1] = 1;
        a.m[5][i] = 1;
    }
    a.m[5][0] = 0;
    a.m[1][0] = 1;
    a.m[2][2] = 1;
    a.m[3][2] = 3;
    a.m[4][2] = 3;
    a.m[3][3] = 1;
    a.m[4][3] = 2;
    a.m[4][4] = 1;
    ll c[10] = {0,1,8,4,2,1};
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        mat tp = quickPow(a,n);
        ll val = 0; //f(n)
        for(int i = 0; i < 6; i++)
        {
            val = (val%mod + tp.m[i][0]*c[i]%mod)%mod;
        }
        printf("%lld\n",val);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/tb_youth/article/details/90941875