Language: C++

/*
 #pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
*/
#pragma GCC optimize("O3")
#pragma GCC target ("sse4")

#define DEBUG 0
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <climits>
#include <list>
#include <functional>
#include <numeric>

//find out the use of the below
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> set_t;
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pdd pair<double,double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x)*(x))
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
#define EPS 1e-6
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define PI acos(-1)
long n, ans;
int gcd(int a, int b) {
    if (a == 0)
        return b;
    return gcd(b%a,a);
}
long bs(vector<pair<long, int> >&v, long fac) {
    vector<long> factors;
    vector<int> fq;
    int gc = -1;
    for (auto&& pr: v) {
        if (fac % pr.x == 0) {
            factors.pb(pr.x);
            int x = 0;
            while (fac % pr.x == 0) {
                fac/=pr.x;
                x++;
            }
            if (gc == -1)
                gc = x;
            else
                gc = gcd(gc, x);
            fq.pb(x);
        }
    }
    long res = 1;
    for (int i = 0; i < factors.size(); ++i) {
        res *= pow(factors[i], fq[i]/gc);
    }
    return res;
}

        

void perm(vector<pair<long, int> >& v, int ind, long fac) {
    if (ind == v.size()) {
        assert(n % fac == 0);
        if (fac == 1 || (n/fac)%fac == 0)
            return;
        if (DEBUG)
            cerr << "factor " << fac << endl;
       
        if ((n/fac-1)%bs(v, fac) == 0) {
            if (DEBUG)
                cerr << "chosen fac " << fac << endl;
            ans++;
        }
        return;
    }
    long prod = 1;
    for (int i = 0; i <= v[ind].y; ++i) {
        perm(v, ind+1, fac*prod);
        prod *= v[ind].x;
    }
}

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt","w", stdout);
    fio;
    cin >> n;
    ans = 1;
    long tmp = n-1;
    for (long i = 2; i*i <= n-1; ++i) {
        int cnt = 0;
        while(tmp%i == 0) {
            tmp/=i;
            cnt++;
        }
        ans = ans*(cnt+1);
        if (DEBUG && cnt != 0) {
            cerr << ans << " and i=" << i << " is curr proc of n-1\n";
        }
    }
    if (tmp > 1) {
        ans*=2;
    }
    ans--;
    if (DEBUG) {
        cerr << ans << " is (no of div of (n-1))-1\n";
    }
    vector<pair<long, int> > fq;
    tmp = n;
    for (long i = 2; i*i <= tmp; ++i) {
        if (tmp%i == 0){
            pair<long, int> pr = {i, 0};
            while (tmp%i == 0) {
                tmp/=i;
                pr.y++;
            }
            fq.pb(pr);
        }
    }
    if (tmp > 1) {
        fq.pb({tmp, 1});
    }
    if (DEBUG) {
        cerr << "print fq\n";
        for (auto&& x: fq) {
            cerr << x.x << ' ' << x.y << endl; 
        }
    }
    perm(fq, 0, 1);
    cout << ans;
    return 0;
}
/* BEFORE submitting, check for:
 *  1. Integer overflow
 *  2. Float/Double precision errors
 *  3. Array index out of bounds
 *  4. Edge cases.(n=1, n=max, etc.)
 *  5. Time complexity. No TLEs
 */



Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:9:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Vector_base<long int, std::allocator<long int> >::_Vector_impl::~_Vector_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = long int]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:17:
/usr/include/c++/14/bits/stl_vector.h:132:14: note: called from here
  132 |       struct _Vector_impl
      |              ^~~~~~~~~~~~