結果

問題 No.1498 Factorization from -1 to 1
ユーザー koba-e964koba-e964
提出日時 2021-11-01 23:31:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 84 ms / 3,000 ms
コード長 4,009 bytes
コンパイル時間 6,784 ms
コンパイル使用メモリ 166,376 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-04-18 23:27:31
合計ジャッジ時間 5,184 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
8,960 KB
testcase_01 AC 30 ms
8,960 KB
testcase_02 AC 31 ms
8,960 KB
testcase_03 AC 45 ms
9,728 KB
testcase_04 AC 64 ms
9,600 KB
testcase_05 AC 83 ms
9,728 KB
testcase_06 AC 83 ms
9,856 KB
testcase_07 AC 83 ms
9,728 KB
testcase_08 AC 83 ms
9,728 KB
testcase_09 AC 84 ms
9,728 KB
testcase_10 AC 32 ms
8,960 KB
testcase_11 AC 32 ms
8,960 KB
testcase_12 AC 32 ms
8,960 KB
testcase_13 AC 31 ms
8,960 KB
testcase_14 AC 31 ms
8,960 KB
testcase_15 AC 32 ms
8,960 KB
testcase_16 AC 31 ms
8,960 KB
testcase_17 AC 30 ms
8,960 KB
testcase_18 AC 31 ms
8,960 KB
testcase_19 AC 30 ms
8,960 KB
testcase_20 AC 30 ms
8,960 KB
testcase_21 AC 32 ms
8,960 KB
testcase_22 AC 31 ms
8,960 KB
権限があれば一括ダウンロードができます

ソースコード

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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

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
}

/**
 * Calculates x s.t. x^2 = a (mod p)
 * p is prime
 * Verified by: CF #395 Div1-C
 *              (http://codeforces.com/contest/763/submission/24380573)
 */
fn modsqrt(mut a: i64, p: i64) -> Option<i64> {
    a %= p;
    if a == 0 {
        return Some(0);
    }
    if p == 2 {
        return Some(a);
    }
    if powmod(a, (p - 1) / 2, p) != 1 {
        return None;
    }
    let mut b = 1;
    while powmod(b, (p - 1) / 2, p) == 1 {
        b += 1;
    }
    let mut e = 0;
    let mut m = p - 1;
    while m % 2 == 0 {
        m /= 2;
        e += 1;
    }
    let mut x = powmod(a, (m - 1) / 2, p);
    let mut y = a * (x * x % p) % p;
    x = x * a % p;
    let mut z = powmod(b, m, p);
    while y != 1 {
        let mut j = 0;
        let mut t = y;
        while t != 1 {
            j += 1;
            t = t * t % p;
        }
        assert!(j < e);
        z = powmod(z, 1 << (e - j - 1), p);
        x = x * z % p;
        z = z * z % p;
        y = y * z % p;
        e = j;
    }
    Some(x)
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    macro_rules! putvec {
        ($v:expr) => {
            for i in 0..$v.len() {
                puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});
            }
        }
    }
    input! {
        q: usize,
        a: [usize; q],
    }
    const W: usize = 100_100;
    let mut pr = vec![true; W];
    pr[0] = false;
    pr[1] = false;
    for i in 2..W {
        if !pr[i] { continue; }
        for j in 2..(W - 1) / i + 1 {
            pr[i * j] = false;
        }
    }
    let mut sieve = vec![vec![]; W];
    for p in 2..W {
        if !pr[p] { continue; }
        if let Some(x) = modsqrt(p as i64 - 1, p as i64) {
            let x = x as usize;
            for j in 0..(W - x - 1) / p + 1 {
                sieve[x + j * p].push(p);
            }
            if 2 * x != p {
                let y = p - x;
                for j in 0..(W - y - 1) / p + 1 {
                    sieve[y + j * p].push(p);
                }
            }
        }
    }
    for a in a {
        let mut ans = vec![];
        let mut v = a * a + 1;
        for &p in &sieve[a] {
            while v % p == 0 {
                ans.push(p);
                v /= p;
            }
        }
        if v != 1 {
            ans.push(v);
        }
        putvec!(ans);
    }
}
0