题目链接:https://vjudge.net/problem/HDU-2281
解题思路:
原式可以化为x^2 = (n+1)(2*n+1)/6,(4n+3)^2 - 48*x^2 = 1
然后就是佩尔方程求解了。
x^2 - n*y^2 = 1
x[i+1] = x[1]*x[i] + n* y[1]*y[i]
y[i+1] = x[1]*y[i] + y[1]*x[i]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 hll;
const int mx = 1e2 + 10;
vector <ll> g;
ll ans[mx];
int main()
{
hll x = 7,y = 1;
while((x-3)/4<=1e18){
if((x-3)%4==0) ans[g.size()] = y,g.push_back((x-3)/4);
hll x1 = x,y1 = y;
x = 7*x1 + 48*y1;
y = 7*y1 + x1;
}
while(scanf("%lld",&x)&&x)
{
int k = upper_bound(g.begin(),g.end(),x) - g.begin() - 1;
printf("%lld %lld\n",g[k],ans[k]);
}
return 0;
}