洛谷题单指南-概率与统计-P1297 [国家集训队] 单选错位

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

题意解读:n道题,每道题ai个选项,一组正确答案整体后移一位,求正确的数量期望。

解题思路:

主要看相邻两题选项数量:

如果ai == ai+1,两道题正确率一致,为1/ai

如果ai > ai+1,第i+1道题能正确的概率为(ai+1/ai) * (1/ai+1) = 1/ai

如果ai < ai+1,第i+道题能正确的概率为(1/ai) * (ai/ai+1) = 1/ai+1

100分代码:

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

const int N = 10000005;
int n, A, B, C, a[N];
double ans;

int main()
{
    scanf("%d%d%d%d%d", &n, &A, &B, &C, a + 1);
    for (int i = 2; i <= n; i++)
	a[i] = ((long long) a[i - 1] * A + B) % 100000001;
    for (int i = 1; i <= n; i++)
	a[i] = a[i] % C + 1;
    
    for(int i = 1; i <= n; i++)
    {
        int cur = i, nxt = i + 1;
        if(nxt == n + 1) nxt = 1;
        if(a[cur] >= a[nxt]) ans += 1.0 / a[cur];
        else ans += 1.0 / a[nxt];
    }

    printf("%.3lf", ans);
    return 0;
}

 

posted @ 2026-01-08 11:07  hackerchef  阅读(21)  评论(0)    收藏  举报