This commit is contained in:
Zengtudor 2024-10-11 05:34:41 +00:00
parent 267483c098
commit 76e6a85b44
2 changed files with 30 additions and 2 deletions

View File

@ -1,11 +1,12 @@
#include <iostream>
#include <exception>
using ull = unsigned long long;
auto &is = std::cin;
auto &os = std::cout;
ull n, a, b;
ull n, a, b, depth;
/*
cal(a,b)
@ -16,7 +17,28 @@ cal(a,b)
5)a<b/2 && b is odd return cal(a,b/2)+1;
6)a<b/2 && b is even return cal(a,b-1)+1;
*/
ull cal(ull a,ull b){
if(a==b){
return 0;
}else if(a>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;
}else if(a<=b/2){
if(b&1)return cal(a,b+1)+1;
else return cal(a,b/2)+1;
}
throw std::runtime_error("cannot calculate");
}
int main(){
is>>n;
for(ull i {0};i<n;i++){
is>>a>>b;
os<<cal(a,b)<<'\n';
}
}

6
test.txt Normal file
View File

@ -0,0 +1,6 @@
8
3
8
3
0
20