HDU 6198 number number number(二进制枚举子集打表找规律+矩阵快速幂)

number number number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 875 Accepted Submission(s): 533

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,问任意你k项的斐波那契数列所不能组成的最小的数是几

思路

先用二进制枚举子集找一下前几项的值只用用到前20项斐波那契数列就够了

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
    int f[22];
    f[0]=0;
    f[1]=1;
    int maxn=1;
    for(int i=2; i<20; i++)
    {
        f[i]=f[i-1]+f[i-2];
        maxn+=f[i];
    }
    int vis[maxn+5];
    for(int w=1; w<=7; w++)
    {
        memset(vis,0,sizeof(vis));
        for(int i=0; i<(1<<20); i++)
        {
            for(int j=1; j<=w; j++)
            {
                int cnt=0;
                int sum=0;
                for(int k=0; k<20; k++)
                    if(i&(1<<k))
                    {
                        cnt++;
                        sum+=f[k];
                    }
                if(cnt==j)
                    vis[sum]=1;
            }
        }
        cout<<w<<" ";
        for(int i=0; i<=maxn; i++)
            if(vis[i]==0)
                {
                    cout<<i<<endl;
                    break;
                }
    }
    return 0;
}

得到

1 4 2 12 3 33 4 88 5 232 6 609 7 1596

在比较一下斐波那契数列就会的到答案为 f [ 2 k + 2 ] 1 ,由于k较大所以我们用矩阵快速幂优化

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 2
using namespace std;
typedef struct {
long long m[MAX][MAX];
}Matrix;
const long long mod=998244353;
Matrix P={1,1,1,0};
Matrix I={1,0,0,1};
Matrix Matrixmul(Matrix a,Matrix b)
{
    int i,j,k;
    Matrix c;
    for(i=0;i<MAX;i++)
        for(j=0;j<MAX;j++)
        {
            c.m[i][j]=0;
            for(k=0;k<MAX;k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
            }
            c.m[i][j]%=mod;
        }
        return c;
}
Matrix quickpow(long long n)
{
    Matrix m=P,b=I;
    while(n>0)
    {
        if(n%2==1)
            b=Matrixmul(b,m);
        n=n/2;
        m=Matrixmul(m,m);
    }
    return b;
}
int main()
{
    long long n;
    while(scanf("%lld",&n)!=EOF)
    {
            Matrix a;
            a=quickpow(2*n+2);
            printf("%lld\n",a.m[0][0]-1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ftx456789/article/details/80586347
今日推荐