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分解质因子就好了

  1. //meek///#include<bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include<iostream>
  7. #include<bitset>
  8. #include<vector>
  9. using namespace std ;
  10. #define mem(a) memset(a,0,sizeof(a))
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14. #define MP make_pair
  15. typedef long long ll;
  16.  
  17. const int N = ;
  18. const int M = ;
  19. const int inf = 0x3f3f3f3f;
  20. const int MOD = ;
  21. const double eps = 0.000001;
  22.  
  23. vector<int > G;
  24. int H[N],n,m,cnt[N];
  25. int init() {
  26. for(int i=;i<=n;i++) {
  27. int tmp = i;
  28. for(int j=;j*j<=tmp;j++) {
  29. // if(tmp%j==0)
  30. while(tmp%j==) tmp/=j,H[j]++;
  31. }
  32. if(tmp!=) H[tmp] ++;
  33. }
  34. int ans = inf;
  35. for(int i=;i<G.size();i++) ans=min(ans,H[G[i]]/cnt[G[i]]);
  36. return ans;
  37. }
  38. int main() {
  39. int T,cas=;
  40. scanf("%d",&T);
  41. while(T--) {
  42. G.clear();mem(H);mem(cnt);
  43. scanf("%d%d",&m,&n);
  44. for(int i=;i*i<=m;i++) {
  45. if(m%i==) G.pb(i);
  46. while(m%i==) m/=i,cnt[i]++;
  47. }
  48. if(m!=) G.pb(m),cnt[m]++;
  49. printf("Case %d:\n",cas++);
  50. int ans = init();
  51. if(ans == || ans == inf) printf("Impossible to divide\n");
  52. else
  53. printf("%d\n",ans);
  54. }
  55. return ;
  56. }

代码

UVA 10780 Again Prime? No Time. 分解质因子的更多相关文章

  1. Alternate Task UVA - 11728 (暴力。。分解质因子)

    题意: 输入一个正整数S,(S  <= 1000)求一个最大的正整数N,使得N的所有正因子之和为S. 解析: ..求1000以内的所有数的正因子和 ...输出.. #include <io ...

  2. HDU 4497 GCD and LCM(分解质因子+排列组合)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...

  3. hdu6237 分解质因子

    题意:给一堆石子,每次移动一颗到另一堆,要求最小次数使得,所有石子数gcd>1 题解:枚举所有质因子,然后找次数最小的那一个,统计次数时,我们可以事先记录下每堆石子余质因子 的和,对所有石子取余 ...

  4. NYOJ-476谁是英雄,分解质因子求约数个数!

    谁是英雄 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 十个数学家(编号0-9)乘气球飞行在太平洋上空.当横越赤道时,他们决定庆祝一下这一壮举.于是他们开了一瓶香槟.不 ...

  5. Minimum Sum LCM UVA - 10791(分解质因子)

    对于一个数n 设它有两个不是互质的因子a和b   即lcm(a,b) = n 且gcd为a和b的最大公约数 则n = a/gcd * b: 因为a/gcd 与 b 的最大公约数也是n 且 a/gcd ...

  6. Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)

    题目链接 题目大意 给定a,b,c,d四个数,其中a<c,b<c,现在让你寻找一对数(x,y),满足一下条件: 1. a<x<c,b<y<d 2. (x*y)%(a ...

  7. HDU 4135 Co-prime (容斥+分解质因子)

    <题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...

  8. BNU 13259.Story of Tomisu Ghost 分解质因子

    Story of Tomisu Ghost It is now 2150 AD and problem-setters are having a horrified time as the ghost ...

  9. Uva 10780 Again Prime? No Time.(分解质因子)

    题意:给你两个数m和n,问 n! 可以被 m^k 整除的k的最大值 思路:从这道我们可以想到n!末尾有多少个0的问题,让我们先想一下它的思想,我们找 n! 末尾有多少0, 实际上我们是在找n!中5的个 ...

随机推荐

  1. python: 命令选项及归类

    usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresp ...

  2. git的初步使用

    1.在GitHub上建立项目登录GitHub后,你可以在右边靠中那里找到一个按钮“New Repository”,点击过后,填入项目名称.说明和网址过后就可以创建了,然后会出现一个提示页面,记下类似g ...

  3. iOS学习之UI自定义cell

    一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...

  4. 条款38:通过聚合设计has-a或者is-implemented-in-terms-of

    聚合:类型之间的一种关系,就是一种类型内含有另一种类型的变量. has-a: class Address { }; class PhoneNumber { }; class Person { publ ...

  5. ATR与ATS

    ATR:answer to reset  复位应答 ATS:answer to select 选择应答

  6. MyEclipse 中的各种有的没的快捷方式

    快捷键1 (CTRL) Ctrl+1 快速修复Ctrl+D: 删除当前行  Ctrl+Q  定位到最后编辑的地方  Ctrl+L  定位在某行   Ctrl+O  快速显示 OutLine  Ctrl ...

  7. Xamarin.Android之山有木兮之木有枝,心悦君兮君不知。

    Xamarin.Android之山有木兮之木有枝,心悦君兮君不知. 第一步,写项目中的第一个界面. <?xml version="1.0" encoding ="  ...

  8. Careercup - Microsoft面试题 - 5680049562845184

    2014-05-10 06:51 题目链接 原题: "How would you find the number of gas stations in the United States?& ...

  9. Surface Pro 3 扩展坞体验

    Surface Pro 3 的确是Microsoft的诚意之作,虽然价格不便宜,但值得入手. 买完Surface Pro 3 后,没事在某网站上闲逛,这货(扩展坞)映入我的眼帘,看完后,心想,这货必须 ...

  10. WebService相关概念介绍

    最近重新拾起WebService,之前用过Axis2开发过服务,但是非常具体的概念还不是很清楚,在此粗略总结一下. 本文重点研究以下几个问题:     1.WebService以及相关的概念介绍    ...