HDU

来源:互联网 发布:下载itools软件 编辑:程序博客网 时间:2024/06/06 17:05

number number number


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
 

解题思路:矩阵快速幂求斐波那契数列,队友A了



#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;const int mod=998244353;typedef long long int ll;typedef vector<long long> vec;typedef vector<vec> mat;ll n;mat mul(mat A,mat B){    mat C(A.size(),vec(B[0].size()));    for(int i=0;i<A.size();i++)    {        for(int j=0;j<B.size();j++)        {            for(int k=0;k<B[0].size();k++)            {                C[i][j]+=(A[i][k]*B[k][j])%mod;                C[i][j]%=mod;            }        }    }    return C;}mat pow(mat A,ll n){    mat B(A.size(),vec(A.size()));    for(int i=0;i<A.size();i++)    {        B[i][i]=1;    }    while(n)    {        if(n&1)        {            B=mul(B,A);        }        A=mul(A,A);        n>>=1;    }    return B;}void slove(ll n){    mat A(2,vec(2));    A[0][0]=1; A[0][1]=1;    A[1][0]=1; A[1][1]=0;    A=pow(A,n);    printf("%lld\n",A[1][0]-1);}int main(){    while(scanf("%lld",&n)!=EOF)    {        slove(2*(n+1)+1);    }    return 0;}









原创粉丝点击