[백준] 22986 Flat Earth

0

백준

목록 보기
266/271
post-thumbnail

[백준] 22986 Flat Earth

틀린 풀이

  • 시간 초과
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t;
	cin >> t;
	while (t--) {
		ll N;
		ll K;
		cin >> N >> K;
		ll answer = N;
		for (int i = 1; i <= K; ++i) {
			if ((N - i) <= 0) break;
			answer += (N - i);
		}
		answer *= 4;
		cout << answer << "\n";
	}

	return 0;
}

풀이

  • 등차수열의 합 공식 이용
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t;
	cin >> t;
	while (t--) {
		ll N;
		ll K;
		cin >> N >> K;
		ll answer = N;
		if(K > 0) answer += min(K, N) * ((N-1)+ max(N-K, 0LL)) / 2;
		answer *= 4;
		cout << answer << "\n";
	}

	return 0;
}

profile
Be able to be vulnerable, in search of truth

0개의 댓글