He is Flying
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1054 Accepted Submission(s): 277
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
Recommend
wange2014 | We have carefully selected several similar problems for you: 6032 6031 6030 6029 6028
【分析】
生成函数+FFT
出题人脑洞也是大的要死。
精度也是卡的要死。
【代码】
//hdu 5307 He is flying
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const long double pi=acos(-1);
const int mxn=400005;
ll zero;
int n,m,L,N,T;
int R[mxn],sum[mxn],w[mxn];
struct E
{
long double r,f;
E (long double u,long double v) {r=u,f=v;}
E () {}
E operator + (E u) {return E(r+u.r,f+u.f);}
E operator - (E u) {return E(r-u.r,f-u.f);}
E operator * (E u) {return E(r*u.r-f*u.f,r*u.f+f*u.r);}
E operator / (int v) {return E(r/v,f/v);}
}a[mxn],b[mxn],c[mxn];
inline void FFT(E *a,int f)
{
int i,j,k;
fo(i,0,n-1) if(i<R[i]) swap(a[i],a[R[i]]);
for(i=1;i<n;i<<=1)
{
E wn(cos(pi/i),f*sin(pi/i));
for(j=0;j<n;j+=(i<<1))
{
E w(1,0);
for(k=0;k<i;k++,w=w*wn)
{
E x=a[j+k],y=w*a[j+k+i];
a[j+k]=x+y,a[j+k+i]=x-y;
}
}
}
if(f==-1) fo(i,0,n-1) a[i]=a[i]/n;
}
inline void solve()
{
int i,j;
sum[0]=0;
ll cnt=0;zero=0;
fo(i,1,N) //init
{
scanf("%d",&w[i]);
sum[i]=sum[i-1]+w[i];
if(!w[i]) cnt++,zero+=cnt*(cnt+1)/2;
else cnt=0;
}
}
int main()
{
int i,j;
scanf("%d",&T);
while(T--)
{
M(a),M(b),M(c),M(R),L=0;
scanf("%d",&n);N=n;
solve();
m=sum[N]<<1;for(n=1;n<=m;n<<=1) L++;
fo(i,0,n-1) R[i]=(R[i>>1]>>1)|((i&1)<<L-1);
fo(i,1,N)
{
a[sum[i]].r+=i;
b[sum[N]-sum[i-1]].r+=1; //防止下标变负
}
FFT(a,1),FFT(b,1);
fo(i,0,n) c[i]=a[i]*b[i];
FFT(c,-1);
M(a),M(b);
fo(i,1,N)
{
a[sum[i]].r+=1;
b[sum[N]-sum[i-1]].r+=i-1;
}
FFT(a,1),FFT(b,1);
fo(i,0,n) a[i]=a[i]*b[i];
FFT(a,-1);
fo(i,0,n) c[i]=c[i]-a[i];
printf("%lld\n",zero);
fo(i,1,sum[N]) printf("%lld\n",(ll)(c[i+sum[N]].r+0.5));
}
return 0;
}