hdu 5656 dp
来源:互联网 发布:网络推广文案例子 编辑:程序博客网 时间:2024/05/07 14:35
CA Loves GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1212 Accepted Submission(s): 397
Problem Description
CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too.
Now, there areN 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.
Now, there are
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
We guarantee that all numbers in the test are in the range [1,1000].
Output
Sample Input
222 431 2 3
Sample Output
810
Source
BestCoder Round #78 (div.2)
看了题解才知道是个dp 比赛中一直以为是数论题。。。(不过有大神用了容斥做的)
题目要求给你n个数问所有组合的最大公约数求和%10e8+7
令dp[i][j] 为前i个数取若干个数的gcd为j的方法数
dp[i][j]+=dp[i-1][j] 前i个数肯定包含前i-1个数的方法
当我们加入第i个数a[i] 会对原先的状态产生什么影响呢?
dp[i][gcd(a[i],j)]+=dp[i-1][j] 前i-1个数gcd为j的方法数有dp[i-1][j]个 a[i]与j 产生的gcd的方法数可以再加上dp[i-1][j] 个
#include <bits/stdc++.h>using namespace std;#define ll long longconst int Mod=100000007;int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int a[1005];int GCD[1005][1005];__int64 dp[1005][1005];void gg(){ memset(GCD,0,sizeof(GCD)); for(int i=1;i<1005;i++) for(int k=1;k<1005;k++) GCD[i][k]=GCD[k][i]?GCD[k][i]:gcd(k,i);}int main(){ int t; gg(); scanf("%d",&t); while(t--) { int n; int tem=0; scanf("%d",&n); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%d",a+i); tem=max(tem,a[i]); } for(int i=1;i<=n;i++) { for(int j=1;j<=tem;j++) { dp[i][j]=(dp[i][j]+dp[i-1][j]+(a[i]==j))%Mod; //当a[i] == j 时个数加1 记得取模 dp[i][GCD[a[i]][j]]=(dp[i][GCD[a[i]][j]]+dp[i-1][j])%Mod; } } ll res=0; for(ll j=1;j<=tem;j++) { res=(res+dp[n][j]*j)%Mod; } printf("%lld\n",res); } return 0;}
0 0
- hdu 5656 dp
- HDU DP
- DP? HDU
- DP? HDU
- DP? HDU
- 【dp】HDU
- 【dp】HDU
- 【dp】 HDU
- DP? HDU
- HDU 5656 CA Loves GCD (dp)
- HDU 1003 && HDU 1231 DP
- hdu 1058 DP
- HDU 1069 dp
- HDU 1024(DP)
- hdu 1257 dp+贪心
- HDU 1087(DP)
- HDU 1506(DP)
- hdu 2844 DP 背包
- AVPlayer 的几个关键点
- POJ-2777 Count Color(线段树)
- 第五周上机实践项目 项目3--时间类(2)
- ytu 2614 A代码完善--系统日期
- ListView设置tag展示图片
- hdu 5656 dp
- hadoop中的sqoop工具的使用
- c++:私有继承,公有继承,保护继承
- Java中内部类
- Codeforces Round #251(Div. 2) 439D. Devu and his Brother 枚举
- 时间类2
- 队列的实现
- 增长黑客(Growth Hacker)介绍
- java基础