UVa 10780 (质因数分解) Again Prime? No Time.
求mk整除n!,求k的最大值。
现将m分解质因数,比如对于素数p1分解出来的指数为k1,那么n!中能分解出多少个p1出来呢?
考虑10!中2的个数c:1~10中有10/2个数是2的倍数,c += 5;1~10中有10/4个数是4的倍数,所以c += 2,其中有10/8 = 1个数是8的倍数,所以c += 1;
这样10!中就能分解出8个2
对于每个素数p,求出ci / ki的最小值就是答案。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std; const int INF = ;
const int maxn = ;
bool vis[maxn + ];
int prime[], pcnt = ; void Init()
{
int m = sqrt(maxn + 0.5);
for(int i = ; i <= m; i++) if(!vis[i])
for(int j = i*i; j<= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; i++) if(!vis[i]) prime[pcnt++] = i;
} vector<int> p, e, a; int main()
{
Init();
//printf("%d\n", pcnt);
int T;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
p.clear(); e.clear(); a.clear();
int m, n;
scanf("%d%d", &m, &n);
for(int i = ; i < pcnt && m > ; i++)
{
if(m % prime[i] == )
{
int c = ;
while(m % prime[i] == )
{
m /= prime[i];
c++;
}
e.push_back(c);
p.push_back(prime[i]);
}
} int ans = INF;
for(int i = ; i < p.size(); i++)
{
int c = , base = p[i];
while(n >= base)
{
c += n / base;
base *= p[i];
}
ans = min(ans, c / e[i]);
}
printf("Case %d:\n", kase);
if(ans) printf("%d\n", ans);
else puts("Impossible to divide");
} return ;
}
代码君
UVa 10780 (质因数分解) Again Prime? No Time.的更多相关文章
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- POJ1365 - Prime Land(质因数分解)
题目大意 给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式 题解 预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解....其实主要就是字符 ...
- 2012_p1 质因数分解 (prime.cpp/c/pas)
2012_p1 质因数分解 (prime.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 80 解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- LightOJ 1356 Prime Independence(质因数分解+最大独立集+Hopcroft-Carp)
http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1356 题意: 给出n个数,问最多能选几个数,使得该集合中的 ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
- 数论 UVA 10780
数论题目.有关内容:整数质因数分解,N的阶乘质因数分解,整除的判断. 这道题的题意是给你两个数n.m,要求你求出n!所能整除的m^k的最大值的k是多少. 由于数据范围:1<m<5000,1 ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu1405 第六周J题(质因数分解)
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
随机推荐
- jquery 图片背景透明度(支持IE5/IE6/IE7)
设置背景图片,以突出透明度的效果及jquery png背景透明插件实例教程 <head> <title>toggle()</title> <style typ ...
- Introduction to Deep Neural Networks
Introduction to Deep Neural Networks Neural networks are a set of algorithms, modeled loosely after ...
- 【MongoDb--初入江湖】windows下安装MongoDb
一.windows下安装MongoDb http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
- xargs 加 gm批量转换图片
%x50% @ ../-\ 南澳西涌_50%/@ 看了很多说明上都在用-i,这是一个已经废弃了的参数
- mysql 。。。
MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格 ...
- recursion lead to out of memory
There are two storage areas involved: the stack and the heap. The stack is where the current state o ...
- php数组函数序列之array_unshift() 在数组开头插入一个或多个元素
array_unshift() 函数在数组开头插入一个或多个元素.被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样 array_unshift()定义和用法 array_uns ...
- 架构探险——从零开始写Java Web框架》第二章照作
沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...
- NET 使用HtmlAgilityPack抓取网页数据
刚刚学习了XPath路径表达式,主要是对XML文档中的节点进行搜索,通过XPath表达式可以对XML文档中的节点位置进行快速定位和访问,html也是也是一种类似于xml的标记语言,但是语法没有那么严谨 ...
- java基础面试题(转)
JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...