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

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;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/83045429
今日推荐