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 .
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

解题思路:
首先可以得到一个暴力的做法,就是枚举每一段的起点终点将答案计入数组,
输出就好了,时间复杂度$O(n^3)$,前缀和优化就是$O(n^2)$。
分析上述算法,发现如果固定住答案数组的一位(假设现在就是在求第$x$项的答案,设前$i$项$s[]$的前缀和为$sum[i]$)
那么上述算法可以表述为:
$Ans(x)=\sum\limits_{i=1}^{n}\sum\limits_{j=i}^{n}[sum[j]-sum[i-1]==x](j-i+1)$
由于$sum[j]-sum[i-1]$为固定的,可以认为这是在暗示这题正解是求生成函数第$x$项系数。
那我们就往生成函数上想吧。
那么由于后面是$j-i+1$是加减法关系,由于次数不同,可以认为是两个生成函数分别与全是1的多项式乘积后的累和。
那么考虑是哪两个结果相加。这个好像只能是$j$和$i-1$了吧(指数形式提醒我们)。
设答案的生成函数多项式为$F(x)=f(0)+f(1)x+f(2)x^2+...$
设左右两部分贡献分别为$G$和$H$。(左边是根据$j$列出的多项式,右边是根据$i-1$列出的多项式)
那么
$F=G-H$
由于需要将多项式次数调平,所以:
$G=\sum\limits_{i=1}^{n}(i*x^{sum[i]})\sum\limits_{i=1}^{n}(x^{-sum[i-1]})$
同理:
$H=\sum\limits_{i=1}^{n}((i-1)x^{-sum[i-1]})\sum\limits_{i=1}^{n}x^{sum[i]}$
负数次幂加个大数最后减掉就好了。
注意这个式子并不能处理(i==0)的情况,因为$sum[-1]$并没有被定义。
分类讨论好麻烦,所以直接$O(n)$统计(i==0)的答案即可。
注意一定要写using namespace std;
代码:
 #include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int DLT=;
const long double PI=acos(-1.0);
struct cp{
long double x,y;
cp(){}
cp(long double a,long double b){x=a;y=b;}
cp friend operator + (cp a,cp b){return cp(a.x+b.x,a.y+b.y);}
cp friend operator - (cp a,cp b){return cp(a.x-b.x,a.y-b.y);}
cp friend operator * (cp a,cp b){return cp(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
}A[],B[],C[],D[];
int T;
int n,m;
int lim;
int len;
int s[];
int pos[];
int sum[];
void init(void)
{
memset(A,,sizeof(A));
memset(B,,sizeof(B));
memset(C,,sizeof(C));
memset(D,,sizeof(D));
return ;
}
void FFT(cp *a,double flag)
{
for(int i=;i<len;i++)if(i<pos[i])std::swap(a[i],a[pos[i]]);
for(int i=;i<=len;i<<=)
{
cp wn(cos(2.00*flag*PI/i),sin(2.00*flag*PI/i));
for(int j=;j<len;j+=i)
{
cp w(,),t;
for(int k=;k<(i>>);k++,w=w*wn)
{
t=a[j+k+(i>>)]*w;
a[j+k+(i>>)]=a[j+k]-t;
a[j+k]=a[j+k]+t;
}
}
}
return ;
}
void work(void)
{
init();
long long a0();
int cnt();
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&s[i]);
if(!s[i])
{
cnt++;
a0+=1ll*cnt*(cnt+)/;
}else cnt=;
sum[i]=sum[i-]+s[i];
}
printf("%lld\n",a0);
while((<<lim)<(DLT<<))lim++;len=<<lim;
for(int i=;i<len;i++)pos[i]=(pos[i>>]>>)|((i&)<<(lim-));
for(int i=;i<=n;i++)
{
A[sum[i]].x+=i;B[DLT-sum[i-]].x+=;
C[sum[i]].x+=;D[DLT-sum[i-]].x+=i-;
}
FFT(A,),FFT(B,),FFT(C,),FFT(D,);
for(int i=;i<len;i++)A[i]=A[i]*B[i]-C[i]*D[i];
FFT(A,-);
for(int i=;i<=sum[n];i++)printf("%I64d\n",(long long)(A[i+DLT].x/len+0.2));
return ;
}
int main()
{
scanf("%d",&T);
while(T --> )work();
return ;
}

HDU-5307 He is Flying (FFT)的更多相关文章

  1. HDU 5307 He is Flying ——FFT

    卷积的妙用,显然我们可以求出所有符合条件的右端点的和,然后减去左端点的和. 就是最后的答案.然后做一次前缀和,然后就变成了统计差是一个定值的情况. 令$A(s[i])++$ $B(s[i])+=i$ ...

  2. FFT(快速傅里叶变换):HDU 5307 He is Flying

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8IAAAPeCAIAAABInTQaAAAgAElEQVR4nOy9fZReVXk3vP8ia+HqCy

  3. HDU 5307 He is Flying (生成函数+FFT)

    题目传送门 题目大意:给你一个长度为$n$的自然数序列$a$,定义一段区间的权值为这一段区间里所有数的和,分别输出权值为$[0,\sum a_{i}]$的区间的长度之和 想到了生成函数的话,这道题并不 ...

  4. HDU - 5307 :He is Flying (分治+FFT)(非正解)

    JRY wants to drag racing along a long road. There are nn sections on the road, the ii-th section has ...

  5. HDU 5515 Game of Flying Circus 二分

    Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  6. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

  7. hdu 4656 Evaluation [任意模数fft trick]

    hdu 4656 Evaluation 题意:给出\(n,b,c,d,f(x) = \sum_{i=1}^{n-1} a_ix^i\),求\(f(b\cdot c^{2k}+d):0\le k < ...

  8. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  9. hdu 5730 Shell Necklace —— 分治FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 DP式:\( f[i] = \sum\limits_{j=1}^{i} f[i-j] * a[j] ...

  10. hdu 4609 3-idiots(快速傅里叶FFT)

    比较裸的FFT(快速傅里叶变换),也是为了这道题而去学的,厚的白书上有简单提到,不过还是推荐看算法导论,讲的很详细. 代码的话是照着别人敲的,推荐:http://www.cnblogs.com/kua ...

随机推荐

  1. Ubuntu设置显示桌面快捷键

    Ubuntu设置显示桌面快捷键 直接在系统设置中没有效果, 学习了:http://www.cnblogs.com/pluse/p/5286585.html 需要进行安装compizconfig,然后在 ...

  2. tomcat怎样禁止显示文件夹和文件列表

    查看原文:http://www.ibloger.net/article/300.html Tomcat禁止显示文件夹和文件列表 打开   tomcat的安装文件夹/conf/web.xml 文件 &l ...

  3. java 基本类型、包装类、字符串之间的转换

    1.基本类型和包装类 基本类型和包装类可通过自动装箱和拆箱实现. int i = 24; Integer a = new Integer(i); //手动装箱 Integer b = i; //自动装 ...

  4. 可以通过shadowserver来查看开放的mdns(用以反射放大攻击)——中国的在 https://mdns.shadowserver.org/workstation/index.html

    Open mDNS Scanning Project 来自:https://mdns.shadowserver.org/ If you are looking at this page, then m ...

  5. Kali linux 2016.2(Rolling)里Metasploit的口令猜测与嗅探

    不多说,直接上干货! 对于发现的系统与文件管理类网络服务,比如Telnet.SSH.FTP等,可以进行弱口令的猜测,以及对明文传输口令的嗅探,从而尝试获取直接通过这些服务进入目标网络的通道. 对于SS ...

  6. The evolution of cluster scheduler architectures--转

    原文地址:http://www.firmament.io/blog/scheduler-architectures.html cluster schedulers are an important c ...

  7. P2264 情书(字符串hash90分)

    题目背景 一封好的情书需要撰写人全身心的投入.lin_toto同学看上了可爱的卡速米想对她表白,但却不知道自己写的情书是否能感动她,现在他带着情书请你来帮助他. 题目描述 为了帮助lin_toto,我 ...

  8. manacherO(n)求最长回文子串 hihocoder1032

    原文地址:https://segmentfault.com/a/1190000003914228   http://blog.csdn.net/synapse7/article/details/189 ...

  9. Spring 注解拦截器使用详解

    Spring mvc拦截器 平时用到的拦截器通常都是xml的配置方式.今天就特地研究了一下注解方式的拦截器. 配置Spring环境这里就不做详细介绍.本文主要介绍在Spring下,基于注解方式的拦截器 ...

  10. sql server 中查询数据库下有多少张表以及同义词等信息

    --查询数据库有多少张表SELECT count(0) from sysobjects where xtype = 'u' 复制代码 解释:sysobjects系统对象表. 保存当前数据库的对象.如约 ...