题意:求方程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. H - Hamiltonian Hypercube Gym - 101170H

    规律题 首先我们要知道他的顺序是怎么来的,首先当n等于1时,是0,1 当n=2时,先按照与按顺序在他们前面分别加0,即00,01,在逆序加1,即11,10 构成的顺序为00,01,11,10:往后同理 ...

  2. 磁盘性能测试工具之fio

    fio是测试磁盘性能的一个非常好的工具,用来对硬件进行压力测试和验证. 注意事项 CentOS 6.5等较老版本的操作系统用fdisk创建分区时,默认为非4KB对齐选择初始磁柱编号,对性能有较大的影响 ...

  3. 【Django 2.2文档系列】Model 外键中的on_delete参数用法

    场景 我们用Django的Model时,有时候需要关联外键.关联外键时,参数:on_delete的几个配置选项到底是干嘛的呢,你知道吗? 参数介绍 models.CASCADE 级联删除.Django ...

  4. 关于Swiper和vue数据顺序加载问题处理

    在使用swiper插件的时候,常常因为异步加载数据产生的顺序问题而使插件不能正常实行,所以可以使用vue的updated来解决. 问:什么时候 进updated方法? 答:只有事先设置好的data变量 ...

  5. CentOS 7.4 安装网易云音乐

    1.下包–>网易云音乐 Ubuntu14.04(推荐14.04依赖包网上能找到) 提示:16.04有部分依赖包还找不到,有兴趣可以自行打包RPM安装. 2.解包 (1)使用 ar -vx解压ub ...

  6. Android | 教你如何使用HwCameraKit接入相机人像模式

    目录 介绍 简介 关于本次CodeLab 你将建立什么 你会学到什么 你需要什么 申请Camera相关权限 集成HwCameraKit开放能力 步骤1 模式创建:获取CameraKit实例,创建人像模 ...

  7. Spring Boot中Spring data注解的使用

    文章目录 Spring Data Annotations @Transactional @NoRepositoryBean @Param @Id @Transient @CreatedBy, @Las ...

  8. 网络传输 socket

    一.Socket语法及相关 前言:osi七层模型: 第七层:应用层.     各种应用程序协议,如HTTP,FTP,SMTP,POP3. 第六层:表示层.     信息的语法语义以及它们的关联,如加密 ...

  9. 【kafka KSQL】游戏日志统计分析(1)

    [kafka KSQL]游戏日志统计分析(1) 以游戏结算日志为例,展示利用KSQL对日志进行统计分析的过程. 启动confluent cd ~/Documents/install/confluent ...

  10. JS点击按钮,提示确认后跳转网页,并可传递参数

    综合参考: http://jingyan.baidu.com/article/47a29f242b180ac0142399f9.html http://blog.csdn.net/hshl1214/a ...