链接:

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

题意:

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).

思路:

枚举使用的个数,判断n-i*p能不能满足条件。

代码:

#include <bits/stdc++.h>
typedef long long LL;
using namespace std; LL n, p; int Cal(LL x)
{
int cnt = 0;
while (x)
{
if (x&1)
cnt++;
x >>= 1;
}
return cnt;
} int main()
{
ios::sync_with_stdio(false);
cin >> n >> p;
for (int i = 1;i <= 1e4;i++)
{
LL v = n-p*i;
if (v <= 0)
break;
int num = Cal(v);
if (num <= i && i <= v)
{
cout << i << endl;
return 0;
}
}
cout << -1 << endl; return 0;
}

Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary的更多相关文章

  1. 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. 题解:很显然只有 \( ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. if("\v"=="v")来判断IE浏览器

    if(!+"\v1"){ IE代码}else{ 其他浏览器代码} if("\v"=="v"){//true为IE浏览器, document. ...

  2. [转帖]linux下查找文件及查找包含指定内容的文件常用命令。

    linux下查找文件及查找包含指定内容的文件常用命令. https://blog.csdn.net/yangyu19910407/article/details/18266821 最简单的查找 fin ...

  3. selenium之定位详篇

    一.Web定位方法(find_elements返回元素list) 1.id: find_element_by_id()  # find_element_by_id("id") fi ...

  4. 抽象类 and 接口

    目录 抽象类 抽象类的域和方法的权限: 接口 接口中的域和方法的权限: 实现多个接口 接口继承 接口嵌套 抽象类 一个类,如果有抽象方法(没有方法体),则该类必须被限定为抽象类(abstract):当 ...

  5. LC 33. Search in Rotated Sorted Array

    问题描述 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ...

  6. WUSTOJ 1275: 男神的逆袭(Java)

    1275: 男神的逆袭 题目   计算两个日期相差的天数.更多内容点击标题. 分析   下面说一下我的思路(自己写的,无扩展性): 给定一个日期,首先计算这个日期是这一年的第多少天. 给定两个日期,直 ...

  7. Django入门(下)

    一.创建APP 在每一个django项目中可以包含多个APP,相当于一个大型项目中的分系统.子模块.功能部件等.互相之间比较独立,但也有联系. 在pycharm下方的Terminal终端中输入命令: ...

  8. 移动构造函数应用最多的地方就是STL中(原文详解移动构造函数)

    移动构造函数应用最多的地方就是STL中 给出一个代码,大家自行验证使用move和不适用move的区别吧 #include <iostream> #include <cstring&g ...

  9. LOJ2557 CTSC2018组合数问题(提交答案)

    直接利用simulator退火应该可以得到大量分数. op=1:1,4,5,6,10 即构造序列{ai},最小化Σti,ai+rai,aj. 1:暴搜/退火. 4:观察到图大致成一条链(注意其中有两个 ...

  10. RMAN执行crosscheck archive报错ORA-19633问题处理

    一.问题现象 RMAN connect target /; run { crosscheck archivelog all; } ORA-: control file record is out of ...