Codeforces Round #647 (Div. 2)日常训练

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.

Example
inputCopy
10
10 5
11 44
17 21
1 1
96 3
2 128
1001 1100611139403776
1000000000000000000 1000000000000000000
7 1
10 8
outputCopy
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).

#include<iostream>
using namespace std;
long long t,a,b,ans;
int main(){
	cin>>t;
	while(t--){
		cin>>a>>b;
		ans=0;
		while(a<b){
			a<<=1;
			ans=ans+1;
		}
		while(a>b&&a%2==0){
			a>>=1;
			ans=ans+1;
		}
		if(a==b){
			ans=(ans+2)/3;
		}
		else ans=-1;
		cout<<ans<<endl;
	}
	return 0;
} 

B. Johnny and His Hobbies
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
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
outputCopy
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<bits/stdc++.h>
using namespace std;
long long t,n,m,i,j,k;
bool f,ff;
int s[2000],b[2000];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		memset(b,0,sizeof(b));
		f=false;
		for(i=1;i<=n;i++){
			cin>>s[i];
			b[s[i]]=1;
		}
		k=0;
		while(!f && k<=1024){
			k++;
			ff=true;
			for(i=1;i<=n;i++){
				m=s[i]^k;
				if (m>1024 || b[m]==0){
					ff=false;
					break;
				}
			}
			if(ff){
				f=true;
				cout<<k<<endl;
		    }
		}
		if(!f) cout<<-1<<endl;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值