codeforces 300E Empire Strikes Back 数论+二分查找
题意:给定N个数a1,a2,a3...aN,现在要求最小的n满足 n!/(a1!*a2!*...*aN!) 是一个正整数的最小的n。
分析:这题的想法很明确,就是分解a1!*a2!*...*aN!,把其分解成质因子相乘的形式,这个都很熟悉了,然后就是对每一个质因子二分搜索出一个数字下界,最后求其中最大的一个数,问题的关键就是如何分解这样一个表达式成一个质因子相乘的形式。使用一个cnt数组来表示每一个数的在乘积中出现的次数,然后从后往前假设一个数出现了k次,那么如果这个数是素数则不用更新,如果一个数是合数则将其分解成两部分,一个是该数最小的质因子,一个是除以这个质因子之后的值,接着一直做下去,就能够把所有的素因子全部统计起来,最后再对每一个素因子都二分搜索。
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <cmath>
#include <vector>
using namespace std; typedef long long LL;
const int N = ;
vector<int>vv;
LL cnt[N];
int p[N];
int Max;
LL sum;
int n; void pre() {
for (int i = ; i < N; ++i) {
if (!p[i]) {
p[i] = i;
vv.push_back(i);
}
for (int j = ; i*vv[j] < N; ++j) {
p[i*vv[j]] = vv[j];
if (i % vv[j] == ) break;
}
}
} LL cal(LL mid, LL base) {
LL ret = ;
while (mid) {
ret += (mid /= base);
}
return ret;
} void deal() {
for (int i = Max; i >= ; --i) {
if (p[i] != i) {
cnt[p[i]] += cnt[i];
cnt[i/p[i]] += cnt[i];
}
}
} LL get(LL base, LL x) {
LL l = , r = sum;
LL ret;
while (l <= r) {
LL mid = (l + r) >> ;
if (cal(mid, base) >= x) {
ret = mid;
r = mid - ;
} else {
l = mid + ;
}
}
return ret;
} int main() {
pre();
scanf("%d", &n);
int x;
for (int i = ; i < n; ++i) {
scanf("%d", &x);
sum += x;
Max = max(Max, x);
++cnt[x];
}
for (int i = Max-; i >= ; --i) {
cnt[i] += cnt[i+];
} // 模拟阶乘,1-n之间每个数都有一个
deal();
LL ret = ;
for (int i = ; i < vv.size(); ++i) {
ret = max(ret, get(vv[i], cnt[vv[i]]));
}
printf("%I64d\n", ret);
return ;
}
codeforces 300E Empire Strikes Back 数论+二分查找的更多相关文章
- Codeforces 955C - Sad powers(数论 + 二分)
链接: http://codeforces.com/problemset/problem/955/C 题意: Q次询问(1≤Q≤1e5),每次询问给出两个整数L, R(1≤L≤R≤1e18),求所有符 ...
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
- CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)
传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #377 (Div. 2)D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...
- Codeforces 484B Maximum Value(排序+二分)
题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...
随机推荐
- PHP的数据类型、常量、时间
[数据类型之间相互转换] ◆ 一种是强制转换: setType( 变量, 类型); // 类型 int, integer, float, double,real, bool, boolena, st ...
- JavaEE基础(二十一)/IO流
1.IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. 2 ...
- Codeforces Round #336 Marbles
E. Marbles time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard in ...
- Alignment
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14547 Accepted: 4718 Descriptio ...
- java的myeclipse生成webservice的service和client
前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 ...
- java提高篇---HashTable
在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相 ...
- DLL学习笔记一(DLL导入导出)
创建DLL: 先声明导出函数:使用__declspec(dllexport) #include"DLLSample.h" #ifndef _DLL_SAMPLE_H #define ...
- 2016年11月26日 星期六 --出埃及记 Exodus 20:17
2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...
- iOS之地理位置及定位系统 -- 入门笔记
这是因为xcode升级造成的定位权限设置问题.升级xcode6.xcode7以后打开以前xcode5工程,程序不能定位.工程升级到xcode6或xcode7编译时需要iOS8 要自己写授权,不然没权限 ...
- uTenux——LED驱动讲解
LED驱动讲解,对于一个嵌入式的工程师还是一个刚开是学习相关电子设计的朋友,对于LED的驱动问题应该不甚陌生.我所说的LED驱动并不是类似大功率LED照明的那个驱动,而是简单的控制器对LED的控制驱动 ...