当前位置:网站首页>区间乘积的因子数之和——前缀和思想+定一移二
区间乘积的因子数之和——前缀和思想+定一移二
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)
边栏推荐
- Talk about the pessimistic strategy that triggers full GC?
- Want to ask, is there a discount for opening a securities account? Is it safe to open a mobile account?
- Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%
- TMUX usage
- 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%
- 二叉堆(一) - 原理与C实现
- 我在中山,到哪里开户比较好?实际上网上开户安全么?
- IPlImage的width和widthStep
- [classic example] classic list questions @ list
- 241. 为运算表达式设计优先级 : DFS 运用题
猜你喜欢

Learning summary on June 30, 2022

On recursion and Fibonacci sequence

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

Redis' attack tactics

Introduction to unittest framework and the first demo

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

研发效能度量框架解读

Acly and metabolic diseases

Matrix of numpy

深入理解 grpc part1
随机推荐
S7-1500PLC仿真
证券账户销户后果 开户安全吗
耐克如何常年霸榜第一名?最新財報答案來了
ABBIRB120工业机器人机械零点位置
分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
Kafuka learning path (I) Kafuka installation and simple use
Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
MQ-防止消息丢失及重复消费
Are the consequences of securities account cancellation safe
redis中value/hush
Value/set in redis
sshd_ Discussion on permitrotlogin in config
No statements may be issued when any streaming result sets are open and in use on a given connection
Theoretical basis of graph
Wonderful! MarkBERT
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%
Value/hush in redis
基于IMDB评论数据集的情感分析
Redis启动与库进入
[classic example] classic list questions @ list