BZOJ1053 [HAOI2007]反素数 & BZOJ3085 反质数加强版SAPGAP
BZOJ 1053
Description
Input
一个数N(1<=N<=2,000,000,000)。
Output
不超过N的最大的反质数。
Sample Input
Sample Output
题解
可以发现,如果某个数是反质数,那么它的质因子一定是前$k$个质数,且这些质数的指数$q_i$是不增的(否则我可以交换两个质数的质数的指数,在因数个数不变的前提下把答案变小),于是直接爆搜。
代码:
#include <cstdio>
typedef long long LL;
int n;
int ans;
int divnum;
const int prime[20] = {
2, 3, 5, 7, 11,
13, 17, 19, 23, 29,
31, 37, 41, 43, 47,
53, 59, 61, 67, 71
};
void dfs(int x, int y, int la, int d) {
if (d > divnum || d == divnum && ans > y) {
ans = y;
divnum = d;
}
if (x >= 20) return;
for (int i = 0; i <= la && y <= n; ++i, y *= prime[x])
dfs(x + 1, y, i, d * (i + 1));
}
int main() {
scanf("%d", &n);
divnum = 0;
dfs(0, 1, 10000, 1);
printf("%d\n", ans);
return 0;
}
程序中取前20个质数是因为它们乘起来已经超过2000000000了。
BZOJ 3085
同上题,但$N\leq10^{100}$。
我们考虑,若m个质因子为$p_0, \ldots, p_{m-1}$, 指数为$q_0, \ldots, q_{m-1}$,那么对于每个质数$p_i$,我们找出最小的$k_i$使$2^{k_i} > p_i$,就有
$2^{k_i - 1} < p_i$,从而$2^{p_0 + k_i - 1}p_i^{q_i - 1} < 2^{p_0}p_i^{q_i}$。
那么既然它是反素数,我们把$2^{p_0}p_i^{q_i}$换成$2^{p_0 + k_i - 1}p_i^{q_i - 1}$质因数个数一定要减少,即$(p_0 + k_i)q_i < (p_0 + 1)(q_i + 1)$,解得
$$p_i < \frac{p_0 + 1}{k_i - 1}$$
这样就对于所有$p_i (i > 0)$给出了约束。
我们考虑一定不会出现在答案里的$p_m$,对于每个$p_i$,我们找到最小的$t_i$使$p_i^{t_i} > p_m$,那么$p_i^{q_i} > p_i^{q_i - t_i}p_m$,就一定有
$q_i+1 > 2(q_i - t_i + 1)$,即$q_i < 2t_i - 1$。
$q_m$取第一个使$q_0q_1q_2q_3\ldots q_m > n$的质数即可。
然后高精,爆搜。
代码:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#define reg register
typedef long long LL; struct Int{
static const int w = 10000;
int a[30];
Int() {}
Int& operator=(const Int &x) {
memcpy(a, x.a, sizeof a);
return *this;
}
Int(int x) {
memset(a, 0, sizeof a);
a[0] = x;
}
Int& operator*=(int x) {
for (reg int i = 0, t = 0; i < 30; ++i) {
t = (a[i] = a[i] * x + t) / w;
a[i] %= w;
}
return *this;
}
Int& operator+=(int x) {
for (reg int i = 0; i < 30 && x; ++i) {
x = (a[i] += x) / w;
a[i] %= w;
}
return *this;
}
Int operator*(int x) {
Int tmp(*this);
tmp *= x;
return tmp;
}
bool operator<(const Int &y)const{
for (reg int i = 29; ~i; --i)
if (a[i] != y.a[i]) return a[i] < y.a[i];
return 0;
}
bool operator==(const Int &y)const{
for (reg int i = 29; ~i; --i)
if (a[i] != y.a[i]) return 0;
return 1;
}
bool operator<=(const Int &y)const{
return !(y < *this);
}
};
Int n;
Int ans;
LL divnum;
const int prime[] = {
2, 3, 5, 7, 11,
13, 17, 19, 23, 29,
31, 37, 41, 43, 47,
53, 59, 61, 67, 71,
73, 79, 83, 89, 97,
101, 103, 107, 109, 113,
127, 131, 137, 139, 149,
151, 157, 163, 167, 173,
179, 181, 191, 193, 197,
199, 211, 223, 227, 229,
233, 239, 241, 251, 257
};
const int K[] = {
2, 2, 3, 3, 4,
4, 5, 5, 5, 5,
5, 6, 6, 6, 6,
6, 6, 6, 7, 7,
7, 7, 7, 7, 7,
7, 7, 7, 7, 7,
7, 8, 8, 8, 8,
8, 8, 8, 8, 8,
8, 8, 8, 8, 8,
8, 8, 8, 8, 8,
8, 8, 8, 8, 8,
};
int t[55];
int q[55];
int m;
void dfs(int x, Int y, LL d) {
if (d > divnum || (d == divnum && y < ans)) {
ans = y;
divnum = d;
}
if (x >= 55) return;
int Max = 2 * (t[x] - 1);
if (x) Max = std::min(Max, std::min(q[x - 1], (q[0] + 1) / (K[x] - 1)));
LL t;
y *= prime[x];
for (q[x] = 1; q[x] <= Max && y <= n; ++q[x], y *= prime[x])
dfs(x + 1, y, d * (q[x] + 1));
}
void readInt(Int &x) {
x = 0;
char c;
while (!isdigit(c = getchar()));
do (x *= 10) += c - '0';
while (isdigit(c = getchar()));
}
void putInt(const Int &x) {
int t = 30;
while (!x.a[--t]);
printf("%d", x.a[t]);
while (t--) printf("%04d", x.a[t]);
}
int main() {
readInt(n);
divnum = 0;
Int c(1);
for (m = 0; c <= n; ++m)
c *= prime[m];
for (int i = 0; i < m; ++i) {
t[i] = 0;
for (int j = 1; j <= prime[m]; j *= prime[i], ++t[i]);
}
dfs(0, 1, 1);
putInt(ans);
return 0;
}
BZOJ1053 [HAOI2007]反素数 & BZOJ3085 反质数加强版SAPGAP的更多相关文章
- BZOJ 3085: 反质数加强版SAPGAP (反素数搜索)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3085 题意:求n(<=10^100)之内最大的反素数. 思路: 优化2: i ...
- bzoj 1053 [HAOI2007]反素数ant——关于质数的dfs / 打表
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053 写了个打表程序. #include<iostream> #include& ...
- bzoj:3085: 反质数加强版SAPGAP
Description 先解释一下SAPGAP=Super AntiPrime, Greatest AntiPrime(真不是网络流),于是你就应该知道本题是一个关于反质数(Antiprime)的问题 ...
- POJ 2886 Who Gets the Most Candies?(反素数+线段树)
点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...
- BZOJ 1053 反素数ant
初读这道题,一定有许多疑惑,其中最大的疑惑便是"反素数",反素数的概念很简单,就是,a<b同时a的因数个数大于b的因数个数.但是想要完成本题还需要一些信息,关于 ...
- 51nod 1060 最复杂的数 反素数
1060 最复杂的数 基准时间限制:1 秒 空间限制:131072 KB 把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数. 例如:12的约数为:1 2 3 4 6 ...
- zoj 2562 反素数
题目大意:求n范围内最大的反素数(反素数定义:f(x)表示x的因子数,f(x)>f(x1) (0<x1<x)) x用质因数形式为:x=a1^p1*a2^p2......an^pn(a ...
- 【BZOJ1053】[HAOI2007]反素数ant 暴力
[BZOJ1053][HAOI2007]反素数ant Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) ...
- bzoj1053: [HAOI2007]反素数ant
51nod有一道类似的题...我至今仍然不会写暴搜!!! #include<cstdio> #include<cstring> #include<iostream> ...
随机推荐
- leetcode-884-两句话中的不常见单词
题目描述: 给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回 ...
- 丢用lamp手动安装apache php mysql
Centos7环境下. 使用lamp环境无法正常显示出thinkphp站点的内容,一气之下,选择手动安装 第一步: 安装apache php 和php连接数据库的工具php-mysql [root@ ...
- User类 新增共有属性Current ID
一.题目描述 每个用户有用户编号(id),用户名(name),密码(passwd)三个属性.其中: 用户编号(id)由系统自动顺序编号,用户名和密码都是字母.数字.符合的组合,新用户密码,默认“111 ...
- Hibernate 工具类
1.HibernateConfigUtil.java(HIbernate配置工具类) import org.hibernate.Session; import org.hibernate.Sessio ...
- Service Discovery protocol(SDP)
locating services provided by Volume 3 , Part C , section 8 2.1sdp client-server architecture 2.2 se ...
- CountDownLatch的简单实现
1. @Data public abstract class BaseLatch { private int limit; protected int running; BaseLatch(int l ...
- Mac下使用Wine安装Xshell 4和Xftp 4
下载: (链接: https://pan.baidu.com/s/1o78qisM 密码: 79sq) 安装: 1.安装Wine 参考:http://www.cnblogs.com/EasonJim/ ...
- 菜单根据菜单ID向下递归
第一步:我们根据这个类定义数据库,并插入菜单数据 DROP TABLE IF EXISTS `jrbac_menu`; CREATE TABLE `jrbac_menu` ( `id` ) NOT N ...
- 关于clear与清除浮动
今天看bootstrap突然看到了 .container:after { clear: both; } 好像对clear的用法有点模糊,于是于是又研究一下用法. 上面搜资料总会搜到张鑫旭老师的相关文章 ...
- WPF的ItemsControl设置数据源以及Binding使用
Student类: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...