結果

問題 No.2045 Two Reflections
ユーザー koba-e964koba-e964
提出日時 2023-07-20 00:17:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,460 bytes
コンパイル時間 3,140 ms
コンパイル使用メモリ 172,088 KB
実行使用メモリ 9,068 KB
最終ジャッジ日時 2023-10-20 02:25:40
合計ジャッジ時間 4,692 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 3 ms
4,548 KB
testcase_03 AC 7 ms
5,284 KB
testcase_04 AC 7 ms
5,144 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 7 ms
5,056 KB
testcase_08 AC 8 ms
6,528 KB
testcase_09 AC 9 ms
6,456 KB
testcase_10 AC 8 ms
5,840 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 11 ms
9,068 KB
testcase_18 AC 10 ms
6,996 KB
testcase_19 AC 11 ms
6,996 KB
testcase_20 AC 11 ms
9,068 KB
testcase_21 AC 10 ms
6,432 KB
testcase_22 AC 10 ms
6,440 KB
testcase_23 AC 10 ms
6,700 KB
testcase_24 AC 10 ms
6,136 KB
testcase_25 AC 11 ms
6,496 KB
testcase_26 AC 13 ms
5,872 KB
testcase_27 AC 14 ms
5,872 KB
testcase_28 AC 13 ms
5,988 KB
testcase_29 AC 12 ms
5,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
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() }

fn perm_comp(a: &[usize], b: &[usize]) -> Vec<usize> {
    let n = a.len();
    assert_eq!(b.len(), n);
    let mut c = vec![0; n];
    for i in 0..n {
        c[i] = a[b[i]];
    }
    c
}

fn perm_exp(a: &[usize], mut e: i64) -> Vec<usize> {
    let n = a.len();
    let mut cur: Vec<_> = (0..n).collect();
    let mut prod = a.to_vec();
    while e > 0 {
        if e % 2 == 1 {
            prod = perm_comp(&cur, &prod)
        }
        cur = perm_comp(&cur, &cur);
        e /= 2;
    }
    prod
}

// [2, 4, 0, 1, 3, 7, 6] ==> [[0, 2], [1, 4, 3], [6, 7]]
// Verified by: https://atcoder.jp/contests/joisc2007/submissions/24248388
fn decompose_into_cycles(a: &[usize]) -> Vec<Vec<usize>> {
    let n = a.len();
    let mut vis = vec![false; n];
    let mut ans = vec![];
    for i in 0..n {
        if vis[i] { continue; }
        vis[i] = true;
        let mut cyc = vec![i];
        let mut v = a[i];
        while v != i {
            vis[v] = true;
            cyc.push(v);
            v = a[v];
        }
        ans.push(cyc)
    }
    ans
}

fn gcd(mut x: i64, mut y: i64) -> i64 {
    while y != 0 {
        let r = x % y;
        x = y;
        y = r;
    }
    x
}

// https://yukicoder.me/problems/no/2045 (3.5)
// GAP で実験したところ、位数はおよそ O(n^2) でありそうなことがわかった。
// 操作 1 を a, 操作 2 を b と呼ぶことにする。<a, b> の元は (ab)^?, (ab)^?a, (ba)^?, (ba)^?b の形に限られる。
// ここで、ab が有限位数であること、ba = (ab)^{-1} であることから、(ba)^? と (ab)^? の取り得る値の集合は等しいことがわかる。
// よって、(ab)^?, (ab)^?a だけ考えれば良い。
// 答えは ord(ab) * (1 or 2) である。1 or 2 が 2 であることは a = (ab)^i なる i が存在することと同値。
// a の位数は 1 or 2 である。a の位数が 2 のとき、ab^{ord(ab)/2} と a が等しいかどうか比較すれば良い。
// Tags: group-theory, permutation-groups
fn main() {
    let n: usize = get();
    let p: usize = get();
    let q: usize = get();
    let mut a: Vec<_> = (0..n).collect();
    let mut ab: Vec<_> = (0..n).collect();
    a[..p].reverse();
    ab[..p].reverse();
    ab[n - q..].reverse();
    let cyc = decompose_into_cycles(&ab);
    let mut ord = 1i64;
    let mut is_even = false;
    for c in cyc {
        let len = c.len() as i64;
        let g = gcd(ord, len);
        ord = ord / g * len;
        if len % 2 == 0 {
            is_even = true;
        }
    }
    let mut fac = if p == 1 { 1 } else { 2 };
    if is_even {
        let abx = perm_exp(&ab, ord / 2);
        if a == abx {
            fac = 1;
        }
    }
    println!("{}", ord * fac % 998_244_353);
}
0