F. Kate and imperfection

Title Portal

Topic: In the n numbers from 1 to n, for k ∈ [2, n], take the k numbers from the n numbers, perform gcd on the k numbers in pairs, and output the minimum maximum value of the gcd.

Idea: Because gcd is required, we can think of the update operation of the Ehrlich sieve. Every time we encounter an i, we mark the multiple of i. Among the marked numbers, the current largest factor is i. After we have sieved within n, what is stored in the array is his largest factor within n. However, for prime numbers, he has not been screened by other numbers, so the array stores 0, we replace all 0s with 1, and then output the array n-1 from small to large.

Proof of correctness: Because we are seeking the minimum and maximum value of two numbers gcd, so we take the prime number as much as possible. After the prime number is taken, then take a number, there must be a pair of non-prime primes, so we must try to make the answer Become smaller, that is, take the small first, and finally you can get the conclusion from small to large output.

Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=0x7fffffff;
const int mod=1e9+7;
const int eps=1e-6;
typedef long long ll;
typedef unsigned long long ull;
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define null NULL
int ok[N];
signed main()
{
    IOS;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        for(int j=i*2;j<=n;j+=i)
        {
            ok[j]=i;
        }
    }
    priority_queue<int,vector<int>,greater<int> >q;
    for(int i=2;i<=n;i++)
    {
        if(ok[i]==0)
        {
            q.push(1);
        }
        else
        {
            q.push(ok[i]);
        }
    }
    while(!q.empty())
    {
        cout<<q.top()<<' ';
        q.pop();
    }
    cout<<endl;
}
Published 93 original articles · won praise 9 · views 4203

Guess you like

Origin blog.csdn.net/Joker_He/article/details/105423207