[Codeforces1487D]Pythagorean Triples
Link:https://codeforces.com/problemset/problem/1487/D
description:
Find the numbers of triples\((a,b,c)\)s.t.\(a^2+b^2=c^2\)and \(a^2=b+c\), where 1≤a≤b≤c≤n .
solution:
\(b+c+b^2=c^2\)
\(b(b+1)=c(c-1)\)
\(c=b+1\)
\(a^2=2b+1, b>=3.\)
in other words, we have:\(a^2+(\frac{a^2-1}{2})^2=(\frac{a^2+1}{2})^2\)
\(\frac{a^2+1}{2}\leqslant n\), i.e. \(a^2\leqslant 2n-1\), \(1<a\leqslant\sqrt{2n-1}\), odd \(a\).
so the anser is :
code:
#include<cstdio>
#include<cmath>
int main() {
int T; scanf("%d", &T);
while (T--) {
int n; scanf("%d", &n);
int bound = floor(sqrt(2 * n - 1));
if (bound & 1)printf("%d\n", bound >> 1);
else printf("%d\n", (bound >> 1) - 1);
}
}