Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.

Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
The first line contains a single integer n (0 ≤ n ≤ 1018) — the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64dspecifier.
Print a single integer — the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
1
3
2
10
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll Mod=1000000007;
struct Matrix
{
ll ma[2][2];
Matrix(){
memset(ma,0,sizeof(ma));
}
void init(int a,int b,int c,int d){
ma[0][0]=a;ma[0][1]=b;
ma[1][0]=c;ma[1][1]=d;
}
Matrix operator * (const Matrix &m){
Matrix res;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
res.ma[i][j]=(res.ma[i][j]+ma[i][k]*m.ma[k][j])%Mod;
return res;
}
};
Matrix quick_pow(Matrix m,ll n)
{
if(n==1)return m;
Matrix res=quick_pow(m,n/2);
Matrix ans=res*res;
if(n%2==1)
ans=ans*m;
return ans;
}
int main()
{
ll n;
scanf("%lld",&n);
if(n==0){printf("1\n");return 0;}
Matrix m,ans;
m.init(3,1,1,3);
ans=quick_pow(m,n);
printf("%lld\n",ans.ma[0][0]);
return 0;
}

这篇博客探讨了一个有趣的数学问题,关于一个三角形植物每年以特定方式分裂的情况。在n年之后,求向上生长的三角形植物的数量对10^9 + 7取模的结果。博客内容涉及递推关系和大型整数运算,特别是使用矩阵快速幂来高效解决这个问题。
1307

被折叠的 条评论
为什么被折叠?



