题目背景

模板题,无背景

题目描述

给定 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. Oracle数据库日期格式转换操作

    1. 日期转化为字符串 (以2016年10月20日为例) select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')  strDateTime from dual; ...

  2. 机器学习中正则惩罚项L0/L1/L2范数详解

    https://blog.csdn.net/zouxy09/article/details/24971995 原文转自csdn博客,写的非常好. L0: 非零的个数 L1: 参数绝对值的和 L2:参数 ...

  3. python相关知识/技巧文摘

    python文件和目录操作 python连接mysql数据库 Python字符编码详解 unicode相关介绍

  4. Jboss Jmx-Console和 Jboss web-console安全设置

    1.介绍 如果你暴露你的JBoss服务器通过网络(如通过启动服务器使用选项B 0.0.0.0或者通过改变jboss.bind.address首次出现0.0.0.0在.../jboss/server/d ...

  5. Jmeter入门--关联

    名称解释 关联是请求与请求之间存在数据依赖关系,需要从上一个请求获取下一个请求需要回传回去的数据. 具体方法 Jmeter关联有两种方法:Xpath.正则表达式 方法一:Xpath主要用于响应是格式是 ...

  6. Mysql5.7 的错误日志中最常见的note级别日志解释

          在使用mysql5.7的时候,发现了不少在mysql5.6上不曾见过的日志,级别为note, 最常见的note日志以下三种,下面我们来逐个解释. 第一种,Aborted connectio ...

  7. spring定时,cronExpression表达式解释

    附:cronExpression表达式解释: 0 0 12 * * ?---------------在每天中午12:00触发 0 15 10 ? * *---------------每天上午10:15 ...

  8. Linux查看系统当前字符集

    常用的命令展示 参考当前环境的字符集 方法一: cat /etc/sysconfig/i18n [ssh客户端工具最好也是utf-8,保持一致] 方法二:echo $LANG 设置当前环境的字符集 方 ...

  9. 【2017-01-08】QTimer与QThread的调度时间精度

    在最近的项目开发中,我发现有的人喜欢用QThread来实现需要循环执行的工作流,而有的人又喜欢用QTimer来实现. 在表面上,两种实现方式似乎都可以,但我觉得QTimer的精度可能会有问题,首先看一 ...

  10. oracle查看用户属于哪个表空间

    select username,default_tablespace from dba_users  where username='用户名';