本文主要是介绍Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目连接:
http://codeforces.com/contest/785/problem/C
Description
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Sample Input
5 2
Sample Output
4
题意:有一个最多装有N个粮食的粮仓,每晚增加M的粮食,有一个最多装有n个粮食的粮仓,每天晚上会增加m的粮食,但是最多不超过n,然后第i天早上都会被麻雀吃掉i个粮食。问第几天,这个粮仓会被吃空。(1 ≤ n, m ≤ 1018)。
题解:首先可以找到K的上界和下界,如果M>=N,那么一定是在第N天的时候能吃光,否则晚上都会补满粮仓。在第1-M天是不可能吃光的,因为你吃掉,晚上就是补M个,所以就可以找到一个上界和一个下界,然后二分天数即可。对于每晚补M可以在一开始减去M,然后在最后的天数加上M天即可。否则会爆longlong。
AC代码:
#include<cstdio>
using namespace std;
long long n,m;
int main()
{scanf("%I64d%I64d",&n,&m);if(m>n)printf("%I64d\n",n);else{n-=m;long long l = 0;long long r = 2e9;long long ans;while(l<r){long long mid = (l+r)/2;if(mid*(mid+1)/2>=n){r = mid;}else{l = mid + 1;}ans = r;}long long k = ans+m;printf("%I64d\n",k);}return 0;
}
这篇关于Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!