题目1104:整除问题

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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值