P1880--"UFO" water pipe
Time limit: 1000MS memory limit: 131072KB Pass/Submit number: 32/100
Status: Tags: math problem-Combinatorial math no no no
Description
Lzl: I recently bought a set of 2147483647 of the house, but the yellow real estate developer SXBK, unexpectedly only installed a pipe for me.
UFO: Isn't it enough?
Lzl: Given my weight-gain plan, I need n kitchens, with at least 1 plumbing in each kitchen.
Ufo:... Don't worry, you see I brought you a magical water pipe converter that can make the water pipe 1 x.
UFOs have 1 to 2 to 1 variable k converters each, ask at least a few converters to meet the Lzl fertilizer needs.
It's a 1-to-4 converter.
Input Format
Two integers n,k
Output Format
The minimum number of converters required to meet LZL requirements, output that cannot be met-1
Sample Input
4 3
Sample Output
2
Hint
For data 1≤n≤10 of 30%, 2≤k≤10
For data 1≤n≤10^18 of 100%, 2≤k≤10^9
UFO: Did you put a hose in the yellow property?
Lzl: No. All right, big belly can tolerate the world hiccup ~ ~ ~
Solving
According to the greedy thought, we must choose the converter from large to small to get the optimal solution. It is not difficult to find, in this selection process, each increase in the number of water pipes constitute arithmetic progression, then you can use the arithmetic progression summation formula.
Method one: In [1,k] The two-part answer, with the arithmetic progression summation formula to determine the best solution.
Method two: By arithmetic progression summation formula to get one yuan two times equation, solution equation.
Code
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 Long LongN, K;6 Long LongL, R;7 8 DoubleCalc (Long LongKLong LongN) {9n = n-1;Ten return(Double) (K-0.5) * N-0.5* N * n +1; One } A - intMain () { -scanf"%lld%lld", &n, &K); the if(Calc (k, k) <N) { -printf"-1\n"); - return 0; - } +L =1; -R =K; + while(L <=R) { A Long LongMid = (L + R) >>1; at DoubleSum =Calc (K, Mid); - if(Sum < N) L = Mid +1; - ElseR = Mid-1; - } -printf"%lld\n"+ K-1); -}
FZYZOJ-1880 "UFO" water pipe