Log Concave Sequences(矩陣快速冪求遞推)

Log Concave Sequences

 Gym - 102302H 

A sequence of numbers A is said to be logarithm concave if, and only if, for every 2 ≤ i ≤ n - 1, ai - 1 * ai + 1 ≤ ai2.

For example the sequence A = (1, 2, 3) is logarithm concave. The sequence A = (2, 2, 3) is not logarithm concave.

In this problem, you will have to answer what is the number of log concave sequences which contain only the numbers 0, 1 or 2, with N elements.

As this number could be very large, print the answer mod 109 + 7.

Input

The input consists of a single integer N (3 ≤ N ≤ 1018) indicating the size of the log concave sequence.

Output

One integer with the number of log concave sequences with size N that contains only the numbers 0, 1 or 2.

Example

Input

3

Output

20

 

兩個數組合有九種方式:00,01,02,10,11,12,20,21,22,分別爲dp[1~9] ;

則根據每一個狀態可以由前一個狀態添加一個數(0,1,2)取後兩位,轉化而來,可以得到

dp[1] = dp[1] + dp[4] + dp[7]

dp[2] = dp[1]

dp[3] = dp[1]

dp[4] = dp[2] + dp[5] + dp[8]

dp[5] = dp[2] + dp[5]

dp[6] = dp[2]

dp[7] = dp[3] + dp[6] + dp[9]

dp[8] = dp[3] + dp[6] + dp[9]

dp[9] = dp[3] + dp[6] + dp[9]

轉化爲矩陣

dp

1     2     3    4    5     6     7    8    9

1  1  1  0  0  0  0  0  0 
0  0  0  1  1  1  0  0  0 
0  0  0  0  0  0  1  1  1 
1  0  0  0  0  0  0  0  0 
0  0  0  1  1  0  0  0  0 
0  0  0  0  0  0  1  1  1 
1  0  0  0  0  0  0  0  0 
0  0  0  1  0  0  0  0  0 
0  0  0  0  0  0  1  1  1 

此矩陣的每個單位的和爲n ==3式的答案

求矩陣的n-3次冪即爲答案
 

 

 

#include<bits/stdc++.h>
using namespace std;
const int maxn= 1e9+7;

struct node
{
    long long mar[10][10];
};

node a,b;

node mul(node a, node b)
{
    node c;
    memset(c.mar,0,sizeof(c.mar));

    for(int i = 0; i < 9; i ++)
    {
        for(int j = 0; j < 9; j ++)
        {
            if(a.mar[j][i]!= 0)
            {
                for(int k = 0; k < 9; k ++)
                {
                    c.mar[j][k] += a.mar[j][i]*b.mar[i][k];
                    c.mar[j][k] %= maxn;
                }

            }
        }
    }
    return c;
}

node quick_mul(node a, long long num)
{
    node ans;
    memset(ans.mar,0,sizeof(ans.mar));
    for(int i = 0; i < 9; i ++)
    {
        ans.mar[i][i] = 1;
    }
    while(num)
    {
        if(num&1)
        {
            ans = mul(ans,a);
        }
        a = mul(a,a);
        num >>= 1;
    }
    return ans;
}
int main()
{
    long long i,j,k,m,n,ans;
    scanf("%lld",&n);

    memset(a.mar,0,sizeof(a.mar));
    for(i = 0; i <= 2; i ++)//求得初始矩陣
    {
        for(j = 0; j <= 2; j ++)
        {
            for(k = 0; k <= 2; k ++)
            {
                if(i*i >= k*j)
                    a.mar[k*3+i][i*3+j] =1;

            }
        }
    }
    for(i = 0; i < 9; i ++)
    {
        for(j = 0; j < 9; j ++)
        {
            if(a.mar[i][j])
                printf("1 ");
            else
                printf("0 ");
        }
        printf("\n");
    }

    b = quick_mul(a,n-2);
    ans = 0;
    for(i = 0; i < 9; i ++)
    {
        for(j = 0; j < 9; j ++)
        {
            ans = (ans+b.mar[i][j])%maxn;
        }
    }
    printf("%lld\n",ans);

    return 0;
}

 

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