【HAOI2007】反素数

【题目链接】

           点击打开链接

【算法】

          稍加分析可知,问题等价于“求1到n中,因子个数最多的数,若有多个,求最小的”

          那么我们该怎么求这个数呢?

          约数个数定理 : x = p1^a1p2^a2p3^a3...pn^an

          则x的约数个数为 : (a1 + 1)(a2 + 1)(a3 + 1) ... (an + 1)

          我们发现,一定有 : a1 >= a2 >= a3 >= ... an,也就是说,a是一个降序的排列

          同时,我们发现,如果我们希望让这个数的因子尽可能多,那么p1...pn要尽可能的小

          只需将前10个素数存在一张表内,然后dfs即可

【代码】

           

#include<bits/stdc++.h>
using namespace std;

long long i,n,num = 1e18,ans;
long long prime[12] = {0,2,3,5,7,11,13,17,19,23,29};

template <typename T> inline void read(T &x) {
        long long f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
} 
template <typename T> inline void write(T x) {
        if (x < 0) { putchar('-'); x = -x; }    
        if (x > 9) write(x/10);
        putchar(x%10+'0');
}
template <typename T> inline void writeln(T x) {
        write(x);
        puts("");    
}
inline void dfs(long long dep,long long last,long long sum,long long cnt) {
        long long i;
        if (sum > n) return;
        if ((cnt > ans) || ((cnt == ans) && (sum < num))) {
                ans = cnt;
                num = sum;
        } 
        if (!last) return;
        for (i = 0; i <= last; i++) {
                dfs(dep+1,i,sum,cnt*(i+1));
                sum *= prime[dep];
                if (sum > n) return;
        }
}

int main() {
        
        read(n);
        dfs(1,32,1,1);
        writeln(num);
        
        return 0;
}
原文地址:https://www.cnblogs.com/evenbao/p/9196358.html