【ybtoj高效进阶6-2-4】【luogu P1463】反素数
题目大意:
定义反素数:对于正整数 x ,其约数个数为
g
(
x
)
g(x)
g(x) ,满足
g
(
1
)
,
g
(
2
)
⋅
⋅
⋅
⋅
⋅
g
(
x
−
1
)
g(1),g(2)·····g(x-1)
g(1),g(2)⋅⋅⋅⋅⋅g(x−1)都小于
g
(
x
)
g(x)
g(x)的 x 为反素数。
现在让你求1-n中最大的反素数。
思路:
首先我们先来证明一些东西。
(1):不同的质因子个数不会超过10,因为前10个质数的积为
2
∗
3
∗
5
∗
7
∗
11
∗
13
∗
17
∗
19
∗
23
∗
29
=
6
,
469
,
693
,
230
>
2
∗
1
0
9
2*3*5*7*11*13*17*19*23*29=6,469,693,230>2*10^9
2∗3∗5∗7∗11∗13∗17∗19∗23∗29=6,469,693,230>2∗109
(2):质因子的个数总和不会超过31个,因为
2
31
>
2
∗
1
0
9
2^{31}>2*10^9
231>2∗109
然后我们发现,反素数其实就是 1~n 中 约数个数最多的数中最小的那个 (如果不是最小就意味着有一个比它小的数约数个数和它一样,不符合小于的条件)
那么我们要满足最小的原则,肯定是贪心啦。
假设我们取的质数从小到大,那么小数的数量一定要大于大数的数量,这样我们就可以满足最小了。
因为数据不大,我们使用深搜dfs搞定
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define r register
#define rep(i,x,y) for(r ll i=x;i<=y;++i)
#define per(i,x,y) for(r ll i=x;i>=y;--i)
using namespace std;
typedef long long ll;
ll p[]={0,2,3,5,7,11,13,17,19,23,29,31},a[50];
ll top,n,maxn,maxt;
void dfs(ll now,ll val,ll tot)
//now 现在取到第几个质数,val 现在数的值,tot 现在的约数个数
{
if(val>n) return ;
if(now>11) return ;
if((tot>maxt)||(tot==maxt&&val<maxn))
{
maxn=val;
maxt=tot;
}
rep(i,0,a[now-1])
{
a[now]=i;//记录次数
dfs(now+1,val,tot*(i+1));
val*=p[now];
if(val>n) return ;
}
}
int main()
{
scanf("%lld",&n);
a[0]=32; //初始可以最多取32个
dfs(1,1,1);
cout<<maxn;
return 0;
}