题目大意:给你$n$项多项式$A(x)$,求出$B(x)$满足$B^2(x)\equiv A(x)\pmod{x^n}$

题解:考虑已经求出$B_0(x)$满足$B_0^2(x)\equiv A(x)\pmod{x^{\lceil\frac n 2\rceil}}$
$$
B(x)-B_0(x)\equiv0\pmod{x^{\lceil\frac n 2\rceil}}\\
B^2(x)−2B(x)B_0(x)+B_0^2(x)≡0\pmod{x^n}\\
A(x)-2B(x)B_0(x)+B_0^2(x)≡0\pmod{x^n}\\
B(x)\equiv\dfrac{A(x)+B_0^2(x)}{2B_0(x)}\pmod{x^n}\\
$$


update:(2019-2-10)

$$
B(x)\equiv\dfrac{A(x)+B_0^2(x)}{2B_0(x)}\pmod{x^n}\\
B(x)\equiv\dfrac{A(x)}{2B_0(x)}+\dfrac{B_0(x)}2\pmod{x^n}\\
$$
发现$\dfrac{B_0(x)}2$只会影响$B(x)$数组的前半部分(即$\pmod{x^{\lceil\frac n2\rceil}}$的部分),但是$B(x)\equiv B_0(x)\pmod{x^{\lceil\frac n2\rceil}}$,所以可以不做考虑,直接把$B_0(x)$拉过来

卡点:求$INV$时注意清空数组,防止因为$B$数组不干净导致出锅

C++ Code:

  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cstdio>
  4. #define maxn 262144
  5. const int mod = 998244353, __2 = mod + 1 >> 1;
  6.  
  7. namespace std {
  8. struct istream {
  9. #define M (1 << 21 | 3)
  10. char buf[M], *ch = buf - 1;
  11. inline istream() { fread(buf, 1, M, stdin); }
  12. inline istream& operator >> (int &x) {
  13. while (isspace(*++ch));
  14. for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
  15. return *this;
  16. }
  17. #undef M
  18. } cin;
  19. struct ostream {
  20. #define M (1 << 21 | 3)
  21. char buf[M], *ch = buf - 1;
  22. inline ostream& operator << (int x) {
  23. if (!x) {*++ch = '0'; return *this;}
  24. static int S[20], *top; top = S;
  25. while (x) {*++top = x % 10 ^ 48; x /= 10;}
  26. for (; top != S; --top) *++ch = *top;
  27. return *this;
  28. }
  29. inline ostream& operator << (const char x) {*++ch = x; return *this;}
  30. inline ~ostream() { fwrite(buf, 1, ch - buf + 1, stdout); }
  31. #undef M
  32. } cout;
  33. }
  34.  
  35. namespace Math {
  36. inline int pw(int base, int p) {
  37. static int res;
  38. 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;
  39. return res;
  40. }
  41. inline int inv(int x) { return pw(x, mod - 2); }
  42. }
  43.  
  44. inline void reduce(int &x) { x += x >> 31 & mod; }
  45. inline void clear(register int *l, const int *r) {
  46. if (l >= r) return ;
  47. while (l != r) *l++ = 0;
  48. }
  49.  
  50. namespace Poly {
  51. #define N maxn
  52. int lim, s, rev[N];
  53. int Wn[N + 1];
  54.  
  55. inline void init(const int n) {
  56. lim = 1, s = -1; while (lim < n) lim <<= 1, ++s;
  57. for (register int i = 1; i < lim; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1) << s;
  58. const int t = Math::pw(3, (mod - 1) / lim);
  59. *Wn = 1; for (register int *i = Wn; i != Wn + lim; ++i) *(i + 1) = static_cast<long long> (*i) * t % mod;
  60. }
  61. inline void FFT(int *A, const int op = 1) {
  62. for (register int i = 1; i < lim; ++i) if (i < rev[i]) std::swap(A[i], A[rev[i]]);
  63. for (register int mid = 1; mid < lim; mid <<= 1) {
  64. const int t = lim / mid >> 1;
  65. for (register int i = 0; i < lim; i += mid << 1)
  66. for (register int j = 0; j < mid; ++j) {
  67. const int X = A[i + j], Y = static_cast<long long> (A[i + j + mid]) * Wn[t * j] % mod;
  68. reduce(A[i + j] += Y - mod), reduce(A[i + j + mid] = X - Y);
  69. }
  70. }
  71. if (!op) {
  72. const int ilim = Math::inv(lim);
  73. for (register int *i = A; i != A + lim; ++i) *i = static_cast<long long> (*i) * ilim % mod;
  74. std::reverse(A + 1, A + lim);
  75. }
  76. }
  77.  
  78. void INV(int *A, int *B, int n) {
  79. if (n == 1) { *B = Math::inv(*A); return ; }
  80. const int len = n + 1 >> 1;
  81. INV(A, B, len); init(len * 3);
  82. static int C[N], D[N];
  83. std::copy(A, A + n, C); clear(C + n, C + lim);
  84. std::copy(B, B + len, D); clear(D + len, D + lim);
  85. FFT(D), FFT(C);
  86. for (register int i = 0; i < lim; ++i) D[i] = (2 - static_cast<long long> (D[i]) * C[i] % mod + mod) * D[i] % mod;
  87. FFT(D, 0); std::copy(D + len, D + n, B + len);
  88. }
  89. void SQRT(int *A, int *B, int n) {
  90. if (n == 1) { *B = 1; return ; }
  91. static int C[N], D[N];
  92. const int len = n + 1 >> 1;
  93. SQRT(A, B, len);
  94. INV(B, D, n), clear(D + n, D + lim);
  95. std::copy(A, A + n, C); clear(C + n, C + lim);
  96. FFT(C), FFT(D);
  97. for (register int i = 0; i < lim; ++i) D[i] = static_cast<long long> (C[i]) * D[i] % mod * __2 % mod;
  98. FFT(D, 0); std::copy(D + len, D + n, B + len);
  99. }
  100. #undef N
  101. }
  102.  
  103. int n, A[maxn], B[maxn];
  104. int main() {
  105. std::cin >> n;
  106. for (int i = 0; i < n; ++i) std::cin >> A[i];
  107. Poly::SQRT(A, B, n);
  108. for (int i = 0; i < n; ++i) std::cout << B[i] << ' ';
  109. std::cout << '\n';
  110. return 0;
  111. }

  

[洛谷P5205]【模板】多项式开根的更多相关文章

  1. 洛谷.3803.[模板]多项式乘法(FFT)

    题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. 5.4 又看了一遍,这个也不错. 2019.3.7 叕看了一遍,推荐这个. #inclu ...

  2. 洛谷.3803.[模板]多项式乘法(NTT)

    题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...

  3. 洛谷.4512.[模板]多项式除法(NTT)

    题目链接 多项式除法 & 取模 很神奇,记录一下. 只是主要部分,更详细的和其它内容看这吧. 给定一个\(n\)次多项式\(A(x)\)和\(m\)次多项式\(D(x)\),求\(deg(Q) ...

  4. 洛谷.4238.[模板]多项式求逆(NTT)

    题目链接 设多项式\(f(x)\)在模\(x^n\)下的逆元为\(g(x)\) \[f(x)g(x)\equiv 1\ (mod\ x^n)\] \[f(x)g(x)-1\equiv 0\ (mod\ ...

  5. 洛谷 P4512 [模板] 多项式除法

    题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...

  6. 洛谷 P4238 [模板] 多项式求逆

    题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...

  7. 洛谷P2293 高精开根

    锣鼓2293 写完了放代码 应该没什么思维难度 ———————————————————————————————————————————————————————— python真香 m=input() ...

  8. P5277 【模板】多项式开根(加强版)(bsgs or Cipolla)

    题面 传送门 题解 首先你得会多项式开根->这里 其次你得会解形如 \[x^2\equiv a \pmod{p}\] 的方程 这里有两种方法,一个是\(bsgs\)(这里),还有一种是\(Cip ...

  9. FFT模板 生成函数 原根 多项式求逆 多项式开根

    FFT #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> ...

  10. 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)

    To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...

随机推荐

  1. 02-JVM内存模型:虚拟机栈与本地方法栈

    一.虚拟机栈(VM Stack) 1.1)什么是虚拟机栈 虚拟机栈是用于描述java方法执行的内存模型. 每个java方法在执行时,会创建一个“栈帧(stack frame)”,栈帧的结构分为“局部变 ...

  2. selenium自动化一点记录

    UI自动化 1.webdriver的findElement方法可以查找页面某元素,通常使用方式是通过id和name进行查找 1.By ID根据id进行定位 WebElement element=dri ...

  3. 安装SQLSEVER与MySQL

    昨天装了一整填的SQLSEVER,今天是把昨天遗留的问题给重新整合一下,上午安装MySQL的时候,是在网上找的帖子通过压缩包安装的,捣鼓了一上午,下午花不到一个小时, 去安装好了,我觉得通过压缩包安装 ...

  4. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  5. 《Effective C++》读书笔记 资源管理

    C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...

  6. Java基础知识:Java实现Map集合二级联动1

    Java实现Map集合二级联动 Map集合可以保存键值映射关系,这非常适合本实例所需要的数据结构,所有省份信息可以保存为Map集合的键,而每个键可以保存对应的城市信息,本实例就是利用Map集合实现了省 ...

  7. 使用Docker部署java web项目

    在国内可能会有源下载失败问题,docker安装失败, 这里提供docker离线安装包如有需要可以进行下载 docker离线安装包下载 ##本文环境使用centos 7 进行部署. #1安装docker ...

  8. 三:QJM HDFS高可用

    本文介绍的是HDFS的一种HA方案.虽然有checkpoint node \backup node等,但是不能实现自动的failover. http://hadoop.apache.org/docs/ ...

  9. 软件工程课堂作业(三)——Right-BICEP软件单元测试

    一.测试方法:Right-BICEP Right-结果是否正确?B-是否所有的边界条件都是正确的?I-能查一下反向关联吗?C-能用其他手段交叉检查一下结果吗?E-你是否可以强制错误条件发生?P-是否满 ...

  10. vim编辑器配置及常用命令

    最近工作不安分, 没有了刚入行时候的锐气, 不知道什么时候开始懈怠起来, 周末在电脑旁边看新闻, 搞笑图片, 追美剧, 一坐就是一天, 很是空虚. 我需要摆脱这种状态, 正好想学习一下安卓底层, An ...