当前位置:网站首页>Sum of factor numbers of interval product -- prefix sum idea + fixed one shift two
Sum of factor numbers of interval product -- prefix sum idea + fixed one shift two
2022-07-01 11:53:00 【hans774882968】
author :hans774882968 as well as hans774882968
subject
(sorry No link to the original question found ~) I have an array a, length ≤1e5,1 <= a[i] <= 3. Set interval [l,r] The weight of is the number of factors of the product of interval elements , Find the weight sum of all intervals , model 1e9+7.
Ideas
Interval weight is ( Section 2 The number of + 1) * ( Section 3 The number of + 1), These two quantities can be expressed by prefixes and , Might as well set up s2, s3. Then the demand is
∑ 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)
Let's enumerate r, Think r Is constant , and l It is changing. . It's easy to take apart
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
except s2, s3 outside , It needs maintenance sum(s2[i]), sum(s3[i]), sum(s2[i]*s3[i]) this 3 Prefixes and arrays .
The code generates a set of random data each time , Multiple runs pass the beat , We just think n^2 Both violence and positive solutions have been correctly realized .
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)
边栏推荐
- Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
- Summary of JFrame knowledge points 1
- 指纹浏览器工作原理、使用场景以及重要性简单讲解
- Epoll introduction
- 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%
- ABBIRB120工业机器人机械零点位置
- 用于分类任务的数据集划分脚本
- Software project management 9.2 Software project configuration management process
- 我在中山,到哪里开户比较好?实际上网上开户安全么?
- epoll介绍
猜你喜欢

Matrix of numpy

Learning summary on June 28, 2022

2022/6/29学习总结

CPI tutorial - asynchronous interface creation and use

Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project

Emotion analysis based on IMDB comment data set

C knowledge point form summary 2

Adjacency matrix undirected graph (I) - basic concepts and C language

Harbor webhook from principle to construction
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls
随机推荐
Dlhsoft Kanban, Kanban component of WPF
2022/6/29学习总结
ABBIRB120工业机器人机械零点位置
印象深刻的bug汇总(持续更新)
Want to ask, is there a discount for opening a securities account? Is it safe to open a mobile account?
小米手机解BL锁教程
Raspberry pie 4B installation tensorflow2.0[easy to understand]
C summary of knowledge points 3
Value/sortedset in redis
Value/list in redis
自定义 grpc 插件
VScode快捷键(最全)[通俗易懂]
Redis的攻击手法
Introduction to unittest framework and the first demo
JS日期格式化转换方法
区间乘积的因子数之和——前缀和思想+定一移二
IPlImage的width和widthStep
Value/hush in redis
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
耐克如何常年霸榜第一名?最新財報答案來了