結果

問題 No.1694 ZerOne
ユーザー koba-e964koba-e964
提出日時 2021-10-02 12:33:38
言語 Rust
(1.77.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 2,298 bytes
コンパイル時間 2,220 ms
コンパイル使用メモリ 162,332 KB
実行使用メモリ 53,384 KB
最終ジャッジ日時 2023-09-27 12:03:56
合計ジャッジ時間 4,104 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 24 ms
28,788 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 3 ms
5,324 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
6,812 KB
testcase_09 AC 21 ms
27,008 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 6 ms
8,776 KB
testcase_14 AC 19 ms
26,940 KB
testcase_15 AC 4 ms
5,772 KB
testcase_16 AC 3 ms
4,524 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 43 ms
53,384 KB
testcase_21 AC 41 ms
53,336 KB
testcase_22 AC 38 ms
50,224 KB
testcase_23 AC 41 ms
53,372 KB
testcase_24 AC 40 ms
53,332 KB
testcase_25 AC 39 ms
53,328 KB
testcase_26 AC 35 ms
47,056 KB
testcase_27 AC 33 ms
43,904 KB
testcase_28 AC 15 ms
21,416 KB
testcase_29 AC 18 ms
24,880 KB
testcase_30 AC 23 ms
30,944 KB
testcase_31 AC 35 ms
43,900 KB
testcase_32 AC 30 ms
38,356 KB
testcase_33 AC 33 ms
43,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 naive(s: &[char]) -> i64 {
    let n = s.len();
    let mut a = 0u64;
    for i in 0..n {
        if s[i] == '1' {
            a |= 1 << i;
        }
    }
    // Swapping with length 2 is sufficient.
    let mut vis = HashSet::new();
    let mut que = VecDeque::new();
    que.push_back(a);
    while let Some(v) = que.pop_front() {
        for i in 0..n - 1 {
            if ((v >> i) & 3) != 1 { continue; }
            for j in 0..n - 1 {
                if ((v >> j) & 3) != 2 { continue; }
                if (i as i32 - j as i32).abs() <= 1 { continue; }
                let sw = v ^ 3 << i ^ 3 << j;
                if vis.contains(&sw) { continue; }
                vis.insert(sw);
                que.push_back(sw);
            }
        }
    }
    
    let mut vis: Vec<_> = vis.into_iter().collect();
    vis.sort_by_key(|&x| x.reverse_bits());
    for &v in &vis {
        for j in 0..n {
            eprint!("{}", (v >> j) & 1);
        }
        eprintln!();
    }
    vis.len() as i64
}

fn main() {
    let s: Vec<_> = get_word().chars().collect();
    let n = s.len();
    let mut dp = vec![vec![vec![0i64; n * (n - 1) / 2 + 1]; n + 1]; n + 1];
    dp[0][0][0] = 1;
    for i in 0..n {
        let lim = i * (i + 1) / 2 - i;
        for j in 0..i + 1 {
            for k in 0..lim + 1 {
                let val = dp[i][j][k];
                dp[i + 1][j][k] += val;
                dp[i + 1][j + 1][k + i] += val;
            }
        }
    }
    let mut one = 0;
    let mut sum = 0;
    for i in 0..n {
        if s[i] == '1' {
            one += 1;
            sum += i;
        }
    }
    println!("{}", dp[n][one][sum]);
    if n <= 10 {
        eprintln!("naive = {}", naive(&s));
    }
}
0