sgu 178 Golden chain

39 篇文章 0 订阅
18 篇文章 0 订阅
178. Golden chain
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard



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 test(s)

Input
9

Output
2

果然我只是适合做这种找规律的题吗?
我们可以假设拆n个可以的最大的链的长度。
那么很容易就可以预处理这些值。

我的代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 1010;
int const base = 10;
typedef long long LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

LL ans[nMax];

void init(){
    for(int i=0;i<=50;i++) {
        LL t=0;
        ans[i]+=i;
        for(t=0;t<=i;t++){
            ans[i]=ans[i]*2+1;
        }
    //    printf("a[%d]=%lld\n",i,ans[i]);
    }
    return ;
}

int main(){
    init();
    LL k;
    scanf("%I64d",&k) ;
    int p=0;
    while(ans[p]<k) p++;
    printf("%d\n",p);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值