A divisor tree is a rooted tree that meets the following conditions:
- Each vertex of the tree contains a positive integer number.
- The numbers written in the leaves of the tree are prime numbers.
- For any inner vertex, the number within it is equal to the product of the numbers written in its children.
Manao has n distinct integers a1, a2, ..., an. He tries to build a divisor tree which contains each of these numbers. That is, for each ai, there should be at least one vertex in the tree which contains ai. Manao loves compact style, but his trees are too large. Help Manao determine the minimum possible number of vertices in the divisor tree sought.
The first line contains a single integer n (1 ≤ n ≤ 8). The second line contains n distinct space-separated integers ai(2 ≤ ai ≤ 1012).
Print a single integer — the minimum number of vertices in the divisor tree that contains each of the numbers ai.
2 6 10
7
4 6 72 8 4
12
1 7
1
Sample 1. The smallest divisor tree looks this way:
Sample 2. In this case you can build the following divisor tree:
Sample 3. Note that the tree can consist of a single vertex.
思路
全排列 会有一个顺序满足最小节点要求
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<bitset>
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0x3fffffff
#define zero 1e-8
const int li[] = { -1, 0, 1, 0};
const int lj[] = {0, -1, 0, 1};
using namespace std;
const int N = 1000;
long long edge[10][N], edgenum[10], num[10], nn[10],fj[10][N];
long long arr[10];
int cnt;
void makeprime(long long n, int id)
{
num[id] = 0;
long long mod=n;
double q=n;
for (int i=2;i<=(long long)(sqrt(q))&&n!=1;++i){
while (n%i==0){
num[id]++;
n/=i;
}
}
if (n!=1&&n<mod) num[id]+=1;
}
int n;
bool vis[10];
void INIT()
{
memset(vis, 0, sizeof(vis));
memset(edgenum, 0, sizeof(edgenum));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
if (i != j) {
if (arr[j] % arr[i] == 0)
edge[i][edgenum[i]++] = j, vis[j] = true;
}
}
}
int ans;
bool flag[10];
int minn;
int every[10][10];
void dfs(long long t, long long cnt, long long tot, int k)
{
if (t == cnt) {
if (k > 1) tot++;
minn = minn < tot ? minn : tot;
return;
}
if (minn <= tot) return ;
int id;
for (int tt = 0; tt < n; ++tt) {
if (!flag[tt]) {
id = tt;
flag[tt] = true;
int tttt = 1;
for (int i = 0; i < edgenum[id]; ++i) {
if (flag[edge[id][i]]) continue;
long long it = 1;
for (int j = 0; j < n; ++j)
if (every[edge[id][i]][j]) {
it *= arr[j];
}
if (it > 10000000 && arr[id] > 10000000) continue;
it *= arr[id];
if (arr[edge[id][i]] % it) {
continue;
}
tttt = 0;
every[edge[id][i]][id] = true;
nn[edge[id][i]] -= (num[id]);
dfs(t + 1, cnt, tot + nn[id], k);
every[edge[id][i]][id] = false;
nn[edge[id][i]] += (num[id]);
}
if (tttt) {
int nnn = 0;
if (!num[id]) nnn++;
dfs(t + 1, cnt, tot + nn[id] + nnn, k + 1);
}
flag[tt] = false;
}
}
}
void DO()
{
memset(flag, 0, sizeof(flag));
memset(every, 0, sizeof(every));
minn = inf;
for (int i=0;i<10;++i) nn[i]=num[i];
dfs(0, n, 0, 0);
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
while(~scanf("%d", &n)) {
for (int i = 0; i < n; ++i) {
scanf("%lld", &arr[i]);
}
for (int i = 0; i < n; ++i){
makeprime(arr[i], i);
}
INIT();
DO();
for (int i=0;i<n;++i)
if (num[i]) minn++;
printf("%d\n", minn);
}
return 0;
}