poj1320(解pell方程)

Street Numbers
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2457 Accepted: 1357

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

题目大意:求1+2+...+(a-1) = (a+1)+(a+2)+...+b的从6 8开始的10个解。

解题思路:pell方程。 1+2+...+(a-1) = (a+1)+(a+2)+...+b                                      ==> 8*a^2     = 4*b^2 +4*b +1-1                                     ==> 2*(2*a)^2 = (2*b+1)^2 -1                                              ==> (2*b+1)^2 - 8*(a)^2 = 1 令 x = 2*b+1, y= a    则 x^2 - 8*y^2 = 1 pell方程:x^2-d*y^2=1 (其中,d>1且为不完全平方数) 迭代法求解: x[n]=x[n-1]*x[1]+d*y[n-1]*y[1]; y[n]=x[n-1]*y[1]+y[n-1]*x[1]; 其中(x[1],y[1])可利用暴力发求的,本题易得 x[1]=3,y[1]=1;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int x,y,x1,y1,px,py,d;
    x1=3;
    y1=1;
    px=3;
    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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值