TaoSama's Microcosm

本博客不再更新,新博客地址: http://taosama.github.io

LightOJ 1050 - Marbles (概率dp)

题意:

r<=500,b<=500,,b
,b,

分析:

dp[r][b]:=,rb,,

代码:

//
//  Created by TaoSama on 2015-11-20
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int r, b;
double dp[505][505];

double dfs(int r, int b) {
    double &ret = dp[r][b];
    if(ret >= 0) return ret;
    if(r == 0) return ret = 1;
    if(b == 0) return ret = 0;
    int sum = r + b;
    ret = 1.0 * r / sum * dfs(r - 1, b - 1); //I take red
    if(b > 1) ret += 1.0 * b / sum * dfs(r, b - 2); //I take blue
    return ret;
}

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    int kase = 0;
    fill(dp[0], dp[0] + 505 * 505, -1.0);
    while(t--) {
        scanf("%d%d", &r, &b);
        printf("Case %d: %.10f\n", ++kase, dfs(r, b));
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwt36/article/details/49948877
文章标签: dp
个人分类: LightOJ
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

LightOJ 1050 - Marbles (概率dp)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭