ICPC2017网络赛(沈阳)number number number

来源:互联网 发布:知几是什么意思 编辑:程序博客网 时间:2024/05/11 23:23

number number number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 99    Accepted Submission(s): 64


Problem Description
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
 

Source
2017 ACM/ICPC Asia Regional Shenyang Online
 

Recommend
liuyiding
 

矩阵快速幂,通过计算发现前几组数据是4,12,33,88等

而Fn数列前几项为 0,1,1,2,3,5,8,13,21,34,55,89............发现规律F[2*n+4]-1  就是我们要找的公式



代码如下:

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;typedef long long ll;struct Matrix{    ll matrix[5][5];};int n;//矩阵的阶数const int mod=998244353;void init(Matrix &res){    memset(res.matrix,0,sizeof(res.matrix));    for(int i=0;i<n;i++)        res.matrix[i][i]=1;}Matrix multiplicative(Matrix a,Matrix b){    Matrix res;    for(int i = 0 ; i < n ; i++){        for(int j = 0 ; j < n ; j++){            res.matrix[i][j]=0;            for(int k = 0 ; k < n ; k++){                res.matrix[i][j] = (res.matrix[i][j]%mod+a.matrix[i][k]*b.matrix[k][j]%mod)%mod;            }        }    }    return res;}Matrix pow(Matrix mx,ll m){    Matrix res,base=mx;    init(res);    while(m)    {        if(m&1)            res=multiplicative(res,base);        base=multiplicative(base,base);        m>>=1;    }    return res;}int main(){    Matrix mx,nx;    ll m,ans;    ll k;    while(~scanf("%lld",&k))    {        n=2;        m=2*k+4;        mx.matrix[0][0]=mx.matrix[0][1]=mx.matrix[1][0]=1;        mx.matrix[1][1]=0;        nx=pow(mx,m-1);        ans=(nx.matrix[0][0]*0+nx.matrix[0][1]*1)%mod;        if(ans==0)            printf("%lld\n",mod-1);        else            printf("%lld\n",ans-1);    }    return 0;}