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

#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
int n;
int mod_pow(int x, int n, int mod) {
  int res = 1;
  while (n) {
    if (n % 2)
      res = res * x % mod;
    x = x * x % mod;
    n /= 2;
  }
  return res;
}

int f(int a) { return mod_pow(a, n, n); }
int F(int a, int k) {
  if (k == 1)
    return f(a);
  return F(f(a), k - 1);
}

int mem[100];
int F2(int a, int k) {
  if (k == 1)
    return mem[a] = f(a);
  return mem[a] = mod_pow(mem[a], n, n);
}

double calcTime() {
  static clock_t start = clock(); // ??????????????????
  clock_t end = clock();          // ????????????
  return (double)(end - start) / CLOCKS_PER_SEC;
}

bool check(int k) {
  int res = 1;
  for (int i = 2; i < min(5LL, n); i++) {
    int a = i;
    if (F(a, k) != a)
      res = 0;
  }
  return res;
}

int solve() {
  int cnt = 0;
  for (int k = 1; calcTime() < 1000; k++, cnt++)
    if (check(k))
      return k;
  return -1;
}
void factor(int n) {
  for (int i = 1; i <= n; i++) {
    if (n % i == 0)
      cout << i << " ";
  }
  cout << endl;
}

int lcm(int a, int b) { return (a / __gcd(a, b)) * b; }
int carmichealLambda(int n) {
  int res = 1;
  if (n % 8 == 0)
    n /= 2;

  auto compute = [&](int i) {
    int sub = i - 1;
    n /= i;
    while (n % i == 0)
      n /= i, sub *= i;
    res = lcm(res, sub);
  };

  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      compute(i);
  if (n != 1)
    compute(n);
  return res;
}

signed main() {
  cin >> n;
  int car = carmichealLambda(n);
  int car2 = carmichealLambda(car);
  if (car == 1) {
    cout << 1 << endl;
    return 0;
  }

  // cout<<"car="<<car<<endl;
  // cout<<"car2="<<car2<<endl;
  // cout<<"mod"<<mod_pow(n,car2,car)<<endl;

  int ans = car2;
  if (mod_pow(n, 1, car) == 1)
    ans = 1;

  for (int i = 1; i * i <= car2; i++) {
    if (mod_pow(n, car2 / i, car) == 1)
      Min(ans, car2 / i);
    if (mod_pow(n, i, car) == 1)
      Min(ans, i);
  }
  if (mod_pow(n, car2, car) != 1)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
int n;
int mod_pow(int x, int n, int mod) {
  int res = 1;
  while (n) {
    if (n % 2)
      res = res * x % mod;
    x = x * x % mod;
    n /= 2;
  }
  return res;
}

int f(int a) { return mod_pow(a, n, n); }
int F(int a, int k) {
  if (k == 1)
    return f(a);
  return F(f(a), k - 1);
}

int mem[100];
int F2(int a, int k) {
  if (k == 1)
    return mem[a] = f(a);
  return mem[a] = mod_pow(mem[a], n, n);
}

double calcTime() {
  static clock_t start = clock(); // ??????????????????
  clock_t end = clock();          // ????????????
  return (double)(end - start) / CLOCKS_PER_SEC;
}

bool check(int k) {
  int res = 1;
  for (int i = 2; i < min(5LL, n); i++) {
    int a = i;
    if (F(a, k) != a)
      res = 0;
  }
  return res;
}

int solve() {
  int cnt = 0;
  for (int k = 1; calcTime() < 1; k++, cnt++)
    if (check(k))
      return k;
  return -1;
}

vector<bool> visited;
int find(int num) {
  if (visited[num])
    return 0;
  visited[num] = 1;
  int res = 1 + find(F(num, 1));
  visited[num] = 0;
  return res;
}

int find2(int start) {
  int num = F(start, 1);
  int cnt = 1;
  while (num != start)
    num = F(num, 1), cnt++;
  return cnt;
}

int lcm(int a, int b) { return (a / __gcd(a, b)) * b; }

int solve2() {
  visited.resize(n, 0);
  int res = 1;
  for (int i = 0; i < 3; i++) {
    int num = rand() % (n - 2) + 1;
    res = max(res, find2(num));
  }
  cout << "res=" << res << endl;
  return check(res) ? res : -1;
}

void factor(int n) {
  for (int i = 1; i <= n; i++) {
    if (n % i == 0)
      cout << i << " ";
  }
  cout << endl;
}

int carmichealLambda(int n) {
  int res = 1;
  if (n % 8 == 0)
    n /= 2;

  for (int i = 2; i <= n; i++) {
    if (n % i != 0)
      continue;
    int sub = i - 1;
    n /= i;
    while (n % i == 0)
      n /= i, sub *= i;
    res = lcm(res, sub);
  }
  return res;
}

signed main() {
  srand((unsigned)time(NULL));
  cin >> n;
  // cout<<solve2()<<endl;

  /*  int cnt = 0;
  for(int i=1;i<n;i++){
    continue;
    if(__gcd(i,n) == 1) cnt++;
    int flag = 0;
    for(int k=1;k<10;k++)if(F(i,k) == 2) flag = 1;
    if(!flag) continue;
    cout<<"i="<<i<<":";
    for(int k=1;k<10;k++)cout<<"\\t"<<F(i,k);
    cout<<endl;
  }
  */

  int car = carmichealLambda(n);
  if (car == 1) {
    cout << 1 << endl;
    return 0;
  }
  int c = n % car, b = 1;
  int find = 1;
  vector<bool> used(car);
  while (c % car != 1) {
    if (used[c]) {
      find = 0;
      break;
    }
    used[c] = 1;
    c = c * n % car, b++;
  }
  if (find)
    cout << b << endl;
  else
    cout << -1 << endl;
  return 0;
}