牛客C - 煤气灶(二分)

通过高中数学知识求得通项公式, 同时一定要注意答案为mid而不是l, 可见二分的情况还是要具体分析讨论

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 1e6+10;
 
LL n, m, d, x;
bool check(LL i)
{
    return i*n+i*(i-1)/2*d >= m;
}
int main()
{
    cin >> n >> m >> d >> x;
    LL l = 0, r = x, ans = r; //切记答案为mid
    while(l <= r){
        LL mid = (l+r)/2;
        if(check(mid)) r = mid-1, ans = mid;
        else l = mid+1;
    }
    cout << ans << endl;
     
    return 0;
}

相关文章:

  • 2021-05-09
  • 2021-10-28
  • 2021-08-22
  • 2021-09-27
  • 2019-08-03
  • 2021-09-10
  • 2021-09-15
  • 2021-10-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-11-17
  • 2022-01-14
  • 2022-12-23
相关资源
相似解决方案