当前位置:网站首页>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)
边栏推荐
- Redis startup and library entry
- kafuka学习之路(一)kafuka安装和简单使用
- Seckill system 03 - redis cache and distributed lock
- 树莓派4B安装tensorflow2.0[通俗易懂]
- openinstall:微信小程序跳转H5配置业务域名教程
- Unity XLua 协程封装
- CAD如何设置标注小数位
- 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%
- Talk about the pessimistic strategy that triggers full GC?
- JS日期格式化转换方法
猜你喜欢

邻接矩阵无向图(一) - 基本概念与C语言

用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义

Learning summary on June 30, 2022

图的理论基础

Skip the test cases to be executed in the unittest framework

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

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%

How to understand the developed query statements

二叉堆(一) - 原理与C实现

博途V15添加GSD文件
随机推荐
C#依赖注入(直白明了)讲解 一看就会系列
Use set_ Handler filters out specific SystemC wrapping & error messages
Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
【单片机】【数码管】数码管显示
How to set decimal places in CAD
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
Value/set in redis
Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
自定義 grpc 插件
Harbor webhook from principle to construction
JS日期格式化转换方法
Theoretical basis of graph
241. Design priority for operational expressions: DFS application questions
Mechanism and type of CPU context switch
CPU 上下文切换的机制和类型 (CPU Context Switch)
241. 为运算表达式设计优先级 : DFS 运用题
Exposure: a white box photo post processing framework reading notes
Vscode shortcut key (the most complete) [easy to understand]
Istio, ebpf and rsocket Broker: in depth study of service grid
Can solo be accessed through IPv6?