标签:
题目链接:
http://poj.org/problem?id=1320
题目大意:
求解两个不相等的正整数N、M(N<M),使得 1 + 2 + … + N = (N+1) + … + M。输出前10组满足要求
的(N,M)。
思路:
要使 1 + 2 + … + N = (N+1) + … + M,那么 N*(N+1)/2 = (M-N)(M+N+1)/2,即
(2*M+1)^2 - 8*N^2 - 1,令x = 2*M + 1,y = N,就有x^2 - 8*y^2 = 1,就变成了典型的佩尔方程,
已知x1 = 3,y1 = 1,由迭代公式得:
xn = x(n-1)*x1 + d*y(n-1)*y1
yn = x(n-1)*y1 + y(n-1)*x1
那么
x(n+1) = 3*xn + 8*yn
y(n+1) = xn + 3*yn
AC代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; int main() { int x,y,x1,y1,px,py,d; x1 = px = 3; y1 = 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; }
标签:
原文地址:http://blog.csdn.net/lianai911/article/details/44465685