• A * B Problem Plus HDU


    A * B Problem Plus

     HDU - 1402 

    第一道FFT...

     1 #include <iostream>
     2 #include <complex>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 const int maxn = 200010;
    10 const double pi = acos(-1.0);
    11 complex<double> omega[maxn], inverse[maxn];
    12 
    13 void init(int n){
    14     for(int i = 0; i < n; i++){
    15         omega[i] = complex<double>(cos(2 * pi / n * i), sin(2 * pi / n * i));
    16         inverse[i] = conj(omega[i]);
    17     }
    18 }
    19 void transform(complex<double> *a, int n, complex<double> *omega){
    20     int k = 0;
    21     while((1 << k) < n) k++;
    22     for(int i = 0; i < n; i++){
    23         int t = 0;
    24         for(int j = 0; j < k; j++) if(i & (1 << j)) t |= (1 << (k - j - 1));
    25         if(i < t) swap(a[i], a[t]);
    26     }
    27     for(int l = 2;  l <= n; l *= 2){
    28         int m = l / 2;
    29         for(complex<double> *p = a; p != a + n; p += l){
    30             for(int i = 0; i < m; i++){
    31                 complex<double> t = omega[n / l * i] * p[m + i];
    32                 p[m + i] = p[i] - t;
    33                 p[i] += t;
    34             }
    35         }
    36     }
    37 }
    38 void dft(complex<double> *a, int n){
    39     transform(a, n, omega);
    40 }
    41 void idft(complex<double> *a, int n){
    42     transform(a, n, inverse);
    43     for(int i = 0; i < n; i++) a[i] /= n;
    44 }
    45 
    46 void mul(int *a, int n1, int *b, int n2, int *res){
    47     int n = 1;
    48     while(n < n1 + n2) n <<= 1;
    49     complex<double> c1[maxn], c2[maxn];
    50     for(int i = 0; i < n1; i++) c1[i].real(a[i]);
    51     for(int i = 0; i < n2; i++) c2[i].real(b[i]);
    52     init(n);
    53     dft(c1, n); dft(c2, n);
    54     for(int i = 0; i < n; i++) c1[i] *= c2[i];
    55     idft(c1, n);
    56     for(int i = 0; i < n1 + n2 - 1; i++) res[i] = (int)(floor(c1[i].real() + 0.5));
    57 }
    58 char s1[maxn], s2[maxn];
    59 int a[maxn], b[maxn], res[maxn];
    60 
    61 void in(){
    62     freopen("in.txt", "w", stdout);
    63     for(int i= 0; i < 100; i++){
    64         cout<<rand() % 131313 <<endl;
    65     }
    66 }
    67 int main(){
    68    // freopen("in.txt", "r", stdin);
    69    // freopen("out.txt", "w", stdout);
    70     while(scanf("%s %s", s1, s2) != EOF){
    71         memset(res, 0, sizeof res);
    72         int n1 = strlen(s1), n2 = strlen(s2);
    73         for(int i = 0; i < n1; i++) a[i] = s1[n1 - i - 1] - '0';
    74         for(int i = 0; i < n2; i++) b[i]  =s2[n2 - i - 1] - '0';
    75         mul(a, n1, b, n2, res);
    76         int m = n1 + n2 -1;
    77         for(int i = 0; i < m; i++) res[i + 1] += res[i] / 10, res[i] %= 10;
    78         if(res[m]) m++;
    79         while(m >= 1 && !res[m - 1]) m--;
    80         for(int i = m - 1; i >= 0; i--) printf("%d", res[i]);
    81         if(m == 0) printf("0");
    82         puts("");
    83     }
    84 }
    View Code
  • 相关阅读:
    记录Log4Net的使用
    利用Ihttpmodel实现网站缓存,解决Server.Transfer 直接输出HTML源代码的问题
    ASP.NET利用byte检测上传图片安全
    通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)-大壮他哥
    winform利用代码将控件置于顶端底端
    查询
    字符数组实例化
    三维数组
    填充和批量替换
    遍历二维数组
  • 原文地址:https://www.cnblogs.com/yijiull/p/8295921.html
Copyright © 2020-2023  润新知