HDU-5307 He is Flying (FFT)
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
#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)的更多相关文章
- HDU 5307 He is Flying ——FFT
卷积的妙用,显然我们可以求出所有符合条件的右端点的和,然后减去左端点的和. 就是最后的答案.然后做一次前缀和,然后就变成了统计差是一个定值的情况. 令$A(s[i])++$ $B(s[i])+=i$ ...
- FFT(快速傅里叶变换):HDU 5307 He is Flying
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8IAAAPeCAIAAABInTQaAAAgAElEQVR4nOy9fZReVXk3vP8ia+HqCy
- HDU 5307 He is Flying (生成函数+FFT)
题目传送门 题目大意:给你一个长度为$n$的自然数序列$a$,定义一段区间的权值为这一段区间里所有数的和,分别输出权值为$[0,\sum a_{i}]$的区间的长度之和 想到了生成函数的话,这道题并不 ...
- 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 ...
- HDU 5515 Game of Flying Circus 二分
Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- HDU 5763 Another Meaning(FFT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...
- 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 < ...
- hdu 5730 Shell Necklace [分治fft | 多项式求逆]
hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...
- 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] ...
- hdu 4609 3-idiots(快速傅里叶FFT)
比较裸的FFT(快速傅里叶变换),也是为了这道题而去学的,厚的白书上有简单提到,不过还是推荐看算法导论,讲的很详细. 代码的话是照着别人敲的,推荐:http://www.cnblogs.com/kua ...
随机推荐
- Tarjan 割点割边【模板】
#include <algorithm> #include <cstring> #include <cstdio> using namespace std; +); ...
- php持续集成环境笔记
记录下php集成环境中若干个工具的安装步骤和过程: 安装pear wget http://pear.php.net/go-pear.phar $ php go-pear.phar 使用:pear in ...
- Mahout推荐算法API具体解释【一起学Mahout】
阅读导读: 1.mahout单机内存算法实现和分布式算法实现分别存在哪些问题? 2.算法评判标准有哪些? 3.什么会影响算法的评分? 1. Mahout推荐算法介绍 Mahout推荐算法,从数据处理能 ...
- TextView超链接
这里面涉及两个知识点--超链接和跳转.以下进行逐一解说: 1.实现超链接: 1.1形成超链接文本 public static SpannableString getUserlink(String us ...
- Linux下安装过程中编译PHP时报错:configure: error: libjpeg.(a|so) not found
在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决的方法: 检查之后发现已经安装libjpeg.可是/usr/l ...
- codeforces#281 A
脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...
- [JZOJ4024] [佛山市选2015] 石子游戏 解题报告
Description Alice 和 Bob 总喜欢聚在一起玩游戏(T_T),今天他(她)们玩的是一款新型的取石子游戏.游戏一开始有N堆石子,Alice 和 Bob 轮流取出石子.在每次操作 ...
- 18.查询效率最高的unordered_map
#include <string> #include <iostream> //查询性能最高 //增删查改与map是一样的,但是本质区别就是unordered_map底层是ha ...
- ubuntu14.04下snort的安装(官方文档安装)(图文详解)
不多说,直接上干货! 最近为了科研,需要安装和使用Snort. snort的官网 https://www.snort.org/ Snort作为一款优秀的开源主机入侵检测系统,在windows和Linu ...
- PostgreSQL两种事务隔离级
PostgreSQL两种事务隔离级别: 读已提交:PostgreSQL中缺省隔离级别.当一个事务运行在这个隔离级别时,一个SELECT查询只能看到查询开始之前提交的数据而永远无法看到未提交的数据或者在 ...