Pagini recente » Cod sursa (job #1688850) | Cod sursa (job #618623) | Cod sursa (job #2513272) | Cod sursa (job #3003422) | Cod sursa (job #1491903)
#include <stdio.h>
#define MAX 5000005
#define min(a, b) (a < b ? a : b)
int n, d, i, fact1[MAX], fact2[MAX], nr, aux, rez;
int main(){
freopen("pascal.in", "r", stdin);
freopen("pascal.out", "w", stdout);
scanf("%d%d", &n, &d);
if(d == 2 || d == 3 || d == 5){
for(i = 1; i <= n; i++){
nr = 0;
aux = i;
while(aux % d == 0){
nr++;
aux /= d;
}
fact1[i] = fact1[i - 1] + nr;
}
for(i = 1; i <= n / 2; i++)
if(fact1[n] - fact1[n - i] - fact1[i] > 0)
rez++;
rez *= 2;
if(!(n & 1))
if(fact1[n] - 2 * fact1[n / 2] > 0)
rez--;
}
else if(d == 4){
for(i = 1; i <= n; i++){
nr = 0;
aux = i;
while(aux % 2 == 0){
nr++;
aux /= 2;
}
fact1[i] = fact1[i - 1] + nr;
}
for(i = 1; i <= n / 2; i++)
if(fact1[n] - fact1[n - i] - fact1[i] > 1)
rez++;
rez *= 2;
if(!(n & 1))
if(fact1[n] - 2 * fact1[n / 2] > 1)
rez--;
}
else if(d == 6){
for(i = 1; i <= n; i++){
nr = 0;
aux = i;
while(aux % 2 == 0){
nr++;
aux /= 2;
}
fact1[i] = fact1[i - 1] + nr;
aux = i;
nr = 0;
while(aux % 3 == 0){
nr++;
aux /= 3;
}
fact2[i] = fact2[i - 1] + nr;
}
for(i = 1; i <= n / 2; i++)
if(min(fact1[n] - fact1[n - i] - fact1[i], fact2[n] - fact2[n - i] - fact2[i]) > 0)
rez++;
rez *= 2;
if(!(n & 1))
if(min(fact1[n] - 2 * fact1[n / 2], fact2[n] - 2 * fact2[n / 2]) > 0)
rez--;
}
printf("%d\n", rez);
return 0;
}