zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    最小生成树(Prime算法)
    Spiral Matrix
    大于非负整数N的第一个回文数 Symmetric Number
    毁灭者问题
    查看Centos7虚拟机的IP
    创建Redis集群时遇到问题(二)
    EditPlus通过FTP远程连接Linux
    Redis集群搭建
    创建Redis集群时遇到问题(一)
    安装redis报错“系统 Ruby 版本过低”的解决办法
  • 原文地址:https://www.cnblogs.com/evenbao/p/9196358.html
Copyright © 2011-2022 走看看