結果
| 問題 |
No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
|
| コンテスト | |
| ユーザー |
stoq
|
| 提出日時 | 2020-11-04 09:10:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,300 ms / 3,500 ms |
| コード長 | 6,291 bytes |
| コンパイル時間 | 2,174 ms |
| コンパイル使用メモリ | 210,208 KB |
| 最終ジャッジ日時 | 2025-01-15 19:41:51 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#define MOD_TYPE 2
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 0
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;
constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-7;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";
struct io_init
{
io_init()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(30) << setiosflags(ios::fixed);
};
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
inline ll CEIL(ll a, ll b)
{
return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
is >> p.first >> p.second;
return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
os << p.first << " " << p.second;
return os;
}
#pragma endregion
ll mod_pow(ll a, ll n, ll m)
{
ll res = 1, p = a % m;
while (n)
{
if (n & 1)
res = res * p % m;
p = p * p % m;
n >>= 1;
}
return res;
}
template <ll mod = 998244353, ll root = 3> //特殊な素数と原始根 998244353のとき3
class NTT
{
private:
template <typename T>
inline void bit_reverse(vector<T> &a)
{
int n = a.size();
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]);
}
}
void _ntt(vector<long long> &a, int sign)
{
const int n = a.size();
assert((n ^ (n & -n)) == 0); //n = 2^k
long long tmp = (mod - 1) * mod_pow((ll)n, mod - 2, mod) % mod; // -1/n
long long h = mod_pow(root, tmp, mod); // ^n√g
if (sign == -1)
h = mod_pow(h, mod - 2, mod);
bit_reverse(a);
for (ll m = 1; m < n; m <<= 1)
{
const ll m2 = 2 * m;
long long _base = mod_pow((ll)h, (ll)(n / m2), mod);
long long _w = 1;
for (int x = 0; x < m; ++x)
{
for (ll s = x; s < n; s += m2)
{
long long u = a[s];
long long d = (a[s + m] * _w) % mod;
a[s] = (u + d) % mod;
a[s + m] = (u - d + mod) % mod;
}
_w = (_w * _base) % mod;
}
}
}
void ntt(vector<long long> &input) { _ntt(input, 1); }
void intt(vector<long long> &input)
{
_ntt(input, -1);
const long long n_inv = mod_pow((ll)input.size(), mod - 2, mod);
for (auto &x : input)
x = (x * n_inv) % mod;
}
public:
// 畳み込み演算を行う
vector<long long> convolution(const vector<long long> &a, const vector<long long> &b)
{
int result_size = a.size() + b.size() - 1;
int n = 1;
while (n < result_size)
n <<= 1;
vector<long long> _a = a, _b = b;
_a.resize(n, 0);
_b.resize(n, 0);
ntt(_a);
ntt(_b);
for (int i = 0; i < n; ++i)
_a[i] = (_a[i] * _b[i]) % mod;
intt(_a);
_a.resize(result_size);
return _a;
}
};
template <typename T>
class SegmentTree
{
private:
using Fn = function<T(T &, T &)>;
int N;
vector<T> dat;
T unit;
Fn func;
public:
SegmentTree() {}
SegmentTree(int n_, Fn func_, T unit_) : func(func_), unit(unit_)
{
N = 1;
while (N < n_)
N *= 2;
dat.assign(2 * N - 1, unit);
}
SegmentTree(const vector<T> &v, Fn func_, T unit_) : func(func_), unit(unit_)
{
N = 1;
int sz = v.size();
while (N < sz)
N *= 2;
dat.resize(2 * N - 1);
for (int i = 0; i < N; ++i)
dat[i + N - 1] = (i < sz ? v[i] : unit);
for (int i = N - 2; i >= 0; --i)
dat[i] = func(dat[i * 2 + 1], dat[i * 2 + 2]);
}
void update(int k, T a)
{
k += N - 1;
dat[k] = a;
while (k > 0)
{
k = (k - 1) / 2;
dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
T get(int k) { return dat[k + N - 1]; }
T query(int l, int r)
{
T vl = unit, vr = unit;
for (l += (N - 1), r += (N - 1); l < r; l >>= 1, r >>= 1)
{
if ((l & 1) == 0)
vl = func(vl, dat[l]);
if ((r & 1) == 0)
vr = func(vr, dat[--r]);
}
return func(vl, vr);
}
};
void solve()
{
NTT<> ntt;
int n, q;
cin >> n >> q;
vector<ll> a(n);
rep(i, n)
{
cin >> a[i];
a[i] %= MOD;
}
auto prod = [&](vector<ll> &f, vector<ll> &g) {
if (f.size() == 1 and f[0] == 1)
return g;
if (g.size() == 1 and g[0] == 1)
return f;
return ntt.convolution(f, g);
};
vector<ll> unit = {1};
vector<vector<ll>> f(n, vector<ll>(2));
rep(i, n)
{
f[i][0] = a[i] - 1, f[i][1] = 1;
}
SegmentTree<vector<ll>> sg(f, prod, unit);
vector<ll> g = sg.query(0, n);
rep(qi, q)
{
int b;
cin >> b;
cout << g[b] << "\n";
}
}
int main()
{
solve();
}
stoq