結果

問題 No.1315 渦巻洞穴
ユーザー koba-e964koba-e964
提出日時 2023-08-09 13:50:07
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 4,908 bytes
コンパイル時間 15,174 ms
コンパイル使用メモリ 402,648 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 08:45:26
合計ジャッジ時間 19,159 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 WA -
testcase_10 AC 1 ms
6,944 KB
testcase_11 WA -
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 WA -
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 1 ms
6,948 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1 ms
6,944 KB
testcase_34 WA -
testcase_35 AC 1 ms
6,944 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,944 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 2 ms
6,940 KB
testcase_43 WA -
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 1 ms
6,940 KB
testcase_46 AC 1 ms
6,944 KB
testcase_47 WA -
testcase_48 AC 1 ms
6,940 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 2 ms
6,940 KB
testcase_52 WA -
testcase_53 AC 2 ms
6,948 KB
testcase_54 AC 1 ms
6,940 KB
testcase_55 AC 2 ms
6,944 KB
testcase_56 WA -
testcase_57 AC 2 ms
6,944 KB
testcase_58 WA -
testcase_59 AC 1 ms
6,940 KB
testcase_60 AC 1 ms
6,940 KB
testcase_61 AC 1 ms
6,940 KB
testcase_62 AC 1 ms
6,944 KB
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 AC 1 ms
6,944 KB
testcase_67 AC 1 ms
6,944 KB
testcase_68 AC 1 ms
6,940 KB
testcase_69 AC 1 ms
6,940 KB
testcase_70 AC 1 ms
6,940 KB
testcase_71 WA -
testcase_72 AC 1 ms
6,940 KB
testcase_73 WA -
testcase_74 WA -
testcase_75 AC 1 ms
6,940 KB
testcase_76 AC 1 ms
6,940 KB
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: method `as_int` is never used
  --> src/main.rs:79:12
   |
42 | impl Spiral {
   | ----------- method in this implementation
...
79 |     pub fn as_int(self) -> i64 {
   |            ^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    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.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

// Counter-clockwise.
// 1 -> (0, 0), 2 -> (1, 0), 3 -> (1, 1), ...
#[derive(Debug, Copy, Clone)]
struct Spiral(i64, i64);
impl Spiral {
    fn sqrt(a: i64) -> i64 {
        let mut pass = 0;
        let mut fail = std::cmp::min(a, 1 << 30) + 1;
        while fail - pass > 1 {
            let mid = (fail + pass) / 2;
            let tmp = mid * mid;
            if tmp <= a {
                pass = mid;
            } else {
                fail = mid;
            }
        }
        pass
    }
    pub fn from(a: i64) -> Self {
        if a == 1 {
            return Spiral(0, 0);
        }
        let x = Self::sqrt(a - 1);
        let lay = (x + 1) / 2;
        let rem = a - 4 * lay * lay + 4 * lay - 2;
        if x % 2 == 1 {
            // [0, 2 * lay - 1]
            if rem < 2 * lay {
                return Spiral(lay, rem - lay + 1);
            }
            // [2 * lay - 1, 4 * lay - 1]
            return Spiral(lay - (rem - 2 * lay + 1), lay);
        }
        // [4 * lay - 1, 6 * lay - 1]
        if rem < 6 * lay {
            return Spiral(-lay, lay - rem + 4 * lay - 1);
        }
        // [6 * lay - 1, 8 * lay - 1]
        Spiral(-lay + rem - 6 * lay + 1, -lay)
    }
    pub fn as_int(self) -> i64 {
        let Spiral(x, y) = self;
        let lay = std::cmp::max(x.abs(), y.abs());
        let rem = if y == -lay {
            // [6 * lay - 1, 8 * lay - 1]
            6 * lay - 1 + x + lay
        } else if x == -lay {
            // [4 * lay - 1, 6 * lay - 1]
            4 * lay - 1 + lay - y
        } else if y == lay {
            // [2 * lay - 1, 4 * lay - 1]
            2 * lay - 1 + lay - x
        } else {
            y + lay - 1
        };
        4 * lay * (lay - 1) + 2 + rem
    }
}

fn append(mut from: Spiral, to: Spiral, ans: &mut String) {
    assert_ne!((from.0 + from.1 + to.0 + to.1) % 2, 0);
    let mut even = true;
    while from.0 > to.0 {
        from.0 -= 1;
        ans.push('L');
        if even {
            ans.push('R');
            ans.push('L');
        }
        even = !even;
    }
    while from.0 < to.0 {
        from.0 += 1;
        ans.push('R');
        if even {
            ans.push('L');
            ans.push('R');
        }
        even = !even;
    }
    while from.1 > to.1 {
        from.1 -= 1;
        ans.push('D');
        if even {
            ans.push('U');
            ans.push('D');
        }
        even = !even;
    }
    while from.1 < to.1 {
        from.1 += 1;
        ans.push('U');
        if even {
            ans.push('D');
            ans.push('U');
        }
        even = !even;
    }
}

// https://yukicoder.me/problems/no/1315 (3.5)
// 1 から移動することを考える。距離が (10^5 以下の) 奇数であれば、ababcdcdefef... と移動することでペナルティを 0 にできる。
// 距離が偶数の場合、1 -> 2 -> 3 -> 4 (RUL) と進んで 4 から開始することにすれば、4 からの距離は奇数であるため、奇数の場合と同様にできる。
// 任意の 2 地点 S, T については、S -> 1 -> T と移動すれば良い。
// 詳しく書くと、1 から T までの距離が偶数であれば S -> 1 の後どこかから T までの道を二重にすれば良い。
// 1 から T までの距離が奇数であれば、 S -> 1 -> 2 -> 3 -> 4 -> 1 -> 4 -> (二重化された道) とすれば、
// 4 から T までの距離は偶数なので問題ない。
// Tags: spiral
fn main() {
    let n: i64 = get();
    let m: i64 = get();
    let sn = Spiral::from(n);
    let sm = Spiral::from(m);
    let mut ans = "".to_string();
    if (sn.0 + sn.1 + sm.0 + sm.1) % 2 != 0 {
        append(sn, sm, &mut ans);
    } else if (sn.0 + sn.1) % 2 == 0 {
        append(sn, Spiral::from(2), &mut ans);
        ans += "LRUL";
        append(Spiral::from(4), sm, &mut ans);
    } else {
        append(sn, Spiral::from(1), &mut ans);
        ans += "RULDUL";
        append(Spiral::from(1), sm, &mut ans);
    }
    println!("{}\n{}\n{}", 0, ans.len(), &ans);
}
0