- MysGln 的博客
Programming exercise on 21 August
- @ 2024-8-21 16:37:11
Programming exercise on 21 August
A
The value can be obtained by performing the procedure operation for in order:
- Refer to the -th least significant digit in the binary representation of .
- Do nothing if it is .
- If it is
1, immediately terminate the procedure. The current value of subtracted by equals sought .
Here, the -th least significant digit (0 or 1) in the binary representation of can be obtained by taking the logical product (AND) of shifted bits to the right.
Since , the iteration above is repeated at most times, which finishes within sufficiently short time. Therefore, the problem has been solved.
Alternatively, one can store the bit sequence in the binary representation of in an array of integers or booleans, or inspect the last bit while right-shifting one by one. These are all fast enough.
If you are not very used to handling bit sequences, you can regard each step as an operation against an integer. They can be regarded as:
- the -th least significant digit in the binary representation of → remainder when is divided by ;
- shifting to the right by one bit → dividing by (with fractional part rounded down).
Thus, the effectively same operations can be achieved by combining these operations. Under the constraints of this problem, it is fast enough.
Intermediate and rather difficult problems sometimes require constant-factor optimization using say bitsets, which is based on bit operations which tend to be fast, so it may be good idea to learn bit operations.
Sample code in Python:
n=int(input())
for i in range(30):
if n&(1<<i):
print(i)
break
B
The answer for this problem can be found by inspecting each of day , day , … to check whether it is within the first days, and add to the answer if it is, and terminating the loop if it is not. This can be implemented with a for statement. The following it
sample codes in Python:
n, m, p = map(int, input().split())
res = 0
while m <= n:
res += 1
m += p
print(res)
C
By the constraints of the problem, we do not need to consider the region outside and . Also, for any pair , the region within and is either entirely covered by a sheet or not covered at all. Therefore, it is sufficient to consider the following problem.
- There is a grid. Let us call the cell in the -th row from the top and -th column from the left cell . Suppose that each cell is initially painted white.
- For each in order, the -th operation paints cell for all integer pairs with and .
- Find the number of cells painted black after the operations.
The process can be actually simulated with for statements, and one can use for statements and if statements to count the cells ending up being painted black, in order to find the answer.
The simulation requires at most about , which is fast enough even with a naive implementation. Therefore, the problem has been solved.
Sample code in Python:
n=int(input())
g=[[False for i in range(100)]for i in range(100)]
for k in range(n):
a,b,c,d=map(int, input().split())
for i in range(a,b):
for j in range(c,d):
g[i][j]=True
ans=0
for i in range(100):
for j in range(100):
if g[i][j]==True:
ans+=1
print(ans)
D
We can sort to assume without changing the answer.
If two adjacent terms differ by two, the answer is between them. Since the Constraints guarantee that the answer is uniquely determined, such a position always occurs once.
The complexity of this problem is .
Sample code (Python):
N=int(input())
A=sorted(list(map(int,input().split())))
for i in range(N-1):
if A[i+1]-A[i]==2:
print(A[i]+1)