結果

問題 No.1141 田グリッド
ユーザー magurofly
提出日時 2025-10-19 21:18:25
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 3,439 bytes
コンパイル時間 12,604 ms
コンパイル使用メモリ 398,252 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2025-10-19 21:18:54
合計ジャッジ時間 16,994 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(dead_code, unused_imports, unused_macros, non_snake_case)]

fn main() {
    input! {
      H: usize, W: usize,
      A: [[i64; W]; H],
      Q: usize,
      queries: [(Usize1, Usize1); Q],
    }

    let A = (0 .. H).map(|i| (0 .. W).map(|j|
      if A[i][j] == 0 {
        (1, 1)
      } else {
        (A[i][j], 0)
      }
    ).to_vec() ).to_vec();

    let row_product = (0 .. H).map(|i| {
      let mut prod = (1, 0);
      for j in 0 .. W {
        prod = mul(prod, A[i][j]);
      }
      prod
    }).to_vec();

    let col_product = (0 .. W).map(|j| {
      let mut prod = (1, 0);
      for i in 0 .. H {
        prod = mul(prod, A[i][j]);
      }
      prod
    }).to_vec();let mut all_product = (1, 0);

    for i in 0 .. H {
      for j in 0 .. W {
        all_product = mul(all_product, A[i][j]);
      }
    }

    for &(r, c) in &queries {
      let mut ans = mul(all_product, A[r][c]);
      ans = mul(ans, inv(row_product[r]));
      ans = mul(ans, inv(col_product[c]));
      say(if ans.1 > 0 { 0 } else { ans.0 });
    }
}

fn mul(a: (i64, i64), b: (i64, i64)) -> (i64, i64) {
  (a.0 * b.0 % MOD, a.1 + b.1)
}

fn inv(a: (i64, i64)) -> (i64, i64) {
  let mut e = MOD - 2;
  let mut u = a.0;
  let mut r = 1;
  while e > 0 {
    if e & 1 == 1 {
      r = r * u % MOD;
    }
    e >>= 1;
    u = u * u % MOD;
  }
  (r, -a.1)
}

type Int = i64;
// const MOD: Int = 998244353;
const MOD: Int = 1_000_000_007;
const INF: Int = 1_000_000_000_000_000_000;
const YESNO: [&'static str; 2] = ["Yes", "No"];

use proconio::{input, marker::{Chars, Bytes, Usize1}};
use std::*;
use std::ops::*;
use collections::*; // (BTree|Hash)(Set|Map), BinaryHeap, VecDeque, LinkedList
use cmp::{self, Reverse}; // cmp::{min, max}

fn yes() { println!("{}", YESNO[0]); }
fn no() { println!("{}", YESNO[1]); }
fn yesno(c: bool) { println!("{}", if c { YESNO[0] } else { YESNO[1] }); }
fn say<T: std::fmt::Display>(x: T) -> T { println!("{}", x); x }
fn neighbor4<F: FnMut(usize, usize)>(i: usize, j: usize, h: usize, w: usize, mut f: F) { if i > 0 { (f)(i - 1, j); } if i < h - 1 { (f)(i + 1, j); } if j > 0 { (f)(i, j - 1); } if j < w - 1 { (f)(i, j + 1); } }

trait MyItertools : Iterator + Sized {
  fn to_vec(self) -> Vec<Self::Item> { self.collect::<Vec<_>>() }
  fn to_vec_rev(self) -> Vec<Self::Item> { let mut v = self.collect::<Vec<_>>(); v.reverse(); v }
  fn tally(self) -> HashMap<Self::Item, usize> where Self::Item: Copy + Eq + hash::Hash { let mut counts = HashMap::new(); self.for_each(|item| *counts.entry(item).or_default() += 1 ); counts }
  fn count_if<P: Fn(Self::Item) -> bool>(self, predicate: P) -> usize { self.map(predicate).filter(|&x| x ).count() }
  fn implode(self, sep: &str) -> String where Self::Item: std::string::ToString { self.map(|x| x.to_string()).to_vec().join(sep) }
  fn mex(self, gen: impl IntoIterator<Item = Self::Item>) -> Self::Item where Self::Item: Ord { let mut v = self.collect::<Vec<_>>(); v.sort(); v.dedup(); let mut it = v.into_iter(); gen.into_iter().find(|a| if let Some(x) = it.next() { a != &x } else { true }).unwrap() }
}
impl<T: ?Sized> MyItertools for T where T: Iterator + Sized {}

trait MyOrd : PartialOrd + Sized {
  fn chmax(&mut self, mut rhs: Self) -> bool { if self < &mut rhs { *self = rhs; true } else { false } }
  fn chmin(&mut self, mut rhs: Self) -> bool { if self > &mut rhs { *self = rhs; true } else { false } }
}
impl<T: Sized + PartialOrd> MyOrd for T {}

0