【题意】
A,B 俩人 比赛, 三局两胜, 前两局A 胜, 最后一局B胜, 这是定好了的, 两个人分别取数, 取完后在放回去,大的获胜,
现在问, 在这种情况下 B的数的总和大于A的概率是多少?
【思路】
用F【】 表示 第一句 两个人所有可能的差值, S【】 表示 前两次两个人的差值的总和,
答案是 第三场大于前两场总和 += F*S ;
样本总和为 Sum*Sum*Sum; Sum=1/2 * (n*(n-1));
【代码实现】
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define SHUT ios_base::sync_with_stdio(false);cout.precision(20);cout.setf(ios::fixed);cout.tie(nullptr);cin.tie(nullptr);
const int INF=0x3f3f3f3f;
const double esp=1e-8;
typedef long long ll;
typedef long double ld;
const int MAXN=1e6+5;
using namespace std;
ll a[MAXN];
ll F[MAXN];
ll S[MAXN];
int main()
{
SHUT;
int n;
cin>>n;
mem(a,0);
mem(F,0);
mem(S,0);
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
F[a[j]-a[i]]++; // 第一局 所有差值的可能
}
}
for(int i=1;i<=5005;i++)
for(int j=1;j<=5005;j++)
{
S[i+j]+=F[i]*F[j];// 第二局 第一局的 积
}
ll sum=0;
for(int i=0;i<5005;i++)
{
for(int j=0;j<i;j++)
{
sum+=F[i]*S[j];// 所有可能
}
}
// cout<<sum<<endl;
ll Sum=((n-1)*n)/2;
ll D=Sum*Sum*Sum;
// cout<<D<<endl;
ld ans= (ld)(sum)/(D*1.0);
cout<<ans<<endl;
return 0;
}
123