結果

問題 No.3253 Banned Product
コンテスト
ユーザー vjudge1
提出日時 2025-11-20 22:33:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,504 bytes
コンパイル時間 1,861 ms
コンパイル使用メモリ 198,044 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-11-20 22:33:30
合計ジャッジ時間 2,567 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// Precompute all primes up to 1e6+10
const int MAX = 1000010;
vector<int> primes;
bitset<MAX> isPrime;

void sieve() {
    isPrime.set();
    isPrime[0] = isPrime[1] = 0;
    for (ll i = 2; i < MAX; i++) {
        if (isPrime[i]) {
            primes.push_back(i);
            for (ll j = i * i; j < MAX; j += i)
                isPrime[j] = 0;
        }
    }
}

// Fast largest prime factor
int largest_pf(ll n) {
    if (n <= 1) return 0;
    int res = 0;
    while (n % 2 == 0) { res = 2; n /= 2; }
    for (ll i = 3; i * i <= n; i += 2) {
        while (n % i == 0) { res = i; n /= i; }
    }
    if (n > 1) res = n;
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    sieve();

    int t;
    cin >> t;
    while (t--) {
        ll n, k;
        cin >> n >> k;

        if (n == k) {
            cout << "-1\n";
            continue;
        }

        ll ans = -1;

        // Step 1: Try multiples of all primes > k
        for (int p : primes) {
            if (p <= k) continue;
            if (p > n) break;
            ans = max(ans, n / p * p);
        }

        // Step 2: Check if n itself has a large prime factor
        int temp = n;
        for (int p : primes) {
            if (1LL * p * p > temp) break;
            while (temp % p == 0) temp /= p;
        }
        if (temp > k) {
            ans = max(ans, n);
        }

        cout << ans << "\n";
    }
    return 0;
}
0