标签:
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 5 const long long p=1e9+7; 6 long long a[100100],fac[100100],ans; 7 int n,t; 8 9 long long power(long long a,long long b){ 10 if(a<=1)return a; 11 if(b==0)return 1; 12 if(b==1)return a; 13 long long tmp=power(a,b>>1); 14 tmp=(tmp*tmp)%p; 15 if(b&1==1)tmp=(tmp*a)%p; 16 return tmp; 17 } 18 19 long long C(int m,int n){ 20 if(m==0)return 1; 21 if(n<0)return 0; 22 long long tmp=(fac[n]*power(fac[m],p-2))%p; 23 tmp=(tmp*power(fac[n-m],p-2))%p; 24 return tmp; 25 } 26 27 int main(){ 28 scanf("%d",&t); 29 int i,j,s,k,l; 30 fac[0]=1; 31 for(i=1;i<=100000;i++)fac[i]=(fac[i-1]*i)%p; 32 for(l=1;l<=t;l++){ 33 ans=0; 34 scanf("%d",&n); 35 for(i=1;i<=n;i++)scanf("%I64d",&a[i]); 36 a[n+1]=0; 37 sort(a+1,a+1+n); 38 s=k=0; 39 long long div=1; 40 for(i=1;i<=n;i++){ 41 ++k; 42 if(a[i]!=a[i+1]){ 43 long long tmp,ss=0; 44 tmp=(fac[s]*fac[k])%p; 45 tmp=(tmp*fac[n-s-k])%p; 46 tmp=(tmp*C(s,n))%p; 47 for(j=1;j<=k;j++)ss+=(((a[i]*j)%p)*C(k-j,n-s-j-1))%p; 48 ss%=p; 49 ans+=(ss*tmp)%p; 50 ans%=p; 51 div=(div*fac[k])%p; 52 s+=k; 53 k=0; 54 } 55 } 56 ans=(ans*power(div,p-2))%p; 57 printf("Case #%d: %I64d\n",l,ans); 58 } 59 return 0; 60 }
标签:
原文地址:http://www.cnblogs.com/Randolph87/p/5199439.html