/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 2ms 432.0 KiB
#3 Accepted 11ms 572.0 KiB
#4 Accepted 12ms 532.0 KiB
#5 Accepted 209ms 576.0 KiB
#6 Accepted 202ms 580.0 KiB
#7 Accepted 10ms 324.0 KiB
#8 Accepted 13ms 532.0 KiB
#9 Accepted 1184ms 616.0 KiB
#10 Time Exceeded ≥1595ms ≥636.0 KiB
#11 Accepted 499ms 596.0 KiB
#12 Accepted 44ms 572.0 KiB

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
/*
        OBSERVATIONS:


    x has some smallest factor


    and a lot of other factor..


    the array woudl look something like this:

    2,2,2,2...,3,3,3,3,3...,5,5,5,5,5...,7,7,7..


    roy keeps adding in front

    and hridoy keeps poping from the back..

    although they have said current x ..

    that means it will be the factor only...


    12

    2,3

    2,2,3
    2,3


    adding is constant

    that is (k+1)/2 times ..

    and then i have k/2 operations that i can mostly

    BF .




*/
vector<pair<int, int>> prime_factors(int n)
{
    vector<pair<int, int>> factors;

    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            int cnt = 0;
            while (n % i == 0)
            {
                n /= i;
                cnt++;
            }
            factors.push_back({(int)i, cnt});
        }
    }

    if (n > 1)
        factors.push_back({(int)n, 1});

    return factors;
}
int binpow(int a, int b)
{
    int res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        int first = (k + 1) / 2;
        int second = k / 2;
        vector<pair<int, int>> factors = prime_factors(n);

        factors[0].second += first;

        int size = factors.size() - 1;
        int last_index = size;
        while (true)
        {
            int curr = factors[last_index].second;
            if (second <= curr)

            {

                factors[last_index].second -= second;
                if (factors[last_index].second == 0)
                {
                    last_index--;
                }
                second = 0;
                break;
            }
            else
            {

                second -= factors[last_index].second;
                last_index--;
            }
        }
        int res = 1;

        for (int i = 0; i <= last_index; i++)
        {
            pair<int, int> a = factors[i];

            res = (res * binpow(a.first, a.second));
        }
        cout << res << endl;
    }
    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:47:22
Judged At
2026-01-06 16:47:22
Judged By
Score
95
Total Time
≥1595ms
Peak Memory
≥636.0 KiB