Luo Gu P2719 funny solution to a problem probability entry DP World Cup

  • Author: zifeiy
  • Tags: probability DP

Topic links: https://www.luogu.org/problem/P2719

We provided f[n][m]for indicating remaining n Zhang Zhang A Class B Class m votes votes same last two tickets probability, then:

  • If \ (n \ le 1 \) and \ (m \ Le. 1 \) , then \ ([n-] F [m] = 0 \) (Minato missing two of the same)
  • Otherwise, if the \ (n == 0 \) or \ (m == 0 \) , then \ (F [n-] [m] =. 1 \) (Sure as two tickets)
  • Otherwise, \ (F [n-] [m] = (F [n--. 1] [m] + F [n-] [. 1-m]) \ Times 0.5 \) (half of the probability of a Type A ticket take the other half took the probability of a B class ticket)

So we can write the following code in accordance with the following ideas:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1300;
double f[maxn][maxn];
int n;

int main() {
    scanf("%d", &n);
    n /= 2;
    for (int i = 0; i <= n; i ++) {
        for (int j = 0; j <= n; j ++) {
            if (i < 2 && j < 2) f[i][j] = 0.;
            else if (i == 0 || j == 0) f[i][j] = 1.;
            else f[i][j] = 0.5 * (f[i-1][j] + f[i][j-1]);
        }
    }
    printf("%.4lf\n", f[n][n]);
    return 0;
}

Memory search way to write code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1300;
bool vis[maxn][maxn];
double f[maxn][maxn];
int n;

double dfs(int n, int m) {
    if (vis[n][m]) return f[n][m];
    vis[n][m] = true;
    if (n <= 1 && m <= 1) return 0.;
    if (n == 0 || m == 0) return 1.;
    f[n][m] = 0.5 * (dfs(n-1, m) + dfs(n, m-1));
    return f[n][m];
}

int main() {
    scanf("%d", &n);
    n /= 2;
    printf("%.4lf\n", dfs(n, n));
    return 0;
}

Guess you like

Origin www.cnblogs.com/codedecision/p/11640951.html