http://codeforces.com/group/NVaJtLaLjS/contest/241861/problem/A
In the beginning God created the heaven and the earth, the next five days God created the light, the water, the sky, the land, the trees and the living creatures, and on the sixth day he formed a man from the dust of the ground, called Adam. Afterwards, God took one of the man’s ribs and made a woman from the rib, called Eve. Adam and Eve lived in peace and harmony in the land, until God told them they should not take any apple from the only apple tree in Earth (at that moment, in year 0).

How they disobeyed God is a story from another day, now we are interested in how apple trees population increased over all those years. Apple trees are very interesting because their cycle of life is the following: first an apple fall into the ground and immediately a new apple tree blooms, from this moment we consider this as an apple tree, exactly ten years later the tree is full-grown and it starts to generate apples, from here every ten years the tree generates f(x) apples, where and x is the age of the tree, note that x will be always be a multiple of 10. Every apple generated from a tree will fall into the ground and generate a new tree, finally every apple tree dies exactly at the age of 45.

Now we want to know how many apple trees will be living on Earth after n years of the creation. At year 0 the apple tree created by God was not full-grown (this happened 10 years later).

Input
The input consists of only one integer n (0 ≤ n ≤ 1015) - the number of years after the creation of the Earth including Adam, Eve and the first apple tree.

Output
Print a single integer - the number of apple trees on Earth at the end of the n - th year after the creation of Earth, as this number can be very big print it modulo 109 + 7

Examples
inputCopy
9
outputCopy
1
inputCopy
10
outputCopy
17
inputCopy
44
outputCopy
77328
inputCopy
45
outputCopy
77327
Note
In the first case, at 9th year there was on Earth only the apple tree that God first created.

In second case we have that in the 10th year the first apple tree was full-grown and 16 apples that belonged to that tree made new 16 trees, therefore there were 17 trees.

In fourth case, we have that at year 45 the first apple tree died, so it doesn’t count anymore.

题意:
1.0年时有一颗树0岁
2.树在10,20,30,40岁时各生16,9,4,1棵0岁的小树
3.树在45岁时死亡
求n年时有多少颗活着的树。
思路:
兔子繁殖问题,不考虑死亡:https://blog.csdn.net/Wen_Yongqi/article/details/83757775
如果考虑死亡,就要表示出来每个年龄当前的个数。
对于这道题,设f(n,i):n年时年龄为i的个数。
f(n,0)=16f(n-1,9)+9f(n-1,19)+4f(n-1,29)+1f(n-1,39)
f(n,i)=f(n-1,i-1)
开一个45*45的矩阵,用矩阵快速幂就行了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

ll n;
struct mat{
    ll a[45][45];
};

mat mul(mat x,mat y,int s1,int s2,int s3)
{
    mat res={0};
    for(int i=0;i<s1;i++)
        for(int j=0;j<s3;j++)
            for(int k=0;k<s2;k++)
                res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
    return res;
}

ll pow(ll n)
{
    mat res={0},c={0};  
    for(int i=0;i<45;i++)res.a[i][i]=1;
    c.a[0][9]=16;c.a[0][19]=9;c.a[0][29]=4;c.a[0][39]=1;
    for(int i=1;i<45;i++)c.a[i][i-1]=1;
    while(n)
    {
        if(n&1)res=mul(res,c,45,45,45);
        c=mul(c,c,45,45,45);
        n>>=1;
    }
    
    ll ans=0;
    mat init={0};
    init.a[0][0]=1;
    mat ret=mul(res,init,45,45,1);
    for(int i=0;i<45;i++)ans=(ans+ret.a[i][0])%mod;
    return ans;
}

int main()
{
    cin>>n;
    cout<<pow(n)<<endl;
    return 0;
}

实际上,这道题矩阵开一个5*5的就够了,因为这题的特殊性在于,对于任意一个时刻,如果0~9内存在年龄为i的,那么所有的年龄中,只可能有i,10+i,20+i,30+i,40+i这5个年龄存在树。这是为什么呢?我们从一开始推,第0年只有1个0岁,第1 ~ 9年只有1个i岁,第10年时候有10岁和0岁,之后类似,所有树恰好相差0岁或者10岁,20岁,30岁,40岁。
那么我们以10年为一个单位,f(0):0岁的个数,f(1):10岁的个数,…f(40):40岁的个数,
进行m=(n-n%10)/10次幂,然后根据n%10是否<=4来决定是否把40岁的算进答案里。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

ll n;
struct mat{
    ll a[5][5];
};

mat mul(mat x,mat y)
{
    mat res={0};
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            for(int k=0;k<5;k++)
                res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
    return res;
}

ll pow(ll n)
{
    mat res={0},c={0};  
    for(int i=0;i<5;i++)res.a[i][i]=1;
    c.a[0][0]=16;c.a[0][1]=9;c.a[0][2]=4;c.a[0][3]=1;
    for(int i=1;i<5;i++)c.a[i][i-1]=1;
    ll m=(n-n%10)/10;
    while(m)
    {
        if(m&1)res=mul(res,c);
        c=mul(c,c);
        m>>=1;
    }
    ll ans=0;
    ll A=res.a[0][0],B=res.a[1][0],C=res.a[2][0],D=res.a[3][0],E=res.a[4][0];
    ans=(A+B+C+D)%mod;
    if(n%10<=4)ans=(ans+E)%mod;
    return ans;
}

int main()
{
    cin>>n;
    cout<<pow(n)<<endl;
    return 0;
}