Codeforces 185 A Plant

原创 2016年08月29日 19:23:37
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 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);
        }
}

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

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 #185 A Plant(矩阵快速幂+递推)

题目地址:http://codeforces.com/problemset/problem/185/A 通过这个题终于找回了点找递推公式的信心。。TAT。。 首先,第n个图形的上方,左下方,右下方的三...

Codeforces Round #118 (Div. 2) C. Plant (找规律)

题目链接:点击打开链接 问你第n个大三角形中有几个小三角形是朝上的,每个朝上的小三角形下一年会变成三个朝上一个朝下,每个向下的小三角下年会变成三个朝下一个朝上。 我们可以设第i年朝上的是ai个朝下...
  • CillyB
  • CillyB
  • 2016年11月19日 15:18
  • 334

Codeforces Round #118 (Div. 2)-C-Plant(构造矩阵)

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

【水分配】codeforces 185B (#118 D)- Mushroom Scientists

这使我有史以来过的第一道最奇怪的
  • okcd00
  • okcd00
  • 2014年08月03日 15:56
  • 679

codeforces185D.Visit of the Great

http://codeforces.com/contest/185/problem/D 题意: 求LCM(k^(2^l)+1, k^(2^(l+1)) + 1, k^(2^(l+2)) + 1, ....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces 185 A Plant
举报原因:
原因补充:

(最多只允许输入30个字)