結果

問題 No.1574 Swap and Repaint
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-08-07 04:28:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 10,000 ms
コード長 13,329 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 115,612 KB
実行使用メモリ 18,652 KB
最終ジャッジ日時 2024-10-03 12:37:50
合計ジャッジ時間 6,151 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,412 KB
testcase_01 AC 7 ms
9,412 KB
testcase_02 AC 8 ms
9,556 KB
testcase_03 AC 7 ms
9,448 KB
testcase_04 AC 8 ms
9,600 KB
testcase_05 AC 7 ms
9,432 KB
testcase_06 AC 8 ms
9,416 KB
testcase_07 AC 8 ms
9,416 KB
testcase_08 AC 7 ms
9,472 KB
testcase_09 AC 9 ms
9,600 KB
testcase_10 AC 8 ms
9,600 KB
testcase_11 AC 7 ms
9,596 KB
testcase_12 AC 8 ms
9,580 KB
testcase_13 AC 9 ms
9,564 KB
testcase_14 AC 8 ms
9,600 KB
testcase_15 AC 9 ms
9,600 KB
testcase_16 AC 8 ms
9,548 KB
testcase_17 AC 8 ms
9,592 KB
testcase_18 AC 73 ms
11,772 KB
testcase_19 AC 274 ms
18,400 KB
testcase_20 AC 68 ms
11,952 KB
testcase_21 AC 65 ms
11,800 KB
testcase_22 AC 63 ms
11,456 KB
testcase_23 AC 21 ms
10,104 KB
testcase_24 AC 235 ms
16,228 KB
testcase_25 AC 129 ms
13,888 KB
testcase_26 AC 14 ms
9,728 KB
testcase_27 AC 55 ms
10,944 KB
testcase_28 AC 276 ms
18,652 KB
testcase_29 AC 272 ms
18,520 KB
testcase_30 AC 270 ms
18,648 KB
testcase_31 AC 270 ms
18,644 KB
testcase_32 AC 268 ms
18,648 KB
testcase_33 AC 271 ms
18,652 KB
testcase_34 AC 282 ms
18,524 KB
testcase_35 AC 271 ms
18,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353U;
constexpr unsigned MO2 = 2U * MO;
constexpr int FFT_MAX = 23;
using Mint = ModInt<MO>;
constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U};
constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U};
constexpr Mint FFT_RATIOS[FFT_MAX] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U};
constexpr Mint INV_FFT_RATIOS[FFT_MAX] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U};

// as[rev(i)] <- \sum_j \zeta^(ij) as[j]
void fft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = n;
  if (m >>= 1) {
    for (int i = 0; i < m; ++i) {
      const unsigned x = as[i + m].x;  // < MO
      as[i + m].x = as[i].x + MO - x;  // < 2 MO
      as[i].x += x;  // < 2 MO
    }
  }
  if (m >>= 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned x = (prod * as[i + m]).x;  // < MO
        as[i + m].x = as[i].x + MO - x;  // < 3 MO
        as[i].x += x;  // < 3 MO
      }
      prod *= FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  for (; m; ) {
    if (m >>= 1) {
      Mint prod = 1U;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i + m].x = as[i].x + MO - x;  // < 4 MO
          as[i].x += x;  // < 4 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
    if (m >>= 1) {
      Mint prod = 1U;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
          as[i + m].x = as[i].x + MO - x;  // < 3 MO
          as[i].x += x;  // < 3 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
    as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x;  // < MO
  }
}

// as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)]
void invFft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = 1;
  if (m < n >> 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
    m <<= 1;
  }
  for (; m < n >> 1; m <<= 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + (m >> 1); ++i) {
        const unsigned long long y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
        as[i].x += as[i + m].x;  // < 4 MO
        as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      for (int i = i0 + (m >> 1); i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  if (m < n) {
    for (int i = 0; i < m; ++i) {
      const unsigned y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
      as[i].x += as[i + m].x;  // < 4 MO
      as[i + m].x = y;  // < 4 MO
    }
  }
  const Mint invN = Mint(n).inv();
  for (int i = 0; i < n; ++i) {
    as[i] *= invN;
  }
}

void fft(vector<Mint> &as) {
  fft(as.data(), as.size());
}
void invFft(vector<Mint> &as) {
  invFft(as.data(), as.size());
}

vector<Mint> convolve(vector<Mint> as, vector<Mint> bs) {
  if (as.empty() || bs.empty()) return {};
  const int len = as.size() + bs.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  bs.resize(n); fft(bs);
  for (int i = 0; i < n; ++i) as[i] *= bs[i];
  invFft(as);
  as.resize(len);
  return as;
}
vector<Mint> square(vector<Mint> as) {
  if (as.empty()) return {};
  const int len = as.size() + as.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  for (int i = 0; i < n; ++i) as[i] *= as[i];
  invFft(as);
  as.resize(len);
  return as;
}
////////////////////////////////////////////////////////////////////////////////


constexpr int LIM = 300'010;
Mint inv[LIM], fac[LIM], invFac[LIM];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(Int n, Int k) {
  if (n < 0) {
    if (k >= 0) {
      return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
    } else {
      return 0;
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM);
      return fac[n] * invFac[k] * invFac[n - k];
    } else {
      return 0;
    }
  }
}


/*
  A(x) := \sum_{j=0}^{N-1} A[j] x^j
  B(x) := x^-1 + (N - 3) x^0 + x^1
  C[i][j] := [x^j] (A(x) B(x)^i)  (0 <= i <= N, -N <= j <= 2 N - 1)
  ans[i] := \sum_{j=-N}^{2N-1} C[i][j] D[j]
  
  D[j] := (contribution of balls at j)
  D[-1 - j] = D[j] = D[2 N - 1 - j]  (0 <= j <= N - 1)
  
  transpose
  for e[i]  (0 <= i <= N)
  \sum_{i=0}^N C[i][j] e[i] = [x^j] (A(x) \sum_{i=0}^N e[i] B(x)^i)
  DC
*/

/*
  [0, n] * [0, n - m + 1]
  
  [ a[0]                     ]
  [ ...    a[0]              ]
  [ a[m-1] ...               ]
  [        a[m-1]            ]
  [               ...        ]
  [                   a[0]   ]
  [                   ...    ]
  [                   a[m-1] ]
  
  [x^j] (rev(a) b)  m - 1 <= j <= n - 1
*/
vector<Mint> middle(vector<Mint> as, vector<Mint> bs) {
  const int m = as.size();
  const int n = bs.size();
  assert(m <= n);
  int nn = 1;
  for (; nn < n; nn <<= 1) {}
  reverse(as.begin(), as.end());
  as.resize(nn, 0);
  fft(as);
  bs.resize(nn, 0);
  fft(bs);
  for (int i = 0; i < nn; ++i) {
    bs[i] *= as[i];
  }
  invFft(bs);
  bs.resize(n);
  bs.erase(bs.begin(), bs.begin() + (m - 1));
  return bs;
}

Mint two[LIM], invTwo[LIM];

int N;
vector<int> A;

vector<Mint> ans;

/*
  \sum_{i=l}^{r-1} e[i] B(x)^(i-l)
*/
void solve(int l, int r, const vector<Mint> &zs) {
  assert((int)zs.size() == 2 * (r - l - 1) + 1);
  if (r - l == 1) {
    ans[l] = zs[0];
  } else {
    const int mid = (l + r) / 2;
    const int nL = mid - l;
    const int nR = r - mid;
    
    /*
      [-(nL + nR - 1), nL + nR - 1] * ([-(nL - 1), (nL - 1)], [-(nL + nR - 1), nL + nR - 1])
      
      [       | 1         ]
      [ 1     |   1       ]
      [   1   |     1     ]
      [     1 |       1   ]
      [       |         1 ]
    */
    vector<Mint> zsL(2 * (nL - 1) + 1);
    for (int i = 0; i <= 2 * (nL - 1); ++i) {
      zsL[i] = zs[nR + i];
    }
    
    /*
      B(x)^nL
    */
    vector<Mint> bs(2 * nL + 1);
    bs[0] = 1;
    bs[1] = nL * Mint(N - 3);
    for (int j = 2; j <= 2 * nL; ++j) {
      bs[j] = inv[j] * ((nL - (j - 1)) * Mint(N - 3) * bs[j - 1] + (nL * 2 - (j - 2)) * bs[j - 2]);
    }
// cerr<<"bs = ";pv(bs.begin(),bs.end());
    
    /*
      [-(nL + nR - 1), nL + nR - 1] * [-(nR - 1), nR - 1]
    */
    const auto zsR = middle(bs, zs);
    
    solve(l, mid, zsL);
    solve(mid, r, zsR);
  }
}

int main() {
  prepare();
  two[0] = invTwo[0] = 1;
  for (int i = 1; i < LIM; ++i) {
    two[i] = two[i - 1] * 2;
    invTwo[i] = invTwo[i - 1] * inv[2];
  }
  
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    for (int j = 0; j < N; ++j) {
      scanf("%d", &A[j]);
    }
    
    /*
      overwritten: (1/2^(j-k+1)) * (j - k) (1/(j-k+1))!  (k < j < N - 1)
      not overwritten: (1/2^(j-k+[j<N-1])) * (1/(j-k)!)  (k <= j)
    */
    vector<Mint> D(N, 0);
    {
      Mint sum = 0;
      for (int j = 0; j < N - 1; ++j) {
        sum += invTwo[j + 1] * j * invFac[j + 1];
        sum += invTwo[j + 1] * invFac[j];
        D[j] = sum;
      }
    }
    for (int k = 0; k <= N - 1; ++k) {
      D[N - 1] += invTwo[N - 1 - k] * invFac[N - 1 - k];
    }
    {
      const Mint all = two[2 * (N - 1)] * fac[N - 1];
      for (int j = 0; j < N; ++j) {
        D[j] *= all;
      }
    }
// cerr<<"D = ";pv(D.begin(),D.end());
// {Mint ans0=0;for(int j=0;j<N;++j){ans0+=A[j]*D[j];}cerr<<"ans0 = "<<ans0<<endl;}
    
    
    /*
      [-N, 2 N - 1] * [-N, N]
    */
    vector<Mint> as(N), ds(3 * N);
    for (int j = 0; j < N; ++j) {
      as[j] = A[j];
      ds[N - 1 - j] = ds[N + j] = ds[3 * N - 1 - j] = D[j];
    }
    const auto zs = middle(as, ds);
    ans.assign(N + 1, 0);
    solve(0, N + 1, zs);
    for (int i = 0; i <= N; ++i) {
      printf("%u\n", ans[i].x);
    }
  }
  return 0;
}
0