Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/shift_of_sampling_points_of_polynomial

#include "../../template/template.hpp"

#include "../../math/fft/number-theoretic-transform-friendly-mod-int.hpp"

#include "../../math/combinatorics/lagrange-polynomial-3.hpp"

#include "../../math/combinatorics/montgomery-mod-int.hpp"

using mint = modint998244353;

int main() {
  int N, T, M;
  cin >> N >> T >> M;
  vector< mint > ys(N);
  for(int i = 0; i < N; i++) cin >> ys[i];
  NumberTheoreticTransformFriendlyModInt< mint > v;
  auto multiply = [&](const vector< mint > &a, const vector< mint > &b) { return v.multiply(a, b); };
  cout << lagrange_polynomial(ys, M, T, multiply) << "\n";
}
#line 1 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/shift_of_sampling_points_of_polynomial

#line 1 "template/template.hpp"
#include<bits/stdc++.h>

using namespace std;

using int64 = long long;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};
 
template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
#line 4 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"

#line 1 "math/fft/number-theoretic-transform-friendly-mod-int.hpp"
/**
 * @brief Number Theoretic Transform Friendly ModInt
 */
template< typename Mint >
struct NumberTheoreticTransformFriendlyModInt {

  static vector< Mint > roots, iroots, 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);
      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 - 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(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);
      Mint rot = 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(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;
    }
  }

  static vector< Mint > multiply(vector< Mint > a, 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 >
vector< Mint > NumberTheoreticTransformFriendlyModInt< Mint >::roots = vector< Mint >();
template< typename Mint >
vector< Mint > NumberTheoreticTransformFriendlyModInt< Mint >::iroots = vector< Mint >();
template< typename Mint >
vector< Mint > NumberTheoreticTransformFriendlyModInt< Mint >::rate3 = vector< Mint >();
template< typename Mint >
vector< Mint > NumberTheoreticTransformFriendlyModInt< Mint >::irate3 = vector< Mint >();
template< typename Mint >
int NumberTheoreticTransformFriendlyModInt< Mint >::max_base = 0;
#line 6 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"

#line 1 "math/combinatorics/lagrange-polynomial-3.hpp"
/**
 * @brief Lagrange Polynomial(多項式補間, 値)
 */
template< typename Mint, typename F >
vector< Mint > lagrange_polynomial(const vector< Mint > &y, int64_t T, const int &m, const F &multiply) {
  int k = (int) y.size() - 1;
  T %= Mint::mod();
  if(T <= k) {
    vector< Mint > ret(m);
    int ptr = 0;
    for(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);
    copy(begin(suf), end(suf), back_inserter(pref));
    return pref;
  }
  
  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];
  }

  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);

  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 8 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"

#line 2 "math/combinatorics/montgomery-mod-int.hpp"

template< uint32_t mod_, bool fast = false >
struct MontgomeryModInt {
private:
  using mint = MontgomeryModInt;
  using i32 = int32_t;
  using i64 = int64_t;
  using u32 = uint32_t;
  using u64 = 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 ostream &operator<<(ostream &os, const mint &p) {
    return os << p.val();
  }

  friend istream &operator>>(istream &is, mint &a) {
    i64 t;
    is >> t;
    a = mint(t);
    return is;
  }

  static constexpr u32 mod() { return mod_; }
};

template< uint32_t mod >
using modint = MontgomeryModInt< mod >;
using modint998244353 = modint< 998244353 >;
using modint1000000007 = modint< 1000000007 >;
#line 10 "test/verify/yosupo-shift-of-sampling-points-of-polynomial.test.cpp"

using mint = modint998244353;

int main() {
  int N, T, M;
  cin >> N >> T >> M;
  vector< mint > ys(N);
  for(int i = 0; i < N; i++) cin >> ys[i];
  NumberTheoreticTransformFriendlyModInt< mint > v;
  auto multiply = [&](const vector< mint > &a, const vector< mint > &b) { return v.multiply(a, b); };
  cout << lagrange_polynomial(ys, M, T, multiply) << "\n";
}

Test cases

Env Name Status Elapsed Memory
g++ N_1_00 :heavy_check_mark: AC 114 ms 11 MB
g++ c_0_00 :heavy_check_mark: AC 190 ms 21 MB
g++ example_00 :heavy_check_mark: AC 7 ms 4 MB
g++ example_01 :heavy_check_mark: AC 6 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 368 ms 34 MB
g++ max_random_01 :heavy_check_mark: AC 369 ms 34 MB
g++ max_random_02 :heavy_check_mark: AC 369 ms 34 MB
g++ max_random_03 :heavy_check_mark: AC 369 ms 34 MB
g++ medium_random_00 :heavy_check_mark: AC 13 ms 4 MB
g++ medium_random_01 :heavy_check_mark: AC 12 ms 4 MB
g++ medium_random_02 :heavy_check_mark: AC 12 ms 4 MB
g++ medium_random_03 :heavy_check_mark: AC 12 ms 4 MB
g++ small_random_00 :heavy_check_mark: AC 6 ms 4 MB
g++ small_random_01 :heavy_check_mark: AC 6 ms 4 MB
g++ small_random_02 :heavy_check_mark: AC 6 ms 4 MB
g++ small_random_03 :heavy_check_mark: AC 6 ms 4 MB
g++ type0_random_00 :heavy_check_mark: AC 37 ms 5 MB
g++ type0_random_01 :heavy_check_mark: AC 38 ms 5 MB
g++ type0_random_02 :heavy_check_mark: AC 10 ms 4 MB
g++ type0_random_03 :heavy_check_mark: AC 43 ms 6 MB
g++ type1_random_00 :heavy_check_mark: AC 319 ms 32 MB
g++ type1_random_01 :heavy_check_mark: AC 296 ms 31 MB
g++ type1_random_02 :heavy_check_mark: AC 30 ms 6 MB
g++ type1_random_03 :heavy_check_mark: AC 213 ms 22 MB
g++ type2_random_00 :heavy_check_mark: AC 329 ms 31 MB
g++ type2_random_01 :heavy_check_mark: AC 344 ms 32 MB
g++ type2_random_02 :heavy_check_mark: AC 130 ms 12 MB
g++ type2_random_03 :heavy_check_mark: AC 178 ms 20 MB
g++ type3_random_00 :heavy_check_mark: AC 212 ms 20 MB
g++ type3_random_01 :heavy_check_mark: AC 326 ms 31 MB
g++ type3_random_02 :heavy_check_mark: AC 149 ms 11 MB
g++ type3_random_03 :heavy_check_mark: AC 176 ms 20 MB
clang++ N_1_00 :heavy_check_mark: AC 123 ms 11 MB
clang++ c_0_00 :heavy_check_mark: AC 207 ms 21 MB
clang++ example_00 :heavy_check_mark: AC 7 ms 4 MB
clang++ example_01 :heavy_check_mark: AC 6 ms 4 MB
clang++ max_random_00 :heavy_check_mark: AC 403 ms 34 MB
clang++ max_random_01 :heavy_check_mark: AC 398 ms 34 MB
clang++ max_random_02 :heavy_check_mark: AC 397 ms 34 MB
clang++ max_random_03 :heavy_check_mark: AC 404 ms 34 MB
clang++ medium_random_00 :heavy_check_mark: AC 14 ms 4 MB
clang++ medium_random_01 :heavy_check_mark: AC 13 ms 4 MB
clang++ medium_random_02 :heavy_check_mark: AC 13 ms 4 MB
clang++ medium_random_03 :heavy_check_mark: AC 13 ms 4 MB
clang++ small_random_00 :heavy_check_mark: AC 6 ms 4 MB
clang++ small_random_01 :heavy_check_mark: AC 6 ms 4 MB
clang++ small_random_02 :heavy_check_mark: AC 6 ms 4 MB
clang++ small_random_03 :heavy_check_mark: AC 6 ms 4 MB
clang++ type0_random_00 :heavy_check_mark: AC 36 ms 5 MB
clang++ type0_random_01 :heavy_check_mark: AC 38 ms 5 MB
clang++ type0_random_02 :heavy_check_mark: AC 11 ms 4 MB
clang++ type0_random_03 :heavy_check_mark: AC 41 ms 6 MB
clang++ type1_random_00 :heavy_check_mark: AC 343 ms 32 MB
clang++ type1_random_01 :heavy_check_mark: AC 317 ms 31 MB
clang++ type1_random_02 :heavy_check_mark: AC 31 ms 6 MB
clang++ type1_random_03 :heavy_check_mark: AC 223 ms 22 MB
clang++ type2_random_00 :heavy_check_mark: AC 349 ms 31 MB
clang++ type2_random_01 :heavy_check_mark: AC 363 ms 32 MB
clang++ type2_random_02 :heavy_check_mark: AC 133 ms 12 MB
clang++ type2_random_03 :heavy_check_mark: AC 189 ms 20 MB
clang++ type3_random_00 :heavy_check_mark: AC 222 ms 20 MB
clang++ type3_random_01 :heavy_check_mark: AC 348 ms 31 MB
clang++ type3_random_02 :heavy_check_mark: AC 154 ms 11 MB
clang++ type3_random_03 :heavy_check_mark: AC 189 ms 20 MB
Back to top page