hdu 6172 - 打表找规律

题目链接:点击打开链接


解题思路:打表找规律发现f(n) = 4 f(n-1) + 17 f(n-2) - 12 f(n-3),然后矩阵快速幂即可


代码:

#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int mx = 1e5+10,mod = 1e9+7;
ll n;
struct data{
    ll mart[3][3];
}ma;
const data da = {1255,197,31,0,0,0,0,0,0},base = {4,1,0,17,0,1,-12,0,0};
data juzhen(data A,data B){
    data ans;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            ll c = 0;
            for(int k=0;k<3;k++)
            c = (c+A.mart[i][k]*B.mart[k][j])%mod;
            ans.mart[i][j] = c;
        }
    }
    return ans;
}
ll kuaisu(ll x){
    data ret = base;
    while(x){
        if(x&1) ma = juzhen(ma,ret);
        x >>= 1;
        ret = juzhen(ret,ret);
    }
    return (ma.mart[0][0]+mod)%mod;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
       scanf("%lld",&n);
       ma = da;
       if(n==2) puts("31");
       else if(n==3) puts("197");
       else if(n==4) puts("1255");
       else{
            n -= 4;
            printf("%lld\n",kuaisu(n));
       }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值