Golden chain

Golden chain
(sgu178)

Time Limit:500MS    Memory Limit:4096KB    64bit IO Format:%I64d & %I64u

Description

Peter arrived to the city and stayed in the hotel. He has no money but few day's later (maximum N days) he will receive a lot of money. And so he has a golden chain with N links. Peter must pay one link everyday, but he can pay more and take change by some of his links paid before.
When he receives money he will get his chain out of pawn. Peter likes his chain and he wants to break minimal number of links. Help him!
For example, if N=5, he must break one link (the second). After it his chain will consist of 3 parts with lengths 1, 1 and 3.
At the first day he will pay one link, at the second - one link too. Now he have one part consists of three connected links. At the third day he will pay 3-linked part and take two separated links (as change) back. On the fourth and fifth day he will pay one link.
You may assume that Peter can choose the way to get change.

Input

Input contains one integer number N (1<=N<=10^16).

Output

In output file you must write one integer number (number of the broken links).

Sample Input


Input
9

Output
2

题目大意:
你要在旅馆住n天,你正好有一条由n个珠子连成的长链,你每天必须支付至少一个珠子,或者支付一条珠子连成的长链(这样你可以换回之前支付总和的比这条长链短几条的链子)。你想要把长链尽量小的打断。求至少要打断几次。

思路:
考虑有n个珠子。
1.n是偶数,第一次打断在(n - 2) / 2的位置最合适。
2.n是奇数,第一次打断在(n - 2) / 2的位置最合适。
综上,第一次打断在(n - 2)/ 2的位置最合适。
然后:如果(n - 2) / 2 - 1的珠子可以支付(n - 2) / 2 - 1天,打断结束。否则继续。

第k次打断,由于在第一次打断的时候我们有了k颗自由的珠子。在(n - 2 - k) / 2的位置打断最合适。
很明显的是n次打断所产生的k颗珠子可以用在前k次支付。这样我们判断是否可以支付就可以用k + 1 >= n来判断。

#include <stdio.h>

long long n, k;

int main()
{
    scanf("%lld", &n);

    while (n - k > 1)
    {
        n = (n - k - 2) >> 1;
        k++;
    }

    printf("%d\n", k);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值