※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 13226번 문제인 Divisors Again이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/13226
13226번: Divisors Again
The first line will contain an integer C with the number of ranges to process. The next C lines will contain a pair of integers L, U. You have to count the divisors for each number in the range and output the biggest count. Constraints 1 <= C <= 10 L <= U
www.acmicpc.net
주어진 각 수의 약수의 개수를 세어 문제를 해결하자.
각 n의 소인수는 sqrt(n) 이하라는 점을 이용해 효율적으로 소인수분해를 해보자.
아래는 제출한 소스코드이다.
#include <iostream>
using namespace std;
void solve() {
int L, R; cin >> L >> R;
int ans = 1;
while (L <= R) {
int cur = L;
int tmp = 1;
for (int i = 2; i * i <= L; i++) {
int cnt = 1;
while (cur % i == 0) {
cnt++;
cur /= i;
}
tmp *= cnt;
}
if (cur > 1) tmp *= 2;
ans = max(ans, tmp);
L++;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T;
while (T--) {
solve();
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 12353 // C++] Dr. Spaceman의 특별한 알고리즘 (0) | 2022.07.17 |
---|---|
[BOJ 12352 // C++] Hedgemony (Large) (0) | 2022.07.17 |
[BOJ 13982 // C++] Shopping (0) | 2022.07.15 |
[BOJ 25025 // C++] 다항식 계산 (0) | 2022.07.14 |
[BOJ 24956 // C++] 나는 정말 휘파람을 못 불어 (0) | 2022.07.13 |