Shell Necklace

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 647    Accepted Submission(s): 287

Problem Description
Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n beautiful shells contains the most sincere feeling for my best lover Arrietty, but even that is not enough.

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.

 
Input
There are multiple test cases(no more than 20 cases and no more than 1 in extreme case), ended by 0.

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.

 
Output
For each test case, print one line containing the total number of schemes module 313(Three hundred and thirteen implies the march 13th, a special and purposeful day).
 
Sample Input
3
1 3 7
4
2 2 2 2
0
 
Sample Output
14
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分治的更多相关文章

  1. hdu 5730 Shell Necklace fft+cdq分治

    题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...

  2. [玲珑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 ...

  3. 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 ...

  4. 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 ...

  5. 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. ...

  6. hdu 5126 stars cdq分治套cdq分治+树状数组

    题目链接 给n个操作, 第一种是在x, y, z这个点+1. 第二种询问(x1, y1, z1). (x2, y2, z2)之间的总值. 用一次cdq分治可以将三维变两维, 两次的话就变成一维了, 然 ...

  7. HDU Shell Necklace CDQ分治+FFT

    Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...

  8. HDU - 5126 stars (CDQ分治)

    题目链接 题目大意:一共有Q(1<=Q<=50000)组操作,操作分为两种: 1.在x,y,z处添加一颗星星 2.询问以(x1,y1,z1)与(x2,y2,z2)为左上和右下顶点的矩形之间 ...

  9. hdu 1541 (cdq分治)

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

随机推荐

  1. 关于from nltk.book import * 报错解决方法

    import nltk nltk.download() 在使用上面命令安装了nltk库并运行下载后,再输入from nltk.book import * 往往会出现这样的错误提示: 出现这种错误往往是 ...

  2. python实现简单tftp(基于udp)

    tftp是基于udp的协议 实现简单的tftp,首先要有tftp的协议图. tftp默认接收端口为69,但每次有连接过来后,tftp会随机分配一个端口来专门为这个连接来服务. 操作码:1.上传 2.下 ...

  3. Web前端性能分析

    Web前端性能通常上代表着一个完全意义上的用户响应时间,包含从开始解析HTML文件到最后渲染完成开始的整个过程,但不包括在输入url之后与服务器的交互阶段.下面是整个过程的各个步骤: 开始解析html ...

  4. (原创)带模板的OLE输出EXCEL

    其实带模板的OLE输出EXCEL就是将要输出的EXCEL中一些拥有固定值(如标题,表头行等)的单元格先填充好数据和设置好格式后作为模板上传到SAP 中.这样后续在输出EXCEL时只需从SAP中将模板下 ...

  5. [NOI2015]软件包管理器

    4621 [NOI2015]软件包管理器  题目等级 : 钻石 Diamond   题目描述 Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过 ...

  6. 吝啬的国度 nyoj

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...

  7. 构建微服务开发环境7————使用Github管理项目代码的版本

    [内容指引] 1.注册GitHub帐号: 2.下载Github Desktop客户端: 3.macOS安装Github Desktop客户端: 4.windows安装Github Desktop客户端 ...

  8. python之路--day11---迭代器和生成器

    迭代: 迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 为什么要有迭代器: 数据类型的取值,字符串,列表,元组依靠索引可以取值,但是字典,集合,文件这些数据类型无法 ...

  9. Linux--初次体验

    关于Linux已经听闻很久的大名了,但是一直没有机会来使用,这次趁着放假的机会,来体验一把Linux吧. 之前使用visuabox和Ubuntu16,但是虚拟机总是不能连接互联网,在虚拟机上面无法上网 ...

  10. Spring知识点回顾(06)Profile 和 条件注解 @Conditional

    1.设定环境中的active profiles 如:DispatcherServerlet的init-param spring.profiles.active=production spring.pr ...