結果

問題 No.1747 Many Formulae 2
ユーザー togatogatogatoga
提出日時 2021-11-19 22:02:19
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 6,297 bytes
コンパイル時間 12,896 ms
コンパイル使用メモリ 399,520 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-10 08:57:45
合計ジャッジ時間 13,899 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::collections::BTreeSet`
 --> src/main.rs:1:5
  |
1 | use std::collections::BTreeSet;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

ソースコード

diff #

use std::collections::BTreeSet;

pub mod utils {
    const DYX: [(isize, isize); 8] = [
        (0, 1),
        (1, 0),
        (0, -1),
        (-1, 0),
        (1, 1),
        (-1, 1),
        (1, -1),
        (-1, -1),
    ];
    pub fn try_adj(y: usize, x: usize, dir: usize, h: usize, w: usize) -> Option<(usize, usize)> {
        let ny = y as isize + DYX[dir].0;
        let nx = x as isize + DYX[dir].1;
        if ny >= 0 && nx >= 0 && ny < h as isize && nx < w as isize {
            Some((ny as usize, nx as usize))
        } else {
            None
        }
    }
}
#[derive(Default)]
/// NOTE
/// declare variables to reduce the number of parameters for dp and dfs etc.
pub struct Solver {}
impl Solver {
    pub fn is_prime(&mut self, x: i64) -> bool {
        if x == 1 {
            return false;
        }
        for y in 2.. {
            if y * y > x {
                break;
            }
            if x % y == 0 {
                return false;
            }
        }
        true
    }
    pub fn solve(&mut self) {
        let stdin = std::io::stdin();
        #[allow(unused_mut, unused_variables)]
        let mut scn = fastio::Scanner::new(stdin.lock());
        let s = scn
            .chars()
            .into_iter()
            .map(|x| x.to_digit(10).expect("digit") as i64)
            .collect::<Vec<i64>>();
        let n = s.len();
        if n == 1 {
            let result = if self.is_prime(s[0]) { 1 } else { 0 };
            println!("{}", result);
            return ;
        }
        let m = n-1;
        let mut results = vec![];
        for bit in 0..1<<m {
            let mut total = 0;
            let mut x = s[0];
            for i in 0..m {
                if bit >> i & 1 != 0 {
                    total += x;
                    x = s[i + 1];
                } else {
                    x = 10 * x + s[i+1];
                }
            }
            total += x;
            results.push(total);
        }
        let mut result = 0;
        results.into_iter().for_each(|x| {
            //cecho!(x, self.is_prime(x));
            if self.is_prime(x) {
                result += 1;
            }
        });
        println!("{}", result);
    }
}
pub mod fastio {
    use std::collections::VecDeque;
    use std::io::BufWriter;
    use std::io::Write;
    pub struct Writer<W: std::io::Write> {
        writer: std::io::BufWriter<W>,
    }
    impl<W: std::io::Write> Writer<W> {
        pub fn new(write: W) -> Writer<W> {
            Writer {
                writer: BufWriter::new(write),
            }
        }
        pub fn flush(&mut self) {
            self.writer.flush().unwrap();
        }
        pub fn write<S: std::string::ToString>(&mut self, s: S) {
            self.writer.write(s.to_string().as_bytes()).unwrap();
        }
        pub fn writeln<S: std::string::ToString>(&mut self, s: S) {
            self.write(s);
            self.write('\n');
        }
    }
    pub struct Scanner<R> {
        stdin: R,
        buffer: VecDeque<String>,
    }
    impl<R: std::io::BufRead> Scanner<R> {
        pub fn new(s: R) -> Scanner<R> {
            Scanner {
                stdin: s,
                buffer: VecDeque::new(),
            }
        }
        pub fn read<T: std::str::FromStr>(&mut self) -> T {
            while self.buffer.is_empty() {
                let line = self.read_line();
                for w in line.split_whitespace() {
                    self.buffer.push_back(String::from(w));
                }
            }
            self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn read_line(&mut self) -> String {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            line.trim_end().to_string()
        }
        pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.read()).collect()
        }
        pub fn chars(&mut self) -> Vec<char> {
            self.read::<String>().chars().collect()
        }
    }
}
pub mod macros {
    #[macro_export]
    #[allow(unused_macros)]
    macro_rules ! max {($ x : expr ) => ($ x ) ; ($ x : expr , $ ($ y : expr ) ,+ ) => {std :: cmp :: max ($ x , max ! ($ ($ y ) ,+ ) ) } }
    #[macro_export]
    #[allow(unused_macros)]
    macro_rules ! min {($ x : expr ) => ($ x ) ; ($ x : expr , $ ($ y : expr ) ,+ ) => {std :: cmp :: min ($ x , min ! ($ ($ y ) ,+ ) ) } }
    #[macro_export]
    #[allow(unused_macros)]
    /// Display a line of variables
    macro_rules ! echo {() => {{use std :: io :: {self , Write } ; writeln ! (io :: stderr () , "{}:" , line ! () ) . unwrap () ; } } ; ($ e : expr , $ ($ es : expr ) ,+ $ (, ) ? ) => {{use std :: io :: {self , Write } ; write ! (io :: stderr () , "{}:" , line ! () ) . unwrap () ; write ! (io :: stderr () , " {} = {:?}" , stringify ! ($ e ) , $ e ) . unwrap () ; $ (write ! (io :: stderr () , " {} = {:?}" , stringify ! ($ es ) , $ es ) . unwrap () ; ) + writeln ! (io :: stderr () ) . unwrap () ; } } ; ($ e : expr ) => {{use std :: io :: {self , Write } ; let result = $ e ; writeln ! (io :: stderr () , "{}: {} = {:?}" , line ! () , stringify ! ($ e ) , result ) . unwrap () ; result } } ; }
    #[macro_export]
    #[allow(unused_macros)]
    /// Display a line of variables with colors
    macro_rules ! cecho {() => {{use std :: io :: {self , Write } ; writeln ! (io :: stderr () , "\x1b[31;1m{}\x1b[m:" , line ! () ) . unwrap () ; } } ; ($ e : expr , $ ($ es : expr ) ,+ $ (, ) ? ) => {{use std :: io :: {self , Write } ; write ! (io :: stderr () , "\x1b[31;1m{}\x1b[m:" , line ! () ) . unwrap () ; write ! (io :: stderr () , " \x1b[92;1m{}\x1b[m = {:?}" , stringify ! ($ e ) , $ e ) . unwrap () ; $ (write ! (io :: stderr () , " \x1b[92;1m{}\x1b[m = {:?}" , stringify ! ($ es ) , $ es ) . unwrap () ; ) + writeln ! (io :: stderr () ) . unwrap () ; } } ; ($ e : expr ) => {{use std :: io :: {self , Write } ; let result = $ e ; writeln ! (io :: stderr () , "\x1b[31;1m{}\x1b[m: \x1b[92;1m{}\x1b[m = {:?}" , line ! () , stringify ! ($ e ) , result ) . unwrap () ; result } } ; }
}
fn main() {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024)
        .spawn(|| Solver::default().solve())
        .unwrap()
        .join()
        .unwrap();
}
0