概率题

题目一

题目链接

https://www.acwing.com/problem/content/description/219/

题目大意

image

题目代码

from sys import stdin,setrecursionlimit
from functools import lru_cache
from math import inf
from collections import defaultdict as df
from collections import deque
setrecursionlimit(1000000)
input = lambda: stdin.readline().strip()

r1 = lambda: int(input())
r2 = lambda: map(int,input().split())
r3 = lambda: [*map(int,input().split())]

def solve():
    n,m = r2()
    g = df(list) 
    d = [0] * (n + 1)
    for _ in range(m):
        u,v,w = r2()
        g[u].append([v,w])
        d[u] += 1
    # dfs(u)表示从 u -> n 的期望长度
    '''
    爆栈喽!
    @lru_cache(None)
    def dfs(u):
        ans = 0
        for v,w in g[u]:
            ans += (w + dfs(v)) / d[u]
        return ans
    '''
    ans = 0
    q = deque([(1,1,0)])
    while q:
        u,p,dis = q.popleft()
        for v,w in g[u]:
            new_p = p * d[u]
            new_dis = dis + w
            if v == n:
                ans += new_dis / new_p
            else:
                q.append((v,new_p,new_dis))
            
    print("%.2f" % ans)
    
if __name__ == "__main__":

    t = 1
    for _ in range(t):
        solve()

题目二

题目链接

https://www.acwing.com/problem/content/220/

题目大意

image

题目代码

from sys import stdin,setrecursionlimit
from functools import lru_cache
from math import inf
from collections import defaultdict as df
from collections import deque
setrecursionlimit(1000000)
input = lambda: stdin.readline().strip()

r1 = lambda: int(input())
r2 = lambda: map(int,input().split())
r3 = lambda: [*map(int,input().split())]

def solve():
    A,B,C,D = r2()

    @lru_cache(None)
    def dfs(a,b,c,d,x,y):
        a1 = a + (x == 0) + (y == 0)
        b1 = b + (x == 1) + (y == 1)
        c1 = c + (x == 2) + (y == 2)
        d1 = d + (x == 3) + (y == 3)
        if a1 >= A and b1 >= B and c1 >= C and d1 >= D:
            return 0
        total = a + b + c + d + (x != 4) + (y != 4)
        remain = 54 - total
        if remain <= 0:
            return inf
        ans = 1
        if a < 13:
            ans += (13 - a) / remain * dfs(a + 1,b,c,d,x,y)
        if b < 13:
            ans += (13 - b) / remain * dfs(a,b + 1,c,d,x,y)
        if c < 13:
            ans += (13 - c) / remain * dfs(a,b,c + 1,d,x,y)
        if d < 13:
            ans += (13 - d) / remain * dfs(a,b,c,d + 1,x,y)
        if x == 4:
            tmp = inf
            for i in range(4):
                tmp = min(tmp,1 / remain * dfs(a,b,c,d,i,y))
            ans += tmp
        if y == 4:
            tmp = inf
            for i in range(4):
                tmp = min(tmp,1 / remain * dfs(a,b,c,d,x,i))
            ans += tmp
        return ans
    print("%.3f" % dfs(0,0,0,0,4,4))
    
if __name__ == "__main__":

    t = 1
    for _ in range(t):
        solve()

题目三

题目链接

https://codeforces.com/problemset/problem/148/D

题目大意

image

题目代码

image

from sys import stdin,setrecursionlimit
from functools import lru_cache
setrecursionlimit(100000)
input = lambda: stdin.readline().strip()

r1 = lambda: int(input())
r2 = lambda: map(int,input().split())
r3 = lambda: [*map(int,input().split())]

def solve():
    A,B = map(int,input().split())
    @lru_cache(None)
    def dfs(i,j):
        if i == 0:
            return 0
        if j == 0:
            return 1
        # 公主抓到一只白鼠
        ans = i / (i + j)
        # 公主抓到一只黑鼠,龙抓到一只黑鼠,跑出一只黑鼠
        if j >= 3:
            ans += j / (i + j) * (j - 1) / (i + j - 1) * (j -2) / (i + j - 2) * dfs(i,j - 3)
        # 公主抓到一只黑鼠,龙抓到一只黑鼠,跑出一只白鼠
        if i >= 1 and j >= 2:
            ans += j / (i + j) * (j - 1) / (i + j - 1) *    i   / (i + j - 2) * dfs(i - 1,j - 2)
        return ans
    print("%.9f" % dfs(A,B))
    
if __name__ == "__main__":

    t = 1
    for _ in range(t):
        solve()
posted @ 2024-04-10 13:46  gebeng  阅读(22)  评论(0)    收藏  举报