转载请注明出处,http://blog.csdn.net/Bule_Zst/article/details/77933566
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6198
Problem Description
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+...+Fakwhere0≤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
找规律,答案为斐波那契数列的第 2∗n+3 项再减一,n为输入的数,斐波那契第0项为0
比赛的时候用一个坑爹的公式( 15√∗[(1+5√2)n−(1−5√2)n] ),应该是WA在精度上了,其实用矩阵快速幂就可以了
代码:
// @Team : nupt2017team12
// @Author : Zst
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
#define MOD 998244353
#define CLR(a,x) memset(a,x,sizeof(a))
#define INF 0x3f3f3f3f
#define pb push_back
#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )
LL n;
struct Matrix {
LL m[2][2];
Matrix( LL a[2][2] ) {
FOR( i, 0, 1 ) {
FOR( j, 0, 1 ) {
m[i][j] = a[i][j];
}
}
}
Matrix() {
CLR( m, 0 );
}
Matrix operator*( Matrix b ) {
Matrix a = *this;
Matrix result = Matrix();
FOR( i, 0, 1 ) {
FOR( j, 0, 1 ) {
result.m[i][j] = ( a.m[i][0] * b.m[0][j] ) % MOD + ( a.m[i][1] * b.m[1][j] ) % MOD;
result.m[i][j] %= MOD;
}
}
return result;
}
};
void show( Matrix m ) {
FOR( i, 0, 1 ) {
FOR( j, 0, 1 ) {
cout<<m.m[i][j]<<" ";
}
cout<<endl;
}
}
Matrix solve( LL a ) {
LL tmp[2][2] = {
{ 1, 1 },
{ 1, 0 }
};
LL tmp2[2][2] = {
{ 1, 0 },
{ 0, 1 }
};
Matrix base = Matrix( tmp );
Matrix result = Matrix( tmp2 );
while( a ) {
if( ( a & 1 ) ) {
result = result * base;
}
base = base * base;
a = a >> 1;
}
return result;
}
int main()
{
// freopen( "5.1", "r", stdin );
while( scanf( "%lld", &n ) != EOF ) {
n = 2 * n + 3;
// 0, 1, 1, 2
Matrix m = solve( n-1 );
printf( "%lld\n", m.m[0][0]-1 );
}
return 0;
}