Acdream-K-GCD

Source: Internet
Author: User

Start with the question:

B-K-GCD Time limit:2000/1000 ms (Java/Others) Memory limit:128000/64000 KB (Java/others) submitstatusproblem description gives n numbers a [1], a [2] ...... A [n] and a positive integer k allow you to obtain the number of K in the N number and calculate their GCD. Ask the maximum GCD value.
PS: K = 1, gcd is equal to the selected number. Input

The integer T in the first line represents the number of groups of test data.
Each group of test data has two rows.
The first row has two integers, N and K;
The second row has n integers A [1], a [2] ...... A [n]:

1 <= T <= 100;
2 <= k <= n <= 10000;
1 <= A [I] <= 10000;

 

 

 

Output

Each group of data outputs a row. An integer represents the largest GCD.

 

 

Sample Input
25 312 36 20 15 95 412 36 20 15 9
Sample output
43

In fact, this question was not difficult, but why was it unexpected at the beginning? It is probably because the brain is used to think about the time complexity that may be required must be in O (n )~ O (N ^ 2), then it is easy to think about whether O (N), O (nlogn), or O (N ^ 2). In other words, it is easy for us not to calculate the time complexity, but to subconsciously think about the time complexity of the question. We often forget the time complexity prompt in the question, so we don't have to guess it.
The practice of this question is to find out every factor in each number and then determine which of all the factors is greater than or equal to K and select the largest factor. The time complexity is only O (N ^ (3/2 )).

Code:

 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define MAX 10002 6 using namespace std; 7   8 int a[MAX]; 9 int s[MAX];10 int maxn,mm,n,k;11  12 int main()13 {14     int t,sq,e;15     //freopen("data.txt","r",stdin);16     scanf("%d",&t);17     while(t--){18         memset(s,0,sizeof(s));19         scanf("%d %d",&n,&k);20         for(int i=0;i<n;i++) scanf("%d",&a[i]);21         mm=0;22         for(int i=0;i<n;i++){23             mm = max(a[i],mm);24             sq = (int)sqrt(a[i]*1.0);25             for(int j=1;j<=sq;j++){26                 if(a[i]%j==0){27                     s[j]++;28                     e = a[i]/j;29                     if(e != j)s[a[i]/j]++;30                 }31             }32         }33         maxn=0;34         for(int i=1;i<=mm;i++){35             if(s[i]>=k) maxn = i;36         }37         printf("%d\n",maxn);38     }39     return 0;40 }
K-GCD

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.