这个题还有一些其他的做法,以后再补,先记一下三模数$NTT$的方法。

发现这个题不取模最大的答案不会超过$10^5 \times 10^9 \times 10^9 = 10^{23}$,也就是说我们可以取三个满足$NTT$性质的模数先算然后再合并起来。

比如三个模数可以分别取$998244353, 1004535809, 469762049$。

那么我们现在要做的就是合并三个同余方程:

$$x \equiv a_1(\mod P_1)$$

$$x \equiv a_2(\mod P_2)$$

$$x \equiv a_3(\mod P_3)$$

直接上$crt$的话会爆$long \ long$,我们需要一些其他技巧。

先用$crt$合并前两个方程,记

$$t = a_1P_2 \times inv(P_2, P_1) + a_2P_1 \times inv(P_1, P_2) $$

相当于

$$x \equiv t (\mod M = P_1P_2)$$

我们设$x = kM + t$,代入第三个方程,

$$kM + t \equiv a_3(\mod P_3)$$

可以解

$$k \equiv (a_3 - t) \times inv(M, P_3) (\mod P_3)$$

最后代回去算出$kM + t$即可。

在计算$t$的时候需要快速乘。

时间复杂度$O(nlogn)$。

注意到几个逆元没有必要计算多次,可以节省大量常数;用$O(1)$快速乘也可以大大加快速度。

Code:

// luogu-judger-enable-o2
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 3e5 + ;
const ll Mod[] = {998244353LL, 1004535809LL, 469762049LL}; int n, m, lim = , pos[N];
ll a[N], b[N], tmp[N], ans[][N]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for (; ch > '' || ch < ''; ch = getchar())
if (ch == '-') op = -;
for (; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} template <typename T>
inline void swap(T &x, T &y) {
T t = x; x = y; y = t;
} inline ll fmul(ll x, ll y, ll P) {
ll res = 0LL;
for (; y; y >>= ) {
if (y & ) res = (res + x) % P;
x = (x + x) % P;
}
return res;
} inline ll fpow(ll x, ll y, ll P) {
ll res = 1LL;
/* for (; y > 0; y >>= 1) {
if (y & 1) res = fmul(res, x, P);
x = fmul(x, x, P);
} */ for (; y > ; y >>= ) {
if (y & ) res = res * x % P;
x = x * x % P;
} return res;
} inline ll getInv(ll x, ll y) {
return fpow(x % y, y - , y);
} inline void prework() {
int l = ;
for (; lim <= n + m; lim <<= , ++l);
for (int i = ; i < lim; i++)
pos[i] = (pos[i >> ] >> ) | ((i & ) << (l - ));
} inline void ntt(ll *c, int opt, ll P) {
for (int i = ; i < lim; i++)
if (i < pos[i]) swap(c[i], c[pos[i]]);
for (int i = ; i < lim; i <<= ) {
ll wn = fpow(, (P - ) / (i << ), P);
if (opt == -) wn = fpow(wn, P - , P);
for (int len = i << , j = ; j < lim; j += len) {
ll w = 1LL;
for (int k = ; k < i; k++, w = w * wn % P) {
ll x = c[j + k], y = w * c[j + k + i] % P;
c[j + k] = (x + y) % P, c[j + k + i] = (x - y + P) % P;
}
}
} if (opt == -) {
ll inv = getInv(lim, P);
for (int i = ; i < lim; i++) c[i] = c[i] * inv % P;
}
} inline void solve(int id) {
for (int i = ; i < lim; i++)
tmp[i] = b[i] % Mod[id], ans[id][i] = a[i] % Mod[id];
ntt(tmp, , Mod[id]), ntt(ans[id], , Mod[id]);
for (int i = ; i < lim; i++) ans[id][i] = ans[id][i] * tmp[i] % Mod[id];
ntt(ans[id], -, Mod[id]);
} inline ll get(int k, ll P) {
ll M = (Mod[] * Mod[]);
ll t1 = fmul(ans[][k] * Mod[] % M, getInv(Mod[], Mod[]), M);
ll t2 = fmul(ans[][k] * Mod[] % M, getInv(Mod[], Mod[]), M);
ll t = (t1 + t2) % M;
ll invM = getInv(M, Mod[]), c = t;
t = (ans[][k] - t % Mod[] + Mod[]) % Mod[];
t = t * invM % Mod[];
return ((M % P) * (t % P) % P + c % P) % P;
} int main() {
ll P;
read(n), read(m), read(P);
for (int i = ; i <= n; i++) {
read(a[i]);
a[i] %= P;
}
for (int i = ; i <= m; i++) {
read(b[i]);
b[i] %= P;
} prework();
for (int i = ; i < ; i++) solve(i); /* for (int i = 0; i < 3; i++, printf("\n"))
for (int j = 0; j < lim; j++)
printf("%lld ", ans[i][j]); */ for (int i = ; i <= n + m; i++)
printf("%lld%c", get(i, P), i == (n + m) ? '\n' : ' '); return ;
}

Luogu 4245 【模板】任意模数NTT的更多相关文章

  1. 洛谷.4245.[模板]任意模数NTT(MTT/三模数NTT)

    题目链接 三模数\(NTT\): 就是多模数\(NTT\)最后\(CRT\)一下...下面两篇讲的都挺明白的. https://blog.csdn.net/kscla/article/details/ ...

  2. [题解] Luogu P4245 [模板]任意模数NTT

    三模NTT 不会... 都0202年了,还有人写三模NTT啊... 讲一个好写点的做法吧: 首先取一个阀值\(w\),然后把多项式的每个系数写成\(aw + c(c < w)\)的形式,换句话说 ...

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

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

  4. 【模板】任意模数NTT

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

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

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

  6. 再探快速傅里叶变换(FFT)学习笔记(其三)(循环卷积的Bluestein算法+分治FFT+FFT的优化+任意模数NTT)

    再探快速傅里叶变换(FFT)学习笔记(其三)(循环卷积的Bluestein算法+分治FFT+FFT的优化+任意模数NTT) 目录 再探快速傅里叶变换(FFT)学习笔记(其三)(循环卷积的Blueste ...

  7. 任意模数NTT

    任意模数\(NTT\) 众所周知,为了满足单位根的性质,\(NTT\)需要质数模数,而且需要能写成\(a2^{k} + r\)且\(2^k \ge n\) 比较常用的有\(998244353,1004 ...

  8. BZOJ1042 HAOI2008硬币购物(任意模数NTT+多项式求逆+生成函数/容斥原理+动态规划)

    第一眼生成函数.四个等比数列形式的多项式相乘,可以化成四个分式.其中分母部分是固定的,可以多项式求逆预处理出来.而分子部分由于项数很少,询问时2^4算一下贡献就好了.这个思路比较直观.只是常数巨大,以 ...

  9. 【知识总结】多项式全家桶(三)(任意模数NTT)

    经过两个月的咕咕,"多项式全家桶" 系列终于迎来了第三期--(雾) 上一篇:[知识总结]多项式全家桶(二)(ln和exp) 先膜拜(伏地膜)大恐龙的博客:任意模数 NTT (在页面 ...

  10. MTT:任意模数NTT

    MTT:任意模数NTT 概述 有时我们用FFT处理的数据很大,而模数可以分解为\(a\cdot 2^k+1\)的形式.次数用FFT精度不够,用NTT又找不到足够大的模数,于是MTT就应运而生了. MT ...

随机推荐

  1. Python学习问题记录

    1.在windows的cmd中使用open方法打开文件时,报如下错误: (unicode error) 'unicodeescape' codec can't decode bytes in posi ...

  2. Erlang generic standard behaviours -- gen_server hibernate

    hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...

  3. TCP/IP网络编程系列之四(初级)

    TCP/IP网络编程系列之四-基于TCP的服务端/客户端 理解TCP和UDP 根据数据传输方式的不同,基于网络协议的套接字一般分为TCP和UDP套接字.因为TCP套接字是面向连接的,因此又称为基于流的 ...

  4. centos6.9 x64安装http,php5.6,curl5.29,mysql最后安装zabbix3.4+zabbix客户端

    https://www.zabbix.com/documentation/3.4/zh/manual/installation/requirementshttps://www.zabbix.com/d ...

  5. JNI的一个简单实例

    本例子使用的操作系统MacOS, 64位JVM. JNI编写的几个步骤如下: 编写Java代码,并注明native方法: public class HelloJni { public native v ...

  6. 安装Elastix-2.4版本

    首先,下载Elastix地址:http://www.elastix.org,下载里面的2.4版本 第一步:选择安装,Enter 选择语言,默认就行 选择us,默认 选择全部 选择默认分区,点击OK 配 ...

  7. Linux的内存管理机制

    原文作者:技术成就梦想 链接:http://ixdba.blog.51cto.com/2895551/541355 一 物理内存和虚拟内存          我们知道,直接从物理内存读写数据要比从硬盘 ...

  8. canvas之旋转一条线段

    <canvas id="canvas" width="600" height="500" style="background ...

  9. MyBatis单个参数的动态语句引用

    参考:http://blog.csdn.net/viviju1989/article/details/17071909 是当我们的参数为String时,在sql语句中#{name} 会去我们传进来的参 ...

  10. System.Security.Cryptography.CryptographicException: 系统找不到指定的文件

    默认为false 改为true