【解題報告】 洛谷P1297 單選錯位
阿新 • • 發佈:2021-10-10
【解題報告】 洛谷P1297 單選錯位
題目連結
https://www.luogu.com.cn/problem/P1297
思路
這道題目一看是一道期望的題目,但是這個不用 DP ,我們發現,後一個和前一個答案的情況總共有 \(a_{i-1} \times a_i\) 種
然後我們為了使得這兩種答案重合的概率肯定是 \(\dfrac {min \{ a_{i-1},a_i\}} {a_{i-1} \times a_i}\)
上下約分 \(\dfrac 1 {max \{a_{i-1},a_i \}}\)
於是我們直接統計一下答案就可以了
本博文為wweiyi原創,若想轉載請聯絡作者,qq:2844938982#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> using namespace std; const int maxn=10000005; double ans; int a[maxn]; int n,A,B,C; void init() { 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; } int main() { init(); a[0]=a[n]; for(int i=1;i<=n;i++) ans+=(double)(1.0/max(a[i-1],a[i])); printf("%.3lf\n",ans); return 0; }