HDU6198 矩阵快速幂

简略题意:问 k 个斐波那契数不能组成的最小的数是多少。

DP打个表,发现规律很显然是f(2k+3)1

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const LL mod = 998244353;
const int maxn = 2;

struct Matrix{
    int a[maxn][maxn];
    Matrix(){
        memset(a, 0, sizeof a);
    }
    void Init(){
        for(int i=0; i<maxn; i++)
            for(int j=0; j<maxn; j++)
                a[i][j] = (i == j);
    }
    Matrix operator + (const Matrix & B) const{
        Matrix C;
        for(int i=0; i<maxn; i++)
            for(int j=0; j<maxn; j++)
            C.a[i][j] = (a[i][j] + B.a[i][j])%mod;
        return C;
    }
    Matrix operator * (const Matrix & B) const{
        Matrix C;
        for(int i=0; i<maxn; i++)
            for(int j=0; j<maxn; j++)
                for(int k=0; k<maxn; k++){
            C.a[i][j] = (C.a[i][j] + (1LL*a[i][k] * B.a[k][j])%mod)%mod;
        }
        return C;
    }
    Matrix operator ^ (const LL &t)const{
        Matrix A = (*this), res;
        res.Init();
        LL p = t;
        while(p){
            if(p & 1) res = res * A;
            A = A * A;
            p >>= 1;
        }
        return res;
    }
} mat;

Matrix trans;

int f(int x) {
    if(x == 0) return 0;
    if(x == 1) return 1;
    Matrix ans;
    ans.a[0][0] = 1, ans.a[0][1] = 0;
    ans.a[1][0] = 0, ans.a[1][0] = 0;
    return ((trans^(x-1))*ans).a[0][0];
}

int main() {
    trans.a[0][0] = 1, trans.a[0][1] = 1;
    trans.a[1][0] = 1, trans.a[1][1] = 0;
    int n;
    while(~scanf("%d", &n)) {
        printf("%lld\n", ((1LL*f(2*n+3))-1+mod)%mod);
    }
    return 0;
}

附上暴力程序

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1100;

int f[111];
int dp[11111111];

int main() {
    f[0] = 0, f[1] = 1;
    for(int i = 2; i <= 30; i++)
        f[i] = f[i-1] + f[i-2], cout<<i<<" "<<f[i]<<endl;
    dp[0] = 1;
    for(int k = 1; k <= 9;k++) {
        for(int j = 10000; j >= 0; j--){
            for(int k = 0; k < 30; k++) {
                dp[j+f[k]] |= dp[j];
            }
        }
        int p = 0;
        while(1) {
            if(!dp[p]) {
                cout<<k<<" "<<p<<endl;
                break;
            }
            p++;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值