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.
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 ≤ 1018, 2 ≤ 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
4 3
2
5 5
1
8 4
-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;
}