結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー かりあげクンかりあげクン
提出日時 2020-09-15 14:05:27
言語 Rust
(1.77.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,152 bytes
コンパイル時間 4,845 ms
コンパイル使用メモリ 154,156 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-11 23:07:57
合計ジャッジ時間 6,483 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 392 ms
4,388 KB
testcase_05 AC 379 ms
4,384 KB
testcase_06 AC 149 ms
4,384 KB
testcase_07 AC 147 ms
4,380 KB
testcase_08 AC 147 ms
4,384 KB
testcase_09 AC 728 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
   --> Main.rs:108:21
    |
108 |             let mut i = r;
    |                     ^ help: if this is intentional, prefix it with an underscore: `_i`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `i`
   --> Main.rs:109:17
    |
109 |             for i in 0..r {
    |                 ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: variable does not need to be mutable
   --> Main.rs:108:17
    |
108 |             let mut i = r;
    |                 ----^
    |                 |
    |                 help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

warning: function `gcd` is never used
  --> Main.rs:47:4
   |
47 | fn gcd(mut a: u64, mut b: u64) -> u64 {
   |    ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 4 warnings emitted

ソースコード

diff #

use std::io::Write;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn gcd(mut a: u64, mut b: u64) -> u64 {
    if a == 0 || b == 0 {
        return a + b;
    }
    let k = (a | b).trailing_zeros();
    a >>= a.trailing_zeros();
    b >>= b.trailing_zeros();
    while a != b {
        if a < b {
            std::mem::swap(&mut a, &mut b);
        }
        a -= b;
        a >>= a.trailing_zeros();
    }
    a << k
}
fn mul(mut a: u64, mut b: u64, m: u64) -> u64 {
    let mut x = 0;
    while a > 0 {
        if a % 2 > 0 {
            x += b;
            if x >= m {
                x -= m;
            }
        }
        a /= 2;
        b += b;
        if b >= m {
            b -= m;
        }
    }
    x
}
fn powmod(mut a: u64, mut e: u64, m: u64) -> u64 {
    let mut x = 1;
    while e > 0 {
        if e % 2 > 0 {
            x = mul(x, a, m);
        }
        a = mul(a, a, m);
        e /= 2;
    }
    x
}
fn is_prime(n: u64) -> bool {
    if n <= 3 {
        return n == 2 || n == 3;
    } else if n % 2 == 0 {
        return false;
    }
    let r = (n - 1).trailing_zeros();
    let d = (n - 1) >> r;
    let mut test = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
        .iter()
        .filter(|&&a| a <= n - 2);
    'witness_loop: loop {
        if let Some(a) = test.next() {
            let mut x = powmod(a % n, d, n);
            if x == 1 || x == n - 1 {
                continue 'witness_loop;
            }
            let mut i = r;
            for i in 0..r {
                x = mul(x, x, n);
                if x == n - 1 {
                    continue 'witness_loop;
                }
            }
            return false;
        } else {
            return true;
        }
    }
}

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    input! {
        n: usize,
        x: [u64; n],
    }
    for x in x {
        let ans = if is_prime(x) {1} else {0};
        writeln!(out, "{} {}", x, ans).unwrap();
    }
}

fn main() {
    run();
}
0