A question about the greatest common divisor: easy number

There are n Zacks, each Zack has an ability value, and k are selected from these n Zacks to make the value of their greatest common divisor the largest

【Input format】

The first line has an integer n, which means there are n Zacks

The second line contains n integers ai, representing the ability value of each Zack

【Output format】

A total of n rows, the nth row is the greatest common factor of the ability value in the case of k=i

 

【Sample input】

4

1 2 3 4

 

【Example output】

 

4

 

2

 

1

1

 

 

All problems about divisors we can operate by their common factors;

In this question, we can count how many numbers have factor x for each factor x;

So we loop the simulation to answer the answer;

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int c[1000010];
inline void fenjie(register int x)
{
    for(int i=1;i<=sqrt(x);i++){
        if(x%i==0){
            c[i]++;
            if(i!=x/i) c[x/i]++;
        }
    }
}
int read()
{
    char c=getchar();int tmp=0;
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') tmp=tmp*10+c-'0',c=getchar();
    return tmp;
}
intmain ()
{
    register int n;
    n=read();
    for(register int i=1;i<=n;i++){
        register int x;
        x=read();
        fenjie(x);
    }
    register int j=1000000;
    register int i=1;
    for(i=1;i<=n;i++){
        while(c[j]<i){
            --j;            
        }
           printf("%d\n",j);
    }
}

 

Reprinted in: https://www.cnblogs.com/kamimxr/p/11523249.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973165&siteId=291194637