您的位置:首页 > 其它

POJ1320 Street Numbers(佩尔方程)

2015-08-08 20:19 288 查看
Street Numbers

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 2784Accepted: 1547
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


题目大意:

求解两个不相等的正整数n,m(n<m),使得1 + 2 + .....+ n = (n+1) + .... + m.

解题思路:

根据等比数列求和公式可得 n(n+1) / 2 = (m- n) (m+n+1) / 2,即(2m + 1)^2 - 8n ^ 2 = 1,令 x = 2m + 1,y = n,有x ^ 2 - 8y^2 = 1,这是典型的佩尔方程,可知x1 = 3,y1 = 1,由迭代公式可得

xn = xn-1x1 + dyn-1y1

yn = xn-1y1 + yn-1x1

那么

xn+1 = 3xn + 8yn

yn+1 = xn + 3yn

递推有了,迭代就可以了。

AC代码:

#include<iostream>

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: