// #pragma GCC target("avx2,bmi2,popcnt,lzcnt") // #pragma GCC optimize("O3,unroll-loops") #include // #include 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 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 T ceil_div(T x, U y){ assert(y > 0); return x / y + (x % y > 0); } template T &ctmin(T &x){ return x; } template T &ctmin(T &x, const Head &h, const Tail &... t){ return ctmin(x = min(x, h), t...); } template T &ctmax(T &x){ return x; } template T &ctmax(T &x, const Head &h, const Tail &... t){ return ctmax(x = max(x, h), t...); } // Assumes there exists p, q in [low, high) such that // f(i) > f(i+1) if i> 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 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] */