区间权值 前缀和

链接:https://www.nowcoder.com/acm/contest/204/G
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld

题目描述

小 Bo 有 n 个正整数 a1..an,以及一个权值序列 w1…wn,现在他定义
现在他想知道 的值,需要你来帮帮他
你只需要输出答案对 109+7 取模后的值

输入描述:

第一行一个正整数 n
第二行 n 个正整数 a1..an
第三行 n 个正整数 w1..wn

输出描述:

输出答案对 109+7 取模后的值

示例1

输入

复制

3
1 1 1
1 1 1

输出

复制

10

备注:

1≤ n≤ 3x 105
1≤ ai≤ 107
1≤ wi≤ 107

题解地址

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
//#define mod 998244353
#define INF 0x3f3f3f3f
#define eps 1e-6
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=3e5+10;
ll w[maxn];
ll a[maxn];
ll f[maxn];//a[i]的前缀和
ll g[maxn];//g[i]的前缀和
int main()
{
    int n;
    scanf("%d",&n);
    a[0]=0;f[0]=0;

    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        f[i]=(f[i-1]+a[i])%mod;
    }

    for(int i=1;i<=n;i++)
        scanf("%lld",&w[i]);

    g[0]=0;
    for(int i=1;i<=n;i++)
        g[i]=(f[i]+g[i-1])%mod;

    ll ans=0;
    for(int i=1;i<=n;i++)
        ans=(ans+(g[n]-g[n-i]-g[i-1])*w[i]%mod)%mod;

    printf("%lld\n",(ans+mod)%mod);
}

猜你喜欢

转载自blog.csdn.net/qq_40507857/article/details/82937473