He is Flying
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 328 Accepted Submission(s): 93
Problem Description
JRY wants to drag racing along a long road. There are
n
sections on the road, the
i
-th section has a non-negative integer length
si
. JRY will choose some continuous sections to race (at an unbelievable speed), so there are totally
n(n+1)2
different ways for him to ride. If JRY rides across from the
i
-th section to the
j
-th section, he would gain
j−i+1
pleasure. Now JRY wants to know, if he tries all the ways whose length is
s
, what's the total pleasure he can get. Please be aware that in the problem, the length of one section could be zero, which means that the length is so trivial that we can regard it as
0
.
Input
The first line of the input is a single integer
T (T=5)
, indicating the number of testcases.
For each testcase, the first line contains one integer n . The second line contains n non-negative integers, which mean the length of every section. If we denote the total length of all the sections as s , we can guarantee that 0≤s≤50000 and 1≤n≤100000 .
For each testcase, the first line contains one integer n . The second line contains n non-negative integers, which mean the length of every section. If we denote the total length of all the sections as s , we can guarantee that 0≤s≤50000 and 1≤n≤100000 .
Output
For each testcase, print
s+1
lines. The single number in the
i
-th line indicates the total pleasure JRY can get if he races all the ways of length
i−1
.
Sample Input
2 3 1 2 3 4 0 1 2 3
Sample Output
0 1 1 3 0 2 3 1 3 1 6 0 2 7
Author
XJZX
Source
题意:
有n段路线A[i](长度可能相同),某位runner可以任意选择连续段(i -> i+1)路线跑,如果从i-.......-> j 那么,能获得 j-i+1的欢乐值。
问,长度为L能获得的欢乐值是多少。(0<=L<=total(总长))
母函数&FFT
对长度预处理前缀和sum。
对于长度L=sum[i]-sum[i-1]
能获得欢乐值为 sigma(i*x^(sum[i]))*sigma(1*x^(-sum[i-1]))-sigma(x^(sum[i]))*sigma((i-1)*x^(-sum[i-1]))
前面一个乘积表示长度为L,以i结束。
后面一个乘积表示长度为L,以i-1开始。
幂代表的是长度,系数代表的是情况数。
通过FFT做卷积就可以的书答案了。
技巧处理,total-sum[i-1],统一移位。
长度为0特殊处理。
/**********************************************************
* 2015多校He is Fly 题意:n段路,任意选连续的路
* 问,所有长度可能的欢乐值总和(欢乐值=j-i+1)i-->j
***********************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define db long double
#define LL long long
#define maxn 2*65536+10//最接近的且大的2的幂
const db pi = acos(-1.0);
struct cpx{
db r,i;
cpx(db r_=0,db i_=0):r(r_),i(i_){}
cpx operator + (cpx a){return cpx(r+a.r,i+a.i); }
cpx operator - (cpx a){return cpx(r-a.r,i-a.i); }
cpx operator * (cpx a){return cpx(r*a.r-i*a.i,r*a.i+i*a.r); }
};
//进行FFT和IFFT前的反转变换,len必须取2的幂次方
void change(cpx y[],LL len){
LL i,j,k;
for(i=1,j=len/2;i<len-1;i++){
if(i<j)swap(y[i],y[j]);
k=len/2;
while(j>=k){
j-=k;
k/=2;
}
if(j<k)j+=k;
}
}
void fft(cpx y[],LL len,LL on){
change(y,len);
for(LL h=2;h<=len;h<<=1){
cpx wm(cos(-on*2*pi/h),sin(-on*2*pi/h));
for(LL j=0;j<len;j+=h){
cpx w(1,0);
for(LL k=j;k<j+h/2;k++){
cpx u=y[k];
cpx t=w*y[k+h/2];
y[k]=u+t;
y[k+h/2]=u-t;
w=w*wm;
}
}
}
if(on==-1)
for(LL i=0;i<len;i++)
y[i].r/=len;
}
void convex(cpx x[],cpx y[],LL len){
fft(x,len,1);
fft(y,len,1);
for(LL i=0;i<=len;i++)x[i]=x[i]*y[i];
fft(x,len,-1);
}
LL sum[maxn];
cpx X1[maxn],Y1[maxn],X2[maxn],Y2[maxn];
int main(){
#ifdef ACBang
freopen("5321.in","r",stdin);
#endif // ACBang
LL T,n;
scanf("%lld",&T);
while(T--){
scanf("%lld",&n);
sum[0]=0;
LL zero=0,cnt=0;
for(LL i=1;i<=n;i++){
LL x;scanf("%lld",&x);//第i段路长x
sum[i]=sum[i-1]+x;
if(x==0){
cnt++;
zero+=(cnt+1)*(cnt)/2;
}else cnt=0;
}
LL len=1,tot=sum[n];
while(len<2*tot)len<<=1; //两倍初始长度
memset(X1,0,sizeof X1); memset(Y1,0,sizeof Y1);
memset(X2,0,sizeof X2); memset(Y2,0,sizeof Y2);
for(LL i=1;i<=n;i++){
X1[sum[i]].r+=i; Y1[tot-sum[i-1]].r+=1;
X2[sum[i]].r+=1; Y2[tot-sum[i-1]].r+=i-1;
}
convex(X1,Y1,len);
convex(X2,Y2,len);
printf("%lld\n",zero);//0的时候特殊处理
for(LL i=tot+1;i<=2*tot;i++)
printf("%lld\n",(LL)(X1[i].r+0.5)-(LL)(X2[i].r+0.5));
}
return 0;
}