hdu 5656 CA Loves GCD

参考题解: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. 
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]. 
1T501≤T≤50Output TT lines, each line prints the sum of GCDs mod  100000007100000007.Sample Input
2
2
2 4
3
1 2 3
Sample 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为方案数*公约数得值*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值