2018icpc南京 Problem J. Prime Game

在这里插入图片描述

题意
计算出来每个数的质因子在各个区间的贡献
Input
The first line contains one integer n (1 ≤ n ≤ 106) — the length of the sequence.
The second line contains n integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 106) — 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

思路
素数筛 + 一个pre数组表示之前的位置
训练时看懂了题目想到了素数筛(这是必然要用的),后来看到数三角形就去做数三角形了,再后来经人点拨pre数组,想到了这个做法。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
inline void input(int& res) {
    char c = getchar();res = 0;int f = 1;
    while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
    while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
    res = f ? res : -res;
}
int prime[N+5],vis[N+5],a[N+5],pre[N+5];
int Case,n,cnt;
ll ans,l,r;
void Euler() {
    for(int i=2;i<=N;i++) {
        if(vis[i]==0) prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<=N;j++) {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
int main(){
	Euler();
	input(n);
	for(int i=1;i<=n;i++) input(a[i]);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=cnt&&a[i]>=prime[j]*prime[j];j++){
			if(a[i]%prime[j]==0){
				l=i-pre[prime[j]];
				r=n-i+1;
				ans+=l*r;
				pre[prime[j]]=i;
				while(a[i]%prime[j]==0){
					a[i]/=prime[j];
				}
			}
		}
		if(a[i]!=1){
			l=i-pre[a[i]];
			r=n-i+1;
			ans+=l*r;
			pre[a[i]]=i;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值