UVA 10780 C - Again Prime? No Time.
2016-07-18 23:47
387 查看
Question:
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
题意大意:输入两个数n,m,求最大的k使得m^k为n!的约数。
思路:将m质因数分解,并求出每个质因数的数量,找到n!对应的质因数的个数,其系数比值最小的则为K的值
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/C)
体会:重点求n!中质因数的个数while(n!=0) { t2+=n/prim[j];n/=prim[j];}自己去验证,很奇妙
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
题意大意:输入两个数n,m,求最大的k使得m^k为n!的约数。
思路:将m质因数分解,并求出每个质因数的数量,找到n!对应的质因数的个数,其系数比值最小的则为K的值
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/C)
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int INF=0x3f3f3f3f; int prim[10005],vis[10005],curn=0; void init() { memset(vis,0,sizeof(vis)); for(int i=2;i<=10000;i++) { if(!vis[i]) { prim[curn++]=i; for(int j=i;j*i<=10000;j++) vis[i*j]=1; } } } //构建质数数组; int main() { int n,m,k; 4000 init(); scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d%d",&m,&n); printf("Case %d:\n",i); int t1,t2,t,sum=INF; for(int j=0;m!=1;j++) { t1=t2=0; while(m%prim[j]==0) { t1++; m/=prim[j]; } //找到每个质因数的个数 if(t1!=0) { t=n; while(t!=0) { t2+=t/prim[j]; t/=prim[j]; } //找到n!对应的质因数的个数; sum=min(t2/t1,sum); } } if(sum==0) printf("Impossible to divide\n"); else printf("%d\n",sum); } return 0; }
体会:重点求n!中质因数的个数while(n!=0) { t2+=n/prim[j];n/=prim[j];}自己去验证,很奇妙
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法