链接:

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. oracle管道函数的用法(一行拆为多行)

    oracle管道函数是一类特殊的函数,oracle管道函数返回值类型必须为集合 如果需要在客户端实时的输出函数执行过程中的一些信息,在oracle9i以后可以使用管道函数(pipeline funct ...

  2. 汉字在unicode中的位置

    在www.unicode.org中查找汉字.china找不到,后来查资料才明白,应该查CJK,为什么内? unicode这个组织吧中国日本韩国的字合并了   中日韩统一表意文字(CJK Unified ...

  3. python实现数字0开始的索引,对应Execl的字母方法

    字母转数字方法: import re col = row = [] # 输入正确格式的定位,A2,AA2有效,AAB2无效 while len(col) == 0 or len(row) == 0 o ...

  4. uboot 与 代码重定位

    ref: https://blog.csdn.net/dhauwd/article/details/78566668 https://blog.csdn.net/yueqian_scut/articl ...

  5. [Vue]避免 v-if 和 v-for 用在同一个元素上

    一般我们在两种常见的情况下会倾向于这样做: 情形1:为了过滤一个列表中的项目 (比如 v-for="user in users" v-if="user.isActive& ...

  6. (十)springmvc之文件的处理

    一.同步上传文件 导入common-fileupload这个jar包. 配置     springmvc-servlet.xml <?xml version="1.0" en ...

  7. C# 控制台日历 region分区编写思想

    已知道1900-1-1为星期一. 模块分区 //获取用户的正确输入并分别保存到变量year和month中 //声明一个用于保存空白和当月日期数的集合dates //遍历输出集合dates using ...

  8. vue html属性绑定

    业务场景: element ui里面select的组件,通过勾选是否启动搜索功能 解决: 在select组件上绑定一个 :filterable,值就是是否可搜索的checkbox的值  true和fa ...

  9. 关于jQuery的源码学习

    注:该思维学习自另一个博客:https://blog.csdn.net/software0017/article/details/80317348 以下为我自己总结的jQuery结构:

  10. 简单SQL注入试探、二

    DVWA——简单SQL注入小记 今天我们来记录简单的盲注过程 简单的SQL injection(blind) Level:low 登陆后选择SQL Injection(Blind) 能看到这样的界面 ...