kuangbin专题二十一:概率&期望
思路:简单概率。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 105; int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { int n; cin >> n; int tot = 0, neg = 0; for(int i = 0; i < n; i++) { int x; cin >> x; if(x < 0) x = -x, neg++; tot += x; } cout << "Case " << t << ": "; if(neg == n) cout << "inf" << endl; else { int y = gcd(tot, n - neg); cout << tot / y << "/" << (n - neg) / y << endl; } } return 0; }
思路:不能重复计算,需要倒序。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 105; int gold[maxn]; double e[maxn]; int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { int n; cin >> n; memset(e, 0, sizeof(e)); for(int i = 0; i < n; i++) cin >> gold[i]; e[n-1] = gold[n-1]; for(int i = n - 2; i >= 0; i--) { e[i] = gold[i]; int len = min(6, n - 1 - i); for(int j = 1; j <= len; j++) e[i] += e[i + j] / len; } printf("Case %d: %.7f\n", t, e[0]); } return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1e5 + 5; double ary[maxn]; int main(){ int T; cin >> T; for(int i = 2; i < maxn; i++) { double tot = 0; int c = 0; for(int j = 1; j * j <= i; j++) { if(i % j == 0) { tot += ary[j]; c++; if(j != i / j) { tot += ary[i / j]; c++; } } } ary[i] = (tot + c) / (c - 1); } for(int t = 1; t <= T; t++) { int n; cin >> n; printf("Case %d: %.6lf\n", t, ary[n]); } return 0; }
LightOJ1079 Just another Robbery
思路:概率dp。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1e4 + 5; float dp[maxn]; int gain[maxn]; float risk[maxn]; int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { memset(dp, 0, sizeof(dp)); dp[0] = 1; float P; int n; cin >> P >> n; int sum = 0; for(int i = 1; i <= n; i++){ cin >> gain[i] >> risk[i]; sum += gain[i]; } for(int i = 1; i <= n; i++) { for(int j = sum; j >= gain[i]; j--){ dp[j] = max(dp[j], dp[j - gain[i]] * (1 - risk[i])); } } for(int i = sum; i >= 0; i--) if(dp[i] >= (1 - P)) { printf("Case %d: %d\n", t, i); break; } } return 0; }
思路:算无相同生日概率。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int maxn = 1e5 + 5; int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { int n; cin >> n; int cnt = 1; double p = 1; while(p > 0.5) { p *= (n - cnt) * 1.0 / n; cnt++; } printf("Case %d: %d\n", t, --cnt); } return 0; }
LightOJ1151 Snakes and Ladders
思路:递推。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { int n; cin >> n; double ans = 0; for(int i = 1; i <= n; i++) ans += 1.0 / i; printf("Case %d: %.6lf\n", t, ans * n); } return 0; }
LightOJ 1265 Island of Survival
思路:deer有关的对结果无影响。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { int tiger, deer; cin >> tiger >> deer; if(tiger % 2 == 1) { printf("Case %d: 0\n", t); } else { double p = 1; while(tiger) { p *= (tiger - 1.0) / (tiger + 1); tiger -= 2; } printf("Case %d: %.6lf\n", t, p); } } return 0; }
LightOJ 1274 Beating the Dataset
LightOJ1284 Lights inside 3D Grid
思路:排除法。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; using ll = long long; int x, y, z, k; double p(int xx, int nn) { return 1. - 1.0 * ((nn - xx) * (nn - xx) + (xx - 1) * (xx - 1)) / (nn * nn); } int main(){ int T; cin >> T; for(int t = 1; t <= T; t++) { cin >> x >> y >> z >> k; double cnt = 0; for(int i = 1; i <= x; i++) for(int j = 1; j <= y; j++) for(int l = 1; l <= z; l++) { double possible = p(i, x) * p(j, y) * p(l, z); cnt += (1 - pow(1 - 2.0 * possible, k)) / 2.0; } printf("Case %d: %.6lf\n", t, cnt); } return 0; }
LightOJ1317 Throwing Balls into the Baskets