D. Jon and Orbs 概率dp

概率论与动态规划求解
本文探讨了在《冰与火之歌》背景下,通过概率论和动态规划解决收集特定物品的问题。故事中,Jon Snow为了战胜异鬼,需要收集k种不同的魔球,每天在绝境长城北侧的鱼梁木下会随机出现一种魔球。文章通过建立动态规划模型dp[i][j],表示在第i天收集到j种不同魔球的概率,进而求解至少收集到每种魔球一个所需的最小天数。

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.
Examples
Input
Copy

1 1
1

Output
Copy

1

Input
Copy

2 2
1
2

Output
Copy

2
2


考虑 dp [ i ] [ j ];
表示第 i 天 有 j 种 物品的概率,
所以-------->
dp[ i ] [ j ]= (dp[ i-1 ][ j ] * j / k)+( dp[ i-1 ][ j-1 ] * ( k-j+1 )/k;)
orz , dp 我真的一点都不熟。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 19005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-7
typedef pair<int, int> pii;


inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

double dp[maxn][1002];
int k, q;

int main()
{
	//ios::sync_with_stdio(false);
	rdint(k); rdint(q);
	dp[0][0] = 1.0;
	for (int i = 1; i <= 10050; i++) {
		for (int j = 1; j <= k; j++) {
			dp[i][j] += 1.0*dp[i - 1][j] * 1.0*j / k;
			dp[i][j] += 1.0*dp[i - 1][j - 1] * 1.0*(k - j + 1) / k;
		}
	}
	while (q--) {
		int x; rdint(x);
		for (int i = 1; i <= 10050; i++) {
			if (dp[i][k] * 2000.0 > (x - eps)) {
				cout << i << endl; break;
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值