0%

二进制

python的bin()函数返回的是符号+'0b'+无符号下的原码,但用x >> i & 1求第i为的二进制表示时,是补码形式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bin(5)
Out[2]: '0b101'
bin(-5)
Out[3]: '-0b101'
bin(-7)
Out[4]: '-0b111'
[ 5 >> i & 1 for i in range(8,-1,-1)]
Out[5]: [0, 0, 0, 0, 0, 0, 1, 0, 1]
for x in [-5,-27,7,15]:
print(x,[x >> i & 1 for i in range(8, -1, -1)])

-5 [1, 1, 1, 1, 1, 1, 0, 1, 1]
-27 [1, 1, 1, 1, 0, 0, 1, 0, 1]
7 [0, 0, 0, 0, 0, 0, 1, 1, 1]
15 [0, 0, 0, 0, 0, 1, 1, 1, 1]
阅读全文 »

记录一下抠二分查找细节的过程。

// 运算符

是除了之后向下取整。

阅读全文 »

1
2
3
4
5
FileInput = False

if FileInput:
sys.stdin = open('flower.in', 'r')
sys.stdout = open('flower.out', 'w')

李白打酒加强版

https://www.lanqiao.cn/problems/2114/learning/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# n酒店,m花
n, m = map(int, input().split())
dp = [[[0 for k in range(m + 1)] for j in range(m + 1)] for i in range(n + 1)]
# dp[i][j][k] 遇到i个酒店j个花后剩余k酒量的方案数
dp[0][0][2] = 1
# dp[1][0][4] = 1
# dp[0][1][1] = 1
# dp[0][2][0] = 1

for i in range(0, n + 1):
for j in range(0, m + 1):
for k in range(m + 1):
if j == m and i != n: # 不能没花了但酒店还有
continue
if not k & 1 and i > 0: # 现在这一个是酒店,i减1,k是一半
dp[i][j][k] += dp[i - 1][j][k // 2]
if k != m and j > 0:
dp[i][j][k] += dp[i][j - 1][k + 1]

print(dp[n][m][0] % 1000000007)
阅读全文 »