当前位置:网站首页>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)
边栏推荐
- CAD如何设置标注小数位
- NOV Schedule for . Net to display and organize appointments and recurring events
- Are the consequences of securities account cancellation safe
- Value/set in redis
- JS日期格式化转换方法
- Raspberry pie 4B installation tensorflow2.0[easy to understand]
- ACLY与代谢性疾病
- Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
- Dlhsoft Kanban, Kanban component of WPF
- Redis startup and library entry
猜你喜欢

Binary stack (I) - principle and C implementation

Chen Gong: Micro service, is it still so pure?

Redis' attack tactics
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls

Learning summary on June 28, 2022

On recursion and Fibonacci sequence

邻接矩阵无向图(一) - 基本概念与C语言
![[classic example] classic list questions @ list](/img/d8/a259e5f9d08eacbef31254d1bc3304.jpg)
[classic example] classic list questions @ list

Matrix of numpy

Summary of JFrame knowledge points 1
随机推荐
Implementation of address book management system with C language
超详细黑苹果安装图文教程送EFI配置合集及系统
MQ-防止消息丢失及重复消费
Can solo be accessed through IPv6?
sshd_config 中 PermitRootLogin 的探讨
分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
Force button homepage introduction animation
Personnaliser le plug - in GRPC
Vscode shortcut key (the most complete) [easy to understand]
Exploration and practice of inress in kubernetes
241. Design priority for operational expressions: DFS application questions
Value/sortedset in redis
[Maui] add click events for label, image and other controls
微信小程序开发 – 用户授权登陆「建议收藏」
Theoretical basis of graph
Value/set in redis
[classic example] classic list questions @ list
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
JS日期格式化转换方法
Extended tree (I) - concept and C implementation