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])的余数的最大值 分析: 要求余数的最 ...
随机推荐
- tcpproxy:基于 Swoole 实现的 TCP 数据包转发工具的方法
假设我们希望有一台机器A(ip 192.168.1.101)要开放端口6379给用户访问,但可能实际情况是用户无法直接访问到A(ip 192.168.1.101), 但却有一台机器B(ip 192.1 ...
- 给我发邮件(qq)| 和我联系
qq邮箱开放平台(只能是qq对qq): 简单点的发邮件: 和我联系
- python:配置文件configparser
#-*- coding:utf8 -*- # Auth:fulimei import configparser #第一个标签 conf=configparser.ConfigParser() conf ...
- Android Webview实现文件下载功能
在做美图欣赏Android应用的时候,其中有涉及到Android应用下载的功能,这个应用本身其实也比较简单,就是通过WebView控制调用相应的WEB页面进行展示.刚开始以为和普通的文件下载实 ...
- Python模块学习
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...
- OracleHelper 动软生成
using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...
- Sublime Text shift+ctrl妙用、Sublime Text快捷组合键大全
Package Control 安装方法 首先通过快捷键 ctrl+` 或者 View > Show Console 打开控制台,然后粘贴相应的 Python 安装代码. 1 :按住shift+ ...
- stuts-security.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- QT笔记之实现阴影窗口
方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...
- Python3基础 random 产生置顶区间的随机整数
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...