POJ 2096 Collecting Bugs (probabilistic DP)

Source: Internet
Author: User
Tags gcd cmath

Test instructions: Given N-class bugs, and s subsystems, you can find a bug every day, find out the N type of bug, and s have at least one of the expectations of how much.

Analysis: Should be a very simple probability dp,dp[i][j] indicates that I have been from the J subsystem to find the type I bug, to achieve the desired number of days of the target,

Obviously dp[n][s] = 0.0, and dp[0][0] is the answer, the rest is relatively simple,

DP[I][J] = (dp[i+1][j]* (n-i) *j + dp[i][j+1]*i* (s-j) + dp[i+1][j+1]* (n-i) * (s-j) + n*s)/(n*s*1.0-i*j*1.0);

The code is as follows:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1//#include <tr1/ unordered_map> #define FREOPENR freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using Namespace std;//using namespace std:: tr1;typedef Long Long ll;typedef pair<int, int> p;const int INF = 0x3f3f3f3f;  Const double inf = 0x3f3f3f3f3f3f;const LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const  int MAXN = 1e3 + 5;const LL mod = 10000000000007;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1,-1, -1};const int dc[] = {0, 1, 0,-1, 1,-1, 1, -1};const char *hex[] = {"0000 "," 0001 "," 0010 "," 0011 "," 0100 "," 0101 "," 0110 "," 0111 "," 1000 "," 1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "};inline ll GCD (ll A, ll b) {return b = = 0? A:GCD (b, a%b);  }int N, m;const int mon[] = {0, 31, 29, 31, 30, 31, 0, +, +,, +, +, +, +, +, 31};const int monn[] N, H, H, C, h, 31};inline int Min (int a, int b) {return a < b? A:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;}    Double Dp[maxn][maxn];int Main () {int s;        while (scanf ("%d%d", &n, &s) = = 2) {Dp[n][s] = Dp[n+1][s] = dp[n][s+1] = dp[n+1][s+1] = 0.0; for (int i = n; i >= 0; i.) for (int j = s; J >= 0;--j) {if (i = = N && j = = s) con                Tinue; DP[I][J] = (dp[i+1][j]* (N-i) *j + dp[i][j+1]*i* (s-j) + dp[i+1][j+1]* (n-i) * (s-j) + n*s)/(n*s*1.0-i*j*1.0);    } printf ("%.4f\n", dp[0][0]); } return 0;}

POJ 2096 Collecting Bugs (probabilistic DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.