UVa10780

Posted Yan_Bin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa10780相关的知识,希望对你有一定的参考价值。

10780 Again Prime? No time.
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
22
10
2 100
Sample Output
Case 1:
8
Case 2:
97

题意:
       输入两正整数m、n(1<m<5000、0<n<10000)求最大的正整数k使得m^k是n!的约数。如果无解则输出“Impossible to divide”。

分析:

       枚举m的每个素因子i的幂次p以及n!这一素因子(i)的幂次p2,计算它们的比值res =(int)(p2/p),每枚举一个素因子就更新一次res值。如果res值最终大于零,则那个值就是答案,否则输出“无解”。计算某个素因子i的幂次时,对于一个数m,只需要不断除以i直到除尽i为止;而对于一个正整数n!,则只需要根据公式:n/i + n/(i^2) + n/(i^3) + …计算即可。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 const int inf = 0x3f3f3f3f;
 7 int main(){
 8     int t,n,m,cas = 1;
 9     scanf("%d",&t);
10     while(t--){
11         scanf("%d%d",&m,&n);
12         int i = 2,ans = inf;
13         while(m != 1){
14             int p = 0;
15             while(m % i == 0) m /= i,p++;
16             if(p){
17                 int num = n,p2 = 0;
18                 while(num) p2 += num / i,num /= i;
19                 ans = min(ans,p2 / p);
20             }
21             i++;
22         }
23         printf("Case %d:\\n",cas++);
24         if(ans) printf("%d\\n",ans);
25         else printf("Impossible to divide\\n");
26     }
27     return 0;
28 }
View Code

 

以上是关于UVa10780的主要内容,如果未能解决你的问题,请参考以下文章

UVa 10780 - Again Prime? No Time

UVA10780 Again Prime? No Time.

UVa 1442 - Cave

UVa10410代码

UVa 1593代码对齐

UVa 1593 代码对齐