Problem Description
On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis. Among the lines parallel to the x axis, the i-th from the bottom is represented by y=yi. Similarly, among the lines parallel to the y axis, the i-th from the left is represented by x=xi.
For every rectangle that is formed by these lines, find its area, and print the total area modulo 109+7.
That is, for every quadruple (i,j,k,l) satisfying 1≤i<j≤n and 1≤k<l≤m, find the area of the rectangle formed by the lines x=xi, x=xj, y=yk and y=yl, and print the sum of these areas modulo 109+7.
Constraints
- 2≤n,m≤105
- −109≤x1<…<xn≤109
- −109≤y1<…<ym≤109
- xi and yi are integers.
Input
Input is given from Standard Input in the following format:
n m
x1 x2 … xn
y1 y2 … ymOutput
Print the total area of the rectangles, modulo 109+7.
Example
Sample Input 1
3 3
1 3 4
1 3 6Sample Output 1
60
The following figure illustrates this input:
The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.
Sample Input 2
6 5
-790013317 -192321079 95834122 418379342 586260100 802780784
-253230108 193944314 363756450 712662868 735867677Sample Output 2
835067060
题意:在一个二维坐标系上,有 n 条平行 y 轴的线和 m 条平行于 x 轴的线,求这些线组成的矩形的面积和
思路:
根据题意,可以推出:
化简后有:
那么问题就转化成求 和
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
LL x[N],y[N];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lld",&x[i]);
for(int j=1;j<=m;j++)
scanf("%lld",&y[j]);
LL sx=0;
for(int i=1;i<=n;i++){
sx=(sx+x[i]*(i-1))%MOD;
sx=(sx-x[i]*(n-i))%MOD;
}
LL sy=0;
for(int i=1;i<=m;i++){
sy=(sy+y[i]*(i-1))%MOD;
sy=(sy-y[i]*(m-i))%MOD;
}
LL res=(sx*sy)%MOD;
printf("%lld\n",res);
return 0;
}