1. 程式人生 > >A. Packets(數論小知識,1, 2, 4, .... , 2^n可以組成2^(n+1)

A. Packets(數論小知識,1, 2, 4, .... , 2^n可以組成2^(n+1)

A. Packets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have n
coins, each of the same value of 1

.

Distribute them into packets such that any amount x
(1≤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 x
, however it may be reused for the formation of other x

’s.

Find the minimum number of packets in such a distribution.
Input

The only line contains a single integer n
(1≤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 1
, 2 and 3 coins can be made to get any amount x (1≤x≤6

).

To get 1

use the packet with 1
coin.
To get 2
use the packet with 2
coins.
To get 3
use the packet with 3
coins.
To get 4
use packets with 1 and 3
coins.
To get 5
use packets with 2 and 3
coins
To get 6

use all packets. 

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

題目大意

用最少N可以組成1~X所有數, 輸入一個X求N。
這裡使用了一個數論小知識,1,2,4,….,2^n可以組成2^(n+1) - 1所有數。
所以這道題,可以先用1,2,4,這些2^n數來選,然後剩下用一個數就可以填充了。

程式碼

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    int now = 0;
    int ans = 0;
    scanf("%d", &n);
    for(int i = 1; i < n; i*=2) {
        now += i;
        ans++;
    }
    if(now < n) ans++;
    cout << ans << endl;
    return 0;
}