結果

問題 No.980 Fibonacci Convolution Hard
ユーザー keikei
提出日時 2020-01-31 22:11:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,467 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 182,284 KB
実行使用メモリ 150,080 KB
最終ジャッジ日時 2024-09-17 08:30:07
合計ジャッジ時間 42,766 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

// https://satanic0258.github.io/snippets/math/NTT.html
// Description: 整数列a[i],b[i]から列c[k]=sum{a[i]*b[k-i]}を生成する.任意modに対応.O(NlogN).
namespace NTT {
    std::vector<int> tmp;
    size_t sz = 1;
 
    inline int powMod(int n, int p, int m) {
        int res = 1;
        while (p) {
            if (p & 1) res = (ll)res * n % m;
            n = (ll)n * n % m;
            p >>= 1;
        }
        return (int)res;
    }
    inline int invMod(int n, int m) {
        return powMod(n, m - 2, m);
    }
 
    template <int Mod, int PrimitiveRoot>
    struct NTTPart {
        static std::vector<int> ntt(std::vector<int> a, bool inv = false) {
            size_t mask = sz - 1;
            size_t p = 0;
            for (size_t i = sz >> 1; i >= 1; i >>= 1) {
                auto& cur = (p & 1) ? tmp : a;
                auto& nex = (p & 1) ? a : tmp;
                int e = powMod(PrimitiveRoot, (Mod - 1) / sz * i, Mod);
                if (inv) e = invMod(e, Mod);
                int w = 1;
                for (size_t j = 0; j < sz; j += i) {
                    for (size_t k = 0; k < i; ++k) {
                        nex[j + k] = (cur[((j << 1) & mask) + k] + (ll)w * cur[(((j << 1) + i) & mask) + k]) % Mod;
                    }
                    w = (ll)w * e % Mod;
                }
                ++p;
            }
            if (p & 1) std::swap(a, tmp);
            if (inv) {
                int invSz = invMod(sz, Mod);
                for (size_t i = 0; i < sz; ++i) a[i] = (ll)a[i] * invSz % Mod;
            }
            return a;
        }
        static std::vector<int> mul(std::vector<int> a, std::vector<int> b) {
            a = ntt(a);
            b = ntt(b);
            for (size_t i = 0; i < sz; ++i) a[i] = (ll)a[i] * b[i] % Mod;
            a = ntt(a, true);
            return a;
        }
    };
 
    constexpr int M[] = {1224736769, 469762049, 167772161};
    constexpr int PR[] = {3, 3, 3};
    constexpr int NTT_CONVOLUTION_TIME = 3;
    /*
        X := max(a)*max(b)*min(|a|, |b|) のとき,
        NTT_CONVOLUTION_TIME <- 1: X <         1224736769 = 1.2*10^ 9 ~ 2^30
        NTT_CONVOLUTION_TIME <- 2: X < 575334854091079681 = 5.8*10^17 ~ 2^59
        NTT_CONVOLUTION_TIME <- 3: X < 2^86 (32bit * 32bit * 10^7くらいまで)
    */
 
    inline void garner(std::vector<int> *c, int mod) {
        if (NTT_CONVOLUTION_TIME == 1) {
            for(auto& x : c[0]) x %= mod;
        }
        else if (NTT_CONVOLUTION_TIME == 2) {
            const int r01 = invMod(M[0], M[1]);
            for (size_t i = 0; i < sz; ++i) {
                c[1][i] = (c[1][i] - c[0][i]) * (ll)r01 % M[1];
                if (c[1][i] < 0) c[1][i] += M[1];
                c[0][i] = (c[0][i] + (ll)c[1][i] * M[0]) % mod;
            }
        }
        else if (NTT_CONVOLUTION_TIME == 3) {
            const int R01 = invMod(M[0], M[1]);
            const int R02 = invMod(M[0], M[2]);
            const int R12 = invMod(M[1], M[2]);
            const int M01 = (ll)M[0] * M[1] % mod;
            for (size_t i = 0; i < sz; ++i) {
                c[1][i] = (c[1][i] - c[0][i]) * (ll)R01 % M[1];
                if (c[1][i] < 0) c[1][i] += M[1];
                c[2][i] = ((c[2][i] - c[0][i]) * (ll)R02 % M[2] - c[1][i]) * R12 % M[2];
                if (c[2][i] < 0) c[2][i] += M[2];
                c[0][i] = (c[0][i] + (ll)c[1][i] * M[0] + (ll)c[2][i] * M01) % mod;
            }
        }
    }
    std::vector<int> mul(std::vector<int> a, std::vector<int> b, int mod) {
        for (auto& x : a) x %= mod;
        for (auto& x : b) x %= mod;
 
        size_t m = a.size() + b.size() - 1;
        sz = 1;
        while (m > sz) sz <<= 1;
        tmp.resize(sz);
        a.resize(sz, 0);
        b.resize(sz, 0);
 
        std::vector<int> c[NTT_CONVOLUTION_TIME];
        if (NTT_CONVOLUTION_TIME >= 1) c[0] = NTTPart<M[0], PR[0]>::mul(a, b);
        if (NTT_CONVOLUTION_TIME >= 2) c[1] = NTTPart<M[1], PR[1]>::mul(a, b);
        if (NTT_CONVOLUTION_TIME >= 3) c[2] = NTTPart<M[2], PR[2]>::mul(a, b);
        for (auto& v : c) v.resize(m);
        garner(c, mod);
        return c[0];
    }
}; // !!! CHECK NTT_CONVOLUTION_TIME !!!

constexpr ll MOD = 1000000007;
constexpr int MAXN = 2000001;
template<class Type>
Type solve(Type res = Type()){
    ll p; cin >> p;
    ll Q; cin >> Q;
    
    vector<int> a(MAXN);
    a[1] = 0; a[2] = 1;
    for(int i = 3; i < MAXN;i++){
        a[i] = p*a[i-1]%MOD + a[i-2];
        a[i] %= MOD;
    }

    auto b = a;
    vector<int> c(NTT::mul(a,b,MOD));
    while(Q--){
        ll q; cin >> q;
        cout << c[q] << endl;
    }
    return res;
}
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    solve<ll>(0);
    // cout << fixed << setprecision(12) << solve<ll>() << endl;
    return 0;
}
0