UVA 10780 Again Prime? No Time. 分解质因子
2015-12-26 17:41
501 查看
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,计算n!中有多少个因子m。
题解:先将m分解质因子,再将1-n分解质因子就好了
代码
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,计算n!中有多少个因子m。
题解:先将m分解质因子,再将1-n分解质因子就好了
//meek///#include<bits/stdc++.h> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include<iostream> #include<bitset> #include<vector> using namespace std ; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back #define fi first #define se second #define MP make_pair typedef long long ll; const int N = 10005; const int M = 1000001; const int inf = 0x3f3f3f3f; const int MOD = 1000000007; const double eps = 0.000001; vector<int > G; int H ,n,m,cnt ; int init() { for(int i=2;i<=n;i++) { int tmp = i; for(int j=2;j*j<=tmp;j++) { // if(tmp%j==0) while(tmp%j==0) tmp/=j,H[j]++; } if(tmp!=0) H[tmp] ++; } int ans = inf; for(int i=0;i<G.size();i++) ans=min(ans,H[G[i]]/cnt[G[i]]); return ans; } int main() { int T,cas=1; scanf("%d",&T); while(T--) { G.clear();mem(H);mem(cnt); scanf("%d%d",&m,&n); for(int i=2;i*i<=m;i++) { if(m%i==0) G.pb(i); while(m%i==0) m/=i,cnt[i]++; } if(m!=1) G.pb(m),cnt[m]++; printf("Case %d:\n",cas++); int ans = init(); if(ans == 0|| ans == inf) printf("Impossible to divide\n"); else printf("%d\n",ans); } return 0; }
代码
相关文章推荐
- Canvas,Paint简介
- 【郑轻】[1415]A+B Again!
- Mybaits 语法细节
- Failed to find provider info for com.sina.weibo.sdkProvider
- synchronize和生产者消费者模式
- ping www.baidu.com和ping 8.8.8.8出现ping不通
- 如何解决mysql stop fail的问题
- Binder框架及AIDL解析
- 如果将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法
- iPhone开发教程之retain/copy/assign/setter/getter
- Intellij Idea 主题下载(Eclectide Monokai)
- HDU3537-Daizhenyang's Coin(博弈SG-打表)
- 时间复杂度和空间复杂度详解 http://blog.csdn.net/booirror/article/details/7707551
- 5.3.1.1 ChainMap的例子和技巧
- 5.3.1.1 ChainMap的例子和技巧
- VS2010升级VS2013后,出现没有定义类型“PowerPacks.ShapeContainer”错误解决方法
- RAID 各级别特性
- Gfx.WaitForPresent
- POJ 3735 Training Little Cats
- error TRK0005: Failed to locate: "CL.exe"