Submission

Status:
-PPPPPPPPP-PxPPPxxPP

Score: 75

User: Dormon

Problemset: จำนวนเฉพาะก่อนหน้า

Language: cpp

Time: 0.017 second

Submitted On: 2024-12-11 18:18:01

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <queue>
#include <numeric>
#include <stack>

#define debug(...) Debug(#__VA_ARGS__, __VA_ARGS__)
using namespace std;
const bool TEST_CASE = 0;

template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type
Debug(const char* name, T value) {
    std::cout << name << " : " << value << '\n';
}

template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {
    for(auto &x : a) out << x << ' '; 
    return out;
};
template<typename T, typename... Args>
typename std::enable_if<std::is_integral<T>::value>::type
Debug(const char* names, T value, Args... args) {
    const char* comma = strchr(names, ',');
    std::cout.write(names, comma - names) << " : " << value << " | ";
    Debug(comma + 1, args...);
}

void solve(){
    int n;
    cin >> n;
    vector<int> prime(n+2, true);
    prime[0] = prime[1] = false;
    for (int i = 2;i <= n;i++){
        if (!prime[i]) continue;
        for (int j = i<<1;j <= n;j+=i)
            prime[j] = false;
    }
    stack<int> ans;
    int cnt = 0;
    for (int i = n;i >= 2 && cnt < 5;i--){
        if (prime[i]){
            ans.push(i);
            cnt++;
        }
    }
    while (!ans.empty()){
        cout << ans.top() << ' ';
        ans.pop();
    }
}

int main()
{
    #ifndef DORMON
        ios_base::sync_with_stdio(0); 
    #endif
    cin.tie(0);
    int q = 1; 
    if (TEST_CASE) cin >> q;
    while (q--){
        solve();
    }
}