結果
問題 | No.1901 bitwise xor convolution (characteristic 2) |
ユーザー |
|
提出日時 | 2023-03-27 23:22:48 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,484 bytes |
コンパイル時間 | 13,076 ms |
コンパイル使用メモリ | 402,704 KB |
実行使用メモリ | 110,068 KB |
最終ジャッジ日時 | 2024-09-19 17:35:20 |
合計ジャッジ時間 | 23,582 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 WA * 1 |
other | AC * 1 WA * 6 |
ソースコード
#[allow(unused_imports)]use std::cmp::*;#[allow(unused_imports)]use std::collections::*;use std::io::{Write, BufWriter};// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8macro_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);read_value!($next, [$t; len])}};($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));}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; } }}#[cfg(x86_64)]fn bit_conv(a: u32, b: u32) -> u64 {use std::arch::x86_64::*;unsafe {let a = _mm_set_epi32(0, 0, 0, a as i32);let b = _mm_set_epi32(0, 0, 0, b as i32);let prod = _mm_clmulepi64_si128(a, b, 0);(_mm_extract_epi32(prod, 1) as u32 as u64) << 32 | _mm_extract_epi32(prod, 0) as u32 as u64}}#[cfg(not(x86_64))]fn bit_conv(a: u32, b: u32) -> u64 {let mut res = 0;for i in 0..32 {if (a & 1 << i) != 0 {res ^= (b as u64) << i;}}res}// https://yukicoder.me/problems/no/1901 (4)// アダマール変換 -> 点ごとの積 -> アダマール変換 -> 2^n で割る をすればよく、// そのために各点で n+1 整数係数の 32 次未満・64 次未満の多項式を持ちたい。// これは 32bit 整数・64bit 整数を n+1 個持つ方針でできる。// 計算量は O(2^n n^2)、ただし _mm_clmulepi64_si128 を 2^n (n^2/2 + O(n)) 回呼ぶ。fn main() {let out = std::io::stdout();let mut out = BufWriter::new(out.lock());macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}#[allow(unused)]macro_rules! putvec {($v:expr) => {for i in 0..$v.len() {puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});}}}input! {n: usize,a: [[i32; 32]; 1 << n],b: [[i32; 32]; 1 << n],}let mut x = vec![[0u32; 32]; 1 << n];let mut y = vec![[0u32; 32]; 1 << n];for i in 0..1 << n {for j in 0..32 {if a[i][j] == 1 {x[i][j] = 1;}}for j in 0..32 {if b[i][j] == 1 {y[i][j] = 1;}}}for i in 0..n {for bits in 0..1 << n {if (bits & 1 << i) != 0 { continue; }for u in 0..32 {let p = x[bits][u];let q = x[bits | 1 << i][u];x[bits][u] = p.wrapping_add(q);x[bits | 1 << i][u] = p.wrapping_sub(q);let p = y[bits][u];let q = y[bits | 1 << i][u];y[bits][u] = p.wrapping_add(q);y[bits | 1 << i][u] = p.wrapping_sub(q);}}}let mut prod = vec![[0u32; 63]; 1 << n];for bits in 0..1 << n {let mut p = vec![0u32; n + 1];let mut q = vec![0u32; n + 1];for i in 0..n + 1 {for j in 0..32 {if (x[bits][j] & 1 << i) != 0 {p[i] |= 1 << j;}if (y[bits][j] & 1 << i) != 0 {q[i] |= 1 << j;}}}let mut res = vec![0u64; n + 1];for i in 0..n + 1 {let mut carry = 0u64;for j in 0..n + 1 - i {let tmp = bit_conv(p[i], q[j]);let me = res[i + j] ^ carry ^ tmp;carry = (res[i + j] & carry) | (carry & tmp) | (res[i + j] & tmp);res[i + j] = me;}}for i in 0..n + 1 {for j in 0..63 {if (res[i] & 1 << j) != 0 {prod[bits][j] |= 1 << i;}}}}for i in 0..n {for bits in 0..1 << n {if (bits & 1 << i) != 0 { continue; }for u in 0..63 {let p = prod[bits][u];let q = prod[bits | 1 << i][u];prod[bits][u] = p.wrapping_add(q);prod[bits | 1 << i][u] = p.wrapping_sub(q);}}}for v in &mut prod {for j in 0..63 {v[j] = (v[j] >> n) & 1;}putvec!(v);}}