結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2021-09-15 21:11:18
言語 Rust
(1.77.0 + proconio)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,192 bytes
コンパイル時間 12,427 ms
コンパイル使用メモリ 387,880 KB
最終ジャッジ日時 2024-11-15 05:21:31
合計ジャッジ時間 13,402 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error[E0614]: type `i64` cannot be dereferenced
  --> src/main.rs:10:17
   |
10 |         if n <= *b {
   |                 ^^

error[E0614]: type `i64` cannot be dereferenced
  --> src/main.rs:14:28
   |
14 |         let mut y = modpow(*b as i128, t as i128, n as i128);
   |                            ^^

error[E0614]: type `i32` cannot be dereferenced
  --> src/main.rs:35:17
   |
35 |         if n <= *a {
   |                 ^^

error[E0614]: type `i32` cannot be dereferenced
  --> src/main.rs:39:28
   |
39 |         let mut y = modpow(*a as i128, t as i128, n as i128);
   |                            ^^

For more information about this error, try `rustc --explain E0614`.
error: could not compile `main` (bin "main") due to 4 previous errors

ソースコード

diff #

static A: [i32; 3] = [2, 7, 61];
static B: [i64; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022];
fn miller_rabin_i64(n: i64) -> bool {
    let mut d = n - 1;
    while d & 1 == 0 {
        d /= 2;
    }
    let (e, rev) = (1, n as i128 - 1);
    for b in B.into_iter() {
        if n <= *b {
            break;
        }
        let mut t = d as i128;
        let mut y = modpow(*b as i128, t as i128, n as i128);
        while t != n as i128 - 1 && y != e && y != rev {
            y *= y;
            y %= n as i128;
            t *= 2;
            t %= n as i128;
        }
        if y != rev && t % 2 == 0 {
            return false;
        }
    }
    return true;
}

fn miller_rabin_i32(n: i32) -> bool {
    let mut d = n - 1;
    while d & 1 == 0 {
        d /= 2;
    }
    let (e, rev) = (1, n as i128 - 1);
    for a in A.into_iter() {
        if n <= *a {
            break;
        }
        let mut t = d as i128;
        let mut y = modpow(*a as i128, t as i128, n as i128);
        while t as i128 != n as i128 - 1 && y != e && y != rev {
            y *= y;
            y %= n as i128;
            t *= 2;
            t %= n as i128;
        }
        if y != rev && t & 1 == 0 {
            return false;
        }
    }
    true
}

fn modpow(_n: i128, _t: i128, modulo: i128) -> i128 {
    let mut ret = 1i128;
    let mut n = _n;
    let mut t = _t;
    while t != 0 {
        if t & 1 == 1 {
            ret *= n;
        }
        ret %= modulo;
        t >>= 1;
        n *= n;
        n %= modulo;
    }
    ret
}

fn is_prime(n: i64) -> bool {
    if n == 2 {
        return true;
    } else if n == 1 || n & 1 == 0 {
        return false;
    } else if n < (1i64 << 31) {
        return miller_rabin_i32(n as i32);
    }
    miller_rabin_i64(n)
}
#[allow(dead_code)]
fn solve(read: &mut snio::Reader<std::io::StdinLock<'_>>) {
    let n = read.i64();
    for _ in 0..n {
        let a = read.i64();
        println!("{} {}",a ,is_prime(a) as u8);
    }
}
 
//use proconio::input;
fn main() {
    let t = std::io::stdin();
    let mut read = snio::Reader::new(t.lock());
    let n = 1;
    for _ in 0..n {
        solve(&mut read);
    }
}
 
#[allow(dead_code)]
pub mod snio {
    pub struct Reader<R: std::io::BufRead> {
        reader: R,
        buf: std::collections::VecDeque<String>,
    }
 
    impl<R: std::io::BufRead> Reader<R> {
        pub fn new(reader: R) -> Self {
            Self {
                reader,
                buf: std::collections::VecDeque::new(),
            }
        }
        fn load(&mut self) {
            while self.buf.is_empty() {
                let mut s = String::new();
                let length = self.reader.read_line(&mut s).unwrap();
                if length == 0 {
                    break;
                }
                self.buf.extend(s.split_whitespace().map(|s| s.to_owned()));
            }
        }
        pub fn string(&mut self) -> String {
            self.load();
            self.buf.pop_front().unwrap_or_else(|| panic!("input ended"))
        }
        pub fn char(&mut self) -> char {
            let string = self.string();
            let mut chars = string.chars();
            let res = chars.next().unwrap();
            assert!(chars.next().is_none(), "invalid input!");
            res
        }
        pub fn chars(&mut self) -> Vec<char> {
            self.read::<String>().chars().collect()
        }
        pub fn read<T: std::str::FromStr>(&mut self) -> T
            where
                <T as ::std::str::FromStr>::Err: ::std::fmt::Debug,
        {
            self.string().parse::<T>().expect("Failed to parse the input.")
        }
    }
    macro_rules! definition_of_reader_of_numbers {
            ($($ty:tt,)*) => {
                impl <R:std::io::BufRead> Reader<R> {
                    $(
                    #[inline]
                    pub fn $ty (&mut self) -> $ty {
                        self.read::<$ty>()
                    }
                    )*
                }
            }
        }
    definition_of_reader_of_numbers! {
        u8,u16,u32,u64,usize,
        i8,i16,i32,i64,isize,
        f32,f64,
    }
}

const INF:i64 = 1i64 << 60;

0