Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs

地址:http://codeforces.com/contest/768/problem/D

题目:

D. Jon and Orbs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 kq (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.

Examples
input
1 1
1
output
1
input
2 2
1
2
output
2
2

 思路:

  概率dp,dp[i][j]表示i天后获得j种不同魔法球的概率

    dp[i][j]=dp[i-1][j]*(k-j)/k+dp[i][j-1]*(k-j+1)/k;

  因为概率至少大于0.5,所以不超过10000天。

  因为有多次询问所以需要先预处理出所有答案。

  参考:http://blog.csdn.net/johsnows/article/details/56289552

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-7;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
13 
14 double dp[1050];
15 int x,q,k,ans[1050];
16 int main(void)
17 {
18     cin>>k>>q;
19     dp[0]=1;
20     for(int i=1;x<=1000;i++)
21     {
22         for(int j=k;j;j--)
23             dp[j]=dp[j]*j/k+dp[j-1]*(k-j+1)/k;
24         while(x<=1000 && dp[k]*2000>=x-eps)
25             ans[x++]=i;
26         dp[0]=0;
27     }
28     while(q--)
29         scanf("%d",&x),printf("%d\n",ans[x]);
30     return 0;
31 }

 

posted @ 2017-02-21 22:07  weeping  阅读(150)  评论(0编辑  收藏  举报