Codeforces Round #647 (Div. 2)-{部分题解}

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 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.

Example
input
10
10 5
11 44
17 21
1 1
96 3
2 128
1001 1100611139403776
1000000000000000000 1000000000000000000
7 1
10 8
output
1
1
-1
0
2
2
14
0
-1
-1
Note
In the first test case, Johnny can reach 5 from 10 by using the shift to the right by one (i.e. divide by 2).

In the second test case, Johnny can reach 44 from 11 by using the shift to the left by two (i.e. multiply by 4).

In the third test case, it is impossible for Johnny to reach 21 from 17.

In the fourth test case, initial and target values are equal, so Johnny has to do 0 operations.

In the fifth test case, Johnny can reach 3 from 96 by using two shifts to the right: one by 2, and another by 3 (i.e. divide by 4 and by 8).
题意:输入两个数,有六种操作方法 乘除2,4,8;问你需要多少步,最后可以让两个数相等。
思路:自己当时状态太差了,这个我总想着去 光进行乘除八这一步,然后看看需要几次,可惜啊自己想错了啊!

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<iomanip>
#include<vector>
#include<fstream>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
int main()
{
	
	int t; cin >> t;
	while (t--)
	{
		int a, b;
		int u = max(a, b);
		int m = min(a, b);
		a = u; b = m;
		if (a % b != 0)
		{
			cout << -1 << endl;
		}
		else
		{
			int s = 0;
			int c = a / b;
			while (c % 8 == 0)
			{
				c = c / 8; s++;
			}
			while (c % 4 == 0)
			{
				c = c / 4; s++;
			}
			while (c % 2 == 0)
			{
				c = c / 2;
				s++;
			}
			if (c != 1)
				cout << -1 << endl;
			else cout << s << endl;
		}
	}
}

B.Johnny and His Hobbies

Among Johnny’s numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad’s office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.

There is a set S containing very important numbers on his dad’s desk. The minute Johnny heard about it, he decided that it’s a good idea to choose a positive integer k and replace each element s of the set S with s⊕k (⊕ denotes the exclusive or operation).

Help him choose such k that Johnny’s dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn’t matter, i.e. set {1,2,3} equals to set {2,1,3}.

Formally, find the smallest positive integer k such that {s⊕k|s∈S}=S or report that there is no such number.

For example, if S={1,3,4} and k=2, new set will be equal to {3,1,6}. If S={0,1,2,3} and k=1, after playing set will stay the same.

Input
In the first line of input, there is a single integer t (1≤t≤1024), the number of test cases. In the next lines, t test cases follow. Each of them consists of two lines.

In the first line there is a single integer n (1≤n≤1024) denoting the number of elements in set S. Second line consists of n distinct integers si (0≤si<1024), elements of S.

It is guaranteed that the sum of n over all test cases will not exceed 1024.

Output
Print t lines; i-th line should contain the answer to the i-th test case, the minimal positive integer k satisfying the conditions or −1 if no such k exists.

Example
input
6
4
1 0 2 3
6
10 7 14 8 3 12
2
0 2
3
1 2 3
6
1 4 6 10 11 12
2
0 1023
output
1
4
2
-1
-1
1023
Note
In the first test case, the answer is 1 because it is a minimum positive integer and it satisfies all the conditions.
题意:自己就是缺少积累吧 到了运算都不会了,当时没看懂唉太可惜了
& 按位与
| 按位或
^ 按位异或
~ 取反
思路:因为数值不大所以直接暴力来做就行~~~

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<iomanip>
#include<vector>
#include<fstream>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
int a[1100], b[1100];
int main()
{
	int t; cin >> t;
	while (t--)
	{
		int ans = 0;
		int f = 0;
		int n; cin >> n;
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		sort(a + 1, a + 1 + n);
		for (int i = 1; i <= 1024; i++)
		{
			f =0;
			ans = i;
			for (int j = 1; j <= n; j++)
				b[j] = a[j] ^ i;
			sort(b + 1, b + 1 + n);
			for (int j = 1; j <= n; j++)
			{
				if (b[j] != a[j])
				{
					f =i; break;
				}
			}
			if (f==0)
				break;
		}
		if (f == 0) cout <<ans << endl;
		else cout << -1 << endl;
	}
}

C.Johnny and Another Rating Drop
The last contest held on Johnny’s favorite competitive programming platform has been received rather positively. However, Johnny’s rating has dropped again! He thinks that the presented tasks are lovely, but don’t show the truth about competitors’ skills.

The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of 5=1012 and 14=11102 equals to 3, since 0101 and 1110 differ in 3 positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.

Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you’ve got a sequence of consecutive integers from 0 to n. That’s strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.

Input
The input consists of multiple test cases. The first line contains one integer t (1≤t≤10000) — 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 a single integer n (1≤n≤1018).

Output
Output t lines. For each test case, you should output a single line with one integer — the unfairness of the contest if the rating sequence equals to 0, 1, …, n−1, n.

Example
input
5
5
7
11
1
2000000000000
output
8
11
19
1
3999999999987
Note
For n=5 we calculate unfairness of the following sequence (numbers from 0 to 5 written in binary with extra leading zeroes, so they all have the same length):

000
001
010
011
100
101
The differences are equal to 1, 2, 1, 3, 1 respectively, so unfairness is equal to 1+2+1+3+1=8.
题意:输入一个n,从0-n的前后两个数的二进制的差别数字 的数目 加在一起 输出和
题意:给一个N,问二进制下0~N的所有数中相邻的数的二进制同一位不同的个数和。如0101和1110有3个数位不同。

思路:这一位每一位相邻的数都不一样,对答案的贡献为N,这一位每两位对答案贡献位1…类推,当N有cnt位二进制位的时候,每对答案贡献为1,所以计算出N有多少位二进制,然后算每一位对答案的贡献就行了。
————————————————

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<iomanip>
#include<vector>
#include<fstream>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
int main()
{
	int t; cin >> t;
	while (t--)
	{
		ll n; cin >> n;
		ll ans = 0;
		while (n) {
			ans += n;
			n /= 2;
		}
		cout << ans << endl;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值