Luo Gu P1939 [template] matrix accelerate (the number of columns)

Luo Gu P1939 [template] matrix accelerate (the number of columns)

Description

  • a[1]=a[2]=a[3]=1

    a[x]=a[x-3]+a[x-1] (x>3)

    Find a value of the number of columns n items of 1,000,000,007 (10 ^ 9 + 7) modulo a.

Input

  • A first line integer T, denotes the number of interrogation.

    The following T lines, each a positive integer n.

Output

  • Each line of output a non-negative integer answers.

Sample Input

3
6
8
10

Sample Output

4
9
19

Data Size

  • 30% of the data for n <= 100;

    60% of the data for n <= 2 * 10 ^ 7;

    To 100% of the data T <= 100, n <= 2 * 10 ^ 9;

answer:

  • A matrix accelerate.
  • Water cut off direct questions
#include <iostream>
#include <cstdio>
#include <cstring>
#define mod 1000000007
#define LL long long
using namespace std;

struct Obj
{
    LL m[4][4];
    Obj () {memset(m, 0, sizeof(m));}
};
LL T, n;

Obj mul(Obj a, Obj b)
{
    Obj c;
    for(LL i = 1; i <= 3; i++)
        for(LL j = 1; j <= 3; j++)
            for(LL k = 1; k <= 3; k++)
                c.m[i][j] += (a.m[i][k] % mod * b.m[k][j] % mod) % mod,
                c.m[i][j] %= mod;
    return c;
}

Obj power(Obj a, LL b)
{
    Obj d; d.m[1][1] = d.m[2][2] = d.m[3][3] = 1;
    Obj r = d, base = a;
    while(b)
    {
        if(b & 1) r = mul(r, base);
        base = mul(base, base);
        b >>= 1;
    }
    return r;
}

int main()
{
    cin >> T;
    while(T--)
    {
        cin >> n;
        if(n <= 3) {cout << 1 << endl; continue;}
        Obj t1;
        t1.m[1][1] = t1.m[1][3] = t1.m[2][1] = t1.m[3][2] = 1;
        t1 = power(t1, n - 3);
        Obj t2, ans; t2.m[1][1] = t2.m[2][1] = t2.m[3][1] = 1;
        for(LL i = 1; i <= 3; i++)
            for(LL j = 1; j <= 1; j++)
                for(LL k = 1; k <= 3; k++)
                    ans.m[i][j] += (t1.m[i][k] % mod * t2.m[k][j] % mod) % mod,
                    ans.m[i][j] %= mod;
        cout << ans.m[1][1] << endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11204530.html