Codeforces Round #176 (Div. 2)

B - Pipeline

 1 #include <iostream>
 2 using namespace std;
 3 long long n, k;
 4 int main() 
 5 {
 6     cin >> n >> k;
 7     long long l = 0, r = k, mid;
 8     while (l < r) 
 9     {
10         mid = (l + r) / 2;
11         long long s = mid * k - (mid - 1) * (mid + 2) / 2;
12         if (s < n) 
13             l = mid + 1;
14         else
15             r = mid;
16     }
17     mid = (l + r) / 2;
18     if (mid * k - (mid - 1) * (mid + 2) / 2 < n)
19         cout << -1 << endl;
20     else
21         cout << mid << endl;
22     return 0;
23 }

 

posted @ 2015-03-10 16:05  jzwong  阅读(175)  评论(0编辑  收藏  举报