您的位置:首页 > 其它

codeforces 768D Jon and Orbs (概率dp)

2017-02-21 16:53 405 查看
题目原文: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: