QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#346555#8329. Excuseucup-team055#AC ✓526ms11660kbC++203.7kb2024-03-08 17:41:092024-03-08 17:41:10

Judging History

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

  • [2024-03-08 17:41:10]
  • 评测
  • 测评结果:AC
  • 用时:526ms
  • 内存:11660kb
  • [2024-03-08 17:41:09]
  • 提交

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 << 17;
  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;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 512ms
memory: 11388kb

input:

1

output:

499122177

result:

ok 1 number(s): "499122177"

Test #2:

score: 0
Accepted
time: 519ms
memory: 11524kb

input:

3

output:

561512450

result:

ok 1 number(s): "561512450"

Test #3:

score: 0
Accepted
time: 518ms
memory: 11520kb

input:

10

output:

609769250

result:

ok 1 number(s): "609769250"

Test #4:

score: 0
Accepted
time: 517ms
memory: 11500kb

input:

1000

output:

475714976

result:

ok 1 number(s): "475714976"

Test #5:

score: 0
Accepted
time: 516ms
memory: 11524kb

input:

1024

output:

178624793

result:

ok 1 number(s): "178624793"

Test #6:

score: 0
Accepted
time: 511ms
memory: 11372kb

input:

100000

output:

537516197

result:

ok 1 number(s): "537516197"

Test #7:

score: 0
Accepted
time: 519ms
memory: 11460kb

input:

99471

output:

489775976

result:

ok 1 number(s): "489775976"

Test #8:

score: 0
Accepted
time: 526ms
memory: 11660kb

input:

65536

output:

171446457

result:

ok 1 number(s): "171446457"

Test #9:

score: 0
Accepted
time: 512ms
memory: 11360kb

input:

84792

output:

371578800

result:

ok 1 number(s): "371578800"

Test #10:

score: 0
Accepted
time: 517ms
memory: 11520kb

input:

93846

output:

841905002

result:

ok 1 number(s): "841905002"

Extra Test:

score: 0
Extra Test Passed