Codeforces Round #191 (Div. 2) 327B. Hungry Sequence Solution



 Problem  Link  : https://codeforces.com/problemset/problem/327/B

Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>

using namespace std;
#define ll long long int
const ll N = 1e7;
vector < ll > prime;
ll i, j;
bool vis[N];
void sieve() {
  for (i = 3; i * i <= N; i += 2) {
    if (vis[i] == 0) {
      for (j = i * i; j <= N; j += 2 * i) {
        vis[j] = 1;
      }
    }
  }
  prime.push_back(2);
  for (i = 3; i <= N; i += 2) {
    if (vis[i] == 0) {
      prime.push_back(i);
    }
  }
}
int main() {
  ll t, n, i, cn = 0;
  sieve();
  cin >> n;

  for (i = 0; n > 0; i++) {
    cn++;
    cout << prime[i] << " ";
    n--;
  }
}

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.