AtCoder Beginner Contest 058 D井井井 / ###【“贡献”】

来源:互联网 发布:杭州正规淘宝运营公司 编辑:程序博客网 时间:2024/06/05 08:44

思路:

对x1,x2,...xn,对于相邻两点,构成一条边,(x1,x2),(x2,x3),...,(xn-1,xn),考虑这每条边的贡献次数 (蛮好想的,靠自己吧~)×每条边的长度,就是这条边的“贡献”

同理,考虑 y 方向的每条边的“贡献”,两个相乘既是answer.

#include<bits/stdc++.h>using namespace std;typedef long long LL; const LL mod=1e9+7;const int N=1e5+10;LL a[N],b[N],n,m; int main(){    LL pre,now;    scanf("%lld%lld",&n,&m);    for(int i=0; i<n; i++)    {        scanf("%lld",&now);        if(i) a[i]=(now-pre)%mod;        pre=now;    }    for(int i=0; i<m; i++)    {        scanf("%lld",&now);        if(i) b[i]=(now-pre)%mod;        pre=now;    }    LL cnt = 1,sum1=0,sum2=0;    int Left=1,Right=n-1;    while(Left<=Right)    {        sum1=(sum1+a[Left]*cnt%mod*(n-cnt)%mod)%mod;        if(Right!=Left)            sum1=(sum1+a[Right]*cnt%mod*(n-cnt)%mod)%mod;        Right--;        Left++;        cnt=(cnt+1)%mod;    }    cnt = 1;    Left = 1;    Right = m - 1;    while(Left<=Right)    {        sum2=(sum2+b[Left]*cnt%mod*(m-cnt)%mod*sum1%mod)%mod;        if(Right!=Left) sum2=(sum2+b[Right]*cnt%mod*(m-cnt)%mod*sum1%mod)%mod;        Right--;        Left++;        cnt=(cnt+1)%mod;    }    printf("%lld\n",sum2%mod);    return 0;}


0 0
原创粉丝点击