Shell Necklace (dp递推改cdq分治 + fft)
首先读出题意,然后发现这是一道DP,我们可以获得递推式为

然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法,时间复杂度又变成O(n2 * log n),显然不可以。然后又利用c分治思维吧问题进行拆分问题但是,前面求出来的结果对后面的结果会产生影响,所以我们使用cdq分治思想来解决这个问题,时间复杂度变为O(n * log2n)。
#include<bits/stdc++.h>
using namespace std; const double pi = acos(-1.0);
const int mod = ;
const int maxn = 4e5 + ;
int in[maxn], dp[maxn]; struct Complex{
double r,i;
Complex(double r = 0.0, double i = 0.0):r(r),i(i){};
Complex operator+(const Complex &rhs){
return Complex(r + rhs.r, i + rhs.i);
}
Complex operator-(const Complex &rhs){
return Complex(r - rhs.r, i - rhs.i);
}
Complex operator*(const Complex & rhs){
return Complex(r*rhs.r - i*rhs.i, i*rhs.r + r * rhs.i);
}
}x1[maxn],x2[maxn]; void rader(Complex *F, int len){
int j = len >> ;
for(int i = , j = len/; i < len - ; i ++){
if(i < j)swap(F[i], F[j]);
int k = len / ;
while(j >= k){
j -= k; k /= ;
}
if(j < k) j += k;
}
} void FFT(Complex *F, int len, int t){
rader(F, len);
for(int h = ; h <= len; h <<= ){
Complex wn(cos(-t**pi/h), sin(-t**pi/h));
for(int j = ; j < len; j += h){
Complex E(, );
for(int k = j; k < j + h/; k ++){
Complex u = F[k];
Complex v = E * F[k + h/];
F[k] = u + v;
F[k + h/] = u - v;
E = E * wn;
}
}
}
if(t == -)
for(int i = ; i < len; i ++)
F[i].r /= len;
} void cdq(int l, int r){
if(l == r){
dp[l] = (in[l]+dp[l])%mod;
return ;
}
int m = l + r>>;
cdq(l,m);
int len1 = r - l + ;
int len2 = m - l + ;
int len = ;while(len < (len1 + len2)) len <<= ;
for(int i = ; i < len; i ++) x1[i] = x2[i] = Complex(,);
for(int i = ; i < len2; i ++) x1[i] = Complex(dp[i + l], );
for(int i = ; i < len1; i ++) x2[i] = Complex(in[i], );
FFT(x1, len, );FFT(x2, len, ); for(int i = ; i < len; i ++) x1[i] = x1[i] * x2[i];
FFT(x1, len, -);
for(int i = m + ; i <= r; i ++) dp[i] = (dp[i] + (int)(x1[i - l].r + 0.5)) % mod;
cdq(m + , r);
} int main(){
int n;
while(~scanf("%d",&n), n){
for(int i = ; i <= n; i ++){
scanf("%d",&in[i]);
in[i] %= mod;
}
memset(dp, , sizeof(dp));
cdq(, n);
printf("%d\n", dp[n] % mod);
for(int i = ; i <= n; i ++)printf("%d ",dp[i]);printf("\n");
}
return ;
}
Shell Necklace (dp递推改cdq分治 + fft)的更多相关文章
- 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 ...
- hdu2089(数位DP 递推形式)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【BZOJ3456】轩辕朗的城市规划 无向连通图计数 CDQ分治 FFT 多项式求逆 多项式ln
题解 分治FFT 设\(f_i\)为\(i\)个点组成的无向图个数,\(g_i\)为\(i\)个点组成的无向连通图个数 经过简单的推导(枚举\(1\)所在的连通块大小),有: \[ f_i=2^{\f ...
- [BZOJ 3456]城市规划(cdq分治+FFT)
[BZOJ 3456]城市规划(cdq分治+FFT) 题面 求有标号n个点无向连通图数目. 分析 设\(f(i)\)表示\(i\)个点组成的无向连通图数量,\(g(i)\)表示\(i\)个点的图的数量 ...
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 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 Shell Necklace CDQ分治+FFT
Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...
- HDU 5730 Shell Necklace(CDQ分治+FFT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...
- #8 //HDU 5730 Shell Necklace(CDQ分治+FFT)
Description 给出长度分别为1~n的珠子,长度为i的珠子有a[i]种,每种珠子有无限个,问用这些珠子串成长度为n的链有多少种方案 题解: dp[i]表示组合成包含i个贝壳的项链的总方案数 转 ...
随机推荐
- LeetCode 590 N-ary Tree Postorder Traversal 解题报告
题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...
- shell脚本中sqlite3命令查询数据库失败返回空,并将错误信息打印到标准错误输出
shell脚本中sqlite3命令查询数据库失败返回空,并将错误信息打印到标准错误输出 如: #/bin/sh local ret='sqlite3 test.db "select test ...
- nginx 负载均衡5种配置方式
nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成正比, ...
- vue获取内存中的值并写入
<template> <div class="container"> <h3>发表评论</h3> <hr> <te ...
- 4.0-uC/OS-III目录结构
本文章都是基于学习野火STMF4系列的开发板的学习做的,大部分都是开发手册的内容,做笔记用,具体请参考野火官方的开发手册. 1. uC/OS-III 文件结构 ①配置文件,通过定义这些文件里宏的值可以 ...
- Android Studio安装配置
1.首先我们进官网 http://www.android-studio.org/ (注意一下除了SDK外还需要JDK) 2.选择历史版本下载 3.随意选择版本这里笔者选用1.2.1版本,主要下带bu ...
- 【F12】谷歌浏览器--前台效果可以在不访问服务器的前提下直接改样式看效果是否是预期值。
F12-前台效果可以在不访问服务器的前提下直接改样式看效果是否是预期值. 1.Element---页面所有元素,通过它可以做selenium的元素定位,删除页面元素,增加页面属性(通过增加页面属性便于 ...
- linux init命令
init命令用于切换到指定的运行级别,用法如下: [root@localhost ~]# init //关机 [root@localhost ~]# init //切换到单用户模式/救援模式 [roo ...
- 小程序要求的 TLS 版本必须大于等于 1.2
1.打开windows powershell 右击屏幕左下角的开始->所有程序->附件->“Windows PowerShell”. 2.在 PowerShell中运行以 ...
- Spark --- 启动、运行、关闭过程
// scalastyle:off println package org.apache.spark.examples import scala.math.random import org.apac ...