3 solutions
-
-2
MX-X7—T1题解
解题方法
a可以×2(无线)
b可以+1或-1(微调)
所以要在最短步骤内让a=b,就要让b=最近的a*(2的次方(包括0次方))
Code
#include <bits/stdc++.h> using namespace std; long long a,b,ans=999999999,i=1; int main(){ cin>>a>>b; ans=min(abs(b-a),abs(2*a-b)+1); for(long long x=0;pow(2,x-1)*a<=b;x++){ ans=min(abs((i*a)-b)+x,ans); i=i*2; } cout<<ans; return 0; }
Information
- ID
- 102
- Time
- 3000ms
- Memory
- 512MiB
- Difficulty
- 2
- Tags
- # Submissions
- 782
- Accepted
- 156
- Uploaded By