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
6 8 35 49
题意是有一条街编号从1~n排列,一个人从门口出发往两边走,经过的门牌号的和相同,即若此人住在k号有题意1+2+3+...+k=k+(k+1)+(k+2)+...+n,让求k,n
用佩尔方程解,看到的不错的解释先贴上:(链接http://blog.youkuaiyun.com/u010885899/article/details/46765007),https://www.cnblogs.com/devymex/archive/2010/09/07/1818983.html
1+2+...+x = x+(x+1)+(x+2)...+y
求x,y。
方程就变为x*(x+1)/2 = (x+y)(y-x+1)/2 => y^2+y-2*x^2=0 => (2*y+1)^2-8*x^2=0
做这道题的收获就是通过这道题了解了佩尔方程,它是一个解x^2-d*y^2=1这类方程的方法。
佩尔方程的意思就是x^2-d*y^2=1的第一个解x0,y0已知的话,其余的值有一个递推公式了:
X(n)=X(n-1)*x0+d*Y(n-1)*y0
Y(n)=X(n-1)*y0+Y(n-1)*x0
以后记住解x^2-d*y^2=1的方程有一个简便算法~
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
int main()
{
int i,x0=3,y0=1,x,y,prv_x=3,prv_y=1;
for(i=1;i<=10;i++)
{
x=prv_x*x0+8*prv_y*y0;
y=prv_x*y0+prv_y*x0;
cout<<setw(10)<<y<<setw(10)<<(x-1)/2<<endl;
prv_x=x;
prv_y=y;
}
return 0;
}