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: ...
随机推荐
- 第二十八条:利用有限制通配符来提升API的灵活性
如第二十五条所述,参数化类型是不可变的.类型Type1和Type2而言,不管Type1与Type2的关系,List<Type1>既不是List<Type2>的子类型,也不是也不 ...
- Codeforces 837E. Vasya's Function
http://codeforces.com/problemset/problem/837/E 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...
- faster-rcnn 结构杂谈
faster-rcnn结构图: (只截取了最难理解的部分) 这个网络看似很复杂,但是理解了其中关键的层,就基本可以掌握这个结构了.要看源码!!要看源码!!要看源码 !!重要的事情说三遍. 关键的层: ...
- 13-TypeScript单例模式
在JavaScript中,要实现设计模式比较复杂.而在TypeScript中因为使用面向对象的思想编程,要实现设计模式的方式与后端语言C#.Java等非常类似. 单例模式是一种常用的设计模式,通常用于 ...
- PC或者手机弹出窗效果
http://layer.layui.com/ 这个网站提供弹窗,是在jq封装的,弹窗之后,背景页面还可以滑动. 这个里面的js可能也会包含css,这个css不能移动位置,否则会报错,还有谷歌浏览器在 ...
- 帧动画的创建方式 - 纯Java代码方式
废话不多说,先看东西 帧动画的创建方式主要以下2种: * 用xml创建动画: * 纯Java代码创建动画: 本文内容主要关注 纯java代码创建帧动画 的方式: 用xml创建帧动画:http:// ...
- 用Vue.js开发微信小程序:开源框架mpvue解析
前言 mpvue 是一款使用 Vue.js 开发微信小程序的前端框架.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为 H5 和小程序提供了代码复用的能力.如果想将 H5 项目改造为小程 ...
- [UWP] Custom Capability的使用
Custom Capability 是uwp开发中普通开发者较为不常用的内容,但是在一些OEM和驱动厂商,使用频率比较高 Custom Capability 有两种用户: 1.普通应用程序开发者: 2 ...
- 深入了解GOT,PLT和动态链接
之前几篇介绍exploit的文章, 有提到return-to-plt的技术. 当时只简单介绍了 GOT和PLT表的基本作用和他们之间的关系, 所以今天就来详细分析下其具体的工作过程. 本文所用的依然是 ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)
Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...