조합 0의 개수

딱 2,000,000,000까지가 int라서 i는 long long을 해줘야 한다.

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
#include <time.h>
using namespace std;

int fiveCount(int n) {
	int cnt = 0;
	for (long long i = 5; i <= n; i *= 5) {
		cnt = cnt + n / i;
	}
	return cnt;
}
int twoCount(int n) {
	int cnt = 0;
	for (long long i = 2; i <= n; i *= 2) {
		cnt = cnt + n / i;
	}
	return cnt;
}

int get_min(int a, int b) {
	return a < b ? a : b;
}

int main() {
	//freopen("input.txt", "rt", stdin);

	int n, m;

	scanf("%d %d", &n, &m);

	
	int five = fiveCount(n);
	if (m != 0) five -= fiveCount(m);
	if (n != m) five -= fiveCount(n - m);


	int two = twoCount(n);
	if (m != 0) two -= twoCount(m);
	if (n != m) two -= twoCount(n - m);

	printf("%d", get_min(five, two));

	return 0;
}

좋은 웹페이지 즐겨찾기