hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD
CA喜欢是一个热爱党和人民的优秀同♂志,所以他也非常喜欢GCD(请在输入法中输入GCD得到CA喜欢GCD的原因)。
现在他有N个不同的数,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去。
为了使自己不会无聊,CA会把每种不同的选法都选一遍,CA想知道他得到的所有GCD的和是多少。
我们认为两种选法不同,当且仅当有一个数在其中一种选法中被选中了,而在另外一种选法中没有被选中。
第一行 TT,表示有 TT 组数据。
接下来 TT 组数据,每组数据第一行一个整数 NN,表示CA的数的个数,接下来一行 NN 个整数 A_iAi 表示CA的每个数。
1 \le T \le 50,~1 \le N \le 1000,~1 \le A_i \le 10001≤T≤50, 1≤N≤1000, 1≤Ai≤1000
对于每组数据输出一行一个整数表示CA所有的选法的GCD的和对 100000007100000007 取模的结果。
2
2
2 4
3
1 2 3
8
10
/*
hdu 5656 CA Loves GCD(n个任选k个的最大公约数和) 给你n个数,每次任选k个数出来求GCD,求所有不重复情况的和 开始试了好几次都TLE,卒。
1.给出的题解是可以用dp来解决,dp[i][j]表示前i个数的GCD为j的个数情况
2.求出给出数中所有i的倍数的个数lan[i],那么它的抽取方案数2^lan[i]-1,再利用容斥原理
可以处理出最大公约数为i的方案数
hhh-2016-04-03 14:29:30
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int mod = 1e8+7;
const int maxn = 1005;
int mm = 1000;
int gc[maxn][maxn];
ll dp[maxn][maxn];
int a[maxn];
int gcd(int a,int b)
{
while(a%b)
{
int t = a%b;
a = b;
b = t;
}
return b;
} int main()
{
int n,m;
int t;
for(int i = 1; i <= mm; i++)
{
for(int j = i; j <= mm; j++)
gc[i][j] =gc[j][i]= gcd(i,j);
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(int i =1; i <= n; i++)
{
scanf("%d",&a[i]);
}
for(int i = 1; i <= n; i++)
{ for(int j = 1; j <= mm; j++)
{
dp[i][j] = (dp[i][j]+dp[i-1][j])%mod;
dp[i][gc[a[i]][j]] =(dp[i][gc[a[i]][j]]+dp[i-1][j])%mod;
}
dp[i][a[i]]++;
}
ll ans = 0;
for(int i = 1; i <= mm; i++)
{
ans = (ans+(ll)i*dp[n][i]%mod)%mod;
}
printf("%I64d\n",ans);
}
return 0;
} /*
hdu 5656 CA Loves GCD(n个任选k个的最大公约数和) hhh-2016-04-02 22:14:36
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int maxn = 1050;
int mm = 1000;
ll bin[maxn];
const ll mod = 100000007;
vector<int >vec;
ll fa[maxn];
ll lan[maxn];
int main()
{
int T,x,n;
bin[0] = 1;
for(int i = 1; i <= mm; i++)
{
bin[i] = bin[i-1]<<1;
bin[i] %= mod;
}
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(lan,0,sizeof(lan));
memset(fa,0,sizeof(fa));
for(int i = 0; i < n; i++)
{
scanf("%d",&x);
lan[x]++;
}
for(int i = 1; i <= mm; i++)
{
ll t = 0;
for(int j = i; j<=mm; j+=i)
t += lan[j];
fa[i] = bin[t]-1;
}
ll ans = 0;
for(int k = mm; k; k--)
{
for(int t = k+k; t <=mm; t+=k)
fa[k] = (fa[k]-fa[t]+mod)%mod; //减去抽取到不包含k的情况
ans=(ans+k*fa[k]%mod)%mod;
}
printf("%I64d\n",ans%mod);
}
return 0;
}
hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)的更多相关文章
- 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 ...
- 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 ...
- 数学(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——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- 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(dp)
题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...
- 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: ...
随机推荐
- node创建第一个应用
如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...
- nyoj 第几是谁
第几是谁? 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有"abcdefghijkl"12个字符,将其按字典序排列,如果给出任意一种排列, ...
- 新概念英语(1-5)Nice to meet you.
Is Chang-woo Chinese? Blake:Good morning. B:Good morning, Mr Blake. Blake:This is Miss Sophie Dupont ...
- Spring Security 入门(1-6-1)Spring Security - 配置文件解析和访问请求处理
1.在pom.xml中添加maven坐标 <dependency> <groupId>org.springframework.security</groupId> ...
- SpringCloud是什么?
参考链接: http://blog.csdn.net/forezp/article/details/70148833 一.概念定义 Spring Cloud是一个微服务框架,相比Dubbo ...
- java 数组排序方法整理,简单易懂,
1.快速排序:首先是最简单的Array.sort,直接进行排序: public static void main(String[] args) { int[] arr = {4,3,5,1,7,9,3 ...
- ccf认证 201709-4 通信网络 java实现
试题编号: 201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...
- 表单提交中的input、button、submit的区别
1.input[type=submit] 我们直接来看例子: 代码如下: <form> <input name="name"> <input type ...
- MySql查询正在进行中的事务
用法 SELECT * FROM information_schema.INNODB_TRX 这个只能查询此刻正在进行中的事务,已经完成的是查不到的 表字段定义 The INFORMATION_SCH ...
- Java练习(模拟扫雷游戏)
要为扫雷游戏布置地雷,扫雷游戏的扫雷面板可以用二维int数组表示.如某位置为地雷,则该位置用数字-1表示, 如该位置不是地雷,则暂时用数字0表示. 编写程序完成在该二维数组中随机布雷的操作,程序读入3 ...