[NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程

题意:求方程x2-Dy2=1的最小正整数解

思路:用连分数法解佩尔方程,关键是找出√d的连分数表示的循环节。具体过程参见:http://m.blog.csdn.net/blog/wh2124335/8871535

  • 当d为完全平方数时无解
  • 将√d表示成连分数的形式,例如:[NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程
  • 当d不为完全平方数时,√d为无理数,那么√d总可以表示成:[NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程
  • [NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程
  • 当n为偶数时,x0=p,y0=q;当n为奇数时,x0=2p2+1,y0=2pq

求d在1000以内佩尔方程的最小正整数解的c++打表程序(正常跑比较慢,这个题需要离线打表):

  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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; #ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-12; /* -------------------------------------------------------------------------------- */ struct BigInt {
const static int maxI = 1e8;
const static int Len = ;
typedef vector<int> vi;
typedef long long LL;
vi num;
bool symbol; BigInt() {
num.clear();
symbol = ;
}
BigInt(int x) {
symbol = ;
if (x < ) {
symbol = ;
x = -x;
}
num.push_back(x % maxI);
if (x >= maxI) num.push_back(x / maxI);
}
BigInt(bool s, vi x) {
symbol = s;
num = x;
}
BigInt(char s[]) {
int len = strlen(s), x = , sum = , p = s[] == '-';
symbol = p;
for (int i = len - ; i >= p; i--) {
sum += (s[i] - '0') * x;
x *= ;
if (x == 1e8 || i == p) {
num.push_back(sum);
sum = ;
x = ;
}
}
while (num.back() == && num.size() > ) num.pop_back();
} void push(int x) {
num.push_back(x);
} BigInt abs() const {
return BigInt(false, num);
} bool smaller(const vi &a, const vi &b) const {
if (a.size() != b.size()) return a.size() < b.size();
for (int i = a.size() - ; i >= ; i--) {
if (a[i] != b[i]) return a[i] < b[i];
}
return ;
} bool operator < (const BigInt &p) const {
if (symbol && !p.symbol) return true;
if (!symbol && p.symbol) return false;
if (symbol && p.symbol) return smaller(p.num, num);
return smaller(num, p.num);
} bool operator > (const BigInt &p) const {
return p < *this;
} bool operator == (const BigInt &p) const {
return !(p < *this) && !(*this < p);
} bool operator != (const BigInt &p) const {
return *this < p || p < *this;
} bool operator >= (const BigInt &p) const {
return !(*this < p);
} bool operator <= (const BigInt &p) const {
return !(p < *this);
} vi add(const vi &a, const vi &b) const {
vi c;
c.clear();
int x = ;
for (int i = ; i < a.size(); i++) {
x += a[i];
if (i < b.size()) x += b[i];
c.push_back(x % maxI);
x /= maxI;
}
for (int i = a.size(); i < b.size(); i++) {
x += b[i];
c.push_back(x % maxI);
x /= maxI;
}
if (x) c.push_back(x);
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi sub(const vi &a, const vi &b) const {
vi c;
c.clear();
int x = ;
for (int i = ; i < b.size(); i++) {
x += maxI + a[i] - b[i] - ;
c.push_back(x % maxI);
x /= maxI;
}
for (int i = b.size(); i < a.size(); i++) {
x += maxI + a[i] - ;
c.push_back(x % maxI);
x /= maxI;
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi mul(const vi &a, const vi &b) const {
vi c;
c.resize(a.size() + b.size());
for (int i = ; i < a.size(); i++) {
for (int j = ; j < b.size(); j++) {
LL tmp = (LL)a[i] * b[j] + c[i + j];
c[i + j + ] += tmp / maxI;
c[i + j] = tmp % maxI;
}
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi div(const vi &a, const vi &b) const {
vi c(a.size()), x(, ), y(, ), z(, ), t(, );
y.push_back();
for (int i = a.size() - ; i >= ; i--) {
z[] = a[i];
x = add(mul(x, y), z);
if (smaller(x, b)) continue;
int l = , r = maxI - ;
while (l < r) {
int m = (l + r + ) >> ;
t[] = m;
if (smaller(x, mul(b, t))) r = m - ;
else l = m;
}
c[i] = l;
t[] = l;
x = sub(x, mul(b, t));
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} BigInt operator + (const BigInt &p) const {
if (!symbol && !p.symbol) return BigInt(false, add(num, p.num));
if (!symbol && p.symbol) {
return *this >= p.abs() ?
BigInt(false, sub(num, p.num)) : BigInt(true, sub(p.num, num));
}
if (symbol && !p.symbol) {
return (*this).abs() > p ?
BigInt(true, sub(num, p.num)) : BigInt(false, sub(p.num, num));
}
return BigInt(true, add(num, p.num));
} BigInt operator - (const BigInt &p) const {
return *this + BigInt(!p.symbol, p.num);
} BigInt operator * (const BigInt &p) const {
BigInt res(symbol ^ p.symbol, mul(num, p.num));
if (res.symbol && res.num.size() == && res.num[] == )
res.symbol = false;
return res;
} BigInt operator / (const BigInt &p) const {
if (p == BigInt()) return p;
BigInt res(symbol ^ p.symbol, div(num, p.num));
if (res.symbol && res.num.size() == && res.num[] == )
res.symbol = false;
return res;
} BigInt operator % (const BigInt &p) const {
return *this - *this / p * p;
} void show() const {
if (symbol) putchar('-');
printf("%d", num[num.size() - ]);
for (int i = num.size() - ; i >= ; i--) {
printf("%08d", num[i]);
}
//putchar('\n');
} int TotalDigit() const {
int x = num[num.size() - ] / , t = ;
while (x) {
x /= ;
t++;
}
return t + (num.size() - ) * Len;
} }; template<typename T>
T gcd(T a, T b) {
return b == ? a : gcd(b, a % b);
} template<typename T>
struct Fraction {
T a, b;
Fraction(T a, T b) {
T g = gcd(a, b);
this->a = a / g;
this->b = b / g;
if (this->b < ) {
this->a = this->a * T(- );
this->b = this->b * T(- );
}
}
Fraction(T a) {
this->a = a;
this->b = ;
}
Fraction() {}
Fraction operator + (const Fraction &that) const {
T x = a * that.b + b * that.a, y = b * that.b;
return Fraction(x, y);
}
Fraction operator - (const Fraction &that) const {
T x = a * that.b - b * that.a, y = b * that.b;
return Fraction(x, y);
}
Fraction operator * (const Fraction &that) const {
T x = a * that.a, y = b * that.b;
return Fraction(x, y);
}
Fraction operator / (const Fraction &that) const {
T x = a * that.b, y = b * that.a;
return Fraction(x, y);
}
Fraction operator += (const Fraction &that) {
return *this = *this + that;
}
Fraction operator -= (const Fraction &that) {
return *this = *this - that;
}
Fraction operator *= (const Fraction &that) {
return *this = *this * that;
}
Fraction operator /= (const Fraction &that) {
return *this = *this / that;
}
Fraction operator ! () const {
return Fraction(b, a);
}
bool operator == (const Fraction &that) const {
return a == that.a && b == that.b;
}
bool operator != (const Fraction &that) const {
return a != that.a || b != that.b;
}
}; template<typename T>
T getInt(Fraction<T> a, T d, Fraction<T> b) {
T Min = , Max;
Fraction<T> buf = a * d + b;
Max = buf.a / buf.b;
while (Min < Max) {
T Mid = (Min + Max + ) / ;
buf = (b - Mid) * (b - Mid);
buf = buf / a / a;
if (buf.a <= buf.b * d) Min = Mid;
else Max = Mid - ;
}
return Min;
} void work(int n) {
int k = (int)sqrt(n + 0.5);
if (k * k == n) {
printf("no solution");
return ;
}
Fraction<BigInt> a(), b(), aa, bb;
BigInt d(n);
vector<BigInt> R;
BigInt t = getInt(a, d, b);
aa = a / (a * a * d - (b - t) * (b - t));
bb = (b - t) * BigInt(- ) / (a * a * d - (b - t) * (b - t));
a = aa;
b = bb;
do {
R.pb(t);
t = getInt(a, d, b);
aa = a / (a * a * d - (b - t) * (b - t));
bb = (b - t) * BigInt(- ) / (a * a * d - (b - t) * (b - t));
a = aa;
b = bb;
} while (t != R[] * );
Fraction<BigInt> ans(R[R.size() - ]);
for (int i = ; i < R.size(); i ++) {
ans = !ans + R[R.size() - i - ];
}
BigInt x0 = ans.a, y0 = ans.b;
if (R.size() & ) {
x0 = ans.a * ans.a * + ;
y0 = ans.a * ans.b * ;
}
x0.show();
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n;
puts("char ans[][100] = {\"\", ");
for (int i = ; i <= ; i ++) {
printf("\"");
work(i);
printf("\", ");
if (i % == ) puts("");
}
puts("\n};");
return ;
}
上一篇:Confluence 6 连接到 Jira 用户管理的限制


下一篇:常用笔记:Linux