結果

問題 No.1440 The Quiz Competition
ユーザー KKT89KKT89
提出日時 2021-03-26 23:08:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,297 bytes
コンパイル時間 2,416 ms
コンパイル使用メモリ 202,084 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-19 09:54:19
合計ジャッジ時間 3,290 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int q; cin >> q;
	while(q--){
		ll n,a,w,k; cin >> n >> a >> w >> k;
		if(k==1){ // 多分OK
			if(a)printf("%lld\n",a);
			else{
				if(w<n-1){
					printf(":(\n");
				}
				else{
					printf("0\n");
				}
			}
		}
		else if(n==k){ 
			if(w==0){ // ここも多分OK
				if(a<n-1){
					printf(":(\n");
				}
				else{
					printf("0\n");
				}
			}
			else{
				ll L=-1,R=w; // 最下位が不正解する回数
				while(R-L>1){
					ll mid=(L+R)/2;
					ll tmp=(w-mid)/(n-1);
					ll m=tmp*(tmp+1)/2*(n-1);
					ll u=w-tmp-tmp*(n-1);
					ll uu=(n-1)-u;
					ll aa=((mid*(mid+1)/2-1)>=m+(tmp+1)?0:(mid*(mid+1)/2-1)-m-(tmp+1));
					aa*=u;
					ll aaa=((mid*(mid+1)/2-1)>=m?0:(mid*(mid+1)/2-1)-m);
					aaa*=uu;
					if(aa+aaa<=a){
						R=mid;
					}
					else{
						L=mid;
					}
				}
				ll pi=(w+n-1)/n;
				L=max(R,pi);
				ll ans=-1e18;
				auto calc=[&](ll val,ll rem)->ll{
					ll b=(w-val)/(n-1); ll bb=b+1;
					ll cc=w-val-b*(n-1); ll c=n-1-cc;
					b=b*(b+1)/2,bb=bb*(bb+1)/2;
					val=val*(val+1)/2;
					ll res=-val;
					ll u=min(max(0LL,val-bb-1),rem);
					rem-=u;
					val+=u; res+=u;
					u=min(max(0LL,val-b-1),rem/(cc+1));
					res+=u; val+=u;
					rem-=u*(cc+1);
					res+=rem/n;
					return res;
				};
				R=w;
				for(int i=0;i<50 and L+100<R;i++){
					ll nl=(2*L+R)/3,nr=(L+2*R)/3;
					if(calc(nl,a)<calc(nr,a)){
						L=nl;
					}
					else{
						R=nr;
					}
				}
				for(int i=L;i<=L+100 and i<=w;i++){
					ans=max(ans,calc(i,a));
				}
				for(int i=R;i>=L and i>=R-100;i--){
					ans=max(ans,calc(i,a));
				}
				printf("%lld\n",ans);
			}
		}
		else{
			if(a==0){ // ここもOK
				if(w>=1+(n-k)){
					printf("-1\n");
				}
				else{
					printf(":(\n");
				}
			}
			else if(k-1<=a){ // ここも多分OKなはず…
				ll L=0,R=2e9;
				while(R-L>1){
					ll mid=(L+R)/2;
					if((k-1)*(mid+1)+mid<=a)L=mid;
					else R=mid;
				}
				printf("%lld\n",L);
			}
			else{
				if(w<n-k+1){
					printf(":(\n");
				}
				else{
					printf("-1\n");
				}
			}
		}
	}
}
0