重温了这道cdq+FFT
讲白了就是不断对 dp[l~mid] 和 sh[1~r] 进行fft 得到 dp[mid+1~r]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+5;
const int MOD = 313; int N;
int sh[MAXN], dp[MAXN];
int ans;
/****************FFT*************/
int A[MAXN<<2], B[MAXN<<2], C[MAXN<<2];
struct FFTSOLVE {
int pos[MAXN<<2];
struct comp {
double r , i ;
comp ( double _r = 0 , double _i = 0 ) : r ( _r ) , i ( _i ) {}
comp operator + ( const comp& x ) {
return comp ( r + x.r , i + x.i ) ;
}
comp operator - ( const comp& x ) {
return comp ( r - x.r , i - x.i ) ;
}
comp operator * ( const comp& x ) {
return comp ( r * x.r - i * x.i , i * x.r + r * x.i ) ;
}
comp conj () {
return comp ( r , -i ) ;
}
} A[MAXN<<2] , B[MAXN<<2] ;
const double pi = acos ( -1.0 ) ;
void FFT ( comp a[] , int n , int t ) {
for ( int i = 1 ; i < n ; ++ i ) if ( pos[i] > i ) swap ( a[i] , a[pos[i]] ) ;
for ( int d = 0 ; ( 1 << d ) < n ; ++ d ) {
int m = 1 << d , m2 = m << 1 ;
double o = pi * 2 / m2 * t ;
comp _w ( cos ( o ) , sin ( o ) ) ;
for ( int i = 0 ; i < n ; i += m2 ) {
comp w ( 1 , 0 ) ;
for ( int j = 0 ; j < m ; ++ j ) {
comp& A = a[i + j + m] , &B = a[i + j] , t = w * A ;
A = B - t ;
B = B + t ;
w = w * _w ;
}
}
}
if ( t == -1 ) for ( int i = 0 ; i < n ; ++ i ) a[i].r /= n ;
}
void mul ( int *a , int *b , int *c ,int k) {
int i , j ;
for ( i = 0 ; i < k ; ++ i ) A[i] = comp ( a[i] , b[i] ) ;
j = __builtin_ctz ( k ) - 1 ;
for ( int i = 0 ; i < k ; ++ i ) {
pos[i] = pos[i >> 1] >> 1 | ( ( i & 1 ) << j ) ;
}
FFT ( A , k , 1 ) ;
for ( int i = 0 ; i < k ; ++ i ) {
j = ( k - i ) & ( k - 1 ) ;
B[i] = ( A[i] * A[i] - ( A[j] * A[j] ).conj () ) * comp ( 0 , -0.25 ) ;
}
FFT ( B , k , -1 ) ;
for ( int i = 0 ; i < k ; ++ i ) {
c[i] = ( long long ) ( B[i].r + 0.5 );
}
}
}boy;
void cdq(int l,int r) {
if(l == r) {
dp[l] = (dp[l] + sh[l]) % MOD;
return;
}
int mid = (l+r)>>1;
cdq(l,mid); int l1 = 0, l2 = 0;
for(int i = l; i <= mid; ++i) A[l1++] = dp[i];
for(int i = 1; i <= N; ++i) {
if(i+l > r) break;
B[l2++] = sh[i];
}
int len = 1;
while(len < l1*2 || len < l2*2) len <<= 1;
for(int i = l1; i < len; ++i) A[i]=0;
for(int i = l2; i < len; ++i) B[i]=0;
boy.mul(A,B,C,len);
for(int i = mid+1; i <= r; ++i) {
dp[i] = (dp[i]+C[i-l-1]) %MOD;
}
cdq(mid+1,r);
}
int main(){
while(~scanf("%d",&N)) {
memset(dp,0,sizeof(dp));
if(N == 0) break;
for(int i = 1; i <= N; ++i) {
scanf("%d",&sh[i]); sh[i] %= MOD;
} cdq(1,N);
printf("%d\n",dp[N]);
}
return 0;
}

hdu5730 Shell Necklace的更多相关文章

  1. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  2. hdu5730 Shell Necklace 【分治fft】

    题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...

  3. 【HDU5730】 Shell Necklace

    HDU5730 Shell Necklace 题目大意 已知连续i(1<=i<=n)个贝壳组合成一段项链的方案数a[i],求组合成包含n个贝壳的项链的总方案数. Solution cdq分 ...

  4. 【HDU5730】Shell Necklace(多项式运算,分治FFT)

    [HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[i]\) 求 ...

  5. 2016 Multi-University Training Contest 1 H.Shell Necklace

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  7. hdu Shell Necklace 5730 分治FFT

    Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...

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

  9. HDU Shell Necklace CDQ分治+FFT

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

随机推荐

  1. Java程序占用的内存可能会大于Xmx

    很多人认为Xmx和-Xms参数指定的就是Java程序将会占用的内存,但是这实际上只是Java堆对象将会占用的内存.堆只是影响Java程序占用内存数量的一个因素. 除了堆,影响Java程序所占用内存的因 ...

  2. 洛谷 [P3384] 树链剖分 模版

    支持各种数据结构上树,注意取膜. #include <iostream> #include <cstring> #include <algorithm> #incl ...

  3. JavaScript之父谈JavaScript

    本文翻译自popularity,为了更好的阅读我把部分内容进行了增删改,如果你英语比较好,建议直接阅读原文,因为这篇文章是我通过google翻译再进行修改的. 貌似(根据一位精神导师的说法)JavaS ...

  4. UbuntuNFS服务器配置

    NFS服务的配置====================1,下载并安装NFS sudo apt-get install nfs-kernel-server 2,配置NFS sudo vi /etc/e ...

  5. zabbix客户端一键安装脚本(主动模式监控)

    #!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...

  6. Linux expect自动登录ssh,ftp

    [http://blog.51yip.com/linux/1462.html#] #!/usr/bin/expect -f set ip 192.168.1.201 set password meim ...

  7. css去除ios文本框默认圆角

    css去除ios文本框默认圆角 input, textarea {-webkit-appearance: none;}

  8. ps通道抠章

    1. 打开图片 2. 使用椭圆形选框工具,选择章所在范围(ALT+SHITF+鼠标左键) 3.复制图层(CTRL+J)为图层1,隐藏背景 4.进入通道,选择对比度最大的通道,复制通道副本 5.反选(C ...

  9. llinux基本指令

    1.关机重启1)关机shutdown -h now (立刻关机)shutdown -h 5 (5分钟后关机)2)重启shutdown -r now (立刻重启)shutdown -r 5 (5分钟后重 ...

  10. 批处理文件:windows下关闭指定端口

    @echo offsetlocal enabledelayedexpansionset /p port=please input port number:for /f "tokens=1-5 ...