Codeforces Round #399 (Div. 1 + Div. 2, combined) D题Jon and Orbs(dp)解题报告

本文探讨了一个基于《权力的游戏》背景的数学概率问题,即Jon Snow为击败白步兵需要收集特定数量的魔法球。文章提供了一段C++代码,通过递推公式计算了收集到所有种类魔法球所需最小天数的概率。

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\n",j);
44     }
45 }

 


转载于:https://www.cnblogs.com/quintessence/p/6432556.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值