【HDOJ】4652 Dice
1. 题目描述
对于m面的骰子。有两种查询,查询0表示求最后n次摇骰子点数相同的期望;查询1表示最后n次摇骰子点数均不相同的期望。
2. 基本思路
由期望DP推导,求得最终表达式。
(1) 查询0
不妨设$dp[k]$表示当前已经有k次相同而最终实现n次相同的期望。
\begin{align}
dp[0] &= 1 + dp[1] \notag \\
dp[1] &= 1 + \frac{1}{m}dp[2] + \frac{m-1}{m}dp[1] \notag \\
dp[2] &= 1 + \frac{1}{m}dp[3] + \frac{m-1}{m}dp[1] \notag \\
&\cdots \notag \\
dp[n-1] &= 1 + \frac{1}{m}dp[n] + \frac{m-1}{m}dp[1] \notag \\
dp[n] &= 0
\end{align}
两两相减可得。
\begin{align}
dp[0]-dp[1] &= \frac{1}{m}(dp[1]-dp[2]) \notag \\
dp[1]-dp[2] &= \frac{1}{m}(dp[2]-dp[3]) \notag \\
dp[2]-dp[3] &= \frac{1}{m}(dp[3]-dp[4]) \notag \\
&\cdots \notag \\
dp[n-2]-dp[n-1] &= \frac{1}{m}(dp[n-1]-dp[n])
\end{align}
由$dp[0]=1+dp[1], dp[0]-dp[1]=1$代入可得。
\begin{align}
dp[0]-dp[1] &= 1 \notag \\
dp[1]-dp[2] &= m \notag \\
dp[2]-dp[3] &= m^2 \notag \\
&\cdots \notag \\
dp[n-1]-dp[n] &= m^{n-1}
\end{align}
显然是一个等比数列,累加后可得$dp[0]-dp[n]=dp[0]$.
\begin{align}
dp[0] = \frac{m^n-1}{m-1}
\end{align}
(2)查询1
不妨设$dp[k]$表示当前已经有k个不同而最终实现n个不同的期望。
\begin{align}
dp[0] &= 1 + dp[1] \notag \\
dp[1] &= 1 + \frac{1}{m}dp[1] + \frac{m-1}{m}dp[2] \notag \\
dp[2] &= 1 + \frac{1}{m}dp[1] + \frac{1}{m}dp[2] + \frac{m-2}{m}dp[3] \notag \\
&\cdots \notag \\
dp[n-1] &= 1 + \Sigma_{i=1}^{n-1}{\frac{1}{m}dp[i]} + \frac{m-(n-1)}{m}dp[n] \notag \\
dp[n] &= 0
\end{align}
两两相减可得。
\begin{align}
dp[0]-dp[1] &= \frac{m-1}{m}(dp[1]-dp[2]) \notag \\
dp[1]-dp[2] &= \frac{m-2}{m}(dp[2]-dp[3]) \notag \\
&\cdots \notag \\
dp[n-2]-dp[n-1] &= \frac{m-(n-1)}{m}(dp[n-1]-dp[n])
\end{align}
由$dp[0]=1+dp[1], dp[0]-dp[1]=1$代入累加后可得。
\begin{align}
dp[0] = \Sigma_{i=1}^{n} \frac{m^i}{\prod_{j=0}^{i-1}(m-j)}
\end{align}
3. 代码
/* 4652 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 #define LL __int64 int t;
int op, n, m; LL Pow(LL base, int n) {
LL ret = ; while (n) {
if (n & )
ret *= base;
n >>= ;
base *= base;
} return ret;
} void solve() {
double ans = 0.0; if (op) {
double tmp = 1.0;
rep(i, , n) {
tmp = tmp * m / (m-i);
ans += tmp;
}
} else {
LL fz = Pow(m, n) - ;
ans = fz / (m-);
}
printf("%.9lf\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d", &t)!=EOF) {
while (t--) {
scanf("%d%d%d", &op, &m, &n);
solve();
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】4652 Dice的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- 静态的html页面想要设置使用浏览器缓存
设置html页面缓存方法: 静态的html页面想要设置使用缓存: 通过HTTP的META设置expires和cache-control code 1. <meta http-equiv=&qu ...
- 全面理解BFC
BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...
- ActiveMq+zookeeper+levelDB集群整合配置
ActiveMq+zookeeper+levelDB集群整合配置 环境:linux系统,jdk1.7 三台linux系统电脑.我这里使用一台window,分别远程3台linux电脑.三台电脑的ip分 ...
- python字典操作
Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.一.创建字典字典由键和对应值成对组成.字典也被称作关联数组或哈希表.基本语法如下: 代码如下: dict ...
- what is the virtual machine, when and why we need use it ?
虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. 通过虚拟机软件,你可以在一台物理计算机上模拟出二台或多台虚拟的计算机,这些虚 ...
- ueditor使用中的坑
项目中要使用富文本编辑于是采用了百度的开源富文本编辑器 ueditor 官网 http://ueditor.baidu.com/website/ 使用方法就按照官方的来的. 经过使用记录以下要点 ...
- C#向C++编写的DLL传递字符串参数的办法
使用StringBuilder,举例: C++代码中函数定义如下: PVPOWERFORCASTDLL_API int PVPowerForcast(SForcastInfo &_Forcas ...
- 微软职位内部推荐-Senior NLP Scientist & Developer
微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com )Senior Software Development Engineer/NL ...
- springside springmvc 的一个SB问题
<form id="inputForm" modelAttribute="order" action="${ctx}/myorder/orde ...
- 1024: [SCOI2009]生日快乐 - BZOJ
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋 ...