about summary refs log tree commit diff
path: root/others/other/fdp.c
blob: 439716bb0e8624efe1b6a85c4bc3e969bd5ecc9b (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <math.h>
#include <stdio.h>

int main()
{
	FILE *F = fopen("FDP.INP", "r");
	long long n, m, j, factors[11];
	long i;
	char f = 0, fm[11] = {}, fn[11] = {}, k = 127;

	fscanf(F, "%lld %lld", &n, &m);
	fclose(F);

	if (m % 2 == 0) {
		factors[0] = 2;
		do {
			m /= 2;
			fm[0]++;
		} while (m % 2 == 0);
		f = 1;
	}

	for (i = 3; i <= (long) sqrt((double) m); i += 2)
		if (m % i == 0) {
			factors[f] = i;
			do {
				m /= i;
				fm[f]++;
			} while (m % i == 0);
			f++;
		}

	if (m != 1) {
		factors[f] = m;
		fm[f] = 1;
		f++;
	}

	for (i = 0; i < f; i++) {
		for (j = factors[i]; j <= n; j *= factors[i])
			fn[i] += n / j;
		k = (fn[i] / fm[i] < k) ? (fn[i] / fm[i]) : k;
	}

	F = fopen("FDP.OUT", "w");
	fprintf(F, "%hhd\n", k);
	fclose(F);

	return 0;
}