UVA 10780-Again Prime? No Time. (数学-质因子)
来源:互联网 发布:name.com 域名证书 编辑:程序博客网 时间:2024/06/06 07:50
Description
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 integersm (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 that500 test cases.
Output
For each case in the input, print the case number and result in separate lines. The result is either an integer ifm 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 的最多次幂。
若m 是素数时,只要将n 的阶层中的所有数单个整除m 即可。若m 非素数,则 阶层中其他书整除之后得到的数相乘得到的数可以整除m。
所以题目转化成求m 的质因子,和质因子的幂,最少数目的幂就是答案。
CODE:
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;int prime[5005],p[5005];bool is_prime(int x){ for(int i=2;i*i<=x;i++) { if(x % i == 0) return false; } return true;}void prime_biao(){ int cnt=0; for(int i=2;i<=5005;i++) { if(is_prime(i)) prime[cnt++]=i; }}int seize(int n){ int k=0; memset(p,0,sizeof(p)); for(int i=0;i<n;i++) { if(n < prime[i]) break; if(n % prime[i] == 0) { p[k++] = prime[i]; } } return k;}int mul(int nn,int m){ int ans =0 ; for(int i=m;i<=nn;i++) { int ii=i; while(ii > 0) { if(ii % m) break; if(ii % m == 0) { ans++; ii /=m; } } } return ans;}int mul2(int n,int m){ int ans=0; while(n>0){ if(n % m) break; if(n % m ==0){ ans++; n /= m; } } return ans;}int main(){ //freopen("in.in","r",stdin); int T,m,n; scanf("%d",&T); prime_biao(); for(int t_=1;t_<=T;t_++) { scanf("%d%d",&m,&n); int kk=seize(m); int ans = inf; for(int i=0;i<kk;i++) { int a=mul(n,p[i])/mul2(m,p[i]); ans = min(ans, a); } if(ans == 0) { printf("Case %d:\nImpossible to divide\n",t_); continue; } printf("Case %d:\n%d\n",t_,ans); } return 0;}
- UVA 10780-Again Prime? No Time. (数学-质因子)
- uva 10780 Again Prime? No Time. 质因子乱搞
- UVA - 10780 Again Prime? No Time. (质因子分解)
- UVA 10780 Again Prime? No Time. [质因子分解]【数论】
- Uva 10780 Again Prime? No time. 解题报告(数学)
- UVa 10780 Again Prime? No Time. (数论&素因子分解)
- Again Prime? No Time.(UVA 10780)
- UVa 10780 - Again Prime? No time (质因式分解)
- uva 10780 Again Prime? No Time.
- UVa 10780-Again Prime? No Time.
- UVA 10780 - Again Prime No Time
- UVA - 10780 Again Prime? No Time.
- UVa:10780 Again Prime? No Time.
- 【UVa 10780】 Again Prime? No time.
- UVA 10780 Again Prime? No time
- UVa - 10780 - Again Prime? No Time.
- [UVa 10780]Again Prime?No time.
- UVa 10780 - Again Prime? No Time.
- pycharm 3.4.1 安装
- [LeetCode]Remove Duplicates from Sorted Array
- FineUI Grid不显示数据解决方案
- Nginx使用教程一(安装与配置)
- GetDIBits使用方法
- UVA 10780-Again Prime? No Time. (数学-质因子)
- java多线程-概述-创建线程_继承Thread类-run和start特点
- EL 表达式中自定义函数
- 冒泡排序
- 大数进制转化
- 循环-12. 打印九九口诀表(15)
- 异步POST请求及加载信息到UitabView上
- cin标准输入流错误处理
- 使用 audio 和 embed 实现浏览器兼容的网页声音播放