【2017 ACM/ICPC Asia Regional Shenyang Online 1005】hdu 6198 number number number

10 篇文章 0 订阅

number number number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 97    Accepted Submission(s): 63


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+...+Fak where 0a1a2ak , this positive number is mjfgood . Otherwise, this positive number is mjfbad .
Now, give you an integer k , you task is to find the minimal positive mjfbad 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 mjfbad number mod 998244353.
 

Sample Input
  
  
1
 

Sample Output
  
  
4
 

【题意】:

给出一个数k,问用k个斐波那契数相加,得不到的数最小是几。

【解析】:

暴力写了下前几项,发现有规律。f[n] = f[n-1]*3 - f[n-2] + 1

然后用矩阵快速幂可以直接得出答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define mset(a,i) memset(a,i,sizeof(a))
using namespace std;
typedef long long ll;
const int mod = 998244353;
ll qpow(ll n, ll m)
{
    n %= mod;
    ll ans = 1;
    while (m)
    {
        if (m % 2)
        {
            ans = (ans * n) % mod;
        }
        m /= 2;
        n = (n * n) % mod;
    }
    return ans;
}
struct zhen
{
    int r, c; //行列始于1
    ll a[16][16];
    zhen(int R = 0, int C = 0)
    {
        memset(a, 0, sizeof(a));
        r = R;
        c = C;
    }
};
zhen operator*(zhen A, zhen B) //矩阵A*B
{
    zhen C(A.r, B.c);
    for (int i = 1; i <= A.r; i++)
        for (int j = 1; j <= B.c; j++)
            for (int k = 1; k <= A.c; k++)
            {
                C.a[i][j] += ((A.a[i][k] + mod) * (mod + B.a[k][j])) % mod;
                C.a[i][j] %= mod;
            }
    return C;
}
zhen qpow(zhen A, ll m) //方阵A的m次幂
{
    zhen ans(A.r, A.c);
    for (int i = 1; i <= A.r; i++)
    {
        ans.a[i][i] = 1;    //单位矩阵
    }
    while (m)
    {
        if (m % 2)
        {
            ans = ans * A;
        }
        A = A * A;
        m /= 2;
    }
    return ans;
}
int main()
{
    ll n;
    while (cin >> n)
    {
        if (n == 1)
        {
            puts("4");
            continue;
        }
        if (n == 2)
        {
            puts("12");
            continue;
        }
        zhen A(3, 3); //构造矩阵A
        A.a[1][1] = 3;
        A.a[1][3] = A.a[2][1] = A.a[3][3] = 1;
        A.a[1][2] = -1;
        //矩阵A构造完毕
        //所以 Xn+1 = A^(n-2) * X3;
        zhen X3(A.r, 1), X;
        X3.a[1][1] = 12;
        X3.a[2][1] = 4;
        X3.a[3][1] = 1;
        X = qpow(A, n - 2) * X3;
        ll ans = X.a[1][1];
        cout << ans % mod << endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值