P5686 [CSP-SJX2019]和积和
非常简单的一道题,此博客的意义在于一个细节
(简单的式子推导略过)
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
int n;
ll ans,a[500005],b[500005],suma[500005],sumb[500005],sumc[500005],sumaa[500005],sumbb[500005];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
a[i]%=mod;
suma[i]=(suma[i-1]+a[i])%mod;
sumaa[i]=(sumaa[i-1]+suma[i])%mod;
}
for(int i=1;i<=n;i++){
scanf("%lld",&b[i]);
b[i]%=mod;
sumb[i]=(sumb[i-1]+b[i])%mod;
sumbb[i]=(sumbb[i-1]+sumb[i])%mod;
sumc[i]=(sumc[i-1]+(suma[i]*sumb[i])%mod)%mod;
}
for(int r=1;r<=n;r++){
ll sum1=(((suma[r]*sumb[r])%mod)*r)%mod;//这一行我调试了一万年,最终发现是三个数乘起来炸裂了,也告诉我每一步运算都要模一次,不然就会出锅,而且非常难发现这种失误
ll sum2=(sumaa[r-1]*sumb[r])%mod;
ll sum3=(sumbb[r-1]*suma[r])%mod;
ans=(ans+((sum1-sum2+mod)%mod-sum3+sumc[r-1]+mod)%mod)%mod;
}
printf("%lld\n",ans);
return 0;
}