Problem:
Score : 600 points

Problem Statement

Given is an integer N.
Find the minimum possible positive integer k such that (1+2+\cdots+k) is a multiple of N.
It can be proved that such a positive integer k always exists.

Constraints

1 \leq N \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer in a line.

Sample Input 1

11

Sample Output 1

10

1+2+\cdots+10=55 holds and 55 is indeed a multple of N=11.
There are no positive integers k \leq 9 that satisfy the condition, so the answer is k = 10.

Sample Input 2

20200920

Sample Output 2

1100144

Solution in C++ (GCC 9.2.1 with AC Library v1.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------/
// ax+by=gcd(a,b)を満たすx,yを求める
ll extgcd(ll a, ll b, ll &x, ll &y){
    ll gc = a;
    if(b!=0){
        gc = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else{
        x = 1, y = 0;
    }
    return gc;
}
// 値A, B
// ll x, y;
// extgcd(A, B, x, y); //x,yにAx+By=gcd(a,b)を満たす値を返す

// ACL template
int main() {
    ll N;
    cin >> N;

    ll tmp = N;
    map<ll, ll> mp;
    repe(x, 2, (ll)sqrt(N) + 1){
        while(tmp % x == 0){
            mp[x]++;
            tmp /= x;
        }
    }
    if (tmp > 1) mp[tmp]++;

    ll a = 2;
    rep(i, mp[2]) a *= 2;
    ll cnt = mp[2] + 1;

    ll b = 1;
    for(auto [p, c]: mp){
        if (p == 2) continue;
        rep(i, c) b *= p;
    }

    ll x, y;
    extgcd(a, b, x, y);
    ll ans = min(a * abs(x), b * abs(y));

    vc<pair<ll, ll>> P;
    for(auto [p, c]: mp){
        if (p == 2) continue;
        if (c == 0) continue;
        P.eb(p, c);
    }

    ll pn = P.size();
    
    rep(bit, 1ll << pn){
        ll ta = a, tb = b;
        rep(i, bit){
            if (!(bit & (1ll << i))) continue;
            auto [p, c] = P[i];
            rep(i, c) ta *= p, tb /= p;
            // err(ta, tb, x, y);
        }
        extgcd(ta, tb, x, y);
        if(x != 0 and y != 0) chmin(ans, min(ta * abs(x), tb * abs(y)));
    }

    cout << ans << endl;

    // ll ans = INF;
    // repe(i, cnt, 60){
    //     ll p2 = 1ll << i;
    //     ll k1 = p2 + 1, k2 = p2 - 1;
    //     err("k", k1, k2);
    //     if(k2 % b == 0){
    //         err(k2, b, (1ll << i - cnt));
    //         ans = k2 / b * (1ll << (i - cnt));
    //         break;
    //     }
    //     if(k1 % b == 0){
    //         err(1ll << (i - cnt));
    //         ans = 1ll << (i - cnt);
    //         break;
    //     }
    // }
    // cout << ans << endl;
}