Codeforces Round #399 (Div. 1 + Div. 2, combined) D题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 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.

Example

Input
1 1
1
Output
1
Input
2 2
1
2
Output
2
2

用二维数组 a i,j 表示第i天,已有j种不同的概率。很容易推得递推公式
(a i,j)= (a i-1,j )* j/k if(j>1) += (a i,j-1)* (k-j+1)/k
 1 #include <iostream>
 2 //#include<bits/stdc++.h>
 3 #include <stack>
 4 #include <queue>
 5 #include <map>
 6 #include <set>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned long long ull;
14 const double ds=0.0000001;
15 double pos[10000][1002];
16 int k,q;
17 int main()
18 {
19     scanf("%d %d",&k,&q);
20     memset(pos,0,sizeof(pos));
21     pos[1][1]=1.0;
22     int i,j;
23     int tem;
24     for(i=2;;i++)
25     {
26         for(j=1;j<=k;j++)
27         {
28             pos[i][j]=pos[i-1][j]*(double)j/(double)k;
29             if(j>1)
30                 pos[i][j]+=pos[i-1][j-1]*(double)(k-j+1)/(double)k;
31         }
32         if(pos[i][k]>=0.5)
33             break;
34     }
35     for(i=1;i<=q;i++)
36     {
37         scanf("%d",&tem);
38         for(j=1;;j++)
39         {
40             if(pos[j][k]*2000.0-(double)tem>=-ds)
41                 break;
42         }
43         printf("%d
",j);
44     }
45 }

原文地址:https://www.cnblogs.com/quintessence/p/6432556.html