zoukankan      html  css  js  c++  java
  • hdu6198 number number number(递推公式黑科技)

    number number number

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


    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
     

    Source
     

    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201 
     

    Statistic | Submit | Discuss | Note

    题意:斐波拉契数列,求不能由这些k个斐波那契数列数组成的最小整数

    思路:先手写找规律,再用黑科技代码模板

    //递推公式黑科技
    #include<bits/stdc++.h>
    using namespace std;
    #define X first
    #define Y second
    #define PB push_back
    #define MP make_pair
    #define MEM(x,y) memset(x,y,sizeof(x));
    #define bug(x) cout<<"bug"<<x<<endl;
    typedef long long ll;
    typedef pair<int,int> pii;
    using namespace std;
    const int maxn=1e3+10;
    const int mod=998244353;
    ll powmod(ll a,ll b){
        ll res=1;a%=mod;
        assert(b>=0);
        for(;b;b>>=1){
            if(b&1)res=res*a%mod;a=a*a%mod;
        }
        return res;
    }
    // head
    namespace linear_seq {
        const int N=10010;
        ll res[N],base[N],_c[N],_md[N];
        vector<int> Md;
        void mul(ll *a,ll *b,int k) {
            for(int i=0;i<k+k;i++) _c[i]=0;
            for(int i=0;i<k;i++)
                if (a[i])
                    for(int j=0;j<k;j++) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
            for (int i=k+k-1;i>=k;i--)
                if (_c[i])
                    for(int j=0;j<Md.size();j++)
                        _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
            for(int i=0;i<k;i++) a[i]=_c[i];
        }
        int solve(ll n,vector<int> a,vector<int> b) {
        // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
            ll ans=0,pnt=0;
            int k=a.size();
            assert(a.size()==b.size());
            for(int i=0;i<k;i++) _md[k-1-i]=-a[i];_md[k]=1;
            Md.clear();
            for(int i=0;i<k;i++) if (_md[i]!=0) Md.push_back(i);
            for(int i=0;i<k;i++) res[i]=base[i]=0;
            res[0]=1;
            while ((1ll<<pnt)<=n) pnt++;
            for (int p=pnt;p>=0;p--) {
                mul(res,res,k);
                if ((n>>p)&1) {
                    for (int i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
                    for(int j=0;j<Md.size();j++) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
                }
            }
            for(int i=0;i<k;i++) ans=(ans+res[i]*b[i])%mod;
            if (ans<0) ans+=mod;
            return ans;
        }
        vector<int> BM(vector<int> s) {
            vector<int> C(1,1),B(1,1);
            int L=0,m=1,b=1;
            for(int n=0;n<s.size();n++) {
                ll d=0;
                for(int i=0;i<L+1;i++) d=(d+(ll)C[i]*s[n-i])%mod;
                if (d==0) ++m;
                else if (2*L<=n) {
                    vector<int> T=C;
                    ll c=mod-d*powmod(b,mod-2)%mod;
                    while (C.size()<B.size()+m) C.PB(0);
                    for(int i=0;i<B.size();i++) C[i+m]=(C[i+m]+c*B[i])%mod;
                    L=n+1-L; B=T; b=d; m=1;
                } else {
                    ll c=mod-d*powmod(b,mod-2)%mod;
                    while (C.size()<B.size()+m) C.PB(0);
                    for(int i=0;i<B.size();i++) C[i+m]=(C[i+m]+c*B[i])%mod;
                    ++m;
                }
            }
            return C;
        }
        int gao(vector<int> a,ll n) {
            vector<int> c=BM(a);
            c.erase(c.begin());
            for(int i=0;i<c.size();i++) c[i]=(mod-c[i])%mod;
            return solve(n,c,vector<int>(a.begin(),a.begin()+c.size()));
        }
    };
    
    int main(){
        ll t,n;
    //    cin>>t;
        while(cin>>n){
            cout<<(linear_seq::gao(vector<int>{5,13,34,89},n-1)%mod-1)%mod<<endl;
        }
    }



  • 相关阅读:
    唐骏:学会高明的让员工加班
    [ZT]微软亚洲技术中心面试题[附非標准答案]
    企业的后ERP时代
    WinForm中使用DXperience控件中XtraForm,如何实现换肤
    世卫组织健康食品排行榜:健康肉类减为三种
    [轉載]在研究院的新兵训练
    [转]怎样面对同事升职?
    今天是个好日子!
    程序员跳槽动机被误读,薪资只是表象(图文)
    2008年个人技术十大趋势
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387251.html
Copyright © 2011-2022 走看看