UVA - 10780 Again Prime? No Time. (质因子分解)

Description

Download as PDF

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 divides n!.

 

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

题意:给你两个整数m和n,求最大的k使得m^k是n!的约数

思路:首先我们先将m分解质因数,那么m^k就是各个质因数*k而已,那么我们只要判断每个质因数的指数在n!的对应的质因数的指数范围内,求所有质因数最小的一个,至于

怎么求n!里某个质因数的个数,比如是2的话,那么只要计算n/2+n/4+n/8...的个数就行了,可以这么想没隔2个数就有一个2,然后是4...

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int inf = 0x3f3f3f3f;

int main() {
	int t, n, m, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &m, &n);
		int i = 2;
		int ans = inf;
		while (m != 1) {
			int p = 0;
			while (m % i == 0) {
				m /= i;
				p++;
			}
			if (p) {
				int num = n;
				int tmp = 0;
				while (num) {
					tmp += num/i;
					num /= i;
				}
				ans = min(ans, tmp/p);
			}
			i++;
		}
		printf("Case %d:\n", cas++);
		if (ans)
			printf("%d\n", ans);
		else printf("Impossible to divide\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值