SRM 620 D2L3: RandomGraph, dp
称号:http://community.topcoder.com/stat?
c=problem_statement&pm=13143&rd=15853
參考:http://apps.topcoder.com/wiki/display/tc/SRM+620
又是一道关于概率的题目,考虑dp方法。关键是找准突破口,将题目条件的“至少有一个大于4的连通图”转换为“全部连通图都小于等于3”。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip> #include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std; #define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair /*************** Program Begin **********************/ double dp[51][51][51];
bool solved[51][51][51]; class RandomGraph {
public:
int n;
double p;
double rec(int a, int b, int c)
{
double & res = dp[a][b][c];
if (solved[a][b][c]) {
return res;
}
int r = n - (a + 2 * b + 3 * c);
// base case
if (0 == r) {
res = 1.0;
solved[a][b][c] = true;
return res;
}
res = 0.0;
// r != 0
res += pow(1 - p, a + 2 * b + 3 * c) * rec(a + 1, b, c);
if (a >= 1) {
res += pow(1 - p, a + 2 * b + 3 * c - 1) * a * p * rec(a - 1, b + 1, c);
}
if (a >= 2) {
res += pow(1 - p, a + 2 * b + 3 * c - 2) * p * p * a * (a - 1) / 2 * rec(a - 2, b, c + 1);
}
if (b >= 1) {
res += pow(1 - p, a + 2 * b + 3 * c - 1) * p * 2 * b * rec(a, b - 1, c + 1);
res += pow(1 - p, a + 2 * b + 3 * c - 2) * p * p * b * rec(a, b - 1, c + 1);
}
solved[a][b][c] = true; return res;
}
double probability(int n, int p) {
double res = 0;
this->n = n;
this->p = p / 1000.0; memset(solved, 0, sizeof(solved));
res = 1 - rec(0, 0, 0); return res;
} }; /************** Program End ************************/
版权声明:本文博主原创文章。博客,未经同意不得转载。
SRM 620 D2L3: RandomGraph, dp的更多相关文章
- SRM 514 DIV1 500pt(DP)
题目简述 给定一个H×W大小的矩阵,每个格子要么是1~9中的一个数,要么是".",要求你把“.”填成具体的数字(1~9),并且符合以下两个要求: 对于所有的整数r 和 c( 0 & ...
- SRM 511 DIV1 500pt(DP)
题目简述 给定n个数,两个人轮流取数,和之前两个人的取的数或起来,谁不能取数或者谁取到的数和之前的数或值为511谁输,问谁能够赢? 题解 刚开始的想法是直接搜,不过需要记录取过的值的状态,2^50显然 ...
- SRM 508 DIV1 500pt(DP)
题目简述 给定一个大小为 n的序列(n<=10)R,要求你计算序列A0, A1, ..., AN-1的数量,要求A序列满足A0 + A1 + ... + AN-1 = A0 | A1 | ... ...
- SRM 509 DIV1 500pt(DP)
题目简述 给定一个字符串,可以对其进行修改,删除,增加操作,相应的操作有对应的花费,要求你用最小的花费把字符串变为回文串 题目做法 先搞一遍floyed把各种操作的最小花费求出来,然后就是类似编辑距离 ...
- SRM 502 DIV1 500pt(DP)
题目简述 给定比赛时间T和n个题目,你可以在任意时间提交题目,每个题目有一个初始分数maxPoints[i],每个单位时间题目的分数将会减少pointsPerMinute[i],即如果在时间t解决了第 ...
- SRM 501 DIV1 500pt(DP)
题目简述 给定一个长度为n的序列,每个数值的范围为[-1,40],-1可以替换成0~40之间的数,要求你求出符合以下条件的序列有多少个? 1.每个数都是0~40之间的数 2.对于每一个数A[i],都需 ...
- UVA 620 Cellular Structure (dp)
Cellular Structure A chain of connected cells of two types A and B composes a cellular structure o ...
- SRM 620 DIV1 L2
题意:有n个等长的string(设string的长度为m),string中的字符从'A'到'Z',容许对m列执行稳定的排序操作,问说是否能通过这m种操作将这n个string调整成对应的顺序. 题解: ...
- topcoder srm 620 div1
problem1 link 分别计算可以得到(a,b)的有哪些二元组,以及可以得到(c,d)的有哪些二元组.然后在公共的二元组中找到和最大的即可. problem2 link 设最后的排序为$r=[2 ...
随机推荐
- Android自己定义组件系列【2】——Scroller类
在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...
- poj1185(状压dp)
题目连接:http://poj.org/problem?id=1185 题意:给出一张n*m的地图,'H'表示高地,不能部署炮兵,'P'表示平原,可以部署炮兵,炮兵之间必须保持横向.纵向至少2个格子的 ...
- 在IIS上发布Web(使用VS2005)
最近想在IIS上发布网站,弄了一下午.遇到很多问题,幸运的是都一一解决了,现在把解决问题的过程分享出来: 安装好IIS后,在VS2005上写了个网站(新建-->网站-->ASP.NET网站 ...
- .Net C# Windows Service于server无法启动,错误 193:0xc1
1.情况说明:的近期发展windows维修,当地win7系统正常.把server安装会失败. 图中的引导失败的例子.: 解决方法:执行->输入:eventvwr.msc 打开你的事件查看器 ...
- ubuntu下使用自带的openJDK查看java源码
如题 Ubuntu自带的OpenJDK仅仅有jre环境,不提供源代码,所以我们还是须要去下载. JDK6:http://download.java.net/openjdk/jdk6/ JDK7:htt ...
- PHP实现栈(Stack)数据结构
栈(Stack),是一种特殊的后进先出线性表,其只能在一端进行插入(插入一般称为压栈.进栈或入栈)和删除(删除一般称为弹栈.退栈或出栈)操作,允许进行插入和删除操作的一端称为栈顶,另一端则称为栈底.栈 ...
- SVN的命令解析(感觉不错就转了)
本文链接: http://www.php-oa.com/2008/03/12/svnminglingzailinuxxiadeshiyong.html .将文件checkout到本地目录 svn ch ...
- Maven插件之buildnumber-maven-plugin
某些情况下(这种情况一般很少见),使用maven构建项目时,需要一个不重复的序列号,比如说,打包时,包名称以当前构建时间结尾,或者每次生成的jar包中包含唯一的序列号,等等; 这个时候,就用到了bui ...
- 李林APUE之进程的封装
1.子进程是父进程的副本,获得父进程的数据空间/堆/栈,父子进程共享代码段.子进程从fork后開始运行.返回值=0表示子进程,由于子进程能够通过函数来获取父进程的ID,可是父进程无法知道子进程的ID. ...
- Oracle语句集锦
创建用户并赋予dba权限 1)进入cmd 2)sqlplus / as sysdba 或者 sqlplus sys/密码 as sysdba SQL> conn sys/wcq123@orcl ...