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

佩尔方程x*x-d*y*y=1,当d不为完全平方数时,有无数个解,并且知道一个解可以推其他解。 如果d为完全平方数时,可知佩尔方程无解。

假设(x0,y0)是最小正整数解。

则:

xn=xn-1*x0+d*yn-1*y0

yn=xn-1*y0+yn-1*x0

证明只需代入。 如果忘记公式可以自己用(x0*x0-d*y0*y0)*(x1*x1-d*y1*y1)=1 推。

这样只要暴力求出最小特解,就可以用快速幂求出任意第K个解。

Street Numbers

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2813   Accepted: 1568

Description

A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. One night she adds up the street numbers of the houses she passes (excluding her own). The next time she walks the other way she repeats this and finds, to her astonishment, that the two sums are the same. Although this is determined in part by her house number and in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses should exhibit it. 
Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number):

         6         8
        35        49

Input

There is no input for this program.

Output

Output will consist of 10 lines each containing a pair of numbers, in increasing order with the last number, each printed right justified in a field of width 10 (as shown above).

Sample Input


Sample Output

         6         8
        35        49

这题可以得到佩尔方程s*s-8*t*t=1 ,s=2n+1,t=x (n表示总长,x表示取的n中某个位置)

s0=3,t0=1 然后就很好弄了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    long long s0=3;
    long long t0=1;
    long long s1=3;
    long long t1=1;
    for(int i=1;i<=10;i++)
    {
        long long s,t;
        s=s1*s0+8*t1*t0;
        t=t1*s0+t0*s1;
        s1=s;
        t1=t;
        printf("%10lld%10lld\n",t,(s-1)/2);
        //cout<<t<<" "<<(s-1)/2<<endl;
    }
    return 0;
}

这题用暴力然后打表也是可以0MS过的。

时间: 2024-08-15 22:25:07

Pell方程(求形如x*x-d*y*y=1的通解。)的相关文章

1040: 方程求零点

1040: 方程求零点 时间限制: 1 Sec  内存限制: 128 MB提交: 276  解决: 108[提交][状态][讨论版] 题目描述 设函数f(x)是在区间[a,b]内的连续函数,且f(a)f(b)<0, 根据Role定理,f(x)在区间内必存在零点.已知f(x)=x3-x-1, 输入区间端点的值,令精确度为eps=10-5, 判断是否存在零点,如果存在,输出该零点的近似值,否则输出No zero point. 输入 输入包括若干行,表示该区间端点 输出 每行对应每个区间的计算结果,如

关于方程求根的解决方案

对于方程求根主要的思想主要采取迭代的思想,通过条件判断,循环执行直到满足条件以后直接跳出循环输出 下面以x-cos(x)=0:为例  采用do-while 循环,输出Root: #include "stdio.h"#include "math.h"#include "stdio.h"double fun(){ double x1=0,x0; do { x0=x1; x1=cos(x0); } while(fabs(x0-x1)>=1e-6)

方程求根——牛顿迭代法

这段代码实现了牛顿切线法.简化牛顿法和牛顿下山法这三种方程求解法,由于输出结果较长,只以牛顿下山法为例写一段例题 1.代码 %%牛顿迭代法 %%method为-1时为牛顿切线法,method为0时为简化牛顿法,method为1时为牛顿下山法 %%f是表达式f(x) = 0,X0是初值,epsilon是精度,interval是包含解的区间 function NM = Newton_method(f,X0,epsilon,interval,method) Y0 = subs(f,X0); %%作图

PJOI PKU Campus 2011 B:A Problem about Tree LCA 求任意点x为根的y的父节点

题目链接:点击打开链接 题意:给定n个点 m个询问 下面n-1行给定一棵树 m个询问 x y 问把树转成以x为根 y的父节点是谁 第一种情况lca==y那就是x的第 dep[x] - dep[y] -1 父亲,依次向上爬山坡,利用倍增的二进制加速. 第二种就是Father[y]; #include"cstdio" #include"iostream" #include"queue" #include"algorithm" #i

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

题意:求方程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++打表程序(正常跑比较慢,这个题需要离

方程求根

一. 二分法 题目:用二分法求方程x3-2x-5=0在区间[2,3]内的一个实根,要求误差不超过0.01. 1 #include <iostream> 2 using namespace std; 3 4 double f(double x) 5 { 6 return x*x*x - 2*x - 5; 7 } 8 9 int main() 10 { 11 double left = 2.0, right = 3.0; 12 double mid; 13 while(right - left &

进一步完善之后的一元N次方程求导算法

祝大家节日快乐.......写代码就是过节.... package com.system.Tools; /** * 这个类,实现对函数的求导算法 * 最大目标  实现对任意多元函数的偏导数和全导数的求导算法 * 最小目标  实现对一元N次函数的求导算法 *  * @author Administrator */public class SystemMathTools { /* *  还不是很完善,需要进一步修改...     *      *  by comsci 2019.2.4 经过进一步的

hdu2281 Squre Number——Pell方程

题意 输入一个 $N$,求最大的 $n$($n \leq N$)和 $x$,使得 $x^2 = \frac{1^2+2^2+...+n^2}{n}$. 分析 将右边式子的分子求和化简,有:$x^2 = \frac{(n+1)(2n+1)}{6}$. 变换成:$(4n+3)^2-48x^2 = 1$. 这就是佩尔方程的形式,且样例给出了最小整数解(7, 1). 求出long long范围内的所有解(也就9个) #include<bits/stdc++.h> using namespace std

方程求根——二分法

二分法求根主要应用了区间套定理,这一算法实现简单且结果也迭代的较好,但对于复杂函数其结果不理想 1.代码 %%二分法求根 %%f为函数表达式,interval0为初始区间,epsilon为控制精度 function RD = Roots_dichotomy(f,interval0,epsilon) x_low = interval0(1);x_up = interval0(2);x_ave = (x_low+x_up)/2; %%作图 t = x_low:(x_up-x_low)/1000:x_