Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>

int pr[50847534];
int fact[5000];
int x = 0;
int n, pn = 0;
short *arr;

int nextpr(int p) {
  int nxp = p + 2;
  int i;
  int ok;
  pr[x] = p;
  x++;
  if (p == 2)
    return 3;
  while (1) {
    /*printf("p=%d->",nxp);*/
    for (i = 0; pr[i] * pr[i] <= nxp; i++) {
      ok = 1;
      if (!(nxp % pr[i])) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      /*printf("O\\n");*/
      return nxp;
    } else {
      /*printf("X\\n");*/
      nxp += 2;
    }
  }
}

int main() {
  int n2;
  int p = 2, prod, ans = 0;
  int i, j, k;
  scanf("%d", &n);
  n2 = n;
  while (n > 1) {
    if (!(n % p)) {
      while (!(n % p))
        n /= p;
      fact[pn] = p;
      pn++;
    } else {
      p = nextpr(p);
    }
    if (p * p > n) {
      /*?´???°?????£??????????????????????????????*/
      if (n - 1) {
        fact[pn] = n;
        pn++;
      }
      break;
    }
  }
  /*printf("facts:");
  for(i=0;i<pn;i++){
      printf(" %d",fact[i]);
  }
  printf("\\n");*/
  arr = (short *)calloc(pn, sizeof(short));
  for (i = 0; i < pn; i++)
    arr[i] = 0;
  while (1) {
    /*for(i=0;i<pn;i++)printf(" %d",arr[i]);*/
    prod = 1;
    for (j = 0; j < pn; j++) {
      if (arr[j]) {
        prod *= fact[j];
        prod *= -1;
      }
    }
    ans += (n2 / prod);
    /*printf(" prod=%d ans=%d\\n",prod,ans);*/
    /*????????????arr????¬???????????????????????????????*/
    arr[0] += 1;
    for (k = 0; k < pn; k++) {
      if (arr[k] > 1) {
        if (k == pn - 1) {
          printf("%d\\n", ans);
          exit(0);
        }
        arr[k] = 0;
        arr[k + 1] += 1;
      }
    } /*???????????§arr????¬???????????????????????????????*/
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

int pr[50847534];
int fact[5000];
int x = 0;
int n, pn = 0;
short *arr;

int nextpr(int p) {
  int nxp = p + 2;
  int i;
  int ok;
  pr[x] = p;
  x++;
  if (p == 2)
    return 3;
  while (1) {
    /*printf("p=%d->",nxp);*/
    for (i = 0; pr[i] * pr[i] <= nxp; i++) {
      ok = 1;
      if (!(nxp % pr[i])) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      /*printf("O\\n");*/
      return nxp;
    } else {
      /*printf("X\\n");*/
      nxp += 2;
    }
  }
}

int main() {
  int n2;
  int p = 2, done = 1, prod, ans = 0;
  int i, j, k;
  scanf("%d", &n);
  n2 = n;
  while (n > 1) {
    if (!(n % p)) {
      while (!(n % p))
        n /= p;
      fact[pn] = p;
      pn++;
      done = 0;
    } else {
      p = nextpr(p);
    }
    if (p * p > n) {
      fact[pn] = n;
    }
  }
  /*printf("facts:");
  for(i=0;i<pn;i++){
      printf(" %d",fact[i]);
  }
  printf("\\n");*/
  arr = (short *)calloc(pn, sizeof(short));
  for (i = 0; i < pn; i++)
    arr[i] = 0;
  while (1) {
    /*for(i=0;i<pn;i++)printf(" %d",arr[i]);*/
    prod = 1;
    for (j = 0; j < pn; j++) {
      if (arr[j]) {
        prod *= fact[j];
        prod *= -1;
      }
    }
    ans += (n2 / prod);
    /*printf(" prod=%d ans=%d\\n",prod,ans);*/
    /*????????????arr????¬???????????????????????????????*/
    arr[0] += 1;
    for (k = 0; k < pn; k++) {
      if (arr[k] > 1) {
        if (k == pn - 1) {
          printf("%d\\n", ans);
          exit(0);
        }
        arr[k] = 0;
        arr[k + 1] += 1;
      }
    } /*???????????§arr????¬???????????????????????????????*/
  }
}