HDU 5730: Shell Necklace 分治FFT

Shell Necklace

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


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 1n105. Following line is a sequence with n non-negative integer a1,a2,,an, and ai107 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
Hint
HDU 5730: Shell Necklace 分治FFT_第1张图片 For the first test case in Sample Input, the Figure 1 provides all schemes about it. The total number of schemes is 1 + 3 + 3 + 7 = 14.
 

Author
HIT
 

Source
2016 Multi-University Training Contest 1 


递推式很显然


然而这个东西虽然是卷积 但显然不能直接FFT

所以呢 分治FFT/NTT !

具体来讲就是

每次处理区间 [l,r]

先处理 [l,mid]

对于 [l,mid] 已求出真值 处理其对 [mid+1,r] 取值的影响

之后再处理 [mid+1,r]


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef double db;
typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=10*x+ch-'0';ch=getchar();}
	return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=300100,mod=313;
const db pi=acos(-1);

struct cp
{
	db real,image;
	
	friend cp operator +(const cp &x,const cp &y)
	{return (cp){x.real+y.real,x.image+y.image};}
	
	friend cp operator -(const cp &x,const cp &y)
	{return (cp){x.real-y.real,x.image-y.image};}
	
	friend cp operator *(const cp &x,const cp &y)
	{return (cp){x.real*y.real-x.image*y.image,x.image*y.real+x.real*y.image};}
	
}A[N],B[N];

int R[N];

void fft(cp *x,int lim,int opt)
{
	register int i,j,k,m;
	for(i=0;i>1;
		cp wn=(cp){cos(2*pi*opt/m),sin(2*pi*opt/m)};
		for(i=0;i>1);
	cdq(l,mid);
	
	int len(r-l),lim(1);
	while(lim<=(len+mid-l)) lim<<=1;
	for(int i=0;i>1)+(R[i>>1]>>1),
		A[i].real=A[i].image=B[i].real=B[i].image=0;
	for(int i=l;i<=mid;++i) A[i-l].real=f[i];
	for(int i=0;i<=len;++i) B[i].real=a[i];
	fft(A,lim,1);fft(B,lim,1);
	for(int i=0;i

你可能感兴趣的:(CDQ,FFT,—————————数学)