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 ...
随机推荐
- mysql中列的增删改
增加列: ); ) after id; ) first; 修改列名: ); #change可改名字与字段类型 mysql> alter table a change uid uid int; Q ...
- 64_r3
rubygem-resque-cleaner-0.3.0-5.fc24.noarch.rpm 24-Sep-2016 22:26 22422 rubygem-resque-cleaner-doc-0. ...
- 全面了解 Nginx 主要应用场景
前言 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流 N ...
- 画弧线DrawArc的研究-我自己 -- 直线交接圆角
procedure TForm4.Button7Click(Sender: TObject); var pwith: Integer; //画笔的宽度 hx1, hy1: Integer; //横线第 ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- Asp.net vNext 学习之路(二)
View component(视图组件)应该是MVC6 新加的一个东西,类似于分部视图.本文将演示在mvc 6中 怎么添加视图组件以及怎么在视图中注入一个服务. 本文包括以下内容: 1,创建一个新的a ...
- 纯css导航下划线跟随效果【转载】
css: ul { display: flex; position: absolute; width: 800px; top: 50%; left: 50%; transform: translate ...
- Python全栈开发之3、数据类型set补充、深浅拷贝与函数
转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5466082.html 一.基本数据类型补充 set是一个无序而且不重复的集合,有些类似于数学中的集合,也可以求交集, ...
- 性能测试常用的linux命令
性能测试常用的linux命令 linux测试nginx64bitredhatlighttpd 查看日志 awk '$4 ~/^\[27\/Nov\/2008:15:2[0-5]/ {print ...
- 【笔试题】怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串?
笔试题 怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串? import java.io.UnsupportedEncodingException; public clas ...