Sgu 499 greatest common divisor"

Source: Internet
Author: User
Tags greatest common divisor

Question address: http://acm.sgu.ru/problem.php? Contest = 0 & problem = 499

Analysis logic: Directly calculating the maximum common number of two pairs will time out. Let's look at the problem from another perspective. We can find all the approximate number of input numbers. If a number appears twice or more, then, the most satisfying condition is ggcd.

Then it is similar to the sieve method to find the approximate number, so you can find 1 ~ In N, each number cannot exceed all the approx. Of its own square root. Then the one that is bigger than the square root can be used for Division (the exact number of vertices is special ).

Then, instead of brute force, we can directly find the one that appears twice from the largest one. We can set a maxn to record it. Then we can compare the number of the second appearance with maxn.

First look at the Code:

#include<iostream>#include<vector>#include<cmath>//#include<cstdio>using namespace std;#define  N 1000005vector<int>  v[N+1];//int cnt[N+1]={0};bool b[N+1]={0};void pre(){    int len=sqrt(N);   for(int i=1;i<=len;i++)     for(int j=i*i;j<=N;j+=i)        v[j].push_back(i);}int maxn=1;void check(int n){   if(b[n])   {       maxn=n>maxn?n:maxn;   }   b[n]=1;}int main(){   pre();   int size;   cin>>size;   for(int i=0;i<size;i++)      {         int n;         cin>>n;               if(n<=maxn)  continue;         for(int j=0;j<v[n].size();j++)         {              check(v[n][j]);             if(v[n][j]*v[n][j]!=n)              check(n/v[n][j]);         }//            int sqrtn=sqrt(n);//            for(int j=1;j<=20&&j<=sqrtn;j++)//          {//              if(n%j==0)//              { check(j);////                if(j*j!=n) check(n/j);////              }//          }      }//    for(int i=1000000;i>0;i--)//       if(cnt[i]>=2)//       {//           cout<<i<<endl;//           break;//       }     cout<<maxn<<endl; }

This will time out, because each number has a factor of 1, and the general number has a factor of 2. When we create a table, we choose not to input the first 20 orders, then, check the first 20 items for the input number (after all, only 10 million items are smaller than the data range of 100 million.

Then, SQRT (n) should not appear in for (INT I = 0; I, <SQRT (n); I ++, otherwise, the computation is performed every time you make a decision. Because this TLE is used several times.

When TLE is always stuck in test 20, change all CIN cout to scanf printf. You can run test 38 to improve the fur.

The Code is as follows:

#include<iostream>#include<vector>#include<cmath>//#include<cstdio>using namespace std;#define  N 1000005vector<int>  v[N+1];//int cnt[N+1]={0};bool b[N+1]={0};void pre(){    int len=sqrt(N);   for(int i=21;i<=len;i++)     for(int j=i*i;j<=N;j+=i)        v[j].push_back(i);}int maxn=1;void check(int n){   if(b[n])   {       maxn=n>maxn?n:maxn;   }   b[n]=1;}int main(){   pre();   int size;   cin>>size;//   scanf("%d",&size);   for(int i=0;i<size;i++)      {         int n;         cin>>n;       // scanf("%d",&n);          if(n<=maxn)  continue;         for(int j=0;j<v[n].size();j++)         {              check(v[n][j]);             if(v[n][j]*v[n][j]!=n)              check(n/v[n][j]);         }            int sqrtn=sqrt(n);            for(int j=1;j<=20&&j<=sqrtn;j++)          {              if(n%j==0)              { check(j);                if(j*j!=n) check(n/j);              }          }      }//    for(int i=1000000;i>0;i--)//       if(cnt[i]>=2)//       {//           cout<<i<<endl;//           break;//       }     cout<<maxn<<endl;  // printf("%d\n",maxn);}

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.