[洛谷P4245]【模板】任意模数NTT
题目大意:给你两个多项式f(x)和g(x)以及一个模数p(p⩽109),求f*g\pmod p
题解:任意模数NTT,最大的数为p^2\times\max\{n,m\}\leqslant10^{23},所以一般选3个模数即可,求出这三个模数下的答案,然后中国剩余定理即可。
假设这一位的答案是x,三个模数分别为A,B,C,那么:
x\equiv x_1\pmod{A}\\ x\equiv x_2\pmod{B}\\ x\equiv x_3\pmod{C}
先把前两个合并:
x_1+k_1A=x_2+k_2B\\ x_1+k_1A\equiv x_2\pmod{B}\\ k_1\equiv \frac{x_2-x_1}A\pmod{B}
于是求出了k_1,也就求出了x\equiv x_1+k_1A\pmod{AB},记x_4=x_1+k_1A
x_4+k_4AB=x_3+k_3C\\ x_4+k_4AB\equiv x_3\pmod{C}\\ k_4\equiv \dfrac{x_3-x_4}{AB}\pmod{C}
求出了k_4,x\equiv x_4+k_4AB\pmod{ABC},因为x<ABC,所以x=x_4+k_4AB
卡点:Wn数组开小,中国剩余定理写错
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <algorithm> #include <cstdio> #include <cstring> int mod; namespace Math { inline int pw( int base, int p, const int mod) { static int res; for (res = 1; p; p >>= 1, base = static_cast < long long > (base) * base % mod) if (p & 1) res = static_cast < long long > (res) * base % mod; return res; } inline int inv( int x, const int mod) { return pw(x, mod - 2, mod); } } const int mod1 = 998244353, mod2 = 1004535809, mod3 = 469762049, G = 3; const long long mod_1_2 = static_cast < long long > (mod1) * mod2; const int inv_1 = Math::inv(mod1, mod2), inv_2 = Math::inv(mod_1_2 % mod3, mod3); struct Int { int A, B, C; explicit inline Int() { } explicit inline Int( int __num) : A(__num), B(__num), C(__num) { } explicit inline Int( int __A, int __B, int __C) : A(__A), B(__B), C(__C) { } static inline Int reduce( const Int &x) { return Int(x.A + (x.A >> 31 & mod1), x.B + (x.B >> 31 & mod2), x.C + (x.C >> 31 & mod3)); } inline friend Int operator + ( const Int &lhs, const Int &rhs) { return reduce(Int(lhs.A + rhs.A - mod1, lhs.B + rhs.B - mod2, lhs.C + rhs.C - mod3)); } inline friend Int operator - ( const Int &lhs, const Int &rhs) { return reduce(Int(lhs.A - rhs.A, lhs.B - rhs.B, lhs.C - rhs.C)); } inline friend Int operator * ( const Int &lhs, const Int &rhs) { return Int( static_cast < long long > (lhs.A) * rhs.A % mod1, static_cast < long long > (lhs.B) * rhs.B % mod2, static_cast < long long > (lhs.C) * rhs.C % mod3); } inline int get() { long long x = static_cast < long long > (B - A + mod2) % mod2 * inv_1 % mod2 * mod1 + A; return ( static_cast < long long > (C - x % mod3 + mod3) % mod3 * inv_2 % mod3 * (mod_1_2 % mod) % mod + x) % mod; } } ; #define maxn 131072 namespace Poly { #define N (maxn << 1) int lim, s, rev[N]; Int Wn[N | 1]; inline void init( int n) { s = -1, lim = 1; while (lim < n) lim <<= 1, ++s; for ( register int i = 1; i < lim; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1) << s; const Int t(Math::pw(G, (mod1 - 1) / lim, mod1), Math::pw(G, (mod2 - 1) / lim, mod2), Math::pw(G, (mod3 - 1) / lim, mod3)); *Wn = Int(1); for ( register Int *i = Wn; i != Wn + lim; ++i) *(i + 1) = *i * t; } inline void NTT(Int *A, const int op = 1) { for ( register int i = 1; i < lim; ++i) if (i < rev[i]) std::swap(A[i], A[rev[i]]); for ( register int mid = 1; mid < lim; mid <<= 1) { const int t = lim / mid >> 1; for ( register int i = 0; i < lim; i += mid << 1) { for ( register int j = 0; j < mid; ++j) { const Int W = op ? Wn[t * j] : Wn[lim - t * j]; const Int X = A[i + j], Y = A[i + j + mid] * W; A[i + j] = X + Y, A[i + j + mid] = X - Y; } } } if (!op) { const Int ilim(Math::inv(lim, mod1), Math::inv(lim, mod2), Math::inv(lim, mod3)); for ( register Int *i = A; i != A + lim; ++i) *i = (*i) * ilim; } } #undef N } int n, m; Int A[maxn << 1], B[maxn << 1]; int main() { scanf ( "%d%d%d" , &n, &m, &mod); ++n, ++m; for ( int i = 0, x; i < n; ++i) scanf ( "%d" , &x), A[i] = Int(x % mod); for ( int i = 0, x; i < m; ++i) scanf ( "%d" , &x), B[i] = Int(x % mod); Poly::init(n + m); Poly::NTT(A), Poly::NTT(B); for ( int i = 0; i < Poly::lim; ++i) A[i] = A[i] * B[i]; Poly::NTT(A, 0); for ( int i = 0; i < n + m - 1; ++i) { printf ( "%d" , A[i].get()); putchar (i == n + m - 2 ? '\n' : ' ' ); } return 0; } |
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· 【译】.NET 升级助手现在支持升级到集中式包管理
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· Tinyfox 发生重大改版