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.
Input
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.
Output
Print a single integer — the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
input
1
output
3
input
2
output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one
==================editorial====================
let f(n)= number of upward triangle at n
let g(n)= number of downward triangle at any time n
base case:
f(0)=1;
g(0)=0;
now we can easily find with some example that
f(n)=3*f(n-1)+g(n-1)
g(n)=3*g(n-1)+f(n-1)
so
[ f(n-1),g(n-1)] * matrix=[f(n),g(n)]
note [ f(n-1),g(n-1)] is a 2*1 matrix not 1*2 (we can solve it by considering it as 1*2 than matrix wil be change )
mattrix=[{3,1},{1,3}]
========================code================
/* deepak gautam
codechef - algorithmist2 ,codeforce - gautam27
topcoder- gautam_27 ,spoj - nexus_d
hackerearth-deepak.gautam.127648 , hackerrank- deepakgautam2701
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define ff first
#define ss second
#define mp make_pair
#define ph push_back
#define debug 0
#define mod 1000000007
int size=2;
lli big_mul(lli a,lli b)
{
if(a==1) return b;
else
{
if(a%2==1)
{
return ((big_mul(b,a/2)*2)%mod+b)%mod;
}
else return ((big_mul(b,a/2)*2)%mod);
}
}
void mul(lli mat1[2][2],lli mat2[2][2],lli ret[2][2])
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
ret[i][j]=0;
for(int k=0;k<size;k++)
{
ret[i][j]=(ret[i][j]+big_mul(mat1[i][k],mat2[k][j]))%mod;
//s cout<<ret[i][j]<<endl;
while(ret[i][j]<0)
{
ret[i][j]+=mod;
}
ret[i][j]%=mod;
}
}
}
}
void rais(lli base[][2], lli n,lli ans[][2])
{
// cout<<"rase "<<n<<endl;
ans[0][0]=1;ans[1][0]=0;ans[0][1]=0;ans[1][1]=1;// identity
lli temp[2][2];
while(n>0)
{
if(n%2)
{
/// cout<<"seting ans ";
mul(ans,base,temp);
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
ans[i][j]=temp[i][j];
}
}
}
mul(base,base,temp);
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
base[i][j]=temp[i][j];
}
}
n/=2;
}
}
lli fibo(lli n)
{
lli base[][2]={{3,1},{1,3}};
if(n==1) return base[0][0];
lli ans[2][2];
rais(base,n,ans);
lli f1=1;
lli g1=0;
return ans[0][0];
}
int main()
{
lli n;
cin>>n;
// cout<<n<<endl;
if(n==0)
cout<<1<<endl;
else cout<<fibo(n);
}
No comments:
Post a Comment