Quick actions

cmd+k|ctrl+k

Navigation

Languages

大概是新的题目

Snippet info

Language

Cpp

Visibility

public

Author

dogdie233

Created

2025-02-27T14:16:45.227286Z

Updated

2025-02-27T14:17:21.95737Z

#include<bits/stdc++.h>
 
using namespace std;
 
int main()
{
    auto a = unordered_set<uint32_t>();
    auto b = unordered_set<uint32_t>();
    auto walked = unordered_set<uint32_t>();
    uint32_t cnt = 0, target;
    cin >> target;
    if (target == 1) return cout << 0, 0;
    
    b.insert(1);
    walked.insert(0);
    while (b.size() != 0)
    {
        a.swap(b);
        cnt++;
        b.clear();
        for (auto val : a)
        {
            walked.insert(val);
            uint32_t opResult;
            if (!__builtin_umul_overflow(val, val, &opResult))
            {
                if (opResult == target) return cout << cnt, 0;
                if (walked.find(opResult) == walked.end()) b.insert(opResult);
            }
            opResult = val + 1;
            if (opResult == target) return cout << cnt, 0;
            if (walked.find(opResult) == walked.end()) b.insert(opResult);
            opResult = val - 1;
            if (opResult == target) return cout << cnt, 0;
            if (walked.find(opResult) == walked.end()) b.insert(opResult);
        }
    }

    return 0;
}
INFO