洛谷题单指南-概率与统计-P2111 考场奇遇

原题链接:https://www.luogu.com.cn/problem/P2111

题意解读:小明答题正确率A%,小红与小明答案一致是1不一致是0,一共n道题,小红答题结果串是S,求小红答对Q道题以上的概率。

解题思路:

对于一道题,

如果结果是1,则小红答对的概率是A%,答错的概率是1-A%;

如果结果是0,则小红答对的概率是1-A%,答错的概率是A%。

由于Q与N相差不超过5,考虑枚举所有答错题不超过N-Q的子集,累加概率。

注意:

1、枚举时记录答错题数,如果已经>N-Q,则可以剪枝;

2、对于Q=0的情况进行特殊处理,答对0题及以上的概率是100%。

100分代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 55;
int stat[N];
string s;
int n, a, q;
double ans;

void dfs(int k, int cnt)
{
    if(cnt > n - q) return;
    if(k > n)
    {
        double res = 1.0;
        for(int i = 1; i <= n; i++)
        {
            if(stat[i] == s[i] - '0') res *= (1.0 - 0.01 * a);
            else res *= 0.01 * a;
        }
        ans += res;
        return;
    }

    stat[k] = 1; //第k道题答错
    dfs(k + 1, cnt + 1);
    stat[k] = 0;

    dfs(k + 1, cnt); //第k道题没有答错
}

int main()
{
    cin >> n >> a >> q >> s;
    s = " " + s;
    if(q == 0)
    {
        printf("%.3lf", 1.0);
        return 0;
    }
    dfs(1, 0);
    printf("%.3lf", ans);
    return 0;
}

 

posted @ 2026-01-08 15:53  hackerchef  阅读(6)  评论(0)    收藏  举报