HDU5656 CA Loves GCDGCD+枚举

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5656 CA Loves GCDGCD+枚举相关的知识,希望对你有一定的参考价值。

CA Loves GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2801 Accepted Submission(s): 982

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

Source
BestCoder Round #78 (div.2)

问题链接HDU5656 CA Loves GCD
问题简述:(略)
问题分析:GCD问题,这里用枚举法来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU5656 CA Loves GCD */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MOD = 100000007;
const int N = 1000;
int a[N + 1];
LL cnt[N + N + 1];

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);

        memset(cnt, 0, sizeof cnt);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= N; j++)
                if (cnt[j]) {
                    int t = __gcd(a[i], j);
                    cnt[t] = (cnt[t] + cnt[j]) % MOD;
                }
            cnt[a[i]]++;
        }
        LL ans = 0;
        for (int i = 0; i <= N; i++)
            ans = (ans + cnt[i] * i % MOD) % MOD;

        printf("%lld\\n", ans);
    }

    return 0;
}

以上是关于HDU5656 CA Loves GCDGCD+枚举的主要内容,如果未能解决你的问题,请参考以下文章

hdu 5656 CA Loves GCD dp

hdu-5656 CA Loves GCD(dp+数论)

hdu 5656 CA Loves GCD(dp)

数学(GCD,计数原理)HDU 5656 CA Loves GCD

CA Loves GCD (BC#78 1002) (hdu 5656)

hdu 5655 CA Loves Stick