A. Little Pony and Expected Maximumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains mdots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
InputA single line contains two integers m and n (1 ≤ m, n ≤ 105).
OutputOutput a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examplesinput6 1output3.500000000000input6 3output4.958333333333input2 2output1.750000000000NoteConsider the third test example. If you've made two tosses:
- You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
- You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
- You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
- You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
【分析】
【一开始打了一个DP。。f[i]表示弄了i次的期望然后转移】
【第二个样例就错了】
↑不要说你学过概率好么?
好吧,清醒之后就知道,枚举最大值,然后算最大值是i的概率,是$i^n-(i-1)^n$,算一个小容斥吧,要求一定含一个i嘛。。
累加就好了。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 #define Maxn 100010 9 10 double qpow(double x,int b)11 {12 double ans=1;13 while(b)14 {15 if(b&1) ans=ans*x;16 x=x*x;17 b>>=1;18 }19 return ans;20 }21 22 int main()23 {24 int m,n;25 scanf("%d%d",&m,&n);26 double ans=0;27 for(int i=1;i<=m;i++)28 {29 ans+=i*(qpow(1.0*i/m,n)-qpow(1.0*(i-1)/m,n));30 }31 printf("%.5lf\n",ans);32 return 0;33 }
2017-04-21 19:44:52