Problem A

https://codeforces.com/contest/1362/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

https://codeforces.com/contest/1362/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

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\)然后累加即可得到最后的结果

#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)的更多相关文章

  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. 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。

    Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...

  2. Text Reverse(hdu1062)

    输入方式:先输入整数,再循环输入字符串. 思考:字符串中有空格.那么要在字符串大循环输入前,首先,用"getchar()"函数读取scanf_s()函数缓冲区的空格或者空行或者换行 ...

  3. IDEA提高开发效率的7个插件

    IDEA提高开发效率的7个插件 1. 多行编辑 先来体验一下从xml文件拷贝字段新建实体对象 一般我们为了新建多表连接后映射的 ResultMap ,耗费不少时间,那么我们就来试一试这个多行编辑 表字 ...

  4. vue项目报错Missing space before function parentheses的问题

    问题描述为——函数括号前缺少空格 导致原因主要是,使用eslint时,严格模式下,会报错Missing space before function parentheses的问题,意思是在方法名和刮号之 ...

  5. JS获取两个日期间的所有日期

    var stime = '2018-07-25'; //开始日期 var etime = '2018-08-02'; //结束日期 getdiffdate(stime,etime); //获取两日期之 ...

  6. MongoDB -MSC集群的部署

    一.Shard节点配置过程 1. 目录创建:mkdir -p /mongodb/38021/conf  /mongodb/38021/log  /mongodb/38021/datamkdir -p ...

  7. JavaScript中foreach、map、filter、find、every、some的用法

    foreach:只是循环数组中的每一项,没有返回值 如:  var arr = [2,3,3,4,5,6]; arr.foreach(function(item,index,array){ dosom ...

  8. 【C++】常见易犯错误之数值类型取值溢出与截断(1)

    1. 数据类型数值范围溢出 如标题所述,该错误出现的原因是由于变量的值超出该数据类型取值范围而导致的错误. 例题如下: (IDE环境:C-Free,编译器为mingw5,如下图) # include ...

  9. meavn项目由打包方式jar改为war报Cannot install Dynamic Web Module 2.5 facet. It is incompatibile with already installed facets: Utility Module. Please modify project configuration.处理方式

    找到  \项目名\.setting\文件夹下的   org.eclipse.wst.common.project.facet.core.xml  xml文件. 添加或修改 <installed ...

  10. Chisel3 - 模块

    https://mp.weixin.qq.com/s/2vjM-gcauvHnn6KJzlOm4g   Chisel的模块和Verilog的模块很相似,都用来定义模块结构(hierarchical s ...