題目描述
給出一個整數N,將N表示爲2個整數i j的平方和(i <= j),如果有多種表示,按照i的遞增序輸出。
例如:N = 130,130 = 3^2 + 11^2 = 7^2 + 9^2 (注:3 11同11 3算1種)
Input
一個數N(1 <= N <= 10^9)
Output
共K行:每行2個數,i j,表示N = i^2 + j^2(0 <= i <= j)。
如果無法分解爲2個數的平方和,則輸出No Solution
Input示例
130
Output示例
3 11
7 9
做法:一開始覺得判重不好搞,然後突然發現N只有10^9。。
代碼如下:
#include <cstdio>
#include <cstring>
#include <cmath>
#define N 1000007
#include <iostream>
using namespace std;
int n;
bool b[N];
int main()
{
cin >> n;
bool v = 1;
for (int i = 0; i <= sqrt(n) + 1; i++)
{
long long p = n - i * i;
double q = sqrt((double)p);
long long q1 = sqrt(p);
if (q == q1 && !b[q1])
{
b[q1] = 1;
v = 0;
b[i] = 1;
printf("%lld %lld\n", i, q1);
}
}
if (v) printf("No Solution");
}