Pagini recente » Monitorul de evaluare | Cod sursa (job #542474) | Cod sursa (job #77947) | Cod sursa (job #1469607) | Cod sursa (job #26301)
Cod sursa(job #26301)
#include <stdio.h>
#define LL unsigned long long
int N, B;
int pr[20];
int nr[20];
LL get_nr(int x, int N)
{
int aux = x, q, r;
LL rez = 0;
for (; x > 0 && x <= N; x *= aux) {
q = N / x;
r = N % x;
rez += (LL) x * (q-1) * (q) / 2 + (LL) q * (r + 1);
}
return rez;
}
int main()
{
int T, i;
LL jeg;
freopen("zero2.in", "r", stdin);
freopen("zero2.out", "w", stdout);
for (T = 1; T <= 10; T++) {
scanf("%d %d", &N, &B);
pr[0] = 0;
for (i = 2; i * i <= B; i++) {
if (B % i == 0) pr[++pr[0]] = i, nr[pr[0]] = 0;
while (B % i == 0) {
B /= i;
nr[pr[0]]++;
}
}
if (B > 1) pr[++pr[0]] = B, nr[pr[0]] = 1;
LL rez = ((LL)1 << 62);
for (i = 1; i <= pr[0]; i++) {
jeg = get_nr(pr[i], N);
if (jeg / nr[i] < rez) rez = jeg / nr[i];
}
printf("%llu\n", rez);
}
fclose(stdin);
fclose(stdout);
return 0;
}