hdu 5730 (CDQ+FFT)模板题

Shell Necklace

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


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

题意:

一段长为i的项链有a[i]中装饰方法,问长度为n的项链有多少种装饰方式。

思路:

容易推出,dp[i]=∑dp[j]*a[i-j],(1<=j<=i-1)那么这样就刚好符合卷积的运算,这样就可以愉快地使用fft了,不过数量级在1e5,所以应该采用分治来处理,算法复杂度nlognlogn。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <cmath>
using namespace std;

const double PI = acos(-1.0);
const int maxn = 100010 * 4;
struct complex
{
    double r,i;
    complex(double _r = 0.0,double _i = 0.0)
    {
        r = _r; i = _i;
    }
    complex operator +(const complex &b)
    {
        return complex(r+b.r,i+b.i);
    }
    complex operator -(const complex &b)
    {
        return complex(r-b.r,i-b.i);
    }
    complex operator *(const complex &b)
    {
        return complex(r*b.r-i*b.i,r*b.i+i*b.r);
    }
};
/*
 * 进行FFT和IFFT前的反转变换。
 * 位置i和 (i二进制反转后位置)互换
 * len必须去2的幂
 */
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]);
        //交换互为小标反转的元素,i<j保证交换一次
        //i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
        k = len/2;
        while( j >= k)
        {
            j -= k;
            k /= 2;
        }
        if(j < k) j += k;
    }
}
/*
 * 做FFT
 * len必须为2^k形式,
 * on==1时是DFT,on==-1时是IDFT
 */
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].r /= len;
}

complex x1[maxn], x2[maxn];
void getfft(int arg1[], int len1, int arg2[], int len2, int fft_out[])//模板:第一个数组,数组元素数量。 第二个数组,数组元素数量,输出数组
{
    int len = 1;
    while(len < len1*2 || len < len2*2)len<<=1;
    for(int i = 0;i < len1;i++)
        x1[i] = complex(arg1[i] , 0);
    for(int i = len1;i < len;i++)
        x1[i] = complex(0 , 0);
    for(int i = 0;i < len2;i++)
        x2[i] = complex(arg2[i] , 0);
    for(int i = len2;i < len;i++)
        x2[i] = complex(0, 0);
    fft(x1,len,1);
    fft(x2,len,1);
    for(int i = 0;i < len;i++)
        x1[i] = x1[i]*x2[i];
    fft(x1,len,-1);
    for(int i = 0;i < len;i++)
        fft_out[i] = x1[i].r+0.5;
}



int n;
const int mod = 313;
int a[maxn], f[maxn], sum[maxn];

void init()
{
    memset(f, 0, sizeof(f));
    f[0]=1;
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        a[i] %= 313;
    }
}

void solve(int l ,int r)
{
    if (l == r)
    {
        f[l] = (f[l] + a[l]) % mod;
        //f[l]一定已经求出了
        return;
    }
    int mid = l + (r-l)/2;
    solve(l, mid);
    int len1 = mid -l + 1;
    int len2 = r - l;


    getfft(f+l, len1, a+1, len2, sum);


    for (int i = mid + 1; i <= r; ++ i)
        f[i] = (f[i] + sum[i-(l+1)]) % mod;
    solve(mid + 1, r);
}

void doit()
{
    solve(1, n);
    printf("%d\n", f[n]);
}

int main()
{
    while (~scanf("%d", &n))
    {
        if (!n)    break;
        init();
        doit();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值