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. Font Awesome 完美的图标字体

    好久没来,虽说鄙人的人气不咋地,但还是很想念自己这一亩二分田地. 近期用在平台开发中,看着设计师摆开阵势,准备大画图标,想着自己将会很KUBI拼凑css-sprite图片,接着写一大堆 class^= ...

  2. 单元测试模块unittest使用学习

    工作原理: unittest中最核心的四个概念是:test case, test suite, test runner, test fixture. 一个TestCase的实例就是一个测试用例.什么是 ...

  3. Case 降序升序排列

    select nc.Class_Name,hn.home_news_id,hn.hemo_id,hn.hemo_Date, hn.hemo_title,hemo_order from Hemo_New ...

  4. SpringBoot在IDEA下使用JPA

    1依赖 使用IDEA构建基于JPA的项目需要引用JPA.MYSQL依赖 2配置文件修改 2.1连接库 spring.datasource.url=jdbc:mysql://localhost:3306 ...

  5. 七:HDFS Permissions Guide 权限

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

  6. 1.安装CDH5.12.x

    安装方式安装前准备安装步骤安装过程修改/etc/hosts设置ssh 互信修改linux 系统设置安装JDK1.8安装python2.7安装mysql/postgreysql数据库安装ntp设置本地y ...

  7. week1 四人小组项目

    小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 项目选题:东北师范大学论坛 作为东北师范大学同学间的信息交流平台,要满足的需求如下: 1.校内信息及公告 2.毕业生招聘信息 3.课程查 ...

  8. 在vue项目中使用monaco-editor

    monaco-editor: https://github.com/Microsoft/monaco-editor 在ESM中的使用官方也有对应文档:https://github.com/Micros ...

  9. 提升MyEclipse运行速度

    修改MyEclipse.ini文件中的,将-vmargs后面的参数修改为 -Xms256m -Xmx768m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:Max ...

  10. 只有父类的init方法才有创建servletConfig与servletContext的能力

    如果重写了父类的init方法 但又没有显示调用父类的init方法 那么子类无法使用servletContext 因为 子类方法没有创建出 servletConfig