[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 = ...
随机推荐
- [开源] 基于Layui组件封装的后台模版,HG-Layui-UI通用后台管理框架V1.0版
HG框架简介 HG-Layui-UI框架,是基于layui最新版UI搭建的一套通用后台管理框架,借鉴了市面上各大主流框架风格,采用iframe标签页实现,保留了传统开发模式的简单实用性. 为快速开发减 ...
- ajax加载引起瀑布流布局堆叠
jQuery 瀑布流使用ajax加载数据库图片url ,ajax每次请求到的数据不变时,瀑布流效果没问题. 但当请求到的数据变化时,瀑布流图片格式会重叠 或者相隔很远等等的格式问题,这是由于图片加载是 ...
- SwiftyUserDefaults-封装系统本地化的框架推荐
// // ViewController.swift // Test4SwiftyUserDefaults // Copyright © 2017年. All rights reserved. // ...
- 纯CSS与HTML实现垂直时间轴
原创YouTube HTML源码 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 使用Docker测试静态网站
参考书籍 :第一本docker书[澳]James Turnbull 1.Sample网站的初始Dockerfile 文件目录如下: Dockerfile文件代码: 安装nginx 在容器中创建一个目 ...
- 大数据之Linux基础
回顾这一个多月以来闭关学大数据的一些相关重要知识,就当复习,顺便以备以后查看 Linux学习第一步自然是安装Linux. 关于Linux 首先介绍下Linux,Linux系统很多程序员开发者其实都耳熟 ...
- tensorflow mnist模块详解
tensorflow的官方文档是以mnist数据集为例子开始的.文档本身没有介绍tensorflow.contrib.learn.python.learn.datasets.mnist模块.要想用te ...
- PC端、移动端页面适配方案
前言 页面自适应PC端.移动端大体上可以分为两种: 1.在同一个页面进行自适应布局,通常使用CSS3 @media 媒体查询器实现 2.两套页面,在后端进行统一适配,根据不同的浏览器UA返回对应的页面 ...
- Django ContentType 的使用
引入 一切优化,最终都是关于需求的优化.本文介绍需求确定之后的数据库表结构设计优化. 程序员应该都知道,编程是数据结构和算法的结合.所谓数据就是用户需要访问和操作的资源,比如购物类App里面的商品,图 ...
- 【Java Web开发学习】远程方法调用RMI
Java RMI 远程方法调用Remote Method Invocation 转载:http://www.cnblogs.com/yangchongxing/p/9078061.html 1.创建远 ...