Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iomanip>
#include <iostream>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

void walk_on_multiplication_table() {
  long long int n;
  cin >> n;

  long long int ans = n;
  for (long long int i = 1; i <= n; ++i) {
    if (n % i == 0 && ans > (i - 1) + (n / i - 1)) {
      ans = (i - 1) + (n / i - 1);
    }
  }

  cout << ans << '\n';
}

void average_length() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];

  double sum = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j)
        continue;
      sum += sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2));
    }
  }

  cout << setprecision(14) << sum / n << '\n';
}

int calc_digits(long long int n) {
  int digits = 0;
  while (n >= 1) {
    n /= 10;
    ++digits;
  }

  return digits;
}

void buy_an_interger() {
  long long int a, b;
  long long int x;
  cin >> a >> b >> x;

  long long int left = 0, right = 1000000001;
  long long int mid;
  while (right - left > 1) {
    mid = left + (right - left) / 2;
    if (a * mid + b * calc_digits(mid) <= x)
      left = mid;
    else
      right = mid;
  }

  cout << left << '\n';
}

void honest_or_unkind2() {
  // 後回し
}

long long int gcd(int a, int b) {
  if (a < b) {
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
  }

  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

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

void snack() {
  long long int a, b;
  cin >> a >> b;

  cout << lcm(a, b) << "\n";
}

bool is_prime(int n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;

  if (n % 2 == 0)
    return false;

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }

  return true;
}

void next_prime() {
  int x;
  cin >> x;

  int prime = x;
  while (!is_prime(prime))
    ++prime;

  cout << prime << "\n";
}

int factorial(int n) {
  int fact = 1;
  for (int i = n; i > 0; --i)
    fact *= i;
  return fact;
}

int calc_order(int n, vector<int> x) {
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (x[i] - 1) * factorial(n - 1 - i);
    for (int j = i + 1; j < n; ++j) {
      if (x[j] > x[i])
        --x[j];
    }
  }
  return cnt;
}

void count_order() {
  int n;
  cin >> n;

  vector<int> p(n);
  vector<int> q(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> q[i];

  int p_cnt = 0, q_cnt = 0;
  p_cnt = calc_order(n, p);
  q_cnt = calc_order(n, q);

  cout << abs(p_cnt - q_cnt);
}

int main() {
  walk_on_multiplication_table();
  // average_length();
  // buy_an_interger();
  // cout << calc_digits(1000000000) << '\n';
  // honest_or_unkind2();
  // snack();
  // next_prime();
  // count_order();
}


Fixed submission:
#include <iomanip>
#include <iostream>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

void walk_on_multiplication_table() {
  long long int n;
  cin >> n;

  long long int ans = n;
  for (int i = 1; i <= sqrt(n); ++i) {
    if (n % i == 0 && ans > (i - 1) + (n / i - 1)) {
      ans = (i - 1) + (n / i - 1);
    }
  }

  cout << ans << '\n';
}

void average_length() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];

  double sum = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j)
        continue;
      sum += sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2));
    }
  }

  cout << setprecision(14) << sum / n << '\n';
}

int calc_digits(long long int n) {
  int digits = 0;
  while (n >= 1) {
    n /= 10;
    ++digits;
  }

  return digits;
}

void buy_an_interger() {
  long long int a, b;
  long long int x;
  cin >> a >> b >> x;

  long long int left = 0, right = 1000000001;
  long long int mid;
  while (right - left > 1) {
    mid = left + (right - left) / 2;
    if (a * mid + b * calc_digits(mid) <= x)
      left = mid;
    else
      right = mid;
  }

  cout << left << '\n';
}

void honest_or_unkind2() {
  // 後回し
}

long long int gcd(int a, int b) {
  if (a < b) {
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
  }

  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

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

void snack() {
  long long int a, b;
  cin >> a >> b;

  cout << lcm(a, b) << "\n";
}

bool is_prime(int n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;

  if (n % 2 == 0)
    return false;

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }

  return true;
}

void next_prime() {
  int x;
  cin >> x;

  int prime = x;
  while (!is_prime(prime))
    ++prime;

  cout << prime << "\n";
}

int factorial(int n) {
  int fact = 1;
  for (int i = n; i > 0; --i)
    fact *= i;
  return fact;
}

int calc_order(int n, vector<int> x) {
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (x[i] - 1) * factorial(n - 1 - i);
    for (int j = i + 1; j < n; ++j) {
      if (x[j] > x[i])
        --x[j];
    }
  }
  return cnt;
}

void count_order() {
  int n;
  cin >> n;

  vector<int> p(n);
  vector<int> q(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> q[i];

  int p_cnt = 0, q_cnt = 0;
  p_cnt = calc_order(n, p);
  q_cnt = calc_order(n, q);

  cout << abs(p_cnt - q_cnt);
}

int main() {
  walk_on_multiplication_table();
  // average_length();
  // buy_an_interger();
  // cout << calc_digits(1000000000) << '\n';
  // honest_or_unkind2();
  // snack();
  // next_prime();
  // count_order();
}