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. 22.allegro中PCB打印设置[原创]

    1. -- 2. 3. 4. ----

  2. 网站TCP链接暴增

    昨天上线后,TCP链接暴增,红点增多. 问题在查.其中有一部分,多线程修改,突破了线程数 64的限制.线程内,会发起网络请求. 怀疑是热点之一.其他的部分也有修改,也被怀疑.准备下次,2部分分开上线. ...

  3. combination-sum-ii(熟悉下Java排序)

    代码还是这一块代码,但是还是写的很慢.. 其中用到了Java对 List的排序.查了很久,发现使用 Collections.sort 很方便. 另外对结果的去重,使用了 Java的HashSet. h ...

  4. uva12230Crossing Rivers

    数学期望. 过每条河的时间的可能在[L/v,3*L/v]间均匀分布,数学期望为2*L/v. 然后在加上在陆上走的时间. #include<cstdio> #include<algor ...

  5. 发布 windows 10 universal app 时微软账号验证失败

    具体错误:Visual Studio encountered an unexpected network error and can't contact the Microsoft account s ...

  6. C# 创建iis站点以及IIS站点属性,iis不能启动站点

    DontLog = False是否将客户端的请求写入日志文件 2011年04月09日 #region CreateWebsite 新增网站 public string CreateWebSite(st ...

  7. kindeditor编辑器图片水印

    //upload_pic.ashx源码 <%@ webhandler Language="C#" class="edit_html_upload_pic" ...

  8. unison + inotify 实现文件实时双向同步部署步骤

    unison + inotify 实现文件实时双向同步部署步骤 一. Unison简介 Unison是Windows.Linux以及其他Unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或 ...

  9. Java [Leetcode 169]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  10. oracle查询某个用户下的所有视图

    SYS>select view_name from dba_views where owner = 'NS_RAS3'; VIEW_NAME--------------------------- ...