This commit is contained in:
암냥 2026-04-18 22:25:36 +09:00
commit 6fef06ae70
No known key found for this signature in database
67 changed files with 436 additions and 1 deletions

59
index.js Normal file
View file

@ -0,0 +1,59 @@
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (line) => {
if (!line) return;
const k = BigInt(line);
let a, b, c, op1, op2;
// k = a^2 + b^2 - c^2 구조로 풀기
// k - a^2 = b^2 - c^2 = (b-c)(b+c)
// 계산의 편의를 위해 a=1 또는 a=2를 선택
// 1. a=1 선택
a = 1n;
let target = k - (a * a);
// target이 홀수라면: (n+1)^2 - n^2 = 2n + 1 = target
// 2n = target - 1 => n = (target - 1) / 2
if (target % 2n !== 0n) {
let n = (target - 1n) / 2n;
b = n + 1n;
c = n;
op1 = '+';
op2 = '-';
}
// target이 짝수라면: a=2로 변경하여 target을 홀수로 만듦
else {
a = 2n;
target = k - (a * a);
let n = (target - 1n) / 2n;
b = n + 1n;
c = n;
op1 = '+';
op2 = '-';
}
// 만약 c가 0이 되는 경우 (target이 1인 경우) 등 예외 처리
// 문제 조건 1 <= a, b, c를 맞추기 위해 조정
if (c === 0n) {
// k = a^2 + b^2 - c^2 가 안되면 다른 조합 시도 (k는 충분히 큼)
// 보통 k가 1 이상이므로 위 로직에서 a, b, c >= 1을 만족하는 해가 대부분 존재함
// k=1인 경우: 1 = 1^2 + 1^2 - 1^2 (1 + 1 - 1)
if (k === 1n) {
console.log("1 + 1 - 1");
} else {
// 일반적인 출력
console.log(`${a} ${op1} ${b} ${op2} ${c}`);
}
} else {
console.log(`${a} ${op1} ${b} ${op2} ${c}`);
}
rl.close();
});