zoukankan      html  css  js  c++  java
  • 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].
    1T50
     
    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)
     
    附上代码:
     
     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
    ",sum);
    54     }
    55 }
  • 相关阅读:
    BZOJ3589: 动态树
    BZOJ3631: [JLOI2014]松鼠的新家
    BZOJ3307: 雨天的尾巴
    BZOJ1895: Pku3580 supermemo
    BZOJ3786: 星系探索
    BZOJ2819: Nim
    解题:POI 2009 Lyz
    解题:POI 2016 Nim z utrudnieniem
    解题:POI 2004 Bridge
    解题:POI 2018 Prawnicy
  • 原文地址:https://www.cnblogs.com/pshw/p/5351435.html
Copyright © 2011-2022 走看看