HDU 6198number number number(矩阵快速幂)

来源:互联网 发布:网络信息安全的定义 编辑:程序博客网 时间:2024/06/05 03:30

number number number

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


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
想法:
找第(4+2*(n-1))个斐波拉契数列,矩阵快速幂。
代码:
#include <stdio.h>#include <algorithm>using namespace std;typedef long long ll;const ll mod=998244353;ll A[2][2],B[2][2],T[2][2];void pow(ll n)//求第n个的斐波拉契数{    if(n==0)    {        //for(int i=0;i<2;i++)            //for(int j=0;j<2;j++)               // B[i][j]=(i==j);        B[0][0]=1;B[0][1]=0;B[1][0]=0;B[1][1]=1;        return;    }    if(n&1)    {        pow(n-1);        for(ll i=0;i<2;i++)            for(ll j=0;j<2;j++)            {                T[i][j]=0;                    for(ll k=0;k<2;k++)                        T[i][j]=(T[i][j]+A[i][k]*B[k][j])%mod;            }       for(ll i=0;i<2;i++)            for(ll j=0;j<2;j++)            {                B[i][j]=T[i][j];            }    }    else    {        pow(n/2);        for(ll i=0;i<2;i++)            for(ll j=0;j<2;j++)            {                T[i][j]=0;                for(ll k=0;k<2;k++)                    T[i][j]=(T[i][j]+B[i][k]*B[k][j])%mod;            }       for(ll i=0;i<2;i++)          for(ll j=0;j<2;j++)            {                B[i][j]=T[i][j];            }    }}int main(){  ll n;  A[0][0]=1; A[0][1]=1;  A[1][0]=1; A[1][1]=0;  while (scanf("%lld", &n)!=EOF)  {        ll ans;        pow(4+2*(n-1));        ans=B[0][0]%mod;        if(ans<0) ans+=mod;        //printf("%lld\n",B[0][0]);        printf("%lld\n",ans-1);  }  return 0;}