题目链接: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]的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机

    题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...

  6. 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的长 ...

  7. Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律

    题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...

  8. Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流

    题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...

  9. Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖

    题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...

随机推荐

  1. webpack 配置缓存

    1.输出文件的文件名 加hash 2.提取引导模板 3.模块标识符 https://webpack.docschina.org/guides/caching/#src/components/Sideb ...

  2. 词向量可视化--[tensorflow , python]

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ ---------------------------------- ...

  3. weak_ptr<T>智能指针

    weak_ptr是为配合shared_ptr而引入的一种智能指针,它更像是shared_ptr的一个助手,而不是智能指针,因为它不具有普通指针的行为,没有重载operator*和operator-&g ...

  4. ip代理优化

    如何保证可用ip不低于2000个,代理ip池优化策略 第一:获得大量ip: 第二:验证可用ip: 第三:监控可用ip: 第三:保证可用ip不低于3000或者5000: 截图是实时可用ip数量 心得:不 ...

  5. 当 return 遇到 try

    . . . . . 今天有同事和我探讨在群里看到的一道有趣的题目,在探讨的过程中让我搞清楚了一些曾经模糊的概念,特此记录下来. 题目给出如下代码,问运行后打印的结果是什么. public static ...

  6. C# SMTP 邮件发送之QQ邮箱篇

    邮件发送大家都已经非常熟悉了,微软自带的System.Net.Mail也很好用,那为什么还要说呢? QQ邮箱的SMTP以前是非SSL,用未加密的25端口,后来发送都改成SSL了,端口为465或587( ...

  7. C语言 结构体中的零长度数组

    /* C语言零长度数组大小和取值问题 */ #include <stdio.h> #include <stdlib.h> #include <string.h> s ...

  8. 【转】WPF Template模版之DataTemplate与ControlTemplate的关系和应用(二)

    1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它 ...

  9. (笔记)ubuntu下安装jdk

    注:此文章转自“http://www.cnblogs.com/a2211009/p/4265225.html”,本人使用的是第二种方式. ubuntu 安装jdk 的两种方式: 1:通过ppa(源) ...

  10. git报错You are not allowed to force push code to a protected branch on this project

    当我们有时候回滚了代码,想强制push到远程仓库的时候, git push origin --force 会报如下错误: You are not allowed to force push code ...