您的位置:首页 > 其它

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

2017-09-13 19:58 555 查看
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 <cstdio>
#include <string>
#include <string.h>
#include <iostream>
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: