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 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的更多相关文章
- 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) 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 ...
随机推荐
- [转帖]详解shell脚本括号区别--$()、$「 」、$「 」 、$(()) 、「 」 、「[ 」]
详解shell脚本括号区别--$().$「 」.$「 」 .$(()) .「 」 .「[ 」] 原创 波波说运维 2019-07-31 00:01:00 https://www.toutiao.com ...
- MarkdownPad 2 用 LaTeX 编写公式(17)
方法一:(可离线显示) 1.解压「jaxedit-master.zip」,解压后找到「jaxedit-master」文件夹下「MathJax.js」文件的路径,这个文件在该文件下的路径是「jaxedi ...
- C++经典类库
现实中,C++的库门类繁多,解决的问题也是极其广泛,库从轻量级到重量级的都有.本文为你介绍了十一种类库,有我们常见的,也有不常见的,一起来看. C++类库介绍 再次体现了C++保持核心语言的效率同时大 ...
- Linux下的静态库与动态库的生成与调用
静态库与动态库 静态函数库 这类库的名字一般是libxxx.a,xxx为库的名字.利用静态函数库编译成的文件比较大,因为整个函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行 ...
- PCA降维笔记
PCA降维笔记 一个非监督的机器学习算法 主要用于数据的降维 通过降维, 可以发现更便 于人类理解的特征 其他应用:可视化:去噪 PCA(Principal Component Analysis)是一 ...
- linux重启php服务
- (三)easyUI之树形组件
一.同步树 1.1 概念 所有节点一次性加载完成 1.2 案例 1.2.1 数据库设计 1.2.2 编码 index.jsp <%@ page language="java" ...
- 奇妙的算法【4】-汉诺塔&哈夫曼编码
1,汉诺塔问题[还是看了源码才记起来的,记忆逐渐清晰] 汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着6 ...
- SQL优化中的重要概念:锁定
原文:SQL优化中的重要概念:锁定 上篇文章讲的是事务,这篇就引出另一个重要概念,就是锁定. 当一个用户要读取另一个用户正在修改的数据,或者一个用户正在修改另一个用户正在读取的数据,或者一个用户要修改 ...
- 解决https 请求过程中SSL问题
最近一个项目中用到了https的请求,在实际调用过程中发现之前的http方法不支持https,调用一直报错. 查询了一下,添加几行代码解决问题. public string HttpPost(stri ...