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. Java多线程与并发——线程同步

    1.多线程共享数据 在多线程的操作中,多个线程有可能同时处理同一个资源,这就是多线程中的共享数据. 2.线程同步 解决数据共享问题,必须使用同步,所谓同步就是指多个线程在同一时间段内只能有一个线程执行 ...

  2. vue resource patch方法的传递数据 form data 为 [object Object]

    今天在测试 iblog 登录时,传送过去的数据总是 [object Object],以至于后台识别不出来. vue 使用了 vueResource 组件,登录方法为 patch. 经过探索,终于在官网 ...

  3. html 02-认识html

    1. HTML 初识 HTML 指的是超文本标记语言 (Hyper Text Markup Language)是用来描述网页的一种语言. HTML 不是一种编程语言,而是一种标记语言 (markup ...

  4. 云为 | 提供海外 IT 人才派遣、猎头、人力资源外包服务

    云为是大连信为软件开发有限公司为人力资源外包服务创建的品牌,是中国专业的人力资源外包领域的服务商,在信息技术行业为海外企业雇主招聘合格.专业且技能熟练的精英人士.我们的客户涵盖了日本上市公司和​​株式 ...

  5. Eucalyptus简介

    1.Eucalyptus是什么? Eucalyptus  n.桉树 Eucalyptus is a Linux-based software architecture that implements ...

  6. C#基础知识图谱

  7. /opt/metasploit/msf3

    启动msf终端 cd  /opt/metasploit/msf3 msfconsole

  8. MongoDB-2.6.0 (OpenLogic CentOS7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: mongodb basic software database linux open source 服务优惠价: 按服务商许可协议 云服务器费用:查 ...

  9. c++ vector & 二维数组 & MessageBox

    vector: https://www.cnblogs.com/mr-wid/archive/2013/01/22/2871105.html c++ 二维数组: int **p; p = new in ...

  10. 【BZOJ1925】 [SDOI2010] 地精部落(带有一堆性质的动态规划)

    点此看题面 大致题意: 问你有多少长度为\(n\)的数列,它当中每个数字要么比旁边两个数字都小,要么比旁边两个数字都大. 性质 这题应该比较显然是一道动态规划题,但刚看到这题时我却无从下手. 其实,了 ...