当前位置:网站首页>Sum of factor numbers of interval product
Sum of factor numbers of interval product
2022-06-28 17:03:00 【solego】
The question :
Given a length of n n n Array of a a a, Each number in the array is not greater than 3 3 3 The positive integer . It is known that the weight of an interval is the number of factors of the product of all numbers in the interval , Find the sum of the weights of all intervals . The answer is right 1 e 9 + 7 1e9+7 1e9+7 modulus .
Data range :
1 ≤ n ≤ 1 0 5 1\leq n\leq 10^5 1≤n≤105
1 ≤ a i ≤ 3 1\leq a_i\leq 3 1≤ai≤3
Answer key :
a number x x x The number of factors for is :
take x x x Split into the product form of all prime factors , Count the number of each germplasm factor , The first i i i A quality factor p i p_i pi The quantity of is c n t p i cnt_{p_i} cntpi
So count x x x The number of factors for is : ∏ i = 1 ( c n t p i + 1 ) \prod\limits_{i=1} (cnt_{p_i}+1) i=1∏(cntpi+1)
p r e 2 i = ∑ j = 1 i [ a j = 2 ] pre2_i = \sum\limits_{j=1}^i [a_j=2] pre2i=j=1∑i[aj=2] , p r e 3 i = ∑ j = 1 i [ a j = 3 ] pre3_i = \sum\limits_{j=1}^i [a_j=3] pre3i=j=1∑i[aj=3]
p p r e 2 i = ∑ j = 1 i p r e 2 j ppre2_i = \sum\limits_{j=1}^i pre2_j ppre2i=j=1∑ipre2j, p p r e 3 i = ∑ j = 1 i p r e 3 j ppre3_i=\sum\limits_{j=1}^i pre3_j ppre3i=j=1∑ipre3j
m u l 2 3 i = ∑ j = 1 i p r e 2 j × p r e 3 j mul23_i = \sum\limits _{j=1}^{i}pre2_j\times pre3_j mul23i=j=1∑ipre2j×pre3j
For the interval weight in the topic , Equivalent to ( p r e 2 + 1 ) × ( p r e 3 + 1 ) (pre2+1)\times (pre3+1) (pre2+1)×(pre3+1).
So the question is : ∑ r = 1 n ∑ l = 1 r ( p r e 2 r − p r e 2 l − 1 + 1 ) × ( p r e 3 r − p r e 3 l − 1 + 1 ) \sum\limits_{r=1}^{n}\sum\limits_{l=1}^{r} (pre2_r-pre2_{l-1}+1)\times (pre3_r-pre3_{l-1}+1) r=1∑nl=1∑r(pre2r−pre2l−1+1)×(pre3r−pre3l−1+1)
Simplification :
( p r e 2 r − p r e 2 l − 1 + 1 ) × ( p r e 3 r − p r e 3 l − 1 + 1 ) = p r e 2 r × p r e 3 r − p r e 2 r × p r e 3 l − 1 + p r e 2 r − p r e 2 l − 1 × p r e 3 r + p r e 2 l − 1 × p r e 3 l − 1 − p r e 2 l − 1 + p r e 3 r − p r e 3 l − 1 + 1 = ( p r e 2 r × p r e 3 r + p r e 2 r + p r e 3 r + 1 ) ① + ( p r e 2 l − 1 × p r e 3 l − 1 ) ② − ( p r e 2 l − 1 × ( p r e 3 r + 1 ) + p r e 3 l − 1 × ( p r e 2 r + 1 ) ) ③ (pre2_r-pre2_{l-1}+1)\times (pre3_r-pre3_{l-1}+1) \\ =pre2_r\times pre3_r - pre2_r\times pre3_{l-1}+pre2_r-pre2_{l-1}\times pre3_r+pre2_{l-1}\times pre3_{l-1}-pre2_{l-1}+ pre3_r-pre3_{l-1}+1 \\ = (pre2_r\times pre3_r+pre2_r+pre3_r+1)_{①}+(pre2_{l-1}\times pre3_{l-1})_{②}-(pre2_{l-1}\times (pre3_r+1)+pre3_{l-1}\times (pre2_r+1))_{③} (pre2r−pre2l−1+1)×(pre3r−pre3l−1+1)=pre2r×pre3r−pre2r×pre3l−1+pre2r−pre2l−1×pre3r+pre2l−1×pre3l−1−pre2l−1+pre3r−pre3l−1+1=(pre2r×pre3r+pre2r+pre3r+1)①+(pre2l−1×pre3l−1)②−(pre2l−1×(pre3r+1)+pre3l−1×(pre2r+1))③
Sum the above three expressions respectively :
① ∑ r = 1 n ∑ l = 1 r ( p r e 2 r × p r e 3 r + p r e 2 r + p r e 3 r + 1 ) = ∑ r = 1 n r × ( p r e 2 r × p r e 3 r + p r e 2 r + p r e 3 r + 1 ) \sum\limits_{r=1}^{n}\sum\limits_{l=1}^{r} (pre2_r\times pre3_r+pre2_r+pre3_r+1)\\ =\sum\limits_{r=1}^{n} r\times (pre2_r\times pre3_r+pre2_r+pre3_r+1) r=1∑nl=1∑r(pre2r×pre3r+pre2r+pre3r+1)=r=1∑nr×(pre2r×pre3r+pre2r+pre3r+1)
② ∑ r = 1 n ∑ l = 1 r ( p r e 2 l − 1 × p r e 3 l − 1 ) = ∑ r = 1 n m u l 2 3 r − 1 \sum\limits_{r=1}^{n}\sum\limits_{l=1}^{r} (pre2_{l-1}\times pre3_{l-1}) \\ = \sum\limits_{r=1}^{n}mul23_{r-1} r=1∑nl=1∑r(pre2l−1×pre3l−1)=r=1∑nmul23r−1
③ ∑ r = 1 n ∑ l = 1 r − ( p r e 2 l − 1 × ( p r e 3 r + 1 ) + p r e 3 l − 1 × ( p r e 2 r + 1 ) ) = − ∑ r = 1 n ∑ l = 1 r ( p r e 2 l − 1 × ( p r e 3 r + 1 ) + p r e 3 l − 1 × ( p r e 2 r + 1 ) ) = − ∑ r = 1 n ( p p r e 2 r − 1 × ( p r e 3 r + 1 ) + p p r e 3 r − 1 × ( p r e 2 r + 1 ) ) \sum\limits_{r=1}^{n}\sum\limits_{l=1}^{r} -(pre2_{l-1}\times (pre3_r+1)+pre3_{l-1}\times (pre2_r+1)) \\ =-\sum\limits_{r=1}^{n}\sum\limits_{l=1}^{r} \ (pre2_{l-1}\times (pre3_r+1)+pre3_{l-1}\times (pre2_r+1)) \\ =-\sum\limits_{r=1}^{n} (ppre2_{r-1}\times (pre3_r+1)+ppre3_{r-1}\times (pre2_r+1)) r=1∑nl=1∑r−(pre2l−1×(pre3r+1)+pre3l−1×(pre2r+1))=−r=1∑nl=1∑r (pre2l−1×(pre3r+1)+pre3l−1×(pre2r+1))=−r=1∑n(ppre2r−1×(pre3r+1)+ppre3r−1×(pre2r+1))
Can be in O ( n ) O(n) O(n) Complete in time complexity .
Code :
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 100010;
int a[N], n;
int pre2[N], ppre2[N];
int pre3[N], ppre3[N];
int mul23[N];
void add(int& a, int b) {
a = (a + b) % mod;
if (a < 0) a += mod;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) {
pre2[i] = pre2[i - 1];
pre3[i] = pre3[i - 1];
if (a[i] == 2) {
add(pre2[i], 1);
} else if (a[i] == 3) {
add(pre3[i], 1);
}
ppre2[i] = ppre2[i - 1];
ppre3[i] = ppre3[i - 1];
add(ppre2[i], pre2[i]);
add(ppre3[i], pre3[i]);
mul23[i] = mul23[i - 1];
add(mul23[i], 1ll * pre2[i] * pre3[i] % mod);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
int temp = 0;
add(temp, 1ll * pre2[i] * pre3[i] % mod);
add(temp, pre2[i]);
add(temp, pre3[i]);
add(temp, 1);
add(ans, 1ll * i * temp % mod);
add(ans, -1ll * (1 + pre3[i]) * ppre2[i - 1] % mod);
add(ans, -1ll * (1 + pre2[i]) * ppre3[i - 1] % mod);
add(ans, mul23[i - 1]);
}
printf("%d\n", ans);
return 0;
}
边栏推荐
- Gartner announces five privacy trends from today to 2024
- offsetwidth\clientwidth\scrollwidth
- 如何登录到你的 WordPress 管理仪表板
- Flex布局
- 高并发、高可用、弹性扩展,天翼云护航企业云上业务
- 6 - 字典
- 天翼云Web应用防火墙(边缘云版)通过首批可信认证
- Solve the problem that subcomponents will not be destroyed through setTimeout
- AUTOSAR software development training
- 【TcaplusDB知识库】TcaplusDB限制条件介绍
猜你喜欢

After the first failure, AMEC rushed to the Hong Kong stock exchange for the second time, and the financial principal changed frequently

Cardinality sorting - common sorting method (2/8)

解决sqoop出现 ERROR manager.SqlManager: Generic SqlManager.listDatabases() not implemented

Fs2k face sketch attribute recognition

This simple little function saves 213 hours for our production research team in half a year

PotPlayer播放百度云盘视频

Introduction to LTSpice circuit simulation

抓取手机端变体组合思路设想

大促场景下,如何做好网关高可用防护

中国SSD行业企业势力全景图
随机推荐
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
如何将你的 WordPress 网站置于维护模式
【TcaplusDB】祝大家端午安康!
R 编程语言 - 简介
这个简单的小功能,半年为我们产研团队省下213个小时
MetaQ安装部署文档
大型体育赛事与犯罪风险
Flex布局
[force button] 977 Square of ordered array
Can SQL queries be used in the tablestore to find out all the data in the table?
老司机总结的12条 SQL 优化方案(非常实用)
The intelligent transformation is accelerated, and enterprises need a new toolbox
Noip1998-2018 popularization group csp-j2 2019 2020 problem solving report and video
Noip1998-2018 csp-s2 2019 2021 improvement group problem solving report and video
手机买场内基金开户选哪家证券公司比较好,比较安全呢
这个简单的小功能,半年为我们产研团队省下213个小时
【TcaplusDB知识库】WebClient用户如何读取和修改数据
What you have to know under the digital collection boom
55. maximum sum of continuous subarrays
【TcaplusDB知识库】批量复制游戏区