给定两个整数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. AzCopy Upload Files

    We can use many ways upload our Files to Azure, Than I  Introduction to you a good way, AzCopy ! 1. ...

  2. Neural Style学习2——环境安装

    neural-style Installation This guide will walk you through the setup for neural-style on Ubuntu. Ste ...

  3. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  4. python面向对象初级(七)

    概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...

  5. 点评前端开发工具cortex安装使用方法

    cortex安装方法: 安装最新版 sudo npm install -g cortex cortex config set registry http://registry.cortexjs.org ...

  6. How to create vlan on Linux (with Cisco Catalyst Switch)

    In this article I want to share to you on how to create and configure vlan on Linux through Cisco Ca ...

  7. gis数据格式转换(数据导入)ConvertFeaCls

    本文主要对数据导入等里 常用的不同格式数据创建.转换等 进行代码示例.主要用到IFeatureDataConverter.ConvertFeatureClass方法. 代码如下,难度不大,只是个技巧问 ...

  8. 暴力枚举N级子域名

    #!/usr/bin/env python# -*- encoding: utf-8 -*-# A simple and fast sub domains brute tool for pentest ...

  9. API接口:分页

    // 查询满足要求的总记录数 $count = M("back")->where($back_map)->count(); $pagecount = ceil($cou ...

  10. [NHibernate]HQL查询

    目录 写在前面 文档与系列文章 查询的几种方式 HQL查询 一个例子 总结 写在前面 上篇文章介绍了nhibernate在项目中的基本配置,包括数据库连接字符串的设置,映射文件的配置及需注意的地方,这 ...