Submission #2893648


Source Code Expand

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    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;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

const MOD: i64 = 1_000_000_007;

/// Refers external ::MOD.
/// Verified by: https://beta.atcoder.jp/contests/arc099/submissions/2893570
mod mod_int {
    use ::MOD;
    use std::ops::*;
    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt { pub x: i64 }
    impl ModInt {
        fn check_integrity(self) {
            debug_assert!(self.x >= 0);
            debug_assert!(self.x < MOD);
        }
        // x >= 0
        pub fn new(x: i64) -> Self { ModInt { x: x % MOD } }
        #[allow(dead_code)]
        pub fn mul_fast(self, other: Self) -> Self {
            self.check_integrity();
            other.check_integrity();
            ModInt { x: self.x * other.x % MOD }
        }
        #[allow(dead_code)]
        pub fn mul_slow(self, other: Self) -> Self {
            // Naive multiplication in order to avoid overflow
            self.check_integrity();
            other.check_integrity();
            let mut sum = ModInt::new(0);
            let mut cur = self;
            let mut e = other.x;
            if self.x < other.x {
                cur = other;
                e = self.x;
            }
            while e > 0 {
                if e % 2 == 1 {
                    sum = sum + cur;
                }
                cur = cur + cur;
                e /= 2;
            }
            sum
        }
        pub fn pow(self, mut e: i64) -> Self {
            self.check_integrity();
            debug_assert!(e >= 0);
            let mut sum = ModInt::new(1);
            let mut cur = ModInt::new(self.x);
            while e > 0 {
                if e % 2 != 0 {
                    sum = sum * cur;
                }
                cur = cur * cur;
                e /= 2;
            }
            sum
        }
        pub fn inv(self) -> Self { self.pow(MOD - 2) }
    }
    impl Add for ModInt {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            self.check_integrity();
            other.check_integrity();
            let mut sum = self.x + other.x;
            if sum >= MOD { sum -= MOD; }
            ModInt { x: sum }
        }
    }
    impl Sub for ModInt {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            self.check_integrity();
            other.check_integrity();
            let mut sum = self.x - other.x;
            if sum < 0 { sum += MOD; }
            ModInt { x: sum }
        }
    }
    impl Mul for ModInt {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            self.mul_fast(other)
        }
    }
} // mod mod_int


fn solve() {
    use mod_int::*;
    let n: usize = get();
    let s: Vec<_> = get_word().chars().collect();
    const A: usize = 3;
    let b = [ModInt::new(41), ModInt::new(255), ModInt::new(36)];
    let mut hsh = vec![vec![ModInt::new(0); n + 1]; A];
    let mut inv = [ModInt::new(0); A];
    for i in 0 .. A { inv[i] = b[i].inv(); }
    let mut pos = vec![0; n + 1];
    for c in 0 .. A {
        let mut p = 0;
        let mut cur = ModInt::new(1);
        for (i, &ch) in s.iter().enumerate() {
            let mut tmp = hsh[c][i];
            match ch {
                '>' => {
                    p += 1;
                    cur = cur * b[c];
                },
                '<' => {
                    p -= 1;
                    cur = cur * inv[c];
                },
                '+' => tmp = tmp + cur,
                '-' => tmp = tmp - cur,
                _ => panic!(),
            }
            hsh[c][i + 1] = tmp;
            pos[i + 1] = p;
        }
    }
    let mut meguru = [ModInt::new(0); A];
    for c in 0 .. A { meguru[c] = hsh[c][n]; }
    let mut kirika = HashMap::new();
    kirika.insert(meguru, 1);
    let mut tot = 0i64;
    for i in (0 .. n).rev() {
        let mut cur = [ModInt::new(0); A];
        for c in 0 .. A { cur[c] = b[c].pow(MOD - 1 + pos[i]); }
        let mut ken = [ModInt::new(0); A];
        for c in 0 .. A { ken[c] = cur[c] * meguru[c] + hsh[c][i]; }
        tot += kirika.get(&ken).cloned().unwrap_or(0);
        let mut tt = [ModInt::new(0); A];
        for c in 0 .. A { tt[c] = hsh[c][i]; }
        *kirika.entry(tt).or_insert(0) += 1;
    }
    println!("{}", tot);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

Submission Info

Submission Time
Task F - Eating Symbols Hard
User kobae964
Language Rust (1.15.1)
Score 1200
Code Size 5360 Byte
Status AC
Exec Time 176 ms
Memory 47812 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1200 / 1200
Status
AC × 3
AC × 86
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 3.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 4.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 5.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 6.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 7.txt, 70.txt, 71.txt, 72.txt, 73.txt, 74.txt, 75.txt, 76.txt, 77.txt, 78.txt, 8.txt, 9.txt, a.txt, b.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
1.txt AC 3 ms 8572 KB
10.txt AC 143 ms 20860 KB
11.txt AC 143 ms 20860 KB
12.txt AC 141 ms 20860 KB
13.txt AC 140 ms 20860 KB
14.txt AC 138 ms 16764 KB
15.txt AC 136 ms 16764 KB
16.txt AC 155 ms 29696 KB
17.txt AC 141 ms 20860 KB
18.txt AC 146 ms 24828 KB
19.txt AC 148 ms 24828 KB
2.txt AC 3 ms 8572 KB
20.txt AC 140 ms 20860 KB
21.txt AC 137 ms 16764 KB
22.txt AC 155 ms 29696 KB
23.txt AC 141 ms 20860 KB
24.txt AC 146 ms 24828 KB
25.txt AC 146 ms 24828 KB
26.txt AC 141 ms 20860 KB
27.txt AC 137 ms 16764 KB
28.txt AC 155 ms 29696 KB
29.txt AC 141 ms 20860 KB
3.txt AC 9 ms 8572 KB
30.txt AC 147 ms 24828 KB
31.txt AC 155 ms 29696 KB
32.txt AC 141 ms 20860 KB
33.txt AC 138 ms 16764 KB
34.txt AC 155 ms 29696 KB
35.txt AC 141 ms 20860 KB
36.txt AC 146 ms 24828 KB
37.txt AC 147 ms 24828 KB
38.txt AC 140 ms 20860 KB
39.txt AC 137 ms 16764 KB
4.txt AC 155 ms 29696 KB
40.txt AC 155 ms 29696 KB
41.txt AC 141 ms 20860 KB
42.txt AC 146 ms 24828 KB
43.txt AC 146 ms 24828 KB
44.txt AC 140 ms 20860 KB
45.txt AC 137 ms 16764 KB
46.txt AC 135 ms 16764 KB
47.txt AC 135 ms 16764 KB
48.txt AC 131 ms 16764 KB
49.txt AC 154 ms 29696 KB
5.txt AC 154 ms 29696 KB
50.txt AC 176 ms 47812 KB
51.txt AC 141 ms 16764 KB
52.txt AC 155 ms 29696 KB
53.txt AC 141 ms 20860 KB
54.txt AC 146 ms 24828 KB
55.txt AC 152 ms 29696 KB
56.txt AC 140 ms 20860 KB
57.txt AC 137 ms 16764 KB
58.txt AC 157 ms 29696 KB
59.txt AC 157 ms 29696 KB
6.txt AC 141 ms 20860 KB
60.txt AC 147 ms 20860 KB
61.txt AC 147 ms 20860 KB
62.txt AC 169 ms 34556 KB
63.txt AC 169 ms 34556 KB
64.txt AC 143 ms 20860 KB
65.txt AC 143 ms 20860 KB
66.txt AC 144 ms 20860 KB
67.txt AC 131 ms 34556 KB
68.txt AC 87 ms 28296 KB
69.txt AC 97 ms 28292 KB
7.txt AC 141 ms 20860 KB
70.txt AC 112 ms 32508 KB
71.txt AC 118 ms 34556 KB
72.txt AC 123 ms 34556 KB
73.txt AC 160 ms 34556 KB
74.txt AC 161 ms 34556 KB
75.txt AC 160 ms 34556 KB
76.txt AC 161 ms 34556 KB
77.txt AC 154 ms 29696 KB
78.txt AC 161 ms 34556 KB
8.txt AC 146 ms 24828 KB
9.txt AC 146 ms 24828 KB
a.txt AC 3 ms 8572 KB
b.txt AC 3 ms 8572 KB
sample1.txt AC 3 ms 8572 KB
sample2.txt AC 3 ms 8572 KB
sample3.txt AC 3 ms 8572 KB