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 ...
随机推荐
- [Erlang07] Erlang 做图形化编程的尝试:纯Erlang做2048游戏
用Erlang久了,以为erlang做类似于As3,JS的图形化界面是绝对不可能的,多少次,多少次想用erlang做个炫酷的图形游戏.终于:折腾出来了结果:纯Erlang也可以做到! 因为以前接触过W ...
- django 生命周期
客户端发送请求 客户端(浏览器) → 发送请求 → 服务器(wsgi) → 解析请求 → 服务器(Middleware) → process_request → 服务器(urls) → 通过路由寻vi ...
- Luckily general gradient for spherical harmonics is defined
http://web4.cs.ucl.ac.uk/staff/j.kautz/publications/gradientSH_RS04.pdf
- java远程开关机
最近,很多客户向我们反馈终端启动后异常的问题,因此,我自己做了一个远程开关的小工具,该工具的目的在于通过批量的方式来控制终端启动.其设计逻辑是通过服务端发送cmd指令 ,客户端接受并执行指令,把结果返 ...
- Android应用开发以及设计思想深度剖析
Android应用开发以及设计思想深度剖析(1) 21cnbao.blog.51cto.com/109393/956049
- Objective-C适用C数学函数 <math.h>
在实际工作中有些程序不可避免的需要使用数学函数进行计算,比如地图程序的地理坐标到地图坐标的变换.Objective-C做为ANSI C的扩展,使用C标准库头文件<math.h>中定义的数学 ...
- jxl操作excel单个单元格换行和获取换行
excel中同表格换行: a+"\n"+b 1.读取 String str = sheet.getCell(c, r).getContents(); String[] split ...
- WPF捕获全局未处理异常
在WPF开发过程中我们一般都用try/catch块来捕获异常,但不是每个异常我们都能捕获,程序总会出现一些意想不到情况,抛出一些未捕获的异常,这时就要用到全局异常捕获,即在程序的最外层加上捕获未处理异 ...
- JavaScript实现自定义alert弹框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO
- 关于JAVA泛型中的通配符类型
之前对JAVA一知半解时就拿起weiss的数据结构开始看,大部分数据结构实现都是采取通配符的思想,好处不言而喻. 首先建立两个类employee和manager,继承关系如下.其次Pair类是一个简单 ...