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

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

  • 当d为完全平方数时无解
  • 将√d表示成连分数的形式,例如:
  • 当d不为完全平方数时,√d为无理数,那么√d总可以表示成:
  • 当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 ;
}

[NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程的更多相关文章

  1. NBUT 1224 Happiness Hotel 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB The life of Little A is good, and, he managed to get enoug ...

  2. exgcd求解同余方程的最小正整数解 poj1061 poj2115

    这两题都是求解同余方程,并要求出最小正整数解的 对于给定的Ax=B(mod C) 要求x的最小正整数解 首先这个式子可转化为 Ax+Cy=B,那么先用exgcd求出Ax+Cy=gcd(A,C)的解x ...

  3. ex_gcd求不定方程的最小正整数解

    #include<bits/stdc++.h> using namespace std; int gcd(int a,int b) {return b?gcd(b,a%b):a;} int ...

  4. POJ - 1061 青蛙的约会 扩展欧几里得 + (贝祖公式)最小正整数解

    题意: 青蛙 A 和 青蛙 B ,在同一纬度按照相同方向跳跃相同步数,A的起点为X ,每一步距离为m,B的起点为Y,每一步距离为 n,一圈的长度为L,求最小跳跃步数. 思路: 一开始按照追击问题来写, ...

  5. LeetCode1237找出给定方程的正整数解

    题目 给定方程f和值z,找出给定方程f(x,y)=z的正整数解x,y.f(x,y)关于x.y都是严格单调的. 题目保证 f(x, y) == z 的解处于 1 <= x, y <= 100 ...

  6. leetcode-160场周赛-5238-找出给定方程的正整数解

    题目描述: class Solution: def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[Li ...

  7. POJ - 1061 扩展欧几里德算法+求最小正整数解

    //#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) #inclu ...

  8. Pell方程(求形如x*x-d*y*y=1的通解。)

    佩尔方程x*x-d*y*y=1,当d不为完全平方数时,有无数个解,并且知道一个解可以推其他解. 如果d为完全平方数时,可知佩尔方程无解. 假设(x0,y0)是最小正整数解. 则: xn=xn-1*x0 ...

  9. Pell方程及其一般形式

    一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...

随机推荐

  1. asp.net core webapi Session 内存缓存

    Startup.cs文件中的ConfigureServices方法配置: #region Session内存缓存 services.Configure<CookiePolicyOptions&g ...

  2. 基于Neo4j的个性化Pagerank算法文章推荐系统实践

    新版的Neo4j图形算法库(algo)中增加了个性化Pagerank的支持,我一直想找个有意思的应用来验证一下此算法效果.最近我看Peter Lofgren的一篇论文<高效个性化Pagerank ...

  3. 掌握游戏开发中类Message、Handle

    1.   实验目的 1. 自主地设计图形界面 2. 掌握消息类Message的应用 3. 掌握消息处理类Handle的应用 4. 掌握子线程中中更新UI界面的方法 2.  实验内容 1. 在主界面设置 ...

  4. Xshell下载和连接Linux

    Xshell下载和连接Linux 第一步.Xshell的下载 方法1:从官网下载个人使用时免费的,商业使用是要收费的. http://www.xshellcn.com/ 方法二2:百度云下载Xshel ...

  5. 详解PHP中instanceof关键字及instanceof关键字有什么作用

    来源:https://www.jb51.net/article/74409.htm PHP5的另一个新成员是instdnceof关键字.使用这个关键字可以确定一个对象是类的实例.类的子类,还是实现了某 ...

  6. Vue-cli4脚手架搭建

    一:要安装Node.js:安装路径要默认安装(node-v12.16.2-x64.msi-长支持 二:要安装cnpm 1)说明:npm(node package manager)是nodejs的包管理 ...

  7. 利用python画出SJF调度图

    最先发布在csdn.本人原创. https://blog.csdn.net/weixin_43906799/article/details/105510046 SJF算法: 最短作业优先(SJF)调度 ...

  8. eclipse 创建maven项目失败

    问题描述: eclipse 初次创建maven项目报错 可能是maven-archetype-quickstart:1.1.jar 包失效了或者没有? 有人说把这个jar包放在maven本地仓库里 我 ...

  9. Material Design 设计规范总结(2)

    本文是Material Design设计规范总结的第二部分,是进行UI设计与前端开发的必备参考资料. 八.布局 (1)所有可操作元素最小点击区域尺寸:48dp X 48dp. (2)栅格系统的最小单位 ...

  10. 你所不知道的Python | 字符串连接的秘密

    字符串连接,就是将2个或以上的字符串合并成一个,看上去连接字符串是一个非常基础的小问题,但是在Python中,我们可以用多种方式实现字符串的连接,稍有不慎就有可能因为选择不当而给程序带来性能损失. 方 ...