codeforce 785 C.Anton and Fairy Tale(二分)
来源:互联网 发布:淘宝电动清扫车多少钱 编辑:程序博客网 时间:2024/05/20 16:41
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 ofn 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 dayi 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!
The only line of the input contains two integers n andm (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
5 2
4
8 1
5
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.
#include<bits/stdc++.h>using namespace std;typedef long long ll;int main(){ ll n,m; cin>>n>>m; if(n<=m) { cout<<n<<endl; return 0; } ll l,r; l=1; r=INT_MAX; while(l<r) { ll mid=(l+r)/2; if(mid*(mid+1)/2>=n-m) r=mid; else l=mid+1; } cout<<l+m<<endl; return 0; }
- codeforce 785 C.Anton and Fairy Tale(二分)
- codeforce Anton and Fairy Tale
- Codeforecs 785C Anton and Fairy Tale 数学+二分
- codeforces 785C. Anton and Fairy Tale(二分)
- Codeforces 785C Anton and Fairy Tale 找规律 二分
- CodeForces 785C Anton and Fairy Tale【二分答案+思维】
- codeforces 785 C. Anton and Fairy Tale (数学 二分搜索)
- Codeforces 785C Anton and Fairy Tale (规律+二分查找)
- 【codeforces 785C】Anton and Fairy Tale
- Codeforces 785C-Anton and Fairy Tale
- CodeForces #785 c Anton and Fairy Tale
- Codeforces 785C Anton and Fairy Tale
- C. Anton and Fairy Tale
- C. Anton and Fairy Tale
- C. Anton and Fairy Tale
- C. Anton and Fairy Tale
- C. Anton and Fairy Tale----思维+二分答案
- Codeforces #404 (Div. 2) C. Anton and Fairy Tale (二分
- 神秘的包含块
- 设计模式--简单工厂模式
- 从零开始学习OpenCL开发(一)架构
- requests库使用总结
- Intellij IDEA配置tomcat热部署
- codeforce 785 C.Anton and Fairy Tale(二分)
- 二分查找法
- 1262: 魔法宝石 (“玲珑杯”河南工业大学ACM程序设计大赛)
- 有3行文字,每行有80个字符,分别统计出其中大小写字母,数字,空格以及其他字符个数
- mac和linux系统配置git tab自动补全
- 大话设计模式读书笔记(十七) 桥接模式
- 画出使用分类器得到的决策边界
- 面向接口编程详解
- [BZOJ4551][Tjoi2016&Heoi2016]树(dfs序+线段树)