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;
}
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;
}
相关文章推荐
- CodeForces 148D 概率dp
- Codeforces 167B Wizards and Huge Prize(概率dp)
- Codeforces 148D - Bag of mice 概率dp
- Codeforces 54C First Digit Law 数位dp+概率dp
- codeforces 148D 概率DP
- Codeforces 167B Wizards and Huge Prize 概率dp
- CodeForces 148D Bag of mice 详解 (概率DP)
- codeforces-148D Bag of mice【概率dp】
- Codeforces 148D - Bag of mice 概率dp
- codeforces 28C (概率DP)
- CodeForces 351 B.Jeff and Furik(概率DP)
- CodeForces 28c Bath Queue(概率dp)
- Codeforces 482C Game with Strings(dp+概率)
- codeforces 280c 概率dp + dfs
- CodeForces 518 D. Ilya and Escalator(概率DP)
- codeforces 540D D. Bad Luck Island( 概率dp)
- CodeForces 398B Painting The Wall 概率DP
- Codeforces-148D Bag of mice (概率DP)
- CodeForces 148D Bag of Mice 概率DP
- CodeForces 464D World of Darkraft - 2 概率DP 近似计算