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. JVM基础知识1--JAVA内存区域与内存溢出异常

    1,运行时数据区域 根据JAVA虚拟机规范的规定:JAVA虚拟机所管理的内存将会包括以下几个运行时数据区域 程序计数器(Program Counter Register)是一块较小的内存空间,它的作用 ...

  2. Storm概念学习系列之storm的设计思想

    不多说,直接上干货! storm的设计思想 在 Storm 中也有对流(Stream)的抽象,流是一个不间断的.无界的连续 Tuple(Storm在建模事件流时,把流中的事件抽象为 Tuple 即元组 ...

  3. 一次Zookeeper 扩展之殇

    一.背景 基于公司发展硬性需求,生产VM服务器要统一迁移到ZStack 虚拟化服务器.检查自己项目使用的服务器,其中zookeeper集群中招,所以需要进行迁移. 二.迁移计划 为了使迁移不对业务产生 ...

  4. android 开发-系统设置界面的实现

    具体与Preference的用法类似,这里就不做过多解释,直接贴示例代码,需要在res下新建xml文件夹,在xml文件夹下添加xml文件. xml:(注意:root节点是:PreferenceScre ...

  5. client的使用

    document.documentElement.clientHeight = 464 // 指窗口的可见高度的大小 document.body.clientHeight = 1577 // 指窗口的 ...

  6. Storm里面fieldsGrouping和Field的概念详解

    这个Field通常和fieldsGrouping分组机制一起使用,这个Field特别难理解,我自己也是在网上看了好多文章,感觉依旧讲的不是很清楚,是似而非,没有抓到重点.这个问题足足困扰了我3-4天时 ...

  7. <转载>为什么VR不可能成功?

    这是一个来自Quora的回答,我把要点总结翻译了下,供大家参考批判. How big and issue the nausea problem for Virtual Reality products ...

  8. 第1章 .Net应用程序体系结构

    1. CLR:公共语言运行库,是每种.Net编程语言都使用的运行库 Windows 8为Windows Store应用程序引入了一个新的编程接口:Windows运行库. C# 6 具有许多小而实用的语 ...

  9. LeetCode Excel Sheet Column Number 表列数

    题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA&qu ...

  10. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...