标签:write ios ble second const help contains ring string
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:
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
5 2
4
8 1
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
So the answer is 4, because by the end of the fourth day the barn becomes empty.
题解:从第开始到第M天,仓库必定是满的,那么从第 m+1 天开始第 k 天,每天减少的数量分别为 m+1 , m+2 , m+3 , ... ,m+k ;求和就是 2KM+(1+K)K/2
而第 m-1 天到第 k-1 天每天补充后剩余的数量为 n - (m+1)+m , n - (m+2) + m , .... , n-(m+k-1) + m ,补充的数量为 M*(K-1),总和为M*(K-1) + N
那么,2KM+(1+K)K/2 >= M*(K-1) + N , 即 sqrt ( 2*n - 2*m ) +1 ,+1防止带有小数
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <string> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <list> 9 #include <deque> 10 #include <queue> 11 #include <stack> 12 #include <cstdlib> 13 #include <cstdio> 14 #include <cmath> 15 #include <iomanip> 16 #define ull unsigned long long 17 #define ll long long 18 #define pb push_back 19 #define rep(i,start,end) for(int i=start;i<=end;i++) 20 #define per(i,end,start) for(int i=end;i>=start;i--) 21 #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 22 #define lc now<<1 23 #define rc now<<1|1 24 using namespace std; 25 const int mod = 998244353; 26 const int mxn = 1e9 +7; 27 ll _,n,m,t,k,u,v,ans,cnt,ok,lim; 28 /// ll w[mxn] , cost[mxn] , far[mxn] , siz[mxn]; 29 int main() 30 { 31 while(cin>>n>>m) 32 { 33 if(m>=n) cout<<n<<endl; 34 else { 35 ll tmp = sqrt(n*2-m*2) + 1 ; 36 if(tmp*(tmp-1)>=2*n-2*m) tmp--; 37 cout<<tmp+m<<endl; 38 } 39 } 40 }
标签:write ios ble second const help contains ring string
原文地址:https://www.cnblogs.com/Shallow-dream/p/12850841.html