题解:P14551 【模板】Pell 方程

· · 题解

佩尔方程

:::info[前言] 题目传送门。

\color{white}\small\text{蒟蒻的第一篇黑题题解,点个赞吧。}

::: :::info[契约]{open}

I 型方程为 $x^2-ny^2=1$ 的 Pell 方程。 II 型方程为 $x^2-ny^2=-1$ 的 Pell 方程。 $\{a_L\}$ 表示连分数序列。 $\{p_{2L-1}\},\{q_{2L-1}\}$ 表示渐近分数的分子和分母序列,与大写的 $P,Q$ 表示的不为同一序列。 ::: ## 解法 **本部分主要说明做法,解释见后文。** 显然地,$x$ 最小的解即为佩尔方程的基本解。 --- 首先考虑连分数展开: $$\sqrt n=a_0+\dfrac1{a_1+\dfrac1{a_2+\dfrac1{a_3+\cdots}}}=\big[a_0;\overline{a_1,a_2,\ldots\ \!,a_L}\big]$$ --- 然后生成渐近分数序列 $\{p_{2L-1}\}$ 和 $\{q_{2L-1}\}$: $$ \left\{\begin{array}{l} p_{-2}=0\ ,\ p_{-1}=1\\ q_{-2}=1\ ,\ q_{-1}=0 \end{array}\right. $$ $$ \left\{\begin{array}{l} p_k=a_kp_{k-1}+p_{k-2}\quad\ \!k\ge0\\ q_k=a_kq_{k-1}+q_{k-2}\quad\ k\ge0 \end{array}\right. $$ --- 最后求解基本解: 1. $\ 2\nmid L$ 时: :::info[I 型方程的基本解] $$ \left\{\begin{array}{l} x_0=p_{2L-1}\\ y_0=q_{2L-1} \end{array}\right. $$ ::: :::info[II 型方程的基本解] $$ \left\{\begin{array}{l} x_0=p_{L-1}\\ y_0=q_{L-1} \end{array}\right. $$ ::: 2. $\ 2\ |\ L$ 时: :::info[I 型方程的基本解] $$ \left\{\begin{array}{l} x_0=p_{L-1}\\ y_0=q_{L-1} \end{array}\right. $$ ::: :::info[II 型方程的基本解] 此时 II 型佩尔方程无解。 ::: ## 原理与具体实现 本部分用于解释之前的内容或证明解法的正确性,同时也包含我做此题时感到困惑的内容。 ### 连分数展开及其合法性 对于非完全平方数的正整数 $n$ ,$\sqrt n$ 的连分数展开都是周期性的,这一点由拉格朗日的 Lagrange 定理(其中之一)证明。 求二次无理数的连分数可以使用其递推公式: $$a_0=\lfloor\sqrt n\rfloor,P_0=0,Q_0=1$$ $$ \left\{\begin{array}{l} P_i=a_{i-1}Q_{i-1}-P_{i-1}\\ Q_i=\dfrac{n-P_i^2}{Q_i}\\ a_i=\dfrac{\sqrt n+p_i}{Q_i} \end{array}\right. $$ 当 $i>0$ 且 $Q_i=1$ 时,循环结束,此时就可以得出 $L$ 和连分数了。 ### 为何渐近分数可以求解及解的固定位置证明 对于渐近分数 $\frac{p_i}{q_i}$ 满足以下性质: $$p_i^2-nq_i^2=(-1)^{i+1}Q_{i+1}\text{,其中}\ Q_{i+1} \text{为上方递推公式中的}\ Q_{i+1}\text{。}$$ 使用数学归纳法易证明。 显然地,$\sqrt n$ 的连分数展开是周期性的,所以 $Q_L=Q_0=Q_{2L}=1$,对应的 $p_{L-1},p_{2L-1},q_{L-1},q_{2L-1}$,就满足了 Pell 方程的解。 而 $2\ |\ L$ 时,$(-1)^L$ 与 $(-1)^{2L}$ 均为 $1$,故此时 II 行方程无解。 ## 代码 :::warning{open} 本题需使用高精度或 $128$ 位的整型变量。 同时需要手写其输出方式。 ::: :::info 随便卡卡常就冲进最优解了。 ~~其实是因为人少。~~ 一次的时间复杂度上限是 $O(\sqrt n\log n)$,但是跑不满。 似乎 $n$ 越接近一个完全平方数越慢。 最坏总复杂度为 $O(T\sqrt n\log n)$,可以通过本题。 ::: :::success[code] ```cpp line-numbers #include<bits/stdc++.h> using namespace std; int t,n; inline int rin(){ char c; int f=1,res=0; c=getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1; c=getchar(); } while(c>='0'&&c<='9'){ res=(res<<1)+(res<<3)+(c^48); c=getchar(); } return res*f; } inline void print(__int128 x){ if(x<0)cout<<"-",x=-x; string s=""; do{ s=char(x%10+'0')+s; x/=10; }while(x); cout<<s; } void solve(){ n=rin(); __int128 ai=sqrt(n); if(ai*ai==n){ cout<<"-1 -1\n-1 -1\n"; return; } vector<__int128>a; a.push_back(ai); __int128 cnt=1,pi=0,qi=1; for(;;cnt++){ pi=ai*qi-pi; qi=(n-pi*pi)/qi; ai=(sqrt(n)+pi)/qi; a.push_back(ai); if(qi==1)break; } __int128 Ix0=-1,Iy0=-1,IIx0=-1,IIy0=-1; __int128 p[cnt*2+9],q[cnt*2+9]; for(__int128 i=0;i<cnt*2;i++){ if(i==0){ p[0]=a[0]; q[0]=1; } else if(i==1){ p[1]=a[1]*p[0]+1; q[1]=a[1]*q[0]; } else{ p[i]=a[(i>cnt?i-cnt:i)]*p[i-1]+p[i-2]; q[i]=a[(i>cnt?i-cnt:i)]*q[i-1]+q[i-2]; } if(cnt&1){ if(i==cnt-1){ IIx0=p[i]; IIy0=q[i]; } if(i==cnt*2-1){ Ix0=p[i]; Iy0=q[i]; } } else if(i==cnt-1){ Ix0=p[i]; Iy0=q[i]; } } print(Ix0); cout<<" "; print(Iy0); cout<<"\n"; if(cnt%2==0){ cout<<"-1 -1\n"; return; } print(IIx0); cout<<" "; print(IIy0); cout<<"\n"; } int main(){ t=rin(); while(t--)solve(); return 0; } ``` :::