HDU 5656 CA Loves GCD

CA Loves GCD

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


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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5694  5693  5692  5689  5687 
 

题意:n个数里面任意挑选几个数出来,求剩下的数的gcd之和。因为范围在1000之间,我们可以计算出gcd为1到1000的所有方案数。

令dp[i][j]代表选了前i个数 gcd为j的方案数。选i+1个数转移为dp[i][j]+=dp[i+1][f[j][a[i+1]]] 。不选为dp[i][j]+=dp[i+1][j];其中f[i][j]代表i,j两数的gcd

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
typedef  long long LL;
int f[1001][1001];
int dp[1001][1001];
int a[1001];
const int mod=100000007;
inline void add(int b,int &a)
{
    if((a+=b)>=mod)a-=mod;
}
int main()
{
    int t;
    int i,j;
    for(i=0; i<=1000; i++)
        for(j=0; j<=1000; j++)
            f[i][j]=__gcd(i,j);
    scanf("%d",&t);
    while(t--)
    {
        int n;
        cin>>n;
        memset(a,0,sizeof(a));
        for(i=1; i<=n; i++)
            cin>>a[i];
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(i=0; i<n; i++)
            for(j=0; j<=1000; j++)
            {
                if(dp[i][j])
                    add(dp[i][j],dp[i+1][j]);
                add(dp[i][j],dp[i+1][f[j][a[i+1]]]);
            }
        LL ans=0;
        for(i=1; i<=1000; i++)
            ans+=LL (i)*dp[n][i];
        printf("%lld\n",ans%mod);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值