1037A - Packets(二进制想法)

1037A - Packets

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<cstdio>
#include<cmath>
#include<iostream>
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<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/baiyifeifei/article/details/82353244