Kids and Prizes(SGU 495)

495. Kids and Prizes

Time limit per test: 0.25 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

ICPC (International Cardboard Producing Company) is in the business of producing cardboard boxes. Recently the company organized a contest for kids for the best design of a cardboard box and selected M winners. There are Nprizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process will be as follows:

  • All the boxes with prizes will be stored in a separate room.
  • The winners will enter the room, one at a time.
  • Each winner selects one of the boxes.
  • The selected box is opened by a representative of the organizing committee.
  • If the box contains a prize, the winner takes it.
  • If the box is empty (because the same box has already been selected by one or more previous winners), the winner will instead get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course).
  • Whether there is a prize or not, the box is re-sealed and returned to the room.

The management of the company would like to know how many prizes will be given by the above process. It is assumed that each winner picks a box at random and that all boxes are equally likely to be picked. Compute the mathematical expectation of the number of prizes given (the certificates are not counted as prizes, of course).

Input

The first and only line of the input file contains the values of N and M (Kids and Prizes(SGU 495)).

Output

The first and only line of the output file should contain a single real number: the expected number of prizes given out. The answer is accepted as correct if either the absolute or the relative error is less than or equal to 10-9.

Example(s)
sample input
sample output
5 7
3.951424
sample input
sample output
4 3
2.3125

两种方法:

1.设A = “所有奖品都不被取到”, P(A) = ((n - 1) / n) ^ m;

E(A) =  n * P(A);

E(!A) = n - E(A);

2.P(i) 表示第i个人拿到奖品的概率;

P(i) = P(i - 1) * (1 - P(i - 1)) + P(i - 1) * (P(i - 1) - 1 / n);

E = ∑(P(i) * 1);

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 100005 double d[MAXN];
int main()
{
double n, m;
while(~scanf("%lf%lf", &n, &m))
{
d[] = 1.0;
for(int i = ; i <= (int)m; i++)
d[i] = (1.0 - d[i - ]) * d[i - ] + d[i - ] * (d[i - ] - 1.0 / n); double e = 0.0;
for(int i = ; i <= (int)m; i++) e += d[i];
printf("%.10lf\n", e);
} return ;
}
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 1005 int main()
{
double n, m;
while(~scanf("%lf%lf", &n, &m))
printf("%.9lf\n", n - n * pow((n - ) / n, m));
return ;
}
上一篇:C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案


下一篇:jQuery之回到顶部