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

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
#include 
#include 
#include 
#include 
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const LL MOD = 998244353;
struct node
{
    LL m[2][2];
} ans,base;
node multi(node a,node b)
{
    node tmp;
    mem(tmp.m,0);
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            for(int k=0; k<2; k++)
                tmp.m[i][j]=(tmp.m[i][j]+a.m[i][k]*b.m[k][j]%MOD)%MOD;
    return tmp;
}
LL fast_mod(LL n)
{
  base.m[0][0]=base.m[0][1]=base.m[1][0]=1;
  base.m[1][1]=0;
  ans.m[0][0]=ans.m[1][1]=1;
  ans.m[0][1]=ans.m[1][0]=0;
  while(n)
  {
     if(n&1)
     {
        ans=multi(ans,base);
     }
     base=multi(base,base);
     n>>=1;
  }
  return ans.m[0][1];
}
int main()
{
    LL n;
    while(~scanf("%lld",&n))
    {
       printf("%lld\n",fast_mod(n*2+3)-1);
    }
    return 0;
}

你可能感兴趣的:(number number number HDU - 6198(矩阵快速幂))