hdu6198 number number number(找规律+矩阵快速幂)

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≤10^9)

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

Sample Input
1

Sample Output
4

大致题意:如果从斐波那契数列中任选k个数相加(可重复选),都不能得到某个数,则称这个数为坏数,现在告诉你k的值,让你找出最小的坏数。

思路:暴力地去求下前几种情况,可以发现当k=1时,答案为5-1=4,当k=2时答案为13-1=12,当k=3时,答案为34-1=33。。。假设5为斐波那契数列的第一项,8为第二项,f(x)表示第x项斐波那契的值,那么答案为f(2*k-1)-1。然后用矩阵快速幂去求第2*k-1项斐波那契数列的值。

代码如下

#include  
#include 
#include 
using namespace std; 
#define LL long long 
const int mod= 998244353; 
struct matrix
{
    LL x[2][2];
};
matrix mutimatrix(matrix a,matrix b)
{
    matrix temp; 
    memset(temp.x,0,sizeof(temp.x));    
    for(int i=0;i<2;i++)
    for(int j=0;j<2;j++)
    for(int k=0;k<2;k++)
    {
        temp.x[i][j]+=a.x[i][k]*b.x[k][j];
        temp.x[i][j]%=mod;
    }
    return temp;
}

matrix k_powmatrix(matrix a,LL n)//矩阵快速幂
{
    matrix temp;
    memset(temp.x,0,sizeof(temp.x));
    for(int i=0;i<2;i++)
    temp.x[i][i]=1;

    while(n)
    {
        if(n&1)
        temp=mutimatrix(temp,a);

        a=mutimatrix(a,a);
        n>>=1;
    }
    return temp;
} 


int main()
{

    LL k;

    while(scanf("%lld",&k)!=EOF)
    {
        matrix st;
        //memset(st.x,0,sizeof(st.x));
        st.x[0][0]=1;
        st.x[0][1]=1;
        st.x[1][0]=1;
        st.x[1][1]=0;


        matrix init;//初始矩阵
        //memset(init.x,0,sizeof(init.x));
        init.x[0][0]=8;
        init.x[0][1]=5;


        st=k_powmatrix(st,2*(k-1));
        st=mutimatrix(init,st);//然后再乘上初始矩阵

        printf("%lld\n",(st.x[0][1]-1+mod)%mod);
    }
    return 0; 
} 

你可能感兴趣的:(乱搞,矩阵快速幂)