結果

問題 No.1315 渦巻洞穴
ユーザー koba-e964koba-e964
提出日時 2023-08-09 13:51:37
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,672 bytes
コンパイル時間 12,685 ms
コンパイル使用メモリ 390,636 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 08:47:30
合計ジャッジ時間 16,088 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 0 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,940 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 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 1 ms
6,944 KB
testcase_31 AC 1 ms
6,944 KB
testcase_32 AC 1 ms
6,944 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 1 ms
6,944 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 1 ms
6,944 KB
testcase_41 AC 1 ms
6,944 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 1 ms
6,944 KB
testcase_44 AC 1 ms
6,944 KB
testcase_45 AC 1 ms
6,940 KB
testcase_46 AC 1 ms
6,940 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 1 ms
6,944 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 1 ms
6,944 KB
testcase_51 AC 2 ms
6,940 KB
testcase_52 AC 1 ms
6,940 KB
testcase_53 AC 1 ms
6,940 KB
testcase_54 AC 1 ms
6,940 KB
testcase_55 AC 1 ms
6,940 KB
testcase_56 AC 1 ms
6,940 KB
testcase_57 AC 1 ms
6,944 KB
testcase_58 AC 1 ms
6,944 KB
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 AC 1 ms
6,944 KB
testcase_64 AC 1 ms
6,944 KB
testcase_65 AC 1 ms
6,940 KB
testcase_66 AC 1 ms
6,940 KB
testcase_67 AC 1 ms
6,940 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 AC 1 ms
6,940 KB
testcase_72 AC 1 ms
6,940 KB
testcase_73 AC 1 ms
6,940 KB
testcase_74 AC 2 ms
6,944 KB
testcase_75 AC 1 ms
6,944 KB
testcase_76 AC 1 ms
6,940 KB
testcase_77 AC 1 ms
6,940 KB
testcase_78 AC 1 ms
6,944 KB
testcase_79 AC 1 ms
6,940 KB
testcase_80 AC 1 ms
6,940 KB
testcase_81 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

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;
        }
    }
}

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)
    }
    #[allow(unused)]
    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 += "RULDUD";
        append(Spiral::from(1), sm, &mut ans);
    }
    println!("{}\n{}\n{}", 0, ans.len(), &ans);
}
0