Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A、Johnny and Ancient Computer B、Johnny and His Hobbies C、Johnny and Another Rating Drop
题目链接:A、Johnny and Ancient Computer
题意:
给你两个数a,b。问你可不可以通过左移位运算或者右移位运算使得它们两个相等。可以的话输出操作次数,不可以输出-1
一次操作可以最多左移3次或者右移3次
题解:
首先找寻一下这两个数的二进制形式下最右边那个1在什么位置。然后看一下它们的差距是多少(设为x)
那么就让a,b中小的那个数左移x位。之后判断一下它们两个相等不相等就可以了
代码:

1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string>
5 #include<queue>
6 #include<deque>
7 #include<string.h>
8 #include<map>
9 #include <iostream>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 const int maxn=500+10;
14 int main()
15 {
16 ll t;
17 scanf("%I64d",&t);
18 while(t--)
19 {
20 ll a,b,sum1=0,sum2=0,sum=0;
21 scanf("%I64d%I64d",&a,&b);
22 ll aa=a,bb=b;//printf("%I64d**%I64d\n",aa,aa&1);
23 while((aa&1)==0)
24 {
25
26 sum1++;
27 aa>>=1;
28 }
29 while((bb&1)==0)
30 {
31 sum2++;
32 bb>>=1;
33 }
34 if(a<b) swap(a,b);
35 //printf("%I64d****%I64d %I64d\n",sum1-sum2,sum1,sum2);
36 if(sum1<sum2) swap(sum1,sum2);
37
38 if((sum1-sum2)==0)
39 {
40 if(a==b)
41 printf("0\n");
42 else printf("-1\n");
43 }
44 else if((sum1-sum2)%3)
45 {
46
47 sum=(sum1-sum2)/3+1;
48 //printf("%I64d %I64d %I64d\n",sum1-sum2,b,b<<(sum1-sum2));
49 b<<=(sum1-sum2);
50
51 if(a==b)
52 printf("%I64d\n",sum);
53 else printf("-1\n");
54 }
55 else
56 {
57 sum=(sum1-sum2)/3;
58 b<<=(sum1-sum2);
59 if(a==b)
60 printf("%I64d\n",sum);
61 else printf("-1\n");
62 }
63 }
64 return 0;
65 }
题目链接:B、Johnny and His Hobbies
题意:
给你一个有n个元素的集合v,给你两个集合a,b(集合内元素都是int类型)。如果把a数组中元素和b中元素都分别按照从小到大排序。如果排序后两个集合一摸一样,那就说着a,b两个集合相等
现在你需要找到一个最小的k,使得v集合中每一个元素都与k进行异或操作,你需要保证异或后得到的那个集合和原集合相等。
如果你找不到这个k,那就输出-1
题解:
首先如果n为奇数那么肯定输出-1,因为如果要满足题意的话,那肯定是v[i]异或k之后这个值和v[i]相互对应,毕竟k被异或两个相当于没有被异或,即v[i]=v[i]^k^k
之后我还想着n为偶数情况也是找规律,没想到。。。
偶数方面就暴力,因为n本身就不大
如果v集合中两个元素x和y相对应,那么x^y这个值就满足题意(虽然可能不是最小的k),那么我们就先确定x为v集合中第一个元素v[1],对y就是暴力枚举。
找到x和y之后,我们这里设ans=x^y
那么v集合中其他元素与ans异或之后的元素肯定也在v集合中,如果有一个不在,那么ans就不满足题意
用一个变量 minn来保存那个满足题意得最小的ans就行
代码:

1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string>
5 #include<queue>
6 #include<deque>
7 #include<string.h>
8 #include<map>
9 #include <iostream>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 const int maxn=1024+10;
14 const int INF=0x3f3f3f3f;
15 int w[maxn],v[maxn];
16 int main()
17 {
18 int t;
19 scanf("%d",&t);
20 while(t--)
21 {
22 int n;
23 memset(w,0,sizeof(w));
24 scanf("%d",&n);
25 for(int i=1;i<=n;++i)
26 {
27 scanf("%d",&v[i]);
28 w[v[i]]=1;
29 }
30 if(n%2)
31 {
32 printf("-1\n");
33 continue;
34 }
35 //sort(v+1,v+1+n);
36 int minn=INF;
37 for(int i=2;i<=n;++i)
38 {
39 int ans=v[1]^v[i],flag=0;
40 for(int j=2;j<=n;++j)
41 {
42 if(i==j) continue;
43 if(w[ans^v[j]]);
44 else
45 {
46 flag=1;
47 break;
48 }
49 }
50 if(flag==0)
51 {
52 minn=min(minn,ans);
53 }
54 }
55 if(minn==INF)
56 {
57 printf("-1\n");
58 }
59 else
60 {
61 printf("%d\n",minn);
62 }
63 }
64 return 0;
65 }
题目链接:C、Johnny and Another Rating Drop
题意:
给你一个n,然后找出来1,2,3...n中相邻得两个数中二进制形式下有多少位不相同
例如3和4
3二进制为011
4二进制为100
那么它们两个有3位不同
题解:
找规律,好多题解都说的找规律;没找规律的题解也没看太明白
F(n)=F(n/2)+n
代码:
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string>
5 #include<queue>
6 #include<deque>
7 #include<string.h>
8 #include<map>
9 #include <iostream>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 const int maxn=1024+10;
14 const int INF=0x3f3f3f3f;
15 int main()
16 {
17 int t;
18 cin>>t;
19 while(t--)
20 {
21 long long n,ans=0;
22 cin>>n;
23 while(n)
24 {
25 ans+=n;
26 n/=2;
27 }
28 cout<<ans<<endl;
29 }
30 }
Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A、Johnny and Ancient Computer B、Johnny and His Hobbies C、Johnny and Another Rating Drop的更多相关文章
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)
题意:有\(n\)个点,\(m\)条边,现在要给这些点赋值,,每次只能赋给某一点的四周(所连边)的最小没出现过的值.如果不能按照所给的数赋值,输出\(-1\),否则输出赋值顺序. 题解:我们用\(pa ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! C. Johnny and Another Rating Drop (规律,二进制)
题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数. 题解:打表找规律,不难发现: \(00000\) \(00001\) ...
- Codeforces Round #647 (Div. 2)
Problem A https://codeforces.com/contest/1362/problem/A 判断x/y是不是2的k次方, 如果是 k/3 + (k%3)/2 + (k%3%2)即为 ...
- Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)
题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...
- Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...
- Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)
题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...
- Codeforces Round #647 (Div. 2) A. Johnny and Ancient Computer
题目链接:https://codeforces.com/contest/1362/problem/A 题意 有一个正整数 $a$,可选择的操作如下: $a \times 2$ $a \times 4$ ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- 【Samba】共享服务器的搭建和相关权限设置
1.查看防护墙 [root@zhang~ ]# /etc/init.d/iptables status iptables:Firewall is not running. 如果没有关闭的话将他 ...
- 【ORA】ORA-01756: quoted string not properly terminated
出现ORA-01756: quoted string not properly terminated 后,查看SQL是否有中文符号 修改为英文的符号,运行正常
- 工作记录:记一次线上ZK掉线问题排查
目录 问题的发现 zk的情况以及分析 总结 问题的发现 最早问题的发现在于用户提的,用户提出他支付时支付失败,过了一会儿再试就好了,于是翻日志,查询到当时duboo调用出现了下类错误: [TraceI ...
- 【一天一个知识点系列】- Http之状态码
状态码 简介 HTTP 状态码负责表示客户端 HTTP 请求的返回结果. 标记服务器端的处理是否正常. 通知出现的错误等工作 作用及类别 作用:状态码告知从服务器端返回的请求结果 状态码的类别 注意: ...
- python3.8.1安装cx_Freeze
按照官网的提示命令python -m pip install cx_Freeze --upgrade安装,不成功,报了一个错误,说cx_Freeze找不到需要的版本,还有一些警告说PIP需要升级,没理 ...
- 如何在K8s,Docker-Compose注入镜像Tag
最近在做基于容器的CI/CD, 一个朴素的自动部署的思路是: 从Git Repo打出git tag,作为镜像Tag ssh远程登录到部署机器 向部署环境注入镜像Tag,拉取镜像,重新部署 下面分享我是 ...
- java 日期与时间操作
我们先来了解一下基本的概念 日期 2020-11-21 2020-11-22 时间 15:36:43 2020-11-21 15:36:43 时区 北京时间 2020-11-21 15:36:43 东 ...
- Java集合List-差集、并集、交集
Java集合List的差集.并集.交集 转载于:https://www.cnblogs.com/qlqwjy/p/9812919.html 一.List的差集 @Test public void te ...
- Py编程方法,尾递归优化,map函数,filter函数,reduce函数
函数式编程 1.面向过程 把大的问题分解成流程,按照流程来编写过程 2.面向函数 面向函数编程=编程语言定义的函数+数学意义上的函数先弄出数学意义上的方程式,再用编程方法编写这个数学方程式注意面向函数 ...
- SpringMVC Tomcat 启动时报错:java.lang.IllegalStateException: Error starting child
大概原因如下: 1.Controller里RequestMapping("/test")前面没有"/"; 2.jar包冲突,比如我的将数据库连接版本由5.1.6 ...