codeforces 768D Jon and Orbs (概率dp)

题目原文:Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least , where ε < 10 - 7.

To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help.

Input

First line consists of two space separated integers k, q (1 ≤ k, q ≤ 1000) — number of different kinds of orbs and number of queries respectively.

Each of the next q lines contain a single integer pi (1 ≤ pi ≤ 1000) — i-th query.

Output

Output q lines. On i-th of them output single integer — answer for i-th query.

题目大意:总共有k种物品,每天产生一种物品(等可能)。问至少需要几天才能保证收集齐K种物品的概率超过 (p - 1e-7)/2000。

解题思路:题目所求的概率具有很好的递推性,状态和状态转移也很清楚,所以使用概率dp。

                状态: dp[i][j] 表示前 i 天总共得到了 j 种物品。

                状态转移: dp[i][j] =  (k-j+1)/k * dp[i-1][j-1] + j /k * dp[i-1][j];

                根据题意可以知道要保证最后的概率至少要打过1/2,所以可以试一下所需的最大天数,经测试8000可以满足题意。

AC代码:

#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 2000
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int n,m;

double dp[12000][1005];
vector<double> ans;
int main()
{
    //freopen("test.txt","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int k,q;
    cin>>k>>q;
    dp[0][0] = 1;
    for(int i = 1; i<=8000; i++)
    {
        for(int j = 1; j<=k; j++)
            dp[i][j] = (1.0 * (k-j+1))/k * dp[i-1][j-1] + (1.0 * j)/k * dp[i-1][j];
    }
    //cout << dp[8000][1000] << endl;
    for(int i = 1; i<=8000; i++)
        ans.push_back(dp[i][k]);
    while(q--)
    {
        int p;
        cin>>p;
        double limit = (p*1.0 - (1e-7))/2000;
        cout << lower_bound(ans.begin(),ans.end(),limit) - ans.begin() + 1 << endl;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值