首页 > 其他分享 >洛谷P8774 [蓝桥杯 2022 省 A] 爬树的甲壳虫 题解 期望DP

洛谷P8774 [蓝桥杯 2022 省 A] 爬树的甲壳虫 题解 期望DP

时间:2024-09-18 14:24:41浏览次数:3  
标签:洛谷 gcd int 题解 ll long 蓝桥 maxn ldots

题目链接:https://www.luogu.com.cn/problem/P8774

思路:

设 \(f_i\) 为甲壳虫从高度 \(i\) 到达高度 \(n\)

因为从高度 \(i\) 走 \(1\) 步有 \(1-P_{i+1}\) 的概率到达高度 \(i+1\),有 \(P_{i+1}\) 的概率到达高度 \(0\),所以:

\(f_i = 1 + (1 - P_{i+1}) \times f_{i+1} + P_{i+1} \times f_0\)

可以高斯消元,但是高斯消元没咋写过啊

\(f_0 = (1-P_1) f_1 + P_1 f_0 + 1\)

\(f_1 = (1-P_2) f_2 + P_2 f_0 + 1\)

\(f_2 = (1-P_3) f_3 + P_3 f_0 + 1\)

\(\ldots\)

\(f_{n-1} = (1-P_n) f_n + P_n f_n + 1\)

\(f_n = 0\)

考虑 \(f_0\)

令 \(X = P_1 + (1-P_1) P_2 + (1-P_1)(1-P_2) P_3 + \ldots (1-P_1)(1-P_2) \cdots (1-P_{n-1}) P_n\)

考虑常数项:

令 \(Y = 1 + (1-P1) + (1-P_1)(1-P2) + \ldots + (1-P_1)(1-P_2) \cdots (1-P_{n-1})\)

所以

\(f_0 = X f_0 + Y\)

\(f_0 = \dfrac{Y}{1 - X}\)

示例程序:

#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
typedef long long ll;
void gcd(ll a , ll b , ll &d , ll &x , ll &y) {
    if(!b) {d = a; x = 1; y = 0;}
    else { gcd(b , a%b,d,y , x); y -= x * (a/b); }
}
ll inv(ll a , ll n = mod) {
    ll d , x , y;
    gcd(a , n , d,  x , y);
    return d == 1 ? (x+n)%n : -1;
}

const int maxn = 1e5 + 5;
int n;
long long x[maxn], y[maxn], p[maxn];

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%lld%lld", x+i, y+i);
        p[i] = x[i] * inv(y[i]) % mod;
    }
    ll X = p[1], Y = 1, tmp = 1;
    for (int i = 2; i <= n; i++) {
        // tmp = tmp * (1 - p[i-1] + mod) % mod;
        tmp = tmp * (y[i-1] - x[i-1]) % mod * inv(y[i-1]) % mod;
        X = (X + tmp * p[i] % mod) % mod;
        Y = (Y + tmp) % mod;
    }
    ll ans = Y * inv((1 - X + mod) % mod) % mod;
    printf("%lld\n", ans);
    return 0;
}



标签:洛谷,gcd,int,题解,ll,long,蓝桥,maxn,ldots
From: https://blog.51cto.com/u_13536312/12045937

相关文章

  • 洛谷P4550 收集邮票 题解 期望DP
    题目链接:https://www.luogu.com.cn/problem/P4550解题思路:定义状态\(f_i\)表示目前已经取到\(i\)种邮票的情况下,取完所有\(n\)很明显,\(f_n=0\),因为此时已经取完了\(n\)如果当前已经取到了\(i\)有\(\frac{i}{n}\)的概率取到现有的邮票(此时仍然拥有\(i\)有\(1-\frac{i......
  • 2024 CCPC 网赛题解
    G最大流#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineMAXN205#defineINF0x3f3f3f3f3f3f3f3f#defineLLlonglong#defineIntregisterintusingnamespacestd;inlinevo......
  • 洛阳师范学院 ACM实验室 中秋娱乐赛“月饼代码大逃杀”题解
    题解包括C和C++两种语言_壹我要洋人死!1、直接输出即可C语言题解:#include<stdio.h>intmain(){printf("woyaoyangrensi!");return0;}C++语言题解:#include<iostream>usingnamespacestd;intmain(){ printf("woyaoyangrensi!"); return0;}......
  • 蓝桥杯【物联网】零基础到国奖之路:八. RTC
    蓝桥杯【物联网】零基础到国奖之路:八.RTC第一节RTC的基本知识第二节CubeMX配置第三节代码第一节RTC的基本知识RTC是实时时钟,指可以想时钟一样输出实际时间的电子设备,一般会是集成电路,也被称为是时钟芯片。总之,RTC只是一个靠电池维持运行的32位定时器,并不像实时......
  • P11072 Alice and Bob 题解
    简单博弈题。先说结论,如果存在\(a_i=0\)使得\(1\lei\lea_1\)的话,那么先手必胜,否则后手必胜。若满足上述条件显然先手必胜,将\(0\)搞到第一个就行。否则Alice每操作一次,如果操作后满足了上述条件,那么Bob赢,否则Bob只要不动就行。但是下一轮Alice必须动,要不然两......
  • 蓝桥杯-STM32G431RBT6(串口)
    前言一、配置二、使用步骤1.串口发送代码逻辑效果展示2.串口接收单个字符代码逻辑中断回调函数3.串口接受字符串代码逻辑字符串函数中断回调函数声明代码开源前言一、配置二、使用步骤1.串口发送代码逻辑sprintf(tx_buf,"jinke\r\n"):这行代码使用......
  • 蓝桥杯—STM32G431RBT6按键的多方式使用(包含软件消抖方法精讲)从原理层面到实际应用(一)
    新建工程教程见http://t.csdnimg.cn/JySLg点亮LED教程见http://t.csdnimg.cn/Urlj5末尾含所有代码目录按键原理图一、按键使用需要解决的问题1.抖动   1.什么是抖动   2.抖动类型   3.如何去消除抖动FIRST.延时函数消抖(缺点:浪费CPU资源)SECOND.中......
  • 【洛谷 P1048】[NOIP2005 普及组] 采药 题解(动态规划+01背包)
    [NOIP2005普及组]采药题目描述辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株......
  • 历年CSP-J初赛真题解析 | 2019年CSP-J初赛阅读程序(16-33)
    学习C++从娃娃抓起!记录下CSP-J备考学习过程中的题目,记录每一个瞬间。附上汇总贴:历年CSP-J初赛真题解析|汇总_热爱编程的通信人的博客-CSDN博客#include<cstdio>#include<cstring>usingnamespacestd;charst[100];intmain(){scanf("%s",st);intn......
  • 【架构设计】多级缓存:应用案例与问题解决策略
    【架构设计】多级缓存:应用案例与问题解决策略多级缓存系统的工作原理及其在提升应用性能方面的关键作用。通过对比本地缓存与分布式缓存的特点| 原创作者/编辑:凯哥Java                    | 分类:架构设计系列教程多级缓存系统:提升性能的......