HDU5656 CA Loves GCD (BC)

CA Loves GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 19    Accepted Submission(s): 7


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.
 

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].
1T50
 

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
 

Source

题意:给一个集合,求所有子集的最大公约数之和。
分析:暴力方法;枚举所有可能gcd数,出现一次就加1。

<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 100000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define MAXN 100010

int main()
{
    int T,n;
    int a[1010];
    ll vis[2010];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        CL(vis, 0);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        ll ans = 0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=1000; j++)///j为最大公约数
            {
                if(vis[j])
                {
                    int t = __gcd(a[i], j);
                    vis[t]=(vis[t]+vis[j])%MOD;
                    //cout<<vis[__gcd(a[i], j)]<<" ";
                }
            }
            vis[a[i]]++;
        }
        for(int i=1; i<=1000; i++)
            ans = (ans+vis[i]*i%MOD)%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}
</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值