题目

简述:

有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案

题解

设\(f[i]\)表示长度为\(i\)时的方案数

不难得dp方程:

\[f[i] = \sum\limits_{j=0}^{i} a[j] * f[i - j]
\]

考虑转移

直接转移是\(O(n^2)\)的

如何优化?

容易发现这个转移方程非常特别,是一个卷积的形式

考虑fft

分治fft##

分治fft解决的就是这样一个转移方程的快速计算的问题

\[f[i] = \sum\limits_{j=0}^{i} a[j] * f[i - j]
\]

考虑cdq分治的模式:

我们先处理左半区间,然后用左半区间的\(f[i]\)来更新右半区间的答案

具体地,左半区间对右边一个位置\(r\)的贡献是:

\[\sum\limits_{i=l}^{mid} f[i] * a[r - i]
\]

也是一个卷积的形式,为多项式乘积的第\(r\)项

如此我们便可以用\(f[i]\)和\(a[i]\)构造两个多项式,作fft,然后直接将相应位置的值累加到右半边相应位置的\(f[i]\)中去

我们便解决了这道题

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 1000000000,P = 313;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
const double pi = acos(-1);
struct E{
double r,i;
E(){}
E(double a,double b):r(a),i(b){}
E operator =(const int& b){
r = b; i = 0;
return *this;
}
};
inline E operator +(const E& a,const E& b){
return E(a.r + b.r,a.i + b.i);
}
inline E operator -(const E& a,const E& b){
return E(a.r - b.r,a.i - b.i);
}
inline E operator *(const E& a,const E& b){
return E(a.r * b.r - a.i * b.i,a.r * b.i + b.r * a.i);
}
inline E operator *=(E& a,const E& b){
return (a = a * b);
}
inline E operator /(E& a,const double& b){
return E(a.r / b,a.i / b);
}
inline E operator /=(E& a,const double& b){
return (a = a / b);
}
int n,m,L,R[maxn];
void fft(E* a,int f){
for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
for (int i = 1; i < n; i <<= 1){
E wn(cos(pi / i),f * sin(pi / i));
for (int j = 0; j < n; j += (i << 1)){
E w(1,0);
for (int k = 0; k < i; k++,w *= wn){
E x = a[j + k],y = w * a[j + k + i];
a[j + k] = x + y; a[j + k + i] = x - y;
}
}
}
if (f == -1) for (int i = 0; i < n; i++) a[i] /= n;
}
E A[maxn],B[maxn];
int N,a[maxn],f[maxn];
void solve(int l,int r){
if (l == r){
f[l] = (f[l] + a[l]) % P;
return;
}
int mid = l + r >> 1;
solve(l,mid);
n = mid - l + 1;
for (int i = 0; i < n; i++) A[i] = f[l + i];
m = r - l + 1;
for (int i = 0; i < m; i++) B[i] = a[i + 1];
m = n + m; L = 0;
for (n = 1; n <= m; n <<= 1) L++;
for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
fft(A,1); fft(B,1);
for (int i = 0; i < n; i++) A[i] *= B[i];
fft(A,-1);
for (int i = mid + 1; i <= r; i++){
f[i] = (f[i] + (int)floor(A[i - l - 1].r + 0.5) % P) % P;
}
for (int i = 0; i < n; i++) A[i] = B[i] = 0;
solve(mid + 1,r);
}
int main(){
while ((~scanf("%d",&N)) && N){
for (int i = 1; i <= N; i++){
a[i] = read() % P;
f[i] = 0;
}
solve(1,N);
printf("%d\n",f[N]);
}
return 0;
}

hdu5730 Shell Necklace 【分治fft】的更多相关文章

  1. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  2. HDU.5730.Shell Necklace(分治FFT)

    题目链接 \(Description\) 有\(n\)个长度分别为\(1,2,\ldots,n\)的珠子串,每个有\(a_i\)种,每种个数不限.求有多少种方法组成长度为\(n\)的串.答案对\(31 ...

  3. 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] ...

  4. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  5. hdu5730 Shell Necklace

    重温了这道cdq+FFT 讲白了就是不断对 dp[l~mid] 和 sh[1~r] 进行fft 得到 dp[mid+1~r] #include<bits/stdc++.h> using n ...

  6. 【HDU5730】 Shell Necklace

    HDU5730 Shell Necklace 题目大意 已知连续i(1<=i<=n)个贝壳组合成一段项链的方案数a[i],求组合成包含n个贝壳的项链的总方案数. Solution cdq分 ...

  7. 【HDU5730】Shell Necklace(多项式运算,分治FFT)

    [HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[i]\) 求 ...

  8. hdu Shell Necklace 5730 分治FFT

    Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...

  9. HDU - 5730 :Shell Necklace(CDQ分治+FFT)

    Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n b ...

随机推荐

  1. JDBC对数据库的简单操作

    /** * 获取数据库连接 */ public Connection GetConnection(){ Connection connection=null; try { Class.forName( ...

  2. [论文理解] Connectionist Text Proposal Network

    Connectionist Text Proposal Network 简介 CTPN是通过VGG16后在特征图上采用3*3窗口进行滑窗,采用与RPN类似的anchor机制,固定width而只预测an ...

  3. npm 安装插件失败

    由于npm的很多安装包的下载源来自国外网站,所以比较缓慢甚至访问失败. 再此可以用淘宝的镜像文件来安装插件.方法其实很简单:

  4. 直接用bcdedit创建bootmgr数据库和修复启动菜单

    直接用bcdedit创建bootmgr数据库和修复启动菜单 使用下面方法之前需要bcdedit和bootsect两条命令,这两条命令可以到vista或者windows 7安装光盘上获得.bootsec ...

  5. 学习笔记(二):使用 TensorFlow 的起始步骤(First Steps with TensorFlow)

    目录 1.工具包 TensorFlow 张量 (Tensor) 图 (graph) TensorBoard 2.tf.estimator API Estimator 预创建的 Estimator (p ...

  6. 标准C++(2)

    一.类 C++是一种面向对象的语言,它在C语言的基础上添加了一种新的数据结构,类 ——class class是一种复合型的数据结构 它能够由不同类型的变量及函数组成 C++中的class与struct ...

  7. SSH框架面试总结----1

    1:struts2的工作流程 1)客户端浏览器发出HTTP请求. 2)根据web.xml配置,HTTP请求会被FilterDispatcher接收. 3)根据struts.xml,找到对应的Actio ...

  8. [提供可行性脚本] RHEL 7/CentOS 7/Fedora28 重命名网卡名称

    实验说明: 在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各 ...

  9. 阿里云全国快递物流查询api接口

    口地址: https://market.aliyun.com/products/56928004/cmapi021863.html?spm=5176.730005.productlist.d_cmap ...

  10. ESP8266入门学习笔记1:资料获取

    乐鑫官网:https://www.espressif.com/zh-hans/products/hardware/esp8266ex/overview 乐鑫资料:https://www.espress ...