看土神录播秒懂,主要就是把大于b的点产生伤害的概率和小于b的点产生伤害的概率算出来,由于两部分分别概率都是一样的,可以使用前缀和去处理。
#include<bits/stdc++.h>
#define compute(x) cout<<#x<<" is "<<x<<endl
using namespace std;
typedef unsigned long long ll;
const ll mod = 998244353;
ll fast_pow(ll a,ll b){
ll ans = 1;
while(b){
if(b&1)ans = (ans * a)%mod;
a = (a * a)%mod;
b >>= 1;
}
return ans%mod;
}
ll inv(ll x){
return fast_pow(x,mod - 2)%mod;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
vector<ll> v(n);
for(int i = 0;i < n;i++)
cin>>v[i];
sort(v.begin(),v.end());
vector<ll> pref(n + 1);
for(int i = 0;i < n;i++){
pref[i + 1] = pref[i] + v[i];
}
for(int i = 1;i <= m;i++){
ll a,b;
cin>>a>>b;
int pos = lower_bound(v.begin(),v.end(),b) - v.begin();
int big = n - pos;
if(big < a){
cout<<0<<endl;
continue;
}
ll small_damage = (((big + 1 - a) * inv(big + 1))%mod * ((pref[pos])%mod))%mod;
ll big_damage = (((big - a) * inv(big))%mod * ((pref[n] - pref[pos])%mod))%mod;
cout<<(small_damage + big_damage)%mod<<endl;
}
}