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

//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 = ;
const int M = ;
const int inf = 0x3f3f3f3f;
const int MOD = ;
const double eps = 0.000001; vector<int > G;
int H[N],n,m,cnt[N];
int init() {
for(int i=;i<=n;i++) {
int tmp = i;
for(int j=;j*j<=tmp;j++) {
// if(tmp%j==0)
while(tmp%j==) tmp/=j,H[j]++;
}
if(tmp!=) H[tmp] ++;
}
int ans = inf;
for(int i=;i<G.size();i++) ans=min(ans,H[G[i]]/cnt[G[i]]);
return ans;
}
int main() {
int T,cas=;
scanf("%d",&T);
while(T--) {
G.clear();mem(H);mem(cnt);
scanf("%d%d",&m,&n);
for(int i=;i*i<=m;i++) {
if(m%i==) G.pb(i);
while(m%i==) m/=i,cnt[i]++;
}
if(m!=) G.pb(m),cnt[m]++;
printf("Case %d:\n",cas++);
int ans = init();
if(ans == || ans == inf) printf("Impossible to divide\n");
else
printf("%d\n",ans);
}
return ;
}

代码

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. Amazon Kindle Device is hiring in Beijing Shanghai and Shenzhen!

    This is Angela from recruitment team of Amazon Kindle Device Software & Applications, we are exp ...

  2. Mac下如何显示隐藏文件/文件夹_百度经验

    在应用程序里打开终端, cd 你的文件夹名 ls -a 即可显示该文件夹下的所有隐藏文件   如果你想打开整个系统的隐藏文件可以在终端下输入以下命令: defaults write com.apple ...

  3. Object-c 语法 - 头文件引用(@class/#import/#include)

    一. Objective-C 中 #import 和 #include 的区别 预编译指令 Objective-C:#import:由gcc编译器支持 C,C++:#include 在 Objecti ...

  4. vi/vim编辑器

    vi / vim是Unix / Linux上最常用的文本编辑器而且功能非常强大.

  5. OC学习笔记之属性详解和易错点

    属性的概念在OC1.0中就存在,格式是定义实例变量,然后定义setter和getter方法,用点操作符操作属性 举例,类的接口部分 @interface Father : NSObject { NSI ...

  6. 认识Linux

    Linux的内核版本 1.如何查看Linux的内核版本 # uname -r -.el6.i686 2. 2.6.32-358的含义    主版本.次版本.释出版本-修改版本 3.主次版本编号规则  ...

  7. 将SQLServer结果导出为excel文件

    相信大家常常会遇到将SqlServer查询结果导出到Excel的问题.如果导出的次数少,直接“Save Results As...”就是了:但是当要分别在每个表取样,那就相当麻烦了.今天就为大家提供一 ...

  8. Ant学习---第三节:使用Ant实现一个最小的项目编译

    1.编译 .java 文件,生成 .jar 包,代码如下: <?xml version="1.0" encoding="UTF-8"?> <p ...

  9. jta.properties transactions.properties Log already in use 解决方法

    当在resin里跑多个含有atomikos控制事物的项目时,会报错,Log already in use. 解决方法: 加jta.properties或者transactions.properties ...

  10. 玩耍Hibernate系列(二)--基础知识

    Hibernate思维导图   Hibernate映射 关于hibernate的映射要说明的一点就是关于ID的访问权限,peroperty以及field的区别: 表的主键在内存中对应一个OID对象描述 ...