JRY wants to drag racing along a long road. There are nn sections on the road, the ii-th section has a non-negative integer length sisi. JRY will choose some continuous sections to race (at an unbelievable speed), so there are totally n(n+1)2n(n+1)2 different ways for him to ride. If JRY rides across from the ii-th section to the jj-th section, he would gain j−i+1j−i+1 pleasure. Now JRY wants to know, if he tries all the ways whose length is ss, 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 00.

InputThe first line of the input is a single integer T (T=5)T (T=5), indicating the number of testcases.

For each testcase, the first line contains one integer nn. The second line contains nnnon-negative integers, which mean the length of every section. If we denote the total length of all the sections as ss, we can guarantee that 0≤s≤500000≤s≤50000 and 1≤n≤1000001≤n≤100000. 
OutputFor each testcase, print s+1s+1 lines. The single number in the ii-th line indicates the total pleasure JRY can get if he races all the ways of length i−1i−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

题意:总区间中有n个数(n<=100000),求每种区间和,累加出所对应的区间长度(j-i+1)和;

思路:可以通过母函数得到方程,然后FFT求。 也有一种暴力一点的方式,分治+FFT:即求跨过每个点的区间的贡献。 然后两次FFT分别算出左边和右边的贡献。

(4900ms卡过去了。要用long double。

母函数的方法可以看:https://blog.csdn.net/kyleyoung_ymj/article/details/51712329

#include<bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define rep2(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
const int maxn=;
const double pi=acos(-1.0);
struct cp
{
double r,i;
cp(){}
cp(double rr,double ii):r(rr),i(ii){}
cp operator +(const cp&x)const{return cp(r+x.r,i+x.i);}
cp operator -(const cp&x)const{return cp(r-x.r,i-x.i);}
cp operator *(const cp&x)const{return cp(r*x.r-i*x.i,i*x.r+r*x.i);}
};
ll ans[maxn];int s[maxn],sum[maxn],R[maxn],n;
cp a[maxn],b[maxn],W,w,p;
void read(int &x){
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c>=''&&c<='') x=x*+c-'',c=getchar();
}
inline void fft(cp*c,int t)
{
int i,j,k;
for(i=;i<n;i++) R[i]<i?swap(c[R[i]],c[i]),:;
for(i=;i<n;i<<=)
for(j=,W={cos(pi/i),sin(pi/i)*t};j<n;j+=i<<)
for(k=,w={,};k<i;k++,w=w*W)
p=c[j+k+i]*w,c[j+k+i]=c[j+k]-p,c[j+k]=c[j+k]+p;
}
void solve(int l,int r)
{
if(l==r){ ans[s[l]]++; return ;}
int mid=(l+r)>> ,delta=sum[r]-sum[l-];
solve(l,mid); solve(mid+,r);
for(n=;(n-)<=delta;n<<=);
rep(i,,n-) R[i]=R[i>>]>>|(i&?n>>:); rep2(i,mid,l) a[sum[mid]-sum[i-]].r+=mid-i+;
rep(i,mid+,r) ++b[sum[i]-sum[mid]].r;
fft(a,); fft(b,);
rep(i,,n-) a[i]=a[i]*b[i];
fft(a,-);
rep(i,,delta) ans[i]+=(ll)((a[i].r)/n+0.5);
rep(i,,n) a[i]=b[i]=cp(.,.); rep2(i,mid,l) ++a[sum[mid]-sum[i-]].r;
rep(i,mid+,r) b[sum[i]-sum[mid]].r+=i-mid;
fft(a,); fft(b,);
rep(i,,n-) a[i]=a[i]*b[i];
fft(a,-);
rep(i,,delta) ans[i]+=(ll)((a[i].r)/n+0.5);
rep(i,,n) a[i]=b[i]=cp(.,.);
}
int main()
{
int N,T;
scanf("%d",&T);
while(T--){
read(N);
rep(i,,N) read(s[i]),sum[i]=sum[i-]+s[i];
rep(i,,sum[N]) ans[i]=;
solve(,N);
rep(i,,sum[N]) printf("%lld\n",ans[i]);
}
return ;
}

HDU - 5307 :He is Flying (分治+FFT)(非正解)的更多相关文章

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

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

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

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8IAAAPeCAIAAABInTQaAAAgAElEQVR4nOy9fZReVXk3vP8ia+HqCy

  3. HDU 5307 He is Flying ——FFT

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

  4. HDU 5730 Shell Necklace cdq分治+FFT

    题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...

  5. hdu_5683_zxa and xor(非正解的暴力)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5683 题意: 问题描述 zxa最近对按位异或(exclusive disjunction)产生了极大的 ...

  6. HDU 4251 --- 主席树(划分树是正解)

    题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...

  7. BZOJ3110 K大数查询 【线段树 + 整体二分 或 树套树(非正解)】

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  8. 【P2387】魔法森林(SPFA非正解)

    题目链接 不会LCTqwq,看题解似乎SPFA也可以. 把边按a排序,从小到大每加一条边就以b为距离跑一遍SPFA,类似于Kruskal的想法吧…… 貌似是个暴力 (luoguLCT模块的题我都快通过 ...

  9. [Bzoj2120]数颜色 (非正解 )(莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 6286  Solved: 2489[Submit][Status][Discuss] ...

随机推荐

  1. VMware虚拟机NAT模式的具体配置

      NAT模式的具体配置 NAT方式:虚拟机可以上外网,可以访问宿主计算机所在网络的其他计算机(反之不行). 1.1.1.        查看虚拟机的网络参数 1)      打开虚拟机,选择菜单&q ...

  2. HackerRank - maximum-perimeter-triangle 【水】

    题意 给出一系列数字,判断其中哪三个数字可以构成一个三角形,如果有多个,输出周长最大的那个,如果没有输出 - 1 思路 数据较小,所有情况FOR一遍 判断一下 AC代码 #include <cs ...

  3. $《第一行代码:Android》读书笔记——第10章 Android网络编程

    (一)WebView的用法 1.WebView也是一个普通的控件. 2.常用用法: WebView webView = (WebView)findViewById(R.id.web_view); we ...

  4. CSS3中新颖的布局方法

    本人已经很久没用 bootstrap 什么的了,而现阶段一点卑微的梦想就是自己做框架,毕竟也才入门不久. 所以在寻找布局的共通性/稳定性及拓展性时,会发觉 CSS3 的这三种方法比栅栏布局要有趣得多. ...

  5. P4501 [ZJOI2018]胖

    题目 P4501 [ZJOI2018]胖 官方口中的送分题 做法 我们通过手玩(脑补),\(a_i\)所作的贡献(能更新的点)为:在\(a_i\)更新\(\forall x\)更新前前没有其他点能把\ ...

  6. Swift_初识Swift

    初识Swift语言 Swift结合了C和OC的优点并且不受C兼容性的限制.Swift采用安全的编程模式并添加了很多新特性,这将是编程更简单,更灵活也更有趣,Swift是基于成熟而且倍受喜爱的Cocoa ...

  7. MyBatis联合查询association使用

    1.需求 两张表 channels(频道表)  member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...

  8. java深入探究12-框架之Hibernate

    1.引入SSH框架 Struts框架,基于MVC 模式的应用层框架技术 Hibernate,基于持久层框架(数据访问层使用) Dao代码编写的几种方式: 1.原始jdbc操作,Connection/S ...

  9. Hadoop的Docker镜像构建

    1.Dockerfile ###Dockerfile -- beagin FROM ubuntu:trusty #MAINTAINER The Hue Team "https://githu ...

  10. hive学习7(条件函数case)

    case函数 语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END 说明:如果a为TRUE,则返回b:如果c为TRUE,则返回d:否则返回e 实例 ...