description
Did you hear about the Millennium Prize Problems? They are seven problems in mathematics that were stated by the Clay Mathematics Institute on May 24, 2000. A correct solution for any of these problems results in a million dollar prize being awarded by the institute to the discoverer(s).
Soliman, one of the ECPC judges came up with a new mathematics problem and claimed that it should be the eighth Millennium Prize Problem as he is pretty sure no one could solve it. Here’s the problem:
Given an array A of length N (the array can have duplicate values). What is the sum of all least common multiple (LCM) values for every pair of the array? More formally you need to calculate the following:
Tefa, one of the ECPC judges suggested that we can add it in the problem set to see if someone can come up with a solution, so can you do it?
Input
The first line of the input contains a single integer T the number of test cases, each test case consists of one line containing N+1 space-separated integers, the first integer is the number N the size of the array and the remaining integers are the elements of the array A, where 1≤N≤105 and 1≤Ai≤105.
Output
For each test case output the answer to the problem modulo 109+7.
Example
Input
2
4 2 6 12 15
5 2 3 7 11 12
Output
335
861
分析
好久沒做莫比烏斯函數題了,來一發hhh
這題也許算比較經典?
其實就是要枚舉值域來求。
記值域上界爲 , 爲 出現的次數
然後枚舉 ,然後枚舉 的倍數,有
然後枚舉 ,有
推式子推到這裏做法已經出來了。
預處理
即可
複雜度是
代碼如下
#include <bits/stdc++.h>
#define LL long long
#define N 100005
using namespace std;
const int mod = 1e9 + 7;
int cnt[N], p[N], mu[N], x[N], g[N], s[N], ret, maxn = N - 5;
LL t, z = 1;
int main(){
freopen("lcm.in", "r", stdin);
int i, j, d, n, m, T, tmp, ans;
mu[1] = 1;
for(i = 2; i <= maxn; i++){
if(!x[i]) p[++ret] = x[i] = i, mu[i] = -1;
for(j = 1; j <= ret; j++){
t = z * i * p[j];
if(t > maxn) break;
x[t] = p[j];
if(i % p[j] == 0) break;
mu[t] = -mu[i];
}
}
scanf("%d", &T);
while(T--){
ans = 0;
memset(cnt, 0, sizeof(cnt));
memset(g, 0, sizeof(g));
memset(s, 0, sizeof(s));
scanf("%d", &n);
for(i = 1; i <= n; i++){
scanf("%d", &j);
cnt[j]++;
}
n = maxn;
//for(i = 1; i <= n; i++) printf("%d %d=====\n", i, cnt[i]);
for(i = 1; i <= n; i++){
for(j = 1; j <= n / i; j++){
g[i] = (g[i] + z * cnt[i * j] * j % mod) % mod;
}
g[i] = z * g[i] * g[i] % mod;
//printf("%d %d\n", i, g[i]);
}
for(d = 1; d <= n; d++){
tmp = 0;
for(i = 1; i <= n / d; i++){
tmp = (tmp + z * i * i % mod * mu[i] * g[(d * i)] % mod) % mod;
}
tmp = (tmp + mod) % mod;
ans = (ans + z * d * tmp % mod) % mod;
}
printf("%d\n", ans);
}
return 0;
}