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 ...
随机推荐
- bug终结者 团队作业第三周
bug终结者 团队作业第三周 团队展示 队名 bug终结者 队员风采: 杨京典 20162302 风格:先构建框架,在一 一实现,在实现的过程中不断测试和修改. 擅长的技术:拆分问题,使用相对简单的思 ...
- 201621123040《Java程序设计》第3周学习总结
1.本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 面向对象的思想 对象 类 1.2 用思维导图或者Onenote或其他工具将这些关键词组织起来. 掌握的还不够深 ...
- iOS 简易无限滚动的图片轮播器-SDCycleScrollView
@interface ViewController () <</span>SDCycleScrollViewDelegate> @end @implementation Vie ...
- 关于mule中Spring使用中的一个问题
在mule中连接数据库时,大家通常喜欢使用spring的数据库连接以及bean的配置,但是在使用时会出现一些问题,即bean无法找到,这些,就是需要把bean的id属性改成name属性:可能是因为mu ...
- Struts2之配置文件中Action的详细配置
在Struts2之配置一文中,我们知道一个struts配置文件可以分为三部分:常量配置 包含其他配置文件的配置 Action配置 . 这其中 常量配置 和 包含其他配置文件的配置 二 ...
- Environment.getExternalStorageDirectory()
Environment.getExternalStorageDirectory()得到的是storage/emulated/0
- java 细说String
String类内部用一个字符数组表示字符串,实例变量定义为: private final char value[]; String有两个构造方法,可以根据char数组创建String public S ...
- 新概念英语(1-139)Is that you, John?
Lesson 139 Is that you, John? 是你吗,约翰? Listen to the tape then answer this question. Which John Smith ...
- EasyUI easyui-combobox实现数据联动
实现效果:当用户选择了调查地区以后,只显示当前选择地区的频道,如果没有选择地区,那么频道下拉列表是空的.实现效果,如下
- C#微信公众号——自定义菜单
自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单.一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替.自定义菜单的介绍,可以看官方开发文档http://mp. ...