题目链接: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的更多相关文章

  1. Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)

    题意:有\(n\)个点,\(m\)条边,现在要给这些点赋值,,每次只能赋给某一点的四周(所连边)的最小没出现过的值.如果不能按照所给的数赋值,输出\(-1\),否则输出赋值顺序. 题解:我们用\(pa ...

  2. Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! C. Johnny and Another Rating Drop (规律,二进制)

    题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数. 题解:打表找规律,不难发现: ​ \(00000\) ​ \(00001\) ​ ...

  3. 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)即为 ...

  4. Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)

    题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...

  5. Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)

    题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...

  6. Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)

    题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...

  7. Codeforces Round #647 (Div. 2) A. Johnny and Ancient Computer

    题目链接:https://codeforces.com/contest/1362/problem/A 题意 有一个正整数 $a$,可选择的操作如下: $a \times 2$ $a \times 4$ ...

  8. 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 ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. 配置 Docker 镜像加速源地址

    docker 安装官方文档 根据实例的操作系统类型,参考相应的文档进行安装. 查看 linux 是 CentOS 还是 Ubuntu uname -a #查看系统信息 lsb_release -a # ...

  2. ctfhub技能树—文件上传—双写后缀

    双写后缀绕过 用于只将文件后缀名,例如"php"字符串过滤的场合: 例如:上传时将Burpsuite截获的数据包中文件名[evil.php]改为[evil.pphphp],那么过滤 ...

  3. webapi Swagger 配置 services.BuildServiceProvider() 报警 ASP0000 问题处理

    问题起源 网上的常见配置 Swagger 配置 在Startup类的 ConfigureServices 使用 services.BuildServiceProvider() ,其中有段代码如下: v ...

  4. [Usaco2016 Dec]Moocast

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4749 可以对于每个点\(i\),往跟\(i\)距离小于等于\(p[i]\)的点\(j\)都 ...

  5. 计算机之路 -MySQL 初学

    照着电脑学了一天终于把MySQL装上了. 明天打算重新装一次 然后再自己记录一下步骤

  6. ModelForm的基本用法:

    一.ModelForm的基本用法示例: from django import forms from app01 import models class BookModelForm(forms.Mode ...

  7. 【9k字+】第二篇:进阶:掌握 Redis 的一些进阶操作(Linux环境)

    九 Redis 常用配置文件详解 能够合理的查看,以及理解修改配置文件,能帮助我们更好的使用 Redis,下面按照 Redis 配置文件的顺序依次往下讲 1k 和 1kb,1m 和 1mb .1g 和 ...

  8. SpringCloud zuul 网关限流分析

    最近项目中 spring cloud zuul 运用到限流功能,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结果 yml.pom配置 强烈推荐,按最新gith ...

  9. Fastjson1.2.24反序列化漏洞复现

    Fastjson1.2.24 目录 1. 环境简介 1.1 物理环境 1.2 网络环境 1.3 工具 1.4 流程 2. Docker+vulhub+fastjson1.2.24 2.1 Docker ...

  10. Tensorflow-交叉熵&过拟合

    交叉熵 二次代价函数 原理 缺陷 假如我们目标是收敛到0.A点为0.82离目标比较近,梯度比较大,权值调整比较大.B点为0.98离目标比较远,梯度比较小,权值调整比较小.调整方案不合理. 交叉熵代价函 ...