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 Rust (1.42.0) (judge verdict: Accepted)

use std::cmp::min;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let mut p = vec![];
    let n: i128 = sc.read();

    let mut t = n * 2;
    for x in 2.. {
        if x * x > n {
            break;
        }
        let mut cur = 1;
        while t % x == 0 {
            cur *= x;
            t /= x;
        }
        if cur > 1 {
            p.push(cur);
        }
    }
    if t > 1 {
        p.push(t);
    }

    let p_len = p.len();
    let mut ans = if (n * (n + 1) / 2) % n == 0 { n } else { 2 * n };
    for mask in 0..(1 << p_len) {
        let mut a = 1;
        let mut b = 1;
        for i in 0..p_len {
            if mask & (1 << i) != 0 {
                a *= p[i];
            } else {
                b *= p[i];
            }
        }

        if a == 1 || b == 1 {
            continue;
        }

        let mut x = 0;
        let mut y = 0;
        let g = ext_gcd(a, b, &mut x, &mut y);
        assert_eq!(g, 1);
        assert_eq!(a * x + b * y, 1);
        let k = min((a * x).abs(), (b * y).abs());

        let z = k * (k + 1) / 2;
        assert_eq!(z % n, 0);
        ans = min(ans, k);
    }

    println!("{}", ans);
}

fn ext_gcd(a: i128, b: i128, x: &mut i128, y: &mut i128) -> i128 {
    let mut d = a;
    if b != 0 {
        d = ext_gcd(b, a % b, y, x);
        *y -= (a / b) * *x;
    } else {
        *x = 1;
        *y = 0;
    }
    d
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write_all(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}