/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Wrong Answer 2ms 540.0 KiB
#3 Wrong Answer 44ms 576.0 KiB

Code


#include <bits/stdc++.h>
using namespace std;
int koro(int cur, int B, unordered_map<int, int> &dp)
{
    if (cur > B)
    {
        return INT_MAX;
    }
    if (cur == B)
    {
        return 0;
    }
    if (dp.count(cur))
    {
        return dp[cur];
    }
    int ans1 = koro(cur * 2, B, dp);
    int ans2 = koro(cur * 10 + 1, B, dp);
    if (min(ans1, ans2) == INT_MAX)
    {
        return dp[cur] = INT_MAX;
    }
    return dp[cur] = min(ans1, ans2) + 1;
}

int main()
{
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int t;
    cin>>t;
    while (t--)
    {
        int A, B;
        cin >> A >> B;
        unordered_map<int, int> dp;
        int ans = koro(A, B, dp);
        if (ans == INT_MAX)
        {
            ans = 0;
            while (1)
            {
                if(A*2>B) break;
                ans++;
                A*=2;
            }
            ans+=abs(A-B);
            cout<<ans;
            
        }
        else
        {
            cout << ans;
        }
        cout<<endl;
    }
    
}

Information

Submit By
Type
Submission
Problem
P1044 Add or multiple
Contest
TLE_Headquarters - round #1
Language
C++20 (G++ 13.2.0)
Submit At
2024-03-27 16:30:57
Judged At
2024-11-11 03:38:36
Judged By
Score
20
Total Time
44ms
Peak Memory
576.0 KiB