大家一起做训练 第一场 E Number With The Given Amount Of Divisors

题目来源:CodeForce #27 E

题目意思和题目标题一样,给一个n,求约数的个数恰好为n个的最小的数。保证答案在1018内。

Orz,这题训练的时候没写出来。

这道题目分析一下,1018的不大,比264要小,所以这题可以枚举。

一个数 A 可以分解成 p1k1 * p2k2 * …… * pnkn 其中p为素数。这样分解之后,A的因子个数

S = (k1+1) *( k2+1) * …… *( kn+1)

然后用dfs枚举 + 剪枝。

剪枝的话大于现有结果return。就这样就能AC了。

附AC代码(手残,勿喷):

   1: #include <iostream>
   2: #include <cstdio>
   3: #include <cmath>
   4: #include <cstdlib>
   5: #define LL __int64
   6: using namespace std;
   7:  
   8: const LL MAX = 1e18 + 9;
   9: const LL p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
  10: LL res;
  11:  
  12: void dfs(LL now, LL num, LL x, LL n)
  13: {
  14:     if (num > n) return;
  15:     if (num == n && res > now)
  16:     {
  17:         res = now;
  18:         return ;
  19:     }
  20:     for (int i = 1; i <= 64; i++)
  21:         if (now * p[x] > res)
  22:             break;
  23:         else
  24:             dfs(now *= p[x], num * (i+1), x+1, n);
  25: }
  26:  
  27: int main()
  28: {
  29:     int n;
  30:     while(~scanf("%d", &n))
  31:     {
  32:         res = MAX;
  33:         dfs(1, 1, 0, n);
  34:         printf("%I64d
", res); 
  35:     }
  36: }
原文地址:https://www.cnblogs.com/wuhenqs/p/3400281.html