This commit is contained in:
Zengtudor 2024-10-11 15:20:00 +08:00
parent f3f3fbc327
commit d1288853ee

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <exception>
#include <algorithm>
using ull = unsigned long long;
@ -24,12 +25,11 @@ ull cal(ull a,ull b){
if(a&1)return cal(a+1,b)+1;
else return cal(a/2,b)+1;
}else if(a>b/2){
if(a*2-b<b-a){
if(b&1)return cal(a,b-1)+1;
else return cal(a,b/2)+1;
}else return b-a;
const auto ans = b-a;
if(b&1)return std::min(ans,cal(a,b-1)+1);
else return std::min(ans,cal(a,b/2)+1);
}else if(a<=b/2){
if(b&1)return cal(a,b+1)+1;
if(b&1)return cal(a,b-1)+1;
else return cal(a,b/2)+1;
}
throw std::runtime_error("cannot calculate");