参考题解:https://blog.csdn.net/xym_CSDN/article/details/51052074
CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too.
Now, there are NN 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.
InputFirst line contains
TT denoting the number of testcases.
Now, there are NN 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.
TT testcases follow. Each testcase contains a integer in the first time, denoting NN, the number of the numbers CA have. The second line is NN numbers.
We guarantee that all numbers in the test are in the range [1,1000].
1≤T≤501≤T≤50Output TT lines, each line prints the sum of GCDs mod 100000007100000007.Sample Input
2 2 2 4 3 1 2 3Sample Output
8 10
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
const int mod=100000007;
int n;
LL pow[1001];
LL g[1005];
LL c[1005];
int a[1005];
int num[1005];
void init()
{
pow[0]=1;
for(int i=1;i<=1000;++i)
{
pow[i]=(pow[i-1]<<1)%mod;
}
}
void M()
{
memset(g,0,sizeof(g));
memset(c,0,sizeof(c));
memset(num,0,sizeof(num));
}
int main()
{
int t;
cin>>t;
init();
while(t--)
{
M();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=1000;j++)
{
if(a[i]%j==0)
{
num[j]++;
}
}
}
for(int i=1;i<=1000;i++)
{
c[i]=pow[num[i]]-1;
}
for(int i=1000;i>=1;i--)
{
g[i]=c[i];
for(int j=2;j*i<=1000;j++)
{
g[i]=(g[i]-g[j*i]+mod)%mod;
}
}
LL ans=0;
for(int i=1;i<=1000;i++)
{
ans=(ans+(g[i]*i)%mod)%mod;
}
cout<<ans<<endl;
}
}
/*数据范围1-1000
因此公约数范围1-1000
计算出每个公约数的方案数(非空子集的个数)
利用容斥原理得到每个公约数的方案数
最终的ans为方案数*公约数得值*/