0
点赞
收藏
分享

微信扫一扫

Plant CodeForces - 185A


我们设 第n个三角形中 上三角形数量为an 下三角形数量为bn

不难找出这样的递推关系 an = 3*an-1 + bn-1 , bn = an-1 + 3*bn-1

而找规律的题一般可用矩阵快速幂解决 试着凑出这样一个式子

| 3 1 | * | an-1 0 | = | 3*an-1 + bn-1 0 |  ==> | an 0 | = ( | 3 1 | ^ n ) * | 1 0 |

| 1 3 |   | bn-1 0 |    | an-1 + 3*bn-1 0 |          | bn 0 |      | 1 3 |            | 0 0 |

然后就是套模板了

 

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007

ll pre[2][2],ans[2][2],mat[2][2];
ll n;

void quickpow();
void mul(ll a[][2],ll b[][2],ll c[][2]);

int main()
{
pre[0][0]=1,pre[0][1]=0,pre[1][0]=0,pre[1][1]=0;
while(scanf("%lld",&n)!=EOF)
{
quickpow();
mul(ans,pre,pre);
printf("%lld\n",ans[0][0]);
}
return 0;
}

void quickpow()
{
ans[0][0]=1,ans[0][1]=0,ans[1][0]=0,ans[1][1]=1;
mat[0][0]=3,mat[0][1]=1,mat[1][0]=1,mat[1][1]=3;
while(n>0)
{
if(n%2==1)
{
mul(ans,mat,ans);
}
mul(mat,mat,mat);
n/=2;
}
return;
}

void mul(ll a[][2],ll b[][2],ll c[][2])
{
ll t[2][2];
memset(t,0,sizeof(t));
int i,j,k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
t[i][j]+=((a[i][k]%M)*(b[k][j]%M))%M;
t[i][j]%=M;
}
}
}
memcpy(c,t,sizeof(t));
return;
}

 

 

 


举报

相关推荐

0 条评论