[CodeForces - 1225C]p-binary 【数论】【二进制】
[CodeForces - 1225C]p-binary 【数论】【二进制】
标签: 题解 codeforces题解 数论
题目描述
Time limit
2000 ms
Memory limit
524288 kB
Source
Technocup 2020 - Elimination Round 2
Tags
bitmasks brute force math *1600
Site
https://codeforces.com/problemset/problem/1225/c
题面
Example
Input1
24 0
Output1
2
Input2
24 1
Output2
3
Input3
24 -1
Output3
4
Input4
4 -7
Output4
2
Input5
1 1
Output5
-1
题目大意
给定\(n, p\),使\(n\)能表达成这样的形式\(n = \sum_{i = 1}^{m}(2^{a[i]} + p)\)(\(a[i] = 0,1,2,3, \dots\))。问最小的\(m\)是多少?如果无法写成上述表达,则输出-1。
例如,
给定\(n = 24, k = 1\),\(24 = (2^4 + 1) + (2^2 + 1)+ (2^0 + 1)\)。这样\(m\)最小为3。
解析
可将上式变形,\(n - m \times p = \sum_{i = 1}^{m}2^{a[i]}\)。
令\(d(x)\)表示\(x\)的二进制形式中\(1\)的个数。
我们不难发现,满足\(d(n - m \times p) \leq m \leq n - m \times p\),即有\(n - m \times p = \sum_{i = 1}^{m}2^{a[i]}\)。
因为\(2^i = 2^{i - 1} + 2 ^ {i - 1}\),所以\(m\)可以大于这个数的二进制中\(1\)的个数。
而\(2^0 = 1\)的时候就无法再往下分了,所以\(m\)要小于等于这个数的本身。
这样我们就可以通过简单枚举\(m\)得出答案。
为什么m可以通过枚举得出?m不会很大吗?
\(n - m \times p = \sum_{i = 1}^{m}2^{a[i]}\)等式左边是线性增长,等式右边是指数增长。能使等号成立的\(m\)不会很大。
通过代码
/*
Status
Accepted
Time
31ms
Memory
8kB
Length
584
Lang
GNU G++11 5.1.0
Submitted
2019-12-20 09:17:54
RemoteRunId
67258530
*/
#include <bits/stdc++.h>
#define lowbit(i) i & -i //一个数的二进制表示中,1的最低位.
using namespace std;
const int INF = 1e5;
int n, p;
int binary_digit(int x) //找到一个数的二进制表示中,有几个1.
{
int cnt = 0;
while(x){
x -= lowbit(x);
cnt ++;
}
return cnt;
}
void work()
{
for(int i = 1; i < INF; i ++){ //枚举.
if(n - i * p < 0){ //n>0,出现小于0的情况就直接结束.
puts("-1");
return;
}
if(binary_digit(n - i * p) <= i && i <= n - i * p){ //落在这个区间的就能满足等式.
printf("%d", i);
return;
}
}
puts("-1");
return;
}
int main()
{
scanf("%d%d", &n, &p);
work();
return 0;
}
[CodeForces - 1225C]p-binary 【数论】【二进制】的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Binary Gap 二进制间隙
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- LeetCode OJ:Add Binary(二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- codeforces gym/100814 humming distance (二进制位数比较)
Gym - 100814I I. Salem time limit per test 1 second memory limit per test 1024 megabytes input stand ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
随机推荐
- MYSQL删除
1.使用360卸载,并强力删除相关东东 2.清理注册表: A.HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application ...
- 生活小插曲(长篇连载,持续更新ing)^_^
这个帖子,长期记录一些小小的生活插曲 在北京朋友开店了-关于同学开快餐店的故事.今天下午听说这个朋友在附近开了一个店,下午5点多吧,出门去他那里去了.走路过去的.在那里聊了将近一个小时吧.对最近我们自 ...
- Python列表中的字典按照该字典下的键值进行排序
列表中的字典按照该字典下的键值进行排序 这算是排序中比较复杂的一种情况吧,多重嵌套,按照某种规则进行排序.如下面这个json(注:这里这是该列表中的一个项): [ { "stat" ...
- idea object is not a member of package
可能的原因: 1. pom.xml 依赖的工程没有deploy, 所有需要本地install改工程 2. 工程以来的库没有加入到 pom.xml中 3. 以上都没有问题,则右键该工程 => ma ...
- 深入理解 BigDecimal
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Json schema 以及在python中的jsonschema
目录 1. JSON Schema简介 2. JSON Schema关键字详解 2.1 $schema 2.2 title和description 2.3 type 3 type常见取值 3.1 当t ...
- react-native Android release打包失败
npm run build报错(android) react-native 0.5x在安卓环境 gradle 3.x版本下编译release版本的时候提示编译失败,但是debug模式下是没有问题的. ...
- 第一节:Shiro HelloWorld 实现
1.新建maven工程,pom配置maven jar包 <dependency> <groupId>org.apache.shiro</groupId> <a ...
- 常见的linux快捷方式和英文错误提示
第5章 linux常见的快捷方式 Ctrl +l 清屏的意思 2 Ctrl +c 退出当前的进程 3 Ctrl +w 删除光标到空格之间的信息 4 Ctrl +a 快速移动到光标行首 5 Ctrl + ...
- Spring整合Shiro 权限 角色 用户关系分析
Spring整合Shiro 权限 角色 用户关系分析 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 前置内容 之前我们学习了,使用注解的方式去完成权限的控制,当然,也是静态的,也就 ...