HDU5307 He is Flying
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的更多相关文章
- [hdu5307] He is Flying [FFT+数学推导]
题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...
- HDU-5307 He is Flying (FFT)
Problem DescriptionJRY wants to drag racing along a long road. There are n sections on the road, the ...
- $FFT/NTT/FWT$题单&简要题解
打算写一个多项式总结. 虽然自己菜得太真实了. 好像四级标题太小了,下次写博客的时候再考虑一下. 模板 \(FFT\)模板 #include <iostream> #include < ...
- PDF 生成插件 flying saucer 和 iText
最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...
- 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 ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- HDU 5515 Game of Flying Circus 二分
Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- about building flying sauser
download flying sauser: unzip flyingsaucer-master.zip cd flyingsaucer-master/ mvn install
- hdu 1800 Flying to the Mars
Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...
随机推荐
- Linux段式管理与页式管理
内存管理有2种机制:1.段式管理:2.页式管理 在80386CPU中增加了2个寄存器:1.全局性的段描述表寄存器GDTR 2.局部性的段描述表寄存器LDTR 段寄存器的高13位用于在全局或局部描述表项 ...
- 全方位认识HDMI接口技术
HDMI接口并不是一个开放的标准.制造商必须向HDMI标准制定协会支付版税,来换取一个生产许可证.不过这个版税可不便宜,每年要交纳15000美元的许可费,并且更黑的是每生产一个HDMI接口就要支付0. ...
- CSS3裁剪与遮罩解析
一.用途 CSS3裁剪与遮罩(Clipping and Masking)用来隐藏元素的一部分而显示另一部分 二.区别 CSS3裁剪与遮罩(Clipping and Masking)用来隐藏元素的一部分 ...
- bootstrap重新设计按钮样式
1.将btn的样式换成以下的样式 2.思路: (1)将原来的btn样式设置color:#FFF,同时text-shadow设置,这样原来的btn样式就会变淡了,后面再加上新的样式就可以覆盖掉 注意:要 ...
- 栈和队列&前缀,中缀,后缀
1.堆和栈的区别? (1)栈内存操作系统来分配,堆内存由程序员自己来分配. (2)栈有系统自动分配,只要栈 剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出. 2.栈(线性表) 仅 ...
- 2018"百度之星"程序设计大赛 - 资格赛 - 题集
1001 $ 1 \leq m \leq 10 $ 像是状压的复杂度. 于是我们(用二进制)枚举留下的问题集合 然后把这个集合和问卷们的答案集合 $ & $ 一下 就可以只留下被选中的问题的答 ...
- iOS程序执行顺序和UIViewController 的生命周期(整理)
说明:此文是自己的总结笔记,主要参考: iOS程序的启动执行顺序 AppDelegate 及 UIViewController 的生命周期 UIView的生命周期 言叶之庭.jpeg 一. iOS程序 ...
- 《数据结构与算法分析:C语言描述》复习——第四章“树”——二叉树
2014.06.14 22:49 简介: 二叉树是学习树结构时接触的第一个概念,其他衍生的表示形式包括N叉树(随便多少叉).二叉链表(土话也叫左孩子右兄弟).由于单纯的二叉树是无序的,能做的事情不太多 ...
- Windows10使用pip安装python包时报错-UnicodeDecodeError: 'ascii' codec c
本人是Windows10,用的方法2解决的 原文链接http://blog.csdn.net/all_over_servlet/article/details/45112221 先交待下开发环境: 操 ...
- Git——1.简介
关于版本控制 Git基础 安装Git 初始运行Git前的配置 获取帮助 关于版本控制 版本控制(VCS)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 本地版本控制系统 大多都 ...