题解(647.div2)

题解(647.div2)

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 aa 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 tt (1≤t≤1000) — the number of test cases. The following tt lines contain a description of test cases.
The first and only line in each test case contains integers aa and bb (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.

通过数学方式得解,将ab缩减到最小之后引入算法求解。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main(){
	LL t=read(); 
	while(t--){
		LL a=read(),b=read(),tmpa=a,tmpb=b;
		while(tmpa%2==0)tmpa/=2;
		while(tmpb%2==0)tmpb/=2;
		if(tmpa!=tmpb)printf("-1\n");
		else{
			LL cha=a>b?a/b:b/a,cnt=0,is=0;
		//cout<<cha<<" cha"<<endl;
			while(cha%2==0)cha/=2,cnt++;
			if(cnt%3)is=1;
			cout<<cnt/3+is<<endl;
		}
	}
    return 0;
}

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

使用哈希表求解。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main(){
	int t=read();
	while(t--){
		int n=read();
		vector<int>a(n),harsh(2000);
		for(int i=0;i<n;i++)a[i]=read();
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				harsh[a[i]^a[j]]++;
		int ans=-1;
		for(int i=1;i<2000;i++)
			if(harsh[i]==n){
				ans=i; 
				break;
			}
		cout<<ans<<endl;
	} 
    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 tt (1≤t≤10000) — the number of test cases. The following tt 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 tt 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.

一道数学题,通过自己现场总结算法来作答。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main(){
	LL t;
	cin>>t;
	while(t--){
		LL n=read(),i=1,j=1,ans=0;
		while(j<(n*2))ans+=n/i,i*=2,j*=2;
		cout<<ans<<endl;
	}
    return 0;
}

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 nn different topics, numbered from 1 to nn 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 44 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 mm 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, ii-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 nn 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.

这是一道图论题,是让我们求有没有一个能让节点互相“提到”的顺序方案。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
vector<int>dis[600000];
struct mapp{
	int v,id;
}Map[600000];
int ans[600000],mx[600000];
bool cmp(mapp x,mapp y){
	if(x.v!=y.v)return x.v<y.v;
	return x.id<y.id;
}
int main(){
	int n=read(),m=read();
	for(int i=1;i<=m;i++){
		int a=read(),b=read();
		dis[a].push_back(b);
		dis[b].push_back(a);
	}
	for(int i=1;i<=n;i++)Map[i].v=read(),Map[i].id=i;
	sort(Map+1,Map+1+n,cmp);
	for(int i=1;i<=n;i++){
		int x=Map[i].id;
		int val=Map[i].v;
		if(mx[x]!=val-1){
			cout<<-1;
			return 0;
		}
		ans[i]=x;
		for(int v:dis[x]){
			if(mx[v]==val-1){
				mx[v]=val;
			}
		}
	}
	for(int i=1;i<=n;i++)cout<<ans[i]<<" ";
	return 0;
}

E:Johnny and Grandmaster

Johnny has just found the new, great tutorial: “How to become a grandmaster?”. The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems.
The boy has found an online judge with tasks divided by topics they cover. He has picked pki problems from ii-th category (pp is his favorite number). He wants to solve them in two weeks (the patience condition is too hard for Johnny, so for simplicity, he looks only at easy tasks, which can be solved in such a period). Now our future grandmaster has to decide which topics to cover first and which the second week. Help him assign topics in such a way, that workload is balanced.
Formally, given n numbers pki, the boy wants to divide them into two disjoint sets, minimizing the absolute difference between sums of numbers in each set. Find the minimal absolute difference. Output the result modulo 109+7.
Input
Input consists of multiple test cases. The first line contains one integer t (1≤t≤105) — the number of test cases. Each test case is described as follows:
The first line contains two integers n and p (1≤n,p≤106). The second line contains nn integers ki (0≤ki≤106).
The sum of nn over all test cases doesn’t exceed 106.
Output
Output one integer — the reminder of division the answer by 1000000007.

这道题是一道数学题,与上面的题同样需要我们自己现场总结算法解决问题。当然我最初是使用了dp所以导致数据结构名字有点不和谐

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
LL T=read();
LL Pow(LL a,LL b){
	LL sum=1;
	while(b){
		if(b%2) sum=sum*a%1000000007;
		a=a*a%1000000007;
		b/=2;
	}
	return sum;
}
int main(){
	while(T--){
		LL n=read(),p=read();
		vector<LL>dp(n);
		for(LL i=0;i<n;i++)cin>>dp[i];
		if(p==1){
			if(n%2)cout<<1;
			else cout<<0;
			cout<<endl;
		}else{
			sort(dp.begin(),dp.end());
			vector<LL>l;
			vector<LL>r;
			while(dp.size()){
				r.push_back(dp.back());
				dp.pop_back();
				LL nownum=1;
				LL last=r.back();
				for(LL i=dp.size()-1;i>=0;i--){
					l.push_back(dp[i]);
					LL d=last-dp[i];
					last=dp[i];
					dp.pop_back();
					while(d&&nownum<n){
						nownum*=p;
						d--;
					}
					nownum--;
					if(!d&&!nownum)break;
				}
			}
			LL ans=0;
			for(auto i:l){
				ans=(ans+1000000007-Pow(p,i))%1000000007;
			}
			for(auto i:r){
				ans=(ans+Pow(p,i))%1000000007;
			}
			cout<<ans<<endl;
		}
		
	}
	return 0;
}

F:Johnny and Megan’s Necklace

Johnny’s younger sister Megan had a birthday recently. Her brother has bought her a box signed as “Your beautiful necklace — do it yourself!”. It contains many necklace parts and some magic glue.
The necklace part is a chain connecting two pearls. Color of each pearl can be defined by a non-negative integer. The magic glue allows Megan to merge two pearls (possibly from the same necklace part) into one. The beauty of a connection of pearls in colors u and v is defined as follows: let 2k be the greatest power of two dividing u⊕v — exclusive or of u and v. Then the beauty equals k. If u=v, you may assume that beauty is equal to 20.
Each pearl can be combined with another at most once. Merging two parts of a necklace connects them. Using the glue multiple times, Megan can finally build the necklace, which is a cycle made from connected necklace parts (so every pearl in the necklace is combined with precisely one other pearl in it). The beauty of such a necklace is the minimum beauty of a single connection in it. The girl wants to use all available necklace parts to build exactly one necklace consisting of all of them with the largest possible beauty. Help her!
Input
The first line contains n(1≤n≤5⋅105) — the number of necklace parts in the box. Each of the next nn lines contains two integers a and b(0≤a,b<220), which denote colors of pearls presented in the necklace parts. Pearls in the i-th line have indices 2i−1 and 2i respectively.
Output
The first line should contain a single integer bb denoting the maximum possible beauty of a necklace built from all given parts.
The following line should contain 2n distinct integers pi(1≤pi≤2n) — the indices of initial pearls in the order in which they appear on a cycle. Indices of pearls belonging to the same necklace part have to appear at neighboring positions in this permutation (so 1432 is not a valid output, whereas 2143 and 4312 are). If there are many possible answers, you can print any.

这是一道图论题,需要用到vector。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main(){
    int n=read();
    vector<int>a(n);
    vector<int>b(n);
    for(int i=0;i<n;i++){
        a[i]=read(),b[i]=read();
    }
    for(int k=20;k>=0;k--){
        int t=1<<k;
        vector<vector<pair<int, int> > >mapp(t+n);
        for(int i=0;i<n;i++){
            mapp[a[i]&(t-1)].push_back(make_pair(i+t,2*i));
            mapp[i+t].push_back(make_pair(a[i]&(t-1),2*i));
            mapp[b[i]&(t-1)].push_back(make_pair(i+t,2*i+1));
            mapp[i+t].push_back(make_pair(b[i]&(t-1),2*i+1));
        }
        bool flag=1;
        for(int i=0;i<t+n;i++){
            if((int)(mapp[i]).size()&1){
                flag=0;
            }
        }
        if(flag==0)continue;
        vector<bool>del(2*n);
        vector<int>ans;
        function<void(int,int)>dfs=[&](int u,int from){
            while(mapp[u].size()){
                auto it=mapp[u].back();
				mapp[u].pop_back();
                int v=it.first,id=it.second;
                if(!del[id]){
                    del[id]=1;
                    dfs(v,id);
                }
            }
            if(from!=-1)ans.push_back(from);
        };
        ans.clear();
        for(int i=0;i<n+t;i++){
            if((int)(mapp[i]).size()){
                dfs(i,-1);
                break;
            }
        }
        if((int)(ans).size()<2*n){
            continue;
        }
        cout<<k<<endl;
        for(auto it:ans){
            cout<<it+1<<" ";   
        }
        return 0;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值