称号: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的更多相关文章

  1. SRM 514 DIV1 500pt(DP)

    题目简述 给定一个H×W大小的矩阵,每个格子要么是1~9中的一个数,要么是".",要求你把“.”填成具体的数字(1~9),并且符合以下两个要求: 对于所有的整数r 和 c( 0 & ...

  2. SRM 511 DIV1 500pt(DP)

    题目简述 给定n个数,两个人轮流取数,和之前两个人的取的数或起来,谁不能取数或者谁取到的数和之前的数或值为511谁输,问谁能够赢? 题解 刚开始的想法是直接搜,不过需要记录取过的值的状态,2^50显然 ...

  3. SRM 508 DIV1 500pt(DP)

    题目简述 给定一个大小为 n的序列(n<=10)R,要求你计算序列A0, A1, ..., AN-1的数量,要求A序列满足A0 + A1 + ... + AN-1 = A0 | A1 | ... ...

  4. SRM 509 DIV1 500pt(DP)

    题目简述 给定一个字符串,可以对其进行修改,删除,增加操作,相应的操作有对应的花费,要求你用最小的花费把字符串变为回文串 题目做法 先搞一遍floyed把各种操作的最小花费求出来,然后就是类似编辑距离 ...

  5. SRM 502 DIV1 500pt(DP)

    题目简述 给定比赛时间T和n个题目,你可以在任意时间提交题目,每个题目有一个初始分数maxPoints[i],每个单位时间题目的分数将会减少pointsPerMinute[i],即如果在时间t解决了第 ...

  6. SRM 501 DIV1 500pt(DP)

    题目简述 给定一个长度为n的序列,每个数值的范围为[-1,40],-1可以替换成0~40之间的数,要求你求出符合以下条件的序列有多少个? 1.每个数都是0~40之间的数 2.对于每一个数A[i],都需 ...

  7. UVA 620 Cellular Structure (dp)

     Cellular Structure  A chain of connected cells of two types A and B composes a cellular structure o ...

  8. SRM 620 DIV1 L2

    题意:有n个等长的string(设string的长度为m),string中的字符从'A'到'Z',容许对m列执行稳定的排序操作,问说是否能通过这m种操作将这n个string调整成对应的顺序. 题解: ...

  9. topcoder srm 620 div1

    problem1 link 分别计算可以得到(a,b)的有哪些二元组,以及可以得到(c,d)的有哪些二元组.然后在公共的二元组中找到和最大的即可. problem2 link 设最后的排序为$r=[2 ...

随机推荐

  1. hdu5171(矩阵快速幂)

    传送门:GTY's birthday gift 题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法 ...

  2. C语言数组

    在C语言中,对于三维或三维以上数组的使用并没有很好的支持,而且使用率也非常的低,后面会对三维数组做一些简单的分析,这篇文章主要以二维数组来探讨一些C语言中数组使用的相关概念和技巧. 1 一个var[i ...

  3. SharePoint 2013的HTML5特性之响应式布局

    今天偶然看到一本书<Pro SharePoint 2013 Branding and Responsive Web Development>,看到SharePoint 2013基于HTML ...

  4. unity3D的FingerGestures小工具

    夹 FingerGestures包结构 FingerGestures样例列表 设置场景 教程:识别一个轻敲手势 教程:手势识别器 教程:轻击手势识别器 教程:拖拽手势识别器 教程:滑动手势识别器 教程 ...

  5. Android 网络编程 Socket Http

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  6. SE 2014年4月2日

    一 描述OSPF协议 LSA(Type 1~5)的名称,始发者以及特点 第一类LSA (router lsa)该类lSA为启动了ospf进程的所有路由器都可以产生,该类LSA主要含有本地路由器的接口状 ...

  7. Mac下配置Cocos2d-x3.1环境

    一.前期准备 1.ADT:百度下就OK 2.NDK:百度下就OK 3.ANT: http://124.254.47.39/download/55152992/78533365/4/zip/57/132 ...

  8. getline与get函数的区别

    get()函数相对getline来说使用方法要灵活的多了. 1.   int get()是指从流中抽取单个字符并返回,这个是没有參数的形式.由于c++不像c语言使用getchar() 2.istrea ...

  9. 向日葵sunlogin配置

    客户端配置: xxxx@TIM sunlogin_linux_1.0.0.25020]$ lsbin  html  install_sunlogin.sh  readme.txt  script  u ...

  10. 天翼玩家wifi,鸡肋or神器?

    昨天,天一在成都,一个举行4G体验活动.谁是背着一个婴儿每一翼4G MiFi终奌站.市民可进入用户password自由的直接经验wifi互联网. 天翼随身wifi是什么? 这样的4G MiFi就是天翼 ...