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. pat1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  2. quartz任务调度初次使用记录

    近期公司开发的数据交换系统嵌入了quartz任务调度功能,大概了解了任务调度的整个流程,项目中需要用到它来进行定时任务操作,对数据定时检查以及及时交换. Quartz是OpenSymphony开源组织 ...

  3. (转)IE6 死后即将大快人心的10件事

    (转)未来五年程序员应当具备的十项技能 W3C CSS 2.1 Specification(Quick Table of Contents) (转)IE6 死后即将大快人心的10件事 2009-04- ...

  4. java-类的定义和用法

    1.类的定义 public class Human{ }//每个源文件必须也只能有一个public类 class boy{ }//可以定义多个class类 class girl{ } 上面的类定义好后 ...

  5. hibernate课程 初探单表映射2-5 session详解(上)

    1 本章目的:获得session的两种方式: openSession 和 getCurrentSession 2 两种session的使用方法 1openSession可以直接写,getCurrent ...

  6. echarts折柱混合(图表数据与x轴对应显示)

    一天24个小时,每个小时不一定都有对应的数据,所以后台给出的数据,只有每个时间点对应的数据,比如4点,给的是112,5点的242,其他时间没有,则只显示4点,5点时候的数据,那么现在对应的时间点就是后 ...

  7. Markdown引用图片,且不使用网上链接的解决方法

    首先介绍下markdown使用图片的3种方法 使用本地图片,缺点是要用到本地的绝对路径,不适合对文档做迁移,否则会有图片链接失效的情况 ![thisisimage](C:\\Users\\Goose\ ...

  8. meterpreter > run post/windows/capture/keylog_recorder

    meterpreter > migrate 1548[*] Migrating to 1548...[*] Migration completed successfully.meterprete ...

  9. hadoop启动中缺少datanode

    原文链接地址:https://blog.csdn.net/islotus/article/details/78357857 本人测试有效: 首先删除hadoop下的dfs文件(注:本文件不一定在had ...

  10. 项目移动后报error LNK1123

    VS20101.解决方案窗口 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 “是”改为“否”:2.项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”改为“否”:3.对于64位的操 ...