java实现:
import java.util.Scanner;
import java.io.IOException;
import java.io.FileReader;
import java.util.Vector;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
class Main
{
public static final boolean DEBUG = false;
public static final int N = 1000;
public static boolean[] vis;
public static Vector<Integer> vPrime;
public static void sieve()
{
vis = new boolean[N];
Arrays.fill(vis, true);
vis[0] = vis[1] = false;
for (int i = 2; i < 35; i++) {
if (vis[i]) {
for (int j = i * i; j < N; j += i) {
vis[j] = false;
}
}
}
vPrime = new Vector<Integer>();
for (int i = 2; i < N; i++) {
if (vis[i]) {
vPrime.add(new Integer(i));
}
}
}
public static void main(String[] args) throws IOException
{
Scanner cin;
int n, a;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(System.in);
}
sieve();
while (cin.hasNext()) {
n = cin.nextInt();
a = cin.nextInt();
TreeMap<Integer, Integer> m = new TreeMap<Integer, Integer>();
for (int i = 0; i < vPrime.size(); i++) {
int div = vPrime.elementAt(i);
if (div > a) break;
if (a % div == 0) {
int c = 0;
while (a % div == 0) {
c++;
a /= div;
}
m.put(div, c);
}
}
if (a != 1) m.put(a, 1);
TreeMap<Integer, Integer> m2 = new TreeMap<Integer, Integer>();
for (int i = 2; i <= n; i++) {
int tmp = i;
Set<Integer> s = m.keySet();
Iterator<Integer> it = s.iterator();
while (it.hasNext()) {
int div = it.next().intValue();
if (div > tmp) break;
if (tmp % div == 0) {
int c = 0;
while (tmp % div == 0) {
c++;
tmp /= div;
}
if (m2.containsKey(div)) {
int temp = m2.get(div);
m2.put(div, temp + c);
} else {
m2.put(div, c);
}
}
}
}
int ans = Integer.MAX_VALUE;
Vector<Integer> v = new Vector<Integer>(m.values());
int i = 0;
Collection<Integer> c = m2.values();
Iterator<Integer> it = c.iterator();
while (it.hasNext()) {
ans = Math.min(ans, it.next().intValue() / v.elementAt(i));
i++;
}
if (ans == Integer.MAX_VALUE) ans = 0;
System.out.println(ans);
}
}
}
C++实现:
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <bitset>
#include <algorithm>
#include <limits>
using namespace std;
const int N = 1000;
bitset<N> vis;
vector<int> vPrime;
void sieve();
int main()
{
int n, a;
#ifndef ONLINE_JUDGE
ifstream cin("d:\\OJ\\uva_in.txt");
#endif // ONLINE_JUDGE
sieve();
while (cin >> n >> a) {
map<int, int> m;
for (int i = 0; i < vPrime.size(); i++) {
if (vPrime[i] > a) break;
if (a % vPrime[i] == 0) {
int c = 0;
while (a % vPrime[i] == 0) {
c++;
a /= vPrime[i];
}
m[vPrime[i]] = c;
}
}
if (a != 1) m[a] = 1;
map<int, int> m2;
for (int i = 2; i <= n; i++) {
int tmp = i;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
if (it->first > tmp) break;
if (tmp % it->first == 0) {
int c = 0;
while (tmp % it->first == 0) {
c++;
tmp /= it->first;
}
m2[it->first] += c;
}
}
}
vector<int> v;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) v.push_back(it->second);
int i = 0;
int ans = numeric_limits<int>::max();
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++, i++) {
it->second = it->second / v[i];
ans = min(ans, it->second);
}
if (ans == numeric_limits<int>::max()) ans = 0;
cout << ans << endl;
}
return 0;
}
void sieve()
{
vis.set();
vis[0] = vis[1] = 0;
for (int i = 2; i < 40; i++) {
if (vis[i]) {
for (int j = i * i; j < N; j += i) {
vis[j] = 0;
}
}
}
for (int i = 0; i < N; i++) {
if (vis[i]) vPrime.push_back(i);
}
}