Hướng dẫn bài PIN

Trùm CUỐI 2020-10-11 7:03:56

Tổng cộng 2 trả lời

Trùm CUỐI

Một code khác để tham khảo:

#include <bits/stdc++.h>
using namespace std;
#define base 37
typedef long long ll;
int const maxValue = base * base * base * base;
int n, d;
int f[maxValue];

int digit(char x, bool isequal) {
    if (!isequal)
        return base - 1;
    return x <= '9' ? x - '0' : 10 + x - 'a';
}
int base37(string s, int mask) {
    int res = digit(s[0], mask & 1);
    for (int i = 1; i < 4; i++) res = res * base + digit(s[i], (mask >> i) & 1);
    return res;
}
int fixcount(int b) {
    int res = 4;
    while (b) {
        if (b % base == base - 1)
            res--;
        b /= base;
    }
    return res;
}
void enter() {
    cin >> n >> d;
    memset(f, 0, sizeof f);
    for (int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        for (int mask = 0; mask < 16; mask++) f[base37(s, mask)]++;
    }
}
void solve() {
    int fix[5] = { 0, 0, 0, 0, 0 }, g[5];
    for (int b = 0; b < maxValue; b++) fix[fixcount(b)] += ll(f[b]) * (f[b] - 1) / 2;
    if (d == 1)
        cout << fix[3] << '\n';
    if (d == 2)
        cout << fix[2] - 3 * fix[3] << '\n';
    if (d == 3)
        cout << fix[1] - 2 * fix[2] + 3 * fix[3] << '\n';
    if (d == 4)
        cout << fix[0] - fix[1] + fix[2] - fix[3] << '\n';
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    enter();
    solve();
}
Trùm CUỐI

Code tham khảo:

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#define filein "pin.inp"
#define fileout "pin.out"
using namespace std;

int digit(char c) { return isdigit(c) ? c - '0' : c - 'a' + 10; }
long long cnt[37][37][37][37], a[4], fix[4];

int main() {
#ifndef ONLINE_JUDGE
    freopen(filein, "r", stdin);
    freopen(fileout, "w", stdout);
#endif  // ONLINE_JUDGE
    int N, D;
    cin >> N >> D;
    vector<string> PIN(N);
    for (int n = 0; n < N; ++n) {
        cin >> PIN[n];
        for (int keep = 0; keep < 16; ++keep) {
            for (int i = 0; i < 4; ++i)
                if (keep & 1 << i)
                    a[i] = digit(PIN[n][i]);
                else
                    a[i] = 36;
            ++cnt[a[0]][a[1]][a[2]][a[3]];
        }
    }
    for (a[0] = 0; a[0] < 37; ++a[0])
        for (a[1] = 0; a[1] < 37; ++a[1])
            for (a[2] = 0; a[2] < 37; ++a[2])
                for (a[3] = 0; a[3] < 37; ++a[3]) {
                    int f = (a[0] < 36) + (a[1] < 36) + (a[2] < 36) + (a[3] < 36);
                    long long c = cnt[a[0]][a[1]][a[2]][a[3]];
                    fix[f] += c * (c - 1) / 2;
                }
    if (D == 1)
        cout << fix[3] << endl;
    if (D == 2)
        cout << fix[2] - 3 * fix[3] << endl;
    if (D == 3)
        cout << fix[1] - 2 * fix[2] + 3 * fix[3] << endl;
    if (D == 4)
        cout << fix[0] - fix[1] + fix[2] - fix[3] << endl;
    return 0;
}