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个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...
随机推荐
- Device does not seem to be present [常见错误解决]
一.故障现象: [root@c1node01 ~]# service network restart Shutting down loopback insterface: ...
- 在IIS7上导出所有应用程序池的方法批量域名绑定(网站绑定)
资料来源: http://www.2cto.com/os/201410/341882.html 一点经验:导出配置文件后,建议打开看看,如果有需要调整的需要做修改. 在IIS7+上导出所有应用程序池的 ...
- Java编程的逻辑 (92) - 函数式数据处理 (上)
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- VirtualBox通过Host-Only网络连接方式实现宿主机与虚拟机通信
适用情况 (1)没有联网, 不插网线 (2)宿主机直接连接宽带(无路由器) 情景: 宿主机 Windows 7 虚拟机 Windows XP 虚拟机安装了SQLServer2005,宿主机想连接使用虚 ...
- mybatis打印完整的sql
mybatis log plugin
- List接口:(介绍其下的两个实现类:ArrayList和LinkedList)
以下介绍接口: List接口:(介绍其下的两个实现类:ArrayList和LinkedList) ArrayList和数组非常类似,其底层①也用数组组织数据,ArrayList是动态可变数组. ① ...
- Apple Watch S3 解锁 MacBook Pro 2015版失败的解决办法
我的MacBook Pro MF839由于只有128G的内存,所以就只能藏在我的抽屉底下,偶尔想体验一下xcode的时候再拿回来用下,想想都浪费 也不是不想换SSD,只是看了一下,价格太贵了,256G ...
- VSCode之快捷键和常用插件
前言 介绍一下我在VSCode中常用的一些快捷方式: ctrl+上下箭头 上下滚动页面 Ctrl+Shift+K 删除某一行 Alt+ ↑ / ↓ 移动某一行 Shift+Alt + ↓ / ↑ 复制 ...
- iOS 定时器 NSTimer、CADisplayLink、GCD3种方式的实现
在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 然而,在iOS中有很多方法完成以上的任务,到底有多少种方法呢?经过查阅资 ...
- maven项目中使用redis集群报错: java.lang.NumberFormatException: For input string: "7001@17001"
解决:由于redis集群的采用的版本是2.7的,在maven的pom.xml中将jedis的版本改成2.9的就可以了