码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-18 14:34:52      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

Description

bubuko.com,布布扣

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


UVA - 10780 Again Prime? No Time. (质因子分解),布布扣,bubuko.com

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

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/u011345136/article/details/38658977

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!