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;
}