SGU-495 Kids and Prizes 概率DP
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495
题意:有n个盒子,每个盒子里面放了一个奖品,m个人轮流去选择盒子,如果盒子里面有奖品,就把奖品拿走,盒子留下,否则直接走人。求最后被抽走的奖品数期望。。。
如果直接考虑用期望来建立DP,复杂度都很高,而且题目精度过高。换一个角度思考,考虑每个奖品不被拿走的概率(1/n)^m,那么不被拿走的期望就是n*(1/n)^m,则拿走的期望n-n*(1/n)^m,复杂度O(1)。还可以考虑f[i]表示第 i 个人拿到奖品的概率,那么1-f[i]就是没有拿到奖品的概率,是对立事件,则f[i]=(1-f[i-1])*f[i-1] + f[i-1]*(f[i-1]-1/n),(1-f[i-1])*f[i-1]表示第 j-1 个人没有拿到奖品,f[i-1]*(f[i-1]-1/n)表示第 j-1 个人拿到奖品了,那么接下来拿到奖品的概率就会减小1/n。。
O(1):
//STATUS:C++_AC_15MS_320KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const LL MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double n,m; int main(){
// freopen("in.txt","r",stdin);
int i,j;
while(~scanf("%lf%lf",&n,&m))
{
printf("%.10lf\n",n*(-pow((n-)/n,m)));
}
return ;
}
O(m):
//STATUS:C++_AC_15MS_1675KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const LL MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double f[N];
int n,m; int main(){
// freopen("in.txt","r",stdin);
int i,j;
double ans;
while(~scanf("%d%d",&n,&m))
{
f[]=;ans=;
for(i=;i<m;i++){
f[i]=(-f[i-])*f[i-]+f[i-]*(f[i-]-1.0/n);
ans+=f[i];
} printf("%.10lf\n",ans);
}
return ;
}
SGU-495 Kids and Prizes 概率DP的更多相关文章
- sgu 495. Kids and Prizes (简单概率dp 正推求期望)
题目链接 495. Kids and Prizes Time limit per test: 0.25 second(s)Memory limit: 262144 kilobytes input: s ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- SGU 495. Kids and Prizes( 数学期望 )
题意: N个礼品箱, 每个礼品箱内的礼品只有第一个抽到的人能拿到. M个小孩每个人依次随机抽取一个, 求送出礼品数量的期望值. 1 ≤ N, M ≤ 100, 000 挺水的说..设f(x)表示前x ...
- SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意: 有n个礼物盒,m个人. 最开始每个礼物盒中都有一个礼物. m个人依次随 ...
- [SGU495] Kids and Prizes (概率dp)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题目大意:有N个盒子,里面都放着礼物,M个人依次去选择盒子,每人仅能选一次,如 ...
- 495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 学习:当一条路走不通,换一种对象考虑,还有考虑对立面. 495. Kids and Pr ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- 概率dp专场
专题链接 第一题--poj3744 Scout YYF I 链接 (简单题) 算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 ...
随机推荐
- POJ 3786 Adjacent Bit Counts (DP)
点我看题目 题意 :给你一串由1和0组成的长度为n的数串a1,a2,a3,a4.....an,定义一个操作为AdjBC(a) = a1*a2+a2*a3+a3*a4+....+an-1*an.输入两个 ...
- GIT:本地有更改,但强制作远程仓库里作更新
有时,紧急线上修改时,这个功能有用处的. git fetch --all git reset --hard origin/master ================ git reset --har ...
- PJSIP在windows(xp或者win7)下的编译,编译工具是vs2008,PJSIP版本2.3
PJSIP是一个开源的SIP协议库,它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多媒体通信框架提供了非常清晰的API,以及NAT穿越的功能.PJSIP具有 ...
- Win7 默认hosts文件
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP f ...
- Tomcat 部署Undeployment Failure
Tomcat 部署Undeployment Failure - yongjava的日志 - 网易博客 http://blog.163.com/qiangyongbin2000@126/blog/sta ...
- SwapEffect 枚举(定义交换效果)
由于创建设备时要用到这个值,所以在这里总结一下,以免以后再找. 首先引自msdn: Copy 只能为构成单个后台缓冲区的交换链指定此交换效果. 无论交换链是有窗口的还是全屏的,运行库都保证 Devic ...
- 如何修改linux系统主机名称
完成目标: 修改centos 7系统的主机名称 使用命令: hostnamectl [root@ossec-server ~]# hostnamectl --help hostnamectl [OPT ...
- java遍历树(深度遍历和广度遍历
java遍历树如现有以下一颗树:A B B1 B11 B2 B22 C C ...
- 简单的神经元算法实现(python)
参考python代码如下 #perceptron x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]] y ...
- getchar()函数的返回值赋给char型,用if(ch=getchar() != EOF)测试,输入ctrl+z同样可以结束循环的分析
2013-07-18 21:35:58 getchar()函数的返回值赋给char型,用if(ch=getchar() != EOF)测试,输入ctrl+z同样可以结束循环的分析. char是字符型数 ...