【BZOJ】1053: [HAOI2007]反素数ant
题解
从每个质因子小到大指数非严格递减,直接搜就行
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {out(x / 10);}
putchar('0' + x % 10);
}
int N,ans,res;
int prime[MAXN],tot;
bool isprime[MAXN];
void dfs(int pos,int val,int pre,int cnt) {
if(cnt <= ans && val > res) return;
if(cnt > ans) {
res = val;
ans = cnt;
}
else if(cnt == ans && val < res) res = val;
int t = 1;
for(int i = 1 ; i <= pre ; ++i) {
t = t * prime[pos];
if(val > N / t) break;
dfs(pos + 1,val * t,i,cnt * (i + 1));
}
}
void Solve() {
read(N);
for(int i = 2 ; i <= 100000 ; ++i) {
if(!isprime[i]) {
prime[++tot] = i;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > 100000 / i) break;
isprime[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
dfs(1,1,32,1);
out(res);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}