[关闭]
@ljt12138 2017-03-19T00:08:31.000000Z 字数 3464 阅读 862

两道FFT的应用题

解题报告


BZOJ 快速傅里叶之二

题意

计算:

C[k]=ki<n(a[i]b[ik])

思路

正好看到《具体数学》上处理和式的Tricks,虽然热身题也不会做,但碾OI题还是很稳的(Orz神犇高教授)...

对于:

ki<n(a[i]b[ik])

将b数组倒置,即b[i]=b[n1i],原式变为:

c[k]=0i<n,0n+k1i<n(a[i]b[n+k1i])

化简得到

c[k]=kin+k1(a[i]b[n+k1i])

我们想得到形如

c[k]=0ika[i]b[ki]

的形式。只需要将后式中的kk+n1替换:

c[k+n1]=ia[i]b[k+n1i]

条件是

0k+n1i<nkin+k1

所以

c[k]=c[k+n1]=kin+k1a[i]b[k+n1i]

然后用卷积做就好了。

Code

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef complex<double> Complex;
  4. const int MAXN = 300005;
  5. int rev[MAXN];
  6. Complex A[MAXN];
  7. Complex a[MAXN], b[MAXN], c[MAXN];
  8. int n;
  9. void fft(Complex a[], int n, int flag)
  10. {
  11. int lgn = int(log2(n)+0.01);
  12. rev[0] = 0;
  13. for (int i = 1; i < n; i++)
  14. rev[i] = (rev[i>>1]>>1)|((i&1)<<(lgn-1));
  15. for (int i = 0; i < n; i++)
  16. A[rev[i]] = a[i];
  17. Complex u, t;
  18. for (int k = 2; k <= n; k <<= 1) {
  19. Complex dw = Complex(cos(2*M_PI/k), flag*sin(2*M_PI/k));
  20. for (int i = 0; i < n; i += k) {
  21. Complex w = 1;
  22. for (int j = 0; j < k>>1; j++) {
  23. u = A[i+j], t = w*A[i+j+(k>>1)];
  24. A[i+j] = u+t, A[i+j+(k>>1)] = u-t;
  25. w *= dw;
  26. }
  27. }
  28. }
  29. for (int i = 0; i < n; i++)
  30. a[i] = A[i]/Complex(flag==1?1:n,0);
  31. }
  32. int main()
  33. {
  34. scanf("%d", &n);
  35. for (int i = 0; i < n; i++) {
  36. double x, y; scanf("%lf%lf", &x, &y);
  37. a[i] = Complex(x, 0), b[n-i-1] = Complex(y, 0);
  38. }
  39. int nn = 1;
  40. while (nn < n*2) nn <<= 1;
  41. fft(a, nn, 1), fft(b, nn, 1);
  42. for (int i = 0; i < nn; i++) c[i] = a[i]*b[i];
  43. fft(c, nn, -1);
  44. for (int i = 0; i < n; i++) printf("%d\n", int(c[i+n-1].real()+0.01));
  45. return 0;
  46. }

BZOJ3160 万境人踪灭

题意

给定一个ab串,求所有不连续回文子序列的数量和。

思路

由于卷积可以处理关于一个对称轴两侧对称的字符总数,因此将串中的a、b变为0、1,自己和自己做卷积,就可以求出所有对称轴下b所对应数对称的字符总数。然后把a、b变为1、0,求出a的结果,相加即为关于对称轴两边对称的总字符数Ai,则2Ai就是关于其对称的字符串总数。

那么如何去除连续的呢?跑一遍Manacher就好了。复杂度O(nlgn)

Code

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 300005;
  4. typedef complex<double> Complex;
  5. int rev[MAXN];
  6. Complex A[MAXN];
  7. const long long mod = 1000000007ll;
  8. void fft(Complex a[], int n, int flag)
  9. {
  10. rev[0] = 0;
  11. int lgn = int(log2(n)+0.01);
  12. for (int i = 1; i < n; i++)
  13. rev[i] = (rev[i>>1]>>1)|((i&1)<<(lgn-1));
  14. for (int i = 0; i < n; i++)
  15. A[rev[i]] = a[i];
  16. Complex u, t;
  17. for (int k = 2; k <= n; k <<= 1) {
  18. Complex dw = Complex(cos(2*M_PI/k), sin(flag*2*M_PI/k));
  19. for (int i = 0; i < n; i += k) {
  20. Complex w = 1;
  21. for (int j = 0; j < k>>1; j++) {
  22. u = A[i+j], t = A[i+j+(k>>1)]*w;
  23. A[i+j] = u+t, A[i+j+(k>>1)] = u-t;
  24. w *= dw;
  25. }
  26. }
  27. }
  28. for (int i = 0; i < n; i++)
  29. a[i] = A[i]/(flag == 1?1:Complex(n,0));
  30. }
  31. int p[MAXN];
  32. long long manacher(char str[], int n)
  33. {
  34. str[0] = '^', str[++n] = '#';
  35. int id = 0, mx = 0;
  36. long long ans = 0;
  37. for (int i = 1; i <= n; i++) {
  38. if (mx >= i) p[i] = min(mx-i, p[id-(i-id)]);
  39. else p[i] = 0;
  40. while (str[i+p[i]+1] == str[i-p[i]-1]) p[i]++;
  41. if (i+p[i] > mx) id = i, mx = i+p[i];
  42. (ans += p[i]+1) %= mod;
  43. }
  44. ans--;
  45. id = mx = 0;
  46. for (int i = 1; i < n; i++) {
  47. if (mx >= i) p[i] = min(mx-i, p[id-(i-id)]);
  48. else p[i] = 0;
  49. while (str[i+p[i]+1] == str[i-p[i]]) p[i]++;
  50. if (i+p[i] > mx) id = i, mx = i+p[i];
  51. (ans += p[i]) %= mod;
  52. }
  53. return ans;
  54. }
  55. long long power(int a, int n)
  56. {
  57. if (n == 0) return 1;
  58. long long p = power(a, n>>1);
  59. (p *= p) %= mod;
  60. if (n&1) (p *= a) %= mod;
  61. return p;
  62. }
  63. char str[MAXN];
  64. Complex a[MAXN], c[MAXN];
  65. int cp[MAXN];
  66. int main()
  67. {
  68. scanf("%s", str+1);
  69. int len = strlen(str+1), n;
  70. for (n = 1; n < (len+1)*2; n<<=1);
  71. long long sub = manacher(str, len), ans = 0;
  72. for (int i = 1; i <= len; i++) a[i] = str[i] == 'a';
  73. fft(a, n, 1);
  74. for (int i = 0; i < n; i++) a[i] *= a[i];
  75. fft(a, n, -1);
  76. for (int i = 0; i < n; i++) cp[i] = int(a[i].real()+0.1), a[i] = 0;
  77. for (int i = 1; i <= len; i++) a[i] = str[i] == 'b';
  78. fft(a, n, 1);
  79. for (int i = 0; i < n; i++) a[i] *= a[i];
  80. fft(a, n, -1);
  81. for (int i = 0; i < n; i++) cp[i] += int(a[i].real()+0.1);
  82. for (int i = 0; i < n; i++) (ans += power(2, (cp[i]+1)/2)-1) %= mod;
  83. cout << ((ans-sub)%mod+mod)%mod << endl;
  84. return 0;
  85. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注