結果

問題 No.1440 The Quiz Competition
ユーザー chocoruskchocorusk
提出日時 2021-04-01 05:49:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,428 bytes
コンパイル時間 3,297 ms
コンパイル使用メモリ 180,732 KB
最終ジャッジ日時 2025-01-20 04:53:32
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;

int main()
{
    int t; cin>>t;
    while(t--){
        ll n, a, w, k;
        cin>>n>>a>>w>>k;
        if(k<n){
            if(a>=k-1){
                cout<<(a-(k-1))/k<<endl;
            }else if(w>=n-(k-1)){
                cout<<-1<<endl;
            }else{
                cout<<":("<<endl;
            }
            continue;
        }
        if(w==0 && a<n-1){
            cout<<":("<<endl;
            continue;
        }
        using lll=__int128_t;
        ll l=-1e18, r=1e9;
        while(r-l>1){
            ll m=(l+r)/2;
            bool ok=0;
            ll p=w%n, q=w/n;
            if(p==0){
                if(-q*(q+1)/2>=m){
                    if(a>=n-1) ok=1;
                }else{
                    ll s=(m+q*(q+1)/2)+(m+q*(q+1)/2+1)*(n-1);
                    if(a>=s) ok=1;
                }
                if(w!=0){
                    if(-(q+2)*(q+1)/2>=m){
                        ok=1;
                    }else{
                        ll s=(m+(q+2)*(q+1)/2)+max(0ll, m+q*(q+1)/2+1)*(n-2)+max(0ll, m+q*(q-1)/2+1);
                        if(a>=s) ok=1;
                    }
                }
            }else{
                if(-(q+2)*(q+1)/2>=m){
                    ok=1;
                }else{
                    ll s=(m+(q+2)*(q+1)/2)+max(0ll, m+(q+2)*(q+1)/2+1)*(p-1)+max(0ll, m+q*(q+1)/2+1)*(n-p);
                    if(a>=s) ok=1;
                }
                if(p!=1){
                    if(-(q+2)*(q+3)/2>=m){
                        ok=1;
                    }else{
                        ll s=(m+(q+2)*(q+3)/2)+max(0ll, m+(q+2)*(q+1)/2+1)*(p-1)+max(0ll, m+q*(q+1)/2+1)*(n-p);
                        if(a>=s) ok=1;
                    }
                }
            }
            if(ok) l=m;
            else r=m;
        }
        cout<<l<<endl;
    }
    return 0;
}
0