題目描述
方程: a^2 + b^2 + c^2 = 1000
這個方程有正整數解嗎?有:a,b,c=6,8,30 就是一組解。
求出 a^2 + b^2 + c^2 = n(1<=n<=10000)的所有解,解要保證c>=b>=a>=1。
輸入
存在多組測試數據,每組測試數據一行包含一個正整數n(1<=n<=10000)
輸出
如果無解則輸出"No Solution"。
如果存在多解,每組解輸出1行,輸出格式:a b c,以一個空格分隔
按照a從小到大的順序輸出,如果a相同則按照b從小到大的順序輸出,如果a,b都相同則按照c從小到大的順序輸出。
樣例輸入 Copy
4
1000
樣例輸出 Copy
No Solution
6 8 30
10 18 24
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define x first
#define y second
using namespace std;
const int N = 2500010;
int n, m;
unordered_map<int, int> S;
int main()
{
while(cin >> n){
int res = 0;
for (int c = 0; c * c <= n; c ++ )
{
int t = c * c;
if (S.count(t) == 0) S[t] = {c};
}
for (int a = 1; a * a <= n; a ++ )
for (int b = a; a * a + b * b <= n; b ++ )
{
int t = n - a * a - b * b;
if (S.count(t) && a <= b && b<= S[t])
{
printf("%d %d %d\n", a, b, S[t]);
res++;
}
}
if(!res){
cout << "No Solution" << endl;
}
}
return 0;
}