链接
题解
把 a a a数组排序之后,答案就是:
∑ i = 1 n ∑ x = a i − 1 + 1 a i x ( x n − i + 1 − ( x − 1 ) n − i + 1 ) \sum_{i=1}^n \sum_{x=a_{i-1}+1}^{a_i} x ( x^{n-i+1} - (x-1)^{n-i+1} ) i=1∑nx=ai−1+1∑aix(xn−i+1−(x−1)n−i+1)
直接照着式子进行插值、求值即可
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 2000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
#define mod 1000000007ll
struct LagrangeInterpolation
{
ll _fact[maxn], inv[maxn], pref[maxn], suf[maxn];
ll calc(ll n, ll *y, ll X)
{
ll i, ans=0;
X%=mod;
inv[1]=1; rep(i,2,n)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
_fact[0]=1; rep(i,1,n)_fact[i]=_fact[i-1]*inv[i]%mod;
pref[0]=X, suf[n+1]=1;
rep(i,1,n)pref[i]=pref[i-1]*(X-i)%mod;
drep(i,n,0)suf[i]=suf[i+1]*(X-i)%mod;
rep(i,0,n)
{
ll t=suf[i+1];
if(i)(t*=pref[i-1])%=mod;
(t*=_fact[i]*_fact[n-i]%mod)%=mod;
if(n-i&1)t=-t;
(ans+=t*y[i]%mod)%=mod;
}
return ans;
}
}lagrange;
struct EasyMath
{
ll prime[maxn], phi[maxn], mu[maxn];
bool mark[maxn];
ll fastpow(ll a, ll b, ll c)
{
ll t(a%c), ans(1ll);
for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(!b){x=1,y=0;return;}
ll xx, yy;
exgcd(b,a%b,xx,yy);
x=yy, y=xx-a/b*yy;
}
ll inv(ll x, ll p) //p是素数
{return fastpow(x%p,p-2,p);}
ll inv2(ll a, ll p)
{
ll x, y;
exgcd(a,p,x,y);
return (x+p)%p;
}
void shai(ll N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
phi[1]=mu[1]=1;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
{
ll M=1, ans=0, n=a.size(), i;
for(i=0;i<n;i++)M*=m[i];
for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
return ans;
}
}em;
ll a[maxn], n, pre[maxn], y[maxn];
int main()
{
ll ans=0, i, j;
while(~scanf("%lld",&n))
{
rep(i,1,n)scanf("%lld",a+i);
sort(a+1,a+n+1);
pre[0]=1; rep(i,1,n)pre[i]=pre[i-1]*a[i]%mod;
ans=0;
rep(i,1,n)
{
rep(j,1,n-i+2)( y[j] = y[j-1] + j * ( em.fastpow(j,n-i+1,mod) - em.fastpow(j-1,n-i+1,mod) ) )%=mod;
ans += pre[i-1]*( lagrange.calc(n-i+2,y,a[i]) - lagrange.calc(n-i+2,y,a[i-1]) );
ans %= mod;
}
printf("%lld\n",(ans+mod)%mod);
}
return 0;
}