Hdu 6198 number number number 矩阵快速幂+找规律

53 篇文章 0 订阅

We define a sequence FF :

⋅⋅ F0=0,F1=1F0=0,F1=1 ;
⋅⋅ Fn=Fn−1+Fn−2 (n≥2)Fn=Fn−1+Fn−2 (n≥2) .

Give you an integer kk , if a positive number nn can be expressed by
n=Fa1+Fa2+...+Fakn=Fa1+Fa2+...+Fak where 0≤a1≤a2≤⋯≤ak0≤a1≤a2≤⋯≤ak , this positive number is mjf−goodmjf−good . Otherwise, this positive number is mjf−badmjf−bad .
Now, give you an integer kk , you task is to find the minimal positive mjf−badmjf−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 kk which is described above. (1≤k≤1091≤k≤109 )

Output

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

Sample Input

1

Sample Output

4

 所求结果的第k项等于斐波那契数列的第2*k+3项-1;

斐波那契数列可以通过矩阵快速幂求解....

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int mod=998244353;
int k;
struct mat
{
    ll a[3][3];
};
mat mul (mat a,mat b)
{
    mat c;
    for (int i=0;i<2;i++)
    {
        for (int j=0;j<2;j++)
        {
            c.a[i][j]=0;
            for (int k=0;k<2;k++)
            {
                c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;
            }
        }
    }
    return c;
}
mat Fast (mat a,int b)
{
    mat c;
    memset (c.a,0,sizeof(c.a));
    for (int i=0;i<2;i++)
        c.a[i][i]=1;
    while (b)
    {
        if(b&1)
        {
            c=mul(c,a);
        }
        a=mul(a,a);
        b>>=1;
    }
    return c;
}
int main()
{
    while (scanf("%d",&k)!=EOF)
    {
        mat a;
        a.a[0][0]=a.a[0][1]=a.a[1][0]=1;
        a.a[1][1]=0;
        if(k==1)
        {
            printf("4\n");
        }
        else if(k==2)
        {
            printf("12\n");
        }
        else
        {
            mat ans=Fast(a,2*k+2);
            printf("%lld\n",ans.a[0][0]-1);
        }

    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值