小工具知识网

当前位置: 首页 > 行业知识 > 蓝桥杯刷题--python--17-前戳和

行业知识

蓝桥杯刷题--python--17-前戳和

2026-11-11 07:02:52 来源:互联网转载

1230. K倍区间 - index.php/tags-41973.html" >AcWing题库

()

# n*n

N,K=map(int,input().split())A=[0 for _ in range (N)]s_A=[0]*(N+1)res=0for  i in range (N):    A[i]=int(input())for i in range (1,N+1):    s_A[i]=s_A[i-1]+A[i-1]for i in range (1,N+1):    for j in range (i,N+1):        if (s_A[j]-s_A[i-1])%K==0:            res+=1print(res)

# 哈希表优化

()

n, k = map(int, input().split())
s=[0]*(n+1)
for i in range (1,n+1):
    s[i]=int(input())

res = 0
cnt = [0] * k
cnt[0] = 1
for i in range(1, n + 1):
    s[i] += s[i - 1]
    res += cnt[s[i] % k]
    cnt[s[i] % k] += 1

print(res)

上一篇:dns服务器出问题怎么处理?

下一篇:宝塔面板IP网页怎么改?