给定两个整数m和n,求最大的k使得m^k是n!的约数

对m质因子分解,然后使用勒让德定理求得n!包含的质数p的阶数,min(b[i] / a[i])即为结果k, 若为0无解

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 5008, INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
int p[N], a[N], b[N]; int lp(int n, int p){
int ans = 0;
for(int i = p; i <= n; i*= p){
ans += n / i;
}
return ans;
}
int solve(int m, int n){
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int tot = 0;
for(int i = 2; i * i <= m; i++){
if(m % i == 0){
p[tot] = i;
while(m % i == 0){
a[tot]++;
m /= i;
}
tot++;
}
}
if(m > 1){
p[tot] = m;
a[tot++] = 1;
}
int ans = INF;
for(int i = 0; i < tot; i++){
b[i] = lp(n, p[i]);
if(b[i] < a[i]){
return -1;
}
ans = min(ans, b[i] / a[i]);
}
return ans; }
int main(){
int t, m, n;
cin >>t;
for(int cas = 1; cas <= t; cas++){
scanf("%d %d", &m, &n);
int ans = solve(m, n);
printf("Case %d:\n", cas);
if(ans == -1){
printf("Impossible to divide\n");
}else{
printf("%d\n", ans);
} }
return 0;
}

  

UVA 10780 Again Prime No Time.(数学)的更多相关文章

  1. 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,no ...

  2. UVA 10780 - Again Prime? No Time.

    题目链接 思路好想,注意细节.错了很多次. #include <cstdio> #include <cstring> #include <string> #incl ...

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

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

  4. uva 1415 - Gauss Prime(高斯素数)

    题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=−2‾‾‾√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != ...

  5. UVA 10539 - Almost Prime Numbers(数论)

    UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...

  6. UVA 1415 - Gauss Prime(数论,高斯素数拓展)

    UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或 ...

  7. UVa 10780 (质因数分解) Again Prime? No Time.

    求mk整除n!,求k的最大值. 现将m分解质因数,比如对于素数p1分解出来的指数为k1,那么n!中能分解出多少个p1出来呢? 考虑10!中2的个数c:1~10中有10/2个数是2的倍数,c += 5: ...

  8. UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)

    两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> ...

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

    m^k就是让m的每个质因子个数都增加了k倍 求m的质因子 在n!中增加了多少倍就好了,因为m^k 表示每一个质因子增加相同的倍数k  所以我们需要找到增加倍数最小的那个..短板效应  其它质因子多增加 ...

随机推荐

  1. Lua和C++交互详细总结

    转自:http://cn.cocos2d-x.org/tutorial/show?id=1474 一.Lua堆栈 要理解Lua和C++交互,首先要理解Lua堆栈. 简单来说,Lua和C/C++语言通信 ...

  2. c++调用lua环境配置

    安装完luaforwindows后进行配置 配置项目属性            

  3. 【BZOJ-3643】Phi的反函数 数论 + 搜索

    3643: Phi的反函数 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 141  Solved: 96[Submit][Status][Discuss ...

  4. 做一个阅读管理APP

    背景 由于最近在看的书有点多,所以一直想找一个能够管理阅读进度的书(鄙人记性不是很好,两天不看就忘了)可惜Android平台上一直找不到合适的APP: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...

  5. BUAA_OVERWATCH第一次行动前战略部署

    这太IMBA了! 需求调研问卷的反馈 #define A 调查问卷 A设计背景 随着各种新兴手游的兴起,以及各大直播间内Lying Man的火热,以及各种娱乐方式的发展,传统桌游很好地移植到app上的 ...

  6. APP开发+发布流程

    ios开发(证书+应用发布) 证书获取:http://newdocx.appcan.cn/newdocx/docx?type=1297_1291ios应用发布:http://newdocx.appca ...

  7. java中的equals()方法重写

    如何java中默认的equals方法跟实际不符的话,需要重写equals方法.例如: public class TestEquals { public static void main(String[ ...

  8. “基础提供程序在Open上失败”

    本来布置在IP为[x.x.x.x]的WCF服务好好的,但是今天突然就有问题了,一调用报错"基础提供程序在Open上失败"... 服务器上的有问题,先试试本地的服务能不能用吧,连的都 ...

  9. JAX-WS服务端及客户端

    一.概述 Java API for XML Web Services (JAX-WS)是Java程序设计语言一个用来创建Web服务的API. 在服务器端,用户只需要通过Java语言定义远程调用所需要实 ...

  10. R语言-用R眼看琅琊榜小说的正确姿势

    博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html 目录: 零:写在前面的一些废话 一.R眼看琅琊榜的基本原理 1.导入数据 2.筛选数据 3.多条 ...