CodeForces

来源:互联网 发布:苏州大学网络教育 编辑:程序博客网 时间:2024/06/11 18:57
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.

题意: 一看题目就知道是找规律,否则10的18次方这数据不得炸了,然后写了5组大概就发现了,但是考虑怎么求,肯定不能用数组存,后来想了想最近看过的矩阵快速幂,然后用规律构建矩阵求解,不会矩阵快速幂的百度下,挺简单的意思就是将数字转化成01的串,然后进行次方的累加。如果有其他的更好的方法,希望告诉我,谢谢。

1  1 0   3

2  3 1   10

3 10 6  36

4 36 28 136   多写几组发现要求的那个等于下一组向上的三角形的个数  ,语言表达能力欠缺哈,手写下~

#include<stdio.h>#include<iostream> #include <algorithm>#include<string.h>#include<math.h>#include<queue>#include<set>#define LL long long#define INf 0x3f3f3f3fusing namespace std;long long temp[2][2];long long temp1[2][2];void m(long long a[][2],long long b[][2]){long long temp2[2][2];memset(temp2,0,sizeof(temp2));for(int i=0;i<2;i++){for(int j=0;j<2;j++){for(int k=0;k<2;k++){temp2[i][j]+=(a[i][k]*b[k][j])%1000000007;} temp2[i][j]=temp2[i][j]%1000000007;}}for(int i=0;i<2;i++)for(int j=0;j<2;j++)b[i][j]=temp2[i][j];}void ju(long long a){temp1[0][0]=temp1[1][1]=1;temp1[0][1]=temp1[1][0]=0;while(a){if(a&1)m(temp,temp1);//==temp1m(temp,temp);a>>=1;}}int main(){long long a;while(cin>>a){temp[0][0]=temp[1][1]=3;temp[0][1]=temp[1][0]=1;ju(a);cout<<temp1[0][0]<<endl;}return 0;} 




原创粉丝点击