結果

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

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define nl cout << "\n"
#define io ios_base::sync_with_stdio(false); cin.tie(nullptr);

vector<bool> is_prime;

void sieve(int limit = 1000000) {
    is_prime.assign(limit + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i * i <= limit; i++) {
        if (is_prime[i]) {
            for (ll j = i * i; j <= limit; j += i)
                is_prime[j] = false;
        }
    }
}

// Fastest way to find largest prime factor of a number <= 1e9
int largestPF(ll n) {
    if (n <= 1) return 0;
    int maxP = -1;

    // Handle 2
    while (n % 2 == 0) {
        maxP = 2;
        n /= 2;
    }

    // Odd factors
    for (ll i = 3; i * i <= n; i += 2) {
        while (n % i == 0) {
            maxP = i;
            n /= i;
        }
    }
    if (n > 1) maxP = n;

    return maxP;
}

int main() {
    io;
    sieve();

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

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

        bool found = false;
        // Genius part: only check last 10000 numbers (more than enough)
        for (ll x = n; x >= max(2LL, n - 20000); x--) {  // 20k bhi chalega, safe side
            if (largestPF(x) > k) {
                cout << x << "\n";
                found = true;
                break;
            }
        }

        if (!found) {
            cout << -1 << "\n";
        }
    }
    return 0;
}
0