博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 785C Anton and Fairy Tale (规律+二分查找)
阅读量:5138 次
发布时间:2019-06-13

本文共 3975 字,大约阅读时间需要 13 分钟。

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.

数据量过大, 暴力会超时, ;

找规律, 容易发现 当第m 天后 粮仓容量开始减少,  每天递增的数目是 以公差为1的等差数列,  前n项和为 n*(n+1)/2  所以

通俗的讲就是 二分查找 Sn>= n-m  即为结果;

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const double PI=acos(-1); const int INF=0x3f3f3f3f; const double esp=1e-6; const int maxn=1e18+2; const int MOD=1e9+7;#define mem(a,b) memset(a,b,sizeof(a))#define findx(x) lower_bound(b+1,b+1+bn,x)-bll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}int main(){ ll n,m; while(~scanf("%I64d %I64d",&n,&m)) { ll ans; ll mid; ll le=0,ri=maxn; if(n<=m) { ans=n; printf("%I64d\n",ans); continue; } while(le
=n-m) { ri=mid; } else le=mid+1; } printf("%I64d\n",le+m); }}//570441179141911871 511467058318039545

转载于:https://www.cnblogs.com/sizaif/p/9078488.html

你可能感兴趣的文章
gzip
查看>>
转负二进制(个人模版)
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
[LeetCode] Palindrome Number
查看>>
我对于脚本程序的理解——百度轻应用有感
查看>>
SQL更新某列包含XX的所有值
查看>>
网易味央第二座猪场落户江西 面积超过3300亩
查看>>
面试时被问到的问题
查看>>
spring 事务管理
查看>>
VS2008 去掉msvcr90的依赖
查看>>
当前记录已被另一个用户锁定
查看>>
Node.js 连接 MySQL
查看>>
那些年,那些书
查看>>
注解小结
查看>>
java代码编译与C/C++代码编译的区别
查看>>
Bitmap 算法
查看>>
转载 C#文件中GetCommandLineArgs()
查看>>
list control控件的一些操作
查看>>
SNF快速开发平台MVC-EasyQuery-拖拽生成SQL脚本
查看>>