标签:blog article net def sdn size while https href
输入一个 $N$,求最大的 $n$($n \leq N$)和 $x$,使得 $x^2 = \frac{1^2+2^2+...+n^2}{n}$.
将右边式子的分子求和化简,有:$x^2 = \frac{(n+1)(2n+1)}{6}$.
变换成:$(4n+3)^2-48x^2 = 1$.
这就是佩尔方程的形式,且样例给出了最小整数解(7, 1)。
求出long long范围内的所有解(也就9个)
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll n; vector<ll>nn, xx; void init() { ll pre_x = 7, pre_y = 1; nn.push_back(1), xx.push_back(1); for(int i;;i++) { ll tmpx = pre_x*7 + pre_y*1*48; ll tmpy = pre_x*1 + pre_y*7; if(tmpx < 0) break; if((tmpx-3)%4 == 0) { nn.push_back((tmpx-3)/4); xx.push_back(tmpy); } pre_x = tmpx; pre_y = tmpy; } nn.push_back((ll)1e18+5); //设置一个边界 } int main() { init(); //printf("%d\n", nn.size()); while(scanf("%lld", &n) == 1 && n) { for(int i = 0;i < nn.size();i++) { if(n < nn[i]) { printf("%lld %lld\n", nn[i-1], xx[i-1]); break; } } } }
参考链接:https://blog.csdn.net/u011815404/article/details/88723480
标签:blog article net def sdn size while https href
原文地址:https://www.cnblogs.com/lfri/p/11650074.html