题意是敲n个字符,每次敲有p概率崩溃返回上一次的保存状态,保存需要按下x个建。求最小的按键次数的期望。
求出敲n个字母的期望后枚举分成i段,要使得尽量均匀。
#include <bits/stdc++.h>
using namespace std;
#define maxn 111111
#define INF 1e20double dp[maxn], f[maxn];
double p;
int x, n;
double ans;int main () {int t, kase = 0;cin >> t;while (t--) {cin >> n >> p >> x;f[0] = 0;memset (dp, 0, sizeof dp);for (int i = 1; i <= n; i++) {f[i] = (f[i-1]+1.0)/(1.0-p);}ans = INF;for (long long i = 1; i <= n; i++) {long long j = n/i, k = n-j*i;dp[i] = x*i + f[j+1]*k + f[j]*(i-k);ans = min (ans, dp[i]);}printf ("Case #%d: %.6lf\n", ++kase, ans);}return 0;
}
/*
10
10 0.5 0
*/