原计算公式为:
\[\sum\limits_{1\le i<j\le n}\sum\limits_{1\le k<l\le m}(x_j-x_i)(y_l-y_k) \]可将xy拆分:
\[\left(\sum\limits_{1\leq i<j\leq n}(x_j-x_i)\right)\left(\sum\limits_{1\leq k<l\leq m}(y_l-y_k)\right) \]仅计算x侧可进一步化简:
\[\sum\limits_{1\le i<j\le n}(x_j-x_i)=\sum\limits_{1\le k\le n}((k-1)x_k-(n-k)x_k) \]// https://atcoder.jp/contests/abc058/tasks/arc071_b
// 公式化简
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10, mod = 1e9 + 7;
int n, m;
int x[N], y[N];
LL qpow(LL a, LL k)
{
LL res = 1;
a %= mod;
while (k)
{
if (k & 1) res = res * a % mod;
k >>= 1;
a = a * a % mod;
}
return res;
}
LL C(LL n, LL m)
{
if (m < 0 || m > n) return 0;
if (m > n / 2) return C(n, n - m);
LL res = 1;
LL t = 1;
for (int i = n, j = 1; j <= m; j ++, i --)
{
res = res * i % mod;
t = t * j % mod;
}
return res * qpow(t, mod-2) % mod;
}
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++) cin >> x[i];
for (int i = 1; i <= m; i ++) cin >> y[i];
sort(x+1, x+1+n); sort(y+1, y+1+m);
LL xx = 0, yy = 0;
for (LL i = 1; i <= n; i ++) xx = (xx + (i*2 - 1 - n) * x[i] % mod) % mod;
for (LL i = 1; i <= m; i ++) yy = (yy + (i*2 - 1 - m) * y[i] % mod) % mod;
LL ans = xx * yy % mod;
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
while (T --)
{
solv();
}
return 0;
}
标签:return,abc058d,int,res,LL,sum,mod
From: https://www.cnblogs.com/o2iginal/p/17499345.html