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. Uva116 Unidirectional TSP

    https://odzkskevi.qnssl.com/292ca2c84ab5bd27a2a91d66827dd320?v=1508162936 https://vjudge.net/problem ...

  2. 杨柳絮-Info:太原市多部门通力合作科学治理杨柳飞絮效果好

    ylbtech-杨柳絮-Info:太原市多部门通力合作科学治理杨柳飞絮效果好 1.返回顶部 1. 太原市多部门通力合作科学治理杨柳飞絮效果好 2016-04-21 07:16 4月10日,随着气温升高 ...

  3. Oracle 行转列及列转行

    参考网址:http://blog.163.com/fushahui_1988@126/blog/static/82879994201192844355174/ 一.多行转一列select id, vn ...

  4. 【python小随笔】celery周期任务(简单原理)

    1:目录结构 |--celery_task |--celery.py # 执行任务的main函数 |--task_one # 第一个任务 |--task_two # 第2个任务 . . . . |-- ...

  5. java获取外网ip地址

    转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...

  6. startActivity 流程图

  7. R语言因子

    R语言因子 因子是它们用于将数据进行分类并将其存储为级别的数据对象.它们可以同时存储字符串和整数.它们在具有唯一值的有限数目的列是有用的. 例如,"male, "Female&qu ...

  8. 列表list和元祖tuple

    list和tuple list列表: Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: ...

  9. springmvc restful风格操作

    ssm框架 controller: package com.sgcc.controller; import java.util.ArrayList; import java.util.List; im ...

  10. scala2.11读取文件

    1.读取行 要读取文件的所有行,可以调用scala.io.Source对象的getLines方法: import scala.io.Source val source = Source.fromFil ...