C - 権限管理システム / Permission Management System 解説 by admin
GPT 5.2 HighOverview
For each request specifying multiple resources, find the permission bit string \(K\) that grants access to all of them simultaneously, minimizing \(K\) as a binary number.
Analysis
When the required permission for resource \(i\) is \(S_i\), the access condition is: \(K \mathbin{\&} S_i = S_i\) This means “every bit that is 1 in \(S_i\) must also be 1 in \(K\).”
If a request wants to access the resource set \(\{c_1, c_2, \dots\}\), then \(K\) must satisfy all of them:
- The 1-bits of \(S_{c_1}\) must be 1 in \(K\)
- The 1-bits of \(S_{c_2}\) must also be 1 in \(K\)
- …
In the end, any bit required by at least one resource must be 1 in \(K\). This is the bitwise union, expressed by the following formula: \(K \supseteq S_{c_1} \lor S_{c_2} \lor \cdots\)
Furthermore, to minimize \(K\) as a binary number, it is optimal to set all unnecessary bits to 0. Therefore, the minimum \(K\) is:
\[K = S_{c_1} \lor S_{c_2} \lor \cdots\]
Concrete Example
Let \(L=5\), and suppose we want to access both: - \(S_1 = 00101\) - \(S_3 = 10000\)
Then setting all required bits gives: - \(K = 00101 \lor 10000 = 10101\)
This is the minimum. To make it any smaller, we would need to turn some 1 into 0, but that would prevent access to the corresponding resource.
A naive search for \(K\) satisfying the conditions would have \(2^L\) candidates (up to \(2^{64}\)), which is infeasible. However, from the observation above, each query can be answered by simply taking the OR.
Algorithm
- Read each \(S_i\) (a bit string of length \(L\)) as an integer (binary number) and store it in an array.
- Since \(L \le 64\), Python’s
inthandles this safely.
- Since \(L \le 64\), Python’s
- For each request, sequentially OR the integer values of the specified resources to build
val:val |= S[idx]
- Convert
valto a binary string of length \(L\) (zero-padded on the left) and output it.
Complexity
- Time complexity: \(O\!\left(N + \sum_{j=1}^{Q} M_j\right)\) (Each query only performs OR for the specified number of resources)
- Space complexity: \(O(N)\) (Storing each resource’s bit string as an integer)
Implementation Notes
Fast input: Since \(N, Q, \sum M_j\) can be up to \(10^5\), reading all at once with
sys.stdin.buffer.read().split()is reliable.Zero-padded output: Using
format(val, "0{}b".format(L))pads the result to length \(L\).Rather than performing bit operations on strings each time, converting to integers and using OR is both simpler and faster.
Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
N = int(next(it))
L = int(next(it))
Q = int(next(it))
S = [0] * (N + 1)
for i in range(1, N + 1):
S[i] = int(next(it).decode(), 2)
fmt = "0{}b".format(L)
out_lines = []
for _ in range(Q):
m = int(next(it))
val = 0
for _ in range(m):
idx = int(next(it))
val |= S[idx]
out_lines.append(format(val, fmt))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: