hdu5730 Shell Necklace 【分治fft】
题目
简述:
有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案
题解
设\(f[i]\)表示长度为\(i\)时的方案数
不难得dp方程:
\]
考虑转移
直接转移是\(O(n^2)\)的
如何优化?
容易发现这个转移方程非常特别,是一个卷积的形式
考虑fft
分治fft##
分治fft解决的就是这样一个转移方程的快速计算的问题
\]
考虑cdq分治的模式:
我们先处理左半区间,然后用左半区间的\(f[i]\)来更新右半区间的答案
具体地,左半区间对右边一个位置\(r\)的贡献是:
\]
也是一个卷积的形式,为多项式乘积的第\(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】的更多相关文章
- 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 DP式:\( f[i] = \sum\limits_{j=1}^{i} f[i-j] * a[j] ...
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- hdu5730 Shell Necklace
重温了这道cdq+FFT 讲白了就是不断对 dp[l~mid] 和 sh[1~r] 进行fft 得到 dp[mid+1~r] #include<bits/stdc++.h> using n ...
- 【HDU5730】 Shell Necklace
HDU5730 Shell Necklace 题目大意 已知连续i(1<=i<=n)个贝壳组合成一段项链的方案数a[i],求组合成包含n个贝壳的项链的总方案数. Solution cdq分 ...
- 【HDU5730】Shell Necklace(多项式运算,分治FFT)
[HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[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 ...
- 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 ...
随机推荐
- 前端面试题总结(三)JavaScript篇
前端面试题总结(三)JavaScript篇 一.谈谈对this的理解? this是一个关键字. this总是指向函数的直接调用者(而非间接调用者). 如果有new关键字,this指向new出来的那个对 ...
- cv2.Laplacian 模糊判断
简介 cv2.Laplacian是用来判断图像模糊度的 函数原型 dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta ...
- SpringMVC归纳
SpringMVC归纳 操作流程 配置前端控制器 在web.xml中配置 配置处理器映射器 在springmvc配置文件中配置 配置处理器适配器 在springmvc配置文件中配置 配置注解适配器和映 ...
- RSA AES 前端JS与后台JAVA的加密解密的是实现
AES CryptoJS 前提是编码方式,key,vi中设置一样,就可以进行跨语言加密解密 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- DB2中创建表
CONNECT TO TEST; CREATE TABLE DB2ADMIN.PERSON ( ID BIGINT NOT NULL , NAME BIGINT , FLAG BIGINT , ADD ...
- Tomcat详细安装配置
1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方网站下载,开源免费,而且带宽也足够.下载会很快. 这是两种不同的下载,一个是普通安装版本,一个是解压安装版本.使用起来是一样的,只是 ...
- java 第11次作业:你能看懂就说明你理解了——this关键字
this 代表当前对象
- SVN:The working copy is locked due to a previous error (一)
使用 Cornerstone 时,碰到如题问题,SVN无法Update.Commit等操作. 解决办法:Working Copies ⟹ '右键' ⟹ Clean 即可解决! 尊重作者劳动成果,转载 ...
- logging console not work
配置 log 信息传输到控制台 参考官网:https://docs.djangoproject.com/en/2.1/topics/logging/ By default, this config o ...
- 拓扑排序 topsort
拓扑排序 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序 ...