跳到正文

闲话

11111

zzq3约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年1月3日
更新
2026年2月4日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

是谁场切T3

可能因为我暑假整天刷二分的原因吧(

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int T, n, m, k, ans;
bool check(int x) {
    if(n <= x) return 1;
    int t = n / x;
    if(t > m) return 0;
    if(t > 1) {
        int low = n*(t-1)/2;
        if (low > k) return 0;
    }
    double c = (double)t*n - (double)x*t*(t+1) / 2.0;
    if(c > (double)k + 1e-9) return 0;
    if(c < (double)k - 1e-9) return 1;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while (T--) {
        ans = 0;
        bool flag = 0;
        cin >> n >> m >> k;
        if(m == 1){
            cout << (n == 1?0:-1) << '\n';continue;
        }
        if(n == 1){
            cout << 0 << '\n';continue;
        }
        if(n > k){
            int t = (n - k - 1) / k;
            ans += t;
            n -= t * k;
        }
        while(n > 1){
            int l = (n + m - 1) / m,r = n, res = n;
		    while(l <= r) {
		        int mid = (l + r) / 2;
		        if(check(mid)) {
		            res = mid;
		            r = mid - 1;
		        }
		        else l = mid + 1;
		    }
            if(res >= n) {
                flag = 1;
                cout << -1 << '\n';
                break;
            }
            n = res; ans++;
        }
        if(!flag) cout << ans << '\n';
    }
    return 0;
}
原文