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

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  vector<int> furui(300001, 0);
  vector<int> furui2(300001, 0);
  furui[0] = furui[1] = furui2[0] = furui2[1] = 1;
  for (int i = 2; i < 300001; i++) {
    if (i % 7 == 1 || i % 7 == 6) {
    } else {
      furui[i] = furui2[i] = 1;
    }
  }
  for (long long int i = 6; i < 300001; i++) {
    if (furui[i] == 0) {
      for (long long int j = i; j < 300001; j++) {
        if (furui2[j] == 0) {
          if (i * j < 300001) {
            furui[i * j] = 1;
          } else {
            break;
          }
        }
      }
    }
  }
  int n;
  while (cin >> n) {
    if (n == 1) {
      break;
    }
    priority_queue<int, vector<int>, greater<int>> pq;
    cout << n << ":";
    int kari;
    if (furui[n] != 0) {
      vector<int> furui3(300001, 0);
      for (int i = 6; i < n; i++) {
        kari = n;
        if (furui[i] == 0 && furui3[i] == 0) {
          if (i > n) {
            break;
          }
          if (n % i == 0) {
            queue<int> q;
            kari /= i;
            q.push(i);
            for (int j = 6; j < n; j++) {
              if (furui[j] == 0 && kari % j == 0) {
                q.push(j);
                while (kari % j == 0) {
                  kari /= j;
                }
              }
              if (kari == 1) {
                furui3[i] = 1;
                while (!q.empty()) {
                  pq.push(q.front());
                  furui3[q.front()] = 1;
                  q.pop();
                }
              }
              if (kari % 7 != 1 && kari % 7 != 6) {
                break;
              }
            }
          }
        }
      }
    } else {
      pq.push(n);
    }
    int a = pq.top();
    pq.pop();
    cout << " " << a;
    while (!pq.empty()) {
      if (pq.top() != a) {
        cout << " " << pq.top();
        a = pq.top();
      }
      pq.pop();
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  vector<int> furui(300001, 0);
  vector<int> furui2(300001, 0);
  furui[0] = furui[1] = furui2[0] = furui2[1] = 1;
  for (int i = 2; i < 300001; i++) {
    if (i % 7 == 1 || i % 7 == 6) {
    } else {
      furui[i] = furui2[i] = 1;
    }
  }
  for (long long int i = 6; i < 300001; i++) {
    if (furui[i] == 0) {
      for (long long int j = i; j < 300001; j++) {
        if (furui2[j] == 0) {
          if (i * j < 300001) {
            furui[i * j] = 1;
          } else {
            break;
          }
        }
      }
    }
  }
  int n;
  while (cin >> n) {
    if (n == 1) {
      break;
    }
    priority_queue<int, vector<int>, greater<int>> pq;
    cout << n << ":";
    int kari;
    if (furui[n] != 0) {
      for (int i = 6; i < 300001; i++) {
        kari = n;
        if (furui[i] == 0) {
          if (i > n) {
            break;
          }
          if (n % i == 0) {
            queue<int> q;
            kari /= i;
            q.push(i);
            for (int j = 6; j < 300001; j++) {
              if (furui[j] == 0 && kari % j == 0) {
                q.push(j);
                while (kari % j == 0) {
                  kari /= j;
                }
              }
              if (kari == 1) {
                while (!q.empty()) {
                  pq.push(q.front());
                  q.pop();
                }
              }
              if (kari % 7 != 1 && kari % 7 != 6) {
                break;
              }
            }
          }
        }
      }
    } else {
      pq.push(n);
    }
    int a = pq.top();
    pq.pop();
    cout << " " << a;
    while (!pq.empty()) {
      if (pq.top() != a) {
        cout << " " << pq.top();
        a = pq.top();
      }
      pq.pop();
    }
    cout << endl;
  }
  return 0;
}