题意:(dp[n]=sum_{i=1}^ndp[i]*a[n-i]+a[n]),求dp[n],
题解:分治fft裸题,就是用cdq分治加速fft,因为后面的需要用到前面的dp来算,不可能每次都fft过去,那样复杂度就(O(n^2logn))了
考虑当前枚举到[l,r]区间,左侧是[l,m]对于右侧每一个dp[x],左侧的贡献有(sum_{i=l}^m dp[i]*a[x-i]),那么我们需要快速算出左侧所有dp对右侧每个dp的所有贡献
(x_0|x_1|x_2|...|x_{m-l})
(dp_l|dp_{l+1}|dp_{l+2}|...|dp_{m})
(y_0|y_1|y_2|...|y_{r-l})
(a_1|a_2|a_3|...|a_{r-1})
那么卷积之后就变成了系数就变成
(m-l|m-l+1|...|r-l-1|)
挨个加到对应的dp{m+1->r}里去即可
需要注意的是cdq时每次一定是先把左侧算完,再算右边,(以前的cdq是先算底层,从叶到根,因为要做归并操作),这里是因为对于更新dp[x]的dp[i],dp[i]必须要更新完才能更新dp[x],
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 313
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=400000+10,inf=0x3f3f3f3f;
struct cd{
db x,y;
cd(db _x=0.0,db _y=0.0):x(_x),y(_y){}
cd operator +(const cd &b)const{
return cd(x+b.x,y+b.y);
}
cd operator -(const cd &b)const{
return cd(x-b.x,y-b.y);
}
cd operator *(const cd &b)const{
return cd(x*b.x - y*b.y,x*b.y + y*b.x);
}
cd operator /(const db &b)const{
return cd(x/b,y/b);
}
}x[N<<3],y[N<<3];
int rev[N<<3];
void getrev(int bit)
{
for(int i=0;i<(1<<bit);i++)
rev[i]=(rev[i>>1]>>1) | ((i&1)<<(bit-1));
}
void fft(cd *a,int n,int dft)
{
for(int i=0;i<n;i++)
if(i<rev[i])
swap(a[i],a[rev[i]]);
for(int step=1;step<n;step<<=1)
{
cd wn(cos(dft*pi/step),sin(dft*pi/step));
for(int j=0;j<n;j+=step<<1)
{
cd wnk(1,0);
for(int k=j;k<j+step;k++)
{
cd x=a[k];
cd y=wnk*a[k+step];
a[k]=x+y;a[k+step]=x-y;
wnk=wnk*wn;
}
}
}
if(dft==-1)for(int i=0;i<n;i++)a[i]=a[i]/n;
}
int dp[N],a[N];
void cdq(int l,int r)
{
if(l==r)return ;
int m=(l+r)>>1;
cdq(l,m);
int sz=0;
while((1<<sz)<=(r-l+1))sz++;sz++;
getrev(sz);int len=(1<<sz);
for(int i=0;i<=len;i++)x[i]=y[i]=cd(0,0);
for(int i=l;i<=m;i++)x[i-l]=cd(dp[i],0);
for(int i=1;i<=r-l;i++)y[i-1]=cd(a[i],0);
fft(x,len,1),fft(y,len,1);
for(int i=0;i<=len;i++)x[i]=x[i]*y[i];
fft(x,len,-1);
for(int i=m+1;i<=r;i++)
{
dp[i]+=(x[i-l-1].x+0.5);
dp[i]%=313;
}
cdq(m+1,r);
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]%=313;dp[i]=a[i];
}
cdq(1,n);
printf("%d
",dp[n]);
}
return 0;
}
/********************
********************/