BZOJ 1053

Description

  对于任何正整数x,其约数的个数记作g(x)。例如g(1)=1、g(6)=4。如果某个正整数x满足:g(x)>g(i) 0<i<x
,则称x为反质数。例如,整数1,2,4,6等都是反质数。现在给定一个数N,你能求出不超过N的最大的反质数么

Input

  一个数N(1<=N<=2,000,000,000)。

Output

  不超过N的最大的反质数。

Sample Input

1000

Sample Output

840

题解

可以发现,如果某个数是反质数,那么它的质因子一定是前$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的更多相关文章

  1. BZOJ 3085: 反质数加强版SAPGAP (反素数搜索)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3085 题意:求n(<=10^100)之内最大的反素数. 思路: 优化2: i ...

  2. bzoj 1053 [HAOI2007]反素数ant——关于质数的dfs / 打表

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053 写了个打表程序. #include<iostream> #include& ...

  3. bzoj:3085: 反质数加强版SAPGAP

    Description 先解释一下SAPGAP=Super AntiPrime, Greatest AntiPrime(真不是网络流),于是你就应该知道本题是一个关于反质数(Antiprime)的问题 ...

  4. POJ 2886 Who Gets the Most Candies?(反素数+线段树)

    点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...

  5. BZOJ 1053 反素数ant

           初读这道题,一定有许多疑惑,其中最大的疑惑便是"反素数",反素数的概念很简单,就是,a<b同时a的因数个数大于b的因数个数.但是想要完成本题还需要一些信息,关于 ...

  6. 51nod 1060 最复杂的数 反素数

    1060 最复杂的数 基准时间限制:1 秒 空间限制:131072 KB 把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数. 例如:12的约数为:1 2 3 4 6 ...

  7. zoj 2562 反素数

    题目大意:求n范围内最大的反素数(反素数定义:f(x)表示x的因子数,f(x)>f(x1) (0<x1<x)) x用质因数形式为:x=a1^p1*a2^p2......an^pn(a ...

  8. 【BZOJ1053】[HAOI2007]反素数ant 暴力

    [BZOJ1053][HAOI2007]反素数ant Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) ...

  9. bzoj1053: [HAOI2007]反素数ant

    51nod有一道类似的题...我至今仍然不会写暴搜!!! #include<cstdio> #include<cstring> #include<iostream> ...

随机推荐

  1. Java文件字节流和字符流

    输入流:只能从中读取数据,不能向其写入数据. InputStream,Reader 输出流:只能向其中写入数据,不能从中读取数据. OutputStream, Writer 输入流是相对于程序而言,外 ...

  2. web站点启用https (一)

    HTTPS技术是现在主流网站都采用的安全加密传输数据的技术,本篇文档将分为2部分讲解PKI的基本原理及在web站点配置https访问. 一.理论知识 1.PKI(public key infrastr ...

  3. 设计简单登录界面(Java web)

    程序设计思想: 在Input.jsp中创建一个表格里边分别是课程名称,任课老师,教学地点,并分别用三个文本框来接受输入的三个属性, 并传到另外的Jsp页面中,又来接受三个数据,并判断传来的教师,与教室 ...

  4. Eclipse安装genymotion最新的方法

    https://www.cnblogs.com/WXBai/p/5938884.html 安卓开发: http://tools.android-studio.org/index.php/sdkhttp ...

  5. mongodb主从备份 和 手动主从切换

    环境: 主机A:172.16.160.91 主机B:172.16.160.92 配置主机A [root@master zhxf]# cat docker-compose.yml version: '3 ...

  6. 牛客Wannafly挑战赛26E 蚂蚁开会(树链剖分+线段树)

    传送门 题面描述 一颗n个节点的树,m次操作,有点权(该节点蚂蚁个数)和边权(相邻节点的距离). 三种操作: 操作1:1 i x将节点i的点权修改为x.(1 <= i <= n; 1 &l ...

  7. ASP.NET MVC 下拉列表实现

    https://blog.csdn.net/Ryan_laojiang/article/details/75349555?locationNum=10&fps=1 前言 我们今天开始好好讲讲关 ...

  8. (转)MySql数据库4【命令行赋权操作】

    MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 原文:http://www.cnblogs.com/zhuyibo/p/3980328.html 一.g ...

  9. Web App、Hybrid App、 Native App

    1.特点: 1. 偏交互的Native,偏浏览的Web:交互指复杂操作,输入/选择什么的2. 已稳定的Native,试错中的Web:H5页面用来做低成本验证很好3. 访问硬件Native,信息展示We ...

  10. Mac 提交代码到Github

    然后在GitHub上创建版本库(Repository),在GitHub首页上,点击“Create a New Repository”,如下所示(为了便于后面演示,创建README.md这步暂不勾选): ...