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 ...
随机推荐
- selenium爬虫 | 爬取疫情实时动态
import csvimport selenium.webdriverfrom selenium.webdriver.chrome.options import Optionsclass spider ...
- python sqlite3增加表字段
给sqlite3表格增加新字段,要注意大小写,要不然不成功. 一开始这样写,不成功! 后面规范写,按大小写严格规范写! 成功了!现在查看新增加的字段commit: 仔细看,这下全部小写,括表名称.co ...
- 攻防世界—pwn—level0
题目分析 下载文件后首先使用checksec检查文件保护机制 文件名太长了,就更改了一下 发现是一个64位程序,使用ida查看伪代码 注意到一个特殊的函数名callsystem 确定思路,直接栈溢出 ...
- kubectl命令管理
kubectl命令管理 查看更多帮助命令 [root@k8s-master ~]# kubectl --help 创建一个命名空间 [root@k8s-master ~]# kubectl creat ...
- WPF NET5 Prism8.0的升级指南
前言 曾经我以学习的目的写了关于在.NET Core3.1使用Prism的系列文章.NET Core 3 WPF MVVM框架 Prism系列文章索引,也谢谢大家的支持,事实上当初的版本则是Pri ...
- Nginx(七):location的使用以及nginx关闭原理
上一篇中,我们了解了如何nginx的配置原则及解析框架,以及解析location配置的具体实现,相信大家对该部分已经有了比较深刻的认识. 本篇,我们进一步来了解下,解析之后的配置,如何应用到实际中的吧 ...
- Http中的options请求
引自:https://www.jianshu.com/p/5cf82f092201.https://www.cnblogs.com/mamimi/p/10602722.html 一.options是什 ...
- MongoDB 总结
目录 1. 逻辑结构 2. 安装部署 2.1 系统准备 2.2 mongodb安装 2.2.1 创建所需用户和组 2.2.2 创建mongodb所需目录结构 2.2.3 上传并解压软件到指定位置 2. ...
- jmeter---线程组执行顺序记录
一.默认未勾选测试计划中独立运行每个线程组时,线程组并行执行,如下,设置三个请求,每个线程组设置5秒启动5个线程. 未勾选独立运行 运行结果如下,三个线程并行执行 勾选后,一个线程组执行完后才执行下一 ...
- 详解Mybatisplus
详解Mybatisplus MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性: 无侵入**:只 ...