Codeforces1983 E. I Love Balls(概率期望,cf2300)

题意:

在这里插入图片描述
在这里插入图片描述

解法:在这里插入图片描述
Code:
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define int long long
#define PI pair<int, int>
const int maxm=2e6+5;
// const int mod=998244353;
const int mod=1e9+7;

int n,k;
int a[maxm];
int ppow(int a,int b,int mod){
  int ans=1%mod;a%=mod;
  while(b){
    if(b&1)ans=ans*a%mod;
    a=a*a%mod;
    b>>=1;
  }
  return ans;
}
void solve() {
  cin>>n>>k;
  for(int i=1;i<=n;i++){
    cin>>a[i];
  }
  int ans[3]{0};
  // 1.普通球的期望
  // A预期(n-k+1)/2个球
  if(n-k){
    int np=n-k;
    int np1=(np+1)/2;
    int np2=np-np1;
    int w1=np1*ppow(np,mod-2,mod)%mod;
    int w2=np2*ppow(np,mod-2,mod)%mod;
    for(int i=k+1;i<=n;i++){
      ans[1]=(ans[1]+w1*a[i]%mod)%mod;
      ans[2]=(ans[2]+w2*a[i]%mod)%mod;
    }
  }
  // 2.特殊球的期望
  // 相对于把k个球放到n-k+1个空隙
  // A预期拥有其中一半向上取整的空隙,即(n-k+2)/2个空隙
  // 球进每个空隙的概率相等,即每个空隙预期k/(n-k+1)个球
  // 因此A预期[(n-k+2)/2]*[k/n-k+1]个球
  if(k){
    int cnt=n-k+1;
    int c1=(cnt+1)/2;
    int c2=cnt-c1;
    int cw=k*ppow(n-k+1,mod-2,mod)%mod;
    c1=c1*cw%mod;
    c2=c2*cw%mod;
    c1=c1*ppow(k,mod-2,mod)%mod;
    c2=c2*ppow(k,mod-2,mod)%mod;
    for(int i=1;i<=k;i++){
      ans[1]=(ans[1]+c1*a[i]%mod)%mod;
      ans[2]=(ans[2]+c2*a[i]%mod)%mod;
    }
  }
  cout<<ans[1]<<' '<<ans[2]<<endl;
}
signed main() {
#define MULTI_CASE
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("../in.txt", "r", stdin);
  freopen("../out.txt", "w", stdout);
#endif
#ifdef MULTI_CASE
  int T;
  cin >> T;
  while (T--)
#endif
    solve();
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值