※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 11522번 문제인 Sum Kind of Problem이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/11522 

 

11522번: Sum Kind of Problem

For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by three space separated integers S1, S2 and S3 such that: S1 = The sum of the first N positive integers. S2 = The

www.acmicpc.net

1부터 N까지의 수의 합은 N(N+1)/2와 같고, 1부터 2N까지의 짝수의 합은 1부터 N까지의 수의 합의 두배이므로 N(N+1)로 구해진다.

 

1부터 2N까지의 홀수의 합은 1부터 2N까지의 짝수의 합에서 N을 빼 구할 수 있으므로 N^2으로 계산할 수 있다.

 

이를 이용해 문제를 해결하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int T; cin >> T;
	while (T--) {
		int K; ll N; cin >> K >> N;
		cout << K << ' ' << N * (N + 1) / 2 << ' ' << N * N << ' ' << N * (N + 1) << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 2520 // C++] 팬케이크 사랑  (0) 2023.02.09
[BOJ 1092 // C++] 배  (0) 2023.02.08
[BOJ 27433 // C++] 팩토리얼 2  (0) 2023.02.07
[BOJ 27389 // C++] Metronome  (0) 2023.02.07
[BOJ 11340 // C++] Making the Grade?  (0) 2023.02.07

+ Recent posts