문제 링크입니다: https://www.acmicpc.net/problem/1024
1024번: 수열의 합
첫째 줄에 N과 L이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이고, L은 2보다 크거나 같고, 100보다 작거나 같은 자연수이다.
www.acmicpc.net
수열의 시작 직전인 임의의 x에 대해
N = (x + 1) + ... + (x + L) 로 정의할 수 있습니다.
따라서, N = Lx + L * (L + 1) / 2 로 정의할 수 있으며 이는 즉 N - (L * (L + 1) / 2) 가 L로 나누어 떨어지는 시점에서 (x + 1) 부터 (x + L) 까지 출력하면 된다는 뜻입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int N, L; | |
cin >> N >> L; | |
for (int i = L; i <= 100; i++) | |
{ | |
int temp = N - i * (i + 1) / 2; | |
if (temp % i == 0) | |
{ | |
int j = temp / i + 1; | |
if (j >= 0) | |
{ | |
for (int k = 0; k < i; k++) | |
{ | |
cout << k + j << " "; | |
} | |
cout << "\n"; | |
return 0; | |
} | |
} | |
} | |
cout << -1 << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 2355번 시그마 (0) | 2019.11.11 |
---|---|
백준 9506번 약수들의 합 (0) | 2019.11.11 |
백준 1834번 나머지와 몫이 같은 수 (0) | 2019.11.11 |
백준 4504번 배수 찾기 (0) | 2019.11.10 |
백준 1964번 오각형, 오각형, 오각형... (0) | 2019.11.10 |