C - 権限管理システム / Permission Management System 解説 by admin
DeepSeek V3Overview
Given a required permission bit string for each resource, find the minimum permission bit string that grants access to multiple resources.
Analysis
The core of the problem is finding the minimum permission bit string needed to access multiple resources. Each resource \(i\) has a required permission bit string \(S_i\) defined, and an employee’s permission bit string \(K\) can access that resource when \(K \mathbin{\&} S_i = S_i\).
The key observation is that to access multiple resources, you need the combination of all permissions required by each resource. In other words, the required permissions equal the bitwise OR of all \(S_i\). If we let \(K\) be this OR value, it is the minimum value satisfying the condition. This is because any smaller value would be missing at least one of the required permission bits.
Algorithm
- Convert each resource’s permission bit string to an integer value
- For each query, compute the bitwise OR of the permission bit strings of the specified resources
- Convert the resulting integer value to a binary string of length \(L\) and output it
Specifically, if the set of resources given in a query is \(R\), the desired permission bit string \(K\) is: $\(K = \bigvee_{i \in R} S_i\)\( where \)\vee$ denotes the bitwise OR operation.
Complexity
- Time complexity: \(O(N \cdot L + \sum M_j)\)
- \(O(N \cdot L)\) for converting permission bit strings to integers
- \(O(\sum M_j)\) for query processing, performing OR operations in each query
- Space complexity: \(O(N + \sum M_j)\)
- For storing integer representations of permission bit strings and holding query data
Implementation Notes
When converting bit strings to integers, combining left shifts and OR operations is efficient
When outputting, you need to pad with leading zeros so that the bit string has length \(L\)
Resource numbers specified in queries are 1-indexed, so they need to be converted to 0-indexed
Source Code
import sys
def main():
data = sys.stdin.read().splitlines()
if not data:
return
first_line = data[0].split()
N = int(first_line[0])
L = int(first_line[1])
Q = int(first_line[2])
S_list = []
for i in range(1, 1 + N):
S_list.append(data[i].strip())
queries = []
index = N + 1
for i in range(Q):
parts = data[index].split()
index += 1
M_j = int(parts[0])
resources = list(map(int, parts[1:1+M_j]))
queries.append(resources)
# Precompute the integer representation of each S_i
S_ints = []
for s in S_list:
num = 0
for char in s:
num = (num << 1) | (1 if char == '1' else 0)
S_ints.append(num)
results = []
for resources in queries:
required_mask = 0
for r in resources:
idx = r - 1
required_mask |= S_ints[idx]
# We need to find the minimum K such that (K & required_mask) == required_mask
# Actually, the minimal K is exactly required_mask itself.
# Because if we set K = required_mask, then for each resource, we have exactly the required bits.
# Any smaller value would miss at least one required bit.
k_val = required_mask
# Convert to binary string of length L
bin_str = bin(k_val)[2:]
if len(bin_str) < L:
bin_str = '0' * (L - len(bin_str)) + bin_str
results.append(bin_str)
for res in results:
print(res)
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.
投稿日時:
最終更新: