結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー iwkjosec
提出日時 2019-06-02 16:50:53
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,385 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 931 ms
コンパイル使用メモリ 147,852 KB
最終ジャッジ日時 2026-05-13 04:37:04
合計ジャッジ時間 1,879 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: unsafe attribute used without unsafe
  --> src/main.rs:18:3
   |
18 | #[no_mangle]
   |   ^^^^^^^^^ usage of unsafe attribute
   |
help: wrap the attribute in `unsafe(...)`
   |
18 | #[unsafe(no_mangle)]
   |   +++++++         +

error: could not compile `main` (bin "main") due to 1 previous error

ソースコード

diff #
raw source code

use std::io;

fn main() {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    let n: i32 = buf.trim().parse().unwrap();

    for _ in 0..n {
        let mut buf = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        let i: i64 = buf.trim().parse().unwrap();

        println!("{} {}", i, if miller_rabin(i) { 1 } else { 0 });
    }
}
const BASES64: [i128; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022];

#[no_mangle]
pub extern "C" fn miller_rabin(n: i64) -> bool {
    if n < 2 {
        return false;
    }
    if (n & 1) == 0 {
        return n == 2;
    }
    let mut d = n - 1;
    let n = n as i128;
    let mut s = 0;
    while (d & 1) == 0 {
        s += 1;
        d >>= 1;
    }
    for a in BASES64.iter() {
        if *a >= n {
            break;
        }
        let mut c = true;
        let mut x = mod_pow(*a, d, n);
        if x == 1 {
            c = false;
        }
        for _ in 0..s {
            if x == n - 1 {
                c = false;
            }
            x = x * x % n;
        }
        if c {
            return false;
        }
    }
    return true;
}

fn mod_pow(mut x: i128, mut n: i64, m: i128) -> i128 {
    let mut res = 1i128;
    while n != 0 {
        if (n & 1) == 1 {
            res = res * x % m;
        }
        x = x * x % m;
        n >>= 1;
    }
    return res;
}
0