zoukankan      html  css  js  c++  java
  • Divisors

    计算小于n的数中,约数个数最多的数,若有多个最输出最小的一个数。

    http://hihocoder.com/problemset/problem/1187

    对于100有 60 = 2 * 2 * 3 * 5,共 (2 + 1) * (1 + 1) * (1 + 1) = 12个约数。

    对于 n <= 10 ^ 16,int最大值为10位,所以这里要用long long。很显然:约数要尽量小,然后小的约数的指数一定大于大的约数的指数。

    所以对于 10^16,有质因子:2,3,5,7,11,13,17,19,23,29,31,37,41,43,47...后面的可以不考虑了。

    #include <cstdio>
    #include <cmath>
    
    int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
    long long n, result = 1 << 30;
    int maxDivisors = 1;
    
    
    void dfs(long long res, int divisors, int np, int ti) {
        if (np > 12 || res > n) return;
        if (divisors > maxDivisors || (divisors == maxDivisors && res < result)) {
            maxDivisors = divisors;
            result = res;
        }
        int t = p[np];
        int i = 1;
        long long val;
        while ((val = res * std::pow(t, i)) < n && i <= ti) {
            dfs(val, divisors * (i + 1), np + 1, i);
            ++i;
        }
    }
    
    int main() {
        scanf("%lld", &n);
        dfs(1, 1, 0, 20);
        printf("%lld
    ", result);
        return 0;
    }
  • 相关阅读:
    非线性数据结构——树
    排序算法之插入排序
    web框架之environment处理
    web开发之http和wsgi
    python os模块和shutil模块
    python路径操作
    stringIO和bytesIO
    python文件操作
    设计模式
    设计模式
  • 原文地址:https://www.cnblogs.com/fripside/p/5294190.html
Copyright © 2011-2022 走看看