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 ...
随机推荐
- typescript多维对象数组仿List泛型
定义对象: namespace entity{ export class MyClass{ public value:number; public rect:string; public constr ...
- on where having总结
1. ON 和WHERE 所有的查询都回产生一个中间临时报表,查询结果就是从返回临时报表中得到.ON和WHERE后面所跟限制条件的区别,主要与限制条件起作用的时机有关, ON根据限制条件对数据库记录进 ...
- react学习之路-配制antd-mobile
经过将近三个月的使用,现在终于在我老大的带领下做完了一个react的项目,感觉还可以,最大的不足就是,对于react中的很多的东西都是掺杂着jq使用来做的,这是最不满意的一点吧,但是开发进度很近,只能 ...
- mysql扩展库应用---在线词典程序范例
1,在mysql中创建数据表words. create table words( id int primary key not null auto_increment, enword varchar( ...
- [ActionScript 3.0] 亮度、对比度、饱和度、色相的调整
import fl.motion.ColorMatrix; import flash.filters.ColorMatrixFilter; //**调整亮度**// var ld_Matrix:Col ...
- redis 3.0 集群__监控警报工具(sentinel)
参考文档 http://redis.readthedocs.org/en/latest/topic/sentinel.html 因为目前还处于开发阶段,就先不研究了,待续
- 新建MAVEN项目--pom.xml报错
使用集成了maven的Eclipse版本新建maven项目后,配置文件pom.xml会在project以及引用的xsd文件处出现错误(第一.二行报错) 其中一个报错例子: Multiple annot ...
- java内存模型(jMM)(一)
在说java的内存模型之前先简单的了解计算机的主存和缓存的相关概念. 多任务和高并发是衡量一台计算机处理器的重要指标.一般衡量一个服务器性能的高低好坏,使用每秒事务处理数(Transactions P ...
- Django 学习资源
相关的分享: 开发者头条:http://toutiao.io/search?utf8=%E2%9C%93&q=django 极客头条及Django资讯:http://www.csdn.net/ ...
- fdisk 和 parted 分区工具
fdisk 和 parted: fdisk 是用来对 Linux 下的 MBR 分区进行操作的一款分区工具, 由于 MBR 的设计缺陷导致 MBR 不能处理大于 2TB 的硬盘, 并且主分区个数不能超 ...