UVA 10780 Again Prime? No Time. ——质因分解

来源:互联网 发布:tp路由器访客网络设置 编辑:程序博客网 时间:2024/05/22 00:27
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#define OUT freopen("out.txt", "w", stdout);using namespace std;const int maxn = 1e4 + 5;const int INF = 0x3f3f3f3f;bool isprime[maxn];int prime[maxn], primecnt;void init() {    memset(isprime, true, sizeof(isprime));    primecnt = 0;    for (int i = 2; i < maxn; i++) {        if (isprime[i]) {            prime[++primecnt] = i;            for (int j = i * 2; j < maxn; j += i) isprime[j] = false;        }    }}int p[maxn], q[maxn];void breakdown(int x, int *a) {    for (int i = 1; i <= primecnt && x > 1; i++) {        while (x % prime[i] == 0 && x > 1) {            a[i]++;            x /= prime[i];        }    }}int ans;bool solve() {    ans = INF;    for (int i = 1; i <= primecnt; i++) {        if (p[i] > q[i]) return false;        if (p[i] == 0) continue;        ans = min(ans, (int)(q[i]/p[i]));    }    return true;}int main() {    //OUT;    init();    int T; scanf("%d", &T);    for (int kase = 1; kase <= T; kase++) {        int n, m; scanf("%d %d", &n, &m);        memset(p, 0, sizeof(p));        memset(q, 0, sizeof(q));        breakdown(n, p);        for (int i = 2; i <= m; i++) breakdown(i, q);        printf("Case %d:\n", kase);        if (!solve()) printf("Impossible to divide\n");        else printf("%d\n", ans);    }    return 0;}