码迷,mamicode.com
首页 > 其他好文 > 详细

ACdream 1029 前缀和

时间:2016-05-29 00:57:08      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

Multiplication

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Problem Description

If C=A?B, then

C[k]=max(i,j)=kA[i]B[j]mod(109+7)

.

 

Work out sequence C=A?Bfor 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 nn integers a1,a2,,ana1,a2,…,an , which denote sequence A.

The thrid line contains nn integers b1,b2,,bnb1,b2,…,bn , which denote sequenceB.

(1n105,0ai,bi109)

Output

nn integers, which denotes sequence C.

Sample Input

2
1 2
3 4

Sample Output

3 18 

Source

ftiasch

Manager

 
题意:  长度为n的a,b序列   求C[k]=max(i,j)A[i]B[j]mod(109+7)
 
题解:  max(i,j) 可以看出 相乘的两个数中一定有a[n]或者b[n]
          前缀和处理
 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<stack>
 6 #define ll long long 
 7 #define pi acos(-1.0)
 8 #define mod 1000000007
 9 using namespace std;
10 int n;    
11 ll a[100005];
12 ll b[100005];
13 ll suma[100005];
14 ll sumb[100005];
15 ll ans;
16 ll gg[100005];
17 int main()
18 {
19     while(scanf("%d",&n)!=EOF)
20     {
21         suma[0]=0;
22         sumb[0]=0;
23         a[0]=0;
24         b[0]=0;
25         for(int i=1;i<=n;i++)
26         {
27             scanf("%d",&a[i]);
28             suma[i]=(suma[i-1]+a[i])%mod;
29         }
30             for(int i=1;i<=n;i++)
31         {
32             scanf("%d",&b[i]);
33             sumb[i]=(sumb[i-1]+b[i])%mod;
34         }
35         ans=0;
36         for(int i=1;i<=n;i++)
37         {
38           ans=((a[i]*b[i]%mod+a[i]*sumb[i-1]%mod+b[i]*suma[i-1]%mod))%mod;
39           gg[i]=ans;
40         }
41         printf("%lld",gg[1]);
42         for(int i=2;i<=n;i++)
43          printf(" %lld",gg[i]);
44          cout<<endl; 
45     }
46     return 0;
47 } 

 

 
 

ACdream 1029 前缀和

标签:

原文地址:http://www.cnblogs.com/hsd-/p/5538624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!