Problem p03241 (C++)

Buggy submission (status: Runtime Error):
// Devarshi

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (i = 0; i < n; i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it, x) for (auto it : x)
#define PI 3.14159265
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 1000001;

bool isprime(int n) {
  int n1 = n;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  // if(n1==n) return true;
  return true;
}

int main() {

  sync

#ifndef ONLINE_JUDGE
      // for getting input from input.txt
      freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("out.txt", "w", stdout);
#endif

  int n, m;
  cin >> n >> m;
  if (m % n == 0)
    cout << m / n;
  else {
    // cout<<1<<endl;
    if (isprime(m))
      cout << 1;
    else {
      int i = 1;
      int ans = 1;
      while (m - (n - 1) * i > 0) {
        int k = m - (n - 1) * i;
        if (k % i == 0)
          ans = i;
        i++;
      }
      cout << ans;
    }
  }
  return 0;
}


Fixed submission:
// Devarshi

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (i = 0; i < n; i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it, x) for (auto it : x)
#define PI 3.14159265
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 1000001;

bool isprime(int n) {
  int n1 = n;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  // if(n1==n) return true;
  return true;
}

int main() {

  sync
      /*
      #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("out.txt", "w", stdout);
      #endif
      */
      int n,
      m;
  cin >> n >> m;
  if (m % n == 0)
    cout << m / n;
  else {
    // cout<<1<<endl;
    if (isprime(m))
      cout << 1;
    else {
      int i = 1;
      int ans = 1;
      while (m - (n - 1) * i > 0) {
        int k = m - (n - 1) * i;
        if (k % i == 0)
          ans = i;
        i++;
      }
      cout << ans;
    }
  }
  return 0;
}