-4

Why is this failing for just a corner case? Question link-https://www.hackerearth.com/problem/algorithm/chandu-and-his-interns/description/#c190148 It ran fine for all the other cases. I took all the three cases where number of divisors could be less than 4. (prime number, 1 and square of prime number)

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if 
#define ff first 
#define ss second
#define pii pair<ll int ll int>
#define mp make_pair
#define pb push_back
#define CLEAR(array, value) memset(ptr, value, sizeof(array));
#define si(a)     scanf("%d", &a)
#define sl(a)     scanf("%lld", &a)
#define pi(a)     printf("%d", a)
#define pl(a)     printf("%lld", a)
#define pn        printf("\n")
#define int long long int 

int32_t main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,x,t,i;
    cin >> n;
    int max=10000009;
    int prime[max];
    int j;

    for(int i=2; i*i<=max; i++)
    {
        if (prime[i]==0)
        {
            for(j=i*i; j<=max; j+=i)
            {
                    prime[j]=1;

            }
        }
    }
    rep(i,n)
    {
        cin >> x;    
        t=sqrt(x);
        if (prime[x]==0)
             cout << "NO" << endl;
        else if ((t*t)==x && prime[t]==0)
             cout << "NO" << endl;
        else
             cout << "YES" << endl;   
    }


}
Reetik Raj
  • 11
  • 2
  • 2
    Please provide a list of the actual input you used, their actual output, and their expected output, please. Otherwise it's hard to understand what "corner cases" you mean. –  Jul 14 '19 at 20:52
  • 6
    Those defines are really messi... :/ – BlameTheBits Jul 14 '19 at 21:05
  • I'd recommend to use standard C++ and avoid any extensions such as variable-length arrays. – Walter Jul 14 '19 at 21:30
  • Please don’t redefine language keywords, it’s not even supposed to compile if you do that. – van dench Jul 14 '19 at 23:04
  • @vandench It's undefined behavior, so the standard allows the compiler to do anything. – BessieTheCookie Jul 15 '19 at 01:09
  • @AlexanderZhang Every compiler I’ve worked with explicitly prohibited it. Independent of what the standard says the standard compilers generally frown upon redefining keywords. – van dench Jul 15 '19 at 01:23
  • @vandench I `#define int long long` all the time when I'm writing crap code for contests. Both MSVC++ and GCC have no problem with that. – BessieTheCookie Jul 15 '19 at 03:53

2 Answers2

2

This appears to be a stack overflow.

You are allocating the prime array on the stack.

The failing test case is for value 9999863 which is close to the end of the prime array.

If you move the line to a non-stack based allocation, e.g. via

static int prime[10000009];

then all tests pass.

Peter de Rivaz
  • 30,956
  • 4
  • 41
  • 68
1

You should move these 2 lines outside main function and rename max variable because it will become ambiguous. Moreover max variable should be constant because ISO C++ forbids variable length array:

int max=10000009;
int prime[max];

So it should look like this

const int maximum=10000009;
int prime[maximum];

Then it passes all test cases.

Such big arrays shouldn't be allocated in main unless you don't increase stack size or use dynamic memory allocation because otherwise you will get stack overflow. Here you can read more about this.

Paweł Bęza
  • 875
  • 8
  • 20