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. C语言结构体指针成员强制类型转换

    #include <stdio.h> #include <stdlib.h> typedef struct ListElmt_ { void *data; struct Lis ...

  2. 图解HTTP总结(3)——HTTP报文内的HTTP信息

    HTTP通信过程包括从客户端发往服务端的请求及从服务器端返回客户端的响应. 用于HTTP协议交互的信息被称为HTTP报文.客户端的HTTP报文叫做请求报文,服务器端的叫做响应报文.HTTP报文本身是多 ...

  3. 2019-04-11 python入门学习——配置机器及搭建开发环境

    # 在windows操作系统中搭建python 3.x版本的开发环境,开发工具为 Anaconda 3. # 1.1 下载及安装Anaconda 3 Anaconda的特点:集成性高,包含很多常用的开 ...

  4. C语言进阶——变量属性05

    C语言变量属性: C语言的变量可以有自己的属性 在定义变量的时候加上“属性”关键字 “属性”关键字指明变量的特有意义 语法:property type value_name; auto关键字: aut ...

  5. 007---Django的视图层

    视图函数 一个视图函数,简称视图,是一个简单的python函数.它接收web请求并且返回web响应. 1.一张网页的HTML内容 2.一个重定向 3.一个404错误 4.一个xml文档 5.一个字符串 ...

  6. Oozie wordcount实战

    一.定义 基本概念 Action: An execution/computation task (Map-Reduce job, Pig job, a shell command). It can a ...

  7. python-openpyxl操作excel

    python 读写 excel有很多选择,但是,方便操作的库不多,在我尝试了几个库之后,我觉得两个比较方便的库是xlrd/xlwt.openpyxl. 之所以推荐这两个库是因为这两个库分别操作的是不同 ...

  8. 微信小程序 | 49,小程序入门集锦系列文章20篇

    以下20篇文章,都是关于微信小程序的文章,以入门常见问题为主.如发现谬误,请与笔者联系. [小程序入门集锦]1,微信小程序在哪里打开 [小程序入门集锦]2,小程序商店 [小程序入门集锦]3,微信小程序 ...

  9. AD高级规则设置

    inpolygon 是所有的覆铜 ispad 是焊盘到焊盘的间距 IsVia 过孔间距 ispad and InComponent('S1')    设置某个器件的焊盘间距规则 ispad and H ...

  10. echart搭配时间轴进行展示 (本例展示的是多时间 多地区 多指标条件 )

    1:照常先来几张图 看效果  2:首先 看官方文档 我把echart官方的例子给扒下来并整理了得出如下效果 上 案例图和代码 效果图 : 代码: <style type="text/c ...