zoukankan      html  css  js  c++  java
  • fft模板

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cmath> 
     4 #include <complex>
     5 using namespace std;
     6 const int N = 4e6 + 5;
     7 const double pi = acos(-1);
     8 typedef complex<double> cd;
     9 int n, m, l, r[N];
    10 cd a[N], b[N];
    11 void fft(cd *c, int type){
    12     for(int i = 0; i < n; i++)
    13     if(i < r[i]) swap(c[i], c[r[i]]);
    14     for(int i = 1; i < n; i <<= 1){
    15         cd x(cos(pi / i), type * sin(pi / i));
    16         for(int j = 0; j < n; j += (i << 1)){
    17             cd y(1, 0);
    18             for(int k = 0; k < i; k++, y *= x){
    19                 cd p = c[j + k], q = y * c[i + j + k];
    20                 c[j + k] = p + q, c[i + j + k] = p - q;
    21             }
    22         }
    23     }
    24 }
    25 
    26 inline void init(){
    27     scanf("%d %d", &n, &m);
    28     for(int i = 0; i <= n; i++) {
    29         int x; scanf("%d", &x);
    30         a[i] = x;
    31     }
    32     for(int i = 0; i <= m; i++) {
    33         int x; scanf("%d", &x);
    34         b[i] = x;
    35     }
    36     for(m += n, n = 1; n <= m; n <<= 1) l++;
    37 }
    38 
    39 inline void cal(){
    40     for(int i = 0; i < n; i++)
    41         r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
    42 }
    43 
    44 int main(){
    45     init();
    46     cal();
    47     fft(a, 1); fft(b, 1);
    48     for(int i = 0; i <= n; i++) a[i] *= b[i];
    49     fft(a, -1);
    50     for(int i = 0; i <= m; i++)
    51         printf("%d ", (int)(a[i].real() / n + 0.5));
    52     return 0;    
    53 }
    fft
  • 相关阅读:
    jquery 应用
    SQL Server表分区
    .NET Framework 各版本区别
    后台添加控件时,必须每次重画控件,才能从前台获取控件数据。
    SVN文件库移植(转)
    C# WebService 的缓存机制
    OpenGL C#绘图环境配置
    java 调用webservice的各种方法总结
    SQLServer锁的概述
    C# Word 类库
  • 原文地址:https://www.cnblogs.com/hjmmm/p/9266325.html
Copyright © 2011-2022 走看看