/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 447ms 161.543 MiB
#2 Accepted 546ms 161.664 MiB
#3 Wrong Answer 658ms 161.621 MiB
#4 Time Exceeded ≥1110ms ≥161.5 MiB

Code

#include <bits/stdc++.h>
#define int long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 1e7 + 9999 + 9;

vector<int> prime, spf(N), primeVis(N);
void sp()
{
    for (int i = 1; i < N; i++)
        spf[i] = i;
    for (int p = 2; p < N; p++)
    {
        if (spf[p] == p)
        {
            prime.pb(p);
            primeVis[p] = 1;
            for (int i = p * 2; i < N; i += p)
                spf[i] = min(p, spf[i]);
        }
    }
}

int k, n, m;

void solve()
{
    map<int, int> mp;

    int prev = -1;
    // memset(dp, 0, sizeof dp);
    // memset(vis, 0, sizeof dp);
    // cout << endll;
    cin >> n >> m >> k;

    queue<pair<int, int>> q;
    q.push({n, k});
    map<pair<int, int>, int> vis;
    int ans = 0;
    // vis[spf[n]]++

    while (q.size())
    {
        auto p = q.front();
        q.pop();
        for (int i = 1; i <= m; i++)
        {
            int samne = p.first + i;
            int pichone = p.first - i;

            samne = spf[samne];

            if (p.second == 0)
            {
                ans = max(ans, p.first);
                break;
            }

            // if (vis[samne] && abs((p.first + i) - samne) <= m && p.second % 2 == 0)
            // {
            //     ans = max(ans, samne);
            // }
            // else

            {

                int starting = spf[p.first], ending = spf[p.first + i];
                // cout << starting << " " << ending << endll;

                if (vis[{samne, samne}])
                {
                    if (p.second % 2)
                    {
                        ans = max(ans, ending);
                    }
                    else
                        ans = max(ans, starting);
                }
                else
                {

                    q.push({samne, p.second - 1});
                    vis[{samne, samne}] = p.second - 1;
                }
            }

            if (pichone >= 2)
            {
                pichone = spf[pichone];
                int starting = spf[p.first], ending = spf[p.first - i];
                // cout << starting << " " << ending << endll;
                ;

                if (vis[{pichone, pichone}])
                {
                    if (p.second % 2)
                    {
                        ans = max(ans, ending);
                    }
                    else
                        ans = max(ans, starting);
                }
                else
                {

                    q.push({pichone, p.second - 1});
                    vis[{pichone, pichone}] = p.second - 1;
                }
            }
            // cout << endll;
            // cout << ans << endll;
            // cout << samne << " " << pichone << endll;
        }
        // cout << endll;
    }
    cout << ans << endll;
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    sp();
    // for (int i = 1; i <= 100; i++)
    // {
    //     cout << spf[i] << " ";
    // }
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1146 Yet Another Battle Between Roy and Hridoy!
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-26 13:59:57
Judged At
2024-12-26 13:59:57
Judged By
Score
2
Total Time
≥1110ms
Peak Memory
≥161.664 MiB