5791. 【NOIP2008模拟】阶乘 
(File IO): input:factorial.in output:factorial.out

Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits  

Description

有n个正整数a[i],设它们乘积为p,你可以给p乘上一个正整数q,使p*q刚好为正整数m的阶乘,求m的最小值。
 

Input

共两行。
第一行一个正整数n。
第二行n个正整数a[i]。

Output

共一行
一个正整数m。
 

Sample Input

1
6

Sample Output

3

样例解释:
当p=6,q=1时,p*q=3!
 
 

Data Constraint

对于10%的数据,n<=10
对于30%的数据,n<=1000
对于100%的数据,n<=100000,a[i]<=100000
 
 
做法:

题目要求一个最小的m使m!包含p这个因子。

可以把p分解质因数,假设p=∏ai^bi(ai为质数),那么只要m!包含了每个ai^bi,m!就包含p。

所以对于每个ai^bi,分别求出满足条件的最小的m,取最大值即可。

怎么求m?

先看一个简单的问题:

27!里面有多少个3相乘?

27!=1*2*...*27

包含1个3的数有27/(3^1)=9个

包含2个3的数有27/(3^2)=3个

包含3个3的数有27/(3^3)=1个

总共:9+3+1=13个

所以27!里面有13个3相乘。

用这个方法就可以求得m!有多少个ai相乘,二分判断即可。

代码如下:

 #include <cstdio>
#include <iostream>
#include <cstring>
#define LL long long
#define N 600007
using namespace std;
LL n, zs[N], T, a[N];
LL tot = , ans;
bool b[N]; void Pre_work()
{
for (int i = ; i <= N / ; i++)
{
if (!b[i])
{
zs[++zs[]] = i;
for (int j = ; j <= zs[]; j++)
if (i * zs[j] > N / ) break;
else b[zs[j] * i] = ;
}
else
{
for (int j = ; j <= zs[]; j++)
if (i * zs[j] > N / ) break;
else b[zs[j] * i] = ;
}
}
} LL max(LL a, LL b)
{
return a > b ? a : b;
} void Cl(LL x)
{
for (int i = , p = x; p > ; i++)
for (; p % zs[i] == ; p /= zs[i])
{
if (!b[p])
{
a[p]++;
T = max(T, p);
p = ;
break;
}
a[zs[i]]++, T = max(T, zs[i]);
}
} bool Check(LL ain)
{
for (int i = ; i <= T; i++)
{
int j = zs[i];
LL Jl = ;
for (LL k = j; (k <= ain) && (Jl < a[zs[i]]); k *= j) Jl += ain / k;
if (Jl < a[zs[i]]) return ;
}
return ;
} void Find()
{
LL l = , r = ;
while (l < r)
{
LL mid = (l + r) / ;
if (Check(mid)) r = mid;
else l = mid + ;
}
printf("%lld", l);
} int main()
{
freopen("factorial.in", "r", stdin);
freopen("factorial.out", "w", stdout);
scanf("%lld", &n);
LL x;
Pre_work();
for (int i = ; i <= n; i++)
{
scanf("%lld", &x);
if (!b[x])
{
a[x]++, T = max(T, x);
continue;
}
Cl(x);
}
Find();
}

JZOJ 5791. 【NOIP2008模拟】阶乘的更多相关文章

  1. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  2. JZOJ 5809. 【NOIP2008模拟】数羊

    5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms  Memory Limits: ...

  3. JZOJ 5793. 【NOIP2008模拟】小S练跑步

    5793. [NOIP2008模拟]小S练跑步 (File IO): input:run.in output:run.out Time Limits: 2000 ms  Memory Limits:  ...

  4. JZOJ 5776. 【NOIP2008模拟】小x游世界树

    5776. [NOIP2008模拟]小x游世界树 (File IO): input:yggdrasil.in output:yggdrasil.out Time Limits: 1500 ms  Me ...

  5. JZOJ 5775. 【NOIP2008模拟】农夫约的假期

    5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms  Memory Lim ...

  6. JZOJ 5773. 【NOIP2008模拟】简单数学题

    5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms  Memory Limits ...

  7. JZOJ 5771. 【NOIP2008模拟】遨游

    5771. [NOIP2008模拟]遨游 (File IO): input:trip.in output:trip.out Time Limits: 2000 ms  Memory Limits: 2 ...

  8. JZOJ5776. 【NOIP2008模拟】小x游世界树

    题目:[NOIP2008模拟]小x游世界树: 题目的附加题解给的很清楚,这里只给一个代码: #include<iostream> #include<cstdio> #inclu ...

  9. JZOJ【NOIP2013模拟联考14】隐藏指令

    JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...

随机推荐

  1. Hive 基本语法操练(一):表操作

    Hive 和 Mysql 的表操作语句类似,如果熟悉 Mysql,学习Hive 的表操作就非常容易了,下面对 Hive 的表操作进行深入讲解. **(1)先来创建一个表名为student的内部表** ...

  2. yii2 AR模型使用exists添加子查询与父查询关联

    有A,B两个表对应A_AR,B_AR两个模型B表interval_id对应A表id现在要查a表的数据,且没有code为a的子数据要求使用yii2的AR模型写查询: A_AR::find()->w ...

  3. java中key值可以重复的map:IdentityHashMap

    在Java中,有一种key值可以重复的map,就是IdentityHashMap.在IdentityHashMap中,判断两个键值k1和 k2相等的条件是 k1 == k2 .在正常的Map 实现(如 ...

  4. PHPGGC学习----理论

    本文首发于先知:https://xz.aliyun.com/t/5450 PHPGGC 是一款能够自动生成主流框架的序列化测试payload的工具,类似 Java 中的 ysoserial, 当前支持 ...

  5. arcgis 定位图斑,并且高亮显示

    ///图斑定位 function TabQuery(instance_id, layer_name) { require(["esri/map", "esri/geome ...

  6. MvcPager.dll使用实现无刷新分页以及MvcPager的Nuget程序包实现刷新分页

    无刷新分页: 1.引入JQuery的NuGet程序包 2.引入程序包 3.引入MvcPager.dll  ,MvcPager.dll文件下载链接http://pan.baidu.com/s/1hsvB ...

  7. 封装一个帮助类来写文件到android外置存储器上

    项目地址:点击打开 项目简介:写文件到android外置存储器的一个帮助类,和它的demo程序 它是如何工作的呢? 1.创建 AppExternalFileWriter 对象并传递context(上下 ...

  8. WIn10 电脑运行Docker

    参考地址: https://www.cnblogs.com/linjj/p/5606687.html https://docs.docker.com/engine/reference/commandl ...

  9. CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1

    CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1 下载软件 1.下载nginx http://nginx.org ...

  10. Centos6.5安装部署nodejs

    使用编译好的包安装 一.在官网下载包 https://nodejs.org/en/download/ 二.把包传送到服务器,进入到包目录并解压 tar axvf node-v6.9.5-linux-x ...