标签:
Problem Description
Now we define a function F(x), which means the factors of x. In particular, F(1) = 1,since 1 only has 1 factor 1, F(9) = 3, since 9 has 3 factors 1, 3, 9. Now give you an integer k, please find out the minimum number x that makes F(x) = k.
Input
The first line contains an integer t means the number of test cases.
The follows t lines, each line contains an integer k. (0 < k <= 100).
Output
For each case, output the minimum number x in one line. If x is larger than 1000, just output -1.
Sample Input
4
4
2
5
92
Sample Output
6
2
16
-1
题目连接:HDU-3189
题目大意:求约数个数为k的最小数。
题目思路:先求出每个数字的质因数,然后dfs求出所有的约数。
以下是代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
//#include<bits\stdc++.h>
#define ll unsigned long long
using namespace std;
#define MAXN 1010
#define ANS_SIZE 505
ll f[ANS_SIZE],nf[ANS_SIZE]; //f存放质因数,nf存放对应质因数的个数
ll plist[MAXN], pcount=0;
bool isPrime[MAXN+1];
void initprime()//素数且不说,所有合数都能分解成任意素数之积
{
int i,j;
pcount = 0;
for(i = 2; i<MAXN; i++)
{
if(!isPrime[i]) plist[pcount++] = i;//打下素数表
for(int j = 0; j<pcount && i*plist[j]<MAXN; j++)
{
isPrime[i*plist[j]] = true;//所有非素数排除
if(i%plist[j]==0) break;
}
}
}
int prime_factor(ll n) {
int cnt = 0;
int n2 = (int)sqrt((double)n);
for(int i = 0; n > 1 && plist[i] <= n2 && i < pcount; ++i)
{
if (n % plist[i] == 0) {
for (nf[cnt] = 0; n % plist[i] == 0; ++nf[cnt], n /= plist[i]);
f[cnt++] = plist[i];
}
}
if (n > 1) nf[cnt] = 1, f[cnt++] = n;
return cnt; //返回不同质因数的个数
}
vector<ll> yue[1005];
void dfs( int x, int t, ll ss ,int numpos){
if( x==t ) return;
dfs( x+1, t, ss,numpos);
for( int i=0 ; i<nf[x] ; i++ ){
ss *= f[x];
yue[numpos].push_back(ss);
dfs( x+1, t, ss,numpos);
}
}
int ans[1005];
int main(){
ll m;
initprime();
for (int i = 1; i < 1005; i++)
{
memset(f,0,sizeof(f));
memset(nf,0,sizeof(nf));
int ret = prime_factor(i);
yue[i].push_back (1);
dfs(0,ret,1,i);
ans[i] = yue[i].size();
}
cin >> m;
while(m--)
{
int k;
cin >> k;
int flag = 0;
for (int i = 1; i < 1005; i++)
{
if (ans[i] == k)
{
flag = 1;
cout << i << endl;
break;
}
}
if (!flag) cout << -1 << endl;
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/loy_184548/article/details/51444398