题目背景

模板题,无背景

题目描述

给定 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. 朴素贝叶斯文本分类实现 python cherry分类器

    贝叶斯模型在机器学习以及人工智能中都有出现,cherry分类器使用了朴素贝叶斯模型算法,经过简单的优化,使用1000个训练数据就能得到97.5%的准确率.虽然现在主流的框架都带有朴素贝叶斯模型算法,大 ...

  2. 使用Axure设计中,大型的后台系统原型总结

    使用Axure设计中,大型的后台系统原型总结 2018年4月16日luodonggan 在产品原型设计中,经常会涉及到后台系统原型的设计,如何设计出更规范标准的后台系统原型,是很多产品同行们都会遇到的 ...

  3. [翻译] PPiAwesomeButton

    PPiAwesomeButton https://github.com/pepibumur/PPiAwesomeButton UIButton category with new methods to ...

  4. [控件] TranformFadeView

    TranformFadeView 效果图: 源码地址: https://github.com/YouXianMing/UI-Component-Collection 注意: maskView是iOS8 ...

  5. Windows与Linux 互相访问,挂载过程

    开始使用Linux时浏览器无法访问,多次尝试以失败告终,果断放弃自我动手, 找了大神帮助,弄了半天终于可以访问.但是之前在Windows下的文件也不能放弃,从大神那里那里文档,然后进行尝试 1.在Wi ...

  6. 执行一条sql语句update多条记录实现思路

    如果你想更新多行数据,并且每行记录的各字段值都是各不一样,你会怎么办呢?本文以一个示例向大家讲解下如何实现如标题所示的情况,有此需求的朋友可以了解下 通常情况下,我们会使用以下SQL语句来更新字段值: ...

  7. C++ 读书笔记1

    c++ 笔记1 body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding ...

  8. 内置函数 sorted

    内置函数 sorted 语法: sorted(iterable,key = None,reverse= false)iterable: 可迭代的对象key:排序规则(排序函数),在sorted内部将& ...

  9. 字符串到-->list到-->字典的转变

    怎么把字符串变成字典呢?? 要先转成列表list(用split方法),然后再把列表转成字典,这时候就用到-->怎么把列表转换成字典呢??列表的索引和字典的新增,然后就能把字符串转成字典了.

  10. 利用Jquey.hover来实现 鼠标移入出现删除按钮,鼠标移出删除消失

    Html代码 <div class="box"><div class="bmbox" onclick="$('.box:first' ...