結果

問題 No.3318 客に卵をかける
コンテスト
ユーザー Aeren
提出日時 2025-11-01 05:10:11
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 3,382 bytes
コンパイル時間 3,538 ms
コンパイル使用メモリ 281,876 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-11-01 05:10:16
合計ジャッジ時間 4,292 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2,bmi2,popcnt,lzcnt")
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
using namespace numbers;
#ifdef LOCAL
	#include "Debug.h"
#else
	#define debug_endl() 42
	#define debug(...) 42
	#define debug2(...) 42
	#define debug_bin(...) 42
#endif
// Returns the largest integer k with x >= k * y
template<class T, class U> T floor_div(T x, U y){
	assert(y > 0);
	return x / y - (x % y < 0);
}
// Returns the smallest integer k with x <= k * y
template<class T, class U> T ceil_div(T x, U y){
	assert(y > 0);
	return x / y + (x % y > 0);
}
template<class T> T &ctmin(T &x){ return x; }
template<class T, class Head, class ...Tail> T &ctmin(T &x, const Head &h, const Tail &... t){ return ctmin(x = min<T>(x, h), t...); }
template<class T> T &ctmax(T &x){ return x; }
template<class T, class Head, class ...Tail> T &ctmax(T &x, const Head &h, const Tail &... t){ return ctmax(x = max<T>(x, h), t...); }

// Assumes there exists p, q in [low, high) such that
// f(i) > f(i+1) if i<p,
// f(i) = f(i+1) if p<=i<q
// f(i) < f(i+1) if q<=i
// Returns a point in [p,q]
// O(log(high - low))
// Minimizes the # of function calls
template<class T, class Compare = less<>>
T discrete_golden_section_search(T low, T high, auto f, Compare cmp = less{}){
	assert(low < high);
	if(high - low >= 10){
		double phi = (sqrt(5) - 1) / 2;
		T mid1 = high - (high - low) * phi, mid2 = low + (high - low) * phi;
		auto val1 = f(mid1), val2 = f(mid2);
		while(high - low >= 10){
			if(cmp(val1, val2)){
				high = mid2, mid2 = mid1, val2 = val1;
				mid1 = high - (high - low) * phi, val1 = f(mid1);
			}
			else{
				low = mid1, mid1 = mid2, val1 = val2;
				mid2 = low + (high - low) * phi, val2 = f(mid2);
			}
		}
	}
	T res = low;
	auto val = f(res);
	for(auto x = low + 1; x < high; ++ x) if(auto xval = f(x); cmp(xval, val)) res = x, val = xval;
	return res;
}

// Returns x + x * b + ... + x * b^(e-1)
// O(log(e))
template<class T, class U, class V>
T binary_geometric_sum(T x, U b, V e){
	assert(e >= 0);
	T res{}; // This should be the additive identity
	for(; e; e >>= 1){
		if(e & 1){
			res = x + res;
			x *= b;
		}
		x += x * b;
		b *= b;
	}
	return res;
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	cout << fixed << setprecision(15);
	auto solve_testcase = [&](auto testcase_id)->int{
		long long n;
		int x, y, p, q, r;
		cin >> n >> x >> y >> p >> q >> r;
		auto solve_training = [&](long long nt)->long double{
			long long nr = n - nt;
			long double skill = x + 1.0L * nt * y;
			long long th = *ranges::partition_point(views::iota(1LL, nr), [&](long long day){
				return binary_geometric_sum(1.0L * q, 1.0L * (skill - 1) / skill, day) < r;
			});
			return p * nt + binary_geometric_sum(1.0L * q, 1.0L * (skill - 1) / skill, th) + (q - 1.0L * r / skill) * (nr - th);
		};
		long long nt = discrete_golden_section_search(0LL, n, solve_training, greater{});
		cout << solve_training(nt) << "\n";
		return 0;
	};
	int testcase_count;
	cin >> testcase_count;
	for(auto testcase_id = 0; testcase_id < testcase_count; ++ testcase_id){
		solve_testcase(testcase_id);
	}
	return 0;
}

/*
m days training
n-m days regular work

skill = x + nt * y
base wage = nt * p
wage: dp[rem] = q + 1/skill * max(0, dp[rem - 1] - r) + (skill - 1)/skill * dp[rem - 1]
*/
0