結果

問題 No.16 累乗の加算
ユーザー sino
提出日時 2020-03-26 00:06:17
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,889 bytes
コンパイル時間 14,570 ms
コンパイル使用メモリ 405,148 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-01-02 00:23:52
合計ジャッジ時間 15,698 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: trait `IteratorExt` is never used
  --> src/main.rs:29:7
   |
29 | trait IteratorExt: Iterator + Sized {
   |       ^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: creating a mutable reference to mutable static is discouraged
  --> src/main.rs:56:21
   |
56 |             let m = MAP.as_mut().unwrap();
   |                     ^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
   = note: `#[warn(static_mut_refs)]` on by default

warning: creating a mutable reference to mutable static is discouraged
  --> src/main.rs:61:17
   |
61 |         let m = MAP.as_mut().unwrap();
   |                 ^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/main.rs:64:25
   |
64 |                 let m = MAP.as_mut().unwrap();
   |                         ^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable sta

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::collections::HashMap;

#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim_end().to_owned()
}

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}
impl<T: Iterator> IteratorExt for T {}

const LAW: u64 = 1_000_003;

fn main() {
    let (x, _n) = read!(u64, usize);
    let a = read!([u64]);

    let ans = a
        .iter()
        .map(|&e|  my_pow(x, e) % LAW)
        .fold(0, |acc, e| (acc + e) % LAW);

    println!("{}", ans);
}

// (x^n) % LAW
fn my_pow(x: u64, n: u64) -> u64 {
    static mut MAP: Option<HashMap<u64, u64>> = None;
    unsafe {
        if let None = MAP {
            MAP = Some(HashMap::new());
            let m = MAP.as_mut().unwrap();
            m.insert(0, 1);
            m.insert(1, x);
        }

        let m = MAP.as_mut().unwrap();
        if let None = m.get(&n) {
            if n % 2 == 1 {
                let m = MAP.as_mut().unwrap();
                m.insert(n, (x * my_pow(x, n/2).pow(2)) % LAW);
            }
            else {
                let m = MAP.as_mut().unwrap();
                m.insert(n, (my_pow(x, n/2).pow(2)) % LAW);
            }        
        }

        *m.get(&n).unwrap()
    }
}
0