题意:求两个不相等的正整数n,m(n<m),使得1+2+……+n = (n+1) + ……+m,输出前十组答案即可,,注意输出格式 每个数占10格
解析前小广告!总是做算法,不如来个陶冶情操的文章一篇: http://www.sanwen.net/subject/3628849/
解析:
使得1+2+……+n = (n+1) + ……+m,那么 n(n+1)/2 = (m-n)(m+n+1)/2,化简 (2m+1)^2 - 8*n^2 = 1,令x = 2*m+1,y=n,那么方程可以转化为x^2 - 8*y^2 = 1,看到这里的话 这个是典型的佩尔方程,比较偏,有迭代公式可以利用 令x1 = 3,y1 = 1;
佩尔方程介绍:http://baike.baidu.com/link?url=QAt4OjP_zUgUOqTXVn78t00tC4DQR9MRHAQEyJF4YgSgndUgGaOXRcxRG9YoL5gv
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#define ll long long
#define eps 1e-8
#define inf 0xfffffff
//const ll INF = 1ll<<61;
using namespace std;
//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;
int main() {
int x0,y0,x1,y1,tmpx,tmpy,d;
x1 = 3;
y1 = 1;
tmpx = 3;
tmpy = 1;
d = 8;
for(int i=1;i<=10;i++) {
x0 = tmpx * x1 + d * tmpy * y1;
y0 = tmpx * y1 + tmpy * x1;
printf("%10d%10d\n",y0,(x0 - 1)/2);
tmpx = x0;
tmpy = y0;
}
return EXIT_SUCCESS;
}