CA Loves GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 61 Accepted Submission(s): 14
Problem Description
CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too.
Now, there are N different numbers. Each time, CA will select several numbers (at least one), and find the GCD of these numbers. In order to have fun, CA will try every selection. After that, she wants to know the sum of all GCDs.
If and only if there is a number exists in a selection, but does not exist in another one, we think these two selections are different from each other.
Now, there are N different numbers. Each time, CA will select several numbers (at least one), and find the GCD of these numbers. In order to have fun, CA will try every selection. After that, she wants to know the sum of all GCDs.
If and only if there is a number exists in a selection, but does not exist in another one, we think these two selections are different from each other.
Input
First line contains
T
denoting the number of testcases.
T testcases follow. Each testcase contains a integer in the first time, denoting N , the number of the numbers CA have. The second line is N numbers.
We guarantee that all numbers in the test are in the range [1,1000].
1≤T≤50
T testcases follow. Each testcase contains a integer in the first time, denoting N , the number of the numbers CA have. The second line is N numbers.
We guarantee that all numbers in the test are in the range [1,1000].
1≤T≤50
Output
T
lines, each line prints the sum of GCDs mod
100000007
.
Sample Input
2 2 2 4 3 1 2 3
Sample Output
8 10
题意:求一个数组所有与的子集的gcd之和.
F(x)表示gcd为x的倍数的子集个数,f(x)表示gcd为x的子集个数,这两个函数满足莫比乌斯反演的条件,
答案就是1*f(1)+2*f(2)+...+Max_v*f(Max_v).
复杂度O(n^2).
#include <bits/stdc++.h>
using namespace std;
#define maxn 1111
long long prime[maxn], cnt;
bool vis[maxn];
int mu[maxn];
int n;
int a[maxn];
void get_mu () {
cnt = 0;
memset (vis, 0, sizeof vis);
mu[1] = 1;
for (long long i = 2; i <= 1000; i++) {
if (!vis[i]) {
prime[cnt++] = i;
mu[i] = -1;
}
for (long long j = 0; j < cnt; j++) {
if (i*prime[j] > 1000)
break;
vis[i*prime[j]] = 1;
if (i%prime[j] == 0) {
mu[i*prime[j]] = 0;
break;
}
else {
mu[i*prime[j]] = -mu[i];
}
}
}
}
int fac[maxn];
#define mod 100000007
#define pow Pow
long long pow[maxn];
long long F (int x) {
return pow[fac[x]]-1;
}
int main () {
//freopen ("in.txt", "r", stdin);
get_mu ();
pow[0] = 1;
for (int i = 1; i <= 1000; i++) {
pow[i] = pow[i-1]*2%mod;
}
int t;
scanf ("%d", &t);
while (t--) {
int Max = 0;
scanf ("%d", &n);
memset (fac, 0, sizeof fac);
for (int i = 0; i < n; i++) {
scanf ("%d", &a[i]);
Max = max (Max, a[i]);
for (int j = 1; j*j <= a[i]; j++) {
if (a[i]%j == 0) {
fac[j]++;
if (a[i]/j != j)
fac[a[i]/j]++;
}
}
}
long long ans = 0;
for (long long i = 1; i <= Max; i++) {
long long cur = 0;
for (int j = i, k = 1; j <= Max; j += i, k++) {
cur += F(j)*mu[k]%mod;
if (cur < 0) {
cur = (cur+mod)%mod;
}
}
ans = (ans+cur*i)%mod;
if (ans < 0)
ans = (ans+mod)%mod;
}
cout << ans << endl;
}
return 0;
}