結果

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

ソースコード

diff #
raw source code

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

const int MAXP = 1000010;
vector<int> primes;
bool is_composite[MAXP];

void sieve() {
    memset(is_composite, 0, sizeof(is_composite));
    is_composite[0] = is_composite[1] = true;
    for (ll i = 2; i < MAXP; i++) {
        if (!is_composite[i]) {
            primes.push_back((int)i);
            for (ll j = i * i; j < MAXP; j += i)
                is_composite[j] = true;
        }
    }
}

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

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

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

        long long ans = -1;

        // 1. Try every prime p > k
        for (int p : primes) {
            if (p <= k) continue;
            if (p > n) break;
            ans = max(ans, (long long)(n / p) * p);
        }

        // 2. If n has a prime factor > k (could be a large prime itself)
        int rem = n;
        for (int p : primes) {
            if (1LL * p * p > rem) break;
            while (rem % p == 0) rem /= p;
        }
        if (rem > k) ans = max(ans, (long long)n);

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