結果

問題 No.1440 The Quiz Competition
ユーザー KKT89KKT89
提出日時 2021-03-26 22:36:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,170 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 207,628 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-06 16:23:04
合計ジャッジ時間 3,144 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
5,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;
					}
				}
				vector<ll> v(n);
				v[n-1]=-R*(R+1)/2;
				ll tmp=(w-R)/(n-1);
				ll u=w-R-tmp*(n-1);
				for(int i=0;i+1<n;i++){
					v[i]=-tmp*(tmp+1)/2;
				}
				for(int i=0;i<n;i++){
					if(u==0)break;
					v[i]-=(tmp+1);
					u--;
				}
				for(int i=0;i+1<n;i++){
					if(v[i]<=v[n-1]){
						ll ad=v[n-1]-v[i]+1;
						v[i]+=ad;
						a-=ad;
					}
				}
				sort(v.begin(), v.end());
				ll j=1;
				for(int i=1;i<n;i++){
					if(v[0]+1==v[i]){
						j++;
					}
					else{
						ll rem=v[i]-v[0]-1;
						if(j>a)break;
						ll uu=min(rem,a/j);
						v[0]+=uu;
						a-=uu*j; j++;
					}
				}
				if(j==n){
					v[0]+=a/j;
				}
				printf("%lld\n",v[0]);
			}
		}
		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