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 ...
随机推荐
- HTML+CSS : H5+CSS3
HTML5语义化标签: header nav(导航) article section(章节) aside(侧边栏) footer------------------------------------ ...
- array_unique() - 去除数组中重复的元素值
array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名 ...
- php+MySQL(存储过程) +yii2完整的增删改查
1在MySQL中创建存储过程 a 我将添加和修改 作为 一起 ), ), ), )) BEGIN FROM t_boss_role WHERE id = _id) THEN UPDATE t_boss ...
- 17-比赛1 F - 较小元素 Weak in the Middle (set)
Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton. He ...
- Snowflake Snow Snowflakes【Poj3349】
Description You may have heard that no two snowflakes are alike. Your task is to write a program to ...
- http与www服务精解
TCP/IP 协议介绍 在介绍 HTTP 协议之前,先简单说一下TCP/IP协议的相关内容.TCP/IP协议是分层的,从底层至应用层分别为:物理层.链路层.网络层.传输层和应用层,如下图所示: 从应用 ...
- Android ANR详解
如何避免KeyDispatchTimeout 1:UI线程尽量只做跟UI相关的工作 2:耗时的工作(比如数据库操作,I/O,连接网络或者别的有可能阻碍UI线程的操作)把它放入单独的线程处理 3:尽量用 ...
- 1.bootstrap的HTML文件编写规范
1.head标签里面的内容 <!DOCTYPE html> <html lang="zh-cn"> <head> <!-- 页面编码 -- ...
- border与background定位
1.background定位的局限 只能相对于左上角数值定位,不能相对于右下 即background-position默认相对于左上方定位的 2.怎样让图片相对于右下角? background-pos ...
- jQuery easyuI datagrid 多行编辑
在easyUI 动态绑定部分数据后,需要有部分列可以修改,研究了一天终于搞定.这是小弟的做法,望各位有好招的大侠指点. 1.添加jQuery 和jQuery easyuI的引用. 2.添加id为tt的 ...