# Codeforces 185 A Plant

A. Plant
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 cincout 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.

f(n)=3*f(n-1)+g(n-1)-f(n-1)=2*f(n-1)+g(n-1);

g(n)=4*g(n-1);

#include<stdio.h>
#include<iostream>
#define mod 1000000007
using namespace std;

struct mat
{
long long int m[2][2];
}base;

mat mulmat(mat a,mat b);
mat fast_mod(mat base,long long int n);

int main()
{
long long int n;
while(scanf("%I64d",&n)!=EOF)
{
base.m[0][0]=2;
base.m[0][1]=1;
base.m[1][0]=0;
base.m[1][1]=4;

if(n==0)  printf("1\n");
else
{

mat ans;
ans=fast_mod(base,n-1);
long long int x=((ans.m[0][0]*3)%mod+(ans.m[0][1]*4)%mod)%mod;
printf("%I64d\n",x);
}
}
return 0;
}

mat mulmat(mat a,mat b)
{
int i,j,k;
mat c;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
c.m[i][j]=0;
for(k=0;k<2;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j]%mod)%mod;
}
return c;
}

mat fast_mod(mat base,long long int n)
{
mat E,p;
E.m[0][0]=1;
E.m[0][1]=0;
E.m[1][0]=0;
E.m[1][1]=1;
if(n==0)  return E;
else if(n%2==1)
return mulmat(base,fast_mod(base,n-1));
else
{
p=fast_mod(base,n/2);
return mulmat(p,p);
}
}

• 本文已收录于以下专栏：

## Codeforces 185A Plant

Codeforces Round #118 (Div. 1) A. Plant time limit per test 2 seconds memory limit per te...

## codeforces 185A. Plant（规律）

codeforces 185A. Plant http://codeforces.com/problemset/problem/185/A 问经过N年后，向上的三角形的个数。 第一种解法...

## codeforces 185A Plant(矩阵快速幂)

A. Plant time limit per test 2 seconds memory limit per test 256 megabytes input s...

## CodeForces 185A. Plant（矩阵快速幂）

CodeForces 185A. Plant（矩阵快速幂）

## CodeForces 185A. Plant（矩阵快速幂） 构造

Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)