HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1707 Accepted Submission(s): 543
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.
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
10
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e4 + 30;
const LL INF = 0x3f3f3f3f;
const int mod = 1e8+7;
int a[maxn], dp[maxn];
int GCD(int a, int b){
return b == 0? a: GCD(b,a%b);
}
int main(){
// freopen("INPUT.txt","r",stdin);
// freopen("my.txt","w",stdout);
int T, n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int Max = 1;
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
Max = max(Max, a[i]);
}
sort(a+1,a+1+n);
int flag = 0;
for(int i = 2; i <= n; i++){
if(a[i] == a[i-1]){
flag = 1; break;
}
}
if(flag) while(1){}
memset(dp,0,sizeof(dp));
if(flag == 1){ //这种处理不需要数不相同的条件
dp[a[1]] = 1;
for(int i = 2; i <= n; ++i){
for(int j = 1; j <= 1000; ++j){
if(dp[j] == 0) continue;
int gcd = GCD(j,a[i]);
// printf("%d %d...\n",gcd,dp[gcd]);
dp[gcd] = (dp[gcd] + dp[j]) % mod;
}
dp[a[i]]++;
}
}else{ //题目说得不重复,自己最开始的做法
for(int i = 1; i <= n; ++i){
dp[a[i]]++;
for(int j = 1; j < a[i]; ++j){
if(dp[j] == 0) continue;
int gcd = GCD(j,a[i]);
// printf("%d %d...\n",gcd,dp[gcd]);
dp[gcd] = (dp[gcd] + dp[j]) % mod;
}
}
}
LL res = 0;
for(int i = 1; i <= Max; ++i){
if(dp[i])
res = (res + (LL)dp[i]*(LL)i) % mod;
// printf("%d %d+++\n",i,dp[i]);
}
printf("%I64d\n",res);
}
return 0;
}
HDU 5656 ——CA Loves GCD——————【dp】的更多相关文章
- hdu 5656 CA Loves GCD(dp)
题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...
- 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.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...
- 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 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 (容斥)
题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...
- hdu 6169 gems gems gems【DP】
题目链接:hdu 6169 gems gems gems Now there are n gems, each of which has its own value. Alice and Bob pl ...
随机推荐
- C#反射(转载)
转载原文出处忘了,一直保存在本地(勿怪) 前期准备 在VS2012中新建一个控制台应用程序(我的命名是ReflectionStudy),这个项目是基于.net 4.0.接着我们打开Program.cs ...
- LA 3401
彩色立方体. [分析] 弄清楚24种状态,可以由标准姿态旋转而来. 计算24种状态: #include <iostream> #include <string> ,,,,, } ...
- cannot be resolved to a type (Java)
最近经常遇到cannot be resolved to a type (Java)报错,以下为在网上找到的解决方案: 1.先看看有没有引用相关jar包2.检查jar是否引用了多个相同的,或者多个jar ...
- 清北学堂2019NOIP提高储备营DAY3
今天是钟神讲课,讲台上照旧摆满了冰红茶 目录时间到: $1. 动态规划 $2. 数位dp $3. 树形dp $4. 区间dp $5. 状压dp $6. 其它dp $1. 动态规划: ·以斐波那契数列为 ...
- Delphi XE8中的DBExpress使用ODBC方式连接MySQL 5.6.24 X64!!!!
Delphi:XE2.XE8 MySQL:5.6.24 X64 今天准备做一个使用数据库的REST程序,最开始使用XE2,后来使用XE8,但是我遇到一个让我头疼的问题,就是无法连接我要使用的数据库,这 ...
- 小记一次shellscript的麻烦
小记一次shellscript的麻烦 一.起因: 之前写过篇文章 文本分析实例 ,大致的内容就是对 "nginx的web服务器进行日志分析,删除不被访问的截图". 点我打开哔哩哔哩 ...
- Flink学习笔记:Flink Runtime
本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...
- 原始MAC地址
D8CB8AD97D47
- [BZOJ 4921][Lydsy1706月赛]互质序列
传送门 因为区间 gcd 的变换不会超过 log 个,所以我们可以暴力枚举区间起点,复杂度是 n*logn 的 #include <bits/stdc++.h> using namespa ...
- leetcode 4 - binary search
注意: 1)需要保证nums1 的长度比 nums2 的长度小:(否则vector指针会越界) 2) 当分割线(partition)在首或尾时,用INT_MIN 和 INT_MAX 代替. 思路: ...