結果

問題 No.980 Fibonacci Convolution Hard
ユーザー minatominato
提出日時 2021-01-23 07:13:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,683 ms / 2,000 ms
コード長 6,934 bytes
コンパイル時間 4,478 ms
コンパイル使用メモリ 273,128 KB
実行使用メモリ 137,764 KB
最終ジャッジ日時 2024-06-09 12:07:27
合計ジャッジ時間 37,023 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,602 ms
137,592 KB
testcase_01 AC 1,592 ms
137,636 KB
testcase_02 AC 1,598 ms
137,412 KB
testcase_03 AC 1,683 ms
137,628 KB
testcase_04 AC 1,605 ms
137,764 KB
testcase_05 AC 1,604 ms
137,636 KB
testcase_06 AC 1,601 ms
137,636 KB
testcase_07 AC 1,594 ms
137,636 KB
testcase_08 AC 1,634 ms
137,508 KB
testcase_09 AC 1,594 ms
137,636 KB
testcase_10 AC 1,587 ms
137,632 KB
testcase_11 AC 1,582 ms
137,508 KB
testcase_12 AC 1,586 ms
137,636 KB
testcase_13 AC 1,570 ms
137,508 KB
testcase_14 AC 1,599 ms
137,584 KB
testcase_15 AC 1,582 ms
137,636 KB
testcase_16 AC 1,566 ms
137,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
constexpr char ln = '\n';
istream& operator>>(istream& is, __int128_t& x) {
    x = 0;
    string s;
    is >> s;
    int n = int(s.size()), it = 0;
    if (s[0] == '-') it++;
    for (; it < n; it++) x = (x * 10 + s[it] - '0');
    if (s[0] == '-') x = -x;
    return is;
}
ostream& operator<<(ostream& os, __int128_t x) {
    if (x == 0) return os << 0;
    if (x < 0) os << '-', x = -x;
    deque<int> deq;
    while (x) deq.emplace_front(x % 10), x /= 10;
    for (int e : deq) os << e;
    return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template<class T> 
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    for (int i = 0; i < int(v.size()); i++) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "}";
}
template<class Container> inline int SZ(const Container& v) { return int(v.size()); }
template<class T> inline void UNIQUE(vector<T>& v) { v.erase(unique(v.begin(), v.end()), v.end()); }
template<class T1, class T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; }
template<class T1, class T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; }
inline int topbit(ull x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); }
inline int botbit(ull x) { return x == 0 ? 64 : __builtin_ctzll(x); }
inline int popcount(ull x) { return __builtin_popcountll(x); }
inline int kthbit(ull x, int k) { return (x >> k) & 1; }
inline constexpr long long TEN(int x) { return x == 0 ? 1 : TEN(x-1) * 10; }
template<class T> inline T ABS(T x) { return max(x, -x); }
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
inline void YES(bool t = 1) { cout << YESNO[t] << "\n"; }
inline void Yes(bool t = 1) { cout << YesNo[t] << "\n"; }
inline void yes(bool t = 1) { cout << yesno[t] << "\n"; }
inline void print() { cout << "\n"; }
template<class T>
inline void print(const vector<T>& v) {
    for (auto it = begin(v); it != end(v); ++it) {
        if (it != begin(v)) cout << " ";
        cout << *it;
    }
    print();
}
template<class T, class... Args>
inline void print(const T& x, const Args& ... args) {
    cout << x << " ";
    print(args...);
}
#ifdef MINATO_LOCAL
inline void debug_out() { cerr << endl; }
template <class T, class... Args>
inline void debug_out(const T& x, const Args& ... args) {
    cerr << " " << x;
    debug_out(args...);
}
#define debug(...) cerr << __LINE__ << " : [" << #__VA_ARGS__ << "] =", debug_out(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(7); }; } fast_ios_;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
using mint = modint1000000007;
//using mint = modint;

istream& operator>>(istream& is, modint998244353& M) { long long x; is >> x; M = x; return is; }
ostream& operator<<(ostream& os, const modint998244353& M) { return os << M.val(); }
istream& operator>>(istream& is, modint1000000007& M) { long long x; is >> x; M = x; return is; }
ostream& operator<<(ostream& os, const modint1000000007& M) { return os << M.val(); }
template<int m> istream& operator>>(istream& is, static_modint<m>& M) { long long x; is >> x; M = x; return is; }
template<int m> ostream& operator<<(ostream& os, const static_modint<m>& M) { return os << M.val(); }
istream& operator>>(istream& is, modint& M) { long long x; is >> x; M = x; return is; }
ostream& operator<<(ostream& os, const modint& M) { return os << M.val(); }

namespace ArbitraryNTT {
    constexpr int m0 = 167772161;
    constexpr int m1 = 469762049;
    constexpr int m2 = 754974721;
    constexpr int r01 = 104391568; // mint1(m0).inv()
    constexpr int r02 = 323560596; // mint2(m0).inv()
    constexpr int r12 = 399692502; // mint2(m1).inv()
    // constexpr int m0 = 1045430273;
    // constexpr int m1 = 1051721729;
    // constexpr int m2 = 1053818881;
    // constexpr int r01 = 175287122; // mint1(m0).inv()
    // constexpr int r02 = 395182206; // mint2(m0).inv()
    // constexpr int r12 = 526909943; // mint2(m1).inv()
    constexpr int r02r12 = (long long)(r02) * r12 % m2;
    constexpr long long w1 = m0;
    constexpr long long w2 = (long long)(m0) * m1;

    template<class T>
    vector<int> multiply(const vector<T>& a, const vector<T>& b, int mod) {
        int n = int(a.size()), m = int(b.size());

        vector<T> v0 = convolution<m0>(a, b);
        vector<T> v1 = convolution<m1>(a, b);
        vector<T> v2 = convolution<m2>(a, b);
        vector<int> ret(n + m - 1);

        const int W1 = w1 % mod;
        const int W2 = w2 % mod;
        for (int i = 0; i < n + m - 1; i++) {
            int n1 = v1[i], n2 = v2[i], x = v0[i];
            int y = (long long)(n1 + m1 - x) * r01 % m1;
            int z = ((long long)(n2 + m2 - x) * r02r12 + (long long)(m2 - y) * r12) % m2;
            ret[i] = ((long long)(x) + (long long)(y) * W1 + (long long)(z) * W2) % mod;
        }
        return ret;
    }

    template<class D>
    vector<D> multiply(vector<D> a, vector<D> b) {
        int n = int(a.size()), m = int(b.size());
        if (!n or !m) return {};
        if (min(n,m) < 128) {
            if (n < m) {
                swap(n, m);
                swap(a, b);
            }
            vector<D> ret(n + m - 1);
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ret[i + j] += a[i] * b[j];
                }
            }
            return ret;
        }
        vector<int> a_(n), b_(m);
        for (int i = 0; i < n; i++) a_[i] = a[i].val();
        for (int i = 0; i < m; i++) b_[i] = b[i].val();
        vector<int> c = multiply<int>(a_, b_, D::mod());
        vector<D> ret(n + m - 1);
        for (int i = 0; i < n + m - 1; i++) ret[i] = D::raw(c[i]);
        return ret;
    }
}

constexpr int MAX = 2e6;

int main() {
    int p,q; cin >> p >> q;


	vec<mint> a(MAX+1);
    a[1] = 0;
    a[2] = 1;
    for(int i = 3; i <= MAX; i++) a[i] = mint(p)*a[i-1] + a[i-2];

    auto c = ArbitraryNTT::multiply(a,a);
    rep(_,q) {
        int k; cin >> k;
        cout << c[k] << ln;
    }
}
0