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

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//(double)clock() / CLOCKS_PER_SEC;
ll n, m;

// from
// https://github.com/ADJA/algos/blob/master/NumberTheory/DiophantineEquation.cpp
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// Finds such x and y, that a * x + b * y = gcd(a, b)
long long extended_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long x1, y1;
  long long g = extended_gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return g;
}

// Solves equation a * x + b * y = c, writes answer to x and y
void solveDiophantine(long long a, long long b, long long c, long long &x,
                      long long &y) {
  long long g = extended_gcd(a, b, x, y);

  if (c % g != 0) {
    puts("Impossible");
    exit(0);
  }

  c /= g;

  x = x * c;
  y = y * c;
}

ll solve(ll a, ll b) {
  ll g = gcd(a, b);

  ll x, y;

  solveDiophantine(a, b, 1, x, y);

  long long add = b / g;
  long long num = 0;
  if (add < 0)
    num = (long long)floor(1.0 * -x / add);
  else
    num = (long long)ceil(1.0 * -x / add);

  x = x + b / g * num;

  return a * x - 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  n *= 2;
  m = n;
  vll ps;
  ll cnt = 2;
  while (cnt * cnt <= m) {
    if (m % cnt == 0) {
      ll x = 1;
      while (m % cnt == 0) {
        x *= cnt;
        m /= cnt;
      }
      ps.push_back(x);
    }
    cnt++;
  }
  if (m > 1) {
    ps.push_back(m);
  }
  int P = ps.size();
  ll ans = (n - 1);
  for (int msk = 0; msk < (1 << (P - 1)); msk++) {
    ll x = 1;
    ll y = 1;
    for (int i = 0; i < P; i++) {
      if ((msk & (1 << i)) > 0)
        x *= ps[i];
      else
        y *= ps[i];
    }
    ll s1 = solve(x, y);
    ll s2 = solve(y, x);
    // cout<<x<<" "<<y<<endl;
    // cout<<s1<<" "<<s2<<endl;
    if (s1 > 0)
      ans = min(ans, s1);
    if (s2 > 0)
      ans = min(ans, s2);
    /*if(x>y) swap(x, y);

    for(ll yc=1; yc*y<=n; yc++)
    {
        if((yc*y-1)%x==0)
        {
            ans=min(ans, (yc*y-1));
            break;
        }
        if((yc*y+1)%x==0)
        {
            ans=min(ans, yc*y);
            break;
        }
    }*/
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//(double)clock() / CLOCKS_PER_SEC;
ll n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  n *= 2;
  m = n;
  vll ps;
  ll cnt = 2;
  while (cnt * cnt <= m) {
    if (m % cnt == 0) {
      ll x = 1;
      while (m % cnt == 0) {
        x *= cnt;
        m /= cnt;
      }
      ps.push_back(x);
    }
    cnt++;
  }
  if (m > 1) {
    ps.push_back(m);
  }
  int P = ps.size();
  ll ans = (n - 1);
  for (int msk = 0; msk < (1 << (P - 1)); msk++) {
    ll x = 1;
    ll y = 1;
    for (int i = 0; i < P; i++) {
      if ((msk & (1 << i)) > 0)
        x *= ps[i];
      else
        y *= ps[i];
    }
    if (x > y)
      swap(x, y);
    for (ll yc = 1; yc * y <= n; yc++) {
      if ((yc * y - 1) % x == 0) {
        ans = min(ans, (yc * y - 1));
        break;
      }
      if ((yc * y + 1) % x == 0) {
        ans = min(ans, yc * y);
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}