結果

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

ソースコード

diff #
raw source code

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

const int MAXP = 1000010;
vector<int> primes;
bitset<MAXP> isp;

void sieve() {
    isp.set(); 
    isp[0] = isp[1] = 0;
    for (long long i = 2; i < MAXP; i++) {
        if (isp[i]) {
            primes.push_back((int)i);
            for (long long j = i * i; j < MAXP; j += i) 
                isp[j] = 0;
        }
    }
}

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 multiples of every prime p > k and p <= n
        for (int p : primes) {
            if (p > k) {
                if (p > n) break;
                ans = max(ans, (long long)(n / p) * p);
            }
        }

        // 2. Check if n itself has a large prime factor > k
        //    (for when n is prime >1e6 or has large prime factor not caught above)
        int x = n;
        for (int p : primes) {
            if ((long long)p * p > x) break;
            while (x % p == 0) x /= p;
        }
        if (x > k) {
            ans = max(ans, (long long)n);
        }

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