题目链接:

CA Loves GCD

Time Limit: 6000/3000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)

Problem Description
 
CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too. 
Now, there are N 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 T denoting the number of testcases.
T testcases follow. Each testcase contains a integer in the first time, denoting N, the number of the numbers CA have. The second line is N numbers. 
We guarantee that all numbers in the test are in the range [1,1000].
1≤T≤50
 
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
 
题意:
 
给n个数,问n个数的全组合的gcd的和是多少;
 
思路:
 
注意到数都在1000以内,所以把所以得组合都找出来,但是数量巨大所以相同的要压缩;
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll dp[],num[],p[][];
int n,a[];
const ll mod=1e8+;//注意是8,比赛的时候这个地方直接wa到cry
int gcd(int x,int y)
{
if(y==)return x;
return gcd(y,x%y);
}
int main()
{
for(int i=;i<=;i++)
{
for(int j=i;j<=;j++)
{
p[i][j]=gcd(i,j);
}
}
int t;
scanf("%d",&t);
while(t--)
{
for(int i=;i<=;i++)
{
num[i]=;
}
ll ans=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+);
for(int i=;i<=n;i++)
{
dp[i]=(ll)a[i];
for(int j=;j<=a[i];j++)
{
if(num[j])
{
num[j]%=mod;
dp[i]+=num[j]*p[j][a[i]];//压缩的地方
dp[i]%=mod;
num[p[j][a[i]]]+=num[j];
num[p[j][a[i]]]%=mod;
}
}
num[a[i]]++;
ans+=dp[i];
ans%=mod;
}
cout<<ans<<"\n";
} return ;
}
 

hdu-5656 CA Loves GCD(dp+数论)的更多相关文章

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

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

  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 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  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(dp)

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

  9. HDU 5656 CA Loves GCD (容斥)

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

随机推荐

  1. code::blocks(版本号10.05) 配置opencv2.4.3

    (1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 详细操作例如以下: 第一步, 配置compiler, 操作步骤为Settings  -> Comp ...

  2. Linux在中国正在走向没落

    在中国,Linux正在走向没落,一片萧条景象. 在这样的大背景下.居然有人愿意接手中科红旗,令人佩服! 在中国,没有一个关于国际Linux的官方刊物(或站点)反映国际Linux运动的真实声音.Linu ...

  3. 深入浅出Attribute (一)

    正文: 什么是Attribute?Attribute是干什么使的?Attribute与Property到底有什么区别?…… 长久以来,这些问题一直困扰着并不怎么广大的C#初学者.原因大概有两个,一是A ...

  4. Azure、数据、AI开发工具

    Azure.数据.AI开发工具 在今天召开的 Connect(); 2017 开发者大会上,微软宣布了 Azure.数据.AI 开发工具的内容.这是第一天的 Connect(); 2017 的主题演讲 ...

  5. C语言的运算符的优先级与结合性+ASCII表

    [0]README 0.1) 内容来源于 C程序设计语言, 旨在整理出C语言的运算符的优先级与结合性, 如下图所示(哥子 记了大半年都没有记住,也是醉了,每次都要去翻): Alert)以下内容转自:h ...

  6. 获取网站的BaseURL

    //get base URL            var _urlstr = window.location.href;            if (_urlstr.indexOf("? ...

  7. iOS10.3 UILable中划线失效问题

    iOS10.3系统的一个Bug,在UILable中含有中文时,中划线会失效 NSString *priceStr = [NSString stringWithFormat:@"%.2f元&q ...

  8. 用php動態產生各種尺寸的圖片

    我的做法是用php動態產生各種尺寸的圖片,例如<img src="img.php?src=a.jpg&width=100&height=200"...< ...

  9. Flask中的模板语法jinjia2

    Flask中默认的模板语言是Jinja2 I. Jinja2模板语言中的 for {% for foo in g %} {% endfor %} II. Jinja2模板语言中的 if {% if g ...

  10. Swift - 修改导航栏的样式(文字颜色,背景颜色,背景图片)

    默认情况,导航栏UINavigationController的样式如下,如果想要使用代码修改样式也是比较简单的. 1,修改导航栏背景色 1 2 3 //修改导航栏背景色 self.navigation ...