当前位置:网站首页>区间乘积的因子数之和——前缀和思想+定一移二
区间乘积的因子数之和——前缀和思想+定一移二
2022-07-01 11:51:00 【hans774882968】
作者:hans774882968以及hans774882968
题目
(sorry没找到原题链接~)有一个数组a,长度≤1e5,1 <= a[i] <= 3。设区间[l,r]的权值为区间元素乘积的因数个数,求所有区间的权值和,模1e9+7。
思路
区间权值为(区间2的个数 + 1) * (区间3的个数 + 1),这两个量可以用前缀和表示,不妨设为s2, s3。则所求为
∑ l = 0 r − 1 ( s 2 [ r ] − s 2 [ l ] + 1 ) ∗ ( s 3 [ r ] − s 3 [ l ] + 1 ) \sum_{l=0}^{r-1} (s2[r]-s2[l]+1)*(s3[r]-s3[l]+1) l=0∑r−1(s2[r]−s2[l]+1)∗(s3[r]−s3[l]+1)
我们枚举r,则认为r是固定的,而l是变化的。拆开得
r ∗ s 2 [ r ] ∗ s 3 [ r ] − s 2 [ r ] ∗ ∑ s 3 [ l ] + r ∗ s 2 [ r ] − s 3 [ r ] ∗ ∑ s 2 [ l ] + ∑ s 2 [ l ] ∗ s 3 [ l ] − ∑ s 2 [ l ] + r ∗ s 3 [ r ] − ∑ s 3 [ l ] + r r*s2[r]*s3[r] - s2[r]*\sum s3[l] + r*s2[r] - s3[r]*\sum s2[l] + \sum s2[l]*s3[l] - \sum s2[l] + r*s3[r] - \sum s3[l] + r r∗s2[r]∗s3[r]−s2[r]∗∑s3[l]+r∗s2[r]−s3[r]∗∑s2[l]+∑s2[l]∗s3[l]−∑s2[l]+r∗s3[r]−∑s3[l]+r
除了s2, s3之外,还需要维护sum(s2[i]), sum(s3[i]), sum(s2[i]*s3[i])这3个前缀和数组。
代码每次会生成一组随机数据,多次运行都对拍通过,我们就认为n^2暴力和正解都正确实现了。
import random
mod = int(1e9) + 7
def data_gen(n):
return [random.randint(1, 3) for _ in range(n)]
def bf(a):
n = len(a)
ans = 0
for i in range(n):
v2, v3 = 0, 0
for j in range(i, n):
v2 += (a[j] == 2)
v3 += (a[j] == 3)
ans = (ans + (v2 + 1) * (v3 + 1) % mod) % mod
return ans
def solve(a):
n = len(a)
s2, s3 = [0], [0]
for v in a:
s2.append(s2[-1] + (v == 2))
s3.append(s3[-1] + (v == 3))
ss2, ss3, s23 = [0], [0], [0]
for i in range(1, n + 1):
ss2.append((ss2[-1] + s2[i]) % mod)
for i in range(1, n + 1):
ss3.append((ss3[-1] + s3[i]) % mod)
for i in range(1, n + 1):
s23.append((s23[-1] + s2[i] * s3[i] % mod) % mod)
ans = 0
for r in range(1, n + 1):
u = ((r * s2[r] * s3[r] % mod - s2[r] * ss3[r - 1] % mod +
r * s2[r] % mod - s3[r] * ss2[r - 1] % mod +
s23[r - 1] - ss2[r - 1] + r * s3[r] % mod -
ss3[r - 1] + r) % mod + mod) % mod
ans = (ans + u) % mod
return ans
if __name__ == '__main__':
a = [2, 1, 3]
ans1 = solve(a)
ans2 = bf(a)
assert ans1 == 13 and ans2 == 13
a = data_gen(2000)
ans1 = solve(a)
ans2 = bf(a)
assert ans1 == ans2
print(a[:20], ans1, ans2)
边栏推荐
猜你喜欢

Seckill system 03 - redis cache and distributed lock

How to set decimal places in CAD

Harbor webhook from principle to construction

Theoretical basis of graph

Mingchuang plans to be listed on July 13: the highest issue price is HK $22.1, and the net profit in a single quarter decreases by 19%

CAD如何設置標注小數比特

S7-1500PLC仿真

Wonderful! MarkBERT

Emotion analysis based on IMDB comment data set

Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%
随机推荐
Force button homepage introduction animation
S7-1500PLC仿真
On recursion and Fibonacci sequence
kafuka学习之路(一)kafuka安装和简单使用
TMUX usage
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
耐克如何常年霸榜第一名?最新财报答案来了
Why must we move from Devops to bizdevops?
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
Impressive bug summary (continuously updated)
证券账户销户后果 开户安全吗
Kernel synchronization mechanism
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
[Maui] add click events for label, image and other controls
redis中value/SortedSet
Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
Use set_ Handler filters out specific SystemC wrapping & error messages
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
深入理解 grpc part1
印象深刻的bug汇总(持续更新)