hdu5730Shell Necklace

链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5730

题意:给定a数组,f[i]=sigma(f[i-j]*a[j]),j<i,求f[n]。

分析:分治FFT,这题是学snowy_smile的,他分析的很详细,我就不再重复一遍了。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int mod=313;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
int n,a[N],f[N];
struct Complex{
    db r,i;
    Complex() {}
    Complex(db r,db i):r(r),i(i) {}
    Complex operator + (const Complex &t) const {
        return Complex(r+t.r,i+t.i);
    }
    Complex operator - (const Complex &t) const {
        return Complex(r-t.r,i-t.i);
    }
    Complex operator * (const Complex &t) const {
        return Complex(r*t.r-i*t.i,r*t.i+i*t.r);
    }
}A[2*N],B[2*N];
void FFT(Complex x[],int n,int rev) {
    int i,j,k,t,ds;
    Complex w,u,wn;
    for (i=1;i<n;i++) {
        for (j=0,t=i,k=n>>1;k;k>>=1,t>>=1) j=j<<1|t&1;
        if (i<j) swap(x[i],x[j]);
    }
    for (i=2,ds=1;i<=n;ds=i,i<<=1) {
        w=Complex(1,0);wn=Complex(cos(rev*2*pi/i),sin(rev*2*pi/i));
        for (j=0;j<ds;j++,w=w*wn)
            for (k=j;k<n;k+=i) {
                u=w*x[k+ds];x[k+ds]=x[k]-u;x[k]=x[k]+u;
            }
    }
    if (rev==-1) for (i=0;i<n;i++) x[i].r/=n;
}
void cdq(int l,int r) {
    if (l==r) { (f[l]+=a[l])%=mod;return ; }
    int i,mid=(l+r)>>1,len=1,len1=mid-l+1,len2=r-l;
    cdq(l,mid);
    while (len<len2) len<<=1;
    for (i=0;i<len1;i++) A[i]=Complex(f[l+i],0);
    for (i=len1;i<len;i++) A[i]=Complex(0,0);
    for (i=0;i<len2;i++) B[i]=Complex(a[i+1],0);
    for (i=len2;i<len;i++) B[i]=Complex(0,0);
    FFT(A,len,1);FFT(B,len,1);
    for (i=0;i<len;i++) A[i]=A[i]*B[i];
    FFT(A,len,-1);
    for (i=mid+1;i<=r;i++) (f[i]+=(int)(A[i-l-1].r+0.5))%=mod;
    cdq(mid+1,r);
}
int main()
{
    while (scanf("%d", &n)&&n) {
        memset(f,0,sizeof(f));
        for (int i=1;i<=n;i++) scanf("%d", &a[i]),a[i]%=mod;
        cdq(1,n);
        printf("%d\n", (f[n]+mod)%mod);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值