转自:http://blog.csdn.net/f_zyj/article/details/52226481
把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数。
例如:12的约数为:1 2 3 4 6 12,共6个数,所以12的复杂程度是6。如果有多个数复杂度相等,输出最小的。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 100) 第2 - T + 1行:T个数,表示需要计算的n。(1 <= n <= 10^18)
Output
共T行,每行2个数用空格分开,第1个数是答案,第2个数是约数的数量。
Input示例
5 1 10 100 1000 10000
Output示例
1 1 6 4 60 12 840 32 7560 64#include <iostream> typedef long long ll; using namespace std; const int MAXP = 16; const int prime[MAXP] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; ll n, res, ans; void dfs(ll cur, ll num, int key, ll pre) { if (key >= MAXP) { return ; } else { if (num > ans) { res = cur; ans = num; } else if (num == ans) { res = min(cur, res); } ll i; for ( i = 1; i <= pre; i++) { if (cur <= n / prime[key]) // cur*prime[key]<=n { cur *= prime[key]; dfs(cur, num * (i + 1), key + 1, i); } else { break; } } } } void solve() { res = 1; ans = 1; dfs(1, 1, 0, 15); cout << res << ' ' << ans << endl; } int main(int argc, const char * argv[]) { int T; cin >> T; while (T--) { cin >> n; solve(); } return 0; }