CodeForces #785 c Anton and Fairy Tale

11 篇文章 0 订阅
6 篇文章 0 订阅
C. Anton and Fairy Tale
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
5 2
output
4
input
8 1
output
5
Note

In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:

  • At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
  • At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
  • At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
  • At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
  • At the beginning of the third day two grains are brought. The barn becomes full again.
  • At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
  • At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
  • At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.

So the answer is 4, because by the end of the fourth day the barn becomes empty.


队内训练时出现的一道题,对我来说挺难的,毕竟脑子不够用,后来老大耐心地教我,我也弄明白了。
题意:
先给出一个n和一个m,n表示谷仓地最大容量,m表示每天会添加进来地谷堆数目,然后从第一天开始,每天有比前一天多一只地鸟来吃稻谷,每只鸟吃一单位,第一天有一只鸟,也就是第1 2 3 天稻谷会减少 1 2 3,但是每天都增加 m,但是若加m之后超过了n,也只能有n单位稻谷。问第几天谷仓会为0。
思路:
由于在前m天的时候,稻谷总是会被添加回原来的n,所以我们从第m天开始谷堆里的数量才真正的开始减少,并且第 x - m天减少1,x - m + 1天减少2以此类推。 也就是 第x天的时候会减少的数量是 (x - m)的阶层 (x-m)! 。公式为 (x - m) * (x - m + 1) / 2;
但是要注意,题目中数据开的很大,所以在用这个公式的时候的写成 
(x - m) * 0.5 * (x - m + 1) ; 不然两个数相乘会爆long long,所以先乘0.5。
然后我们就可以推出一个公式(n - m) - (x + 1) * x / 2 <= 0  注:(由于前面的m天里每天来的鸟数已经增加到了m,所以后面x天的鸟数为 (x + m) ,这里的x是指除了前m天以外的天数,所以只要满足 (n - m)的量即可)。列出了公式之后,由于数据量大,我们用二分来找答案。

特别感谢我的老大,教会我这么多。

代码如下:
#include<stdio.h>
#define ll long long
ll n,m;
ll check(ll d){
	// 不能写成 (n - m) - x * (x + 1) * 0.5 <= 0 得先* 0.5 ,不然会爆ll 
	return -d * 0.5 * (d + 1) + n - m <= 0; 
}
int main(){
	ll l,r,mid,ans;
	while(scanf("%lld %lld",&n,&m) != EOF){
		if(n < m){
			printf("%lld\n",n);
		}else{
			l = 0;
			r = 1e18;
			while(l <= r){
				mid = (r + l) / 2;
				if(check(mid)){
					r = mid - 1;
					ans = mid;
				}else{
					l = mid + 1;
				}
			}
			printf("%lld\n",ans + m);
		}	
	}
	return 0;
} 
//公式 (n - m) - (x + 1) * x / 2 <= 0 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值