Pipeline

Pipeline

Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.

A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.

 The figure shows a 4-output splitter

Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.

Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.

Input

The first line contains two space-separated integers n and k (1 ≤ n ≤ 10182 ≤ k ≤ 109).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.

Example

Input
4 3
Output
2
Input
5 5
Output
1
Input
8 4
Output
-1

题意:给你k-1个分流器,就是接在一个水管上面有很多出水口的那种,分流器的出口分别为2,3.......k,问你最少用多少个分流器能到达n个地点,二分分流器的个数,假设分流器的个数为x,那就肯定是最后的x个,k的值为10的9次方,肯定不能循环求和啦,恰好给出的k个分流器的出口个数是等差数列,所以我们就能用等差数列求和来求解!!
在用等差数列求解的时候没有理清楚思路,一直纳闷为什么不是(temp+k)*x/2,因为从temp到k一共x个的和,然后终于发现我好像对题目存在某种误解,居然没脑子的以为就是简单的求2到k中最小数的个数大于等于n,但是题目要求的是只给了一个水源,也就是意味着,我插一个分流器在水源上,接下来的那个只能通过接在上一个分流器上面,这就能很好的解释代码中用等差数列求解的公式了!!!
sum=1+2-1+3-1+4-1.......k-1,因为每加上去一个就减去一个!化简以后就是sum=1+1+2+......+(k-1)
AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<iomanip>
#define N  500005
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define INF 1000000100
template<typename T> inline T max(T a,T b,T c){
    return max(a,max(b,c));
}
template<typename T> inline T min(T a,T b,T c){
    return min(a,min(b,c));
}
template<typename T> inline T max(T a,T b,T c,T d){
    return max(a,max(b,c,d));
}
template<typename T> inline T min(T a,T b,T c,T d){
    return min(a,min(b,c,d));
}
const int  dx[]={0,1,0,-1,0,1,-1,1,-1};
const int  dy[]={0,0,1,0,-1,1,-1,-1,1};
typedef long long ll;
using namespace std;
//coding............................
ll n,k;
int Check(ll x)
{
    ll temp,ans;
    temp=k-x;
    ans=(temp+k-1)*x/2+1;
     return ans>=n;
}
int solve()
{
    if (!Check(k-1))
        return -1;
    if (Check(0))
        return 0;
    ll lb=0,ub=k-1;
    while (ub-lb>1)
    {
        ll mid=(lb+ub)>>1;
        if (Check(mid))
            ub=mid;
        else
            lb=mid;
    }
    return ub;
}
int main()
{
    IOS;
    cin>>n>>k;
    cout << solve() << endl;

    return 0;
}

 



转载于:https://www.cnblogs.com/lisijie/p/7868247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值