求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.的更多相关文章

  1. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  2. POJ1365 - Prime Land(质因数分解)

    题目大意 给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式 题解 预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解....其实主要就是字符 ...

  3. 2012_p1 质因数分解 (prime.cpp/c/pas)

    2012_p1 质因数分解 (prime.cpp/c/pas) 时间限制: 1 Sec  内存限制: 128 MB提交: 80  解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 ...

  4. 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 ...

  5. LightOJ 1356 Prime Independence(质因数分解+最大独立集+Hopcroft-Carp)

    http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1356 题意: 给出n个数,问最多能选几个数,使得该集合中的 ...

  6. 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 726  Solved: 309[Submit][Status ...

  7. 数论 UVA 10780

    数论题目.有关内容:整数质因数分解,N的阶乘质因数分解,整除的判断. 这道题的题意是给你两个数n.m,要求你求出n!所能整除的m^k的最大值的k是多少. 由于数据范围:1<m<5000,1 ...

  8. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  9. hdu1405 第六周J题(质因数分解)

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Desc ...

随机推荐

  1. Careercup - Google面试题 - 5205167846719488

    2014-05-03 23:35 题目链接 原题: For a given node in binary search tree find a next largest number in searc ...

  2. 【转,未试】android 9path教程与去黑边

      本帖最后由 ArcherFMY 于 2013-4-24 17:39 编辑 <ignore_js_op> draw9patch.zip (124.83 KB) 这是Draw9patch& ...

  3. android 中设置HttpURLConnection 超时并判断是否超时

    设置超时: URL url1 = new URL(url); HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); c ...

  4. Android 自定义Toast,不使用系统Toast

    效果图: 创建Toast类 package com.example.messageboxtest; import android.app.Activity; import android.conten ...

  5. ffmpeg 打开视频流太慢(下)

    前面的博文中已经交代过,ffmpeg打开视频慢主要是因为av_find_stream_info 耗时久.下面给出重写查找音视频stream info的一段代码,用来替代av_find_stream_i ...

  6. redis 资料链接

    redis 资料链接  http://blog.nosqlfan.com/html/3537.html

  7. linux cmake 安装mysql5.5.11,以及更高版本

    1.下载mysql5.5.12和cmake wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.12-linux2.6-i686.tar.gz ...

  8. Unity3D与iOS的交互设计<ViewController 的跳转>

    原地址:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/28797_12.html Unity3D与iOS的交互设计<View ...

  9. c# 4.0新特性一览

    原文:http://www.cnblogs.com/palo/archive/2009/03/01/1400949.html 终于静下心来仔细听了一遍Anders Hejlsberg(Visual S ...

  10. Chp12: Testing

    What the Interviewer is Looking for: Big Picture Understanding Knowing How the Pieces Fit Together O ...