CF-1333F Kate and imperfection

F. Kate and imperfection

Suppose you put elements into the collection one by one. Obviously, before putting an element, we don't want to have its multiples already in the collection. Because before this, we might as well put this number first, and then put its multiple into it is better (because its multiple is easier to produce greater gcd with other numbers).

So when putting an element, all the factors of this element should already be in the set. For a set, if for all the numbers in the set, their factors are in the set, then the maximum of some two numbers in the set gcd is the largest true factor of a certain number (not itself).

Suppose \ (d [x] \) is the maximum true factor of x, and sort according to \ (d [x] \)

If the explanation of the first conjecture is not clear enough, you can refer to the official problem solution. First of all, you must know that the sequence of answers must not decrease, and increase in segments. Then if \ (a_i \ in A = \ {a_1, a_2, \ cdots a_k \} \) , if a factor of \ (a_i \) is not in the set, you can replace \ (a_i \) with this factor , The imperfection of this set will only decrease and not increase, so we have reason to make all factors of \ (a_i \) appear in this set.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
#define dbg(x...) do { cout << "\033[32;1m" << #x <<" -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<class T, class... Ts> void err(const T& arg,const Ts&... args) { cout << arg << " "; err(args...); }
const int N = 500000 + 5;
int primes[N], v[N], m;
void prime(int n){
    v[1] = 1;
    for(int i=2;i<=n;i++){
        if(!v[i]) primes[++m] = i, v[i] = 1;
        for(int j=1;j<=m;j++){
            if(primes[j] > n / i) break;
            v[primes[j]*i] = i;
            if(i % primes[j] == 0) break;
        }
    }
}

int main(){
    int n;scanf("%d", &n);
    prime(n);
    sort(v+1, v+1+n);
    for(int i=2;i<=n;i++)
        printf("%d ", v[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/1625--H/p/12687993.html