# hdu 5730 2016 Multi-University Training Contest 1 Shell Necklace 解题报告

457人阅读 评论(0)

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<vector>
#include<string>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
using namespace std;

const int Mod = 313;
const int maxn = 410000;
const double pi = acos( -1 );

struct E
{
double x,y;
}a[maxn],b[maxn],temp[maxn],zero;
int ln,n,len,id[maxn],q[maxn],f[maxn];

E operator +( E x,E y ) { x.x += y.x; x.y += y.y; return x; }
E operator -( E x,E y ) { x.x -= y.x; x.y -= y.y; return x; }
E operator *( E x,E y )
{
E ret;
ret.x = x.x*y.x - x.y*y.y;
ret.y = x.y*y.x + x.x*y.y;
return ret;
}

void FFT( E *s,int sig )
{
for( int i=0;i<n;i++ ) temp[i] = s[id[i]];
for( int m=2;m<=n;m<<=1 )
{
int t = m>>1;
E wn = zero;
wn.x = cos( -sig*2*pi/m ); wn.y = sin( -sig*2*pi/m );
for( int j=0;j<n;j+=m )
{
E w = zero;
w.x = 1;
for( int i=0;i<t;i++ )
{
E tx,ty;
tx = temp[i+j], ty = temp[i+j+t]*w;
temp[i+j] = tx+ty;
temp[i+j+t] = tx-ty;
w = w*wn;
}
}
}
if( sig == -1 )
for( int i=0;i<n;i++ ) temp[i].x /= double(n);
for( int i=0;i<n;i++ ) s[i] = temp[i];
}
void cdq( int l,int r )
{
if( l == r )
{
( f[l] += q[l] ) %= Mod;
return ;
}
int mid = ( l+r )>>1;
cdq( l,mid );

for( ln=0,n=1;n<=(r-l+1);n<<=1,ln++ );
for( int i=0;i<n;i++ )
{
int now=0,nowi=i;
for( int j=0;j<ln;j++ )
{
now <<= 1;
now |= ( nowi&1 );
nowi >>= 1;
}
id[i] = now;
}

for( int i=0;i<n;i++ ) a[i] = b[i] = zero;
for( int i=l;i<=mid;i++ ) a[i-l].x = f[i];
for( int i=l;i<=r;i++ ) b[i-l].x = q[i-l+1];
FFT( a,1 ); FFT( b,1 );
for( int i=0;i<n;i++ ) a[i] = a[i]*b[i];
FFT( a,-1 );
for( int i=mid+1;i<=r;i++ ) ( f[i] += int(a[i-l-1].x+0.5) ) %= Mod;
cdq( mid+1,r );
}

int main()
{
zero.x = zero.y = 0;
while( scanf("%d",&len) != EOF )
{
if( len == 0 ) break;
for( int i=1;i<=len;i++ )
{
scanf("%d",&q[i]);
q[i] %= Mod;
f[i] = 0;
}
cdq( 1,len );
printf("%d\n",f[len]);
}

return 0;
}


0
0

个人资料
• 访问：134155次
• 积分：5643
• 等级：
• 排名：第5549名
• 原创：445篇
• 转载：0篇
• 译文：0篇
• 评论：18条
一些大神.....
小伙伴们