hdu 5730 Shell Necklace —— 分治FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730
DP式:\( f[i] = \sum\limits_{j=1}^{i} f[i-j] * a[j] \)
因为没有给 \( f[0] \) 赋初值,所以在递归底层令 \( f[l] += a[l] \)
注意多组数据清空数组;
读入 \( s[i] \) 时要取模!!
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
typedef double db;
int const xn=(<<),mod=;
db const Pi=acos(-1.0);
int n,rev[xn],f[xn],s[xn];
struct cpl{
db x,y;
cpl(db xx=,db yy=):x(xx),y(yy) {}
}a[xn],b[xn];
cpl operator + (cpl a,cpl b){return cpl(a.x+b.x,a.y+b.y);}
cpl operator - (cpl a,cpl b){return cpl(a.x-b.x,a.y-b.y);}
cpl operator * (cpl a,cpl b){return cpl(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
void fft(cpl *a,int tp,int lim)
{
for(int i=;i<lim;i++)
if(i<rev[i])swap(a[i],a[rev[i]]);
for(int mid=;mid<lim;mid<<=)
{
cpl wn=cpl(cos(Pi/mid),tp*sin(Pi/mid));
for(int j=,len=(mid<<);j<lim;j+=len)
{
cpl w=cpl(,);
for(int k=;k<mid;k++,w=w*wn)
{
cpl x=a[j+k],y=w*a[j+mid+k];
a[j+k]=x+y; a[j+mid+k]=x-y;
}
}
}
if(tp==)return;
for(int i=;i<lim;i++)a[i].x=a[i].x/lim;
}
int upt(int x){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
void work(int l,int r)
{
if(l==r){f[l]=upt(f[l]+s[l]); return;}//f[0]=0...
int len=r-l+,mid=((l+r)>>);
work(l,mid);
int lim=,L=;
while(lim<len)lim<<=,L++;
for(int i=;i<lim;i++)rev[i]=((rev[i>>]>>)|((i&)<<(L-)));
for(int i=l;i<=mid;i++)a[i-l].x=f[i],a[i-l].y=;//y
for(int i=mid-l+;i<lim;i++)a[i].x=,a[i].y=;
for(int i=;i<lim;i++)b[i].x=s[i],b[i].y=;
fft(a,,lim); fft(b,,lim);
for(int i=;i<lim;i++)a[i]=a[i]*b[i];
fft(a,-,lim);
for(int i=mid+;i<=r;i++)f[i]+=(ll)(a[i-l].x+0.5)%mod;
work(mid+,r);
}
int main()
{
while()
{
n=rd(); if(!n)return ;
memset(f,,sizeof f); memset(s,,sizeof s);
for(int i=;i<=n;i++)s[i]=rd()%mod;//%mod!!
work(,n);
printf("%d\n",f[n]);
}
}
hdu 5730 Shell Necklace —— 分治FFT的更多相关文章
- hdu 5730 Shell Necklace [分治fft | 多项式求逆]
hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...
- HDU.5730.Shell Necklace(分治FFT)
题目链接 \(Description\) 有\(n\)个长度分别为\(1,2,\ldots,n\)的珠子串,每个有\(a_i\)种,每种个数不限.求有多少种方法组成长度为\(n\)的串.答案对\(31 ...
- hdu 5730 Shell Necklace——多项式求逆+拆系数FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...
- HDU 5730 Shell Necklace(CDQ分治+FFT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...
- HDU 5730 Shell Necklace cdq分治+FFT
题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...
- hdu 5730 Shell Necklace fft+cdq分治
题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...
- #8 //HDU 5730 Shell Necklace(CDQ分治+FFT)
Description 给出长度分别为1~n的珠子,长度为i的珠子有a[i]种,每种珠子有无限个,问用这些珠子串成长度为n的链有多少种方案 题解: dp[i]表示组合成包含i个贝壳的项链的总方案数 转 ...
- HDU 5730 - Shell Necklace
题意: 给出连续的1-n个珠子的涂色方法 a[i](1<=i<=n), 问长度为n的珠链共有多少种涂色方案 分析: 可以得到DP方程: DP[n] = ∑(i=1,n) (DP[n-i]* ...
- hdu Shell Necklace 5730 分治FFT
Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...
随机推荐
- Bootstrap学习速查表(四) 栅格系统
Bootstrap框架的网格系统工作原理如下: 1.数据行(.row)必须包含在容器(.container)中,以便为其赋予合适的对齐方式和内距(padding).如: 2.在行(.row)中可以添加 ...
- IOS-4-面试题1:黑马程序猿IOS面试题大全
一.多线程网络 1. 多线程的底层实现? 1> 首先搞清楚什么是线程.什么是多线程 2> Mach是第一个以多线程方式处理任务的系统.因此多线程的底层实现机制是基于Mach的线程 3> ...
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- Makefile调用shell应该注意的地方
转载:http://blog.csdn.net/ninlei0115/article/details/9732191 1.在Makefile中只能在target中调用Shell脚本,其他地方是不能输出 ...
- LightOJ 1068 Investigation (数位dp)
problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...
- 修复open-ssl漏洞,升级open-ssl版本
升级openssl环境至openssl-1.0.1g 1.查看源版本 [root@zj ~]# openssl version -a OpenSSL 0.9.8e-fips-rhel5 01 Jul ...
- 目标检测之人头检测(HaarLike Adaboost)---高密度环境下行人检测和统计
实验程序视频 下载 1 问题描述 高密度环境下的行人统计一直没有得到很好的解决,主要原因是对高密度人群中的行人检测和跟踪是一个很难的问题,如下图所示环境,存在的困难包括: 检测方面: 由于人群整体处于 ...
- Oracle学习第三篇—多行函数
0 order by asc/desc 默认升序 order by 列的名字|表达式|别名|序号 把空放在后边:order by desc nulls last 1分组函数--会自动滤空值 count ...
- Android发送验证码的倒计时button
1 直接上图 2 原理 原理非常easy,就是把对应的倒计时逻辑等封装到一个控件中,并向外部提供接口. 3 代码 import java.util.Timer; import java.util.Ti ...
- c# 枚举返回字符串操作
//内部类public static class EnumHelper { public static string GetDescription(Enum value) { if (value == ...