Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
ArXiv:
Tags:
competitive-programming
code-generation
specification-generation
proof-generation
verification
rust
License:
File size: 3,726 Bytes
4c0e24e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | use vstd::prelude::*;
fn main() {}
verus! {
pub struct Solution;
pub open spec fn is_all_ones_square(mat: Seq<Vec<i32>>, r: int, c: int, s: int) -> bool {
&&& s >= 1
&&& r >= 0 && r + s <= mat.len()
&&& c >= 0 && c + s <= mat[0int].len()
&&& forall|dr: int, dc: int|
0 <= dr < s && 0 <= dc < s ==> #[trigger] mat[r + dr][c + dc] == 1
}
pub open spec fn min3(a: int, b: int, c: int) -> int {
if a <= b && a <= c { a }
else if b <= c { b }
else { c }
}
pub open spec fn dp_val(mat: Seq<Vec<i32>>, i: int, j: int) -> int
decreases (if i > 0 { i } else { 0 }) + (if j > 0 { j } else { 0 })
{
if i < 0 || j < 0 { 0 }
else if mat[i][j] != 1 { 0 }
else if i == 0 || j == 0 { 1 }
else { 1 + min3(dp_val(mat, i - 1, j), dp_val(mat, i, j - 1), dp_val(mat, i - 1, j - 1)) }
}
pub open spec fn row_sum(mat: Seq<Vec<i32>>, i: int, j: int, n: int) -> int
decreases (if j < n { n - j } else { 0 })
{
if j >= n { 0 }
else { dp_val(mat, i, j) + row_sum(mat, i, j + 1, n) }
}
pub open spec fn total_sum(mat: Seq<Vec<i32>>, i: int, m: int, n: int) -> int
decreases (if i < m { m - i } else { 0 })
{
if i >= m { 0 }
else { row_sum(mat, i, 0, n) + total_sum(mat, i + 1, m, n) }
}
impl Solution {
fn set_dp(dp: &mut Vec<Vec<i32>>, row: usize, col: usize, value: i32)
requires
row < old(dp)@.len(),
col < old(dp)@[row as int].len(),
ensures
dp@.len() == old(dp)@.len(),
forall |r: int| 0 <= r < dp@.len() ==> (#[trigger] dp@[r]).len() == old(dp)@[r].len(),
forall |r: int, c: int|
0 <= r < dp@.len() && 0 <= c < dp@[r].len()
==> #[trigger] dp@[r][c] == if r == row as int && c == col as int { value } else { old(dp)@[r][c] },
{
let mut current_row = dp[row].clone();
current_row.set(col, value);
dp.set(row, current_row);
}
pub fn count_squares(matrix: Vec<Vec<i32>>) -> (result: i32)
requires
1 <= matrix.len() <= 300,
forall |i: int| 0 <= i < matrix.len() ==> 1 <= (#[trigger] matrix[i]).len() <= 300,
forall |i: int| 0 <= i < matrix.len() ==> (#[trigger] matrix[i]).len() == matrix[0].len(),
forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len()
==> #[trigger] matrix[i][j] == 0 || matrix[i][j] == 1,
ensures
result as int == total_sum(matrix@, 0, matrix.len() as int, matrix[0].len() as int),
{
let m = matrix.len();
let n = matrix[0].len();
let mut dp: Vec<Vec<i32>> = Vec::new();
let mut i: usize = 0;
while i < m {
let mut row: Vec<i32> = Vec::new();
let mut j: usize = 0;
while j < n {
row.push(0);
j = j + 1;
}
dp.push(row);
i = i + 1;
}
let mut ans: i32 = 0;
i = 0;
while i < m {
let mut j: usize = 0;
while j < n {
if matrix[i][j] == 1 {
if i == 0 || j == 0 {
Self::set_dp(&mut dp, i, j, 1);
} else {
let a = dp[i - 1][j];
let b = dp[i][j - 1];
let c = dp[i - 1][j - 1];
let min_val = if a <= b && a <= c { a } else if b <= c { b } else { c };
Self::set_dp(&mut dp, i, j, 1 + min_val);
}
}
ans = ans + dp[i][j];
j = j + 1;
}
i = i + 1;
}
ans
}
}
}
|