題目

C 是個非常好的 c++ STL 練習題。

D - Disjoint Set of Common Divisors

進行質因數分解後,就會發現他其實就是算質因數個數啦!

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

ll gcd(ll a, ll b)
{
    return a == 0 ? b : gcd(b % a, a);
}

int main()
{
    ll a, b;
    scanf("%lld %lld", &a, &b);

    ll g = gcd(a, b);

    int ans = 1;
    for (ll i = 2; i * i <= g; i++) {
        int cnt = 0;
        while (g % i == 0) {
            g /= i;
            cnt++;
        }

        if (cnt > 0)
            ans++;
    }

    if (g > 1)
        ans++;

    printf("%d\n", ans);

    return 0;
}

E - Get Everything

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);

    // read
    int dp[1 << n];
    fill(dp, dp + (1 << n), INT_MAX);
    bool ok[1 << n] = {false};
    dp[0] = 0;
    ok[0] = true;
    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);

        int mask = 0;
        for (int j = 0; j < b; j++) {
            int num;
            scanf("%d", &num);
            num--;
            mask |= (1 << num);
        }

        // printf("mask %d\n", mask);
        dp[mask] = min(dp[mask], a); // for each combination, use the min val one
        ok[mask] = true;
    }

    // solve
    int ans[1 << n];
    fill(ans, ans + (1 << n), INT_MAX);
    for (int i = 0; i < (1 << n); i++) {
        if (ok[i] == true) {
            // printf("ok %d\n", i);
            ans[i] = min(ans[i], dp[i]);
            for (int j = 0; j < (1 << n); j++) {
                if (ans[j] != INT_MAX) {
                    int target = (i | j);
                    // printf("%d %d %d %d\n", i, j, dp[i], ans[j]);
                    ans[target] = min(ans[target], dp[i] + ans[j]);
                }
            }
        }
    }
    printf("%d\n", ans[(1 << n) - 1] == INT_MAX ? -1 : ans[(1 << n) - 1]);

    return 0;
}