zoukankan      html  css  js  c++  java
  • FFT


    1 HDU 1402 A * B Problem Plus

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 #include <ext/rope>
    15 
    16 using namespace std;
    17 using namespace __gnu_cxx;
    18 
    19 #define REP(i, n) for (int i = 0; i < (n); ++i)
    20 #define eps 1e-9
    21 #define SZ(x) ((int)x.size())
    22 
    23 typedef long long ll;
    24 typedef pair<int, int> pii;
    25 
    26 #define PI acos(-1.0)
    27 const int maxn = 1e5 + 10;
    28 struct Complex {
    29     double x, y;
    30     Complex(double _x = 0.0, double _y = 0.0) { x = _x; y = _y; }
    31     Complex operator - (const Complex &b) const { return Complex(x - b.x, y - b.y); }
    32     Complex operator + (const Complex &b) const { return Complex(x + b.x, y + b.y); }
    33     Complex operator * (const Complex &b) const { return Complex(x * b.x - y * b.y, x * b.y + y * b.x); }
    34 };
    35 char str_A[maxn], str_B[maxn];
    36 Complex A[maxn * 4], B[maxn * 4], ans[maxn * 4];
    37 int ans_final[maxn * 4], len_final = 0;
    38 int N, M, n = 1;
    39 
    40 void fft(Complex a[], int len, int oper) {
    41     for (int i = 1, j = 0; i < len - 1; ++i) {
    42         for (int s = len; ~j & s;) { j ^= s >>= 1; }
    43         if (i < j) { swap(a[i], a[j]); }
    44     }
    45     Complex omega_m, omega, t, u;
    46     int m, m2;
    47     for (int d = 0; (1 << d) < len; ++d) {
    48         m = (1 << d); m2 = m << 1;
    49         omega_m = Complex(cos(PI / m * oper), sin(PI / m * oper));
    50         for (int i = 0; i < len; i += m2) {
    51             omega = Complex(1, 0);
    52             for (int j = 0; j < m; ++j) {
    53                 u = a[i + j]; t = omega * a[i + j + m];
    54                 a[i + j] = u + t; a[i + j + m] = u - t;
    55                 omega = omega * omega_m;
    56             }
    57         }
    58     }
    59     if (oper == -1) { REP(i, N + M - 1) { a[i].x /= len; } }
    60 }
    61 
    62 
    63 int main() {
    64 #ifdef __AiR_H
    65     freopen("in.txt", "r", stdin);
    66 //    freopen("out.txt", "w", stdout);
    67 #endif // __AiR_H
    68     while (scanf("%s %s", str_A, str_B) != EOF) {
    69         memset(ans, 0, sizeof(ans)); memset(ans_final, 0, sizeof(ans_final));
    70         N = strlen(str_A); M = strlen(str_B); n = 1;
    71         while (n < N + M - 1) { n <<= 1; }
    72         REP(i, N) { A[i] = Complex(str_A[N - 1 - i] - '0', 0); }
    73         for (int i = N; i < n; ++i) { A[i] = Complex(0, 0); }
    74         REP(i, M) { B[i] = Complex(str_B[M - 1 - i] - '0', 0); }
    75         for (int i = M; i < n; ++i) { B[i] = Complex(0, 0); }
    76         fft(A, n, 1); fft(B, n, 1);
    77         REP(i, n) { ans[i] = A[i] * B[i]; }
    78         fft(ans, n, -1);
    79         REP(i, N + M) { ans_final[i] = ans[i].x + 0.5; }
    80         REP(i, N + M - 1) { ans_final[i + 1] += ans_final[i] / 10; ans_final[i] %= 10; }
    81         len_final = N + M - 1;
    82         if (ans_final[len_final]) { ++len_final; }
    83         while (ans_final[len_final - 1] == 0 && len_final > 1) { --len_final; }
    84         for (int i = len_final - 1; i > 0; --i) { printf("%d", ans_final[i]); } printf("%d
    ", ans_final[0]);
    85     }
    86     return 0;
    87 }
    View Code

    2 HDU 4609 3-idiots

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 #include <ext/rope>
    15 
    16 using namespace std;
    17 using namespace __gnu_cxx;
    18 
    19 #define REP(i, n) for (int i = 0; i < (n); ++i)
    20 #define eps 1e-9
    21 #define SZ(x) ((int)x.size())
    22 
    23 typedef long long ll;
    24 typedef pair<int, int> pii;
    25 
    26 #define PI acos(-1.0)
    27 const int maxn = 1e5 + 10;
    28 struct Complex {
    29     double x, y;
    30     Complex(double _x = 0.0, double _y = 0.0) { x = _x; y = _y; }
    31     Complex operator - (const Complex &b) const { return Complex(x - b.x, y - b.y); }
    32     Complex operator + (const Complex &b) const { return Complex(x + b.x, y + b.y); }
    33     Complex operator * (const Complex &b) const { return Complex(x * b.x - y * b.y, x * b.y + y * b.x); }
    34 };
    35 int T, n_t, N, M, n;
    36 Complex A[maxn * 4], B[maxn * 4], mul[maxn * 4];
    37 int a[maxn];
    38 ll ans[maxn * 4], sum[maxn * 4];
    39 
    40 void fft(Complex a[], int len, int oper) {
    41     for (int i = 1, j = 0; i < len - 1; ++i) {
    42         for (int s = len; ~j & s;) { j ^= s >>= 1; }
    43         if (i < j) { swap(a[i], a[j]); }
    44     }
    45     Complex omega_m, omega, t, u;
    46     int m, m2;
    47     for (int d = 0; (1 << d) < len; ++d) {
    48         m = (1 << d); m2 = m << 1;
    49         omega_m = Complex(cos(PI / m * oper), sin(PI / m * oper));
    50         for (int i = 0; i < len; i += m2) {
    51             omega = Complex(1, 0);
    52             for (int j = 0; j < m; ++j) {
    53                 u = a[i + j]; t = omega * a[i + j + m];
    54                 a[i + j] = u + t; a[i + j + m] = u - t;
    55                 omega = omega * omega_m;
    56             }
    57         }
    58     }
    59     if (oper == -1) { REP(i, N + M - 1) { a[i].x /= len; } }
    60 }
    61 void init() {
    62     memset(A, 0 ,sizeof(A)); memset(B, 0, sizeof(B));
    63     memset(mul, 0, sizeof(mul)); memset(ans, 0, sizeof(ans)); n = 1;
    64 }
    65 
    66 int main() {
    67 #ifdef __AiR_H
    68     freopen("in.txt", "r", stdin);
    69 //    freopen("out.txt", "w", stdout);
    70 #endif // __AiR_H
    71     scanf("%d", &T);
    72     while (T--) {
    73         scanf("%d", &n_t); init();
    74         for (int i = 1; i <= n_t; ++i) {
    75             scanf("%d", &a[i]); ++A[a[i]].x; ++B[a[i]].x;
    76         }
    77         sort(a + 1, a + n_t + 1); N = M = a[n_t] + 1;
    78         while (n < N + M - 1) { n <<= 1; }
    79         fft(A, n, 1); fft(B, n, 1);
    80         for (int i = 0; i < n; ++i) { mul[i] = A[i] * B[i]; } fft(mul, n, -1);
    81         for (int i = 1; i < N + M; ++i) { ans[i] = (ll)(mul[i].x + 0.5); }
    82         for (int i = 1; i <= n_t; ++i) { --ans[a[i] + a[i]]; }
    83         for (int i = 1; i <= N + M; ++i) { ans[i] /= 2; }
    84         for (int i = 1; i <= N + M; ++i) { sum[i] = sum[i - 1] + ans[i]; }
    85         ll ans1 = 1ll * n_t * (n_t - 1) * (n_t - 2) / 6, ans2 = 0;
    86         for (int i = 1; i <= n_t; ++i) {
    87             ans2 += sum[N + M] - sum[a[i]];
    88             ans2 -= 1ll * (i - 1) * (n_t - i);
    89             ans2 -= n_t - 1;
    90             ans2 -= 1ll * (n_t - i) * (n_t - i - 1) / 2;
    91         }
    92         printf("%.7f
    ", 1.0 * ans2 / ans1);
    93     }
    94     return 0;
    95 }
    View Code


    1 UVALive 4671 K-neighbor substrings

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <algorithm>
      6 #include <queue>
      7 #include <vector>
      8 #include <stack>
      9 #include <map>
     10 #include <set>
     11 #include <cmath>
     12 #include <cctype>
     13 #include <ctime>
     14 #include <ext/rope>
     15 
     16 using namespace std;
     17 using namespace __gnu_cxx;
     18 
     19 #define REP(i, n) for (int i = 0; i < (n); ++i)
     20 #define eps 1e-9
     21 #define SZ(x) ((int)x.size())
     22 #define PI acos(-1.0)
     23 
     24 typedef long long ll;
     25 typedef unsigned long long ull;
     26 typedef pair<int, int> pii;
     27 const int maxn = 1e5 + 10;
     28 struct Complex {
     29     double x, y;
     30     Complex(double _x = 0.0, double _y = 0.0) { x = _x; y = _y; }
     31     Complex operator - (const Complex &b) const { return Complex(x - b.x, y - b.y); }
     32     Complex operator + (const Complex &b) const { return Complex(x + b.x, y + b.y); }
     33     Complex operator * (const Complex &b) const { return Complex(x * b.x - y * b.y, x * b.y + y * b.x); }
     34 };
     35 ull Hash[maxn], Pow[maxn];
     36 set<ull> s;
     37 int k, len1, len2, Case = 0, N, M, n;
     38 char s1[maxn], s2[maxn];
     39 Complex A[maxn * 4], B[maxn * 4], mul[maxn * 4];
     40 int ans1[maxn * 4], ans2[maxn * 4];
     41 
     42 void fft(Complex a[], int len, int oper) {
     43     for (int i = 1, j = 0; i < len - 1; ++i) {
     44         for (int s = len; ~j & s;) { j ^= s >>= 1; }
     45         if (i < j) { swap(a[i], a[j]); }
     46     }
     47     Complex omega_m, omega, t, u;
     48     int m, m2;
     49     for (int d = 0; (1 << d) < len; ++d) {
     50         m = (1 << d); m2 = m << 1;
     51         omega_m = Complex(cos(PI / m * oper), sin(PI / m * oper));
     52         for (int i = 0; i < len; i += m2) {
     53             omega = Complex(1, 0);
     54             for (int j = 0; j < m; ++j) {
     55                 u = a[i + j]; t = omega * a[i + j + m];
     56                 a[i + j] = u + t; a[i + j + m] = u - t;
     57                 omega = omega * omega_m;
     58             }
     59         }
     60     }
     61     if (oper == -1) { REP(i, N + M - 1) { a[i].x /= len; } }
     62 }
     63 int solve(int ans[], char c) {
     64     memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
     65     for (int i = 0; i < len1; ++i) {
     66         if (s1[i] == c) { A[i].x = 1; }
     67     }
     68     for (int i = 0; i < len2; ++i) {
     69         if (s2[i] == c) { B[i].x = 1; }
     70     }
     71     fft(A, n, 1); fft(B, n, 1);
     72     for (int i = 0; i < n; ++i) { mul[i] = A[i] * B[i]; }
     73     fft(mul, n, -1);
     74     for (int i = len2 - 1; i < N + M; ++i) { ans[i] = mul[i].x + 0.5; }
     75 }
     76 
     77 int main() {
     78 #ifdef __AiR_H
     79     freopen("in.txt", "r", stdin);
     80 //    freopen("out.txt", "w", stdout);
     81 #endif // __AiR_H
     82     Pow[0] = 1; ull t; Hash[0] = 0;
     83     for (int i = 1; i < maxn; ++i) { Pow[i] = Pow[i - 1] * 131; }
     84     while (scanf("%d", &k) && k != -1) {
     85         scanf("%s %s", s1, s2); len1 = strlen(s1); len2 = strlen(s2);
     86         if (len1 < len2) { printf("Case %d: 0
    ", ++Case); continue; }
     87         reverse(s2, s2 + len2); s.clear();
     88         N = len1; M = len2; n = 1; while (n < N + M - 1) { n <<= 1; }
     89         solve(ans1, 'a'); solve(ans2, 'b');
     90         for (int i = 0; i < len1; ++i) { Hash[i + 1] = Hash[i] * 131 + s1[i]; }
     91         for (int i = 0; i <= len1 - len2; ++i) {
     92             t = Hash[i + len2] - Hash[i] * Pow[len2];
     93             if (len2 - ans1[i + len2 - 1] - ans2[i + len2 - 1] <= k) {
     94                 s.insert(t);
     95             }
     96         }
     97         printf("Case %d: %d
    ", ++Case, (int)s.size());
     98     }
     99     return 0;
    100 }
    View Code

  • 相关阅读:
    第十一周作业
    第十周作业
    第九周编程
    第十二周作业
    第十一周作业
    第十周作业
    第八周作业
    第七周作业
    第五周作业
    2019春季学期第四周作业
  • 原文地址:https://www.cnblogs.com/zhaoyz/p/7684927.html
Copyright © 2011-2022 走看看