[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 = ...
随机推荐
- Android项目依赖库管理方式简介
在实际的android项目开发过程中,我们一般都会用一些现有的第三方库来实现我们的需求,避免重复造轮子.普遍使用到的,例如:网络请求库.图片处理库.界面UI库(自定义View.动画效果等).各种第三方 ...
- postgresql密码加强-passwordcheck源码修改三种以上字符
目录 1.使用方式 2.效果 3.源码修改 1.参考pg_cron的源码在配置文件内增加一个参数 2.修改源码配置校验数字 因数据库入网检测须修改密码级别,在源有的passwordcheck插件上进行 ...
- @PathVariable 处理参数为空的情况
@RequestMapping(value = "/get/{id}/{userId}", method = RequestMethod.GET) public Result ge ...
- 分布式主键解决方案之--Snowflake雪花算法
0--前言 对于分布式系统环境,主键ID的设计很关键,什么自增intID那些是绝对不用的,比较早的时候,大部分系统都用UUID/GUID来作为主键,优点是方便又能解决问题,缺点是插入时因为UUID/G ...
- docker-primary
docker-ce docker网址 https://docs.docker.com/docsarchive/ Docker的安装和启动 官方安装文档链接:https://docs.docker.c ...
- 全栈项目|小书架|微信小程序-实现搜索功能
效果图 上图是小程序端实现的搜索功能效果图. 从图中可以看出点击首页搜索按钮即可进入搜索页面. 布局样式是:搜索框 + 热搜内容 + 搜索列表. 搜索框使用 lin-ui 中的 Searchbar组件 ...
- RestTemplate常用的get和post带参数请求
在RestTemplate提供的方法中,有一个参数就是目标URL,参数是跟在后面的一个数量可变参数,但是在这里就有个问题,这个方法怎么知道我传的参数值是对应在目标接口的哪个参数的呢: public & ...
- mysql-常用组件之触发器
基本概念 触发器是一种特殊的存储过程,不像存储过程需要显示调用,触发器通过监控表事件(增删改操作)自动触发某条 sql 的执行,可以用于购物车加购后库存减少等场景. 触发器基本操作 1. 创建触发器 ...
- Xcode9 脚本打包报错
Xcode9 脚本编译报错 xcodebuild -exportArchive fails with error Locating signing assets failed X9的exporto ...
- python数学工具(一)
python 数学工具包括: 1.函数的逼近 1.1.回归 1.2.插值 2.凸优化3.积分4.符号数学 本文介绍函数的逼近的回归方法 1.作为基函数的单项式 对函数 的拟合 首先定义函数并且可视化 ...