【快速幂】 CodeForces 185A Plant

点击打开链接

△-> 3*△ +1*▽

▽-> 1*△ +3*▽

所以 a[i][0]=a[i-1][0]*3+a[i-1][1];

a[i][1]=a[i-1][0]+a[i-1][1]*3

构造个矩阵

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
typedef long long  LL;
const int MAXN = 191;
const int MAXM = 11111;
const int INF = 0x3f3f3f3f;
const LL mod =1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
LL n;
struct mat
{
    LL s[4][4];
};
void operator*=( mat &a,const mat &b)
{
    static mat c;
    int i,j,k;
    memset(c.s,0,sizeof(c));
    for(k=0; k<2; k++)
        for(i=0; i<2; i++)
            for(j=0; j<2; j++)
                c.s[i][j]+=a.s[i][k]*b.s[k][j]%mod,c.s[i][j]%=mod;
    for(i=0; i<2; i++)
        for(j=0; j<2; j++)
            a.s[i][j]=c.s[i][j];
}
mat ans,t;

int main()
{
    cin>>n;
    t.s[0][0]=3;
    t.s[0][1]=1;
    t.s[1][0]=1;
    t.s[1][1]=3;
    cler(ans.s,0);
    ans.s[0][0]=ans.s[1][1]=1;
    for(LL i=n; i; i=i>>1,t*=t)
        if(i&1) ans*=t;
    cout<<ans.s[0][0]<<endl;
    return 0;
}


△-> 3*△ +1*▽
△-> 3*△ +1*▽
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值