您的位置:首页 > 其它

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

2017-09-20 08:08 399 查看

链接:

  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+...+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

题意:

  找出最小的不能由k个斐波那契数加和而得的数。

思路:

  找规律发现这个数是斐波那契第2k+3项-1,也就是ans=F2k+3−1。

实现:

#include <cstdio>
#include <cstring>
const int maxn = 4, INF = 0x3f3f3f3f, mod = 998244353;
struct Matrix{
long long m[maxn][maxn];
Matrix(int a=0) {
memset(m,0,sizeof(m));
if(a == 1) for(int i=0 ; i<maxn ; i++) m[i][i] = 1;
}
Matrix operator * (const Matrix tmp) const {
Matrix ret;
long long x;
for(int i=0 ; i<maxn ; i++) {
for(int j=0 ; j<maxn ; j++) {
x = 0;
for(int k=0 ; k<maxn ; k++)
x += (m[i][k] * tmp.m[k][j]) % mod;
ret.m[i][j] = x % mod;
}
}
return ret;
}
Matrix qpow(long long n) {
Matrix ret = 1, tmp = *this;
while(n>0) {
if(n&1) ret = ret * tmp;
tmp = tmp * tmp;
n >>= 1;
}
return ret;
}
}base;
int main() {
int n;
base.m[0][0] = base.m[0][1] = base.m[1][0] = 1;
while(~scanf("%d", &n)) {
printf("%lld\n", base.qpow(n*2+2).m[0][0]-1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息