关闭

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

标签: 多校快速傅里叶变换
457人阅读 评论(0) 收藏 举报
分类:



题意大概是说有n个石头,要你分成若干部分,一个部分的数量为k,那么他的价值就是a[k],这n个石头的价值就是每部分的价值的乘积,求所有分这n个石头的方法的价值和,对313取模




然后看完题意可以想到一个简单的DP,设f[i]表示i个石头的价值和,可以得到一个方程 f[i] = ∑f[j]*a[i-j] ,初始化一下f[0]=1,就可以了。

但是看下数据范围发现这样做并不可行,时间复杂度是n^2的,而n最大有10^5,所以考虑用cdq+fft加速,每次cdq里面,递归求完左边的f,用fft算出它对右边f的贡献,然后时间复杂度是 O (n log^2 n) 


打的时候打了int pi .....................

就这一个地方整了我一天

然后读入记得取模,因为a[i]最大10^7,不取模fft的时候可能爆


#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
查看评论

2016 Multi-University Training Contest 9题解报告

2016 Multi-University Training Contest 9题解报告
  • queuelovestack
  • queuelovestack
  • 2016-08-16 17:04
  • 1235

2014 Multi-University Training Contest 解题报告

多校比赛解题报告: 2014 Multi-University Training Contest 8:多校第八场 2014 Multi-University Training Contest 7:多校第七场 2014 Multi-University Training Contest 6:多校第...
  • u011466175
  • u011466175
  • 2014-08-16 11:42
  • 930

Hdu 2015 Multi-University Training Contest8

Hdu 2015 多校
  • u013508213
  • u013508213
  • 2015-08-14 20:07
  • 389

HDU 6069 Counting Divisors(素数筛法+枚举+技巧)——2017 Multi-University Training Contest - Team 4

传送门 Counting DivisorsTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 7...
  • qingshui23
  • qingshui23
  • 2017-08-03 20:34
  • 1239

HDU 3605 Escape 最大流or二分图多重匹配 2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU

Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1122 Accepted Submission(s):
  • power721
  • power721
  • 2011-09-10 09:43
  • 1551

HDU 3602 2012(2010 ACM-ICPC Multi-University Training Contest(16)——Host by NUDT)

//dp[i]表示挣i块钱最少需要的座位数 //01背包的思想,大家可以这样想,对于每个船我们可选可不选,那么这样 //很明显就是一个01背包了。 #include <cstdio> #include <cstring> #include <iostream> #...
  • chen_ze_hua
  • chen_ze_hua
  • 2016-09-14 10:54
  • 181

Tree //2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

Tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 17   Accepted...
  • hqd_acm
  • hqd_acm
  • 2010-08-29 19:23
  • 733

2010 ACM-ICPC Multi-University Training Contest(2)——Host by BUPT

依然是树状数组+离散化+dp http://acm.hdu.edu.cn/showproblem.php?pid=3450 AC #include #include #include #include #define N 100005 #define M 9901 u...
  • smallacmer
  • smallacmer
  • 2012-03-01 21:54
  • 609

【索引】2013 Multi-University Training Contest

2013 Multi-University Training Contest 1 Problem 1001: hdu 4600 Harvest MoonProblem 1002: hdu 4601 Letter TreeProblem 1003: hdu 46...
  • u011328934
  • u011328934
  • 2015-11-14 21:31
  • 688

(更新中.....)2014 Multi-University Training Contest 1总结

总体来说还不错吧,大概挺长一段shiji
  • u013559351
  • u013559351
  • 2014-07-23 22:08
  • 262
    个人资料
    • 访问:134155次
    • 积分:5643
    • 等级:
    • 排名:第5549名
    • 原创:445篇
    • 转载:0篇
    • 译文:0篇
    • 评论:18条