結果

問題 No.981 一般冪乗根
ユーザー koba-e964koba-e964
提出日時 2020-03-03 02:06:17
言語 Rust
(1.77.0 + proconio)
結果
TLE  
実行時間 -
コード長 4,240 bytes
コンパイル時間 16,072 ms
コンパイル使用メモリ 378,884 KB
実行使用メモリ 9,520 KB
最終ジャッジ日時 2024-10-13 21:31:27
合計ジャッジ時間 142,714 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 AC 5,992 ms
5,248 KB
testcase_04 TLE -
testcase_05 AC 2,423 ms
5,248 KB
testcase_06 AC 2,340 ms
5,248 KB
testcase_07 AC 2,341 ms
5,248 KB
testcase_08 AC 2,339 ms
5,248 KB
testcase_09 AC 2,337 ms
5,248 KB
testcase_10 AC 2,330 ms
5,248 KB
testcase_11 AC 2,334 ms
5,248 KB
testcase_12 AC 2,322 ms
5,248 KB
testcase_13 AC 2,334 ms
5,248 KB
testcase_14 AC 2,331 ms
5,248 KB
testcase_15 AC 2,368 ms
5,248 KB
testcase_16 AC 2,322 ms
5,248 KB
testcase_17 AC 2,333 ms
5,248 KB
testcase_18 AC 2,330 ms
5,248 KB
testcase_19 AC 2,327 ms
5,248 KB
testcase_20 AC 2,414 ms
5,248 KB
testcase_21 AC 2,325 ms
5,248 KB
testcase_22 AC 2,346 ms
5,248 KB
testcase_23 AC 2,360 ms
5,248 KB
testcase_24 AC 2,338 ms
5,248 KB
testcase_25 TLE -
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
evil_60bit1.txt -- -
evil_60bit2.txt -- -
evil_60bit3.txt -- -
evil_hack -- -
evil_hard_random -- -
evil_hard_safeprime.txt -- -
evil_hard_tonelli0 -- -
evil_hard_tonelli1 -- -
evil_hard_tonelli2 -- -
evil_hard_tonelli3 -- -
evil_sefeprime1.txt -- -
evil_sefeprime2.txt -- -
evil_sefeprime3.txt -- -
evil_tonelli1.txt -- -
evil_tonelli2.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn powmod(x: i64, mut e: i64, m: i64) -> i64 {
    let mut sum = 1;
    let mut cur = x % m;
    while e > 0 {
        if e % 2 != 0 {
            sum = sum * cur % m;
        }
        cur = cur * cur % m;
        e /= 2;
    }
    sum
}

fn extgcd(x: i64, y: i64) -> (i64, i64, i64) {
    if y == 0 {
        return (x, 1, 0);
    }
    let r = x % y;
    let q = x / y;
    let (g, a, b) = extgcd(y, r);
    (g, b, a - b * q)
}

// Baby-step giant-step. https://codeforces.com/contest/1106/submission/49502575
// Find x s.t. x^exp = m
fn modroot(m: i64, exp: i64, p: i64) -> Option<i64> {
    // prime factors of p - 1
    let mut rel = vec![];
    {
        let mut v = p - 1;
        let mut q = 2;
        while v > 1 && v >= q * q {
            if v % q == 0 {
                rel.push(q);
                while v % q == 0 {
                    v /= q;
                }
            }
            q += 1;
        }
        if v > 1 {
            rel.push(v);
        }
    }
    let mut gen = 2;
    loop {
        let ok = rel.iter().all(|q| powmod(gen, (p - 1) / q, p) != 1);
        if ok {
            break;
        }
        gen += 1;
    }
    const SQRT: i64 = 40000;
    let mut cur = 1;
    let prog = powmod(gen, SQRT, p);
    let mut tbl = HashMap::new();
    for i in 0..SQRT {
        tbl.insert(cur, SQRT * i);
        cur = cur * prog % p;
    }
    let discrete_log = |x: i64| {
        for i in 0..SQRT {
            let key = x * powmod(gen, i, p) % p;
            if let Some(&y) = tbl.get(&key) {
                return (y - i + p - 1) % (p - 1);
            }
        }
        unreachable!();
    };
    let logm = discrete_log(m);
    let (g, inv, _) = extgcd(exp, p - 1);
    if logm % g != 0 {
        return None;
    }
    let inv = (inv + p - 1) % (p - 1);
    let ans = (logm / g * inv) % (p - 1);
    Some(powmod(gen, ans, p))
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (let _ = write!(out,$($format)*););
    }
    input! {
        pka: [(i64, i64, i64)],
    }
    for (p, k, a) in pka {
        let ans = modroot(a, k, p);
        puts!("{}\n", ans.unwrap_or(-1));
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0