UVALive-8138 Number Generator 概率dp+优化

题目链接:https://cn.vjudge.net/problem/UVALive-8138

题意

有一个随机数生成器,输出1~n的整数。

现在已经输出了k个数,问再取几个数才能使取出的所有数的个数至少为2。

注意T<=1e5, \sum k<=1e5

思路

(听说存在公式?理论上说有了转移方程和边界,公式就是存在

概率dp,注意状态的选取。

设i为出现0次的数的个数,j为出现1次的数的个数。

\[
\begin{align*}
dp(i, j) &= \frac{i}{n}[dp(i-1, j+1)+1]+\frac{j}{n}[dp(i, j-1)+1]+\frac{n-i-j}{n}[dp(i, j)+1] \\
dp(i, j) &= \frac{i}{i+j}dp(i-1, j+1)+\frac{j}{i+j}dp(i, j-1)+\frac{n}{i+j}
\end{align*}
\]

$ dp(0, 0)=0 $

实际上,n是可以提出来的,这一点还请注意啊。

提交过程

TLE 状态没选对,导致n没提出来
AC

代码

#include <cstdio>
#include <cstring>
const int maxn=3e3+20;
const int INF=0x3f3f3f3f;
double data[maxn][maxn];
int n, k;
double dp(int i, int j){
    if (i==0 && j==0) return 0;
    if (data[i][j]>0) return data[i][j];

    data[i][j]=1;
    if (i>=1) data[i][j]+=i*dp(i-1, j+1);
    if (j>=1) data[i][j]+=j*dp(i, j-1);
    data[i][j]/=(double)(i+j);
    return data[i][j];
}

int main(void){
    int T, tmp;

    scanf("%d", &T);
    while (T--){
        scanf("%d%d", &n, &k);

        int vis[maxn]={0};
        for (int i=0; i<k; i++){
            scanf("%d", &tmp);
            vis[tmp]++;
        }

        int cnt_1=0, cnt_0=0;
        for (int i=1; i<=n; i++){
            if (vis[i]==1) cnt_1++;
            else if (vis[i]==0) cnt_0++;
        }

        printf("%.6f\n", n*dp(cnt_0, cnt_1));
    }

    return 0;
}
Time Memory Length Lang Submitted
449ms None 827 C++ 5.3.0 2018-08-28 13:23:33

原文地址:https://www.cnblogs.com/tanglizi/p/9551626.html

时间: 2024-08-30 00:57:54

UVALive-8138 Number Generator 概率dp+优化的相关文章

UVALive 6672 Bonus Cards 概率dp

题意呢 就是有两种售票方式 一种是icpc 一种是其他方式 icpc抢票成功的概率是其他方式的2倍…… 这时 一个人出现了 他通过内幕知道了两种抢票方式各有多少人 他想知道自己如果用icpc抢票成功的概率是多少 用acm抢票成功的概率是多少…… 做过不多的概率dp 还在摸索…… dp[i][j]代表第i轮有j个icpc的人已经有票了…… 当然同时i-j个通过其他方式抢票的人也有票了 这就是用同样的函数搜两次的原理…… 优化一次i<=a 一次是把初始化放到for里…… 第一次见这么卡时间的题……

UVALive 2522 Chocolate(概率DP)

思路:定义DP方程dp[i][j]标记选到第i个巧克力的时候,桌面上还剩下j个巧克力,状态转移有两个方向,dp[i-1][j-1],dp[i-1]lj+1],分别表示桌面上多了一个和消了一个,乘上需要的概率即可. 注意:这个题目的输入量很大,所以需要优化,首先是n+m是奇数的时候,或者m > c的时候概率是0. 然后就是当n很大的时候,可以知道后面所加的概率越来越小,题目要求精确到三位小数,所以大约1500以后的值就不会影响答案了,可以直接去掉.如果没有这两个优化,很容易超时.dp过程并不难,难

Codeforces 498B. Name That Tune 概率DP+优化

dp[i][j] 第i首歌在第j分钟听出来..... 一般情况下: dp[i][j]= dp[ i-1] [ j-k ] * p[i] * (1-p[i])^(k-1) 当k==t[i]时,一定可以听出来还要另加上 dp[ i-1] [ j-k ]*(1-p[i])^k 需要维护一段dp[i-1][k]的和,将时间复制度降到O(n^2) B. Name That Tune time limit per test 1 second memory limit per test 256 megabyt

13年山东省赛 The number of steps(概率dp水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro

sdut2623--The number of steps(概率dp第一弹)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

[2013山东ACM省赛] The number of steps (概率DP,数学期望)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

UVALive 6514:Crusher’s Code(概率dp)

题目链接 https://icpcarchive.ecs.baylor.edu/external/65/6514.pdf 题意:给出n个数(n<8) 求这n个数分别两个程序排成有序时,程序的期望迭代次数.排序程序如下. // Monty's Code while (!sorted(a)) { int i = random(n) ; int j = random(n) ; if (a[min(i,j)] > a[max(i,j)]) swap(a[i], a[j]) ; } //Carlos's

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

LightOJ1287---Where to Run (概率dp)

Last night you robbed a bank but couldn't escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of some junctions which are connected by some bidirectional roads. Since police is behind, you