lightOJ 1248 - Dice (III) 概率DP

  • 题目链接
  • 题目大意:扔一个n面(各面的出现是等可能的)的色子,求各面至少出现一次的扔的次数的期望值。

思路:开始想着直接推公式,然后发现公式的形式是递归的,所以想到用DP。
1. 设dp[i]为还剩下i个面要扔的期望值
2. n 为色子的面数
3. 如果要求dp[i],则有可能下一次扔的是在之前扔过的那些面,也有可能是在没有扔过的面,有这两种可能,所以:dp[i] = (i+1)/n * dp[i+1] + (n-i-1)n * dp[i] + 1;移项后得:dp[i] = dp[i+1] + n/(i+1);
4. dp[n] = 0;
5. 最后的结果为dp[0];

参考代码如下:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
#include <complex>
#include <set>
#include <deque>
#define DEBUG(x) cerr<<"line:"<<__LINE__<<", "<<#x" == "<<(x)<<endl;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define FOR(it,s) for(__typeof(s.begin()) it=s.begin();it!=s.end();it++)
#define ALL(a) a.begin(),a.end()
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define DRI(x) int (x);scanf("%d",&(x))
#define DRII(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define DRIII(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define MS0(a) memset((a),0,sizeof((a)))
#define MS1(a) memset((a),-1,sizeof((a)))
#define MS(a,b) memset((a),(b),sizeof((a)))
using namespace std;

typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;

#define INF (int)1e9
#define EPS (1.0e-8)
// ------------------
const int maxn= 1e5 + 10;
double dp[maxn];

int main(void)
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef LOCAL
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
#endif
  DRI(t);
  for(int kase = 1;kase <= t; kase++){
    DRI(n);
    dp[n] = 0;
    for(int i = n-1; i >= 0; i--){
      dp[i] = dp[i+1] + (n * 1.0) / (i+1);
    }
    printf("Case %d: %lf\n", kase, dp[0]);
  }
#ifdef LOCAL
        cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值