Coprime Sequence

Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, and the GCD (Greatest Common Divisor) of them is equal to 1. 
``Coprime Sequence'' is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements.

InputThe first line of the input contains an integer T(1≤T≤10)T(1≤T≤10), denoting the number of test cases. 
In each test case, there is an integer n(3≤n≤100000)n(3≤n≤100000) in the first line, denoting the number of integers in the sequence. 
Then the following line consists of nn integers a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the elements in the sequence.OutputFor each test case, print a single line containing a single integer, denoting the maximum GCD.Sample Input

3
3
1 1 1
5
2 2 2 3 2
4
1 2 4 8

Sample Output

1
2

题意:删掉一个数,求最大的公约数

思路:先排序,找前两个的因子,用哈希判断随后因子个数是n-1的输出最大的

#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
map<int,int>::iterator it;
int main()
{    
     int T;    
     scanf("%d",&T);    
     while(T--)    
    {       
        int n,x=0,t=0,i,j,a[100001],ans=1;       
        scanf("%d",&n);       
        map<int,int>mp;       
        mp.clear();       
        for(i=0;i<n;i++) 
        {           
            scanf("%d",&a[i]);          
            if(a[i]==1)               
               x++;       
        }      
        if(x>=2)           
             printf("1\n");        
        else 
        {            
           sort(a,a+n);           
           for(i=1;i<=sqrt(a[0]);i++)
           {                
              if(a[0]%i==0) 
              {                  
                  mp[i]++;                  
                  if(i*i!=a[0])                  
                  mp[a[0]/i]++;}}           
                  for(i=1;i<=sqrt(a[1]);i++) 
                  {               
                      if(a[1]%i==0)  
                      {                
                        mp[i]++;                 
                        if(i*i!=a[1])                 
                         mp[a[i]/i]++;
                        }  
                   }
                   for(i=2;i<n;i++)              
                        for(it=mp.begin();it!=mp.end();it++)  
                        {                    
                            if(a[i]%(it->first)==0)                        
                                it->second++;  
                        }              
                    for(it=mp.begin();it!=mp.end();it++)                   
                            if(it->second==n-1)                       
                                ans=max(ans,it->first);                      
                printf("%d\n",ans);            
                }    
            }    
         return 0;
}

 

posted @ 2018-04-28 17:22  ~~zcy  阅读(71)  评论(0编辑  收藏  举报