zoukankan      html  css  js  c++  java
  • HDU 1402 A * B Problem Plus (FFT)

    题意:给定 a 和 b,求 a * b。

    析:一个FFT的裸板。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #define debug() puts("++++")
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define be begin()
    #define ed end()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    //#define all 1,n,1
    #define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.in", "r", stdin)
    #define freopenw freopen("out.out", "w", stdout)
    using namespace std;
     
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 200000 + 20;
    const int maxm = 1e6 + 10;
    const int mod = 1000000007;
    const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
    const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    inline int readInt(){ int x;  scanf("%d", &x);  return x; }
    
    struct Complex{
      double x, y;
      Complex(double x_ = 0., double y_ = 0.) : x(x_), y(y_) {}
      Complex operator - (const Complex &c) const{
        return Complex(x - c.x, y - c.y);
      }
      Complex operator + (const Complex &c) const{
        return Complex(x + c.x, y + c.y);
      }
      Complex operator * (const Complex &c) const{
        return Complex(x * c.x - y * c.y, x * c.y + c.x * y);
      }
    };
    
    
    void change(Complex *y, int len){
      for(int i = 1, j = (len>>1); i < len-1; ++i){
        if(i < j)  swap(y[i], y[j]);
        int k = len>>1;
        while(j >= k){
          j -= k;
          k >>= 1;
        }
        if(j < k)  j += k;
      }
    }
    
    void fft(Complex *y, int len, int on){
      change(y, len);
      for(int h = 2; h <= len; h <<= 1){
        Complex wn(cos(-on*2*PI/h), sin(-on*2*PI/h));
        for(int j = 0; j < len; j += h){
          Complex w(1, 0);
          for(int k = j; k < j+h/2; ++k){
            Complex u = y[k];
            Complex t = w * y[k+h/2];
            y[k] = u + t;
            y[k+h/2] = u - t;
            w = w * wn;
          }
        }
      }
      if(-1 == on)  for(int i = 0; i < len; ++i)
        y[i].x /= len;
    }
    
    char s1[maxn>>2], s2[maxn>>2];
    int sum[maxn];
    Complex x1[maxn], x2[maxn];
    
    int main(){
      while(scanf("%s %s", s1, s2) == 2){
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        int len = 1;
        while(len < (len1<<1) || len < (len2<<1))  len <<= 1;
        for(int i = 0; i < len1; ++i)
          x1[i] = Complex(s1[len1-i-1]-'0', 0);
        for(int i = len1; i < len; ++i)
          x1[i] = Complex();
        for(int i = 0; i < len2; ++i)
          x2[i] = Complex(s2[len2-1-i]-'0', 0);
        for(int i = len2; i < len; ++i)
          x2[i] = Complex();
        fft(x1, len, 1);
        fft(x2, len, 1);
        for(int i = 0; i < len; ++i)
          x1[i] = x1[i] * x2[i];
        fft(x1, len, -1);
        for(int i = 0; i < len; ++i)
          sum[i] = (int)(x1[i].x + 0.5);
        for(int i = 0; i < len; ++i){
          sum[i+1] += sum[i] / 10;
          sum[i] %= 10;
        }
        len = len1 + len2;
        while(len > 0 && sum[len] <= 0)  --len;
        for(int i = len; i >= 0; --i)  printf("%c", sum[i]+'0');
        printf("
    ");
      }
      return 0;
    }
    

      

  • 相关阅读:
    浏览器内核
    手机端开发适配问题
    关于样式的问题
    nginx和uwsgi的区别和作用
    Flask (七) 部署
    Flask (六) 项目(淘票票)
    Flask (五) RESTful API
    Flask (四) 模型进阶
    Flask (三) 数据迁移
    Flask (二) cookie 与 session 模型
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/9455924.html
Copyright © 2011-2022 走看看