diff --git a/043/main.rs b/043/main.rs new file mode 100644 index 0000000..916d457 --- /dev/null +++ b/043/main.rs @@ -0,0 +1,100 @@ +use std::collections::HashSet; +use std::collections::VecDeque; + +type Str = VecDeque; +type Res = HashSet; + + +fn nstr(n: u64) -> Str { + let mut res = Str::new(); + let mut nn = n; + while nn > 0 { + res.push_front((nn % 10) as u8); + nn /= 10; + } + res +} + + +fn strn(s: &Str) -> u64 { + let mut res = 0; + for d in s.iter() { + res *= 10; + res += *d as u64; + } + res +} + +fn strnn(s: &Str, p: usize) -> u64 { + let mut ss = Str::new(); + for q in p..p + 3 { + ss.push_back(*s.get(q).unwrap()); + } + return strn(&ss); +} + + +fn test(s: &Str) -> bool { + // { + // let nnn = strn(s); + // if nnn == 1406357289 { + // println!("!!{}", nnn); + // } + //} + let ps = [2,3,5,7,11,13,17]; + for k in 1..8 { + let n = strnn(s, k); + //println!("{}", n); + if n % ps[(k - 1)] != 0 { + //println!("{} {}", n, ps[k-1]); + return false; + } + } + return true; +} + +fn sp(s: &Str, fx: usize, k: usize) -> Str { + let mut res = Str::new(); + for i in 0..fx { + res.push_back(s[i]); + } + res.push_back(s[k]); + for i in fx..s.len() { + if i != k { res.push_back(s[i]); } + } + res +} + +fn lp(s: &Str, fx: usize, keep_first: bool, hs: &mut Res) { + let kf = 0; //if keep_first { 0usize } else { 1usize }; + for k in (fx + kf)..s.len() { + let ss = sp(&s, fx, k); + if ss != *s || keep_first { + if test(&ss) { + let n = strn(&ss); + hs.insert(n); + } + } + lp(&ss, fx + 1, false, hs); + } +} + +fn main() { + let mut hs = HashSet::new(); + let s = nstr(1234567890); + let mut sum = 0u64; + //let mut ttt = 9876543210u64; + //{ + // let rrrr = nstr(1406357289); + // let tt = test(&rrrr); + // println!("{}", tt); + //} + //return; + lp(&s, 0, true, &mut hs); + for h in hs.iter() { + println!("{}", h); + sum += *h; + } + println!("{}", sum); +} +