题目背景

模板题,无背景

题目描述

给定 22 个多项式 F(x), G(x)F(x),G(x) ,请求出 F(x) * G(x)F(x)∗G(x) 。

系数对 pp 取模,且不保证 pp 可以分解成 p = a \cdot 2^k + 1p=a⋅2k+1 之形式。

输入输出格式

输入格式:

输入共 33 行。
第一行 33 个整数 n, m, pn,m,p ,分别表示 F(x), G(x)F(x),G(x) 的次数以及模数 pp 。
第二行为 n+1n+1 个整数, 第 ii 个整数 a_iai​ 表示 F(x)F(x) 的 i-1i−1 次项的系数。
第三行为 m+1m+1 个整数, 第 ii 个整数 b_ibi​ 表示 G(x)G(x) 的 i-1i−1 次项的系数。

输出格式:

输出 n+m+1n+m+1 个整数, 第 ii 个整数 c_ici​ 表示 F(x) * G(x)F(x)∗G(x) 的 i-1i−1 次项的系数。

输入输出样例

输入样例#1: 复制

5 8 28
19 32 0 182 99 95
77 54 15 3 98 66 21 20 38
输出样例#1: 复制

7 18 25 19 5 13 12 2 9 22 5 27 6 26

说明

1 \leq n \leq 10^5, 0 \leq a_i, b_i \leq 10^9, 2 \leq p \leq 10^9 + 91≤n≤105,0≤ai​,bi​≤109,2≤p≤109+9

MTT不会,

只好用三模数NTT搞

板子题

原理可以看这里

真TM恶心。。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<21, stdin), p1 == p2) ? EOF : *p1++)
#define swap(x,y) x ^= y, y ^= x, x ^= y
#define LL long long
const int MAXN = * 1e6 + ;
using namespace std;
char buf[<<], *p1 = buf, *p2 = buf;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
const int P1 = , P2 = , P3 = , g = ;
const LL PP = 1ll * P1 * P2;
int N, M, P, limit = , L;
int A[MAXN], B[MAXN], C[MAXN], D[MAXN], Ans[][MAXN], r[MAXN];
LL fastmul(LL a, LL b, LL mod) {
a %= mod, b %= mod;
return ((a * b - (LL)((LL)((long double)a / mod * b + 1e-) * mod)) % mod + mod) % mod;
}
int fastpow(int a, int p, int mod) {
int base = ;
while(p) {
if(p & ) base = 1ll * a * base % mod;
a = 1ll * a * a % mod; p >>= ;
}
return base % mod;
}
void NTT(int *A, const int n, const int type, const int mod) {
for(int i = ; i < n; i++)
if(i < r[i]) swap(A[i], A[r[i]]);
for(int mid = ; mid < n; mid <<= ) {
int W = fastpow(type == ? g : fastpow(g, mod - , mod) , (mod - ) / (mid << ), mod);
for(int j = ; j < n; j += (mid << )) {
int w = ;
for(int k = ; k <mid; k++, w = 1ll * w * W % mod) {
int x = A[j + k], y = 1ll * w * A[j + k + mid] % mod;
A[j + k] = (x + y) % mod,
A[j + k + mid] = (x - y + mod) % mod;
}
}
}
if(type == -) {
int inv = fastpow(n, mod - , mod);
for(int i = ; i < n; i++)
A[i] = 1ll * A[i] * inv % mod;
}
} int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
N = read(), M = read(), P = read();
for(int i = ; i <= N; i++) A[i] = read();
for(int i = ; i <= M; i++) B[i] = read(); while(limit <= N + M) limit <<= , L++;
for(int i = ; i <= limit; i++) r[i] = (r[i >> ] >> ) | ((i & ) << (L - )); copy(A, A + N + , C); copy(B, B + M + , D);
NTT(C, limit, , P1); NTT(D, limit, , P1);
for(int i = ; i <= limit; i++) Ans[][i] = 1ll * C[i] * D[i] % P1; memset(C, , sizeof(C)); memset(D, , sizeof(D));
copy(A, A + N + , C); copy(B, B + M + , D);
NTT(C, limit, , P2); NTT(D, limit, , P2);
for(int i = ; i <= limit; i++) Ans[][i] = 1ll * C[i] * D[i] % P2; memset(C, , sizeof(C)); memset(D, , sizeof(D));
copy(A, A + N + , C); copy(B, B + M + , D);
NTT(C, limit, , P3); NTT(D, limit, , P3);
for(int i = ; i <= limit; i++) Ans[][i] = 1ll * C[i] * D[i] % P3; NTT(Ans[], limit, -, P1);
NTT(Ans[], limit, -, P2);
NTT(Ans[], limit, -, P3); for(int i = ; i <= N + M; i++) {
LL A = (fastmul(1ll * Ans[][i] * P2 % PP, fastpow(P2 % P1, P1 - , P1), PP) +
fastmul(1ll * Ans[][i] * P1 % PP, fastpow(P1 % P2, P2 - , P2), PP) ) % PP;
LL K = ((Ans[][i] - A) % P3 + P3) % P3 * fastpow(PP % P3, P3 - , P3) % P3;
printf("%d ",(A % P + ((K % P) * (PP % P)) % P ) % P);
}
return ;
}

洛谷P4245 【模板】MTT(任意模数NTT)的更多相关文章

  1. [洛谷P4245]【模板】任意模数NTT

    题目大意:给你两个多项式$f(x)$和$g(x)$以及一个模数$p(p\leqslant10^9)$,求$f*g\pmod p$ 题解:任意模数$NTT$,最大的数为$p^2\times\max\{n ...

  2. 【模板】任意模数NTT

    题目描述: luogu 题解: 用$fft$水过(什么$ntt$我不知道). 众所周知,$fft$精度低,$ntt$处理范围小. 所以就有了任意模数ntt神奇$fft$! 意思是这样的.比如我要算$F ...

  3. 洛谷 P4245 [模板]任意模数NTT —— 三模数NTT / 拆系数FFT(MTT)

    题目:https://www.luogu.org/problemnew/show/P4245 用三模数NTT做,需要注意时间和细节: 注意各种地方要取模!传入 upt() 里面的数一定要不超过2倍 m ...

  4. 【洛谷P4245】 【模板】任意模数NTT

    三模数 NTT,感觉不是很难写 $?$ 代码借鉴的 https://www.cnblogs.com/Mychael/p/9297652.html code: #include <bits/std ...

  5. 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT

    题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...

  6. Luogu 4245 【模板】任意模数NTT

    这个题还有一些其他的做法,以后再补,先记一下三模数$NTT$的方法. 发现这个题不取模最大的答案不会超过$10^5 \times 10^9 \times 10^9 = 10^{23}$,也就是说我们可 ...

  7. 洛谷4245:【模板】任意模数NTT——题解

    https://www.luogu.org/problemnew/show/P4245 给两个多项式,求其乘积,每个系数对p取模. 参考: 代码与部分理解参考https://www.luogu.org ...

  8. luogu P4245 【模板】任意模数NTT MTT

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...

  9. P4245 【模板】任意模数NTT

    Luogu4245 只要做三次的NTT,快的飞起 普通NTT,做9次 #include<cstdio> #include<cstring> #include<iostre ...

随机推荐

  1. Spark2.x详解

    一.概述 Apache Spark 是一个快速的, 多用途的集群计算系统. 它提供了 Java, Scala, Python 和 R 的高级 API,以及一个支持通用的执行图计算的优化过的引擎. 它还 ...

  2. Dialog向Activity传递数据

    PopupDialog中声明一个内部接口PriorityListener,接口中声明一个回调函数refreshPriorityUI,用于在 Dialog的监听事件触发后刷新Activity的UI显示. ...

  3. CSS深入理解之float(HTML/CSS)

    float的设计初衷仅仅是:为了文字环绕效果 float的包裹与破坏 包裹:收缩.坚挺.隔绝(BFC) 破坏:父元素高度塌陷 <!DOCTYPE html> <html> &l ...

  4. Eigen 学习之块操作

    Eigen 为 Matrix .Array 和  Vector提供了块操作方法.块区域可以被用作 左值 和 右值.在Eigen中最常用的块操作函数是 .block() . block() 方法的定义如 ...

  5. scrapy实战--爬取报刊名称及地址

    目标:爬取全国报刊名称及地址 链接:http://news.xinhuanet.com/zgjx/2007-09/13/content_6714741.htm 目的:练习scrapy爬取数据 学习过s ...

  6. Oracle EBS 验证应收是否等于加税

    --To validate whether a transaction's REC is equal to its REV plus TAX or not --验证应收是否等于收入加税 SELECT ...

  7. 【MySQL】Linux下mysql安装全过程——小白入门篇(含有问题详解)

    本次安装操作在申请的腾讯云上实现(版本:CentOS Linux release 7.4.1708 (Core) ). 根据教程实现(中途各种挖坑,填坑...),地址:http://www.runoo ...

  8. 用C#自定义一个简单的集合

    闲来无聊来自己做了一个简单的'集合',用来加深自己对集合的理解 class listNode { private object value; public listNode(object _value ...

  9. iOS设计模式 - 组合

    iOS设计模式 - 组合 原理图 说明 将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ...

  10. 多台服务器共享session问题

    在现在的大型网站中,如何实现多台服务器中的session数据共享呢 当使用多台服务器架设成集群之后,我们通过负载均衡的方式,同一个用户(或者ip)访问时被分配到不同的服务器上,假设在A服务器登录,如果 ...