/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 2ms 532.0 KiB
#3 Accepted 4ms 640.0 KiB
#4 Accepted 4ms 596.0 KiB
#5 Accepted 29ms 632.0 KiB
#6 Accepted 27ms 532.0 KiB
#7 Accepted 6ms 596.0 KiB
#8 Accepted 12ms 532.0 KiB
#9 Accepted 152ms 664.0 KiB
#10 Accepted 317ms 760.0 KiB
#11 Accepted 66ms 664.0 KiB
#12 Accepted 11ms 580.0 KiB

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include <map>
#include <sstream>
#include <stack>
#include <bitset>
#include <random>

using ll = long long;

using namespace std;

constexpr int nm = 32000;

vector<ll> primes;
vector<bool> isPrime(nm, 1);
ll n, k;

vector<pair<ll, ll>> factorize(ll n) {
    vector<pair<ll, ll>> res;
    for (ll i: primes) {
        if (i * i > n) break;
        if (n % i == 0) {
            ll cnt = 0;
            while (n % i == 0) {
                cnt++;
                n /= i;
            }
            res.emplace_back(i, cnt);
        }
    }
    if (n > 1) res.emplace_back(n, 1);
    return res;
}

void solve() {
    cin >> n >> k;
    auto factors = factorize(n);

    ll i = 0;
    while (i < k && factors.size() > 1) {
        i++;
        if (i % 2) {
            factors.begin()->second++;
        } else {
            auto it = prev(factors.end());
            it->second--;
            if (it->second == 0) factors.pop_back();
        }
    }

    if ((k - i) % 2) {
        i++;
        if (i % 2) {
            factors.begin()->second++;
        } else {
            auto it = prev(factors.end());
            it->second--;
            if (it->second == 0) factors.pop_back();
        }
    }

    ll res = 1;
    for (auto &[x, y]: factors) {
        for (int i = 0; i < y; ++i) res *= x;
    }
    cout << res << "\n";
}

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);

    for (ll i = 2; i < nm; ++i) {
        if (isPrime[i]) {
            primes.push_back(i);
            for (ll j = i * i; j < nm; j += i) isPrime[j] = 0;
        }
    }

    int t;
    cin >> t;
    while (t--) solve();
}

Information

Submit By
Type
Submission
Problem
P1194 D. Roy and Prime Game
Contest
Happy New Year 2026
Language
C++17 (G++ 13.2.0)
Submit At
2026-01-06 15:23:19
Judged At
2026-01-06 15:23:20
Judged By
Score
100
Total Time
317ms
Peak Memory
760.0 KiB