AtCoder 2394
Posted kongbb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder 2394相关的知识,希望对你有一定的参考价值。
Problem Statement
On a two-dimensional plane, there are m lines drawn parallel to the x axis, and nlines 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 … ym
Output
Print the total area of the rectangles, modulo 109+7.
Sample Input 1
3 3 1 3 4 1 3 6
Sample 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 735867677
Sample Output 2
835067060
题意:在一个二维坐标系上,有 n 条平行 y 轴的线和 m 条平行于 x 轴的线,求这些线组成的矩形的面积和
根据组合数学的原理,可以发现每个x/y 各出现 2*i-n次,所以遍历统计一下就好了。
#include <set> #include <map> #include <queue> #include <cmath> #include <cstdio> #include <cctype> #include <vector> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll mod = 1e9+7; const double eps = 1e-7; const int maxn = 1e5+5; ll fast_pow(ll a,ll b,ll mod) ll ans = 1; while(b) if(b&1) ans = ans*a%mod; a = a*a%mod; b>>=1; return ans; int main() ll n, m; ll a, x=0, y=0; scanf("%lld%lld",&n,&m); for(int i=1;i<=n;i++) scanf("%lld",&a); x = (x+(2*i-n-1)*a)%mod; for(int i=1;i<=m;i++) scanf("%lld",&a); y = (y+(2*i-m-1)*a)%mod; printf("%lld\n",x*y%mod); return 0;
以上是关于AtCoder 2394的主要内容,如果未能解决你的问题,请参考以下文章
AtCoder Nuske vs Phantom Thnook AtCoder
AtCoder Regular Contest 092 2D Plane 2N Points AtCoder - 3942 (匈牙利算法)