import sys
a, b = map(int, sys.stdin.readline().strip().split())
res, blood = 0, 0
for i in range(a):
c, d = map(int, sys.stdin.readline().strip().split())
if d*b > c: # 当收益大于花费时选择进入这场战役否则放弃
res += d # 获得的收益
# 计算花费
if blood >= c: # 血量够时,直接减去需消耗的血量
blood -= c
else: # 血量不够时,先购买血量直接减去需消耗的血量
temp = (c-blood)//b+1
res -= temp
blood = (blood + temp*b)-c
print(res)
import sys
a, b = map(int, sys.stdin.readline().strip().split())
res, blood = 0, 0
for i in range(a):
c, d = map(int, sys.stdin.readline().strip().split())
if d*b > c: # 当收益大于花费时选择进入这场战役否则放弃
res += d # 获得的收益
blood += c # 消耗的血量
temp, rem = blood // b, blood % b
if rem == 0:
print(res - temp)
else:
print(res - temp - 1)
import sys
def calcArea1(A: int, a: int, b: int, n: int) -> int:
deltax = (b-a)/n
res = 0
while a<b:
temp = 2*A*a
temp1 = pow(temp, 0.5)*2
res += temp1*deltax
a += deltax
return res
def calcArea2(A: int, B: int, C: int, a: int, b: int, n: int) -> int:
deltax = (b-a)/n
res = 0
while a<b:
temp = 2*A*a
temp1 = pow(temp, 0.5)
temp2 = B*a+C
res += (temp1-temp2)*deltax
a += deltax
return res
num = int(input())
for i in range(num):
a, b, c = map(int, sys.stdin.readline().strip().split())
delta = 1/(b**2)-2*c/(a*b)
if delta <= 0:
print(0)
else:
temp1 = pow(delta, 0.5) * a
temp2 = a/b
y1, y2 = temp2-temp1, temp2+temp1
x1, x2 = y1/b-c/b, y2/b-c/b
res, res1, n = 8, -1, 1024
while abs(res-res1)/abs(res) >= 10**-5:
res = res1
n = 2*n
res1 = calcArea1(a, 0, x1, n)
res1 = res
res, res2, n = 8, -1, 1024
while abs(res-res2)/abs(res) >= 10**-5:
res = res2
n = 2*n
res2 = calcArea2(a, b, c, x1, x2, n)
print(res1+res)
import sys
num = int(input())
for i in range(num):
a, b, c = map(int, sys.stdin.readline().strip().split())
delta = 1/(b**2)-2*c/(a*b)
if delta <= 0:
print(0)
else:
temp1 = pow(delta, 0.5) * a
temp2 = a/b
y1, y2 = temp2-temp1, temp2+temp1
constant1, constant2, constant3 = 1/(2*b), -c/b, -1/(6*a)
res2 = constant1*y2**2 + constant2*y2 + constant3*y2**3
res1 = constant1*y1**2 + constant2*y1 + constant3*y1**3
print(res2-res1)
思路:
全排列(n个房间,每个房间m种选择:m^n)
不冲突的情况(第一个房间选择一个数[m种选择],之后每个房间只要不与前一个数重复即可[每个房间m-1种选择,n-1个房间]:m*(m-1)^(n-1))
冲突的方案总数 = 全排列 - 不冲突情况 = m^n - m*(m-1)^(n-1) = m * ( m^(n-1) - (m-1)^(n-1) )
超时优化思路:
快速幂,m^n 直接快速幂取模,m(m-1)^(n-1) 就对m取模,(m-1)^(n-1)取模,再把俩模乘起来加减就行了;
python的pow()自带快速幂
import sys
for line in sys.stdin:
m, n = map(int, line.strip().split())
temp1 = pow(m, n) % 100003
temp2 = m % 100003
temp3 = pow(m-1, n-1) % 100003
res = (temp1 - temp2 * temp3) % 100003
print(res)
import sys
n, k = map(int, sys.stdin.readline().strip().split())
data = []
for i in range(n):
temp = list(map(int, sys.stdin.readline().strip().split()))
data.append(temp)
res = 0
if n == 1:
print(res)
for i in range(n):
for j in range(i+1,n):
if data[i][0]+data[j][0] == data[i][1]+data[j][1]:
flag = True
temp = data[i][0]+data[j][0]
for kk in range(2,k):
if data[i][kk]+data[j][kk] != temp:
flag = False
break
if flag:
res += 1
print(res)
思路:
将aij+akj = ai0+ak0变形为aij-ai0 = -(akj-ak0)
对每组数据将 (aij-ai0) 构建Hash表
import sys
from collections import defaultdict
n, k = map(int, sys.stdin.readline().strip().split())
data = defaultdict(lambda: 0)
tempB = [0]*(k-1)
res = 0
for i in range(n):
tempA = list(map(int, sys.stdin.readline().strip().split()))
for j in range(1, k):
tempA[j] -= tempA[0]
tempB[j-1] = -tempA[j]
if tuple(tempB) in data:
res += data[tuple(tempB)]
data[tuple(tempA[1:])] += 1
print(res)
from typing import List
import sys
def bfs(g: List[List[int]], g1: List[List[int]], visit: List[int], pos: int) -> int:
res = 0
table = []
for i in g[pos]:
if visit[i] == 0:
visit[i] = 1
res += 1
table.append(i)
for i in g1[pos]:
if visit[i] == 0:
visit[i] = 1
res += 1
table.append(i)
for p in table:
res += bfs(g, g1, visit, p)
return res
t = int(input())
for i in range(t):
n = int(input())
data = []
for j in range(n):
x, y = map(int, sys.stdin.readline().strip().split())
data.append([x, y])
l = []
d = {}
for line in data: # 获取所有用户的标号
l.append(line[0])
l.append(line[1])
st = list(set(l))
for i in range(len(st)): # 将所有用户重新编号,按顺序编号
d[st[i]] = i
for i in range(len(data)):
data[i][0] = d[data[i][0]]
data[i][1] = d[data[i][1]]
size = len(st) # 构建无向图的邻接表
g = [[] for i in range(size)]
g1 = [[] for i in range(size)]
for line in data:
g[line[0]].append(line[1])
g1[line[1]].append(line[0])
res, tempRes = 0, 0 # 对无向图进行BFS搜索每个连通分量(朋友圈),记录最大的朋友圈
visit = [0]*size
for i in range(size):
if visit[i] == 0:
visit[i] = 1
tempRes = 1 + bfs(g, g1, visit, i)
res = max(res, tempRes)
print(res)