Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs 概率DP
D. Jon and Orbs
链接:
http://codeforces.com/contest/768/problem/D
题解:
设dp[i][j]为第i天时已经产生j种龙晶,转移方程为
dp[i][j] += dp[i - 1][j] * j / k;
dp[i][j + 1] += dp[i - 1][j] * (k - j) / k;
代码:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <cstring> 10 #include <iostream> 11 #include <algorithm> 12 #include <functional> 13 using namespace std; 14 #define rep(i,a,n) for (int i=a;i<=n;i++) 15 #define per(i,a,n) for (int i=n;i>=a;i--) 16 #define pb push_back 17 #define mp make_pair 18 #define all(x) (x).begin(),(x).end() 19 #define fi first 20 #define se second 21 #define SZ(x) ((int)(x).size()) 22 typedef vector<int> VI; 23 typedef long long ll; 24 typedef pair<int, int> PII; 25 const ll mod = 1000000007; 26 const double eps = 1e-7; 27 // head 28 29 double dp[10005][1005]; 30 int main() 31 { 32 int k, q; 33 scanf("%d%d", &k, &q); 34 dp[0][0] = 1.0; 35 rep(i, 1, 10000) rep(j, 0, k) { 36 dp[i][j] += dp[i - 1][j] * j / k; 37 dp[i][j + 1] += dp[i - 1][j] * (k - j) / k; 38 } 39 rep(i, 1, q) { 40 int p; 41 scanf("%d", &p); 42 rep(j, 0, 10000) 43 if (dp[j][k] > (p - eps) / 2000){ 44 printf("%d\n", j); 45 break; 46 } 47 } 48 return 0; 49 }