EOJ Mothly 2020.3 D.钢琴演奏家

 

 思路:

当时比赛的时候思路正确,可是就是超时。

 代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const int mod = 1e9+7;
ll a[maxn];
ll qpow(ll a,ll b){
    ll ans = 1;
    while(b){
        if(b&1) ans=ans*a%mod;
        a = a*a%mod;
        b>>=1; 
    }
    return ans;
} 
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        sort(a+1,a+1+n);
        ll ans = a[m];
        ll p=1;
        for(int i=m+1;i<=n;i++){
            p = p*(i-1)%mod*qpow(i-m,mod-2)%mod; 
            ans=(ans+a[i]*p%mod)%mod;
        }
        cout<<ans<<endl; 
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/lusiqi/p/12536101.html