fn rand_memory() -> usize { Box::into_raw(Box::new("I hope this is a random number")) as usize } fn rand() -> usize { static mut X: usize = 0; unsafe { if X == 0 { X = rand_memory(); } X ^= X << 13; X ^= X >> 17; X ^= X << 5; X } } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- use std::collections::*; use std::io::Write; type Map = BTreeMap; type Set = BTreeSet; fn run() { input! { s: bytes, } let mut cnt = [0; 2]; let mut sum = 0; for (i, s) in s.iter().enumerate() { let x = (*s - b'0') as usize; cnt[x] += 1; if x == 0 { sum += i; } } let mut dp = Map::new(); dp.insert((0, 0), 1u64); for i in 0..s.len() { let mut next = Map::new(); for ((z, s), w) in dp { *next.entry((z + 1, s + i)).or_insert(0) += w; *next.entry((z, s)).or_insert(0) += w; } dp = next; } let ans = *dp.get(&(cnt[0], sum)).unwrap(); println!("{}", ans); } fn test() { for _ in 0..100000 { let n = 12; let ini = (0..n).map(|_| rand() & 1).collect::>(); let mut set = Set::new(); set.insert(ini.clone()); let mut dfs = vec![ini.clone()]; while let Some(s) = dfs.pop() { for len in 1..=n { for i in 0..=(n - len) { for j in (i + len)..=(n - len) { let mut a = Vec::from(&s[i..(i + len)]); let mut b = Vec::from(&s[j..(j + len)]); a.sort(); b.sort(); if a == b { let mut s = s.clone(); for k in 0..len { s.swap(i + k, j + k); } if set.insert(s.clone()) { dfs.push(s); } } } } } } let save = set; let mut set = Set::new(); set.insert(ini.clone()); let mut dfs = vec![ini]; while let Some(s) = dfs.pop() { for len in 2..=2 { for i in 0..=(n - len) { for j in (i + len)..=(n - len) { let mut a = Vec::from(&s[i..(i + len)]); let mut b = Vec::from(&s[j..(j + len)]); a.sort(); b.sort(); if a == b { let mut s = s.clone(); for k in 0..len { s.swap(i + k, j + k); } if set.insert(s.clone()) { dfs.push(s); } } } } } } assert_eq!(set, save); } } fn main() { run(); }