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

来源:互联网 发布:安索夫矩阵实例 编辑:程序博客网 时间:2024/04/29 04:05

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;}
阅读全文
0 0
原创粉丝点击