当前位置:网站首页>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)
边栏推荐
- 我在中山,到哪里开户比较好?实际上网上开户安全么?
- JS date format conversion method
- Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
- 用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
- Kafuka learning path (I) Kafuka installation and simple use
- 谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征
- C#依赖注入(直白明了)讲解 一看就会系列
- Abbirb120 industrial robot mechanical zero position
猜你喜欢

How to understand the developed query statements

MQ prevent message loss and repeated consumption

Redis configuration environment variables

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

谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征

使用set_handler过滤掉特定的SystemC Wraning &Error Message

CAD如何设置标注小数位

Use set_ Handler filters out specific SystemC wrapping & error messages

Mechanism and type of CPU context switch

Learning summary on June 30, 2022
随机推荐
Skip the test cases to be executed in the unittest framework
Width and widthstep of iplimage
Chen Gong: Micro service, is it still so pure?
Talk about the pessimistic strategy that triggers full GC?
[Maui] add click events for label, image and other controls
Redis' attack tactics
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
S7-1500PLC仿真
基于IMDB评论数据集的情感分析
Value/hush in redis
Vscode shortcut key (the most complete) [easy to understand]
博途V15添加GSD文件
CAD如何设置标注小数位
CAD如何設置標注小數比特
Question: what professional qualities should test engineers have?
Nordic nrf52832 flash download M4 error
IPlImage的width和widthStep
Adjacency matrix undirected graph (I) - basic concepts and C language
NOV Schedule for . Net to display and organize appointments and recurring events
Exposure: a white box photo post processing framework reading notes