HDU 5656 ——CA Loves GCD——————【dp】

CA Loves GCD

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


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
 
 
题目大意:首先吐槽一下这题的题目描述!明明说的是不同的n个数,结果测试发现有重复!!!大坑!让你求n个数所有组合的gcd的和。
 
解题思路:定义dp[i] 表示gcd为i的组合数有多少。对于a[i],我们从前面的dp值转移过来就行了。
 
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e4 + 30;
const LL INF = 0x3f3f3f3f;
const int mod = 1e8+7;
int a[maxn], dp[maxn];
int GCD(int a, int b){
    return b == 0? a: GCD(b,a%b);
}
int main(){
//    freopen("INPUT.txt","r",stdin);
//    freopen("my.txt","w",stdout);
    int T, n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int Max = 1;
        for(int i = 1; i <= n; ++i){
            scanf("%d",&a[i]);
            Max = max(Max, a[i]);
        }
        sort(a+1,a+1+n);
        int flag = 0;
        for(int i = 2; i <= n; i++){
            if(a[i] == a[i-1]){
                flag = 1; break;
            }
        }
        if(flag) while(1){}
        memset(dp,0,sizeof(dp));
        if(flag == 1){    //这种处理不需要数不相同的条件 
            dp[a[1]] = 1;  
            for(int i = 2; i <= n; ++i){
                for(int j = 1; j <= 1000; ++j){
                    if(dp[j] == 0) continue;
                    int gcd = GCD(j,a[i]);
    //                printf("%d %d...
",gcd,dp[gcd]);
                    dp[gcd] = (dp[gcd] + dp[j]) % mod;
                }
                dp[a[i]]++;
            }
        }else{ //题目说得不重复,自己最开始的做法
            for(int i = 1; i <= n; ++i){
                dp[a[i]]++;
                for(int j = 1; j < a[i]; ++j){
                    if(dp[j] == 0) continue;
                    int gcd = GCD(j,a[i]);
    //                printf("%d %d...
",gcd,dp[gcd]);
                    dp[gcd] = (dp[gcd] + dp[j]) % mod;
                }
            }
        }
        LL res = 0;
        for(int i = 1; i <= Max; ++i){
            if(dp[i])
            res = (res + (LL)dp[i]*(LL)i) % mod;
//           printf("%d %d+++
",i,dp[i]);
        }
        printf("%I64d
",res);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/chengsheng/p/5503285.html