This documentation is automatically generated by competitive-verifier/competitive-verifier
// clang-format off
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/shift_of_sampling_points_of_polynomial
// clang-format on
#include <iostream>
#include <vector>
#include "../../math/combinatorics/lagrange-polynomial-3.hpp"
#include "../../math/combinatorics/montgomery-mod-int.hpp"
#include "../../math/fft/number-theoretic-transform-friendly-mod-int.hpp"
using namespace std;
using mint = modint998244353;
template <typename Sequence>
void read_sequence(Sequence& values) {
for (auto& value : values) cin >> value;
}
template <typename Sequence>
void print_sequence(const Sequence& values) {
for (int i = 0; i < static_cast<int>(values.size()); i++) {
if (i > 0) cout << " ";
cout << values[i];
}
cout << "\n";
}
int main() {
int N, T, M;
cin >> N >> T >> M;
vector<mint> ys(N);
read_sequence(ys);
NumberTheoreticTransformFriendlyModInt<mint> v;
auto multiply = [&](const vector<mint>& a, const vector<mint>& b) {
return v.multiply(a, b);
};
print_sequence(lagrange_polynomial(ys, M, T, multiply));
}
#line 1 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"
// clang-format off
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/shift_of_sampling_points_of_polynomial
// clang-format on
#include <iostream>
#include <vector>
#line 2 "math/combinatorics/lagrange-polynomial-3.hpp"
#include <algorithm>
#include <cstdint>
#include <iterator>
#line 7 "math/combinatorics/lagrange-polynomial-3.hpp"
/**
* @brief Lagrange Polynomial(多項式補間, 値)
*/
template <typename Mint, typename F>
std::vector<Mint> lagrange_polynomial(const std::vector<Mint>& y,
std::int64_t T, const int& m,
const F& multiply) {
int k = (int)y.size() - 1;
T %= Mint::mod();
if (T <= k) {
std::vector<Mint> ret(m);
int ptr = 0;
for (std::int64_t i = T; i <= k and ptr < m; i++) {
ret[ptr++] = y[i];
}
if (k + 1 < T + m) {
auto suf = lagrange_polynomial(y, k + 1, m - ptr, multiply);
for (int i = k + 1; i < T + m; i++) {
ret[ptr++] = suf[i - (k + 1)];
}
}
return ret;
}
if (T + m > Mint::mod()) {
auto pref = lagrange_polynomial(y, T, Mint::mod() - T, multiply);
auto suf = lagrange_polynomial(y, 0, m - pref.size(), multiply);
std::copy(std::begin(suf), std::end(suf), std::back_inserter(pref));
return pref;
}
std::vector<Mint> finv(k + 1, 1), d(k + 1);
for (int i = 2; i <= k; i++) finv[k] *= i;
finv[k] = Mint(1) / finv[k];
for (int i = k; i >= 1; i--) finv[i - 1] = finv[i] * i;
for (int i = 0; i <= k; i++) {
d[i] = finv[i] * finv[k - i] * y[i];
if ((k - i) & 1) d[i] = -d[i];
}
std::vector<Mint> h(m + k);
for (int i = 0; i < m + k; i++) {
h[i] = Mint(1) / (T - k + i);
}
auto dh = multiply(d, h);
std::vector<Mint> ret(m);
Mint cur = T;
for (int i = 1; i <= k; i++) cur *= T - i;
for (int i = 0; i < m; i++) {
ret[i] = cur * dh[k + i];
cur *= T + i + 1;
cur *= h[i];
}
return ret;
}
#line 2 "math/combinatorics/montgomery-mod-int.hpp"
#line 5 "math/combinatorics/montgomery-mod-int.hpp"
template <std::uint32_t mod_, bool fast = false>
struct MontgomeryModInt {
private:
using mint = MontgomeryModInt;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
static constexpr u32 get_r() {
u32 ret = mod_;
for (i32 i = 0; i < 4; i++) ret *= 2 - mod_ * ret;
return ret;
}
static constexpr u32 r = get_r();
static constexpr u32 n2 = -u64(mod_) % mod_;
static_assert(r * mod_ == 1, "invalid, r * mod != 1");
static_assert(mod_ < (1 << 30), "invalid, mod >= 2 ^ 30");
static_assert((mod_ & 1) == 1, "invalid, mod % 2 == 0");
u32 x;
public:
MontgomeryModInt() : x{} {}
MontgomeryModInt(const i64& a)
: x(reduce(u64(fast ? a : (a % mod() + mod())) * n2)) {}
static constexpr u32 reduce(const u64& b) {
return u32(b >> 32) + mod() - u32((u64(u32(b) * r) * mod()) >> 32);
}
mint& operator+=(const mint& p) {
if (i32(x += p.x - 2 * mod()) < 0) x += 2 * mod();
return *this;
}
mint& operator-=(const mint& p) {
if (i32(x -= p.x) < 0) x += 2 * mod();
return *this;
}
mint& operator*=(const mint& p) {
x = reduce(u64(x) * p.x);
return *this;
}
mint& operator/=(const mint& p) {
*this *= p.inv();
return *this;
}
mint operator-() const { return mint() - *this; }
mint operator+(const mint& p) const { return mint(*this) += p; }
mint operator-(const mint& p) const { return mint(*this) -= p; }
mint operator*(const mint& p) const { return mint(*this) *= p; }
mint operator/(const mint& p) const { return mint(*this) /= p; }
bool operator==(const mint& p) const {
return (x >= mod() ? x - mod() : x) == (p.x >= mod() ? p.x - mod() : p.x);
}
bool operator!=(const mint& p) const {
return (x >= mod() ? x - mod() : x) != (p.x >= mod() ? p.x - mod() : p.x);
}
u32 val() const {
u32 ret = reduce(x);
return ret >= mod() ? ret - mod() : ret;
}
mint pow(u64 n) const {
mint ret(1), mul(*this);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
mint inv() const { return pow(mod() - 2); }
friend std::ostream& operator<<(std::ostream& os, const mint& p) {
return os << p.val();
}
friend std::istream& operator>>(std::istream& is, mint& a) {
i64 t;
is >> t;
a = mint(t);
return is;
}
static constexpr u32 mod() { return mod_; }
};
template <std::uint32_t mod>
using modint = MontgomeryModInt<mod>;
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1000000007>;
#line 2 "math/fft/number-theoretic-transform-friendly-mod-int.hpp"
#include <cassert>
#line 5 "math/fft/number-theoretic-transform-friendly-mod-int.hpp"
/**
* @brief Number Theoretic Transform Friendly ModInt
*/
template <typename Mint>
struct NumberTheoreticTransformFriendlyModInt {
static std::vector<Mint> roots, iroots, rate2, irate2, rate3, irate3;
static int max_base;
NumberTheoreticTransformFriendlyModInt() = default;
static void init() {
if (roots.empty()) {
const unsigned mod = Mint::mod();
assert(mod >= 3 && mod % 2 == 1);
auto tmp = mod - 1;
max_base = 0;
while (tmp % 2 == 0) tmp >>= 1, max_base++;
Mint root = 2;
while (root.pow((mod - 1) >> 1) == 1) {
root += 1;
}
assert(root.pow(mod - 1) == 1);
roots.resize(max_base + 1);
iroots.resize(max_base + 1);
rate2.resize(max_base + 1);
irate2.resize(max_base + 1);
rate3.resize(max_base + 1);
irate3.resize(max_base + 1);
roots[max_base] = root.pow((mod - 1) >> max_base);
iroots[max_base] = Mint(1) / roots[max_base];
for (int i = max_base - 1; i >= 0; i--) {
roots[i] = roots[i + 1] * roots[i + 1];
iroots[i] = iroots[i + 1] * iroots[i + 1];
}
{
Mint prod = 1, iprod = 1;
for (int i = 0; i <= max_base - 2; i++) {
rate2[i] = roots[i + 2] * prod;
irate2[i] = iroots[i + 2] * iprod;
prod *= iroots[i + 2];
iprod *= roots[i + 2];
}
}
{
Mint prod = 1, iprod = 1;
for (int i = 0; i <= max_base - 3; i++) {
rate3[i] = roots[i + 3] * prod;
irate3[i] = iroots[i + 3] * iprod;
prod *= iroots[i + 3];
iprod *= roots[i + 3];
}
}
}
}
static void ntt(std::vector<Mint>& a) {
init();
const int n = (int)a.size();
assert((n & (n - 1)) == 0);
int h = __builtin_ctz(n);
assert(h <= max_base);
int len = 0;
Mint imag = roots[2];
if (h & 1) {
int p = 1 << (h - 1);
for (int i = 0; i < p; i++) {
auto r = a[i + p];
a[i + p] = a[i] - r;
a[i] += r;
}
len++;
}
for (; len + 1 < h; len += 2) {
int p = 1 << (h - len - 2);
{ // s = 0
for (int i = 0; i < p; i++) {
auto a0 = a[i];
auto a1 = a[i + p];
auto a2 = a[i + 2 * p];
auto a3 = a[i + 3 * p];
auto a1na3imag = (a1 - a3) * imag;
auto a0a2 = a0 + a2;
auto a1a3 = a1 + a3;
auto a0na2 = a0 - a2;
a[i] = a0a2 + a1a3;
a[i + 1 * p] = a0a2 - a1a3;
a[i + 2 * p] = a0na2 + a1na3imag;
a[i + 3 * p] = a0na2 - a1na3imag;
}
}
Mint rot = rate3[0];
for (int s = 1; s < (1 << len); s++) {
int offset = s << (h - len);
Mint rot2 = rot * rot;
Mint rot3 = rot2 * rot;
for (int i = 0; i < p; i++) {
auto a0 = a[i + offset];
auto a1 = a[i + offset + p] * rot;
auto a2 = a[i + offset + 2 * p] * rot2;
auto a3 = a[i + offset + 3 * p] * rot3;
auto a1na3imag = (a1 - a3) * imag;
auto a0a2 = a0 + a2;
auto a1a3 = a1 + a3;
auto a0na2 = a0 - a2;
a[i + offset] = a0a2 + a1a3;
a[i + offset + 1 * p] = a0a2 - a1a3;
a[i + offset + 2 * p] = a0na2 + a1na3imag;
a[i + offset + 3 * p] = a0na2 - a1na3imag;
}
rot *= rate3[__builtin_ctz(~s)];
}
}
}
static void intt(std::vector<Mint>& a, bool f = true) {
init();
const int n = (int)a.size();
assert((n & (n - 1)) == 0);
int h = __builtin_ctz(n);
assert(h <= max_base);
int len = h;
Mint iimag = iroots[2];
for (; len > 1; len -= 2) {
int p = 1 << (h - len);
{ // s = 0
for (int i = 0; i < p; i++) {
auto a0 = a[i];
auto a1 = a[i + 1 * p];
auto a2 = a[i + 2 * p];
auto a3 = a[i + 3 * p];
auto a2na3iimag = (a2 - a3) * iimag;
auto a0na1 = a0 - a1;
auto a0a1 = a0 + a1;
auto a2a3 = a2 + a3;
a[i] = a0a1 + a2a3;
a[i + 1 * p] = (a0na1 + a2na3iimag);
a[i + 2 * p] = (a0a1 - a2a3);
a[i + 3 * p] = (a0na1 - a2na3iimag);
}
}
Mint irot = irate3[0];
for (int s = 1; s < (1 << (len - 2)); s++) {
int offset = s << (h - len + 2);
Mint irot2 = irot * irot;
Mint irot3 = irot2 * irot;
for (int i = 0; i < p; i++) {
auto a0 = a[i + offset];
auto a1 = a[i + offset + 1 * p];
auto a2 = a[i + offset + 2 * p];
auto a3 = a[i + offset + 3 * p];
auto a2na3iimag = (a2 - a3) * iimag;
auto a0na1 = a0 - a1;
auto a0a1 = a0 + a1;
auto a2a3 = a2 + a3;
a[i + offset] = a0a1 + a2a3;
a[i + offset + 1 * p] = (a0na1 + a2na3iimag) * irot;
a[i + offset + 2 * p] = (a0a1 - a2a3) * irot2;
a[i + offset + 3 * p] = (a0na1 - a2na3iimag) * irot3;
}
irot *= irate3[__builtin_ctz(~s)];
}
}
if (len >= 1) {
int p = 1 << (h - 1);
for (int i = 0; i < p; i++) {
auto ajp = a[i] - a[i + p];
a[i] += a[i + p];
a[i + p] = ajp;
}
}
if (f) {
Mint inv_sz = Mint(1) / n;
for (int i = 0; i < n; i++) a[i] *= inv_sz;
}
}
/**
* @brief Transpose of ntt()
*/
static void transposed_ntt(std::vector<Mint>& a) {
init();
const int n = (int)a.size();
assert((n & (n - 1)) == 0);
const int h = __builtin_ctz(n);
assert(h <= max_base);
int len = h;
const Mint imag = roots[2];
while (len > 0) {
if (len == 1) {
const int p = 1 << (h - len);
Mint rot = 1;
for (int s = 0; s < (1 << (len - 1)); s++) {
const int offset = s << (h - len + 1);
for (int i = 0; i < p; i++) {
const auto lhs = a[i + offset];
const auto rhs = a[i + offset + p];
a[i + offset] = lhs + rhs;
a[i + offset + p] = (lhs - rhs) * rot;
}
rot *= rate2[__builtin_ctz(~s)];
}
len--;
} else {
const int p = 1 << (h - len);
Mint rot = 1;
for (int s = 0; s < (1 << (len - 2)); s++) {
const int offset = s << (h - len + 2);
const Mint rot2 = rot * rot;
const Mint rot3 = rot2 * rot;
for (int i = 0; i < p; i++) {
const auto a0 = a[i + offset];
const auto a1 = a[i + offset + p];
const auto a2 = a[i + offset + 2 * p];
const auto a3 = a[i + offset + 3 * p];
const auto x = (a2 - a3) * imag;
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + p] = (a0 - a1 + x) * rot;
a[i + offset + 2 * p] = (a0 + a1 - a2 - a3) * rot2;
a[i + offset + 3 * p] = (a0 - a1 - x) * rot3;
}
rot *= rate3[__builtin_ctz(~s)];
}
len -= 2;
}
}
}
/**
* @brief Transpose of intt()
*/
static void transposed_intt(std::vector<Mint>& a, bool f = true) {
init();
const int n = (int)a.size();
assert((n & (n - 1)) == 0);
const int h = __builtin_ctz(n);
assert(h <= max_base);
if (f) {
const Mint inv_sz = Mint(1) / n;
for (auto& value : a) value *= inv_sz;
}
int len = 0;
const Mint iimag = iroots[2];
while (len < h) {
if (len == h - 1) {
const int p = 1 << (h - len - 1);
Mint irot = 1;
for (int s = 0; s < (1 << len); s++) {
const int offset = s << (h - len);
for (int i = 0; i < p; i++) {
const auto lhs = a[i + offset];
const auto rhs = a[i + offset + p] * irot;
a[i + offset] = lhs + rhs;
a[i + offset + p] = lhs - rhs;
}
irot *= irate2[__builtin_ctz(~s)];
}
len++;
} else {
const int p = 1 << (h - len - 2);
Mint irot = 1;
for (int s = 0; s < (1 << len); s++) {
const Mint irot2 = irot * irot;
const Mint irot3 = irot2 * irot;
const int offset = s << (h - len);
for (int i = 0; i < p; i++) {
const auto a0 = a[i + offset];
const auto a1 = a[i + offset + p] * irot;
const auto a2 = a[i + offset + 2 * p] * irot2;
const auto a3 = a[i + offset + 3 * p] * irot3;
const auto x = (a1 - a3) * iimag;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + p] = a0 + a2 - a1 - a3;
a[i + offset + 2 * p] = a0 - a2 + x;
a[i + offset + 3 * p] = a0 - a2 - x;
}
irot *= irate3[__builtin_ctz(~s)];
}
len += 2;
}
}
}
static std::vector<Mint> multiply(std::vector<Mint> a, std::vector<Mint> b) {
int need = a.size() + b.size() - 1;
int nbase = 1;
while ((1 << nbase) < need) nbase++;
int sz = 1 << nbase;
a.resize(sz, 0);
b.resize(sz, 0);
ntt(a);
ntt(b);
Mint inv_sz = Mint(1) / sz;
for (int i = 0; i < sz; i++) a[i] *= b[i] * inv_sz;
intt(a, false);
a.resize(need);
return a;
}
};
template <typename Mint>
std::vector<Mint> NumberTheoreticTransformFriendlyModInt<Mint>::roots =
std::vector<Mint>();
template <typename Mint>
std::vector<Mint> NumberTheoreticTransformFriendlyModInt<Mint>::iroots =
std::vector<Mint>();
template <typename Mint>
std::vector<Mint> NumberTheoreticTransformFriendlyModInt<Mint>::rate2 =
std::vector<Mint>();
template <typename Mint>
std::vector<Mint> NumberTheoreticTransformFriendlyModInt<Mint>::irate2 =
std::vector<Mint>();
template <typename Mint>
std::vector<Mint> NumberTheoreticTransformFriendlyModInt<Mint>::rate3 =
std::vector<Mint>();
template <typename Mint>
std::vector<Mint> NumberTheoreticTransformFriendlyModInt<Mint>::irate3 =
std::vector<Mint>();
template <typename Mint>
int NumberTheoreticTransformFriendlyModInt<Mint>::max_base = 0;
#line 11 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"
using namespace std;
using mint = modint998244353;
template <typename Sequence>
void read_sequence(Sequence& values) {
for (auto& value : values) cin >> value;
}
template <typename Sequence>
void print_sequence(const Sequence& values) {
for (int i = 0; i < static_cast<int>(values.size()); i++) {
if (i > 0) cout << " ";
cout << values[i];
}
cout << "\n";
}
int main() {
int N, T, M;
cin >> N >> T >> M;
vector<mint> ys(N);
read_sequence(ys);
NumberTheoreticTransformFriendlyModInt<mint> v;
auto multiply = [&](const vector<mint>& a, const vector<mint>& b) {
return v.multiply(a, b);
};
print_sequence(lagrange_polynomial(ys, M, T, multiply));
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| g++ | N_1_00 |
|
122 ms | 10 MB |
| g++ | c_0_00 |
|
267 ms | 21 MB |
| g++ | example_00 |
|
2 ms | 4 MB |
| g++ | example_01 |
|
2 ms | 3 MB |
| g++ | max_random_00 |
|
469 ms | 34 MB |
| g++ | max_random_01 |
|
468 ms | 34 MB |
| g++ | max_random_02 |
|
475 ms | 34 MB |
| g++ | max_random_03 |
|
469 ms | 34 MB |
| g++ | medium_random_00 |
|
11 ms | 4 MB |
| g++ | medium_random_01 |
|
10 ms | 4 MB |
| g++ | medium_random_02 |
|
10 ms | 4 MB |
| g++ | medium_random_03 |
|
10 ms | 4 MB |
| g++ | small_random_00 |
|
2 ms | 4 MB |
| g++ | small_random_01 |
|
2 ms | 4 MB |
| g++ | small_random_02 |
|
2 ms | 3 MB |
| g++ | small_random_03 |
|
2 ms | 4 MB |
| g++ | type0_random_00 |
|
115 ms | 5 MB |
| g++ | type0_random_01 |
|
129 ms | 5 MB |
| g++ | type0_random_02 |
|
18 ms | 4 MB |
| g++ | type0_random_03 |
|
131 ms | 5 MB |
| g++ | type1_random_00 |
|
416 ms | 32 MB |
| g++ | type1_random_01 |
|
385 ms | 31 MB |
| g++ | type1_random_02 |
|
36 ms | 6 MB |
| g++ | type1_random_03 |
|
295 ms | 22 MB |
| g++ | type2_random_00 |
|
395 ms | 31 MB |
| g++ | type2_random_01 |
|
437 ms | 32 MB |
| g++ | type2_random_02 |
|
134 ms | 12 MB |
| g++ | type2_random_03 |
|
257 ms | 20 MB |
| g++ | type3_random_00 |
|
284 ms | 20 MB |
| g++ | type3_random_01 |
|
407 ms | 31 MB |
| g++ | type3_random_02 |
|
166 ms | 11 MB |
| g++ | type3_random_03 |
|
257 ms | 20 MB |
| clang++ | N_1_00 |
|
138 ms | 10 MB |
| clang++ | c_0_00 |
|
294 ms | 21 MB |
| clang++ | example_00 |
|
2 ms | 4 MB |
| clang++ | example_01 |
|
2 ms | 4 MB |
| clang++ | max_random_00 |
|
515 ms | 34 MB |
| clang++ | max_random_01 |
|
507 ms | 34 MB |
| clang++ | max_random_02 |
|
506 ms | 34 MB |
| clang++ | max_random_03 |
|
523 ms | 34 MB |
| clang++ | medium_random_00 |
|
11 ms | 4 MB |
| clang++ | medium_random_01 |
|
11 ms | 4 MB |
| clang++ | medium_random_02 |
|
10 ms | 4 MB |
| clang++ | medium_random_03 |
|
11 ms | 4 MB |
| clang++ | small_random_00 |
|
2 ms | 4 MB |
| clang++ | small_random_01 |
|
2 ms | 4 MB |
| clang++ | small_random_02 |
|
2 ms | 4 MB |
| clang++ | small_random_03 |
|
2 ms | 4 MB |
| clang++ | type0_random_00 |
|
120 ms | 5 MB |
| clang++ | type0_random_01 |
|
131 ms | 5 MB |
| clang++ | type0_random_02 |
|
18 ms | 4 MB |
| clang++ | type0_random_03 |
|
126 ms | 5 MB |
| clang++ | type1_random_00 |
|
428 ms | 32 MB |
| clang++ | type1_random_01 |
|
409 ms | 31 MB |
| clang++ | type1_random_02 |
|
38 ms | 6 MB |
| clang++ | type1_random_03 |
|
310 ms | 22 MB |
| clang++ | type2_random_00 |
|
427 ms | 31 MB |
| clang++ | type2_random_01 |
|
459 ms | 32 MB |
| clang++ | type2_random_02 |
|
157 ms | 12 MB |
| clang++ | type2_random_03 |
|
273 ms | 20 MB |
| clang++ | type3_random_00 |
|
305 ms | 20 MB |
| clang++ | type3_random_01 |
|
444 ms | 31 MB |
| clang++ | type3_random_02 |
|
165 ms | 11 MB |
| clang++ | type3_random_03 |
|
270 ms | 20 MB |