Problem A

https://codeforces.com/contest/1362/problem/A

判断x/y是不是2的k次方, 如果是

k/3 + (k%3)/2 + (k%3%2)即为答案

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll log2(ll a) {
  5. ll count = 0;
  6. while (1) {
  7. if (a >>= 1)
  8. count++;
  9. else
  10. break;
  11. }
  12. return count;
  13. }
  14. int main(){
  15. ios::sync_with_stdio(false);
  16. cin.tie(0);
  17. //freopen("input.txt","r",stdin);
  18. //freopen("output.txt","w",stdout);
  19. int t;cin>>t;
  20. ll x,y;
  21. ll ans,tmp;
  22. while(t--){
  23. cin>>x>>y;
  24. if(x<y)swap(x,y);
  25. if(x==y)cout<<"0"<<endl;
  26. else{
  27. if(x%y!=0||(x%y==0&&(x/y&(x/y-1))!=0))cout<<"-1"<<endl;
  28. else{
  29. tmp = log2(x/y);
  30. ans = tmp/3 + (tmp%3)/2 + (tmp%3%2);
  31. cout<<ans<<endl;
  32. }
  33. }
  34. }
  35. return 0;
  36. }

Problem B

https://codeforces.com/contest/1362/problem/B

一开始以为模拟过不了, 结果直接暴力模拟就好了...

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. ios::sync_with_stdio(false);
  5. cin.tie(0);
  6. //freopen("input.txt","r",stdin);
  7. //freopen("output.txt","w",stdout);
  8. set<int> S;
  9. int t;cin>>t;
  10. while(t--){
  11. int n,s,tmp=0,max=-1,flag=1;
  12. cin>>n;
  13. while(n--)
  14. {
  15. cin>>s;
  16. if(s>max)
  17. max=s;
  18. S.insert(s);
  19. }
  20. for(int i=1;i<=1024;++i){
  21. flag = 1;
  22. for(set<int>::iterator it = S.begin(); it!= S.end(); it++)
  23. {
  24. tmp = i^*it;
  25. if(!S.count(tmp)){
  26. flag=0;
  27. }
  28. }
  29. if(flag==1){
  30. cout<<i<<endl;
  31. break;
  32. }
  33. }
  34. if(flag==0)cout<<"-1"<<endl;
  35. S.clear();
  36. }
  37. return 0;
  38. }

Problem C

https://codeforces.com/contest/1362/problem/C

这题我是打表找规律找出来的

首先可以发现像1,10,100,1000,10000这样的数, 假设最高位为第k位

他们的答案应该是\(2^k-1\) , 又由于一个数可以表示成很多个这样的数相加

比如:

110010 = 100000 + 10000 + 10

那么只需要把第k位为1的各项依次取\(2^k-1\)然后累加即可得到最后的结果

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. int main(){
  5. ios::sync_with_stdio(false);
  6. cin.tie(0);
  7. int t;cin>>t;
  8. ll sum=0,cnt=1,tmp=0;
  9. while(t--){
  10. ll n;cin>>n;
  11. while(1)
  12. {
  13. tmp = n&1;
  14. n = n>>1;
  15. if(tmp==1)
  16. sum+=pow(2,cnt)-1;
  17. if(n==0)break;
  18. cnt++;
  19. }
  20. cout<<sum<<endl;
  21. cnt=0;sum=0;
  22. }
  23. return 0;
  24. }

另外一种解法:

for example: input = 5(101)

先来看一组数 :

  1. 000
  2. 001
  3. 010
  4. 011
  5. 100

可以看到, 第一位每次都变, 第二位每2次变一次, 第三位每4次变一次, 即第k位每\(2^{k+1}\)改变一次, 对于一个十进制数n, 只需要累加\(n/2^{k-1}\)(k表示第k位)即可

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. int main(){
  5. ios::sync_with_stdio(false);
  6. cin.tie(0);
  7. int t;cin>>t;
  8. while(t--){
  9. ll ans=0,tmp=0,bi=1;
  10. ll n;cin>>n;tmp=n;
  11. while(n)
  12. {
  13. ans+=tmp/bi;
  14. bi<<=1;
  15. n>>=1;
  16. }
  17. cout<<ans<<endl;
  18. }
  19. return 0;
  20. }

Codeforces Round #647 (Div. 2)的更多相关文章

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

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

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

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

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

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

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

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

  5. 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 一次操作 ...

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

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

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

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

  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. 201771010117马兴德 实验二 Java基本程序设计(1)

    实验二 Java基本程序设计(1)  第一部分    理论知识的学习. 第三章Java基本程序设计结构 1  基本知识: (1)标识符:标识符由字母.下划线.美元符号和数字组成,且第一个符号不能为数字 ...

  2. python爬虫需要掌握哪些知识点

    1.熟练掌握Python语言 2.熟练掌握JS/HTML/CSS,了解HTTP协议. 3.熟练使用chrome 4.爬虫框架 推荐几个大神的教学干货,大家共同进步! https://zhuanlan. ...

  3. c# 优化代码的一些规则——const 和 readonly[二]

    前言 在c# 的世界中,在初学的时候,可能很难区分readonly 和 const,两者都是不可修改. 看到这两个单词,我们想的是,最多他们的区别也不会太大.然后事实却出乎我们的意料. 正文 这两个声 ...

  4. SimpleAuthenticationInfo的参数

    SimpleAuthenticationInfo的参数 仅供个人参考,以及学习记录.SimpleAuthenticationInfo authenticationInfo = new SimpleAu ...

  5. 使用 IdentityService4 集成小程序登录一种尝试

    1 场景介绍 主要业务是通过 App 承载,在 App 中可以注册和登录,为了更好的发展业务引入了微信小程序,于是如何让这两个入口的用户互通便成了需要解决的问题. 看了一下其它 App 大致地思路是两 ...

  6. centos安装以及网络配置

    Linux安装 1.Linux安装完成后 第一个问题就是网络不通的问题 ,问题图片如下: 解决办法: 三种网络模式: 桥接模式:虚拟机和宿主机是兄弟关系,统一由宿主机连接的路由器分发ip NAT模式: ...

  7. flutter 环境出错后排查

    莫名其妙地环境坏了 VSCode 终端里执行 flutter run 卡在 installing.. 模拟器上闪了一下,打不开, 应该是安装出错爆掉了 flutter doctor 检查一下: X A ...

  8. [CSS布局基础]居中布局的实现方式总结

    [原创]码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs 做Web开发少不了做页面布局.码路工人给大家总结一下 ...

  9. 巧用 display: contents 增强页面语义

    display: contents 是一个比较陌生的属性,虽然属于 display 这个基本上是最常见的 CSS 属性,但是 contents 这个取值基本不会用到.但是它早在 2016 年就已经得到 ...

  10. [PHP学习教程 - 类库]002.FTP操作(FTP)

    引言:FTP是大家上传至站点服务器必须要使用的协议.现在常用的FTP客户端工具也很多,如:8uftp,FlashFXP,....但是使用客户端工具就无法真正与自动化联系起来.所以今天,我们为大家讲一下 ...