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. linux最大进程数

    使用 ulimit -a 命令,查看 max user processes 的输出,就是系统最大进程数 core file size (blocks, -c) unlimited data seg s ...

  2. python基础之生成器表达式形式、面向过程编程、内置函数部分

    生成器表达式形式 直接上代码 1 # yield的表达式形式 2 def foo(): 3 print('starting') 4 while True: 5 x=yield #默认返回为空,实际上为 ...

  3. 直接选择排序&堆排序

    1.什么是直接选择排序? 直接选择排序(Straight Select Sort)是一种简单的排序方法,它的基本思想是:通过n-i次关键字之间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i ...

  4. Postman-简单使用(1)

    Postman-简单使用(1) Postman-简单使用 Postman-进阶使用 Postman-CI集成Jenkins Postman功能(https://www.getpostman.com/f ...

  5. js常用框架

    JS常用框架:jQuery.Prototype.MooTools 参考:w3cshool jQuery jQuery 是目前最受欢迎的 JavaScript 框架. 它使用 CSS 选择器来访问和操作 ...

  6. Pascal小游戏之奇葩的RPG

    Pascal吧友作品 一个小RPG Chaobs转载 varplife,plifemax,patt,pre:integer;gr,ex,exmax:integer;alife,alife1,aatt, ...

  7. [译]17-spring基于java代码的配置元数据

    spring还支持基于java代码的配置元数据.不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下. spring基于java代码的配置元数据,可以通过@Configuration注解 ...

  8. winform对图片进行灰度处理

    //图片进行灰度处理 //originalImage为原图像 返回灰度图像 private Bitmap GrayImage(Bitmap originalImage) { ImageAttribut ...

  9. Action参数和View、Json、重定向

    一.Action 1.Action参数: 普通参数.Model类.FormCollection (1).普通参数 Index(string name,int age)   框架会自动把用户请求的Que ...

  10. sql 删除重复的类型并且时间相同的项

    delete RemoteDetection WHERE REMOTEDETECTIONID IN ( select ID from ( select MIN(REMOTEDETECTIONID) I ...