UVA 10780 Again Prime? No Time. 分解质因子
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. 分解质因子的更多相关文章
- Alternate Task UVA - 11728 (暴力。。分解质因子)
题意: 输入一个正整数S,(S <= 1000)求一个最大的正整数N,使得N的所有正因子之和为S. 解析: ..求1000以内的所有数的正因子和 ...输出.. #include <io ...
- 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,求满 ...
- hdu6237 分解质因子
题意:给一堆石子,每次移动一颗到另一堆,要求最小次数使得,所有石子数gcd>1 题解:枚举所有质因子,然后找次数最小的那一个,统计次数时,我们可以事先记录下每堆石子余质因子 的和,对所有石子取余 ...
- NYOJ-476谁是英雄,分解质因子求约数个数!
谁是英雄 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 十个数学家(编号0-9)乘气球飞行在太平洋上空.当横越赤道时,他们决定庆祝一下这一壮举.于是他们开了一瓶香槟.不 ...
- 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 ...
- 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 ...
- HDU 4135 Co-prime (容斥+分解质因子)
<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...
- 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 ...
- Uva 10780 Again Prime? No Time.(分解质因子)
题意:给你两个数m和n,问 n! 可以被 m^k 整除的k的最大值 思路:从这道我们可以想到n!末尾有多少个0的问题,让我们先想一下它的思想,我们找 n! 末尾有多少0, 实际上我们是在找n!中5的个 ...
随机推荐
- Java动态替换InetAddress中DNS的做法简单分析1
在java.net包描述中, 简要说明了一些关键的接口. 其中负责networking identifiers的是Addresses. 这个类的具体实现类是InetAddress, 底层封装了Inet ...
- jquery-弹窗:layer
键: 值 描述 下表的属性都是默认值,您可在调用时按需重新配置,他们可帮助你实现各式各样的风格.如是调用: $.layer({键: 值, 键: 值, …}); type: 0 层的类型.0:信息框(默 ...
- Xcode真机调试错误之"Please valify your...clock not set"
乍一看错误信息是证书过期,其实是描述证书错乱了. Xcode->Preferences->Account 将选中其中一个描述文件 show in finder,将里面的全都删除.
- ASP.NET MVC中使用事务写法
一些项目中,会涉及到事务的写法,比如订单相关,订单成功,会涉及到产品的库存和账户金额的一些信息变动,当然,如果整个流程成功,那是没什么问题,关键是如果中间某一步骤出现bug了,那之前已执行的一些变动就 ...
- [转]STUN和TURN技术浅析
[转]STUN和TURN技术浅析 http://www.h3c.com.cn/MiniSite/Technology_Circle/Net_Reptile/The_Five/Home/Catalog/ ...
- 64.OV7725初始化配置
所有的结局都是好的,不好,是因为你还有坚持到最好. OV7725摄像头的初始化配置,需要SCCB总线即IIC接口配置.先发送配置数据到OV7725中,然后通过随机读取对应地址的数据来验证数据是否写进去 ...
- Qt:禁止qDebug的输出
Qt:禁止qDebug的输出 在工程的.pro文件里加上以下编译批令即可: DEFINES += QT_NO_DEBUG_OUTPUT
- Bootstrap入门一:Hello Bootstrap
一.Bootstrap简介 Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap是基于 HTML5.CSS3和Javascriopt开发的,它在 jQuery的基础上 ...
- Careercup - Google面试题 - 6407924087783424
2014-05-07 15:17 题目链接 原题: Given an array of n elements (a1,a2,..ai,...,an). You are allow to chose a ...
- Java应用程序实现屏幕的"拍照"
有时候,在Java应用程序开发中,如:远程监控或远程教学,常常需要对计算机的屏幕进行截取,由于屏幕截取是比较接近操作系统的操作,在Windows操作系统下,该操作几乎成了VC.VB等的专利,事实上,使 ...