hdu 5830 FFT + cdq分治
Shell Necklace
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 647 Accepted Submission(s): 287
Suppose the shell necklace is a sequence of shells (not a chain end to end). Considering i continuous shells in the shell necklace, I know that there exist different schemes to decorate the i shells together with one declaration of love.
I want to decorate all the shells with some declarations of love and decorate each shell just one time. As a problem, I want to know the total number of schemes.
For each test cases, the first line contains an integer n, meaning the number of shells in this shell necklace, where 1≤n≤105. Following line is a sequence with nnon-negative integer a1,a2,…,an, and ai≤107 meaning the number of schemes to decorate i continuous shells together with a declaration of love.
1 3 7
4
2 2 2 2
0
54
/*
hdu 5830 FFT + cdq分治 problem:
已知长度为i的shells有a[i]种. 求组成长度为n的方案数
f[i]=∑(f[i - j] * a[j]), j∈[1, i];
让你求f[n] % Z 学习参考:http://blog.csdn.net/snowy_smile/article/details/52020971
solve:
首先卷积求出来之后坐标和相等的在同一列.
a1 a2 a3
b1 b2 b3
-->>
a1*b1 a2*b1 a3*b1 |
a1*b2 a2*b2 | a3*b2
a1*b3 | a2*b3 a3*b3
所以可以解决多项式为f[i]=∑(f[i - j] * a[j])这种的问题.但是如果直接暴力的话
必需要n次fft递推出f[n]. 通过上面那个卷积公式可以发现当我们计算f[4]的时候,已经把后面一部分的答案计算了出来.
所以我们可以在计算[l,mid]的时候处理出f[l,mid]对[mid,r]的所有贡献. 那么剩下的就只需要在
[mid,r]中处理就行了.也就是CDQ分治了 hhh-2016-09-22 21:21:08
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <math.h>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 1 << 18;
const int inf = 0x3f3f3f3f;
const int mod = 313;
const double eps = 1e-7;
template<class T> void read(T&num)
{
char CH;
bool F=false;
for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p)
{
if(!p)
{
puts("0");
return;
}
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const double PI = acos(-1.0); struct Complex
{
double x,y;
Complex(double _x = 0.0,double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator-(const Complex &b)const
{
return Complex(x-b.x,y-b.y);
}
Complex operator+(const Complex &b)const
{
return Complex(x+b.x,y+b.y);
}
Complex operator*(const Complex &b)const
{
return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
}
}; void change(Complex y[],int len)
{
int i,j,k;
for(i = 1,j = len/2; i < len-1; i++)
{
if(i < j) swap(y[i],y[j]);
k = len/2;
while(j >= k)
{
j-=k;
k/=2;
}
if(j < k) j+=k;
}
} void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h = 2; h <= len; h <<= 1)
{
Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j = 0; j < len; j+=h)
{
Complex w(1,0);
for(int k = j; k < j+h/2; k++)
{
Complex u = y[k];
Complex t = w*y[k+h/2];
y[k] = u+ t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if(on == -1)
{
for(int i = 0; i < len; i++)
y[i].x /= len;
}
} double dis(int a,int b)
{
return sqrt(a*a + b*b);
} Complex a[maxn];
Complex b[maxn];
int ans[maxn];
int ta[maxn]; void cal(int l,int r)
{
if(l == r)
{
ans[l] = (ans[l]+ta[l])%mod;
return;
}
int mid = (l + r) >> 1;
cal(l,mid);
int len1 = mid-l + 1;
int len2 = r-l + 1;
int len = 1;
while(len < (len1 + len2)) len <<= 1;
for(int i = 0;i < len1;i++) a[i] = ans[l+i];
for(int i = len1;i < len;i++) a[i] = 0;
fft(a,len,1);
for(int i = 0;i < len2;i++) b[i] = ta[i];
for(int i = len2;i < len;i++) b[i] = 0;
fft(b,len,1); for(int i = 0;i < len;i++)
a[i] = a[i] * b[i];
fft(a,len,-1); for(int i = mid + 1;i <= r ;i++)
ans[i] = (ans[i] + int(a[i-l].x + 0.5))%mod; cal(mid+1,r);
} int main()
{
int n;
while(scanf("%d",&n ) != EOF && n)
{
memset(ans,0,sizeof(ans));
for(int i = 1; i <= n;i++){
scanf("%d",&ta[i]);
ta[i] %= mod;
}
cal(1,n);
print(ans[n]);
}
return 0;
}
hdu 5830 FFT + cdq分治的更多相关文章
- hdu 5730 Shell Necklace fft+cdq分治
题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...
- [玲珑OJ1044] Quailty and Binary Operation (FFT+cdq分治)
题目链接 题意:给定两个长度为n的数组a与长度为m的数组b, 给定一个操作符op满足 x op y = x < y ? x+y : x-y. 有q个询问,每次给出询问c,问:有多少对(i, j ...
- HDU5730 FFT+CDQ分治
题意:dp[n] = ∑ ( dp[n-i]*a[i] )+a[n], ( 1 <= i < n) cdq分治. 计算出dp[l ~ mid]后,dp[l ~ mid]与a[1 ~ r-l ...
- hdu 4366 Successor - CDQ分治 - 线段树 - 树分块
Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...
- Boring Class HDU - 5324 (CDQ分治)
Mr. Zstu and Mr. Hdu are taking a boring class , Mr. Zstu comes up with a problem to kill time, Mr. ...
- hdu 5126 stars cdq分治套cdq分治+树状数组
题目链接 给n个操作, 第一种是在x, y, z这个点+1. 第二种询问(x1, y1, z1). (x2, y2, z2)之间的总值. 用一次cdq分治可以将三维变两维, 两次的话就变成一维了, 然 ...
- HDU Shell Necklace CDQ分治+FFT
Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...
- HDU - 5126 stars (CDQ分治)
题目链接 题目大意:一共有Q(1<=Q<=50000)组操作,操作分为两种: 1.在x,y,z处添加一颗星星 2.询问以(x1,y1,z1)与(x2,y2,z2)为左上和右下顶点的矩形之间 ...
- hdu 1541 (cdq分治)
Problem Description Astronomers often examine star maps where stars are represented by points on a p ...
随机推荐
- C博客作业--指针
一.PTA实验作业 题目1:输出月份英文名 1. 本题PTA提交列表 2. 设计思路 3.代码截图 4.本题调试过程碰到问题及PTA提交列表情况说明. 选择这一题是因为这道题的通过率较低.为什么会这样 ...
- 一个C&C++程序的生命历程
翻了好多博客,内容星星点点,没找到我想要的,现在吸取大神精华,加上本人拙见,总结如下: 一个C或C++程序从你开始编写,到结束,整个过程,都做了些什么,请看下文: 先看大体的过程:看图: 我在这里主要 ...
- Scala 快速入门
 Scalable 编程语言 纯正的的面向对象语言 函数式编程语言 无缝的java互操作 scala之父 Martin Odersky 1. 函数式编程 函数式编程(functional progr ...
- ArcGIS地图打印那些事
记录了通过ArcGIS的PringTask进行地图打印,以及借助html2canvas实现屏幕截图的方法.两个方法各有适用的情景.过程中涉及的相关资源和问题给出链接,自行查看. 1.需求背景 地图打印 ...
- js数组string对象api常用方法
charAt() 方法可返回指定位置的字符. stringObject.charAt(index) indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. stringObject ...
- Javascript 装饰器极速指南
pablo.png Decorators 是ES7中添加的JavaScript新特性.熟悉Typescript的同学应该更早的接触到这个特性,TypeScript早些时候已经支持Decorators的 ...
- SpringCloud的应用发布(四)vmvare+linux,防火墙和selinux
一.vmvare网络配置为nat模式 二.vmvare的网络设置为桥接bridge模式 1.linux 网卡的ip获取方式dhcp 三.关闭linux的防火墙和selinux 1.临时关闭防火墙 sy ...
- GIT入门笔记(6)- 向版本库添加文本文件
1.把文本文件添加到版本库? 所有的版本控制系统,其实只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外. 版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词&q ...
- HTML的水平居中和垂直居中解决方案
水平居中:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 让绝对定位的div居中 div { position: a ...
- nginx配置反向代理详细教程(windows版)
内容属于原创,如果需要转载,还请注明地址:http://www.cnblogs.com/j-star/p/8785334.html Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(I ...