codeforces 337E Divisor Tree (dfs)

E. Divisor Tree
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains a single integer n (1 ≤ n ≤ 8). The second line contains n distinct space-separated integers ai(2 ≤ ai ≤ 1012).

Output

Print a single integer — the minimum number of vertices in the divisor tree that contains each of the numbers ai.

Examples
input
2
6 10
output
7
input
4
6 72 8 4
output
12
input
1
7
output
1
Note

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值