Problem J. Prime Game (贡献思想)

Problem J. Prime Game
Input file: standard input
Output file: standard output
Given a suqence of n integers ai
.
Let mul(l, r) = ∏r
i=l
ai and fac(l, r) be the number of distinct prime factors of mul(l, r).
Please calculate ∑n
i=1
∑n
j=i
fac(i, j)
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 standard output
10
99 62 10 47 53 9 83 33 15 24
248
10
6 7 5 5 4 9 9 1 8 12
134

弱鸡,看了大佬的模板敲得,大佬题解链接

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M = 1e6 + 5;
int vis[M];
LL ans = 0;
vector<int> v[M];
unordered_map<int, int> ma;
int a[M], n;
void prime() {
    memset(vis, 0, sizeof(vis));
    for (int i = 2; i < M; i++) {
        if (!vis[i]) {
            v[i].push_back(i);
            for (int j = 2 * i; j < M; j += i) {
                vis[j] = 1;
                v[j].push_back(i);
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    prime();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        if (a[i] == 1) continue;
        int len = v[a[i]].size();
        for (int j = 0; j < len; j++) {
            int now = v[a[i]][j];
            ans += (LL)(n + 1 - i) * (i - ma[now]);
            ma[now] = i;
        }
    }
    cout << ans << '\n';
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值