[关闭]
@xunuo 2017-01-20T01:31:44.000000Z 字数 1818 阅读 985

少女输出问题(II)-------(FFT)


Time Limit: 5000 MS Memory Limit: 65536 KB

FFT

来源:swust power oj 2540 少女输出问题(II)


Description

浩姐发现大魔王居然没被砍死,于是换了把更长的剑和更长的剑法,再看看输出效果吧……

Input

第一行:两个整数n,m(1 <= n ,m <= 100,000)。代表两个离散序列的长度。
第二行:n个整数A[i](1 <= A[i] <= 100)。代表第一个离散序列。
第三行:m个整数A[i](1 <= B[i] <= 100)。代表第二个离散序列。
给定的两个序列的第一位都是在0位置!未给出的部分的值都是0!

Output

一行:若干个数用空格隔开,为第一个序列卷积第二个序列的非零值部分,请不要输出多余的空格,并且请在行位加上换行符

Sample Input

3
3
1 1 1
1 1 1

Sample Output

1 2 3 2 1

题意:

这是一个中文题......

解题思路:

阳哥的FFT模板......
FFT是啥?------快速傅里叶变换......
你会吗?你不会!!!!呵呵哒~~

完整代码:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<algorithm>
  5. using namespace std;
  6. #define MAXN 400000
  7. const double pi=acos(-1.0);
  8. struct Complex
  9. {
  10. double R, I;
  11. inline Complex(double real = 0.0, double image = 0.0)
  12. {
  13. R=real,I=image;
  14. }
  15. inline Complex operator+(Complex const &b) const
  16. {
  17. return Complex(R+b.R,I+b.I);
  18. }
  19. inline Complex operator-(Complex const &b) const
  20. {
  21. return Complex(R-b.R,I-b.I);
  22. }
  23. inline Complex operator*(Complex const &b) const
  24. {
  25. return Complex(R*b.R-I*b.I,I*b.R+R*b.I);
  26. }
  27. };
  28. inline int turn(int n)
  29. {
  30. int i=1;
  31. for(;i<n;i<<=1);
  32. return i;
  33. }
  34. void Change(Complex y[],int len)
  35. {
  36. int i,j,k;
  37. for(i=1,j=len/2;i<len-1;i++)
  38. {
  39. if(i<j)
  40. swap(y[i],y[j]);
  41. k=len/2;
  42. while(j>=k)
  43. {
  44. j-=k;
  45. k/=2;
  46. }
  47. if(j<k)j+=k;
  48. }
  49. }
  50. void FFT(Complex P[],int n,int op)
  51. {
  52. Change(P,n);
  53. for(int len=2;len<=n;len<<=1)
  54. {
  55. int m=len>>1;
  56. Complex unit=Complex(cos(pi/m*op),sin(pi/m*op));
  57. for(int i=0;i<n;i+=len)
  58. {
  59. Complex W=Complex(1,0);
  60. for(int j=0;j<m;j++,W=W*unit)
  61. {
  62. Complex p=P[i+j],q=P[i+j+m];
  63. P[i+j]=p+W*q;
  64. P[i+j+m]=p-W*q;
  65. }
  66. }
  67. }
  68. }
  69. Complex A[MAXN], B[MAXN];
  70. char s[MAXN];
  71. int ans[MAXN];
  72. int main()
  73. {
  74. int n, m;
  75. while(scanf("%d%d",&n,&m)!=EOF)
  76. {
  77. memset(A,0,sizeof(A));
  78. memset(B,0,sizeof(B));
  79. for(int i=0;i<n;i++)
  80. scanf("%lf",&A[i].R);
  81. for(int i=0;i<m;i++)
  82. scanf("%lf",&B[i].R);
  83. n=max(n,m);
  84. n=turn(n);
  85. FFT(A,n<<1,1);
  86. FFT(B,n<<1,1);
  87. for(int i=0;i<(n<<1);i++)
  88. A[i]=A[i]*B[i];
  89. FFT(A,n<<1,-1);
  90. for(int i=0;i<(n<<1);i++)
  91. {
  92. int ans=A[i].R/(n<<1)+0.5;
  93. if(ans)
  94. {
  95. if(i)
  96. printf(" ");
  97. printf("%d", ans);
  98. }
  99. else
  100. {
  101. printf("\n");
  102. break;
  103. }
  104. }
  105. }
  106. return 0;
  107. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注