CA Loves GCD

题目链接:

http://acm.hust.edu.cn/vjudge/contest/123316#problem/B

Description

CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too.

Now, there are 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.

Input

First line contains denoting the number of testcases.

testcases follow. Each testcase contains a integer in the first time, denoting , the number of the numbers CA have. The second line is numbers.

We guarantee that all numbers in the test are in the range [1,1000].

Output

T lines, each line prints the sum of GCDs mod 100000007.

Sample Input

2

2

2 4

3

1 2 3

Sample Output

8

10

Hint

题意:

给出N(N<=1000)个不超过1000的数字;

对于每个子集可以求出该子集的最大公约数;

现在要求所有子集的最大公约数之和.

题解:

由于数字的规模不超过1000;

则可以直接用DP暴力枚举所有子集情况;

dp[i] 表示以i为最大公约的子集有多少个;

扫描这N个数字:

对于当前的num[i], 枚举其可能出现的最大公约数并计数:

int tmp = gcd(num[i], j);

dp[tmp] = (dp[tmp] + dp[j]) % mod;

很遗憾,直接枚举1-1000来更新dp会TLE;

优化途径:

1.将1000内任意两个数的gcd值打表.

2.每次枚举k时,若d[j]为0(即不存在以j为gcd的子集),则不需要更新(节省算gcd的时间);

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1500
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int num[maxn];
LL dp[maxn];
int gcc[1001][1001]; int main(int argc, char const *argv[])
{
//IN; for(int i=1; i<=1000; i++) {
for(int j=1; j<=1000; j++) {
gcc[i][j] = __gcd(i,j);
}
} int t; scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", &num[i]); memset(dp, 0, sizeof(dp)); dp[0] = 1;
for(int i=1; i<=n; i++) {
for(int j=1; j<=1000; j++) {
int tmp = gcc[num[i]][j];
dp[tmp] = (dp[tmp] + dp[j]) % mod;
}
dp[num[i]]++;
} LL ans = 0;
for(int i=1; i<=1000; i++) {
ans = (ans + i%mod*dp[i]) % mod;
} printf("%I64d\n", ans);
} return 0;
}

HDU 5656 CA Loves GCD (数论DP)的更多相关文章

  1. HDU 5656 ——CA Loves GCD——————【dp】

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  2. hdu 5656 CA Loves GCD(dp)

    题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...

  3. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  4. 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 ...

  5. 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 ...

  6. 数学(GCD,计数原理)HDU 5656 CA Loves GCD

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  7. hdu 5656 CA Loves GCD

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  8. HDU 5656 CA Loves GCD (容斥)

    题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...

  9. CA Loves GCD (BC#78 1002) (hdu 5656)

    CA Loves GCD  Accepts: 135  Submissions: 586  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: ...

随机推荐

  1. webbrowser代理c#代码实现

    微软webbrowser控件也就是IE插件,他的所有功能就像IE类似,当然设置也是一样的,下面介绍下webbrowser如何设置代理,可不要用这个对抗广告联盟哦 You can change the ...

  2. EXT 数据按F12,F11 显示问题

    最近做关于EXT的项目,因为是刚开始接触EXT,对什么都不熟悉,所以把其他人写好的浏览页代码考过了来,换成自己需要的. 一切都做好了,然后数据不出来,就调试看,后台也出现数据了,然后就按F12调试前台 ...

  3. Woobuntu woobuntu_build.sh hacking

    # Woobuntu woobuntu_build.sh hacking # 说明: # 有时候因为一些需求,我们需要定制一些系统,包括Ubuntu系统,于是 # 我们自然需要知道如何去解包一个Ubu ...

  4. 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...

  5. 【转】u盘不显示盘符

    转自http://jingyan.baidu.com/article/f3ad7d0fd0793e09c3345b31.html 我的情况: 电脑只有一个c盘,插入u盘,u盘的盘符为d. 弹出u盘,但 ...

  6. 【c++内存分布系列】单继承

    父类包括成员函数.静态函数.静态方法,子类包括成员函数.静态函数.静态方法的情况与一个类时完全一致,这里就不做分析了.子类单独包含虚函数时继承无关,也不做分析了. 一.父类子类都为空 #include ...

  7. ArcGIS AO开发高亮显示某些要素

    参考代码1 ifeaturecursor pcur = ifeatureclass.search(iqueryfilter pfilter); pfilter.whereclause = strAdd ...

  8. [Everyday Mathematics]20150210

    设正方体 $ABCD-A_1B_1C_1D_1$ 的棱长为 $1$, $E$ 为 $AB$ 的中点, $P$ 为体对角线 $BD_1$ 上一点, 当 $\angle CPE$ 最大时, 求三菱锥 $P ...

  9. [Everyday Mathematics]20150105

    设 $f\in C^1(a,b)$ 适合 $$\bex \lim_{x\to a^+}f(x)=+\infty,\quad \lim_{x\to b^-}f(x)=-\infty, \eex$$ 并且 ...

  10. 走向DBA[MSSQL篇] - 从SQL语句的角度提高数据库的访问性能(转)

    最近公司来一个非常虎的DBA,10几年的经验,这里就称之为蔡老师吧,在征得我们蔡老同意的前提下 ,我们来分享一下蔡老给我们带来的宝贵财富,欢迎其他的DBA来拍砖.  目录 1.什么是执行计划?执行计划 ...