题目链接:

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. Node.js 把抓取到的电影节目列表单发或者群发到QQ邮箱

    代码地址如下:http://www.demodashi.com/demo/12381.html 一.前言 上一节我们演示了如何用Node的各种包去抓取电影天堂最新电影列表,接下来我们会讲解如何发送我们 ...

  2. C#.Net调试时调无法“编辑并继续”

    ‘启用编辑并继续’的作用是允许用户在调试的过程中修改源代码,并且修改的代码会编译到调试进程中立刻生效. 在调试时,无法查到变量的值,也无法编辑,若编辑会跳出如下弹框: 解决方法: 1:如下图,选择De ...

  3. linux init->upstart->systemd

    http://en.wikipedia.org/wiki/Init init From Wikipedia, the free encyclopedia     This article is abo ...

  4. CentOS6 图形界面(gnome)安装(转)

    CentOS6相对于CentOS5的安装有了不少的进步,有不少默认的选项可以选择,如: Desktop :基本的桌面系统,包括常用的桌面软件,如文档查看工具. Minimal Desktop :基本的 ...

  5. Struts2学习二----------访问Servlet API

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...

  6. cmake学习之- cmake_parse_arguments

    最后更新: 2019-06-08 一.指令介绍 cmake_parse_arguments 为解析函数(function)或 宏(macros) 参数的命令: cmake_parse_argument ...

  7. 基于友善之臂ARM-tiny4412--uboot源代码分析

    /* * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core * * Copyright (c) 2004 Texas Instrument ...

  8. python学习(十一)函数、作用域、参数

    定义和调用函数 在这里函数的定义和调用和一般的语句没什么不一样,感觉函数也是对象 #!/usr/bin/python def times(x, y):                # 定义函数    ...

  9. mongodb的IO测试工具 mongoperf

    之前没发现mongoperf这个工具,测试IO的状态用的是iostat来进行观察. mongoperf < myjsonconfigfile  echo "{nThreads:2,fi ...

  10. python从安装与使用pip到入门

    官方下载地址:https://www.python.org/downloads/ 下载后直接安装就可以了 再配一下环境变量, cmd运行python -V (注意,这里是大写的V) 打开python跑 ...