Gym 101981J - Prime Game - [数学题][线性筛+分解质因数][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem J]
题目链接:http://codeforces.com/gym/101981/attachments
题意:
令 $mul(l,r) = \prod_{i=l}^{r}a_i$,且 $fac(l,r)$ 代表 $mul(l,r)$ 的不同素因子个数。求 $\sum_{i=1}^{n}\sum_{j=i}^{n}fac(i,j)$。
Input
The first line contains one integer n (1 \le n \le 10^6) — the length of the sequence.
The second line contains n integers ai (1 \le i \le n, 1 \le a_i \le 10^6) — the sequence.
Output
Print the answer to the equation.
Examples
standard input
10
99 62 10 47 53 9 83 33 15 24
standard output
248
standard input
10
6 7 5 5 4 9 9 1 8 12
standard output
134
题解:
考虑每个质因子对于整体答案的贡献。
拿第二组样例算一算就不难发现:第 $p$ 个位置上的数,其包含的任意一个素因子,它原本应当产生的贡献有 $(n-p+1) \cdot p$,
但是考虑到若其前面出现过一样的素数,那么应当减去一些重复计算的区间。假设它前面的和它一样的素数,最后一次出现在 $q$ 位置,那么就应当减去 $(n-p+1) \cdot q$,即 $a[p]$ 包含的任意一个质因子其产生的贡献为 $(n-p+1) \cdot p - (n-p+1) \cdot q = (n-p+1) \cdot (p - q)$。
不妨用 $pos[i][k]$ 来存储每个素因子的 “$p$”,$pos[i][k-1]$ 存储每个素因子的 “$q$”。换句话说,$pos[i][k]$ 代表某个素因子 $i$ 在 $a[1 \sim n]$ 中第 $k$ 次“出现”的位置是 $pos[i][k]$;特别地,令 $pos[i][0]=0$。那么对于任意素因子 $i$,它对答案的贡献是 $(n-pos[i][k]+1) \cdot (pos[i][k]-pos[i][k-1])$。
我们可以对 $a[1 \sim n]$ 分解质因数,然后更新相应的 $pos[i][k]$。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+; int n,a[maxn];
vector<int> pos[maxn];
void dec(int p)
{
int n=a[p];
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
pos[i].push_back(p);
while(n%i==) n/=i;
}
}
if(n>) pos[n].push_back(p);
}
int main()
{
for(int i=;i<maxn;i++) pos[i].push_back(); scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dec(i);
} ll ans=;
for(int i=;i<maxn;i++)
{
for(int k=;k<pos[i].size();k++)
ans+=(ll)(n-pos[i][k]+)*(pos[i][k]-pos[i][k-]);
}
cout<<ans<<endl;
}
分解质因数板子:
vector<int> dec(int n)
{
vector<int> p;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
p.push_back(i);
while(n%i==) n/=i;
}
}
if(n>) p.push_back(n);
return p;
}
交了一发,大概700ms多点过了,那我们能否加快一下速度呢?
我们可以先用线性筛筛出 $[1,1e6]$ 的素数,然后再做分解质因数:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+; int n,a[maxn];
vector<int> pos[maxn]; const int MAX=1e6;
int tot,prime[MAX/];
bool isPrime[MAX+];
void Screen() //欧拉筛
{
tot=;
memset(isPrime,,sizeof(isPrime));
isPrime[]=isPrime[]=;
for(int i=;i<=MAX;i++)
{
if(isPrime[i]) prime[tot++]=i;
for(int j=;j<tot;j++)
{
if(i*prime[j]>MAX) break;
isPrime[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
} void dec(int p)
{
int n=a[p];
for(int i=;i<tot && prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
pos[prime[i]].push_back(p);
while(n%prime[i]==) n/=prime[i];
}
}
if(n>) pos[n].push_back(p);
}
int main()
{
Screen();
for(int i=;i<tot;i++) pos[prime[i]].push_back(); scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dec(i);
} ll ans=;
for(int i=;i<tot;i++)
{
for(int k=;k<pos[prime[i]].size();k++)
ans+=(ll)(n-pos[prime[i]][k]+)*(pos[prime[i]][k]-pos[prime[i]][k-]);
}
printf("%I64d\n",ans);
}
跑了大概400ms,还是快了不少的。
Gym 101981J - Prime Game - [数学题][线性筛+分解质因数][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem J]的更多相关文章
- Gym - 101981J The 2018 ICPC Asia Nanjing Regional Contest J.Prime Game 计数
题面 题意:1e6的数组(1<a[i]<1e6), mul (l,r) =l × (l+1) ×...× r, fac(l,r) 代表 mul(l,r) 中不同素因子的个数,求s ...
- Gym 101981G - Pyramid - [打表找规律][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem G]
题目链接:http://codeforces.com/gym/101981/attachments The use of the triangle in the New Age practices s ...
- Gym 101981I - Magic Potion - [最大流][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem I]
题目链接:http://codeforces.com/gym/101981/attachments There are n heroes and m monsters living in an isl ...
- Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]
题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...
- Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机
题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...
- Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP
题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...
- Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律
题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...
- Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流
题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...
- Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖
题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...
随机推荐
- 1154:LETTERS
题目链接http://bailian.openjudge.cn/practice/1154/ 总时间限制: 1000ms 内存限制: 65536kB 描述 A single-player game i ...
- DEVOPS落地实践分享
DEVOPS落地实践分享 转载本文需注明出处:微信公众号EAWorld,违者必究. 引言: DevOps的理念已经说了很多年,其带来的价值逐渐被接受,很多企业也逐渐引入了DevOps.目前普元DevO ...
- ios the request was denied by service delegate for reason unspecified
报错的情况如下: xcode8(The request was denied by service delegate (SBMainWorkspace) for reason: Unspecified ...
- Dubbo 分布式服务框架简介
1.分布式服务框架 1.1 Dubbo 简介 Dubbo 是一个分布式服务框架,以及阿里巴巴内部的 SOA 服务化治理方案的核心框架.其功能主要包括:高性能 NIO 通讯及多协议集成,服务动态寻址与路 ...
- 使用PHPExcel实现Excel文件的导入和导出(模板导出)
在之前有写过一篇文章讲述了使用PHP快速生成excel表格文件并下载,这种方式生成Excel文件,生成速度很快,但是有缺点是:1.单纯的生成Excel文件,生成的文件没有样式,单元格属性(填充色,宽度 ...
- [ci]容器ci索引
伙计们: 有任何意见或建议或看不懂的请在对应的文章下留言(请注明上下文) 我会及时改动. 这是以前的一些在物理机上搞过 [ci]容器ci索引 http://www.cnblogs.com/iiiihe ...
- Unable to find IPv4-only network bridge for LXD.
https://github.com/conjure-up/conjure-up/issues/1440It seems like the the installation is complainin ...
- Mysql 索引问题-日期索引使用
这两天发现原来的查询效率慢了,使用explain 查看,居然没有使用索引,我的索引是日期类型的,首先想到的是mysql对日期类型的索引的处理机制是不是不同,在where条件里试了几种,发现效果都差不多 ...
- vim简单的移动光标
vim学习简单的光标移动 ==> 移动到行首 $ ==> 移动到行尾 ^ ==> 移动到本行第一个不是blank的位置 g_ ==> 移动到本行最后一个不是blank的位置^Z ...
- bash python获取文本中每个字符出现的次数
bash: grep -o . myfile | sort |uniq -c python: 使用collections模块 import pprint import collections f = ...