zoukankan      html  css  js  c++  java
  • FFT && NTT板子

    贴板子啦……


    FFT板子:luogu P3803 【模板】多项式乘法(FFT)

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 4e6 + 5;
    const db PI = acos(-1);
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, m, len = 1, lim = 0, rev[maxn];
    struct Comp
    {
      db x, y;
      In Comp operator + (const Comp& oth)const
      {
        return (Comp){x + oth.x, y + oth.y};
      }
      In Comp operator - (const Comp& oth)const
      {
        return (Comp){x - oth.x, y - oth.y};
      }
      In Comp operator * (const Comp& oth)const
      {
        return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
      }
      friend In void swap(Comp& a, Comp& b)
      {
        swap(a.x, b.x); swap(a.y, b.y);
      }
    }a[maxn], b[maxn];
    
    In void fft(Comp* a, int flg)
    {
      for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
      for(int i = 1; i < len; i <<= 1)
        {
          Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
          for(int j = 0; j < len; j += (i << 1))
    	{
    	  Comp o = (Comp){1, 0};
    	  for(int k = 0; k < i; ++k, o = o * omg)
    	    {
    	      Comp tp1 = a[k + j], tp2 = o * a[k + j + i];
    	      a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
    	    }
    	}
        }
    }
    
    int main()
    {
      n = read(); m = read();
      for(int i = 0; i <= n; ++i) a[i].x = read();
      for(int i = 0; i <= m; ++i) b[i].x = read();
      while(len <= n + m) len <<= 1, ++lim;
      for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
      fft(a, 1); fft(b, 1);
      for(int i = 0; i < len; ++i) a[i] = a[i] * b[i];
      fft(a, -1);
      for(int i = 0; i <= n + m; ++i) write((int)(a[i].x / len + 0.5)), space; enter;
      return 0;
    }
    

    NTT板子:[luogu P3803 【模板】多项式乘法(FFT)](https://www.luogu.org/problemnew/show/P3803) ```c++ #include #include #include #include #include #include #include #include #include #include using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define In inline typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxn = 4e6 + 5; const ll mod = 998244353; const ll G = 3; inline ll read() { ll ans = 0; char ch = getchar(), last = ' '; while(!isdigit(ch)) last = ch, ch = getchar(); while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); if(last == '-') ans = -ans; return ans; } inline void write(ll x) { if(x < 0) x = -x, putchar('-'); if(x >= 10) write(x / 10); putchar(x % 10 + '0'); }

    int n, m, len = 1, lim = 0, rev[maxn];
    ll a[maxn], b[maxn];

    In ll quickpow(ll a, ll b)
    {
    ll ret = 1;
    for(; b; b >>= 1, a = a * a % mod)
    if(b & 1) ret = ret * a % mod;
    return ret;
    }

    In void ntt(ll* a, int len, int flg)
    {
    for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
    for(int i = 1; i < len; i <<= 1)
    {
    ll gn = quickpow(G, (mod - 1) / (i << 1));
    for(int j = 0; j < len; j += (i << 1))
    {
    ll g = 1;
    for(int k = 0; k < i; ++k, g = g * gn % mod)
    {
    ll tp1 = a[k + j], tp2 = g * a[k + j + i] % mod;
    a[k + j] = (tp1 + tp2) % mod, a[k + j + i] = (tp1 - tp2 + mod) % mod;
    }
    }
    }
    if(flg == 1) return;
    ll inv = quickpow(len, mod - 2); reverse(a + 1, a + len);
    for(int i = 0; i < len; ++i) a[i] = a[i] * inv % mod;
    }

    int main()
    {
    n = read(), m = read();
    for(int i = 0; i <= n; ++i) a[i] = read();
    for(int i = 0; i <= m; ++i) b[i] = read();
    while(len <= n + m) len <<= 1, ++lim;
    for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
    ntt(a, len, 1); ntt(b, len, 1);
    for(int i = 0; i < len; ++i) a[i] *= b[i];
    ntt(a, len, -1);
    for(int i = 0; i <= n + m; ++i) write(a[i]), space; enter;
    return 0;
    }

    </br>
    高精fft这里走:[[CQOI2018]九连环 题解](https://www.cnblogs.com/mrclr/p/10376699.html)
  • 相关阅读:
    初识python 2.x与3.x 区别
    装饰器
    函数的进阶
    Spring Boot启动问题:Cannot determine embedded database driver class for database type NONE
    22.Spring Cloud Config安全保护
    23.Spring Cloud Bus 无法更新问题(踩坑) Spring cloud config server Could not fetch remote for master remote
    24.Spring Cloud之Spring Cloud Config及Spring Cloud Bus
    Spring Boot整合Spring Data Elasticsearch 踩坑
    项目中Spring Security 整合Spring Session实现记住我功能
    32.再谈SpringBoot文件上传
  • 原文地址:https://www.cnblogs.com/mrclr/p/10087471.html
Copyright © 2011-2022 走看看