赞
踩
if __name__ == '__main__':
res1 = 0
res0 = 0
for i in range(1,100000):
i = str(i)
res1 += i.count('1')
res0 += i.count('0')
if res1 == 2022 or res0 == 2022 :
break
print(int(i)-1)
# 3181
def gcd(a,b):
if b == 0: return a
else: return gcd(b,a%b)
if __name__ == '__main__':
dian = []
for i in range(20):
for j in range(21):
dian.append((i,j))
res = set()
for i in range(len(dian) - 1):
x1,y1 = dian[i]
for j in range(i+1,len(dian)):
x2,y2 = dian[j]
A,B,C = y2-y1,x2-x1,x1*y2-x2*y1
k = gcd(gcd(A,B),C) # 求三个数的最大公约数
res.add((A/k,B/k,C/k))
print(len(res))
import math
yin = []
n = 2021041820210418
# for i in range(1,n+1): # 太慢了
# if n % i == 0:
# yin.append(i)
for i in range(1,int(math.sqrt(n) + 1)):
if n % i == 0:
yin.append(i)
yin.append(n/i)
print('over')
res = 0
for i in yin:
for j in yin:
for k in yin:
if i * k * j == n:
res += 1
print(res)
def gcd(a,b):
if b == 0: return a
return gcd(b,a%b)
def gbs(a,b):
return a*b//gcd(a,b)
inf = 1e9
dp = [inf] * 2022 # dp表示到1的距离
for i in range(1,23):
dp[i] = i
for i in range(23,2022):
for j in range(1,22):
dp[i] = min(dp[i],dp[i-j] + gbs(i,i-j)) # 后面的到1的距离等于前面21中的一个到1的距离加上前面21个中一个到他的距离
print(dp[2021])
import math
n = 21
m = 1<<21
dp = [[0] * n for _ in range(m+1)] # do[i][j]表示状态i到到达j的总数量
g =[[0]*n for _ in range(n)] # g[i][j] 表示I能到J
for i in range(1,n+1):
for j in range(1,n+1):
if math.gcd(i,j) == 1:
g[i-1][j-1] = 1
dp[1][0] = 1
# 如果要刚好dp[i][j] 表示第i个状态到第j个楼的话,要开22位的二进制,会更慢.所以我们j就表示J+1楼
for i in range(1,m):
for j in range(n):
if i >> j & 1: # 判断j+1楼是否在里面
for k in range(n):
if i - (1 << j) >> k & 1 and g[k][j] == 1: #找到符合条件的k
dp[i][j] += dp[i - (1 << j)][k]
print(sum(dp[m-1]) - dp[m-1][0]) # 最后就是状态011111111111(21个1)到达不是1楼的就好了,因为每个楼都可以去1楼
n = int(input())
n = n%(24 * 60 * 60 * 1000) # 抛去前面N天的,剩下当天的毫秒数
hour = n // (60 * 60 *1000)
mins = (n - hour*60*60*1000) // (60 * 1000)
scent = (n - hour * 60 * 60 *1000 - mins * 60 * 1000) // 1000
print('%02d'%hour,end = ':')
print('%02d'%mins,end = ':')
print('%02d'%scent,end = '')
if __name__ == '__main__':
n = int(input())
if n == 1: print(1)
res = 3 # 已经计算的个数
li,l = [1,2],3 # 第三行的一半元素和行数
while n not in li:
res += len(li)*2 - l % 2
li = [1] + [li[i] + li[i+1] for i in range(len(li) - 1)] + ([li[-1]*2] if l % 2 == 0 else [])
l += 1
print(res + li.index(n) + 1)
思路参考:(156条消息) 备战蓝桥杯历年试题:杨辉三角形 省赛B组 Python详解_m0_62277756的博客-CSDN博客
def c(a,b):
res = 1
i,j = a,1
while j<=b:
res = res * i / j
i -= 1
j += 1
return int(res)
def find(j,n): # 在第j斜行找到n的位置
l,r = 2*j,n # C(a,j)的a最小是2*j 最大是n,因为C(n,1)够了已经
while l <= r:
mid = (l+r)//2
if c(mid,j) == n:
print(int(mid*(mid+1)/2) + j + 1) # j+1不能在里面,否则会被忽略
return True
elif c(mid,j) > n:
r = mid - 1
else: l = mid + 1
return False
if __name__ == '__main__':
N = int(input())
for i in range(16,-1,-1):
if find(i,N): break
ans = 0
# f[u]以u结点为根的树的最大高度是u的子节点数量 + 各个子树的最大高度
# 其实就是看子树有多少个孩子,递归下去
def dfs(u): #
global ans
tot = len(s[u])
f[u] = tot
res = 0
for i in range(tot):
v = s[u][i]
m = dfs(v)
res = max(res,m)
f[u] += res
return f[u]
if __name__ == '__main__':
n = int(input())
s = [[] for i in range(n+1)]
for i in range(n-1):
s[int(input())].append(i+2)
# 输入 5 1 1 1 2 那就是 s[1] = [2,3,4] 表示 2,3,4是父节点是1
f = [0]*(n+1)
print(dfs(1))
if __name__ == '__main__':
t = int(input())
for i in range(t):
sum = 0
max2 = 0
list1 = [int(x) for x in input().split()]
for i in range(1,len(list1)):
sum ^= list1[i]
max2 = max(max2,list1[i]) # 顺便记录下来最大的先
if sum == 0 :
print(0)
continue
high = 1
while high < max2:
high = high<<1 # 找到最大的那个的最高位是多少
while high > 0:
temp = 0
for i in list1[1:]:
if i & high != 0: # 最高位和high相同
temp += 1
if temp == 1:
print(1)
break # 先手赢
if temp %2 == 1: # 如果有奇数个最高位为1,那么就要看剩下的有几个
if list1[0] % 2 == 1: # n如果是奇数 减去奇数个最高位相同的,剩下的就是偶数个
print(1)
break # 除去最高位一样的那些,剩下的有偶数,就先手赢
else:
print(-1)
break # 否则后手赢
if temp %2 == 0: #如果最高位有偶数位,则顺延到下一位
high = high >> 1
def func():
dp = [[0] * (n+2) for _ in range(n+2)] # dp[i][j] 表示前i个序列,左括号比右括号多j个的方案数
dp[0][0] = 1
for i in range(1,n+1):
if s[i-1] == '(':
for j in range(1,n+1):
dp[i][j] = dp[i-1][j-1]
else:
dp[i][0] = (dp[i - 1][0] + dp[i-1][1]) % mod
for j in range(1,n+1):
dp[i][j] = (dp[i][j-1] + dp[i-1][j+1]) % mod # 0特判是因为会出界
for i in range(n+1):
if dp[n][i]: #找前n个中左括号比右括号多的数最少,表示我们添加的也最少
return dp[n][i]
return -1
if __name__ == '__main__':
s = list(input())
n = len(s)
mod = 1e9 + 7
left = func()
s.reverse() # 从左往右看是填左括号,右往左是填右括号
# 但是因为我们func是按左括号写的,所以还需要将左括号和右括号对调一下,这样我们就不用重新写一个函数
for i in range(n):
if s[i] == ')':
s[i] = '('
else:
s[i] = ')'
right = func()
print(int(left*right % mod))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。