-7

I have been struggling with this problem http://www.spoj.com/problems/PRIME1/ and if anyone can help me finding the error in my code. I have used segmented sieve of eratosthenes and I have also looked through a lot of online resources but somehow I am getting a runtime error on spoj. Thanks

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <cmath>
    #include <map>
    #include <cstdlib>
    #include <cassert>
    #define fora(i,a,b) for(i = a; i < b; i++)
    #define fin(f) freopen(f, "r", stdin)
    #define fout(f) freopen(f, "w", stdout)
    using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;
typedef vector<bool> vb;

const ll LIMIT = 1000000000;

void segmentedSieve(ll n, ll m, int segment_size) {
    int i, j, s, p, range;
    vb is_prime(range+1, true);
    vb seg_primes(segment_size+1, true);
    vi prime;

    range = floor(sqrt((double)n));

    fora (i, 2, range+1)
        if (is_prime[i]) {
            for (j = i*2; j <= range; j+=i)
                is_prime[j] = false;
        }

    fora (i, 2, range+1)
        if (is_prime[i] == 1)
            prime.push_back(i);

    fora (i, 0, prime.size()) {
        p = prime[i];
        s = m/p;
        s *= p;

        for (j = s; j <= n; j+=p) {
            if (j < m) continue;
            seg_primes[j-m] = false;
        }
    }

    fora (i, 0, prime.size())
        if (prime[i] >= m && prime[i] <= n) {
            cout << prime[i] << endl;
        }

    fora (i, 0, n-m+1)
        if (seg_primes[i] && (i+m) != 1) {
            cout << i+m << endl;
        }
}

int main()
{
    int segment_size = 100000;
    // fin("input.in");
    int t;
    cin >> t;
    while (t--) {
        ll a, b;
        cin >> a >> b;
        if (a > b)
            segmentedSieve(a, b, segment_size);
        else
            segmentedSieve(b, a, segment_size);
        if (t != 0)
            cout << endl;
    }
}
  • 5
    Now's the time to learn to use a debugger. (And do yourself a favor and remove those macros, they're horrible.) – Mat Jan 02 '16 at 08:06
  • 1
    Those macros buy you nothing except obfuscated code. There is no need to have a macro defining a `for` loop -- we all know what a `for` loop looks like. We all know what `long long` is, etc.. I agree with Mat, they're horrible. As to `long long` I saw another SPOJ post a few days ago that uses the same horrible macros. Is SPOJ teaching you to use this? – PaulMcKenzie Jan 02 '16 at 08:12
  • Hahaha...just helps to code a lil bit faster :P – Bhavya Jain Jan 02 '16 at 10:15

1 Answers1

0

It seems range is uninitialized here:

void segmentedSieve(ll n, ll m, int segment_size) {
    int i, j, s, p, range;
    vb is_prime(range+1, true);   // uninitialized range... !!

maybe you want

void segmentedSieve(ll n, ll m, int segment_size) {
    int i, j, s, p, range;

    range = floor(sqrt((double)n));  // This first...

    vb is_prime(range+1, true);      // then this

When possible you should initialize variables when you define them.

void segmentedSieve(ll n, ll m, int segment_size) {
    int i, j, s, p; // no range here

    int range = floor(sqrt((double)n));  // This first...

    vb is_prime(range+1, true);      // then this

In general you should postpone the definition of variables until you need them, i.e. don't define all variables in the start but do it as you need them.

p.s. As others have commented already - get rid of all that macro stuff...

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63