博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1037A - Packets(二进制想法)
阅读量:7090 次
发布时间:2019-06-28

本文共 1865 字,大约阅读时间需要 6 分钟。

A. Packets

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have nn coins, each of the same value of 11.

Distribute them into packets such that any amount xx (1≤x≤n1≤x≤n) can be formed using some (possibly one or all) number of these packets.

Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single xx, however it may be reused for the formation of other xx's.

Find the minimum number of packets in such a distribution.

Input

The only line contains a single integer nn (1≤n≤1091≤n≤109) — the number of coins you have.

Output

Output a single integer — the minimum possible number of packets, satisfying the condition above.

Examples

input

Copy

6

output

Copy

3

input

Copy

2

output

Copy

2

Note

In the first example, three packets with 11, 22 and 33 coins can be made to get any amount xx (1≤x≤61≤x≤6).

  • To get 11 use the packet with 11 coin.
  • To get 22 use the packet with 22 coins.
  • To get 33 use the packet with 33 coins.
  • To get 44 use packets with 11 and 33 coins.
  • To get 55 use packets with 22 and 33 coins
  • To get 66 use all packets.

In the second example, two packets with 11 and 11 coins can be made to get any amount xx (1≤x≤21≤x≤2).

题意:给出一个数n,问最少多少个数可以组成从1到n的所有数。

反思:首先反思一下自己吧,最近学习的态度确实是不够端正的,状态也不是很好,都是致命的问题,但是我却一直没有注意。导致这场第一个题都没有A。最近要积极调整状态吧。

思路:可以扩展想到二进制的想法。那么就是从2^0开始到2^x使得期间的和大于给定的n即可

可行性分析:

不难发现必然是等比数列表示的数最多,而等比数列表现数的数量则为(k-1)logk(n)其中k为等比数列的公倍数,n为数列的长度,然后,证明如下。

AC代码:

#include
#include
#include
using namespace std;typedef long long LL;int main(){ LL n; scanf("%lld",&n); LL l=1,r=32; LL ans; while(l<=r) { LL mid=(l+r)/2; if(pow(2,mid)-1>=n) { ans=mid; r=mid-1; } else l=mid+1; } cout<
<

 

转载于:https://www.cnblogs.com/fly-white/p/10092725.html

你可能感兴趣的文章
Winform 进程之间通讯的几种方法
查看>>
c++中冒号(:)和双冒号(::)的用法
查看>>
dubbo工作原理
查看>>
驱动开发利器Microsoft Windows Driver Kit 7.1.0下载
查看>>
maven_项目的依赖、聚合、继承
查看>>
一个C++类的注释:
查看>>
Winsock IO模型之select模型
查看>>
开发规范
查看>>
PHP json_decode object时报错Cannot use object of type stdClass as array
查看>>
hibernate一对一外键双向关联
查看>>
SharePoint 2013 同步FBA认证用户
查看>>
二叉树的遍历实现
查看>>
Sublimetext 3 经常使用插件
查看>>
flutter安装开发环境-问题记录
查看>>
第十四课时: 登录/登出以及JWT认证
查看>>
渲染机制/页面性能/错误监控
查看>>
Dom中高big 事件总结(持续更新中)
查看>>
Immutable.js 源码解析 --List 类型
查看>>
【修真院“善良”系列之十六】代码结构中Dao,Service,Controller,Util,Model是什么意思,为什么划分...
查看>>
js数据结构-栈
查看>>