Codeforces Round #118 (Div. 2) C. Plant

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

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 %I64d specifier.

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.

有两种做法:第一种:找规律,一般这么大的数,都是有规律的,我们可以将前几项写出来

              向上a     向下b

n=1:         3        1

n=2:     3*3+1=10    1*3+3=6

n=3     10*3+6=36    6*3+10=28

...........

n项:an=3*(a(n-1))+b(n-1),bn=3*(b(n-1))+a(n-1)

所以我们可以推导公式:an-bn=2(a(n-1)-b(n-1)我们还知道,每次三角形都会增加4个所以得到an+bn=4^n

两式相加得到:2*an=2^2*n+2(an-1-bn-1),又因为a(n)-bn=2^n-1;

所以化简得到通项公式:an=2^(n-1)+2^(2*n-1);

但是数据较大,我们可以使用快速幂

代码:

#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1010;
const int inf=0x3f3f3f;
const int mod=1e9+7;
#define T() int test;scanf("%d",&test);while(test--)
ll powmi(ll a,ll n)
{
    ll ans=1,res=a%mod;
    while(n!=0)
    {
        if(n&1)
            ans=ans*res%mod;
        res=res*res%mod;
        n>>=1;
    }
    return ans%mod;
}
int main()
{
    ll n;
    while(scanf("%I64d",&n)!=EOF)
    {
        if(n==0)
           printf("1\n");
        else
            printf("%I64d\n",(powmi(2,n-1)+powmi(2,2*n-1))%mod);
    }
    return 0;
}

第二种方法:矩阵快速幂

我们可以设前一个为[x,y]*[3,1]

                                 [y,x] [1,3]

每一个为:3*x+y

所以可以使用矩阵快速幂的方法

代码:

#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1010;
const int inf=0x3f3f3f;
const int mod=1e9+7;
const int N=2;
#define T() int test;scanf("%d",&test);while(test--)
void multi(ll a[][N],ll b[][N])
{
    ll c[N][N];

    memset(c,0,sizeof c);
    for(ll k=0; k<2; k++)
    {
        for(ll i=0; i<2; i++)
        {
            for(ll j=0; j<2; j++)
            {
                c[k][i]+=a[k][j]*b[j][i];
                c[k][i]%=mod;
            }
        }
    }
    for(ll i=0; i<2; i++)
        for(ll j=0; j<2; j++)
            a[i][j]=c[i][j];
}
ll res[N][N];
void Pow(ll a[][N],ll n)
{
    memset(res,0,sizeof res);
    for(ll i=0; i<N; i++)
        res[i][i]=1;
    while(n)
    {
        if(n&1)
            multi(res,a);
        multi(a,a);
        n>>=1;
    }
    printf("%I64d\n",res[0][0]%mod);
}
int main()
{
    ll n;
    while(scanf("%I64d",&n)!=EOF)
    {
        ll a[2][2];
        a[0][0]=a[1][1]=3;
        a[0][1]=a[1][0]=1;
        Pow(a,n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lee371042/article/details/81408491