結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー Pachicobue
提出日時 2017-10-23 22:53:36
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,242 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 173,736 KB
実行使用メモリ 36,436 KB
最終ジャッジ日時 2024-11-21 15:48:03
合計ジャッジ時間 7,550 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 39 WA * 1 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7;
constexpr ll MAX = 10000000000LL;
constexpr ll SQRT = 100000LL;
bool isprime[SQRT + 1];

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    fill(isprime, isprime + SQRT + 1, true);
    for (ll i = 2; i <= SQRT; i++) {
        if (isprime[i]) {
            for (ll j = 2; i * j <= SQRT; j++) {
                isprime[i * j] = false;
            }
        }
    }
    vector<ll> prime;
    for (ll i = 2; i <= SQRT; i++) {
        if (isprime[i]) {
            prime.push_back(i);
        }
    }
    isprime[1] = true;
    ll L, H;
    cin >> L >> H;
    if (H - L < 40000) {
        ll maxi = 0;
        ll maxnum = 0;
        for (ll i = L; i <= H; i++) {
            for (const ll p : prime) {
                if (i % p == 0 and i != p) {
                    if (maxi <= p) {
                        maxi = p;
                        maxnum = i;
                    }
                    break;
                }
            }
        }
        cout << maxnum << endl;
        return 0;
    }
    reverse(prime.begin(), prime.end());


    vector<ll> cand(1, 1LL);
    for (const ll p : prime) {
        vector<ll> tmp = cand;
        ll maxi = -1;
        for (const ll e : cand) {
            ll num = e;
            while (num * p <= H) {
                num *= p;
                if (num >= L and num != p) {
                    maxi = max(maxi, num);
                }
                tmp.push_back(num);
            }
        }
        if (maxi != -1) {
            cout << maxi << endl;
            return 0;
        }
        cand = tmp;
    }

    return 0;
}
0