Codeforces 185 A Plant

来源:互联网 发布:面向对象编程步骤 编辑:程序博客网 时间:2024/06/05 08:28
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.

题目模型(和题目内容不同):给出一个三角形,然后再在三角形上增加三角形,输入n,求第n个图形向上三角形的个数。

题目类型:快速幂。

思路:我是用矩阵快速幂做的,方法过程有点复杂。f(n)为第n个图形向上三角形的个数。g(n)为第n个图形三角形 的个数。

向上的三角形:会产生3个向上的三角形

向下的三角形:会产生1个向上的三角形

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 1000000007using 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);        }}

这道题貌似公式推的简单一点可以用裸的快速幂做。百度了一下,公式为f(n)= (1+2n)*2n-1.


0 0
原创粉丝点击