結果

問題 No.1509 Swap!!
ユーザー akakimidoriakakimidori
提出日時 2021-05-14 23:08:14
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 4,485 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 172,392 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 02:36:49
合計ジャッジ時間 3,510 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 23 ms
6,940 KB
testcase_03 AC 23 ms
6,940 KB
testcase_04 AC 23 ms
6,940 KB
testcase_05 AC 23 ms
6,940 KB
testcase_06 AC 22 ms
6,944 KB
testcase_07 AC 23 ms
6,944 KB
testcase_08 AC 23 ms
6,940 KB
testcase_09 AC 23 ms
6,944 KB
testcase_10 AC 23 ms
6,944 KB
testcase_11 AC 23 ms
6,944 KB
testcase_12 AC 23 ms
6,944 KB
testcase_13 AC 23 ms
6,940 KB
testcase_14 AC 23 ms
6,940 KB
testcase_15 AC 23 ms
6,944 KB
testcase_16 AC 23 ms
6,944 KB
testcase_17 AC 22 ms
6,940 KB
testcase_18 AC 22 ms
6,944 KB
testcase_19 AC 23 ms
6,940 KB
testcase_20 AC 22 ms
6,940 KB
testcase_21 AC 23 ms
6,944 KB
testcase_22 AC 23 ms
6,944 KB
testcase_23 AC 23 ms
6,944 KB
testcase_24 AC 22 ms
6,940 KB
testcase_25 AC 22 ms
6,940 KB
testcase_26 AC 23 ms
6,944 KB
testcase_27 AC 17 ms
6,944 KB
testcase_28 AC 16 ms
6,940 KB
testcase_29 AC 17 ms
6,944 KB
testcase_30 AC 23 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        assert!(n < std::i32::MAX as usize);
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        self.p.iter_mut().for_each(|p| *p = -1);
    }
    pub fn root(&self, mut x: usize) -> usize {
        assert!(x < self.p.len());
        while self.p[x] >= 0 {
            x = self.p[x] as usize;
        }
        x
    }
    pub fn same(&self, x: usize, y: usize) -> bool {
        assert!(x < self.p.len() && y < self.p.len());
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.p.len() && y < self.p.len());
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if self.p[x] > self.p[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.p[x] += self.p[y];
        self.p[y] = x as i32;
        Some((x, y))
    }
    pub fn parent(&self, x: usize) -> Option<usize> {
        assert!(x < self.p.len());
        let p = self.p[x];
        if p >= 0 {
            Some(p as usize)
        } else {
            None
        }
    }
    pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
    where
        F: FnMut(usize),
    {
        while let Some(p) = self.parent(x) {
            f(x);
            x = p;
        }
        x
    }
    pub fn size(&self, x: usize) -> usize {
        assert!(x < self.p.len());
        let r = self.root(x);
        (-self.p[r]) as usize
    }
}
//---------- end union_find ----------
// ---------- begin binary_gcd ----------
pub fn binary_gcd(a: u64, b: u64) -> u64 {
    if a == 0 || b == 0 {
        return a + b;
    }
    let x = a.trailing_zeros();
    let y = b.trailing_zeros();
    let mut a = a >> x;
    let mut b = b >> y;
    while a != b {
        let x = (a ^ b).trailing_zeros();
        if a < b {
            std::mem::swap(&mut a, &mut b);
        }
        a = (a - b) >> x;
    }
    a << x.min(y)
}
// ---------- end binary_gcd ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::io::Write;

fn run() {
    input! {
        t: usize,
        ask: [(u64, u64, u64); t],
    }
    /*
    for n in 2..=300 {
        for a in 2..n {
            for b in 2..a {
                let mut dsu = DSU::new(n);
                for i in a..n {
                    dsu.unite(i, i - a);
                }
                for i in b..n {
                    dsu.unite(i, i - b);
                }
                if dsu.size(0) == n && a + b > n + 1{
                    println!("OK {} {} {}", n, a, b);
                }
            }
        }
    }
    */
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for (n, a, b) in ask {
        let mut ok = true;
        ok &= binary_gcd(a, b) == 1;
        ok &= a + b <= n + 1;
        if ok {
            writeln!(out, "YES").ok();
        } else {
            writeln!(out, "NO").ok();
        }
    }
}

fn main() {
    run();
}
0