zoukankan      html  css  js  c++  java
  • HDU1042 A * B Problem Plus

    A * B Problem Plus

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 24620    Accepted Submission(s): 6271


    Problem Description

    Calculate A * B.
     

    Input

    Each line will contain two integers A and B. Process to end of file.

    Note: the length of each integer will not exceed 50000.
     

    Output

    For each case, output A * B in one line.
     

    Sample Input

    1 2 1000 2
     

    Sample Output

    2 2000
     

    分析

    一直不过,最后发现数组开小了qwq。

    思路:把每个数分解成多项式

    $n = a_0*10^0+a_1*10^1+...a_k*10^k$

    然后多项式乘法,FFT模板题。

    code 

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<iostream>
     6 
     7 using namespace std;
     8 
     9 const int N = 200100;
    10 const double pi = acos(-1.0);
    11 char a[N],b[N];
    12 int ans[N]; //数组大小!
    13 
    14 struct Complex{
    15     double x,y;
    16     Complex() {x=0,y=0;}
    17     Complex(double _x,double _y) {x = _x,y = _y;}
    18 }A[N],B[N];
    19 Complex operator + (Complex a,Complex b) {
    20     return Complex(a.x+b.x,a.y+b.y);
    21 }
    22 Complex operator - (Complex a,Complex b) {
    23     return Complex(a.x-b.x,a.y-b.y);
    24 }
    25 Complex operator * (Complex a,Complex b) {
    26     return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
    27 }
    28 void FFT(Complex *a,int n,int ty) {
    29     for (int i=0,j=0; i<n; ++i) {
    30         if (i < j) swap(a[i],a[j]);
    31         for (int k=n>>1; (j^=k)<k; k>>=1); //妙啊!!!
    32     }
    33     for (int m=2; m<=n; m<<=1) {
    34         Complex w1 = Complex(cos(2*pi/m),ty*sin(2*pi/m));
    35         for (int i=0; i<n; i+=m) {
    36             Complex w = Complex(1,0);
    37             for (int k=0; k<(m>>1); ++k) {
    38                 Complex t = w * a[i+k+(m>>1)];
    39                 a[i+k+(m>>1)] = a[i+k] - t;
    40                 a[i+k] = a[i+k] + t;
    41                 w = w * w1;
    42             }
    43         }
    44     }
    45 }
    46 int main () {
    47     while (scanf("%s%s",a,b)!=EOF) {
    48         int len1 = strlen(a),len2 = strlen(b);
    49         int n = 1;
    50         while (n < (len1+len2)) n <<= 1;
    51         for (int i=0; i<n; ++i) {
    52             if (i < len1) A[i] = Complex(a[len1-i-1]-'0',0);
    53             else A[i] = Complex(0,0);
    54             if (i < len2) B[i] = Complex(b[len2-i-1]-'0',0);
    55             else B[i] = Complex(0,0);
    56         }
    57         FFT(A,n,1);
    58         FFT(B,n,1);
    59         for (int i=0; i<n; ++i) A[i] = A[i] * B[i];
    60         FFT(A,n,-1);
    61         for (int i=0; i<n; ++i) 
    62             ans[i] = (int)(A[i].x/n+0.5);
    63         for (int i=0; i<n-1; ++i) {
    64             ans[i+1] += (ans[i]/10);
    65             ans[i] %= 10;
    66         }
    67         bool fir = false;
    68         for (int i=n-1; i>=0; --i) {
    69             if (ans[i]) printf("%d",ans[i]),fir = true;
    70             else if (fir || i==0) printf("0");
    71         }
    72         puts("");
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    金融系列10《发卡行脚本》
    金融系列9《发卡行认证》
    金融系列8《应用密文产生》
    ED/EP系列5《消费指令》
    ED/EP系列4《圈存指令》
    ED/EP系列2《文件结构》
    ED/EP系列1《简介》
    社保系列11《ATR》
    社保系列3《文件结构》
    社保系列2《文件系统》
  • 原文地址:https://www.cnblogs.com/mjtcn/p/8454477.html
Copyright © 2011-2022 走看看