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

数学问题 生成函数 FFT

给一个数列,若有一个数对(i,j)满足sum[i]-sum[j-1]==S,则得到i-(j-1)的收益,求S取0到[数列总和]的每一个值时,各自的全部收益。

神一样的构造解……

看到数据范围这么大,又是求所有方案的累计贡献,普通的方法显然难以奏效。这时候就要考虑生成函数了。

如果把这看成一个多项式问题,两元相乘时次数相加,系数相乘,那么让题目中的"定值"在指数上体现出来。

  ↑ ΣS <=50000,那么让x^i这一位存储S=i时的收益,那么应该计算出所有的 [i-(j-1)]*x^s ,即为路程为s时的收益

那么就要构造能得到  [i-(j-1)]*x^s 形式的项的多项式。

根据sum[i]-sum[j-1]==S可以有:

Σ([ai]*x^sum[i])*Σ(x^-(sum[j-1])   -  Σ(x^sum[i])*Σ([a(j-1)]x^-(sum[j-1])

这样算卷积,指数部分得到sum[i]-sum[j-1],系数部分得到所有的(i-(j-1)),岂不美哉。

S取0的情况可以特判O(n)处理

传说FFT会爆精度,用了Long double以后成功AC

然后试了试NTT取超大模数强行算,对拍过了一些小数据,然而交上去TLE了

↑看到别人的NTT是可以过的,那就是我写的有问题,然而懒得改了先放着

FFT:

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const long double pi=acos(-1.0);
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct com{
long double x,y;
com operator + (const com b){return (com){x+b.x,y+b.y};}
com operator - (const com b){return (com){x-b.x,y-b.y};}
com operator * (const com b){return (com){x*b.x-y*b.y,x*b.y+y*b.x};}
}a[mxn],b[mxn],c[mxn];
int N,l,rev[mxn];
void FFT(com *a,int flag){
int i,j,k;
for(i=;i<N;i++)if(rev[i]>i)swap(a[rev[i]],a[i]);
for(i=;i<N;i<<=){
com wn=(com){cos(pi/i),flag*sin(pi/i)};
for(j=;j<N;j+=(i<<)){
com w=(com){,};
for(k=;k<i;k++,w=w*wn){
com x=a[j+k],y=w*a[j+k+i];
a[j+k]=x+y;
a[i+j+k]=x-y;
}
}
}
if(flag==-)for(i=;i<N;i++)a[i].x/=N;
return;
}
int n,w[mxn];
LL ans[mxn];
int smm[mxn];
void init(){
n=read();
LL cnt=;
ans[]=;
for(int i=;i<=n;i++){
w[i]=read();
smm[i]=smm[i-]+w[i];
if(!w[i]){//
cnt++;
ans[]+=cnt*(cnt+)/;
}
else cnt=;
}
return;
}
int main(){
int i,j;
int T=read(); while(T--){
init();
// for(i=1;i<=n;i++)printf("%d ",smm[i]);
// printf("\n");
memset(a,,sizeof a);
memset(b,,sizeof b);
memset(c,,sizeof c);
int ed=smm[n];
int m=ed<<;
for(N=,l=;N<=m;N<<=)l++;
for(i=;i<N;i++){
rev[i]=(rev[i>>]>>)|((i&)<<(l-));
}
//
for(i=;i<=n;i++){
a[smm[i]].x+=i;
b[ed-smm[i-]].x+=;
}
/*
for(i=0;i<=ed;i++)printf("%.2Lf ",a[i].x);
printf("\n");
for(i=0;i<=ed;i++)printf("%.2Lf ",b[i].x);
printf("\n");
*/
FFT(a,);FFT(b,);
for(i=;i<=N;i++)
c[i]=a[i]*b[i];
FFT(c,-);
memset(a,,sizeof a);
memset(b,,sizeof b);
for(i=;i<=n;i++){
a[smm[i]].x+=;
b[ed-smm[i-]].x+=i-;
}
FFT(a,);FFT(b,);
for(i=;i<=N;i++){
a[i]=a[i]*b[i];
}
FFT(a,-);
for(i=;i<=N;i++)c[i]=c[i]-a[i];
printf("%lld\n",ans[]);
for(i=;i<=ed;i++){
printf("%lld\n",(LL)(c[i+ed].x+0.5));
}
}
return ;
}

TLE的NTT

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
//const LL P=(1LL<<47)*7*4451+1;
const LL P=*(<<)+;
//const LL mod=479*(1<<21)+1;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL a[mxn],b[mxn],c[mxn];
int N,l;
LL mul(LL x,LL y) {
LL res=;
while(y){
if(y&)res=(res+x)%P;
x=(x<<)%P;
y>>=;
}
return res;
}
LL ksm(LL a,LL k){
LL res=;
while(k){
if(k&)res=mul(res,a);
a=mul(a,a);
k>>=;
}
return res;
}
int rev[mxn];
void NTT(LL *a,int flag){
int i,j,k;
for(i=;i<N;i++)if(rev[i]>i)swap(a[rev[i]],a[i]);
for(i=;i<N;i<<=){
LL gn=ksm(,(P-)/(i<<));
int p=i<<;
for(j=;j<N;j+=p){
LL g=;
for(k=;k<i;k++,g=mul(g,gn)){
LL x=a[j+k],y=mul(g,a[j+k+i]);
a[j+k]=(x+y)%P;
a[i+j+k]=(x-y+P)%P;
}
}
}
if(flag==-){
reverse(a+,a+N);
LL inv=ksm(N,P-);
for(i=;i<N;i++)a[i]=mul(a[i],inv)%P;
}
return;
}
int n,w[mxn];
LL ans[mxn];
int smm[mxn];
void init(){
n=read();
LL cnt=;
ans[]=;
for(int i=;i<=n;i++){
w[i]=read();
smm[i]=smm[i-]+w[i];
if(!w[i]){//
cnt++;
ans[]+=cnt*(cnt+)/;
}
else cnt=;
}
return;
}
int main(){
int i,j;
int T=read();
while(T--){
init();
memset(a,,sizeof a);
memset(b,,sizeof b);
memset(c,,sizeof c);
int ed=smm[n];
int m=ed<<;
for(N=,l=;N<=m;N<<=)l++;
for(i=;i<N;i++){
rev[i]=(rev[i>>]>>)|((i&)<<(l-));
}
//
for(i=;i<=n;i++){
a[smm[i]]+=i;
b[ed-smm[i-]]+=;
} NTT(a,);NTT(b,);
// for(i=0;i<=N;i++)printf("%lld ",a[i]);printf("\n");
for(i=;i<=N;i++)
c[i]=mul(a[i],b[i])%P;
NTT(c,-);
memset(a,,sizeof a);
memset(b,,sizeof b);
for(i=;i<=n;i++){
a[smm[i]]+=;
b[ed-smm[i-]]+=i-;
}
NTT(a,);NTT(b,);
for(i=;i<=N;i++){
a[i]=mul(a[i],b[i])%P;
}
NTT(a,-);
for(i=;i<=N;i++)c[i]=(c[i]-a[i]+P)%P;
printf("%lld\n",ans[]);
for(i=;i<=ed;i++){
printf("%lld\n",c[i+ed]);
}
}
return ;
}

HDU5307 He is Flying的更多相关文章

  1. [hdu5307] He is Flying [FFT+数学推导]

    题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...

  2. HDU-5307 He is Flying (FFT)

    Problem DescriptionJRY wants to drag racing along a long road. There are n sections on the road, the ...

  3. $FFT/NTT/FWT$题单&简要题解

    打算写一个多项式总结. 虽然自己菜得太真实了. 好像四级标题太小了,下次写博客的时候再考虑一下. 模板 \(FFT\)模板 #include <iostream> #include < ...

  4. PDF 生成插件 flying saucer 和 iText

    最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...

  5. hdu---(1800)Flying to the Mars(trie树)

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  7. HDU 5515 Game of Flying Circus 二分

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

  8. about building flying sauser

    download flying sauser: unzip flyingsaucer-master.zip cd flyingsaucer-master/ mvn install

  9. hdu 1800 Flying to the Mars

    Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...

随机推荐

  1. python基础之模块part1

    模块: 模块本质上就是一个Python程序. 所有说是对象的,一定可以通过  对象.方法  来实现某些操作. 模块种类: 内置模块 第三方模块 自定义模块 import在查找模块的顺序:内置模块--- ...

  2. CSS计数器(自定义列表)Demo

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  3. 3771: Triple

    3771: Triple 链接 题意 n个斧头,每个斧头的价值都不同(开始时没注意到),可以取1个,2个,3个斧头组成不同的价值,求每种价值有多少种组成方案(顺序不同算一种) 分析: 生成函数 + 容 ...

  4. notepad++ 换行技巧 log换行

    有时候,服务器收集上来的日志,格式很乱,看log很难,如下: java.lang.IllegalStateException: BEvent.init() must be call first\n\t ...

  5. python语法re.compile模块介绍

    1. re模块是正则表达式模块,re模块中包含一个重要函数是compile(pattern [, flags]) ,该函数根据包含的正则表达式的字符串创建模式对象.可以实现更有效率的匹配. impor ...

  6. 当Hadoop 启动节点Datanode失败解决

    Hadoop 启动节点Datanode失败解决 [日期:2014-11-01] 来源:Linux社区  作者:shuideyidi [字体:大 中 小] 当我动态添加一个Hadoop从节点的之后,出现 ...

  7. ios在tableview里面加subview后在ip4和ip5上显示不一样的问题

    文章链接:http://quke.org/post/ios-tableview-addsubview-height.html (转载时请注明本文出处及文章链接) 我在在tableview里面加subv ...

  8. USACO Section2.2 Runaround Numbers 解题报告 【icedream61】

    runround解题报告---------------------------------------------------------------------------------------- ...

  9. JMeter学习笔记(四) HTTP Cookies 管理器

    有些接口执行时,要求要先登录,此时就需要用到 HTTP Cookies 管理器.不过有些项目是使用的token,即添加HTTP信息头管理器,获取登录后的token,至于token与cookies的区别 ...

  10. DDT驱动

    下载ddt并安装 Pip install ddt 或者官网下载安装 http://ddt.readthedocs.io/en/latest/ https://github.com/txels/ddt ...