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 ...
随机推荐
- python多线程下载文件
从文件中读取图片url和名称,将url中的文件下载下来.文件中每一行包含一个url和文件名,用制表符隔开. 1.使用requests请求url并下载文件 def download(img_url, i ...
- Java中Enum的使用
http://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html Enum有一个好处,可以使用EnumMap和EnumSet,而En ...
- Myeclipse编辑jsp文件很卡是什么原因?
可能是配置问题,配置的时候不要把myeclipse连接到网络.否则每次编辑的时候要在网上查找,所以照成很卡.window->perferences->java->Installed ...
- Prime
#include<iostream>#include<cstdio>#include<cstring>using namespace std; const int ...
- java图片转byte转string
第一种:原始乱码: public static void main(String[] args) throws IOException { File imgFile = new File(" ...
- centos7 lvs+keepalived nat模式
1.架构图 3.地址规划 主机名 内网ip 外网ip lvs-master 192.168.137.111(仅主机)eth1 172.16.76.111(桥接)eth0 lvs-slave 192 ...
- Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp
C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...
- Diffie–Hellman key exchange
General overview[edit] Illustration of the idea behind Diffie–Hellman key exchange Diffie–Hellman ...
- 【WPF】Behavior的使用
如何将一个行为附加到某个元素上呢?我们可以通过自定义一个Behavior! 我们首先看一下IAttachedObject接口,Behavior默认继承之这个接口 // 摘要: // 供可以附加到另一个 ...
- SQL必知必会 -------- 聚集函数、分组排序
聚集函数 1.AVG()函数 输入:SELECT AVG(prod_price) AS avg_price FROM Products 输出: 警告:只用于单个列AVG()只能用来确定特定数值列的平均 ...