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 ...
随机推荐
- POJ3436
题目链接:http://poj.org/problem?id=3436 题目大意: 一台电脑可以分成P个部分,在生产过程中,半成品电脑有的部分已经完成(记为1),而有的部分还没有完成(记为0).电脑生 ...
- 解决iframe重定向让父级页面跳转
原文:http://www.jb51.net/article/40583.htm 有内嵌iframe的页面,当session过期时,点击连接重定向后的跳转会在iframe中跳转,在登录页面中加入下面的 ...
- Java 泛型与集合
1.List练习,请用泛型的写法来完成. 已知有一个Worker 类如下: public class Worker { private int age; private String name; p ...
- 【SocketIoClientDotNet】Nuget包安装问题
问题: Nuget安装[SocketIoClientDotNet]失败 错误信息: Operation failed Expected 1 export(s) with contract name & ...
- 公有继承中派生类Student对基类Person成员的访问 代码参考
#include <iostream> #include <cstring> using namespace std; class Person { private: char ...
- DDD之1微服务设计为什么选择DDD
背景 名词解释 如果你的团队目前正是构建微服务架构风格的软件系统,问自己两个问题? 软件架构演进 软件架构大致经历了从单机架构,集中式架构,分布式微服架构,程序的层次图如下所示. 单机架构 特点如下: ...
- 利用metasploit复现永恒之蓝
环境 目标机器:windows 7 ,172.16.136.169 攻击机:安装了Metasploit 的 ubuntu16.04 ,172.16.136.130 (安装Metasploit:在 Ub ...
- 08 . Python3高阶函数之迭代器、装饰器
Python3高阶函数之迭代器.装饰器 列表生成式 推导式就是构建比较有规律的列表,生成器. 孩子,我现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],我要求你把列表里 ...
- Remote desktop cannot verify?教你如何应对
远程桌面:IIS7远程桌面IIS7远程桌面管理工具(3389.vps.服务器批量管理.批量远程工具)简介: 1.批量管理WIN系列服务器,VPS,电脑. 2.批量导入服务器的IP,端口,账号和密码 ...
- 快速复习C语言 - 1变量与运算符
变量与运算符 本篇以读者知道 int.char.float.double 等数据类型为前提条件. float 类型注意事项 float 类型数没有办法跟一个数真正比较是否相等,可以定义借助绝对值在一定 ...