Algorithm

수학_백준_팩토리얼 0의 개수_1676

MoYoungmin 2018. 8. 21. 01:34

N! = 1 x 2 x 3 x - - - x N

팩토리얼 값은 매우크다

10! = 3628800 으로 우리가 보통 문제에서 풀 수있는 최대 값이다.




이 문제는 실제로 팩토리얼 값을 구할 수 없다. 데이터의 범위를 초과하기 때문이다. 여기서 알아야 할것은 0의 개수이지 값이 아니다. 즉 N을 소인수 분해 했을 떄, 2와 5의 개수를 세어주면 된다.

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
29
#include <iostream>
using namespace std;
 
int main() {
    int input, value;
    int twoCount = 0, fiveCount = 0;
    cin >> value;
 
    while (value > 1) {
        input = value--;
        while ((input % 2 == 0|| (input % 5 == 0)) {
            if (input % 2 == 0) {
                input /= 2;
                twoCount++;
            }
            else if (input % 5 == 0) {
                input /= 5;
                fiveCount++;
            }
        }
    }
 
    if (twoCount > fiveCount) {
        cout << fiveCount << endl;
    }
    else {
        cout << twoCount << endl;
    }
}
cs