So I thought for a long time in this two-forced exam, I was so weak ...
First, since ANS is i* (i-1)/2, all that is required is the weight of each number of possible occurrences * of this number.
We find that the essence of each number is the same, we remember a sum of sums, so as long as the statistics are OK.
We abstract each selection into an edge, each state as a point, thus constituting a root tree.
We only consider the 1 contribution to the answer. To calculate the contribution of the current merge to the answer at each level, that is, to know that I have chosen to merge 1 or 1 of the unicom blocks in this node, how many leaf nodes can I cover?
Then it becomes an O (n) combinatorial math problem. The factorial and factorial inverse of the combined number can be preprocessed by O (n).
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #definell Long Long7 intans=0, a[200005],n,cnt[ -],size[ -];8 Constll mod=1000000007;9ll sum,jcny[200005],jc[200005],f[200005],son[200005],num[200005];Ten intRead () { One intt=0, f=1;CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} - returnt*F; the } - BOOLcmpConst int&a,Const int&b) { - returnA>b; - } + ll gcd (ll A,ll b) { - if(b==0)returnA; + Else returnGCD (b,a%b); A } at voidEXGCD (ll a,ll b,ll &x,ll &y) { - if(b==0){ -x=1; y=0; - return; - } -EXGCD (b,a%b,x,y); inll t=x; -x=y; toY=t-(A/b) *y; + } - voidinit () { thejc[0]=1; jcny[0]=1; * for(LL i=1; i<=n+1; i++) jc[i]= (jc[i-1]*i)%Mod; $ ll x, y;Panax NotoginsengEXGCD (jc[n+1],mod,x,y); -jcny[n+1]= (x%mod+mod)%Mod; the for(inti=n;i>=1; i--) jcny[i]= (jcny[i+1]* (i+1))%Mod; + } All C (intNintm) { the return(((jc[n]*jcny[m)%mod) *jcny[n-m])%Mod; + } - intDfsintX1,intX2,intX3,intX4,intX5,intNumintRes) { $ intb[5]; $ intres=1; - if(num==1){ -ans= (ans+res)%Mod; the returnRes; - }Wuyib[0]=x1;b[1]=x2;b[2]=x3;b[3]=x4;b[4]=X5; theStd::sort (b,b+num,cmp); - for(intI=0; i<num;i++) Wu for(intj=i+1; j<num;j++){ -B[I]+=B[J];inttmp=b[j];b[j]=0; Std::swap (b[j],b[num-1]); AboutRes+=dfs (b[0],b[1],b[2],b[3],b[4],num-1, (Res+b[i])%Mod); $Std::swap (b[j],b[num-1]); -b[i]-=tmp;b[j]=tmp; - } - returnRes; A } + LL INV (ll a) { the ll x, y; - EXGCD (a,mod,x,y); $ returnx; the } the voidSbpianfen () { the intK=dfs (a[1],a[2],a[3],a[4],a[5],n,0); theprintf"%d\n", ans); - } inll A (intNintm) { the return(Jc[n]*jcny[n-m])%Mod; the } About ll Pow (ll X,ll y) { thell res=1; the while(y) { the if(y%2) res= (res*x)%Mod; +x= (x*x)%Mod; -Y/=2; the }Bayi returnRes; the } the voidSxpianfen () { -ll res=0; -son[1]=1; son[0]=1; the for(intI=2, num=n;i<=n;i++,num--) theSon[i]= (son[i-1]*c (NUM,2))%mod;//This line thenum[n-1]=1; num[n]=1; num[n+1]=1; num[n+2]=1; the for(inti=n-2, num=2; i>=1; I--, num++) -Num[i]= (num[i+1]*c (num+1,2))%mod;//leaves the for(intI=1, num=n;i<=n-1; i++,num--){ theRes= (res+ (son[i]*num[i+1])%mod* (num-1))%Mod; the }94res= (res*sum)%Mod; theprintf"%lld\n", res); the } the intMain () {98n=read (); Init (); About for(intI=1; i<=n;i++) -A[i]=read (), sum= (Sum+a[i])%Mod;101 if(n<=5) {Sbpianfen ();return 0;}102 if(n<=100000) {Sxpianfen ();return 0;}103}
Noi 10 even test DAY3 T1