結果
問題 | No.1498 Factorization from -1 to 1 |
ユーザー | koba-e964 |
提出日時 | 2021-11-01 23:31:44 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 78 ms / 3,000 ms |
コード長 | 3,709 bytes |
コンパイル時間 | 13,452 ms |
コンパイル使用メモリ | 379,148 KB |
実行使用メモリ | 9,856 KB |
最終ジャッジ日時 | 2024-10-10 17:01:44 |
合計ジャッジ時間 | 16,068 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 25 ms
8,960 KB |
testcase_01 | AC | 26 ms
8,960 KB |
testcase_02 | AC | 26 ms
8,960 KB |
testcase_03 | AC | 41 ms
9,728 KB |
testcase_04 | AC | 61 ms
9,856 KB |
testcase_05 | AC | 76 ms
9,728 KB |
testcase_06 | AC | 71 ms
9,856 KB |
testcase_07 | AC | 75 ms
9,600 KB |
testcase_08 | AC | 74 ms
9,600 KB |
testcase_09 | AC | 78 ms
9,728 KB |
testcase_10 | AC | 29 ms
9,088 KB |
testcase_11 | AC | 31 ms
8,960 KB |
testcase_12 | AC | 29 ms
8,960 KB |
testcase_13 | AC | 29 ms
8,960 KB |
testcase_14 | AC | 29 ms
8,960 KB |
testcase_15 | AC | 28 ms
8,960 KB |
testcase_16 | AC | 28 ms
9,088 KB |
testcase_17 | AC | 27 ms
8,960 KB |
testcase_18 | AC | 27 ms
8,960 KB |
testcase_19 | AC | 27 ms
8,960 KB |
testcase_20 | AC | 27 ms
8,960 KB |
testcase_21 | AC | 29 ms
8,832 KB |
testcase_22 | AC | 28 ms
8,960 KB |
ソースコード
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) } // Tags: quadratic-reciprocity 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); } }