Luogu1919 【模板】A*B Problem升级版(FFT)

简单\(A*B\) \(Problem\),卡精度卡到想女装

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int type = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  type = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= type;
        return *this;
    }
}io;
using namespace std;

const int N = 240000; // how much should I open QAQ ? // Oh, I undersand ! It's influenced by 'limit'
const double pi = acos(-1.0);

struct Complex{
    double x, y;
    Complex (double xx = 0, double yy = 0) {x = xx, y = yy;}
    
    Complex operator + (Complex b){ return Complex(x + b.x, y + b.y); }
    Complex operator - (Complex b){ return Complex(x - b.x, y - b.y); }
    Complex operator * (Complex b){ return Complex(x * b.x - y * b.y, x * b.y + y * b.x); }	
}a[N], b[N];

int r[N];
inline void FFT(int limit, Complex *a, int type){
    R(i,0,limit - 1)
    	if(i < r[i])
    		swap(a[i], a[r[i]]);
    for(register int mid = 1; mid < limit; mid <<= 1){
    	Complex Wn(cos(pi / mid), type * sin(pi / mid));
    	int len = mid << 1;
    	for(register int j = 0; j < limit; j += len){
    		Complex w(1, 0);
    		R(k,0,mid - 1){
    			Complex x = a[j + k], y = w * a[j + mid + k];
    			a[j + k] = x + y;
    			a[j + mid + k] = x - y;
    			w = w * Wn;
    		}
    	}
    }
}

int c[N];
int main(){
//	FileOpen();
	
	int n;
    io >> n;
	--n;
	int m = n << 1;
	
	R(i,0,n) scanf("%1lf", &a[n - i].x);
	R(i,0,n) scanf("%1lf", &b[n - i].x);
    
    int limit = 1, len = 0;
    while(limit <= m){
    	++len;
    	limit <<= 1;
    }
    
    R(i,0,limit - 1){
    	r[i] = (r[i >> 1] >> 1) | ((i & 1) << (len - 1));
    }
        
    FFT(limit, a, 1);
	FFT(limit, b, 1);
    R(i,0,limit){
    	a[i] = a[i] * b[i];
    }
    FFT(limit, a, -1);
    
    R(i,0,limit)
        a[i].x /= limit;
        
    R(i,0,m){
    	c[i] = (int)(a[i].x + 0.1);
    }
        
    R(i,0,m)
        if(c[i] > 9){
        	c[i + 1] += c[i] / 10;
        	c[i] %= 10;
            if(i == m)
				++m;
        }
    while(c[m] == 0) --m;
    
    nR(i,m,0){
		printf("%d", c[i]);    	
    }

    return 0;
}

posted @ 2019-07-23 13:43  邱涵的秘密基地  阅读(148)  评论(0编辑  收藏  举报