Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2753 | Accepted: 1530 |
Description
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
Output
Sample Input
Sample Output
6 8 35 49
題意是一個計算機程序員住在一個門牌號(從1開始計)連續的街道上,每天晚上她都在這個街道上從頭走到尾,有一天晚上她把她走過的門牌號相加起來,下一次她走另一條路還是相加門牌號,令她吃驚的是(我都不知道這有什麼好吃驚的),兩個和是相等的。讓打出表house number,last number
題意的意思翻譯過來就是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<iomanip>
#pragma warning(disable:4996)
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long i,x0=3,y0=1,last_x=3,last_y=1,x,y;
for(i=1;i<=10;i++)
{
x=last_x*x0+8*last_y*y0;
y=last_x*y0+last_y*x0;
cout<<setw(10)<<y<<setw(10)<<(x-1)/2<<endl;
last_x=x;
last_y=y;
}
system("pause");
return 0;
}