**
cf Apple trees——递推要记得想想矩阵快速幂
**
先上原题:
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 ≤ 10^15) - 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
input
9
output
1
input
10
output
17
input
44
output
77328
input
45
output
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.
题意
最初有一棵苹果树,每十年会播撒一次种子(保证掉下的种子都能落地马上变成苹果树,以同样的规律成长和播撒种子),对于每棵树,第10年会播撒16颗种子,第20年会播撒9颗种子,第30年会播撒4颗种子,第40年会播撒1颗种子,第45年树死亡。问第n年一共有多少棵苹果树。
思路
很明显这是个递推,且前面递推出的数据在后面的计算中用得到。比如:
第k年 | 0岁树的数量 | 10岁的树数量 | 20岁的树数量 | 30岁的树数量 | 40岁的树数量 |
---|---|---|---|---|---|
0 | 1 | 0 | 0 | 0 | 0 |
10 | 16 | 1 | 0 | 0 | 0 |
20 | 16*16+9 | 16 | 1 | 0 | 0 |
再看看数据,1e15。暴力递推就别想了,矩阵快速幂了解一下 ~
(我不会在博客里搞矩阵啊苟命)
原本初始矩阵如下:
1
0
0
0
本来想的是这样的一个递推矩阵:
16 9 4 1
1 0 0 0
0 1 0 0
0 0 1 0
但是发现在树第45年的时候答案是只算活着的树的,所以得再加一行一列来减去死去的树的数量。新矩阵如下:
16 9 4 1 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
那么初始矩阵也要相应变成:
1
0
0
0
0
这样就能推出每整十年年龄多少的树有多少颗了,注意年数个位数的特判。
代码
#include <stdio.h>
#include <math.h>
#include <iostream>
#include