hdu 5656 CA Loves GCD

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

BestCoder
Round #78 (div.2)

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)

附上代码:

 1 #include <cstring>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <iostream>
 5 #define mod 100000007
 6 using namespace std;
 7
 8 int xx(int a,int b)
 9 {
10     int c,t;
11     if(a<b)
12     {
13         t=a;
14         a=b;
15         b=t;
16     }
17     while(b)
18     {
19         c=a%b;
20         a=b;
21         b=c;
22     }
23     return a;
24 }
25
26 int main()
27 {
28     int T,i,j,a,b,k,n,m,w;
29     int ai[1005];
30     long long vis[1005];
31     scanf("%d",&T);
32     while(T--)
33     {
34         long long sum=0;
35         scanf("%d",&n);
36         memset(vis,0,sizeof(vis));
37         for(i=0; i<n; i++)
38         {
39             scanf("%d",&ai[i]);
40         }
41         for(i=0; i<n; i++)
42         {
43             for(j=1; j<=1000; j++)
44                 if(vis[j])
45                 {
46                     vis[xx(ai[i],j)]=(vis[xx(ai[i],j)]+vis[j])%mod;
47                 }
48             vis[ai[i]]=(vis[ai[i]]+1)%mod;
49         }
50         for(i=1; i<=1000; i++)
51             if(vis[i])
52                 sum=(sum+(i*vis[i])%mod)%mod;
53         printf("%I64d\n",sum);
54     }
55 }
时间: 2025-01-12 04:40:03

hdu 5656 CA Loves GCD的相关文章

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

CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 CA喜欢是一个热爱党和人民的优秀同♂志,所以他也非常喜欢GCD(请在输入法中输入GCD得到CA喜欢GCD的原因). 现在他有N个不同的数,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去. 为了使自己不会无聊,CA会把每

hdu 5656 CA Loves GCD dp

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 diffe

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

CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 CA喜欢是一个热爱党和人民的优秀同♂志,所以他也非常喜欢GCD(请在输入法中输入GCD得到CA喜欢GCD的原因). 现在他有N个不同的数,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去. 为了使自己不会无聊,CA会把每

hdu-5656 CA Loves GCD(dp+数论)

题目链接: 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 

hdu 5655 CA Loves Stick

CA Loves Stick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 612    Accepted Submission(s): 214 Problem Description CA loves to play with sticks.One day he receives four pieces of sticks, he

HDU 5658 CA Loves Palindromic(回文树)

CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 301    Accepted Submission(s): 131 Problem Description CA loves strings, especially loves the palindrome strings. One day

hdu CA Loves GCD(dp)

一道我想骂人的题,差点把我气炸了. 题意: 求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和.结果MOD10^8+7输出. 输入输出不说了,自己看吧,不想写了. 当时我真把它当作数论题来写了,以为可以推导出什么公式然后化简大量重复的操作的.结果最后也没找到.最后题解说是dp,我同学说是暴力,吐血10升. 然后弄出来dp方程之后还是反复的wa,方程明明没啥问题,愣是卡了2个小时找不出错误,心情烦躁的要命,坑爹的室友还各种看视频打游戏,还不带耳机,我自己只好带着耳机大声放音乐,最后

HDU 5656

CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 809    Accepted Submission(s): 283 Problem Description CA is a fine comrade who loves the party and people; inevitably she loves GC

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[