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. Day9 - Python基础9 socket基础、粘包

    本节内容: 1.socket的介绍 2.基于tcp的socket 3.基于tcp的问题分析 4.基于udp的socket 5.基于udp的问题分析 6.基于udp的ntp服务 7.基于tcp的远程执行 ...

  2. Mysql中事务ACID实现原理

    引言 照例,我们先来一个场景~ 面试官:"知道事务的四大特性么?"你:"懂,ACID嘛,原子性(Atomicity).一致性(Consistency).隔离性(Isola ...

  3. UOJ #450. 【集训队作业2018】复读机

    前置知识单位根反演自己去浅谈单位根反演看(此外可能需要一定的生成函数的姿势) 首先一看\(d\)这么小,那我们来分类讨论一下吧 当\(d=1\)时,显然答案就是\(k^n\) 当\(d=2\)时,如果 ...

  4. 反射(hasattr和getattr和setattr和delattr)

    目录 一.反射在类中的使用 1.1 应用 二.反射在模块中的使用 2.1 前言 2.2 反射机制 2.2.1 getattr() 2.2.2 hasattr(object, name) 2.2.3 s ...

  5. 重载&重写的区别

    重载(Overload) 1.重载(Overload)是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 2.重载(Overload)是一个类中多态性的一种 ...

  6. Delphi 10.2 Tokyo新增JSON类学习——TJsonSerializer

    Delphi 10.3.2 for windows 7 编译通过,源码下载地址: Tokyo 10.2新增类,效率更高更快 TJsonSerializer 需要引用单元:System.JSON.Ser ...

  7. Mysql优化之Explain查询计划查看

    我们经常说到mysql优化,优化中一种常见的方式就是对于经常查询的字段创建索引.那么mysql中有哪些索引类型呢? 一.索引分类1.普通索引:即一个索引只包含单个列,一个表可以有多个单列索引 2.唯一 ...

  8. 【UOJ#242】【UR#16】破坏蛋糕(计算几何)

    [UOJ#242][UR#16]破坏蛋糕(计算几何) 题面 UOJ 题解 为了方便,我们假定最后一条直线是从上往下穿过来的,比如说把它当成坐标系的\(y\)轴. 于是我们可以处理出所有交点,然后把它们 ...

  9. 【linux】linux命令--uptime查看机器存活多久和平均负载 解读平均负载含义

    一.uptime命令,查看机器存活时间和平均负载 键入命令: uptime 该结果和 top命令查看结果最上面一行的 是一样的显示. 返回数据介绍: #当前服务器时间: 19:56:44 #当前服务器 ...

  10. oracle学习笔记(十六) PL/SQL 异常和goto语句

    PL/SQL 异常和goto语句 异常 预定义异常 oracle常见预定义异常: 错误号 异常错误信息名称 说明 ORA-0001 DUP_VAL_ON_INDEX 试图破坏一个唯一性限制 ORA-0 ...