大概是新的题目

Run Settings
LanguageC++
Language Version
Run Command
#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; }
#include<bits/stdc++.h> using namespace std; void dfs(uint32_t val, uint32_t step, uint32_t &ans) { if (step >= ans) return; uint32_t down = sqrt((double)val); if (down == 1) ans = min(step + val - 1, ans); else dfs(down, step + 1 + (val - (down * down)), ans); uint32_t up = down + 1; uint32_t mulRes; if (__builtin_umul_overflow(up, up, &mulRes)) return; dfs(up, step + 1 + (mulRes - val), ans); } int main() { uint32_t target, ans = UINT32_MAX; cin >> target; if (target == 1) return cout << 0, 0; dfs(target, 0, ans); cout << ans; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines