Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define max 9000
- #define pb push_back
- #define pairs pair<int, int>
- #define vi vector<int>
- #define vb vector<bool>
- #define vii vector<pairs>
- #define lb lower_bound
- #define ub upper_bound
- #define lli long long int
- #define endl '\n'
- using namespace std;
- bool isPrime[max];
- vi Prime;
- vi output;
- void sieve() {
- memset(isPrime, true, sizeof(isPrime)); // first all odd number's prime
- isPrime[0] = 0;
- // Sieve
- for (int i = 2; i * i <= max; i++) {
- if(isPrime[i]) {
- for (int j = i * i; j <= max; j += i) {
- isPrime[j] = 0;
- }
- }
- }
- }
- int middle(int n) {
- return n / 2;
- }
- void handleEven(int c, int mid) {
- int count = 0;
- int index = mid;
- output.push_back(Prime[mid]);
- while(true) {
- if(count == c - 1) {
- break;
- }
- output.push_back(Prime[++index]);
- count++;
- }
- count = 0;
- index = mid;
- while(true) {
- if(count == c) {
- break;
- }
- output.push_back(Prime[--index]);
- count++;
- }
- }
- void handleOdd(int c, int mid) {
- int count = 0;
- int index = mid;
- output.push_back(Prime[mid]);
- while(true) {
- if(count == c - 1) {
- break;
- }
- output.push_back(Prime[++index]);
- count++;
- }
- count = 0;
- index = mid;
- while(true) {
- if(count == c - 1) {
- break;
- }
- output.push_back(Prime[--index]);
- count++;
- }
- }
- void print(int n, int c) {
- cout << n << " " << c << ":";
- if(n == 1) {
- cout << " " << 1 << endl;
- } else {
- for (int i : output) {
- if(i < Prime[n])
- cout << " " << i;
- }
- cout << endl;
- }
- }
- int main() {
- #ifndef Niloy
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- sieve();
- for (int i = 0; i <= max; i++) {
- if(isPrime[i] == 1) {
- Prime.push_back(i);
- }
- }
- int n, c, length;
- while(cin >> n >> c) {
- output.clear();
- length = 0;
- for (int i = 0; i < n; i++) {
- if(Prime[i] <= n) {
- length++;
- }
- }
- int mid = middle(length);
- if(n == 1) {
- print(n, c);
- } else {
- if(length < c) {
- for (int i = 0; i < length; i++) {
- output.push_back(Prime[i]);
- }
- } else if(length % 2 == 0) {
- handleEven(c, mid);
- } else {
- handleOdd(c, mid);
- }
- sort(output.begin(), output.end());
- print(n, c);
- }
- cout << endl;
- output.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement