Codeforces Round #647 (Div. 2)
Problem A
判断x/y是不是2的k次方, 如果是
k/3 + (k%3)/2 + (k%3%2)即为答案
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll log2(ll a) {
ll count = 0;
while (1) {
if (a >>= 1)
count++;
else
break;
}
return count;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int t;cin>>t;
ll x,y;
ll ans,tmp;
while(t--){
cin>>x>>y;
if(x<y)swap(x,y);
if(x==y)cout<<"0"<<endl;
else{
if(x%y!=0||(x%y==0&&(x/y&(x/y-1))!=0))cout<<"-1"<<endl;
else{
tmp = log2(x/y);
ans = tmp/3 + (tmp%3)/2 + (tmp%3%2);
cout<<ans<<endl;
}
}
}
return 0;
}
Problem B
一开始以为模拟过不了, 结果直接暴力模拟就好了...
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
set<int> S;
int t;cin>>t;
while(t--){
int n,s,tmp=0,max=-1,flag=1;
cin>>n;
while(n--)
{
cin>>s;
if(s>max)
max=s;
S.insert(s);
}
for(int i=1;i<=1024;++i){
flag = 1;
for(set<int>::iterator it = S.begin(); it!= S.end(); it++)
{
tmp = i^*it;
if(!S.count(tmp)){
flag=0;
}
}
if(flag==1){
cout<<i<<endl;
break;
}
}
if(flag==0)cout<<"-1"<<endl;
S.clear();
}
return 0;
}
Problem C
这题我是打表找规律找出来的
首先可以发现像1,10,100,1000,10000这样的数, 假设最高位为第k位
他们的答案应该是\(2^k-1\) , 又由于一个数可以表示成很多个这样的数相加
比如:
110010 = 100000 + 10000 + 10
那么只需要把第k位为1的各项依次取\(2^k-1\)然后累加即可得到最后的结果
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;cin>>t;
ll sum=0,cnt=1,tmp=0;
while(t--){
ll n;cin>>n;
while(1)
{
tmp = n&1;
n = n>>1;
if(tmp==1)
sum+=pow(2,cnt)-1;
if(n==0)break;
cnt++;
}
cout<<sum<<endl;
cnt=0;sum=0;
}
return 0;
}
另外一种解法:
for example: input = 5(101)
先来看一组数 :
000
001
010
011
100
可以看到, 第一位每次都变, 第二位每2次变一次, 第三位每4次变一次, 即第k位每\(2^{k+1}\)改变一次, 对于一个十进制数n, 只需要累加\(n/2^{k-1}\)(k表示第k位)即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;cin>>t;
while(t--){
ll ans=0,tmp=0,bi=1;
ll n;cin>>n;tmp=n;
while(n)
{
ans+=tmp/bi;
bi<<=1;
n>>=1;
}
cout<<ans<<endl;
}
return 0;
}
Codeforces Round #647 (Div. 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 #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 一次操作 ...
- 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 #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 ...
随机推荐
- 深入剖析ThreadLocal原理
描述 ThreadLocal,很多地方叫做线程本地变量,也有些地方叫做线程本地存储.ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量. 通常使用静态的变 ...
- Vue基础:子组件抽取与父子组件通信
在工作中承担一部分前端工作,主要使用Vue + Element UI. 随着版本迭代,需求增加,页面往往变得更加臃肿,不易维护.学习子组件的封装和抽取,能更好适应需求. 为什么需要子组件 可复用 将重 ...
- 带你学够浪:Go语言基础系列 - 8分钟学基础语法
对于一般的语言使用者来说 ,20% 的语言特性就能够满足 80% 的使用需求,剩下在使用中掌握. 基于这一理论,Go 基础系列的文章不会刻意追求面面俱到,但该有知识点都会覆盖,目的是带你快跑赶上 Go ...
- xshell使用技巧
XShell是一款Windows下的一款远程连接Linux主机的工具,类似的软件还有SecureCRT,putty等,但是个人感觉XShell好用,功能强大.. 一.复制和粘贴 linux的Shell ...
- Vue 更换页面图标和title
1.基础的做法就是直接换掉,logo换的时候需要使用icon格式的图标 title 直接在index.html 里面把原来的title注释掉 或者直接改了就行 2. 如果需要进行相应的改变啥的 ,需要 ...
- Git-Jenkins-代码的上线
第一章:自动化上线代码基本介绍 1.软件开发生命周期 老板的创意---产品经理---立项---开发团队---测试团队---运维上线 产品经理---加需求---开发团队---测试----更新代码,上线 ...
- JavaScript的流程控制语句以及函数
一.流程控制 1. 作用:控制代码的执行顺序 2. 分类 2.1顺序结构:从上到下依次执行代码语句 2.2选择结构: 1. if语句 简单if结构 if(条件表达式){ 表达式成立时执行的代码段 } ...
- Life In Changsha College- 第三次冲刺
第三次冲刺任务 设计登录注册功能. 用户故事 用户打开“生活在长大”的界面,选择登录 已注册过则输入用户名和密码直接登录 未注册用户则可选择注册功能,注册成功后登录 登录成功则弹出提示框 系统结构图环 ...
- Android_适配器(adapter)之SimpleAdapter
概述 SimpleAdapter是一种 简单的适配器,将静态数据映射到布局xml对应的视图上.它也是BaseAdapter的子类. SimpleAdapter数据映射的组件有3类(从官网api或Sim ...
- MySQL 5.7.30 的安装/升级(所有可能的坑都在这里)
楔子 由于之前电脑上安装的MySQL版本是比较老的了,大概是5.1的版本,不支持JSON字段功能.而最新开发部门开发的的编辑器产品,使用到了JSON字段的功能. 因此需要升级MySQL版本,升级的目标 ...