Given two integers N and D, you will have to find how many of the factors of N! (factorial N) are divisible by D.
Input
The input file contains several lines of input. Each line contains two integers N (0<=N<= 100) and D(0<|D|<=2^31-1). Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each line of input produce one line of output. This line contains a single integer, which denotes of many different factors of N! are divisible byD.
Sample Input
10 2
9 3
0 0
Sample Output
240
128
主要用到素数筛选法,求出50000内的素数#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
using namespace std;
const int N = 50000;
vector<int> vPrime;
bool vis[N];
int n, d;
void calc(int n, map<int, int>& m);
void sieve_of_sundaram();
bool input();
void solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
#endif
sieve_of_sundaram();
while (input()) {
solve();
}
return 0;
}
bool input()
{
scanf("%d%d", &n, &d);
if (n == 0 && d == 0) return false;
d = abs(d);
return true;
}
void solve()
{
if (n == 0 && d == 1) {
printf("1\n");
return;
}
map<int, int> nmap, dmap;
for (int i = 2; i <= n; i++) calc(i, nmap);
calc(d, dmap);
for (map<int, int>::iterator it = dmap.begin(); it != dmap.end(); it++) {
if (nmap.count(it->first) == 0) {
printf("0\n");
return;
}
if (nmap[it->first] < it->second) {
printf("0\n");
return;
}
nmap[it->first] -= it->second;
}
long long ans = 1;
for (map<int, int>::iterator it = nmap.begin(); it != nmap.end(); it++) {
ans *= (nmap[it->first] + 1);
}
printf("%lld\n", ans);
}
void sieve_of_sundaram()
{
memset(vis, false, sizeof(vis));
int m = (int)sqrt(N / 2);
for (int i = 1; i < m; i++) {
if (vis[i]) continue;
for (int k = 2 * i + 1, j = 2 * i * (i + 1); j < N; j += k) {
vis[j] = true;
}
}
vPrime.push_back(2);
for (int i = 1; i < N / 2; i++) {
if (!vis[i]) vPrime.push_back(2 * i + 1);
}
}
void calc(int n, map<int, int>& m)
{
for (size_t i = 0; i < vPrime.size(); i++) {
int cnt = 0;
if (n < vPrime[i]) break;
if (n % vPrime[i] == 0) {
while (n % vPrime[i] == 0) {
n /= vPrime[i];
cnt++;
}
m[vPrime[i]] += cnt;
}
}
if (n != 1) m[n] += 1;
}