湖南省第十四届程序设计大赛K题CSU 2173:useFFT(前缀和)

题目链接:http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2173
Description
Bobo computes the product P(x)⋅Q(x)=c0 + c1x + … + cn + mxn + m for two polynomials P(x)=a0 + a1x + … + anxn and Q(x)=b0 + b1x + … + bmxm. Find (cL + cL + 1 + … + cR) modulo (109 + 7) for given L and R.

1 ≤ n, m ≤ 5 × 105
0 ≤ L ≤ R ≤ n + m
0 ≤ ai, bi ≤ 109
Both the sum of n and the sum of m do not exceed 106.
Input
The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains four integers n, m, L, R.

The second line contains (n + 1) integers a0, a1, …, an.

The third line contains (m + 1) integers b0, b1, …, bm.

Output
For each test case, print an integer which denotes the reuslt.

Sample Input
1 1 0 2
1 2
3 4
1 1 1 2
1 2
3 4
2 3 0 5
1 2 999999999
1 2 3 1000000000
Sample Output
21
18
5
Hint
Source
2018湖南省第14届大学生计算机程序设计竞赛

Author
ftiasch

思路:看到这题标题就很显然了,好吧,让我们来用FFT解题吧! 。好吧,其实我也不会FFT,那么这题主要是我们要找出这么一个关系:
在这里插入图片描述
所以我们只要对b求前缀和,用a1-an去乘它就好啦~

代码:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
ll a[500050];
ll b[500050];
int main()
{
    int n,m,l,r;
    while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF)
    {
        for(int i=0;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=0;i<=m;i++)
            scanf("%lld",&b[i]);
        ll num=0;
        for(int i=l;i<=r&&i<=m;i++)
            num=(num+b[i])%mod;
        ll ans=0;
        for(int i=0;i<=n;i++)
        {
            ans=(ans+a[i]*num)%mod;
            l--;
            if(l>=0&&l<=m) num=(num+b[l])%mod;
            if(r>=0&&r<=m)
                 num=(num+mod-b[r])%mod;
            r--;
        }
        printf("%lld\n",ans);
    }
}
展开阅读全文

没有更多推荐了,返回首页