結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2021-09-12 15:27:04
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 4,006 bytes
コンパイル時間 15,483 ms
コンパイル使用メモリ 402,496 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 05:21:40
合計ジャッジ時間 14,288 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: value assigned to `t` is never read
 --> src/main.rs:7:17
  |
7 |         let mut t = 0;
  |                 ^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default

warning: unused variable: `times`
  --> src/main.rs:38:26
   |
38 | fn miller_rabin(n: i128, times: i64) -> bool {
   |                          ^^^^^ help: if this is intentional, prefix it with an underscore: `_times`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `i`
  --> src/main.rs:72:9
   |
72 |     for i in 0..n {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: static `XORSHIFT1` is never used
 --> src/main.rs:1:12
  |
1 | static mut XORSHIFT1: i128 = 123456789;
  |            ^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: static `XORSHIFT2` is never used
 --> src/main.rs:2:12
  |
2 | static mut XORSHIFT2: i128 = 362436069;
  |            ^^^^^^^^^

warning: static `XORSHIFT3` is never used
 --> src/main.rs:3:12
  |
3 | static mut XORSHIFT3: i128 = 521288629;
  |            ^^^^^^^^^

warning: static `XORSHIFT4` is never used
 --> src/main.rs:4:12
  |
4 | static mut XORSHIFT4: i128 = 88675123;
  |            ^^^^^^^^^

warning: function `xorshift` is never used
 --> src/main.rs:5:4
  |
5 | fn xorshift() -> i128 {
  |    ^^^^^^^^

warning: constant `INF` is never used
   --> src/main.rs:152:7
    |
152 | const INF:i64 = 1i64 << 60;
    |       ^^^

ソースコード

diff #

static mut XORSHIFT1: i128 = 123456789;
static mut XORSHIFT2: i128 = 362436069;
static mut XORSHIFT3: i128 = 521288629;
static mut XORSHIFT4: i128 = 88675123;
fn xorshift() -> i128 {
    unsafe {
        let mut t = 0;
        t = XORSHIFT1 ^ (XORSHIFT1 << 11i128);
        XORSHIFT1 = XORSHIFT2;
        XORSHIFT2 = XORSHIFT3;
        XORSHIFT3 = XORSHIFT4;
        XORSHIFT4 = (XORSHIFT4 ^ (XORSHIFT4 >> 19i128)) ^ (t ^ (t >> 8i128));
        t
    }
}
fn witness(n: i128, a: i128) -> bool {
    let mut t = 0;
    let mut u = n - 1;
    while u & 1i128 == 0 && u != 0{
        t += 1;
        u >>= 1;
    }
    let mut x = modpow(a, u, n);

    for _ in 0..t {
        let b = x * x % n;
        if b == 1 {
            if b != 1 && b != n - 1 {
                return true;
            } else {
                return false;
            }
        }
        x = b;
    }
    true
}
fn miller_rabin(n: i128, times: i64) -> bool {
    if n == 1 {
        return false;
    }
    if n % 2 == 0 {
        return n == 2;
    }

    for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022].to_vec() {
        //let mut a = rand::thread_rng().gen_range(1,n);
        /*let mut a = xorshift() % n;
        while a == 0 {
            a = xorshift() % n;
        }*/
        if witness(n, a % n) {
            return false;
        }
    }
    true
}
fn modpow(_x: i128, _n: i128, modulo: i128) -> i128 {
    if _n == 0 {
        return 1;
    }
    if _n % 2 == 0 {
        let t = modpow(_x, _n / 2, modulo);
        return t * t % modulo;
    }
    _x * modpow(_x, _n - 1, modulo)
}

#[allow(dead_code)]
fn solve(read: &mut snio::Reader<std::io::StdinLock<'_>>) {
    let n = read.usize();
    for i in 0..n {
        let t = read.i128();
        println!("{} {}",t,miller_rabin(t,100) 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,i128,
    }
}

const INF:i64 = 1i64 << 60;
0