結果

問題 No.1781 LCM
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-12-10 00:50:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,185 ms / 5,000 ms
コード長 5,456 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 100,284 KB
実行使用メモリ 8,096 KB
最終ジャッジ日時 2023-09-07 02:23:23
合計ジャッジ時間 23,887 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,656 KB
testcase_01 AC 3 ms
5,532 KB
testcase_02 AC 3 ms
5,564 KB
testcase_03 AC 3 ms
5,664 KB
testcase_04 AC 3 ms
5,532 KB
testcase_05 AC 3 ms
5,880 KB
testcase_06 AC 3 ms
5,596 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
5,588 KB
testcase_09 AC 2 ms
5,596 KB
testcase_10 AC 2 ms
5,848 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
5,532 KB
testcase_14 AC 2 ms
5,660 KB
testcase_15 AC 2 ms
5,584 KB
testcase_16 AC 2 ms
5,504 KB
testcase_17 AC 2 ms
5,520 KB
testcase_18 AC 2 ms
5,552 KB
testcase_19 AC 2 ms
5,616 KB
testcase_20 AC 2 ms
5,524 KB
testcase_21 AC 3,184 ms
7,924 KB
testcase_22 AC 3,185 ms
7,852 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 3,158 ms
7,984 KB
testcase_26 AC 3,147 ms
7,904 KB
testcase_27 AC 3,182 ms
8,096 KB
testcase_28 AC 2,644 ms
7,756 KB
testcase_29 AC 678 ms
6,512 KB
testcase_30 AC 719 ms
6,588 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 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 = 998244353;
using Mint = ModInt<MO>;

// floor(sqrt(a))
long long floorSqrt(long long a) {
  long long b = a, x = 0, y = 0;
  for (int e = (63 - __builtin_clzll(a)) & ~1; e >= 0; e -= 2) {
    x <<= 1;
    y <<= 1;
    if (b >= (y | 1) << e) {
      b -= (y | 1) << e;
      x |= 1;
      y += 2;
    }
  }
  return x;
}


constexpr int LIM = 320'000;

Int N, sqrtN;
bool isPrime[LIM];
int primesLen;
Int primes[LIM];
Mint small[LIM], large[LIM];
Mint get(Int n) {
  return (n <= sqrtN) ? small[n] : large[N / n];
}
void primeSum0() {
  sqrtN = floorSqrt(N);
  fill(isPrime + 2, isPrime + (sqrtN + 1), true);
  primesLen = 0;
  fill(small, small + (sqrtN + 1), 0);
  fill(large, large + (sqrtN + 1), 0);
  for (Int n = 1; n <= sqrtN; ++n) small[n] = n;
  for (Int l = 1; l <= sqrtN; ++l) large[l] = N / l;
  for (Int p = 2; p <= sqrtN; ++p) if (isPrime[p]) {
    primes[primesLen++] = p;
    for (Int n = p * p; n <= sqrtN; n += p) isPrime[n] = false;
    for (Int l = 1; l <= sqrtN; ++l) {
      const Int n = N / l;
      if (n < p * p) break;
      large[l] -= (get(n / p) - small[p - 1]);
    }
    for (Int n = sqrtN; n >= 1; --n) {
      if (n < p * p) break;
      small[n] -= (get(n / p) - small[p - 1]);
    }
  }
  for (Int n = 1; n <= sqrtN; ++n) small[n] -= 1;
  for (Int l = 1; l <= sqrtN; ++l) large[l] -= 1;
}


constexpr int E = 40;

Int K;
Mint pw[E];

Mint ans;
void dfs(int pos, Int n, Mint val, int e) {
  if (pos >= 0) {
    ans += (val * pw[e + 1]);
    const Int nn = n / primes[pos];
    if (nn >= primes[pos]) {
      dfs(pos, nn, val, e + 1);
    }
  }
  ans += (get(n) - pos - 1) * (val * pw[e] * pw[1]);
  for (int i = pos + 1; i < primesLen; ++i) {
    const Int nn = n / primes[i];
    if (nn < primes[i]) {
      break;
    }
    dfs(i, nn, val * pw[e], 1);
  }
}

int main() {
  for (; ~scanf("%lld%lld", &K, &N); ) {
    primeSum0();
    
    for (int e = 0; e < E; ++e) {
      pw[e] = Mint(e + 1).pow(K);
    }
    ans = 1;
    dfs(-1, N, 1, 0);
    printf("%u\n", ans.x);
    fflush(stdout);
  }
  return 0;
}
0