A
签到
s = input().strip()
t = "while"
res = 0
for a,b in zip(s,t):
res += int(a!=b)
print(res)
B
定长滑窗
n =int(input())
a = list(map(int,input().strip().split()))
if n < 10:
print(sum(a))
exit()
l,r = 0,0
res = 0
while r < 10:
res += a[r]
r += 1
t = res
while r < n:
t += a[r] - a[l]
if t > res:
res = t
l += 1
r += 1
print(res)
C
贪心,维护前缀最大值
for _ in range(int(input())):
n = int(input())
a = map(int,input().strip().split())
mx = 0
res = 0
for num in a:
if mx > num:
if mx + num > res:
res = mx+num
else:
mx = num
print(res)
D
排序 + 区间划分
先从小到大排序,若a[i+1] - a[i] <= 1
,则划分为同一区间,若区间内所有数相等,相当于有r-l+1
个连通分量,若有任意两个值不等,那整个区间都是同一个连通分量
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().strip().split()))
a.sort()
l,r = 0,0
last = a[0]
t = 0
res = 0
while r < n:
num = a[r]
if num - last <= 1:
r += 1
last = num
else:
if a[l] == a[r-1]:
res += r-l-1
t += 1
else:
t += 1
l = r
last = num
if a[l] == a[r-1]:
res += r-l-1
t += 1
else:
t += 1
# print(res,t)
print(res + t - 1)
E
模拟 + 计数
- 统计矩阵
1
的总数目one
- 统计矩阵每行的
1
的数目 - 统计矩阵每列的
1
的数目
①如果没有1
,返回YES
②如果只存在两行或者两列都是1
,返回YES
第三种,选一行一列,若 one == m + n - 2
,才能采取第三种方案,然后枚举中心,根据每行每列数目的1
是否等于one
即可
for _ in range(int(input())):
n,m = map(int,input().strip().split())
grid = []
one = 0
# 行前缀和
rows = [0 for _ in range(n)]
tr = 0
for i in range(n):
s = input().strip()
grid.append(s)
for w in s:
if w == '1':
one += 1
rows[i] += 1
# 整行都是1
if rows[i] == m:
tr += 1
# 两行反转
if (not tr or tr == 2) and tr * m == one:
print("YES")
continue
# 列
cols = [0 for _ in range(m)]
tc = 0
for j in range(m):
for i in range(n):
w = grid[i][j]
if w == '1':
cols[j] += 1
# 整列都是1
if cols[j] == n:
tc += 1
# 两列反转
if (not tc or tc == 2) and tc * n == one:
print("YES")
continue
# 选一行和一列反转
if one != n+m-2:
print("NO")
continue
flag = False
# 枚举中心
for i in range(n):
for j in range(m):
if grid[i][j] == '1':
continue
if rows[i] == m - 1 and cols[j] == n-1:
flag = True
break
if flag:
break
if flag:
print("YES")
else:
print("NO")
F
假设一个数num
,有odd
个奇质因数 和 even
个偶质因数。
那么根据
- 奇 * 偶 = 偶
- 奇 * 奇 = 奇
想得到奇因数,那不能选择
even
,总共1 / (even+1)
概率能得到奇因数。 偶质因数只有2
,dp[i]
代表i
的阶乘含有质因数2
的数目,若相求dp[i+1]
,只要求i+1
有多少个2
质因数即可,概率就是1/dp[i+1]
,再结合费马小定理求逆元即可。
input()
nums = list(map(int,input().strip().split()))
maxValue = int(1e6)
# dp[i] , i的阶乘含有`2`的数目
dp = [0 for _ in range(maxValue + 1)]
two = 0
for num in range(1,maxValue+1):
i = num
while num % 2 == 0:
num //= 2
two += 1
dp[i] = two
mod = 998244353
res = []
for n in nums:
res.append(pow(dp[n]+1,mod-2,mod))
print(*res)