HDU 5656 CA Loves GCD (数论DP)
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)的更多相关文章
- HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- 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 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,然后求每个 ...
- CA Loves GCD (BC#78 1002) (hdu 5656)
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: ...
随机推荐
- chrome 31删除输入框的历史记录
chrome 31删除输入框的历史记录 TMD居然要用Shift + delete 了.... 为毛!!!
- zlib用法说明
1. 如何获得zlib zlib的主页是:http://www.zlib.net/ 2. 用VC++6.0打开 把 下载的源代码解压打开,VC6.0的工程已经建好了,在\projects\visual ...
- 使用hibernate annotation 为非空列加上默认值
在网上查了很多资料都没找到如何为非空列加上默认值 以前的做法是给字段一个初始值,加上dynamic-insert属性 换了annotation了以后没有找到如何设置dynamic-insert属性 但 ...
- build path功能详解
在项目上右键>Build path>Config build path “web project”中,一般把"src"设置为source folder,把WEB-INF ...
- Delphi 函数参数修饰中的var 、out和const
(1)var修饰符 添加var 是地址传递,会修改原有的变量 var s: string; begin S := 'Hello'; ChangeSVar(s); ShowMessage(S); e ...
- lightoj 1023
题意:让你输出前N个大写字母的前K个排列,按字典序,很水,直接dfs. #include<cstdio> #include<string> #include<cstrin ...
- SQL 语句记录
1.创建一个table @"create table rockTB(myId integer primary key autoincrement not null, time varchar ...
- linux常用命令之--磁盘管理命令
linux的磁盘管理命令 1.查看磁盘空间 df:用于显示磁盘空间的使用情况 其命令格式如下: df [-option] 常用参数: -i:使用inodes显示结果 -k:使用KBytes显示结果 - ...
- MockMvc和Mockito之酷炫使用
由于项目中需要添加单元测试,所以查询之后发现Mockito非常适合现在的web项目. 首先需要添加pom依赖: <dependency> <groupId>junit</ ...
- 读pomelo的教程-2
下面从头到尾记录chat demo的Login的过程 client:点击login按钮,取得username和rid两个值 $("#login").click(function() ...