题目背景

模板题,无背景

题目描述

给定 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. elixir 模块

    模块定义  defmodule 函数定义  def 私有函数  defp  --相当于其他语言 private iex(29)> defmodule Math do...(29)> def ...

  2. 网络I/O模型--07Netty基础

    Netty 是由 JBOSS 提供的一个 Java 开源框架. Netty 提供异步的.事件驱动的网络应用程序框架和工具 ,用以快速开发高性能 . 高可靠性的网络服务器和客户端程序.      Net ...

  3. runloop timer

    RunLoop这个东西,其实我们一直在用,但一直没有很好地理解它,或者甚至没有知道它的存在.RunLoop可以说是每个线程都有的一个对象,是用来接受事件和分配任务的loop.永远不要手动创建一个run ...

  4. 【Android】Retrofit网络请求Service,@Path、@Query、@QueryMap...

    对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...

  5. android:项目迁移error:Please change caller according to com.intellij.....

    迁移到Android Studio中的项目,在运行时有时会在Event Log中报这种错: Please change caller according to com.intellij.openapi ...

  6. web项目启动时,自动执行代码的几种方式

    在项目开发过程中,往往需要一些功能随着项目启动而优先启动,下面我总结几种方式(非spring boot) spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) ...

  7. 【转】教你弄清 OSX 的睡眠模式,以及合法的禁止产生 sleepimage

    原文链接 因为之前用的是网上流传的土法来禁止生成 sleepimage,尝到了苦头,而且2次! 大家知道 OSX 有几种睡眠模式,其中 hibernatemode 可以是 0 (传统睡眠方式,不生成 ...

  8. Java设计模式----初识适配器模式

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  9. virtual memory exhausted: Cannot allocate memory 解决方法

    在阿里云买了个云服务器,内存1G.编译kudu时出现下面的错误: virtual memory exhausted: Cannot allocate memory 问题原因:由于物理内存本身很小,且阿 ...

  10. 转贴:C语言链表基本操作

    http://www.oschina.net/code/snippet_252667_27314#comments 这个代码有很多错误,估计是从老谭书上抄来但是很多还抄错了:对照老谭的书好好研究下.切 ...