当前位置:网站首页>区间乘积的因子数之和——前缀和思想+定一移二
区间乘积的因子数之和——前缀和思想+定一移二
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)
边栏推荐
- Harbor webhook from principle to construction
- Test case writing specification in unittest framework and how to run test cases
- redis配置环境变量
- 二叉堆(一) - 原理与C实现
- Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
- 分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
- 证券账户随便哪里开都能使用吗 开户安全吗
- [buuctf.reverse] 144_ [xman2018 qualifying]easyvm
- 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
- 流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
猜你喜欢

Prepare for the Blue Bridge Cup Day10__ PWM control light brightness

S7-1500PLC仿真

2022/6/30学习总结

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%

陈珙:微服务,它还那么纯粹吗?

深入理解 grpc part1

Why must we move from Devops to bizdevops?

Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million

妙啊!MarkBERT

Introduction to unittest framework and the first demo
随机推荐
ES6 Promise用法小结
Vscode shortcut key (the most complete) [easy to understand]
Is it safe for Huatai Securities to open an account online?
2022/6/28学习总结
Emotion analysis based on IMDB comment data set
Redis的攻击手法
Neo4j 中文开发者月刊 - 202206期
MySQL in and not in() empty list error
CPI tutorial - asynchronous interface creation and use
Value/list in redis
Test case writing specification in unittest framework and how to run test cases
Redis' attack tactics
[classic example] classic list questions @ list
Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
epoll介绍
Redis configuration environment variables
Acly and metabolic diseases
Learning summary on June 29, 2022
邻接矩阵无向图(一) - 基本概念与C语言
Activity workflow engine