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
BestCoder Round #78 (div.2) 

题意:给一个集合,求所有子集的最大公约数之和。
分析:暴力方法;枚举所有可能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>


你可能感兴趣的:(bc,gcd)