Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

걸음마부터 달리기

SWEA 8016. 홀수 피라미드 (long 범위를 넘어가는 BigInteger) 본문

카테고리 없음

SWEA 8016. 홀수 피라미드 (long 범위를 넘어가는 BigInteger)

성추 2025. 4. 18. 22:45

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AWvzGUKKPVwDFASy&categoryId=AWvzGUKKPVwDFASy&categoryType=CODE&problemTitle=&orderBy=INQUERY_COUNT&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=11

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

재밌는 문제이다. 개발하면서 쓰지도 않아본 BigInteger를 코테하면서 쓸 줄이야.

 

BigInteger는 클래스라서 사칙연산에 대해 메서드 호출로 해야된다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

class Main {
    static int T;
    static BigInteger N;

    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        T = Integer.parseInt(br.readLine());
        for (int tc = 1; tc <= T; tc++) {
            N = new BigInteger(br.readLine());

            BigInteger two = BigInteger.valueOf(2);
            BigInteger one = BigInteger.ONE;

            BigInteger termN = N.multiply(N); // N * N
            BigInteger termN1 = termN.subtract(N.multiply(two).subtract(one)); // N^2 - (2N - 1)
            BigInteger first = termN1.multiply(two).subtract(one).add(two); // (2 * termN1 - 1) + 2
            BigInteger second = termN.multiply(two).subtract(one); // (2 * termN - 1)

            System.out.printf("#%d %s %s\n", tc, first.toString(), second.toString());
        }
    }
}

계차수열로 풀 수 있다는데 수학 공부를 하는게 아니니까 스킵