题意:
解法:![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e07ddc28ce2446108fce17c6649ffc4e.png)
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;
}