P9752 题解

· · 题解

容易发现序列是可逆的,所以可以暴力找出每个密码锁能拨成的状态,然后将每个集合求交即可。记得最后要把所有给出的状态去掉。时间复杂度为 O(kn\log kn),其中 k 为 81。

所以这题完全能把密码锁长度开 10^3 啊,强烈谴责 CCF 放了一车枚举原状态的过了。

AC 代码

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const int MAXN = 1e5 + 10;

set<array<int, 5>> s, t; vector<array<int, 5>> v;

int n; array<int, 5> a[8], x;

int main() {
    freopen("lock.in", "r", stdin);
    freopen("lock.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 5; j++) scanf("%d", &a[i][j]);
        for (int j = 0; j < 5; j++) {
            x = a[i];
            for (int k = 0; k < 9; k++) {
                x[j] = (x[j] + 1) % 10, s.insert(x);
            }
        }
        for (int j = 0; j < 4; j++) {
            x = a[i];
            for (int k = 0; k < 9; k++) {
                x[j] = (x[j] + 1) % 10, x[j + 1] = (x[j + 1] + 1) % 10;
                s.insert(x);
            }
        }
        if (!i) t = s;
        else {
            for (auto p : t) if (s.find(p) == s.end()) v.push_back(p);
            for (auto p : v) t.erase(p); v.clear();
        }
        s.clear();
    }
    for (int i = 0; i < n; i++) t.erase(a[i]);
    printf("%d", (int)t.size());
}