UVa 10780 - Again Prime? No time (质因式分解)

Again Prime? No time.
Input: standard input
Output: standard output
Time Limit: 1 second


The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that dividesn!.

 

Input

 

The input file consists of several test cases. The first line in the file is the number of cases to handle. The following lines are the cases each of which contains two integers m (1<m<5000) and n (0<n<10000). The integers are separated by an space. There will be no invalid cases given and there are not more that 500 test cases.

Output

 

For each case in the input, print the case number and result in separate lines. The result is either an integer if m divides n! or a line "Impossible to divide" (without the quotes). Check the sample input and output format.

 

Sample Input

2
2 10
2 100

Sample Output

Case 1:
8
Case 2:
97

 


Problem setter: Anupam Bhattacharjee, CSE, BUET
Thanks to Shabuj for checking and Adrian for alternate solution.

 

"~~ Algorithms are the rhythms of Computer Science ~~



题意:

输入两个正整数nm,求最大的正整数k使得m^k是n!的约数


质因数分解

n!可以分解为2^x1 * 3^x2 * 5^x3 * ... * prime[i]^xi

m同理

m^k要为n!的约数,m^2就是m的分解中的每个指数加倍 并且m^k的分解相应的指数不能超过n!的指数



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 10000 + 20;
int Am[maxn], An[maxn];
int prim[maxn], primNum;
int vis[maxn];

int getPrimeTable(int n) {
    primNum = 0;
    memset(vis, 0, sizeof(vis));
    for(int i=2; i<=n; i++) if(!vis[i]) {
        for(int j=i; j<=n; j+=i) {
            vis[j] = 1;
        }
        prim[primNum++] = i;
    }
    return primNum;
}

void get(int * A, int x) {
    for(int i=0; i<primNum && x > 1; i++) if(x % prim[i] == 0){
        while(x % prim[i] == 0) {
            A[i]++;
            x /= prim[i];
        }
    }
}

int main() {
    int T;

    getPrimeTable(maxn-1);
    scanf("%d", &T);
    for(int kase=1; kase<=T; kase++) {
        int m, n;
        scanf("%d%d", &m, &n);
        memset(Am, 0, sizeof(Am));
        memset(An, 0, sizeof(An));
        get(Am, m);
        for(int i=2; i<=n; i++) {
            get(An, i);
        }
        int ans = INF;
        for(int i=0; i<primNum; i++) {
            if(Am[i] > An[i]) {
                ans = -1;
                break;
            } else if(Am[i] > 0) {
                ans = min(ans, An[i]/Am[i]);
            }
        }
        printf("Case %d:\n", kase);
        if(ans == -1) puts("Impossible to divide");
        else printf("%d\n", ans);
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值