QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346551#8329. Excuseucup-team055#WA 4ms5200kbC++203.7kb2024-03-08 17:40:202024-03-08 17:40:21

Judging History

你现在查看的是最新测评结果

  • [2024-03-08 17:40:21]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:5200kb
  • [2024-03-08 17:40:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = LLONG_MAX / 4;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define all(a) begin(a), end(a)
// ll sz(auto a) { return size(a); }
template <class T> bool chmin(T &a, T b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <class T> bool chmax(T &a, T b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}

struct mint {
  static constexpr int m = 998244353;
  int x;
  mint() : x(0) {}
  mint(ll x_) : x(x_ % m) {
    if (x < 0)
      x += m;
  }
  mint &operator+=(mint b) {
    if ((x += b.x) >= m)
      x -= m;
    return *this;
  }
  mint &operator-=(mint b) {
    if ((x -= b.x) < 0)
      x += m;
    return *this;
  }
  mint &operator*=(mint b) {
    x = (ll)(x)*b.x % m;
    return *this;
  }
  mint &operator/=(mint b) { return *this *= b.inv(); }
  mint pow(ll e) const {
    mint r = 1, b = *this;
    while (e) {
      if (e & 1)
        r *= b;
      b *= b;
      e >>= 1;
    }
    return r;
  }
  mint inv() { return pow(m - 2); }
  friend mint operator+(mint a, mint b) { return a += b; }
  friend mint operator-(mint a, mint b) { return a -= b; }
  friend mint operator*(mint a, mint b) { return a *= b; }
  friend mint operator/(mint a, mint b) { return a /= b; }
};

using vm = vector<mint>;
void ntt(vm &a) {
  int n = a.size();
  vm b(n);
  for (int w = n; w /= 2;) {
    mint r = mint(3).pow((mint::m - 1) / n * w), t = 1;
    for (int i = 0; i < n; i += w) {
      int j = i * 2 & n - 1;
      rep(k, 0, w) b[i + k] = a[j + k] + t * a[j + w + k];
      t *= r;
    }
    swap(a, b);
  }
}
void ntt_inv(vm &a) {
  ntt(a);
  reverse(a.begin() + 1, a.end());
  mint i = 1 / mint(a.size());
  for (mint &e : a)
    e *= i;
}

struct onl_conv {
  vector<mint> f, g, h, buff, bufg;
  onl_conv(vector<mint> g_) : f(), g(g_), h(), buff(), bufg() {}
  void add(int fl, int fr, int gl, int gr) {
    const int n = fr - fl;
    buff.assign(f.begin() + fl, f.begin() + fr);
    buff.resize(2 * n);
    ntt(buff);
    if (g.size() < gr)
      g.resize(gr);
    bufg.assign(g.begin() + gl, g.begin() + gr);
    bufg.resize(2 * n);
    ntt(bufg);
    rep(i, 0, 2 * n) buff[i] *= bufg[i];
    ntt_inv(buff);
    if (h.size() <= fl + gl + 2 * n)
      h.resize(fl + gl + 2 * n);
    rep(i, 0, 2 * n) h[fl + gl + i] += buff[i];
  }
  mint push(mint v) {
    f.push_back(v);
    const int i = f.size();
    int tp = i, p = 0;
    while (true) {
      add(i - (1 << p), i, (1 << p) - 1, (1 << p + 1) - 1);
      p++;
      if (tp & 1)
        break;
      else
        tp >>= 1;
    }
    return h[i - 1];
  }
};

void solve() {
  const int LIM = 1 << 18;
  vector<mint> fact(LIM, 1), inv_fact(LIM, 1);
  rep(i, 1, LIM) fact[i] = i * fact[i - 1];
  inv_fact.back() = 1 / fact.back();
  for (int i = LIM - 1; i >= 1; i--)
    inv_fact[i - 1] = i * inv_fact[i];

  const int N = 1 << 8;
  vector<mint> g(N); // (1 - exp(-x/2))/x
  const mint mi2 = 1 / mint(-2);
  rep(i, 1, N) g[i - 1] = 0 - mi2.pow(i) * inv_fact[i];

  // f(x/2)g(x)x+1 = f(x)
  onl_conv oc(g);
  vector<mint> f(N);
  f[0] = 1;
  rep(i, 1, N) {
    mint res = oc.push(f[i - 1] / mint(2).pow(i - 1));
    f[i] = res;
  }

  vector<mint> expf(N);
  rep(i, 0, N) expf[i] = inv_fact[i];
  {
    f.resize(2 * N);
    ntt(f);
    expf.resize(2 * N);
    ntt(expf);
    rep(i, 0, 2 * N) f[i] *= expf[i];
    ntt_inv(f);
    f.resize(N);
  }

  int n;
  cin >> n;
  cout << (fact[n] * f[n] - 1).x << "\n";
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  ll T = 1;
  // cin >> T;
  while (T--)
    solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 5168kb

input:

1

output:

499122177

result:

ok 1 number(s): "499122177"

Test #2:

score: 0
Accepted
time: 4ms
memory: 5200kb

input:

3

output:

561512450

result:

ok 1 number(s): "561512450"

Test #3:

score: 0
Accepted
time: 4ms
memory: 5052kb

input:

10

output:

609769250

result:

ok 1 number(s): "609769250"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 5056kb

input:

1000

output:

983543180

result:

wrong answer 1st numbers differ - expected: '475714976', found: '983543180'