結果

問題 No.3485 Find 495-like Number
コンテスト
ユーザー Kude
提出日時 2026-03-27 22:13:14
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 6,232 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,403 ms
コンパイル使用メモリ 365,800 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2026-03-27 22:13:51
合計ジャッジ時間 12,492 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

namespace FastPrimeFactorization {

template <typename word, typename dword, typename sword>
struct UnsafeMod {
  UnsafeMod() : x(0) {}

  UnsafeMod(word _x) : x(init(_x)) {}

  bool operator==(const UnsafeMod& rhs) const { return x == rhs.x; }

  bool operator!=(const UnsafeMod& rhs) const { return x != rhs.x; }

  UnsafeMod& operator+=(const UnsafeMod& rhs) {
    if ((x += rhs.x) >= mod) x -= mod;
    return *this;
  }

  UnsafeMod& operator-=(const UnsafeMod& rhs) {
    if (sword(x -= rhs.x) < 0) x += mod;
    return *this;
  }

  UnsafeMod& operator*=(const UnsafeMod& rhs) {
    x = reduce(dword(x) * rhs.x);
    return *this;
  }

  UnsafeMod operator+(const UnsafeMod& rhs) const {
    return UnsafeMod(*this) += rhs;
  }

  UnsafeMod operator-(const UnsafeMod& rhs) const {
    return UnsafeMod(*this) -= rhs;
  }

  UnsafeMod operator*(const UnsafeMod& rhs) const {
    return UnsafeMod(*this) *= rhs;
  }

  UnsafeMod pow(uint64_t e) const {
    UnsafeMod ret(1);
    for (UnsafeMod base = *this; e; e >>= 1, base *= base) {
      if (e & 1) ret *= base;
    }
    return ret;
  }

  word get() const { return reduce(x); }

  static constexpr int word_bits = sizeof(word) * 8;

  static word modulus() { return mod; }

  static word init(word w) { return reduce(dword(w) * r2); }

  static void set_mod(word m) {
    mod = m;
    inv = mul_inv(mod);
    r2 = -dword(mod) % mod;
  }

  static word reduce(dword x) {
    word y =
        word(x >> word_bits) - word((dword(word(x) * inv) * mod) >> word_bits);
    return sword(y) < 0 ? y + mod : y;
  }

  static word mul_inv(word n, int e = 6, word x = 1) {
    return !e ? x : mul_inv(n, e - 1, x * (2 - x * n));
  }

  static word mod, inv, r2;

  word x;
};

using uint128_t = __uint128_t;

using Mod64 = UnsafeMod<uint64_t, uint128_t, int64_t>;
template <>
uint64_t Mod64::mod = 0;
template <>
uint64_t Mod64::inv = 0;
template <>
uint64_t Mod64::r2 = 0;

using Mod32 = UnsafeMod<uint32_t, uint64_t, int32_t>;
template <>
uint32_t Mod32::mod = 0;
template <>
uint32_t Mod32::inv = 0;
template <>
uint32_t Mod32::r2 = 0;

bool miller_rabin_primality_test_uint64(uint64_t n) {
  Mod64::set_mod(n);
  uint64_t d = n - 1;
  while (d % 2 == 0) d /= 2;
  Mod64 e{1}, rev{n - 1};
  // http://miller-rabin.appspot.com/  < 2^64
  for (uint64_t a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
    if (n <= a) break;
    uint64_t t = d;
    Mod64 y = Mod64(a).pow(t);
    while (t != n - 1 && y != e && y != rev) {
      y *= y;
      t *= 2;
    }
    if (y != rev && t % 2 == 0) return false;
  }
  return true;
}

bool miller_rabin_primality_test_uint32(uint32_t n) {
  Mod32::set_mod(n);
  uint32_t d = n - 1;
  while (d % 2 == 0) d /= 2;
  Mod32 e{1}, rev{n - 1};
  for (uint32_t a : {2, 7, 61}) {
    if (n <= a) break;
    uint32_t t = d;
    Mod32 y = Mod32(a).pow(t);
    while (t != n - 1 && y != e && y != rev) {
      y *= y;
      t *= 2;
    }
    if (y != rev && t % 2 == 0) return false;
  }
  return true;
}

bool is_prime(uint64_t n) {
  if (n == 2) return true;
  if (n == 1 || n % 2 == 0) return false;
  if (n < uint64_t(1) << 31) return miller_rabin_primality_test_uint32(n);
  return miller_rabin_primality_test_uint64(n);
}

uint64_t pollard_rho(uint64_t n) {
  if (is_prime(n)) return n;
  if (n % 2 == 0) return 2;
  Mod64::set_mod(n);
  uint64_t d;
  Mod64 one{1};
  for (Mod64 c{one};; c += one) {
    Mod64 x{2}, y{2};
    do {
      x = x * x + c;
      y = y * y + c;
      y = y * y + c;
      d = __gcd((x - y).get(), n);
    } while (d == 1);
    if (d < n) return d;
  }
  assert(0);
}

vector<uint64_t> prime_factor(uint64_t n) {
  if (n <= 1) return {};
  uint64_t p = pollard_rho(n);
  if (p == n) return {p};
  auto l = prime_factor(p);
  auto r = prime_factor(n / p);
  copy(begin(r), end(r), back_inserter(l));
  return l;
}
};  // namespace FastPrimeFactorization

pair<vector<int>, vector<int>> primes_lpf(const int n) {
  vector<int> primes; primes.reserve(n / 10);
  vector<int> lpf(n + 1);
  for (int i = 2; i <= n; i += 2) lpf[i] = 2;
  for (int i = 3; i <= n; i += 6) lpf[i] = 3;
  if (2 <= n) primes.push_back(2);
  if (3 <= n) primes.push_back(3);
  // 5 * x <= n, x <= floor(n / 5)
  const int n5 = n / 5;
  int x = 5;
  char add_next = 2;
  for (; x <= n5; x += add_next, add_next ^= 0x2 ^ 4) {
    int px = lpf[x];
    if (px == 0) {
      lpf[x] = px = x;
      primes.push_back(x);
    }
    for (int i = 2;; ++i) {
      int q = primes[i];
      int y = q * x;
      if (y > n) break;
      lpf[y] = q;
      if (q == px) break;
    }
  }
  for (; x <= n; x += add_next, add_next ^= 0x2 ^ 4) {
    if (lpf[x] == 0) {
      lpf[x] = x;
      primes.push_back(x);
    }
  }
  return {move(primes), move(lpf)};
}

constexpr int PSIZE = 1000000;
auto [primes, lpf] = primes_lpf(PSIZE);

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll l, r;
  cin >> l >> r;
  if (r - l <= 1000 * 45) {
    for (ll x = l; x <= r; x++) {
      auto ps = FastPrimeFactorization::prime_factor(x);
      sort(all(ps));
      if (ps.size() == 4 && ps[0] != 2 && ps[0] == ps[1] && ps[1] != ps[2] && ps[2] != ps[3]) {
        cout << x << '\n';
        return 0;
      }
    }
    cout << -1 << '\n';
    return 0;
  }
  // int a = 3, b = 5;
  // l <= 45x <= r
  l = (l + 44) / 45;
  r = r / 45;
  for (ll x = max(7LL, l); x <= r; x++) {
    if (FastPrimeFactorization::miller_rabin_primality_test_uint64(x)) {
      cout << x * 45 << '\n';
      return 0;
    }
  }
  assert(false);
}
0