鏈接:https://ac.nowcoder.com/acm/problem/21860
來源:牛客網
小j開始打工,準備賺錢買煤氣竈。
第一天,小j的工資爲n元,之後每天他的工資都比前一天多d元。
已知煤氣竈需要m元,求小j最少工作幾天才能買到煤氣竈。
輸入描述:
四個整數 n,m,d,x
分別表示小j第一天的工資,煤氣竈的價格,工資每天的增長量,答案不超過x
輸出描述:
一個數表示答案
示例1
輸入
10 100 20 100
輸出
4
說明
10+30+50+70>=100
備註:
0≤n,d≤10^9^ ,n+d>0
1<m< 10^18^
1<x<10^9^
Sk = (2n - d + kd) * k /2
由於數據太大,所以用二分查找
Code:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll n,m,d,x;
cin>>n>>m>>d>>x;
ll l=1,r=x,mid,a,b;
while(l<=r)
{
mid=(l+r)/2;
a=(2*n-d+mid*d)*mid;
b=(2*n-d+(mid-1)*d)*(mid-1);
if(b<2*m&&a>=2*m)
{
printf("%d\n",mid);
break;
}
else if(a<2*m)
l=mid+1;
else if(b>=2*m)
r=mid-1;
}
return 0;
}