HDU 6198 number number number 题解

转载请注明出处,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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值