QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93843#5817. 小学生数学题nhuang685100 ✓678ms92084kbC++173.8kb2023-04-03 07:07:432023-04-03 07:07:44

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-03 07:07:44]
  • 评测
  • 测评结果:100
  • 用时:678ms
  • 内存:92084kb
  • [2023-04-03 07:07:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

pair<int64_t, int64_t> ex_eucl(int64_t a, int64_t b) {
  if (a < b) {
    auto [x, y] = ex_eucl(b, a);
    return {y, x};
  }
  if (b == 0) {
    assert(a == 1);
    return {1, 0};
  }
  auto [x, y] = ex_eucl(b, a % b);
  return {y, x - (a / b) * y};
}

template <class Md>
class Mod {
 private:
  using T = typename decay<decltype(Md::value)>::type;
  T val = 0;

 public:
  template <class U>
  static T normalize(U val) {
    if (val <= -Md::value || Md::value <= val) val %= Md::value;
    if (val < 0) val += Md::value;
    return static_cast<T>(val);
  }

  Mod() : val(0) {}
  template <class U>
  Mod(U _val) {
    val = normalize(_val);
  }

  // addition
  Mod &operator+=(Mod b) {
    val += b.val;
    if (val >= Md::value) val -= Md::value;
    return *this;
  }
  friend Mod operator+(Mod a, Mod b) { return (a += b); }
  Mod &operator++() { return (*this += 1); }
  Mod operator++(int) {
    Mod res = *this;
    ++(*this);
    return res;
  }

  // subtraction
  Mod &operator-=(Mod b) {
    val -= b.val;
    if (val < 0) val += Md::value;
    return *this;
  }
  friend Mod operator-(Mod a, Mod b) { return (a -= b); }
  Mod &operator--() { return (*this -= 1); }
  Mod operator--(int) {
    Mod res = *this;
    --(*this);
    return res;
  }

  // multiplication
  Mod &operator*=(Mod b) {
    val = static_cast<T>(static_cast<int64_t>(val) * b.val % Md::value);
    return *this;
  }
  friend Mod operator*(Mod a, Mod b) { return (a *= b); }

  // division
  template <class U>
  Mod binpow(U b) const {
    // assumes Mod(0).binpow(0) == 1
    Mod res = 1, a = *this;
    while (b > 0) {
      if (b % 2 == 1) res *= a;
      a *= a;
      b /= 2;
    }
    return res;
  }
  Mod inv() const {
    return Mod(
        ex_eucl(static_cast<int64_t>(val), static_cast<int64_t>(Md::value))
            .first);
  }
  Mod operator/=(Mod b) { return (*this *= b.inv()); }
  friend Mod operator/(Mod a, Mod b) { return (a /= b); }

  // comparison
  bool operator==(Mod b) const { return (val == b.val); }
  // strong_ordering operator<=>(Mod b) { return (val <=> b.val); }
  bool operator!=(Mod b) const { return (val != b.val); }
  bool operator<(Mod b) const { return (val < b.val); }
  bool operator>(Mod b) const { return (val > b.val); }
  bool operator<=(Mod b) const { return (val <= b.val); }
  bool operator>=(Mod b) const { return (val >= b.val); }

  // io
  friend istream &operator>>(istream &in, Mod &a) {
    int64_t val;
    cin >> val;
    a = Mod(val);
    return in;
  }
  friend ostream &operator<<(ostream &out, const Mod a) {
    out << a.val;
    return out;
  }

  // conversion
  explicit operator T() { return val; }
  const T &operator()() { return val; }
};

constexpr int MOD = 998244353;
using Mint = Mod<integral_constant<decay<decltype(MOD)>::type, MOD>>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

#ifndef LOCAL
  // freopen("math.in", "r", stdin);
  // freopen("math.out", "w", stdout);
  // ifstream cin("math.in");
  // ofstream cout("math.out");
#endif

  int n, k;
  cin >> n >> k;

  vector<bool> p(n + 1, true);
  vector<int> primes;
  vector<Mint> inv(n + 1);
  inv[1] = Mint(1).inv();
  for (int i = 2; i <= n; ++i) {
    if (p[i]) {
      inv[i] = Mint(i).binpow((int64_t)k * (MOD - 2));
      primes.push_back(i);
      for (int j = 2 * i; j <= n; j += i) {
        p[j] = false;
      }
    }
    for (int prime : primes) {
      if (i * prime > n) break;
      inv[i * prime] = inv[i] * inv[prime];
      if (i % prime == 0) break;
    }
  }

  Mint fac = 1, ans = 0;
  for (int i = 1; i <= n; ++i) {
    fac *= i;
    ans += fac * inv[i];
  }
  cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 262ms
memory: 47372kb

input:

9450395 1

output:

688545438

result:

ok single line: '688545438'

Test #2:

score: 10
Accepted
time: 246ms
memory: 44912kb

input:

8978812 1

output:

334565356

result:

ok single line: '334565356'

Test #3:

score: 10
Accepted
time: 241ms
memory: 44068kb

input:

8944235 1

output:

982802915

result:

ok single line: '982802915'

Test #4:

score: 10
Accepted
time: 200ms
memory: 33672kb

input:

7081118 3

output:

599009773

result:

ok single line: '599009773'

Test #5:

score: 10
Accepted
time: 225ms
memory: 40364kb

input:

7904241 3

output:

871243720

result:

ok single line: '871243720'

Test #6:

score: 10
Accepted
time: 282ms
memory: 49164kb

input:

9921275 3

output:

549818101

result:

ok single line: '549818101'

Test #7:

score: 10
Accepted
time: 594ms
memory: 83204kb

input:

17575748 14135489

output:

69236780

result:

ok single line: '69236780'

Test #8:

score: 10
Accepted
time: 678ms
memory: 92084kb

input:

19858362 14822524

output:

239890381

result:

ok single line: '239890381'

Test #9:

score: 10
Accepted
time: 632ms
memory: 87488kb

input:

18848696 15530895

output:

88125041

result:

ok single line: '88125041'

Test #10:

score: 10
Accepted
time: 613ms
memory: 84600kb

input:

17787945 13890407

output:

989967864

result:

ok single line: '989967864'

Extra Test:

score: 0
Extra Test Passed