POJ1320 Street Numbers【佩尔方程】

题目链接:

http://poj.org/problem?id=1320

题目大意:

求解两个不相等的正整数N、M(N<M),使得 1 + 2 + … + N = (N+1) + … + M。输出前10组满足要求

的(N,M)。

思路:

要使 1 + 2 + … + N = (N+1) + … + M,那么 N*(N+1)/2 = (M-N)(M+N+1)/2,即

(2*M+1)^2 - 8*N^2 - 1,令x = 2*M + 1,y = N,就有x^2 - 8*y^2 = 1,就变成了典型的佩尔方程,

已知x1 = 3,y1 = 1,由迭代公式得:

xn = x(n-1)*x1 + d*y(n-1)*y1

yn = x(n-1)*y1 + y(n-1)*x1

那么

x(n+1) = 3*xn + 8*yn

y(n+1) = xn + 3*yn

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int x,y,x1,y1,px,py,d;
    x1 = px = 3;
    y1 = py = 1;
    d = 8;

    for(int i = 1; i <= 10; ++i)
    {
        x = px*x1 + d*py*y1;
        y = px*y1 + py*x1;
        printf("%10d%10d\n",y,(x-1)/2);
        px = x;
        py = y;
    }

    return 0;
}
时间: 2024-08-04 17:24:57

POJ1320 Street Numbers【佩尔方程】的相关文章

POJ 1320 Street Numbers 【佩尔方程】

任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3181   Accepted: 1776 Description A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the

Uva 138-Street Numbers(佩尔方程)

题目链接:点击打开链接 题意: 一条街上有n个房子编号从1到n 设某人的房子编号为k 求满足 1+2+3+..(k-1)=(k+1)+...+n 的10组n,k值 两边求和化简得 n^2+n-2k^2=0;  两边同乘4  -> 4n^2+4n+1-8k^2=1;  -> (2n+1)^2-2(2k)^2=1; 令 x=2n+1 y=2k 得 x^2-2y^2=1; 形如 x^2-ny^2=1 (n为任意正整数) 的方程称为佩尔方程,很容易求出其最小解 (x0,y0) 然后 xi=x0*x(i

Street Numbers

 Street Numbers 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 str

(Incomplete) UVa 138 Street Numbers

方法:暴力 设home的序号为n,街尾序号为N,列出方程 (n-1)*n/2 = (N+n+1)*(N-n)/2, 化简得 2*n*n = (N+1)*N.枚举N再检查是否有解.可以直接求,或者打表. code: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector>

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

C语言之基本算法26—佩尔方程求解

//穷举法! /* ====================================================== 题目:求佩尔方程x*x-73*y*y=1的解. ====================================================== */ #include<stdio.h> #include<math.h> int main(void) { int x,y; double t; for(y=1;y<=10000000;y+

Pell佩尔方程 x^2-61*y^2=1

有一个特殊的佩尔方程:x^2-61*y^2=1 下面我将编程求解它的最小解 如果直接枚举x和y的话,大约需要10秒 代码: #include<iostream> using namespace std; int main() { long long x = 2, y = 0, flag = 3; //flag=x*x-61*y*y-1 while (flag) { if (flag > 0) { flag -= 61 * (2 * y + 1); y++; } else { flag +

佩尔方程

什么是佩尔方程 定义:若一个不定方程具有这样的形式:则称此二元二次不定方程为佩尔方程 若n是完全平方数,则这个方程式只有平凡解. 佩尔方程的解    设为的两个解,则有 两式相乘得化简整理得,式子中加一个减一个 可得 所以有 写成矩阵形式,可得 因此只要知道最小的一个特解,就可以算出其他的解 佩尔方程的特解 暴力求解法 令,求出,然后检查通过这个,是否使得佩尔方程成立 void serach(ll n,ll &x,ll &y) { y=1; while(1) { x=(1ll)*sqrt(

HDU 3292 【佩尔方程求解 &amp;&amp; 矩阵快速幂】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 587    Accepted Submission(s): 400 Problem Description Now Sailormoon