[백준] 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;
}