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. Gym 100971D Laying Cables 二分 || 单调栈

    要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...

  2. Storm概念学习系列之什么是实时流计算?

    不多说,直接上干货! 什么是实时流计算?    1.实时流计算背景 2.实时计算应用场景 3.实时计算处理流程 4.实时计算框架 什么是实时流计算? 所谓实时流计算,就是近几年由于数据得到广泛应用之后 ...

  3. c#文件相关笔记

    1.将*.txt文件内容转换为一个字符串str FileStream fs = new FileStream("路径\\*.txt", FileMode.Open); Stream ...

  4. 分享一个WPF下日历控件(Calendar)的样式

    WPF日历控件的一个样式 WPF自带的日历控件样式可能会比较丑,要修改其样式看起来挺复杂的,实际上很简单,用Blend打开,修改三个模板,基本就能改变全部面貌,也很容易 先上图 样式如下: <S ...

  5. Educational Codeforces Round 51 (Rated for Div. 2)

    做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...

  6. weex 项目搭建

    第一步:安装依赖 npm install -g weex-toolkit weex -v //查看当前weex版本 weex update weex-devtool@latest //@后标注版本后, ...

  7. Android仿微信高效压缩图片(libjpeg)

    用过ios手机的同学应该很明显感觉到,ios拍照1M的图片要比安卓拍照排出来的5M的图片还要清晰.这是为什么呢? 这得了解android底层是如何对图片进行处理的. 当时谷歌开发Android的时候, ...

  8. Remote System Explorer Operation在eclipse后台一直跑 解决办法

    在用eclipse开发时,经常遇到卡死的情况,其中一种就是右下角出现:“Remote System Explorer Operation”,解决方案如下: 第一步:Eclipse -> Pref ...

  9. 常用工具使用(sublimeText)

    1.sublime Text  (插件的安装,删除,更新) 1.1 使用 ctrl+`快捷键(Esc下面的波浪线按钮) 或者 菜单项View > Show Console 来调出命令界面,下面代 ...

  10. java Vamei快速教程05 实施接口

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在封装与接口中,private关键字封装了对象的内部成员.经过封装,产品隐藏了内部 ...