HDU dice DP求期望+推公式

题意:
一个m边形的骰子,求连续投出n个相同的面,和m个两两不同的面的期望次数。
solution:
令\(f_i\)表示已经连续投出i个相同的面,到连续投出n个还需要的期望次数.

令\(g_i\)类似的表示第二种问题的期望次数。

对于\(f_i\) ,有两种情况:

① 投出了和前i个相同的面,转移到了\(f_{i+1}\) ,那么\(f_i+=(f_{i+1}+1)*\frac{1}{m}\)

② 投出了一个不同的面,转移到了\(f_1\),那么\(f_i+=(f_1+1)*\frac{m-1}{m}\)

综上,\(f_i=(f_{i+1}+1)*\frac{1}{m}+(f_1+1)*\frac{m-1}{m}=f_{i+1}*\frac{1}{m}+f_1*\frac{m-1}{m}+1\)

继续整理得到:\(m*f_i=m+(m-1)*f_1+f_{i+1}\)

同理的话有:\(m*f_{i+1}=m+(m-1)*f_1+f_{i+2}\)

两式相减得到:\(m*(f_{i+1}-f_i)=f_{i+2}-f_{i+1}\)

可以发现,这成一个等比数列:
\[
f_0-f_1=1\f_1-f_2=m\…\f_{n-1}-f_n=m^{n-1}
\]
运用等比数列求和公式可得:\(ans=\frac{m*(1-m^{n-1})}{1-m}+1\) 。

对于\(g_i\):

① 投出了和前i个都不同的面,转移到了\(g_{i+1}\) ,那么\(g_i+=(g_{i+1}+1)*\frac{m-i}{m}\)

② 投出了以前出现过的面,那么可能转移到了\(g_1,g_2,…g_i\),那么\(g_i+=\sum_{1≤j≤i}{(g_j+1)*\frac{1}{m}}\)

综上,\(g_i=(g_{i+1}+1)*\frac{m-i}{m}+\sum_{1≤j≤i}{(g_j+1)*\frac{1}{m}}=g_{i+1}*\frac{m-i}{m}+\frac{1}{m}*\sum_{1≤j≤i}{g_j}+1\)

同上面\(f_i\)类似的处理之后,可以同样的得到\(g_i-g_{i+1}=\frac{m}{m-i}*(g_{i+1}-g_{i+2})\),直接递推一下即可。

code:

#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define RG register
#define IL inline
#define LL long long
#define DB double
using namespace std;

const int N=1e6+1;

int n,m;
DB ans,s[N];

IL int qpow(int x,int p) {
    RG int ans=1;
    for(;p;p>>=1,x*=x)
        if(p&1) ans*=x;
    return ans;
}

int main()
{
    RG int i,T,typ;
    while(scanf("%d",&T)!=EOF) {
        while(T--) {
            scanf("%d%d%d",&typ,&n,&m);
            if(typ==0) printf("%.9lf\n",(DB)n*(1-qpow(n,m-1))/(1-n)+1);
            else {
                ans=0,s[0]=1;
                for(i=1;i<m;++i) s[i]=s[i-1]*n/(n-i);
                for(i=0;i<m;++i) ans+=s[i];
                printf("%.9lf\n",ans);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Bhllx/p/10848859.html

时间: 2024-10-10 10:24:24

HDU dice DP求期望+推公式的相关文章

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

bnu 12639 Cards (dp求期望)

bnu 12639 Cards dp求期望 区分 全局最优选择 和 当前最优选择. 本题是当前最优选择. 状态表示: double dp[16][16][16][16][5][5]; bool vis[16][16][16][16][5][5]; 状态下参数: vector<int> up, vector<int> tmp. so,记忆化搜索 + 回溯 //#pragma warning (disable: 4786) //#pragma comment (linker, &quo

POJ 2096 (dp求期望)

A - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-05-15) Description Ivan is fond of collecting. Unlike other people who collect post stamps, co

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu