codeforces F. Kate and imperfection

在这里插入图片描述

题目

题意:

给你一个数字 n n ,现在你需要将 1 1 ~ n n 中的数字放入 S S 中,从两个开始放,每次放一个,最后要使每一个的两两之间的最大的 g c d gcd 最小,输出每次的 g c d gcd

思路:

因为每次的 g c d gcd 最大的要最小,所以我们刚开始可以放素数进去,这样两两之间就都是 1 1 ,然后再放入 2 2 的倍数, 3 3 的倍数…,这样我们就可以用埃式筛法求出每一个数字的最大因子。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 5e5 + 10;
int a[maxn] = {0};
void init() {
    for (int i = 1; i < maxn; i++) {
        for (int j = 2 * i; j < maxn; j += i) {
            a[j] = i;
        }
    }
}
int main() {
    init();
    int n;
    read(n);
    sort(a + 2, a + n + 1);
    for (int i = 2; i <= n; i++) {
        printf("%d ", a[i]);
    }
    return 0;
}

发布了463 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_45031646/article/details/105486564