結果

問題 No.321 (P,Q)-サンタと街の子供たち
ユーザー koba-e964koba-e964
提出日時 2016-03-22 17:44:10
言語 Rust
(1.77.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,901 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 145,572 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 03:31:08
合計ジャッジ時間 4,579 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 26 ms
4,380 KB
testcase_15 AC 61 ms
4,380 KB
testcase_16 AC 24 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 29 ms
4,380 KB
testcase_19 AC 39 ms
4,376 KB
testcase_20 AC 60 ms
4,376 KB
testcase_21 AC 36 ms
4,376 KB
testcase_22 AC 11 ms
4,380 KB
testcase_23 AC 51 ms
4,380 KB
testcase_24 AC 13 ms
4,380 KB
testcase_25 AC 34 ms
4,380 KB
testcase_26 AC 57 ms
4,376 KB
testcase_27 AC 43 ms
4,380 KB
testcase_28 AC 42 ms
4,380 KB
testcase_29 AC 61 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 31 ms
4,380 KB
testcase_33 AC 30 ms
4,376 KB
testcase_34 AC 22 ms
4,376 KB
testcase_35 AC 46 ms
4,380 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 37 ms
4,376 KB
testcase_38 AC 30 ms
4,376 KB
testcase_39 AC 10 ms
4,376 KB
testcase_40 AC 57 ms
4,376 KB
testcase_41 AC 15 ms
4,376 KB
testcase_42 AC 48 ms
4,380 KB
testcase_43 AC 24 ms
4,376 KB
testcase_44 AC 44 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
use std::io::*;
use std::ops::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
#[allow(dead_code)]
fn getword() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() ||u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
#[allow(dead_code)]
fn parse<T : std::str::FromStr>(s : &str) -> T {
     return s.parse::<T>().ok().unwrap();
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/*
 * Gaussian integer.
 */
struct Comp {
    x: i64, y: i64,
}
impl Comp {
    fn new() -> Self {
        Comp {x: 0, y: 0}
    }
    fn new_xy(x: i64, y: i64) -> Self {
        Comp {x: x, y: y}
    }
}
impl Sub for Comp {
    type Output = Comp;
    fn sub(self, r: Self) -> Self {
        Comp {x: self.x - r.x, y: self.y - r.y}
    }
}
impl Mul for Comp {
    type Output = Comp;
    fn mul(self, r: Self) -> Self {
        Comp {x: self.x * r.x - self.y * r.y, y: self.x * r.y + self.y * r.x}
    }
}

fn conjg(c: Comp) -> Comp {
    Comp {x: c.x, y: -c.y}
}
fn norm(c: Comp) -> i64 {
    c.x * c.x + c.y * c.y
}

fn good_quo(a: i64, b:i64) -> i64 {
    assert!(b > 0);
    if a < 0 {
        let r = a / b;
        if a % b < -b / 2 {
            return r - 1;
        } else {
            return r;
        }
        } else {
            let r = a / b;
            if a % b > b / 2 {
                return r + 1;
            } else {
                return r;
            }
        }
        
}
fn gcd(x: Comp, y: Comp) -> Comp {
    let mut p = x;
    let mut q = y;
    while q != Comp::new() {
        // p % q
        let pqc = p * conjg(q);
        let qn = norm(q);
        let quo = Comp::new_xy(good_quo(pqc.x, qn), good_quo(pqc.y, qn));
        let r = p - q * quo;
        p = q;
        q = r;
    }
    return p;
}

fn is_mul(a: Comp, b: Comp) -> bool {
    let abc = a * conjg(b);
    let bn = norm(b);
    if bn == 0 {
        return a == Comp::new();
    }
    return abc.x % bn == 0 && abc.y % bn == 0;
}

fn main() {
    let p: i64 = parse(&getword());
    let q: i64 = parse(&getword());
    let pq = Comp {x: p, y: q};
    let pq2 = Comp {x: p, y: -q};
    let n: usize = parse(&getword());
    let mut ary = vec![Comp::new(); n];
    for i in 0 .. n {
        let x = parse(&getword());
        let y = parse(&getword());
        ary[i] = Comp::new_xy(x, y);
    }
    let g = gcd(pq, pq2);
    println!("{}", ary.into_iter().filter(|x| is_mul(*x, g)).count());
}
0