区间权和

区间权值

https://ac.nowcoder.com/acm/problem/19798

代码如下:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
LL a[maxn], w[maxn], f[maxn], g[maxn], ans = 0;
int main()
{
    int n;
    cin >> n;
    g[0] = f[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        f[i] = a[i] + f[i - 1];
        g[i] = (f[i] + g[i - 1]) % mod;
    }
    for(int i = 1; i <= n; i++)
        cin >> w[i];
    for(int i = 1; i <= n; i++)
        ans = (ans + (g[n] - g[n - i] - g[i - 1]) * w[i] % mod) % mod;
    cout << (ans + mod) % mod << endl;
    return 0;
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务