Codeforces #259 DIV2 C question Little Pony and Expected Maximum (exclusion + Rapid power + formula derivation ),
Address: http://codeforces.com/contest/454/problem/C
According to the principle of rejection, a formula can be provided:
Expected P = (m ^ n-(m-1) ^ n) * m + (m-1) ^ n-(m-2) ^ n) * m-1) + ....... + (1 ^ n-0 ^ n) * 1)/m ^ n;
This formula can be easily introduced .. The power from 1 to m can be obtained using a fast power. But one problem is that the number is too big. 100000 ^ 10 ^ 5 is a number with digits .. Even if it is converted to double, it cannot be saved. What should we do at this time. You can simplify the formula. Divisor.
The formula is:
Expected P = (m/m) ^ n-(m-1)/m) ^ n) * m + (m-1)/m) ^ n-(m-2) /m) ^ n) * (m-1) + .... + (1/m) ^ n-(0/m) ^ n) * 1 ).
In this case, big data will not exist. Then the power is obtained by using the quick power. Time Complexity m ^ lgn, enough.
The Code is as follows:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;double a[110000];double quickpow(double m,int n){ double b = 1; while (n > 0) { if (n & 1) b = b*m; n = n >> 1 ; m = m*m; } return b;}int main(){ int n, m, i; double s=0, t; double ans; scanf("%d%d",&m,&n); a[0]=0; for(i=1;i<=m;i++) { a[i]=quickpow(i*1.0/m,n); //printf("%d ",a[i]); } for(i=m;i>=1;i--) { s+=(a[i]-a[i-1])*i; } //printf("%I64d\n",s); printf("%.12lf\n",s); return 0;}
Codeforces question: How can I attack others in codeforces?
For a question, you must first pass TEST. Then return to the PROBLEM list of the game, lock the lock behind the question (the lock won't be submitted again, so don't lock it if you're unsure), and then go to the ROOM, you can check other people's Code. There is a HACK button below. Click it and enter the example that you think is wrong.
How to view test data on codeforces
Enter the competition, click my submissions, and then click the RUN number under # To view the test data.