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
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).
题意
给两个long long 的a和b,有 以下六种操作:a2,a4,a*8; a/2(当a%20时才可以), a/4(当a%40时才可以), a/8(当a%8==0时才可以)
求能否通过这六种操作把a变成b,输出操作次数
思路
如果a>b,那操作肯定是除,如果a<b,操作就是乘,所以可以把a的值赋为max(a,b),b=min(a,b),这样就都只要执行乘法的操作。
2 ,4, 8都是2的倍数,所以把a在小于b的情况下一直乘2并计数,除3就是乘8的个数,除2就是乘4的个数

ll a,b, num = 0,ans = 0;
cin >> a >> b;
if(a > b) swap(a,b);
while(a < b)
{
     a *= 2;
     num++;
}
ll num8 = sum/3;//乘8的次数
sum = sum - i*3;
ll num4 = sum/2;//乘4的次数
num = num - j*2;//剩下的就是乘2的次数
a != b ? cout << -1 << endl : cout << num+num8+num4 << 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
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.
题意
给出n个数,求一个数k,使得这n个数异或k之后,还是原来那n个数
思路
数据只有1024,可以暴力枚举k

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string> 
#include<queue> 
#include<cmath>
#include<map>
#include<vector> 
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
priority_queue<int>ma;
priority_queue<int,vector<int>,greater<int> >mi;
int main()
{
	int t,n,m,x,y,u,v;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int a[3000]={0},b[3000],c[3000];
		for(int i=1;i<=n;i++)
		{
			cin>>c[i];
			a[c[i]]=1;
		}
		int ans=0;
		for(int i=1;i<=2048;i++)
		{
			int flag=1;
			memcpy(b,a,sizeof(a));
			for(int j=1;j<=n;j++)
			{
				int x=c[j]^i;
				if(b[x]==1){
					b[x]--;
				}else {
					flag=0;break;
				}
			}
			if(flag){
				printf("%d\n",i);ans=1;break;
			}
		}
		if(ans==0){
			printf("-1\n");
		}
	}
	return 0;
}

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
inputCopy
5
5
7
11
1
2000000000000
outputCopy
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,规定 a,b两个数的二进制数不同的个数为差异数,求0和1,1和2,2和3…n-1和n的差异数之和
思路
根据0到5的二进制数可以看出,数的第一位(最左边这一位)的是0、1交替变换,所以第一位的贡献就是n,第二位是0、0、1、1、0、0交替所以贡献就是n/2,以此类推第三位贡献就是n/4.
000
001
010
011
100
101

cin>>t;
while(t--)
{
	cin>>n;
	ll now=n,i=1,ans=0;
	while(now)
	{
		ans+=n/i;
		i*=2;
		now>>=1;//看下一位二进制 
	}
	cout<<ans<<endl;
}

D. Johnny and Contribution

Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny’s knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it’s already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It’s easy to see that this strategy will always allow him to choose a topic because there are at most n−1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn’t covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1≤n≤5⋅105) and m (0≤m≤5⋅105) — the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a≠b; 1≤a,b≤n), which mean that there is a reference between blogs a and b. It’s guaranteed that the graph doesn’t contain multiple edges.
The last line contains n integers t1,t2,…,tn, i-th of them denotes desired topic number of the i-th blog (1≤ti≤n).
Output
If the solution does not exist, then write −1. Otherwise, output n distinct integers p1,p2,…,pn (1≤pi≤n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
inputCopy
3 3
1 2
2 3
3 1
2 1 3
outputCopy
2 1 3
inputCopy
3 3
1 2
2 3
3 1
1 1 1
outputCopy
-1
inputCopy
5 3
1 2
2 3
4 5
2 1 2 2 1
outputCopy
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn’t have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值