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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define lson i << 1
#define rson i << 1 | 1
#define up(i, x, y) for (i = x; i <= y; i++)
#define down(i, x, y) for (i = x; i >= y; i--)
#define mem(a, x) memset(a, x, sizeof(a))
#define gi(x) scanf("%d", &x)
#define gi2(x, y) scanf("%d%d", &x, &y)
#define gll(x) scanf("%lld", &x)
#define gll2(x, y) scanf("%lld%lld", &x, &y)
#define gc(x) scanf("%c", &x)
#define gc2(x, y) scanf("%c%c", &x, &y)
using namespace std;
const double eps = 1e-8;
typedef long long ll;
const double PI = acos(-1.0);
const int MAXN = 100005;
const ll mod = 1e7 + 9;
const ll llinf = (ll)(1e18) + 500;
const int inf = 0x3f3f3f3f;
ll n, n2;
vector<ll> di, zhi;
int cnt = 0;
vector<ll> divisor;
void init() {
  n2 = n;
  if (n < 2)
    return; // 小于2的数不合法，若n为质数则输出它本身
  for (ll i = 2; i * i <= n2; i++) // 根号n复杂度
  {
    if (n2 % i == 0) {
      int cnt = 0;
      di.push_back(i);
      while (n2 % i == 0) {
        n2 = n2 / i;
        cnt++;
      }
      zhi.push_back(cnt);
    }
  }
  if (n2 != 1) {
    di.push_back(n2);
    zhi.push_back(1LL);
  }
  return;
}
vector<ll> tmp, tmp1;
ll npow(ll a, int b) {
  ll num = 1;
  while (b--) {
    num *= a;
  }
  return num;
}
void dfs(int cur, ll k, int z) {
  if (cur == tmp.size()) {
    if (k == 1)
      return;
    ll t = n / npow(k, z) - 1;
    if (t % k == 0) {
      cnt++;
    }
    return;
  }
  dfs(cur + 1, k, z);
  for (int i = 1; i <= tmp1[cur]; i++) {
    dfs(cur + 1, k * tmp[cur], z);
    k *= tmp[cur];
  }
}
int main() {

  cin >> n;
  init();
  //	for(int i=0;i<zhi.size();i++){
  //		cout<<di[i]<<' '<<zhi[i]<<endl;
  //	}
  n--;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i) {
        cnt++;
      } else {
        cnt += 2;
      }
    }
  }
  cnt--;
  n++;
  ll maxn = 0;
  for (int i = 0; i < zhi.size(); i++) {
    maxn = max(maxn, zhi[i]);
  }
  for (int i = 1; i <= maxn; i++) {
    tmp.clear();
    tmp1.clear();
    for (int j = 0; j < zhi.size(); j++) {
      if (zhi[j] >= i) {
        tmp.push_back(di[j]);
        tmp1.push_back(zhi[j] / i);
      }
    }
    dfs(0, 1, i);
  }
  printf("%d\\n", cnt);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define lson i << 1
#define rson i << 1 | 1
#define up(i, x, y) for (i = x; i <= y; i++)
#define down(i, x, y) for (i = x; i >= y; i--)
#define mem(a, x) memset(a, x, sizeof(a))
#define gi(x) scanf("%d", &x)
#define gi2(x, y) scanf("%d%d", &x, &y)
#define gll(x) scanf("%lld", &x)
#define gll2(x, y) scanf("%lld%lld", &x, &y)
#define gc(x) scanf("%c", &x)
#define gc2(x, y) scanf("%c%c", &x, &y)
using namespace std;
const double eps = 1e-8;
typedef long long ll;
const double PI = acos(-1.0);
const int MAXN = 100005;
const ll mod = 1e7 + 9;
const ll llinf = (ll)(1e18) + 500;
const int inf = 0x3f3f3f3f;
ll n, n2;
vector<ll> di, zhi;
int cnt = 0;
vector<ll> divisor;
void init() {
  n2 = n;
  if (n < 2)
    return; // 小于2的数不合法，若n为质数则输出它本身
  for (ll i = 2; i * i <= n2; i++) // 根号n复杂度
  {
    if (n2 % i == 0) {
      int cnt = 0;
      di.push_back(i);
      while (n2 % i == 0) {
        n2 = n2 / i;
        cnt++;
      }
      zhi.push_back(cnt);
    }
  }
  if (n2 != 1) {
    di.push_back(n2);
    zhi.push_back(1LL);
  }
  return;
}
vector<ll> tmp;
ll npow(ll a, int b) {
  ll num = 1;
  while (b--) {
    num *= a;
  }
  return num;
}
void dfs(int cur, ll k, int z) {
  if (cur == tmp.size()) {
    if (k == 1)
      return;
    ll t = n / npow(k, z) - 1;
    if (t % k == 0) {
      cnt++;
    }
    return;
  }
  dfs(cur + 1, k, z);
  dfs(cur + 1, k * tmp[cur], z);
}
int main() {

  cin >> n;
  init();
  //	for(int i=0;i<zhi.size();i++){
  //		cout<<di[i]<<' '<<zhi[i]<<endl;
  //	}
  n--;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i) {
        cnt++;
      } else {
        cnt += 2;
      }
    }
  }
  cnt--;
  n++;
  ll maxn = 0;
  for (int i = 0; i < zhi.size(); i++) {
    maxn = max(maxn, zhi[i]);
  }
  for (int i = 1; i <= maxn; i++) {
    tmp.clear();
    for (int j = 0; j < zhi.size(); j++) {
      if (zhi[j] >= i) {
        int t = zhi[j];
        while (t >= i) {
          tmp.push_back(di[j]);
          t -= i;
        }
      }
    }
    dfs(0, 1, i);
  }
  printf("%d\\n", cnt);

  return 0;
}