CF1225C p-binary

洛谷评测传送门

题目描述

Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer pp (which may be positive, negative, or zero). To combine their tastes, they invented pp -binary numbers of the form 2^x + p2x+p , where xx is a non-negative integer.

For example, some -9−9 -binary ("minus nine" binary) numbers are: -8−8 (minus eight), 77 and 10151015 ( -8=2^0-9−8=20−9 , 7=2^4-97=24−9 , 1015=2^{10}-91015=210−9 ).

The boys now use pp -binary numbers to represent everything. They now face a problem: given a positive integer nn , what's the smallest number of pp -binary numbers (not necessarily distinct) they need to represent nn as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.

For example, if p=0p=0 we can represent 77 as 2^0 + 2^1 + 2^220+21+22 .

And if p=-9p=−9 we can represent 77 as one number (2^4-9)(24−9) .

Note that negative pp -binary numbers are allowed to be in the sum (see the Notes section for an example).

输入格式

The only line contains two integers nn and pp ( 1 \leq n \leq 10^91≤n≤109 , -1000 \leq p \leq 1000−1000≤p≤1000 ).

输出格式

If it is impossible to represent nn as the sum of any number of pp -binary numbers, print a single integer -1−1 . Otherwise, print the smallest possible number of summands.

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

输入 #4复制

输出 #4复制

输入 #5复制

输出 #5复制

说明/提示

00 -binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0)24=(24+0)+(23+0) .

In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)24=(24+1)+(22+1)+(20+1) .

In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)24=(24−1)+(22−1)+(22−1)+(22−1) . Note that repeated summands are allowed.

In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7)4=(24−7)+(21−7) . Note that the second summand is negative, which is allowed.

In the fifth sample case, no representation is possible.

题解:

对于一个给定的\(n,p\),试求一个最小的\(k\),使得存在:

\[\sum_{i=1}^{k}{(2^{a_i}+p)}
\]

那么对于题意,我们很容易发现,这个数就是多加了\(i\)个\(p\),如果把这\(i\)个\(p\)去掉,那么就显然可以把这个数\(n-i\times p\)进行二进制拆分,拆成很多个二的整数次幂之和,我们要求出这个最小的加数数量。

根据二进制拆分的原则,我们把这个数\(n-i\times p\)用二进制表示,其中有\(1\)对应的第\(i\)位就是\(2^{i-1}\)(根据位运算的性质)。

那么,我们算出来这个数\(n-i\times p\)的二进制表示中1的个数,如果这个个数比\(i\)大,那么就不合法(这是显然的)。并且,如果\(n-i\times p<i\),那么也不合法。

那么我们从小到大枚举,判断一下即可。

如有对lowbit运算不太了解的同学,可参考本蒟蒻的这篇博客:

浅谈lowbit运算

代码:

#include<cstdio>
using namespace std;
int n,p,ans,flag;
int lowbit(int x)
{
int ret=0;
while(x)
{
x-=x&-x;
ret++;
}
return ret;
}
int main()
{
scanf("%d%d",&n,&p);
for(int i=1;;i++)
{
if(n-i*p<i)
break;
if(lowbit(n-i*p)>i)
continue;
ans=i;
flag=1;
break;
}
if(flag)
{
printf("%d",ans);
return 0;
}
else
{
printf("-1");
return 0;
}
}

CF1225C p-binary的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  8. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. 第05组 Beta冲刺(3/4)

    第05组 Beta冲刺(3/4) 队名:天码行空 组长博客连接 作业博客连接 团队燃尽图(共享): GitHub当日代码/文档签入记录展示(共享): 组员情况: 组员1:卢欢(组长) 过去两天完成了哪 ...

  2. [Vue专题] 对比vue-cli2.x和vue-cli3.x的搭建

    简介:深入了解脚手架vue-cli2.x版本与3.x版本构建项目的区别 搭建前提条件: node环境 node是傻瓜式安装的,直接去官网下载安装不断下一步 命令行输入node -v查询版本号,有版本号 ...

  3. Java连载52-单例模式的缺点以及抽象类

    一.单例模式 1.单例模式的缺点:单例模式的类型没有子类,无法被继承. 例如:下面的例子,由于父类的构造方法是私有的,所以子类中的构造方法是无法创建的,因为它是引用父类的构造方法 package co ...

  4. MySQL中使用group_concat遇到的坑

    在使用group_concat的过程中遇到个问题,这里记录一下:在MySQL中有个配置参数group_concat_max_len,它会限制使用group_concat返回的最大字符串长度,默认是10 ...

  5. Windows安装与配置—MongoDB

    1,下载安装 打开下载链接:http://dl.mongodb.org/dl/win32/x86_64,选择后缀是2008plus-ssl-3.6.15.zip的版本,32位和64位通用. 2,安装配 ...

  6. 【shell脚本】nginx启动脚本

    [root@localhost init.d]# cat nginx #!/bin/bash #nx Startup script for the Nginx HTTP Server # it ver ...

  7. JXL工具包对Excle文件操作

    1.简介: XL - JXL(Java Excel API)是一个用来动态读写 Excel 文件的开源框架,利用它可以 在任何支持 Java 的操作系统上动态读写 Excel 文件. 2.开发步骤 1 ...

  8. 【51Nod1769】Clarke and math2(数论,组合数学)

    [51Nod1769]Clarke and math2(数论,组合数学) 题面 51Nod 题解 考虑枚举一个\(i_k\),枚举一个\(i\),怎么计算\(i_k\)对\(i\)的贡献. 把\(\f ...

  9. Docker - 快速入门(一)

    概念 下面这三个概念一开始可能不好理解,等大家跟着博客把例子做完了,再回头来看应该就能理解了. docker image  # docker镜像 镜像就是一个只读的模板.镜像可以用来创建Docker容 ...

  10. 几个高逼格 Linux 命令!

    作者:忧郁巫师 https://dwz.cn/A1FOjLXk 1. sl 命令 你会看到一辆火车从屏幕右边开往左边…… 安装 $ sudo apt-get install sl 运行 $ sl 命令 ...