不说别的。

这份NTT跑得比FFT快,不知道为什么。

以下代码针对\(10^5\)的数据范围。

#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read() {
    int a = 0, c = getchar(), w = 1;
    for(; c < '0' || c > '9'; c = getchar()) if(c == '-') w = -1;
    for(; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
    return a * w;
}

const int md = 998244353, gmd = 3;
inline int add(int x, int y) {
    x += y;
    return x >= md ? x - md : x;
}
inline void Add(int& x, int y) {
    x += y;
    if(x >= md) x -= md;
}
inline int sub(int x, int y) {
    x -= y;
    return x < 0 ? x + md : x;
}
inline int mul(int x, int y) {
    return (long long)x*y%md;
}
inline int qpow(int a, int x) {
    int ret = 1;
    while(x) {
        if(x&1) ret = mul(ret, a);
        a = mul(a, a);
        x >>= 1;
    }
    return ret;
}
inline int inv(int x) {
    return qpow(x, md-2);
}

const int maxn = 1<<17;
int w[2][1<<19], invn[1<<18];
void nttinit() {
    for(int i = 0; i <= 18; i++) {
        w[1][1<<i] = w[0][1<<i] = 1;
        int wn = qpow(gmd, (md-1)/(1<<i+1)), invwn = inv(wn);
        for(int j = (1<<i)+1; j < 1<<i+1; j++) {
            w[1][j] = mul(w[1][j-1], wn);
            w[0][j] = mul(w[0][j-1], invwn);
        }
    }
    for(int i = 1; i <= 1<<18; i <<= 1) invn[i] = inv(i);
}
void ntt(int a[], int n, bool typ) {
    for(int i = 1, j = n>>1; i < n; i++) {
        if(i < j) swap(a[i], a[j]);
        for(int k = n>>1; (j^=k) < k; k >>= 1);
    }
    for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; j += i<<1) for(int k = 0; k < i; k++) {
        int u = a[j+k], v = mul(w[typ][i+k], a[j+i+k]);
        a[j+k] = add(u, v);
        a[j+i+k] = sub(u, v);
    }
    if(!typ) for(int i = 0; i < n; i++) a[i] = mul(a[i], invn[n]);
}
int tmp[maxn<<1];
void Mul(int a[], int an, int b[], int bn) {
    if(an <= 48 || bn <= 48) {
        memset(tmp, 0, (an+bn-1)*sizeof(int));
        for(int i = 0; i < an; i++) for(int j = 0; j < bn; j++) Add(tmp[i+j], mul(a[i], b[j]));
        memcpy(a, tmp, (an+bn-1)*sizeof(int));
        return;
    }
    int n = 1;
    while(n < an+bn-1) n <<= 1;
    ntt(a, n, 1); ntt(b, n, 1);
    for(int i = 0; i < n; i++) a[i] = mul(a[i], b[i]);
    ntt(a, n, 0);
}

int n, m;
int a[maxn<<1], b[maxn<<1];

int main() {
    n = read(); m = read();
    nttinit();
    for(int i = 0; i < n+1; i++) a[i] = read();
    for(int i = 0; i < m+1; i++) b[i] = read();
    Mul(a, n+1, b, m+1);
    for(int i = 0; i < n+m+1; i++) printf("%d ", a[i]);
    printf("\n");
    return 0;
}

1640ms。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
inline int read() {
    int a = 0, c = getchar(), w = 1;
    for(; c < '0' || c > '9'; c = getchar()) if(c == '-') w = -1;
    for(; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
    return a * w;
}

const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
struct complex {
    double r, v;
    complex() {}
    complex(double rr, double vv) {r = rr; v = vv;}
};
inline complex operator + (complex a, complex b) {
    return complex(a.r+b.r, a.v+b.v);
}
inline complex operator - (complex a, complex b) {
    return complex(a.r-b.r, a.v-b.v);
}
inline complex operator - (complex x) {
    return complex(-x.r, -x.v);
}
inline complex operator * (complex a, complex b) {
    return complex(a.r*b.r-a.v*b.v, a.r*b.v+a.v*b.r);
}

const int maxn = 1<<17;
void fft(complex a[], int n, bool typ) {
    for(int i = 1, j = n>>1; i < n; i++) {
        if(i < j) swap(a[i], a[j]);
        for(int k = n>>1; (j^=k) < k; k >>= 1);
    }
    for(int i = 1; i < n; i <<= 1) for(int j = 0; j < n; j += i<<1) {
        complex w = complex(1, 0), wn = complex(cos(pi/(double)i), (typ?1:-1)*sin(pi/(double)i));
        for(int k = 0; k < i; k++) {
            complex u = a[j+k], v = w * a[j+i+k];
            a[j+k] = u + v;
            a[j+i+k] = u - v;
            w = w * wn;
        }
    }
    if(!typ) for(int i = 0; i < n; i++) a[i].r /= n;
}
complex tmp[maxn<<1];
void Mul(complex a[], int an, complex b[], int bn) {
    if(an <= 48 || bn <= 48) {
        for(int i = 0; i < an+bn-1; i++) tmp[i] = complex(0, 0);
        for(int i = 0; i < an; i++) for(int j = 0; j < bn; j++) tmp[i+j] = tmp[i+j] + a[i] * b[j];
        for(int i = 0; i < an+bn-1; i++) a[i] = tmp[i];
        return;
    }
    int n = 1;
    while(n < an+bn-1) n <<= 1;
    fft(a, n, 1); fft(b, n, 1);
    for(int i = 0; i < n; i++) a[i] = a[i] * b[i];
    fft(a, n, 0);
}

int n, m;
complex a[maxn<<1], b[maxn<<1];

int main() {
    n = read(); m = read();
    for(int i = 0; i < n+1; i++) a[i] = complex(read(), 0);
    for(int i = 0; i < m+1; i++) b[i] = complex(read(), 0);
    Mul(a, n+1, b, m+1);
    for(int i = 0; i < n+m+1; i++) printf("%d ", int(a[i].r+0.5));
    printf("\n");
    return 0;
}

1919ms。

NTT板子的更多相关文章

  1. FFT && NTT板子

    贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...

  2. Codeforces1096G Lucky Tickets(NTT优化dp)

    设\(f[i][j]\)表示填了\(i\)个数,数位和为\(j\)的方案数 于是方程为: \[f[i][j]=\sum_{k=0}^9 f[i-1][j-k]*[CanUse[k]==1]\] 其中\ ...

  3. CF632E: Thief in a Shop(快速幂+NTT)(存疑)

    A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...

  4. xjoi 2082: 小明的序列

    本文为博主原创文章,未均允许…… 反正我也没法管对吧 www点cnblogs点com/AwD-/ 维护一个序列,初始全为\(1\) 支持两种操作: 1.对于所有的位置\(i\),将它的值乘上\(i + ...

  5. 【比赛游记】FJOI2019瞎打记

    \(\mathrm{day}\) \(-4\) 又是睡到中午才起来,这样下去省选会睡迟的. 然后下午在补 WF2019 的题目,很快就能补完的(大雾). \(\mathrm{day}\) \(-3\) ...

  6. BZOJ 3992 【SDOI2015】 序列统计

    题目链接:序列统计 我来复习板子了……这道题也是我写的第一发求原根啊? 求原根方法: 从小到大依次枚举原根.设当前枚举的原根为\(x\),模数为\(p\),\(p-1\)的质因数分别为\(p_1,p_ ...

  7. PKUWC 2019 自闭记

    PKUWC 2019 自闭记 Day -1 考前天天在隔壁的物竞教室划水(雀魂,能和吉老师一起玩的游戏都是好游戏),没有做题. Day 0 早上8:16的高铁,到广州南居然要6个小时...不知道福州和 ...

  8. 51Nod 1028 大数乘法 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 分析: FFT/NTT板子题... 代码: NTT板子: #inc ...

  9. P5162 WD与积木(多项式求逆+生成函数)

    传送门 题解 比赛的时候光顾着算某一个\(n\)的答案是多少忘了考虑不同的\(n\)之间的联系了--而且我也很想知道为什么推着推着会变成一个二项式反演-- 设\(f_n\)为\(n\)块积木时的总的层 ...

随机推荐

  1. 权限管理系统之项目框架搭建并集成日志、mybatis和分页

    前一篇博客中使用LayUI实现了列表页面和编辑页面的显示交互,但列表页面table渲染的数据是固定数据,本篇博客主要是将固定数据变成数据库数据. 一.项目框架 首先要解决的是项目框架问题,搭建什么样的 ...

  2. [JavaScript] 后端js的模块化规范CommonJs

    CommonJs概述 主要是单个文件定义的变量,函数,类都是私有的,其他文件不可见,单位的作用域 通过 exports(modules.exports)对外暴露接口,通过 require 加载模块 n ...

  3. js密码修改显示与隐藏效果

    一.添加input框 <form class="login_form"> <input class="password inputpwd" i ...

  4. winform中获取指定文件夹下的所有图片

    方法一: C#的IO自带了一个方法DirectoryInfo dir = new DirectoryInfo("文件夹名称");dir.getFiles();//这个方法返回值就是 ...

  5. [PHP] foreach循环的引用赋值可能导致的问题

    foreach($arr as &$value)1.引用赋值符号&,是每次循环的时候,把当前元素变成地址,$value变量就是对应元素的地址,循环结束$value是一个指向最后一个元素 ...

  6. 2019-01-28 [日常]Beyond的歌里最多是"唏嘘"吗? - Python分词+词频

    看了一个Beyond的纪录片, 提到这个. 觉得心有不甘, 于是搜集了24首歌词, 用Python做了简单分词和词频统计. 源码(包括歌词)在: program-in-chinese/study 统计 ...

  7. JS最简单的字符串转数字类型

    以前无休止的写parseInt,特别复杂,现在只需要一个加号,就可以完成对字符串的转换 1 === +"1" amazing!

  8. .NET性能优化小技巧

    .NET 性能优化小技巧 Intro 之前做了短信发送速度的提升,在大师的指导下,发送短信的速度有了极大的提升,学到了一些提升 .NET 性能的一些小技巧 HttpClient 优化 关于使用 Htt ...

  9. FIDDLER的使用方法及技巧总结

    转自: https://www.cnblogs.com/ink-marks/p/6363275.html 一.FIDDLER快速入门及使用场景 Fiddler的官方网站:http://www.fidd ...

  10. linux $参数

    $# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示 ...