1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::{cmp::min, io, iter, mem};

#[derive(Debug, Eq, Clone, Copy, PartialEq)]
enum Relation {
    Never,
    IfPrev,
    IfNotPrev,
    Always,
}

fn solve(fst: &[i8], snd: &[i8], sum: &[i8]) -> i64 {
    let all = || iter::zip(iter::zip(fst, snd), sum);
    let rels: Vec<_> = all()
        .rev()
        .scan(false, |state, ((f, s), t)| {
            use Relation::*;
            let prev = *state;
            let mut ret = |x, y| {
                *state = y;
                Some((x, y))
            };
            match (f + s + 10 - t) % 10 {
                0 => ret(if prev { IfNotPrev } else { Always }, f + s >= 10),
                9 => ret(if prev { IfPrev } else { Never }, prev && (f + s + 1 >= 10)),
                _ => ret(Never, false),
            }
        })
        .collect();

    // One scan & one loop... xD
    let mut total = 0;
    let mut curr = 0;
    for (rel, car) in rels {
        match rel {
            Relation::Never => curr = 0,
            Relation::IfPrev => (),
            Relation::IfNotPrev => curr = 1,
            Relation::Always => curr += 1,
        };
        if !car {
            total += curr;
        }
    }

    total
}

pub fn main() -> io::Result<()> {
    let mut buf = String::new();
    let mut inp: Vec<Vec<i8>> = Vec::new();
    for _ in 0..3 {
        io::stdin().read_line(&mut buf)?;
        inp.push(buf.trim().bytes().map(|x| (x - b'0') as i8).collect());
        buf.clear();
    }
    println!("{}", solve(&inp[0], &inp[1], &inp[2]));
    Ok(())
}