Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>

bool exist[1000004];
bool duplicate[1000004];

bool dfs(std::vector<int> pr, std::vector<int> yakusuu, int val, int index,
         int orig) {
  if (index == yakusuu.size()) {
    if (val == orig)
      return false;
    else if (exist[val])
      return true;
    else
      return false;
  } else {
    bool ret = false;
    int target = yakusuu[index];
    int num = pr[target];
    int multiple = 1;
    for (int i = 0; i <= num; i++) {
      ret |= dfs(pr, yakusuu, val * multiple, index + 1, orig);
      multiple *= target;
      if (ret)
        return true;
    }
    return ret;
  }
}

int main() {
  int N;
  scanf("%d", &N);
  std::vector<int> A(N);
  for (int i = 0; i < 1000004; i++) {
    exist[i] = false;
    duplicate[i] = false;
  }
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  std::sort(A.begin(), A.end());
  for (int i = 0; i < N; i++) {
    if (exist[A[i]]) {
      duplicate[A[i]] = true;
      ;
    } else {
      exist[A[i]] = true;
    }
  }
  int ans = 0;
  std::vector<int> prime;
  std::vector<bool> isprime(10004, true);
  for (int i = 2; i * i <= 1000004; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j * j < 1000004; j += i) {
        isprime[j] = false;
      }
    }
  }
  std::sort(prime.begin(), prime.end());

  std::vector<int> pr(1000004, 0);
  for (int i = 0; i < N; i++) {
    // printf("i=%d\n", i);
    if (duplicate[A[i]])
      continue;
    std::vector<int> yakusuu;
    int temp = A[i];
    for (int p : prime) {
      // printf("temp=%d, p=%d\n", temp, p);
      if (p * p > temp)
        break;
      while (temp % p == 0) {
        if (pr[p] == 0)
          yakusuu.push_back(p);
        pr[p]++;
        temp = temp / p;
        // printf("in while temp=%d, p=%d\n", temp, p);
      }
    }
    if (temp != 1) {
      yakusuu.push_back(temp);
      pr[temp]++;
    }
    // printf("prime calc end\n");
    bool existyakusuu = dfs(pr, yakusuu, 1, 0, A[i]);
    if (!existyakusuu)
      ans++;
    // printf("exist yakusuu = %d\n", existyakusuu);

    for (int p : yakusuu) {
      pr[p] = 0;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>

bool exist[1000004];
bool duplicate[1000004];

bool dfs(std::vector<int> &pr, std::vector<int> &yakusuu, int val, int index,
         int orig) {
  if (index == yakusuu.size()) {
    if (val == orig)
      return false;
    else if (exist[val])
      return true;
    else
      return false;
  } else {
    bool ret = false;
    int target = yakusuu[index];
    int num = pr[target];
    int multiple = 1;
    for (int i = 0; i <= num; i++) {
      ret |= dfs(pr, yakusuu, val * multiple, index + 1, orig);
      multiple *= target;
      if (ret)
        return true;
    }
    return ret;
  }
}

int main() {
  int N;
  scanf("%d", &N);
  std::vector<int> A(N);
  for (int i = 0; i < 1000004; i++) {
    exist[i] = false;
    duplicate[i] = false;
  }
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  std::sort(A.begin(), A.end());
  for (int i = 0; i < N; i++) {
    if (exist[A[i]]) {
      duplicate[A[i]] = true;
      ;
    } else {
      exist[A[i]] = true;
    }
  }
  int ans = 0;
  std::vector<int> prime;
  std::vector<bool> isprime(10004, true);
  for (int i = 2; i * i <= 1000004; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j * j < 1000004; j += i) {
        isprime[j] = false;
      }
    }
  }
  std::sort(prime.begin(), prime.end());

  std::vector<int> pr(1000004, 0);
  for (int i = 0; i < N; i++) {
    // printf("i=%d\n", i);
    if (duplicate[A[i]])
      continue;
    std::vector<int> yakusuu;
    int temp = A[i];
    for (int p : prime) {
      // printf("temp=%d, p=%d\n", temp, p);
      if (p * p > temp)
        break;
      while (temp % p == 0) {
        if (pr[p] == 0)
          yakusuu.push_back(p);
        pr[p]++;
        temp = temp / p;
        // printf("in while temp=%d, p=%d\n", temp, p);
      }
    }
    if (temp != 1) {
      yakusuu.push_back(temp);
      pr[temp]++;
    }
    // printf("prime calc end\n");
    bool existyakusuu = dfs(pr, yakusuu, 1, 0, A[i]);
    if (!existyakusuu)
      ans++;
    // printf("exist yakusuu = %d\n", existyakusuu);

    for (int p : yakusuu) {
      pr[p] = 0;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}