HDU 5656 CA Loves GCD dp
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的更多相关文章
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD Accepts: 64 Submissions: 535 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 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 ...
- HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- 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 ...
- 数学(GCD,计数原理)HDU 5656 CA Loves GCD
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...
- hdu 5656 CA Loves GCD
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hdu 5656 CA Loves GCD(dp)
题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...
- HDU 5656 CA Loves GCD (容斥)
题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...
- hdu-5656 CA Loves GCD(dp+数论)
题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
随机推荐
- tp修改的写法
- 自动化测试===adb 解锁手机的思路
在adb里有模拟按键/输入的命令 比如使用 adb shell input keyevent <keycode> 命令,不同的 keycode 能实现不同的功能,完整的 keycode 列 ...
- Linux下用到数据库sqlite3
最近在Linux下用到数据库sqlite3,于是开始了该方面的学习. 0. 引言 我们这篇文章主要讲述了如何在C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理, 包括创建数据库.创建 ...
- Linux shell中运行命令后加上字符“&”的作用(转)
原文链接为:http://blog.sina.com.cn/s/blog_963453200102uya7.html & 放在启动参数后面表示设置此进程为后台进程 默认情况下,进程是前台进程, ...
- centos 下单独安装mysql
https://www.cnblogs.com/running-mydream/p/4666094.html https://www.cnblogs.com/lzj0218/p/5724446.htm ...
- P1084 疫情控制
Solution 二分答案, 尽量往上跳, 不能跳到根节点. 仍然能跳的拿出来.看剩下的点没有覆盖哪个? 贪心的分配一下. Code 70 #include<iostream> #incl ...
- Linux 用户篇——用户管理命令之id、whoami、su、chage
一.浅谈id.whoami.su.chage 本篇是续写上一篇<Linux 用户篇——用户管理命令之useradd.passwd.userdel.usermod>. (1)id命令 命令格 ...
- Lab 4 in Tornado
反正也没给CSS,自己改了下样式…… 效果: 题目给的验证信用卡号码规则不太全,万事达的卡第二位必须是1~5,另外其实visa号码也有13位的……要兼容这个的话只要把正则改成'^4([0-9]{12, ...
- pymongo的一些操作
参考:http://www.yiibai.com/mongodb/mongodb_drop_collection.html http://www.cnblogs.com/zhouxuchen/p/55 ...
- c++ primer 5 表达式
简单总结下容易忽视的地方和易错点吧 1 常用的位操作符,leecode很多算法题都是靠位运算解决的 2 箭头操作符 -> 等价于(* ). 对指针的成员操作 3 sizeof操作符 对 c ...