Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! 参与排名人数12044
[codeforces 1362A] Johnny and Ancient Computer 倍数关系用2的幂次统计
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.com/contest/1362/problem/A
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
A - Johnny and Ancient Computer | GNU C++17 | Accepted | 31 ms | 0 KB |
样例分析如下
10 5
10=5×2^1
2的幂次是1
1<=3
输出1
11 44
44=11×2^2
2的幂次是2
2<=3
输出1
17 21
21无法用17乘2的幂次来表示
输出-1
1 1
1==1
输出0
96 3
96=3×2^5
2的幂次是5
5=3+2
输出2
2 128
128=2*2^6
2的幂次是6
6=3+3
输出2
1001 1100611139403776
1100611139403776=1001*2^40
2的幂次是40
40=3*13+1
输出14
1000000000000000000 1000000000000000000
1000000000000000000==1000000000000000000
输出0
7 1
7无法用1乘2的幂次来表示
输出-1
10 8
10无法用8乘2的幂次来表示
输出-1
AC代码如下
#include <stdio.h>
#define LL long long
int main(){
int t,cnt,flag;
LL a,b,c;
scanf("%d",&t);
while(t--){
scanf("%lld%lld",&a,&b);
if(a==b){printf("0\n");continue;}
if(a<b)c=a,a=b,b=c;//变除法运算为乘法运算
flag=0;cnt=0;
while(1){
b=b*2,cnt++;//cnt统计2的幂次
if(a==b){flag=1;break;}//扩大后,b与a相等
if(a<b)break;//扩大后,b超过a
}
if(!flag)printf("-1\n");
else printf("%d\n",(cnt-1+3)/3);//(cnt-1+3)/3等同于cnt/3向上取整
}
}