• [CF1475G] Strange Beauty


    [CF1475G] Strange Beauty - 数论

    Description

    (n) 个数,从中挑选一个子集,使得集合中任意两个不同的数 (x,y),有 (x|y)(y|x)

    Solution

    子集中最小的数必须要能整除所有的数,所有的数必须要能被最大的数整除

    把整除关系画成有向边,则是一个有向无环完全图,我们取其中的最长路考虑

    假定 (a[]) 升序

    (f[x]) 为最大数选为 (x) 时的方案数,那么 (f[y]=max_{x in S and x | y} f[x])

    暴力枚举因子转移即可

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        int t;
        cin >> t;
    
        const int N = 2e5;
        vector<vector<int>> fac(N + 2);
        for (int i = 1; i <= N; i++)
        {
            vector<int> &factor = fac[i];
            for (int j = 1; j * j <= i; j++)
            {
                if (i % j == 0)
                {
                    factor.push_back(j);
                    factor.push_back(i / j);
                }
            }
        }
        while (t--)
        {
            int n;
            cin >> n;
    
            vector<int> a(n + 2);
            for (int i = 1; i <= n; i++)
                cin >> a[i];
    
            int max_value = *max_element(&a[1], &a[n + 1]);
    
            vector<int> c(max_value + 2);
            vector<int> f(max_value + 2);
    
            for (int i = 1; i <= n; i++)
                c[a[i]]++;
    
            for (int i = 1; i <= max_value; i++)
            {
                if (c[i])
                {
                    vector<int> &factor = fac[i];
    
                    for (auto j : factor)
                    {
                        f[i] = max(f[i], f[j]);
                    }
                    f[i] += c[i];
                }
            }
    
            cout << n - *max_element(&f[1], &f[max_value + 1]) << endl;
        }
    }
    
  • 相关阅读:
    noip模拟赛 软件software
    bzoj1070: [SCOI2007]修车
    bzoj2947: [Poi2000]促销
    bzoj2940: [Poi2000]条纹
    bzoj3714: [PA2014]Kuglarz
    bzoj3717: [PA2014]Pakowanie
    说明
    Hello World!
    牛客网PAT练兵场-旧键盘打字
    牛客网PAT练兵场-锤子剪刀布
  • 原文地址:https://www.cnblogs.com/mollnn/p/14337954.html
Copyright © 2020-2023  润新知