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 ...
随机推荐
- 读书笔记:《梦断代码Dreaming in Code》
读书笔记:<梦断代码Dreaming in Code> 拿到<梦断代码>书后,一口气翻了一遍,然后又用了3天时间仔细读了一遍,也不禁掩卷长叹一声,做软件难.虽难,仍要继续走下去 ...
- 最牛B的编程套路
最近,我大量阅读了Steve Yegge的文章.其中有一篇叫“Practicing Programming”(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反,单纯地每天埋头于工 ...
- Oracle JDBC版本区别(转)
oracle\product\11.2.0\dbhome_1\jdbc\lib ojdbc5.jar ojdbc5dms.jar ojdbc5dms_g.jar ojdbc5_g.jar ojdbc6 ...
- VBA怎样统计同一类型的数据的总和
今天是2014-11-01 是周末,忙了一周了,最终能够闲下来了.想起近期工作用到的VBA的一个场景,结合VBA的数组,所以就想试试看.结果还好.出来了.这年头,又玩起了VB了,经过多时才接受了VB的 ...
- 《Effective C++ 》学习笔记——规定10
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信
为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...
- Java EE (2) -- Java EE 6 Enterprise JavaBeans Developer Certified Expert(1z0-895)
Introduction to Java EE Gain an understanding of the Java Platform, Enterprise Edition (Java EE) Exa ...
- NYOJ710 外星人的供给站 【贪心】
外星人的供给站 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 外星人指的是地球以外的智慧生命.外星人长的是不是与地球上的人一样并不重要,但起码应该符合我们眼下对生命基 ...
- HDU-3502-Huson's Adventure Island(BFS+如压力DP)
Problem Description A few days ago, Tom was tired of all the PC-games, so he went back to some old F ...
- 使用CSS如何悬停背景颜色变色 onmouseover、onmouseout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...