heh
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#![allow(
    confusable_idents,
    uncommon_codepoints,
    non_upper_case_globals,
    internal_features,
    mixed_script_confusables,
    static_mut_refs,
    incomplete_features
)]
#![feature(
    slice_swap_unchecked,
    generic_const_exprs,
    iter_array_chunks,
    get_many_mut,
    maybe_uninit_uninit_array,
    iter_collect_into,
    let_chains,
    anonymous_lifetime_in_impl_trait,
    array_windows,
    try_blocks,
    slice_take,
    portable_simd,
    test,
    slice_as_chunks,
    array_chunks,
    slice_split_once,
    core_intrinsics
)]
extern crate test;
pub mod util;
pub use util::prelude::*;

#[inline]
fn do_(i: &str, search: fn(&[u64], u64) -> bool) -> u64 {
    let mut v = Vec::with_capacity(10);
    i.行()
        .map(|x| {
            v.clear();
            let (a, b) = x.μ(':');
            let should = reading::all(a);
            reading::κ(C! { &b[1..] }, &mut v);
            should * search(&v, should) as u64
        })
        .sum::<u64>()
}

#[no_mangle]
pub fn run(i: &str) -> impl Display {
    // go backwards for extreme pruning ability
    fn search(nums: &[u64], tv: u64) -> bool {
        match nums {
            &[tail] => tv == tail,
            [head @ .., tail] => {
                let (q, r) = (tv / tail, tv % tail);
                if r == 0 && search(head, q) {
                    return true;
                }
                let Some(result) = tv.checked_sub(*tail) else {
                    return false;
                };
                search(head, result)
            }
            [] => unreachable!(),
        }
    }
    do_(i, search)
}

#[no_mangle]
pub fn p2(i: &str) -> impl Display {
    fn search(nums: &[u64], tv: u64) -> bool {
        match nums {
            &[tail] => tv == tail,
            [head @ .., tail] => {
                let (q, r) = (tv / tail, tv % tail);
                if r == 0 && search(head, q) {
                    return true;
                }
                let Some(result) = tv.checked_sub(*tail) else {
                    return false;
                };
                if (tv - *tail) % util::powers[tail.ͱ() as usize] == 0
                    && search(head, tv / util::powers[tail.ͱ() as usize])
                {
                    return true;
                }
                search(head, result)
            }
            [] => unreachable!(),
        }
    }
    do_(i, |n, should| search(n, should))
}

fn main() {
    // (1..u32::MAX as u64).for_each(|a| assert_eq!(a.ilog10() + 1, digs(a)));
    // let mut s = String::new();
    // for i in 0..1280 {
    let i = include_str!("inp.txt");
    //     s.push_str(i);
    // }
    // std::fs::write("src/inp.txt", s);
    // println!("{}", p2(i));
    assert_eq!(run(i).to_string(), 663613490587u64.to_string());
    assert_eq!(p2(i).to_string(), 110365987435001u64.to_string());
}

#[bench]
fn bench(b: &mut test::Bencher) {
    let i = boxd(include_str!("inp.txt").trim());
    b.iter(|| run(i));
}