結果

問題 No.978 Fibonacci Convolution Easy
ユーザー keymoonkeymoon
提出日時 2019-07-11 04:07:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,105 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 183,668 KB
実行使用メモリ 91,308 KB
最終ジャッジ日時 2023-10-14 09:09:51
合計ジャッジ時間 5,754 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <random>

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;

#define FOR(i,n) for(int i = 0; i < (n); i++)
#define ten(x) ((int)1e##x)

template<class T> T extgcd(T a, T b, T & x, T & y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }
template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }

template<int mod, int primitive_root>
class NTT
{
public:
    int get_mod() const { return mod; }
    void _ntt(vector<ll>& a, int sign) {
        const int n = a.size();
        assert((n ^ (n & -n)) == 0); //n = 2^k

        const int g = 3; //g is primitive root of mod
        int h = (int)mod_pow(g, (mod - 1) / n, mod); // h^n = 1
        if (sign == -1) h = (int)mod_inv(h, mod); //h = h^-1 % mod

        //bit reverse
        int i = 0;
        for (int j = 1; j < n - 1; ++j)
        {
            for (int k = n >> 1; k > (i ^= k); k >>= 1);
            if (j < i) swap(a[i], a[j]);
        }

        for (int m = 1; m < n; m *= 2)
        {
            const int m2 = 2 * m;
            const ll base = mod_pow(h, n / m2, mod);
            ll w = 1;
            FOR(x, m) {
                for (int s = x; s < n; s += m2)
                {
                    ll u = a[s];
                    ll d = a[s + m] * w % mod;
                    a[s] = u + d;
                    if (a[s] >= mod) a[s] -= mod;
                    a[s + m] = u - d;
                    if (a[s + m] < 0) a[s + m] += mod;
                }
                w = w * base % mod;
            }
        }

        for (auto& x : a) if (x < 0) x += mod;
    }
    void ntt(vector<ll> & input) {
        _ntt(input, 1);
    }
    void intt(vector<ll> & input) {
        _ntt(input, -1);
        const int n_inv = mod_inv((int)input.size(), mod);
        for (auto& x : input) x = x * n_inv % mod;
    }

    // 畳み込み演算を行う
    vector<ll> convolution(const vector<ll> & a, const vector<ll> & b) {
        int ntt_size = 1;
        while (ntt_size < a.size() + b.size()) ntt_size *= 2;

        vector<ll> _a = a, _b = b;
        _a.resize(ntt_size); _b.resize(ntt_size);

        ntt(_a);
        ntt(_b);

        FOR(i, ntt_size) {
            (_a[i] *= _b[i]) %= mod;
        }

        intt(_a);
        return _a;
    }
};

ll garner(vector<Pii> mr, int mod) {
    mr.emplace_back(mod, 0);

    vector<ll> coffs(mr.size(), 1);
    vector<ll> constants(mr.size(), 0);
    FOR(i, mr.size() - 1) {
        // coffs[i] * v + constants[i] == mr[i].second (mod mr[i].first) を解く
        ll v = (mr[i].second - constants[i]) * mod_inv<ll>(coffs[i], mr[i].first) % mr[i].first;
        if (v < 0) v += mr[i].first;

        for (int j = i + 1; j < mr.size(); j++)
        {
            (constants[j] += coffs[j] * v) %= mr[j].first;
            (coffs[j] *= mr[i].first) %= mr[j].first;
        }
    }

    return constants[mr.size() - 1];
}

typedef NTT<998244353, 3> NTT_1;
typedef NTT<897581057, 3> NTT_2;
typedef NTT<645922817, 3> NTT_3;
typedef NTT<595591169, 3> NTT_4;

const int mod = 1000000007;

ll solve_stupid(vector<ll>& fibs) {
    ll res = 0;
    for (size_t i = 0; i < fibs.size(); i++)
    {
        for (size_t j = i; j < fibs.size(); j++)
        {
            (res += fibs[i] * fibs[j]) %= mod;
        }
    }
    return res;
}

ll solve_ntt(vector<ll>& fibs) {
    //modする前は(10^9)^2*(10^6)^2=10^30くらいなので、NTTを4回してgarnerで復元する
    NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3; NTT_4 ntt4;
    auto ntt1_res = ntt1.convolution(fibs, fibs);
    auto ntt2_res = ntt2.convolution(fibs, fibs);
    auto ntt3_res = ntt3.convolution(fibs, fibs);
    auto ntt4_res = ntt4.convolution(fibs, fibs);


    ll ntt1_sum = 0, ntt2_sum = 0, ntt3_sum = 0, ntt4_sum = 0;
    for (size_t i = 0; i < ntt1_res.size(); i++)
    {
        (ntt1_sum += ntt1_res[i]) %= ntt1.get_mod();
        (ntt2_sum += ntt2_res[i]) %= ntt2.get_mod();
        (ntt3_sum += ntt3_res[i]) %= ntt3.get_mod();
        (ntt4_sum += ntt4_res[i]) %= ntt4.get_mod();
    }

    vector<Pii> ntt_mods(4);
    ntt_mods[0].first = ntt1.get_mod(), ntt_mods[0].second = (int)ntt1_sum;
    ntt_mods[1].first = ntt2.get_mod(), ntt_mods[1].second = (int)ntt2_sum;
    ntt_mods[2].first = ntt3.get_mod(), ntt_mods[2].second = (int)ntt3_sum;
    ntt_mods[3].first = ntt4.get_mod(), ntt_mods[3].second = (int)ntt4_sum;
    ll res = garner(ntt_mods, mod);
    for (size_t i = 0; i < fibs.size(); i++)
        (res += fibs[i] * fibs[i]) %= mod;
    (res *= mod_inv(2, mod)) %= mod;
    return res;
}

int main() {
    int n, p;
    cin >> n >> p;
    if (n == 1)
    {
        cout << 0 << endl;
        return 0;
    }
    vector<ll> fibs(n);
    fibs[0] = 0;
    fibs[1] = 1;
    for (int i = 2; i < n; i++)
        fibs[i] = (fibs[i - 1] * p + fibs[i - 2]) % mod;
    cout << solve_ntt(fibs) << endl;
}
0