[codeforces 1362A] Johnny and Ancient Computer 倍数关系用2的幂次统计

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

ProblemLangVerdictTimeMemory
A - Johnny and Ancient Computer GNU C++17Accepted31 ms0 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向上取整
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值