Notice
Recent Posts
Recent Comments
Link
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

hwooo

BOJ (C/C++) 2312번: 수 복원하기 본문

Study/Algorithm

BOJ (C/C++) 2312번: 수 복원하기

hwooo 2022. 11. 30. 17:29

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

 

2312번: 수 복원하기

첫째 줄에 테스트 케이스의 수가 주어진다. 각 테스트 케이스마다 양의 정수 N (2 ≤ N ≤ 100,000)이 주어진다.

www.acmicpc.net


풀이

에라토스테네스의 체를 활용하여 10만까지의 소수를 구하고, N이 들어올 때마다 소인수분해를 하였다.

코드

#include <stdio.h>
#include <vector>
using namespace std;

vector <int> Prime;

void Get_Prime() {
	bool A[100001] = { 0, };
	int i, j, mul;
	for (i = 2; i <= 100000; i++) {
		if (A[i]) continue;
		for (j = 2; j < i; j++) {
			if (i%j == 0) break;
		}
		if (i == j) {
			A[i] = true;
			Prime.push_back(i);
			mul = i;
			while (mul + i <= 100000) {
				mul += i;
				A[mul] = true;
			}
		}
	}
}
void Print(int n) {
	int i = 0, cnt = 0;
	while (n > 1) {
		while (n%Prime[i] == 0) n /= Prime[i], cnt++;
		if (cnt) printf("%d %d\n", Prime[i], cnt);
		i++, cnt = 0;
	}
}
int main() {
	int T, n;
    
	scanf("%d", &T);
    
	Get_Prime();
	for (int t = 0; t < T; t++) {
		scanf("%d", &n);
		Print(n);
	}
	return 0;
}

'Study > Algorithm' 카테고리의 다른 글

BOJ (C/C++) 11403번: 경로 찾기  (0) 2022.12.01
BOJ (C/C++) 2747번: 피보나치 수  (0) 2022.12.01
BOJ (C/C++) 1237번: 정ㅋ벅ㅋ  (0) 2022.11.30
BOJ (C/C++) 1267번: 핸드폰 요금  (0) 2022.11.30
BOJ (C/C++) 1233번: 주사위  (0) 2022.11.30