Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A Johnny and Ancient Computer

A Johnny and Ancient Computer

入口:A Johnny and Ancient Computer
A. Johnny and Ancient Computer
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 2, 4 or 8, and division is only allowed if the number is divisible by the chosen divisor.

Formally, if the register contains a positive integer x, in one operation it can be replaced by one of the following:

x⋅2
x⋅4
x⋅8
x/2, if x is divisible by 2
x/4, if x is divisible by 4
x/8, if x is divisible by 8
For example, if x=6, in one operation it can be replaced by 12, 24, 48 or 3. Value 6 isn’t divisible by 4 or 8, so there’re only four variants of replacement.

Now Johnny wonders how many operations he needs to perform if he puts a in the register and wants to get b at the end.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases. The following t lines contain a description of test cases.

The first and only line in each test case contains integers a and b (1≤a,b≤1018) — the initial and target value of the variable, respectively.

Output
Output t lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get b at the end, then write −1.

题意:

输入a,b两个数字,若(b<a)a可以通过×2,×4,×8,或者(a<b)(能满足整除情况下)/2,/4,/8等于b。则满足题意,且求a变成b的最少的次数。

解法:

要求a在满足整除情况下/2,/4,/8等于b,那么倒不如在a<b的时候 转换a和b的值,那便不用算除的了。要算次数,一直%8等到 不能再%8的时候,再%4,再%2,从而得出结果,每%一次,次数+1。输出结果就好。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		LL a, b;
		cin >> a >> b;
		if (a > b)//交换a,b,便少了/的情况。
		{
			swap(a, b);
		}
		int count = 0;
		if (b % a == 0)
		{
			LL i = b / a;
			while (i % 8 == 0)
			{ 
				i = i / 8;
				count++;
			}
			while (i % 4 == 0)
			{ 
				i = i / 4;
				count++;
			}
			while (i % 2 == 0)
			{ 
				i = i / 2;
				count++;
			}
			if (i == 1)
			{
				cout << count << endl;
			}
			else
			{
				cout << -1 << endl;
			}
		}
		else
		{
			cout << -1 << endl;
		}
	}
	return 0;
}

注意:

当特殊案例比较多的时候,考虑是否正确(血的教训呀!!!)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值