Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary 水题
C. p-binary
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2x+p, where x is a non-negative integer.
For example, some −9-binary ("minus nine" binary) numbers are: −8 (minus eight), 7 and 1015 (−8=20−9, 7=24−9, 1015=210−9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 20+21+22.
And if p=−9 we can represent 7 as one number (24−9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1≤n≤109, −1000≤p≤1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer −1. Otherwise, print the smallest possible number of summands.
Examples
input
24 0
output
2
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24=(24+0)+(23+0).
In the second sample case, we can represent 24=(24+1)+(22+1)+(20+1).
In the third sample case, we can represent 24=(24−1)+(22−1)+(22−1)+(22−1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4=(24−7)+(21−7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
题意
定义p-binary为2^x+p
现在给你一个数x,和一个p。
问你最少用多少个p-binary能构造出x,如果没有输出-1
题解
转化为:
x = 2^x1 + 2^x2 + ... + 2^xn + n*p
首先我们知道任何数都能用二进制表示,如果p=0的话,肯定是有解的。那么答案最少都是x的2进制1的个数。
另外什么情况无解呢,即x-n*p<0的时候肯定无解,可以更加有优化为x-n*p<n的时候无解。
答案实际上就是n,我们从小到大枚举n,然后check现在的2进制中1的个数是否小于等于n。
代码
#include<bits/stdc++.h>
using namespace std;
int Count(int x){
int number=0;
for(;x;x-=x&-x){
number++;
}
return number;
}
int main(){
int n,p,ans=0;
scanf("%d%d",&n,&p);
while(1){
n-=p;
ans++;
int cnt=Count(n);
if(ans>n){
cout<<"-1"<<endl;
return 0;
}
if(cnt<=ans){
cout<<ans<<endl;
return 0;
}
}
}
Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary 水题的更多相关文章
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)
A - Forgetting Things 题意:给 \(a,b\) 两个数字的开头数字(1~9),求使得等式 \(a=b-1\) 成立的一组 \(a,b\) ,无解输出-1. 题解:很显然只有 \( ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products
链接: https://codeforces.com/contest/1247/problem/D 题意: You are given n positive integers a1,-,an, and ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary
链接: https://codeforces.com/contest/1247/problem/C 题意: Vasya will fancy any number as long as it is a ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)
链接: https://codeforces.com/contest/1247/problem/B2 题意: The only difference between easy and hard ver ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things
链接: https://codeforces.com/contest/1247/problem/A 题意: Kolya is very absent-minded. Today his math te ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题
F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) E. Rock Is Push dp
E. Rock Is Push You are at the top left cell (1,1) of an n×m labyrinth. Your goal is to get to the b ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B. TV Subscriptions 尺取法
B2. TV Subscriptions (Hard Version) The only difference between easy and hard versions is constraint ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things 水题
A. Forgetting Things Kolya is very absent-minded. Today his math teacher asked him to solve a simple ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力
D. Power Products You are given n positive integers a1,-,an, and an integer k≥2. Count the number of ...
随机推荐
- 如何在python中使用Elasticsearch
什么是 Elasticsearch 想查数据就免不了搜索,搜索就离不开搜索引擎,百度.谷歌都是一个非常庞大复杂的搜索引擎,他们几乎索引了互联网上开放的所有网页和数据.然而对于我们自己的业务数据来说 ...
- 第04组 Alpha冲刺(1/4)
队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.安排好各个组员的任务 2.收集各个组员的进度 3.写页面 4.写博客 展示Gi ...
- typescript里一些有趣的点
联合类型 在原生的JS里,null和undefined经常会导致BUG的产生, 在ts里,你又想用null,又担心出错的时候 你可以考虑用联合类型,当某值可能为 number或null,你可以声明它的 ...
- Golang面向并发的内存模型
Import Advanced Go Programming 1.5 面向并发的内存模型 在早期,CPU都是以单核的形式顺序执行机器指令.Go语言的祖先C语言正是这种顺序编程语言的代表.顺序编程语言中 ...
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 7
23.5 创建RESTful规范 WebAPI框架 虽然我们现在可以自己实现API了,也了解了RESTful API的设计原则,但让自己实现的API符合RESTful API规范,对很多刚接触API ...
- 【linux命令】权限管理命令(chattr、lsattr、sudo)
目录 chattr lsattr sudo 一.chattr命令 chattr命令用来修改文件系统的权限属性,只有 root 用户可以使用,建立凌驾于 rwx 基础权限之上的授权. PS:chattr ...
- spring的事件机制实战
理论 在分布式场景下,实现同步转异步的方式有三种方式: 1.异步线程池执行:比如借助@Asyn注解,放到spring自带的线程池中去执行: 2.放到消息队列中,在消费者的代码中异步的消费,执行相关的逻 ...
- Python 如何操作微信
1.给文件传输助手发一条消息 import itchat itchat.auto_login(enableCmdQR=True) # 这里需要你人工手机扫码登录 itchat.send('Hello, ...
- (四)初识NumPy(函数和图像的数组表示)
本章节主要介绍NumPy中的三个主要的函数,分别是随机函数.统计函数和梯度函数,以及一个较经典的用数组来表示图像的栗子!,希望大家能有新的收货,共同进步! 一.np.random的随机函数(1) ra ...
- 【CodeChef】Find a special connected block - CONNECT(斯坦纳树)
[CodeChef]Find a special connected block - CONNECT(斯坦纳树) 题面 Vjudge 题解 还是一样的套路题,把每个数字映射到\([0,K)\)的整数, ...