結果

問題 No.1781 LCM
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-12-10 00:50:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,087 ms / 5,000 ms
コード長 5,456 bytes
コンパイル時間 1,154 ms
コンパイル使用メモリ 103,196 KB
実行使用メモリ 8,824 KB
最終ジャッジ日時 2024-06-24 21:05:02
合計ジャッジ時間 21,862 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
7,892 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
7,888 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 3 ms
7,760 KB
testcase_21 AC 3,085 ms
6,944 KB
testcase_22 AC 3,059 ms
7,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 3,087 ms
6,940 KB
testcase_26 AC 3,050 ms
7,632 KB
testcase_27 AC 3,051 ms
8,192 KB
testcase_28 AC 2,569 ms
8,824 KB
testcase_29 AC 657 ms
6,944 KB
testcase_30 AC 688 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 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