CA Loves GCD

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5656

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

Hint

题意

给n个数,然后你可以选择若干个数出来,然后求他的gcd

然后现在让你遍历所有的方案,问你所有方案的和是多少

题解:

dp[i][j]表示现在选了i个数,gcd为j的方案数

dp[i][j]->dp[i+1][j],dp[i+1][gcd(a[i+1],j)]

然后不停转移就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1050;
const int mod = 100000007;
int dp[maxn][maxn],a[maxn],n;
long long ans;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
void solve()
{
memset(dp,0,sizeof(dp));dp[0][0]=1;ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=0;i<n;i++)
for(int j=0;j<maxn;j++)if(dp[i][j])
{
(dp[i+1][j]+=dp[i][j])%=mod;
(dp[i+1][gcd(j,a[i+1])]+=dp[i][j])%=mod;
}
for(int i=1;i<maxn;i++) (ans+=1ll*i*dp[n][i]%mod)%=mod;
printf("%d\n",ans);
}
int main()
{
int t;scanf("%d",&t);
while(t--)solve();
}

HDU 5656 CA Loves GCD dp的更多相关文章

  1. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  2. HDU 5656 CA Loves GCD (数论DP)

    CA Loves GCD 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/B Description CA is a fine c ...

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

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  4. HDU 5656 CA Loves GCD 01背包+gcd

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...

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

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  6. hdu 5656 CA Loves GCD

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  7. hdu 5656 CA Loves GCD(dp)

    题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...

  8. HDU 5656 CA Loves GCD (容斥)

    题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...

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

    题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Ot ...

随机推荐

  1. bootstrap分页查询传递中文参数到后台(get方式提交)

    <!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...

  2. Java Spring boot 企业微信点餐系统

    欢迎关注我的微信公众号:"Java面试通关手册" 回复关键字" springboot "免费领取(一个有温度的微信公众号,期待与你共同进步~~~坚持原创,分享美 ...

  3. oracle查看表中数据的大小

    通过从视图 user_segments的字段 bytes中找到 select SUM(bytes)/1024/1024 from user_segments where segment_name='E ...

  4. 微信access_token和refresh_token保存于redis

    简介 通常理解的access_token和refresh_token access_token是用来对客户端进行认证的,类似与密码,有一定的有效期.当过期后可使用refresh_token重新获取一个 ...

  5. Linux shell中运行命令后加上字符“&”的作用(转)

    原文链接为:http://blog.sina.com.cn/s/blog_963453200102uya7.html & 放在启动参数后面表示设置此进程为后台进程 默认情况下,进程是前台进程, ...

  6. 垃圾回收算法与 JVM 垃圾回收器综述(转)

    垃圾回收算法与 JVM 垃圾回收器综述 我们常说的垃圾回收算法可以分为两部分:对象的查找算法与真正的回收方法.不同回收器的实现细节各有不同,但总的来说基本所有的回收器都会关注如下两个方面:找出所有的存 ...

  7. 使用coding云作为git远程库

    1.在命令行中创建GIT仓库 mkdir DriveAssistant cd DriveAssistant git init echo "# DriveAssistant" > ...

  8. Porting of cURL to Android OS using NDK (from The Software Rogue)

    Porting of cURL to Android OS using NDK   In continuing my journey into Android territory, I decided ...

  9. git的一些配置

    git使用socks代理加速 原来git可以配置socks代理的,真好,从github上clone了opencv的代码,基本上是满速了. 首先需要配置shadowsocks,然后通过GUI客户端(或命 ...

  10. 洛谷 P1184高手之在一起 题解

    题目传送门 那位高手是谁啊?@jxpxcsh  QWQ. 这道题数据特别水,所以直接使用O(n*m),每读进一个m内的字符串,就扫一遍n的字符串.但注意地点字符串中有可能会有空格,所以这时候就要请出g ...