ICPC2017沈阳网赛1005&&HDU6198 (矩阵快速幂

number number number

Description

We define a sequence F:

⋅ F0=0,F1=1;
⋅ Fn=Fn−1+Fn−2 (n≥2).

Give you an integer k, if a positive number n can be expressed by
n=Fa1+Fa2+…+Fak where 0≤a1≤a2≤⋯≤ak, this positive number is mjf−good. Otherwise, this positive number is mjf−bad.
Now, give you an integer k, you task is to find the minimal positive mjf−bad 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. (1≤k≤109)

Output

For each case, output the minimal mjf−bad number mod 998244353.

Sample Input

1

Sample Output

4

题意

从斐波那契数列中任选k个数相加(可重复选),都不能得到某个数,则称这个数为bad,现在告诉你k的值,让你找出最小的bad数。

题解

学长真是太优秀了 随手写个公式就AC了 Orz 其实暴力打表后发现就是求Fib(2*n+3)-1

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod = 998244353;
const int N = 2;
struct node {
    LL arr[N][N];
}p;
LL n, k;

node mul(node x,node y)
{
    node ans;;
    memset(ans.arr,0,sizeof(ans.arr));
    for(int i = 0;i < 2; i++) {
        for(int j = 0;j < 2; j++) {
            for(int k = 0;k < 2; k++) {
                ans.arr[i][j] = (ans.arr[i][j]+(x.arr[i][k]*y.arr[k][j])%mod)%mod;
            }
        }
    }
    return ans;
}
node powMod(LL u, node x)
{
    node ans;
    for(int i = 0;i < 2; i++) {
        for(int j = 0;j < 2; j++) {
            if(i==j) ans.arr[i][j] = 1;
            else ans.arr[i][j] = 0;
        }
    }
    while(u) {
        if(u&1) ans = mul(ans,x);
        x = mul(x,x);
        u >>= 1;
    }
    return ans;
}
int main()
{
    int n;
    while(~scanf("%d",&n)) {
        p.arr[0][0] = 1; p.arr[0][1] = 1;
        p.arr[1][0] = 1; p.arr[1][1] = 0;
        node x = powMod(n*2+3,p);
        printf("%d\n",x.arr[0][1]-1);

    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值