zoukankan      html  css  js  c++  java
  • 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("
    
    ----------
    
    ")
    #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;
    }
    

  • 相关阅读:
    IPC之util.h源码解读
    新麦装机问题汇
    AngularJS2+调用原有的js脚本(AngularJS脚本跟本地原有脚本之间的关系)
    K60平台智能车开发工作随手记
    苹果手机上下载的文件在哪里?
    Mac电脑C语言开发的入门帖
    Python2中文处理纪要
    比特币核心概念及算法
    将dylib库嵌入macOS应用的方法
    那些令人惊艳的TensorFlow扩展包和社区贡献模型
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11231291.html
Copyright © 2011-2022 走看看