結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー kimiyukikimiyuki
提出日時 2016-06-05 19:21:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 1,000 ms
コード長 1,913 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 81,128 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-29 01:08:30
合計ジャッジ時間 3,596 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 8 ms
4,380 KB
testcase_23 AC 12 ms
4,380 KB
testcase_24 AC 60 ms
4,384 KB
testcase_25 AC 60 ms
4,376 KB
testcase_26 AC 67 ms
4,376 KB
testcase_27 AC 60 ms
4,380 KB
testcase_28 AC 82 ms
4,380 KB
testcase_29 AC 59 ms
4,376 KB
testcase_30 AC 58 ms
4,376 KB
testcase_31 AC 59 ms
4,384 KB
testcase_32 AC 59 ms
4,380 KB
testcase_33 AC 57 ms
4,380 KB
testcase_34 AC 53 ms
4,380 KB
testcase_35 AC 55 ms
4,380 KB
testcase_36 AC 34 ms
4,380 KB
testcase_37 AC 59 ms
4,380 KB
testcase_38 AC 53 ms
4,376 KB
testcase_39 AC 59 ms
4,380 KB
testcase_40 AC 57 ms
4,380 KB
testcase_41 AC 54 ms
4,380 KB
testcase_42 AC 50 ms
4,376 KB
testcase_43 AC 60 ms
4,380 KB
testcase_44 AC 49 ms
4,380 KB
testcase_45 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
typedef long long ll;
template <class T> bool setmax(T & l, T const & r) { if (not (l < r)) return false; l = r; return true; }
using namespace std;

vector<int> sieve_of_eratosthenes(int n) { // enumerate primes in [2,n] with O(n log log n)
    vector<bool> is_prime(n+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i*i <= n; ++i)
        if (is_prime[i])
            for (int k = i+i; k <= n; k += i)
                is_prime[k] = false;
    vector<int> primes;
    for (int i = 2; i <= n; ++i)
        if (is_prime[i])
            primes.push_back(i);
    return primes;
}

vector<ll> factors(ll n, vector<int> const & primes) {
    vector<ll> result;
    for (ll p : primes) {
        if (n < p * p) break;
        while (n % p == 0) {
            result.push_back(p);
            n /= p;
        }
    }
    if (n != 1) result.push_back(n);
    return result;
}

int main() {
    ll l, h; cin >> l >> h; // [l, h]
    vector<int> primes = sieve_of_eratosthenes(sqrt(h) * 10);
    pair<ll,ll> ans = { -1, -1 };
    repeat_reverse (i,primes.size()) {
        ll p = primes[i];
        repeat_from (j,i,min<int>(i+1000,primes.size())) {
            ll q = primes[j];
            if (l <= p * q and p * q <= h) {
                setmax(ans, make_pair(p, p * q));
            }
        }
        if (ans.second != -1) break;
    }
    if (ans.second == -1 or h - l + 1 < 10000) {
        for (ll n = l; n < h+1; ++ n) {
            vector<ll> fs = factors(n, primes);
            if (fs.size() != 1) {
                setmax(ans, make_pair(fs.front(), n));
            }
        }
    }
    cout << ans.second << endl;
    return 0;
}
0