codeforces 185A 矩阵快速幂

来源:互联网 发布:python class是什么 编辑:程序博客网 时间:2024/06/05 01:18

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

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

题目大意:有一个三角形,刚开始是1个up,0个down,1年后,这个三角形变成了3个up,1个down的
之后的每一年,每个三角形都会变成四个,其中三个是相同方向的,1个是相反方向的,问n年后,共有多少个三角形是up的

自己一直没搞懂究竟要循环多少次 在这里 初始状态是0 0循环n次到n-1 得到的是n-1的结果 再乘一个得到n的结果
分析 推出 x和y的关系式

正x 倒y
1 0
3 1
10 6
… …
3*x+y 3*y+x
初始状态是 x y 1 0 公式矩阵就是关系式矩阵就是
3 1
1 3 可以得出结果

优化过的矩阵快速幂
先初始化 然后jk互换 速度快1/3;

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;struct Matrix{    LL m[5][5];}A,B,I,T;int ssize=2;const int mod = 1e9+7;Matrix Mul(Matrix a,Matrix b){    Matrix c;    for(int i=1;i<=2;i++)    for(int j=1;j<=2;j++)    c.m[i][j]=0;    for(int i=1;i<=ssize;i++)    {        for(int k=1;k<=ssize;k++)        {            for(int j=1;j<=ssize;j++)            {                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);                c.m[i][j]%=mod;            }        }    }    return c;}Matrix quickpagow(LL n){    Matrix m=A,b=I;    while(n)    {        if(n&1) b=Mul(m,b);        m=Mul(m,m);        n>>=1;    }    return b;}int main(){    LL n;    while(cin>>n)    {        for(int i=1;i<=ssize;i++)        {            I.m[i][i]=1;        }        B.m[1][1]=0;        B.m[2][1]=1;        A.m[1][1]=3;        A.m[1][2]=1;        A.m[2][1]=1;        A.m[2][2]=3;        T=quickpagow(n);        T=Mul(T,B);        printf("%I64d\n",T.m[2][1] );    }}
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;struct Matrix{    LL m[5][5];}A,B,I,T;int ssize=2;const int mod = 1e9+7;Matrix Mul(Matrix a,Matrix b){    Matrix c;    for(int i=1;i<=ssize;i++)    {        for(int j=1;j<=ssize;j++)        {            c.m[i][j]=0;            for(int k=1;k<=ssize;k++)            {                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);                c.m[i][j]%=mod;            }        }    }    return c;}Matrix quickpagow(LL n){    Matrix m=A,b=I;    while(n)    {        if(n&1) b=Mul(m,b);        m=Mul(m,m);        n>>=1;    }    return b;}int main(){    LL n;    while(cin>>n)    {        for(int i=1;i<=ssize;i++)        {            I.m[i][i]=1;        }        B.m[1][1]=0;        B.m[2][1]=1;        A.m[1][1]=3;        A.m[1][2]=1;        A.m[2][1]=1;        A.m[2][2]=3;        T=quickpagow(n);        T=Mul(T,B);        printf("%I64d\n",T.m[2][1] );    }}
0 0