HDU 6198 number number number 题解

来源:互联网 发布:触摸交互式软件 编辑:程序博客网 时间:2024/06/16 01:09

转载请注明出处,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=Fn1+Fn2(n2).

Give you an integer k, if a positive number n can be expressed by
n=Fa1+Fa2+...+Fakwhere0a1a2ak, 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. (1k109)

Output

For each case, output the minimal mjf−bad number mod 998244353.

Sample Input

1

Sample Output

4

找规律,答案为斐波那契数列的第2n+3项再减一,n为输入的数,斐波那契第0项为0

比赛的时候用一个坑爹的公式(15[(1+52)n(152)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;}
原创粉丝点击