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, each printed right justified in a field of width 10 (as shown above).
等差数列求和,有:(2m+1)^2 - 8n^2 = 1,令x=2*m+1,y=n,可看出为佩尔方程的基本形式。
佩尔方程:
定义:若一个不定方程具有这样的形式:x^2-ny^2=1则称此二元二次不定方程为佩尔方程
若n是完全平方数,则这个方程式只有平凡解(1,0),(-1,0)。
解佩尔方程:若佩尔方程x^2-ny^2=1的最小特解(最小正整数解)是(x1,y1),那么可有迭代公式
求出所有正整数解(xk,xk),用矩阵表示,那么已知最小特解可以用矩阵快速幂求出任何一项解。
此题已知x=3,y=1,代入迭代公式即可,然后需要注意一下输出向右对齐。
int main()
{
int x=3,y=1;
for(int i=1; i<=10; i++)
{
int dx=x*3+y*8,dy=x+y*3;
printf("%10d%10d\n",dy,(dx-1)/2);
x=dx,y=dy;
}
return 0;
}