Session on the Pell Equation:
Wikipedia _ Pel Equation
If a lost graph equation has the following forms:
And is a positive integer, this equation is calledPell Equation(English: Pell's equation German: pellsche gleichung)
If it is a complete number of vertices, the equation has only a solution (in fact, it is a solution for any ). For the rest of the cases, Laplace proves the total solution of the P equation. These solutions can be obtained by the continuous score.
Set the continuous score: indicates the progressive score column, which is made by the theoretical knowledge of the continuous score (PI,QI)
Is the solution of the Pell Equation. Take the smallest of them (PI,QI) called the Pell EquationBasic Solution, OrSolution, (X1,Y1)
, Then all solutions (XI,YI) can be expressed as follows:
You can also obtain the following recursive formula:
------------------ Split line --------------------
After obtaining the minimum positive integer solution of the Pell Equation, the formula and the K solution (X1 and Y1 are the minimum positive integer solutions) can be obtained ).
Here you may want to use recursive methods to solve XK and yk. But in fact, if K is very large, it will take a lot of time. Therefore, the Rapid power of matrices can save a lot of time.
Construct a matrix, as shown in figure
For more information about swun OJ, see
Http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1329
# Include <iostream> # include <cmath> # include <stdio. h> using namespace STD; typedef _ int64 ll; # define mod extends %7ll X, Y, N, K; struct pellans {ll p, q ;}; struct node {ll G, h ;}; struct matrix {ll a [2] [2]; void Init () {A [0] [0] = x % MOD; A [0] [1] = Y % MOD; A [1] [0] = (N % mod * Y % mod) % MOD; A [1] [1] = x % mod ;}; // matrix multiplication matrix matrix_mul (matrix A, matrix B) {ll I, j, k; matrix ans; for (I = 0; I <2; I ++) {for (j = 0; j <2; j ++) {ans. A [I] [J] = 0; For (k = 0; k <2; k ++) ans. A [I] [J] = (ans. A [I] [J] % mod + (. A [I] [k] % mod * B. A [k] [J] % mod) % mod ;}return ans ;}// matrix matrix mult (matrix A, LL B) {matrix ans; ans. A [0] [0] = 1; ans. A [0] [1] = 0; ans. A [1] [0] = 0; ans. A [1] [1] = 1; while (B) {If (B & 1) ans = matrix_mul (ANS, a); B> = 1; // cout <B <Endl; A = matrix_mul (a, a);} return ans;} // calculates the minimum positive integer solution of the Pell Equation... template pellans solve (LL N1) {pellans s [4]; node W [4]; int A [4]; s [0]. P = 0; s [0]. Q = 1; s [1]. P = 1; s [1]. Q = 0; A [0] = (LL) floor (SQRT (double) n); A [2] = A [0]; W [1]. G = 0; W [1]. H = 1; while (1) {W [2]. G =-W [1]. G + A [2] * W [1]. h; W [2]. H = (n1-w [2]. g * W [2]. g)/W [1]. h; A [3] = (LL) floor (double) (W [2]. G + A [0])/W [2]. h); s [2]. P = A [2] * s [1]. P + s [0]. p; s [2]. Q = A [2] * s [1]. q + s [0]. q; If (S [2]. p * s [2]. p-n1 * s [2]. Q * s [2]. q) = 1 & S [2]. p> 0 & S [2]. q> 0) return s [2]; W [0] = W [1]; W [1] = W [2]; A [2] = A [3]; s [0] = S [1]; s [1] = s [2] ;}} int main () {pellans; // freopen (". in "," r ", stdin); // freopen (" 1.out", "W", stdout); While (~ Scanf ("% i64d % i64d", & N, & K) {If (SQRT (double (N) * SQRT (double (N) = N) {printf ("no solution \ n"); continue;} ans = solve (n); // obtain the minimum positive integer solution of the Pell Equation x = ans. P % mod, y = ans. Q % MOD; matrix TMP, ans1; TMP. init (); // initialize ans1 = mult (TMP, (k-1) % mod); LL X1 = x % MOD; X = (ans1.a [0] [0] % mod * x % mod) % mod + (ans1.a [1] [0] % mod * Y % mod) % MOD; y = (ans1.a [0] [1] % mod * X1 % mod) + (ans1.a [1] [1] % mod * Y % mod) % MOD; printf ("% i64d, % i64d % i64d, % i64d \ n", ans. p, ans. q, X % mod, Y % mod);} return 0 ;}