CA Loves GCD

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

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
 
题意:给你 n 个数 求着n个数不同组合情况下的gcd之和
题解:dp[i][j] 代表 前i个数中的组合 使得gcd为j的个数
重在理解下面几句代码
int v=a[j+1];
dp[j+1][k]=(dp[j+1][k]+dp[j][k])%mod; // 前j+1个 gcd 组合为k的包括 dp[j][k]
 if(dp[j][k])
{
 int gg=gcd(k,v); 
 dp[j+1][gg]=(dp[j+1][gg]+dp[j][k])%mod;//若前j个gcd为k然后增加第j+1个a[j+1]得到gg  
                                                                //可知道dp[j+1][gg] 包括dp[j][k](前j个gcd为k)
}
 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll __int64
#define mod 100000007
using namespace std;
ll dp[][];
int a[];
int maxn;
int t,n;
ll ans=;
ll gcd(ll aa,ll bb)// 求解gcd
{
ll exm;
if(aa<bb)
{
exm=aa;
aa=bb;
bb=exm;
}
if(bb==)
return aa;
gcd(bb,aa%bb);
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=; i<=t; i++)
{
ans=;
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
scanf("%d",&n);
maxn=-;
for(int j=; j<=n; j++)
{
scanf("%d",&a[j]);
if(a[j]>maxn)
maxn=a[j];
dp[j][a[j]]=;//初始化 只取第j个a[j]
}
for(int j=; j<=n; j++)
{
int v=a[j+];
for(int k=; k<=maxn; k++)
{
dp[j+][k]=(dp[j+][k]+dp[j][k])%mod;
if(dp[j][k])
{int gg=gcd(k,v);
dp[j+][gg]=(dp[j+][gg]+dp[j][k])%mod;}
}
}
for(int j=; j<=maxn; j++)
ans=(ans+j*dp[n][j])%mod;
printf("%I64d\n",ans);
}
}
return ;
}

HDU 5656的更多相关文章

  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 01背包+gcd

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

  3. HDU 5656 CA Loves GCD dp

    CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...

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

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

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

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

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

  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/Others)To ...

随机推荐

  1. ASP.NET中Gridview一些技巧

    ASP.NET中Gridview一些技巧 一.后台覆盖掉Gridview中自动填充的值 我们可以再Gridview中的事件触发的过程中修改其中的值,而这些值将会在具体的运行过程中覆盖掉那些自动属性.这 ...

  2. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第5章.Git

    第5章--Git 版本控制简介 VCS (version control system) 版本控制系统:记录若干文件的修订记录的系统,帮助查阅/回到某个历史版本 LVCS本地 CVCS集中式(Cent ...

  3. C复合文字

    C99之前,可以传递数组,但是没有所谓的数组常量可供传递,于是新增了复合文字. 普通数组声明方法: int d[2]={10,20}; 复合文字声明: 与数组名相同,常量同时代表元素的地址. (int ...

  4. Paper Reading - CNN+CNN: Convolutional Decoders for Image Captioning

    Link of the Paper: https://arxiv.org/abs/1805.09019 Innovations: The authors propose a CNN + CNN fra ...

  5. Elasticsearch 排序插件的开发

    直接观察到的几个问题 简单expression脚本的执行效率 > java 插件,10000条数据可以测试出1ms左右的差距. Es会不断调用newScript来创建"足够多" ...

  6. LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告

    1.题目大意 Given two strings s and t which consist of only lowercase letters. String t is generated by r ...

  7. SpringCloud IDEA 教学 (五) 断路器控制台(HystrixDashboard)

    写在开头 断路器控制台是为了查看断路器运行情况而研发的.本章介绍了断路器控制台的搭建,代码基于之前Client的搭建.HystrixDashboard基于之前配置好的,使用了HystrixComman ...

  8. 七:HDFS Permissions Guide 权限

    1.权限模式     简单:启动HDFS的操作系统用户即为超级用户,可以通过HADOOP_USER_NAME指定     kerberos: 2.group mapping      组列表由grou ...

  9. 【转】使用CNPM搭建私有NPM

    最近的Node项目中因为数据模型等问题,需要有一个对各个模块进行统一的管理,如果把私有的模型publish到公共的npm不太合适,所以决定使用cnpm搭建一个私有的npm,同时也可以对项目常用的npm ...

  10. hosts_allow配置了却不生效

    hosts_allow配置了却不生效 配置了两台白名单的机器,一台生效一台不生效,google后的结果都是更新libwrap.so  安装openssh等等..(问题还是没有解决) 经过对比发现,原来 ...