CA Loves GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 882    Accepted Submission(s):
305

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
 
Source
 
Recommend
wange2014   |   We have carefully selected several
similar problems for you:  5659 5658 5657 5654 5653 
 
第一次用了三个for循环,结果直接超时,在大神的教导下,改用标记求值,十分巧妙(结果非常大,不要忘记取余!!!)。
 
题意:输入T,代表T个测试数据,再输入n表示n个数,接着输入n个数,求每次至少取一个数,最后的最大公约数之和为多少。
(比如第一组数据,2 4   第一次取2,公约数为2,第二次取4,公约数为4,第三次取2,4,公约数为2,所有公约数和为8)
 
附上代码:
 
 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#define mod 100000007
using namespace std; int xx(int a,int b)
{
int c,t;
if(a<b)
{
t=a;
a=b;
b=t;
}
while(b)
{
c=a%b;
a=b;
b=c;
}
return a;
} int main()
{
int T,i,j,a,b,k,n,m,w;
int ai[];
long long vis[];
scanf("%d",&T);
while(T--)
{
long long sum=;
scanf("%d",&n);
memset(vis,,sizeof(vis));
for(i=; i<n; i++)
{
scanf("%d",&ai[i]);
}
for(i=; i<n; i++)
{
for(j=; j<=; j++)
if(vis[j])
{
vis[xx(ai[i],j)]=(vis[xx(ai[i],j)]+vis[j])%mod;
}
vis[ai[i]]=(vis[ai[i]]+)%mod;
}
for(i=; i<=; i++)
if(vis[i])
sum=(sum+(i*vis[i])%mod)%mod;
printf("%I64d\n",sum);
}
}

hdu 5656 CA Loves GCD的更多相关文章

  1. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

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

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

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

  5. 数学(GCD,计数原理)HDU 5656 CA Loves GCD

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  6. HDU 5656 ——CA Loves GCD——————【dp】

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  7. hdu 5656 CA Loves GCD(dp)

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

  8. HDU 5656 CA Loves GCD (容斥)

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

  9. CA Loves GCD (BC#78 1002) (hdu 5656)

    CA Loves GCD  Accepts: 135  Submissions: 586  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: ...

随机推荐

  1. nodeJs基础方法

    Node.js 是一个基于Chrome javascript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用 Node.js 借助事件驱动, 非阻塞I/O 模型变得轻量和高效, 非常 ...

  2. CTSC2017密钥、吉夫特

    自己是有多么sb. 密钥 大家都说这是一道普及-的题,一年前我做不起,我可以说我太弱啦,我就普及组水平,今年我还是做不起…… 看大佬题解都是:开个桶就好啦! 我:你在说什么…… 首先把环拉成链,倍长. ...

  3. web前端学习(三)css学习笔记部分(7)-- 文字和字体相关样式、盒子相关样式、背景与边框相关样式

    12.  文字和字体相关样式 12.1  CSS3 给文字添加阴影 使用 text-shadow 属性给页面上的文字添加阴影效果,text-shadow 属性是在CSS2中定义的,在 CSS2.1 中 ...

  4. hdu 3791 二叉搜索树(数据结构)

    二叉搜索树 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. [leetcode] Reverse Words in a String [1]

    一.题目: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...

  6. Android基础&进阶

    基础总结篇之一:Activity生命周期 基础总结篇之二:Activity的四种launchMode 基础总结篇之三:Activity的task相关 基础总结篇之四:Service完全解析 基础总结篇 ...

  7. Leetcode695.Max Area of Island岛屿的最大面积

    给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二维数组中 ...

  8. 【心有猛虎】react-lesson

    这个项目标识:构建一套适合 React.ES6 开发的脚手架 项目地址为:https://github.com/ZengTianShengZ/react-lesson 运行的是第一课,基本上可以当作是 ...

  9. Java Map 排序

    1. 按照key值排序 对于java中Map的排序,有排序Map,比如TreeMap,对于这个Map,首先只能按照键排序,其次再put和remove的时候由于需要排序,性能上会有所牺牲. 这种方案,使 ...

  10. TRS OM error

    http://192.168.1.1/ http://tplogin.cn/admin888 wddqaz123456789 package="com.trs.om.bean" m ...