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 ...
随机推荐
- class AClass<E extends Comparable>与class AClass<E extends Comaprable<E>>有什么区别?
new ArrayList<>()与new ArrayList()一样 都是为了做限定用的 如果不了解你可以看API 这个Comparable里面有一个方法compareTo(T o) 如 ...
- Android webview Mixed Content无法显示图片解决
转自:http://blog.csdn.net/crazy_zihao/article/details/51557425 前言 在使用WebView加载https资源文件时,如果认证证书不被Andro ...
- easyUI combobox 添加空白项
今天测试反馈了一个问题,希望可以在下拉框下面加一个空白的选项(下拉框用的是combobox方法). 开始分析这个问题: 首先,这个数据都是后台读出来的,那么我在后台直接添加可以么,答案是可以的,如果没 ...
- python 面向对象设计思想发展史
这篇主要说的是程序设计思想发展历史,分为概述和详细发展历史 一,概述 1940年以前:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的 指令和数 据.简单来 ...
- LeetCode题型分类及索引
目录 这是一个对LeetCode题目归类的索引,分类标准参考了July大神的<编程之法>以及LeetCode的tag项.分类可能还不太合理,逐步完善,请见谅~ 题主本人也在一点一点的刷题, ...
- 工频相位无线同步模块PSYN5000系列在高压设备状态检测和局部放电故障定位的应用方案
关键词: PSYN5000,无线同步模块,工频相位,局部放电,在线监测,高压设备,设备状态,故障定位. 前言: 在电力监测领域,出于方便和安全考虑,有些系统不得不采用无线通信的方式,在这样一个无线通信 ...
- SQL Server元数据损坏(metadata corruption)修复
在升级一个SQL Server 2000的数据库时,遇到了一致性错误,其中有几个错误是元数据损坏(metadata corruption),特意研究了一下这个案例,因为以前也零零散散的遇到过一些一致性 ...
- 解决IE8下opacity属性失效问题
由于opacity属性存在兼容性问题,所以在IE8下,用opacity来设置元素的透明度,会失效,从而导致页面的样式问题. 在IE8及其更早的浏览器下,我们可以使用filter属性,来代替opacit ...
- Python内置函数(1)——abs
英文文档: abs(x) Return the absolute value of a number. The argument may be an integer or a floating poi ...
- 使用 HttpClient3.1 和 HtmlParser2.1 开发Crawler
https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/