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 ...
随机推荐
- EF生成的实体映射含义
如图: 组合效果: LEFT JOIN 效果: this.HasOptional(t => t.子表) .WithMany(t => t.主表) .HasForeignKey(d => ...
- destoon登录后跳转到指定网址
打开module\member\register.inc.php文件搜索:<input type="hidden" name="forward" valu ...
- CV做直方图的比较说明图形越相似性
#include "opencv/cv.hpp" #include "opencv2/objdetect/objdetect.hpp" #include &qu ...
- Voyager下的Dashboard Widgets
widgets设置,voyager.php下找到'widgets': 'widgets' => [ 'TCG\\Voyager\\Widgets\\UserDimmer', 'TCG\\Voya ...
- 微软与百度合作:win10搜索引擎默认百度
全球最大的中文搜索引擎百度公司与微软公司共同宣布双方展开战略合作.百度并将成为中国市场上Windows 10 Microsoft Edge浏览器的默认主页和搜索引擎.也就是说,将来人们在win10的M ...
- LeetCode(242)Valid Anagram
题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- urlopen SSL证书验证
错误描述: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777) 解决方法 ...
- Linux之ssh服务介绍
一.什么是SSH? 简单说,SSH(Secure Shell Protocol)是一种网络协议,用于计算机之间的加密登录.在默认状态下SSH服务提供俩个服务功能,一个是提供类似telnet远程联机服务 ...
- Linux学习-开放源码的软件安装与升级简介
什么是开放源码.编译程序与可执行文件 我们说过,在 Linux 系统上面,一个文件能不能被执行看的是有没有可执行的那个权限 (具有 x permission),不过,Linux 系统上真 正认识的可执 ...
- 像玩魔兽一样编程——谈VS2010键盘流
早年在学校里的时候,经常玩War3,那时候很痴迷,也经常看sky.moon的一些第一视角,有的时候也会模仿模仿...好吧,往事不堪回首,现在工作了,谈一谈.Net程序猿使用VS的键盘流,如果你不知道s ...