2017 ACM/ICPC Asia Regional Shenyang Online(1005)

问题描述:

We define a sequence  F

  F0=0,F1=1 ;
  Fn=Fn1+Fn2 (n2) .

Give you an integer  k , if a positive number  n  can be expressed by

n=Fa1+Fa2+...+Fak  where  0a1a2ak

 this positive number is  mjfgood . Otherwise, this positive number is  mjfbad .

 Now, give you an integer  k , you task is to find the minimal positive  mjfbad  number.
The answer may be too large. Please print the answer modulo 998244353.

Input

There are about 500 test cases, end up with EOF.
Each test case includes an integer  k  which is described above. ( 1k109 )

Output

For each case, output the minimal  mjfbad  number mod 998244353.

Sample Input

1

Sample Output

4

问题描述:题目给我们一个数k,让我们求出k个斐波拉契数不能组成的最小的数.

题目分析:通过自己手动模拟,我们求出当

k=1    4      F(5)-1

k=2    12    F(7)-1

k=3    33    F(9)-1

不难发现,答案就是F(5+(k-1)*2)-1 % mod,然后矩阵快速幂

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;

const ll mod=998244353;

struct matrix
{
    ll f[3][3];
    matrix operator*(const matrix &a) const {
        matrix res;
        for (int i=1;i<=2;i++) {
            for (int j=1;j<=2;j++) {
                res.f[i][j]=0;
                for (int k=1;k<=2;k++)
                    res.f[i][j]=(res.f[i][j]+f[i][k]*a.f[k][j]);
                res.f[i][j]=(res.f[i][j]%mod+mod)%mod;
            }
        }
        return res;
    }
}a,b;

matrix fast_pow(matrix base,ll k)
{
    matrix ans=base;
    while (k) {
        if (k&1)
            ans=ans*base;
        base=base*base;
        k>>=1;
    }
    return ans;
}
void init()
{
    b.f[1][1]=b.f[1][2]=1;
    b.f[2][1]=1,b.f[2][2]=0;
    a.f[1][1]=1;
    a.f[2][1]=0;
}
int main()
{
    ll k;
    while (scanf("%lld",&k)!=EOF) {
        k=5+(k-1)*2;
        init();
        struct matrix cur,ans;
        cur=b;
        cur=fast_pow(b,k-2);//我的矩阵写法不一样,所以减了2
        ans=cur*a;
        printf("%lld\n",(ans.f[1][1]-1)%mod);
    }
    return 0;
}










 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值