G - Card Gamehttps://vjudge.csgrandeur.cn/problem/Gym-102263G
Zeyad and Ehab are playing a simple card game, each player initially has a deck of nn cards numbered from 11 to nn, the game lasts nn turns and in each turn both players privately choose one card and then both players reveal the chosen cards, the player that chose the card with the higher number earns points equal to that number, for example if Ehab chooses 44 and Zeyad chooses 66 Zeyad earns 66 points, if the numbers chosen are equal no one earns any point, the cards chosen do not return to the deck, they are discarded instead.
Ehab doesn't care about winning or losing, he cares about mathematical values of the game, specifically he wants to know the expected number of points he'll earn considering all possible outcomes, can you help him?
Input
The first and only line of input contains a single integer n(1 \leq n \leq 10^6)n(1≤n≤106).
Output
Print a real number, the expected number of points Ehab will earn in the game.
The answer is considered correct if the absolute or relative error is not greater than 10^{-6}10−6.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 | 2.6666666667 |
Sample 2
Inputcopy | Outputcopy |
---|---|
7 | 16.0000000000 |
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n;
- scanf("%d",&n);
- double ans=0;
- for(int i=1;i<=n;i++)
- {
- ans=ans+i*1.0*(i-1)/n;
- }
- printf("%.6lf",ans);
- return 0;
- }
题解:
就是算数学期望,对于选择的i,只有对手选择1~i-1时才能得分,所以期望是对于所有i,i*(i-1)/n的和。