\(设最后的答案为t,那么有\)

\[2^{x_1}+2^{x_2}+...2^{x_t}+tp=n
\]

\(那我们完全可以枚举这个t,判断n-tp(我们下面记为z)能刚好被t个二进制表示\)

\(首先,z如果小于t,那一定无法表示,因为每一个二进制最小是2^0=1,t个二进制最小是t\)

\(然后,我们数一下z的二进制1的个数,假如t不够的话也不行。\)

\(为什么?因为二进制高位的1可以由低位的1补齐,所以t大了没事,但不能比z的二进制1个数少.\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cha(ll s)
{
ll ans=0;
while(s)
{
if(s&1) ans++;
s>>=1;
}
return ans;
}
ll ans=1e18;
int main()
{
ll n,p;
cin>>n>>p;
for(long long i=1;;i++)
{
ll z=n-p*i;
//用i个2进制数凑成z可以吗?
//只要当z>=i,且z的二进制1位数不多于i
if(z>=i&&cha(z)<=i)
{
ans=min(ans,i);
break;
}
if(z<0) break;//继续下去也是小于0
}
if(ans==1e18) cout<<-1;
else cout<<ans;
}

C. p-binary(二进制暴力)的更多相关文章

  1. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  2. Linux学习笔记:ftp中binary二进制与ascii传输模式的区别

    在使用ftp传输文件时,常添加上一句: binary  -- 使用二进制模式传输文件 遂查资料,如下所获. FTP可用多种格式传输文件,通常由系统决定,大多数Linux/UNIX系统只有两种模式:文本 ...

  3. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

  4. leetCode 67.Add Binary (二进制加法) 解题思路和方法

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

  5. lintcode:Add Binary 二进制求和

    题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...

  6. lintcode :Count 1 in Binary 二进制中有多少个1

    题目: 二进制中有多少个1 49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111 ...

  7. hdu 3711 Binary Number(暴力 模拟)

    Problem Description For non-negative integers x and y, f(x, y) , )=,f(, )=, f(, )=. Now given sets o ...

  8. 【LeetCode每天一题】Add Binary(二进制加法)

    Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...

  9. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  10. [Leetcode] add binary 二进制加法

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

随机推荐

  1. Problem F Free Weights

    二分答案. 思路:对于二分给定的mid,即当前允许移动的最大重量,我们可以把小于改重量的标记一下,然后把没有标记的按照顺序放到另一个数组,然后判断是否满足两两相同. #include<bits/ ...

  2. Co-prime 杭电4135

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  3. 今天开始让我们一起来学JavaScript吧!(今天先扯先别的)

    1.为什么要学习JavaScript? 首先它是web开发人员必须学习的3门语言之一: ①HTML定义了网页的内容 ②CSS描述了网页的布局: ③JavaScript网页的行为 首先JavaScrip ...

  4. 详解 迭代器 —— Iterator接口、 ListIterator接口 与 并发修改异常

    (请关注 本人"Collection集合"博文--<详解 Collection集合>) Iterator接口(迭代器): 概述: 对 collection 进行迭代的迭 ...

  5. <algorithm>中常用函数

    先说一下STL操作的区间是 [a, b),左边是闭区间,右边是开区间,这是STL的特性,所以<algorithm>里面的函数操作的区间也都是 [a, b). 先声明一下, sort()函数 ...

  6. strpos的坑

    $a = 'abcd'; $c = 'a'; echo strpos($a,$c)!==false ? '原来是兄弟' : '非我族类,砍ta';

  7. redis的5种数据类型

    卸载服务:redis-server --service-uninstall 开启服务:redis-server --service-start 停止服务:redis-server --service- ...

  8. VHD VHDX 区别

    A Virtual hard disk is saved either with VHD or VHDX file extension. VHD is the older while VHDX is ...

  9. QFileDialog::getOpenFileName() hangs

    https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-d ...

  10. Codeforces Round #628 (Div. 2) 题解

    人闲桂花落,夜静春山空. 月出惊山鸟,时鸣春涧中.--王维 A. EhAb AnD gCd You are given a positive integer x. Find any such 2 po ...