洛谷P4245 【模板】MTT(任意模数NTT)
题目背景
模板题,无背景
题目描述
给定 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 次项的系数。
输入输出样例
5 8 28
19 32 0 182 99 95
77 54 15 3 98 66 21 20 38
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)的更多相关文章
- [洛谷P4245]【模板】任意模数NTT
题目大意:给你两个多项式$f(x)$和$g(x)$以及一个模数$p(p\leqslant10^9)$,求$f*g\pmod p$ 题解:任意模数$NTT$,最大的数为$p^2\times\max\{n ...
- 【模板】任意模数NTT
题目描述: luogu 题解: 用$fft$水过(什么$ntt$我不知道). 众所周知,$fft$精度低,$ntt$处理范围小. 所以就有了任意模数ntt神奇$fft$! 意思是这样的.比如我要算$F ...
- 洛谷 P4245 [模板]任意模数NTT —— 三模数NTT / 拆系数FFT(MTT)
题目:https://www.luogu.org/problemnew/show/P4245 用三模数NTT做,需要注意时间和细节: 注意各种地方要取模!传入 upt() 里面的数一定要不超过2倍 m ...
- 【洛谷P4245】 【模板】任意模数NTT
三模数 NTT,感觉不是很难写 $?$ 代码借鉴的 https://www.cnblogs.com/Mychael/p/9297652.html code: #include <bits/std ...
- 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT
题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...
- Luogu 4245 【模板】任意模数NTT
这个题还有一些其他的做法,以后再补,先记一下三模数$NTT$的方法. 发现这个题不取模最大的答案不会超过$10^5 \times 10^9 \times 10^9 = 10^{23}$,也就是说我们可 ...
- 洛谷4245:【模板】任意模数NTT——题解
https://www.luogu.org/problemnew/show/P4245 给两个多项式,求其乘积,每个系数对p取模. 参考: 代码与部分理解参考https://www.luogu.org ...
- luogu P4245 【模板】任意模数NTT MTT
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- P4245 【模板】任意模数NTT
Luogu4245 只要做三次的NTT,快的飞起 普通NTT,做9次 #include<cstdio> #include<cstring> #include<iostre ...
随机推荐
- Myeclipse使用过程配置汇总
1.下载安装及破解方法 myeclipse2014专业版下载地址链接:https://pan.baidu.com/s/1i62YOGt 密码:nlqj : 下载后安装到最后一步先不要打开软件,如 ...
- qemu模拟vexpress-a9及u-boot引导 linux
前言 本文讲述使用 qemu 来模拟 vexpress-a9 开发板 ,同时介绍使用 u-boot 引导 linux 的流程.整个坐下来对 qemu 和 u-boot 以及嵌入式 linux 的工作方 ...
- 多个raq导出一个excel2007中
需求描述: 客户以前通过润乾API把多个raq模板数据来导出到一个excel文件中,由于现在数据量过大一个raq数据就超过了65535,原来的2003接口已经满足不了现在的需求, ...
- 深入浅出React的一些细节——State
(来源于: https://facebook.github.io/react/docs/state-and-lifecycle.html 翻译by:@TimRChen) Using State Cor ...
- 【转】SSL协议详解
作者:朱祁林 出处:http://zhuqil.cnblogs.com 原文链接 背景介绍 最近在看<密码学与网络安全>相关的书籍,这篇文章主要详细介绍一下著名的网络安全协议SSL. ...
- Vue2学习笔记:数据交互vue-resource
基本语法 必须引入一个库:vue-resource github地址 // 基于全局Vue对象使用http Vue.http.get('/someUrl', [options]).then(succe ...
- 转:Window_Open详解
引:Window_Open详解一.window.open()支持环境:JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法:window.op ...
- UIButton的resizableImageWithCapInsets使用解析
UIButton的resizableImageWithCapInsets使用解析 效果: 使用的源文件: 源码: // // ViewController.m // SpecialButton // ...
- Elasticsearch部分节点不能发现集群(脑裂)问题处理
**现象描述** es1,es2,es3三台es组成一个集群,集群状态正常, 当es1 服务器重启后,es1不能加到集群中,自己选举自己为master,这就产生了es集群中所谓的“脑裂” , 把es1 ...
- 17 汽车服务工程 李腾飞 MP4