C - 権限管理システム / Permission Management System 解説 by admin
Claude 4.6 Opus (Thinking)Overview
This problem asks you to find the minimum permission bit string that grants access to all specified resources. It can be solved by taking the bitwise OR of the required permissions for each resource.
Analysis
Understanding the Access Condition
The condition for an employee with permission \(K\) to access resource \(i\) is \(K \mathbin{\&} S_i = S_i\). This means “all bits that are 1 in \(S_i\) must also be 1 in \(K\).”
Let’s consider a concrete example. When \(L = 4\):
- Required permission for resource 1: \(S_1 = \) 1010
- Required permission for resource 2: \(S_2 = \) 0110
To access resource 1, the 1st and 3rd bits must be 1. To access resource 2, the 2nd and 3rd bits must be 1.
Key Insight: Taking the OR is Sufficient
To access all of multiple resources, all bits required by any resource must be set to 1. In other words, the required permission is at least \(S_{c_1} \mathbin{|} S_{c_2} \mathbin{|} \cdots \mathbin{|} S_{c_M}\) (bitwise OR).
Conversely, if we use this OR result directly as the permission \(K\), then \(K \mathbin{\&} S_i = S_i\) holds for each \(S_i\). This is because the OR ensures that all 1 bits from each \(S_i\) are included in \(K\).
Furthermore, since this OR result sets only the minimally necessary bits to 1, it minimizes the value as a binary number. Setting extra bits to 1 would only increase the value, and removing any bit would make some resource inaccessible.
In the example above, the answer is \(K = \) 1010 \(\mathbin{|}\) 0110 \(=\) 1110.
Algorithm
- Convert each resource’s permission bit string \(S_i\) to an integer and store it.
- For each query, combine the permission bit strings of all specified resources using OR.
- Convert the result to a bit string of length \(L\) and output it.
Complexity
- Time complexity: \(O(N \cdot L + \sum_{j=1}^{Q} M_j)\)
- Preprocessing (reading bit strings and converting to integers): \(O(N \cdot L)\)
- For each query, taking the OR of \(M_j\) resources: \(O(M_j)\) (integer OR is \(O(1)\))
- Since \(\sum M_j \leq 10^5\), the overall computation is sufficiently fast.
- Space complexity: \(O(N)\)
- Storing each resource’s permission as an integer.
Implementation Notes
Since bit strings are given as strings like
"1010", converting them to integers as binary numbers usingint(s, 2)makes bitwise operations easy to perform.For output,
format(result, '0Lb')(whereLis the number of digits) converts the result to a bit string of length \(L\) with leading zero padding.Since \(L \leq 64\), Python’s integer arithmetic handles this without issues (in C++ and similar languages,
unsigned long longcan be used).Source Code
import sys
input = sys.stdin.readline
def main():
N, L, Q = map(int, input().split())
S = []
for i in range(N):
s = input().strip()
# Convert bit string to integer
val = int(s, 2)
S.append(val)
for _ in range(Q):
line = list(map(int, input().split()))
M = line[0]
resources = line[1:M+1]
# OR all required permission bit strings
result = 0
for c in resources:
result |= S[c - 1]
# Convert back to bit string of length L
print(format(result, '0' + str(L) + 'b'))
main()
This editorial was generated by claude4.6opus-thinking.
投稿日時:
最終更新: