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 ...
随机推荐
- VMware+Linux更改磁盘配额(使用gparted LiveCd) 实用!!!
转载:http://blog.csdn.net/microad_liy/article/details/7667670 写这篇文章的原因: 最近要给服务器Centos上的ruby版本升级,由于是第一次 ...
- sql server不用安装sql管理工具开启sa
今天无意间发现腾讯云镜像有windows server2016,追求新系统的我,马上就重装了云服务器.重装完后发现,配置太低,远程桌面都变得不是很顺畅,于是装完sql server2016后便不打算另 ...
- CancellationTokenSource 取消任务
using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp1 { class Pr ...
- WebSerervice webapi使用
WebSerervice webapi使用 1.传json参数: 2.返回json数据: 3.权限控制: Authorize特性:必须经过认证,请求头必须具有token信息 4.路由: 5.过滤器: ...
- NOIP提高组题目归类+题解摘要(2008-2017)
因为前几天作死立了一个flag说要把NOIP近十年的题目做一做,并写一个题目归类+题解摘要出来,所以这几天就好好的(然而还是颓废了好久)写了一些这些往年的NOIP题目. 这篇博客有什么: 近十年NOI ...
- 二十四、MongoDB数据库的使用
首先按照上一篇文章的介绍,启动并连接数据库 然后我们开始学习如何使用MongoDB数据库: 1.创建数据库 第一步,在cmd窗口执行: use dbname dbname是你打算要创建的数据库名称 执 ...
- Ubuntu16.04实用python脚本 - 启动nautilus(Gnome的文件管理器)!
nautilus是Gnome的图形的文件管理器,可以很方便管理各种文件,但是通常我们不是在root用户下,如果想在root下使用,必须在shell里面输入命令: sudo nautilus 这样做固然 ...
- link和@import的区别是什么 ?
1.link是XHTML标签,除了加载CSS外,还可以定义RSS等其他事务:@import属于CSS范畴,只能加载CSS. 2.link引入CSS是,在页面载入时间同时加载:@import页面网页完全 ...
- flask内置函数 send_static_file(filename)
内部使用的函数将静态文件从静态文件夹发送到浏览器. current_app.send_static_file(filename)
- css ie6双倍margin现象
IE6双倍margin bug 当出现连续浮动的元素,携带和浮动方向相同的margin时,队首的元素,会双倍marign. 解决方案: 1)使浮动的方向和margin的方向,相反. 所以,你就会发现, ...