Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A. Johnny and Ancient Computer(思维)
Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by 22 , 44 or 88 , and division is only allowed if the number is divisible by the chosen divisor.
Formally, if the register contains a positive integer xx , in one operation it can be replaced by one of the following:
- x⋅2x⋅2
- x⋅4x⋅4
- x⋅8x⋅8
- x/2x/2 , if xx is divisible by 22
- x/4x/4 , if xx is divisible by 44
- x/8x/8 , if xx is divisible by 88
For example, if x=6x=6 , in one operation it can be replaced by 1212 , 2424 , 4848 or 33 . Value 66 isn't divisible by 44 or 88 , so there're only four variants of replacement.
Now Johnny wonders how many operations he needs to perform if he puts aa in the register and wants to get bb at the end.
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000 ) — the number of test cases. The following tt lines contain a description of test cases.
The first and only line in each test case contains integers aa and bb (1≤a,b≤10181≤a,b≤1018 ) — the initial and target value of the variable, respectively.
Output tt lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get bb at the end, then write −1−1 .
10 10 5 11 44 17 21 1 1 96 3 2 128 1001 1100611139403776 1000000000000000000 1000000000000000000 7 1 10 8
1 1 -1 0 2 2 14 0 -1 -1
首先乘除互为逆运算,因此可以使a为较小的数,b为较大的数,全部转化为乘。
然后看a能否通过数次*2得到b,不能的话输出-1,否则贪心地先选择*8..来凑次数。
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { long long a,b; int ans=0; cin>>a>>b; if(a>b) swap(a,b); bool flag=0; int cnt=0; while(a<b) { cnt++; a*=2; } if(a>b) { cout<<-1<<endl; continue; } else { ans+=cnt/3; cnt%=3; ans+=cnt/2; cnt%=2; ans+=cnt; } cout<<ans<<endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!