链接:

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. 【C++札记】命名空间(namespace)

    介绍 命名空间可以解决程序中的同名冲突,尤其大型项目多人开发中经常用到.比如我们使用C++的标准输出std::cout就使用了std命名空间. 使用作用域符:: #include <iostre ...

  2. 5-6 c语言之【枚举,联合体,递归】

    今天学习了枚举,联合体,递归,重点在于递归,所以从网上找到了一些递归的题目进行练习,毕竟程序员界流传一句话,会用循环的是人,会用递归的是神,哈哈哈 还是按次序进行梳理,第一个枚举,枚举和宏定义很相似, ...

  3. Geometers Anonymous Club CodeForces - 1195F (闵可夫斯基和)

    大意: 给定$n$个凸多边形, $q$个询问, 求$[l,r]$内闵可夫斯基区间和的顶点数. 要用到一个结论, 闵可夫斯基和凸包上的点等于向量种类数. #include <iostream> ...

  4. hdu 6377 度度熊看球赛 (dp)

    大意: $n$对情侣, $2n$个座位, 对于一个方案, 若$k$对情侣相邻, 则喧闹值增加$D^k$, 求喧闹值期望. 跟CF 840C一样, 设$dp[i][j]$为$i$个人, 有$j$对情侣相 ...

  5. vscode安装

    vscode是一个很好用而且开源的编辑器,支持多种编程语言,这里稍微总结一下使用,省的以后再安装时候再百度查 下载地址:https://code.visualstudio.com/,打开以后点击dow ...

  6. 关于MySQL的驱动org.gjt.mm.mysql.Driver

    今天看了一个比较老视频使用org.gjt.mm.mysql.Driver来驱动连接,便试了一下看看怎么样,结果一直连不上数据库,后来看了tomcat的后台发现有报这个问题,于是把驱动改成com.mys ...

  7. C#强制回收垃圾

    [DllImport("psapi.dll")] private static extern int EmptyWorkingSet(int hProcess); public v ...

  8. 关于__new__和__init__

    关于__new__和__init__ 例如一个类 class Foo(object): def __init__(self): print(1) def __new__(self): print(2) ...

  9. CAS单点登录相关配置

    一.CAS单点登录服务端的部署 部署 把CAS所对应的war包部署到tomcat中 4.品优购资源V1.3\配套软件\配套软件\CAS\cas.war 配置 更改tomcat的端口号 <Conn ...

  10. js的页面交互

    与html标签建立关系 //获取标签,js如何与html标签建立联系 两种方式 //1. let num = document.getElementsByClassName('d1'); consol ...