結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー firiexpfiriexp
提出日時 2020-05-29 22:01:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,029 ms / 3,500 ms
コード長 5,999 bytes
コンパイル時間 1,248 ms
コンパイル使用メモリ 114,256 KB
実行使用メモリ 67,932 KB
最終ジャッジ日時 2024-04-23 22:23:45
合計ジャッジ時間 20,663 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
20,024 KB
testcase_01 AC 14 ms
20,076 KB
testcase_02 AC 14 ms
20,172 KB
testcase_03 AC 32 ms
21,132 KB
testcase_04 AC 24 ms
20,636 KB
testcase_05 AC 23 ms
20,760 KB
testcase_06 AC 24 ms
20,768 KB
testcase_07 AC 22 ms
20,760 KB
testcase_08 AC 23 ms
20,512 KB
testcase_09 AC 23 ms
20,600 KB
testcase_10 AC 18 ms
20,408 KB
testcase_11 AC 23 ms
20,856 KB
testcase_12 AC 18 ms
20,312 KB
testcase_13 AC 999 ms
67,932 KB
testcase_14 AC 979 ms
67,800 KB
testcase_15 AC 990 ms
67,800 KB
testcase_16 AC 988 ms
67,800 KB
testcase_17 AC 998 ms
67,800 KB
testcase_18 AC 983 ms
67,800 KB
testcase_19 AC 1,003 ms
67,924 KB
testcase_20 AC 992 ms
67,712 KB
testcase_21 AC 979 ms
67,800 KB
testcase_22 AC 985 ms
67,800 KB
testcase_23 AC 984 ms
67,876 KB
testcase_24 AC 998 ms
67,796 KB
testcase_25 AC 987 ms
67,840 KB
testcase_26 AC 987 ms
67,756 KB
testcase_27 AC 1,029 ms
67,800 KB
testcase_28 AC 998 ms
67,932 KB
testcase_29 AC 967 ms
67,800 KB
testcase_30 AC 930 ms
67,768 KB
testcase_31 AC 14 ms
20,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

constexpr int ntt_mod = 998244353, ntt_root = 3;
// 1012924417 -> 5, 924844033 -> 5
// 998244353  -> 3, 897581057 -> 3
// 645922817  -> 3;
template<u32 M = 1000000007>
struct modint{
    u32 val;
    modint(): val(0){}
    template<typename T>
    modint(T t){t %= (T)M; if(t < 0) t += (T)M; val = t;}

    modint pow(ll k) const {
        modint res(1), x(val);
        while(k){
            if(k&1) res *= x;
            x *= x;
            k >>= 1;
        }
        return res;
    }
    template<typename T>
    modint& operator=(T t){t %= (T)M; if(t < 0) t += (T)M; val = t; return *this; }
    modint inv() const {return pow(M-2);}
    modint& operator+=(modint a){ val += a.val; if(val >= M) val -= M; return *this;}
    modint& operator-=(modint a){ if(val < a.val) val += M-a.val; else val -= a.val; return *this;}
    modint& operator*=(modint a){ val = (u64)val*a.val%M; return *this;}
    modint& operator/=(modint a){ return (*this) *= a.inv();}
    modint operator+(modint a) const {return modint(val) +=a;}
    modint operator-(modint a) const {return modint(val) -=a;}
    modint operator*(modint a) const {return modint(val) *=a;}
    modint operator/(modint a) const {return modint(val) /=a;}
    modint operator-(){ return modint(M-val);}
    bool operator==(const modint a) const {return val == a.val;}
    bool operator!=(const modint a) const {return val != a.val;}
    bool operator<(const modint a) const {return val < a.val;}
};

using mint = modint<ntt_mod>;

class NTT {
    static constexpr int max_base = 20, maxN = 1 << max_base; // N <= 524288 * 2
    mint roots[maxN << 1], iroots[maxN << 1];
public:
    NTT() {
        for (int i = 0; i <= max_base; ++i) {
            const int offset = (1 << i) - 1;
            const mint g = mint(ntt_root).pow((ntt_mod)/(1 << i)), ginv = g.inv();
            mint x = 1, y = 1;
            for (int j = 0; j < 1 << i; ++j) {
                roots[offset+j] = x;
                x *= g;
                iroots[offset+j] = y;
                y *= ginv;
            }
        }
    }

    void transform(vector<mint> &a, int sign){
        const int n = a.size();
        if(!sign){ // fft
            for(int k = n >> 1; k >= 1; k >>= 1){
                for (int i = 0; i < n; i += k << 1) {
                    for (int j = 0; j < k; ++j) {
                        const mint tmp = a[i+j]-a[i+j+k];
                        a[i+j] += a[i+j+k];
                        a[i+j+k] = tmp*roots[(k << 1)-1+j];
                    }
                }
            }
        }else { // ifft
            for(int k = 1; k <= (n >> 1); k <<= 1){
                for (int i = 0; i < n; i += k << 1) {
                    for (int j = 0; j < k; ++j) {
                        a[i+j+k] *= iroots[(k << 1)-1+j];
                        const mint tmp = a[i+j]-a[i+j+k];
                        a[i+j] += a[i+j+k];
                        a[i+j+k] = tmp;
                    }
                }
            }
            const mint x = mint(n).inv();
            for (auto &&i : a) i *= x;
        }
    }
};

NTT ntt;

struct poly {
    vector<mint> v;
    poly() = default;
    explicit poly(int n) : v(n) {};
    explicit poly(vector<mint> vv) : v(std::move(vv)) {};
    int size() const {return (int)v.size(); }
    poly cut(int len){
        if(len < v.size()) v.resize(static_cast<unsigned long>(len));
        return *this;
    }
    inline mint& operator[] (int i) {return v[i]; }
    poly& operator+=(const poly &a) {
        this->v.resize(max(size(), a.size()));
        for (int i = 0; i < a.size(); ++i) this->v[i] += a.v[i];
        return *this;
    }
    poly& operator-=(const poly &a) {
        this->v.resize(max(size(), a.size()));
        for (int i = 0; i < a.size(); ++i) this->v[i] -= a.v[i];
        return *this;
    }

    poly& operator*=(poly a) {
        int N = size()+a.size()-1;
        int sz = 1;
        while(sz < N) sz <<= 1;
        this->v.resize(sz); a.v.resize(sz);
        ntt.transform(this->v, 0); ntt.transform(a.v, 0);
        for(int i = 0; i < sz; ++i) this->v[i] *= a.v[i];
        ntt.transform(this->v, 1);
        this->v.resize(N);
        return *this;
    }
    poly& operator/=(const poly &a){ return (*this *= a.inv()); }
    poly operator+(const poly &a) const { return poly(*this) += a; }
    poly operator-(const poly &a) const { return poly(*this) -= a; }
    poly operator*(const poly &a) const { return poly(*this) *= a; }

    poly inv() const {
        int n = size();
        poly r(1);
        r[0] = (this->v[0]).inv();
        int k = 1;
        while(k < n){
            k *= 2;
            poly ff(k);
            for (int i = 0; i < min(k, n); ++i) {
                ff[i] = this->v[i];
            }
            poly nr = (r*r*ff).cut(k);
            for (int i = 0; i < k/2; ++i) {
                nr[i] = (r[i]+r[i]-nr[i]);
                nr[i+k/2] = -nr[i+k/2];
            }
            r = nr;
        }
        r.v.resize(n);
        return r;
    }
};


int main() {
    int n, q;
    cin >> n >> q;
    int sz = 1;
    while(sz < n) sz <<= 1;
    vector<poly> v(2*sz);
    for (int i = 0; i < n; ++i) {
        ll a;
        cin >> a;
        poly x(vector<mint>{a-1, 1});
        v[i+sz] = x;
    }
    for (int i = 0; i < sz; ++i) {
        if(v[i+sz].size() == 0){
            v[i+sz] = poly(vector<mint>{1, 0});
        }
    }
    for (int i = sz-1; i > 0; --i) {
        v[i] = v[2*i]*v[2*i+1];
        v[2*i].v.clear();
        v[2*i+1].v.clear();
    }
    for (int i = 0; i < q; ++i) {
        int x;
        scanf("%d", &x);
        printf("%d\n", v[1][x].val);
    }
    return 0;
}
0