1028: Multiplication
Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 547 Solved: 126
[Submit][Status][Web Board]
Description
If C=A⋄B, then
C[k]=∑max(i,j)=kA[i]⋅B[j]mod(109+7)
.
Work out sequence C=A⋄B for given sequence A and B.
Input
The first line contains a integer n, which denotes the length of sequence A,B.
The second line contains n integers a1,a2,…,an, which denote sequence A.
The thrid line contains n integers b1,b2,…,bn, which denote sequenceB.
(1≤n≤105,0≤ai,bi≤109)
Output
n integers, which denotes sequence C.
Sample Input
2 1 2 3 4
Sample Output
3 18
HINT
Source
ftiasch
http://www.acdream.net/problem.php?id=1028
O(n^2) 算法是肯定超时的
想下就可以发现这样就是可以用O(N)的算法 记录前i个数和就可以了
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100002
#define Max 1000000007
#define __int64 long long
__int64 a[N],b[N],aa[N],bb[N];
int main()
{
int n;
int i;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
aa[i]=aa[i-1]+a[i];
}
for(i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
bb[i]=bb[i-1]+b[i];
}
__int64 ans;
printf("%lld",a[1]*b[1]%Max);
for(i=2;i<=n;i++)
{
ans=(a[i]*(bb[i-1]%Max)+b[i]*(aa[i-1]%Max)+a[i]*b[i]%Max)%Max;
printf(" %lld",ans);
}
printf("\n");
}
return 0;
}