/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Runtime Error terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 4ms 532.0 KiB
#2 Runtime Error terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 2ms 576.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
     
const long long mod = 10000007;
const long long INF = LLONG_MAX;
const int N = 1e5 + 9;

vector<int> v1;
bitset<N> nprime;

void SieveOfEratosthenes(int n) {
    v1.push_back(2);
    
    for (int p = 3; p * p < n; p += 2) {
        if (!nprime[p]) {
            for (long long i = (long long)p * p; i < n; i += 2 * p) {
                nprime[i] = true;
            }
        }
    }
    for (int p = 3; p < n; p += 2) {
        if (!nprime[p]) {
            v1.push_back(p);
        }
    }
}

void solve () {
    long long n, k;
    vector <long long> v;
    cin >> n >> k;

    long long sum = n;

    for (int i = 0; v1[i] * v1[i] <= sum; i++) {
        while (n % v1[i] == 0) {
            v.push_back (v1[i]);
            n = n / v1[i];
        }
    }
    if (n != 1) {
        v.push_back (n);
    }
    int x = v.size() - 1;

    long long sum1 = sum;

    for (int i = 1; i <= k; i++) {
        if (i % 2 == 1) {
            sum = sum * v[0];
        } else {
            sum = sum / v[x];
            x--;
            if (x == -1) {
                break;
            }
        }
        if (sum == sum1) {
            if (k % 2 == 0) {
                cout << sum;
                return;
            } else {
                cout << (sum * v[0]);
                return;
            }
        }
        sum1 = sum;
    }
    cout << sum;
} 
int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); 
    SieveOfEratosthenes(100100);

    long long t;
    cin >> t;

    while (t--) {
        solve();
        cout << "\n";
    }
    return 0;
}      

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 16:49:58
Judged At
2026-01-06 16:49:58
Judged By
Score
0
Total Time
4ms
Peak Memory
576.0 KiB