[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 【数论】【二进制】的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】

    [067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...

  2. [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 ...

  3. [LeetCode] Binary Gap 二进制间隙

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  4. 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 ...

  5. LeetCode OJ:Add Binary(二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  6. codeforces gym/100814 humming distance (二进制位数比较)

    Gym - 100814I I. Salem time limit per test 1 second memory limit per test 1024 megabytes input stand ...

  7. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  8. LeetCode 67 Add Binary(二进制相加)(*)

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

  9. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

随机推荐

  1. Airtest介绍与脚本入门

    前言 通过阅读本小节教程,你将了解以下内容: 一个Airtest脚本例子的详细解析 如何在Python脚本中调用Airtest接口 图片语句的参数介绍 Airtest介绍 Airtest是一款基于Py ...

  2. Linux查看系统基本信息、版本信息等

    Linux下如何查看版本信息, 包括位数.版本信息以及CPU内核信息.CPU具体型号 1.uname -a   (Linux查看版本当前操作系统内核信息) 2.cat /proc/version (L ...

  3. java 数组注意细节,例子解析

    1. int x[]; 或int [] x; 此时却无物理的存在数组.需:数组名= new 数组元素类型[size]: a = new int [10]; 2. 不能使用任何未初始化的数组. 默认的初 ...

  4. Python的特有的参数传递(*和**)

    目录 值传递 引用传递 python的传递方式具有两种值传递和引用传递.除此之外,python中还允许包裹方式的参数传递,这未不确定参数个数和参数类型的函数调用提供了基础: 值传递 int.float ...

  5. 【SSL1457】翻币问题

    题面: \[\Large\text{翻币问题}\] \[Time~Limit:1000MS~~Memory~Limit:65536K\] Description 有N个硬币(6<=N<=2 ...

  6. PHP自动发红包代码示例

    <?php header('Content-type:text'); define("TOKEN", "weixin"); $wechatObj = ne ...

  7. 为什么查询出来的数据保存到Arraylist?插入删除数据为啥用LinkedList?

    引言:这是我在回答集合体系时,被问到的一个问题,也是因为没有深入学习所以回答的并不是很好,所以这两天看了一下,以下是我的一些回答与学习方法. 学习方法:我们学习,系统性的学习肯定是比零散的学习更有效的 ...

  8. vue axios 总结篇

    1.npm --save 和 --save-dev 有什么区别 发布到线上的叫生产环境~,在本地开发的时候叫开发环境,--save就是会打包到线上去并且在线上环境能用到的,比如你npm install ...

  9. sublime text3安装中文版插件

    安装插件前需要先安装 package control 扩展包管理器,安装方法为:进入 https://sublime.wbond.net/Package%20Control.sublime-packa ...

  10. 首次自动化测试,使用selenium+scapy

    痛苦而艰难 才写出这一点点,这是个登陆测试 main # -*- coding: utf-8 -*- from selenium import webdriver import login_tst i ...