結果

問題 No.368 LCM of K-products
ユーザー Min_25Min_25
提出日時 2016-05-06 13:25:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 6,228 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 110,060 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-22 14:46:30
合計ジャッジ時間 2,425 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 7 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 4 ms
6,940 KB
testcase_16 AC 4 ms
6,940 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 3 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cassert>
#include <cmath>

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

#define _fetch(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _fetch(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

#define getchar getchar_unlocked
#define putchar putchar_unlocked

using namespace std;

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u8 = unsigned char;
using f80 = long double;
using f64 = double;

using u128 = __uint128_t;

template <typename word, typename dword>
struct Mod {
  Mod() : n_(0) {}
  Mod(word n) : n_(init(n)) {}

  static void init_mod(word m) { mod = m, inv = -mul_inv(m), r2 = dword(-m) * (-m) % m; }
  static word mul_inv(word n) {
    word x = n;
    rep(_, 5) x *= 2 - n * x;
    return x;
  }
  static word reduce(dword w) {
    word x = word(w) * inv;
    word y = (dword(x) * mod + w) >> (8 * sizeof(word));
    return (y >= mod) ? y - mod : y;
  }
  static word init(word n) { return reduce(dword(n) * r2); }
  static word ilog2(word n) { return (n == 0) ? 0 : (63 - __builtin_clzll(n)); }

  Mod& operator += (Mod rhs) { if ((n_ += rhs.n_) >= mod) n_ -= mod; return *this; }
  Mod& operator -= (Mod rhs) { if ((n_ += mod - rhs.n_) >= mod) n_ -= mod; return *this; }
  Mod& operator *= (Mod rhs) { n_ = reduce(dword(n_) * rhs.n_); return *this; }

  bool operator == (Mod rhs) { return n_ == rhs.n_; }
  bool operator != (Mod rhs) { return !(*this == rhs); }

  Mod operator + (Mod rhs) { return Mod(*this) += rhs; }
  Mod operator - (Mod rhs) { return Mod(*this) -= rhs; }
  Mod operator * (Mod rhs) { return Mod(*this) *= rhs; }

  Mod operator - () { return (n_ == 0) ? *this : Mod() - *this; };

  Mod pow(word e) {
    if (e == 0) return Mod(1);
    Mod ret = Mod(*this);
    for (word mask = (word(1) << ilog2(e)) >> 1; mask > 0; mask >>= 1) {
      ret *= ret;
      if (e & mask) ret *= *this;
    }
    return ret;
  }
  word val() { return reduce(n_); }
  friend ostream& operator << (ostream& os, Mod& m) { return os << m.val(); }

  word n_;
  static word mod, inv, r2;
};
using word = u32;
using dword = u64;
using mint = Mod<word, dword>;
template <> word mint::mod = 0;
template <> word mint::inv = 0;
template <> word mint::r2 = 0;

template <typename fint, typename mint>
class Factor {
private:
  struct ExactDiv {
    ExactDiv() {}
    ExactDiv(fint n) : n(n), i(mint::mul_inv(n)), t(fint(-1) / n) {}
    friend fint operator / (fint n, ExactDiv d) { return n * d.i; };
    bool divide(fint n) { return n / *this <= this->t; }
    fint n, i, t;
  };

  vector<ExactDiv> primes;

public:
  Factor(u32 n) { init(n); }

  void init(u32 n) {
    u32 sqrt_n = sqrt(n);
    vector<u8> isprime(n + 1, 1);
    rep(i, 2, sqrt_n + 1) if (isprime[i]) rep(j, i * i, n + 1, i) isprime[j] = 0;

    primes.clear();
    rep(i, 2, n + 1) if (isprime[i]) primes.push_back(ExactDiv(i));
  }

  fint brent(fint n, fint c) {
    // n must be composite and odd.
    mint::init_mod(n);

    const fint s = 256;
    const mint one = mint(1);
    auto f = [&](mint x) { return x * x + c; };
    mint y = one;
    for (fint l = 1; ; l <<= 1) {
      auto x = y;
      rep(_, l) y = f(y);
      mint p = one;
      rep(k, 0, l, s) {
        rep(_, min(s, l - k)) y = f(y), p *= y - x;
        fint g = gcd(n, p.n_);
        if (g == 1) continue;
        if (g == n) for (g = 1; g == 1; ) y = f(y), g = gcd(n, (y - x).n_);
        return g;
      }
    }
  }

  bool miller_rabin(fint n) {
    if (!(n & 1)) return n == 2;
    if (n <= 8) return true;

    mint::init_mod(n);
    fint d = n - 1;
    fint s = __builtin_ctzll(d);
    d >>= s;

    mint one = mint(1);
    auto composite = [&](mint b) {
      b = b.pow(d);
      if (b == one || b == -one) return false;
      rep(_, s - 1) {
        b *= b; if (b == -one) return false;
      }
      return true;
    };

    fint bases[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    rep(i, 7) {
      mint b = mint(bases[i] % n);
      if (b == 0) return true;
      if (composite(b)) return false;
    }
    return true;
  }

  using P = pair<fint, u32>;
  vector<P> factors(fint n) {
    assert(n < (fint(-1) >> 1));

    auto ret = vector<P>();

    if (!(n & 1)) {
      u32 e = ctz(n);
      ret.emplace_back(2, e);
      n >>= e;
    }

    fint lim = square(primes[primes.size()-1].n);
    rep(pi, 1, primes.size()) {
      auto p = primes[pi];
      if (square(p.n) > n) break;
      if (p.divide(n)) {
        u32 e = 1; n = n / p;
        while (p.divide(n)) n = n / p, e++;
        ret.emplace_back(p.n, e);
      }
    }

    u32 s = ret.size();
    while (n > lim && !miller_rabin(n)) { 
      for (fint c = 1; ; ++c) {
        fint p = brent(n, c);
        if (!miller_rabin(p)) continue;
        u32 e = 1; n /= p;
        while (n % p == 0) {
          n /= p; e += 1;
        }
        ret.emplace_back(p, e);
        break;
      }
    }
    if (n > 1) ret.emplace_back(n, 1);
    if (ret.size() - s >= 2) sort(ret.begin() + s, ret.end());
    return ret;
  }

private:
  fint gcd(fint a, fint b) {
    while (b) { fint t = a % b; a = b; b = t; }
    return a;
  }

  fint ctz(fint n) {
    return __builtin_ctzll(n);
  }

  fint square(fint n) {
    return n * n;
  }

};

void solve() {
  auto f = Factor<u32, mint>(1000);

  const u32 MOD = 1e9 + 7;
  u32 n, k;
  while (~scanf("%u %u", &n, &k)) {
    map<u32, vector<u32> > cnts;
    rep(i, n) {
      u32 a; scanf("%u", &a);
      for (auto& p : f.factors(a)) cnts[p.first].push_back(p.second);
    }
    u32 ans = 1;
    for (auto& pp : cnts) {
      auto p = pp.first;
      auto& v = pp.second;
      sort(v.begin(), v.end());
      u32 e = 0;
      rep(i, min(u32(v.size()), k)) {
        e += v[v.size() - 1 - i];
      }
      rep(i, e) ans = u64(ans) * p % MOD;
    }
    printf("%u\n", ans);
  }
}

int main() {
  // clock_t beg = clock();
  solve();
  // clock_t end = clock();
  // fprintf(stderr, "%.3f sec.\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
0