P10781 【MX-J1-T1】『FLA - III』Spectral 题解
大致思路:
这道题目最直接的方法就是暴力找最大值,显然会超时,接着就是进行打表,我们会发现,这道题的函数,是一个单峰函数,只会在一开始持续上升,在一定的位置持续下降,所以我们只需要找到那个转折点,直接退出即可。
代码实现:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int N = 5e5 + 10;
inline int read()
{
int x = 0, f = 1; char ch = getchar();
while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
int T;
signed main() {
T = read();
while (T--) {
double k, res = 0;
int n;
n = read();
cin >> k;
if(n == 1) cout << fixed << setprecision(1) << k << "\n";
else{
for (int i = 1;i <= n; ++ i)
{
if(k + res / i < res) break;
else res = k + res / i;
}
cout << fixed << setprecision(1) << res << "\n";
}
}
return 0;
}
这样这道题目就完成啦!!!