{"problem": "cf1036D", "family": "codeforces", "description": "# Vasya and Arrays\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nVasya has two arrays $$$A$$$ and $$$B$$$ of lengths $$$n$$$ and $$$m$$$, respectively.\n\nHe can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $$$[1, 10, 100, 1000, 10000]$$$ Vasya can obtain array $$$[1, 1110, 10000]$$$, and from array $$$[1, 2, 3]$$$ Vasya can obtain array $$$[6]$$$.\n\nTwo arrays $$$A$$$ and $$$B$$$ are considered equal if and only if they have the same length and for each valid $$$i$$$ $$$A_i = B_i$$$.\n\nVasya wants to perform some of these operations on array $$$A$$$, some on array $$$B$$$, in such a way that arrays $$$A$$$ and $$$B$$$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.\n\nHelp Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $$$A$$$ and $$$B$$$ equal.\n\n## Input\n\nThe first line contains a single integer $$$n~(1 \\le n \\le 3 \\cdot 10^5)$$$ \u2014 the length of the first array.\n\nThe second line contains $$$n$$$ integers $$$a_1, a_2, \\cdots, a_n~(1 \\le a_i \\le 10^9)$$$ \u2014 elements of the array $$$A$$$.\n\nThe third line contains a single integer $$$m~(1 \\le m \\le 3 \\cdot 10^5)$$$ \u2014 the length of the second array.\n\nThe fourth line contains $$$m$$$ integers $$$b_1, b_2, \\cdots, b_m~(1 \\le b_i \\le 10^9)$$$ - elements of the array $$$B$$$.\n\n## Output\n\nPrint a single integer \u2014 the maximum length of the resulting arrays after some operations were performed on arrays $$$A$$$ and $$$B$$$ in such a way that they became equal.\n\nIf there is no way to make array equal, print \"`-1`\".\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n11 2 3 5 7\n4\n11 7 3 7\n```\n**Output:**\n```\n3\n```\n\n### Example 2\n\n**Input:**\n```\n2\n1 2\n1\n100\n```\n**Output:**\n```\n-1\n```\n\n### Example 3\n\n**Input:**\n```\n3\n1 2 3\n3\n1 2 3\n```\n**Output:**\n```\n3\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_equal_block_count(a: Vec, b: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < n {\n let v: i64 = it.next().unwrap().parse().unwrap();\n a.push(v);\n idx = idx + 1;\n }\n let m: usize = it.next().unwrap().parse().unwrap();\n let mut b: Vec = Vec::new();\n idx = 0usize;\n while idx < m {\n let v: i64 = it.next().unwrap().parse().unwrap();\n b.push(v);\n idx = idx + 1;\n }\n let out = Solution::max_equal_block_count(a, b);\n println!(\"{}\", out);\n}\n```\n", "tags": "Rating: 1600\ngreedy, two pointers\nSolved by: 10509 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn total_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, nums.len() as int)\n }\n\n pub open spec fn block_sum(nums: Seq, lo: int, hi: int) -> int {\n Self::prefix_sum(nums, hi) - Self::prefix_sum(nums, lo)\n }\n\n pub open spec fn exists_valid_partition(a: Seq, na: int, b: Seq, nb: int, k: int) -> bool {\n exists|pa: Seq, pb: Seq| Self::valid_equal_partition(a, na, b, nb, k, pa, pb)\n }\n\n pub open spec fn valid_equal_partition(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq,\n ) -> bool {\n &&& k >= 1\n &&& pa.len() == k + 1\n &&& pb.len() == k + 1\n &&& pa[0] == 0\n &&& pa[k] == na\n &&& pb[0] == 0\n &&& pb[k] == nb\n &&& forall|i: int| 0 <= i < k ==> pa[i] < #[trigger] pa[i + 1]\n &&& forall|i: int| 0 <= i < k ==> pb[i] < #[trigger] pb[i + 1]\n &&& forall|i: int| 0 <= i < k ==>\n Self::block_sum(a, pa[i], #[trigger] pa[i + 1])\n == Self::block_sum(b, pb[i], pb[i + 1])\n }\n\n pub fn max_equal_block_count(a: Vec, b: Vec) -> (result: i64)\n requires\n 1 <= a.len() <= 300_000,\n 1 <= b.len() <= 300_000,\n forall|x: int| 0 <= x < a.len() ==> 1 <= #[trigger] a[x] as int && (a[x] as int) <= 1_000_000_000,\n forall|x: int| 0 <= x < b.len() ==> 1 <= #[trigger] b[x] as int && (b[x] as int) <= 1_000_000_000,\n Self::total_sum(a@) <= i64::MAX,\n Self::total_sum(b@) <= i64::MAX,\n ensures\n result >= -1,\n (result == -1) == (Self::total_sum(a@) != Self::total_sum(b@)),\n result >= 0 ==> result >= 1,\n result >= 0 ==> exists|pa: Seq, pb: Seq|\n Self::valid_equal_partition(a@, a.len() as int, b@, b.len() as int, result as int, pa, pb),\n result >= 0 ==> forall|k: int| k > result as int ==>\n !(#[trigger] Self::exists_valid_partition(a@, a.len() as int, b@, b.len() as int, k)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_equal_block_count(a: Vec, b: Vec) -> i64 {\n let n = a.len();\n let m = b.len();\n let mut ta: i64 = 0;\n let mut tb: i64 = 0;\n let mut u = 0usize;\n while u < n {\n ta = ta + a[u];\n u = u + 1;\n }\n u = 0usize;\n while u < m {\n tb = tb + b[u];\n u = u + 1;\n }\n if ta != tb {\n return -1;\n }\n let mut i = 0usize;\n let mut j = 0usize;\n let mut sa: i64 = 0;\n let mut sb: i64 = 0;\n let mut ans: i64 = 0;\n while i < n || j < m {\n if sa <= sb {\n if i < n {\n sa = sa + a[i];\n i = i + 1;\n } else {\n sb = sb + b[j];\n j = j + 1;\n }\n } else {\n if j < m {\n sb = sb + b[j];\n j = j + 1;\n } else {\n sa = sa + a[i];\n i = i + 1;\n }\n }\n if sa == sb && sa > 0 {\n ans = ans + 1;\n sa = 0;\n sb = 0;\n }\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn total_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, nums.len() as int)\n }\n\n pub open spec fn block_sum(nums: Seq, lo: int, hi: int) -> int {\n Self::prefix_sum(nums, hi) - Self::prefix_sum(nums, lo)\n }\n\n pub open spec fn exists_valid_partition(a: Seq, na: int, b: Seq, nb: int, k: int) -> bool {\n exists|pa: Seq, pb: Seq| Self::valid_equal_partition(a, na, b, nb, k, pa, pb)\n }\n\n pub open spec fn valid_equal_partition(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq,\n ) -> bool {\n &&& k >= 1\n &&& pa.len() == k + 1\n &&& pb.len() == k + 1\n &&& pa[0] == 0\n &&& pa[k] == na\n &&& pb[0] == 0\n &&& pb[k] == nb\n &&& forall|i: int| 0 <= i < k ==> pa[i] < #[trigger] pa[i + 1]\n &&& forall|i: int| 0 <= i < k ==> pb[i] < #[trigger] pb[i + 1]\n &&& forall|i: int| 0 <= i < k ==>\n Self::block_sum(a, pa[i], #[trigger] pa[i + 1])\n == Self::block_sum(b, pb[i], pb[i + 1])\n }\n\n pub fn max_equal_block_count(a: Vec, b: Vec) -> (result: i64)\n requires\n 1 <= a.len() <= 300_000,\n 1 <= b.len() <= 300_000,\n forall|x: int| 0 <= x < a.len() ==> 1 <= #[trigger] a[x] as int && (a[x] as int) <= 1_000_000_000,\n forall|x: int| 0 <= x < b.len() ==> 1 <= #[trigger] b[x] as int && (b[x] as int) <= 1_000_000_000,\n Self::total_sum(a@) <= i64::MAX,\n Self::total_sum(b@) <= i64::MAX,\n ensures\n result >= -1,\n (result == -1) == (Self::total_sum(a@) != Self::total_sum(b@)),\n result >= 0 ==> result >= 1,\n result >= 0 ==> exists|pa: Seq, pb: Seq|\n Self::valid_equal_partition(a@, a.len() as int, b@, b.len() as int, result as int, pa, pb),\n result >= 0 ==> forall|k: int| k > result as int ==>\n !(#[trigger] Self::exists_valid_partition(a@, a.len() as int, b@, b.len() as int, k)),\n {\n let n = a.len();\n let m = b.len();\n let mut ta: i64 = 0;\n let mut tb: i64 = 0;\n let mut u = 0usize;\n while u < n {\n ta = ta + a[u];\n u = u + 1;\n }\n u = 0usize;\n while u < m {\n tb = tb + b[u];\n u = u + 1;\n }\n if ta != tb {\n return -1;\n }\n let mut i = 0usize;\n let mut j = 0usize;\n let mut sa: i64 = 0;\n let mut sb: i64 = 0;\n let mut ans: i64 = 0;\n while i < n || j < m {\n if sa <= sb {\n if i < n {\n sa = sa + a[i];\n i = i + 1;\n } else {\n sb = sb + b[j];\n j = j + 1;\n }\n } else {\n if j < m {\n sb = sb + b[j];\n j = j + 1;\n } else {\n sa = sa + a[i];\n i = i + 1;\n }\n }\n if sa == sb && sa > 0 {\n ans = ans + 1;\n sa = 0;\n sb = 0;\n }\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn total_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, nums.len() as int)\n }\n\n pub open spec fn block_sum(nums: Seq, lo: int, hi: int) -> int {\n Self::prefix_sum(nums, hi) - Self::prefix_sum(nums, lo)\n }\n\n pub open spec fn exists_valid_partition(a: Seq, na: int, b: Seq, nb: int, k: int) -> bool {\n exists|pa: Seq, pb: Seq| Self::valid_equal_partition(a, na, b, nb, k, pa, pb)\n }\n\n pub open spec fn no_bigger_partition_below(a: Seq, na: int, b: Seq, nb: int, threshold: int, ans: int) -> bool {\n forall|pa: Seq, pb: Seq, k: int|\n #[trigger] Self::valid_equal_partition(a, na, b, nb, k, pa, pb) ==>\n forall|p: int| 0 <= p <= k && Self::prefix_sum(a, pa[p]) <= threshold ==> p <= ans\n }\n\n pub open spec fn valid_equal_partition(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq,\n ) -> bool {\n &&& k >= 1\n &&& pa.len() == k + 1\n &&& pb.len() == k + 1\n &&& pa[0] == 0\n &&& pa[k] == na\n &&& pb[0] == 0\n &&& pb[k] == nb\n &&& forall|i: int| 0 <= i < k ==> pa[i] < #[trigger] pa[i + 1]\n &&& forall|i: int| 0 <= i < k ==> pb[i] < #[trigger] pb[i + 1]\n &&& forall|i: int| 0 <= i < k ==>\n Self::block_sum(a, pa[i], #[trigger] pa[i + 1])\n == Self::block_sum(b, pb[i], pb[i + 1])\n }\n\n pub fn max_equal_block_count(a: Vec, b: Vec) -> (result: i64)\n requires\n 1 <= a.len() <= 300_000,\n 1 <= b.len() <= 300_000,\n forall|x: int| 0 <= x < a.len() ==> 1 <= #[trigger] a[x] as int && (a[x] as int) <= 1_000_000_000,\n forall|x: int| 0 <= x < b.len() ==> 1 <= #[trigger] b[x] as int && (b[x] as int) <= 1_000_000_000,\n Self::total_sum(a@) <= i64::MAX,\n Self::total_sum(b@) <= i64::MAX,\n ensures\n result >= -1,\n (result == -1) == (Self::total_sum(a@) != Self::total_sum(b@)),\n result >= 0 ==> result >= 1,\n result >= 0 ==> exists|pa: Seq, pb: Seq|\n Self::valid_equal_partition(a@, a.len() as int, b@, b.len() as int, result as int, pa, pb),\n result >= 0 ==> forall|k: int| k > result as int ==>\n !(#[trigger] Self::exists_valid_partition(a@, a.len() as int, b@, b.len() as int, k)),\n {\n proof {\n assert(Self::total_sum(a@) <= i64::MAX);\n assert(Self::total_sum(b@) <= i64::MAX);\n }\n let n = a.len();\n let m = b.len();\n let mut ta: i64 = 0;\n let mut tb: i64 = 0;\n let mut u = 0usize;\n while u < n\n invariant\n n == a.len(),\n u <= n,\n ta as int == Self::prefix_sum(a@, u as int),\n forall|x: int| 0 <= x < a.len() ==> 1 <= #[trigger] a[x] as int,\n Self::total_sum(a@) <= i64::MAX,\n decreases n - u,\n {\n proof {\n lemma_prefix_sum_step(a@, u as int);\n lemma_prefix_sum_le_total(a@, (u as int) + 1);\n lemma_prefix_sum_nonneg_at(a@, (u as int) + 1);\n }\n ta = ta + a[u];\n u = u + 1;\n }\n proof {\n assert(ta as int == Self::total_sum(a@));\n }\n u = 0usize;\n while u < m\n invariant\n m == b.len(),\n u <= m,\n tb as int == Self::prefix_sum(b@, u as int),\n forall|x: int| 0 <= x < b.len() ==> 1 <= #[trigger] b[x] as int,\n Self::total_sum(b@) <= i64::MAX,\n decreases m - u,\n {\n proof {\n lemma_prefix_sum_step(b@, u as int);\n lemma_prefix_sum_le_total(b@, (u as int) + 1);\n lemma_prefix_sum_nonneg_at(b@, (u as int) + 1);\n }\n tb = tb + b[u];\n u = u + 1;\n }\n proof {\n assert(tb as int == Self::total_sum(b@));\n }\n if ta != tb {\n proof {\n assert(Self::total_sum(a@) != Self::total_sum(b@));\n }\n return -1;\n }\n proof {\n assert(Self::total_sum(a@) == Self::total_sum(b@));\n }\n let mut i = 0usize;\n let mut j = 0usize;\n let mut sa: i64 = 0;\n let mut sb: i64 = 0;\n let mut ans: i64 = 0;\n proof {\n reveal_with_fuel(Solution::prefix_sum, 2);\n assert(Self::prefix_sum(a@, 0) == 0);\n assert(Self::prefix_sum(b@, 0) == 0);\n assert forall|pa: Seq, pb: Seq, k: int|\n #[trigger] Self::valid_equal_partition(a@, n as int, b@, m as int, k, pa, pb)\n implies forall|p: int| 0 <= p <= k && Self::prefix_sum(a@, pa[p]) <= Self::prefix_sum(a@, 0) ==> p <= 0\n by {\n assert forall|p: int| 0 <= p <= k && Self::prefix_sum(a@, pa[p]) <= Self::prefix_sum(a@, 0)\n implies p <= 0\n by {\n if p > 0 {\n assert(pa[0] == 0);\n lemma_valid_partition_pa_mono(a@, n as int, b@, m as int, k, pa, pb, 0, p);\n lemma_valid_partition_pa_le_na(a@, n as int, b@, m as int, k, pa, pb, p);\n lemma_prefix_sum_strict_mono(a@, pa[0], pa[p]);\n assert(false);\n }\n }\n };\n assert(Self::no_bigger_partition_below(a@, n as int, b@, m as int, Self::prefix_sum(a@, 0), 0));\n }\n let ghost mut seg_i: int = 0;\n let ghost mut seg_j: int = 0;\n let ghost mut gpa: Seq = Seq::empty().push(0int);\n let ghost mut gpb: Seq = Seq::empty().push(0int);\n while i < n || j < m\n invariant\n n == a.len(),\n m == b.len(),\n n <= 300_000,\n m <= 300_000,\n i <= n,\n j <= m,\n 0 <= seg_i && seg_i <= i as int,\n 0 <= seg_j && seg_j <= j as int,\n forall|x: int| 0 <= x < a.len() ==> 1 <= #[trigger] a[x] as int,\n forall|x: int| 0 <= x < b.len() ==> 1 <= #[trigger] b[x] as int,\n Self::total_sum(a@) == Self::total_sum(b@),\n Self::total_sum(a@) <= i64::MAX,\n Self::total_sum(b@) <= i64::MAX,\n sa as int == Self::prefix_sum(a@, i as int) - Self::prefix_sum(a@, seg_i),\n sb as int == Self::prefix_sum(b@, j as int) - Self::prefix_sum(b@, seg_j),\n 0 <= sa as int && 0 <= sb as int,\n 0 <= ans,\n seg_i as int >= ans as int,\n seg_j as int >= ans as int,\n Self::prefix_sum(a@, seg_i) == Self::prefix_sum(b@, seg_j),\n sa as int == sb as int ==> sa == 0i64,\n gpa.len() == (ans as int) + 1,\n gpb.len() == (ans as int) + 1,\n gpa[0] == 0int,\n gpb[0] == 0int,\n gpa[ans as int] == seg_i,\n gpb[ans as int] == seg_j,\n forall|p: int| 0 <= p < ans as int ==> gpa[p] < #[trigger] gpa[p + 1],\n forall|p: int| 0 <= p < ans as int ==> gpb[p] < #[trigger] gpb[p + 1],\n forall|p: int| 0 <= p <= ans as int ==>\n Self::prefix_sum(a@, #[trigger] gpa[p]) == Self::prefix_sum(b@, gpb[p]),\n forall|y: int| seg_j < y < j as int ==>\n #[trigger] Self::prefix_sum(b@, y) - Self::prefix_sum(b@, seg_j) < sa as int,\n forall|x: int| seg_i < x < i as int ==>\n #[trigger] Self::prefix_sum(a@, x) - Self::prefix_sum(a@, seg_i) < sb as int,\n forall|x: int, y: int|\n #![trigger Self::prefix_sum(a@, x), Self::prefix_sum(b@, y)]\n seg_i <= x <= i as int && seg_j <= y <= j as int\n && Self::prefix_sum(a@, x) == Self::prefix_sum(b@, y)\n ==> (x == seg_i && y == seg_j) || (x == i as int && y == j as int),\n Self::no_bigger_partition_below(a@, n as int, b@, m as int, Self::prefix_sum(a@, seg_i), ans as int),\n decreases (n + m) - (i + j),\n {\n proof {\n assert((i as int) < (n as int) || (j as int) < (m as int));\n assert((i as int) + (j as int) < (n as int) + (m as int));\n }\n if sa <= sb {\n if i < n {\n proof {\n let ii: int = i as int;\n lemma_prefix_sum_mono(a@, seg_i, ii);\n lemma_prefix_sum_step(a@, ii);\n let t1: int = Self::prefix_sum(a@, ii + 1) - Self::prefix_sum(a@, seg_i);\n assert((sa as int) + (a@[ii] as int) == t1);\n lemma_prefix_sum_le_total(a@, ii + 1);\n lemma_prefix_sum_nonneg_at(a@, seg_i);\n lemma_prefix_sum_mono(a@, ii + 1, a.len() as int);\n assert(t1 <= Self::total_sum(a@));\n assert((sa as int) + (a@[ii] as int) <= i64::MAX);\n assert forall|y: int| seg_j < y < (j as int) implies\n #[trigger] Self::prefix_sum(b@, y) - Self::prefix_sum(b@, seg_j) < t1\n by {\n assert(Self::prefix_sum(b@, y) - Self::prefix_sum(b@, seg_j) < sa as int);\n };\n if seg_i < ii {\n lemma_prefix_sum_strict_mono(a@, seg_i, ii);\n assert((sa as int) > 0);\n assert((sa as int) < (sb as int));\n }\n assert forall|x: int| seg_i < x < ii + 1 implies\n #[trigger] Self::prefix_sum(a@, x) - Self::prefix_sum(a@, seg_i) < (sb as int)\n by {\n if x < ii {\n assert(Self::prefix_sum(a@, x) - Self::prefix_sum(a@, seg_i) < sb as int);\n } else {\n assert(x == ii);\n }\n };\n assert forall|x: int, y: int|\n #![trigger Self::prefix_sum(a@, x), Self::prefix_sum(b@, y)]\n seg_i <= x <= ii + 1 && seg_j <= y <= (j as int)\n && Self::prefix_sum(a@, x) == Self::prefix_sum(b@, y)\n implies (x == seg_i && y == seg_j) || (x == ii + 1 && y == (j as int))\n by {\n if x <= ii {\n assert((x == seg_i && y == seg_j) || (x == ii && y == (j as int)));\n if x == ii && y == (j as int) {\n assert((sa as int) == (sb as int));\n assert((sa as int) == 0);\n lemma_equal_prefix_sum_implies_equal_idx(a@, seg_i, ii);\n lemma_equal_prefix_sum_implies_equal_idx(b@, seg_j, j as int);\n }\n }\n };\n }\n sa = sa + a[i];\n i = i + 1;\n } else {\n proof {\n let jj: int = j as int;\n lemma_prefix_sum_mono(b@, seg_j, jj);\n lemma_prefix_sum_step(b@, jj);\n let t2: int = Self::prefix_sum(b@, jj + 1) - Self::prefix_sum(b@, seg_j);\n assert((sb as int) + (b@[jj] as int) == t2);\n lemma_prefix_sum_le_total(b@, jj + 1);\n lemma_prefix_sum_nonneg_at(b@, seg_j);\n lemma_prefix_sum_mono(b@, jj + 1, b.len() as int);\n assert((sb as int) + (b@[jj] as int) <= i64::MAX);\n }\n sb = sb + b[j];\n j = j + 1;\n }\n } else {\n if j < m {\n proof {\n let jj2: int = j as int;\n lemma_prefix_sum_mono(b@, seg_j, jj2);\n lemma_prefix_sum_step(b@, jj2);\n let t3: int = Self::prefix_sum(b@, jj2 + 1) - Self::prefix_sum(b@, seg_j);\n assert((sb as int) + (b@[jj2] as int) == t3);\n lemma_prefix_sum_le_total(b@, jj2 + 1);\n lemma_prefix_sum_nonneg_at(b@, seg_j);\n lemma_prefix_sum_mono(b@, jj2 + 1, b.len() as int);\n assert((sb as int) + (b@[jj2] as int) <= i64::MAX);\n }\n sb = sb + b[j];\n j = j + 1;\n } else {\n proof {\n let ii2: int = i as int;\n lemma_prefix_sum_mono(a@, seg_i, ii2);\n lemma_prefix_sum_step(a@, ii2);\n let t4: int = Self::prefix_sum(a@, ii2 + 1) - Self::prefix_sum(a@, seg_i);\n assert((sa as int) + (a@[ii2] as int) == t4);\n lemma_prefix_sum_le_total(a@, ii2 + 1);\n lemma_prefix_sum_nonneg_at(a@, seg_i);\n lemma_prefix_sum_mono(a@, ii2 + 1, a.len() as int);\n assert((sa as int) + (a@[ii2] as int) <= i64::MAX);\n }\n sa = sa + a[i];\n i = i + 1;\n }\n }\n if sa == sb && sa > 0 {\n proof {\n assert(Self::prefix_sum(a@, i as int) == Self::prefix_sum(b@, j as int));\n lemma_positive_tail_implies_hi_gt_lo(a@, seg_i, i as int);\n lemma_positive_tail_implies_hi_gt_lo(b@, seg_j, j as int);\n assert((i as int) > seg_i);\n assert((j as int) > seg_j);\n assert((ans as int) + 1 <= (i as int));\n assert((ans as int) + 1 <= (j as int));\n assert((i as int) <= (n as int));\n assert((ans as int) + 1 <= (n as int));\n assert((n as int) <= 300_000);\n assert((ans as int) + 1 <= i64::MAX);\n let old_ans = ans as int;\n gpa = gpa.push(i as int);\n gpb = gpb.push(j as int);\n assert(gpa.len() == old_ans + 2);\n assert(gpb.len() == old_ans + 2);\n assert(gpa[old_ans + 1] == i as int);\n assert(gpb[old_ans + 1] == j as int);\n let old_seg_i: int = seg_i;\n let old_seg_j: int = seg_j;\n assert forall|pa: Seq, pb: Seq, k: int|\n #[trigger] Self::valid_equal_partition(a@, n as int, b@, m as int, k, pa, pb)\n implies forall|p: int|\n 0 <= p <= k && Self::prefix_sum(a@, pa[p]) <= Self::prefix_sum(a@, i as int) ==> p <= old_ans + 1\n by {\n assert forall|p: int|\n 0 <= p <= k && Self::prefix_sum(a@, pa[p]) <= Self::prefix_sum(a@, i as int)\n implies p <= old_ans + 1\n by {\n if Self::prefix_sum(a@, pa[p]) <= Self::prefix_sum(a@, old_seg_i) {\n assert(p <= old_ans);\n } else {\n lemma_valid_partition_prefix_match(a@, n as int, b@, m as int, k, pa, pb, p);\n lemma_valid_partition_pa_le_na(a@, n as int, b@, m as int, k, pa, pb, p);\n lemma_valid_partition_pb_le_nb(a@, n as int, b@, m as int, k, pa, pb, p);\n if pa[p] <= old_seg_i {\n lemma_prefix_sum_mono(a@, pa[p], old_seg_i);\n assert(false);\n }\n if pa[p] > i as int {\n lemma_prefix_sum_strict_mono(a@, i as int, pa[p]);\n assert(false);\n }\n if pb[p] <= old_seg_j {\n lemma_prefix_sum_mono(b@, pb[p], old_seg_j);\n assert(false);\n }\n if pb[p] > j as int {\n lemma_prefix_sum_strict_mono(b@, j as int, pb[p]);\n assert(false);\n }\n assert(old_seg_i <= pa[p] <= i as int);\n assert(old_seg_j <= pb[p] <= j as int);\n assert(Self::prefix_sum(a@, pa[p]) == Self::prefix_sum(b@, pb[p]));\n assert((pa[p] == old_seg_i && pb[p] == old_seg_j) || (pa[p] == i as int && pb[p] == j as int));\n assert(pa[p] == i as int);\n if p >= 1 {\n if Self::prefix_sum(a@, pa[p - 1]) > Self::prefix_sum(a@, old_seg_i) {\n lemma_valid_partition_pa_mono(a@, n as int, b@, m as int, k, pa, pb, p - 1, p);\n assert(pa[p - 1] < i as int);\n if pa[p - 1] <= old_seg_i {\n lemma_prefix_sum_mono(a@, pa[p - 1], old_seg_i);\n assert(false);\n }\n lemma_valid_partition_prefix_match(a@, n as int, b@, m as int, k, pa, pb, p - 1);\n lemma_valid_partition_pa_le_na(a@, n as int, b@, m as int, k, pa, pb, p - 1);\n lemma_valid_partition_pb_le_nb(a@, n as int, b@, m as int, k, pa, pb, p - 1);\n if pb[p - 1] <= old_seg_j {\n lemma_prefix_sum_mono(b@, pb[p - 1], old_seg_j);\n assert(false);\n }\n if pb[p - 1] > j as int {\n lemma_prefix_sum_strict_mono(b@, j as int, pb[p - 1]);\n assert(false);\n }\n assert(old_seg_i <= pa[p - 1] <= i as int);\n assert(old_seg_j <= pb[p - 1] <= j as int);\n assert(Self::prefix_sum(a@, pa[p - 1]) == Self::prefix_sum(b@, pb[p - 1]));\n assert((pa[p - 1] == old_seg_i && pb[p - 1] == old_seg_j)\n || (pa[p - 1] == i as int && pb[p - 1] == j as int));\n assert(false);\n }\n assert(p - 1 <= old_ans);\n }\n }\n };\n };\n assert(Self::no_bigger_partition_below(a@, n as int, b@, m as int, Self::prefix_sum(a@, i as int), old_ans + 1));\n }\n ans = ans + 1;\n sa = 0;\n sb = 0;\n proof {\n seg_i = i as int;\n seg_j = j as int;\n }\n }\n }\n proof {\n assert(i == n);\n assert(j == m);\n assert(sa as int == Self::prefix_sum(a@, n as int) - Self::prefix_sum(a@, seg_i));\n assert(sb as int == Self::prefix_sum(b@, m as int) - Self::prefix_sum(b@, seg_j));\n assert(Self::prefix_sum(a@, seg_i) == Self::prefix_sum(b@, seg_j));\n assert(Self::total_sum(a@) == Self::total_sum(b@));\n assert(sa as int == sb as int);\n assert(sa == 0i64);\n lemma_equal_prefix_sum_implies_equal_idx(a@, seg_i, n as int);\n assert(seg_i == n as int);\n lemma_equal_prefix_sum_implies_equal_idx(b@, seg_j, m as int);\n assert(seg_j == m as int);\n assert(gpa[ans as int] == n as int);\n assert(gpb[ans as int] == m as int);\n if ans == 0 {\n assert(gpa[0] == 0int);\n assert(gpa[0] == n as int);\n assert(n == 0);\n assert(false);\n }\n assert(ans >= 1);\n assert forall|p: int| 0 <= p < ans as int implies\n Self::block_sum(a@, gpa[p], #[trigger] gpa[p + 1])\n == Self::block_sum(b@, gpb[p], gpb[p + 1])\n by {\n assert(Self::prefix_sum(a@, gpa[p]) == Self::prefix_sum(b@, gpb[p]));\n assert(Self::prefix_sum(a@, gpa[p + 1]) == Self::prefix_sum(b@, gpb[p + 1]));\n }\n assert(Self::valid_equal_partition(\n a@, n as int, b@, m as int, ans as int, gpa, gpb,\n ));\n assert(!(Self::total_sum(a@) != Self::total_sum(b@)));\n }\n ans\n }\n}\n\nproof fn lemma_prefix_sum_step(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n ensures\n Solution::prefix_sum(nums, i + 1) == Solution::prefix_sum(nums, i) + nums[i] as int,\n{\n}\n\nproof fn lemma_prefix_sum_le_total(nums: Seq, i: int)\n requires\n 0 <= i <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] as int,\n ensures\n Solution::prefix_sum(nums, i) <= Solution::total_sum(nums),\n decreases nums.len() - i,\n{\n if i == nums.len() as int {\n } else {\n lemma_prefix_sum_step(nums, i);\n lemma_prefix_sum_le_total(nums, i + 1);\n }\n}\n\nproof fn lemma_prefix_sum_nonneg_at(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] as int,\n ensures\n Solution::prefix_sum(nums, end) >= 0,\n decreases end,\n{\n if end <= 0 {\n } else {\n lemma_prefix_sum_nonneg_at(nums, end - 1);\n }\n}\n\nproof fn lemma_prefix_sum_mono(nums: Seq, a: int, b: int)\n requires\n 0 <= a <= b <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] as int,\n ensures\n Solution::prefix_sum(nums, a) <= Solution::prefix_sum(nums, b),\n decreases b - a,\n{\n if a == b {\n } else {\n lemma_prefix_sum_mono(nums, a, b - 1);\n }\n}\n\nproof fn lemma_positive_tail_implies_hi_gt_lo(nums: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] as int,\n Solution::prefix_sum(nums, hi) - Solution::prefix_sum(nums, lo) > 0,\n ensures\n lo < hi,\n{\n if lo >= hi {\n assert(lo == hi);\n assert(Solution::prefix_sum(nums, hi) - Solution::prefix_sum(nums, lo) == 0);\n assert(false);\n }\n}\n\nproof fn lemma_equal_prefix_sum_implies_equal_idx(nums: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] as int,\n Solution::prefix_sum(nums, lo) == Solution::prefix_sum(nums, hi),\n ensures\n lo == hi,\n{\n if lo < hi {\n lemma_prefix_sum_step(nums, lo);\n lemma_prefix_sum_mono(nums, lo + 1, hi);\n }\n}\n\nproof fn lemma_prefix_sum_strict_mono(nums: Seq, a: int, b: int)\n requires\n 0 <= a < b <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] as int,\n ensures\n Solution::prefix_sum(nums, a) < Solution::prefix_sum(nums, b),\n{\n lemma_prefix_sum_step(nums, a);\n lemma_prefix_sum_mono(nums, a + 1, b);\n}\n\nproof fn lemma_valid_partition_pa_mono(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq, p1: int, p2: int,\n)\n requires\n Solution::valid_equal_partition(a, na, b, nb, k, pa, pb),\n 0 <= p1 < p2 <= k,\n ensures\n pa[p1] < pa[p2],\n decreases p2 - p1,\n{\n assert(0 <= p2 - 1 < k);\n assert(pa[p2 - 1] < pa[(p2 - 1) + 1]);\n assert(pa[(p2 - 1) + 1] == pa[p2]);\n assert(pa[p2 - 1] < pa[p2]);\n if p2 - 1 > p1 {\n lemma_valid_partition_pa_mono(a, na, b, nb, k, pa, pb, p1, p2 - 1);\n }\n}\n\nproof fn lemma_valid_partition_pa_le_na(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq, p: int,\n)\n requires\n Solution::valid_equal_partition(a, na, b, nb, k, pa, pb),\n 0 <= p <= k,\n ensures\n pa[p] <= na,\n{\n if p < k {\n lemma_valid_partition_pa_mono(a, na, b, nb, k, pa, pb, p, k);\n }\n}\n\nproof fn lemma_valid_partition_pb_mono(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq, p1: int, p2: int,\n)\n requires\n Solution::valid_equal_partition(a, na, b, nb, k, pa, pb),\n 0 <= p1 < p2 <= k,\n ensures\n pb[p1] < pb[p2],\n decreases p2 - p1,\n{\n assert(0 <= p2 - 1 < k);\n assert(pb[p2 - 1] < pb[(p2 - 1) + 1]);\n assert(pb[(p2 - 1) + 1] == pb[p2]);\n assert(pb[p2 - 1] < pb[p2]);\n if p2 - 1 > p1 {\n lemma_valid_partition_pb_mono(a, na, b, nb, k, pa, pb, p1, p2 - 1);\n }\n}\n\nproof fn lemma_valid_partition_pb_le_nb(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq, p: int,\n)\n requires\n Solution::valid_equal_partition(a, na, b, nb, k, pa, pb),\n 0 <= p <= k,\n ensures\n pb[p] <= nb,\n{\n if p < k {\n lemma_valid_partition_pb_mono(a, na, b, nb, k, pa, pb, p, k);\n }\n}\n\nproof fn lemma_valid_partition_prefix_match(\n a: Seq, na: int, b: Seq, nb: int,\n k: int, pa: Seq, pb: Seq, p: int,\n)\n requires\n Solution::valid_equal_partition(a, na, b, nb, k, pa, pb),\n 0 <= p <= k,\n ensures\n Solution::prefix_sum(a, pa[p]) == Solution::prefix_sum(b, pb[p]),\n decreases p,\n{\n if p == 0 {\n assert(pa[0] == 0);\n assert(pb[0] == 0);\n } else {\n lemma_valid_partition_prefix_match(a, na, b, nb, k, pa, pb, p - 1);\n assert(0 <= p - 1 < k);\n assert(Solution::block_sum(a, pa[p - 1], pa[(p - 1) + 1])\n == Solution::block_sum(b, pb[p - 1], pb[(p - 1) + 1]));\n assert(pa[(p - 1) + 1] == pa[p]);\n assert(pb[(p - 1) + 1] == pb[p]);\n assert(Solution::block_sum(a, pa[p - 1], pa[p]) == Solution::block_sum(b, pb[p - 1], pb[p]));\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_equal_block_count(a: Vec, b: Vec) -> i64 {\n let n = a.len();\n let m = b.len();\n let mut ta: i64 = 0;\n let mut tb: i64 = 0;\n let mut u = 0usize;\n while u < n {\n ta = ta + a[u];\n u = u + 1;\n }\n u = 0usize;\n while u < m {\n tb = tb + b[u];\n u = u + 1;\n }\n if ta != tb {\n return -1;\n }\n let mut i = 0usize;\n let mut j = 0usize;\n let mut sa: i64 = 0;\n let mut sb: i64 = 0;\n let mut ans: i64 = 0;\n while i < n || j < m {\n if sa <= sb {\n if i < n {\n sa = sa + a[i];\n i = i + 1;\n } else {\n sb = sb + b[j];\n j = j + 1;\n }\n } else {\n if j < m {\n sb = sb + b[j];\n j = j + 1;\n } else {\n sa = sa + a[i];\n i = i + 1;\n }\n }\n if sa == sb && sa > 0 {\n ans = ans + 1;\n sa = 0;\n sb = 0;\n }\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < n {\n let v: i64 = it.next().unwrap().parse().unwrap();\n a.push(v);\n idx = idx + 1;\n }\n let m: usize = it.next().unwrap().parse().unwrap();\n let mut b: Vec = Vec::new();\n idx = 0usize;\n while idx < m {\n let v: i64 = it.next().unwrap().parse().unwrap();\n b.push(v);\n idx = idx + 1;\n }\n let out = Solution::max_equal_block_count(a, b);\n println!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf1036D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1036D/tests/mutated_testcases.jsonl"} {"problem": "cf1118B", "family": "codeforces", "description": "# Tanya and Candies\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nTanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.\n\nShe plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, **exactly one candy per day**.\n\nYour task is to find the number of such candies $$$i$$$ (let's call these candies **good**) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.\n\nFor example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad:\n - Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \\ne 3$$$ this case shouldn't be counted to the answer (this candy isn't **good**). \n- Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \\ne 3$$$ this case shouldn't be counted to the answer (this candy isn't **good**). \n- Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case **should be counted** to the answer (this candy is **good**). \n- Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case **should be counted** to the answer (this candy is **good**). \n\nIn total there $$$2$$$ cases which should counted (these candies are **good**), so the answer is $$$2$$$.\n\n## Input\n\nThe first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of candies.\n\nThe second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy.\n\n## Output\n\nPrint one integer \u2014 the number of such candies $$$i$$$ (**good** candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n7\n5 5 4 5 5 5 6\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n8\n4 8 8 7 8 4 4 5\n```\n**Output:**\n```\n2\n```\n\n### Example 3\n\n**Input:**\n```\n9\n2 3 4 2 2 3 2 2 4\n```\n**Output:**\n```\n3\n```\n\n## Note\n\nIn the first example indices of **good** candies are $$$[1, 2]$$$.\n\nIn the second example indices of **good** candies are $$$[2, 3]$$$.\n\nIn the third example indices of **good** candies are $$$[4, 5, 9]$$$.\n\n## Starter Code\n\n```rust\nuse std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_equal_sums(a: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let _n: usize = input.trim().parse().unwrap();\n\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let a: Vec = input\n .trim()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n println!(\"{}\", Solution::count_equal_sums(a));\n}\n```\n", "tags": "Rating: 1200\nimplementation\nSolved by: 13127 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn odd_sum_before(nums: Seq, end_pos: int) -> int\n recommends\n 0 <= end_pos <= nums.len(),\n decreases end_pos,\n {\n if end_pos <= 0 {\n 0\n } else {\n Self::odd_sum_before(nums, end_pos - 1) + if end_pos % 2 == 1 {\n nums[end_pos - 1] as int\n } else {\n 0\n }\n }\n }\n\n pub open spec fn even_sum_before(nums: Seq, end_pos: int) -> int\n recommends\n 0 <= end_pos <= nums.len(),\n decreases end_pos,\n {\n if end_pos <= 0 {\n 0\n } else {\n Self::even_sum_before(nums, end_pos - 1) + if end_pos % 2 == 0 {\n nums[end_pos - 1] as int\n } else {\n 0\n }\n }\n }\n\n pub open spec fn odd_sum_after(nums: Seq, start_idx: int) -> int\n recommends\n 0 <= start_idx <= nums.len(),\n decreases nums.len() as int - start_idx,\n {\n if start_idx >= nums.len() as int {\n 0\n } else {\n let new_pos = start_idx;\n (if new_pos % 2 == 1 { nums[start_idx] as int } else { 0 })\n + Self::odd_sum_after(nums, start_idx + 1)\n }\n }\n\n pub open spec fn even_sum_after(nums: Seq, start_idx: int) -> int\n recommends\n 0 <= start_idx <= nums.len(),\n decreases nums.len() as int - start_idx,\n {\n if start_idx >= nums.len() as int {\n 0\n } else {\n let new_pos = start_idx;\n (if new_pos % 2 == 0 { nums[start_idx] as int } else { 0 })\n + Self::even_sum_after(nums, start_idx + 1)\n }\n }\n\n pub open spec fn odd_day_sum_after_removal(nums: Seq, remove_pos: int) -> int\n recommends\n 1 <= remove_pos <= nums.len(),\n {\n let remove_idx = remove_pos - 1;\n Self::odd_sum_before(nums, remove_idx) + Self::odd_sum_after(nums, remove_pos)\n }\n\n pub open spec fn even_day_sum_after_removal(nums: Seq, remove_pos: int) -> int\n recommends\n 1 <= remove_pos <= nums.len(),\n {\n let remove_idx = remove_pos - 1;\n Self::even_sum_before(nums, remove_idx) + Self::even_sum_after(nums, remove_pos)\n }\n\n pub open spec fn is_good_candy(nums: Seq, i: int) -> bool\n recommends\n 1 <= i <= nums.len(),\n {\n Self::odd_day_sum_after_removal(nums, i) == Self::even_day_sum_after_removal(nums, i)\n }\n\n pub open spec fn count_good_candies_upto(nums: Seq, i_end: int) -> nat\n recommends\n 0 <= i_end <= nums.len(),\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n Self::count_good_candies_upto(nums, i_end - 1) + if Self::is_good_candy(nums, i_end) {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_good_candies(nums: Seq) -> nat {\n Self::count_good_candies_upto(nums, nums.len() as int)\n }\n\n pub fn count_equal_sums(a: Vec) -> (result: i32)\n requires\n 1 <= a.len() <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 10_000,\n ensures\n result as int == Self::count_good_candies(a@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_equal_sums(a: Vec) -> i32 {\n let n = a.len();\n let mut total_even_idx = 0i64;\n let mut total_odd_idx = 0i64;\n let mut t = 0;\n while t < n {\n if t % 2 == 0 {\n total_even_idx = total_even_idx + a[t];\n } else {\n total_odd_idx = total_odd_idx + a[t];\n }\n t = t + 1;\n }\n let mut po = 0i64;\n let mut pe = 0i64;\n let mut count = 0i32;\n let mut i = 0;\n while i < n {\n let odd_tail = total_odd_idx - pe - if i % 2 == 1 { a[i] } else { 0 };\n let even_tail = total_even_idx - po - if i % 2 == 0 { a[i] } else { 0 };\n let odd_sum = po + odd_tail;\n let even_sum = pe + even_tail;\n if odd_sum == even_sum {\n count = count + 1;\n }\n if i % 2 == 0 {\n po = po + a[i];\n } else {\n pe = pe + a[i];\n }\n i = i + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn odd_sum_before(nums: Seq, end_pos: int) -> int\n recommends\n 0 <= end_pos <= nums.len(),\n decreases end_pos,\n {\n if end_pos <= 0 {\n 0\n } else {\n Self::odd_sum_before(nums, end_pos - 1) + if end_pos % 2 == 1 {\n nums[end_pos - 1] as int\n } else {\n 0\n }\n }\n }\n\n pub open spec fn even_sum_before(nums: Seq, end_pos: int) -> int\n recommends\n 0 <= end_pos <= nums.len(),\n decreases end_pos,\n {\n if end_pos <= 0 {\n 0\n } else {\n Self::even_sum_before(nums, end_pos - 1) + if end_pos % 2 == 0 {\n nums[end_pos - 1] as int\n } else {\n 0\n }\n }\n }\n\n pub open spec fn odd_sum_after(nums: Seq, start_idx: int) -> int\n recommends\n 0 <= start_idx <= nums.len(),\n decreases nums.len() as int - start_idx,\n {\n if start_idx >= nums.len() as int {\n 0\n } else {\n let new_pos = start_idx;\n (if new_pos % 2 == 1 { nums[start_idx] as int } else { 0 })\n + Self::odd_sum_after(nums, start_idx + 1)\n }\n }\n\n pub open spec fn even_sum_after(nums: Seq, start_idx: int) -> int\n recommends\n 0 <= start_idx <= nums.len(),\n decreases nums.len() as int - start_idx,\n {\n if start_idx >= nums.len() as int {\n 0\n } else {\n let new_pos = start_idx;\n (if new_pos % 2 == 0 { nums[start_idx] as int } else { 0 })\n + Self::even_sum_after(nums, start_idx + 1)\n }\n }\n\n pub open spec fn odd_day_sum_after_removal(nums: Seq, remove_pos: int) -> int\n recommends\n 1 <= remove_pos <= nums.len(),\n {\n let remove_idx = remove_pos - 1;\n Self::odd_sum_before(nums, remove_idx) + Self::odd_sum_after(nums, remove_pos)\n }\n\n pub open spec fn even_day_sum_after_removal(nums: Seq, remove_pos: int) -> int\n recommends\n 1 <= remove_pos <= nums.len(),\n {\n let remove_idx = remove_pos - 1;\n Self::even_sum_before(nums, remove_idx) + Self::even_sum_after(nums, remove_pos)\n }\n\n pub open spec fn is_good_candy(nums: Seq, i: int) -> bool\n recommends\n 1 <= i <= nums.len(),\n {\n Self::odd_day_sum_after_removal(nums, i) == Self::even_day_sum_after_removal(nums, i)\n }\n\n pub open spec fn count_good_candies_upto(nums: Seq, i_end: int) -> nat\n recommends\n 0 <= i_end <= nums.len(),\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n Self::count_good_candies_upto(nums, i_end - 1) + if Self::is_good_candy(nums, i_end) {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_good_candies(nums: Seq) -> nat {\n Self::count_good_candies_upto(nums, nums.len() as int)\n }\n\n pub open spec fn sum_even_indices(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let last = end - 1;\n Self::sum_even_indices(nums, last) + if last % 2 == 0 { nums[last] as int } else { 0 }\n }\n }\n\n pub open spec fn sum_odd_indices(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let last = end - 1;\n Self::sum_odd_indices(nums, last) + if last % 2 == 1 { nums[last] as int } else { 0 }\n }\n }\n\n pub fn count_equal_sums(a: Vec) -> (result: i32)\n requires\n 1 <= a.len() <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 10_000,\n ensures\n result as int == Self::count_good_candies(a@),\n {\n let n = a.len();\n let mut total_even_idx = 0i64;\n let mut total_odd_idx = 0i64;\n let mut t = 0;\n while t < n\n decreases n - t,\n {\n if t % 2 == 0 {\n total_even_idx = total_even_idx + a[t];\n } else {\n total_odd_idx = total_odd_idx + a[t];\n }\n t = t + 1;\n }\n let mut po = 0i64;\n let mut pe = 0i64;\n let mut count = 0i32;\n let mut i = 0;\n while i < n\n decreases n - i,\n {\n let odd_tail = total_odd_idx - pe - if i % 2 == 1 { a[i] } else { 0 };\n let even_tail = total_even_idx - po - if i % 2 == 0 { a[i] } else { 0 };\n let odd_sum = po + odd_tail;\n let even_sum = pe + even_tail;\n if odd_sum == even_sum {\n count = count + 1;\n }\n if i % 2 == 0 {\n po = po + a[i];\n } else {\n pe = pe + a[i];\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn odd_sum_before(nums: Seq, end_pos: int) -> int\n recommends\n 0 <= end_pos <= nums.len(),\n decreases end_pos,\n {\n if end_pos <= 0 {\n 0\n } else {\n Self::odd_sum_before(nums, end_pos - 1) + if end_pos % 2 == 1 {\n nums[end_pos - 1] as int\n } else {\n 0\n }\n }\n }\n\n pub open spec fn even_sum_before(nums: Seq, end_pos: int) -> int\n recommends\n 0 <= end_pos <= nums.len(),\n decreases end_pos,\n {\n if end_pos <= 0 {\n 0\n } else {\n Self::even_sum_before(nums, end_pos - 1) + if end_pos % 2 == 0 {\n nums[end_pos - 1] as int\n } else {\n 0\n }\n }\n }\n\n pub open spec fn odd_sum_after(nums: Seq, start_idx: int) -> int\n recommends\n 0 <= start_idx <= nums.len(),\n decreases nums.len() as int - start_idx,\n {\n if start_idx >= nums.len() as int {\n 0\n } else {\n let new_pos = start_idx;\n (if new_pos % 2 == 1 { nums[start_idx] as int } else { 0 })\n + Self::odd_sum_after(nums, start_idx + 1)\n }\n }\n\n pub open spec fn even_sum_after(nums: Seq, start_idx: int) -> int\n recommends\n 0 <= start_idx <= nums.len(),\n decreases nums.len() as int - start_idx,\n {\n if start_idx >= nums.len() as int {\n 0\n } else {\n let new_pos = start_idx;\n (if new_pos % 2 == 0 { nums[start_idx] as int } else { 0 })\n + Self::even_sum_after(nums, start_idx + 1)\n }\n }\n\n pub open spec fn odd_day_sum_after_removal(nums: Seq, remove_pos: int) -> int\n recommends\n 1 <= remove_pos <= nums.len(),\n {\n let remove_idx = remove_pos - 1;\n Self::odd_sum_before(nums, remove_idx) + Self::odd_sum_after(nums, remove_pos)\n }\n\n pub open spec fn even_day_sum_after_removal(nums: Seq, remove_pos: int) -> int\n recommends\n 1 <= remove_pos <= nums.len(),\n {\n let remove_idx = remove_pos - 1;\n Self::even_sum_before(nums, remove_idx) + Self::even_sum_after(nums, remove_pos)\n }\n\n pub open spec fn is_good_candy(nums: Seq, i: int) -> bool\n recommends\n 1 <= i <= nums.len(),\n {\n Self::odd_day_sum_after_removal(nums, i) == Self::even_day_sum_after_removal(nums, i)\n }\n\n pub open spec fn count_good_candies_upto(nums: Seq, i_end: int) -> nat\n recommends\n 0 <= i_end <= nums.len(),\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n Self::count_good_candies_upto(nums, i_end - 1) + if Self::is_good_candy(nums, i_end) {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_good_candies(nums: Seq) -> nat {\n Self::count_good_candies_upto(nums, nums.len() as int)\n }\n\n pub open spec fn sum_even_indices(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let last = end - 1;\n Self::sum_even_indices(nums, last) + if last % 2 == 0 { nums[last] as int } else { 0 }\n }\n }\n\n pub open spec fn sum_odd_indices(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let last = end - 1;\n Self::sum_odd_indices(nums, last) + if last % 2 == 1 { nums[last] as int } else { 0 }\n }\n }\n\n proof fn lemma_odd_before_eq_sum_even(nums: Seq, end_pos: int)\n requires\n 0 <= end_pos <= nums.len(),\n ensures\n Self::odd_sum_before(nums, end_pos) == Self::sum_even_indices(nums, end_pos),\n decreases end_pos,\n {\n if end_pos <= 0 {\n reveal_with_fuel(Solution::odd_sum_before, 2);\n reveal_with_fuel(Solution::sum_even_indices, 2);\n } else {\n Self::lemma_odd_before_eq_sum_even(nums, end_pos - 1);\n reveal_with_fuel(Solution::odd_sum_before, 2);\n reveal_with_fuel(Solution::sum_even_indices, 2);\n }\n }\n\n proof fn lemma_even_before_eq_sum_odd(nums: Seq, end_pos: int)\n requires\n 0 <= end_pos <= nums.len(),\n ensures\n Self::even_sum_before(nums, end_pos) == Self::sum_odd_indices(nums, end_pos),\n decreases end_pos,\n {\n if end_pos <= 0 {\n reveal_with_fuel(Solution::even_sum_before, 2);\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n } else {\n Self::lemma_even_before_eq_sum_odd(nums, end_pos - 1);\n reveal_with_fuel(Solution::even_sum_before, 2);\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n }\n }\n\n proof fn lemma_sum_odd_step(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n ensures\n Self::sum_odd_indices(nums, i + 1)\n == Self::sum_odd_indices(nums, i) + if i % 2 == 1 { nums[i] as int } else { 0 },\n {\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n }\n\n proof fn lemma_sum_even_step(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n ensures\n Self::sum_even_indices(nums, i + 1)\n == Self::sum_even_indices(nums, i) + if i % 2 == 0 { nums[i] as int } else { 0 },\n {\n reveal_with_fuel(Solution::sum_even_indices, 2);\n }\n\n proof fn lemma_odd_sum_after_eq_delta(nums: Seq, start: int)\n requires\n 0 <= start <= nums.len(),\n ensures\n Self::odd_sum_after(nums, start)\n == Self::sum_odd_indices(nums, nums.len() as int) - Self::sum_odd_indices(nums, start),\n decreases nums.len() as int - start,\n {\n let n = nums.len() as int;\n if start >= n {\n reveal_with_fuel(Solution::odd_sum_after, 2);\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n } else {\n Self::lemma_odd_sum_after_eq_delta(nums, start + 1);\n reveal_with_fuel(Solution::odd_sum_after, 2);\n Self::lemma_sum_odd_step(nums, start);\n }\n }\n\n proof fn lemma_even_sum_after_eq_delta(nums: Seq, start: int)\n requires\n 0 <= start <= nums.len(),\n ensures\n Self::even_sum_after(nums, start)\n == Self::sum_even_indices(nums, nums.len() as int) - Self::sum_even_indices(nums, start),\n decreases nums.len() as int - start,\n {\n let n = nums.len() as int;\n if start >= n {\n reveal_with_fuel(Solution::even_sum_after, 2);\n reveal_with_fuel(Solution::sum_even_indices, 2);\n } else {\n Self::lemma_even_sum_after_eq_delta(nums, start + 1);\n reveal_with_fuel(Solution::even_sum_after, 2);\n Self::lemma_sum_even_step(nums, start);\n }\n }\n\n proof fn lemma_odd_tail_closed(nums: Seq, i: int, total_odd: int, pe: int)\n requires\n 0 <= i < nums.len(),\n total_odd == Self::sum_odd_indices(nums, nums.len() as int),\n pe == Self::sum_odd_indices(nums, i),\n ensures\n total_odd - pe - (if i % 2 == 1 { nums[i] as int } else { 0 })\n == Self::odd_sum_after(nums, i + 1),\n {\n Self::lemma_sum_odd_step(nums, i);\n Self::lemma_odd_sum_after_eq_delta(nums, i + 1);\n assert(Self::sum_odd_indices(nums, nums.len() as int) - Self::sum_odd_indices(nums, i)\n == (if i % 2 == 1 { nums[i] as int } else { 0 })\n + (Self::sum_odd_indices(nums, nums.len() as int) - Self::sum_odd_indices(nums, i + 1)));\n }\n\n proof fn lemma_even_tail_closed(nums: Seq, i: int, total_even: int, po: int)\n requires\n 0 <= i < nums.len(),\n total_even == Self::sum_even_indices(nums, nums.len() as int),\n po == Self::sum_even_indices(nums, i),\n ensures\n total_even - po - (if i % 2 == 0 { nums[i] as int } else { 0 })\n == Self::even_sum_after(nums, i + 1),\n {\n Self::lemma_sum_even_step(nums, i);\n Self::lemma_even_sum_after_eq_delta(nums, i + 1);\n assert(Self::sum_even_indices(nums, nums.len() as int) - Self::sum_even_indices(nums, i)\n == (if i % 2 == 0 { nums[i] as int } else { 0 })\n + (Self::sum_even_indices(nums, nums.len() as int) - Self::sum_even_indices(nums, i + 1)));\n }\n\n proof fn lemma_sum_even_indices_bounded(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n Self::sum_even_indices(nums, end) <= end * 10_000,\n decreases end,\n {\n if end > 0 {\n Self::lemma_sum_even_indices_bounded(nums, end - 1);\n reveal_with_fuel(Solution::sum_even_indices, 2);\n assert(nums[end - 1] as int <= 10_000);\n } else {\n reveal_with_fuel(Solution::sum_even_indices, 2);\n }\n }\n\n proof fn lemma_sum_odd_indices_bounded(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n Self::sum_odd_indices(nums, end) <= end * 10_000,\n decreases end,\n {\n if end > 0 {\n Self::lemma_sum_odd_indices_bounded(nums, end - 1);\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n assert(nums[end - 1] as int <= 10_000);\n } else {\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n }\n }\n\n proof fn lemma_sum_odd_prefix_le_total(nums: Seq, i: int, n: int)\n requires\n 0 <= i <= n <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n Self::sum_odd_indices(nums, i) <= Self::sum_odd_indices(nums, n),\n decreases n - i,\n {\n if i < n {\n Self::lemma_sum_odd_prefix_le_total(nums, i + 1, n);\n Self::lemma_sum_odd_step(nums, i);\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n } else {\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n }\n }\n\n proof fn lemma_sum_even_prefix_le_total(nums: Seq, i: int, n: int)\n requires\n 0 <= i <= n <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n Self::sum_even_indices(nums, i) <= Self::sum_even_indices(nums, n),\n decreases n - i,\n {\n if i < n {\n Self::lemma_sum_even_prefix_le_total(nums, i + 1, n);\n Self::lemma_sum_even_step(nums, i);\n reveal_with_fuel(Solution::sum_even_indices, 2);\n } else {\n reveal_with_fuel(Solution::sum_even_indices, 2);\n }\n }\n\n proof fn lemma_sum_odd_indices_nonneg(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n Self::sum_odd_indices(nums, end) >= 0,\n decreases end,\n {\n if end > 0 {\n Self::lemma_sum_odd_indices_nonneg(nums, end - 1);\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n } else {\n reveal_with_fuel(Solution::sum_odd_indices, 2);\n }\n }\n\n proof fn lemma_sum_even_indices_nonneg(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n Self::sum_even_indices(nums, end) >= 0,\n decreases end,\n {\n if end > 0 {\n Self::lemma_sum_even_indices_nonneg(nums, end - 1);\n reveal_with_fuel(Solution::sum_even_indices, 2);\n } else {\n reveal_with_fuel(Solution::sum_even_indices, 2);\n }\n }\n\n pub fn count_equal_sums(a: Vec) -> (result: i32)\n requires\n 1 <= a.len() <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 10_000,\n ensures\n result as int == Self::count_good_candies(a@),\n {\n let n = a.len();\n proof {\n assert(n == a@.len());\n }\n let mut total_even_idx = 0i64;\n let mut total_odd_idx = 0i64;\n let mut t = 0;\n while t < n\n invariant\n t <= n,\n n == a.len(),\n 1 <= n <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 10_000,\n total_even_idx as int == Self::sum_even_indices(a@, t as int),\n total_odd_idx as int == Self::sum_odd_indices(a@, t as int),\n -3_000_000_000i64 <= total_even_idx <= 3_000_000_000i64,\n -3_000_000_000i64 <= total_odd_idx <= 3_000_000_000i64,\n decreases n - t,\n {\n proof {\n assert((t as int) < a@.len());\n Self::lemma_sum_even_indices_bounded(a@, t as int);\n Self::lemma_sum_odd_indices_bounded(a@, t as int);\n assert((total_even_idx as int) + 10000 <= 3_000_000_000);\n assert((total_odd_idx as int) + 10000 <= 3_000_000_000);\n }\n if t % 2 == 0 {\n total_even_idx = total_even_idx + a[t];\n } else {\n total_odd_idx = total_odd_idx + a[t];\n }\n proof {\n Self::lemma_sum_even_step(a@, t as int);\n Self::lemma_sum_odd_step(a@, t as int);\n reveal_with_fuel(Solution::sum_even_indices, 10);\n reveal_with_fuel(Solution::sum_odd_indices, 10);\n assert(total_even_idx as int == Self::sum_even_indices(a@, (t + 1) as int));\n assert(total_odd_idx as int == Self::sum_odd_indices(a@, (t + 1) as int));\n }\n t = t + 1;\n }\n let mut po = 0i64;\n let mut pe = 0i64;\n let mut count = 0i32;\n let mut i = 0;\n while i < n\n invariant\n i <= n,\n n == a.len(),\n 1 <= n <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 10_000,\n po as int == Self::sum_even_indices(a@, i as int),\n pe as int == Self::sum_odd_indices(a@, i as int),\n total_even_idx as int == Self::sum_even_indices(a@, n as int),\n total_odd_idx as int == Self::sum_odd_indices(a@, n as int),\n count as int == Self::count_good_candies_upto(a@, i as int),\n count as int <= i as int + 1,\n -3_000_000_000i64 <= total_even_idx <= 3_000_000_000i64,\n -3_000_000_000i64 <= total_odd_idx <= 3_000_000_000i64,\n decreases n - i,\n {\n proof {\n assert((i as int) < (a@.len() as int));\n Self::lemma_sum_odd_prefix_le_total(a@, i as int, a@.len() as int);\n Self::lemma_sum_even_prefix_le_total(a@, i as int, a@.len() as int);\n assert(pe as int <= total_odd_idx as int);\n assert(po as int <= total_even_idx as int);\n Self::lemma_sum_odd_indices_bounded(a@, a@.len() as int);\n Self::lemma_sum_even_indices_bounded(a@, a@.len() as int);\n Self::lemma_sum_odd_indices_nonneg(a@, i as int);\n Self::lemma_sum_even_indices_nonneg(a@, i as int);\n assert(pe as int >= 0);\n assert(po as int >= 0);\n assert(total_odd_idx as int <= (a@.len() as int) * 10000);\n assert(total_even_idx as int <= (a@.len() as int) * 10000);\n assert((total_odd_idx as int) - (pe as int) <= total_odd_idx as int);\n assert((total_even_idx as int) - (po as int) <= total_even_idx as int);\n assert((a@.len() as int) * 10000 < 9223372036854775807);\n }\n let odd_tail = total_odd_idx - pe - if i % 2 == 1 { a[i] } else { 0 };\n let even_tail = total_even_idx - po - if i % 2 == 0 { a[i] } else { 0 };\n let odd_sum = po + odd_tail;\n let even_sum = pe + even_tail;\n proof {\n Self::lemma_odd_before_eq_sum_even(a@, i as int);\n Self::lemma_even_before_eq_sum_odd(a@, i as int);\n Self::lemma_odd_tail_closed(a@, i as int, total_odd_idx as int, pe as int);\n Self::lemma_even_tail_closed(a@, i as int, total_even_idx as int, po as int);\n assert(odd_sum as int == Self::odd_day_sum_after_removal(a@, (i + 1) as int));\n assert(even_sum as int == Self::even_day_sum_after_removal(a@, (i + 1) as int));\n }\n if odd_sum == even_sum {\n proof {\n assert((count as int) + 1 <= 300_000);\n }\n count = count + 1;\n }\n proof {\n reveal_with_fuel(Solution::count_good_candies_upto, 2);\n }\n if i % 2 == 0 {\n po = po + a[i];\n } else {\n pe = pe + a[i];\n }\n proof {\n Self::lemma_sum_even_step(a@, i as int);\n Self::lemma_sum_odd_step(a@, i as int);\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "main": "use std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_equal_sums(a: Vec) -> i32 {\n let n = a.len();\n let mut total_even_idx = 0i64;\n let mut total_odd_idx = 0i64;\n let mut t = 0;\n while t < n {\n if t % 2 == 0 {\n total_even_idx = total_even_idx + a[t];\n } else {\n total_odd_idx = total_odd_idx + a[t];\n }\n t = t + 1;\n }\n let mut po = 0i64;\n let mut pe = 0i64;\n let mut count = 0i32;\n let mut i = 0;\n while i < n {\n let odd_tail = total_odd_idx - pe - if i % 2 == 1 { a[i] } else { 0 };\n let even_tail = total_even_idx - po - if i % 2 == 0 { a[i] } else { 0 };\n let odd_sum = po + odd_tail;\n let even_sum = pe + even_tail;\n if odd_sum == even_sum {\n count = count + 1;\n }\n if i % 2 == 0 {\n po = po + a[i];\n } else {\n pe = pe + a[i];\n }\n i = i + 1;\n }\n count\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let _n: usize = input.trim().parse().unwrap();\n\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let a: Vec = input\n .trim()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n println!(\"{}\", Solution::count_equal_sums(a));\n}\n", "checker": null, "testcases_path": "codeforces/cf1118B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1118B/tests/mutated_testcases.jsonl"} {"problem": "cf1141B", "family": "codeforces", "description": "# Maximal Continuous Rest\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nEach day in Berland consists of $$$n$$$ hours. Polycarp likes time management. That's why he has a fixed schedule for each day \u2014 it is a sequence $$$a_1, a_2, \\dots, a_n$$$ (each $$$a_i$$$ is either $$$0$$$ or $$$1$$$), where $$$a_i=0$$$ if Polycarp works during the $$$i$$$-th hour of the day and $$$a_i=1$$$ if Polycarp rests during the $$$i$$$-th hour of the day.\n\nDays go one after another endlessly and Polycarp uses the same schedule for each day.\n\nWhat is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.\n\n## Input\n\nThe first line contains $$$n$$$ ($$$1 \\le n \\le 2\\cdot10^5$$$) \u2014 number of hours per day.\n\nThe second line contains $$$n$$$ integer numbers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 1$$$), where $$$a_i=0$$$ if the $$$i$$$-th hour in a day is working and $$$a_i=1$$$ if the $$$i$$$-th hour is resting. It is guaranteed that $$$a_i=0$$$ for at least one $$$i$$$.\n\n## Output\n\nPrint the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n1 0 1 0 1\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n6\n0 1 0 1 1 0\n```\n**Output:**\n```\n2\n```\n\n### Example 3\n\n**Input:**\n```\n7\n1 0 1 1 1 0 1\n```\n**Output:**\n```\n3\n```\n\n### Example 4\n\n**Input:**\n```\n3\n0 0 0\n```\n**Output:**\n```\n0\n```\n\n## Note\n\nIn the first example, the maximal rest starts in last hour and goes to the first hour of the next day.\n\nIn the second example, Polycarp has maximal rest from the $$$4$$$-th to the $$$5$$$-th hour.\n\nIn the third example, Polycarp has maximal rest from the $$$3$$$-rd to the $$$5$$$-th hour.\n\nIn the fourth example, Polycarp has no rest at all.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn maximal_continuous_rest(a: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n a.push(x);\n j = j + 1;\n }\n let answer = Solution::maximal_continuous_rest(a);\n println!(\"{}\", answer);\n}\n```\n", "tags": "Rating: 900\nimplementation\nSolved by: 22887 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn circ_rest_segment(a: Seq, n: int, start: int, len: int) -> bool {\n &&& 0 <= start < n\n &&& 0 <= len\n &&& forall|k: int|\n 0 <= k < len ==> #[trigger] a[(start + k) % n] == 1\n}\n\npub open spec fn rest_run_len_ending_at(a: Seq, n: int, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= 2 * n {\n 0\n } else if a[i % n] != 1 {\n 0\n } else if i == 0 {\n 1\n } else if a[(i - 1) % n] == 1 {\n rest_run_len_ending_at(a, n, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_rest_len_upto(a: Seq, n: int, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = rest_run_len_ending_at(a, n, hi);\n let prev = max_rest_len_upto(a, n, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn maximal_continuous_rest(a: Vec) -> (res: i32)\n requires\n 1 <= a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] == 0 || a[i] == 1,\n exists|i: int| 0 <= i < a.len() && #[trigger] a[i] == 0,\n ensures\n 0 <= (res as int) && (res as int) <= a.len() as int,\n (res as int) == max_rest_len_upto(a@, a.len() as int, 2 * (a.len() as int) - 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn maximal_continuous_rest(a: Vec) -> i32 {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n let total: usize = 2 * n;\n while i < total {\n let idx: usize = i % n;\n if a[idx] == 1 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn circ_rest_segment(a: Seq, n: int, start: int, len: int) -> bool {\n &&& 0 <= start < n\n &&& 0 <= len\n &&& forall|k: int|\n 0 <= k < len ==> #[trigger] a[(start + k) % n] == 1\n}\n\npub open spec fn rest_run_len_ending_at(a: Seq, n: int, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= 2 * n {\n 0\n } else if a[i % n] != 1 {\n 0\n } else if i == 0 {\n 1\n } else if a[(i - 1) % n] == 1 {\n rest_run_len_ending_at(a, n, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_rest_len_upto(a: Seq, n: int, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = rest_run_len_ending_at(a, n, hi);\n let prev = max_rest_len_upto(a, n, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn maximal_continuous_rest(a: Vec) -> (res: i32)\n requires\n 1 <= a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] == 0 || a[i] == 1,\n exists|i: int| 0 <= i < a.len() && #[trigger] a[i] == 0,\n ensures\n 0 <= (res as int) && (res as int) <= a.len() as int,\n (res as int) == max_rest_len_upto(a@, a.len() as int, 2 * (a.len() as int) - 1),\n {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n let total: usize = 2 * n;\n while i < total {\n let idx: usize = i % n;\n if a[idx] == 1 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn circ_rest_segment(a: Seq, n: int, start: int, len: int) -> bool {\n &&& 0 <= start < n\n &&& 0 <= len\n &&& forall|k: int|\n 0 <= k < len ==> #[trigger] a[(start + k) % n] == 1\n}\n\npub open spec fn rest_run_len_ending_at(a: Seq, n: int, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= 2 * n {\n 0\n } else if a[i % n] != 1 {\n 0\n } else if i == 0 {\n 1\n } else if a[(i - 1) % n] == 1 {\n rest_run_len_ending_at(a, n, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_rest_len_upto(a: Seq, n: int, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = rest_run_len_ending_at(a, n, hi);\n let prev = max_rest_len_upto(a, n, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nproof fn lemma_max_upto_ge_rest_len(a: Seq, n: int, hi: int, j: int)\n requires\n 0 <= j <= hi,\n ensures\n rest_run_len_ending_at(a, n, j) <= max_rest_len_upto(a, n, hi),\n decreases hi - j,\n{\n if j == hi {\n assert(max_rest_len_upto(a, n, hi) >= rest_run_len_ending_at(a, n, hi));\n } else {\n lemma_max_upto_ge_rest_len(a, n, hi - 1, j);\n assert(max_rest_len_upto(a, n, hi) >= max_rest_len_upto(a, n, hi - 1));\n }\n}\n\nproof fn lemma_max_rest_len_upto_nonneg(a: Seq, n: int, hi: int)\n requires\n hi >= -1,\n ensures\n max_rest_len_upto(a, n, hi) >= 0,\n decreases hi + 1,\n{\n if hi < 0 {\n assert(max_rest_len_upto(a, n, hi) == 0);\n } else {\n lemma_max_rest_len_upto_nonneg(a, n, hi - 1);\n assert(max_rest_len_upto(a, n, hi) >= 0);\n }\n}\n\nproof fn lemma_start_plus_k0_mod_n(\n start: int,\n z: int,\n n: int,\n) requires\n 1 <= n,\n 0 <= start < n,\n 0 <= z < n,\nensures\n ({\n let k0 = (z - start + n) % n;\n &&& 0 <= k0 < n\n &&& (start + k0) % n == z\n }),\n{\n let k0 = (z - start + n) % n;\n if z >= start {\n let diff = z - start;\n assert(0 <= diff < n);\n assert(z - start + n == diff + n);\n assert(k0 == (diff + n) % n);\n lemma_mod_add_multiples_vanish(diff, n);\n assert((diff + n) % n == diff % n);\n lemma_small_mod(diff as nat, n as nat);\n assert(diff % n == diff);\n assert(k0 == diff);\n assert(start + k0 == z);\n lemma_small_mod(z as nat, n as nat);\n assert((start + k0) % n == z);\n } else {\n assert(z < start);\n let t = z - start + n;\n assert(1 <= t);\n assert(t < n);\n assert(k0 == t % n);\n lemma_small_mod(t as nat, n as nat);\n assert(k0 == t);\n assert(start + k0 == z + n);\n assert((start + k0) % n == (z + n) % n);\n lemma_mod_add_multiples_vanish(z, n);\n assert((z + n) % n == z % n);\n lemma_small_mod(z as nat, n as nat);\n assert((start + k0) % n == z);\n }\n}\n\nproof fn lemma_circ_len_le_nm1(\n a: Seq,\n n: int,\n start: int,\n len: int,\n) requires\n 1 <= n,\n a.len() == n,\n circ_rest_segment(a, n, start, len),\n exists|z: int| 0 <= z < n && #[trigger] a[z] == 0,\nensures\n len <= n - 1,\n{\n if len >= n {\n assert(forall|k: int| 0 <= k < len ==> #[trigger] a[(start + k) % n] == 1);\n assert(forall|k: int| 0 <= k < n ==> #[trigger] a[(start + k) % n] == 1);\n assert forall|z: int| 0 <= z < n implies a[z] == 1 by {\n lemma_start_plus_k0_mod_n(start, z, n);\n let k0 = (z - start + n) % n;\n assert(0 <= k0 < n);\n assert(k0 < len);\n assert(a[(start + k0) % n] == 1);\n assert((start + k0) % n == z);\n assert(a[z] == 1);\n };\n assert forall|z: int| 0 <= z < n && a[z] == 0 implies false by {\n assert(a[z] == 1);\n assert(a[z] == 0);\n };\n assert(false);\n }\n}\n\nproof fn lemma_rest_run_ge_1_when_a_i_1(\n a: Seq,\n n: int,\n i: int,\n) requires\n 1 <= n,\n a.len() == n,\n 0 <= i < 2 * n,\n a[i % n] == 1,\nensures\n rest_run_len_ending_at(a, n, i) >= 1,\n decreases i + 1,\n{\n reveal_with_fuel(rest_run_len_ending_at, 10);\n if i == 0 {\n assert(rest_run_len_ending_at(a, n, i) == 1);\n } else if a[(i - 1) % n] == 1 {\n lemma_rest_run_ge_1_when_a_i_1(a, n, i - 1);\n assert(rest_run_len_ending_at(a, n, i) == rest_run_len_ending_at(a, n, i - 1) + 1);\n } else {\n assert(rest_run_len_ending_at(a, n, i) == 1);\n }\n}\n\nproof fn lemma_prefix_ones_implies_rest_len(\n a: Seq,\n n: int,\n start: int,\n len: int,\n) -> (r: int)\n requires\n 1 <= n,\n a.len() == n,\n 0 <= start < 2 * n,\n 1 <= len,\n start + len <= 2 * n,\n forall|k: int| 0 <= k < len ==> #[trigger] a[(start + k) % n] == 1,\n ensures\n r == rest_run_len_ending_at(a, n, start + len - 1),\n len <= r,\n decreases len,\n{\n if len == 1 {\n assert(a[(start + 0) % n] == 1);\n assert(a[start % n] == 1);\n lemma_rest_run_ge_1_when_a_i_1(a, n, start);\n rest_run_len_ending_at(a, n, start)\n } else {\n let last = start + len - 1;\n assert(0 <= last);\n assert(last < 2 * n);\n assert(forall|k: int| 0 <= k < len ==> #[trigger] a[(start + k) % n] == 1);\n assert(a[(start + (len - 1)) % n] == 1);\n assert(forall|k: int| 0 <= k < len - 1 ==> #[trigger] a[(start + k) % n] == 1);\n let r0 = lemma_prefix_ones_implies_rest_len(a, n, start, len - 1);\n assert(r0 == rest_run_len_ending_at(a, n, start + len - 2));\n assert(len - 1 <= r0);\n reveal_with_fuel(rest_run_len_ending_at, 8);\n assert(a[(last - 1) % n] == 1);\n assert(rest_run_len_ending_at(a, n, last) == rest_run_len_ending_at(a, n, last - 1) + 1);\n assert(rest_run_len_ending_at(a, n, last) >= len);\n rest_run_len_ending_at(a, n, last)\n }\n}\n\nproof fn lemma_circ_implies_len_le_max(\n a: Seq,\n n: int,\n start: int,\n len: int,\n) requires\n 1 <= n,\n a.len() == n,\n circ_rest_segment(a, n, start, len),\n exists|z: int| 0 <= z < n && #[trigger] a[z] == 0,\nensures\n len <= max_rest_len_upto(a, n, 2 * n - 1),\n{\n if len == 0 {\n lemma_max_rest_len_upto_nonneg(a, n, 2 * n - 1);\n } else {\n lemma_circ_len_le_nm1(a, n, start, len);\n assert(start + len - 1 < 2 * n);\n let r = lemma_prefix_ones_implies_rest_len(a, n, start, len);\n assert(r == rest_run_len_ending_at(a, n, start + len - 1));\n assert(len <= r);\n lemma_max_upto_ge_rest_len(a, n, 2 * n - 1, start + len - 1);\n assert(r <= max_rest_len_upto(a, n, 2 * n - 1));\n }\n}\n\nproof fn lemma_rest_run_means_ones(\n a: Seq,\n n: int,\n i: int,\n k: int,\n)\n requires\n 1 <= n,\n a.len() == n,\n 0 <= i < 2 * n,\n 0 <= k < rest_run_len_ending_at(a, n, i),\n ensures\n a[(i - k) % n] == 1,\n decreases k,\n{\n reveal_with_fuel(rest_run_len_ending_at, 10);\n if k == 0 {\n assert(a[i % n] == 1);\n assert(a[(i - 0) % n] == 1);\n } else {\n assert(a[i % n] == 1);\n assert(i > 0);\n assert(a[(i - 1) % n] == 1);\n assert(rest_run_len_ending_at(a, n, i)\n == rest_run_len_ending_at(a, n, i - 1) + 1);\n lemma_rest_run_means_ones(a, n, i - 1, k - 1);\n assert(a[((i - 1) - (k - 1)) % n] == 1);\n assert((i - 1) - (k - 1) == i - k);\n }\n}\n\nproof fn lemma_rest_run_bounded_by_n_minus_1(\n a: Seq,\n n: int,\n i: int,\n)\n requires\n 1 <= n,\n a.len() == n,\n 0 <= i < 2 * n,\n forall|j: int| 0 <= j < n ==> #[trigger] a[j] == 0 || a[j] == 1,\n exists|z: int| 0 <= z < n && #[trigger] a[z] == 0,\n ensures\n rest_run_len_ending_at(a, n, i) <= n - 1,\n{\n let r = rest_run_len_ending_at(a, n, i);\n if r >= n {\n assert forall|z: int| 0 <= z < n implies a[z] == 1 by {\n let diff = i - z;\n if diff >= 0 && diff < n {\n let k = diff;\n assert(0 <= k && k < n && k < r);\n lemma_rest_run_means_ones(a, n, i, k);\n assert(a[(i - k) % n] == 1);\n assert(i - k == z);\n lemma_small_mod(z as nat, n as nat);\n assert((i - k) % n == z);\n } else if diff >= n {\n let k = diff - n;\n assert(0 <= k && k < n && k < r);\n lemma_rest_run_means_ones(a, n, i, k);\n assert(a[(i - k) % n] == 1);\n assert(i - k == z + n);\n lemma_mod_add_multiples_vanish(z, n);\n assert((z + n) % n == z % n);\n lemma_small_mod(z as nat, n as nat);\n assert((i - k) % n == z);\n } else {\n let k = diff + n;\n assert(0 < k && k < n && k < r);\n lemma_rest_run_means_ones(a, n, i, k);\n assert(a[(i - k) % n] == 1);\n assert(i - k == z - n);\n lemma_mod_add_multiples_vanish(z - n, n);\n assert((z - n + n) % n == (z - n) % n);\n lemma_small_mod(z as nat, n as nat);\n assert(z % n == z);\n assert((z - n) % n == z);\n assert((i - k) % n == z);\n }\n assert(a[z] == 1);\n };\n let z_wit = choose|z: int| 0 <= z < n && a[z] == 0;\n assert(a[z_wit] == 1);\n assert(false);\n }\n}\n\nproof fn lemma_max_rest_len_upto_bounded(\n a: Seq,\n n: int,\n hi: int,\n)\n requires\n 1 <= n,\n a.len() == n,\n -1 <= hi < 2 * n,\n forall|j: int| 0 <= j < n ==> #[trigger] a[j] == 0 || a[j] == 1,\n exists|z: int| 0 <= z < n && #[trigger] a[z] == 0,\n ensures\n max_rest_len_upto(a, n, hi) <= n - 1,\n decreases hi + 1,\n{\n if hi < 0 {\n assert(max_rest_len_upto(a, n, hi) == 0);\n } else {\n lemma_max_rest_len_upto_bounded(a, n, hi - 1);\n lemma_rest_run_bounded_by_n_minus_1(a, n, hi);\n reveal_with_fuel(max_rest_len_upto, 2);\n }\n}\n\nimpl Solution {\n pub fn maximal_continuous_rest(a: Vec) -> (res: i32)\n requires\n 1 <= a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] == 0 || a[i] == 1,\n exists|i: int| 0 <= i < a.len() && #[trigger] a[i] == 0,\n ensures\n 0 <= (res as int) && (res as int) <= a.len() as int,\n (res as int) == max_rest_len_upto(a@, a.len() as int, 2 * (a.len() as int) - 1),\n {\n let n = a.len();\n let ghost n_int: int = n as int;\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n let total: usize = 2 * n;\n proof {\n assert(n_int == a@.len());\n assert(2 * n_int - 1 >= 0);\n }\n while i < total\n invariant\n 1 <= n <= 200_000,\n n == a.len(),\n n_int == a@.len(),\n total == 2 * n,\n forall|j: int| 0 <= j < a.len() ==> #[trigger] a@[j] == 0 || a@[j] == 1,\n exists|j: int| 0 <= j < a.len() && #[trigger] a@[j] == 0,\n i <= total,\n 0 <= cur as int <= n_int - 1,\n cur as int == rest_run_len_ending_at(a@, n_int, i as int - 1),\n best as int == max_rest_len_upto(a@, n_int, i as int - 1),\n 0 <= best as int <= n_int - 1,\n decreases total - i,\n {\n let idx: usize = i % n;\n proof {\n assert(0 <= i as int);\n assert((i as int) < 2 * n_int);\n assert(0 <= idx as int);\n assert((idx as int) < n_int);\n assert(idx == i % n);\n assert(a@[idx as int] == a@[(i as int) % n_int]);\n }\n if a[idx] == 1 {\n proof {\n reveal_with_fuel(rest_run_len_ending_at, 10);\n if i == 0 {\n assert(cur as int == 0);\n assert(rest_run_len_ending_at(a@, n_int, -1) == 0);\n assert(rest_run_len_ending_at(a@, n_int, 0) == 1);\n } else {\n assert(cur as int == rest_run_len_ending_at(a@, n_int, (i as int) - 1));\n if a@[((i as int) - 1) % n_int] == 1 {\n assert(rest_run_len_ending_at(a@, n_int, i as int)\n == rest_run_len_ending_at(a@, n_int, (i as int) - 1) + 1);\n } else {\n assert(rest_run_len_ending_at(a@, n_int, i as int) == 1);\n assert(rest_run_len_ending_at(a@, n_int, (i as int) - 1) == 0);\n }\n }\n }\n cur = cur + 1;\n proof {\n reveal_with_fuel(rest_run_len_ending_at, 10);\n assert(cur as int == rest_run_len_ending_at(a@, n_int, i as int));\n lemma_rest_run_bounded_by_n_minus_1(a@, n_int, i as int);\n assert((cur as int) <= n_int - 1);\n }\n } else {\n proof {\n reveal_with_fuel(rest_run_len_ending_at, 10);\n assert(rest_run_len_ending_at(a@, n_int, i as int) == 0);\n }\n cur = 0;\n proof {\n assert(cur as int == rest_run_len_ending_at(a@, n_int, i as int));\n assert((cur as int) <= n_int - 1);\n }\n }\n let ghost prev_best = best;\n if cur > best {\n best = cur;\n } else {\n }\n proof {\n reveal_with_fuel(max_rest_len_upto, 10);\n assert(cur as int == rest_run_len_ending_at(a@, n_int, i as int));\n assert(max_rest_len_upto(a@, n_int, i as int)\n == if rest_run_len_ending_at(a@, n_int, i as int)\n > max_rest_len_upto(a@, n_int, i as int - 1)\n {\n rest_run_len_ending_at(a@, n_int, i as int)\n } else {\n max_rest_len_upto(a@, n_int, i as int - 1)\n });\n if cur as int > prev_best as int {\n assert(best as int == cur as int);\n } else {\n assert(best as int == prev_best as int);\n }\n }\n assert(best as int == max_rest_len_upto(a@, n_int, i as int));\n i = i + 1;\n proof {\n assert(cur as int == rest_run_len_ending_at(a@, n_int, (i as int) - 1));\n }\n }\n proof {\n assert(i == total);\n assert((i as int) == 2 * n_int);\n assert(best as int == max_rest_len_upto(a@, n_int, 2 * n_int - 1));\n lemma_max_rest_len_upto_bounded(a@, n_int, 2 * n_int - 1);\n assert(best as int <= n_int - 1);\n assert forall|start: int, len: int|\n circ_rest_segment(a@, n_int, start, len) implies len <= best as int by {\n assert forall|start: int, len: int|\n circ_rest_segment(a@, n_int, start, len) implies len <= best as int by {\n if circ_rest_segment(a@, n_int, start, len) {\n lemma_circ_implies_len_le_max(\n a@,\n n_int,\n start,\n len,\n );\n }\n };\n };\n }\n best\n }\n}\n\n}", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn maximal_continuous_rest(a: Vec) -> i32 {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n let total: usize = 2 * n;\n while i < total {\n let idx: usize = i % n;\n if a[idx] == 1 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n a.push(x);\n j = j + 1;\n }\n let answer = Solution::maximal_continuous_rest(a);\n println!(\"{}\", answer);\n}\n", "checker": null, "testcases_path": "codeforces/cf1141B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1141B/tests/mutated_testcases.jsonl"} {"problem": "cf1200C", "family": "codeforces", "description": "# Round Corridor\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nAmugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by $$$n$$$ sectors, and the outer area is equally divided by $$$m$$$ sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer area. A wall always exists at the 12 o'clock position.\n \nThe inner area's sectors are denoted as $$$(1,1), (1,2), \\dots, (1,n)$$$ in clockwise direction. The outer area's sectors are denoted as $$$(2,1), (2,2), \\dots, (2,m)$$$ in the same manner. For a clear understanding, see the example image above.\n\nAmugae wants to know if he can move from one sector to another sector. He has $$$q$$$ questions.\n\nFor each question, check if he can move between two given sectors.\n\n## Input\n\nThe first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \\le n, m \\le 10^{18}$$$, $$$1 \\le q \\le 10^4$$$)\u00a0\u2014 the number of sectors in the inner area, the number of sectors in the outer area and the number of questions.\n\nEach of the next $$$q$$$ lines contains four integers $$$s_x$$$, $$$s_y$$$, $$$e_x$$$, $$$e_y$$$ ($$$1 \\le s_x, e_x \\le 2$$$; if $$$s_x = 1$$$, then $$$1 \\le s_y \\le n$$$, otherwise $$$1 \\le s_y \\le m$$$; constraints on $$$e_y$$$ are similar). Amague wants to know if it is possible to move from sector $$$(s_x, s_y)$$$ to sector $$$(e_x, e_y)$$$.\n\n## Output\n\nFor each question, print \"`YES`\" if Amugae can move from $$$(s_x, s_y)$$$ to $$$(e_x, e_y)$$$, and \"`NO`\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n## Example\n\n**Input:**\n```\n4 6 3\n1 1 2 3\n2 6 1 2\n2 6 2 4\n```\n**Output:**\n```\nYES\nNO\nYES\n```\n\n## Note\n\nExample is shown on the picture in the statement.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn corridor_same_component(\n n: i64,\n m: i64,\n t1: i32,\n y1: i64,\n t2: i32,\n y2: i64,\n ) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: i64 = it.next().unwrap().parse().unwrap();\n let m: i64 = it.next().unwrap().parse().unwrap();\n let q: usize = it.next().unwrap().parse().unwrap();\n let mut k = 0usize;\n while k < q {\n let x1: i32 = it.next().unwrap().parse().unwrap();\n let y1: i64 = it.next().unwrap().parse().unwrap();\n let x2: i32 = it.next().unwrap().parse().unwrap();\n let y2: i64 = it.next().unwrap().parse().unwrap();\n let ok = Solution::corridor_same_component(n, m, x1, y1, x2, y2);\n if ok {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n k += 1;\n }\n}\n```\n", "tags": "Rating: 1400\nmath, number theory\nSolved by: 10540 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd_u64(a: u64, b: u64) -> u64\n decreases b,\n {\n if b == 0 {\n a\n } else {\n Self::spec_gcd_u64(b, a % b)\n }\n }\n\n pub open spec fn spec_component_id_u64(n: u64, m: u64, t: int, y: int) -> int\n recommends\n n > 0,\n m > 0,\n t == 1 || t == 2,\n {\n let g = Self::spec_gcd_u64(n, m);\n let n2 = n / g;\n let m2 = m / g;\n if t == 1 {\n (y - 1) / (n2 as int)\n } else {\n (y - 1) / (m2 as int)\n }\n }\n\n pub fn corridor_same_component(\n n: i64,\n m: i64,\n t1: i32,\n y1: i64,\n t2: i32,\n y2: i64,\n ) -> (result: bool)\n requires\n 1 <= n <= 1_000_000_000_000_000_000,\n 1 <= m <= 1_000_000_000_000_000_000,\n t1 == 1 || t1 == 2,\n t2 == 1 || t2 == 2,\n t1 == 1 ==> 1 <= y1 <= n,\n t1 == 2 ==> 1 <= y1 <= m,\n t2 == 1 ==> 1 <= y2 <= n,\n t2 == 2 ==> 1 <= y2 <= m,\n ensures\n result == (Self::spec_component_id_u64(n as u64, m as u64, t1 as int, y1 as int)\n == Self::spec_component_id_u64(n as u64, m as u64, t2 as int, y2 as int)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn corridor_same_component(\n n: i64,\n m: i64,\n t1: i32,\n y1: i64,\n t2: i32,\n y2: i64,\n ) -> bool {\n let nu = n as u64;\n let mu = m as u64;\n let mut a: u64 = nu;\n let mut b: u64 = mu;\n while b != 0 {\n let rem: u64 = a % b;\n a = b;\n b = rem;\n }\n let g = a;\n let n2 = nu / g;\n let m2 = mu / g;\n let c1 = if t1 == 1 {\n (y1 - 1) / (n2 as i64)\n } else {\n (y1 - 1) / (m2 as i64)\n };\n let c2 = if t2 == 1 {\n (y2 - 1) / (n2 as i64)\n } else {\n (y2 - 1) / (m2 as i64)\n };\n c1 == c2\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd_u64(a: u64, b: u64) -> u64\n decreases b,\n {\n if b == 0 {\n a\n } else {\n Self::spec_gcd_u64(b, a % b)\n }\n }\n\n pub open spec fn spec_component_id_u64(n: u64, m: u64, t: int, y: int) -> int\n recommends\n n > 0,\n m > 0,\n t == 1 || t == 2,\n {\n let g = Self::spec_gcd_u64(n, m);\n let n2 = n / g;\n let m2 = m / g;\n if t == 1 {\n (y - 1) / (n2 as int)\n } else {\n (y - 1) / (m2 as int)\n }\n }\n\n pub fn corridor_same_component(\n n: i64,\n m: i64,\n t1: i32,\n y1: i64,\n t2: i32,\n y2: i64,\n ) -> (result: bool)\n requires\n 1 <= n <= 1_000_000_000_000_000_000,\n 1 <= m <= 1_000_000_000_000_000_000,\n t1 == 1 || t1 == 2,\n t2 == 1 || t2 == 2,\n t1 == 1 ==> 1 <= y1 <= n,\n t1 == 2 ==> 1 <= y1 <= m,\n t2 == 1 ==> 1 <= y2 <= n,\n t2 == 2 ==> 1 <= y2 <= m,\n ensures\n result == (Self::spec_component_id_u64(n as u64, m as u64, t1 as int, y1 as int)\n == Self::spec_component_id_u64(n as u64, m as u64, t2 as int, y2 as int)),\n {\n let nu = n as u64;\n let mu = m as u64;\n let mut a: u64 = nu;\n let mut b: u64 = mu;\n while b != 0 {\n let rem: u64 = a % b;\n a = b;\n b = rem;\n }\n let g = a;\n let n2 = nu / g;\n let m2 = mu / g;\n let c1 = if t1 == 1 {\n (y1 - 1) / (n2 as i64)\n } else {\n (y1 - 1) / (m2 as i64)\n };\n let c2 = if t2 == 1 {\n (y2 - 1) / (n2 as i64)\n } else {\n (y2 - 1) / (m2 as i64)\n };\n c1 == c2\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::*;\n\nfn main() {}\n\nverus! {\n\nproof fn lemma_spec_gcd_u64_min(a: u64, b: u64)\n requires\n a >= 1,\n b >= 1,\n ensures\n Solution::spec_gcd_u64(a, b) >= 1,\n decreases b,\n{\n reveal_with_fuel(Solution::spec_gcd_u64, 2);\n assert(Solution::spec_gcd_u64(a, b) == Solution::spec_gcd_u64(b, a % b));\n let r = a % b;\n if r == 0 {\n assert(Solution::spec_gcd_u64(a, b) == Solution::spec_gcd_u64(b, 0));\n reveal_with_fuel(Solution::spec_gcd_u64, 1);\n assert(Solution::spec_gcd_u64(b, 0) == b);\n assert(b >= 1);\n } else {\n lemma_spec_gcd_u64_min(b, r);\n }\n}\n\nproof fn lemma_spec_gcd_u64_le_min(a: u64, b: u64)\n requires\n a >= 1,\n b >= 1,\n ensures\n Solution::spec_gcd_u64(a, b) <= a,\n Solution::spec_gcd_u64(a, b) <= b,\n decreases b,\n{\n reveal_with_fuel(Solution::spec_gcd_u64, 2);\n let r = a % b;\n assert(Solution::spec_gcd_u64(a, b) == Solution::spec_gcd_u64(b, r));\n if r == 0 {\n reveal_with_fuel(Solution::spec_gcd_u64, 1);\n assert(Solution::spec_gcd_u64(a, b) == b);\n assert(a % b == 0);\n if b > a {\n let ai = a as int;\n let bi = b as int;\n assert(0 <= ai && ai < bi);\n lemma_basic_div(ai, bi);\n lemma_fundamental_div_mod(ai, bi);\n assert(ai % bi == ai);\n assert(a % b == a);\n assert(a % b == 0);\n assert(a == 0);\n assert(false);\n }\n assert(b <= a);\n assert(Solution::spec_gcd_u64(a, b) <= a);\n assert(Solution::spec_gcd_u64(a, b) <= b);\n } else {\n lemma_spec_gcd_u64_le_min(b, r);\n assert(Solution::spec_gcd_u64(a, b) == Solution::spec_gcd_u64(b, r));\n assert(Solution::spec_gcd_u64(b, r) <= b);\n assert(Solution::spec_gcd_u64(b, r) <= r);\n assert(r < b);\n if a < b {\n let ai = a as int;\n let bi = b as int;\n assert(0 <= ai && ai < bi);\n lemma_basic_div(ai, bi);\n lemma_fundamental_div_mod(ai, bi);\n assert(ai % bi == ai);\n assert(r == a);\n assert(r <= a);\n } else {\n assert(b <= a);\n assert(r < b);\n assert(r <= a);\n }\n assert(Solution::spec_gcd_u64(a, b) <= a);\n assert(Solution::spec_gcd_u64(a, b) <= b);\n }\n}\n\nproof fn lemma_u64_quot_ge_one(nu: u64, g: u64)\n requires\n nu >= g,\n g >= 1,\n ensures\n nu / g >= 1,\n{\n let ni = nu as int;\n let gi = g as int;\n assert(gi <= ni);\n assert(0 < gi);\n lemma_div_is_ordered(gi, ni, gi);\n assert(gi / gi <= ni / gi);\n assert(gi / gi == 1);\n assert(nu / g >= 1);\n}\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd_u64(a: u64, b: u64) -> u64\n decreases b,\n {\n if b == 0 {\n a\n } else {\n Self::spec_gcd_u64(b, a % b)\n }\n }\n\n pub open spec fn spec_component_id_u64(n: u64, m: u64, t: int, y: int) -> int\n recommends\n n > 0,\n m > 0,\n t == 1 || t == 2,\n {\n let g = Self::spec_gcd_u64(n, m);\n let n2 = n / g;\n let m2 = m / g;\n if t == 1 {\n (y - 1) / (n2 as int)\n } else {\n (y - 1) / (m2 as int)\n }\n }\n\n pub fn corridor_same_component(\n n: i64,\n m: i64,\n t1: i32,\n y1: i64,\n t2: i32,\n y2: i64,\n ) -> (result: bool)\n requires\n 1 <= n <= 1_000_000_000_000_000_000,\n 1 <= m <= 1_000_000_000_000_000_000,\n t1 == 1 || t1 == 2,\n t2 == 1 || t2 == 2,\n t1 == 1 ==> 1 <= y1 <= n,\n t1 == 2 ==> 1 <= y1 <= m,\n t2 == 1 ==> 1 <= y2 <= n,\n t2 == 2 ==> 1 <= y2 <= m,\n ensures\n result == (Self::spec_component_id_u64(n as u64, m as u64, t1 as int, y1 as int)\n == Self::spec_component_id_u64(n as u64, m as u64, t2 as int, y2 as int)),\n {\n let nu = n as u64;\n let mu = m as u64;\n proof {\n assert(nu >= 1);\n assert(mu >= 1);\n }\n let mut a: u64 = nu;\n let mut b: u64 = mu;\n while b != 0\n invariant\n nu > 0,\n mu > 0,\n Self::spec_gcd_u64(a, b) == Self::spec_gcd_u64(nu, mu),\n decreases b,\n {\n let rem: u64 = a % b;\n proof {\n assert(Self::spec_gcd_u64(a, b) == Self::spec_gcd_u64(b, a % b));\n assert(a % b == rem);\n }\n a = b;\n b = rem;\n }\n let g = a;\n proof {\n lemma_spec_gcd_u64_min(nu, mu);\n assert(Self::spec_gcd_u64(a, b) == Self::spec_gcd_u64(nu, mu));\n assert(b == 0);\n assert(Self::spec_gcd_u64(a, 0) == a);\n assert(a == Self::spec_gcd_u64(nu, mu));\n assert(a >= 1);\n assert(g >= 1);\n lemma_spec_gcd_u64_le_min(nu, mu);\n assert(g <= nu);\n assert(g <= mu);\n lemma_u64_quot_ge_one(nu, g);\n lemma_u64_quot_ge_one(mu, g);\n }\n let n2 = nu / g;\n let m2 = mu / g;\n proof {\n assert(n2 >= 1);\n assert(m2 >= 1);\n }\n let c1 = if t1 == 1 {\n (y1 - 1) / (n2 as i64)\n } else {\n (y1 - 1) / (m2 as i64)\n };\n let c2 = if t2 == 1 {\n (y2 - 1) / (n2 as i64)\n } else {\n (y2 - 1) / (m2 as i64)\n };\n proof {\n assert(c1 as int == Self::spec_component_id_u64(nu, mu, t1 as int, y1 as int));\n assert(c2 as int == Self::spec_component_id_u64(nu, mu, t2 as int, y2 as int));\n }\n c1 == c2\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn corridor_same_component(\n n: i64,\n m: i64,\n t1: i32,\n y1: i64,\n t2: i32,\n y2: i64,\n ) -> bool {\n let nu = n as u64;\n let mu = m as u64;\n let mut a: u64 = nu;\n let mut b: u64 = mu;\n while b != 0 {\n let rem: u64 = a % b;\n a = b;\n b = rem;\n }\n let g = a;\n let n2 = nu / g;\n let m2 = mu / g;\n let c1 = if t1 == 1 {\n (y1 - 1) / (n2 as i64)\n } else {\n (y1 - 1) / (m2 as i64)\n };\n let c2 = if t2 == 1 {\n (y2 - 1) / (n2 as i64)\n } else {\n (y2 - 1) / (m2 as i64)\n };\n c1 == c2\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: i64 = it.next().unwrap().parse().unwrap();\n let m: i64 = it.next().unwrap().parse().unwrap();\n let q: usize = it.next().unwrap().parse().unwrap();\n let mut k = 0usize;\n while k < q {\n let x1: i32 = it.next().unwrap().parse().unwrap();\n let y1: i64 = it.next().unwrap().parse().unwrap();\n let x2: i32 = it.next().unwrap().parse().unwrap();\n let y2: i64 = it.next().unwrap().parse().unwrap();\n let ok = Solution::corridor_same_component(n, m, x1, y1, x2, y2);\n if ok {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n k += 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1200C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1200C/tests/mutated_testcases.jsonl"} {"problem": "cf1203C", "family": "codeforces", "description": "# Common Divisors\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou are given an array $$$a$$$ consisting of $$$n$$$ integers.\n\nYour task is to say the number of such positive integers $$$x$$$ such that $$$x$$$ divides **each** number from the array. In other words, you have to find the number of common divisors of all elements in the array.\n\nFor example, if the array $$$a$$$ will be $$$[2, 4, 6, 2, 10]$$$, then $$$1$$$ and $$$2$$$ divide each number from the array (so the answer for this test is $$$2$$$).\n\n## Input\n\nThe first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 4 \\cdot 10^5$$$) \u2014 the number of elements in $$$a$$$.\n\nThe second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^{12}$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.\n\n## Output\n\nPrint one integer \u2014 the number of such positive integers $$$x$$$ such that $$$x$$$ divides **each** number from the given array (in other words, the answer is the number of common divisors of all elements in the array).\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n1 2 3 4 5\n```\n**Output:**\n```\n1\n```\n\n### Example 2\n\n**Input:**\n```\n6\n6 90 12 18 30 18\n```\n**Output:**\n```\n4\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_common_divisors(n: usize, a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a_line = lines.next().unwrap().unwrap();\n let mut a: Vec = Vec::new();\n let mut parts = a_line.split_whitespace();\n let mut i: usize = 0;\n while i < n {\n let v: i64 = parts.next().unwrap().parse().unwrap();\n a.push(v);\n i = i + 1;\n }\n println!(\"{}\", Solution::count_common_divisors(n, a));\n}\n```\n", "tags": "Rating: 1300\nimplementation, math\nSolved by: 23835 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd_nat(a: nat, b: nat) -> nat\n decreases b,\n {\n if b == 0 {\n a\n } else {\n Self::spec_gcd_nat(b, a % b)\n }\n }\n\n pub open spec fn spec_gcd(a: int, b: int) -> int\n recommends\n 0 <= a,\n 0 <= b,\n {\n Self::spec_gcd_nat(a as nat, b as nat) as int\n }\n\n pub open spec fn spec_total_gcd_from(a: Seq, i: int, cur: int) -> int\n recommends\n 0 <= i <= a.len(),\n 0 <= cur,\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j],\n decreases a.len() - i,\n {\n if i >= a.len() {\n cur\n } else {\n Self::spec_total_gcd_from(a, i + 1, Self::spec_gcd(cur, a[i] as int))\n }\n }\n\n pub open spec fn spec_total_gcd(a: Seq) -> int\n recommends\n 1 <= a.len(),\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j],\n {\n Self::spec_total_gcd_from(a, 1, a[0] as int)\n }\n\n pub open spec fn spec_divisor_contrib(g: int, d: int) -> int\n recommends\n 1 <= g,\n 1 <= d,\n d <= g / d,\n {\n if g % d == 0 {\n if d == g / d {\n 1int\n } else {\n 2int\n }\n } else {\n 0int\n }\n }\n\n pub open spec fn spec_count_divisors_from(g: int, d: int) -> int\n recommends\n 1 <= g,\n 1 <= d,\n d <= g + 1,\n decreases g - d + 1,\n {\n if d > g {\n 0int\n } else if d > g / d {\n 0int\n } else {\n Self::spec_divisor_contrib(g, d) + Self::spec_count_divisors_from(g, d + 1)\n }\n }\n\n pub open spec fn spec_count_divisors(g: int) -> int {\n if g <= 0 {\n 0int\n } else {\n Self::spec_count_divisors_from(g, 1)\n }\n }\n\n pub fn count_common_divisors(n: usize, a: Vec) -> (res: i64)\n requires\n 1 <= n <= 400000,\n a.len() == n,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] a[i] <= 1000000000000i64,\n ensures\n res as int == Self::spec_count_divisors(Self::spec_total_gcd(a@)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_common_divisors(n: usize, a: Vec) -> i64 {\n let mut g: i64 = a[0];\n let mut i: usize = 1;\n while i < n {\n let mut x: i64 = g;\n let mut y: i64 = a[i];\n while y != 0 {\n let t: i64 = y;\n y = x % y;\n x = t;\n }\n g = x;\n i = i + 1;\n }\n let mut count: i64 = 0;\n let mut d: i64 = 1;\n while d <= g / d {\n if g % d == 0 {\n count = count + 1;\n if d != g / d {\n count = count + 1;\n }\n }\n d = d + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd_nat(a: nat, b: nat) -> nat\n decreases b,\n {\n if b == 0 {\n a\n } else {\n Self::spec_gcd_nat(b, a % b)\n }\n }\n\n pub open spec fn spec_gcd(a: int, b: int) -> int\n recommends\n 0 <= a,\n 0 <= b,\n {\n Self::spec_gcd_nat(a as nat, b as nat) as int\n }\n\n pub open spec fn spec_total_gcd_from(a: Seq, i: int, cur: int) -> int\n recommends\n 0 <= i <= a.len(),\n 0 <= cur,\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j],\n decreases a.len() - i,\n {\n if i >= a.len() {\n cur\n } else {\n Self::spec_total_gcd_from(a, i + 1, Self::spec_gcd(cur, a[i] as int))\n }\n }\n\n pub open spec fn spec_total_gcd(a: Seq) -> int\n recommends\n 1 <= a.len(),\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j],\n {\n Self::spec_total_gcd_from(a, 1, a[0] as int)\n }\n\n pub open spec fn spec_divisor_contrib(g: int, d: int) -> int\n recommends\n 1 <= g,\n 1 <= d,\n d <= g / d,\n {\n if g % d == 0 {\n if d == g / d {\n 1int\n } else {\n 2int\n }\n } else {\n 0int\n }\n }\n\n pub open spec fn spec_count_divisors_from(g: int, d: int) -> int\n recommends\n 1 <= g,\n 1 <= d,\n d <= g + 1,\n decreases g - d + 1,\n {\n if d > g {\n 0int\n } else if d > g / d {\n 0int\n } else {\n Self::spec_divisor_contrib(g, d) + Self::spec_count_divisors_from(g, d + 1)\n }\n }\n\n pub open spec fn spec_count_divisors(g: int) -> int {\n if g <= 0 {\n 0int\n } else {\n Self::spec_count_divisors_from(g, 1)\n }\n }\n\n pub fn count_common_divisors(n: usize, a: Vec) -> (res: i64)\n requires\n 1 <= n <= 400000,\n a.len() == n,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] a[i] <= 1000000000000i64,\n ensures\n res as int == Self::spec_count_divisors(Self::spec_total_gcd(a@)),\n {\n let mut g: i64 = a[0];\n let mut i: usize = 1;\n while i < n {\n let mut x: i64 = g;\n let mut y: i64 = a[i];\n while y != 0 {\n let t: i64 = y;\n y = x % y;\n x = t;\n }\n g = x;\n i = i + 1;\n }\n let mut count: i64 = 0;\n let mut d: i64 = 1;\n while d <= g / d {\n if g % d == 0 {\n count = count + 1;\n if d != g / d {\n count = count + 1;\n }\n }\n d = d + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd_nat(a: nat, b: nat) -> nat\n decreases b,\n {\n if b == 0 {\n a\n } else {\n Self::spec_gcd_nat(b, a % b)\n }\n }\n\n pub open spec fn spec_gcd(a: int, b: int) -> int\n recommends\n 0 <= a,\n 0 <= b,\n {\n Self::spec_gcd_nat(a as nat, b as nat) as int\n }\n\n pub open spec fn spec_total_gcd_from(a: Seq, i: int, cur: int) -> int\n recommends\n 0 <= i <= a.len(),\n 0 <= cur,\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j],\n decreases a.len() - i,\n {\n if i >= a.len() {\n cur\n } else {\n Self::spec_total_gcd_from(a, i + 1, Self::spec_gcd(cur, a[i] as int))\n }\n }\n\n pub open spec fn spec_total_gcd(a: Seq) -> int\n recommends\n 1 <= a.len(),\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j],\n {\n Self::spec_total_gcd_from(a, 1, a[0] as int)\n }\n\n pub open spec fn spec_divisor_contrib(g: int, d: int) -> int\n recommends\n 1 <= g,\n 1 <= d,\n d <= g / d,\n {\n if g % d == 0 {\n if d == g / d {\n 1int\n } else {\n 2int\n }\n } else {\n 0int\n }\n }\n\n pub open spec fn spec_count_divisors_from(g: int, d: int) -> int\n recommends\n 1 <= g,\n 1 <= d,\n d <= g + 1,\n decreases g - d + 1,\n {\n if d > g {\n 0int\n } else if d > g / d {\n 0int\n } else {\n Self::spec_divisor_contrib(g, d) + Self::spec_count_divisors_from(g, d + 1)\n }\n }\n\n pub open spec fn spec_count_divisors(g: int) -> int {\n if g <= 0 {\n 0int\n } else {\n Self::spec_count_divisors_from(g, 1)\n }\n }\n\n pub fn count_common_divisors(n: usize, a: Vec) -> (res: i64)\n requires\n 1 <= n <= 400000,\n a.len() == n,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] a[i] <= 1000000000000i64,\n ensures\n res as int == Self::spec_count_divisors(Self::spec_total_gcd(a@)),\n {\n let mut g: i64 = a[0];\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= n <= 400000,\n a.len() == n,\n 1 <= i <= n,\n 1 <= g <= 1000000000000i64,\n forall|j: int| 0 <= j < n ==> 1 <= #[trigger] a[j] <= 1000000000000i64,\n Self::spec_total_gcd_from(a@, i as int, g as int) == Self::spec_total_gcd(a@),\n decreases n - i,\n {\n let ghost start_g = g as int;\n let mut x: i64 = g;\n let mut y: i64 = a[i];\n while y != 0\n invariant\n 1 <= n <= 400000,\n a.len() == n,\n 1 <= i < n,\n 1 <= start_g <= 1000000000000,\n 1 <= x <= 1000000000000i64,\n 0 <= y <= 1000000000000i64,\n forall|j: int| 0 <= j < n ==> 1 <= #[trigger] a[j] <= 1000000000000i64,\n Self::spec_gcd(x as int, y as int) == Self::spec_gcd(start_g, a@[i as int] as int),\n decreases y,\n {\n let ghost prev_x = x;\n let ghost prev_y = y;\n let t: i64 = y;\n proof {\n assert(1 <= prev_x as int);\n assert(1 <= prev_y as int);\n assert(Self::spec_gcd(prev_x as int, prev_y as int) == Self::spec_gcd(prev_y as int, (prev_x % prev_y) as int));\n }\n y = x % y;\n x = t;\n proof {\n assert(x == prev_y);\n assert(y == prev_x % prev_y);\n assert(Self::spec_gcd(x as int, y as int) == Self::spec_gcd(start_g, a@[i as int] as int));\n }\n }\n proof {\n assert(y == 0);\n assert(Self::spec_gcd(x as int, 0) == x as int);\n assert(x as int == Self::spec_gcd(start_g, a@[i as int] as int));\n assert(Self::spec_total_gcd_from(a@, i as int, start_g) == Self::spec_total_gcd_from(a@, i as int + 1, Self::spec_gcd(start_g, a@[i as int] as int)));\n assert(Self::spec_total_gcd_from(a@, i as int + 1, x as int) == Self::spec_total_gcd(a@));\n }\n g = x;\n i = i + 1;\n }\n let mut count: i64 = 0;\n let mut d: i64 = 1;\n while d <= g / d\n invariant\n 1 <= n <= 400000,\n a.len() == n,\n forall|j: int| 0 <= j < n ==> 1 <= #[trigger] a[j] <= 1000000000000i64,\n 1 <= g <= 1000000000000i64,\n g as int == Self::spec_total_gcd(a@),\n 1 <= d <= g + 1,\n 0 <= count,\n count as int <= 2 * (d as int - 1),\n count as int + Self::spec_count_divisors_from(g as int, d as int) == Self::spec_count_divisors(g as int),\n decreases g + 1 - d,\n {\n if g % d == 0 {\n proof {\n assert(d <= g / d);\n assert(d as int * d as int <= g as int) by (nonlinear_arith)\n requires\n 0 <= d as int,\n d as int <= g as int / d as int,\n 1 <= g as int,\n ;\n assert(d as int * d as int <= 1000000000000) by (nonlinear_arith)\n requires\n d as int * d as int <= g as int,\n g as int <= 1000000000000,\n ;\n assert(d as int <= 1000000) by (nonlinear_arith)\n requires\n 0 <= d as int,\n d as int * d as int <= 1000000000000,\n ;\n assert(count as int <= 2 * (d as int - 1));\n assert(count as int + 2 <= 2000000) by (nonlinear_arith)\n requires\n count as int <= 2 * (d as int - 1),\n d as int <= 1000000,\n ;\n }\n count = count + 1;\n if d != g / d {\n count = count + 1;\n }\n }\n proof {\n assert(d <= g / d);\n assert(Self::spec_count_divisors_from(g as int, d as int) == Self::spec_divisor_contrib(g as int, d as int) + Self::spec_count_divisors_from(g as int, d as int + 1));\n if g % d == 0 {\n if d == g / d {\n assert(Self::spec_divisor_contrib(g as int, d as int) == 1);\n } else {\n assert(Self::spec_divisor_contrib(g as int, d as int) == 2);\n }\n } else {\n assert(Self::spec_divisor_contrib(g as int, d as int) == 0);\n }\n assert(count as int <= 2 * d as int);\n assert(count as int + Self::spec_count_divisors_from(g as int, d as int + 1) == Self::spec_count_divisors(g as int));\n }\n d = d + 1;\n }\n proof {\n assert(d > g / d);\n assert(Self::spec_count_divisors_from(g as int, d as int) == 0);\n assert(count as int == Self::spec_count_divisors(g as int));\n assert(g as int == Self::spec_total_gcd(a@));\n }\n count\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_common_divisors(n: usize, a: Vec) -> i64 {\n let mut g: i64 = a[0];\n let mut i: usize = 1;\n while i < n {\n let mut x: i64 = g;\n let mut y: i64 = a[i];\n while y != 0 {\n let t: i64 = y;\n y = x % y;\n x = t;\n }\n g = x;\n i = i + 1;\n }\n let mut count: i64 = 0;\n let mut d: i64 = 1;\n while d <= g / d {\n if g % d == 0 {\n count = count + 1;\n if d != g / d {\n count = count + 1;\n }\n }\n d = d + 1;\n }\n count\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a_line = lines.next().unwrap().unwrap();\n let mut a: Vec = Vec::new();\n let mut parts = a_line.split_whitespace();\n let mut i: usize = 0;\n while i < n {\n let v: i64 = parts.next().unwrap().parse().unwrap();\n a.push(v);\n i = i + 1;\n }\n println!(\"{}\", Solution::count_common_divisors(n, a));\n}\n", "checker": null, "testcases_path": "codeforces/cf1203C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1203C/tests/mutated_testcases.jsonl"} {"problem": "cf1343D", "family": "codeforces", "description": "# Constant Palindrome Sum\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou are given an array $$$a$$$ consisting of $$$n$$$ integers (it is guaranteed that $$$n$$$ is even, i.e. divisible by $$$2$$$). All $$$a_i$$$ does not exceed some integer $$$k$$$.\n\nYour task is to replace the **minimum** number of elements (replacement is the following operation: choose some index $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$a_i$$$ with some integer in range $$$[1; k]$$$) to satisfy the following conditions:\n - after all replacements, all $$$a_i$$$ are positive integers not greater than $$$k$$$; \n- for all $$$i$$$ from $$$1$$$ to $$$\\frac{n}{2}$$$ the following equation is true: $$$a_i + a_{n - i + 1} = x$$$, where $$$x$$$ should be **the same** for all $$$\\frac{n}{2}$$$ pairs of elements. \n\nYou have to answer $$$t$$$ independent test cases.\n\n## Input\n\nThe first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u2014 the number of test cases. Then $$$t$$$ test cases follow.\n\nThe first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 2 \\cdot 10^5, 1 \\le k \\le 2 \\cdot 10^5$$$) \u2014 the length of $$$a$$$ and the maximum possible value of some $$$a_i$$$ correspondingly. It is guratanteed that $$$n$$$ is even (i.e. divisible by $$$2$$$). The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le k$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.\n\nIt is guaranteed that the sum of $$$n$$$ (as well as the sum of $$$k$$$) over all test cases does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$, $$$\\sum k \\le 2 \\cdot 10^5$$$).\n\n## Output\n\nFor each test case, print the answer \u2014 the **minimum** number of elements you have to replace in $$$a$$$ to satisfy the conditions from the problem statement.\n\n## Example\n\n**Input:**\n```\n4\n4 2\n1 2 1 2\n4 3\n1 2 2 1\n8 7\n6 1 1 7 6 3 4 6\n6 6\n5 2 6 1 3 4\n```\n**Output:**\n```\n0\n1\n4\n2\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn constant_palindrome_sum(n: usize, k: i64, a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let nk: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let n = nk[0];\n let k = nk[1] as i64;\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let ans = Solution::constant_palindrome_sum(n, k, a);\n writeln!(out, \"{}\", ans).unwrap();\n }\n}\n```\n", "tags": "Rating: 1700\nbrute force, data structures, greedy, two pointers\nSolved by: 13487 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn pair_cost(ai: i64, aj: i64, k: i64, x: i64) -> int {\n if ai + aj == x { 0 }\n else if x >= ({let lo = if ai < aj { ai } else { aj }; lo} + 1) &&\n x <= ({let hi = if ai > aj { ai } else { aj }; hi} + k) { 1 }\n else { 2 }\n}\n\npub open spec fn total_cost_rec(a: Seq, n: usize, k: i64, x: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n total_cost_rec(a, n, k, x, end - 1)\n + pair_cost(a[end - 1], a[n as int - end], k, x)\n }\n}\n\npub open spec fn total_cost(a: Seq, n: usize, k: i64, x: i64) -> int {\n total_cost_rec(a, n, k, x, (n / 2) as int)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn constant_palindrome_sum(n: usize, k: i64, a: Vec) -> (ans: i64)\n requires\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= k,\n ensures\n 0 <= ans && ans <= n as i64,\n forall|x: i64| 2 <= x && x <= 2 * k ==>\n ans as int <= total_cost(a@, n, k, x),\n exists|x: i64| 2 <= x && x <= 2 * k && ans as int == total_cost(a@, n, k, x),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn build_tables(n: usize, k: i64, a: &Vec) -> (Vec, Vec) {\n let half = n / 2;\n let size = (2 * k + 2) as usize;\n let mut diff: Vec = Vec::new();\n let mut exact: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < size {\n diff.push(0);\n exact.push(0);\n idx = idx + 1;\n }\n let mut i: usize = 0;\n while i < half {\n let ai = a[i];\n let aj = a[n - 1 - i];\n let lo = if ai < aj { ai } else { aj };\n let hi = if ai > aj { ai } else { aj };\n let left = (lo + 1) as usize;\n let right_plus_one = (hi + k + 1) as usize;\n let sum = (ai + aj) as usize;\n diff[left] = diff[left] + 1;\n diff[right_plus_one] = diff[right_plus_one] - 1;\n exact[sum] = exact[sum] + 1;\n i = i + 1;\n }\n (diff, exact)\n }\n\n pub fn constant_palindrome_sum(n: usize, k: i64, a: Vec) -> i64 {\n let tables = Solution::build_tables(n, k, &a);\n let diff = tables.0;\n let exact = tables.1;\n let mut ans = n as i64;\n let mut cover: i64 = 0;\n let mut x: usize = 2;\n let limit = (2 * k) as usize;\n while x <= limit {\n cover = cover + diff[x];\n let cost = n as i64 - cover - exact[x];\n if cost < ans {\n ans = cost;\n }\n x = x + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn pair_cost(ai: i64, aj: i64, k: i64, x: i64) -> int {\n if ai + aj == x { 0 }\n else if x >= ({let lo = if ai < aj { ai } else { aj }; lo} + 1) &&\n x <= ({let hi = if ai > aj { ai } else { aj }; hi} + k) { 1 }\n else { 2 }\n}\n\npub open spec fn total_cost_rec(a: Seq, n: usize, k: i64, x: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n total_cost_rec(a, n, k, x, end - 1)\n + pair_cost(a[end - 1], a[n as int - end], k, x)\n }\n}\n\npub open spec fn total_cost(a: Seq, n: usize, k: i64, x: i64) -> int {\n total_cost_rec(a, n, k, x, (n / 2) as int)\n}\n\npub open spec fn b2i(b: bool) -> int {\n if b { 1 } else { 0 }\n}\n\npub open spec fn pair_lo(ai: i64, aj: i64) -> i64 {\n if ai < aj { ai } else { aj }\n}\n\npub open spec fn pair_hi(ai: i64, aj: i64) -> i64 {\n if ai > aj { ai } else { aj }\n}\n\npub open spec fn exact_count_rec(a: Seq, n: usize, x: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n exact_count_rec(a, n, x, end - 1)\n + b2i(a[end - 1] + a[n as int - end] == x)\n }\n}\n\npub open spec fn diff_delta_rec(a: Seq, n: usize, k: i64, pos: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n diff_delta_rec(a, n, k, pos, end - 1)\n + b2i(pair_lo(a[end - 1], a[n as int - end]) + 1 == pos)\n - b2i(pair_hi(a[end - 1], a[n as int - end]) + k + 1 == pos)\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n fn build_tables(n: usize, k: i64, a: &Vec) -> (res: (Vec, Vec))\n requires\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= k,\n ensures\n res.0.len() == (2 * k + 2) as usize,\n res.1.len() == (2 * k + 2) as usize,\n forall|pos: int| 0 <= pos && pos < (2 * k + 2) ==>\n res.0@[pos] as int == diff_delta_rec(a@, n, k, pos as i64, (n / 2) as int),\n forall|pos: int| 0 <= pos && pos < (2 * k + 2) ==>\n res.1@[pos] as int == exact_count_rec(a@, n, pos as i64, (n / 2) as int),\n {\n let half = n / 2;\n let size = (2 * k + 2) as usize;\n let mut diff: Vec = Vec::new();\n let mut exact: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < size {\n diff.push(0);\n exact.push(0);\n idx = idx + 1;\n }\n let mut i: usize = 0;\n while i < half {\n let ai = a[i];\n let aj = a[n - 1 - i];\n let lo = if ai < aj { ai } else { aj };\n let hi = if ai > aj { ai } else { aj };\n let left = (lo + 1) as usize;\n let right_plus_one = (hi + k + 1) as usize;\n let sum = (ai + aj) as usize;\n diff.set(left, diff[left] + 1);\n diff.set(right_plus_one, diff[right_plus_one] - 1);\n exact.set(sum, exact[sum] + 1);\n i = i + 1;\n }\n (diff, exact)\n }\n\n pub fn constant_palindrome_sum(n: usize, k: i64, a: Vec) -> (ans: i64)\n requires\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= k,\n ensures\n 0 <= ans && ans <= n as i64,\n forall|x: i64| 2 <= x && x <= 2 * k ==>\n ans as int <= total_cost(a@, n, k, x),\n exists|x: i64| 2 <= x && x <= 2 * k && ans as int == total_cost(a@, n, k, x),\n {\n let tables = Solution::build_tables(n, k, &a);\n let diff = tables.0;\n let exact = tables.1;\n let mut ans = n as i64;\n let mut cover: i64 = 0;\n let mut x: usize = 2;\n let limit = (2 * k) as usize;\n while x <= limit {\n cover = cover + diff[x];\n let cost = n as i64 - cover - exact[x];\n if cost < ans {\n ans = cost;\n }\n x = x + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn pair_cost(ai: i64, aj: i64, k: i64, x: i64) -> int {\n if ai + aj == x { 0 }\n else if x >= ({let lo = if ai < aj { ai } else { aj }; lo} + 1) &&\n x <= ({let hi = if ai > aj { ai } else { aj }; hi} + k) { 1 }\n else { 2 }\n}\n\npub open spec fn total_cost_rec(a: Seq, n: usize, k: i64, x: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n total_cost_rec(a, n, k, x, end - 1)\n + pair_cost(a[end - 1], a[n as int - end], k, x)\n }\n}\n\npub open spec fn total_cost(a: Seq, n: usize, k: i64, x: i64) -> int {\n total_cost_rec(a, n, k, x, (n / 2) as int)\n}\n\npub open spec fn b2i(b: bool) -> int {\n if b { 1 } else { 0 }\n}\n\npub open spec fn pair_lo(ai: i64, aj: i64) -> i64 {\n if ai < aj { ai } else { aj }\n}\n\npub open spec fn pair_hi(ai: i64, aj: i64) -> i64 {\n if ai > aj { ai } else { aj }\n}\n\npub open spec fn one_change(ai: i64, aj: i64, k: i64, x: i64) -> bool {\n x >= pair_lo(ai, aj) + 1 && x <= pair_hi(ai, aj) + k\n}\n\npub open spec fn cover_count_rec(a: Seq, n: usize, k: i64, x: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n cover_count_rec(a, n, k, x, end - 1)\n + b2i(one_change(a[end - 1], a[n as int - end], k, x))\n }\n}\n\npub open spec fn exact_count_rec(a: Seq, n: usize, x: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n exact_count_rec(a, n, x, end - 1)\n + b2i(a[end - 1] + a[n as int - end] == x)\n }\n}\n\npub open spec fn diff_delta_rec(a: Seq, n: usize, k: i64, pos: i64, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else {\n diff_delta_rec(a, n, k, pos, end - 1)\n + b2i(pair_lo(a[end - 1], a[n as int - end]) + 1 == pos)\n - b2i(pair_hi(a[end - 1], a[n as int - end]) + k + 1 == pos)\n }\n}\n\nproof fn lemma_pair_lo_hi_order(ai: i64, aj: i64)\n ensures\n pair_lo(ai, aj) <= pair_hi(ai, aj),\n{\n}\n\nproof fn lemma_pair_exact_implies_cover(ai: i64, aj: i64, k: i64, x: i64)\n requires\n 1 <= ai <= k,\n 1 <= aj <= k,\n 1 <= k,\n x == ai + aj,\n ensures\n one_change(ai, aj, k, x),\n{\n let lo = pair_lo(ai, aj);\n let hi = pair_hi(ai, aj);\n assert(lo <= ai);\n assert(lo <= aj);\n assert(hi >= ai);\n assert(hi >= aj);\n assert(x >= lo + 1) by(nonlinear_arith)\n requires\n x == ai + aj,\n 1 <= aj,\n lo <= ai;\n assert(x <= hi + k) by(nonlinear_arith)\n requires\n x == ai + aj,\n aj <= k,\n ai <= hi;\n assert(one_change(ai, aj, k, x));\n}\n\nproof fn lemma_pair_cost_formula(ai: i64, aj: i64, k: i64, x: i64)\n requires\n 1 <= ai <= k,\n 1 <= aj <= k,\n 1 <= k,\n 2 <= x <= 2 * k,\n ensures\n pair_cost(ai, aj, k, x)\n == 2 - b2i(one_change(ai, aj, k, x)) - b2i(ai + aj == x),\n{\n if ai + aj == x {\n lemma_pair_exact_implies_cover(ai, aj, k, x);\n assert(b2i(one_change(ai, aj, k, x)) == 1);\n assert(b2i(ai + aj == x) == 1);\n } else if one_change(ai, aj, k, x) {\n assert(b2i(one_change(ai, aj, k, x)) == 1);\n assert(b2i(ai + aj == x) == 0);\n } else {\n assert(b2i(one_change(ai, aj, k, x)) == 0);\n assert(b2i(ai + aj == x) == 0);\n }\n}\n\nproof fn lemma_total_cost_split(a: Seq, n: usize, k: i64, x: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n 1 <= k,\n forall|i: int| 0 <= i && i < n ==> 1 <= a[i] && a[i] <= k,\n 2 <= x <= 2 * k,\n ensures\n total_cost_rec(a, n, k, x, end)\n == 2 * end - cover_count_rec(a, n, k, x, end) - exact_count_rec(a, n, x, end),\n decreases end\n{\n if end > 0 {\n lemma_total_cost_split(a, n, k, x, end - 1);\n let ai = a[end - 1];\n let aj = a[n as int - end];\n lemma_pair_cost_formula(ai, aj, k, x);\n }\n}\n\nproof fn lemma_pair_cost_bounds(ai: i64, aj: i64, k: i64, x: i64)\n requires\n 1 <= ai <= k,\n 1 <= aj <= k,\n 1 <= k,\n 2 <= x <= 2 * k,\n ensures\n 0 <= pair_cost(ai, aj, k, x) <= 2,\n{\n}\n\nproof fn lemma_cover_count_bounds(a: Seq, n: usize, k: i64, x: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n 1 <= k,\n forall|i: int| 0 <= i && i < n ==> 1 <= a[i] && a[i] <= k,\n ensures\n 0 <= cover_count_rec(a, n, k, x, end) <= end,\n decreases end\n{\n if end > 0 {\n lemma_cover_count_bounds(a, n, k, x, end - 1);\n }\n}\n\nproof fn lemma_exact_count_bounds(a: Seq, n: usize, x: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n ensures\n 0 <= exact_count_rec(a, n, x, end) <= end,\n decreases end\n{\n if end > 0 {\n lemma_exact_count_bounds(a, n, x, end - 1);\n }\n}\n\nproof fn lemma_diff_delta_bounds(a: Seq, n: usize, k: i64, pos: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n 1 <= k,\n forall|i: int| 0 <= i && i < n ==> 1 <= a[i] && a[i] <= k,\n ensures\n -end <= diff_delta_rec(a, n, k, pos, end) <= end,\n decreases end\n{\n if end > 0 {\n lemma_diff_delta_bounds(a, n, k, pos, end - 1);\n }\n}\n\nproof fn lemma_total_cost_bounds(a: Seq, n: usize, k: i64, x: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n 1 <= k,\n forall|i: int| 0 <= i && i < n ==> 1 <= a[i] && a[i] <= k,\n 2 <= x <= 2 * k,\n ensures\n 0 <= total_cost_rec(a, n, k, x, end) <= 2 * end,\n decreases end\n{\n if end > 0 {\n lemma_total_cost_bounds(a, n, k, x, end - 1);\n lemma_pair_cost_bounds(a[end - 1], a[n as int - end], k, x);\n }\n}\n\nproof fn lemma_pair_cover_step(ai: i64, aj: i64, k: i64, x: i64)\n requires\n 1 <= ai <= k,\n 1 <= aj <= k,\n 1 <= k,\n 2 <= x <= 2 * k,\n ensures\n b2i(one_change(ai, aj, k, x))\n == b2i(one_change(ai, aj, k, (x - 1) as i64))\n + b2i(pair_lo(ai, aj) + 1 == x)\n - b2i(pair_hi(ai, aj) + k + 1 == x),\n{\n let l = pair_lo(ai, aj) + 1;\n let r = pair_hi(ai, aj) + k;\n let r1 = r + 1;\n lemma_pair_lo_hi_order(ai, aj);\n assert(pair_lo(ai, aj) + 1 <= pair_hi(ai, aj) + 1) by(nonlinear_arith)\n requires\n pair_lo(ai, aj) <= pair_hi(ai, aj);\n assert(pair_hi(ai, aj) + 1 <= pair_hi(ai, aj) + k) by(nonlinear_arith)\n requires\n 1 <= k;\n assert(l <= r);\n if x == l {\n assert(!one_change(ai, aj, k, (x - 1) as i64));\n assert(one_change(ai, aj, k, x));\n assert(x <= r);\n assert(x < r1) by(nonlinear_arith)\n requires\n x <= r,\n r1 == r + 1;\n assert(r1 != x);\n } else if x == r1 {\n assert(one_change(ai, aj, k, (x - 1) as i64));\n assert(!one_change(ai, aj, k, x));\n assert(l <= r);\n assert(l < x) by(nonlinear_arith)\n requires\n l <= r,\n x == r1,\n r1 == r + 1;\n assert(l != x);\n } else {\n assert((one_change(ai, aj, k, x)) == (one_change(ai, aj, k, (x - 1) as i64))) by {\n if one_change(ai, aj, k, x) {\n assert(l <= x && x <= r);\n assert(x != l);\n assert(l <= x - 1) by(nonlinear_arith)\n requires\n l <= x,\n x != l;\n assert(x - 1 <= r) by(nonlinear_arith)\n requires\n x <= r;\n assert(one_change(ai, aj, k, (x - 1) as i64));\n } else if one_change(ai, aj, k, (x - 1) as i64) {\n assert(l <= x - 1 && x - 1 <= r);\n assert(x != r1);\n assert(l <= x) by(nonlinear_arith)\n requires\n l <= x - 1;\n assert(x <= r) by(nonlinear_arith)\n requires\n x - 1 <= r,\n x != r + 1;\n assert(one_change(ai, aj, k, x));\n }\n }\n }\n}\n\nproof fn lemma_cover_step(a: Seq, n: usize, k: i64, x: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n 1 <= k,\n forall|i: int| 0 <= i && i < n ==> 1 <= a[i] && a[i] <= k,\n 2 <= x <= 2 * k,\n ensures\n cover_count_rec(a, n, k, x, end)\n == cover_count_rec(a, n, k, (x - 1) as i64, end) + diff_delta_rec(a, n, k, x, end),\n decreases end\n{\n if end > 0 {\n lemma_cover_step(a, n, k, x, end - 1);\n lemma_pair_cover_step(a[end - 1], a[n as int - end], k, x);\n }\n}\n\nproof fn lemma_cover_at_one_zero(a: Seq, n: usize, k: i64, end: int)\n requires\n 0 <= end <= (n / 2) as int,\n 1 <= k,\n forall|i: int| 0 <= i && i < n ==> 1 <= a[i] && a[i] <= k,\n ensures\n cover_count_rec(a, n, k, 1, end) == 0,\n decreases end\n{\n if end > 0 {\n lemma_cover_at_one_zero(a, n, k, end - 1);\n let ai = a[end - 1];\n let aj = a[n as int - end];\n assert(pair_lo(ai, aj) + 1 >= 2) by(nonlinear_arith)\n requires\n 1 <= ai,\n 1 <= aj;\n assert(!one_change(ai, aj, k, 1));\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n fn build_tables(n: usize, k: i64, a: &Vec) -> (res: (Vec, Vec))\n requires\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= k,\n ensures\n res.0.len() == (2 * k + 2) as usize,\n res.1.len() == (2 * k + 2) as usize,\n forall|pos: int| 0 <= pos && pos < (2 * k + 2) ==>\n res.0@[pos] as int == diff_delta_rec(a@, n, k, pos as i64, (n / 2) as int),\n forall|pos: int| 0 <= pos && pos < (2 * k + 2) ==>\n res.1@[pos] as int == exact_count_rec(a@, n, pos as i64, (n / 2) as int),\n {\n let half = n / 2;\n let size = (2 * k + 2) as usize;\n let mut diff: Vec = Vec::new();\n let mut exact: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < size\n invariant\n diff.len() == idx,\n exact.len() == idx,\n idx <= size,\n forall|j: int| 0 <= j && j < idx ==> diff@[j] == 0,\n forall|j: int| 0 <= j && j < idx ==> exact@[j] == 0,\n decreases size - idx\n {\n diff.push(0);\n exact.push(0);\n idx = idx + 1;\n }\n let mut i: usize = 0;\n while i < half\n invariant\n half == n / 2,\n size == (2 * k + 2) as usize,\n 0 <= i && i <= half,\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n diff.len() == size,\n exact.len() == size,\n forall|j: int| 0 <= j && j < n ==> 1 <= a@[j] && a@[j] <= k,\n forall|pos: int| 0 <= pos && pos < size ==> diff@[pos] as int == diff_delta_rec(a@, n, k, pos as i64, i as int),\n forall|pos: int| 0 <= pos && pos < size ==> exact@[pos] as int == exact_count_rec(a@, n, pos as i64, i as int),\n decreases half - i\n {\n let ai = a[i];\n let aj = a[n - 1 - i];\n let lo = if ai < aj { ai } else { aj };\n let hi = if ai > aj { ai } else { aj };\n let left = (lo + 1) as usize;\n let right_plus_one = (hi + k + 1) as usize;\n let sum = (ai + aj) as usize;\n proof {\n assert(1 <= ai <= k);\n assert(1 <= aj <= k);\n lemma_pair_lo_hi_order(ai, aj);\n assert(2 <= lo + 1);\n assert(lo + 1 <= k + 1);\n assert(k + 2 <= hi + k + 1);\n assert(hi + k + 1 <= 2 * k + 1);\n assert(2 <= ai + aj);\n assert(ai + aj <= 2 * k);\n assert(0 <= left < size);\n assert(0 <= right_plus_one < size);\n assert(0 <= sum < size);\n lemma_diff_delta_bounds(a@, n, k, left as i64, i as int);\n lemma_diff_delta_bounds(a@, n, k, right_plus_one as i64, i as int);\n lemma_exact_count_bounds(a@, n, sum as i64, i as int);\n assert(-(i as int) <= diff@[left as int] as int <= i as int);\n assert(-(i as int) <= diff@[right_plus_one as int] as int <= i as int);\n assert(0 <= exact@[sum as int] as int <= i as int);\n }\n let ghost diff0 = diff@;\n let ghost exact0 = exact@;\n let ghost old_left = diff[left as int];\n let ghost old_right = diff[right_plus_one as int];\n let ghost old_sum = exact[sum as int];\n diff.set(left, diff[left] + 1);\n let ghost diff1 = diff@;\n diff.set(right_plus_one, diff[right_plus_one] - 1);\n exact.set(sum, exact[sum] + 1);\n proof {\n assert(left as int <= hi as int + 1);\n assert(hi as int + 1 < hi as int + k as int + 1) by(nonlinear_arith)\n requires\n 1 <= k;\n assert((left as int) < (right_plus_one as int));\n assert(left != right_plus_one);\n assert forall|pos: int| 0 <= pos && pos < size implies diff@[pos] as int == diff_delta_rec(a@, n, k, pos as i64, (i + 1) as int) by {\n if pos == left as int {\n assert(old_left == diff0[left as int]);\n assert(diff1[pos] == old_left + 1);\n assert(diff@[pos] == diff1[pos]);\n assert(diff_delta_rec(a@, n, k, pos as i64, (i + 1) as int)\n == diff_delta_rec(a@, n, k, pos as i64, i as int)\n + 1 - 0);\n } else if pos == right_plus_one as int {\n assert(diff1[pos] == diff0[pos]);\n assert(old_right == diff1[pos]);\n assert(diff@[pos] == old_right - 1);\n assert(diff_delta_rec(a@, n, k, pos as i64, (i + 1) as int)\n == diff_delta_rec(a@, n, k, pos as i64, i as int)\n + 0 - 1);\n } else {\n assert(diff1[pos] == diff0[pos]);\n assert(diff@[pos] == diff1[pos]);\n assert(diff_delta_rec(a@, n, k, pos as i64, (i + 1) as int)\n == diff_delta_rec(a@, n, k, pos as i64, i as int));\n }\n };\n assert forall|pos: int| 0 <= pos && pos < size implies exact@[pos] as int == exact_count_rec(a@, n, pos as i64, (i + 1) as int) by {\n if pos == sum as int {\n assert(old_sum == exact0[sum as int]);\n assert(exact@[pos] == old_sum + 1);\n assert(exact_count_rec(a@, n, pos as i64, (i + 1) as int)\n == exact_count_rec(a@, n, pos as i64, i as int) + 1);\n } else {\n assert(exact@[pos] == exact0[pos]);\n assert(exact_count_rec(a@, n, pos as i64, (i + 1) as int)\n == exact_count_rec(a@, n, pos as i64, i as int));\n }\n };\n }\n i = i + 1;\n }\n (diff, exact)\n }\n\n pub fn constant_palindrome_sum(n: usize, k: i64, a: Vec) -> (ans: i64)\n requires\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= k,\n ensures\n 0 <= ans && ans <= n as i64,\n forall|x: i64| 2 <= x && x <= 2 * k ==>\n ans as int <= total_cost(a@, n, k, x),\n exists|x: i64| 2 <= x && x <= 2 * k && ans as int == total_cost(a@, n, k, x),\n {\n let tables = Solution::build_tables(n, k, &a);\n let diff = tables.0;\n let exact = tables.1;\n let mut ans = n as i64;\n let ghost mut best_x: i64 = 0;\n let mut cover: i64 = 0;\n let mut x: usize = 2;\n let limit = (2 * k) as usize;\n proof {\n lemma_cover_at_one_zero(a@, n, k, (n / 2) as int);\n assert(cover_count_rec(a@, n, k, 1, (n / 2) as int) == 0);\n }\n while x <= limit\n invariant\n limit == (2 * k) as usize,\n diff.len() == (2 * k + 2) as usize,\n exact.len() == (2 * k + 2) as usize,\n forall|pos: int| 0 <= pos && pos < (2 * k + 2) ==> diff@[pos] as int == diff_delta_rec(a@, n, k, pos as i64, (n / 2) as int),\n forall|pos: int| 0 <= pos && pos < (2 * k + 2) ==> exact@[pos] as int == exact_count_rec(a@, n, pos as i64, (n / 2) as int),\n 2 <= n && n <= 200000,\n n % 2 == 0,\n 1 <= k && k <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= k,\n 2 <= x && x <= limit + 1,\n 0 <= ans && ans <= n as i64,\n cover as int == cover_count_rec(a@, n, k, (x - 1) as i64, (n / 2) as int),\n forall|y: i64| 2 <= y && y < x as i64 ==> ans as int <= total_cost(a@, n, k, y),\n best_x == 0 ==> (x == 2 && ans == n as i64),\n best_x != 0 ==> (2 <= best_x && best_x <= 2 * k\n && ans as int == total_cost(a@, n, k, best_x)),\n decreases limit + 1 - x\n {\n proof {\n lemma_cover_step(a@, n, k, x as i64, (n / 2) as int);\n lemma_cover_count_bounds(a@, n, k, (x - 1) as i64, (n / 2) as int);\n lemma_diff_delta_bounds(a@, n, k, x as i64, (n / 2) as int);\n assert(diff@[x as int] as int == diff_delta_rec(a@, n, k, x as i64, (n / 2) as int));\n assert(-((n / 2) as int) <= diff@[x as int] as int <= (n / 2) as int);\n assert(-200000 <= cover as int + diff@[x as int] as int <= 200000) by(nonlinear_arith)\n requires\n 0 <= cover as int <= (n / 2) as int,\n -((n / 2) as int) <= diff@[x as int] as int <= (n / 2) as int,\n n <= 200000;\n assert(cover as int + diff@[x as int] as int == cover_count_rec(a@, n, k, x as i64, (n / 2) as int));\n }\n cover = cover + diff[x];\n proof {\n assert(cover as int == cover_count_rec(a@, n, k, x as i64, (n / 2) as int));\n lemma_cover_count_bounds(a@, n, k, x as i64, (n / 2) as int);\n lemma_exact_count_bounds(a@, n, x as i64, (n / 2) as int);\n assert(exact@[x as int] as int == exact_count_rec(a@, n, x as i64, (n / 2) as int));\n assert(0 <= exact@[x as int] as int <= (n / 2) as int);\n assert(0 <= n as int - cover as int - exact@[x as int] as int <= n as int) by(nonlinear_arith)\n requires\n 0 <= cover as int <= (n / 2) as int,\n 0 <= exact@[x as int] as int <= (n / 2) as int,\n n % 2 == 0;\n }\n let cost = n as i64 - cover - exact[x];\n proof {\n lemma_total_cost_split(a@, n, k, x as i64, (n / 2) as int);\n lemma_total_cost_bounds(a@, n, k, x as i64, (n / 2) as int);\n assert(cost as int == n as int - cover as int - exact@[x as int] as int);\n assert(cost as int == total_cost(a@, n, k, x as i64));\n assert(0 <= cost <= n as i64);\n }\n let ghost ans_before = ans;\n if cost < ans {\n ans = cost;\n }\n proof {\n if best_x == 0 || cost < ans_before {\n best_x = x as i64;\n }\n }\n proof {\n assert(ans as int <= total_cost(a@, n, k, x as i64));\n assert forall|y: i64| 2 <= y && y < (x + 1) as i64 implies ans as int <= total_cost(a@, n, k, y) by {\n if y < x as i64 {\n } else {\n assert(y == x as i64);\n }\n };\n }\n x = x + 1;\n }\n proof {\n assert(x == limit + 1);\n assert(limit == (2 * k) as usize);\n assert forall|y: i64| 2 <= y && y <= 2 * k implies ans as int <= total_cost(a@, n, k, y) by {\n assert(y < x as i64);\n };\n assert(limit >= 2);\n assert(best_x != 0);\n assert(2 <= best_x && best_x <= 2 * k && ans as int == total_cost(a@, n, k, best_x));\n assert(exists|xx: i64| 2 <= xx && xx <= 2 * k && ans as int == total_cost(a@, n, k, xx));\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n fn build_tables(n: usize, k: i64, a: &Vec) -> (Vec, Vec) {\n let half = n / 2;\n let size = (2 * k + 2) as usize;\n let mut diff: Vec = Vec::new();\n let mut exact: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < size {\n diff.push(0);\n exact.push(0);\n idx = idx + 1;\n }\n let mut i: usize = 0;\n while i < half {\n let ai = a[i];\n let aj = a[n - 1 - i];\n let lo = if ai < aj { ai } else { aj };\n let hi = if ai > aj { ai } else { aj };\n let left = (lo + 1) as usize;\n let right_plus_one = (hi + k + 1) as usize;\n let sum = (ai + aj) as usize;\n diff[left] = diff[left] + 1;\n diff[right_plus_one] = diff[right_plus_one] - 1;\n exact[sum] = exact[sum] + 1;\n i = i + 1;\n }\n (diff, exact)\n }\n\n pub fn constant_palindrome_sum(n: usize, k: i64, a: Vec) -> i64 {\n let tables = Solution::build_tables(n, k, &a);\n let diff = tables.0;\n let exact = tables.1;\n let mut ans = n as i64;\n let mut cover: i64 = 0;\n let mut x: usize = 2;\n let limit = (2 * k) as usize;\n while x <= limit {\n cover = cover + diff[x];\n let cost = n as i64 - cover - exact[x];\n if cost < ans {\n ans = cost;\n }\n x = x + 1;\n }\n ans\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let nk: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let n = nk[0];\n let k = nk[1] as i64;\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let ans = Solution::constant_palindrome_sum(n, k, a);\n writeln!(out, \"{}\", ans).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1343D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1343D/tests/mutated_testcases.jsonl"} {"problem": "cf1358C", "family": "codeforces", "description": "# Celex Update\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nDuring the quarantine, Sicromoft has more free time to create the new functions in \"`Celex-2021`\". The developers made a new function `GAZ-GIZ`, which infinitely fills an infinite table to the right and down from the upper left corner as follows:\n The cell with coordinates $$$(x, y)$$$ is at the intersection of $$$x$$$-th row and $$$y$$$-th column. Upper left cell $$$(1,1)$$$ contains an integer $$$1$$$.\nThe developers of the `SUM` function don't sleep either. Because of the boredom, they teamed up with the developers of the `RAND` function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell $$$(x,y)$$$ in one step you can move to the cell $$$(x+1, y)$$$ or $$$(x, y+1)$$$. \n\nAfter another Dinwows update, Levian started to study \"`Celex-2021`\" (because he wants to be an accountant!). After filling in the table with the `GAZ-GIZ` function, he asked you to calculate the quantity of possible different amounts on the path from a given cell $$$(x_1, y_1)$$$ to another given cell $$$(x_2, y_2$$$), if you can only move one cell down or right.\n\nFormally, consider all the paths from the cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$ such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths.\n\n## Input\n\nThe first line contains one integer $$$t$$$ ($$$1 \\le t \\le 57179$$$) \u2014 the number of test cases.\n\nEach of the following $$$t$$$ lines contains four natural numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \\le x_1 \\le x_2 \\le 10^9$$$, $$$1 \\le y_1 \\le y_2 \\le 10^9$$$) \u2014 coordinates of the start and the end cells.\n\n## Output\n\nFor each test case, in a separate line, print the number of possible different sums on the way from the start cell to the end cell.\n\n## Example\n\n**Input:**\n```\n4\n1 1 2 2\n1 2 2 4\n179 1 179 100000\n5 7 5 7\n```\n**Output:**\n```\n2\n3\n1\n1\n```\n\n## Note\n\nIn the first test case there are two possible sums: $$$1+2+5=8$$$ and $$$1+3+5=9$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn celex_distinct_sums(x1: i64, y1: i64, x2: i64, y2: i64) -> i64 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let mut ti = 0usize;\n while ti < t {\n let line = lines.next().unwrap().unwrap();\n let mut parts = line.split_whitespace();\n let x1: i64 = parts.next().unwrap().parse().unwrap();\n let y1: i64 = parts.next().unwrap().parse().unwrap();\n let x2: i64 = parts.next().unwrap().parse().unwrap();\n let y2: i64 = parts.next().unwrap().parse().unwrap();\n let ans = Solution::celex_distinct_sums(x1, y1, x2, y2);\n println!(\"{}\", ans);\n ti = ti + 1;\n }\n}\n```\n", "tags": "Rating: 1600\nmath\nSolved by: 18296 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_celex_distinct_sums(x1: int, y1: int, x2: int, y2: int) -> int {\n (x2 - x1) * (y2 - y1) + 1\n}\n\nimpl Solution {\n pub fn celex_distinct_sums(x1: i64, y1: i64, x2: i64, y2: i64) -> (r: i64)\n requires\n 1 <= x1 <= x2 <= 1_000_000_000,\n 1 <= y1 <= y2 <= 1_000_000_000,\n ensures\n r as int == spec_celex_distinct_sums(x1 as int, y1 as int, x2 as int, y2 as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn celex_distinct_sums(x1: i64, y1: i64, x2: i64, y2: i64) -> i64 {\n let dx = x2 - x1;\n let dy = y2 - y1;\n dx * dy + 1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_celex_distinct_sums(x1: int, y1: int, x2: int, y2: int) -> int {\n (x2 - x1) * (y2 - y1) + 1\n}\n\nimpl Solution {\n pub fn celex_distinct_sums(x1: i64, y1: i64, x2: i64, y2: i64) -> (r: i64)\n requires\n 1 <= x1 <= x2 <= 1_000_000_000,\n 1 <= y1 <= y2 <= 1_000_000_000,\n ensures\n r as int == spec_celex_distinct_sums(x1 as int, y1 as int, x2 as int, y2 as int),\n {\n let dx = x2 - x1;\n let dy = y2 - y1;\n dx * dy + 1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_celex_distinct_sums(x1: int, y1: int, x2: int, y2: int) -> int {\n (x2 - x1) * (y2 - y1) + 1\n}\n\nimpl Solution {\n pub fn celex_distinct_sums(x1: i64, y1: i64, x2: i64, y2: i64) -> (r: i64)\n requires\n 1 <= x1 <= x2 <= 1_000_000_000,\n 1 <= y1 <= y2 <= 1_000_000_000,\n ensures\n r as int == spec_celex_distinct_sums(x1 as int, y1 as int, x2 as int, y2 as int),\n {\n let dx = x2 - x1;\n let dy = y2 - y1;\n proof {\n assert(dx as int == x2 as int - x1 as int);\n assert(dy as int == y2 as int - y1 as int);\n assert(0 <= dx <= 999_999_999);\n assert(0 <= dy <= 999_999_999);\n assert((dx as int) * (dy as int) <= 999_999_999 * 999_999_999) by (nonlinear_arith)\n requires\n 0 <= dx <= 999_999_999,\n 0 <= dy <= 999_999_999;\n assert(999_999_999 * 999_999_999 < 1_000_000_000_000_000_000) by (nonlinear_arith);\n assert((dx * dy + 1) as int == (dx as int) * (dy as int) + 1);\n assert((dx as int) * (dy as int) + 1 == spec_celex_distinct_sums(\n x1 as int,\n y1 as int,\n x2 as int,\n y2 as int,\n ));\n }\n dx * dy + 1\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn celex_distinct_sums(x1: i64, y1: i64, x2: i64, y2: i64) -> i64 {\n let dx = x2 - x1;\n let dy = y2 - y1;\n dx * dy + 1\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let mut ti = 0usize;\n while ti < t {\n let line = lines.next().unwrap().unwrap();\n let mut parts = line.split_whitespace();\n let x1: i64 = parts.next().unwrap().parse().unwrap();\n let y1: i64 = parts.next().unwrap().parse().unwrap();\n let x2: i64 = parts.next().unwrap().parse().unwrap();\n let y2: i64 = parts.next().unwrap().parse().unwrap();\n let ans = Solution::celex_distinct_sums(x1, y1, x2, y2);\n println!(\"{}\", ans);\n ti = ti + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1358C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1358C/tests/mutated_testcases.jsonl"} {"problem": "cf136A", "family": "codeforces", "description": "# Presents\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nLittle Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited $n$ his friends there.\n\nIf there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from $1$ to $n$. Petya remembered that a friend number $i$ gave a gift to a friend number $p_i$. He also remembered that each of his friends received exactly one gift.\n\nNow Petya wants to know for each friend $i$ the number of a friend who has given him a gift.\n\n## Input\n\nThe first line contains one integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u2009100$) \u2014 the quantity of friends Petya invited to the party. The second line contains $n$ space-separated integers: the $i$-th number is $p_i$ \u2014 the number of a friend who gave a gift to friend number $i$. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves.\n\n## Output\n\nPrint $n$ space-separated integers: the $i$-th number should equal the number of the friend who gave a gift to friend number $i$.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4\n2 3 4 1\n```\n**Output:**\n```\n4 1 2 3\n```\n\n### Example 2\n\n**Input:**\n```\n3\n1 3 2\n```\n**Output:**\n```\n1 3 2\n```\n\n### Example 3\n\n**Input:**\n```\n2\n1 2\n```\n**Output:**\n```\n1 2\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn inverse_presents(p: Vec, n: usize) -> Vec {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let p: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let result = Solution::inverse_presents(p, n);\n let out: Vec = result.iter().map(|x| x.to_string()).collect();\n println!(\"{}\", out.join(\" \"));\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 144275 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn value_occurs(p: Seq, n: int, v: int) -> bool {\n exists|i: int| 0 <= i < n && p[i] == v\n}\n\npub open spec fn is_permutation_1_to_n(p: Seq, n: int) -> bool {\n p.len() == n\n && 1 <= n <= 100\n && forall|i: int| 0 <= i < n ==> 1 <= #[trigger] p[i] <= n\n && forall|i: int, j: int| 0 <= i < j < n ==> p[i] != p[j]\n && forall|v: int| 1 <= v <= n ==> #[trigger] value_occurs(p, n, v)\n}\n\nimpl Solution {\n pub fn inverse_presents(p: Vec, n: usize) -> (result: Vec)\n requires\n 1 <= n <= 100,\n p.len() == n,\n is_permutation_1_to_n(p@, n as int),\n ensures\n result.len() == n,\n forall|i: int| 0 <= i < n as int ==> #[trigger] result@[p@[i] as int - 1] == (i + 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn inverse_presents(p: Vec, n: usize) -> Vec {\n let mut result = Vec::new();\n let mut i = 0usize;\n while i < n {\n result.push(0i32);\n i += 1;\n }\n i = 0usize;\n while i < n {\n let idx = (p[i] as usize) - 1;\n result[idx] = (i + 1) as i32;\n i += 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn value_occurs(p: Seq, n: int, v: int) -> bool {\n exists|i: int| 0 <= i < n && p[i] == v\n}\n\npub open spec fn is_permutation_1_to_n(p: Seq, n: int) -> bool {\n p.len() == n\n && 1 <= n <= 100\n && forall|i: int| 0 <= i < n ==> 1 <= #[trigger] p[i] <= n\n && forall|i: int, j: int| 0 <= i < j < n ==> p[i] != p[j]\n && forall|v: int| 1 <= v <= n ==> #[trigger] value_occurs(p, n, v)\n}\n\nimpl Solution {\n pub fn inverse_presents(p: Vec, n: usize) -> (result: Vec)\n requires\n 1 <= n <= 100,\n p.len() == n,\n is_permutation_1_to_n(p@, n as int),\n ensures\n result.len() == n,\n forall|i: int| 0 <= i < n as int ==> #[trigger] result@[p@[i] as int - 1] == (i + 1),\n {\n let mut result = Vec::new();\n let mut i = 0usize;\n while i < n {\n result.push(0i32);\n i += 1;\n }\n i = 0usize;\n while i < n {\n let idx = (p[i] as usize) - 1;\n result.set(idx, (i + 1) as i32);\n i += 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn value_occurs(p: Seq, n: int, v: int) -> bool {\n exists|i: int| 0 <= i < n && p[i] == v\n}\n\npub open spec fn is_permutation_1_to_n(p: Seq, n: int) -> bool {\n p.len() == n\n && 1 <= n <= 100\n && forall|i: int| 0 <= i < n ==> 1 <= #[trigger] p[i] <= n\n && forall|i: int, j: int| 0 <= i < j < n ==> p[i] != p[j]\n && forall|v: int| 1 <= v <= n ==> #[trigger] value_occurs(p, n, v)\n}\n\nimpl Solution {\n pub fn inverse_presents(p: Vec, n: usize) -> (result: Vec)\n requires\n 1 <= n <= 100,\n p.len() == n,\n is_permutation_1_to_n(p@, n as int),\n ensures\n result.len() == n,\n forall|i: int| 0 <= i < n as int ==> #[trigger] result@[p@[i] as int - 1] == (i + 1),\n {\n let mut result = Vec::new();\n let mut i = 0usize;\n while i < n\n invariant\n 1 <= n <= 100,\n p.len() == n,\n is_permutation_1_to_n(p@, n as int),\n 0 <= i <= n,\n result.len() == i,\n forall|j: int| 0 <= j < i ==> result@[j] == 0,\n decreases n - i,\n {\n result.push(0i32);\n i += 1;\n }\n i = 0usize;\n while i < n\n invariant\n 1 <= n <= 100,\n p.len() == n,\n is_permutation_1_to_n(p@, n as int),\n result.len() == n,\n forall|k: int| 0 <= k < i ==> #[trigger] result@[p@[k] as int - 1] == (k + 1),\n decreases n - i,\n {\n let idx = (p[i] as usize) - 1;\n let ghost old_result = result@;\n result.set(idx, (i + 1) as i32);\n proof {\n assert(1 <= p@[i as int] <= n as int);\n assert(0 <= idx as int);\n assert((idx as int) < (n as int));\n assert(result@ == old_result.update(idx as int, (i + 1) as i32));\n assert forall|k: int| 0 <= k < i as int + 1 implies #[trigger] result@[p@[k] as int - 1] == (k + 1) by {\n if k == i as int {\n assert(p@[k] as int - 1 == idx as int);\n assert(result@[idx as int] == (i + 1) as i32);\n } else {\n assert(0 <= k < i as int);\n assert(old_result[p@[k] as int - 1] == (k + 1));\n assert(p@[k] != p@[i as int]);\n assert((p@[k] as int - 1) != (p@[i as int] as int - 1));\n assert(result@[p@[k] as int - 1] == old_result[p@[k] as int - 1]);\n }\n }\n }\n i += 1;\n }\n result\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn inverse_presents(p: Vec, n: usize) -> Vec {\n let mut result = Vec::new();\n let mut i = 0usize;\n while i < n {\n result.push(0i32);\n i += 1;\n }\n i = 0usize;\n while i < n {\n let idx = (p[i] as usize) - 1;\n result[idx] = (i + 1) as i32;\n i += 1;\n }\n result\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let p: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let result = Solution::inverse_presents(p, n);\n let out: Vec = result.iter().map(|x| x.to_string()).collect();\n println!(\"{}\", out.join(\" \"));\n}\n", "checker": null, "testcases_path": "codeforces/cf136A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf136A/tests/mutated_testcases.jsonl"} {"problem": "cf1433B", "family": "codeforces", "description": "# Yet Another Bookshelf\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nThere is a bookshelf which can fit $$$n$$$ books. The $$$i$$$-th position of bookshelf is $$$a_i = 1$$$ if there is a book on this position and $$$a_i = 0$$$ otherwise. It is guaranteed that there is **at least one book** on the bookshelf.\n\nIn one move, you can choose some contiguous segment $$$[l; r]$$$ consisting of books (i.e. for each $$$i$$$ from $$$l$$$ to $$$r$$$ the condition $$$a_i = 1$$$ holds) and:\n - Shift it to the right by $$$1$$$: move the book at index $$$i$$$ to $$$i + 1$$$ for all $$$l \\le i \\le r$$$. This move can be done only if $$$r+1 \\le n$$$ and there is no book at the position $$$r+1$$$. \n- Shift it to the left by $$$1$$$: move the book at index $$$i$$$ to $$$i-1$$$ for all $$$l \\le i \\le r$$$. This move can be done only if $$$l-1 \\ge 1$$$ and there is no book at the position $$$l-1$$$. \n\nYour task is to find the **minimum** number of moves required to collect all the books on the shelf as a **contiguous** (consecutive) segment (i.e. the segment without any gaps).\n\nFor example, for $$$a = [0, 0, 1, 0, 1]$$$ there is a gap between books ($$$a_4 = 0$$$ when $$$a_3 = 1$$$ and $$$a_5 = 1$$$), for $$$a = [1, 1, 0]$$$ there are no gaps between books and for $$$a = [0, 0,0]$$$ there are also no gaps between books.\n\nYou have to answer $$$t$$$ independent test cases.\n\n## Input\n\nThe first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 200$$$) \u2014 the number of test cases. Then $$$t$$$ test cases follow.\n\nThe first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) \u2014 the number of places on a bookshelf. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 1$$$), where $$$a_i$$$ is $$$1$$$ if there is a book at this position and $$$0$$$ otherwise. It is guaranteed that there is **at least one book** on the bookshelf.\n\n## Output\n\nFor each test case, print one integer: the **minimum** number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps).\n\n## Example\n\n**Input:**\n```\n5\n7\n0 0 1 0 1 0 1\n3\n1 0 0\n5\n1 1 0 0 1\n6\n1 0 0 0 0 1\n5\n1 1 0 1 1\n```\n**Output:**\n```\n2\n0\n2\n4\n1\n```\n\n## Note\n\nIn the first test case of the example, you can shift the segment $$$[3; 3]$$$ to the right and the segment $$$[4; 5]$$$ to the right. After all moves, the books form the contiguous segment $$$[5; 7]$$$. So the answer is $$$2$$$.\n\nIn the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already.\n\nIn the third test case of the example, you can shift the segment $$$[5; 5]$$$ to the left and then the segment $$$[4; 4]$$$ to the left again. After all moves, the books form the contiguous segment $$$[1; 3]$$$. So the answer is $$$2$$$.\n\nIn the fourth test case of the example, you can shift the segment $$$[1; 1]$$$ to the right, the segment $$$[2; 2]$$$ to the right, the segment $$$[6; 6]$$$ to the left and then the segment $$$[5; 5]$$$ to the left. After all moves, the books form the contiguous segment $$$[3; 4]$$$. So the answer is $$$4$$$.\n\nIn the fifth test case of the example, you can shift the segment $$$[1; 2]$$$ to the right. After all moves, the books form the contiguous segment $$$[2; 5]$$$. So the answer is $$$1$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_moves_books(n: usize, a: Vec) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n a.push(iter.next().unwrap().parse().unwrap());\n }\n let ans = Solution::min_moves_books(n, a);\n writeln!(out, \"{}\", ans).unwrap();\n }\n}\n```\n", "tags": "Rating: 800\ngreedy, implementation\nSolved by: 29831 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn first_one_idx(a: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n -1\n } else {\n let r = first_one_idx(a, k - 1);\n if r != -1 {\n r\n } else if a[k - 1] == 1u8 {\n k - 1\n } else {\n -1\n }\n }\n}\n\npub open spec fn last_one_idx(a: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n -1\n } else if a[k - 1] == 1u8 {\n k - 1\n } else {\n last_one_idx(a, k - 1)\n }\n}\n\npub open spec fn count_zeros_prefix(a: Seq, lo: int, k: int) -> int\n decreases k - lo,\n{\n if k <= lo {\n 0\n } else {\n let prev = count_zeros_prefix(a, lo, k - 1);\n if a[k - 1] == 0u8 { prev + 1 } else { prev }\n }\n}\n\nimpl Solution {\n pub fn min_moves_books(n: usize, a: Vec) -> (result: usize)\n requires\n 1 <= n <= 50,\n a.len() == n,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 1u8,\n exists|i: int| 0 <= i < a.len() && #[trigger] a[i] == 1u8,\n ensures\n result as int == count_zeros_prefix(a@, first_one_idx(a@, n as int), last_one_idx(a@, n as int) + 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_moves_books(n: usize, a: Vec) -> usize {\n let mut first: usize = 0;\n let mut found_first = false;\n let mut i: usize = 0;\n while i < n {\n if a[i] == 1u8 && !found_first {\n first = i;\n found_first = true;\n }\n i += 1;\n }\n let mut last: usize = 0;\n let mut found_last = false;\n let mut j: usize = 0;\n while j < n {\n if a[j] == 1u8 {\n last = j;\n found_last = true;\n }\n j += 1;\n }\n if !found_first || !found_last {\n return 0;\n }\n let mut count: usize = 0;\n let mut k: usize = first;\n while k <= last {\n if a[k] == 0u8 {\n count += 1;\n }\n k += 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn first_one_idx(a: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n -1\n } else {\n let r = first_one_idx(a, k - 1);\n if r != -1 {\n r\n } else if a[k - 1] == 1u8 {\n k - 1\n } else {\n -1\n }\n }\n}\n\npub open spec fn last_one_idx(a: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n -1\n } else if a[k - 1] == 1u8 {\n k - 1\n } else {\n last_one_idx(a, k - 1)\n }\n}\n\npub open spec fn count_zeros_prefix(a: Seq, lo: int, k: int) -> int\n decreases k - lo,\n{\n if k <= lo {\n 0\n } else {\n let prev = count_zeros_prefix(a, lo, k - 1);\n if a[k - 1] == 0u8 { prev + 1 } else { prev }\n }\n}\n\nimpl Solution {\n pub fn min_moves_books(n: usize, a: Vec) -> (result: usize)\n requires\n 1 <= n <= 50,\n a.len() == n,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 1u8,\n exists|i: int| 0 <= i < a.len() && #[trigger] a[i] == 1u8,\n ensures\n result as int == count_zeros_prefix(a@, first_one_idx(a@, n as int), last_one_idx(a@, n as int) + 1),\n {\n let mut first: usize = 0;\n let mut found_first = false;\n let mut i: usize = 0;\n while i < n {\n if a[i] == 1u8 && !found_first {\n first = i;\n found_first = true;\n }\n i += 1;\n }\n let mut last: usize = 0;\n let mut found_last = false;\n let mut j: usize = 0;\n while j < n {\n if a[j] == 1u8 {\n last = j;\n found_last = true;\n }\n j += 1;\n }\n if !found_first || !found_last {\n return 0;\n }\n let mut count: usize = 0;\n let mut k: usize = first;\n while k <= last {\n if a[k] == 0u8 {\n count += 1;\n }\n k += 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn first_one_idx(a: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n -1\n } else {\n let r = first_one_idx(a, k - 1);\n if r != -1 {\n r\n } else if a[k - 1] == 1u8 {\n k - 1\n } else {\n -1\n }\n }\n}\n\npub open spec fn last_one_idx(a: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n -1\n } else if a[k - 1] == 1u8 {\n k - 1\n } else {\n last_one_idx(a, k - 1)\n }\n}\n\npub open spec fn count_zeros_prefix(a: Seq, lo: int, k: int) -> int\n decreases k - lo,\n{\n if k <= lo {\n 0\n } else {\n let prev = count_zeros_prefix(a, lo, k - 1);\n if a[k - 1] == 0u8 { prev + 1 } else { prev }\n }\n}\n\nproof fn lemma_exists_implies_first_one(a: Seq, n: int)\n requires\n 0 <= n <= a.len(),\n exists|i: int| 0 <= i < n && #[trigger] a[i] == 1u8,\n ensures\n first_one_idx(a, n) != -1,\n 0 <= first_one_idx(a, n) < n,\n decreases n,\n{\n if n == 0 {\n assert(false);\n } else {\n let r = first_one_idx(a, n - 1);\n if r != -1 {\n lemma_first_one_idx_in_bounds(a, n - 1);\n } else if a[n - 1] == 1u8 {\n } else {\n \n assert(exists|i: int| 0 <= i < n && #[trigger] a[i] == 1u8);\n let w = choose|i: int| 0 <= i < n && #[trigger] a[i] == 1u8;\n assert(w < n - 1 || w == n - 1);\n if w == n - 1 {\n assert(a[n - 1] == 1u8);\n assert(false);\n } else {\n assert(0 <= w < n - 1);\n assert(exists|i: int| 0 <= i < n - 1 && #[trigger] a[i] == 1u8);\n lemma_exists_implies_first_one(a, n - 1);\n assert(first_one_idx(a, n - 1) != -1);\n assert(false);\n }\n }\n }\n}\n\nproof fn lemma_first_one_idx_in_bounds(a: Seq, n: int)\n requires\n 0 <= n <= a.len(),\n ensures\n first_one_idx(a, n) == -1 || (0 <= first_one_idx(a, n) < n),\n decreases n,\n{\n if n == 0 {\n } else {\n lemma_first_one_idx_in_bounds(a, n - 1);\n }\n}\n\nproof fn lemma_exists_implies_last_one(a: Seq, n: int)\n requires\n 0 <= n <= a.len(),\n exists|i: int| 0 <= i < n && #[trigger] a[i] == 1u8,\n ensures\n last_one_idx(a, n) != -1,\n 0 <= last_one_idx(a, n) < n,\n decreases n,\n{\n if n == 0 {\n assert(false);\n } else if a[n - 1] == 1u8 {\n } else {\n let w = choose|i: int| 0 <= i < n && #[trigger] a[i] == 1u8;\n if w == n - 1 {\n assert(false);\n } else {\n assert(0 <= w < n - 1);\n assert(exists|i: int| 0 <= i < n - 1 && #[trigger] a[i] == 1u8);\n lemma_exists_implies_last_one(a, n - 1);\n lemma_last_one_idx_in_bounds(a, n - 1);\n }\n }\n}\n\nproof fn lemma_last_one_idx_in_bounds(a: Seq, n: int)\n requires\n 0 <= n <= a.len(),\n ensures\n last_one_idx(a, n) == -1 || (0 <= last_one_idx(a, n) < n),\n decreases n,\n{\n if n == 0 {\n } else if a[n - 1] == 1u8 {\n } else {\n lemma_last_one_idx_in_bounds(a, n - 1);\n }\n}\n\nproof fn lemma_last_one_idx_lower_bound(a: Seq, n: int)\n requires\n 0 <= n <= a.len(),\n ensures\n last_one_idx(a, n) == -1 || (0 <= last_one_idx(a, n) < n),\n \n n + 1 <= a.len() ==> last_one_idx(a, n + 1) >= last_one_idx(a, n) || last_one_idx(a, n + 1) == n,\n decreases n,\n{\n lemma_last_one_idx_in_bounds(a, n);\n}\n\nproof fn lemma_first_one_idx_le_last(a: Seq, n: int)\n requires\n 0 <= n <= a.len(),\n first_one_idx(a, n) != -1,\n ensures\n last_one_idx(a, n) != -1,\n first_one_idx(a, n) <= last_one_idx(a, n),\n decreases n,\n{\n if n == 0 {\n assert(false);\n } else {\n let r = first_one_idx(a, n - 1);\n if r != -1 {\n lemma_first_one_idx_le_last(a, n - 1);\n lemma_last_one_idx_in_bounds(a, n - 1);\n if a[n - 1] == 1u8 {\n \n lemma_first_one_idx_in_bounds(a, n - 1);\n assert(r < n - 1);\n assert(last_one_idx(a, n) == n - 1);\n assert(first_one_idx(a, n) == r);\n } else {\n assert(last_one_idx(a, n) == last_one_idx(a, n - 1));\n assert(first_one_idx(a, n) == r);\n }\n } else if a[n - 1] == 1u8 {\n assert(first_one_idx(a, n) == n - 1);\n assert(last_one_idx(a, n) == n - 1);\n } else {\n assert(first_one_idx(a, n) == -1);\n assert(false);\n }\n }\n}\n\nimpl Solution {\n pub fn min_moves_books(n: usize, a: Vec) -> (result: usize)\n requires\n 1 <= n <= 50,\n a.len() == n,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 1u8,\n exists|i: int| 0 <= i < a.len() && #[trigger] a[i] == 1u8,\n ensures\n result as int == count_zeros_prefix(a@, first_one_idx(a@, n as int), last_one_idx(a@, n as int) + 1),\n {\n let mut first: usize = 0;\n let mut found_first = false;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n a.len() == n,\n 1 <= n <= 50,\n forall|j: int| 0 <= j < a.len() ==> #[trigger] a[j] <= 1u8,\n found_first <==> first_one_idx(a@, i as int) != -1,\n found_first ==> (first as int) == first_one_idx(a@, i as int),\n found_first ==> first < i,\n decreases n - i,\n {\n if a[i] == 1u8 && !found_first {\n first = i;\n found_first = true;\n }\n i += 1;\n }\n let mut last: usize = 0;\n let mut found_last = false;\n let mut j: usize = 0;\n while j < n\n invariant\n 0 <= j <= n,\n a.len() == n,\n 1 <= n <= 50,\n forall|k: int| 0 <= k < a.len() ==> #[trigger] a[k] <= 1u8,\n found_last <==> last_one_idx(a@, j as int) != -1,\n found_last ==> (last as int) == last_one_idx(a@, j as int),\n found_last ==> last < j,\n found_first <==> first_one_idx(a@, n as int) != -1,\n found_first ==> (first as int) == first_one_idx(a@, n as int),\n decreases n - j,\n {\n if a[j] == 1u8 {\n last = j;\n found_last = true;\n }\n j += 1;\n }\n proof {\n lemma_exists_implies_first_one(a@, n as int);\n lemma_exists_implies_last_one(a@, n as int);\n lemma_first_one_idx_le_last(a@, n as int);\n }\n if !found_first || !found_last {\n assert(false);\n return 0;\n }\n let mut count: usize = 0;\n let mut k: usize = first;\n while k <= last\n invariant\n first <= k <= last + 1,\n last < n,\n first <= last,\n a.len() == n,\n 1 <= n <= 50,\n forall|m: int| 0 <= m < a.len() ==> #[trigger] a[m] <= 1u8,\n count as int == count_zeros_prefix(a@, first as int, k as int),\n count as int <= k as int - first as int,\n first as int == first_one_idx(a@, n as int),\n last as int == last_one_idx(a@, n as int),\n decreases last + 1 - k,\n {\n if a[k] == 0u8 {\n count += 1;\n }\n k += 1;\n }\n count\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_moves_books(n: usize, a: Vec) -> usize {\n let mut first: usize = 0;\n let mut found_first = false;\n let mut i: usize = 0;\n while i < n {\n if a[i] == 1u8 && !found_first {\n first = i;\n found_first = true;\n }\n i += 1;\n }\n let mut last: usize = 0;\n let mut found_last = false;\n let mut j: usize = 0;\n while j < n {\n if a[j] == 1u8 {\n last = j;\n found_last = true;\n }\n j += 1;\n }\n if !found_first || !found_last {\n return 0;\n }\n let mut count: usize = 0;\n let mut k: usize = first;\n while k <= last {\n if a[k] == 0u8 {\n count += 1;\n }\n k += 1;\n }\n count\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n a.push(iter.next().unwrap().parse().unwrap());\n }\n let ans = Solution::min_moves_books(n, a);\n writeln!(out, \"{}\", ans).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1433B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1433B/tests/mutated_testcases.jsonl"} {"problem": "cf1462D", "family": "codeforces", "description": "# Add to Neighbour and Remove\n\nTime limit: 3 seconds | Memory limit: 256 megabytes\n\nPolycarp was given an array of $$$a[1 \\dots n]$$$ of $$$n$$$ integers. He can perform the following operation with the array $$$a$$$ no more than $$$n$$$ times:\n - Polycarp selects the index $$$i$$$ and adds the value $$$a_i$$$ to **one of his choice** of its neighbors. More formally, Polycarp adds the value of $$$a_i$$$ to $$$a_{i-1}$$$ or to $$$a_{i+1}$$$ (if such a neighbor does not exist, then it is impossible to add to it). \n- After adding it, Polycarp removes the $$$i$$$-th element from the $$$a$$$ array. During this step the length of $$$a$$$ is decreased by $$$1$$$. \n\nThe two items above together denote one single operation.\n\nFor example, if Polycarp has an array $$$a = [3, 1, 6, 6, 2]$$$, then it can perform the following sequence of operations with it: \n - Polycarp selects $$$i = 2$$$ and adds the value $$$a_i$$$ to $$$(i-1)$$$-th element: $$$a = [4, 6, 6, 2]$$$. \n- Polycarp selects $$$i = 1$$$ and adds the value $$$a_i$$$ to $$$(i+1)$$$-th element: $$$a = [10, 6, 2]$$$. \n- Polycarp selects $$$i = 3$$$ and adds the value $$$a_i$$$ to $$$(i-1)$$$-th element: $$$a = [10, 8]$$$. \n- Polycarp selects $$$i = 2$$$ and adds the value $$$a_i$$$ to $$$(i-1)$$$-th element: $$$a = [18]$$$. \n\nNote that Polycarp could stop performing operations at any time.\n\nPolycarp wondered how many minimum operations he would need to perform to make all the elements of $$$a$$$ equal (i.e., he wants all $$$a_i$$$ are equal to each other).\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 3000$$$)\u00a0\u2014 the number of test cases in the test. Then $$$t$$$ test cases follow.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 3000$$$)\u00a0\u2014 the length of the array. The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^5$$$)\u00a0\u2014 array $$$a$$$.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.\n\n## Output\n\nFor each test case, output a single number\u00a0\u2014 the minimum number of operations that Polycarp needs to perform so that all elements of the $$$a$$$ array are the same (equal).\n\n## Example\n\n**Input:**\n```\n4\n5\n3 1 6 6 2\n4\n1 2 2 1\n3\n2 2 2\n4\n6 3 2 1\n```\n**Output:**\n```\n4\n2\n0\n2\n```\n\n## Note\n\nIn the first test case of the example, the answer can be constructed like this (just one way among many other ways):\n\n$$$[3, 1, 6, 6, 2]$$$ $$$\\xrightarrow[]{i=4,~add~to~left}$$$ $$$[3, 1, 12, 2]$$$ $$$\\xrightarrow[]{i=2,~add~to~right}$$$ $$$[3, 13, 2]$$$ $$$\\xrightarrow[]{i=1,~add~to~right}$$$ $$$[16, 2]$$$ $$$\\xrightarrow[]{i=2,~add~to~left}$$$ $$$[18]$$$. All elements of the array $$$[18]$$$ are the same.\n\nIn the second test case of the example, the answer can be constructed like this (just one way among other ways):\n\n$$$[1, 2, 2, 1]$$$ $$$\\xrightarrow[]{i=1,~add~to~right}$$$ $$$[3, 2, 1]$$$ $$$\\xrightarrow[]{i=3,~add~to~left}$$$ $$$[3, 3]$$$. All elements of the array $$$[3, 3]$$$ are the same.\n\nIn the third test case of the example, Polycarp doesn't need to perform any operations since $$$[2, 2, 2]$$$ contains equal (same) elements only.\n\nIn the fourth test case of the example, the answer can be constructed like this (just one way among other ways):\n\n$$$[6, 3, 2, 1]$$$ $$$\\xrightarrow[]{i=3,~add~to~right}$$$ $$$[6, 3, 3]$$$ $$$\\xrightarrow[]{i=3,~add~to~left}$$$ $$$[6, 6]$$$. All elements of the array $$$[6, 6]$$$ are the same.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_operations(n: usize, a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let ans = Solution::min_operations(n, a);\n writeln!(out, \"{}\", ans).unwrap();\n }\n}\n```\n", "tags": "Rating: 1400\ngreedy, math, number theory\nSolved by: 18025 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn sum_range(a: Seq, start: int, end: int) -> int\n decreases end - start\n{\n if end <= start {\n 0\n } else {\n a[end - 1] as int + sum_range(a, start, end - 1)\n }\n}\n\npub open spec fn can_partition_k(a: Seq, n: usize, k: int) -> bool {\n k >= 1 && k <= n &&\n sum_range(a, 0, n as int) % k == 0 &&\n {\n let target = sum_range(a, 0, n as int) / k;\n exists|splits: Seq|\n splits.len() == k + 1 &&\n splits[0] == 0 && splits[k] == n &&\n (forall|j: int| 0 <= j && j < k ==>\n splits[j] < splits[j + 1] &&\n sum_range(a, splits[j], #[trigger] splits[j + 1]) == target)\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn min_operations(n: usize, a: Vec) -> (ans: i64)\n requires\n 1 <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n ensures\n ans >= 0 && ans < n,\n can_partition_k(a@, n, (n as int - ans as int)),\n forall|k2: int| (n as int - ans as int) < k2 && k2 <= n as int ==> !can_partition_k(a@, n, k2),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn compute_sum(n: usize, a: &Vec) -> i64 {\n let mut total: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n total += a[i];\n i += 1;\n }\n total\n }\n\n fn check_partition(n: usize, a: &Vec, g: usize, total: i64) -> bool {\n let target = total / (g as i64);\n let mut seg_sum: i64 = 0;\n let mut count: usize = 0;\n let mut j: usize = 0;\n let mut ok = true;\n while j < n {\n if ok {\n seg_sum += a[j];\n }\n j += 1;\n if ok && seg_sum == target {\n count += 1;\n seg_sum = 0;\n } else if ok && seg_sum > target {\n ok = false;\n }\n }\n if ok && count == g && seg_sum == 0 {\n true\n } else {\n false\n }\n }\n\n pub fn min_operations(n: usize, a: Vec) -> i64 {\n let total = Solution::compute_sum(n, &a);\n let mut best_k: usize = 1;\n let mut g: usize = n;\n while g >= 1 {\n if total % (g as i64) == 0 && total >= g as i64 {\n let ok = Solution::check_partition(n, &a, g, total);\n if ok {\n best_k = g;\n return (n as i64) - (best_k as i64);\n }\n }\n g -= 1;\n }\n (n as i64) - (best_k as i64)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn sum_range(a: Seq, start: int, end: int) -> int\n decreases end - start\n{\n if end <= start {\n 0\n } else {\n a[end - 1] as int + sum_range(a, start, end - 1)\n }\n}\n\npub open spec fn can_partition_k(a: Seq, n: usize, k: int) -> bool {\n k >= 1 && k <= n &&\n sum_range(a, 0, n as int) % k == 0 &&\n {\n let target = sum_range(a, 0, n as int) / k;\n exists|splits: Seq|\n splits.len() == k + 1 &&\n splits[0] == 0 && splits[k] == n &&\n (forall|j: int| 0 <= j && j < k ==>\n splits[j] < splits[j + 1] &&\n sum_range(a, splits[j], #[trigger] splits[j + 1]) == target)\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n fn compute_sum(n: usize, a: &Vec) -> (total: i64)\n requires\n 1 <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n ensures\n total as int == sum_range(a@, 0, n as int),\n 1 <= total && total <= 300000000,\n {\n let mut total: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n total += a[i];\n i += 1;\n }\n total\n }\n\n fn check_partition(n: usize, a: &Vec, g: usize, total: i64) -> (ok: bool)\n requires\n 1 <= g && g <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n total as int == sum_range(a@, 0, n as int),\n 1 <= total && total <= 300000000,\n total % (g as i64) == 0,\n total >= g as i64,\n ensures\n ok == can_partition_k(a@, n, g as int),\n {\n let target = total / (g as i64);\n let mut seg_sum: i64 = 0;\n let mut count: usize = 0;\n let mut j: usize = 0;\n let mut ok = true;\n while j < n {\n if ok {\n seg_sum += a[j];\n }\n j += 1;\n if ok && seg_sum == target {\n count += 1;\n seg_sum = 0;\n } else if ok && seg_sum > target {\n ok = false;\n }\n }\n if ok && count == g && seg_sum == 0 {\n true\n } else {\n false\n }\n }\n\n pub fn min_operations(n: usize, a: Vec) -> (ans: i64)\n requires\n 1 <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n ensures\n ans >= 0 && ans < n,\n can_partition_k(a@, n, (n as int - ans as int)),\n forall|k2: int| (n as int - ans as int) < k2 && k2 <= n as int ==> !can_partition_k(a@, n, k2),\n {\n let total = Solution::compute_sum(n, &a);\n let mut best_k: usize = 1;\n let mut g: usize = n;\n while g >= 1 {\n if total % (g as i64) == 0 && total >= g as i64 {\n let ok = Solution::check_partition(n, &a, g, total);\n if ok {\n best_k = g;\n return (n as i64) - (best_k as i64);\n }\n }\n g -= 1;\n }\n (n as i64) - (best_k as i64)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn sum_range(a: Seq, start: int, end: int) -> int\n decreases end - start\n{\n if end <= start {\n 0\n } else {\n a[end - 1] as int + sum_range(a, start, end - 1)\n }\n}\n\nproof fn lemma_sum_range_additive(a: Seq, s: int, m: int, e: int)\n requires\n 0 <= s <= m <= e <= a.len(),\n ensures\n sum_range(a, s, e) == sum_range(a, s, m) + sum_range(a, m, e),\n decreases e - m,\n{\n if e > m {\n lemma_sum_range_additive(a, s, m, e - 1);\n }\n}\n\nproof fn lemma_sum_range_pos_strict(a: Seq, s: int, e: int)\n requires\n 0 <= s < e <= a.len(),\n forall|i: int| s <= i < e ==> a[i] >= 1,\n ensures\n sum_range(a, s, e) >= e - s,\n decreases e - s,\n{\n if e - 1 > s {\n lemma_sum_range_pos_strict(a, s, e - 1);\n assert(sum_range(a, s, e) == a[e - 1] as int + sum_range(a, s, e - 1));\n } else {\n assert(e - 1 == s);\n assert(sum_range(a, s, e) == a[e - 1] as int + sum_range(a, s, e - 1));\n assert(sum_range(a, s, e - 1) == 0);\n assert(a[e - 1] >= 1);\n }\n}\n\nproof fn lemma_sum_range_mono_strict(a: Seq, s: int, m: int, e: int)\n requires\n 0 <= s <= m < e <= a.len(),\n forall|i: int| s <= i < a.len() ==> a[i] >= 1,\n ensures\n sum_range(a, s, m) < sum_range(a, s, e),\n{\n lemma_sum_range_additive(a, s, m, e);\n lemma_sum_range_pos_strict(a, m, e);\n}\n\nproof fn lemma_seq_mono_le(splits: Seq, bound: int, i: int, j: int)\n requires\n 0 <= i <= j <= bound,\n bound < splits.len(),\n forall|k: int| 0 <= k < bound ==> splits[k] < #[trigger] splits[k + 1],\n ensures\n splits[i] <= splits[j],\n decreases j - i,\n{\n if i < j {\n lemma_seq_mono_le(splits, bound, i, j - 1);\n let k = j - 1;\n assert(0 <= k < bound);\n assert(splits[k] < splits[k + 1]);\n assert(splits[i] <= splits[k]);\n assert(splits[j] == splits[k + 1]);\n } else {\n assert(i == j);\n }\n}\n\nproof fn lemma_sum_range_zero_implies_eq(a: Seq, m: int, pos: int)\n requires\n 0 <= m <= pos <= a.len(),\n forall|i: int| m <= i < a.len() ==> a[i] >= 1,\n sum_range(a, m, pos) == 0,\n ensures\n m == pos,\n{\n if m < pos {\n lemma_sum_range_pos_strict(a, m, pos);\n assert(false);\n }\n}\n\npub open spec fn greedy_partition(a: Seq, target: int, pos: int, seg_sum: int, count: int, g: int) -> bool\n decreases a.len() - pos when 0 <= pos <= a.len()\n{\n if pos >= a.len() {\n seg_sum == 0 && count == g\n } else {\n let new_sum = seg_sum + a[pos] as int;\n if new_sum == target {\n greedy_partition(a, target, pos + 1, 0, count + 1, g)\n } else if new_sum > target {\n false\n } else {\n greedy_partition(a, target, pos + 1, new_sum, count, g)\n }\n }\n}\n\nproof fn lemma_greedy_count_le_g(a: Seq, target: int, pos: int, seg_sum: int, count: int, g: int)\n requires\n 0 <= pos <= a.len(),\n greedy_partition(a, target, pos, seg_sum, count, g) == true,\n ensures\n count <= g,\n decreases a.len() - pos,\n{\n if pos < a.len() {\n let new_sum = seg_sum + a[pos] as int;\n if new_sum == target {\n lemma_greedy_count_le_g(a, target, pos + 1, 0, count + 1, g);\n } else if new_sum > target {\n } else {\n lemma_greedy_count_le_g(a, target, pos + 1, new_sum, count, g);\n }\n }\n}\n\nproof fn lemma_greedy_segment(a: Seq, target: int, s: int, e: int, pos: int, count: int, g: int)\n requires\n 0 <= s <= pos < e <= a.len(),\n target == sum_range(a, s, e),\n forall|i: int| s <= i < a.len() ==> a[i] >= 1,\n ensures\n greedy_partition(a, target, pos, sum_range(a, s, pos), count, g)\n == greedy_partition(a, target, e, 0, count + 1, g),\n decreases e - pos,\n{\n let seg_sum = sum_range(a, s, pos);\n lemma_sum_range_additive(a, s, pos, pos + 1);\n if pos + 1 == e {\n assert(sum_range(a, s, pos + 1) == sum_range(a, s, e));\n assert(seg_sum + a[pos] as int == target);\n } else {\n lemma_sum_range_mono_strict(a, s, pos + 1, e);\n assert((seg_sum + a[pos] as int) < target);\n lemma_greedy_segment(a, target, s, e, pos + 1, count, g);\n assert(sum_range(a, s, pos + 1) == seg_sum + a[pos] as int);\n }\n}\n\nproof fn lemma_greedy_from_witness(a: Seq, target: int, g: int, splits2: Seq, count: int)\n requires\n 0 <= count <= g,\n splits2.len() == g + 1,\n splits2[0] == 0,\n splits2[g] == a.len(),\n forall|j: int| 0 <= j < g ==> splits2[j] < #[trigger] splits2[j + 1] && sum_range(a, splits2[j], splits2[j + 1]) == target,\n forall|i: int| 0 <= i < a.len() ==> a[i] >= 1,\n ensures\n greedy_partition(a, target, splits2[count], 0, count, g) == true,\n decreases g - count,\n{\n lemma_seq_mono_le(splits2, g, 0, count);\n lemma_seq_mono_le(splits2, g, count, g);\n assert(0 <= splits2[count] <= a.len());\n if count == g {\n assert(splits2[count] == a.len());\n } else {\n lemma_greedy_from_witness(a, target, g, splits2, count + 1);\n assert(splits2[count] < splits2[count + 1]);\n assert(sum_range(a, splits2[count], splits2[count + 1]) == target);\n lemma_seq_mono_le(splits2, g, count + 1, g);\n assert(splits2[count + 1] <= a.len());\n lemma_greedy_segment(a, target, splits2[count], splits2[count + 1], splits2[count], count, g);\n assert(sum_range(a, splits2[count], splits2[count]) == 0);\n }\n}\n\nproof fn lemma_greedy_sound(a: Seq, target: int, pos: int, count: int, g: int, prefix: Seq)\n requires\n 0 <= pos <= a.len(),\n 0 <= count <= g,\n prefix.len() == count + 1,\n prefix[0] == 0,\n forall|j: int| 0 <= j < count ==> prefix[j] < #[trigger] prefix[j + 1] && sum_range(a, prefix[j], prefix[j + 1]) == target,\n prefix[count] <= pos,\n forall|i: int| 0 <= i < a.len() ==> a[i] >= 1,\n greedy_partition(a, target, pos, sum_range(a, prefix[count], pos), count, g) == true,\n ensures\n exists|splits2: Seq|\n splits2.len() == g + 1 && splits2[0] == 0 && splits2[g] == a.len()\n && (forall|j: int| 0 <= j < g ==> splits2[j] < #[trigger] splits2[j + 1] && sum_range(a, splits2[j], splits2[j + 1]) == target),\n decreases a.len() - pos,\n{\n lemma_seq_mono_le(prefix, count, 0, count);\n assert(0 <= prefix[count]);\n let seg_sum = sum_range(a, prefix[count], pos);\n if pos >= a.len() {\n assert(seg_sum == 0 && count == g);\n lemma_sum_range_zero_implies_eq(a, prefix[count], pos);\n assert(prefix[count] == pos);\n assert(prefix[g] == a.len());\n assert(exists|splits2: Seq|\n splits2.len() == g + 1 && splits2[0] == 0 && splits2[g] == a.len()\n && (forall|j: int| 0 <= j < g ==> splits2[j] < #[trigger] splits2[j + 1] && sum_range(a, splits2[j], splits2[j + 1]) == target)) by {\n assert(prefix.len() == g + 1);\n assert(prefix[0] == 0);\n assert(prefix[g] == a.len());\n assert(forall|j: int| 0 <= j < g ==> prefix[j] < #[trigger] prefix[j + 1] && sum_range(a, prefix[j], prefix[j + 1]) == target);\n };\n } else {\n lemma_sum_range_additive(a, prefix[count], pos, pos + 1);\n let new_sum = seg_sum + a[pos] as int;\n if new_sum == target {\n let prefix2 = prefix.push(pos + 1);\n assert(prefix2.len() == count + 2);\n assert(prefix2[count] == prefix[count]);\n assert(prefix2[count + 1] == pos + 1);\n assert forall|j: int| 0 <= j < count + 1 implies prefix2[j] < #[trigger] prefix2[j + 1] && sum_range(a, prefix2[j], prefix2[j + 1]) == target by {\n if j < count {\n assert(prefix2[j] == prefix[j]);\n assert(prefix2[j + 1] == prefix[j + 1]);\n } else {\n assert(j == count);\n assert(prefix2[count] == prefix[count]);\n assert(prefix2[count + 1] == pos + 1);\n assert(prefix[count] <= pos);\n assert(sum_range(a, prefix[count], pos + 1) == new_sum);\n }\n };\n lemma_greedy_count_le_g(a, target, pos + 1, 0, count + 1, g);\n lemma_greedy_sound(a, target, pos + 1, count + 1, g, prefix2);\n } else if new_sum > target {\n assert(false);\n } else {\n lemma_greedy_sound(a, target, pos + 1, count, g, prefix);\n }\n }\n}\n\npub open spec fn can_partition_k(a: Seq, n: usize, k: int) -> bool {\n k >= 1 && k <= n &&\n sum_range(a, 0, n as int) % k == 0 &&\n {\n let target = sum_range(a, 0, n as int) / k;\n exists|splits: Seq|\n splits.len() == k + 1 &&\n splits[0] == 0 && splits[k] == n &&\n (forall|j: int| 0 <= j && j < k ==>\n splits[j] < splits[j + 1] &&\n sum_range(a, splits[j], #[trigger] splits[j + 1]) == target)\n }\n}\n\nproof fn lemma_mul_step(cnt: int, tgt: int, x: int, y: int, z: int)\n requires\n cnt >= 1,\n x == y + z,\n y == (cnt - 1) * tgt,\n z == tgt,\n ensures\n x == cnt * tgt,\n{\n assert(x == cnt * tgt) by (nonlinear_arith)\n requires\n x == y + z,\n y == (cnt - 1) * tgt,\n z == tgt;\n}\n\nproof fn lemma_witness_total(a: Seq, g: int, target: int, splits2: Seq, count: int)\n requires\n 0 <= count <= g,\n splits2.len() == g + 1,\n splits2[0] == 0,\n splits2[g] <= a.len(),\n forall|j: int| 0 <= j < g ==> splits2[j] < #[trigger] splits2[j + 1] && sum_range(a, splits2[j], splits2[j + 1]) == target,\n ensures\n sum_range(a, 0, splits2[count]) == count * target,\n decreases count,\n{\n lemma_seq_mono_le(splits2, g, 0, count);\n lemma_seq_mono_le(splits2, g, count, g);\n if count > 0 {\n lemma_witness_total(a, g, target, splits2, count - 1);\n lemma_seq_mono_le(splits2, g, 0, count - 1);\n lemma_seq_mono_le(splits2, g, count - 1, count);\n lemma_seq_mono_le(splits2, g, count, g);\n lemma_sum_range_additive(a, 0, splits2[count - 1], splits2[count]);\n let jj = count - 1;\n assert(0 <= jj < g);\n assert(splits2[jj] < splits2[jj + 1] && sum_range(a, splits2[jj], splits2[jj + 1]) == target);\n assert(splits2[count - 1] < splits2[count]);\n assert(sum_range(a, splits2[count - 1], splits2[count]) == target);\n assert(sum_range(a, 0, splits2[count - 1]) == (count - 1) * target);\n lemma_mul_step(\n count,\n target,\n sum_range(a, 0, splits2[count]),\n sum_range(a, 0, splits2[count - 1]),\n sum_range(a, splits2[count - 1], splits2[count]),\n );\n assert(sum_range(a, 0, splits2[count]) == count * target);\n } else {\n assert(count == 0);\n assert(splits2[count] == 0);\n assert(sum_range(a, 0, splits2[count]) == 0);\n assert(count * target == 0);\n }\n}\n\nproof fn lemma_greedy_iff_can_partition(a: Seq, n: usize, g: int, target: int)\n requires\n a.len() == n as int,\n 1 <= g <= n as int,\n target == sum_range(a, 0, n as int) / g,\n forall|i: int| 0 <= i < a.len() ==> a[i] >= 1,\n ensures\n greedy_partition(a, target, 0, 0, 0, g) == can_partition_k(a, n, g),\n{\n if greedy_partition(a, target, 0, 0, 0, g) {\n let prefix: Seq = seq![0int];\n assert(prefix[0] == 0);\n assert(sum_range(a, prefix[0], 0) == 0);\n lemma_greedy_sound(a, target, 0, 0, g, prefix);\n assert(exists|splits2: Seq|\n splits2.len() == g + 1 && splits2[0] == 0 && splits2[g] == a.len()\n && (forall|j: int| 0 <= j < g ==> splits2[j] < #[trigger] splits2[j + 1] && sum_range(a, splits2[j], splits2[j + 1]) == target));\n let splits2 = choose|splits2: Seq|\n splits2.len() == g + 1 && splits2[0] == 0 && splits2[g] == a.len()\n && (forall|j: int| 0 <= j < g ==> splits2[j] < #[trigger] splits2[j + 1] && sum_range(a, splits2[j], splits2[j + 1]) == target);\n lemma_witness_total(a, g, target, splits2, g);\n assert(sum_range(a, 0, splits2[g]) == g * target);\n assert(sum_range(a, 0, n as int) == g * target);\n assert(sum_range(a, 0, n as int) % g == 0) by (nonlinear_arith)\n requires sum_range(a, 0, n as int) == g * target, g >= 1;\n assert(sum_range(a, 0, n as int) / g == target) by (nonlinear_arith)\n requires sum_range(a, 0, n as int) == g * target, g >= 1;\n }\n if can_partition_k(a, n, g) {\n assert(sum_range(a, 0, n as int) % g == 0);\n let target2 = sum_range(a, 0, n as int) / g;\n assert(target2 == target);\n assert(exists|splits: Seq|\n splits.len() == g + 1 &&\n splits[0] == 0 && splits[g] == n &&\n (forall|j: int| 0 <= j && j < g ==>\n splits[j] < splits[j + 1] &&\n sum_range(a, splits[j], #[trigger] splits[j + 1]) == target2));\n let splits2 = choose|splits: Seq|\n splits.len() == g + 1 &&\n splits[0] == 0 && splits[g] == n &&\n (forall|j: int| 0 <= j && j < g ==>\n splits[j] < splits[j + 1] &&\n sum_range(a, splits[j], #[trigger] splits[j + 1]) == target2);\n assert(splits2[g] == n as int);\n lemma_greedy_from_witness(a, target, g, splits2, 0);\n assert(splits2[0] == 0);\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n fn compute_sum(n: usize, a: &Vec) -> (total: i64)\n requires\n 1 <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n ensures\n total as int == sum_range(a@, 0, n as int),\n 1 <= total && total <= 300000000,\n {\n let mut total: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i && i <= n,\n n <= 3000,\n a.len() == n,\n forall|j: int| 0 <= j && j < n ==> 1 <= a@[j] && a@[j] <= 100000,\n total as int == sum_range(a@, 0, i as int),\n total >= i as i64,\n total <= (i as i64) * 100000,\n decreases n - i\n {\n total += a[i];\n i += 1;\n }\n total\n }\n\n fn check_partition(n: usize, a: &Vec, g: usize, total: i64) -> (ok: bool)\n requires\n 1 <= g && g <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n total as int == sum_range(a@, 0, n as int),\n 1 <= total && total <= 300000000,\n total % (g as i64) == 0,\n total >= g as i64,\n ensures\n ok == can_partition_k(a@, n, g as int),\n {\n let target = total / (g as i64);\n proof {\n lemma_sum_range_pos_strict(a@, 0, n as int);\n assert(total as int >= n as int);\n assert(n as int >= g as int);\n assert(total as int >= g as int);\n assert(target as int >= 1) by (nonlinear_arith)\n requires\n total as int >= g as int,\n g as int >= 1,\n target as int == total as int / (g as int);\n }\n let mut seg_sum: i64 = 0;\n let mut count: usize = 0;\n let mut j: usize = 0;\n let mut ok = true;\n while j < n\n invariant\n 0 <= j && j <= n,\n n <= 3000,\n a.len() == n,\n 1 <= g && g <= n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n total as int == sum_range(a@, 0, n as int),\n 1 <= total && total <= 300000000,\n target == total / (g as i64),\n target >= 1,\n count <= j && count <= n,\n ok ==> 0 <= seg_sum && seg_sum < total,\n ok ==> greedy_partition(a@, target as int, j as int, seg_sum as int, count as int, g as int)\n == greedy_partition(a@, target as int, 0, 0, 0, g as int),\n !ok ==> !greedy_partition(a@, target as int, 0, 0, 0, g as int),\n decreases n - j\n {\n let ghost old_ok = ok;\n let ghost old_seg_sum = seg_sum;\n let ghost old_count = count;\n let ghost old_j = j;\n if ok {\n seg_sum += a[j];\n }\n j += 1;\n if ok && seg_sum == target {\n count += 1;\n seg_sum = 0;\n } else if ok && seg_sum > target {\n ok = false;\n }\n proof {\n if old_ok {\n let new_sum = old_seg_sum as int + a@[old_j as int] as int;\n assert(greedy_partition(a@, target as int, old_j as int, old_seg_sum as int, old_count as int, g as int)\n == greedy_partition(a@, target as int, 0, 0, 0, g as int));\n if new_sum == target as int {\n assert(greedy_partition(a@, target as int, old_j as int, old_seg_sum as int, old_count as int, g as int)\n == greedy_partition(a@, target as int, old_j as int + 1, 0, old_count as int + 1, g as int));\n assert(ok);\n assert(count as int == old_count as int + 1);\n assert(seg_sum == 0);\n assert(j as int == old_j as int + 1);\n } else if new_sum > target as int {\n assert(greedy_partition(a@, target as int, old_j as int, old_seg_sum as int, old_count as int, g as int)\n == false);\n assert(!ok);\n } else {\n assert(greedy_partition(a@, target as int, old_j as int, old_seg_sum as int, old_count as int, g as int)\n == greedy_partition(a@, target as int, old_j as int + 1, new_sum, old_count as int, g as int));\n assert(ok);\n assert(seg_sum as int == new_sum);\n assert(count as int == old_count as int);\n assert(j as int == old_j as int + 1);\n }\n } else {\n assert(!ok);\n }\n }\n }\n proof {\n assert((ok && count == g && seg_sum == 0) == greedy_partition(a@, target as int, 0, 0, 0, g as int)) by {\n if ok {\n assert(greedy_partition(a@, target as int, n as int, seg_sum as int, count as int, g as int)\n == greedy_partition(a@, target as int, 0, 0, 0, g as int));\n assert(greedy_partition(a@, target as int, n as int, seg_sum as int, count as int, g as int)\n == (seg_sum as int == 0 && count as int == g as int));\n }\n };\n lemma_greedy_iff_can_partition(a@, n, g as int, target as int);\n }\n if ok && count == g && seg_sum == 0 {\n true\n } else {\n false\n }\n }\n\n pub fn min_operations(n: usize, a: Vec) -> (ans: i64)\n requires\n 1 <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n ensures\n ans >= 0 && ans < n,\n can_partition_k(a@, n, (n as int - ans as int)),\n forall|k2: int| (n as int - ans as int) < k2 && k2 <= n as int ==> !can_partition_k(a@, n, k2),\n {\n let total = Solution::compute_sum(n, &a);\n let mut best_k: usize = 1;\n proof {\n lemma_partition_one(a@, n);\n }\n let mut g: usize = n;\n while g >= 1\n invariant\n 0 <= g && g <= n,\n 1 <= n && n <= 3000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 100000,\n total as int == sum_range(a@, 0, n as int),\n 1 <= total && total <= 300000000,\n 1 <= best_k && best_k <= n,\n can_partition_k(a@, n, best_k as int),\n forall|k2: int| (g as int) < k2 && k2 <= (n as int) ==> !can_partition_k(a@, n, k2),\n decreases g,\n {\n proof {\n lemma_sum_range_pos_strict(a@, 0, n as int);\n }\n if total % (g as i64) == 0 && total >= g as i64 {\n let ok = Solution::check_partition(n, &a, g, total);\n if ok {\n best_k = g;\n proof {\n assert(can_partition_k(a@, n, g as int));\n assert(forall|k2: int| (g as int) < k2 && k2 <= (n as int) ==> !can_partition_k(a@, n, k2));\n }\n return (n as i64) - (best_k as i64);\n } else {\n proof {\n assert(!can_partition_k(a@, n, g as int));\n }\n }\n } else {\n proof {\n if total % (g as i64) != 0 {\n assert(sum_range(a@, 0, n as int) % (g as int) != 0);\n assert(!can_partition_k(a@, n, g as int));\n } else {\n assert(total < g as i64);\n assert(false);\n }\n }\n }\n g -= 1;\n }\n proof {\n assert(g == 0);\n assert(can_partition_k(a@, n, 1));\n assert((g as int) < 1 && 1 <= (n as int));\n assert(!can_partition_k(a@, n, 1));\n assert(false);\n }\n (n as i64) - (best_k as i64)\n }\n}\n\nproof fn lemma_partition_one(a: Seq, n: usize)\n requires\n n >= 1,\n a.len() == n,\n ensures\n can_partition_k(a, n, 1),\n{\n let splits: Seq = seq![0, n as int];\n assert(splits.len() == 2);\n assert(splits[0] == 0);\n assert(splits[1] == n as int);\n assert(splits[0] < splits[1]);\n assert(sum_range(a, splits[0], splits[1]) == sum_range(a, 0, n as int));\n}\n\n}\n", "main": "use std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n fn compute_sum(n: usize, a: &Vec) -> i64 {\n let mut total: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n total += a[i];\n i += 1;\n }\n total\n }\n\n fn check_partition(n: usize, a: &Vec, g: usize, total: i64) -> bool {\n let target = total / (g as i64);\n let mut seg_sum: i64 = 0;\n let mut count: usize = 0;\n let mut j: usize = 0;\n let mut ok = true;\n while j < n {\n if ok {\n seg_sum += a[j];\n }\n j += 1;\n if ok && seg_sum == target {\n count += 1;\n seg_sum = 0;\n } else if ok && seg_sum > target {\n ok = false;\n }\n }\n if ok && count == g && seg_sum == 0 {\n true\n } else {\n false\n }\n }\n\n pub fn min_operations(n: usize, a: Vec) -> i64 {\n let total = Solution::compute_sum(n, &a);\n let mut best_k: usize = 1;\n let mut g: usize = n;\n while g >= 1 {\n if total % (g as i64) == 0 && total >= g as i64 {\n let ok = Solution::check_partition(n, &a, g, total);\n if ok {\n best_k = g;\n return (n as i64) - (best_k as i64);\n }\n }\n g -= 1;\n }\n (n as i64) - (best_k as i64)\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let ans = Solution::min_operations(n, a);\n writeln!(out, \"{}\", ans).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1462D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1462D/tests/mutated_testcases.jsonl"} {"problem": "cf148A", "family": "codeforces", "description": "# Insomnia cure\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\n\u00abOne dragon. Two dragon. Three dragon\u00bb, \u2014 the princess was counting. She had trouble falling asleep, and she got bored of counting lambs when she was nine.\n\nHowever, just counting dragons was boring as well, so she entertained herself at best she could. Tonight she imagined that all dragons were here to steal her, and she was fighting them off. Every $k$-th dragon got punched in the face with a frying pan. Every $l$-th dragon got his tail shut into the balcony door. Every $m$-th dragon got his paws trampled with sharp heels. Finally, she threatened every $n$-th dragon to call her mom, and he withdrew in panic.\n\nHow many imaginary dragons suffered moral or physical damage tonight, if the princess counted a total of $d$ dragons?\n\n## Input\n\nInput data contains integer numbers $k,\u2009l,\u2009m,\u2009n$ and $d$, each number in a separate line ($1\u2009\u2264\u2009k,\u2009l,\u2009m,\u2009n\u2009\u2264\u200910$, $1\u2009\u2264\u2009d\u2009\u2264\u200910^5$).\n\n## Output\n\nOutput the number of damaged dragons.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n1\n2\n3\n4\n12\n```\n**Output:**\n```\n12\n```\n\n### Example 2\n\n**Input:**\n```\n2\n3\n4\n5\n24\n```\n**Output:**\n```\n17\n```\n\n## Note\n\nIn the first case every first dragon got punched with a frying pan. Some of the dragons suffered from other reasons as well, but the pan alone would be enough.\n\nIn the second case dragons 1, 7, 11, 13, 17, 19 and 23 escaped unharmed.\n\n## Starter Code\n\n```rust\nuse std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_damaged(k: i32, l: i32, m: i32, n: i32, d: i32) -> i32 {\n \n }\n}\n\nfn main() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"read k\");\n let k: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read l\");\n let l: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read m\");\n let m: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read n\");\n let n: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read d\");\n let d: i32 = line.trim().parse().expect(\"integer\");\n println!(\"{}\", Solution::count_damaged(k, l, m, n, d));\n}\n```\n", "tags": "Rating: 800\nconstructive algorithms, implementation, math\nSolved by: 110942 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn damaged(i: int, k: int, l: int, m: int, n: int) -> bool {\n i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0\n}\n\npub open spec fn count_damaged_spec(k: int, l: int, m: int, n: int, d: int) -> int\n decreases d,\n{\n if d < 1 {\n 0\n } else {\n let add: int = if damaged(d, k, l, m, n) { 1int } else { 0int };\n add + count_damaged_spec(k, l, m, n, d - 1)\n }\n}\n\nimpl Solution {\n pub fn count_damaged(k: i32, l: i32, m: i32, n: i32, d: i32) -> (result: i32)\n requires\n 1 <= k <= 10,\n 1 <= l <= 10,\n 1 <= m <= 10,\n 1 <= n <= 10,\n 1 <= d <= 100_000,\n ensures\n result as int == count_damaged_spec(k as int, l as int, m as int, n as int, d as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_damaged(k: i32, l: i32, m: i32, n: i32, d: i32) -> i32 {\n let mut count = 0i32;\n let mut i = 1i32;\n while i <= d {\n if i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0 {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn damaged(i: int, k: int, l: int, m: int, n: int) -> bool {\n i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0\n}\n\npub open spec fn count_damaged_spec(k: int, l: int, m: int, n: int, d: int) -> int\n decreases d,\n{\n if d < 1 {\n 0\n } else {\n let add: int = if damaged(d, k, l, m, n) { 1int } else { 0int };\n add + count_damaged_spec(k, l, m, n, d - 1)\n }\n}\n\nimpl Solution {\n pub fn count_damaged(k: i32, l: i32, m: i32, n: i32, d: i32) -> (result: i32)\n requires\n 1 <= k <= 10,\n 1 <= l <= 10,\n 1 <= m <= 10,\n 1 <= n <= 10,\n 1 <= d <= 100_000,\n ensures\n result as int == count_damaged_spec(k as int, l as int, m as int, n as int, d as int),\n {\n let mut count = 0i32;\n let mut i = 1i32;\n while i <= d {\n if i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0 {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn damaged(i: int, k: int, l: int, m: int, n: int) -> bool {\n i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0\n}\n\npub open spec fn count_damaged_spec(k: int, l: int, m: int, n: int, d: int) -> int\n decreases d,\n{\n if d < 1 {\n 0\n } else {\n let add: int = if damaged(d, k, l, m, n) { 1int } else { 0int };\n add + count_damaged_spec(k, l, m, n, d - 1)\n }\n}\n\nproof fn lemma_count_step(k: int, l: int, m: int, n: int, i: int)\n requires\n 1 <= k,\n 1 <= l,\n 1 <= m,\n 1 <= n,\n 1 <= i,\n ensures\n count_damaged_spec(k, l, m, n, i) == count_damaged_spec(k, l, m, n, i - 1)\n + (if damaged(i, k, l, m, n) { 1int } else { 0int }),\n decreases i,\n{\n reveal_with_fuel(count_damaged_spec, 2);\n}\n\nimpl Solution {\n pub fn count_damaged(k: i32, l: i32, m: i32, n: i32, d: i32) -> (result: i32)\n requires\n 1 <= k <= 10,\n 1 <= l <= 10,\n 1 <= m <= 10,\n 1 <= n <= 10,\n 1 <= d <= 100_000,\n ensures\n result as int == count_damaged_spec(k as int, l as int, m as int, n as int, d as int),\n {\n let mut count = 0i32;\n let mut i = 1i32;\n while i <= d\n invariant\n 1 <= k <= 10,\n 1 <= l <= 10,\n 1 <= m <= 10,\n 1 <= n <= 10,\n 1 <= d <= 100_000,\n 1 <= i <= d + 1,\n 0 <= count <= i - 1,\n count as int == count_damaged_spec(k as int, l as int, m as int, n as int, (i - 1) as int),\n decreases (d - i + 1) as nat,\n {\n proof {\n lemma_count_step(k as int, l as int, m as int, n as int, i as int);\n }\n if i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0 {\n count = count + 1;\n }\n proof {\n assert(count as int == count_damaged_spec(k as int, l as int, m as int, n as int, i as int));\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "main": "use std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_damaged(k: i32, l: i32, m: i32, n: i32, d: i32) -> i32 {\n let mut count = 0i32;\n let mut i = 1i32;\n while i <= d {\n if i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0 {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n\nfn main() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"read k\");\n let k: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read l\");\n let l: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read m\");\n let m: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read n\");\n let n: i32 = line.trim().parse().expect(\"integer\");\n line.clear();\n io::stdin().read_line(&mut line).expect(\"read d\");\n let d: i32 = line.trim().parse().expect(\"integer\");\n println!(\"{}\", Solution::count_damaged(k, l, m, n, d));\n}\n", "checker": null, "testcases_path": "codeforces/cf148A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf148A/tests/mutated_testcases.jsonl"} {"problem": "cf14A", "family": "codeforces", "description": "# Letter\n\nTime limit: 1 second | Memory limit: 64 megabytes\n\nA boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with $n$ rows and $m$ columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides.\n\n## Input\n\nThe first line of the input data contains numbers $n$ and $m$ ($1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950$), $n$ \u2014 amount of lines, and $m$ \u2014 amount of columns on Bob's sheet. The following $n$ lines contain $m$ characters each. Character \u00ab`.`\u00bb stands for a non-shaded square on the sheet, and \u00ab`*`\u00bb \u2014 for a shaded square. It is guaranteed that Bob has shaded at least one square.\n\n## Output\n\nOutput the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..\n```\n**Output:**\n```\n***\n*..\n***\n*..\n***\n```\n\n### Example 2\n\n**Input:**\n```\n3 3\n***\n*.*\n***\n```\n**Output:**\n```\n***\n*.*\n***\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn bounding_box(grid: &Vec>, n: usize, m: usize) -> (usize, usize, usize, usize) {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let m: usize = iter.next().unwrap().parse().unwrap();\n let mut grid: Vec> = Vec::with_capacity(n);\n let mut raw_grid: Vec> = Vec::with_capacity(n);\n for _ in 0..n {\n let s = iter.next().unwrap();\n let row: Vec = s.bytes().map(|b| if b == b'*' { 1u8 } else { 0u8 }).collect();\n let raw_row: Vec = s.bytes().collect();\n grid.push(row);\n raw_grid.push(raw_row);\n }\n let (min_r, max_r, min_c, max_c) = Solution::bounding_box(&grid, n, m);\n for r in min_r..=max_r {\n for c in min_c..=max_c {\n out.write_all(&[raw_grid[r][c]]).unwrap();\n }\n writeln!(out).unwrap();\n }\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 12356 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn bounding_box(grid: &Vec>, n: usize, m: usize) -> (result: (usize, usize, usize, usize))\n requires\n 1 <= n <= 50,\n 1 <= m <= 50,\n grid.len() == n,\n forall|i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == m,\n forall|i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[i].len()\n ==> #[trigger] grid[i][j] == 0u8 || grid[i][j] == 1u8,\n exists|i: int, j: int| 0 <= i < n && 0 <= j < m && #[trigger] grid[i][j] == 1u8,\n ensures\n result.0 < n,\n result.1 < n,\n result.2 < m,\n result.3 < m,\n result.0 <= result.1,\n result.2 <= result.3,\n forall|i: int, j: int| 0 <= i < n && 0 <= j < m && #[trigger] grid@[i][j] == 1u8\n ==> result.0 as int <= i && i <= result.1 as int\n && result.2 as int <= j && j <= result.3 as int,\n exists|j: int| 0 <= j < m && #[trigger] grid@[result.0 as int][j] == 1u8,\n exists|j: int| 0 <= j < m && #[trigger] grid@[result.1 as int][j] == 1u8,\n exists|i: int| 0 <= i < n && #[trigger] grid@[i][result.2 as int] == 1u8,\n exists|i: int| 0 <= i < n && #[trigger] grid@[i][result.3 as int] == 1u8,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn bounding_box(grid: &Vec>, n: usize, m: usize) -> (usize, usize, usize, usize) {\n let mut min_r: usize = n;\n let mut max_r: usize = 0;\n let mut min_c: usize = m;\n let mut max_c: usize = 0;\n let mut found: bool = false;\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = 0;\n while j < m {\n if grid[i][j] == 1u8 {\n if !found {\n min_r = i;\n max_r = i;\n min_c = j;\n max_c = j;\n found = true;\n } else {\n if i < min_r { min_r = i; }\n if i > max_r { max_r = i; }\n if j < min_c { min_c = j; }\n if j > max_c { max_c = j; }\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n (min_r, max_r, min_c, max_c)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn bounding_box(grid: &Vec>, n: usize, m: usize) -> (result: (usize, usize, usize, usize))\n requires\n 1 <= n <= 50,\n 1 <= m <= 50,\n grid.len() == n,\n forall|i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == m,\n forall|i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[i].len()\n ==> #[trigger] grid[i][j] == 0u8 || grid[i][j] == 1u8,\n exists|i: int, j: int| 0 <= i < n && 0 <= j < m && #[trigger] grid[i][j] == 1u8,\n ensures\n result.0 < n,\n result.1 < n,\n result.2 < m,\n result.3 < m,\n result.0 <= result.1,\n result.2 <= result.3,\n forall|i: int, j: int| 0 <= i < n && 0 <= j < m && #[trigger] grid@[i][j] == 1u8\n ==> result.0 as int <= i && i <= result.1 as int\n && result.2 as int <= j && j <= result.3 as int,\n exists|j: int| 0 <= j < m && #[trigger] grid@[result.0 as int][j] == 1u8,\n exists|j: int| 0 <= j < m && #[trigger] grid@[result.1 as int][j] == 1u8,\n exists|i: int| 0 <= i < n && #[trigger] grid@[i][result.2 as int] == 1u8,\n exists|i: int| 0 <= i < n && #[trigger] grid@[i][result.3 as int] == 1u8,\n {\n let mut min_r: usize = n;\n let mut max_r: usize = 0;\n let mut min_c: usize = m;\n let mut max_c: usize = 0;\n let mut found: bool = false;\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = 0;\n while j < m {\n if grid[i][j] == 1u8 {\n if !found {\n min_r = i;\n max_r = i;\n min_c = j;\n max_c = j;\n found = true;\n } else {\n if i < min_r { min_r = i; }\n if i > max_r { max_r = i; }\n if j < min_c { min_c = j; }\n if j > max_c { max_c = j; }\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n (min_r, max_r, min_c, max_c)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn bounding_box(grid: &Vec>, n: usize, m: usize) -> (result: (usize, usize, usize, usize))\n requires\n 1 <= n <= 50,\n 1 <= m <= 50,\n grid.len() == n,\n forall|i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == m,\n forall|i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[i].len()\n ==> #[trigger] grid[i][j] == 0u8 || grid[i][j] == 1u8,\n exists|i: int, j: int| 0 <= i < n && 0 <= j < m && #[trigger] grid[i][j] == 1u8,\n ensures\n result.0 < n,\n result.1 < n,\n result.2 < m,\n result.3 < m,\n result.0 <= result.1,\n result.2 <= result.3,\n forall|i: int, j: int| 0 <= i < n && 0 <= j < m && #[trigger] grid@[i][j] == 1u8\n ==> result.0 as int <= i && i <= result.1 as int\n && result.2 as int <= j && j <= result.3 as int,\n exists|j: int| 0 <= j < m && #[trigger] grid@[result.0 as int][j] == 1u8,\n exists|j: int| 0 <= j < m && #[trigger] grid@[result.1 as int][j] == 1u8,\n exists|i: int| 0 <= i < n && #[trigger] grid@[i][result.2 as int] == 1u8,\n exists|i: int| 0 <= i < n && #[trigger] grid@[i][result.3 as int] == 1u8,\n {\n let mut min_r: usize = n;\n let mut max_r: usize = 0;\n let mut min_c: usize = m;\n let mut max_c: usize = 0;\n let mut found: bool = false;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n grid.len() == n,\n forall|k: int| 0 <= k < grid.len() ==> #[trigger] grid[k].len() == m,\n forall|k: int, l: int| 0 <= k < grid.len() && 0 <= l < grid[k].len()\n ==> #[trigger] grid[k][l] == 0u8 || grid[k][l] == 1u8,\n found ==> min_r < n && max_r < n && min_c < m && max_c < m\n && min_r <= max_r && min_c <= max_c,\n !found ==> min_r == n && max_r == 0 && min_c == m && max_c == 0,\n found ==> exists|l: int| 0 <= l < m && #[trigger] grid@[min_r as int][l] == 1u8,\n found ==> exists|l: int| 0 <= l < m && #[trigger] grid@[max_r as int][l] == 1u8,\n found ==> exists|k: int| 0 <= k < n && #[trigger] grid@[k][min_c as int] == 1u8,\n found ==> exists|k: int| 0 <= k < n && #[trigger] grid@[k][max_c as int] == 1u8,\n forall|k: int, l: int| 0 <= k < (i as int) && 0 <= l < m && #[trigger] grid@[k][l] == 1u8\n ==> min_r as int <= k && k <= max_r as int\n && min_c as int <= l && l <= max_c as int && found,\n decreases n - i,\n {\n let mut j: usize = 0;\n while j < m\n invariant\n 0 <= i < n,\n 0 <= j <= m,\n grid.len() == n,\n forall|k: int| 0 <= k < grid.len() ==> #[trigger] grid[k].len() == m,\n forall|k: int, l: int| 0 <= k < grid.len() && 0 <= l < grid[k].len()\n ==> #[trigger] grid[k][l] == 0u8 || grid[k][l] == 1u8,\n found ==> min_r < n && max_r < n && min_c < m && max_c < m\n && min_r <= max_r && min_c <= max_c,\n !found ==> min_r == n && max_r == 0 && min_c == m && max_c == 0,\n found ==> exists|l: int| 0 <= l < m && #[trigger] grid@[min_r as int][l] == 1u8,\n found ==> exists|l: int| 0 <= l < m && #[trigger] grid@[max_r as int][l] == 1u8,\n found ==> exists|k: int| 0 <= k < n && #[trigger] grid@[k][min_c as int] == 1u8,\n found ==> exists|k: int| 0 <= k < n && #[trigger] grid@[k][max_c as int] == 1u8,\n forall|k: int, l: int| 0 <= k < (i as int) && 0 <= l < m && #[trigger] grid@[k][l] == 1u8\n ==> min_r as int <= k && k <= max_r as int\n && min_c as int <= l && l <= max_c as int && found,\n forall|l: int| 0 <= l < (j as int) && #[trigger] grid@[i as int][l] == 1u8\n ==> min_r as int <= i as int && i as int <= max_r as int\n && min_c as int <= l && l <= max_c as int && found,\n decreases m - j,\n {\n assert(grid[i as int].len() == m);\n if grid[i][j] == 1u8 {\n if !found {\n min_r = i;\n max_r = i;\n min_c = j;\n max_c = j;\n found = true;\n } else {\n if i < min_r { min_r = i; }\n if i > max_r { max_r = i; }\n if j < min_c { min_c = j; }\n if j > max_c { max_c = j; }\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n (min_r, max_r, min_c, max_c)\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn bounding_box(grid: &Vec>, n: usize, m: usize) -> (usize, usize, usize, usize) {\n let mut min_r: usize = n;\n let mut max_r: usize = 0;\n let mut min_c: usize = m;\n let mut max_c: usize = 0;\n let mut found: bool = false;\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = 0;\n while j < m {\n if grid[i][j] == 1u8 {\n if !found {\n min_r = i;\n max_r = i;\n min_c = j;\n max_c = j;\n found = true;\n } else {\n if i < min_r { min_r = i; }\n if i > max_r { max_r = i; }\n if j < min_c { min_c = j; }\n if j > max_c { max_c = j; }\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n (min_r, max_r, min_c, max_c)\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let m: usize = iter.next().unwrap().parse().unwrap();\n let mut grid: Vec> = Vec::with_capacity(n);\n let mut raw_grid: Vec> = Vec::with_capacity(n);\n for _ in 0..n {\n let s = iter.next().unwrap();\n let row: Vec = s.bytes().map(|b| if b == b'*' { 1u8 } else { 0u8 }).collect();\n let raw_row: Vec = s.bytes().collect();\n grid.push(row);\n raw_grid.push(raw_row);\n }\n let (min_r, max_r, min_c, max_c) = Solution::bounding_box(&grid, n, m);\n for r in min_r..=max_r {\n for c in min_c..=max_c {\n out.write_all(&[raw_grid[r][c]]).unwrap();\n }\n writeln!(out).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf14A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf14A/tests/mutated_testcases.jsonl"} {"problem": "cf1535A", "family": "codeforces", "description": "# Fair Playoff\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nFour players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.\n\nIt is known that in a match between two players, the one whose skill is greater will win. The skill of the $$$i$$$-th player is equal to $$$s_i$$$ and all skill levels are pairwise different (i.\u2009e. there are no two identical values in the array $$$s$$$).\n\nThe tournament is called **fair** if the two players with the highest skills meet in the finals.\n\nDetermine whether the given tournament is **fair**.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases.\n\nA single line of test case contains four integers $$$s_1, s_2, s_3, s_4$$$ ($$$1 \\le s_i \\le 100$$$)\u00a0\u2014 skill of the players. It is guaranteed that all the numbers in the array are different.\n\n## Output\n\nFor each testcase, output `YES` if the tournament is **fair**, or `NO` otherwise.\n\n## Example\n\n**Input:**\n```\n4\n3 7 9 5\n4 5 6 9\n5 3 8 1\n6 5 3 2\n```\n**Output:**\n```\nYES\nNO\nYES\nNO\n```\n\n## Note\n\nConsider the example:\n - in the first test case, players $$$2$$$ and $$$3$$$ with skills $$$7$$$ and $$$9$$$ advance to the finals; \n- in the second test case, players $$$2$$$ and $$$4$$$ with skills $$$5$$$ and $$$9$$$ advance to the finals. The player with skill $$$6$$$ does not advance, but the player with skill $$$5$$$ advances to the finals, so the tournament is not fair; \n- in the third test case, players $$$1$$$ and $$$3$$$ with skills $$$5$$$ and $$$8$$$ advance to the finals; \n- in the fourth test case, players $$$1$$$ and $$$3$$$ with skills $$$6$$$ and $$$3$$$ advance to the finals. The player with skill $$$5$$$ does not advance, but the player with skill $$$3$$$ advances to the finals, so the tournament is not fair.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn fair_playoff(s1: i64, s2: i64, s3: i64, s4: i64) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let t: usize = tokens.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case_idx: usize = 0;\n while case_idx < t {\n let s1: i64 = tokens.next().expect(\"s1\").parse().expect(\"valid s1\");\n let s2: i64 = tokens.next().expect(\"s2\").parse().expect(\"valid s2\");\n let s3: i64 = tokens.next().expect(\"s3\").parse().expect(\"valid s3\");\n let s4: i64 = tokens.next().expect(\"s4\").parse().expect(\"valid s4\");\n if Solution::fair_playoff(s1, s2, s3, s4) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n case_idx += 1;\n }\n}\n```\n", "tags": "800\nbrute force, implementation\n56802\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max2(a: i64, b: i64) -> i64 {\n if a >= b { a } else { b }\n }\n\n pub open spec fn min2(a: i64, b: i64) -> i64 {\n if a <= b { a } else { b }\n }\n\n pub open spec fn fair_playoff_spec(s1: i64, s2: i64, s3: i64, s4: i64) -> bool {\n Self::min2(Self::max2(s1, s2), Self::max2(s3, s4)) > Self::max2(Self::min2(s1, s2), Self::min2(s3, s4))\n }\n\n pub fn fair_playoff(s1: i64, s2: i64, s3: i64, s4: i64) -> (result: bool)\n requires\n 1 <= s1 <= 100,\n 1 <= s2 <= 100,\n 1 <= s3 <= 100,\n 1 <= s4 <= 100,\n s1 != s2,\n s1 != s3,\n s1 != s4,\n s2 != s3,\n s2 != s4,\n s3 != s4,\n ensures\n result == Self::fair_playoff_spec(s1, s2, s3, s4),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn fair_playoff(s1: i64, s2: i64, s3: i64, s4: i64) -> bool {\n let w1 = if s1 >= s2 { s1 } else { s2 };\n let w2 = if s3 >= s4 { s3 } else { s4 };\n let l1 = if s1 <= s2 { s1 } else { s2 };\n let l2 = if s3 <= s4 { s3 } else { s4 };\n let weaker_winner = if w1 <= w2 { w1 } else { w2 };\n let stronger_loser = if l1 >= l2 { l1 } else { l2 };\n weaker_winner > stronger_loser\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max2(a: i64, b: i64) -> i64 {\n if a >= b { a } else { b }\n }\n\n pub open spec fn min2(a: i64, b: i64) -> i64 {\n if a <= b { a } else { b }\n }\n\n pub open spec fn fair_playoff_spec(s1: i64, s2: i64, s3: i64, s4: i64) -> bool {\n Self::min2(Self::max2(s1, s2), Self::max2(s3, s4)) > Self::max2(Self::min2(s1, s2), Self::min2(s3, s4))\n }\n\n pub fn fair_playoff(s1: i64, s2: i64, s3: i64, s4: i64) -> (result: bool)\n requires\n 1 <= s1 <= 100,\n 1 <= s2 <= 100,\n 1 <= s3 <= 100,\n 1 <= s4 <= 100,\n s1 != s2,\n s1 != s3,\n s1 != s4,\n s2 != s3,\n s2 != s4,\n s3 != s4,\n ensures\n result == Self::fair_playoff_spec(s1, s2, s3, s4),\n {\n let w1 = if s1 >= s2 { s1 } else { s2 };\n let w2 = if s3 >= s4 { s3 } else { s4 };\n let l1 = if s1 <= s2 { s1 } else { s2 };\n let l2 = if s3 <= s4 { s3 } else { s4 };\n let weaker_winner = if w1 <= w2 { w1 } else { w2 };\n let stronger_loser = if l1 >= l2 { l1 } else { l2 };\n weaker_winner > stronger_loser\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max2(a: i64, b: i64) -> i64 {\n if a >= b { a } else { b }\n }\n\n pub open spec fn min2(a: i64, b: i64) -> i64 {\n if a <= b { a } else { b }\n }\n\n pub open spec fn fair_playoff_spec(s1: i64, s2: i64, s3: i64, s4: i64) -> bool {\n Self::min2(Self::max2(s1, s2), Self::max2(s3, s4)) > Self::max2(Self::min2(s1, s2), Self::min2(s3, s4))\n }\n\n pub fn fair_playoff(s1: i64, s2: i64, s3: i64, s4: i64) -> (result: bool)\n requires\n 1 <= s1 <= 100,\n 1 <= s2 <= 100,\n 1 <= s3 <= 100,\n 1 <= s4 <= 100,\n s1 != s2,\n s1 != s3,\n s1 != s4,\n s2 != s3,\n s2 != s4,\n s3 != s4,\n ensures\n result == Self::fair_playoff_spec(s1, s2, s3, s4),\n {\n let w1 = if s1 >= s2 { s1 } else { s2 };\n let w2 = if s3 >= s4 { s3 } else { s4 };\n let l1 = if s1 <= s2 { s1 } else { s2 };\n let l2 = if s3 <= s4 { s3 } else { s4 };\n let weaker_winner = if w1 <= w2 { w1 } else { w2 };\n let stronger_loser = if l1 >= l2 { l1 } else { l2 };\n weaker_winner > stronger_loser\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn fair_playoff(s1: i64, s2: i64, s3: i64, s4: i64) -> bool {\n let w1 = if s1 >= s2 { s1 } else { s2 };\n let w2 = if s3 >= s4 { s3 } else { s4 };\n let l1 = if s1 <= s2 { s1 } else { s2 };\n let l2 = if s3 <= s4 { s3 } else { s4 };\n let weaker_winner = if w1 <= w2 { w1 } else { w2 };\n let stronger_loser = if l1 >= l2 { l1 } else { l2 };\n weaker_winner > stronger_loser\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let t: usize = tokens.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case_idx: usize = 0;\n while case_idx < t {\n let s1: i64 = tokens.next().expect(\"s1\").parse().expect(\"valid s1\");\n let s2: i64 = tokens.next().expect(\"s2\").parse().expect(\"valid s2\");\n let s3: i64 = tokens.next().expect(\"s3\").parse().expect(\"valid s3\");\n let s4: i64 = tokens.next().expect(\"s4\").parse().expect(\"valid s4\");\n if Solution::fair_playoff(s1, s2, s3, s4) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n case_idx += 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1535A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1535A/tests/mutated_testcases.jsonl"} {"problem": "cf1547D", "family": "codeforces", "description": "# Co-growing Sequence\n\nTime limit: 2 seconds | Memory limit: 512 megabytes\n\nA sequence of non-negative integers $$$a_1, a_2, \\dots, a_n$$$ is called *growing* if for all $$$i$$$ from $$$1$$$ to $$$n - 1$$$ all ones (of binary representation) in $$$a_i$$$ are in the places of ones (of binary representation) in $$$a_{i + 1}$$$ (in other words, $$$a_i \\:\\&\\: a_{i + 1} = a_i$$$, where $$$\\&$$$ denotes bitwise AND). If $$$n = 1$$$ then the sequence is considered *growing* as well.\n\nFor example, the following four sequences are growing: \n - $$$[2, 3, 15, 175]$$$ \u2014 in binary it's $$$[10_2, 11_2, 1111_2, 10101111_2]$$$; \n- $$$[5]$$$ \u2014 in binary it's $$$[101_2]$$$; \n- $$$[1, 3, 7, 15]$$$ \u2014 in binary it's $$$[1_2, 11_2, 111_2, 1111_2]$$$; \n- $$$[0, 0, 0]$$$ \u2014 in binary it's $$$[0_2, 0_2, 0_2]$$$. \n\nThe following three sequences are non-growing: \n - $$$[3, 4, 5]$$$ \u2014 in binary it's $$$[11_2, 100_2, 101_2]$$$; \n- $$$[5, 4, 3]$$$ \u2014 in binary it's $$$[101_2, 100_2, 011_2]$$$; \n- $$$[1, 2, 4, 8]$$$ \u2014 in binary it's $$$[0001_2, 0010_2, 0100_2, 1000_2]$$$. \n\nConsider two sequences of non-negative integers $$$x_1, x_2, \\dots, x_n$$$ and $$$y_1, y_2, \\dots, y_n$$$. Let's call this pair of sequences *co-growing* if the sequence $$$x_1 \\oplus y_1, x_2 \\oplus y_2, \\dots, x_n \\oplus y_n$$$ is *growing* where $$$\\oplus$$$ denotes bitwise XOR.\n\nYou are given a sequence of integers $$$x_1, x_2, \\dots, x_n$$$. Find the lexicographically minimal sequence $$$y_1, y_2, \\dots, y_n$$$ such that sequences $$$x_i$$$ and $$$y_i$$$ are co-growing.\n\nThe sequence $$$a_1, a_2, \\dots, a_n$$$ is lexicographically smaller than the sequence $$$b_1, b_2, \\dots, b_n$$$ if there exists $$$1 \\le k \\le n$$$ such that $$$a_i = b_i$$$ for any $$$1 \\le i \\lt k$$$ but $$$a_k \\lt b_k$$$.\n\n## Input\n\nThe first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow.\n\nThe first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 length of the sequence $$$x_i$$$.\n\nThe second line contains $$$n$$$ integers $$$x_1, x_2, \\dots, x_n$$$ ($$$0 \\le x_i \\lt 2^{30}$$$) \u2014 elements of the sequence $$$x_i$$$.\n\nIt is guaranteed that the sum of $$$n$$$ overall all test cases doesn't exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, print $$$n$$$ integers $$$y_1, y_2, \\dots, y_n$$$ ($$$0 \\le y_i \\lt 2^{30}$$$) \u2014 lexicographically minimal sequence such that such that it's co-growing with given sequence $$$x_i$$$.\n\n## Example\n\n**Input:**\n```\n5\n4\n1 3 7 15\n4\n1 2 4 8\n5\n1 2 3 4 5\n4\n11 13 15 1\n1\n0\n```\n**Output:**\n```\n0 0 0 0 \n0 1 3 7 \n0 1 0 3 2 \n0 2 0 14 \n0\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn co_growing(n: usize, x: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let x: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let y = Solution::co_growing(n, x);\n let parts: Vec = y.iter().map(|v| v.to_string()).collect();\n writeln!(out, \"{}\", parts.join(\" \")).unwrap();\n }\n}\n```\n", "tags": "Rating: 1300\nbitmasks, constructive algorithms, greedy\nSolved by: 18491 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_co_growing(x: Seq, y: Seq, n: usize) -> bool {\n x.len() == n && y.len() == n &&\n forall|i: int| #![trigger x[i]] 0 <= i && i < n as int - 1 ==>\n (x[i] ^ y[i]) & (x[i + 1] ^ y[i + 1]) == (x[i] ^ y[i])\n}\n\npub open spec fn lex_le_u32(a: Seq, b: Seq) -> bool {\n a.len() == b.len() && (\n a =~= b || exists|p: int|\n 0 <= p < a.len()\n && a[p] < b[p]\n && forall|k: int| 0 <= k < p ==> a[k] == b[k]\n )\n}\n\npub open spec fn y_valid(x: Seq, y: Seq, n: usize) -> bool {\n y.len() == n\n && (forall|i: int| 0 <= i < n as int ==> (y[i] as int) < 1073741824)\n && is_co_growing(x, y, n)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn co_growing(n: usize, x: Vec) -> (y: Vec)\n requires\n 1 <= n && n <= 200000,\n x.len() == n,\n forall|i: int| 0 <= i && i < n ==> x@[i] < 1073741824,\n ensures\n y.len() == n,\n y_valid(x@, y@, n),\n forall|alt: Seq| y_valid(x@, alt, n) ==> lex_le_u32(y@, alt),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn co_growing(n: usize, x: Vec) -> Vec {\n let mut y: Vec = Vec::new();\n let mut z: u32 = x[0];\n let y0 = z ^ x[0];\n y.push(y0);\n let mut i: usize = 1;\n while i < n {\n let old_z = z;\n z = z | x[i];\n let yi = z ^ x[i];\n y.push(yi);\n i += 1;\n }\n y\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_co_growing(x: Seq, y: Seq, n: usize) -> bool {\n x.len() == n && y.len() == n &&\n forall|i: int| #![trigger x[i]] 0 <= i && i < n as int - 1 ==>\n (x[i] ^ y[i]) & (x[i + 1] ^ y[i + 1]) == (x[i] ^ y[i])\n}\n\npub open spec fn lex_le_u32(a: Seq, b: Seq) -> bool {\n a.len() == b.len() && (\n a =~= b || exists|p: int|\n 0 <= p < a.len()\n && a[p] < b[p]\n && forall|k: int| 0 <= k < p ==> a[k] == b[k]\n )\n}\n\npub open spec fn y_valid(x: Seq, y: Seq, n: usize) -> bool {\n y.len() == n\n && (forall|i: int| 0 <= i < n as int ==> (y[i] as int) < 1073741824)\n && is_co_growing(x, y, n)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn co_growing(n: usize, x: Vec) -> (y: Vec)\n requires\n 1 <= n && n <= 200000,\n x.len() == n,\n forall|i: int| 0 <= i && i < n ==> x@[i] < 1073741824,\n ensures\n y.len() == n,\n y_valid(x@, y@, n),\n forall|alt: Seq| y_valid(x@, alt, n) ==> lex_le_u32(y@, alt),\n {\n let mut y: Vec = Vec::new();\n let mut z: u32 = x[0];\n let y0 = z ^ x[0];\n y.push(y0);\n let mut i: usize = 1;\n while i < n {\n let old_z = z;\n z = z | x[i];\n let yi = z ^ x[i];\n y.push(yi);\n i += 1;\n }\n y\n }\n}\n\n}\n", "verified": "use vstd::arithmetic::div_mod::lemma_small_mod;\nuse vstd::arithmetic::power2::{lemma2_to64, pow2};\nuse vstd::bits::{lemma_low_bits_mask_values, lemma_u32_low_bits_mask_is_mod, low_bits_mask};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_co_growing(x: Seq, y: Seq, n: usize) -> bool {\n x.len() == n && y.len() == n &&\n forall|i: int| #![trigger x[i]] 0 <= i && i < n as int - 1 ==>\n (x[i] ^ y[i]) & (x[i + 1] ^ y[i + 1]) == (x[i] ^ y[i])\n}\n\npub open spec fn lex_le_u32(a: Seq, b: Seq) -> bool {\n a.len() == b.len() && (\n a =~= b || exists|p: int|\n 0 <= p < a.len()\n && a[p] < b[p]\n && forall|k: int| 0 <= k < p ==> a[k] == b[k]\n )\n}\n\npub open spec fn y_valid(x: Seq, y: Seq, n: usize) -> bool {\n y.len() == n\n && (forall|i: int| 0 <= i < n as int ==> (y[i] as int) < 1073741824)\n && is_co_growing(x, y, n)\n}\n\npub open spec fn prefix_or(x: Seq, len: int) -> u32\n recommends 0 <= len <= x.len()\n decreases len\n{\n if len <= 0 {\n 0u32\n } else {\n prefix_or(x, len - 1) | x[len - 1]\n }\n}\n\nproof fn lemma_bv_init(a: u32)\n ensures a ^ (a ^ a) == a\n{\n assert(a ^ (a ^ a) == a) by (bit_vector);\n}\n\nproof fn lemma_bv_and_or(a: u32, b: u32)\n ensures a & (a | b) == a,\n{\n assert(a & (a | b) == a) by (bit_vector);\n}\n\nproof fn lemma_bv_xor_or_xor(z: u32, xi: u32)\n ensures xi ^ ((z | xi) ^ xi) == z | xi,\n{\n assert(xi ^ ((z | xi) ^ xi) == z | xi) by (bit_vector);\n}\n\nproof fn lemma_bv_xor_cancel(a: u32, b: u32)\n ensures a ^ (a ^ b) == b,\n{\n assert(a ^ (a ^ b) == b) by (bit_vector);\n}\n\nproof fn lemma_bv_xor_cancel_rev(a: u32, b: u32)\n ensures a ^ (b ^ a) == b,\n{\n assert(a ^ (b ^ a) == b) by (bit_vector);\n}\n\nproof fn lemma_bv_or_le_max30(a: u32, b: u32)\n requires\n (a as int) < 1073741824int,\n (b as int) < 1073741824int,\n ensures\n ((a | b) as int) < 1073741824int,\n{\n lemma_bv_u30_to_mask(a);\n lemma_bv_u30_to_mask(b);\n assert(((a | b) & 0x3FFFFFFFu32) == (a | b)) by (bit_vector)\n requires\n a & 0x3FFFFFFFu32 == a,\n b & 0x3FFFFFFFu32 == b,\n ;\n lemma_bv_mask_to_u30(a | b);\n}\n\nproof fn lemma_bv_and_le_right(a: u32, b: u32)\n ensures\n (a & b) <= b,\n{\n assert((a & b) <= b) by (bit_vector);\n}\n\nproof fn lemma_bv_and_le_left(a: u32, b: u32)\n ensures\n (a & b) <= a,\n{\n assert((a & b) <= a) by (bit_vector);\n}\n\nproof fn lemma_bv_xor_le_max30(a: u32, b: u32)\n requires\n (a as int) < 1073741824int,\n (b as int) < 1073741824int,\n ensures\n ((a ^ b) as int) < 1073741824int,\n{\n lemma_bv_u30_to_mask(a);\n lemma_bv_u30_to_mask(b);\n assert(((a ^ b) & 0x3FFFFFFFu32) == (a ^ b)) by (bit_vector)\n requires\n a & 0x3FFFFFFFu32 == a,\n b & 0x3FFFFFFFu32 == b,\n ;\n lemma_bv_mask_to_u30(a ^ b);\n}\n\nproof fn lemma_bv_xor_self_zero(a: u32)\n ensures\n a ^ a == 0u32,\n{\n assert(a ^ a == 0u32) by (bit_vector);\n}\n\nproof fn lemma_bv_zero_or(a: u32)\n ensures\n 0u32 | a == a,\n{\n assert(0u32 | a == a) by (bit_vector);\n}\n\nproof fn lemma_bv_u30_to_mask(a: u32)\n requires\n (a as int) < 1073741824int,\n ensures\n a & 0x3FFFFFFFu32 == a,\n{\n lemma_low_bits_mask_values();\n lemma2_to64();\n lemma_u32_low_bits_mask_is_mod(a, 30);\n lemma_small_mod(a as nat, pow2(30) as nat);\n assert(low_bits_mask(30) == 0x3FFFFFFF);\n assert(pow2(30) == 0x40000000);\n assert(a % 0x40000000u32 == a);\n assert((a & (low_bits_mask(30) as u32)) == a % (pow2(30) as u32));\n assert(a & 0x3FFFFFFFu32 == a);\n}\n\nproof fn lemma_bv_mask_to_u30(a: u32)\n requires\n a & 0x3FFFFFFFu32 == a,\n ensures\n (a as int) < 1073741824int,\n{\n lemma_low_bits_mask_values();\n lemma2_to64();\n lemma_u32_low_bits_mask_is_mod(a, 30);\n assert(low_bits_mask(30) == 0x3FFFFFFF);\n assert(pow2(30) == 0x40000000);\n assert((a & (low_bits_mask(30) as u32)) == a % (pow2(30) as u32));\n assert(a % 0x40000000u32 == a);\n lemma_small_mod((a % 0x40000000u32) as nat, 0x40000000nat);\n assert((a % 0x40000000u32) < 0x40000000u32);\n assert(a < 0x40000000u32);\n assert((a as int) < 1073741824int);\n}\n\nproof fn lemma_bv_y_is_masked_prev(z: u32, xi: u32)\n ensures\n ((z | xi) ^ xi) == (z & (!xi)),\n{\n assert(((z | xi) ^ xi) == (z & (!xi))) by (bit_vector);\n}\n\nproof fn lemma_bv_local_shape(z: u32, xi: u32, alt: u32)\n requires\n z & (xi ^ alt) == z,\n ensures\n ((z | xi) ^ xi) == (z & alt),\n{\n assert(((z | xi) ^ xi) == (z & alt)) by (bit_vector)\n requires\n z & (xi ^ alt) == z,\n ;\n}\n\nproof fn lemma_prefix_or_bound(x: Seq, len: int)\n requires\n 0 <= len <= x.len(),\n forall|j: int| 0 <= j < len ==> x[j] < 1073741824u32,\n ensures\n prefix_or(x, len) < 1073741824u32,\n decreases len,\n{\n if len > 0 {\n lemma_prefix_or_bound(x, len - 1);\n lemma_bv_or_le_max30(prefix_or(x, len - 1), x[len - 1]);\n }\n}\n\nproof fn lemma_prefix_or_step(x: Seq, len: int)\n requires\n 0 < len <= x.len(),\n ensures\n prefix_or(x, len) == prefix_or(x, len - 1) | x[len - 1],\n{\n reveal_with_fuel(prefix_or, 1);\n}\n\nproof fn lemma_local_min(z: u32, xi: u32, alt: u32)\n requires\n (z as int) < 1073741824int,\n (xi as int) < 1073741824int,\n z & (xi ^ alt) == z,\n ensures\n (((z | xi) ^ xi) as int) < 1073741824int,\n ((z | xi) ^ xi) <= alt,\n{\n lemma_bv_local_shape(z, xi, alt);\n lemma_bv_u30_to_mask(z);\n assert((((z | xi) ^ xi) & 0x3FFFFFFFu32) == ((z | xi) ^ xi)) by (bit_vector)\n requires\n ((z | xi) ^ xi) == (z & alt),\n z & 0x3FFFFFFFu32 == z,\n ;\n lemma_bv_mask_to_u30((z | xi) ^ xi);\n lemma_bv_and_le_right(z, alt);\n assert(((z | xi) ^ xi) <= alt);\n}\n\nproof fn lemma_lex_le_u32_witness(a: Seq, b: Seq, p: int)\n requires\n a.len() == b.len(),\n 0 <= p < a.len(),\n a[p] < b[p],\n forall|k: int| 0 <= k < p ==> a[k] == b[k],\n ensures\n lex_le_u32(a, b),\n{\n assert(exists|w: int|\n 0 <= w < a.len()\n && a[w] < b[w]\n && forall|k: int| 0 <= k < w ==> a[k] == b[k]\n ) by {\n assert(0 <= p < a.len());\n assert(a[p] < b[p]);\n assert(forall|k: int| 0 <= k < p ==> a[k] == b[k]);\n }\n}\n\n#[verifier::spinoff_prover]\nproof fn lemma_minimality(\n x: Seq,\n y: Seq,\n alt: Seq,\n n: usize,\n p: int,\n)\n requires\n x.len() == n,\n y.len() == n,\n alt.len() == n,\n y_valid(x, y, n),\n y_valid(x, alt, n),\n y[0] == 0u32,\n forall|j: int| 0 <= j < n as int ==> x[j] < 1073741824u32,\n forall|j: int| 0 < j < n as int ==> #[trigger] y[j] == ((x[j - 1] ^ y[j - 1]) | x[j]) ^ x[j],\n 0 <= p <= n as int,\n forall|k: int| 0 <= k < p ==> y[k] == alt[k],\n ensures\n lex_le_u32(y, alt),\n decreases n as int - p,\n{\n if p == n as int {\n assert(forall|k: int| 0 <= k < n as int ==> y[k] == alt[k]);\n assert(y =~= alt);\n } else if y[p] < alt[p] {\n lemma_lex_le_u32_witness(y, alt, p);\n } else if y[p] == alt[p] {\n lemma_minimality(x, y, alt, n, p + 1);\n } else {\n assert(alt[p] < y[p]);\n if p == 0 {\n assert(y[0] == 0u32);\n assert(0u32 <= alt[0]);\n assert(false);\n } else {\n lemma_bv_xor_le_max30(x[p - 1], y[p - 1]);\n assert(x[p] < 1073741824u32);\n assert(y[p - 1] == alt[p - 1]);\n assert(x[p - 1] ^ alt[p - 1] == x[p - 1] ^ y[p - 1]);\n assert((x[p - 1] ^ alt[p - 1]) & (x[p] ^ alt[p]) == (x[p - 1] ^ alt[p - 1]));\n assert((x[p - 1] ^ y[p - 1]) & (x[p] ^ alt[p]) == (x[p - 1] ^ y[p - 1]));\n lemma_local_min(x[p - 1] ^ y[p - 1], x[p], alt[p]);\n assert(y[p] == ((x[p - 1] ^ y[p - 1]) | x[p]) ^ x[p]);\n assert(y[p] <= alt[p]);\n assert(false);\n }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n #[verifier::spinoff_prover]\n pub fn co_growing(n: usize, x: Vec) -> (y: Vec)\n requires\n 1 <= n && n <= 200000,\n x.len() == n,\n forall|i: int| 0 <= i && i < n ==> x@[i] < 1073741824,\n ensures\n y.len() == n,\n y_valid(x@, y@, n),\n forall|alt: Seq| y_valid(x@, alt, n) ==> lex_le_u32(y@, alt),\n {\n let mut y: Vec = Vec::new();\n let mut z: u32 = x[0];\n let y0 = z ^ x[0];\n proof {\n lemma_bv_init(z);\n assert(y0 == x[0] ^ x[0]);\n lemma_bv_xor_self_zero(x[0]);\n assert(y0 == 0u32);\n }\n y.push(y0);\n proof {\n assert(y@[0] == y0);\n assert(y@[0] == 0u32);\n assert(y@[0] < 1073741824u32);\n assert(x@[0] ^ y@[0] == z);\n }\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= i && i <= n,\n n <= 200000,\n x.len() == n,\n y.len() == i,\n forall|j: int| 0 <= j && j < n ==> x@[j] < 1073741824,\n (z as int) < 1073741824int,\n y@[0] == 0u32,\n forall|j: int| 0 <= j < i as int ==> y@[j] < 1073741824u32,\n x@[i as int - 1] ^ y@[i as int - 1] == z,\n forall|j: int| 0 < j < i as int ==> #[trigger] y@[j] == ((x@[j - 1] ^ y@[j - 1]) | x@[j]) ^ x@[j],\n forall|j: int| #![trigger x@[j]] 0 <= j && j < i as int - 1 ==>\n (x@[j] ^ y@[j]) & (x@[j + 1] ^ y@[j + 1]) == (x@[j] ^ y@[j]),\n decreases n - i\n {\n let ghost xi = x[i as int];\n let old_z = z;\n z = z | x[i];\n let yi = z ^ x[i];\n proof {\n lemma_bv_or_le_max30(old_z, x@[i as int]);\n assert(xi == x@[i as int]);\n assert(z == old_z | xi);\n assert(yi == z ^ xi);\n lemma_bv_xor_or_xor(old_z, x@[i as int]);\n assert((z as int) < 1073741824int);\n lemma_bv_y_is_masked_prev(old_z, xi);\n assert(yi == old_z & (!xi));\n lemma_bv_u30_to_mask(old_z);\n assert((yi & 0x3FFFFFFFu32) == yi) by (bit_vector)\n requires\n yi == old_z & (!xi),\n old_z & 0x3FFFFFFFu32 == old_z,\n ;\n lemma_bv_mask_to_u30(yi);\n assert(yi < 1073741824u32);\n }\n y.push(yi);\n proof {\n assert(y@[i as int] == yi);\n assert forall|j: int| 0 <= j < i as int + 1 implies y@[j] < 1073741824u32 by {\n if j < i as int {\n } else {\n assert(j == i as int);\n assert(y@[j] == yi);\n }\n }\n assert(x@[i as int] ^ y@[i as int] == z);\n assert forall|j: int| 0 < j < i as int + 1 implies #[trigger] y@[j] == ((x@[j - 1] ^ y@[j - 1]) | x@[j]) ^ x@[j] by {\n if j < i as int {\n } else {\n assert(j == i as int);\n assert(y@[j] == yi);\n assert(x@[j - 1] ^ y@[j - 1] == old_z);\n }\n }\n assert forall|j: int| #![trigger x@[j]] 0 <= j && j < i as int ==>\n (x@[j] ^ y@[j]) & (x@[j + 1] ^ y@[j + 1]) == (x@[j] ^ y@[j]) by {\n if 0 <= j && j < i as int {\n if j < i as int - 1 {\n } else {\n assert(i as int - 1 <= j);\n assert(j < i as int);\n assert(j == i as int - 1);\n assert(x@[j] ^ y@[j] == old_z);\n assert(x@[j + 1] ^ y@[j + 1] == z);\n lemma_bv_and_or(old_z, x@[i as int]);\n }\n }\n }\n }\n i += 1;\n }\n proof {\n assert(i == n);\n assert(y.len() == n);\n assert(y_valid(x@, y@, n));\n assert forall|alt: Seq| y_valid(x@, alt, n) implies lex_le_u32(y@, alt) by {\n lemma_minimality(x@, y@, alt, n, 0);\n }\n }\n y\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn co_growing(n: usize, x: Vec) -> Vec {\n let mut y: Vec = Vec::new();\n let mut z: u32 = x[0];\n let y0 = z ^ x[0];\n y.push(y0);\n let mut i: usize = 1;\n while i < n {\n let old_z = z;\n z = z | x[i];\n let yi = z ^ x[i];\n y.push(yi);\n i += 1;\n }\n y\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let x: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let y = Solution::co_growing(n, x);\n let parts: Vec = y.iter().map(|v| v.to_string()).collect();\n writeln!(out, \"{}\", parts.join(\" \")).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1547D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1547D/tests/mutated_testcases.jsonl"} {"problem": "cf158A", "family": "codeforces", "description": "# Next Round\n\nTime limit: 3 seconds | Memory limit: 256 megabytes\n\n\"Contestant who earns a score equal to or greater than the $k$-th place finisher's score will advance to the next round, as long as the contestant earns a positive score...\" \u2014 an excerpt from contest rules.\n\nA total of $n$ participants took part in the contest ($n\u2009\u2265\u2009k$), and you already know their scores. Calculate how many participants will advance to the next round.\n\n## Input\n\nThe first line of the input contains two integers $n$ and $k$ ($1\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u200950$) separated by a single space.\n\nThe second line contains $n$ space-separated integers $a_1,\u2009a_2,\u2009...,\u2009a_n$ ($0\u2009\u2264\u2009a_i\u2009\u2264\u2009100$), where $a_i$ is the score earned by the participant who got the $i$-th place. The given sequence is non-increasing (that is, for all $i$ from $1$ to $n\u2009-\u20091$ the following condition is fulfilled: $a_i\u2009\u2265\u2009a_i\u2009+\u20091$).\n\n## Output\n\nOutput the number of participants who advance to the next round.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n8 5\n10 9 8 7 7 7 5 5\n```\n**Output:**\n```\n6\n```\n\n### Example 2\n\n**Input:**\n```\n4 2\n0 0 0 0\n```\n**Output:**\n```\n0\n```\n\n## Note\n\nIn the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.\n\nIn the second example nobody got a positive score.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_advancing(scores: Vec, k: usize) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n let mut scores: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n scores.push(it.next().unwrap().parse().unwrap());\n i += 1;\n }\n let answer = Solution::count_advancing(scores, k);\n println!(\"{}\", answer);\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: many users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn advances(score: int, threshold: int) -> bool {\n score >= threshold && score > 0\n}\n\npub open spec fn count_advancing_to(scores: Seq, threshold: int, end: int) -> nat\n recommends\n 0 <= end <= scores.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n count_advancing_to(scores, threshold, end - 1)\n + if advances(scores[end - 1] as int, threshold) {\n 1nat\n } else {\n 0nat\n }\n }\n}\n\npub open spec fn count_advancing(scores: Seq, k: int) -> nat\n recommends\n 1 <= k <= scores.len(),\n{\n count_advancing_to(scores, scores[k - 1] as int, scores.len() as int)\n}\n\nimpl Solution {\n pub fn count_advancing(scores: Vec, k: usize) -> (result: usize)\n requires\n 1 <= scores.len() <= 50,\n 1 <= k <= scores.len(),\n forall |i: int| 0 <= i < scores.len() ==> 0 <= #[trigger] scores[i] <= 100,\n forall |i: int| 0 <= i < scores.len() - 1 ==> #[trigger] scores[i] >= scores[i + 1],\n ensures\n result as int == count_advancing(scores@, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_advancing(scores: Vec, k: usize) -> usize {\n let threshold = scores[k - 1];\n let mut count = 0usize;\n let mut i = 0usize;\n while i < scores.len() {\n if scores[i] >= threshold && scores[i] > 0 {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn advances(score: int, threshold: int) -> bool {\n score >= threshold && score > 0\n}\n\npub open spec fn count_advancing_to(scores: Seq, threshold: int, end: int) -> nat\n recommends\n 0 <= end <= scores.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n count_advancing_to(scores, threshold, end - 1)\n + if advances(scores[end - 1] as int, threshold) {\n 1nat\n } else {\n 0nat\n }\n }\n}\n\npub open spec fn count_advancing(scores: Seq, k: int) -> nat\n recommends\n 1 <= k <= scores.len(),\n{\n count_advancing_to(scores, scores[k - 1] as int, scores.len() as int)\n}\n\nimpl Solution {\n pub fn count_advancing(scores: Vec, k: usize) -> (result: usize)\n requires\n 1 <= scores.len() <= 50,\n 1 <= k <= scores.len(),\n forall |i: int| 0 <= i < scores.len() ==> 0 <= #[trigger] scores[i] <= 100,\n forall |i: int| 0 <= i < scores.len() - 1 ==> #[trigger] scores[i] >= scores[i + 1],\n ensures\n result as int == count_advancing(scores@, k as int),\n {\n let threshold = scores[k - 1];\n let mut count = 0usize;\n let mut i = 0usize;\n while i < scores.len() {\n if scores[i] >= threshold && scores[i] > 0 {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn advances(score: int, threshold: int) -> bool {\n score >= threshold && score > 0\n}\n\npub open spec fn count_advancing_to(scores: Seq, threshold: int, end: int) -> nat\n recommends\n 0 <= end <= scores.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n count_advancing_to(scores, threshold, end - 1)\n + if advances(scores[end - 1] as int, threshold) {\n 1nat\n } else {\n 0nat\n }\n }\n}\n\npub open spec fn count_advancing(scores: Seq, k: int) -> nat\n recommends\n 1 <= k <= scores.len(),\n{\n count_advancing_to(scores, scores[k - 1] as int, scores.len() as int)\n}\n\nimpl Solution {\n pub fn count_advancing(scores: Vec, k: usize) -> (result: usize)\n requires\n 1 <= scores.len() <= 50,\n 1 <= k <= scores.len(),\n forall |i: int| 0 <= i < scores.len() ==> 0 <= #[trigger] scores[i] <= 100,\n forall |i: int| 0 <= i < scores.len() - 1 ==> #[trigger] scores[i] >= scores[i + 1],\n ensures\n result as int == count_advancing(scores@, k as int),\n {\n let threshold = scores[k - 1];\n let mut count = 0usize;\n let mut i = 0usize;\n while i < scores.len()\n invariant\n i <= scores.len(),\n count <= i,\n count as int == count_advancing_to(scores@, threshold as int, i as int),\n decreases\n scores.len() - i,\n {\n proof {\n reveal_with_fuel(count_advancing_to, 2);\n }\n if scores[i] >= threshold && scores[i] > 0 {\n count += 1;\n }\n i += 1;\n }\n proof {\n assert(threshold == scores@[k as int - 1]);\n }\n count\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_advancing(scores: Vec, k: usize) -> usize {\n let threshold = scores[k - 1];\n let mut count = 0usize;\n let mut i = 0usize;\n while i < scores.len() {\n if scores[i] >= threshold && scores[i] > 0 {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n let mut scores: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n scores.push(it.next().unwrap().parse().unwrap());\n i += 1;\n }\n let answer = Solution::count_advancing(scores, k);\n println!(\"{}\", answer);\n}\n", "checker": null, "testcases_path": "codeforces/cf158A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf158A/tests/mutated_testcases.jsonl"} {"problem": "cf1598B", "family": "codeforces", "description": "# Groups\n\nTime limit: 4 seconds | Memory limit: 256 megabytes\n\n$$$n$$$ students attended the first meeting of the Berland SU programming course ($$$n$$$ is even). All students will be divided into two groups. Each group will be attending exactly one lesson each week during one of the five working days (Monday, Tuesday, Wednesday, Thursday and Friday), and the days chosen for the groups must be different. Furthermore, both groups should contain the same number of students.\n\nEach student has filled a survey in which they told which days of the week are convenient for them to attend a lesson, and which are not. \n\nYour task is to determine if it is possible to choose two different week days to schedule the lessons for the group (the first group will attend the lesson on the first chosen day, the second group will attend the lesson on the second chosen day), and divide the students into two groups, so the groups have equal sizes, and for each student, the chosen lesson day for their group is convenient.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases.\n\nThen the descriptions of $$$t$$$ testcases follow.\n\nThe first line of each testcase contains one integer $$$n$$$ ($$$2 \\le n \\le 1\\,000$$$)\u00a0\u2014 the number of students.\n\nThe $$$i$$$-th of the next $$$n$$$ lines contains $$$5$$$ integers, each of them is $$$0$$$ or $$$1$$$. If the $$$j$$$-th integer is $$$1$$$, then the $$$i$$$-th student can attend the lessons on the $$$j$$$-th day of the week. If the $$$j$$$-th integer is $$$0$$$, then the $$$i$$$-th student cannot attend the lessons on the $$$j$$$-th day of the week. \n\nAdditional constraints on the input: for each student, at least one of the days of the week is convenient, the total number of students over all testcases doesn't exceed $$$10^5$$$.\n\n## Output\n\nFor each testcase print an answer. If it's possible to divide the students into two groups of equal sizes and choose different days for the groups so each student can attend the lesson in the chosen day of their group, print \"`YES`\" (without quotes). Otherwise, print \"`NO`\" (without quotes).\n\n## Example\n\n**Input:**\n```\n2\n4\n1 0 0 1 0\n0 1 0 0 1\n0 0 0 1 0\n0 1 0 1 0\n2\n0 0 0 1 0\n0 0 0 1 0\n```\n**Output:**\n```\nYES\nNO\n```\n\n## Note\n\nIn the first testcase, there is a way to meet all the constraints. For example, the first group can consist of the first and the third students, they will attend the lessons on Thursday (the fourth day); the second group can consist of the second and the fourth students, and they will attend the lessons on Tuesday (the second day).\n\nIn the second testcase, it is impossible to divide the students into groups so they attend the lessons on different days.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn groups(n: usize, days: Vec>) -> bool {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(t_str)) = lines.next() {\n if let Ok(t) = t_str.trim().parse::() {\n for _ in 0..t {\n if let Some(Ok(n_str)) = lines.next() {\n let n: usize = n_str.trim().parse().unwrap();\n let mut days = Vec::new();\n for _ in 0..n {\n if let Some(Ok(line)) = lines.next() {\n let parts: Vec = line.split_whitespace().map(|s| s == \"1\").collect();\n days.push(parts);\n }\n }\n if Solution::groups(n, days) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n }\n }\n}\n```\n", "tags": "Rating: 1000\nbrute force, implementation\nSolved by: 16860 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn student_can_attend(days: Seq>, i: int, d: int) -> bool {\n days[i]@[d]\n}\n\npub open spec fn count_can_attend_one(days: Seq>, d: int, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else { count_can_attend_one(days, d, end - 1) + (if student_can_attend(days, end - 1, d) { 1int } else { 0int }) }\n}\n\npub open spec fn count_can_attend_either(days: Seq>, d1: int, d2: int, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else { count_can_attend_either(days, d1, d2, end - 1) + (if student_can_attend(days, end - 1, d1) || student_can_attend(days, end - 1, d2) { 1int } else { 0int }) }\n}\n\npub open spec fn is_valid_pair(n: usize, days: Seq>, d1: int, d2: int) -> bool {\n 0 <= d1 && d1 < d2 && d2 < 5 &&\n count_can_attend_either(days, d1, d2, n as int) == n as int &&\n count_can_attend_one(days, d1, n as int) >= (n / 2) as int &&\n count_can_attend_one(days, d2, n as int) >= (n / 2) as int\n}\n\npub open spec fn has_valid_pair(n: usize, days: Seq>) -> bool {\n exists|d1: int, d2: int| is_valid_pair(n, days, d1, d2)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn groups(n: usize, days: Vec>) -> (res: bool)\n requires\n 2 <= n && n <= 1000,\n n % 2 == 0,\n days.len() == n,\n forall|i: int| 0 <= i && i < n ==> days@[i].len() == 5,\n forall|i: int| 0 <= i && i < n ==>\n days@[i]@[0] || days@[i]@[1] || days@[i]@[2] || days@[i]@[3] || days@[i]@[4],\n ensures\n res == has_valid_pair(n, days@)\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn groups(n: usize, days: Vec>) -> bool {\n let mut found = false;\n let mut d1: usize = 0;\n while d1 < 5 && !found {\n let mut d2: usize = d1 + 1;\n while d2 < 5 && !found {\n let mut either: usize = 0;\n let mut can_d1: usize = 0;\n let mut can_d2: usize = 0;\n let mut i: usize = 0;\n while i < n {\n let c1 = days[i][d1];\n let c2 = days[i][d2];\n if c1 || c2 {\n either += 1;\n }\n if c1 {\n can_d1 += 1;\n }\n if c2 {\n can_d2 += 1;\n }\n i += 1;\n }\n \n if either == n && can_d1 >= n / 2 && can_d2 >= n / 2 {\n found = true;\n }\n d2 += 1;\n }\n d1 += 1;\n }\n found\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn student_can_attend(days: Seq>, i: int, d: int) -> bool {\n days[i]@[d]\n}\n\npub open spec fn count_can_attend_one(days: Seq>, d: int, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else { count_can_attend_one(days, d, end - 1) + (if student_can_attend(days, end - 1, d) { 1int } else { 0int }) }\n}\n\npub open spec fn count_can_attend_either(days: Seq>, d1: int, d2: int, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else { count_can_attend_either(days, d1, d2, end - 1) + (if student_can_attend(days, end - 1, d1) || student_can_attend(days, end - 1, d2) { 1int } else { 0int }) }\n}\n\npub open spec fn is_valid_pair(n: usize, days: Seq>, d1: int, d2: int) -> bool {\n 0 <= d1 && d1 < d2 && d2 < 5 &&\n count_can_attend_either(days, d1, d2, n as int) == n as int &&\n count_can_attend_one(days, d1, n as int) >= (n / 2) as int &&\n count_can_attend_one(days, d2, n as int) >= (n / 2) as int\n}\n\npub open spec fn has_valid_pair(n: usize, days: Seq>) -> bool {\n exists|d1: int, d2: int| is_valid_pair(n, days, d1, d2)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn groups(n: usize, days: Vec>) -> (res: bool)\n requires\n 2 <= n && n <= 1000,\n n % 2 == 0,\n days.len() == n,\n forall|i: int| 0 <= i && i < n ==> days@[i].len() == 5,\n forall|i: int| 0 <= i && i < n ==>\n days@[i]@[0] || days@[i]@[1] || days@[i]@[2] || days@[i]@[3] || days@[i]@[4],\n ensures\n res == has_valid_pair(n, days@)\n {\n let mut found = false;\n let mut d1: usize = 0;\n while d1 < 5 && !found {\n let mut d2: usize = d1 + 1;\n while d2 < 5 && !found {\n let mut either: usize = 0;\n let mut can_d1: usize = 0;\n let mut can_d2: usize = 0;\n let mut i: usize = 0;\n while i < n {\n let c1 = days[i][d1];\n let c2 = days[i][d2];\n if c1 || c2 {\n either += 1;\n }\n if c1 {\n can_d1 += 1;\n }\n if c2 {\n can_d2 += 1;\n }\n i += 1;\n }\n if either == n && can_d1 >= n / 2 && can_d2 >= n / 2 {\n found = true;\n }\n d2 += 1;\n }\n d1 += 1;\n }\n found\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn student_can_attend(days: Seq>, i: int, d: int) -> bool {\n days[i]@[d]\n}\n\npub open spec fn count_can_attend_one(days: Seq>, d: int, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else { count_can_attend_one(days, d, end - 1) + (if student_can_attend(days, end - 1, d) { 1int } else { 0int }) }\n}\n\npub open spec fn count_can_attend_either(days: Seq>, d1: int, d2: int, end: int) -> int\n decreases end\n{\n if end <= 0 { 0 }\n else { count_can_attend_either(days, d1, d2, end - 1) + (if student_can_attend(days, end - 1, d1) || student_can_attend(days, end - 1, d2) { 1int } else { 0int }) }\n}\n\npub open spec fn is_valid_pair(n: usize, days: Seq>, d1: int, d2: int) -> bool {\n 0 <= d1 && d1 < d2 && d2 < 5 &&\n count_can_attend_either(days, d1, d2, n as int) == n as int &&\n count_can_attend_one(days, d1, n as int) >= (n / 2) as int &&\n count_can_attend_one(days, d2, n as int) >= (n / 2) as int\n}\n\npub open spec fn has_valid_pair(n: usize, days: Seq>) -> bool {\n exists|d1: int, d2: int| is_valid_pair(n, days, d1, d2)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn groups(n: usize, days: Vec>) -> (res: bool)\n requires\n 2 <= n && n <= 1000,\n n % 2 == 0,\n days.len() == n,\n forall|i: int| 0 <= i && i < n ==> days@[i].len() == 5,\n forall|i: int| 0 <= i && i < n ==>\n days@[i]@[0] || days@[i]@[1] || days@[i]@[2] || days@[i]@[3] || days@[i]@[4],\n ensures\n res == has_valid_pair(n, days@)\n {\n let mut found = false;\n let mut d1: usize = 0;\n while d1 < 5 && !found\n invariant\n 0 <= (d1 as int) && (d1 as int) <= 5,\n days.len() == n,\n n % 2 == 0,\n forall|i: int| 0 <= i && i < n ==> days@[i].len() == 5,\n forall|i: int| 0 <= i && i < n ==>\n days@[i]@[0] || days@[i]@[1] || days@[i]@[2] || days@[i]@[3] || days@[i]@[4],\n found ==> has_valid_pair(n, days@),\n !found ==> forall|d_a: int, d_b: int| 0 <= d_a && d_a < (d1 as int) && d_a < d_b && d_b < 5 ==> !is_valid_pair(n, days@, d_a, d_b)\n decreases 5 - (d1 as int)\n {\n let mut d2: usize = d1 + 1;\n while d2 < 5 && !found\n invariant\n 0 <= (d1 as int) && (d1 as int) < 5,\n (d1 as int) + 1 <= (d2 as int) && (d2 as int) <= 5,\n days.len() == n,\n n % 2 == 0,\n forall|i: int| 0 <= i && i < n ==> days@[i].len() == 5,\n forall|i: int| 0 <= i && i < n ==>\n days@[i]@[0] || days@[i]@[1] || days@[i]@[2] || days@[i]@[3] || days@[i]@[4],\n found ==> has_valid_pair(n, days@),\n !found ==> forall|d_a: int, d_b: int| 0 <= d_a && d_a < (d1 as int) && d_a < d_b && d_b < 5 ==> !is_valid_pair(n, days@, d_a, d_b),\n !found ==> forall|d_b: int| (d1 as int) < d_b && d_b < (d2 as int) ==> !is_valid_pair(n, days@, d1 as int, d_b)\n decreases 5 - (d2 as int)\n {\n let mut either: usize = 0;\n let mut can_d1: usize = 0;\n let mut can_d2: usize = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= (d1 as int) && (d1 as int) < (d2 as int) && (d2 as int) < 5,\n 0 <= i && i <= n,\n either <= i,\n can_d1 <= i,\n can_d2 <= i,\n days.len() == n,\n n % 2 == 0,\n forall|k: int| 0 <= k && k < n ==> days@[k].len() == 5,\n forall|k: int| 0 <= k && k < n ==>\n days@[k]@[0] || days@[k]@[1] || days@[k]@[2] || days@[k]@[3] || days@[k]@[4],\n (either as int) == count_can_attend_either(days@, d1 as int, d2 as int, i as int),\n (can_d1 as int) == count_can_attend_one(days@, d1 as int, i as int),\n (can_d2 as int) == count_can_attend_one(days@, d2 as int, i as int)\n decreases n - i\n {\n let c1 = days[i][d1];\n let c2 = days[i][d2];\n if c1 || c2 {\n either += 1;\n }\n if c1 {\n can_d1 += 1;\n }\n if c2 {\n can_d2 += 1;\n }\n i += 1;\n }\n \n if either == n && can_d1 >= n / 2 && can_d2 >= n / 2 {\n found = true;\n proof {\n assert(is_valid_pair(n, days@, d1 as int, d2 as int));\n assert(has_valid_pair(n, days@));\n }\n }\n d2 += 1;\n }\n d1 += 1;\n }\n found\n }\n}\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn groups(n: usize, days: Vec>) -> bool {\n let mut found = false;\n let mut d1: usize = 0;\n while d1 < 5 && !found {\n let mut d2: usize = d1 + 1;\n while d2 < 5 && !found {\n let mut either: usize = 0;\n let mut can_d1: usize = 0;\n let mut can_d2: usize = 0;\n let mut i: usize = 0;\n while i < n {\n let c1 = days[i][d1];\n let c2 = days[i][d2];\n if c1 || c2 {\n either += 1;\n }\n if c1 {\n can_d1 += 1;\n }\n if c2 {\n can_d2 += 1;\n }\n i += 1;\n }\n \n if either == n && can_d1 >= n / 2 && can_d2 >= n / 2 {\n found = true;\n }\n d2 += 1;\n }\n d1 += 1;\n }\n found\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(t_str)) = lines.next() {\n if let Ok(t) = t_str.trim().parse::() {\n for _ in 0..t {\n if let Some(Ok(n_str)) = lines.next() {\n let n: usize = n_str.trim().parse().unwrap();\n let mut days = Vec::new();\n for _ in 0..n {\n if let Some(Ok(line)) = lines.next() {\n let parts: Vec = line.split_whitespace().map(|s| s == \"1\").collect();\n days.push(parts);\n }\n }\n if Solution::groups(n, days) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n }\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1598B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1598B/tests/mutated_testcases.jsonl"} {"problem": "cf1602C", "family": "codeforces", "description": "# Array Elimination\n\nTime limit: 2 seconds | Memory limit: 512 megabytes\n\nYou are given array $$$a_1, a_2, \\ldots, a_n$$$, consisting of non-negative integers.\n\nLet's define operation of \"elimination\" with integer parameter $$$k$$$ ($$$1 \\leq k \\leq n$$$) as follows:\n - Choose $$$k$$$ distinct array indices $$$1 \\leq i_1 \\lt i_2 \\lt \\ldots \\lt i_k \\le n$$$. \n- Calculate $$$x = a_{i_1} ~ \\& ~ a_{i_2} ~ \\& ~ \\ldots ~ \\& ~ a_{i_k}$$$, where $$$\\&$$$ denotes the bitwise AND operation (notes section contains formal definition). \n- Subtract $$$x$$$ from each of $$$a_{i_1}, a_{i_2}, \\ldots, a_{i_k}$$$; all other elements remain untouched. \n\nFind all possible values of $$$k$$$, such that it's possible to make all elements of array $$$a$$$ equal to $$$0$$$ using a finite number of elimination operations with parameter $$$k$$$. It can be proven that exists at least one possible $$$k$$$ for any array $$$a$$$.\n\nNote that you **firstly choose $$$k$$$ and only after that perform elimination operations with value $$$k$$$ you've chosen initially**.\n\n## Input\n\nEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$). Description of the test cases follows.\n\nThe first line of each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 200\\,000$$$)\u00a0\u2014 the length of array $$$a$$$.\n\nThe second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\lt 2^{30}$$$)\u00a0\u2014 array $$$a$$$ itself.\n\nIt's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200\\,000$$$.\n\n## Output\n\nFor each test case, print all values $$$k$$$, such that it's possible to make all elements of $$$a$$$ equal to $$$0$$$ in a finite number of elimination operations with the given parameter $$$k$$$.\n\n**Print them in increasing order.**\n\n## Example\n\n**Input:**\n```\n5\n4\n4 4 4 4\n4\n13 7 25 19\n6\n3 5 3 1 7 1\n1\n1\n5\n0 0 0 0 0\n```\n**Output:**\n```\n1 2 4\n1 2\n1\n1\n1 2 3 4 5\n```\n\n## Note\n\nIn the first test case:\n - If $$$k = 1$$$, we can make four elimination operations with sets of indices $$$\\{1\\}$$$, $$$\\{2\\}$$$, $$$\\{3\\}$$$, $$$\\{4\\}$$$. Since $$$\\&$$$ of one element is equal to the element itself, then for each operation $$$x = a_i$$$, so $$$a_i - x = a_i - a_i = 0$$$. \n- If $$$k = 2$$$, we can make two elimination operations with, for example, sets of indices $$$\\{1, 3\\}$$$ and $$$\\{2, 4\\}$$$: $$$x = a_1 ~ \\& ~ a_3$$$ $$$=$$$ $$$a_2 ~ \\& ~ a_4$$$ $$$=$$$ $$$4 ~ \\& ~ 4 = 4$$$. For both operations $$$x = 4$$$, so after the first operation $$$a_1 - x = 0$$$ and $$$a_3 - x = 0$$$, and after the second operation\u00a0\u2014 $$$a_2 - x = 0$$$ and $$$a_4 - x = 0$$$. \n- If $$$k = 3$$$, it's impossible to make all $$$a_i$$$ equal to $$$0$$$. After performing the first operation, we'll get three elements equal to $$$0$$$ and one equal to $$$4$$$. After that, all elimination operations won't change anything, since at least one chosen element will always be equal to $$$0$$$. \n- If $$$k = 4$$$, we can make one operation with set $$$\\{1, 2, 3, 4\\}$$$, because $$$x = a_1 ~ \\& ~ a_2 ~ \\& ~ a_3 ~ \\& ~ a_4$$$ $$$= 4$$$. \n\nIn the second test case, if $$$k = 2$$$ then we can make the following elimination operations:\n - Operation with indices $$$\\{1, 3\\}$$$: $$$x = a_1 ~ \\& ~ a_3$$$ $$$=$$$ $$$13 ~ \\& ~ 25 = 9$$$. $$$a_1 - x = 13 - 9 = 4$$$ and $$$a_3 - x = 25 - 9 = 16$$$. Array $$$a$$$ will become equal to $$$[4, 7, 16, 19]$$$. \n- Operation with indices $$$\\{3, 4\\}$$$: $$$x = a_3 ~ \\& ~ a_4$$$ $$$=$$$ $$$16 ~ \\& ~ 19 = 16$$$. $$$a_3 - x = 16 - 16 = 0$$$ and $$$a_4 - x = 19 - 16 = 3$$$. Array $$$a$$$ will become equal to $$$[4, 7, 0, 3]$$$. \n- Operation with indices $$$\\{2, 4\\}$$$: $$$x = a_2 ~ \\& ~ a_4$$$ $$$=$$$ $$$7 ~ \\& ~ 3 = 3$$$. $$$a_2 - x = 7 - 3 = 4$$$ and $$$a_4 - x = 3 - 3 = 0$$$. Array $$$a$$$ will become equal to $$$[4, 4, 0, 0]$$$. \n- Operation with indices $$$\\{1, 2\\}$$$: $$$x = a_1 ~ \\& ~ a_2$$$ $$$=$$$ $$$4 ~ \\& ~ 4 = 4$$$. $$$a_1 - x = 4 - 4 = 0$$$ and $$$a_2 - x = 4 - 4 = 0$$$. Array $$$a$$$ will become equal to $$$[0, 0, 0, 0]$$$. \n\n**Formal definition of bitwise AND:**\n\nLet's define bitwise AND ($$$\\&$$$) as follows. Suppose we have two non-negative integers $$$x$$$ and $$$y$$$, let's look at their binary representations (possibly, with leading zeroes): $$$x_k \\dots x_2 x_1 x_0$$$ and $$$y_k \\dots y_2 y_1 y_0$$$. Here, $$$x_i$$$ is the $$$i$$$-th bit of number $$$x$$$, and $$$y_i$$$ is the $$$i$$$-th bit of number $$$y$$$. Let $$$r = x ~ \\& ~ y$$$ is a result of operation $$$\\&$$$ on number $$$x$$$ and $$$y$$$. Then binary representation of $$$r$$$ will be $$$r_k \\dots r_2 r_1 r_0$$$, where:\n\n$$$$$$ r_i = \\begin{cases} 1, ~ \\text{if} ~ x_i = 1 ~ \\text{and} ~ y_i = 1 \\\\ 0, ~ \\text{if} ~ x_i = 0 ~ \\text{or} ~ y_i = 0 \\end{cases} $$$$$$\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn valid_k_values(n: usize, cnt: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case_id: usize = 0;\n\n while case_id < t {\n let n: usize = it.next().expect(\"n\").parse().expect(\"valid n\");\n let mut cnt: Vec = vec![0; 30];\n\n let mut i: usize = 0;\n while i < n {\n let x: i32 = it.next().expect(\"a_i\").parse().expect(\"valid a_i\");\n let mut b: usize = 0;\n while b < 30 {\n if ((x >> b) & 1) == 1 {\n cnt[b] = cnt[b] + 1;\n }\n b = b + 1;\n }\n i = i + 1;\n }\n\n let ans = Solution::valid_k_values(n, cnt);\n let mut j: usize = 0;\n while j < ans.len() {\n if j > 0 {\n print!(\" \");\n }\n print!(\"{}\", ans[j]);\n j = j + 1;\n }\n println!();\n\n case_id = case_id + 1;\n }\n}\n```\n", "tags": "Rating: 1300\nbitmasks, math, number theory\nSolved by: N/A users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn common_divisor(cnt: Seq, d: int) -> bool\n recommends\n cnt.len() == 30,\n {\n &&& 1 <= d\n &&& forall|b: int| 0 <= b < 30 ==> #[trigger] ((cnt[b] as int) % d) == 0\n }\n\n pub open spec fn valid_answer(n: int, cnt: Seq, ans: Seq) -> bool\n recommends\n cnt.len() == 30,\n n >= 1,\n {\n &&& forall|idx: int| 0 <= idx < ans.len() ==> 1 <= ans[idx] as int <= n && Self::common_divisor(cnt, ans[idx] as int)\n &&& forall|d: int| 1 <= d <= n && Self::common_divisor(cnt, d) ==> exists|idx: int| 0 <= idx < ans.len() && ans[idx] as int == d\n &&& forall|i: int, j: int| 0 <= i < j < ans.len() ==> #[trigger] ans[i] < #[trigger] ans[j]\n }\n\n pub fn valid_k_values(n: usize, cnt: Vec) -> (ans: Vec)\n requires\n 1 <= n <= 200_000,\n cnt.len() == 30,\n forall|i: int| 0 <= i < 30 ==> 0 <= #[trigger] cnt[i] <= n,\n ensures\n Self::valid_answer(n as int, cnt@, ans@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn valid_k_values(n: usize, cnt: Vec) -> Vec {\n let mut ans: Vec = Vec::new();\n let mut k: usize = 1;\n\n while k <= n {\n let k_i32: i32 = k as i32;\n let mut b: usize = 0;\n let mut bad_idx: i32 = -1;\n\n while b < 30 {\n if bad_idx == -1 && cnt[b] % k_i32 != 0 {\n bad_idx = b as i32;\n }\n b = b + 1;\n }\n\n if bad_idx == -1 {\n ans.push(k as i32);\n }\n\n k = k + 1;\n }\n\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn common_divisor(cnt: Seq, d: int) -> bool\n recommends\n cnt.len() == 30,\n {\n &&& 1 <= d\n &&& forall|b: int| 0 <= b < 30 ==> #[trigger] ((cnt[b] as int) % d) == 0\n }\n\n pub open spec fn valid_answer(n: int, cnt: Seq, ans: Seq) -> bool\n recommends\n cnt.len() == 30,\n n >= 1,\n {\n &&& forall|idx: int| 0 <= idx < ans.len() ==> 1 <= ans[idx] as int <= n && Self::common_divisor(cnt, ans[idx] as int)\n &&& forall|d: int| 1 <= d <= n && Self::common_divisor(cnt, d) ==> exists|idx: int| 0 <= idx < ans.len() && ans[idx] as int == d\n &&& forall|i: int, j: int| 0 <= i < j < ans.len() ==> #[trigger] ans[i] < #[trigger] ans[j]\n }\n\n pub fn valid_k_values(n: usize, cnt: Vec) -> (ans: Vec)\n requires\n 1 <= n <= 200_000,\n cnt.len() == 30,\n forall|i: int| 0 <= i < 30 ==> 0 <= #[trigger] cnt[i] <= n,\n ensures\n Self::valid_answer(n as int, cnt@, ans@),\n {\n let mut ans: Vec = Vec::new();\n let mut k: usize = 1;\n\n while k <= n {\n let k_i32: i32 = k as i32;\n let mut b: usize = 0;\n let mut bad_idx: i32 = -1;\n\n while b < 30 {\n if bad_idx == -1 && cnt[b] % k_i32 != 0 {\n bad_idx = b as i32;\n }\n b = b + 1;\n }\n\n if bad_idx == -1 {\n ans.push(k as i32);\n }\n\n k = k + 1;\n }\n\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn common_divisor(cnt: Seq, d: int) -> bool\n recommends\n cnt.len() == 30,\n {\n &&& 1 <= d\n &&& forall|b: int| 0 <= b < 30 ==> #[trigger] ((cnt[b] as int) % d) == 0\n }\n\n pub open spec fn valid_answer(n: int, cnt: Seq, ans: Seq) -> bool\n recommends\n cnt.len() == 30,\n n >= 1,\n {\n &&& forall|idx: int| 0 <= idx < ans.len() ==> 1 <= ans[idx] as int <= n && Self::common_divisor(cnt, ans[idx] as int)\n &&& forall|d: int| 1 <= d <= n && Self::common_divisor(cnt, d) ==> exists|idx: int| 0 <= idx < ans.len() && ans[idx] as int == d\n &&& forall|i: int, j: int| 0 <= i < j < ans.len() ==> #[trigger] ans[i] < #[trigger] ans[j]\n }\n\n pub fn valid_k_values(n: usize, cnt: Vec) -> (ans: Vec)\n requires\n 1 <= n <= 200_000,\n cnt.len() == 30,\n forall|i: int| 0 <= i < 30 ==> 0 <= #[trigger] cnt[i] <= n,\n ensures\n Self::valid_answer(n as int, cnt@, ans@),\n {\n let mut ans: Vec = Vec::new();\n let mut k: usize = 1;\n\n while k <= n\n invariant\n cnt.len() == 30,\n 1 <= n <= 200_000,\n 1 <= k <= n + 1,\n forall|i: int| 0 <= i < 30 ==> 0 <= #[trigger] cnt[i] <= n,\n forall|idx: int| 0 <= idx < ans.len() ==> 1 <= (ans[idx] as int) && (ans[idx] as int) < (k as int) && Self::common_divisor(cnt@, ans[idx] as int),\n forall|d: int| 1 <= d < k as int && #[trigger] Self::common_divisor(cnt@, d)\n ==> exists|idx: int| 0 <= idx < ans.len() && ans[idx] as int == d,\n forall|i: int, j: int| 0 <= i < j < ans.len() ==> #[trigger] ans[i] < #[trigger] ans[j],\n decreases\n n + 1 - k,\n {\n let k_i32: i32 = k as i32;\n let mut b: usize = 0;\n let mut bad_idx: i32 = -1;\n let ghost ans_before = ans@;\n\n while b < 30\n invariant\n cnt.len() == 30,\n 1 <= k <= n,\n 0 <= b <= 30,\n 1 <= k_i32,\n k_i32 as int == k as int,\n forall|i: int| 0 <= i < 30 ==> 0 <= #[trigger] cnt[i] <= n,\n bad_idx == -1 ==> forall|j: int| 0 <= j < b as int ==> #[trigger] ((cnt@[j] as int) % (k as int)) == 0,\n bad_idx != -1 ==> 0 <= bad_idx < b as int && (cnt@[bad_idx as int] as int) % (k as int) != 0,\n decreases\n 30 - b,\n {\n if bad_idx == -1 && cnt[b] % k_i32 != 0 {\n bad_idx = b as i32;\n }\n b = b + 1;\n }\n\n if bad_idx == -1 {\n proof {\n assert forall|j: int| 0 <= j < 30 implies #[trigger] ((cnt@[j] as int) % (k as int)) == 0 by {\n assert(0 <= j < b as int);\n }\n assert(Self::common_divisor(cnt@, k as int));\n }\n ans.push(k as i32);\n proof {\n assert(ans@ == ans_before.push(k as i32));\n assert forall|i: int, j: int| 0 <= i < j < ans@.len() implies #[trigger] ans@[i] < #[trigger] ans@[j] by {\n if j < ans_before.len() {\n assert(ans@[i] == ans_before[i]);\n assert(ans@[j] == ans_before[j]);\n } else {\n assert(j == ans_before.len());\n assert(ans@[j] == k as i32);\n assert(ans@[i] == ans_before[i]);\n }\n };\n }\n } else {\n proof {\n assert(0 <= bad_idx < 30);\n assert((cnt@[bad_idx as int] as int) % (k as int) != 0);\n assert(!Self::common_divisor(cnt@, k as int));\n assert(ans@ == ans_before);\n }\n }\n\n proof {\n assert forall|d: int| 1 <= d < (k as int + 1) && #[trigger] Self::common_divisor(cnt@, d)\n implies exists|idx: int| 0 <= idx < ans@.len() && ans@[idx] as int == d by {\n if d < k as int {\n let idx_w = choose|idx: int| 0 <= idx < ans_before.len() && ans_before[idx] as int == d;\n assert(0 <= idx_w < ans_before.len());\n assert(ans_before[idx_w] as int == d);\n if bad_idx == -1 {\n assert(ans@ == ans_before.push(k as i32));\n assert(ans@[idx_w] == ans_before[idx_w]);\n assert(0 <= idx_w < ans@.len());\n } else {\n assert(ans@ == ans_before);\n assert(0 <= idx_w < ans@.len());\n }\n assert(ans@[idx_w] as int == d);\n } else {\n assert(d == k as int);\n if bad_idx == -1 {\n let idx_new = ans@.len() - 1;\n assert(ans@.len() > 0);\n assert(0 <= idx_new < ans@.len());\n assert(ans@[idx_new] as int == k as int);\n assert(ans@[idx_new] as int == d);\n } else {\n assert(!Self::common_divisor(cnt@, k as int));\n assert(false);\n }\n }\n }\n }\n\n k = k + 1;\n }\n\n proof {\n assert(forall|idx: int| 0 <= idx < ans@.len() ==> 1 <= ans@[idx] as int <= n as int && Self::common_divisor(cnt@, ans@[idx] as int));\n assert forall|d: int| 1 <= d <= n as int && Self::common_divisor(cnt@, d)\n implies exists|idx: int| 0 <= idx < ans@.len() && ans@[idx] as int == d by {\n assert(1 <= d < k as int);\n }\n assert(forall|i: int, j: int| 0 <= i < j < ans@.len() ==> #[trigger] ans@[i] < #[trigger] ans@[j]);\n assert(Self::valid_answer(n as int, cnt@, ans@));\n }\n\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn valid_k_values(n: usize, cnt: Vec) -> Vec {\n let mut ans: Vec = Vec::new();\n let mut k: usize = 1;\n\n while k <= n {\n let k_i32: i32 = k as i32;\n let mut b: usize = 0;\n let mut bad_idx: i32 = -1;\n\n while b < 30 {\n if bad_idx == -1 && cnt[b] % k_i32 != 0 {\n bad_idx = b as i32;\n }\n b = b + 1;\n }\n\n if bad_idx == -1 {\n ans.push(k as i32);\n }\n\n k = k + 1;\n }\n\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case_id: usize = 0;\n\n while case_id < t {\n let n: usize = it.next().expect(\"n\").parse().expect(\"valid n\");\n let mut cnt: Vec = vec![0; 30];\n\n let mut i: usize = 0;\n while i < n {\n let x: i32 = it.next().expect(\"a_i\").parse().expect(\"valid a_i\");\n let mut b: usize = 0;\n while b < 30 {\n if ((x >> b) & 1) == 1 {\n cnt[b] = cnt[b] + 1;\n }\n b = b + 1;\n }\n i = i + 1;\n }\n\n let ans = Solution::valid_k_values(n, cnt);\n let mut j: usize = 0;\n while j < ans.len() {\n if j > 0 {\n print!(\" \");\n }\n print!(\"{}\", ans[j]);\n j = j + 1;\n }\n println!();\n\n case_id = case_id + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1602C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1602C/tests/mutated_testcases.jsonl"} {"problem": "cf1644C", "family": "codeforces", "description": "# Increase Subarray Sums\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou are given an array $$$a_1, a_2, \\dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.\n\nLet $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ **distinct** positions. An empty subarray should also be considered, it has sum $$$0$$$.\n\nNote that the subarray doesn't have to include all of the increased elements.\n\nCalculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 5000$$$)\u00a0\u2014 the number of testcases.\n\nThe first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 5000$$$; $$$0 \\le x \\le 10^5$$$)\u00a0\u2014 the number of elements in the array and the value to add.\n\nThe second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^5 \\le a_i \\le 10^5$$$).\n\nThe sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.\n\n## Output\n\nFor each testcase, print $$$n + 1$$$ integers\u00a0\u2014 the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.\n\n## Example\n\n**Input:**\n```\n3\n4 2\n4 1 3 2\n3 5\n-2 -7 -1\n10 2\n-6 -1 -2 4 -6 -1 -4 4 -5 -4\n```\n**Output:**\n```\n10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8\n```\n\n## Note\n\nIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \\cdot x$$$ will be added to the sum.\n\nIn the second testcase: \n - For $$$k = 0$$$, the empty subarray is the best option. \n- For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. \n- For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. \n- For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn increase_subarray_sums(a: Vec, x: i64) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n let mut case_idx: usize = 0;\n while case_idx < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let x: i64 = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::increase_subarray_sums(a, x);\n let mut k: usize = 0;\n while k <= n {\n if k > 0 {\n out.push(' ');\n }\n out.push_str(&ans[k].to_string());\n k = k + 1;\n }\n out.push('\\n');\n case_idx = case_idx + 1;\n }\n print!(\"{}\", out);\n}\n```\n", "tags": "Rating: 1400\nbrute force, dp, greedy, implementation\nSolved by: 14904 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn neg_inf() -> int {\n -1_000_000_000_000\n }\n\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn range_sum(a: Seq, start: int, len: int) -> int\n decreases if len <= 0 { 0 } else { len },\n {\n if start < 0 || len <= 0 || start + len > a.len() {\n 0\n } else {\n a[start] as int + Self::range_sum(a, start + 1, len - 1)\n }\n }\n\n pub open spec fn max_len_sum_upto(a: Seq, len: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if len <= 0 {\n 0\n } else if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::max_len_sum_upto(a, len, upto - 1);\n let cur = Self::range_sum(a, upto - 1, len);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn max_len_sum(a: Seq, len: int) -> int {\n if len == 0 {\n 0\n } else if len < 0 || len > a.len() {\n Self::neg_inf()\n } else {\n Self::max_len_sum_upto(a, len, a.len() as int - len + 1)\n }\n }\n\n pub open spec fn candidate_value(a: Seq, x: int, k: int, len: int) -> int {\n Self::max_len_sum(a, len) + x * Self::min_int(k, len)\n }\n\n pub open spec fn best_value_upto(a: Seq, x: int, k: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::best_value_upto(a, x, k, upto - 1);\n let cur = Self::candidate_value(a, x, k, upto - 1);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn best_value_for_k(a: Seq, x: int, k: int) -> int {\n Self::best_value_upto(a, x, k, a.len() as int + 1)\n }\n\n pub fn increase_subarray_sums(a: Vec, x: i64) -> (res: Vec)\n requires\n 1 <= a.len() <= 5000,\n 0 <= x <= 100000,\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n ensures\n res.len() == a.len() + 1,\n forall|k: int| 0 <= k < res.len() ==> #[trigger] res@[k] as int == Self::best_value_for_k(a@, x as int, k),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn max_sum_for_len(a: &Vec, len: usize) -> i64 {\n let n = a.len();\n let mut j: usize = 0;\n let mut window_sum: i64 = 0;\n while j < len {\n let j0 = j;\n window_sum = window_sum + a[j0];\n j = j0 + 1;\n }\n let mut best_len = window_sum;\n let mut start: usize = 0;\n while start + len < n {\n let start0 = start;\n window_sum = window_sum - a[start0] + a[start0 + len];\n start = start0 + 1;\n if window_sum > best_len {\n best_len = window_sum;\n }\n }\n best_len\n }\n\n fn best_answer_from_best(best: &Vec, x: i64, k: usize) -> i64 {\n let n = best.len() - 1;\n let mut cur: i64 = -1_000_000_000_000;\n let mut used_len: usize = 0;\n while used_len <= n {\n let used0 = used_len;\n let boosted = if used0 < k { used0 as i64 } else { k as i64 };\n let cand = best[used0] + x * boosted;\n if cand > cur {\n cur = cand;\n }\n used_len = used0 + 1;\n }\n cur\n }\n\n pub fn increase_subarray_sums(a: Vec, x: i64) -> Vec {\n let n = a.len();\n let mut best: Vec = Vec::new();\n best.push(0);\n let mut len: usize = 1;\n while len <= n {\n let best_len = Solution::max_sum_for_len(&a, len);\n best.push(best_len);\n len = len + 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n {\n let cur = Solution::best_answer_from_best(&best, x, k);\n ans.push(cur);\n k = k + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn neg_inf() -> int {\n -1_000_000_000_000\n }\n\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn range_sum(a: Seq, start: int, len: int) -> int\n decreases if len <= 0 { 0 } else { len },\n {\n if start < 0 || len <= 0 || start + len > a.len() {\n 0\n } else {\n a[start] as int + Self::range_sum(a, start + 1, len - 1)\n }\n }\n\n pub open spec fn max_len_sum_upto(a: Seq, len: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if len <= 0 {\n 0\n } else if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::max_len_sum_upto(a, len, upto - 1);\n let cur = Self::range_sum(a, upto - 1, len);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn max_len_sum(a: Seq, len: int) -> int {\n if len == 0 {\n 0\n } else if len < 0 || len > a.len() {\n Self::neg_inf()\n } else {\n Self::max_len_sum_upto(a, len, a.len() as int - len + 1)\n }\n }\n\n pub open spec fn candidate_value(a: Seq, x: int, k: int, len: int) -> int {\n Self::max_len_sum(a, len) + x * Self::min_int(k, len)\n }\n\n pub open spec fn best_value_upto(a: Seq, x: int, k: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::best_value_upto(a, x, k, upto - 1);\n let cur = Self::candidate_value(a, x, k, upto - 1);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn best_value_for_k(a: Seq, x: int, k: int) -> int {\n Self::best_value_upto(a, x, k, a.len() as int + 1)\n }\n\n pub open spec fn candidate_from_best(best: Seq, x: int, k: int, len: int) -> int {\n if len < 0 || len >= best.len() {\n Self::neg_inf()\n } else {\n best[len] as int + x * Self::min_int(k, len)\n }\n }\n\n pub open spec fn best_from_best_upto(best: Seq, x: int, k: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::best_from_best_upto(best, x, k, upto - 1);\n let cur = Self::candidate_from_best(best, x, k, upto - 1);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn best_from_best(best: Seq, x: int, k: int) -> int {\n Self::best_from_best_upto(best, x, k, best.len() as int)\n }\n\n fn max_sum_for_len(a: &Vec, len: usize) -> (res: i64)\n requires\n 1 <= a.len() <= 5000,\n 1 <= len <= a.len(),\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n ensures\n res as int == Self::max_len_sum(a@, len as int),\n -500000000 <= res as int <= 500000000,\n {\n let n = a.len();\n let mut j: usize = 0;\n let mut window_sum: i64 = 0;\n while j < len {\n let j0 = j;\n window_sum = window_sum + a[j0];\n j = j0 + 1;\n }\n let mut best_len = window_sum;\n let mut start: usize = 0;\n while start + len < n {\n let start0 = start;\n window_sum = window_sum - a[start0] + a[start0 + len];\n start = start0 + 1;\n if window_sum > best_len {\n best_len = window_sum;\n }\n }\n best_len\n }\n\n fn best_answer_from_best(best: &Vec, x: i64, k: usize) -> (res: i64)\n requires\n 1 <= best.len() <= 5001,\n 0 <= x <= 100000,\n 0 <= k < best.len(),\n forall|l: int| 0 <= l < best.len() ==> -500000000 <= #[trigger] best@[l] as int <= 500000000,\n ensures\n res as int == Self::best_from_best(best@, x as int, k as int),\n Self::neg_inf() <= res as int <= 1000000000,\n {\n let n = best.len() - 1;\n let mut cur: i64 = -1_000_000_000_000;\n let mut used_len: usize = 0;\n while used_len <= n {\n let used0 = used_len;\n let boosted = if used0 < k { used0 as i64 } else { k as i64 };\n let cand = best[used0] + x * boosted;\n if cand > cur {\n cur = cand;\n }\n used_len = used0 + 1;\n }\n cur\n }\n\n pub fn increase_subarray_sums(a: Vec, x: i64) -> (res: Vec)\n requires\n 1 <= a.len() <= 5000,\n 0 <= x <= 100000,\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n ensures\n res.len() == a.len() + 1,\n forall|k: int| 0 <= k < res.len() ==> #[trigger] res@[k] as int == Self::best_value_for_k(a@, x as int, k),\n {\n let n = a.len();\n let mut best: Vec = Vec::new();\n best.push(0);\n let mut len: usize = 1;\n while len <= n {\n let best_len = Solution::max_sum_for_len(&a, len);\n best.push(best_len);\n len = len + 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n {\n let cur = Solution::best_answer_from_best(&best, x, k);\n ans.push(cur);\n k = k + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn neg_inf() -> int {\n -1_000_000_000_000\n }\n\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn range_sum(a: Seq, start: int, len: int) -> int\n decreases if len <= 0 { 0 } else { len },\n {\n if start < 0 || len <= 0 || start + len > a.len() {\n 0\n } else {\n a[start] as int + Self::range_sum(a, start + 1, len - 1)\n }\n }\n\n pub open spec fn max_len_sum_upto(a: Seq, len: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if len <= 0 {\n 0\n } else if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::max_len_sum_upto(a, len, upto - 1);\n let cur = Self::range_sum(a, upto - 1, len);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn max_len_sum(a: Seq, len: int) -> int {\n if len == 0 {\n 0\n } else if len < 0 || len > a.len() {\n Self::neg_inf()\n } else {\n Self::max_len_sum_upto(a, len, a.len() as int - len + 1)\n }\n }\n\n pub open spec fn candidate_value(a: Seq, x: int, k: int, len: int) -> int {\n Self::max_len_sum(a, len) + x * Self::min_int(k, len)\n }\n\n pub open spec fn best_value_upto(a: Seq, x: int, k: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::best_value_upto(a, x, k, upto - 1);\n let cur = Self::candidate_value(a, x, k, upto - 1);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn best_value_for_k(a: Seq, x: int, k: int) -> int {\n Self::best_value_upto(a, x, k, a.len() as int + 1)\n }\n\n pub open spec fn candidate_from_best(best: Seq, x: int, k: int, len: int) -> int {\n if len < 0 || len >= best.len() {\n Self::neg_inf()\n } else {\n best[len] as int + x * Self::min_int(k, len)\n }\n }\n\n pub open spec fn best_from_best_upto(best: Seq, x: int, k: int, upto: int) -> int\n decreases if upto <= 0 { 0 } else { upto },\n {\n if upto <= 0 {\n Self::neg_inf()\n } else {\n let prev = Self::best_from_best_upto(best, x, k, upto - 1);\n let cur = Self::candidate_from_best(best, x, k, upto - 1);\n if prev <= cur { cur } else { prev }\n }\n }\n\n pub open spec fn best_from_best(best: Seq, x: int, k: int) -> int {\n Self::best_from_best_upto(best, x, k, best.len() as int)\n }\n\n proof fn lemma_range_sum_bounds(a: Seq, start: int, len: int)\n requires\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a[i] <= 100000,\n 0 <= start,\n 0 <= len,\n start + len <= a.len(),\n ensures\n -100000 * len <= Self::range_sum(a, start, len) <= 100000 * len,\n decreases len,\n {\n if len > 0 {\n Self::lemma_range_sum_bounds(a, start + 1, len - 1);\n }\n }\n\n proof fn lemma_range_sum_extend(a: Seq, start: int, len: int)\n requires\n 0 <= start,\n 0 <= len,\n start + len < a.len(),\n ensures\n Self::range_sum(a, start, len + 1) == Self::range_sum(a, start, len) + a[start + len] as int,\n decreases len,\n {\n if len == 0 {\n assert(start + 1 <= a.len());\n assert(Self::range_sum(a, start, 1) == a[start] as int + Self::range_sum(a, start + 1, 0));\n assert(Self::range_sum(a, start, 0) == 0);\n } else {\n Self::lemma_range_sum_extend(a, start + 1, len - 1);\n assert(Self::range_sum(a, start, len + 1) == a[start] as int + Self::range_sum(a, start + 1, len));\n assert(Self::range_sum(a, start, len) == a[start] as int + Self::range_sum(a, start + 1, len - 1));\n }\n }\n\n proof fn lemma_range_sum_slide(a: Seq, start: int, len: int)\n requires\n 0 <= start,\n 1 <= len,\n start + len < a.len(),\n ensures\n Self::range_sum(a, start + 1, len)\n == Self::range_sum(a, start, len) - a[start] as int + a[start + len] as int,\n decreases len,\n {\n if len == 1 {\n assert(start + 2 <= a.len());\n assert(Self::range_sum(a, start + 1, 1) == a[start + 1] as int + Self::range_sum(a, start + 2, 0));\n assert(Self::range_sum(a, start, 1) == a[start] as int + Self::range_sum(a, start + 1, 0));\n } else {\n Self::lemma_range_sum_slide(a, start + 1, len - 1);\n assert(Self::range_sum(a, start + 1, len) == a[start + 1] as int + Self::range_sum(a, start + 2, len - 1));\n assert(Self::range_sum(a, start, len) == a[start] as int + Self::range_sum(a, start + 1, len - 1));\n }\n }\n\n proof fn lemma_best_from_best_matches_array(a: Seq, best: Seq, x: int, k: int, upto: int)\n requires\n best.len() == a.len() + 1,\n forall|l: int| 0 <= l < best.len() ==> #[trigger] best[l] as int == Self::max_len_sum(a, l),\n 0 <= upto <= best.len(),\n ensures\n Self::best_from_best_upto(best, x, k, upto) == Self::best_value_upto(a, x, k, upto),\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_best_from_best_matches_array(a, best, x, k, upto - 1);\n assert(Self::candidate_from_best(best, x, k, upto - 1) == Self::candidate_value(a, x, k, upto - 1));\n }\n }\n\n fn max_sum_for_len(a: &Vec, len: usize) -> (res: i64)\n requires\n 1 <= a.len() <= 5000,\n 1 <= len <= a.len(),\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n ensures\n res as int == Self::max_len_sum(a@, len as int),\n -500000000 <= res as int <= 500000000,\n {\n let n = a.len();\n let mut j: usize = 0;\n let mut window_sum: i64 = 0;\n while j < len\n invariant\n 0 <= j <= len <= n <= 5000,\n n == a.len(),\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n window_sum as int == Self::range_sum(a@, 0, j as int),\n -100000 * j as int <= window_sum as int <= 100000 * j as int,\n decreases len - j,\n {\n let j0 = j;\n proof {\n assert(0 <= j0 < n);\n Self::lemma_range_sum_extend(a@, 0, j0 as int);\n assert(-100000 <= a@[j0 as int] <= 100000);\n assert(-100000 * (j0 as int + 1) <= window_sum as int + a@[j0 as int] as int <= 100000 * (j0 as int + 1)) by(nonlinear_arith)\n requires\n -100000 * j0 as int <= window_sum as int <= 100000 * j0 as int,\n -100000 <= a@[j0 as int] <= 100000;\n }\n window_sum = window_sum + a[j0];\n j = j0 + 1;\n proof {\n Self::lemma_range_sum_bounds(a@, 0, j as int);\n }\n }\n let mut best_len = window_sum;\n let mut start: usize = 0;\n proof {\n assert(Self::max_len_sum_upto(a@, len as int, 0) == Self::neg_inf());\n assert(Self::max_len_sum_upto(a@, len as int, 1)\n == if Self::max_len_sum_upto(a@, len as int, 0) <= Self::range_sum(a@, 0, len as int) {\n Self::range_sum(a@, 0, len as int)\n } else {\n Self::max_len_sum_upto(a@, len as int, 0)\n });\n assert(best_len as int == Self::max_len_sum_upto(a@, len as int, 1));\n }\n while start + len < n\n invariant\n 1 <= len <= n <= 5000,\n n == a.len(),\n 0 <= start <= n - len,\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n window_sum as int == Self::range_sum(a@, start as int, len as int),\n best_len as int == Self::max_len_sum_upto(a@, len as int, start as int + 1),\n -100000 * len as int <= window_sum as int <= 100000 * len as int,\n -100000 * len as int <= best_len as int <= 100000 * len as int,\n decreases n - len - start,\n {\n let start0 = start;\n proof {\n assert(0 <= start0 < n);\n assert(start0 + len < n);\n Self::lemma_range_sum_slide(a@, start0 as int, len as int);\n assert(-100000 <= a@[start0 as int] <= 100000);\n assert(-100000 <= a@[(start0 + len) as int] <= 100000);\n assert(-600000000 <= window_sum as int - a@[start0 as int] as int <= 600000000) by(nonlinear_arith)\n requires\n -100000 * len as int <= window_sum as int <= 100000 * len as int,\n len <= 5000,\n -100000 <= a@[start0 as int] <= 100000;\n assert(-700000000 <= window_sum as int - a@[start0 as int] as int + a@[(start0 + len) as int] as int <= 700000000) by(nonlinear_arith)\n requires\n -600000000 <= window_sum as int - a@[start0 as int] as int <= 600000000,\n -100000 <= a@[(start0 + len) as int] <= 100000;\n }\n window_sum = window_sum - a[start0] + a[start0 + len];\n start = start0 + 1;\n if window_sum > best_len {\n best_len = window_sum;\n }\n proof {\n Self::lemma_range_sum_bounds(a@, start as int, len as int);\n assert(Self::max_len_sum_upto(a@, len as int, start as int + 1)\n == if Self::max_len_sum_upto(a@, len as int, start as int) <= Self::range_sum(a@, start as int, len as int) {\n Self::range_sum(a@, start as int, len as int)\n } else {\n Self::max_len_sum_upto(a@, len as int, start as int)\n });\n }\n }\n proof {\n assert(start == n - len);\n assert(start as int + 1 == a@.len() as int - len as int + 1);\n assert(best_len as int == Self::max_len_sum(a@, len as int));\n }\n best_len\n }\n\n fn best_answer_from_best(best: &Vec, x: i64, k: usize) -> (res: i64)\n requires\n 1 <= best.len() <= 5001,\n 0 <= x <= 100000,\n 0 <= k < best.len(),\n forall|l: int| 0 <= l < best.len() ==> -500000000 <= #[trigger] best@[l] as int <= 500000000,\n ensures\n res as int == Self::best_from_best(best@, x as int, k as int),\n Self::neg_inf() <= res as int <= 1000000000,\n {\n let n = best.len() - 1;\n let mut cur: i64 = -1_000_000_000_000;\n let mut used_len: usize = 0;\n while used_len <= n\n invariant\n 0 <= used_len <= n + 1,\n 1 <= best.len() == n + 1,\n n <= 5000,\n 0 <= x <= 100000,\n 0 <= k < best.len(),\n forall|l: int| 0 <= l < best.len() ==> -500000000 <= #[trigger] best@[l] as int <= 500000000,\n cur as int == Self::best_from_best_upto(best@, x as int, k as int, used_len as int),\n Self::neg_inf() <= cur as int <= 1000000000,\n decreases n + 1 - used_len,\n {\n let used0 = used_len;\n let boosted = if used0 < k { used0 as i64 } else { k as i64 };\n proof {\n if used0 < k {\n assert(boosted == used0 as i64);\n assert(Self::min_int(k as int, used0 as int) == used0 as int);\n assert(0 <= boosted);\n assert(boosted <= 5000) by(nonlinear_arith)\n requires\n boosted == used0 as i64,\n used0 <= n,\n n <= 5000;\n } else {\n assert(boosted == k as i64);\n assert(Self::min_int(k as int, used0 as int) == k as int);\n assert(0 <= boosted);\n assert(boosted <= 5000) by(nonlinear_arith)\n requires\n boosted == k as i64,\n k < n + 1,\n n <= 5000;\n }\n assert(boosted as int == Self::min_int(k as int, used0 as int));\n assert(0 <= x as int * boosted as int <= 500000000) by(nonlinear_arith)\n requires\n 0 <= x <= 100000,\n 0 <= boosted <= 5000;\n assert(-500000000 <= best@[used0 as int] as int <= 500000000);\n assert(Self::neg_inf() <= best@[used0 as int] as int + x as int * boosted as int <= 1000000000);\n }\n let cand = best[used0] + x * boosted;\n if cand > cur {\n cur = cand;\n }\n used_len = used0 + 1;\n proof {\n assert(Self::best_from_best_upto(best@, x as int, k as int, used_len as int)\n == if Self::best_from_best_upto(best@, x as int, k as int, used0 as int)\n <= Self::candidate_from_best(best@, x as int, k as int, used0 as int) {\n Self::candidate_from_best(best@, x as int, k as int, used0 as int)\n } else {\n Self::best_from_best_upto(best@, x as int, k as int, used0 as int)\n });\n }\n }\n cur\n }\n\n pub fn increase_subarray_sums(a: Vec, x: i64) -> (res: Vec)\n requires\n 1 <= a.len() <= 5000,\n 0 <= x <= 100000,\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n ensures\n res.len() == a.len() + 1,\n forall|k: int| 0 <= k < res.len() ==> #[trigger] res@[k] as int == Self::best_value_for_k(a@, x as int, k),\n {\n let n = a.len();\n let mut best: Vec = Vec::new();\n best.push(0);\n let mut len: usize = 1;\n while len <= n\n invariant\n best.len() == len,\n n == a.len(),\n 1 <= len <= n + 1,\n n <= 5000,\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n forall|l: int| 0 <= l < best.len() ==> #[trigger] best@[l] as int == Self::max_len_sum(a@, l),\n forall|l: int| 0 <= l < best.len() ==> -500000000 <= #[trigger] best@[l] as int <= 500000000,\n decreases n + 1 - len,\n {\n let best_len = Solution::max_sum_for_len(&a, len);\n best.push(best_len);\n len = len + 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n\n invariant\n ans.len() == k,\n 0 <= k <= n + 1,\n best.len() == n + 1,\n n == a.len(),\n n <= 5000,\n forall|i: int| 0 <= i < a.len() ==> -100000 <= #[trigger] a@[i] <= 100000,\n forall|l: int| 0 <= l < best.len() ==> #[trigger] best@[l] as int == Self::max_len_sum(a@, l),\n forall|l: int| 0 <= l < best.len() ==> -500000000 <= #[trigger] best@[l] as int <= 500000000,\n forall|q: int| 0 <= q < ans.len() ==> #[trigger] ans@[q] as int == Self::best_value_for_k(a@, x as int, q),\n 0 <= x <= 100000,\n decreases n + 1 - k,\n {\n let cur = Solution::best_answer_from_best(&best, x, k);\n proof {\n Self::lemma_best_from_best_matches_array(a@, best@, x as int, k as int, best.len() as int);\n assert(cur as int == Self::best_value_for_k(a@, x as int, k as int));\n }\n ans.push(cur);\n k = k + 1;\n }\n proof {\n assert(k > n);\n assert(k == n + 1);\n assert(ans.len() == n + 1);\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n fn max_sum_for_len(a: &Vec, len: usize) -> i64 {\n let n = a.len();\n let mut j: usize = 0;\n let mut window_sum: i64 = 0;\n while j < len {\n let j0 = j;\n window_sum = window_sum + a[j0];\n j = j0 + 1;\n }\n let mut best_len = window_sum;\n let mut start: usize = 0;\n while start + len < n {\n let start0 = start;\n window_sum = window_sum - a[start0] + a[start0 + len];\n start = start0 + 1;\n if window_sum > best_len {\n best_len = window_sum;\n }\n }\n best_len\n }\n\n fn best_answer_from_best(best: &Vec, x: i64, k: usize) -> i64 {\n let n = best.len() - 1;\n let mut cur: i64 = -1_000_000_000_000;\n let mut used_len: usize = 0;\n while used_len <= n {\n let used0 = used_len;\n let boosted = if used0 < k { used0 as i64 } else { k as i64 };\n let cand = best[used0] + x * boosted;\n if cand > cur {\n cur = cand;\n }\n used_len = used0 + 1;\n }\n cur\n }\n\n pub fn increase_subarray_sums(a: Vec, x: i64) -> Vec {\n let n = a.len();\n let mut best: Vec = Vec::new();\n best.push(0);\n let mut len: usize = 1;\n while len <= n {\n let best_len = Solution::max_sum_for_len(&a, len);\n best.push(best_len);\n len = len + 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n {\n let cur = Solution::best_answer_from_best(&best, x, k);\n ans.push(cur);\n k = k + 1;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n let mut case_idx: usize = 0;\n while case_idx < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let x: i64 = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::increase_subarray_sums(a, x);\n let mut k: usize = 0;\n while k <= n {\n if k > 0 {\n out.push(' ');\n }\n out.push_str(&ans[k].to_string());\n k = k + 1;\n }\n out.push('\\n');\n case_idx = case_idx + 1;\n }\n print!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf1644C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1644C/tests/mutated_testcases.jsonl"} {"problem": "cf1676B", "family": "codeforces", "description": "# Equal Candies\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nThere are $$$n$$$ boxes with different quantities of candies in each of them. The $$$i$$$-th box has $$$a_i$$$ candies inside.\n\nYou also have $$$n$$$ friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don't want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.\n\nWhat's the minimum total number of candies you have to eat to satisfy the requirements?\n\n## Input\n\nThe first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$)\u00a0\u2014 the number of boxes you have.\n\nThe second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^7$$$)\u00a0\u2014 the quantity of candies in each box.\n\n## Output\n\nFor each test case, print a single integer denoting the minimum number of candies you have to eat to satisfy the requirements.\n\n## Example\n\n**Input:**\n```\n5\n5\n1 2 3 4 5\n6\n1000 1000 5 1000 1000 1000\n10\n1 2 3 5 1 2 7 9 13 5\n3\n8 8 8\n1\n10000000\n```\n**Output:**\n```\n10\n4975\n38\n0\n0\n```\n\n## Note\n\nFor the first test case, you can eat $$$1$$$ candy from the second box, $$$2$$$ candies from the third box, $$$3$$$ candies from the fourth box and $$$4$$$ candies from the fifth box. Now the boxes have $$$[1, 1, 1, 1, 1]$$$ candies in them and you ate $$$0 + 1 + 2 + 3 + 4 = 10$$$ candies in total so the answer is $$$10$$$.\n\nFor the second test case, the best answer is obtained by making all boxes contain $$$5$$$ candies in them, thus eating $$$995 + 995 + 0 + 995 + 995 + 995 = 4975$$$ candies in total.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\npub struct Solution;\n\nimpl Solution {\n pub fn min_operations_to_equal(candies: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n\n for _ in 0..t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut candies: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let x: i64 = it.next().unwrap().parse().unwrap();\n candies.push(x);\n }\n let ans = Solution::min_operations_to_equal(candies);\n out.push_str(&format!(\"{}\\n\", ans));\n }\n\n print!(\"{}\", out);\n}\n```\n", "tags": "Rating: 800\ngreedy, math, sortings\nSolved by: N/A users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_min_value(s: Seq, m: int) -> bool {\n &&& s.len() > 0\n &&& exists|j: int| 0 <= j < s.len() && s[j] as int == m\n &&& forall|j: int| 0 <= j < s.len() ==> m <= s[j] as int\n}\n\npub open spec fn sum_decrease_with(s: Seq, end: int, m: int) -> int\n recommends\n 0 <= end <= s.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n sum_decrease_with(s, end - 1, m) + (s[end - 1] as int - m)\n }\n}\n\nimpl Solution {\n pub fn min_operations_to_equal(candies: Vec) -> (result: i64)\n requires\n 1 <= candies.len() <= 50,\n forall|i: int| 0 <= i < candies.len() ==> 1 <= #[trigger] candies[i] as int <= 10_000_000,\n ensures\n exists|min_v: int| is_min_value(candies@, min_v)\n && result as int == sum_decrease_with(candies@, candies.len() as int, min_v),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_operations_to_equal(candies: Vec) -> i64 {\n let n = candies.len();\n let mut min_val = candies[0];\n let mut i: usize = 1;\n while i < n {\n if candies[i] < min_val {\n min_val = candies[i];\n }\n i += 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < n {\n ans += candies[i] - min_val;\n i += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_min_value(s: Seq, m: int) -> bool {\n &&& s.len() > 0\n &&& exists|j: int| 0 <= j < s.len() && s[j] as int == m\n &&& forall|j: int| 0 <= j < s.len() ==> m <= s[j] as int\n}\n\npub open spec fn sum_decrease_with(s: Seq, end: int, m: int) -> int\n recommends\n 0 <= end <= s.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n sum_decrease_with(s, end - 1, m) + (s[end - 1] as int - m)\n }\n}\n\nimpl Solution {\n pub fn min_operations_to_equal(candies: Vec) -> (result: i64)\n requires\n 1 <= candies.len() <= 50,\n forall|i: int| 0 <= i < candies.len() ==> 1 <= #[trigger] candies[i] as int <= 10_000_000,\n ensures\n exists|min_v: int| is_min_value(candies@, min_v)\n && result as int == sum_decrease_with(candies@, candies.len() as int, min_v),\n {\n let n = candies.len();\n let mut min_val = candies[0];\n let mut i: usize = 1;\n while i < n {\n if candies[i] < min_val {\n min_val = candies[i];\n }\n i += 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < n {\n ans += candies[i] - min_val;\n i += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_min_value(s: Seq, m: int) -> bool {\n &&& s.len() > 0\n &&& exists|j: int| 0 <= j < s.len() && s[j] as int == m\n &&& forall|j: int| 0 <= j < s.len() ==> m <= s[j] as int\n}\n\npub open spec fn sum_decrease_with(s: Seq, end: int, m: int) -> int\n recommends\n 0 <= end <= s.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n sum_decrease_with(s, end - 1, m) + (s[end - 1] as int - m)\n }\n}\n\nproof fn lemma_sum_step(s: Seq, end: int, m: int)\n requires\n 1 <= end <= s.len(),\n ensures\n sum_decrease_with(s, end, m) == sum_decrease_with(s, end - 1, m) + (s[end - 1] as int - m),\n{\n reveal_with_fuel(sum_decrease_with, 2);\n}\n\nimpl Solution {\n pub fn min_operations_to_equal(candies: Vec) -> (result: i64)\n requires\n 1 <= candies.len() <= 50,\n forall|i: int| 0 <= i < candies.len() ==> 1 <= #[trigger] candies[i] as int <= 10_000_000,\n ensures\n exists|min_v: int| is_min_value(candies@, min_v)\n && result as int == sum_decrease_with(candies@, candies.len() as int, min_v),\n {\n let n = candies.len();\n let mut min_val = candies[0];\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= n <= 50,\n candies.len() == n,\n 1 <= i <= n,\n forall|k: int| 0 <= k < candies.len() ==> 1 <= #[trigger] candies[k] as int <= 10_000_000,\n exists|j: int| 0 <= j < i as int && candies[j] == min_val,\n forall|j: int| 0 <= j < i as int ==> min_val <= candies[j],\n decreases n - i,\n {\n if candies[i] < min_val {\n min_val = candies[i];\n }\n i += 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < n\n invariant\n 1 <= n <= 50,\n candies.len() == n,\n 0 <= i <= n,\n forall|k: int| 0 <= k < candies.len() ==> 1 <= #[trigger] candies[k] as int <= 10_000_000,\n exists|j: int| 0 <= j < n as int && candies[j] == min_val,\n forall|j: int| 0 <= j < n as int ==> min_val <= candies[j],\n 0 <= ans as int,\n ans as int <= i as int * 10_000_000,\n ans as int == sum_decrease_with(candies@, i as int, min_val as int),\n decreases n - i,\n {\n proof {\n assert(0 <= (i as int) && (i as int) < (n as int));\n lemma_sum_step(candies@, i as int + 1, min_val as int);\n assert(sum_decrease_with(candies@, i as int + 1, min_val as int)\n == sum_decrease_with(candies@, i as int, min_val as int)\n + (candies@[i as int] as int - min_val as int));\n assert(min_val as int <= candies@[i as int] as int);\n assert(0 <= candies@[i as int] as int - min_val as int);\n assert(candies@[i as int] as int <= 10_000_000);\n assert(min_val as int >= 1);\n assert(candies@[i as int] as int - min_val as int <= 10_000_000);\n assert(ans as int + (candies@[i as int] as int - min_val as int) <= (i as int + 1) * 10_000_000);\n assert(ans as int + (candies@[i as int] as int - min_val as int) < 9_223_372_036_854_775_807);\n }\n ans += candies[i] - min_val;\n proof {\n assert(ans as int == sum_decrease_with(candies@, i as int + 1, min_val as int));\n assert(ans as int <= (i as int + 1) * 10_000_000);\n }\n i += 1;\n }\n\n proof {\n assert(i == n);\n assert(ans as int == sum_decrease_with(candies@, candies.len() as int, min_val as int));\n assert(is_min_value(candies@, min_val as int));\n }\n\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\npub struct Solution;\n\nimpl Solution {\n pub fn min_operations_to_equal(candies: Vec) -> i64 {\n let n = candies.len();\n let mut min_val = candies[0];\n let mut i: usize = 1;\n while i < n {\n if candies[i] < min_val {\n min_val = candies[i];\n }\n i += 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < n {\n ans += candies[i] - min_val;\n i += 1;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n\n for _ in 0..t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut candies: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let x: i64 = it.next().unwrap().parse().unwrap();\n candies.push(x);\n }\n let ans = Solution::min_operations_to_equal(candies);\n out.push_str(&format!(\"{}\\n\", ans));\n }\n\n print!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf1676B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1676B/tests/mutated_testcases.jsonl"} {"problem": "cf1678A", "family": "codeforces", "description": "# Tokitsukaze and All Zero Sequence\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nTokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \\ne j$$$; $$$1 \\leq i,j \\leq n$$$). \n - If $$$a_i = a_j$$$, change one of them to $$$0$$$. \n- Otherwise change both of them to $$$\\min(a_i, a_j)$$$. \n\nTokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.\n\n## Input\n\nThe first line contains a single positive integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)\u00a0\u2014 the number of test cases.\n\nFor each test case, the first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$)\u00a0\u2014 the length of the sequence $$$a$$$.\n\nThe second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 100$$$)\u00a0\u2014 the sequence $$$a$$$.\n\n## Output\n\nFor each test case, print a single integer\u00a0\u2014 the minimum number of operations to change all numbers in the sequence to $$$0$$$.\n\n## Example\n\n**Input:**\n```\n3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0\n```\n**Output:**\n```\n4\n3\n2\n```\n\n## Note\n\nIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:\n\nIn the $$$1$$$-st operation, $$$a_1 \\lt a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.\n\nIn the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.\n\nIn the $$$3$$$-rd operation, $$$a_1 \\lt a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.\n\nIn the $$$4$$$-th operation, $$$a_2 \\lt a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.\n\nSo the minimum number of operations is $$$4$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_ops_to_all_zero(a: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t_line = lines.next().unwrap().unwrap();\n let t: usize = t_line.trim().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n_line = lines.next().unwrap().unwrap();\n let n: usize = n_line.trim().parse().unwrap();\n let a_line = lines.next().unwrap().unwrap();\n let mut parts = a_line.split_whitespace();\n let mut a: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let x: i32 = parts.next().unwrap().parse().unwrap();\n a.push(x);\n j = j + 1;\n }\n let ans = Solution::min_ops_to_all_zero(a);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 19146 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_count(s: Seq, i: int, v: int) -> int\n decreases i,\n{\n if i <= 0 {\n 0int\n } else {\n spec_prefix_count(s, i - 1, v) + if s[i - 1] as int == v {\n 1int\n } else {\n 0int\n }\n }\n}\n\npub open spec fn spec_has_duplicate(s: Seq) -> bool {\n exists|vv: int|\n #![trigger spec_prefix_count(s, s.len() as int, vv)]\n 0 <= vv && vv <= 100 && spec_prefix_count(s, s.len() as int, vv) >= 2\n}\n\npub open spec fn spec_min_ops_answer(s: Seq) -> int {\n let n = s.len() as int;\n let z = spec_prefix_count(s, n, 0);\n if z > 0 {\n n - z\n } else if spec_has_duplicate(s) {\n n\n } else {\n n + 1\n }\n}\n\nimpl Solution {\n pub fn min_ops_to_all_zero(a: Vec) -> (res: i32)\n requires\n 2 <= a.len() <= 100,\n forall|t: int|\n #![trigger a[t]]\n 0 <= t < a.len() ==> 0 <= (a[t] as int) <= 100,\n ensures\n (res as int) == spec_min_ops_answer(a@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_ops_to_all_zero(a: Vec) -> i32 {\n let n = a.len();\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101 {\n cnt.push(0i32);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n {\n let x = a[i] as usize;\n let prev = cnt[x];\n cnt[x] = prev + 1;\n i = i + 1;\n }\n let zc = cnt[0];\n if zc > 0 {\n return n as i32 - zc;\n }\n let mut v = 0usize;\n let mut dup = false;\n while v < 101 {\n if cnt[v] >= 2 {\n dup = true;\n }\n v = v + 1;\n }\n if dup {\n return n as i32;\n }\n (n as i32) + 1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_count(s: Seq, i: int, v: int) -> int\n decreases i,\n{\n if i <= 0 {\n 0int\n } else {\n spec_prefix_count(s, i - 1, v) + if s[i - 1] as int == v {\n 1int\n } else {\n 0int\n }\n }\n}\n\npub open spec fn spec_has_duplicate(s: Seq) -> bool {\n exists|vv: int|\n #![trigger spec_prefix_count(s, s.len() as int, vv)]\n 0 <= vv && vv <= 100 && spec_prefix_count(s, s.len() as int, vv) >= 2\n}\n\npub open spec fn spec_min_ops_answer(s: Seq) -> int {\n let n = s.len() as int;\n let z = spec_prefix_count(s, n, 0);\n if z > 0 {\n n - z\n } else if spec_has_duplicate(s) {\n n\n } else {\n n + 1\n }\n}\n\nimpl Solution {\n pub fn min_ops_to_all_zero(a: Vec) -> (res: i32)\n requires\n 2 <= a.len() <= 100,\n forall|t: int|\n #![trigger a[t]]\n 0 <= t < a.len() ==> 0 <= (a[t] as int) <= 100,\n ensures\n (res as int) == spec_min_ops_answer(a@),\n {\n let n = a.len();\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101 {\n cnt.push(0i32);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n {\n let x = a[i] as usize;\n let prev = cnt[x];\n cnt.set(x, prev + 1);\n i = i + 1;\n }\n let zc = cnt[0];\n if zc > 0 {\n return n as i32 - zc;\n }\n let mut v = 0usize;\n let mut dup = false;\n while v < 101 {\n if cnt[v] >= 2 {\n dup = true;\n }\n v = v + 1;\n }\n if dup {\n return n as i32;\n }\n (n as i32) + 1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_count(s: Seq, i: int, v: int) -> int\n decreases i,\n{\n if i <= 0 {\n 0int\n } else {\n spec_prefix_count(s, i - 1, v) + if s[i - 1] as int == v {\n 1int\n } else {\n 0int\n }\n }\n}\n\npub open spec fn spec_has_duplicate(s: Seq) -> bool {\n exists|vv: int|\n #![trigger spec_prefix_count(s, s.len() as int, vv)]\n 0 <= vv && vv <= 100 && spec_prefix_count(s, s.len() as int, vv) >= 2\n}\n\npub open spec fn spec_min_ops_answer(s: Seq) -> int {\n let n = s.len() as int;\n let z = spec_prefix_count(s, n, 0);\n if z > 0 {\n n - z\n } else if spec_has_duplicate(s) {\n n\n } else {\n n + 1\n }\n}\n\nproof fn lemma_spec_prefix_count_nonneg(s: Seq, i: int, v: int)\n requires\n 0 <= i <= s.len(),\n ensures\n spec_prefix_count(s, i, v) >= 0,\n decreases i,\n{\n if i <= 0 {\n } else {\n lemma_spec_prefix_count_nonneg(s, i - 1, v);\n }\n}\n\nproof fn lemma_count_le_i(s: Seq, i: int, v: int)\n requires\n 0 <= i <= s.len(),\n ensures\n spec_prefix_count(s, i, v) <= i,\n decreases i,\n{\n if i <= 0 {\n } else {\n lemma_count_le_i(s, i - 1, v);\n assert(spec_prefix_count(s, i, v) <= spec_prefix_count(s, i - 1, v) + 1);\n assert(spec_prefix_count(s, i, v) <= i);\n }\n}\n\nproof fn lemma_prefix_other(s: Seq, i: int, j: int)\n requires\n 0 <= i < s.len(),\n 0 <= j <= 100,\n s[i] as int != j,\n ensures\n spec_prefix_count(s, i + 1, j) == spec_prefix_count(s, i, j),\n{\n}\n\nproof fn lemma_prefix_hit(s: Seq, i: int, j: int)\n requires\n 0 <= i < s.len(),\n s[i] as int == j,\n ensures\n spec_prefix_count(s, i + 1, j) == spec_prefix_count(s, i, j) + 1,\n{\n}\n\nproof fn lemma_cnt_implies_spec_dup(s: Seq, cnt: Seq)\n requires\n cnt.len() == 101,\n forall|u: int|\n 0 <= u && u <= 100 ==> (cnt[u] as int) == spec_prefix_count(s, s.len() as int, u),\n exists|u: int| 0 <= u && u <= 100 && (cnt[u] as int) >= 2,\n ensures\n spec_has_duplicate(s),\n{\n let u = choose|u: int| 0 <= u && u <= 100 && (cnt[u] as int) >= 2;\n assert((cnt[u] as int) == spec_prefix_count(s, s.len() as int, u));\n assert(spec_has_duplicate(s));\n}\n\nproof fn lemma_no_dup_cnt(s: Seq, cnt: Seq)\n requires\n cnt.len() == 101,\n forall|u: int|\n 0 <= u && u <= 100 ==> (cnt[u] as int) == spec_prefix_count(s, s.len() as int, u),\n forall|u: int| 0 <= u && u <= 100 ==> (cnt[u] as int) <= 1,\n ensures\n !spec_has_duplicate(s),\n{\n assert forall|vv: int|\n 0 <= vv && vv <= 100 implies spec_prefix_count(s, s.len() as int, vv) < 2 by {\n assert((cnt[vv] as int) == spec_prefix_count(s, s.len() as int, vv));\n assert((cnt[vv] as int) <= 1);\n }\n}\n\nimpl Solution {\n pub fn min_ops_to_all_zero(a: Vec) -> (res: i32)\n requires\n 2 <= a.len() <= 100,\n forall|t: int|\n #![trigger a[t]]\n 0 <= t < a.len() ==> 0 <= (a[t] as int) <= 100,\n ensures\n (res as int) == spec_min_ops_answer(a@),\n {\n let n = a.len();\n let ghost ni = n as int;\n proof {\n assert(ni == n as int);\n assert(2 <= a.len() && a.len() <= 100);\n assert(2 <= ni && ni <= 100);\n }\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101\n invariant\n t <= 101,\n cnt.len() == t,\n forall|j: int|\n 0 <= j < t as int ==> #[trigger] cnt@[j] == 0,\n decreases 101 - t,\n {\n cnt.push(0i32);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n\n invariant\n n == a.len(),\n ni == a@.len(),\n ni <= 100,\n i <= n,\n cnt.len() == 101,\n forall|k: int|\n 0 <= k < ni ==> 0 <= #[trigger] a@[k] <= 100,\n forall|j: int|\n 0 <= j < 101 ==> #[trigger] cnt@[j] == spec_prefix_count(a@, i as int, j),\n decreases n - i,\n {\n proof {\n assert(i < n);\n assert(i < a.len());\n assert((i as int) < ni);\n assert(ni == a@.len());\n assert(0 <= a@[i as int] && a@[i as int] <= 100);\n }\n let x = a[i] as usize;\n proof {\n assert(x as int == a@[i as int] as int);\n assert(x <= 100);\n assert(x < cnt.len());\n }\n let prev = cnt[x];\n let ghost i0 = i as int;\n proof {\n assert(i0 < a@.len());\n assert(0 <= a@[i0] && a@[i0] <= 100);\n assert(a@[i0] as int == x as int);\n assert(prev == spec_prefix_count(a@, i0, x as int));\n lemma_spec_prefix_count_nonneg(a@, i0, x as int);\n lemma_count_le_i(a@, i0, x as int);\n assert((prev as int) <= i0);\n assert(i0 < ni);\n assert(ni <= 100);\n assert(i0 <= ni - 1);\n assert((prev as int) <= ni - 1);\n assert(prev + 1 <= ni);\n }\n let ghost cnt0 = cnt@;\n cnt.set(x, prev + 1);\n proof {\n assert forall|j: int|\n 0 <= j < 101 implies cnt@[j] == spec_prefix_count(a@, i0 + 1, j) by {\n if 0 <= j && j < 101 {\n if j == x as int {\n assert(a@[i0] as int == j);\n lemma_prefix_hit(a@, i0, j);\n assert(cnt@[j] == prev + 1);\n assert(spec_prefix_count(a@, i0 + 1, j) == prev + 1);\n } else {\n assert(j != x as int);\n assert(a@[i0] as int == x as int);\n assert(a@[i0] as int != j);\n lemma_prefix_other(a@, i0, j);\n assert(cnt@[j] == cnt0[j]);\n assert(cnt0[j] == spec_prefix_count(a@, i0, j));\n assert(cnt@[j] == spec_prefix_count(a@, i0 + 1, j));\n }\n }\n };\n }\n i = i + 1;\n }\n proof {\n assert(i == n);\n assert forall|j: int|\n 0 <= j < 101 implies cnt@[j] == spec_prefix_count(a@, ni, j) by {\n assert(cnt@[j] == spec_prefix_count(a@, i as int, j));\n assert(i as int == ni);\n }\n }\n let zc = cnt[0];\n proof {\n lemma_spec_prefix_count_nonneg(a@, ni, 0);\n assert((cnt@[0] as int) == spec_prefix_count(a@, ni, 0));\n assert(zc as int >= 0);\n }\n if zc > 0 {\n proof {\n let zn = spec_prefix_count(a@, ni, 0);\n assert(zn == zc as int);\n assert(zn > 0);\n assert(spec_min_ops_answer(a@) == ni - zn);\n assert((n as i32 - zc) as int == ni - zn);\n }\n return n as i32 - zc;\n }\n let mut v = 0usize;\n let mut dup = false;\n while v < 101\n invariant\n cnt.len() == 101,\n v <= 101,\n forall|j: int|\n 0 <= j < 101 ==> #[trigger] cnt@[j] == spec_prefix_count(a@, ni, j),\n dup == exists|x: int|\n #![trigger cnt@[x]]\n 0 <= x && x < v as int && (cnt@[x] as int) >= 2,\n decreases 101 - v,\n {\n if cnt[v] >= 2 {\n dup = true;\n }\n v = v + 1;\n }\n proof {\n assert(v == 101);\n if dup {\n assert(exists|x: int| 0 <= x && x <= 100 && (cnt@[x] as int) >= 2);\n } else {\n assert forall|x: int|\n 0 <= x && x <= 100 implies (cnt@[x] as int) < 2 by {\n assert(!(exists|y: int|\n 0 <= y && y < v as int && (cnt@[y] as int) >= 2));\n assert(x < v as int);\n }\n }\n }\n if dup {\n proof {\n assert(exists|x: int| 0 <= x && x <= 100 && (cnt@[x] as int) >= 2);\n assert forall|u: int|\n 0 <= u && u <= 100 implies (cnt@[u] as int) == spec_prefix_count(a@, ni, u) by {\n }\n lemma_cnt_implies_spec_dup(a@, cnt@);\n assert((cnt@[0] as int) == spec_prefix_count(a@, ni, 0));\n assert(!(zc > 0));\n assert(zc as int >= 0);\n assert(zc == 0);\n assert(spec_prefix_count(a@, ni, 0) == 0);\n assert(spec_min_ops_answer(a@) == ni);\n }\n return n as i32;\n }\n proof {\n assert forall|u: int| 0 <= u && u <= 100 implies (cnt@[u] as int) <= 1 by {\n assert((cnt@[u] as int) < 2);\n }\n lemma_no_dup_cnt(a@, cnt@);\n assert((cnt@[0] as int) == spec_prefix_count(a@, ni, 0));\n assert(!(zc > 0));\n assert(zc as int >= 0);\n assert(zc == 0);\n assert(spec_prefix_count(a@, ni, 0) == 0);\n assert(!spec_has_duplicate(a@));\n assert(spec_min_ops_answer(a@) == ni + 1);\n }\n (n as i32) + 1\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_ops_to_all_zero(a: Vec) -> i32 {\n let n = a.len();\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101 {\n cnt.push(0i32);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n {\n let x = a[i] as usize;\n let prev = cnt[x];\n cnt[x] = prev + 1;\n i = i + 1;\n }\n let zc = cnt[0];\n if zc > 0 {\n return n as i32 - zc;\n }\n let mut v = 0usize;\n let mut dup = false;\n while v < 101 {\n if cnt[v] >= 2 {\n dup = true;\n }\n v = v + 1;\n }\n if dup {\n return n as i32;\n }\n (n as i32) + 1\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t_line = lines.next().unwrap().unwrap();\n let t: usize = t_line.trim().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n_line = lines.next().unwrap().unwrap();\n let n: usize = n_line.trim().parse().unwrap();\n let a_line = lines.next().unwrap().unwrap();\n let mut parts = a_line.split_whitespace();\n let mut a: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let x: i32 = parts.next().unwrap().parse().unwrap();\n a.push(x);\n j = j + 1;\n }\n let ans = Solution::min_ops_to_all_zero(a);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1678A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1678A/tests/mutated_testcases.jsonl"} {"problem": "cf1690B", "family": "codeforces", "description": "# Array Decrements\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nKristina has two arrays $$$a$$$ and $$$b$$$, each containing $$$n$$$ non-negative integers. She can perform the following operation on array $$$a$$$ any number of times: \n - apply a decrement to each non-zero element of the array, that is, replace the value of each element $$$a_i$$$ such that $$$a_i \\gt 0$$$ with the value $$$a_i - 1$$$ ($$$1 \\le i \\le n$$$). If $$$a_i$$$ was $$$0$$$, its value does not change. \n\nDetermine whether Kristina can get an array $$$b$$$ from an array $$$a$$$ in some number of operations (probably zero). In other words, can she make $$$a_i = b_i$$$ after some number of operations for each $$$1 \\le i \\le n$$$?\n\nFor example, let $$$n = 4$$$, $$$a = [3, 5, 4, 1]$$$ and $$$b = [1, 3, 2, 0]$$$. In this case, she can apply the operation twice: \n - after the first application of the operation she gets $$$a = [2, 4, 3, 0]$$$; \n- after the second use of the operation she gets $$$a = [1, 3, 2, 0]$$$. \n\nThus, in two operations, she can get an array $$$b$$$ from an array $$$a$$$.\n\n## Input\n\nThe first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014the number of test cases in the test.\n\nThe descriptions of the test cases follow.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 5 \\cdot 10^4$$$).\n\nThe second line of each test case contains exactly $$$n$$$ non-negative integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$).\n\nThe third line of each test case contains exactly $$$n$$$ non-negative integers $$$b_1, b_2, \\dots, b_n$$$ ($$$0 \\le b_i \\le 10^9$$$).\n\nIt is guaranteed that the sum of $$$n$$$ values over all test cases in the test does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, output on a separate line:\n - `YES`, if by doing some number of operations it is possible to get an array $$$b$$$ from an array $$$a$$$; \n- `NO` otherwise. \n\nYou can output `YES` and `NO` in any case (for example, strings `yEs`, `yes`, `Yes` and `YES` will be recognized as a positive response).\n\n## Example\n\n**Input:**\n```\n6\n4\n3 5 4 1\n1 3 2 0\n3\n1 2 1\n0 1 0\n4\n5 3 7 2\n1 1 1 1\n5\n1 2 3 4 5\n1 2 3 4 6\n1\n8\n0\n1\n4\n6\n```\n**Output:**\n```\nYES\nYES\nNO\nNO\nYES\nNO\n```\n\n## Note\n\nThe first test case is analyzed in the statement.\n\nIn the second test case, it is enough to apply the operation to array $$$a$$$ once.\n\nIn the third test case, it is impossible to get array $$$b$$$ from array $$$a$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_possible(n: usize, a: Vec, b: Vec) -> bool {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(t_str)) = lines.next() {\n if let Ok(t) = t_str.trim().parse::() {\n for _ in 0..t {\n if let Some(Ok(n_str)) = lines.next() {\n let n: usize = n_str.trim().parse().unwrap();\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n let b: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n if Solution::is_possible(n, a, b) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n }\n }\n}\n```\n", "tags": "Rating: 800\ngreedy, implementation\nSolved by: 26865 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_valid_decrements(n: usize, a: Seq, b: Seq, max_diff: i32) -> bool {\n (forall|i: int| 0 <= i && i < n ==> a[i] >= b[i]) &&\n (forall|i: int| 0 <= i && i < n ==> a[i] - b[i] <= max_diff) &&\n (forall|i: int| 0 <= i && i < n ==>\n (a[i] - b[i] == max_diff || b[i] == 0))\n}\n\npub open spec fn has_valid_decrements(n: usize, a: Seq, b: Seq) -> bool {\n exists|max_diff: i32| max_diff >= 0 && is_valid_decrements(n, a, b, max_diff)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_possible(n: usize, a: Vec, b: Vec) -> (res: bool)\n requires\n 1 <= n && n <= 50000,\n a.len() == n,\n b.len() == n,\n forall|i: int| 0 <= i && i < n ==> 0 <= a@[i] && a@[i] <= 1000000000,\n forall|i: int| 0 <= i && i < n ==> 0 <= b@[i] && b@[i] <= 1000000000,\n ensures\n res == has_valid_decrements(n, a@, b@)\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_possible(n: usize, a: Vec, b: Vec) -> bool {\n let mut max_diff: i32 = 0;\n let mut i: usize = 0;\n let mut possible = true;\n \n while i < n {\n if a[i] < b[i] {\n possible = false;\n } else {\n let diff = a[i] - b[i];\n if diff > max_diff {\n max_diff = diff;\n }\n }\n i += 1;\n }\n \n if !possible {\n return false;\n }\n \n let mut j: usize = 0;\n while j < n {\n let diff = a[j] - b[j];\n if diff < max_diff && b[j] != 0 {\n possible = false;\n }\n j += 1;\n }\n \n possible\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_valid_decrements(n: usize, a: Seq, b: Seq, max_diff: i32) -> bool {\n (forall|i: int| 0 <= i && i < n ==> a[i] >= b[i]) &&\n (forall|i: int| 0 <= i && i < n ==> a[i] - b[i] <= max_diff) &&\n (forall|i: int| 0 <= i && i < n ==>\n (a[i] - b[i] == max_diff || b[i] == 0))\n}\n\npub open spec fn has_valid_decrements(n: usize, a: Seq, b: Seq) -> bool {\n exists|max_diff: i32| max_diff >= 0 && is_valid_decrements(n, a, b, max_diff)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_possible(n: usize, a: Vec, b: Vec) -> (res: bool)\n requires\n 1 <= n && n <= 50000,\n a.len() == n,\n b.len() == n,\n forall|i: int| 0 <= i && i < n ==> 0 <= a@[i] && a@[i] <= 1000000000,\n forall|i: int| 0 <= i && i < n ==> 0 <= b@[i] && b@[i] <= 1000000000,\n ensures\n res == has_valid_decrements(n, a@, b@)\n {\n let mut max_diff: i32 = 0;\n let mut i: usize = 0;\n let mut possible = true;\n \n while i < n {\n if a[i] < b[i] {\n possible = false;\n } else {\n let diff = a[i] - b[i];\n if diff > max_diff {\n max_diff = diff;\n }\n }\n i += 1;\n }\n \n if !possible {\n return false;\n }\n \n let mut j: usize = 0;\n while j < n {\n let diff = a[j] - b[j];\n if diff < max_diff && b[j] != 0 {\n possible = false;\n }\n j += 1;\n }\n \n possible\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_valid_decrements(n: usize, a: Seq, b: Seq, max_diff: i32) -> bool {\n (forall|i: int| 0 <= i && i < n ==> a[i] >= b[i]) &&\n (forall|i: int| 0 <= i && i < n ==> a[i] - b[i] <= max_diff) &&\n (forall|i: int| 0 <= i && i < n ==>\n (a[i] - b[i] == max_diff || b[i] == 0))\n}\n\npub open spec fn has_valid_decrements(n: usize, a: Seq, b: Seq) -> bool {\n exists|max_diff: i32| max_diff >= 0 && is_valid_decrements(n, a, b, max_diff)\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_possible(n: usize, a: Vec, b: Vec) -> (res: bool)\n requires\n 1 <= n && n <= 50000,\n a.len() == n,\n b.len() == n,\n forall|i: int| 0 <= i && i < n ==> 0 <= a@[i] && a@[i] <= 1000000000,\n forall|i: int| 0 <= i && i < n ==> 0 <= b@[i] && b@[i] <= 1000000000,\n ensures\n res == has_valid_decrements(n, a@, b@)\n {\n let mut max_diff: i32 = 0;\n let mut i: usize = 0;\n let mut possible = true;\n \n while i < n\n invariant\n 0 <= i && i <= n,\n a.len() == n,\n b.len() == n,\n forall|k: int| 0 <= k && k < n ==> 0 <= a@[k] && a@[k] <= 1000000000,\n forall|k: int| 0 <= k && k < n ==> 0 <= b@[k] && b@[k] <= 1000000000,\n 0 <= max_diff && max_diff <= 1000000000,\n possible <==> (forall|k: int| 0 <= k && k < i ==> a@[k] >= b@[k]),\n possible ==> forall|k: int| 0 <= k && k < i ==> a@[k] - b@[k] <= max_diff,\n possible ==> (max_diff == 0 || exists|k: int| 0 <= k && k < i && a@[k] - b@[k] == max_diff),\n !possible ==> !has_valid_decrements(n, a@, b@)\n decreases n - i\n {\n if a[i] < b[i] {\n possible = false;\n proof {\n if has_valid_decrements(n, a@, b@) {\n let m = choose|m: i32| m >= 0 && is_valid_decrements(n, a@, b@, m);\n assert(a@[i as int] >= b@[i as int]); \n }\n }\n } else {\n let diff = a[i] - b[i];\n if diff > max_diff {\n max_diff = diff;\n }\n }\n i += 1;\n }\n \n if !possible {\n return false;\n }\n \n let mut j: usize = 0;\n while j < n\n invariant\n 0 <= j && j <= n,\n a.len() == n,\n b.len() == n,\n forall|k: int| 0 <= k && k < n ==> 0 <= a@[k] && a@[k] <= 1000000000,\n forall|k: int| 0 <= k && k < n ==> 0 <= b@[k] && b@[k] <= 1000000000,\n 0 <= max_diff && max_diff <= 1000000000,\n forall|k: int| 0 <= k && k < n ==> a@[k] >= b@[k],\n forall|k: int| 0 <= k && k < n ==> a@[k] - b@[k] <= max_diff,\n max_diff == 0 || exists|k: int| 0 <= k && k < n && a@[k] - b@[k] == max_diff,\n possible ==> forall|k: int| 0 <= k && k < j ==> (a@[k] - b@[k] == max_diff || b@[k] == 0),\n !possible ==> !has_valid_decrements(n, a@, b@)\n decreases n - j\n {\n let diff = a[j] - b[j];\n if diff < max_diff && b[j] != 0 {\n possible = false;\n proof {\n if has_valid_decrements(n, a@, b@) {\n let m = choose|m: i32| m >= 0 && is_valid_decrements(n, a@, b@, m);\n if max_diff > 0 {\n let k = choose|k: int| 0 <= k && k < n && a@[k] - b@[k] == max_diff;\n assert(a@[k] - b@[k] <= m);\n }\n assert(m >= max_diff);\n assert(a@[j as int] - b@[j as int] == m || b@[j as int] == 0);\n }\n }\n }\n j += 1;\n }\n \n if possible {\n proof {\n assert(is_valid_decrements(n, a@, b@, max_diff));\n }\n }\n \n possible\n }\n}\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_possible(n: usize, a: Vec, b: Vec) -> bool {\n let mut max_diff: i32 = 0;\n let mut i: usize = 0;\n let mut possible = true;\n \n while i < n {\n if a[i] < b[i] {\n possible = false;\n } else {\n let diff = a[i] - b[i];\n if diff > max_diff {\n max_diff = diff;\n }\n }\n i += 1;\n }\n \n if !possible {\n return false;\n }\n \n let mut j: usize = 0;\n while j < n {\n let diff = a[j] - b[j];\n if diff < max_diff && b[j] != 0 {\n possible = false;\n }\n j += 1;\n }\n \n possible\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(t_str)) = lines.next() {\n if let Ok(t) = t_str.trim().parse::() {\n for _ in 0..t {\n if let Some(Ok(n_str)) = lines.next() {\n let n: usize = n_str.trim().parse().unwrap();\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n let b: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n if Solution::is_possible(n, a, b) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n }\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1690B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1690B/tests/mutated_testcases.jsonl"} {"problem": "cf1690C", "family": "codeforces", "description": "# Restoring the Duration of Tasks\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nRecently, Polycarp completed $$$n$$$ successive tasks.\n\nFor each completed task, the time $$$s_i$$$ is known when it was given, no two tasks were given at the same time. Also given is the time $$$f_i$$$ when the task was completed. For each task, there is an unknown value $$$d_i$$$ ($$$d_i \\gt 0$$$)\u00a0\u2014 **duration of task execution**.\n\nIt is known that the tasks were completed in the order in which they came. Polycarp performed the tasks as follows:\n - As soon as the very first task came, Polycarp immediately began to carry it out. \n- If a new task arrived before Polycarp finished the previous one, he put the new task at the end of the queue. \n- When Polycarp finished executing the next task and the queue was not empty, he **immediately** took a new task from the head of the queue (if the queue is empty \u2014 he just waited for the next task). \n\nFind $$$d_i$$$ (duration) of each task.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThe descriptions of the input data sets follow.\n\nThe first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$).\n\nThe second line of each test case contains exactly $$$n$$$ integers $$$s_1 \\lt s_2 \\lt \\dots \\lt s_n$$$ ($$$0 \\le s_i \\le 10^9$$$).\n\nThe third line of each test case contains exactly $$$n$$$ integers $$$f_1 \\lt f_2 \\lt \\dots \\lt f_n$$$ ($$$s_i \\lt f_i \\le 10^9$$$).\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each of $$$t$$$ test cases print $$$n$$$ positive integers $$$d_1, d_2, \\dots, d_n$$$\u00a0\u2014 the duration of each task.\n\n## Example\n\n**Input:**\n```\n4\n3\n0 3 7\n2 10 11\n2\n10 15\n11 16\n9\n12 16 90 195 1456 1569 3001 5237 19275\n13 199 200 260 9100 10000 10914 91066 5735533\n1\n0\n1000000000\n```\n**Output:**\n```\n2 7 1 \n1 1 \n1 183 1 60 7644 900 914 80152 5644467 \n1000000000\n```\n\n## Note\n\nFirst test case:\n\nThe queue is empty at the beginning: $$$[ ]$$$. And that's where the first task comes in. At time $$$2$$$, Polycarp finishes doing the first task, so the duration of the first task is $$$2$$$. The queue is empty so Polycarp is just waiting.\n\nAt time $$$3$$$, the second task arrives. And at time $$$7$$$, the third task arrives, and now the queue looks like this: $$$[7]$$$.\n\nAt the time $$$10$$$, Polycarp finishes doing the second task, as a result, the duration of the second task is $$$7$$$.\n\nAnd at time $$$10$$$, Polycarp immediately starts doing the third task and finishes at time $$$11$$$. As a result, the duration of the third task is $$$1$$$.\n An example of the first test case.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn restore_durations(s: Vec, f: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let t: usize = tokens.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case: usize = 0;\n while case < t {\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut s: Vec = Vec::with_capacity(n);\n let mut f: Vec = Vec::with_capacity(n);\n let mut idx: usize = 0;\n while idx < n {\n s.push(tokens.next().expect(\"s\").parse().expect(\"valid\"));\n idx = idx + 1;\n }\n idx = 0;\n while idx < n {\n f.push(tokens.next().expect(\"f\").parse().expect(\"valid\"));\n idx = idx + 1;\n }\n let result = Solution::restore_durations(s, f);\n let mut out = String::new();\n idx = 0;\n while idx < n {\n if idx > 0 {\n out.push(' ');\n }\n out.push_str(&result[idx].to_string());\n idx = idx + 1;\n }\n println!(\"{}\", out);\n case = case + 1;\n }\n}\n```\n", "tags": "Rating: 800\ndata structures, greedy, implementation\nSolved by: 28009 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn duration(s: Seq, f: Seq, i: int) -> int\n recommends\n 0 <= i < s.len(),\n s.len() == f.len(),\n decreases i,\n {\n if i <= 0 {\n f[0] as int - s[0] as int\n } else {\n let prev_finish = f[i - 1] as int;\n let start = if s[i] as int > prev_finish { s[i] as int } else { prev_finish };\n f[i] as int - start\n }\n }\n\n pub fn restore_durations(s: Vec, f: Vec) -> (result: Vec)\n requires\n s.len() == f.len(),\n 1 <= s.len() <= 200_000,\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < f.len() ==> 0 <= #[trigger] f[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < s.len() ==> s[k] < f[k],\n forall |k: int| 0 <= k < s.len() - 1 ==> #[trigger] s[k] < s[k + 1],\n forall |k: int| 0 <= k < f.len() - 1 ==> #[trigger] f[k] < f[k + 1],\n ensures\n result.len() == s.len(),\n forall |k: int| 0 <= k < result.len() ==> #[trigger] result[k] as int == Self::duration(s@, f@, k),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn restore_durations(s: Vec, f: Vec) -> Vec {\n let n = s.len();\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n let start = if i == 0 || s[i] > f[i - 1] { s[i] } else { f[i - 1] };\n let dur = f[i] - start;\n result.push(dur);\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn duration(s: Seq, f: Seq, i: int) -> int\n recommends\n 0 <= i < s.len(),\n s.len() == f.len(),\n decreases i,\n {\n if i <= 0 {\n f[0] as int - s[0] as int\n } else {\n let prev_finish = f[i - 1] as int;\n let start = if s[i] as int > prev_finish { s[i] as int } else { prev_finish };\n f[i] as int - start\n }\n }\n\n pub fn restore_durations(s: Vec, f: Vec) -> (result: Vec)\n requires\n s.len() == f.len(),\n 1 <= s.len() <= 200_000,\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < f.len() ==> 0 <= #[trigger] f[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < s.len() ==> s[k] < f[k],\n forall |k: int| 0 <= k < s.len() - 1 ==> #[trigger] s[k] < s[k + 1],\n forall |k: int| 0 <= k < f.len() - 1 ==> #[trigger] f[k] < f[k + 1],\n ensures\n result.len() == s.len(),\n forall |k: int| 0 <= k < result.len() ==> #[trigger] result[k] as int == Self::duration(s@, f@, k),\n {\n let n = s.len();\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n let start = if i == 0 || s[i] > f[i - 1] { s[i] } else { f[i - 1] };\n let dur = f[i] - start;\n result.push(dur);\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn duration(s: Seq, f: Seq, i: int) -> int\n recommends\n 0 <= i < s.len(),\n s.len() == f.len(),\n decreases i,\n {\n if i <= 0 {\n f[0] as int - s[0] as int\n } else {\n let prev_finish = f[i - 1] as int;\n let start = if s[i] as int > prev_finish { s[i] as int } else { prev_finish };\n f[i] as int - start\n }\n }\n\n pub fn restore_durations(s: Vec, f: Vec) -> (result: Vec)\n requires\n s.len() == f.len(),\n 1 <= s.len() <= 200_000,\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < f.len() ==> 0 <= #[trigger] f[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < s.len() ==> s[k] < f[k],\n forall |k: int| 0 <= k < s.len() - 1 ==> #[trigger] s[k] < s[k + 1],\n forall |k: int| 0 <= k < f.len() - 1 ==> #[trigger] f[k] < f[k + 1],\n ensures\n result.len() == s.len(),\n forall |k: int| 0 <= k < result.len() ==> #[trigger] result[k] as int == Self::duration(s@, f@, k),\n {\n let n = s.len();\n let ghost s_spec = s@;\n let ghost f_spec = f@;\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n\n while i < n\n invariant\n n == s.len(),\n s.len() == f.len(),\n s@ == s_spec,\n f@ == f_spec,\n n <= 200_000,\n 0 <= i <= n,\n result.len() == i,\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s_spec[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < f.len() ==> 0 <= #[trigger] f_spec[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < s.len() ==> s_spec[k] < f_spec[k],\n forall |k: int| 0 <= k < s.len() - 1 ==> #[trigger] s_spec[k] < s_spec[k + 1],\n forall |k: int| 0 <= k < f.len() - 1 ==> #[trigger] f_spec[k] < f_spec[k + 1],\n forall |k: int| 0 <= k < i ==> #[trigger] result[k] as int == Self::duration(s_spec, f_spec, k),\n decreases n - i,\n {\n let start = if i == 0 || s[i] > f[i - 1] { s[i] } else { f[i - 1] };\n let dur = f[i] - start;\n\n proof {\n if i == 0 {\n assert(start as int == s_spec[0] as int);\n assert(dur as int == f_spec[0] as int - s_spec[0] as int);\n assert(dur as int == Self::duration(s_spec, f_spec, 0));\n } else {\n let prev_finish = f_spec[i as int - 1] as int;\n let spec_start = if s_spec[i as int] as int > prev_finish { s_spec[i as int] as int } else { prev_finish };\n assert(Self::duration(s_spec, f_spec, i as int) == f_spec[i as int] as int - spec_start);\n assert(start as int == spec_start);\n assert(dur as int == Self::duration(s_spec, f_spec, i as int));\n }\n }\n\n result.push(dur);\n\n proof {\n assert(result[i as int] == dur);\n assert forall |k: int| 0 <= k < i + 1 implies #[trigger] result[k] as int == Self::duration(s_spec, f_spec, k) by {\n if k < i as int {\n } else {\n assert(k == i as int);\n assert(result[k] == dur);\n }\n };\n }\n\n i = i + 1;\n }\n\n result\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn restore_durations(s: Vec, f: Vec) -> Vec {\n let n = s.len();\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n let start = if i == 0 || s[i] > f[i - 1] { s[i] } else { f[i - 1] };\n let dur = f[i] - start;\n result.push(dur);\n i = i + 1;\n }\n result\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let t: usize = tokens.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case: usize = 0;\n while case < t {\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut s: Vec = Vec::with_capacity(n);\n let mut f: Vec = Vec::with_capacity(n);\n let mut idx: usize = 0;\n while idx < n {\n s.push(tokens.next().expect(\"s\").parse().expect(\"valid\"));\n idx = idx + 1;\n }\n idx = 0;\n while idx < n {\n f.push(tokens.next().expect(\"f\").parse().expect(\"valid\"));\n idx = idx + 1;\n }\n let result = Solution::restore_durations(s, f);\n let mut out = String::new();\n idx = 0;\n while idx < n {\n if idx > 0 {\n out.push(' ');\n }\n out.push_str(&result[idx].to_string());\n idx = idx + 1;\n }\n println!(\"{}\", out);\n case = case + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1690C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1690C/tests/mutated_testcases.jsonl"} {"problem": "cf1690D", "family": "codeforces", "description": "# Black and White Stripe\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou have a stripe of checkered paper of length $$$n$$$. Each cell is either white or black.\n\nWhat is the minimum number of cells that must be recolored from white to black in order to have a segment of $$$k$$$ consecutive black cells on the stripe?\n\nIf the input data is such that a segment of $$$k$$$ consecutive black cells already exists, then print `0`.\n\n## Input\n\nThe first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u2014 the number of test cases.\n\nNext, descriptions of $$$t$$$ test cases follow.\n\nThe first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2\\cdot10^5$$$). The second line consists of the letters '`W`' (white) and '`B`' (black). The line length is $$$n$$$.\n\nIt is guaranteed that the sum of values $$$n$$$ does not exceed $$$2\\cdot10^5$$$.\n\n## Output\n\nFor each of $$$t$$$ test cases print an integer\u00a0\u2014 the minimum number of cells that need to be repainted from white to black in order to have a segment of $$$k$$$ consecutive black cells.\n\n## Example\n\n**Input:**\n```\n4\n5 3\nBBWBW\n5 5\nBBWBW\n5 1\nBBWBW\n1 1\nW\n```\n**Output:**\n```\n1\n2\n0\n1\n```\n\n## Note\n\nIn the first test case, $$$s$$$=\"`BBWBW`\" and $$$k=3$$$. It is enough to recolor $$$s_3$$$ and get $$$s$$$=\"`BBBBW`\". This string contains a segment of length $$$k=3$$$ consisting of the letters '`B`'.\n\nIn the second test case of the example $$$s$$$=\"`BBWBW`\" and $$$k=5$$$. It is enough to recolor $$$s_3$$$ and $$$s_5$$$ and get $$$s$$$=\"`BBBBB`\". This string contains a segment of length $$$k=5$$$ consisting of the letters '`B`'.\n\nIn the third test case of the example $$$s$$$=\"`BBWBW`\" and $$$k=1$$$. The string $$$s$$$ already contains a segment of length $$$k=1$$$ consisting of the letters '`B`'.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_recolors(n: usize, k: usize, s: Vec) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n let stripe = it.next().unwrap().as_bytes();\n\n let mut s: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n if stripe[i] == b'W' {\n s.push(1);\n } else {\n s.push(0);\n }\n i += 1;\n }\n\n let ans = Solution::min_recolors(n, k, s);\n println!(\"{}\", ans);\n case_id += 1;\n }\n}\n```\n", "tags": "Rating: 1200\nbrute force, implementation, strings, two pointers\nSolved by: unknown users", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n s[l] as int + Self::sum_range(s, l + 1, r)\n }\n }\n\n pub open spec fn min_white_windows(s: Seq, k: int, processed: int) -> int\n recommends\n 1 <= k <= s.len(),\n 0 <= processed <= s.len() - k + 1,\n decreases processed,\n {\n if processed <= 0 {\n k\n } else {\n let prev = Self::min_white_windows(s, k, processed - 1);\n let start = processed - 1;\n let here = Self::sum_range(s, start, start + k);\n if here < prev {\n here\n } else {\n prev\n }\n }\n }\n\n pub open spec fn valid_window_start(n: int, k: int, start: int) -> bool {\n 0 <= start <= n - k\n }\n\n pub open spec fn whites_in_window(s: Seq, start: int, k: int) -> int\n recommends\n 0 <= start,\n start + k <= s.len(),\n {\n Self::sum_range(s, start, start + k)\n }\n\n pub fn min_recolors(n: usize, k: usize, s: Vec) -> (result: usize)\n requires\n 1 <= n,\n n <= 200000,\n 1 <= k <= n,\n s.len() == n,\n forall|i: int| 0 <= i < n as int ==> (#[trigger] s@[i] == 0 || s@[i] == 1),\n ensures\n exists|start: int|\n Self::valid_window_start(n as int, k as int, start)\n && result as int == Self::whites_in_window(s@, start, k as int),\n forall|start: int|\n Self::valid_window_start(n as int, k as int, start)\n ==> result as int <= Self::whites_in_window(s@, start, k as int),\n {\n }\n}\n\n}", "code": "impl Solution {\n pub fn min_recolors(n: usize, k: usize, s: Vec) -> usize {\n let mut cur: i64 = 0;\n let mut j: usize = 0;\n while j < k {\n cur = cur + s[j];\n j += 1;\n }\n\n let mut best: i64 = cur;\n let mut left: usize = 0;\n\n while left + k < n {\n let next = cur - s[left] + s[left + k];\n cur = next;\n left += 1;\n if cur < best {\n best = cur;\n }\n }\n\n best as usize\n }\n}", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n s[l] as int + Self::sum_range(s, l + 1, r)\n }\n }\n\n pub open spec fn min_white_windows(s: Seq, k: int, processed: int) -> int\n recommends\n 1 <= k <= s.len(),\n 0 <= processed <= s.len() - k + 1,\n decreases processed,\n {\n if processed <= 0 {\n k\n } else {\n let prev = Self::min_white_windows(s, k, processed - 1);\n let start = processed - 1;\n let here = Self::sum_range(s, start, start + k);\n if here < prev {\n here\n } else {\n prev\n }\n }\n }\n\n pub open spec fn valid_window_start(n: int, k: int, start: int) -> bool {\n 0 <= start <= n - k\n }\n\n pub open spec fn whites_in_window(s: Seq, start: int, k: int) -> int\n recommends\n 0 <= start,\n start + k <= s.len(),\n {\n Self::sum_range(s, start, start + k)\n }\n\n pub fn min_recolors(n: usize, k: usize, s: Vec) -> (result: usize)\n requires\n 1 <= n,\n n <= 200000,\n 1 <= k <= n,\n s.len() == n,\n forall|i: int| 0 <= i < n as int ==> (#[trigger] s@[i] == 0 || s@[i] == 1),\n ensures\n exists|start: int|\n Self::valid_window_start(n as int, k as int, start)\n && result as int == Self::whites_in_window(s@, start, k as int),\n forall|start: int|\n Self::valid_window_start(n as int, k as int, start)\n ==> result as int <= Self::whites_in_window(s@, start, k as int),\n {\n let mut cur: i64 = 0;\n let mut j: usize = 0;\n while j < k {\n cur = cur + s[j];\n j += 1;\n }\n\n let mut best: i64 = cur;\n let mut left: usize = 0;\n\n while left + k < n {\n let next = cur - s[left] + s[left + k];\n cur = next;\n left += 1;\n if cur < best {\n best = cur;\n }\n }\n\n best as usize\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n s[l] as int + Self::sum_range(s, l + 1, r)\n }\n }\n\n pub open spec fn min_white_windows(s: Seq, k: int, processed: int) -> int\n recommends\n 1 <= k <= s.len(),\n 0 <= processed <= s.len() - k + 1,\n decreases processed,\n {\n if processed <= 0 {\n k\n } else {\n let prev = Self::min_white_windows(s, k, processed - 1);\n let start = processed - 1;\n let here = Self::sum_range(s, start, start + k);\n if here < prev {\n here\n } else {\n prev\n }\n }\n }\n\n pub open spec fn valid_window_start(n: int, k: int, start: int) -> bool {\n 0 <= start <= n - k\n }\n\n pub open spec fn whites_in_window(s: Seq, start: int, k: int) -> int\n recommends\n 0 <= start,\n start + k <= s.len(),\n {\n Self::sum_range(s, start, start + k)\n }\n\n proof fn lemma_sum_range_nonneg(s: Seq, l: int, r: int)\n requires\n 0 <= l <= r <= s.len(),\n forall|i: int| l <= i < r ==> 0 <= #[trigger] s[i],\n ensures\n 0 <= Self::sum_range(s, l, r),\n decreases r - l,\n {\n if l < r {\n Self::lemma_sum_range_nonneg(s, l + 1, r);\n }\n }\n\n proof fn lemma_sum_range_le_len(s: Seq, l: int, r: int)\n requires\n 0 <= l <= r <= s.len(),\n forall|i: int| l <= i < r ==> #[trigger] s[i] <= 1,\n ensures\n Self::sum_range(s, l, r) <= r - l,\n decreases r - l,\n {\n if l < r {\n Self::lemma_sum_range_le_len(s, l + 1, r);\n assert(Self::sum_range(s, l, r) == s[l] as int + Self::sum_range(s, l + 1, r));\n assert(s[l] as int <= 1);\n assert(Self::sum_range(s, l + 1, r) <= r - (l + 1));\n assert(Self::sum_range(s, l, r) <= 1 + (r - (l + 1)));\n }\n }\n\n proof fn lemma_sum_range_extend_right(s: Seq, l: int, r: int)\n requires\n 0 <= l <= r < s.len(),\n ensures\n Self::sum_range(s, l, r + 1) == Self::sum_range(s, l, r) + s[r] as int,\n decreases r - l,\n {\n if l == r {\n assert(Self::sum_range(s, l, r + 1) == s[l] as int + Self::sum_range(s, l + 1, r + 1));\n assert(Self::sum_range(s, l + 1, r + 1) == 0);\n assert(Self::sum_range(s, l, r) == 0);\n } else {\n Self::lemma_sum_range_extend_right(s, l + 1, r);\n assert(Self::sum_range(s, l, r + 1) == s[l] as int + Self::sum_range(s, l + 1, r + 1));\n assert(Self::sum_range(s, l, r) == s[l] as int + Self::sum_range(s, l + 1, r));\n }\n }\n\n proof fn lemma_min_white_windows_lower_bound(s: Seq, k: int, processed: int, start: int)\n requires\n 1 <= k <= s.len(),\n 0 <= processed <= s.len() - k + 1,\n 0 <= start < processed,\n ensures\n Self::min_white_windows(s, k, processed) <= Self::sum_range(s, start, start + k),\n decreases processed,\n {\n if processed > 0 {\n let prev = Self::min_white_windows(s, k, processed - 1);\n let here = Self::sum_range(s, processed - 1, processed - 1 + k);\n if start == processed - 1 {\n if here < prev {\n assert(Self::min_white_windows(s, k, processed) == here);\n } else {\n assert(Self::min_white_windows(s, k, processed) == prev);\n assert(prev <= here);\n }\n } else {\n Self::lemma_min_white_windows_lower_bound(s, k, processed - 1, start);\n assert(prev <= Self::sum_range(s, start, start + k));\n if here < prev {\n assert(Self::min_white_windows(s, k, processed) == here);\n assert(here <= prev);\n } else {\n assert(Self::min_white_windows(s, k, processed) == prev);\n }\n }\n }\n }\n\n proof fn lemma_min_white_windows_witness(s: Seq, k: int, processed: int) -> (start: int)\n requires\n 1 <= k <= s.len(),\n 1 <= processed <= s.len() - k + 1,\n forall|i: int| 0 <= i < s.len() ==> 0 <= #[trigger] s[i] <= 1,\n ensures\n 0 <= start < processed,\n Self::min_white_windows(s, k, processed) == Self::sum_range(s, start, start + k),\n decreases processed,\n {\n if processed == 1 {\n reveal_with_fuel(Solution::min_white_windows, 2);\n Self::lemma_sum_range_le_len(s, 0, k);\n assert(Self::sum_range(s, 0, k) <= k);\n assert(Self::min_white_windows(s, k, 1) == Self::sum_range(s, 0, k));\n 0\n } else {\n reveal_with_fuel(Solution::min_white_windows, 2);\n let st_prev = Self::lemma_min_white_windows_witness(s, k, processed - 1);\n let prev = Self::min_white_windows(s, k, processed - 1);\n let here = Self::sum_range(s, processed - 1, processed - 1 + k);\n if here < prev {\n assert(Self::min_white_windows(s, k, processed) == here);\n processed - 1\n } else {\n assert(0 <= st_prev < processed - 1);\n assert(Self::min_white_windows(s, k, processed - 1) == Self::sum_range(s, st_prev, st_prev + k));\n assert(Self::min_white_windows(s, k, processed) == prev);\n assert(Self::min_white_windows(s, k, processed) == Self::sum_range(s, st_prev, st_prev + k));\n st_prev\n }\n }\n }\n\n pub fn min_recolors(n: usize, k: usize, s: Vec) -> (result: usize)\n requires\n 1 <= n,\n n <= 200000,\n 1 <= k <= n,\n s.len() == n,\n forall|i: int| 0 <= i < n as int ==> (#[trigger] s@[i] == 0 || s@[i] == 1),\n ensures\n exists|start: int|\n Self::valid_window_start(n as int, k as int, start)\n && result as int == Self::whites_in_window(s@, start, k as int),\n forall|start: int|\n Self::valid_window_start(n as int, k as int, start)\n ==> result as int <= Self::whites_in_window(s@, start, k as int),\n {\n let mut cur: i64 = 0;\n let mut j: usize = 0;\n while j < k\n invariant\n 0 <= j <= k,\n k <= n,\n n == s.len(),\n n <= 200000,\n forall|i: int| 0 <= i < n as int ==> (#[trigger] s@[i] == 0 || s@[i] == 1),\n cur as int == Self::sum_range(s@, 0, j as int),\n 0 <= cur,\n cur <= j as i64,\n decreases k - j,\n {\n if j > 0 {\n proof {\n Self::lemma_sum_range_extend_right(s@, 0, j as int - 1);\n }\n }\n proof {\n assert(0 <= s@[j as int] <= 1);\n assert(cur + s@[j as int] <= 200001);\n }\n cur = cur + s[j];\n j += 1;\n proof {\n if j == 1 {\n assert(Self::sum_range(s@, 0, 1) == s@[0] as int + Self::sum_range(s@, 1, 1));\n assert(Self::sum_range(s@, 1, 1) == 0);\n } else {\n Self::lemma_sum_range_extend_right(s@, 0, j as int - 1);\n assert(Self::sum_range(s@, 0, j as int)\n == Self::sum_range(s@, 0, j as int - 1) + s@[j as int - 1] as int);\n }\n }\n }\n\n let mut best: i64 = cur;\n let mut left: usize = 0;\n\n proof {\n reveal_with_fuel(Solution::min_white_windows, 2);\n Self::lemma_sum_range_le_len(s@, 0, k as int);\n assert(Self::sum_range(s@, 0, k as int) <= k as int);\n assert(best as int == Self::min_white_windows(s@, k as int, 1));\n }\n\n while left + k < n\n invariant\n 0 <= left,\n left + k <= n,\n n == s.len(),\n n <= 200000,\n 1 <= k <= n,\n forall|i: int| 0 <= i < n as int ==> (#[trigger] s@[i] == 0 || s@[i] == 1),\n cur as int == Self::sum_range(s@, left as int, left as int + k as int),\n 0 <= cur <= k as i64,\n 0 <= best <= k as i64,\n best as int == Self::min_white_windows(s@, k as int, left as int + 1),\n decreases n - (left + k),\n {\n proof {\n assert(left + k < n);\n assert(Self::sum_range(s@, left as int, left as int + k as int)\n == s@[left as int] as int + Self::sum_range(s@, left as int + 1, left as int + k as int));\n Self::lemma_sum_range_nonneg(s@, left as int + 1, left as int + k as int);\n Self::lemma_sum_range_le_len(s@, left as int + 1, left as int + k as int);\n Self::lemma_sum_range_extend_right(s@, left as int + 1, left as int + k as int);\n assert(Self::sum_range(s@, left as int + 1, left as int + k as int + 1)\n == Self::sum_range(s@, left as int + 1, left as int + k as int)\n + s@[left as int + k as int] as int);\n assert(Self::sum_range(s@, left as int + 1, left as int + k as int + 1)\n == Self::sum_range(s@, left as int, left as int + k as int)\n - s@[left as int] as int + s@[left as int + k as int] as int);\n assert(0 <= Self::sum_range(s@, left as int + 1, left as int + k as int));\n assert(Self::sum_range(s@, left as int + 1, left as int + k as int) <= k as int - 1);\n assert(0 <= s@[left as int + k as int] <= 1);\n assert(0 <= cur - s@[left as int] + s@[left as int + k as int]);\n assert(cur - s@[left as int] + s@[left as int + k as int] <= k as int);\n }\n\n let next = cur - s[left] + s[left + k];\n cur = next;\n left += 1;\n let ghost old_best = best;\n if cur < best {\n best = cur;\n }\n\n proof {\n assert(cur as int == Self::sum_range(s@, left as int, left as int + k as int));\n assert(old_best as int == Self::min_white_windows(s@, k as int, left as int));\n assert(Self::min_white_windows(s@, k as int, left as int + 1)\n == if Self::sum_range(s@, left as int, left as int + k as int)\n < Self::min_white_windows(s@, k as int, left as int)\n {\n Self::sum_range(s@, left as int, left as int + k as int)\n } else {\n Self::min_white_windows(s@, k as int, left as int)\n });\n assert(best as int == Self::min_white_windows(s@, k as int, left as int + 1));\n }\n }\n\n proof {\n assert(best as int == Self::min_white_windows(s@, k as int, n as int - k as int + 1));\n assert forall|i: int| 0 <= i < s@.len() implies 0 <= #[trigger] s@[i] <= 1 by {\n assert(s@[i] == 0 || s@[i] == 1);\n }\n let st = Self::lemma_min_white_windows_witness(s@, k as int, n as int - k as int + 1);\n assert(Self::valid_window_start(n as int, k as int, st));\n assert(best as int == Self::whites_in_window(s@, st, k as int));\n\n assert forall|start: int|\n Self::valid_window_start(n as int, k as int, start)\n implies best as int <= Self::whites_in_window(s@, start, k as int) by {\n assert(0 <= start < n as int - k as int + 1);\n Self::lemma_min_white_windows_lower_bound(s@, k as int, n as int - k as int + 1, start);\n assert(best as int == Self::min_white_windows(s@, k as int, n as int - k as int + 1));\n }\n }\n\n best as usize\n }\n\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_recolors(n: usize, k: usize, s: Vec) -> usize {\n let mut cur: i64 = 0;\n let mut j: usize = 0;\n while j < k {\n cur = cur + s[j];\n j += 1;\n }\n\n let mut best: i64 = cur;\n let mut left: usize = 0;\n\n while left + k < n {\n let next = cur - s[left] + s[left + k];\n cur = next;\n left += 1;\n if cur < best {\n best = cur;\n }\n }\n\n best as usize\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n let stripe = it.next().unwrap().as_bytes();\n\n let mut s: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n if stripe[i] == b'W' {\n s.push(1);\n } else {\n s.push(0);\n }\n i += 1;\n }\n\n let ans = Solution::min_recolors(n, k, s);\n println!(\"{}\", ans);\n case_id += 1;\n }\n}", "checker": null, "testcases_path": "codeforces/cf1690D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1690D/tests/mutated_testcases.jsonl"} {"problem": "cf1709A", "family": "codeforces", "description": "# Three Doors\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nThere are three doors in front of you, numbered from $$$1$$$ to $$$3$$$ from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.\n\nThere are three keys\u00a0\u2014 one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.\n\nCan you open all the doors?\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 18$$$)\u00a0\u2014 the number of testcases.\n\nThe first line of each testcase contains a single integer $$$x$$$ ($$$1 \\le x \\le 3$$$)\u00a0\u2014 the number on the key in your hands.\n\nThe second line contains three integers $$$a, b$$$ and $$$c$$$ ($$$0 \\le a, b, c \\le 3$$$)\u00a0\u2014 the number on the key behind each of the doors. If there is no key behind the door, the number is equal to $$$0$$$.\n\nValues $$$1, 2$$$ and $$$3$$$ appear exactly once among $$$x, a, b$$$ and $$$c$$$.\n\n## Output\n\nFor each testcase, print \"`YES`\" if you can open all the doors. Otherwise, print \"`NO`\".\n\n## Example\n\n**Input:**\n```\n4\n3\n0 1 2\n1\n0 3 2\n2\n3 1 0\n2\n1 3 0\n```\n**Output:**\n```\nYES\nNO\nYES\nNO\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_open_all_doors(x: i32, a: i32, b: i32, c: i32) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut i: usize = 0;\n while i < t {\n let x: i32 = it.next().unwrap().parse().unwrap();\n let a: i32 = it.next().unwrap().parse().unwrap();\n let b: i32 = it.next().unwrap().parse().unwrap();\n let c: i32 = it.next().unwrap().parse().unwrap();\n let answer = Solution::can_open_all_doors(x, a, b, c);\n if answer {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n i += 1;\n }\n}\n```\n", "tags": "Rating: 800\nbrute force, greedy, implementation, math\nSolved by: 31504 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_value(v: int, x: int, a: int, b: int, c: int) -> int {\n (if x == v {\n 1int\n } else {\n 0\n }) + (if a == v {\n 1int\n } else {\n 0\n }) + (if b == v {\n 1int\n } else {\n 0\n }) + (if c == v {\n 1int\n } else {\n 0\n })\n }\n\n pub open spec fn key_behind(door: int, a: int, b: int, c: int) -> int {\n if door == 1 {\n a\n } else if door == 2 {\n b\n } else {\n c\n }\n }\n\n pub open spec fn can_open_spec(x: int, a: int, b: int, c: int) -> bool {\n if Self::key_behind(x, a, b, c) <= 0 {\n false\n } else {\n Self::key_behind(Self::key_behind(x, a, b, c), a, b, c) > 0\n }\n }\n\n pub fn can_open_all_doors(x: i32, a: i32, b: i32, c: i32) -> (result: bool)\n requires\n 1 <= x <= 3,\n 0 <= a <= 3,\n 0 <= b <= 3,\n 0 <= c <= 3,\n Self::count_value(1, x as int, a as int, b as int, c as int) == 1,\n Self::count_value(2, x as int, a as int, b as int, c as int) == 1,\n Self::count_value(3, x as int, a as int, b as int, c as int) == 1,\n ensures\n result == Self::can_open_spec(x as int, a as int, b as int, c as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_open_all_doors(x: i32, a: i32, b: i32, c: i32) -> bool {\n let y = if x == 1 {\n a\n } else if x == 2 {\n b\n } else {\n c\n };\n if y == 0 {\n false\n } else {\n let z = if y == 1 {\n a\n } else if y == 2 {\n b\n } else {\n c\n };\n z != 0\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_value(v: int, x: int, a: int, b: int, c: int) -> int {\n (if x == v {\n 1int\n } else {\n 0\n }) + (if a == v {\n 1int\n } else {\n 0\n }) + (if b == v {\n 1int\n } else {\n 0\n }) + (if c == v {\n 1int\n } else {\n 0\n })\n }\n\n pub open spec fn key_behind(door: int, a: int, b: int, c: int) -> int {\n if door == 1 {\n a\n } else if door == 2 {\n b\n } else {\n c\n }\n }\n\n pub open spec fn can_open_spec(x: int, a: int, b: int, c: int) -> bool {\n if Self::key_behind(x, a, b, c) <= 0 {\n false\n } else {\n Self::key_behind(Self::key_behind(x, a, b, c), a, b, c) > 0\n }\n }\n\n pub fn can_open_all_doors(x: i32, a: i32, b: i32, c: i32) -> (result: bool)\n requires\n 1 <= x <= 3,\n 0 <= a <= 3,\n 0 <= b <= 3,\n 0 <= c <= 3,\n Self::count_value(1, x as int, a as int, b as int, c as int) == 1,\n Self::count_value(2, x as int, a as int, b as int, c as int) == 1,\n Self::count_value(3, x as int, a as int, b as int, c as int) == 1,\n ensures\n result == Self::can_open_spec(x as int, a as int, b as int, c as int),\n {\n let y = if x == 1 {\n a\n } else if x == 2 {\n b\n } else {\n c\n };\n if y == 0 {\n false\n } else {\n let z = if y == 1 {\n a\n } else if y == 2 {\n b\n } else {\n c\n };\n z != 0\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_value(v: int, x: int, a: int, b: int, c: int) -> int {\n (if x == v {\n 1int\n } else {\n 0\n }) + (if a == v {\n 1int\n } else {\n 0\n }) + (if b == v {\n 1int\n } else {\n 0\n }) + (if c == v {\n 1int\n } else {\n 0\n })\n }\n\n pub open spec fn key_behind(door: int, a: int, b: int, c: int) -> int {\n if door == 1 {\n a\n } else if door == 2 {\n b\n } else {\n c\n }\n }\n\n pub open spec fn can_open_spec(x: int, a: int, b: int, c: int) -> bool {\n if Self::key_behind(x, a, b, c) <= 0 {\n false\n } else {\n Self::key_behind(Self::key_behind(x, a, b, c), a, b, c) > 0\n }\n }\n\n pub fn can_open_all_doors(x: i32, a: i32, b: i32, c: i32) -> (result: bool)\n requires\n 1 <= x <= 3,\n 0 <= a <= 3,\n 0 <= b <= 3,\n 0 <= c <= 3,\n Self::count_value(1, x as int, a as int, b as int, c as int) == 1,\n Self::count_value(2, x as int, a as int, b as int, c as int) == 1,\n Self::count_value(3, x as int, a as int, b as int, c as int) == 1,\n ensures\n result == Self::can_open_spec(x as int, a as int, b as int, c as int),\n {\n let y = if x == 1 {\n a\n } else if x == 2 {\n b\n } else {\n c\n };\n if y == 0 {\n proof {\n assert(Self::key_behind(x as int, a as int, b as int, c as int) == 0);\n assert(Self::can_open_spec(x as int, a as int, b as int, c as int) == false);\n }\n false\n } else {\n let z = if y == 1 {\n a\n } else if y == 2 {\n b\n } else {\n c\n };\n proof {\n assert(x == 1 || x == 2 || x == 3);\n assert(y == Self::key_behind(x as int, a as int, b as int, c as int));\n assert(y != 0);\n assert(Self::key_behind(x as int, a as int, b as int, c as int) > 0);\n assert(z == Self::key_behind(y as int, a as int, b as int, c as int));\n if z == 0 {\n assert(Self::can_open_spec(x as int, a as int, b as int, c as int) == false);\n } else {\n assert(Self::can_open_spec(x as int, a as int, b as int, c as int) == true);\n }\n }\n z != 0\n }\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_open_all_doors(x: i32, a: i32, b: i32, c: i32) -> bool {\n let y = if x == 1 {\n a\n } else if x == 2 {\n b\n } else {\n c\n };\n if y == 0 {\n false\n } else {\n let z = if y == 1 {\n a\n } else if y == 2 {\n b\n } else {\n c\n };\n z != 0\n }\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut i: usize = 0;\n while i < t {\n let x: i32 = it.next().unwrap().parse().unwrap();\n let a: i32 = it.next().unwrap().parse().unwrap();\n let b: i32 = it.next().unwrap().parse().unwrap();\n let c: i32 = it.next().unwrap().parse().unwrap();\n let answer = Solution::can_open_all_doors(x, a, b, c);\n if answer {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n i += 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1709A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1709A/tests/mutated_testcases.jsonl"} {"problem": "cf1714A", "family": "codeforces", "description": "# Everyone Loves to Sleep\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nVlad, like everyone else, loves to sleep very much.\n\nEvery day Vlad has to do $$$n$$$ things, each at a certain time. For each of these things, he has an alarm clock set, the $$$i$$$-th of them is triggered on $$$h_i$$$ hours $$$m_i$$$ minutes every day ($$$0 \\le h_i \\lt 24, 0 \\le m_i \\lt 60$$$). Vlad uses the $$$24$$$-hour time format, so after $$$h=12, m=59$$$ comes $$$h=13, m=0$$$ and after $$$h=23, m=59$$$ comes $$$h=0, m=0$$$.\n\nThis time Vlad went to bed at $$$H$$$ hours $$$M$$$ minutes ($$$0 \\le H \\lt 24, 0 \\le M \\lt 60$$$) and asks you to answer: how much he will be able to sleep until the next alarm clock.\n\nIf any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length $$$0$$$.\n\n## Input\n\nThe first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases in the test.\n\nThe first line of the case contains three integers $$$n$$$, $$$H$$$ and $$$M$$$ ($$$1 \\le n \\le 10, 0 \\le H \\lt 24, 0 \\le M \\lt 60$$$) \u2014 the number of alarms and the time Vlad went to bed.\n\nThe following $$$n$$$ lines contain two numbers each $$$h_i$$$ and $$$m_i$$$ ($$$0 \\le h_i \\lt 24, 0 \\le m_i \\lt 60$$$) \u2014 the time of the $$$i$$$ alarm. It is acceptable that two or more alarms will trigger at the same time.\n\nNumbers describing time do not contain leading zeros.\n\n## Output\n\nOutput $$$t$$$ lines, each containing the answer to the corresponding test case. As an answer, output two numbers \u00a0\u2014 the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be `0 0`.\n\n## Example\n\n**Input:**\n```\n3\n1 6 13\n8 0\n3 6 0\n12 30\n14 45\n6 0\n2 23 35\n20 15\n10 30\n```\n**Output:**\n```\n1 47\n0 0\n10 55\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nfn next_usize<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nfn next_i32<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nimpl Solution {\n pub fn min_wait_minutes(now: i32, alarms: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n let h: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let m: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let now = h * 60 + m;\n\n let mut alarms: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let ah: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let am: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n alarms.push(ah * 60 + am);\n i = i + 1;\n }\n\n let ans = Solution::min_wait_minutes(now, alarms);\n println!(\"{} {}\", ans / 60, ans % 60);\n\n case_id = case_id + 1;\n }\n}\n```\n", "tags": "Rating: 900\nimplementation, math\nSolved by: 32743 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_wait_minutes(now: int, alarm: int) -> int {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn min_wait_minutes(now: i32, alarms: Vec) -> (res: i32)\n requires\n 0 <= now < 1440,\n 1 <= alarms.len() <= 10,\n forall|j: int| 0 <= j < alarms.len() as int ==> 0 <= #[trigger] alarms[j] < 1440,\n ensures\n 0 <= res < 1440,\n exists|j: int|\n 0 <= j < alarms.len() as int\n && res as int == Self::spec_wait_minutes(now as int, alarms[j] as int),\n forall|j: int|\n 0 <= j < alarms.len() as int\n ==> res as int <= #[trigger] Self::spec_wait_minutes(now as int, alarms[j] as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn wait_minutes(now: i32, alarm: i32) -> i32 {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn min_wait_minutes(now: i32, alarms: Vec) -> i32 {\n let n = alarms.len();\n let mut best = Self::wait_minutes(now, alarms[0]);\n let mut i: usize = 1;\n while i < n {\n let d = Self::wait_minutes(now, alarms[i]);\n if d < best {\n best = d;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_wait_minutes(now: int, alarm: int) -> int {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn wait_minutes(now: i32, alarm: i32) -> (res: i32)\n requires\n 0 <= now < 1440,\n 0 <= alarm < 1440,\n ensures\n res as int == Self::spec_wait_minutes(now as int, alarm as int),\n 0 <= res < 1440,\n {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn min_wait_minutes(now: i32, alarms: Vec) -> (res: i32)\n requires\n 0 <= now < 1440,\n 1 <= alarms.len() <= 10,\n forall|j: int| 0 <= j < alarms.len() as int ==> 0 <= #[trigger] alarms[j] < 1440,\n ensures\n 0 <= res < 1440,\n exists|j: int|\n 0 <= j < alarms.len() as int\n && res as int == Self::spec_wait_minutes(now as int, alarms[j] as int),\n forall|j: int|\n 0 <= j < alarms.len() as int\n ==> res as int <= #[trigger] Self::spec_wait_minutes(now as int, alarms[j] as int),\n {\n let n = alarms.len();\n let mut best = Self::wait_minutes(now, alarms[0]);\n let mut i: usize = 1;\n while i < n {\n let d = Self::wait_minutes(now, alarms[i]);\n if d < best {\n best = d;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_wait_minutes(now: int, alarm: int) -> int {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn wait_minutes(now: i32, alarm: i32) -> (res: i32)\n requires\n 0 <= now < 1440,\n 0 <= alarm < 1440,\n ensures\n res as int == Self::spec_wait_minutes(now as int, alarm as int),\n 0 <= res < 1440,\n {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn min_wait_minutes(now: i32, alarms: Vec) -> (res: i32)\n requires\n 0 <= now < 1440,\n 1 <= alarms.len() <= 10,\n forall|j: int| 0 <= j < alarms.len() as int ==> 0 <= #[trigger] alarms[j] < 1440,\n ensures\n 0 <= res < 1440,\n exists|j: int|\n 0 <= j < alarms.len() as int\n && res as int == Self::spec_wait_minutes(now as int, alarms[j] as int),\n forall|j: int|\n 0 <= j < alarms.len() as int\n ==> res as int <= #[trigger] Self::spec_wait_minutes(now as int, alarms[j] as int),\n {\n let n = alarms.len();\n let mut best = Self::wait_minutes(now, alarms[0]);\n let ghost mut best_idx: usize = 0;\n let mut i: usize = 1;\n while i < n\n invariant\n 0 <= now < 1440,\n n == alarms.len(),\n 1 <= n <= 10,\n 1 <= i <= n,\n forall|j: int| 0 <= j < alarms.len() as int ==> 0 <= #[trigger] alarms[j] < 1440,\n 0 <= best < 1440,\n best_idx < i,\n best as int == Self::spec_wait_minutes(now as int, alarms[best_idx as int] as int),\n forall|j: int|\n 0 <= j < i as int\n ==> best as int <= #[trigger] Self::spec_wait_minutes(now as int, alarms[j] as int),\n decreases n - i,\n {\n let ghost old_best = best;\n let d = Self::wait_minutes(now, alarms[i]);\n if d < best {\n best = d;\n proof { best_idx = i; }\n }\n proof {\n assert(d as int == Self::spec_wait_minutes(now as int, alarms[i as int] as int));\n if d < old_best {\n assert(best == d);\n } else {\n assert(best == old_best);\n assert(old_best as int <= d as int);\n }\n assert(best as int <= d as int);\n assert forall|j: int|\n 0 <= j < i as int + 1 implies best as int <= #[trigger] Self::spec_wait_minutes(now as int, alarms[j] as int) by {\n if j < i as int {\n assert(old_best as int <= Self::spec_wait_minutes(now as int, alarms[j] as int));\n assert(best as int <= old_best as int);\n } else {\n assert(j == i as int);\n assert(best as int <= d as int);\n }\n };\n }\n i = i + 1;\n }\n proof {\n assert(i == n);\n assert(best_idx < alarms.len());\n }\n best\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nfn next_usize<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nfn next_i32<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nimpl Solution {\n pub fn wait_minutes(now: i32, alarm: i32) -> i32 {\n if alarm >= now {\n alarm - now\n } else {\n alarm + 1440 - now\n }\n }\n\n pub fn min_wait_minutes(now: i32, alarms: Vec) -> i32 {\n let n = alarms.len();\n let mut best = Self::wait_minutes(now, alarms[0]);\n let mut i: usize = 1;\n while i < n {\n let d = Self::wait_minutes(now, alarms[i]);\n if d < best {\n best = d;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n let h: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let m: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let now = h * 60 + m;\n\n let mut alarms: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let ah: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let am: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n alarms.push(ah * 60 + am);\n i = i + 1;\n }\n\n let ans = Solution::min_wait_minutes(now, alarms);\n println!(\"{} {}\", ans / 60, ans % 60);\n\n case_id = case_id + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1714A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1714A/tests/mutated_testcases.jsonl"} {"problem": "cf1721A", "family": "codeforces", "description": "# Image\n\nTime limit: 2 seconds | Memory limit: 512 megabytes\n\nYou have an image file of size $$$2 \\times 2$$$, consisting of $$$4$$$ pixels. Each pixel can have one of $$$26$$$ different colors, denoted by lowercase Latin letters.\n\nYou want to recolor some of the pixels of the image **so that all $$$4$$$ pixels have the same color**. In one move, you can choose **no more than two** pixels **of the same color** and paint them into some other color **(if you choose two pixels, both should be painted into the same color)**.\n\nWhat is the minimum number of moves you have to make in order to fulfill your goal?\n\n## Input\n\nThe first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) \u2014 the number of test cases.\n\nEach test case consists of two lines. Each of these lines contains two lowercase letters of Latin alphabet **without any separators**, denoting a row of pixels in the image.\n\n## Output\n\nFor each test case, print one integer \u2014 the minimum number of moves you have to make so that all $$$4$$$ pixels of the image have the same color.\n\n## Example\n\n**Input:**\n```\n5\nrb\nbr\ncc\nwb\naa\naa\nab\ncd\nyy\nxx\n```\n**Output:**\n```\n1\n2\n0\n3\n1\n```\n\n## Note\n\nLet's analyze the test cases of the example.\n\nIn the first test case, you can paint the bottom left pixel and the top right pixel (which share the same color) into the color `r`, so all pixels have this color.\n\nIn the second test case, two moves are enough:\n - paint both top pixels, which have the same color `c`, into the color `b`; \n- paint the bottom left pixel into the color `b`. \n\nIn the third test case, all pixels already have the same color.\n\nIn the fourth test case, you may leave any of the pixels unchanged, and paint all three other pixels into the color of that pixel in three moves.\n\nIn the fifth test case, you can paint both top pixels into the color `x`.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_moves_to_uniform(a: u8, b: u8, c: u8, d: u8) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let row0: String = it.next().unwrap().to_string();\n let row1: String = it.next().unwrap().to_string();\n let b0: u8 = row0.as_bytes()[0];\n let b1: u8 = row0.as_bytes()[1];\n let b2: u8 = row1.as_bytes()[0];\n let b3: u8 = row1.as_bytes()[1];\n let a: u8 = b0 - b'a';\n let b: u8 = b1 - b'a';\n let c: u8 = b2 - b'a';\n let d: u8 = b3 - b'a';\n let ans = Solution::min_moves_to_uniform(a, b, c, d);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n```\n", "tags": "Rating: 800\ngreedy, implementation\nSolved by: 28248 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn letter_mask(a: u8, b: u8, c: u8, d: u8) -> u32 {\n (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d)\n }\n\n pub open spec fn popcount_prefix(mask: u32, k: int) -> int\n decreases k,\n {\n if k <= 0 {\n 0int\n } else {\n let bit = if (mask >> ((k - 1) as u8)) & 1u32 == 0u32 {\n 0int\n } else {\n 1int\n };\n Self::popcount_prefix(mask, k - 1) + bit\n }\n }\n\n pub open spec fn distinct_letters(a: u8, b: u8, c: u8, d: u8) -> int {\n Self::popcount_prefix(Self::letter_mask(a, b, c, d), 26)\n }\n\n pub open spec fn expected_moves(a: u8, b: u8, c: u8, d: u8) -> int {\n let dc = Self::distinct_letters(a, b, c, d);\n if dc == 1 {\n 0int\n } else if dc == 2 {\n 1int\n } else if dc == 3 {\n 2int\n } else {\n 3int\n }\n }\n\n pub fn min_moves_to_uniform(a: u8, b: u8, c: u8, d: u8) -> (result: i32)\n requires\n a < 26,\n b < 26,\n c < 26,\n d < 26,\n ensures\n result as int == Self::expected_moves(a, b, c, d),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn popcount_mask26(mask: u32) -> i32 {\n let mut d: i32 = 0;\n let mut k: u8 = 0;\n while k < 26 {\n let pre_d = d;\n let bit: i32 = if (mask >> k) & 1u32 == 0u32 {\n 0\n } else {\n 1\n };\n d = pre_d + bit;\n k = k + 1;\n }\n d\n }\n\n pub fn min_moves_to_uniform(a: u8, b: u8, c: u8, d: u8) -> i32 {\n let mask: u32 = (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d);\n let distinct_i: i32 = Self::popcount_mask26(mask);\n let r = if distinct_i == 1 {\n 0\n } else if distinct_i == 2 {\n 1\n } else if distinct_i == 3 {\n 2\n } else {\n 3\n };\n r\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn letter_mask(a: u8, b: u8, c: u8, d: u8) -> u32 {\n (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d)\n }\n\n pub open spec fn popcount_prefix(mask: u32, k: int) -> int\n decreases k,\n {\n if k <= 0 {\n 0int\n } else {\n let bit = if (mask >> ((k - 1) as u8)) & 1u32 == 0u32 {\n 0int\n } else {\n 1int\n };\n Self::popcount_prefix(mask, k - 1) + bit\n }\n }\n\n pub open spec fn distinct_letters(a: u8, b: u8, c: u8, d: u8) -> int {\n Self::popcount_prefix(Self::letter_mask(a, b, c, d), 26)\n }\n\n pub open spec fn expected_moves(a: u8, b: u8, c: u8, d: u8) -> int {\n let dc = Self::distinct_letters(a, b, c, d);\n if dc == 1 {\n 0int\n } else if dc == 2 {\n 1int\n } else if dc == 3 {\n 2int\n } else {\n 3int\n }\n }\n\n pub fn popcount_mask26(mask: u32) -> (d: i32)\n ensures\n d as int == Self::popcount_prefix(mask, 26),\n {\n let mut d: i32 = 0;\n let mut k: u8 = 0;\n while k < 26 {\n let pre_d = d;\n let bit: i32 = if (mask >> k) & 1u32 == 0u32 {\n 0\n } else {\n 1\n };\n d = pre_d + bit;\n k = k + 1;\n }\n d\n }\n\n pub fn min_moves_to_uniform(a: u8, b: u8, c: u8, d: u8) -> (result: i32)\n requires\n a < 26,\n b < 26,\n c < 26,\n d < 26,\n ensures\n result as int == Self::expected_moves(a, b, c, d),\n {\n let mask: u32 = (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d);\n let distinct_i: i32 = Self::popcount_mask26(mask);\n let r = if distinct_i == 1 {\n 0\n } else if distinct_i == 2 {\n 1\n } else if distinct_i == 3 {\n 2\n } else {\n 3\n };\n r\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn letter_mask(a: u8, b: u8, c: u8, d: u8) -> u32 {\n (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d)\n }\n\n pub open spec fn popcount_prefix(mask: u32, k: int) -> int\n decreases k,\n {\n if k <= 0 {\n 0int\n } else {\n let bit = if (mask >> ((k - 1) as u8)) & 1u32 == 0u32 {\n 0int\n } else {\n 1int\n };\n Self::popcount_prefix(mask, k - 1) + bit\n }\n }\n\n pub open spec fn distinct_letters(a: u8, b: u8, c: u8, d: u8) -> int {\n Self::popcount_prefix(Self::letter_mask(a, b, c, d), 26)\n }\n\n pub open spec fn expected_moves(a: u8, b: u8, c: u8, d: u8) -> int {\n let dc = Self::distinct_letters(a, b, c, d);\n if dc == 1 {\n 0int\n } else if dc == 2 {\n 1int\n } else if dc == 3 {\n 2int\n } else {\n 3int\n }\n }\n\n proof fn lemma_popcount_prefix_unfold(mask: u32, k: int)\n requires\n k >= 1,\n ensures\n Self::popcount_prefix(mask, k) == Self::popcount_prefix(mask, k - 1)\n + if (mask >> ((k - 1) as u8)) & 1u32 == 0u32 {\n 0int\n } else {\n 1int\n },\n {\n reveal_with_fuel(Solution::popcount_prefix, 2);\n }\n\n proof fn lemma_expected_branch(distinct_i: i32, a: u8, b: u8, c: u8, d: u8)\n requires\n distinct_i as int == Self::distinct_letters(a, b, c, d),\n ensures\n (if distinct_i == 1 {\n 0int\n } else if distinct_i == 2 {\n 1int\n } else if distinct_i == 3 {\n 2int\n } else {\n 3int\n }) == Self::expected_moves(a, b, c, d),\n {\n let dc = Self::distinct_letters(a, b, c, d);\n assert(distinct_i as int == dc);\n assert(Self::expected_moves(a, b, c, d) == if dc == 1 {\n 0int\n } else if dc == 2 {\n 1int\n } else if dc == 3 {\n 2int\n } else {\n 3int\n });\n }\n\n pub fn popcount_mask26(mask: u32) -> (d: i32)\n ensures\n d as int == Self::popcount_prefix(mask, 26),\n {\n let mut d: i32 = 0;\n let mut k: u8 = 0;\n while k < 26\n invariant\n k <= 26,\n d as int == Self::popcount_prefix(mask, k as int),\n d as int <= k as int,\n decreases 26 - k as int,\n {\n proof {\n Self::lemma_popcount_prefix_unfold(mask, (k as int) + 1);\n assert((k as int) < 26);\n assert(d as int <= k as int);\n }\n let pre_d = d;\n let bit: i32 = if (mask >> k) & 1u32 == 0u32 {\n 0\n } else {\n 1\n };\n proof {\n assert(bit as int == if (mask >> k) & 1u32 == 0u32 {\n 0int\n } else {\n 1int\n });\n assert(pre_d + bit <= 26);\n }\n d = pre_d + bit;\n proof {\n assert(d as int == Self::popcount_prefix(mask, (k as int) + 1));\n assert(d as int <= (k as int) + 1);\n }\n k = k + 1;\n }\n d\n }\n\n pub fn min_moves_to_uniform(a: u8, b: u8, c: u8, d: u8) -> (result: i32)\n requires\n a < 26,\n b < 26,\n c < 26,\n d < 26,\n ensures\n result as int == Self::expected_moves(a, b, c, d),\n {\n let mask: u32 = (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d);\n proof {\n assert(mask == Self::letter_mask(a, b, c, d));\n }\n let distinct_i: i32 = Self::popcount_mask26(mask);\n proof {\n assert(distinct_i as int == Self::distinct_letters(a, b, c, d));\n }\n let r = if distinct_i == 1 {\n 0\n } else if distinct_i == 2 {\n 1\n } else if distinct_i == 3 {\n 2\n } else {\n 3\n };\n proof {\n Self::lemma_expected_branch(distinct_i, a, b, c, d);\n assert(r as int == Self::expected_moves(a, b, c, d));\n }\n r\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn popcount_mask26(mask: u32) -> i32 {\n let mut d: i32 = 0;\n let mut k: u8 = 0;\n while k < 26 {\n let pre_d = d;\n let bit: i32 = if (mask >> k) & 1u32 == 0u32 {\n 0\n } else {\n 1\n };\n d = pre_d + bit;\n k = k + 1;\n }\n d\n }\n\n pub fn min_moves_to_uniform(a: u8, b: u8, c: u8, d: u8) -> i32 {\n let mask: u32 = (1u32 << a) | (1u32 << b) | (1u32 << c) | (1u32 << d);\n let distinct_i: i32 = Self::popcount_mask26(mask);\n let r = if distinct_i == 1 {\n 0\n } else if distinct_i == 2 {\n 1\n } else if distinct_i == 3 {\n 2\n } else {\n 3\n };\n r\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let row0: String = it.next().unwrap().to_string();\n let row1: String = it.next().unwrap().to_string();\n let b0: u8 = row0.as_bytes()[0];\n let b1: u8 = row0.as_bytes()[1];\n let b2: u8 = row1.as_bytes()[0];\n let b3: u8 = row1.as_bytes()[1];\n let a: u8 = b0 - b'a';\n let b: u8 = b1 - b'a';\n let c: u8 = b2 - b'a';\n let d: u8 = b3 - b'a';\n let ans = Solution::min_moves_to_uniform(a, b, c, d);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1721A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1721A/tests/mutated_testcases.jsonl"} {"problem": "cf1730A", "family": "codeforces", "description": "# Planets\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nOne day, Vogons wanted to build a new hyperspace highway through a distant system with $$$n$$$ planets. The $$$i$$$-th planet is on the orbit $$$a_i$$$, there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.\n\nVogons have two machines to do that.\n - The first machine in one operation can destroy any planet at cost of $$$1$$$ Triganic Pu. \n- The second machine in one operation can destroy all planets on a single orbit in this system at the cost of $$$c$$$ Triganic Pus. \n\nVogons can use each machine as many times as they want.\n\nVogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project?\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases. Then the test cases follow.\n\nEach test case consists of two lines.\n\nThe first line contains two integers $$$n$$$ and $$$c$$$ ($$$1 \\le n, c \\le 100$$$) \u2014 the number of planets and the cost of the second machine usage.\n\nThe second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the orbit of the $$$i$$$-th planet.\n\n## Output\n\nFor each test case print a single integer \u2014 the minimum cost of destroying all planets.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4\n10 1\n2 1 4 5 2 4 5 5 1 2\n5 2\n3 2 1 2 2\n2 2\n1 1\n2 2\n1 2\n```\n**Output:**\n```\n4\n4\n2\n2\n```\n\n### Example 2\n\n**Input:**\n```\n1\n1 100\n1\n```\n**Output:**\n```\n1\n```\n\n## Note\n\nIn the first test case, the cost of using both machines is the same, so you can always use the second one and destroy all planets in orbit $$$1$$$, all planets in orbit $$$2$$$, all planets in orbit $$$4$$$, all planets in orbit $$$5$$$.\n\nIn the second test case, it is advantageous to use the second machine for $$$2$$$ Triganic Pus to destroy all the planets in orbit $$$2$$$, then destroy the remaining two planets using the first machine.\n\nIn the third test case, you can use the first machine twice or the second machine once.\n\nIn the fourth test case, it is advantageous to use the first machine twice.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_destroy_cost(orbits: Vec, c: i32) -> i32 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let mut tc = 0usize;\n while tc < t {\n let nc: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let c = nc[1];\n let orbits: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::min_destroy_cost(orbits, c);\n println!(\"{}\", ans);\n tc = tc + 1;\n }\n}\n```\n", "tags": "Rating: 800\ndata structures, greedy, sortings\nSolved by: 24707 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_count(a: Seq, i: int, v: int) -> int\n decreases i,\n{\n if i <= 0 {\n 0int\n } else {\n spec_prefix_count(a, i - 1, v) + if a[i - 1] as int == v { 1int } else { 0int }\n }\n}\n\npub open spec fn spec_orbit_contrib(k: int, c: int) -> int {\n if k <= 0 {\n 0int\n } else if k < c {\n k\n } else {\n c\n }\n}\n\npub open spec fn spec_sum_min_cost(a: Seq, c: int, vmax: int) -> int\n decreases vmax,\n{\n if vmax <= 0 {\n 0int\n } else {\n let k = spec_prefix_count(a, a.len() as int, vmax);\n spec_orbit_contrib(k, c) + spec_sum_min_cost(a, c, vmax - 1)\n }\n}\n\nimpl Solution {\n pub fn min_destroy_cost(orbits: Vec, c: i32) -> (res: i32)\n requires\n 1 <= orbits.len() <= 100,\n 1 <= c <= 100,\n forall|i: int| 0 <= i < orbits.len() ==> 1 <= #[trigger] orbits@[i] <= 100,\n ensures\n res as int == spec_sum_min_cost(orbits@, c as int, 100),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_destroy_cost(orbits: Vec, c: i32) -> i32 {\n let n = orbits.len();\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101 {\n cnt.push(0);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n {\n let x = orbits[i] as usize;\n let prev = cnt[x];\n cnt[x] = prev + 1;\n i = i + 1;\n }\n let mut ans = 0i32;\n let mut v = 1usize;\n while v <= 100 {\n let k = cnt[v];\n let add = if k < c {\n k\n } else {\n c\n };\n ans = ans + add;\n v = v + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_count(a: Seq, i: int, v: int) -> int\n decreases i,\n{\n if i <= 0 {\n 0int\n } else {\n spec_prefix_count(a, i - 1, v) + if a[i - 1] as int == v { 1int } else { 0int }\n }\n}\n\npub open spec fn spec_orbit_contrib(k: int, c: int) -> int {\n if k <= 0 {\n 0int\n } else if k < c {\n k\n } else {\n c\n }\n}\n\npub open spec fn spec_sum_min_cost(a: Seq, c: int, vmax: int) -> int\n decreases vmax,\n{\n if vmax <= 0 {\n 0int\n } else {\n let k = spec_prefix_count(a, a.len() as int, vmax);\n spec_orbit_contrib(k, c) + spec_sum_min_cost(a, c, vmax - 1)\n }\n}\n\nimpl Solution {\n pub fn min_destroy_cost(orbits: Vec, c: i32) -> (res: i32)\n requires\n 1 <= orbits.len() <= 100,\n 1 <= c <= 100,\n forall|i: int| 0 <= i < orbits.len() ==> 1 <= #[trigger] orbits@[i] <= 100,\n ensures\n res as int == spec_sum_min_cost(orbits@, c as int, 100),\n {\n let n = orbits.len();\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101 {\n cnt.push(0);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n {\n let x = orbits[i] as usize;\n let prev = cnt[x];\n cnt.set(x, prev + 1);\n i = i + 1;\n }\n let mut ans = 0i32;\n let mut v = 1usize;\n while v <= 100 {\n let k = cnt[v];\n let add = if k < c {\n k\n } else {\n c\n };\n ans = ans + add;\n v = v + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_count(a: Seq, i: int, v: int) -> int\n decreases i,\n{\n if i <= 0 {\n 0int\n } else {\n spec_prefix_count(a, i - 1, v) + if a[i - 1] as int == v { 1int } else { 0int }\n }\n}\n\npub open spec fn spec_orbit_contrib(k: int, c: int) -> int {\n if k <= 0 {\n 0int\n } else if k < c {\n k\n } else {\n c\n }\n}\n\npub open spec fn spec_sum_min_cost(a: Seq, c: int, vmax: int) -> int\n decreases vmax,\n{\n if vmax <= 0 {\n 0int\n } else {\n let k = spec_prefix_count(a, a.len() as int, vmax);\n spec_orbit_contrib(k, c) + spec_sum_min_cost(a, c, vmax - 1)\n }\n}\n\nproof fn lemma_i32_lt_implies_int_lt(x: i32, y: i32)\n requires\n x < y,\n ensures\n (x as int) < (y as int),\n{\n}\n\nproof fn lemma_i32_ge_implies_int_ge(x: i32, y: i32)\n requires\n x >= y,\n ensures\n (x as int) >= (y as int),\n{\n}\n\nproof fn lemma_orbit_contrib_le_c(k: int, c: int)\n requires\n 1 <= c,\n ensures\n spec_orbit_contrib(k, c) <= c,\n{\n if k <= 0 {\n } else if k < c {\n assert(spec_orbit_contrib(k, c) == k);\n assert(k < c);\n } else {\n assert(spec_orbit_contrib(k, c) == c);\n }\n}\n\nproof fn lemma_spec_prefix_count_nonneg(a: Seq, i: int, v: int)\n requires\n 0 <= i <= a.len(),\n ensures\n spec_prefix_count(a, i, v) >= 0,\n decreases i,\n{\n if i <= 0 {\n } else {\n lemma_spec_prefix_count_nonneg(a, i - 1, v);\n }\n}\n\nproof fn lemma_sum_step(a: Seq, c: int, v: int, n: int)\n requires\n 1 <= v <= 100,\n n == a.len(),\n 0 <= n <= 100,\n ensures\n spec_sum_min_cost(a, c, v) == spec_orbit_contrib(spec_prefix_count(a, n, v), c)\n + spec_sum_min_cost(a, c, v - 1),\n{\n assert(spec_sum_min_cost(a, c, v) == ({\n let k = spec_prefix_count(a, a.len() as int, v);\n spec_orbit_contrib(k, c) + spec_sum_min_cost(a, c, v - 1)\n }));\n assert(spec_prefix_count(a, a.len() as int, v) == spec_prefix_count(a, n, v));\n}\n\nproof fn lemma_count_le_i(a: Seq, i: int, v: int)\n requires\n 0 <= i <= a.len(),\n ensures\n spec_prefix_count(a, i, v) <= i,\n decreases i,\n{\n if i <= 0 {\n } else {\n lemma_count_le_i(a, i - 1, v);\n assert(spec_prefix_count(a, i, v) <= spec_prefix_count(a, i - 1, v) + 1);\n assert(spec_prefix_count(a, i, v) <= i);\n }\n}\n\nproof fn lemma_prefix_other(a: Seq, i: int, j: int)\n requires\n 0 <= i < a.len(),\n 0 <= j < 101,\n a[i] as int != j,\n ensures\n spec_prefix_count(a, i + 1, j) == spec_prefix_count(a, i, j),\n{\n}\n\nproof fn lemma_prefix_hit(a: Seq, i: int, j: int)\n requires\n 0 <= i < a.len(),\n a[i] as int == j,\n ensures\n spec_prefix_count(a, i + 1, j) == spec_prefix_count(a, i, j) + 1,\n{\n}\n\nimpl Solution {\n pub fn min_destroy_cost(orbits: Vec, c: i32) -> (res: i32)\n requires\n 1 <= orbits.len() <= 100,\n 1 <= c <= 100,\n forall|i: int| 0 <= i < orbits.len() ==> 1 <= #[trigger] orbits@[i] <= 100,\n ensures\n res as int == spec_sum_min_cost(orbits@, c as int, 100),\n {\n let n = orbits.len();\n let ghost ni = n as int;\n proof {\n assert(ni == orbits@.len());\n assert(1 <= ni <= 100);\n }\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101\n invariant\n t <= 101,\n cnt.len() == t,\n forall|j: int|\n 0 <= j < t as int ==> #[trigger] cnt@[j] == 0,\n decreases 101 - t,\n {\n cnt.push(0);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n\n invariant\n n == orbits.len(),\n ni == orbits@.len(),\n 1 <= ni <= 100,\n i <= n,\n cnt.len() == 101,\n forall|k: int|\n 0 <= k < ni ==> 1 <= #[trigger] orbits@[k] <= 100,\n forall|j: int|\n 0 <= j < 101 ==> #[trigger] cnt@[j] == spec_prefix_count(orbits@, i as int, j),\n decreases n - i,\n {\n proof {\n assert(i < orbits.len());\n assert((i as int) < ni);\n assert(0 <= (i as int) && (i as int) < orbits.len());\n assert(1 <= orbits@[i as int] <= 100);\n }\n let x = orbits[i] as usize;\n proof {\n assert(x >= 1);\n assert(x <= 100);\n assert(x < cnt.len());\n }\n let prev = cnt[x];\n let ghost i0 = i as int;\n proof {\n assert(i0 < orbits@.len());\n assert(1 <= orbits@[i0] <= 100);\n assert(orbits@[i0] as int == x as int);\n assert(prev == spec_prefix_count(orbits@, i0, x as int));\n assert(i0 < ni);\n lemma_count_le_i(orbits@, i0, x as int);\n assert(prev <= i0);\n assert(prev < ni);\n assert(prev + 1 <= 101);\n assert(-2147483648 <= prev + 1 < 2147483648);\n }\n let ghost cnt0 = cnt@;\n cnt.set(x, prev + 1);\n proof {\n assert forall|j: int|\n 0 <= j < 101 implies cnt@[j] == spec_prefix_count(orbits@, i0 + 1, j)\n by {\n if 0 <= j && j < 101 {\n if j == x as int {\n assert(orbits@[i0] as int == j);\n lemma_prefix_hit(orbits@, i0, j);\n assert(cnt@[j] == prev + 1);\n assert(spec_prefix_count(orbits@, i0 + 1, j) == prev + 1);\n } else {\n assert(j != x as int);\n assert(orbits@[i0] as int == x as int);\n assert(orbits@[i0] as int != j);\n lemma_prefix_other(orbits@, i0, j);\n assert(cnt@[j] == cnt0[j]);\n assert(cnt0[j] == spec_prefix_count(orbits@, i0, j));\n assert(cnt@[j] == spec_prefix_count(orbits@, i0 + 1, j));\n }\n }\n };\n }\n i = i + 1;\n }\n let mut ans = 0i32;\n let mut v = 1usize;\n while v <= 100\n invariant\n n == orbits.len(),\n ni == orbits@.len(),\n 1 <= ni <= 100,\n 1 <= c <= 100,\n 1 <= v <= 101,\n ans as int == spec_sum_min_cost(orbits@, c as int, (v - 1) as int),\n ans as int <= (v - 1) as int * 100,\n cnt.len() == 101,\n forall|j: int|\n 0 <= j < 101 ==> #[trigger] cnt@[j] == spec_prefix_count(orbits@, ni, j),\n decreases 101 - v,\n {\n proof {\n assert(v < cnt.len());\n }\n let k = cnt[v];\n let add = if k < c {\n k\n } else {\n c\n };\n proof {\n assert(k == spec_prefix_count(orbits@, ni, v as int));\n lemma_count_le_i(orbits@, ni, v as int);\n lemma_spec_prefix_count_nonneg(orbits@, ni, v as int);\n assert(0 <= k && k <= ni);\n lemma_sum_step(orbits@, c as int, v as int, ni);\n if k < c {\n assert(add == k);\n lemma_i32_lt_implies_int_lt(k, c);\n assert((k as int) < c as int);\n assert(spec_orbit_contrib(k as int, c as int) == k as int);\n } else {\n assert(!(k < c));\n assert(add == c);\n assert(c >= 1);\n assert(k >= c);\n assert(k >= 1);\n lemma_i32_ge_implies_int_ge(k, c);\n assert((k as int) >= c as int);\n assert(spec_orbit_contrib(k as int, c as int) == c as int) by {\n let kk = k as int;\n let cc = c as int;\n assert(kk >= 1);\n assert(kk >= cc);\n assert(!(kk < cc));\n };\n }\n assert(add as int == spec_orbit_contrib(k as int, c as int));\n assert(ans as int + add as int == spec_sum_min_cost(orbits@, c as int, v as int));\n assert((v as int) <= 100);\n assert(add <= 100);\n assert(ans as int <= ((v - 1) as int) * 100);\n assert(ans as int + add as int <= (v as int) * 100);\n assert((v as int) * 100 <= 100 * 100);\n assert(ans as int + add as int <= 100 * 100);\n assert((ans as int) + (add as int) <= 100 * 100);\n }\n ans = ans + add;\n v = v + 1;\n }\n proof {\n assert(v == 101);\n assert(ans as int == spec_sum_min_cost(orbits@, c as int, 100));\n assert forall|vv: int| 1 <= vv <= 100 implies #[trigger] spec_orbit_contrib(\n spec_prefix_count(orbits@, ni, vv),\n c as int,\n ) <= c as int by {\n lemma_orbit_contrib_le_c(spec_prefix_count(orbits@, ni, vv), c as int);\n };\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_destroy_cost(orbits: Vec, c: i32) -> i32 {\n let n = orbits.len();\n let mut cnt: Vec = Vec::new();\n let mut t = 0usize;\n while t < 101 {\n cnt.push(0);\n t = t + 1;\n }\n let mut i = 0usize;\n while i < n {\n let x = orbits[i] as usize;\n let prev = cnt[x];\n cnt[x] = prev + 1;\n i = i + 1;\n }\n let mut ans = 0i32;\n let mut v = 1usize;\n while v <= 100 {\n let k = cnt[v];\n let add = if k < c {\n k\n } else {\n c\n };\n ans = ans + add;\n v = v + 1;\n }\n ans\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let mut tc = 0usize;\n while tc < t {\n let nc: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let c = nc[1];\n let orbits: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::min_destroy_cost(orbits, c);\n println!(\"{}\", ans);\n tc = tc + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1730A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1730A/tests/mutated_testcases.jsonl"} {"problem": "cf1742A", "family": "codeforces", "description": "# Sum\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou are given three integers $$$a$$$, $$$b$$$, and $$$c$$$. Determine if one of them is the sum of the other two.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 9261$$$)\u00a0\u2014 the number of test cases.\n\nThe description of each test case consists of three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$0 \\leq a, b, c \\leq 20$$$).\n\n## Output\n\nFor each test case, output \"`YES`\" if one of the numbers is the sum of the other two, and \"`NO`\" otherwise.\n\nYou can output the answer in any case (for example, the strings \"`yEs`\", \"`yes`\", \"`Yes`\" and \"`YES`\" will be recognized as a positive answer).\n\n## Example\n\n**Input:**\n```\n7\n1 4 3\n2 5 8\n9 11 20\n0 0 0\n20 20 20\n4 12 3\n15 7 8\n```\n**Output:**\n```\nYES\nNO\nYES\nYES\nNO\nNO\nYES\n```\n\n## Note\n\nIn the first test case, $$$1 + 3 = 4$$$.\n\nIn the second test case, none of the numbers is the sum of the other two.\n\nIn the third test case, $$$9 + 11 = 20$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn one_is_sum_of_others(a: i64, b: i64, c: i64) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let a: i64 = it.next().unwrap().parse().unwrap();\n let b: i64 = it.next().unwrap().parse().unwrap();\n let c: i64 = it.next().unwrap().parse().unwrap();\n if Solution::one_is_sum_of_others(a, b, c) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n k = k + 1;\n }\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 102641 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_one_is_sum_of_others(a: int, b: int, c: int) -> bool {\n a == b + c || b == a + c || c == a + b\n}\n\nimpl Solution {\n pub fn one_is_sum_of_others(a: i64, b: i64, c: i64) -> (res: bool)\n requires\n 0 <= a <= 20,\n 0 <= b <= 20,\n 0 <= c <= 20,\n ensures\n res == spec_one_is_sum_of_others(a as int, b as int, c as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn one_is_sum_of_others(a: i64, b: i64, c: i64) -> bool {\n a == b + c || b == a + c || c == a + b\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_one_is_sum_of_others(a: int, b: int, c: int) -> bool {\n a == b + c || b == a + c || c == a + b\n}\n\nimpl Solution {\n pub fn one_is_sum_of_others(a: i64, b: i64, c: i64) -> (res: bool)\n requires\n 0 <= a <= 20,\n 0 <= b <= 20,\n 0 <= c <= 20,\n ensures\n res == spec_one_is_sum_of_others(a as int, b as int, c as int),\n {\n a == b + c || b == a + c || c == a + b\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_one_is_sum_of_others(a: int, b: int, c: int) -> bool {\n a == b + c || b == a + c || c == a + b\n}\n\nimpl Solution {\n pub fn one_is_sum_of_others(a: i64, b: i64, c: i64) -> (res: bool)\n requires\n 0 <= a <= 20,\n 0 <= b <= 20,\n 0 <= c <= 20,\n ensures\n res == spec_one_is_sum_of_others(a as int, b as int, c as int),\n {\n proof {\n assert((a + b) as int == (a as int) + (b as int));\n assert((a + c) as int == (a as int) + (c as int));\n assert((b + c) as int == (b as int) + (c as int));\n assert((a == b + c || b == a + c || c == a + b) == spec_one_is_sum_of_others(a as int, b as int, c as int));\n }\n a == b + c || b == a + c || c == a + b\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn one_is_sum_of_others(a: i64, b: i64, c: i64) -> bool {\n a == b + c || b == a + c || c == a + b\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let a: i64 = it.next().unwrap().parse().unwrap();\n let b: i64 = it.next().unwrap().parse().unwrap();\n let c: i64 = it.next().unwrap().parse().unwrap();\n if Solution::one_is_sum_of_others(a, b, c) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n k = k + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1742A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1742A/tests/mutated_testcases.jsonl"} {"problem": "cf1742D", "family": "codeforces", "description": "# Coprime\n\nTime limit: 3 seconds | Memory limit: 256 megabytes\n\nGiven an array of $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$). Find the maximum value of $$$i + j$$$ such that $$$a_i$$$ and $$$a_j$$$ are coprime,$$$^{\\dagger}$$$ or $$$-1$$$ if no such $$$i$$$, $$$j$$$ exist.\n\nFor example consider the array $$$[1, 3, 5, 2, 4, 7, 7]$$$. The maximum value of $$$i + j$$$ that can be obtained is $$$5 + 7$$$, since $$$a_5 = 4$$$ and $$$a_7 = 7$$$ are coprime.\n\n$$$^{\\dagger}$$$ Two integers $$$p$$$ and $$$q$$$ are coprime if the only positive integer that is a divisor of both of them is $$$1$$$ (that is, their greatest common divisor is $$$1$$$).\n\n## Input\n\nThe input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10$$$)\u00a0\u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains an integer $$$n$$$ ($$$2 \\leq n \\leq 2\\cdot10^5$$$)\u00a0\u2014 the length of the array.\n\nThe following line contains $$$n$$$ space-separated positive integers $$$a_1$$$, $$$a_2$$$,..., $$$a_n$$$ ($$$1 \\leq a_i \\leq 1000$$$)\u00a0\u2014 the elements of the array.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.\n\n## Output\n\nFor each test case, output a single integer \u00a0\u2014 the maximum value of $$$i + j$$$ such that $$$i$$$ and $$$j$$$ satisfy the condition that $$$a_i$$$ and $$$a_j$$$ are coprime, or output $$$-1$$$ in case no $$$i$$$, $$$j$$$ satisfy the condition.\n\n## Example\n\n**Input:**\n```\n6\n3\n3 2 1\n7\n1 3 5 2 4 7 7\n5\n1 2 3 4 5\n3\n2 2 4\n6\n5 4 3 15 12 16\n5\n1 2 2 3 6\n```\n**Output:**\n```\n6\n12\n9\n-1\n10\n7\n```\n\n## Note\n\nFor the first test case, we can choose $$$i = j = 3$$$, with sum of indices equal to $$$6$$$, since $$$1$$$ and $$$1$$$ are coprime.\n\nFor the second test case, we can choose $$$i = 7$$$ and $$$j = 5$$$, with sum of indices equal to $$$7 + 5 = 12$$$, since $$$7$$$ and $$$4$$$ are coprime.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nfn next_usize<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nfn next_i32<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nimpl Solution {\n pub fn max_coprime_index_sum(a: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let v: i32 = match next_i32(&mut it) {\n Some(x) => x,\n None => return,\n };\n a.push(v);\n i = i + 1;\n }\n\n let ans = Solution::max_coprime_index_sum(a);\n println!(\"{}\", ans);\n\n case_id = case_id + 1;\n }\n}\n```\n", "tags": "Rating: 1100\nbrute force, greedy, number theory\nSolved by: 33611 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd(a: nat, b: nat) -> nat\n decreases b\n {\n if b == 0 { a } else { Self::spec_gcd(b, a % b) }\n }\n\n pub open spec fn spec_divides_both(d: int, x: int, y: int) -> bool {\n x % d == 0 && y % d == 0\n }\n\n pub open spec fn spec_coprime_values(x: int, y: int) -> bool {\n x >= 1 && y >= 1 && Self::spec_gcd(x as nat, y as nat) == 1\n }\n\n pub open spec fn spec_last_pos_prefix(a: Seq, v: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n -1\n } else {\n let prev = Self::spec_last_pos_prefix(a, v, i - 1);\n if a[i - 1] as int == v {\n i\n } else {\n prev\n }\n }\n }\n\n pub open spec fn spec_last_pos(a: Seq, v: int) -> int {\n Self::spec_last_pos_prefix(a, v, a.len() as int)\n }\n\n pub open spec fn spec_pair_score(a: Seq, x: int, y: int) -> int {\n let px = Self::spec_last_pos(a, x);\n let py = Self::spec_last_pos(a, y);\n if px >= 1 && py >= 1 && Self::spec_coprime_values(x, y) {\n px + py\n } else {\n -1\n }\n }\n\n pub open spec fn spec_optimal_answer(a: Seq, res: int) -> bool {\n &&& forall|x: int, y: int|\n 1 <= x <= 1000 && 1 <= y <= 1000 ==> Self::spec_pair_score(a, x, y) <= res\n &&& (res == -1 || exists|x: int, y: int|\n 1 <= x <= 1000 && 1 <= y <= 1000 && Self::spec_pair_score(a, x, y) == res)\n }\n\n pub fn max_coprime_index_sum(a: Vec) -> (res: i32)\n requires\n 2 <= a.len() <= 200000,\n forall|j: int| 0 <= j < a.len() as int ==> 1 <= #[trigger] a[j] <= 1000,\n ensures\n Self::spec_optimal_answer(a@, res as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_coprime_values(x: i32, y: i32) -> bool {\n let mut a = x;\n let mut b = y;\n while b != 0 {\n let r = a % b;\n a = b;\n b = r;\n }\n a == 1\n }\n\n pub fn max_coprime_index_sum(a: Vec) -> i32 {\n let n = a.len();\n let mut last: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001 {\n last.push(-1);\n k = k + 1;\n }\n\n let mut i: usize = 0;\n while i < n {\n let v = a[i] as usize;\n last[v] = i as i32 + 1;\n i = i + 1;\n }\n\n let mut ans: i32 = -1;\n let mut x: i32 = 1;\n while x <= 1000 {\n let mut y: i32 = 1;\n while y <= 1000 {\n let cop = Self::is_coprime_values(x, y);\n let lx = last[x as usize];\n let ly = last[y as usize];\n if lx != -1 && ly != -1 && cop {\n let cand = lx + ly;\n if cand > ans {\n ans = cand;\n }\n }\n y = y + 1;\n }\n x = x + 1;\n }\n\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd(a: nat, b: nat) -> nat\n decreases b\n {\n if b == 0 { a } else { Self::spec_gcd(b, a % b) }\n }\n\n pub open spec fn spec_divides_both(d: int, x: int, y: int) -> bool {\n x % d == 0 && y % d == 0\n }\n\n pub open spec fn spec_coprime_values(x: int, y: int) -> bool {\n x >= 1 && y >= 1 && Self::spec_gcd(x as nat, y as nat) == 1\n }\n\n pub open spec fn spec_last_pos_prefix(a: Seq, v: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n -1\n } else {\n let prev = Self::spec_last_pos_prefix(a, v, i - 1);\n if a[i - 1] as int == v {\n i\n } else {\n prev\n }\n }\n }\n\n pub open spec fn spec_last_pos(a: Seq, v: int) -> int {\n Self::spec_last_pos_prefix(a, v, a.len() as int)\n }\n\n pub open spec fn spec_pair_score(a: Seq, x: int, y: int) -> int {\n let px = Self::spec_last_pos(a, x);\n let py = Self::spec_last_pos(a, y);\n if px >= 1 && py >= 1 && Self::spec_coprime_values(x, y) {\n px + py\n } else {\n -1\n }\n }\n\n pub open spec fn spec_optimal_answer(a: Seq, res: int) -> bool {\n &&& forall|x: int, y: int|\n 1 <= x <= 1000 && 1 <= y <= 1000 ==> Self::spec_pair_score(a, x, y) <= res\n &&& (res == -1 || exists|x: int, y: int|\n 1 <= x <= 1000 && 1 <= y <= 1000 && Self::spec_pair_score(a, x, y) == res)\n }\n\n pub fn is_coprime_values(x: i32, y: i32) -> (res: bool)\n requires\n 1 <= x <= 1000,\n 1 <= y <= 1000,\n ensures\n res == Self::spec_coprime_values(x as int, y as int),\n {\n let mut a = x;\n let mut b = y;\n while b != 0 {\n let r = a % b;\n a = b;\n b = r;\n }\n a == 1\n }\n\n pub fn max_coprime_index_sum(a: Vec) -> (res: i32)\n requires\n 2 <= a.len() <= 200000,\n forall|j: int| 0 <= j < a.len() as int ==> 1 <= #[trigger] a[j] <= 1000,\n ensures\n Self::spec_optimal_answer(a@, res as int),\n {\n let n = a.len();\n let mut last: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001 {\n last.push(-1);\n k = k + 1;\n }\n\n let mut i: usize = 0;\n while i < n {\n let v = a[i] as usize;\n last.set(v, i as i32 + 1);\n i = i + 1;\n }\n\n let mut ans: i32 = -1;\n let mut x: i32 = 1;\n while x <= 1000 {\n let mut y: i32 = 1;\n while y <= 1000 {\n let cop = Self::is_coprime_values(x, y);\n let lx = last[x as usize];\n let ly = last[y as usize];\n if lx != -1 && ly != -1 && cop {\n let cand = lx + ly;\n if cand > ans {\n ans = cand;\n }\n }\n y = y + 1;\n }\n x = x + 1;\n }\n\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_gcd(a: nat, b: nat) -> nat\n decreases b\n {\n if b == 0 { a } else { Self::spec_gcd(b, a % b) }\n }\n\n pub open spec fn spec_divides_both(d: int, x: int, y: int) -> bool {\n x % d == 0 && y % d == 0\n }\n\n pub open spec fn spec_coprime_values(x: int, y: int) -> bool {\n x >= 1 && y >= 1 && Self::spec_gcd(x as nat, y as nat) == 1\n }\n\n pub open spec fn spec_last_pos_prefix(a: Seq, v: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n -1\n } else {\n let prev = Self::spec_last_pos_prefix(a, v, i - 1);\n if a[i - 1] as int == v {\n i\n } else {\n prev\n }\n }\n }\n\n pub open spec fn spec_last_pos(a: Seq, v: int) -> int {\n Self::spec_last_pos_prefix(a, v, a.len() as int)\n }\n\n pub open spec fn spec_pair_score(a: Seq, x: int, y: int) -> int {\n let px = Self::spec_last_pos(a, x);\n let py = Self::spec_last_pos(a, y);\n if px >= 1 && py >= 1 && Self::spec_coprime_values(x, y) {\n px + py\n } else {\n -1\n }\n }\n\n pub open spec fn spec_optimal_answer(a: Seq, res: int) -> bool {\n &&& forall|x: int, y: int|\n 1 <= x <= 1000 && 1 <= y <= 1000 ==> Self::spec_pair_score(a, x, y) <= res\n &&& (res == -1 || exists|x: int, y: int|\n 1 <= x <= 1000 && 1 <= y <= 1000 && Self::spec_pair_score(a, x, y) == res)\n }\n\n pub fn is_coprime_values(x: i32, y: i32) -> (res: bool)\n requires\n 1 <= x <= 1000,\n 1 <= y <= 1000,\n ensures\n res == Self::spec_coprime_values(x as int, y as int),\n {\n let ghost sx = x as nat;\n let ghost sy = y as nat;\n let mut a = x;\n let mut b = y;\n while b != 0\n invariant\n 1 <= a <= 1000,\n 0 <= b <= 1000,\n Self::spec_gcd(a as nat, b as nat) == Self::spec_gcd(sx, sy),\n decreases b as int,\n {\n let ghost prev_a = a;\n let ghost prev_b = b;\n proof {\n assert(Self::spec_gcd(prev_a as nat, prev_b as nat)\n == Self::spec_gcd(prev_b as nat, (prev_a % prev_b) as nat));\n }\n let r = a % b;\n a = b;\n b = r;\n }\n proof {\n assert(b == 0);\n assert(Self::spec_gcd(a as nat, 0) == a as nat);\n assert(a as nat == Self::spec_gcd(sx, sy));\n assert((a == 1) == (Self::spec_gcd(sx, sy) == 1));\n assert(Self::spec_coprime_values(x as int, y as int) == (Self::spec_gcd(sx, sy) == 1));\n }\n a == 1\n }\n\n pub fn max_coprime_index_sum(a: Vec) -> (res: i32)\n requires\n 2 <= a.len() <= 200000,\n forall|j: int| 0 <= j < a.len() as int ==> 1 <= #[trigger] a[j] <= 1000,\n ensures\n Self::spec_optimal_answer(a@, res as int),\n {\n let n = a.len();\n let mut last: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001\n invariant\n 0 <= k <= 1001,\n last.len() == k,\n forall|j: int| 0 <= j < k as int ==> last[j] == -1,\n decreases 1001 - k,\n {\n last.push(-1);\n k = k + 1;\n }\n\n let mut i: usize = 0;\n while i < n\n invariant\n a.len() == n,\n 2 <= n <= 200000,\n 0 <= i <= n,\n last.len() == 1001,\n forall|j: int| 0 <= j < a.len() as int ==> 1 <= #[trigger] a[j] <= 1000,\n forall|v: int| 1 <= v <= 1000 ==> last[v] as int == Self::spec_last_pos_prefix(a@, v, i as int),\n forall|v: int| 1 <= v <= 1000 ==> last[v] == -1 || (1 <= last[v] <= i as i32),\n last[0] == -1,\n decreases n - i,\n {\n let v = a[i] as usize;\n let ghost last_prev = last@;\n assert(i < n);\n assert(i <= 199999);\n last.set(v, i as i32 + 1);\n proof {\n assert(v as int == a[i as int] as int);\n assert(1 <= v as int <= 1000);\n assert forall|vv: int| 1 <= vv <= 1000 implies last[vv] as int == Self::spec_last_pos_prefix(a@, vv, i as int + 1) by {\n if vv == v as int {\n assert(a[i as int] as int == vv);\n assert(Self::spec_last_pos_prefix(a@, vv, i as int + 1) == i as int + 1);\n assert(last[vv] as int == i as int + 1);\n } else {\n assert(last[vv] == last_prev[vv]);\n assert(last_prev[vv] as int == Self::spec_last_pos_prefix(a@, vv, i as int));\n assert(a[i as int] as int != vv);\n assert(Self::spec_last_pos_prefix(a@, vv, i as int + 1) == Self::spec_last_pos_prefix(a@, vv, i as int));\n }\n };\n assert forall|vv: int| 1 <= vv <= 1000 implies last[vv] == -1 || (1 <= last[vv] <= i as i32 + 1) by {\n if vv == v as int {\n assert(last[vv] == i as i32 + 1);\n } else {\n assert(last[vv] == last_prev[vv]);\n }\n };\n assert(last[0] == -1);\n }\n i = i + 1;\n }\n\n let mut ans: i32 = -1;\n let mut x: i32 = 1;\n while x <= 1000\n invariant\n a.len() == n,\n 2 <= n <= 200000,\n last.len() == 1001,\n forall|j: int| 0 <= j < a.len() as int ==> 1 <= #[trigger] a[j] <= 1000,\n forall|v: int| 1 <= v <= 1000 ==> last[v] as int == Self::spec_last_pos(a@, v),\n forall|v: int| 1 <= v <= 1000 ==> last[v] == -1 || (1 <= last[v] <= n as i32),\n 1 <= x <= 1001,\n ans >= -1,\n forall|vx: int, vy: int|\n 1 <= vx < x as int && 1 <= vy <= 1000 ==> Self::spec_pair_score(a@, vx, vy) <= ans as int,\n ans == -1 || exists|wx: int, wy: int|\n 1 <= wx < x as int && 1 <= wy <= 1000 && Self::spec_pair_score(a@, wx, wy) == ans as int,\n decreases 1001 - x,\n {\n let mut y: i32 = 1;\n while y <= 1000\n invariant\n a.len() == n,\n 2 <= n <= 200000,\n last.len() == 1001,\n forall|j: int| 0 <= j < a.len() as int ==> 1 <= #[trigger] a[j] <= 1000,\n forall|v: int| 1 <= v <= 1000 ==> last[v] as int == Self::spec_last_pos(a@, v),\n forall|v: int| 1 <= v <= 1000 ==> last[v] == -1 || (1 <= last[v] <= n as i32),\n 1 <= x <= 1000,\n 1 <= y <= 1001,\n ans >= -1,\n forall|vx: int, vy: int|\n 1 <= vx < x as int && 1 <= vy <= 1000 ==> Self::spec_pair_score(a@, vx, vy) <= ans as int,\n forall|vy: int| 1 <= vy < y as int ==> Self::spec_pair_score(a@, x as int, vy) <= ans as int,\n ans == -1 || exists|wx: int, wy: int|\n ((1 <= wx < x as int && 1 <= wy <= 1000)\n || (wx == x as int && 1 <= wy < y as int))\n && Self::spec_pair_score(a@, wx, wy) == ans as int,\n decreases 1001 - y,\n {\n let ghost old_ans = ans;\n let cop = Self::is_coprime_values(x, y);\n let lx = last[x as usize];\n let ly = last[y as usize];\n if lx != -1 && ly != -1 && cop {\n proof {\n assert(1 <= x as int <= 1000);\n assert(1 <= y as int <= 1000);\n assert(lx == last[x as int]);\n assert(ly == last[y as int]);\n assert(1 <= lx <= n as i32);\n assert(1 <= ly <= n as i32);\n }\n let cand = lx + ly;\n if cand > ans {\n ans = cand;\n }\n proof {\n assert(cop == Self::spec_coprime_values(x as int, y as int));\n assert(lx as int == Self::spec_last_pos(a@, x as int));\n assert(ly as int == Self::spec_last_pos(a@, y as int));\n assert(lx >= 1);\n assert(ly >= 1);\n assert(cand as int == Self::spec_pair_score(a@, x as int, y as int));\n assert(old_ans <= ans);\n assert(cand <= ans);\n assert(Self::spec_pair_score(a@, x as int, y as int) <= ans as int);\n }\n } else {\n proof {\n assert(cop == Self::spec_coprime_values(x as int, y as int));\n if lx == -1 {\n assert(Self::spec_pair_score(a@, x as int, y as int) == -1);\n } else if ly == -1 {\n assert(Self::spec_pair_score(a@, x as int, y as int) == -1);\n } else {\n assert(!cop);\n assert(!Self::spec_coprime_values(x as int, y as int));\n assert(Self::spec_pair_score(a@, x as int, y as int) == -1);\n }\n assert(ans == old_ans);\n }\n }\n proof {\n assert(old_ans <= ans);\n assert forall|vx: int, vy: int|\n 1 <= vx < x as int && 1 <= vy <= 1000 implies Self::spec_pair_score(a@, vx, vy) <= ans as int by {\n assert(Self::spec_pair_score(a@, vx, vy) <= old_ans as int);\n };\n assert forall|vy: int| 1 <= vy < y as int implies Self::spec_pair_score(a@, x as int, vy) <= ans as int by {\n assert(Self::spec_pair_score(a@, x as int, vy) <= old_ans as int);\n };\n assert(Self::spec_pair_score(a@, x as int, y as int) <= ans as int);\n assert forall|vy: int| 1 <= vy < y as int + 1 implies Self::spec_pair_score(a@, x as int, vy) <= ans as int by {\n if vy < y as int {\n assert(Self::spec_pair_score(a@, x as int, vy) <= ans as int);\n } else {\n assert(vy == y as int);\n assert(Self::spec_pair_score(a@, x as int, vy) <= ans as int);\n }\n };\n\n if ans == -1 {\n } else if ans == old_ans {\n assert(old_ans != -1);\n assert(exists|wx: int, wy: int|\n ((1 <= wx < x as int && 1 <= wy <= 1000)\n || (wx == x as int && 1 <= wy < y as int))\n && Self::spec_pair_score(a@, wx, wy) == old_ans as int);\n let ex = choose|wx: int, wy: int|\n ((1 <= wx < x as int && 1 <= wy <= 1000)\n || (wx == x as int && 1 <= wy < y as int))\n && Self::spec_pair_score(a@, wx, wy) == old_ans as int;\n assert(((1 <= ex.0 < x as int && 1 <= ex.1 <= 1000)\n || (ex.0 == x as int && 1 <= ex.1 < y as int + 1))\n && Self::spec_pair_score(a@, ex.0, ex.1) == ans as int);\n } else {\n assert(ans != old_ans);\n assert(lx != -1 && ly != -1 && cop);\n assert(cop == Self::spec_coprime_values(x as int, y as int));\n assert(lx as int == Self::spec_last_pos(a@, x as int));\n assert(ly as int == Self::spec_last_pos(a@, y as int));\n assert(Self::spec_pair_score(a@, x as int, y as int) == ans as int);\n assert(1 <= y as int && (y as int) < (y as int) + 1);\n assert((x as int == x as int && 1 <= y as int && (y as int) < (y as int) + 1)\n && Self::spec_pair_score(a@, x as int, y as int) == ans as int);\n }\n }\n y = y + 1;\n }\n proof {\n assert(y == 1001);\n assert forall|vx: int, vy: int|\n 1 <= vx < x as int + 1 && 1 <= vy <= 1000 implies Self::spec_pair_score(a@, vx, vy) <= ans as int by {\n if vx < x as int {\n assert(Self::spec_pair_score(a@, vx, vy) <= ans as int);\n } else {\n assert(vx == x as int);\n assert(1 <= vy < y as int);\n assert(Self::spec_pair_score(a@, x as int, vy) <= ans as int);\n }\n };\n\n if ans == -1 {\n } else {\n assert(exists|wx: int, wy: int|\n ((1 <= wx < x as int && 1 <= wy <= 1000)\n || (wx == x as int && 1 <= wy < y as int))\n && Self::spec_pair_score(a@, wx, wy) == ans as int);\n let ex = choose|wx: int, wy: int|\n ((1 <= wx < x as int && 1 <= wy <= 1000)\n || (wx == x as int && 1 <= wy < y as int))\n && Self::spec_pair_score(a@, wx, wy) == ans as int;\n assert(1 <= ex.0 < x as int + 1);\n assert(1 <= ex.1 <= 1000);\n }\n }\n x = x + 1;\n }\n\n proof {\n assert(x == 1001);\n assert forall|vx: int, vy: int|\n 1 <= vx <= 1000 && 1 <= vy <= 1000\n implies Self::spec_pair_score(a@, vx, vy) <= ans as int by {\n assert(1 <= vx < x as int);\n assert(Self::spec_pair_score(a@, vx, vy) <= ans as int);\n };\n if ans != -1 {\n let ex = choose|wx: int, wy: int|\n 1 <= wx < x as int && 1 <= wy <= 1000 && Self::spec_pair_score(a@, wx, wy) == ans as int;\n let wx = ex.0;\n let wy = ex.1;\n assert(1 <= wx <= 1000);\n assert(1 <= wy <= 1000);\n assert(Self::spec_pair_score(a@, wx, wy) == ans as int);\n }\n assert(Self::spec_optimal_answer(a@, ans as int));\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nfn next_usize<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nfn next_i32<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nimpl Solution {\n pub fn is_coprime_values(x: i32, y: i32) -> bool {\n let mut a = x;\n let mut b = y;\n while b != 0 {\n let r = a % b;\n a = b;\n b = r;\n }\n a == 1\n }\n\n pub fn max_coprime_index_sum(a: Vec) -> i32 {\n let n = a.len();\n let mut last: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001 {\n last.push(-1);\n k = k + 1;\n }\n\n let mut i: usize = 0;\n while i < n {\n let v = a[i] as usize;\n last[v] = i as i32 + 1;\n i = i + 1;\n }\n\n let mut ans: i32 = -1;\n let mut x: i32 = 1;\n while x <= 1000 {\n let mut y: i32 = 1;\n while y <= 1000 {\n let cop = Self::is_coprime_values(x, y);\n let lx = last[x as usize];\n let ly = last[y as usize];\n if lx != -1 && ly != -1 && cop {\n let cand = lx + ly;\n if cand > ans {\n ans = cand;\n }\n }\n y = y + 1;\n }\n x = x + 1;\n }\n\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = match next_usize(&mut it) {\n Some(v) => v,\n None => return,\n };\n\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let v: i32 = match next_i32(&mut it) {\n Some(x) => x,\n None => return,\n };\n a.push(v);\n i = i + 1;\n }\n\n let ans = Solution::max_coprime_index_sum(a);\n println!(\"{}\", ans);\n\n case_id = case_id + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1742D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1742D/tests/mutated_testcases.jsonl"} {"problem": "cf1760C", "family": "codeforces", "description": "# Advantage\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nThere are $$$n$$$ participants in a competition, participant $$$i$$$ having a strength of $$$s_i$$$. \n\nEvery participant wonders how much of an advantage they have over the other best participant. In other words, each participant $$$i$$$ wants to know the difference between $$$s_i$$$ and $$$s_j$$$, where $$$j$$$ is the strongest participant in the competition, not counting $$$i$$$ (a difference can be negative).\n\nSo, they ask you for your help! For each $$$i$$$ ($$$1 \\leq i \\leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any participant other than participant $$$i$$$.\n\n## Input\n\nThe input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)\u00a0\u2014 the number of test cases. The descriptions of the test cases follow.\n\nThe first line of each test case contains an integer $$$n$$$ ($$$2 \\leq n \\leq 2\\cdot10^5$$$)\u00a0\u2014 the length of the array.\n\nThe following line contains $$$n$$$ space-separated positive integers $$$s_1$$$, $$$s_2$$$, ..., $$$s_n$$$ ($$$1 \\leq s_i \\leq 10^9$$$)\u00a0\u2014 the strengths of the participants.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.\n\n## Output\n\nFor each test case, output $$$n$$$ space-separated integers. For each $$$i$$$ ($$$1 \\leq i \\leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any other participant.\n\n## Example\n\n**Input:**\n```\n5\n4\n4 7 3 5\n2\n1 2\n5\n1 2 3 4 5\n3\n4 9 4\n4\n4 4 4 4\n```\n**Output:**\n```\n-3 2 -4 -2 \n-1 1 \n-4 -3 -2 -1 1 \n-5 5 -5 \n0 0 0 0\n```\n\n## Note\n\nFor the first test case:\n- The first participant has a strength of $$$4$$$ and the largest strength of a participant different from the first one is $$$7$$$, so the answer for the first participant is $$$4 - 7 = -3$$$. \n- The second participant has a strength of $$$7$$$ and the largest strength of a participant different from the second one is $$$5$$$, so the answer for the second participant is $$$7 - 5 = 2$$$.\n- The third participant has a strength of $$$3$$$ and the largest strength of a participant different from the third one is $$$7$$$, so the answer for the third participant is $$$3 - 7 = -4$$$.\n- The fourth participant has a strength of $$$5$$$ and the largest strength of a participant different from the fourth one is $$$7$$$, so the answer for the fourth participant is $$$5 - 7 = -2$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn advantages(s: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = it.next().expect(\"n\").parse().expect(\"valid n\");\n let mut s: Vec = Vec::with_capacity(n);\n\n let mut i: usize = 0;\n while i < n {\n s.push(it.next().expect(\"s_i\").parse().expect(\"valid s_i\"));\n i = i + 1;\n }\n\n let ans = Solution::advantages(s);\n let mut j: usize = 0;\n while j < ans.len() {\n if j > 0 {\n print!(\" \");\n }\n print!(\"{}\", ans[j]);\n j = j + 1;\n }\n println!();\n\n case_id = case_id + 1;\n }\n}\n```\n", "tags": "Rating: 800\ndata structures, implementation, sortings\nSolved by: 43524 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn valid_advantage(s: Seq, i: int, d: int) -> bool {\n exists|j: int| {\n &&& 0 <= j < s.len()\n &&& j != i\n &&& d == s[i] as int - s[j] as int\n &&& forall|k: int| 0 <= k < s.len() && k != i ==> s[k] as int <= #[trigger] s[j] as int\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn advantages(s: Vec) -> (result: Vec)\n requires\n 2 <= s.len() <= 200_000,\n forall|i: int| 0 <= i < s.len() ==> 1 <= #[trigger] s[i] <= 1_000_000_000,\n ensures\n result.len() == s.len(),\n forall|i: int| 0 <= i < s.len() ==> valid_advantage(s@, i, result[i] as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn advantages(s: Vec) -> Vec {\n let n = s.len();\n let mut max1: i64 = s[0];\n let mut idx1: usize = 0;\n let mut max2: i64 = s[1];\n let mut idx2: usize = 1;\n\n if max2 > max1 {\n let tv = max1;\n let ti = idx1;\n max1 = max2;\n idx1 = idx2;\n max2 = tv;\n idx2 = ti;\n }\n\n let mut t: usize = 2;\n while t < n {\n if s[t] > max1 {\n max2 = max1;\n idx2 = idx1;\n max1 = s[t];\n idx1 = t;\n } else if s[t] > max2 {\n max2 = s[t];\n idx2 = t;\n }\n t = t + 1;\n }\n\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n let best: i64;\n if i == idx1 {\n best = max2;\n } else {\n best = max1;\n }\n\n result.push(s[i] - best);\n i = i + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn valid_advantage(s: Seq, i: int, d: int) -> bool {\n exists|j: int| {\n &&& 0 <= j < s.len()\n &&& j != i\n &&& d == s[i] as int - s[j] as int\n &&& forall|k: int| 0 <= k < s.len() && k != i ==> s[k] as int <= #[trigger] s[j] as int\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn advantages(s: Vec) -> (result: Vec)\n requires\n 2 <= s.len() <= 200_000,\n forall|i: int| 0 <= i < s.len() ==> 1 <= #[trigger] s[i] <= 1_000_000_000,\n ensures\n result.len() == s.len(),\n forall|i: int| 0 <= i < s.len() ==> valid_advantage(s@, i, result[i] as int),\n {\n let n = s.len();\n let mut max1: i64 = s[0];\n let mut idx1: usize = 0;\n let mut max2: i64 = s[1];\n let mut idx2: usize = 1;\n\n if max2 > max1 {\n let tv = max1;\n let ti = idx1;\n max1 = max2;\n idx1 = idx2;\n max2 = tv;\n idx2 = ti;\n }\n\n let mut t: usize = 2;\n while t < n {\n if s[t] > max1 {\n max2 = max1;\n idx2 = idx1;\n max1 = s[t];\n idx1 = t;\n } else if s[t] > max2 {\n max2 = s[t];\n idx2 = t;\n }\n t = t + 1;\n }\n\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n let best: i64;\n if i == idx1 {\n best = max2;\n } else {\n best = max1;\n }\n\n result.push(s[i] - best);\n i = i + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn valid_advantage(s: Seq, i: int, d: int) -> bool {\n exists|j: int| {\n &&& 0 <= j < s.len()\n &&& j != i\n &&& d == s[i] as int - s[j] as int\n &&& forall|k: int| 0 <= k < s.len() && k != i ==> s[k] as int <= #[trigger] s[j] as int\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn advantages(s: Vec) -> (result: Vec)\n requires\n 2 <= s.len() <= 200_000,\n forall|i: int| 0 <= i < s.len() ==> 1 <= #[trigger] s[i] <= 1_000_000_000,\n ensures\n result.len() == s.len(),\n forall|i: int| 0 <= i < s.len() ==> valid_advantage(s@, i, result[i] as int),\n {\n let n = s.len();\n let mut max1: i64 = s[0];\n let mut idx1: usize = 0;\n let mut max2: i64 = s[1];\n let mut idx2: usize = 1;\n\n if max2 > max1 {\n let tv = max1;\n let ti = idx1;\n max1 = max2;\n idx1 = idx2;\n max2 = tv;\n idx2 = ti;\n }\n\n assert(idx1 != idx2);\n assert(s[idx1 as int] == max1);\n assert(s[idx2 as int] == max2);\n assert(max2 <= max1);\n assert forall|k: int| 0 <= k < 2 implies s[k] <= max1 by {\n if k == idx1 as int {\n assert(s[k] == max1);\n } else {\n assert(k == idx2 as int);\n assert(s[k] == max2);\n assert(max2 <= max1);\n }\n };\n assert forall|k: int| 0 <= k < 2 && k != idx1 as int implies s[k] <= max2 by {\n assert(k == idx2 as int);\n assert(s[k] == max2);\n };\n\n let mut t: usize = 2;\n while t < n\n invariant\n n == s.len(),\n 2 <= n <= 200_000,\n 2 <= t <= n,\n 0 <= idx1 < t,\n 0 <= idx2 < t,\n idx1 != idx2,\n s[idx1 as int] == max1,\n s[idx2 as int] == max2,\n max2 <= max1,\n forall|q: int| 0 <= q < n as int ==> 1 <= #[trigger] s[q] <= 1_000_000_000,\n forall|k: int| 0 <= k < t as int ==> s[k] <= max1,\n forall|k: int| 0 <= k < t as int && k != idx1 as int ==> s[k] <= max2,\n decreases n - t,\n {\n if s[t] > max1 {\n max2 = max1;\n idx2 = idx1;\n max1 = s[t];\n idx1 = t;\n proof {\n assert(max2 <= max1);\n assert forall|k: int| 0 <= k < t as int + 1 implies s[k] <= max1 by {\n if k == t as int {\n assert(s[k] == max1);\n } else {\n assert(k < t as int);\n assert(s[k] <= s[idx2 as int]);\n assert(s[idx2 as int] == max2);\n assert(max2 <= max1);\n }\n };\n assert forall|k: int| 0 <= k < t as int + 1 && k != idx1 as int implies s[k] <= max2 by {\n assert(k < t as int);\n assert(s[k] <= s[idx2 as int]);\n assert(s[idx2 as int] == max2);\n };\n }\n } else if s[t] > max2 {\n max2 = s[t];\n idx2 = t;\n proof {\n assert(max2 <= max1);\n assert forall|k: int| 0 <= k < t as int + 1 implies s[k] <= max1 by {\n if k == t as int {\n assert(s[k] == max2);\n assert(max2 <= max1);\n } else {\n assert(k < t as int);\n assert(s[k] <= max1);\n }\n };\n assert forall|k: int| 0 <= k < t as int + 1 && k != idx1 as int implies s[k] <= max2 by {\n if k == t as int {\n assert(s[k] == max2);\n } else {\n assert(k < t as int);\n assert(s[k] <= s[idx2 as int]);\n assert(s[idx2 as int] == max2);\n }\n };\n }\n } else {\n proof {\n assert(s[t as int] <= max2);\n assert forall|k: int| 0 <= k < t as int + 1 implies s[k] <= max1 by {\n if k == t as int {\n assert(s[k] <= max2);\n assert(max2 <= max1);\n } else {\n assert(k < t as int);\n assert(s[k] <= max1);\n }\n };\n assert forall|k: int| 0 <= k < t as int + 1 && k != idx1 as int implies s[k] <= max2 by {\n if k == t as int {\n assert(s[k] <= max2);\n } else {\n assert(k < t as int);\n assert(s[k] <= max2);\n }\n };\n }\n }\n t = t + 1;\n }\n\n assert(t == n);\n assert forall|k: int| 0 <= k < n as int implies s[k] <= max1 by {\n assert(k < t as int);\n };\n assert forall|k: int| 0 <= k < n as int && k != idx1 as int implies s[k] <= max2 by {\n assert(k < t as int);\n };\n\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n\n invariant\n n == s.len(),\n 2 <= n <= 200_000,\n 0 <= i <= n,\n 0 <= idx1 < n,\n 0 <= idx2 < n,\n idx1 != idx2,\n s[idx1 as int] == max1,\n s[idx2 as int] == max2,\n max2 <= max1,\n result.len() == i,\n forall|q: int| 0 <= q < n as int ==> 1 <= #[trigger] s[q] <= 1_000_000_000,\n forall|k: int| 0 <= k < n as int ==> s[k] <= max1,\n forall|k: int| 0 <= k < n as int && k != idx1 as int ==> s[k] <= max2,\n forall|p: int| 0 <= p < i as int ==> valid_advantage(s@, p, result[p] as int),\n decreases n - i,\n {\n let best: i64;\n if i == idx1 {\n best = max2;\n } else {\n best = max1;\n }\n let ghost witness: usize = if i == idx1 { idx2 } else { idx1 };\n\n assert(0 <= witness < n);\n assert(witness != i);\n assert(s[witness as int] == best);\n assert forall|k: int| 0 <= k < n as int && k != i as int implies s[k] <= best by {\n if i == idx1 {\n assert(best == max2);\n assert(k != idx1 as int);\n assert(s[k] <= max2);\n } else {\n assert(best == max1);\n assert(s[k] <= max1);\n }\n }\n\n assert(1 <= s[i as int] <= 1_000_000_000) by {\n assert(0 <= i as int);\n assert((i as int) < (s.len() as int));\n }\n assert(1 <= s[witness as int] <= 1_000_000_000) by {\n assert(0 <= witness as int);\n assert((witness as int) < (s.len() as int));\n }\n assert(best == s[witness as int]);\n assert(1 <= best <= 1_000_000_000);\n assert(-1_000_000_000i64 <= s[i as int] - best <= 1_000_000_000i64);\n result.push(s[i] - best);\n proof {\n let p = i as int;\n let w = witness as int;\n assert(0 <= w < s.len());\n assert(w != p);\n assert(result[p] == s[p] - best);\n assert(result[p] as int == s[p] as int - best as int);\n assert(result[p] as int == s[p] as int - s[w] as int);\n assert(forall|k: int| 0 <= k < s.len() && k != p ==> s[k] as int <= s[w] as int);\n assert(valid_advantage(s@, p, result[p] as int));\n }\n i = i + 1;\n }\n\n proof {\n assert(!(i < n));\n assert(i == n);\n assert(result.len() == s.len());\n assert(forall|p: int| 0 <= p < s.len() ==> valid_advantage(s@, p, result[p] as int));\n }\n\n result\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn advantages(s: Vec) -> Vec {\n let n = s.len();\n let mut max1: i64 = s[0];\n let mut idx1: usize = 0;\n let mut max2: i64 = s[1];\n let mut idx2: usize = 1;\n\n if max2 > max1 {\n let tv = max1;\n let ti = idx1;\n max1 = max2;\n idx1 = idx2;\n max2 = tv;\n idx2 = ti;\n }\n\n let mut t: usize = 2;\n while t < n {\n if s[t] > max1 {\n max2 = max1;\n idx2 = idx1;\n max1 = s[t];\n idx1 = t;\n } else if s[t] > max2 {\n max2 = s[t];\n idx2 = t;\n }\n t = t + 1;\n }\n\n let mut result: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n let best: i64;\n if i == idx1 {\n best = max2;\n } else {\n best = max1;\n }\n\n result.push(s[i] - best);\n i = i + 1;\n }\n\n result\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n\n let t: usize = it.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case_id: usize = 0;\n while case_id < t {\n let n: usize = it.next().expect(\"n\").parse().expect(\"valid n\");\n let mut s: Vec = Vec::with_capacity(n);\n\n let mut i: usize = 0;\n while i < n {\n s.push(it.next().expect(\"s_i\").parse().expect(\"valid s_i\"));\n i = i + 1;\n }\n\n let ans = Solution::advantages(s);\n let mut j: usize = 0;\n while j < ans.len() {\n if j > 0 {\n print!(\" \");\n }\n print!(\"{}\", ans[j]);\n j = j + 1;\n }\n println!();\n\n case_id = case_id + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1760C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1760C/tests/mutated_testcases.jsonl"} {"problem": "cf1760D", "family": "codeforces", "description": "# Challenging Valleys\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou are given an array $$$a[0 \\dots n-1]$$$ of $$$n$$$ integers. This array is called a \"*valley*\" if there exists **exactly one** subarray $$$a[l \\dots r]$$$ such that:\n - $$$0 \\le l \\le r \\le n-1$$$, \n- $$$a_l = a_{l+1} = a_{l+2} = \\dots = a_r$$$, \n- $$$l = 0$$$ or $$$a_{l-1} \\gt a_{l}$$$, \n- $$$r = n-1$$$ or $$$a_r \\lt a_{r+1}$$$. \n\nHere are three examples:\n \nThe first image shows the array [$$$3, 2, 2, 1, 2, 2, 3$$$], it **is a valley** because only subarray with indices $$$l=r=3$$$ satisfies the condition.\n\nThe second image shows the array [$$$1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6$$$], it **is a valley** because only subarray with indices $$$l=0, r=2$$$ satisfies the codition.\n\nThe third image shows the array [$$$1, 2, 3, 4, 3, 2, 1$$$], it **is not a valley** because two subarrays $$$l=r=0$$$ and $$$l=r=6$$$ that satisfy the condition.\n\nYou are asked whether the given array is a valley or not.\n\n*Note that we consider the array to be indexed from $$$0$$$.*\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$)\u00a0\u2014 the length of the array.\n\nThe second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$)\u00a0\u2014 the elements of the array.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases is smaller than $$$2\\cdot10^5$$$.\n\n## Output\n\nFor each test case, output \"`YES`\" (without quotes) if the array is a valley, and \"`NO`\" (without quotes) otherwise.\n\nYou can output the answer in any case (for example, the strings \"`yEs`\", \"`yes`\", \"`Yes`\" and \"`YES`\" will be recognized as a positive answer).\n\n## Example\n\n**Input:**\n```\n6\n7\n3 2 2 1 2 2 3\n11\n1 1 1 2 3 3 4 5 6 6 6\n7\n1 2 3 4 3 2 1\n7\n9 7 4 6 9 9 10\n1\n1000000000\n8\n9 4 4 5 9 4 9 10\n```\n**Output:**\n```\nYES\nYES\nNO\nYES\nYES\nNO\n```\n\n## Note\n\nThe first three test cases are explained in the statement.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_valley(n: usize, a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let count = Solution::is_valley(n, a);\n if count == 1 {\n writeln!(out, \"YES\").unwrap();\n } else {\n writeln!(out, \"NO\").unwrap();\n }\n }\n}\n```\n", "tags": "Rating: 1000\nimplementation, two pointers\nSolved by: 26784 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_valley_at(a: Seq, n: usize, l: int, r: int) -> bool {\n 0 <= l && l <= r && r < n &&\n (forall|i: int| l <= i && i <= r ==> a[i] == a[l]) &&\n (l == 0 || a[l - 1] > a[l]) &&\n (r == n - 1 || a[r] < a[r + 1])\n}\n\npub open spec fn has_unique_valley(a: Seq, n: usize) -> bool {\n exists|l: int, r: int| #![auto]\n is_valley_at(a, n, l, r) &&\n forall|l2: int, r2: int| is_valley_at(a, n, l2, r2) ==> l2 == l && r2 == r\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_valley(n: usize, a: Vec) -> (count: i64)\n requires\n 1 <= n && n <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 1000000000,\n ensures\n count >= 0,\n count == 1 <==> has_unique_valley(a@, n),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_valley(n: usize, a: Vec) -> i64 {\n let mut count: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let block_start = i;\n while i < n && a[i] == a[block_start] {\n i += 1;\n }\n let block_end = i - 1;\n let left_ok = block_start == 0 || a[block_start - 1] > a[block_start];\n let right_ok = block_end == n - 1 || a[block_end] < a[block_end + 1];\n if left_ok && right_ok {\n count += 1;\n }\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_valley_at(a: Seq, n: usize, l: int, r: int) -> bool {\n 0 <= l && l <= r && r < n &&\n (forall|i: int| l <= i && i <= r ==> a[i] == a[l]) &&\n (l == 0 || a[l - 1] > a[l]) &&\n (r == n - 1 || a[r] < a[r + 1])\n}\n\npub open spec fn has_unique_valley(a: Seq, n: usize) -> bool {\n exists|l: int, r: int| #![auto]\n is_valley_at(a, n, l, r) &&\n forall|l2: int, r2: int| is_valley_at(a, n, l2, r2) ==> l2 == l && r2 == r\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_valley(n: usize, a: Vec) -> (count: i64)\n requires\n 1 <= n && n <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 1000000000,\n ensures\n count >= 0,\n count == 1 <==> has_unique_valley(a@, n),\n {\n let mut count: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let block_start = i;\n while i < n && a[i] == a[block_start] {\n i += 1;\n }\n let block_end = i - 1;\n let left_ok = block_start == 0 || a[block_start - 1] > a[block_start];\n let right_ok = block_end == n - 1 || a[block_end] < a[block_end + 1];\n if left_ok && right_ok {\n count += 1;\n }\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn is_valley_at(a: Seq, n: usize, l: int, r: int) -> bool {\n 0 <= l && l <= r && r < n &&\n (forall|i: int| l <= i && i <= r ==> a[i] == a[l]) &&\n (l == 0 || a[l - 1] > a[l]) &&\n (r == n - 1 || a[r] < a[r + 1])\n}\n\npub open spec fn has_unique_valley(a: Seq, n: usize) -> bool {\n exists|l: int, r: int| #![auto]\n is_valley_at(a, n, l, r) &&\n forall|l2: int, r2: int| is_valley_at(a, n, l2, r2) ==> l2 == l && r2 == r\n}\n\nproof fn lemma_valley_ending_in_block_is_the_block(\n a: Seq,\n n: usize,\n block_start: int,\n block_end: int,\n l: int,\n r: int,\n)\n requires\n 0 <= block_start,\n block_start <= block_end,\n block_end < n as int,\n forall|j: int| block_start <= j && j <= block_end ==> a[j] == a[block_start],\n block_start == 0 || a[block_start - 1] != a[block_start],\n block_end == n as int - 1 || a[block_end] != a[block_end + 1],\n is_valley_at(a, n, l, r),\n block_start <= r,\n r <= block_end,\n ensures\n l == block_start,\n r == block_end,\n{\n if l < block_start {\n assert(block_start > 0);\n assert(l <= block_start - 1 && block_start <= r);\n assert(a[block_start - 1] == a[l]);\n assert(a[block_start] == a[l]);\n assert(a[block_start - 1] == a[block_start]);\n assert(false);\n }\n if l > block_start {\n assert(block_start <= l - 1 && l - 1 < l && l <= block_end);\n assert(a[l - 1] == a[block_start]);\n assert(a[l] == a[block_start]);\n assert(a[l - 1] == a[l]);\n assert(a[l - 1] > a[l]);\n assert(false);\n }\n if r < block_end {\n assert(block_start <= r && r < r + 1 && r + 1 <= block_end);\n assert(a[r] == a[block_start]);\n assert(a[r + 1] == a[block_start]);\n assert(a[r] == a[r + 1]);\n assert(a[r] < a[r + 1]);\n assert(false);\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_valley(n: usize, a: Vec) -> (count: i64)\n requires\n 1 <= n && n <= 200000,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> 1 <= a@[i] && a@[i] <= 1000000000,\n ensures\n count >= 0,\n count == 1 <==> has_unique_valley(a@, n),\n {\n let mut count: i64 = 0;\n let mut i: usize = 0;\n let ghost mut last_valley_l: int = -1;\n let ghost mut last_valley_r: int = -1;\n let ghost mut first_many_l: int = -1;\n let ghost mut first_many_r: int = -1;\n let ghost mut second_many_l: int = -1;\n let ghost mut second_many_r: int = -1;\n while i < n\n invariant\n 0 <= i && i <= n,\n n <= 200000,\n a.len() == n,\n forall|j: int| 0 <= j && j < n ==> 1 <= a@[j] && a@[j] <= 1000000000,\n count >= 0 && count <= i as i64,\n 0 < i && i < n ==> a@[i as int - 1] != a@[i as int],\n count == 0 ==> forall|l: int, r: int|\n 0 <= l && l <= r && r < i as int ==> !is_valley_at(a@, n, l, r),\n count == 1 ==> 0 <= last_valley_l && last_valley_l <= last_valley_r && last_valley_r < i as int &&\n is_valley_at(a@, n, last_valley_l, last_valley_r) &&\n forall|l: int, r: int|\n 0 <= l && l <= r && r < i as int && is_valley_at(a@, n, l, r) ==> l == last_valley_l && r == last_valley_r,\n count > 1 ==> 0 <= first_many_l && first_many_l <= first_many_r && first_many_r < i as int &&\n 0 <= second_many_l && second_many_l <= second_many_r && second_many_r < i as int &&\n is_valley_at(a@, n, first_many_l, first_many_r) &&\n is_valley_at(a@, n, second_many_l, second_many_r) &&\n (first_many_l != second_many_l || first_many_r != second_many_r),\n decreases n - i\n {\n let block_start = i;\n while i < n && a[i] == a[block_start]\n invariant\n block_start <= i && i <= n,\n n <= 200000,\n a.len() == n,\n forall|j: int| 0 <= j && j < n ==> 1 <= a@[j] && a@[j] <= 1000000000,\n forall|j: int| block_start as int <= j && j < i as int ==> a@[j] == a@[block_start as int],\n decreases n - i\n {\n i += 1;\n }\n let block_end = i - 1;\n let left_ok = block_start == 0 || a[block_start - 1] > a[block_start];\n let right_ok = block_end == n - 1 || a[block_end] < a[block_end + 1];\n proof {\n assert forall|j: int| block_start as int <= j && j <= block_end as int implies a@[j] == a@[block_start as int] by {\n assert(block_start as int <= j && j < i as int);\n }\n if block_start > 0 {\n assert(a@[block_start as int - 1] != a@[block_start as int]);\n }\n if block_end < n - 1 {\n assert(a@[block_end as int + 1] != a@[block_end as int]);\n }\n }\n if left_ok && right_ok {\n let ghost prev_last_l = last_valley_l;\n let ghost prev_last_r = last_valley_r;\n proof {\n assert(is_valley_at(a@, n, block_start as int, block_end as int));\n last_valley_l = block_start as int;\n last_valley_r = block_end as int;\n }\n count += 1;\n proof {\n if count == 1 {\n assert forall|l: int, r: int|\n 0 <= l && l <= r && r < i as int && is_valley_at(a@, n, l, r) implies l == last_valley_l && r == last_valley_r by {\n if r < block_start as int {\n assert(!is_valley_at(a@, n, l, r));\n } else {\n lemma_valley_ending_in_block_is_the_block(a@, n, block_start as int, block_end as int, l, r);\n }\n }\n }\n if count > 1 {\n if count == 2 {\n first_many_l = prev_last_l;\n first_many_r = prev_last_r;\n second_many_l = block_start as int;\n second_many_r = block_end as int;\n assert(prev_last_r < block_start as int);\n assert(is_valley_at(a@, n, prev_last_l, prev_last_r));\n assert(is_valley_at(a@, n, block_start as int, block_end as int));\n assert(prev_last_l != block_start as int || prev_last_r != block_end as int);\n }\n }\n }\n } else {\n proof {\n assert forall|l: int, r: int|\n block_start as int <= r && r < i as int && is_valley_at(a@, n, l, r) implies false by {\n lemma_valley_ending_in_block_is_the_block(a@, n, block_start as int, block_end as int, l, r);\n assert(l == block_start as int && r == block_end as int);\n assert(!(left_ok && right_ok));\n }\n }\n }\n }\n proof {\n if count == 1 {\n assert(has_unique_valley(a@, n));\n }\n if has_unique_valley(a@, n) {\n if count == 0 {\n assert(false);\n }\n if count > 1 {\n let (ul, ur) = choose|l: int, r: int|\n is_valley_at(a@, n, l, r) &&\n forall|l2x: int, r2x: int| is_valley_at(a@, n, l2x, r2x) ==> l2x == l && r2x == r;\n assert(first_many_l == ul && first_many_r == ur);\n assert(second_many_l == ul && second_many_r == ur);\n assert(first_many_l == second_many_l && first_many_r == second_many_r);\n assert(false);\n }\n assert(count == 1);\n }\n }\n count\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_valley(n: usize, a: Vec) -> i64 {\n let mut count: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let block_start = i;\n while i < n && a[i] == a[block_start] {\n i += 1;\n }\n let block_end = i - 1;\n let left_ok = block_start == 0 || a[block_start - 1] > a[block_start];\n let right_ok = block_end == n - 1 || a[block_end] < a[block_end + 1];\n if left_ok && right_ok {\n count += 1;\n }\n }\n count\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = stdin.lock().lines();\n\n let t: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a: Vec = lines.next().unwrap().unwrap().split_whitespace()\n .map(|s| s.parse().unwrap()).collect();\n let count = Solution::is_valley(n, a);\n if count == 1 {\n writeln!(out, \"YES\").unwrap();\n } else {\n writeln!(out, \"NO\").unwrap();\n }\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1760D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1760D/tests/mutated_testcases.jsonl"} {"problem": "cf1807D", "family": "codeforces", "description": "# Odd Queries\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou have an array $$$a_1, a_2, \\dots, a_n$$$. Answer $$$q$$$ queries of the following form: \n - If we change all elements in the range $$$a_l, a_{l+1}, \\dots, a_r$$$ of the array to $$$k$$$, will the sum of the entire array be odd? \n Note that queries are **independent** and do not affect future queries.\n\n## Input\n\nEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The description of the test cases follows.\n\nThe first line of each test case consists of $$$2$$$ integers $$$n$$$ and $$$q$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$; $$$1 \\le q \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the array and the number of queries.\n\nThe second line of each test case consists of $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the array $$$a$$$.\n\nThe next $$$q$$$ lines of each test case consists of $$$3$$$ integers $$$l,r,k$$$ ($$$1 \\le l \\le r \\le n$$$; $$$1 \\le k \\le 10^9$$$)\u00a0\u2014 the queries.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$, and the sum of $$$q$$$ doesn't exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each query, output \"`YES`\" if the sum of the entire array becomes odd, and \"`NO`\" otherwise.\n\nYou can output the answer in any case (upper or lower). For example, the strings \"`yEs`\", \"`yes`\", \"`Yes`\", and \"`YES`\" will be recognized as positive responses.\n\n## Example\n\n**Input:**\n```\n2\n5 5\n2 2 1 3 2\n2 3 3\n2 3 4\n1 5 5\n1 4 9\n2 4 3\n10 5\n1 1 1 1 1 1 1 1 1 1\n3 8 13\n2 5 10\n3 8 10\n1 10 2\n1 9 100\n```\n**Output:**\n```\nYES\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nNO\nYES\n```\n\n## Note\n\nFor the first test case: \n - If the elements in the range $$$(2, 3)$$$ would get set to $$$3$$$ the array would become $$$\\{2, 3, 3, 3, 2\\}$$$, the sum would be $$$2+3+3+3+2 = 13$$$ which is odd, so the answer is \"`YES`\".\n- If the elements in the range $$$(2, 3)$$$ would get set to $$$4$$$ the array would become $$$\\{2, 4, 4, 3, 2\\}$$$, the sum would be $$$2+4+4+3+2 = 15$$$ which is odd, so the answer is \"`YES`\".\n- If the elements in the range $$$(1, 5)$$$ would get set to $$$5$$$ the array would become $$$\\{5, 5, 5, 5, 5\\}$$$, the sum would be $$$5+5+5+5+5 = 25$$$ which is odd, so the answer is \"`YES`\".\n- If the elements in the range $$$(1, 4)$$$ would get set to $$$9$$$ the array would become $$$\\{9, 9, 9, 9, 2\\}$$$, the sum would be $$$9+9+9+9+2 = 38$$$ which is even, so the answer is \"`NO`\".\n- If the elements in the range $$$(2, 4)$$$ would get set to $$$3$$$ the array would become $$$\\{2, 3, 3, 3, 2\\}$$$, the sum would be $$$2+3+3+3+2 = 13$$$ which is odd, so the answer is \"`YES`\".\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn odd_queries(\n a: Vec,\n n: usize,\n ls: Vec,\n rs: Vec,\n ks: Vec,\n q: usize,\n ) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = iter.next().unwrap().parse().unwrap();\n let q: usize = iter.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let v: u32 = iter.next().unwrap().parse().unwrap();\n a.push(v);\n }\n let mut ls: Vec = Vec::with_capacity(q);\n let mut rs: Vec = Vec::with_capacity(q);\n let mut ks: Vec = Vec::with_capacity(q);\n for _ in 0..q {\n let l: u32 = iter.next().unwrap().parse().unwrap();\n let r: u32 = iter.next().unwrap().parse().unwrap();\n let k: u32 = iter.next().unwrap().parse().unwrap();\n ls.push(l);\n rs.push(r);\n ks.push(k);\n }\n let result = Solution::odd_queries(a, n, ls, rs, ks, q);\n for v in result {\n writeln!(out, \"{}\", if v { \"YES\" } else { \"NO\" }).unwrap();\n }\n }\n}\n```\n", "tags": "Rating: 900\ndata structures, implementation\nSolved by: 56531 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_range(a: Seq, l: int, r: int) -> int\n recommends 0 <= l <= r <= a.len(),\n decreases r - l,\n{\n if l >= r { 0int }\n else { a[l] as int + sum_range(a, l + 1, r) }\n}\n\nimpl Solution {\n pub fn odd_queries(\n a: Vec,\n n: usize,\n ls: Vec,\n rs: Vec,\n ks: Vec,\n q: usize,\n ) -> (result: Vec)\n requires\n 1 <= n <= 200_000,\n 1 <= q <= 200_000,\n a.len() == n,\n ls.len() == q,\n rs.len() == q,\n ks.len() == q,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n forall|i: int| 0 <= i < ls.len() ==> 1 <= #[trigger] ls[i] && ls[i] as int <= rs[i] as int && rs[i] as int <= n as int,\n forall|i: int| 0 <= i < rs.len() ==> 1 <= #[trigger] rs[i] && rs[i] as int <= n as int,\n forall|i: int| 0 <= i < ks.len() ==> 1 <= #[trigger] ks[i] <= 1_000_000_000,\n ensures\n result.len() == q,\n forall|i: int| 0 <= i < q ==> #[trigger] result[i] == (\n (sum_range(a@, 0, ls[i] as int - 1) +\n ks[i] as int * (rs[i] as int - ls[i] as int + 1) +\n sum_range(a@, rs[i] as int, n as int)) % 2 == 1\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn odd_queries(\n a: Vec,\n n: usize,\n ls: Vec,\n rs: Vec,\n ks: Vec,\n q: usize,\n ) -> Vec {\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0i64);\n let mut s: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n s += a[i] as i64;\n prefix.push(s);\n i += 1;\n }\n let pref_n: i64 = prefix[n];\n let mut result: Vec = Vec::with_capacity(q);\n let mut j: usize = 0;\n while j < q {\n let l = ls[j] as usize;\n let r = rs[j] as usize;\n let k_val = ks[j] as i64;\n let count: i64 = (r - l + 1) as i64;\n let pref_l_minus_1 = prefix[l - 1];\n let pref_r_v = prefix[r];\n let outside: i64 = pref_l_minus_1 + (pref_n - pref_r_v);\n let mid: i64 = k_val * count;\n let total: i64 = outside + mid;\n let answer: bool = total % 2 == 1;\n result.push(answer);\n j += 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_range(a: Seq, l: int, r: int) -> int\n recommends 0 <= l <= r <= a.len(),\n decreases r - l,\n{\n if l >= r { 0int }\n else { a[l] as int + sum_range(a, l + 1, r) }\n}\n\nimpl Solution {\n pub fn odd_queries(\n a: Vec,\n n: usize,\n ls: Vec,\n rs: Vec,\n ks: Vec,\n q: usize,\n ) -> (result: Vec)\n requires\n 1 <= n <= 200_000,\n 1 <= q <= 200_000,\n a.len() == n,\n ls.len() == q,\n rs.len() == q,\n ks.len() == q,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n forall|i: int| 0 <= i < ls.len() ==> 1 <= #[trigger] ls[i] && ls[i] as int <= rs[i] as int && rs[i] as int <= n as int,\n forall|i: int| 0 <= i < rs.len() ==> 1 <= #[trigger] rs[i] && rs[i] as int <= n as int,\n forall|i: int| 0 <= i < ks.len() ==> 1 <= #[trigger] ks[i] <= 1_000_000_000,\n ensures\n result.len() == q,\n forall|i: int| 0 <= i < q ==> #[trigger] result[i] == (\n (sum_range(a@, 0, ls[i] as int - 1) +\n ks[i] as int * (rs[i] as int - ls[i] as int + 1) +\n sum_range(a@, rs[i] as int, n as int)) % 2 == 1\n ),\n {\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0i64);\n let mut s: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n s += a[i] as i64;\n prefix.push(s);\n i += 1;\n }\n let pref_n: i64 = prefix[n];\n let mut result: Vec = Vec::with_capacity(q);\n let mut j: usize = 0;\n while j < q {\n let l = ls[j] as usize;\n let r = rs[j] as usize;\n let k_val = ks[j] as i64;\n let count: i64 = (r - l + 1) as i64;\n let pref_l_minus_1 = prefix[l - 1];\n let pref_r_v = prefix[r];\n let outside: i64 = pref_l_minus_1 + (pref_n - pref_r_v);\n let mid: i64 = k_val * count;\n let total: i64 = outside + mid;\n let answer: bool = total % 2 == 1;\n result.push(answer);\n j += 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_range(a: Seq, l: int, r: int) -> int\n recommends 0 <= l <= r <= a.len(),\n decreases r - l,\n{\n if l >= r { 0int }\n else { a[l] as int + sum_range(a, l + 1, r) }\n}\n\npub open spec fn pref_sum(a: Seq, k: int) -> int\n recommends 0 <= k <= a.len(),\n decreases k,\n{\n if k <= 0 { 0int }\n else { pref_sum(a, k - 1) + a[k - 1] as int }\n}\n\nproof fn lemma_pref_eq_sumrange(a: Seq, k: int)\n requires 0 <= k <= a.len(),\n ensures pref_sum(a, k) == sum_range(a, 0, k),\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_pref_eq_sumrange(a, k - 1);\n lemma_sum_range_extend(a, 0, k);\n }\n}\n\nproof fn lemma_sum_range_extend(a: Seq, l: int, r: int)\n requires 0 <= l, l + 1 <= r, r <= a.len(),\n ensures sum_range(a, l, r) == sum_range(a, l, r - 1) + a[r - 1] as int,\n decreases r - l,\n{\n if l == r - 1 {\n assert(sum_range(a, l + 1, r) == 0);\n assert(sum_range(a, l, r) == a[l] as int);\n assert(sum_range(a, l, r - 1) == 0);\n } else {\n lemma_sum_range_extend(a, l + 1, r);\n }\n}\n\nproof fn lemma_sum_split(a: Seq, l: int, mid: int, r: int)\n requires 0 <= l <= mid <= r <= a.len(),\n ensures sum_range(a, l, r) == sum_range(a, l, mid) + sum_range(a, mid, r),\n decreases mid - l,\n{\n if l >= mid {\n } else {\n lemma_sum_split(a, l + 1, mid, r);\n }\n}\n\nproof fn lemma_sum_range_bound(a: Seq, l: int, r: int, max_val: int)\n requires 0 <= l <= r <= a.len(),\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] as int <= max_val,\n max_val >= 0,\n ensures 0 <= sum_range(a, l, r),\n sum_range(a, l, r) <= (r - l) * max_val,\n decreases r - l,\n{\n if l >= r {\n } else {\n lemma_sum_range_bound(a, l + 1, r, max_val);\n assert(sum_range(a, l, r) == a[l] as int + sum_range(a, l + 1, r));\n assert(a[l] as int <= max_val);\n assert((r - l) * max_val == max_val + (r - l - 1) * max_val) by (nonlinear_arith);\n }\n}\n\nimpl Solution {\n pub fn odd_queries(\n a: Vec,\n n: usize,\n ls: Vec,\n rs: Vec,\n ks: Vec,\n q: usize,\n ) -> (result: Vec)\n requires\n 1 <= n <= 200_000,\n 1 <= q <= 200_000,\n a.len() == n,\n ls.len() == q,\n rs.len() == q,\n ks.len() == q,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n forall|i: int| 0 <= i < ls.len() ==> 1 <= #[trigger] ls[i] && ls[i] as int <= rs[i] as int && rs[i] as int <= n as int,\n forall|i: int| 0 <= i < rs.len() ==> 1 <= #[trigger] rs[i] && rs[i] as int <= n as int,\n forall|i: int| 0 <= i < ks.len() ==> 1 <= #[trigger] ks[i] <= 1_000_000_000,\n ensures\n result.len() == q,\n forall|i: int| 0 <= i < q ==> #[trigger] result[i] == (\n (sum_range(a@, 0, ls[i] as int - 1) +\n ks[i] as int * (rs[i] as int - ls[i] as int + 1) +\n sum_range(a@, rs[i] as int, n as int)) % 2 == 1\n ),\n {\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0i64);\n let mut s: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 1 <= n <= 200_000,\n a.len() == n,\n forall|kk: int| 0 <= kk < a.len() ==> 1 <= #[trigger] a[kk] <= 1_000_000_000,\n 0 <= i <= n,\n prefix.len() == i + 1,\n prefix[0] == 0i64,\n forall|j: int| 0 <= j <= i ==> #[trigger] prefix[j] as int == pref_sum(a@, j),\n s as int == pref_sum(a@, i as int),\n s >= 0,\n s <= (i as i64) * 1_000_000_000,\n decreases n - i,\n {\n s += a[i] as i64;\n prefix.push(s);\n i += 1;\n }\n let pref_n: i64 = prefix[n];\n let mut result: Vec = Vec::with_capacity(q);\n let mut j: usize = 0;\n while j < q\n invariant\n 1 <= n <= 200_000,\n 1 <= q <= 200_000,\n a.len() == n,\n ls.len() == q,\n rs.len() == q,\n ks.len() == q,\n prefix.len() == n + 1,\n forall|kk: int| 0 <= kk < a.len() ==> 1 <= #[trigger] a[kk] <= 1_000_000_000,\n forall|kk: int| 0 <= kk < ls.len() ==> 1 <= #[trigger] ls[kk] && ls[kk] as int <= rs[kk] as int && rs[kk] as int <= n as int,\n forall|kk: int| 0 <= kk < rs.len() ==> 1 <= #[trigger] rs[kk] && rs[kk] as int <= n as int,\n forall|kk: int| 0 <= kk < ks.len() ==> 1 <= #[trigger] ks[kk] <= 1_000_000_000,\n forall|kk: int| 0 <= kk <= n as int ==> #[trigger] prefix[kk] as int == pref_sum(a@, kk),\n pref_n == prefix[n as int],\n pref_n as int == pref_sum(a@, n as int),\n pref_n >= 0,\n pref_n <= (n as i64) * 1_000_000_000,\n 0 <= j <= q,\n result.len() == j,\n forall|i: int| 0 <= i < j as int ==> #[trigger] result[i] == (\n (sum_range(a@, 0, ls[i] as int - 1) +\n ks[i] as int * (rs[i] as int - ls[i] as int + 1) +\n sum_range(a@, rs[i] as int, n as int)) % 2 == 1\n ),\n decreases q - j,\n {\n let l = ls[j] as usize;\n let r = rs[j] as usize;\n let k_val = ks[j] as i64;\n let count: i64 = (r - l + 1) as i64;\n let pref_l_minus_1 = prefix[l - 1];\n let pref_r_v = prefix[r];\n \n \n \n \n proof {\n lemma_pref_eq_sumrange(a@, (l - 1) as int);\n lemma_pref_eq_sumrange(a@, r as int);\n lemma_pref_eq_sumrange(a@, n as int);\n lemma_sum_split(a@, 0, r as int, n as int);\n \n assert(forall|i: int| 0 <= i < a@.len() ==> #[trigger] a@[i] as int <= 1_000_000_000) by {\n assert(forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] && a[i] <= 1_000_000_000);\n };\n lemma_sum_range_bound(a@, 0, n as int, 1_000_000_000);\n lemma_sum_range_bound(a@, 0, r as int, 1_000_000_000);\n lemma_sum_range_bound(a@, r as int, n as int, 1_000_000_000);\n lemma_sum_range_bound(a@, 0, (l - 1) as int, 1_000_000_000);\n }\n let outside: i64 = pref_l_minus_1 + (pref_n - pref_r_v);\n assert(k_val * count <= 1_000_000_000i64 * 200_000i64) by (nonlinear_arith)\n requires 0 <= k_val <= 1_000_000_000, 0 <= count <= 200_000;\n assert(k_val * count >= 0) by (nonlinear_arith)\n requires 0 <= k_val, 0 <= count;\n let mid: i64 = k_val * count;\n let total: i64 = outside + mid;\n let answer: bool = total % 2 == 1;\n proof {\n let l_int = l as int;\n let r_int = r as int;\n let count_int = r_int - l_int + 1;\n let k_int = k_val as int;\n let s_lm1 = sum_range(a@, 0, (l - 1) as int);\n let s_r_n = sum_range(a@, r_int, n as int);\n let s_total = sum_range(a@, 0, n as int);\n let s_0r = sum_range(a@, 0, r_int);\n assert(s_total == s_0r + s_r_n);\n assert(pref_l_minus_1 as int == s_lm1);\n assert(pref_r_v as int == s_0r);\n assert(pref_n as int == s_total);\n assert(outside as int == s_lm1 + s_total - s_0r);\n assert(outside as int == s_lm1 + s_r_n);\n assert(mid as int == k_int * count_int);\n assert(total as int == s_lm1 + k_int * count_int + s_r_n);\n }\n result.push(answer);\n j += 1;\n }\n result\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn odd_queries(\n a: Vec,\n n: usize,\n ls: Vec,\n rs: Vec,\n ks: Vec,\n q: usize,\n ) -> Vec {\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0i64);\n let mut s: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n s += a[i] as i64;\n prefix.push(s);\n i += 1;\n }\n let pref_n: i64 = prefix[n];\n let mut result: Vec = Vec::with_capacity(q);\n let mut j: usize = 0;\n while j < q {\n let l = ls[j] as usize;\n let r = rs[j] as usize;\n let k_val = ks[j] as i64;\n let count: i64 = (r - l + 1) as i64;\n let pref_l_minus_1 = prefix[l - 1];\n let pref_r_v = prefix[r];\n let outside: i64 = pref_l_minus_1 + (pref_n - pref_r_v);\n let mid: i64 = k_val * count;\n let total: i64 = outside + mid;\n let answer: bool = total % 2 == 1;\n result.push(answer);\n j += 1;\n }\n result\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = iter.next().unwrap().parse().unwrap();\n let q: usize = iter.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let v: u32 = iter.next().unwrap().parse().unwrap();\n a.push(v);\n }\n let mut ls: Vec = Vec::with_capacity(q);\n let mut rs: Vec = Vec::with_capacity(q);\n let mut ks: Vec = Vec::with_capacity(q);\n for _ in 0..q {\n let l: u32 = iter.next().unwrap().parse().unwrap();\n let r: u32 = iter.next().unwrap().parse().unwrap();\n let k: u32 = iter.next().unwrap().parse().unwrap();\n ls.push(l);\n rs.push(r);\n ks.push(k);\n }\n let result = Solution::odd_queries(a, n, ls, rs, ks, q);\n for v in result {\n writeln!(out, \"{}\", if v { \"YES\" } else { \"NO\" }).unwrap();\n }\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1807D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1807D/tests/mutated_testcases.jsonl"} {"problem": "cf1829B", "family": "codeforces", "description": "# Blank Space\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou are given a binary array $$$a$$$ of $$$n$$$ elements, a binary array is an array consisting only of $$$0$$$s and $$$1$$$s. \n\nA blank space is a segment of **consecutive** elements consisting of only $$$0$$$s. \n\nYour task is to find the length of the longest blank space.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$)\u00a0\u2014 the length of the array.\n\nThe second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$0 \\leq a_i \\leq 1$$$)\u00a0\u2014 the elements of the array.\n\n## Output\n\nFor each test case, output a single integer\u00a0\u2014 the length of the longest blank space.\n\n## Example\n\n**Input:**\n```\n5\n5\n1 0 0 1 0\n4\n0 1 1 1\n1\n0\n3\n1 1 1\n9\n1 0 0 0 1 0 0 0 1\n```\n**Output:**\n```\n2\n1\n1\n0\n3\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn longest_blank_space(a: &Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::longest_blank_space(&a);\n println!(\"{}\", ans);\n tc = tc + 1;\n }\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 75733 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn zero_run_len_ending_at(a: Seq, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= a.len() {\n 0\n } else if a[i] != 0 {\n 0\n } else if i == 0 {\n 1\n } else if a[i - 1] == 0 {\n zero_run_len_ending_at(a, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_zero_run_upto(a: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = zero_run_len_ending_at(a, hi);\n let prev = max_zero_run_upto(a, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn longest_blank_space(a: &Vec) -> (result: i32)\n requires\n 1 <= a.len() && a.len() <= 100,\n forall|j: int|\n 0 <= j < a.len() ==> #[trigger] a[j] == 0 || a[j] == 1,\n ensures\n 0 <= result as int,\n result as int == max_zero_run_upto(a@, (a.len() as int) - 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn longest_blank_space(a: &Vec) -> i32 {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n if a[i] == 0 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn zero_run_len_ending_at(a: Seq, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= a.len() {\n 0\n } else if a[i] != 0 {\n 0\n } else if i == 0 {\n 1\n } else if a[i - 1] == 0 {\n zero_run_len_ending_at(a, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_zero_run_upto(a: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = zero_run_len_ending_at(a, hi);\n let prev = max_zero_run_upto(a, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn longest_blank_space(a: &Vec) -> (result: i32)\n requires\n 1 <= a.len() && a.len() <= 100,\n forall|j: int|\n 0 <= j < a.len() ==> #[trigger] a[j] == 0 || a[j] == 1,\n ensures\n 0 <= result as int,\n result as int == max_zero_run_upto(a@, (a.len() as int) - 1),\n {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n while i < n\n decreases n - i,\n {\n if a[i] == 0 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n } else {\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn zero_run_len_ending_at(a: Seq, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= a.len() {\n 0\n } else if a[i] != 0 {\n 0\n } else if i == 0 {\n 1\n } else if a[i - 1] == 0 {\n zero_run_len_ending_at(a, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_zero_run_upto(a: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = zero_run_len_ending_at(a, hi);\n let prev = max_zero_run_upto(a, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nproof fn lemma_max_zero_upto_nonneg(a: Seq, hi: int)\n requires\n hi >= -1,\n ensures\n max_zero_run_upto(a, hi) >= 0,\n decreases hi + 1,\n{\n if hi < 0 {\n assert(max_zero_run_upto(a, hi) == 0);\n } else {\n lemma_max_zero_upto_nonneg(a, hi - 1);\n assert(max_zero_run_upto(a, hi) >= 0);\n }\n}\n\nproof fn lemma_max_upto_ge_zero_run(a: Seq, hi: int, j: int)\n requires\n 0 <= j <= hi,\n ensures\n zero_run_len_ending_at(a, j) <= max_zero_run_upto(a, hi),\n decreases hi - j,\n{\n if j == hi {\n assert(max_zero_run_upto(a, hi) >= zero_run_len_ending_at(a, hi));\n } else {\n lemma_max_upto_ge_zero_run(a, hi - 1, j);\n assert(max_zero_run_upto(a, hi) >= max_zero_run_upto(a, hi - 1));\n }\n}\n\nproof fn lemma_zero_run_len_le_index(a: Seq, idx: int)\n requires\n 0 <= idx < a.len(),\n forall|j: int| 0 <= j < a.len() ==> a[j] == 0 || a[j] == 1,\n ensures\n zero_run_len_ending_at(a, idx) <= idx + 1,\n decreases idx + 1,\n{\n if idx == 0 {\n } else {\n lemma_zero_run_len_le_index(a, idx - 1);\n if a[idx] != 0 {\n } else if a[idx - 1] == 0 {\n assert(zero_run_len_ending_at(a, idx) == zero_run_len_ending_at(a, idx - 1) + 1);\n assert(zero_run_len_ending_at(a, idx - 1) <= idx);\n } else {\n }\n }\n}\n\nimpl Solution {\n pub fn longest_blank_space(a: &Vec) -> (result: i32)\n requires\n 1 <= a.len() && a.len() <= 100,\n forall|j: int|\n 0 <= j < a.len() ==> #[trigger] a[j] == 0 || a[j] == 1,\n ensures\n 0 <= result as int,\n result as int == max_zero_run_upto(a@, (a.len() as int) - 1),\n {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n proof {\n reveal_with_fuel(zero_run_len_ending_at, 5);\n reveal_with_fuel(max_zero_run_upto, 5);\n assert(zero_run_len_ending_at(a@, -1) == 0);\n assert(max_zero_run_upto(a@, -1) == 0);\n assert(cur as int == zero_run_len_ending_at(a@, (i as int) - 1));\n assert(best as int == max_zero_run_upto(a@, (i as int) - 1));\n }\n while i < n\n invariant\n 1 <= a.len() && a.len() <= 100,\n n == a.len(),\n forall|j: int|\n 0 <= j < a.len() as int ==> #[trigger] a[j] == 0 || a[j] == 1,\n i <= n,\n cur as int == zero_run_len_ending_at(a@, (i as int) - 1),\n best as int == max_zero_run_upto(a@, (i as int) - 1),\n cur as int <= (i as int) + 1,\n best as int <= (i as int) + 1,\n 0 <= cur as int && 0 <= best as int,\n decreases n - i,\n {\n if a[i] == 0 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n } else {\n }\n proof {\n reveal_with_fuel(zero_run_len_ending_at, 5);\n assert(cur as int == zero_run_len_ending_at(a@, i as int));\n lemma_zero_run_len_le_index(a@, i as int);\n let e = zero_run_len_ending_at(a@, i as int);\n let pm = max_zero_run_upto(a@, (i as int) - 1);\n reveal_with_fuel(max_zero_run_upto, 5);\n assert(max_zero_run_upto(a@, i as int) == if e > pm { e } else { pm });\n assert(best as int == max_zero_run_upto(a@, i as int));\n }\n i = i + 1;\n }\n proof {\n assert(i == n);\n assert(best as int == max_zero_run_upto(a@, (n as int) - 1));\n lemma_max_zero_upto_nonneg(a@, (a.len() as int) - 1);\n }\n best\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn longest_blank_space(a: &Vec) -> i32 {\n let n = a.len();\n let mut best: i32 = 0;\n let mut cur: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n if a[i] == 0 {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::longest_blank_space(&a);\n println!(\"{}\", ans);\n tc = tc + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1829B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1829B/tests/mutated_testcases.jsonl"} {"problem": "cf1833C", "family": "codeforces", "description": "# Vlad Building Beautiful Array\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nVlad was given an array $$$a$$$ of $$$n$$$ positive integers. Now he wants to build a *beautiful* array $$$b$$$ of length $$$n$$$ from it.\n\nVlad considers an array *beautiful* if all the numbers in it are positive and have the same parity. That is, all numbers in the *beautiful* array are **greater** than zero and are either all even or all odd.\n\nTo build the array $$$b$$$, Vlad can assign each $$$b_i$$$ either the value $$$a_i$$$ or $$$a_i - a_j$$$, where any $$$j$$$ from $$$1$$$ to $$$n$$$ can be chosen.\n\nTo avoid trying to do the impossible, Vlad asks you to determine whether it is possible to build a *beautiful* array $$$b$$$ of length $$$n$$$ using his array $$$a$$$.\n\n## Input\n\nThe first line of input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThen follow the descriptions of the test cases.\n\nThe first line of each case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the array $$$a$$$.\n\nThe second line of each case contains $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the elements of the array $$$a$$$.\n\nIt is guaranteed that the sum of $$$n$$$ over all cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nOutput $$$t$$$ strings, each of which is the answer to the corresponding test case. As the answer, output \"`YES`\" if Vlad can build a *beautiful* array $$$b$$$, and \"`NO`\" otherwise.\n\nYou can output the answer in any case (for example, the strings \"`yEs`\", \"`yes`\", \"`Yes`\" and \"`YES`\" will be recognized as a positive answer).\n\n## Example\n\n**Input:**\n```\n7\n5\n2 6 8 4 3\n5\n1 4 7 6 9\n4\n2 6 4 10\n7\n5 29 13 9 10000001 11 3\n5\n2 1 2 4 2\n5\n2 4 5 4 3\n4\n2 5 5 4\n```\n**Output:**\n```\nNO\nYES\nYES\nYES\nYES\nNO\nNO\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn vlad_beautiful(a: Vec, n: usize) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let v: u32 = iter.next().unwrap().parse().unwrap();\n a.push(v);\n }\n let result = Solution::vlad_beautiful(a, n);\n writeln!(out, \"{}\", if result { \"YES\" } else { \"NO\" }).unwrap();\n }\n}\n```\n", "tags": "Rating: 800\ngreedy, math\nSolved by: 27869 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn vlad_beautiful(a: Vec, n: usize) -> (result: bool)\n requires\n 1 <= n <= 200_000,\n a.len() == n,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n ensures\n \n result == (\n (forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] % 2 == 1)\n ||\n (forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] % 2 == 0)\n ||\n (exists|j: int| 0 <= j < a.len() && #[trigger] a[j] % 2 == 1 &&\n (forall|i: int| 0 <= i < a.len() && a[i] % 2 == 0 ==> #[trigger] a[i] > a[j]))\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn vlad_beautiful(a: Vec, n: usize) -> bool {\n let mut min_even: u64 = u64::MAX;\n let mut min_odd: u64 = u64::MAX;\n let mut min_even_idx: usize = 0;\n let mut min_odd_idx: usize = 0;\n let mut i: usize = 0;\n while i < n {\n let v = a[i] as u64;\n if a[i] % 2 == 0 {\n if v < min_even {\n min_even = v;\n min_even_idx = i;\n }\n } else {\n if v < min_odd {\n min_odd = v;\n min_odd_idx = i;\n }\n }\n i += 1;\n }\n if min_even == u64::MAX {\n true\n } else if min_odd == u64::MAX {\n true\n } else {\n let res = min_odd < min_even;\n res\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn vlad_beautiful(a: Vec, n: usize) -> (result: bool)\n requires\n 1 <= n <= 200_000,\n a.len() == n,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n ensures\n result == (\n (forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] % 2 == 1)\n ||\n (forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] % 2 == 0)\n ||\n (exists|j: int| 0 <= j < a.len() && #[trigger] a[j] % 2 == 1 &&\n (forall|i: int| 0 <= i < a.len() && a[i] % 2 == 0 ==> #[trigger] a[i] > a[j]))\n ),\n {\n let mut min_even: u64 = u64::MAX;\n let mut min_odd: u64 = u64::MAX;\n let mut min_even_idx: usize = 0;\n let mut min_odd_idx: usize = 0;\n let mut i: usize = 0;\n while i < n {\n let v = a[i] as u64;\n if a[i] % 2 == 0 {\n if v < min_even {\n min_even = v;\n min_even_idx = i;\n }\n } else {\n if v < min_odd {\n min_odd = v;\n min_odd_idx = i;\n }\n }\n i += 1;\n }\n if min_even == u64::MAX {\n true\n } else if min_odd == u64::MAX {\n true\n } else {\n let res = min_odd < min_even;\n res\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn vlad_beautiful(a: Vec, n: usize) -> (result: bool)\n requires\n 1 <= n <= 200_000,\n a.len() == n,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n ensures\n result == (\n (forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] % 2 == 1)\n ||\n (forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] % 2 == 0)\n ||\n (exists|j: int| 0 <= j < a.len() && #[trigger] a[j] % 2 == 1 &&\n (forall|i: int| 0 <= i < a.len() && a[i] % 2 == 0 ==> #[trigger] a[i] > a[j]))\n ),\n {\n let mut min_even: u64 = u64::MAX;\n let mut min_odd: u64 = u64::MAX;\n let mut min_even_idx: usize = 0;\n let mut min_odd_idx: usize = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 1 <= n <= 200_000,\n a.len() == n,\n forall|k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 1_000_000_000,\n 0 <= i <= n,\n \n (min_even == u64::MAX) <==> (forall|k: int| 0 <= k < i ==> #[trigger] a[k] % 2 == 1),\n (min_odd == u64::MAX) <==> (forall|k: int| 0 <= k < i ==> #[trigger] a[k] % 2 == 0),\n min_even != u64::MAX ==> (\n min_even_idx < i && a[min_even_idx as int] % 2 == 0 && a[min_even_idx as int] as u64 == min_even\n ),\n min_odd != u64::MAX ==> (\n min_odd_idx < i && a[min_odd_idx as int] % 2 == 1 && a[min_odd_idx as int] as u64 == min_odd\n ),\n forall|k: int| 0 <= k < i && a[k] % 2 == 0 ==> #[trigger] (a[k] as u64) >= min_even,\n forall|k: int| 0 <= k < i && a[k] % 2 == 1 ==> #[trigger] (a[k] as u64) >= min_odd,\n decreases n - i,\n {\n let v = a[i] as u64;\n if a[i] % 2 == 0 {\n if v < min_even {\n min_even = v;\n min_even_idx = i;\n }\n } else {\n if v < min_odd {\n min_odd = v;\n min_odd_idx = i;\n }\n }\n i += 1;\n }\n \n if min_even == u64::MAX {\n \n proof {\n assert(forall|k: int| 0 <= k < a.len() ==> #[trigger] a[k] % 2 == 1);\n }\n true\n } else if min_odd == u64::MAX {\n \n proof {\n assert(forall|k: int| 0 <= k < a.len() ==> #[trigger] a[k] % 2 == 0);\n }\n true\n } else {\n \n let res = min_odd < min_even;\n if res {\n \n proof {\n let j = min_odd_idx as int;\n assert(0 <= j < a.len());\n assert(a[j] % 2 == 1);\n assert(a[j] as u64 == min_odd);\n assert(min_odd < min_even);\n assert forall|i2: int| 0 <= i2 < a.len() && a[i2] % 2 == 0 implies #[trigger] a[i2] > a[j] by {\n assert((a[i2] as u64) >= min_even);\n assert((a[i2] as u64) > min_odd);\n assert((a[i2] as u64) > (a[j] as u64));\n }\n }\n } else {\n \n proof {\n \n \n \n assert(min_odd >= min_even);\n let bad_idx = min_even_idx as int;\n assert(0 <= bad_idx < a.len());\n assert(a[bad_idx] % 2 == 0);\n assert(a[bad_idx] as u64 == min_even);\n assert forall|j: int| 0 <= j < a.len() && a[j] % 2 == 1 implies #[trigger] a[j] >= a[bad_idx] by {\n assert((a[j] as u64) >= min_odd);\n assert((a[j] as u64) >= min_even);\n assert((a[j] as u64) >= (a[bad_idx] as u64));\n }\n \n assert(!(exists|j: int| 0 <= j < a.len() && #[trigger] a[j] % 2 == 1 &&\n (forall|i2: int| 0 <= i2 < a.len() && a[i2] % 2 == 0 ==> #[trigger] a[i2] > a[j])));\n \n assert(!(forall|i2: int| 0 <= i2 < a.len() ==> #[trigger] a[i2] % 2 == 1));\n \n assert(!(forall|i2: int| 0 <= i2 < a.len() ==> #[trigger] a[i2] % 2 == 0));\n }\n }\n res\n }\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn vlad_beautiful(a: Vec, n: usize) -> bool {\n let mut min_even: u64 = u64::MAX;\n let mut min_odd: u64 = u64::MAX;\n let mut min_even_idx: usize = 0;\n let mut min_odd_idx: usize = 0;\n let mut i: usize = 0;\n while i < n {\n let v = a[i] as u64;\n if a[i] % 2 == 0 {\n if v < min_even {\n min_even = v;\n min_even_idx = i;\n }\n } else {\n if v < min_odd {\n min_odd = v;\n min_odd_idx = i;\n }\n }\n i += 1;\n }\n if min_even == u64::MAX {\n true\n } else if min_odd == u64::MAX {\n true\n } else {\n let res = min_odd < min_even;\n res\n }\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let v: u32 = iter.next().unwrap().parse().unwrap();\n a.push(v);\n }\n let result = Solution::vlad_beautiful(a, n);\n writeln!(out, \"{}\", if result { \"YES\" } else { \"NO\" }).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1833C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1833C/tests/mutated_testcases.jsonl"} {"problem": "cf1840C", "family": "codeforces", "description": "# Ski Resort\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nDima Vatrushin is a math teacher at school. He was sent on vacation for $$$n$$$ days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several **consecutive days** and go skiing. Since the vacation requires careful preparation, he will only go for **at least $$$k$$$ days**.\n\nYou are given an array $$$a$$$ containing the weather forecast at the resort. That is, on the $$$i$$$-th day, the temperature will be $$$a_i$$$ degrees.\n\nDima was born in Siberia, so he can go on vacation only if the temperature does not rise above $$$q$$$ degrees throughout the vacation.\n\nUnfortunately, Dima was so absorbed in abstract algebra that he forgot how to count. He asks you to help him and count the number of ways to choose vacation dates at the resort.\n\n## Input\n\nThe first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThen follow the descriptions of the test cases.\n\nThe first line of each test case contains three integers $$$n$$$, $$$k$$$, $$$q$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le k \\le n$$$, $$$-10^9 \\le q \\le 10^9$$$)\u00a0\u2014 the length of the array $$$a$$$, the minimum number of days at the resort, and the maximum comfortable temperature for Dima.\n\nThe second line of each test case contains $$$n$$$ integers $$$a_1, a_2, a_3, \\dots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$)\u00a0\u2014 the temperature at the ski resort.\n\nThe sum of all $$$n$$$ values over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nOutput $$$t$$$ integers, each of which is the answer to the corresponding test case\u00a0\u2014 the number of ways for Dima to choose vacation dates at the resort.\n\n## Example\n\n**Input:**\n```\n7\n3 1 15\n-5 0 -10\n5 3 -33\n8 12 9 0 5\n4 3 12\n12 12 10 15\n4 1 -5\n0 -1 2 5\n5 5 0\n3 -1 4 -5 -3\n1 1 5\n5\n6 1 3\n0 3 -2 5 -4 -4\n```\n**Output:**\n```\n6\n0\n1\n0\n0\n1\n9\n```\n\n## Note\n\nIn the first test case of the example, Dima can go on any day, so the suitable dates for him are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3].\n\nIn the second and fourth test cases of the example, Dima cannot go on any day due to the high temperature, so there are no suitable dates.\n\nIn the third test case of the example, Dima can only go on the dates [1, 2, 3].\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_vacations(n: usize, k: usize, q: i64, a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(t_str)) = lines.next() {\n if let Ok(t) = t_str.trim().parse::() {\n for _ in 0..t {\n let nkq = lines.next().unwrap().unwrap();\n let mut nkq_it = nkq.split_whitespace();\n let n: usize = nkq_it.next().unwrap().parse().unwrap();\n let k: usize = nkq_it.next().unwrap().parse().unwrap();\n let q: i64 = nkq_it.next().unwrap().parse().unwrap();\n let a_line = lines.next().unwrap().unwrap();\n let a: Vec = a_line\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::count_vacations(n, k, q, a);\n println!(\"{}\", ans);\n }\n }\n }\n}\n```\n", "tags": "Rating: 1000\ncombinatorics, math, two pointers\nSolved by: 45969 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn subarray_all_leq(a: Seq, q: i64, l: int, r: int) -> bool {\n forall|i: int| l <= i && i <= r ==> #[trigger] a[i] <= q\n}\n\npub open spec fn valid_interval(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> bool {\n 0 <= l && l <= r && r < n && r - l + 1 >= k && subarray_all_leq(a, q, l, r)\n}\n\npub open spec fn pair_contrib(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> int {\n if valid_interval(n, k, q, a, l, r) {\n 1\n } else {\n 0\n }\n}\n\npub open spec fn sum_r(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> int\n decreases n - r\n{\n if r >= n {\n 0\n } else {\n pair_contrib(n, k, q, a, l, r) + sum_r(n, k, q, a, l, r + 1)\n }\n}\n\npub open spec fn sum_l(n: int, k: int, q: i64, a: Seq, l: int) -> int\n decreases n - l\n{\n if l >= n {\n 0\n } else {\n sum_r(n, k, q, a, l, l) + sum_l(n, k, q, a, l + 1)\n }\n}\n\npub open spec fn vacation_count(n: int, k: int, q: i64, a: Seq) -> int {\n if n <= 0 {\n 0\n } else {\n sum_l(n, k, q, a, 0)\n }\n}\n\npub open spec fn extend_good_end(q: i64, a: Seq, pos: int) -> int\n recommends\n 0 <= pos <= a.len(),\n decreases a.len() as int - pos\n{\n let n = a.len() as int;\n if pos >= n {\n n\n } else if a[pos] > q {\n pos\n } else {\n extend_good_end(q, a, pos + 1)\n }\n}\n\npub open spec fn triangle_contrib(L: int, k: int) -> int {\n if L >= k {\n (L - k + 1) * (L - k + 2) / 2\n } else {\n 0\n }\n}\n\npub open spec fn scan_total(k: int, q: i64, a: Seq, pos: int) -> int\n recommends\n 0 <= pos <= a.len(),\n decreases a.len() as int - pos\n{\n let n = a.len() as int;\n if pos >= n {\n 0\n } else if a[pos] > q {\n scan_total(k, q, a, pos + 1)\n } else {\n let end = extend_good_end(q, a, pos);\n triangle_contrib(end - pos, k) + scan_total(k, q, a, end)\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn count_vacations(n: usize, k: usize, q: i64, a: Vec) -> (res: i64)\n requires\n 1 <= n && n <= 200000,\n 1 <= k && k <= n,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> -1000000000 <= a@[i] && a@[i] <= 1000000000,\n -1000000000 <= q && q <= 1000000000,\n ensures\n res as int == scan_total(k as int, q, a@, 0),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_vacations(n: usize, k: usize, q: i64, a: Vec) -> i64 {\n let mut pos: usize = 0;\n let mut total: i64 = 0;\n while pos < n {\n if a[pos] > q {\n pos += 1;\n } else {\n let start = pos;\n while pos < n && a[pos] <= q {\n pos += 1;\n }\n let seg_len = pos - start;\n if seg_len >= k {\n let x = (seg_len - k) as i64 + 1;\n total += x * (x + 1) / 2;\n }\n }\n }\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn subarray_all_leq(a: Seq, q: i64, l: int, r: int) -> bool {\n forall|i: int| l <= i && i <= r ==> #[trigger] a[i] <= q\n}\n\npub open spec fn valid_interval(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> bool {\n 0 <= l && l <= r && r < n && r - l + 1 >= k && subarray_all_leq(a, q, l, r)\n}\n\npub open spec fn pair_contrib(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> int {\n if valid_interval(n, k, q, a, l, r) {\n 1\n } else {\n 0\n }\n}\n\npub open spec fn sum_r(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> int\n decreases n - r\n{\n if r >= n {\n 0\n } else {\n pair_contrib(n, k, q, a, l, r) + sum_r(n, k, q, a, l, r + 1)\n }\n}\n\npub open spec fn sum_l(n: int, k: int, q: i64, a: Seq, l: int) -> int\n decreases n - l\n{\n if l >= n {\n 0\n } else {\n sum_r(n, k, q, a, l, l) + sum_l(n, k, q, a, l + 1)\n }\n}\n\npub open spec fn vacation_count(n: int, k: int, q: i64, a: Seq) -> int {\n if n <= 0 {\n 0\n } else {\n sum_l(n, k, q, a, 0)\n }\n}\n\npub open spec fn extend_good_end(q: i64, a: Seq, pos: int) -> int\n recommends\n 0 <= pos <= a.len(),\n decreases a.len() as int - pos\n{\n let n = a.len() as int;\n if pos >= n {\n n\n } else if a[pos] > q {\n pos\n } else {\n extend_good_end(q, a, pos + 1)\n }\n}\n\npub open spec fn triangle_contrib(L: int, k: int) -> int {\n if L >= k {\n (L - k + 1) * (L - k + 2) / 2\n } else {\n 0\n }\n}\n\npub open spec fn scan_total(k: int, q: i64, a: Seq, pos: int) -> int\n recommends\n 0 <= pos <= a.len(),\n decreases a.len() as int - pos\n{\n let n = a.len() as int;\n if pos >= n {\n 0\n } else if a[pos] > q {\n scan_total(k, q, a, pos + 1)\n } else {\n let end = extend_good_end(q, a, pos);\n triangle_contrib(end - pos, k) + scan_total(k, q, a, end)\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn count_vacations(n: usize, k: usize, q: i64, a: Vec) -> (res: i64)\n requires\n 1 <= n && n <= 200000,\n 1 <= k && k <= n,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> -1000000000 <= a@[i] && a@[i] <= 1000000000,\n -1000000000 <= q && q <= 1000000000,\n ensures\n res as int == scan_total(k as int, q, a@, 0),\n {\n let mut pos: usize = 0;\n let mut total: i64 = 0;\n while pos < n {\n if a[pos] > q {\n pos += 1;\n } else {\n let start = pos;\n while pos < n && a[pos] <= q {\n pos += 1;\n }\n let seg_len = pos - start;\n if seg_len >= k {\n let x = (seg_len - k) as i64 + 1;\n total += x * (x + 1) / 2;\n }\n }\n }\n total\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn subarray_all_leq(a: Seq, q: i64, l: int, r: int) -> bool {\n forall|i: int| l <= i && i <= r ==> #[trigger] a[i] <= q\n}\n\npub open spec fn valid_interval(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> bool {\n 0 <= l && l <= r && r < n && r - l + 1 >= k && subarray_all_leq(a, q, l, r)\n}\n\npub open spec fn pair_contrib(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> int {\n if valid_interval(n, k, q, a, l, r) {\n 1\n } else {\n 0\n }\n}\n\npub open spec fn sum_r(n: int, k: int, q: i64, a: Seq, l: int, r: int) -> int\n decreases n - r\n{\n if r >= n {\n 0\n } else {\n pair_contrib(n, k, q, a, l, r) + sum_r(n, k, q, a, l, r + 1)\n }\n}\n\npub open spec fn sum_l(n: int, k: int, q: i64, a: Seq, l: int) -> int\n decreases n - l\n{\n if l >= n {\n 0\n } else {\n sum_r(n, k, q, a, l, l) + sum_l(n, k, q, a, l + 1)\n }\n}\n\npub open spec fn vacation_count(n: int, k: int, q: i64, a: Seq) -> int {\n if n <= 0 {\n 0\n } else {\n sum_l(n, k, q, a, 0)\n }\n}\n\npub open spec fn extend_good_end(q: i64, a: Seq, pos: int) -> int\n recommends\n 0 <= pos <= a.len(),\n decreases a.len() as int - pos\n{\n let n = a.len() as int;\n if pos >= n {\n n\n } else if a[pos] > q {\n pos\n } else {\n proof {\n assert(pos < n);\n assert(n - (pos + 1) < n - pos);\n }\n extend_good_end(q, a, pos + 1)\n }\n}\n\npub open spec fn triangle_contrib(L: int, k: int) -> int {\n if L >= k {\n (L - k + 1) * (L - k + 2) / 2\n } else {\n 0\n }\n}\n\npub proof fn lemma_extend_good_end_gt_pos(q: i64, a: Seq, pos: int)\n requires\n 0 <= pos < a.len(),\n a[pos] <= q,\n ensures\n extend_good_end(q, a, pos) > pos,\n decreases a.len() as int - pos,\n{\n let n = a.len() as int;\n reveal_with_fuel(extend_good_end, 10);\n if pos + 1 >= n {\n assert(pos == n - 1);\n assert(extend_good_end(q, a, pos) == n);\n assert(n > pos);\n } else if a[pos + 1] > q {\n assert(extend_good_end(q, a, pos) == pos + 1);\n assert(pos + 1 > pos);\n } else {\n assert(a[pos + 1] <= q);\n assert(0 <= pos + 1 < n);\n lemma_extend_good_end_gt_pos(q, a, pos + 1);\n }\n}\n\npub proof fn lemma_extend_good_end_le_len(q: i64, a: Seq, pos: int)\n requires\n 0 <= pos <= a.len(),\n ensures\n pos <= extend_good_end(q, a, pos) && extend_good_end(q, a, pos) <= a.len() as int,\n decreases a.len() as int - pos,\n{\n let n = a.len() as int;\n reveal_with_fuel(extend_good_end, 10);\n if pos >= n {\n assert(extend_good_end(q, a, pos) == n);\n } else if a[pos] > q {\n assert(extend_good_end(q, a, pos) == pos);\n assert(pos <= n);\n } else {\n lemma_extend_good_end_le_len(q, a, pos + 1);\n }\n}\n\npub open spec fn scan_total(k: int, q: i64, a: Seq, pos: int) -> int\n recommends\n 0 <= pos <= a.len(),\n decreases a.len() as int - pos\n when 0 <= pos && pos <= a.len() as int\n{\n let n = a.len() as int;\n if pos >= n {\n 0\n } else if a[pos] > q {\n proof {\n assert(pos < n);\n assert(n - (pos + 1) < n - pos);\n }\n scan_total(k, q, a, pos + 1)\n } else {\n proof {\n assert(pos < n);\n assert(pos < a.len());\n assert(!(a[pos] > q));\n assert(a[pos] <= q);\n lemma_extend_good_end_gt_pos(q, a, pos);\n lemma_extend_good_end_le_len(q, a, pos);\n }\n let end = extend_good_end(q, a, pos);\n proof {\n assert(end > pos);\n assert(end <= n);\n assert(n - end < n - pos);\n }\n triangle_contrib(end - pos, k) + scan_total(k, q, a, end)\n }\n}\n\npub proof fn lemma_scan_total_nonneg(k: int, q: i64, a: Seq, pos: int)\n requires\n 0 <= pos <= a.len(),\n ensures\n scan_total(k, q, a, pos) >= 0,\n decreases a.len() as int - pos,\n{\n let n = a.len() as int;\n reveal_with_fuel(scan_total, 2);\n if pos >= n {\n } else if a[pos] > q {\n lemma_scan_total_nonneg(k, q, a, pos + 1);\n } else {\n lemma_extend_good_end_gt_pos(q, a, pos);\n lemma_extend_good_end_le_len(q, a, pos);\n let end = extend_good_end(q, a, pos);\n lemma_scan_total_nonneg(k, q, a, end);\n }\n}\n\npub proof fn lemma_scan_total_bound(k: int, q: i64, a: Seq, pos: int)\n requires\n 0 <= pos <= a.len(),\n k >= 1,\n ensures\n scan_total(k, q, a, pos) <= (a.len() as int - pos) * (a.len() as int - pos + 1) / 2,\n decreases a.len() as int - pos,\n{\n let n = a.len() as int;\n reveal_with_fuel(scan_total, 2);\n if pos >= n {\n } else if a[pos] > q {\n lemma_scan_total_bound(k, q, a, pos + 1);\n assert((n - (pos + 1)) * (n - (pos + 1) + 1) / 2 <= (n - pos) * (n - pos + 1) / 2) by (nonlinear_arith)\n requires n - pos >= 1;\n } else {\n lemma_extend_good_end_gt_pos(q, a, pos);\n lemma_extend_good_end_le_len(q, a, pos);\n let end = extend_good_end(q, a, pos);\n let seg = end - pos;\n lemma_scan_total_bound(k, q, a, end);\n assert(triangle_contrib(seg, k) <= seg * (seg + 1) / 2) by {\n if seg >= k {\n assert((seg - k + 1) * (seg - k + 2) / 2 <= seg * (seg + 1) / 2) by (nonlinear_arith)\n requires seg >= k, k >= 1;\n }\n }\n assert(seg * (seg + 1) / 2 + (n - end) * (n - end + 1) / 2 <= (n - pos) * (n - pos + 1) / 2) by (nonlinear_arith)\n requires seg == end - pos, end > pos, end <= n, seg >= 1;\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn count_vacations(n: usize, k: usize, q: i64, a: Vec) -> (res: i64)\n requires\n 1 <= n && n <= 200000,\n 1 <= k && k <= n,\n a.len() == n,\n forall|i: int| 0 <= i && i < n ==> -1000000000 <= a@[i] && a@[i] <= 1000000000,\n -1000000000 <= q && q <= 1000000000,\n ensures\n res as int == scan_total(k as int, q, a@, 0),\n {\n proof {\n lemma_scan_total_nonneg(k as int, q, a@, 0);\n lemma_scan_total_bound(k as int, q, a@, 0);\n }\n let mut pos: usize = 0;\n let mut total: i64 = 0;\n #[verifier::loop_isolation(false)]\n while pos < n\n invariant\n 0 <= pos <= n,\n n == a.len(),\n n <= 200000,\n 1 <= k <= n,\n total as int + scan_total(k as int, q, a@, pos as int) == scan_total(k as int, q, a@, 0),\n 0 <= total,\n scan_total(k as int, q, a@, 0) <= (n as int) * (n as int + 1) / 2,\n forall|i: int| 0 <= i && i < n ==> -1000000000 <= a@[i] && a@[i] <= 1000000000,\n decreases n - pos,\n {\n if a[pos] > q {\n proof {\n assert(pos < n);\n assert((a@)[pos as int] > q);\n reveal_with_fuel(scan_total, 2);\n assert(scan_total(k as int, q, a@, pos as int) == scan_total(k as int, q, a@, (pos + 1) as int));\n }\n pos += 1;\n } else {\n let start = pos;\n proof {\n assert((a@)[start as int] <= q);\n assert(start < n);\n lemma_extend_good_end_le_len(q, a@, start as int);\n }\n #[verifier::loop_isolation(false)]\n while pos < n && a[pos] <= q\n invariant\n start <= pos <= n,\n n == a.len(),\n n <= 200000,\n 1 <= k <= n,\n forall|j: int|\n start as int <= j && j < pos as int ==> (a@)[j] <= q,\n extend_good_end(q, a@, start as int) == extend_good_end(q, a@, pos as int),\n total as int + scan_total(k as int, q, a@, start as int) == scan_total(k as int, q, a@, 0),\n 0 <= total,\n scan_total(k as int, q, a@, 0) <= (n as int) * (n as int + 1) / 2,\n forall|i: int| 0 <= i && i < n ==> -1000000000 <= a@[i] && a@[i] <= 1000000000,\n decreases n - pos,\n {\n proof {\n assert(pos < n);\n assert((a@)[pos as int] <= q);\n reveal_with_fuel(extend_good_end, 2);\n assert(extend_good_end(q, a@, pos as int) == extend_good_end(q, a@, (pos + 1) as int));\n }\n pos += 1;\n }\n let seg_len = pos - start;\n proof {\n reveal_with_fuel(extend_good_end, 2);\n assert(extend_good_end(q, a@, pos as int) == pos as int);\n assert(extend_good_end(q, a@, start as int) == pos as int);\n reveal_with_fuel(scan_total, 2);\n lemma_extend_good_end_gt_pos(q, a@, start as int);\n lemma_extend_good_end_le_len(q, a@, start as int);\n assert(\n scan_total(k as int, q, a@, start as int)\n == triangle_contrib(pos as int - start as int, k as int)\n + scan_total(k as int, q, a@, pos as int)\n );\n lemma_scan_total_nonneg(k as int, q, a@, pos as int);\n }\n if seg_len >= k {\n proof {\n assert(seg_len as int <= n as int);\n assert(seg_len as int >= k as int);\n assert(total as int + triangle_contrib(pos as int - start as int, k as int)\n + scan_total(k as int, q, a@, pos as int)\n == scan_total(k as int, q, a@, 0));\n assert(scan_total(k as int, q, a@, pos as int) >= 0);\n assert(total as int + triangle_contrib(pos as int - start as int, k as int)\n <= scan_total(k as int, q, a@, 0));\n assert(scan_total(k as int, q, a@, 0) <= (n as int) * (n as int + 1) / 2);\n assert((n as int) * (n as int + 1) / 2 <= 200000int * 200001 / 2) by (nonlinear_arith)\n requires n as int <= 200000;\n }\n let x = (seg_len - k) as i64 + 1;\n proof {\n assert(x as int == seg_len as int - k as int + 1);\n assert(1 <= x as int <= n as int);\n assert(x as int <= 200000);\n assert(x as int * (x as int + 1) <= 200000int * 200001) by (nonlinear_arith)\n requires 1 <= x as int, x as int <= 200000;\n assert(200000int * 200001 < 9223372036854775807int);\n assert(triangle_contrib(seg_len as int, k as int) == x as int * (x as int + 1) / 2);\n assert(total as int + x as int * (x as int + 1) / 2\n <= scan_total(k as int, q, a@, 0));\n assert(total as int + x as int * (x as int + 1) / 2\n <= 200000int * 200001 / 2);\n assert(200000int * 200001 / 2 < 9223372036854775807int);\n }\n total += x * (x + 1) / 2;\n }\n proof {\n assert(\n total as int + scan_total(k as int, q, a@, pos as int)\n == scan_total(k as int, q, a@, 0)\n );\n }\n }\n }\n proof {\n assert(pos == n);\n reveal_with_fuel(scan_total, 2);\n assert(scan_total(k as int, q, a@, pos as int) == 0);\n assert(total as int == scan_total(k as int, q, a@, 0));\n }\n total\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_vacations(n: usize, k: usize, q: i64, a: Vec) -> i64 {\n let mut pos: usize = 0;\n let mut total: i64 = 0;\n while pos < n {\n if a[pos] > q {\n pos += 1;\n } else {\n let start = pos;\n while pos < n && a[pos] <= q {\n pos += 1;\n }\n let seg_len = pos - start;\n if seg_len >= k {\n let x = (seg_len - k) as i64 + 1;\n total += x * (x + 1) / 2;\n }\n }\n }\n total\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(t_str)) = lines.next() {\n if let Ok(t) = t_str.trim().parse::() {\n for _ in 0..t {\n let nkq = lines.next().unwrap().unwrap();\n let mut nkq_it = nkq.split_whitespace();\n let n: usize = nkq_it.next().unwrap().parse().unwrap();\n let k: usize = nkq_it.next().unwrap().parse().unwrap();\n let q: i64 = nkq_it.next().unwrap().parse().unwrap();\n let a_line = lines.next().unwrap().unwrap();\n let a: Vec = a_line\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::count_vacations(n, k, q, a);\n println!(\"{}\", ans);\n }\n }\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1840C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1840C/tests/mutated_testcases.jsonl"} {"problem": "cf1873B", "family": "codeforces", "description": "# Good Kid\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nSlavic is preparing a present for a friend's birthday. He has an array $$$a$$$ of $$$n$$$ digits and the present will be the product of all these digits. Because Slavic is a good kid who wants to make the biggest product possible, he wants to add $$$1$$$ to exactly one of his digits. \n\nWhat is the maximum product Slavic can make?\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 9$$$)\u00a0\u2014 the number of digits.\n\nThe second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$0 \\leq a_i \\leq 9$$$)\u00a0\u2014 the digits in the array.\n\n## Output\n\nFor each test case, output a single integer\u00a0\u2014 the maximum product Slavic can make, by adding $$$1$$$ to exactly one of his digits.\n\n## Example\n\n**Input:**\n```\n4\n4\n2 2 1 2\n3\n0 1 2\n5\n4 3 2 3 4\n9\n9 9 9 9 9 9 9 9 9\n```\n**Output:**\n```\n16\n2\n432\n430467210\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_product_one_increment(a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let v: i64 = it.next().unwrap().parse().unwrap();\n a.push(v);\n i = i + 1;\n }\n let ans = Solution::max_product_one_increment(a);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n```\n", "tags": "Rating: 800\nbrute force, greedy, math\nSolved by: 61795 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_factor_at(a: Seq, bump: int, k: int) -> int\n recommends\n 0 <= k && k < a.len(),\n{\n if k == bump {\n a[k] as int + 1\n } else {\n a[k] as int\n }\n}\n\npub open spec fn spec_prefix_with_bump(a: Seq, bump: int, until: int) -> int\n recommends\n 0 <= until && until <= a.len() && a.len() <= 9,\n{\n if until == 0 {\n 1\n } else if until == 1 {\n spec_factor_at(a, bump, 0)\n } else if until == 2 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1)\n } else if until == 3 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n } else if until == 4 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3)\n } else if until == 5 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4)\n } else if until == 6 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n } else if until == 7 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6)\n } else if until == 8 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7)\n } else {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7) * spec_factor_at(a, bump, 8)\n }\n}\n\npub open spec fn spec_product_with_bump_at(a: Seq, bump: int) -> int\n recommends\n 0 <= bump && bump < a.len(),\n a.len() <= 9,\n{\n spec_prefix_with_bump(a, bump, a.len() as int)\n}\n\npub open spec fn spec_max2(x: int, y: int) -> int {\n if x > y {\n x\n } else {\n y\n }\n}\n\npub open spec fn spec_max_bumps_upto(a: Seq, end: int) -> int\n recommends\n 0 <= end && end <= a.len() && a.len() <= 9,\n{\n if end == 0 {\n 0\n } else if end == 1 {\n spec_product_with_bump_at(a, 0)\n } else if end == 2 {\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1))\n } else if end == 3 {\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n )\n } else if end == 4 {\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n )\n } else if end == 5 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n )\n } else if end == 6 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n )\n } else if end == 7 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n )\n } else if end == 8 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n )\n } else {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n ),\n spec_product_with_bump_at(a, 8),\n )\n }\n}\n\nimpl Solution {\n pub fn max_product_one_increment(a: Vec) -> (res: i64)\n requires\n 1 <= a.len() && a.len() <= 9,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n ensures\n res as int == spec_max_bumps_upto(a@, a.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_product_one_increment(a: Vec) -> i64 {\n let n = a.len();\n let mut best: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut p: i64 = 1;\n let mut j: usize = 0;\n while j < n {\n if j == i {\n p = p * (a[j] + 1);\n } else {\n p = p * a[j];\n }\n j = j + 1;\n }\n if p > best {\n best = p;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_factor_at(a: Seq, bump: int, k: int) -> int\n recommends\n 0 <= k && k < a.len(),\n{\n if k == bump {\n a[k] as int + 1\n } else {\n a[k] as int\n }\n}\n\npub open spec fn spec_prefix_with_bump(a: Seq, bump: int, until: int) -> int\n recommends\n 0 <= until && until <= a.len() && a.len() <= 9,\n{\n if until == 0 {\n 1\n } else if until == 1 {\n spec_factor_at(a, bump, 0)\n } else if until == 2 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1)\n } else if until == 3 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n } else if until == 4 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3)\n } else if until == 5 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4)\n } else if until == 6 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n } else if until == 7 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6)\n } else if until == 8 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7)\n } else {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7) * spec_factor_at(a, bump, 8)\n }\n}\n\npub open spec fn spec_product_with_bump_at(a: Seq, bump: int) -> int\n recommends\n 0 <= bump && bump < a.len(),\n a.len() <= 9,\n{\n spec_prefix_with_bump(a, bump, a.len() as int)\n}\n\npub open spec fn spec_max2(x: int, y: int) -> int {\n if x > y {\n x\n } else {\n y\n }\n}\n\npub open spec fn spec_max_bumps_upto(a: Seq, end: int) -> int\n recommends\n 0 <= end && end <= a.len() && a.len() <= 9,\n{\n if end == 0 {\n 0\n } else if end == 1 {\n spec_product_with_bump_at(a, 0)\n } else if end == 2 {\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1))\n } else if end == 3 {\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n )\n } else if end == 4 {\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n )\n } else if end == 5 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n )\n } else if end == 6 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n )\n } else if end == 7 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n )\n } else if end == 8 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n )\n } else {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n ),\n spec_product_with_bump_at(a, 8),\n )\n }\n}\n\nimpl Solution {\n pub fn max_product_one_increment(a: Vec) -> (res: i64)\n requires\n 1 <= a.len() && a.len() <= 9,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n ensures\n res as int == spec_max_bumps_upto(a@, a.len() as int),\n {\n let n = a.len();\n let mut best: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut p: i64 = 1;\n let mut j: usize = 0;\n while j < n {\n if j == i {\n p = p * (a[j] + 1);\n } else {\n p = p * a[j];\n }\n j = j + 1;\n }\n if p > best {\n best = p;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::arithmetic::mul::lemma_mul_upper_bound;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_factor_at(a: Seq, bump: int, k: int) -> int\n recommends\n 0 <= k && k < a.len(),\n{\n if k == bump {\n a[k] as int + 1\n } else {\n a[k] as int\n }\n}\n\npub open spec fn spec_prefix_with_bump(a: Seq, bump: int, until: int) -> int\n recommends\n 0 <= until && until <= a.len() && a.len() <= 9,\n{\n if until == 0 {\n 1\n } else if until == 1 {\n spec_factor_at(a, bump, 0)\n } else if until == 2 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1)\n } else if until == 3 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n } else if until == 4 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3)\n } else if until == 5 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4)\n } else if until == 6 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n } else if until == 7 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6)\n } else if until == 8 {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7)\n } else {\n spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7) * spec_factor_at(a, bump, 8)\n }\n}\n\npub open spec fn spec_product_with_bump_at(a: Seq, bump: int) -> int\n recommends\n 0 <= bump && bump < a.len(),\n a.len() <= 9,\n{\n spec_prefix_with_bump(a, bump, a.len() as int)\n}\n\npub open spec fn spec_max2(x: int, y: int) -> int {\n if x > y {\n x\n } else {\n y\n }\n}\n\npub open spec fn spec_max_bumps_upto(a: Seq, end: int) -> int\n recommends\n 0 <= end && end <= a.len() && a.len() <= 9,\n{\n if end == 0 {\n 0\n } else if end == 1 {\n spec_product_with_bump_at(a, 0)\n } else if end == 2 {\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1))\n } else if end == 3 {\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n )\n } else if end == 4 {\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n )\n } else if end == 5 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n )\n } else if end == 6 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n )\n } else if end == 7 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n )\n } else if end == 8 {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n )\n } else {\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n ),\n spec_product_with_bump_at(a, 8),\n )\n }\n}\n\nproof fn lemma_spec_prefix_le_1e10(\n a: Seq,\n bump: int,\n until: int,\n)\n requires\n 0 <= until && until <= a.len(),\n a.len() <= 9,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n ensures\n spec_prefix_with_bump(a, bump, until) <= 10_000_000_000,\n{\n if until == 0 {\n assert(spec_prefix_with_bump(a, bump, 0) == 1);\n } else if until == 1 {\n assert(spec_prefix_with_bump(a, bump, 1) == spec_factor_at(a, bump, 0));\n assert(spec_factor_at(a, bump, 0) <= 10);\n } else if until == 2 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n assert(spec_prefix_with_bump(a, bump, 2) == f0 * f1);\n assert(f0 <= 10 && f1 <= 10);\n lemma_mul_upper_bound(f0, 10, f1, 10);\n assert(f0 * f1 <= 10_000_000_000);\n } else if until == 3 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n assert(spec_prefix_with_bump(a, bump, 3) == f0 * f1 * f2);\n assert(f0 <= 10 && f1 <= 10 && f2 <= 10);\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n } else if until == 4 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n let f3 = spec_factor_at(a, bump, 3);\n assert(spec_prefix_with_bump(a, bump, 4) == f0 * f1 * f2 * f3);\n assert(f0 <= 10 && f1 <= 10 && f2 <= 10 && f3 <= 10);\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n lemma_mul_upper_bound(f0 * f1 * f2, 1000, f3, 10);\n } else if until == 5 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n let f3 = spec_factor_at(a, bump, 3);\n let f4 = spec_factor_at(a, bump, 4);\n assert(spec_prefix_with_bump(a, bump, 5) == f0 * f1 * f2 * f3 * f4);\n assert(f0 <= 10 && f1 <= 10 && f2 <= 10 && f3 <= 10 && f4 <= 10);\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n lemma_mul_upper_bound(f0 * f1 * f2, 1000, f3, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3, 10_000, f4, 10);\n } else if until == 6 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n let f3 = spec_factor_at(a, bump, 3);\n let f4 = spec_factor_at(a, bump, 4);\n let f5 = spec_factor_at(a, bump, 5);\n assert(spec_prefix_with_bump(a, bump, 6) == f0 * f1 * f2 * f3 * f4 * f5);\n assert(f0 <= 10 && f1 <= 10 && f2 <= 10 && f3 <= 10 && f4 <= 10 && f5 <= 10);\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n lemma_mul_upper_bound(f0 * f1 * f2, 1000, f3, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3, 10_000, f4, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4, 100_000, f5, 10);\n } else if until == 7 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n let f3 = spec_factor_at(a, bump, 3);\n let f4 = spec_factor_at(a, bump, 4);\n let f5 = spec_factor_at(a, bump, 5);\n let f6 = spec_factor_at(a, bump, 6);\n assert(spec_prefix_with_bump(a, bump, 7) == f0 * f1 * f2 * f3 * f4 * f5 * f6);\n assert(f0 <= 10 && f1 <= 10 && f2 <= 10 && f3 <= 10 && f4 <= 10 && f5 <= 10 && f6 <= 10);\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n lemma_mul_upper_bound(f0 * f1 * f2, 1000, f3, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3, 10_000, f4, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4, 100_000, f5, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4 * f5, 1_000_000, f6, 10);\n } else if until == 8 {\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n let f3 = spec_factor_at(a, bump, 3);\n let f4 = spec_factor_at(a, bump, 4);\n let f5 = spec_factor_at(a, bump, 5);\n let f6 = spec_factor_at(a, bump, 6);\n let f7 = spec_factor_at(a, bump, 7);\n assert(spec_prefix_with_bump(a, bump, 8) == f0 * f1 * f2 * f3 * f4 * f5 * f6 * f7);\n assert(\n f0 <= 10 && f1 <= 10 && f2 <= 10 && f3 <= 10 && f4 <= 10 && f5 <= 10 && f6 <= 10 && f7 <= 10\n );\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n lemma_mul_upper_bound(f0 * f1 * f2, 1000, f3, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3, 10_000, f4, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4, 100_000, f5, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4 * f5, 1_000_000, f6, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4 * f5 * f6, 10_000_000, f7, 10);\n } else {\n assert(until == 9);\n let f0 = spec_factor_at(a, bump, 0);\n let f1 = spec_factor_at(a, bump, 1);\n let f2 = spec_factor_at(a, bump, 2);\n let f3 = spec_factor_at(a, bump, 3);\n let f4 = spec_factor_at(a, bump, 4);\n let f5 = spec_factor_at(a, bump, 5);\n let f6 = spec_factor_at(a, bump, 6);\n let f7 = spec_factor_at(a, bump, 7);\n let f8 = spec_factor_at(a, bump, 8);\n assert(spec_prefix_with_bump(a, bump, 9) == f0 * f1 * f2 * f3 * f4 * f5 * f6 * f7 * f8);\n assert(\n f0 <= 10 && f1 <= 10 && f2 <= 10 && f3 <= 10 && f4 <= 10 && f5 <= 10 && f6 <= 10 && f7 <= 10\n && f8 <= 10\n );\n lemma_mul_upper_bound(f0, 10, f1, 10);\n lemma_mul_upper_bound(f0 * f1, 100, f2, 10);\n lemma_mul_upper_bound(f0 * f1 * f2, 1000, f3, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3, 10_000, f4, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4, 100_000, f5, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4 * f5, 1_000_000, f6, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4 * f5 * f6, 10_000_000, f7, 10);\n lemma_mul_upper_bound(f0 * f1 * f2 * f3 * f4 * f5 * f6 * f7, 100_000_000, f8, 10);\n }\n}\n\nproof fn lemma_spec_max_bumps_upto_inc(\n a: Seq,\n i: int,\n)\n requires\n 1 <= a.len() && a.len() <= 9,\n 0 <= i && i < a.len(),\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n ensures\n spec_max_bumps_upto(a, i + 1)\n == spec_max2(spec_max_bumps_upto(a, i), spec_product_with_bump_at(a, i)),\n{\n if i == 0 {\n assert(spec_max_bumps_upto(a, 0) == 0);\n assert(spec_max_bumps_upto(a, 1) == spec_product_with_bump_at(a, 0));\n assert(\n spec_max2(spec_max_bumps_upto(a, 0), spec_product_with_bump_at(a, 0))\n == spec_product_with_bump_at(a, 0)\n );\n } else if i == 1 {\n assert(\n spec_max_bumps_upto(a, 2)\n == spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1))\n );\n assert(spec_max_bumps_upto(a, 1) == spec_product_with_bump_at(a, 0));\n assert(\n spec_max2(spec_max_bumps_upto(a, 1), spec_product_with_bump_at(a, 1))\n == spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1))\n );\n } else if i == 2 {\n assert(\n spec_max_bumps_upto(a, 3)\n == spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n )\n );\n assert(\n spec_max_bumps_upto(a, 2)\n == spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1))\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 2), spec_product_with_bump_at(a, 2))\n == spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n )\n );\n } else if i == 3 {\n assert(\n spec_max_bumps_upto(a, 4)\n == spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n )\n );\n assert(\n spec_max_bumps_upto(a, 3)\n == spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n )\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 3), spec_product_with_bump_at(a, 3))\n == spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n )\n );\n } else if i == 4 {\n assert(\n spec_max_bumps_upto(a, 5)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n )\n );\n assert(\n spec_max_bumps_upto(a, 4)\n == spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n )\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 4), spec_product_with_bump_at(a, 4))\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n )\n );\n } else if i == 5 {\n assert(\n spec_max_bumps_upto(a, 6)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n )\n );\n assert(\n spec_max_bumps_upto(a, 5)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n )\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 5), spec_product_with_bump_at(a, 5))\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n )\n );\n } else if i == 6 {\n assert(\n spec_max_bumps_upto(a, 7)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n )\n );\n assert(\n spec_max_bumps_upto(a, 6)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n )\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 6), spec_product_with_bump_at(a, 6))\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n )\n );\n } else if i == 7 {\n assert(\n spec_max_bumps_upto(a, 8)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n )\n );\n assert(\n spec_max_bumps_upto(a, 7)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n )\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 7), spec_product_with_bump_at(a, 7))\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n )\n );\n } else {\n assert(i == 8);\n assert(\n spec_max_bumps_upto(a, 9)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n ),\n spec_product_with_bump_at(a, 8),\n )\n );\n assert(\n spec_max_bumps_upto(a, 8)\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n )\n );\n assert(\n spec_max2(spec_max_bumps_upto(a, 8), spec_product_with_bump_at(a, 8))\n == spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(\n spec_max2(spec_product_with_bump_at(a, 0), spec_product_with_bump_at(a, 1)),\n spec_product_with_bump_at(a, 2),\n ),\n spec_product_with_bump_at(a, 3),\n ),\n spec_product_with_bump_at(a, 4),\n ),\n spec_product_with_bump_at(a, 5),\n ),\n spec_product_with_bump_at(a, 6),\n ),\n spec_product_with_bump_at(a, 7),\n ),\n spec_product_with_bump_at(a, 8),\n )\n );\n }\n}\n\nproof fn lemma_prefix_step_j(\n a: Seq,\n bump: int,\n j: int,\n)\n requires\n 0 <= j && j < a.len(),\n a.len() <= 9,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n ensures\n spec_prefix_with_bump(a, bump, j + 1)\n == spec_prefix_with_bump(a, bump, j) * spec_factor_at(a, bump, j),\n{\n if j == 0 {\n assert(spec_prefix_with_bump(a, bump, 0) == 1);\n assert(spec_factor_at(a, bump, 0) == spec_prefix_with_bump(a, bump, 1));\n } else if j == 1 {\n assert(spec_prefix_with_bump(a, bump, 1) == spec_factor_at(a, bump, 0));\n assert(\n spec_prefix_with_bump(a, bump, 1) * spec_factor_at(a, bump, 1)\n == spec_prefix_with_bump(a, bump, 2)\n );\n } else if j == 2 {\n assert(\n spec_prefix_with_bump(a, bump, 2)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1)\n );\n assert(\n spec_prefix_with_bump(a, bump, 2) * spec_factor_at(a, bump, 2)\n == spec_prefix_with_bump(a, bump, 3)\n );\n } else if j == 3 {\n assert(\n spec_prefix_with_bump(a, bump, 3)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n );\n assert(\n spec_prefix_with_bump(a, bump, 3) * spec_factor_at(a, bump, 3)\n == spec_prefix_with_bump(a, bump, 4)\n );\n } else if j == 4 {\n assert(\n spec_prefix_with_bump(a, bump, 4)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3)\n );\n assert(\n spec_prefix_with_bump(a, bump, 4) * spec_factor_at(a, bump, 4)\n == spec_prefix_with_bump(a, bump, 5)\n );\n } else if j == 5 {\n assert(\n spec_prefix_with_bump(a, bump, 5)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4)\n );\n assert(\n spec_prefix_with_bump(a, bump, 5) * spec_factor_at(a, bump, 5)\n == spec_prefix_with_bump(a, bump, 6)\n );\n } else if j == 6 {\n assert(\n spec_prefix_with_bump(a, bump, 6)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n );\n assert(\n spec_prefix_with_bump(a, bump, 6) * spec_factor_at(a, bump, 6)\n == spec_prefix_with_bump(a, bump, 7)\n );\n } else if j == 7 {\n assert(\n spec_prefix_with_bump(a, bump, 7)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6)\n );\n assert(\n spec_prefix_with_bump(a, bump, 7) * spec_factor_at(a, bump, 7)\n == spec_prefix_with_bump(a, bump, 8)\n );\n } else {\n assert(j == 8);\n assert(\n spec_prefix_with_bump(a, bump, 8)\n == spec_factor_at(a, bump, 0) * spec_factor_at(a, bump, 1) * spec_factor_at(a, bump, 2)\n * spec_factor_at(a, bump, 3) * spec_factor_at(a, bump, 4) * spec_factor_at(a, bump, 5)\n * spec_factor_at(a, bump, 6) * spec_factor_at(a, bump, 7)\n );\n assert(\n spec_prefix_with_bump(a, bump, 8) * spec_factor_at(a, bump, 8)\n == spec_prefix_with_bump(a, bump, 9)\n );\n }\n assert(spec_prefix_with_bump(a, bump, j + 1)\n == spec_prefix_with_bump(a, bump, j) * spec_factor_at(a, bump, j));\n}\n\nimpl Solution {\n pub fn max_product_one_increment(a: Vec) -> (res: i64)\n requires\n 1 <= a.len() && a.len() <= 9,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n ensures\n res as int == spec_max_bumps_upto(a@, a.len() as int),\n {\n let n = a.len();\n let mut best: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == a.len(),\n 1 <= n && n <= 9,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n i <= n,\n (best as int) == spec_max_bumps_upto(a@, i as int),\n best <= 10_000_000_000,\n decreases n - i,\n {\n let mut p: i64 = 1;\n let mut j: usize = 0;\n while j < n\n invariant\n n == a.len(),\n a.len() <= 9,\n i < n,\n j <= n,\n forall|k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] && a[k] <= 9,\n (p as int) == spec_prefix_with_bump(a@, i as int, j as int),\n p >= 0,\n p <= 10_000_000_000,\n decreases n - j,\n {\n proof {\n lemma_prefix_step_j(a@, i as int, j as int);\n lemma_spec_prefix_le_1e10(a@, i as int, j as int);\n assert((p as int) <= 10_000_000_000);\n assert((a@[j as int] as int) <= 9);\n assert((a@[j as int] + 1) as int <= 10);\n assert(spec_factor_at(a@, i as int, j as int) <= 10);\n lemma_mul_upper_bound(\n (p as int),\n 10_000_000_000,\n spec_factor_at(a@, i as int, j as int),\n 10,\n );\n }\n if j == i {\n p = p * (a[j] + 1);\n } else {\n p = p * a[j];\n }\n proof {\n assert((p as int) == spec_prefix_with_bump(a@, i as int, j as int + 1));\n }\n j = j + 1;\n proof {\n assert((p as int) == spec_prefix_with_bump(a@, i as int, j as int));\n lemma_spec_prefix_le_1e10(a@, i as int, j as int);\n }\n }\n proof {\n assert((p as int) == spec_product_with_bump_at(a@, i as int));\n lemma_spec_max_bumps_upto_inc(a@, i as int);\n }\n let ghost old_best = best;\n if p > best {\n best = p;\n }\n proof {\n assert((best as int) == spec_max2(old_best as int, (p as int)));\n assert((old_best as int) == spec_max_bumps_upto(a@, i as int));\n assert((best as int) == spec_max_bumps_upto(a@, (i + 1) as int));\n }\n i = i + 1;\n }\n proof {\n assert((best as int) == spec_max_bumps_upto(a@, n as int));\n }\n best\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_product_one_increment(a: Vec) -> i64 {\n let n = a.len();\n let mut best: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut p: i64 = 1;\n let mut j: usize = 0;\n while j < n {\n if j == i {\n p = p * (a[j] + 1);\n } else {\n p = p * a[j];\n }\n j = j + 1;\n }\n if p > best {\n best = p;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let v: i64 = it.next().unwrap().parse().unwrap();\n a.push(v);\n i = i + 1;\n }\n let ans = Solution::max_product_one_increment(a);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1873B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1873B/tests/mutated_testcases.jsonl"} {"problem": "cf1901A", "family": "codeforces", "description": "# Line Trip\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nThere is a road, which can be represented as a number line. You are located in the point $$$0$$$ of the number line, and you want to travel from the point $$$0$$$ to the point $$$x$$$, and back to the point $$$0$$$.\n\nYou travel by car, which spends $$$1$$$ liter of gasoline per $$$1$$$ unit of distance travelled. When you start at the point $$$0$$$, your car is fully fueled (its gas tank contains the maximum possible amount of fuel).\n\nThere are $$$n$$$ gas stations, located in points $$$a_1, a_2, \\dots, a_n$$$. When you arrive at a gas station, you fully refuel your car. **Note that you can refuel only at gas stations, and there are no gas stations in points $$$0$$$ and $$$x$$$**.\n\nYou have to calculate the minimum possible volume of the gas tank in your car (in liters) that will allow you to travel from the point $$$0$$$ to the point $$$x$$$ and back to the point $$$0$$$.\n\n## Input\n\nThe first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) \u2014 the number of test cases.\n\nEach test case consists of two lines:\n - the first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 50$$$; $$$2 \\le x \\le 100$$$); \n- the second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\lt a_1 \\lt a_2 \\lt \\dots \\lt a_n \\lt x$$$).\n\n## Output\n\nFor each test case, print one integer \u2014 the minimum possible volume of the gas tank in your car that will allow you to travel from the point $$$0$$$ to the point $$$x$$$ and back.\n\n## Example\n\n**Input:**\n```\n3\n3 7\n1 2 5\n3 6\n1 2 5\n1 10\n7\n```\n**Output:**\n```\n4\n3\n7\n```\n\n## Note\n\nIn the first test case of the example, if the car has a gas tank of $$$4$$$ liters, you can travel to $$$x$$$ and back as follows:\n - travel to the point $$$1$$$, then your car's gas tank contains $$$3$$$ liters of fuel; \n- refuel at the point $$$1$$$, then your car's gas tank contains $$$4$$$ liters of fuel; \n- travel to the point $$$2$$$, then your car's gas tank contains $$$3$$$ liters of fuel; \n- refuel at the point $$$2$$$, then your car's gas tank contains $$$4$$$ liters of fuel; \n- travel to the point $$$5$$$, then your car's gas tank contains $$$1$$$ liter of fuel; \n- refuel at the point $$$5$$$, then your car's gas tank contains $$$4$$$ liters of fuel; \n- travel to the point $$$7$$$, then your car's gas tank contains $$$2$$$ liters of fuel; \n- travel to the point $$$5$$$, then your car's gas tank contains $$$0$$$ liters of fuel; \n- refuel at the point $$$5$$$, then your car's gas tank contains $$$4$$$ liters of fuel; \n- travel to the point $$$2$$$, then your car's gas tank contains $$$1$$$ liter of fuel; \n- refuel at the point $$$2$$$, then your car's gas tank contains $$$4$$$ liters of fuel; \n- travel to the point $$$1$$$, then your car's gas tank contains $$$3$$$ liters of fuel; \n- refuel at the point $$$1$$$, then your car's gas tank contains $$$4$$$ liters of fuel; \n- travel to the point $$$0$$$, then your car's gas tank contains $$$3$$$ liters of fuel.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_tank_liters(x: i64, a: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let x: i64 = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let v: i64 = it.next().unwrap().parse().unwrap();\n a.push(v);\n i = i + 1;\n }\n let ans = Solution::min_tank_liters(x, a);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n```\n", "tags": "Rating: 800\ngreedy, math\nSolved by: 73228 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_i2max(a: int, b: int) -> int {\n if a >= b {\n a\n } else {\n b\n }\n}\n\npub open spec fn spec_prefix_gaps_up(s: Seq, k: int) -> int\n recommends\n s.len() >= 2,\n -1 <= k <= s.len() - 2,\n decreases k + 1,\n{\n if k < 0 {\n 0int\n } else {\n spec_i2max(\n spec_prefix_gaps_up(s, k - 1),\n (s[k + 1] as int) - (s[k] as int),\n )\n }\n}\n\npub open spec fn spec_min_tank_liters(s: Seq, x: int) -> int\n recommends\n s.len() >= 1,\n (s[s.len() - 1] as int) < x,\n{\n if s.len() == 1 {\n spec_i2max(s[0] as int, 2 * (x - (s[0] as int)))\n } else {\n let last_st = (s.len() as int) - 1;\n let hi = last_st - 1;\n spec_i2max(\n spec_i2max(s[0] as int, spec_prefix_gaps_up(s, hi)),\n 2 * (x - (s[last_st] as int)),\n )\n }\n}\n\nimpl Solution {\n pub fn min_tank_liters(x: i64, a: Vec) -> (res: i64)\n requires\n 1 <= a.len() <= 50,\n 2 <= x <= 100,\n forall|j: int|\n 0 <= j < a.len() as int - 1 ==> (#[trigger] a[j] as int) < (a[j + 1] as int),\n forall|j: int|\n 0 <= j < a.len() as int ==> 0 < #[trigger] a[j] as int && (a[j] as int) < x as int,\n ensures\n res as int == spec_min_tank_liters(a@, x as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_tank_liters(x: i64, a: Vec) -> i64 {\n let n = a.len();\n let mut ans: i64 = a[0];\n let mut i: usize = 0;\n let bound = n - 1;\n while i < bound {\n let d: i64 = a[i + 1] - a[i];\n if d > ans {\n ans = d;\n }\n i = i + 1;\n }\n let d2: i64 = 2 * (x - a[n - 1]);\n if d2 > ans {\n ans = d2;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_i2max(a: int, b: int) -> int {\n if a >= b {\n a\n } else {\n b\n }\n}\n\npub open spec fn spec_prefix_gaps_up(s: Seq, k: int) -> int\n recommends\n s.len() >= 2,\n -1 <= k <= s.len() - 2,\n decreases k + 1,\n{\n if k < 0 {\n 0int\n } else {\n spec_i2max(\n spec_prefix_gaps_up(s, k - 1),\n (s[k + 1] as int) - (s[k] as int),\n )\n }\n}\n\npub open spec fn spec_min_tank_liters(s: Seq, x: int) -> int\n recommends\n s.len() >= 1,\n (s[s.len() - 1] as int) < x,\n{\n if s.len() == 1 {\n spec_i2max(s[0] as int, 2 * (x - (s[0] as int)))\n } else {\n let last_st = (s.len() as int) - 1;\n let hi = last_st - 1;\n spec_i2max(\n spec_i2max(s[0] as int, spec_prefix_gaps_up(s, hi)),\n 2 * (x - (s[last_st] as int)),\n )\n }\n}\n\nimpl Solution {\n pub fn min_tank_liters(x: i64, a: Vec) -> (res: i64)\n requires\n 1 <= a.len() <= 50,\n 2 <= x <= 100,\n forall|j: int|\n 0 <= j < a.len() as int - 1 ==> (#[trigger] a[j] as int) < (a[j + 1] as int),\n forall|j: int|\n 0 <= j < a.len() as int ==> 0 < #[trigger] a[j] as int && (a[j] as int) < x as int,\n ensures\n res as int == spec_min_tank_liters(a@, x as int),\n {\n let n = a.len();\n let mut ans: i64 = a[0];\n let mut i: usize = 0;\n let bound = n - 1;\n while i < bound {\n let d: i64 = a[i + 1] - a[i];\n if d > ans {\n ans = d;\n }\n i = i + 1;\n }\n let d2: i64 = 2 * (x - a[n - 1]);\n if d2 > ans {\n ans = d2;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_i2max(a: int, b: int) -> int {\n if a >= b {\n a\n } else {\n b\n }\n}\n\npub open spec fn spec_prefix_gaps_up(s: Seq, k: int) -> int\n recommends\n s.len() >= 2,\n -1 <= k <= s.len() - 2,\n decreases k + 1,\n{\n if k < 0 {\n 0int\n } else {\n spec_i2max(\n spec_prefix_gaps_up(s, k - 1),\n (s[k + 1] as int) - (s[k] as int),\n )\n }\n}\n\npub open spec fn spec_min_tank_liters(s: Seq, x: int) -> int\n recommends\n s.len() >= 1,\n (s[s.len() - 1] as int) < x,\n{\n if s.len() == 1 {\n spec_i2max(s[0] as int, 2 * (x - (s[0] as int)))\n } else {\n let last_st = (s.len() as int) - 1;\n let hi = last_st - 1;\n spec_i2max(\n spec_i2max(s[0] as int, spec_prefix_gaps_up(s, hi)),\n 2 * (x - (s[last_st] as int)),\n )\n }\n}\n\nproof fn lemma_prefix_unfold(s: Seq, k: int)\n requires\n s.len() >= 2,\n 0 <= k <= s.len() - 2,\n ensures\n spec_prefix_gaps_up(s, k) == spec_i2max(\n spec_prefix_gaps_up(s, k - 1),\n (s[k + 1] as int) - (s[k] as int),\n ),\n{\n}\n\nimpl Solution {\n pub fn min_tank_liters(x: i64, a: Vec) -> (res: i64)\n requires\n 1 <= a.len() <= 50,\n 2 <= x <= 100,\n forall|j: int|\n 0 <= j < a.len() as int - 1 ==> (#[trigger] a[j] as int) < (a[j + 1] as int),\n forall|j: int|\n 0 <= j < a.len() as int ==> 0 < #[trigger] a[j] as int && (a[j] as int) < x as int,\n ensures\n res as int == spec_min_tank_liters(a@, x as int),\n {\n let n = a.len();\n let mut ans: i64 = a[0];\n let mut i: usize = 0;\n let bound = n - 1;\n #[verifier::loop_isolation(false)]\n while i < bound\n invariant\n n == a.len(),\n n >= 1,\n bound == n - 1,\n i <= bound,\n forall|j: int|\n 0 <= j < a.len() as int - 1 ==> (#[trigger] a[j] as int) < (a[j + 1] as int),\n forall|j: int|\n 0 <= j < a.len() as int ==> 0 < #[trigger] a[j] as int && (a[j] as int) < x as int,\n n >= 2 ==> (ans as int == spec_i2max(\n a@[0] as int,\n spec_prefix_gaps_up(a@, (i as int) - 1int),\n )),\n n == 1 ==> ans as int == a@[0] as int,\n decreases bound - i,\n {\n proof {\n assert(n >= 2);\n assert(i < bound);\n assert(i + 1 < n);\n assert((i as int) <= (n as int) - 2);\n }\n let d: i64 = a[i + 1] - a[i];\n let ghost old_ans = ans;\n if d > ans {\n ans = d;\n }\n proof {\n lemma_prefix_unfold(a@, i as int);\n assert(d as int == (a@[(i + 1) as int] as int) - (a@[i as int] as int));\n assert(old_ans as int == spec_i2max(a@[0] as int, spec_prefix_gaps_up(a@, (i as int) - 1int)));\n if d > old_ans {\n assert(ans as int == d as int);\n } else {\n assert(ans as int == old_ans as int);\n assert(d as int <= old_ans as int);\n }\n assert(ans as int == spec_i2max(old_ans as int, d as int));\n assert(\n ans as int == spec_i2max(\n spec_i2max(a@[0] as int, spec_prefix_gaps_up(a@, (i as int) - 1int)),\n d as int\n )\n );\n assert(\n ans as int == spec_i2max(a@[0] as int, spec_prefix_gaps_up(a@, i as int))\n );\n }\n i = i + 1;\n }\n proof {\n if n >= 2 {\n assert(i == bound);\n assert((i as int) == ((n - 1) as int));\n let hi = (n as int) - 2;\n assert(\n ans as int == spec_i2max(a@[0] as int, spec_prefix_gaps_up(a@, hi))\n );\n } else {\n assert(n == 1);\n assert(i == 0);\n assert(ans as int == a@[0] as int);\n }\n }\n let d2: i64 = 2 * (x - a[n - 1]);\n let ghost mid = ans;\n if d2 > ans {\n ans = d2;\n }\n proof {\n if n == 1 {\n assert(spec_min_tank_liters(a@, x as int) == spec_i2max(\n a@[0] as int,\n 2 * ((x as int) - (a@[0] as int)),\n ));\n if d2 > mid {\n assert(ans as int == 2 * ((x as int) - (a@[0] as int)));\n } else {\n assert(ans as int == mid as int);\n assert(2 * ((x as int) - (a@[0] as int)) <= mid as int);\n }\n assert(ans as int == spec_i2max(a@[0] as int, 2 * ((x as int) - (a@[0] as int))));\n } else {\n let last_st = (n as int) - 1;\n assert(spec_min_tank_liters(a@, x as int) == spec_i2max(\n spec_i2max(a@[0] as int, spec_prefix_gaps_up(a@, last_st - 1)),\n 2 * ((x as int) - (a@[last_st] as int)),\n ));\n assert(mid as int == spec_i2max(a@[0] as int, spec_prefix_gaps_up(a@, last_st - 1)));\n if d2 > mid {\n assert(ans as int == 2 * ((x as int) - (a@[last_st] as int)));\n } else {\n assert(ans as int == mid as int);\n }\n assert(ans as int == spec_i2max(mid as int, 2 * ((x as int) - (a@[last_st] as int))));\n }\n assert(ans as int == spec_min_tank_liters(a@, x as int));\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_tank_liters(x: i64, a: Vec) -> i64 {\n let n = a.len();\n let mut ans: i64 = a[0];\n let mut i: usize = 0;\n let bound = n - 1;\n while i < bound {\n let d: i64 = a[i + 1] - a[i];\n if d > ans {\n ans = d;\n }\n i = i + 1;\n }\n let d2: i64 = 2 * (x - a[n - 1]);\n if d2 > ans {\n ans = d2;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let x: i64 = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let v: i64 = it.next().unwrap().parse().unwrap();\n a.push(v);\n i = i + 1;\n }\n let ans = Solution::min_tank_liters(x, a);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1901A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1901A/tests/mutated_testcases.jsonl"} {"problem": "cf1902B", "family": "codeforces", "description": "# Getting Points\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nMonocarp is a student at Berland State University. Due to recent changes in the Berland education system, Monocarp has to study only one subject\u00a0\u2014 programming.\n\nThe academic term consists of $$$n$$$ days, and in order not to get expelled, Monocarp has to earn at least $$$P$$$ points during those $$$n$$$ days. There are two ways to earn points\u00a0\u2014 completing practical tasks and attending lessons. For each practical task Monocarp fulfills, he earns $$$t$$$ points, and for each lesson he attends, he earns $$$l$$$ points.\n\nPractical tasks are unlocked \"each week\" as the term goes on: the first task is unlocked on day $$$1$$$ (and can be completed on any day from $$$1$$$ to $$$n$$$), the second task is unlocked on day $$$8$$$ (and can be completed on any day from $$$8$$$ to $$$n$$$), the third task is unlocked on day $$$15$$$, and so on.\n\nEvery day from $$$1$$$ to $$$n$$$, there is a lesson which can be attended by Monocarp. And every day, Monocarp chooses whether to study or to rest the whole day. When Monocarp decides to study, he attends a lesson and can complete **no more than $$$2$$$** tasks, which are already unlocked and not completed yet. If Monocarp rests the whole day, he skips a lesson and ignores tasks.\n\nMonocarp wants to have as many days off as possible, i.\u00a0e. he wants to maximize the number of days he rests. Help him calculate the maximum number of days he can rest!\n\n## Input\n\nThe first line contains a single integer $$$tc$$$ ($$$1 \\le tc \\le 10^4$$$)\u00a0\u2014 the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains four integers $$$n$$$, $$$P$$$, $$$l$$$ and $$$t$$$ ($$$1 \\le n, l, t \\le 10^9$$$; $$$1 \\le P \\le 10^{18}$$$)\u00a0\u2014 the number of days, the minimum total points Monocarp has to earn, the points for attending one lesson and points for completing one task.\n\nIt's guaranteed for each test case that it's possible not to be expelled if Monocarp will attend all lessons and will complete all tasks.\n\n## Output\n\nFor each test, print one integer\u00a0\u2014 the maximum number of days Monocarp can rest without being expelled from University.\n\n## Example\n\n**Input:**\n```\n5\n1 5 5 2\n14 3000000000 1000000000 500000000\n100 20 1 10\n8 120 10 20\n42 280 13 37\n```\n**Output:**\n```\n0\n12\n99\n0\n37\n```\n\n## Note\n\nIn the first test case, the term lasts for $$$1$$$ day, so Monocarp should attend at day $$$1$$$. Since attending one lesson already gives $$$5$$$ points ($$$5 \\ge P$$$), so it doesn't matter, will Monocarp complete the task or not.\n\nIn the second test case, Monocarp can, for example, study at days $$$8$$$ and $$$9$$$: at day $$$8$$$ he will attend a lesson for $$$10^9$$$ points and complete two tasks for another $$$5 \\cdot 10^8 + 5 \\cdot 10^8$$$ points. And at day $$$9$$$ he only attends a lesson for another $$$10^9$$$ points.\n\nIn the third test case, Monocarp can, for example, study at day $$$42$$$: attending a lesson gives him $$$1$$$ point and solving $$$2$$$ out of $$$6$$$ available tasks gives him another $$$2 \\cdot 10$$$ points.\n\nIn the fourth test case, Monocarp has to attend all lessons and complete all tasks to get $$$8 \\cdot 10 + 2 \\cdot 20 = 120$$$ points.\n\nIn the fifth test case, Monocarp can, for example, study at days: $$$8$$$\u00a0\u2014 one lesson and first and second tasks; $$$15$$$\u00a0\u2014 one lesson and the third task; $$$22$$$\u00a0\u2014 one lesson and the fourth task; $$$29$$$\u00a0\u2014 one lesson and the fifth task; $$$36$$$\u00a0\u2014 one lesson and the sixth task.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_rest_days(n: i64, p: i64, l: i64, t: i64) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n\n let q: usize = it.next().expect(\"q\").parse().expect(\"valid q\");\n let mut i: usize = 0;\n while i < q {\n let n: i64 = it.next().expect(\"n\").parse().expect(\"valid n\");\n let p: i64 = it.next().expect(\"p\").parse().expect(\"valid p\");\n let l: i64 = it.next().expect(\"l\").parse().expect(\"valid l\");\n let t: i64 = it.next().expect(\"t\").parse().expect(\"valid t\");\n\n let ans = Solution::max_rest_days(n, p, l, t);\n println!(\"{}\", ans);\n i = i + 1;\n }\n}\n```\n", "tags": "1100\nbinary search brute force greedy\n16314\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ceil_div_pos(a: int, b: int) -> int\n recommends\n a >= 0,\n b > 0,\n {\n if a % b == 0 {\n a / b\n } else {\n a / b + 1\n }\n }\n\n pub open spec fn max_rest_days_spec(n: int, p: int, l: int, t: int) -> int\n recommends\n n >= 1,\n p >= 1,\n l >= 1,\n t >= 1,\n {\n let tasks = (n + 6) / 7;\n let pairs = tasks / 2;\n let odd = tasks % 2;\n let pair_pts = l + 2 * t;\n let pair_total = pairs * pair_pts;\n let study_days = if p <= pair_total {\n Self::ceil_div_pos(p, pair_pts)\n } else {\n let rem = p - pair_total;\n if odd == 1 {\n let one_day_pts = l + t;\n if rem <= one_day_pts {\n pairs + 1\n } else {\n pairs + 1 + Self::ceil_div_pos(rem - one_day_pts, l)\n }\n } else {\n pairs + Self::ceil_div_pos(rem, l)\n }\n };\n n - study_days\n }\n\n pub fn max_rest_days(n: i64, p: i64, l: i64, t: i64) -> (res: i64)\n requires\n 1 <= n <= 1_000_000_000,\n 1 <= p <= 1_000_000_000_000_000_000,\n 1 <= l <= 1_000_000_000,\n 1 <= t <= 1_000_000_000,\n ((n + 6) / 7 / 2) * (l + 2 * t) <= 9_000_000_000_000_000_000,\n p <= n * l + ((n + 6) / 7) * t,\n ensures\n res as int == Self::max_rest_days_spec(n as int, p as int, l as int, t as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_rest_days(n: i64, p: i64, l: i64, t: i64) -> i64 {\n let tasks: i64 = (n + 6) / 7;\n let pairs: i64 = tasks / 2;\n let odd: i64 = tasks % 2;\n let pair_pts: i64 = l + 2 * t;\n\n let pair_total: i64 = pairs * pair_pts;\n let mut study_days: i64;\n\n if p <= pair_total {\n if p % pair_pts == 0 {\n study_days = p / pair_pts;\n } else {\n study_days = p / pair_pts + 1;\n }\n } else {\n study_days = pairs;\n let mut rem: i64 = if p >= pair_total { p - pair_total } else { 0 };\n if odd == 1 {\n let one_day_pts: i64 = l + t;\n if rem <= one_day_pts {\n study_days = study_days + 1;\n rem = 0;\n } else {\n study_days = study_days + 1;\n rem = rem - one_day_pts;\n }\n }\n if rem > 0 {\n if rem % l == 0 {\n study_days = study_days + rem / l;\n } else {\n study_days = study_days + rem / l + 1;\n }\n }\n }\n\n n - study_days\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ceil_div_pos(a: int, b: int) -> int\n recommends\n a >= 0,\n b > 0,\n {\n if a % b == 0 {\n a / b\n } else {\n a / b + 1\n }\n }\n\n pub open spec fn max_rest_days_spec(n: int, p: int, l: int, t: int) -> int\n recommends\n n >= 1,\n p >= 1,\n l >= 1,\n t >= 1,\n {\n let tasks = (n + 6) / 7;\n let pairs = tasks / 2;\n let odd = tasks % 2;\n let pair_pts = l + 2 * t;\n let pair_total = pairs * pair_pts;\n let study_days = if p <= pair_total {\n Self::ceil_div_pos(p, pair_pts)\n } else {\n let rem = p - pair_total;\n if odd == 1 {\n let one_day_pts = l + t;\n if rem <= one_day_pts {\n pairs + 1\n } else {\n pairs + 1 + Self::ceil_div_pos(rem - one_day_pts, l)\n }\n } else {\n pairs + Self::ceil_div_pos(rem, l)\n }\n };\n n - study_days\n }\n\n pub fn max_rest_days(n: i64, p: i64, l: i64, t: i64) -> (res: i64)\n requires\n 1 <= n <= 1_000_000_000,\n 1 <= p <= 1_000_000_000_000_000_000,\n 1 <= l <= 1_000_000_000,\n 1 <= t <= 1_000_000_000,\n ((n + 6) / 7 / 2) * (l + 2 * t) <= 9_000_000_000_000_000_000,\n p <= n * l + ((n + 6) / 7) * t,\n ensures\n res as int == Self::max_rest_days_spec(n as int, p as int, l as int, t as int),\n {\n let tasks: i64 = (n + 6) / 7;\n let pairs: i64 = tasks / 2;\n let odd: i64 = tasks % 2;\n let pair_pts: i64 = l + 2 * t;\n\n let pair_total: i64 = pairs * pair_pts;\n let mut study_days: i64;\n\n if p <= pair_total {\n if p % pair_pts == 0 {\n study_days = p / pair_pts;\n } else {\n study_days = p / pair_pts + 1;\n }\n } else {\n study_days = pairs;\n let mut rem: i64 = if p >= pair_total { p - pair_total } else { 0 };\n if odd == 1 {\n let one_day_pts: i64 = l + t;\n if rem <= one_day_pts {\n study_days = study_days + 1;\n rem = 0;\n } else {\n study_days = study_days + 1;\n rem = rem - one_day_pts;\n }\n }\n if rem > 0 {\n if rem % l == 0 {\n study_days = study_days + rem / l;\n } else {\n study_days = study_days + rem / l + 1;\n }\n }\n }\n\n n - study_days\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ceil_div_pos(a: int, b: int) -> int\n recommends\n a >= 0,\n b > 0,\n {\n if a % b == 0 {\n a / b\n } else {\n a / b + 1\n }\n }\n\n pub open spec fn max_rest_days_spec(n: int, p: int, l: int, t: int) -> int\n recommends\n n >= 1,\n p >= 1,\n l >= 1,\n t >= 1,\n {\n let tasks = (n + 6) / 7;\n let pairs = tasks / 2;\n let odd = tasks % 2;\n let pair_pts = l + 2 * t;\n let pair_total = pairs * pair_pts;\n let study_days = if p <= pair_total {\n Self::ceil_div_pos(p, pair_pts)\n } else {\n let rem = p - pair_total;\n if odd == 1 {\n let one_day_pts = l + t;\n if rem <= one_day_pts {\n pairs + 1\n } else {\n pairs + 1 + Self::ceil_div_pos(rem - one_day_pts, l)\n }\n } else {\n pairs + Self::ceil_div_pos(rem, l)\n }\n };\n n - study_days\n }\n\n pub fn max_rest_days(n: i64, p: i64, l: i64, t: i64) -> (res: i64)\n requires\n 1 <= n <= 1_000_000_000,\n 1 <= p <= 1_000_000_000_000_000_000,\n 1 <= l <= 1_000_000_000,\n 1 <= t <= 1_000_000_000,\n ((n + 6) / 7 / 2) * (l + 2 * t) <= 9_000_000_000_000_000_000,\n p <= n * l + ((n + 6) / 7) * t,\n ensures\n res as int == Self::max_rest_days_spec(n as int, p as int, l as int, t as int),\n {\n let tasks: i64 = (n + 6) / 7;\n let pairs: i64 = tasks / 2;\n let odd: i64 = tasks % 2;\n let pair_pts: i64 = l + 2 * t;\n\n let pair_total: i64 = pairs * pair_pts;\n let mut study_days: i64;\n\n if p <= pair_total {\n if p % pair_pts == 0 {\n study_days = p / pair_pts;\n } else {\n study_days = p / pair_pts + 1;\n }\n } else {\n study_days = pairs;\n let mut rem: i64 = if p >= pair_total { p - pair_total } else { 0 };\n if odd == 1 {\n let one_day_pts: i64 = l + t;\n if rem <= one_day_pts {\n study_days = study_days + 1;\n rem = 0;\n } else {\n study_days = study_days + 1;\n rem = rem - one_day_pts;\n }\n }\n if rem > 0 {\n if rem % l == 0 {\n study_days = study_days + rem / l;\n } else {\n study_days = study_days + rem / l + 1;\n }\n }\n }\n\n n - study_days\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_rest_days(n: i64, p: i64, l: i64, t: i64) -> i64 {\n let tasks: i64 = (n + 6) / 7;\n let pairs: i64 = tasks / 2;\n let odd: i64 = tasks % 2;\n let pair_pts: i64 = l + 2 * t;\n\n let pair_total: i64 = pairs * pair_pts;\n let mut study_days: i64;\n\n if p <= pair_total {\n if p % pair_pts == 0 {\n study_days = p / pair_pts;\n } else {\n study_days = p / pair_pts + 1;\n }\n } else {\n study_days = pairs;\n let mut rem: i64 = if p >= pair_total { p - pair_total } else { 0 };\n if odd == 1 {\n let one_day_pts: i64 = l + t;\n if rem <= one_day_pts {\n study_days = study_days + 1;\n rem = 0;\n } else {\n study_days = study_days + 1;\n rem = rem - one_day_pts;\n }\n }\n if rem > 0 {\n if rem % l == 0 {\n study_days = study_days + rem / l;\n } else {\n study_days = study_days + rem / l + 1;\n }\n }\n }\n\n n - study_days\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n\n let q: usize = it.next().expect(\"q\").parse().expect(\"valid q\");\n let mut i: usize = 0;\n while i < q {\n let n: i64 = it.next().expect(\"n\").parse().expect(\"valid n\");\n let p: i64 = it.next().expect(\"p\").parse().expect(\"valid p\");\n let l: i64 = it.next().expect(\"l\").parse().expect(\"valid l\");\n let t: i64 = it.next().expect(\"t\").parse().expect(\"valid t\");\n\n let ans = Solution::max_rest_days(n, p, l, t);\n println!(\"{}\", ans);\n i = i + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1902B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1902B/tests/mutated_testcases.jsonl"} {"problem": "cf1950C", "family": "codeforces", "description": "# Clock Conversion\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nGiven the time in 24-hour format, output the equivalent time in 12-hour format. \n - 24-hour format divides the day into 24 hours from $$$00$$$ to $$$23$$$, each of which has 60 minutes from $$$00$$$ to $$$59$$$. \n- 12-hour format divides the day into two halves: the first half is $$$\\mathrm{AM}$$$, and the second half is $$$\\mathrm{PM}$$$. In each half, the hours are numbered in the order $$$12, 01, 02, 03, \\dots, 11$$$. Each hour has 60 minutes numbered from $$$00$$$ to $$$59$$$.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1440$$$)\u00a0\u2014 the number of test cases.\n\nThe only line of each test case contains a string $$$s$$$ of length $$$5$$$ with format `hh:mm` representing a valid time in the 24-hour format. `hh` represents the hour from $$$00$$$ to $$$23$$$, and `mm` represents the minute from $$$00$$$ to $$$59$$$.\n\nThe input will always be a valid time in 24-hour format.\n\n## Output\n\nFor each test case, output two strings separated by a space (\"`hh:mm AM`\" or \"`hh:mm PM`\"), which are the 12-hour equivalent to the time provided in the test case (without quotes).\n\nYou should output the time exactly as indicated; in particular, you should not remove leading zeroes.\n\n## Example\n\n**Input:**\n```\n11\n09:41\n18:06\n12:14\n00:59\n00:00\n14:34\n01:01\n19:07\n11:59\n12:00\n21:37\n```\n**Output:**\n```\n09:41 AM\n06:06 PM\n12:14 PM\n12:59 AM\n12:00 AM\n02:34 PM\n01:01 AM\n07:07 PM\n11:59 AM\n12:00 PM\n09:37 PM\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn convert_time(h24: u8, minute: u8) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let s = it.next().unwrap();\n let bytes = s.as_bytes();\n \n let h: u8 = (bytes[0] - b'0') * 10 + (bytes[1] - b'0');\n let m: u8 = (bytes[3] - b'0') * 10 + (bytes[4] - b'0');\n let converted = Solution::convert_time(h, m);\n out.write_all(&converted).unwrap();\n writeln!(out).unwrap();\n tc = tc + 1;\n }\n}\n```\n", "tags": "Rating: 800\nimplementation, math\nSolved by: 46828 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn hour12_spec(h24: int) -> int\n recommends 0 <= h24 <= 23,\n{\n if h24 == 0 {\n 12\n } else if h24 <= 11 {\n h24\n } else if h24 == 12 {\n 12\n } else {\n h24 - 12\n }\n}\n\nimpl Solution {\n pub fn convert_hour(h24: u8) -> (result: (u8, bool))\n requires\n h24 <= 23,\n ensures\n 1 <= result.0 <= 12,\n \n result.1 == (h24 >= 12),\n \n (h24 == 0 ==> result.0 == 12),\n (1 <= h24 <= 11 ==> result.0 == h24),\n (h24 == 12 ==> result.0 == 12),\n (13 <= h24 <= 23 ==> result.0 == h24 - 12),\n {\n }\n\n pub fn convert_time(h24: u8, minute: u8) -> (result: Vec)\n requires\n h24 <= 23,\n minute <= 59,\n ensures\n result.len() == 8,\n result[0] as int == 48 + hour12_spec(h24 as int) / 10,\n result[1] as int == 48 + hour12_spec(h24 as int) % 10,\n result[2] == 58u8,\n result[3] as int == 48 + (minute as int) / 10,\n result[4] as int == 48 + (minute as int) % 10,\n result[5] == 32u8,\n result[6] == if h24 >= 12 { 80u8 } else { 65u8 },\n result[7] == 77u8,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn convert_hour(h24: u8) -> (u8, bool) {\n if h24 == 0 {\n (12, false)\n } else if h24 < 12 {\n (h24, false)\n } else if h24 == 12 {\n (12, true)\n } else {\n (h24 - 12, true)\n }\n }\n\n pub fn convert_time(h24: u8, minute: u8) -> Vec {\n let (h12, is_pm) = Self::convert_hour(h24);\n let mut out = Vec::new();\n out.push(48u8 + h12 / 10);\n out.push(48u8 + h12 % 10);\n out.push(58u8);\n out.push(48u8 + minute / 10);\n out.push(48u8 + minute % 10);\n out.push(32u8);\n out.push(if is_pm { 80u8 } else { 65u8 });\n out.push(77u8);\n out\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn hour12_spec(h24: int) -> int\n recommends 0 <= h24 <= 23,\n{\n if h24 == 0 {\n 12\n } else if h24 <= 11 {\n h24\n } else if h24 == 12 {\n 12\n } else {\n h24 - 12\n }\n}\n\nimpl Solution {\n pub fn convert_hour(h24: u8) -> (result: (u8, bool))\n requires\n h24 <= 23,\n ensures\n 1 <= result.0 <= 12,\n result.1 == (h24 >= 12),\n (h24 == 0 ==> result.0 == 12),\n (1 <= h24 <= 11 ==> result.0 == h24),\n (h24 == 12 ==> result.0 == 12),\n (13 <= h24 <= 23 ==> result.0 == h24 - 12),\n {\n if h24 == 0 {\n (12, false)\n } else if h24 < 12 {\n (h24, false)\n } else if h24 == 12 {\n (12, true)\n } else {\n (h24 - 12, true)\n }\n }\n\n pub fn convert_time(h24: u8, minute: u8) -> (result: Vec)\n requires\n h24 <= 23,\n minute <= 59,\n ensures\n result.len() == 8,\n result[0] as int == 48 + hour12_spec(h24 as int) / 10,\n result[1] as int == 48 + hour12_spec(h24 as int) % 10,\n result[2] == 58u8,\n result[3] as int == 48 + (minute as int) / 10,\n result[4] as int == 48 + (minute as int) % 10,\n result[5] == 32u8,\n result[6] == if h24 >= 12 { 80u8 } else { 65u8 },\n result[7] == 77u8,\n {\n let (h12, is_pm) = Self::convert_hour(h24);\n let mut out = Vec::new();\n out.push(48u8 + h12 / 10);\n out.push(48u8 + h12 % 10);\n out.push(58u8);\n out.push(48u8 + minute / 10);\n out.push(48u8 + minute % 10);\n out.push(32u8);\n out.push(if is_pm { 80u8 } else { 65u8 });\n out.push(77u8);\n out\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn hour12_spec(h24: int) -> int\n recommends 0 <= h24 <= 23,\n{\n if h24 == 0 {\n 12\n } else if h24 <= 11 {\n h24\n } else if h24 == 12 {\n 12\n } else {\n h24 - 12\n }\n}\n\nimpl Solution {\n pub fn convert_hour(h24: u8) -> (result: (u8, bool))\n requires\n h24 <= 23,\n ensures\n 1 <= result.0 <= 12,\n result.1 == (h24 >= 12),\n (h24 == 0 ==> result.0 == 12),\n (1 <= h24 <= 11 ==> result.0 == h24),\n (h24 == 12 ==> result.0 == 12),\n (13 <= h24 <= 23 ==> result.0 == h24 - 12),\n {\n if h24 == 0 {\n (12, false)\n } else if h24 < 12 {\n (h24, false)\n } else if h24 == 12 {\n (12, true)\n } else {\n (h24 - 12, true)\n }\n }\n\n pub fn convert_time(h24: u8, minute: u8) -> (result: Vec)\n requires\n h24 <= 23,\n minute <= 59,\n ensures\n result.len() == 8,\n result[0] as int == 48 + hour12_spec(h24 as int) / 10,\n result[1] as int == 48 + hour12_spec(h24 as int) % 10,\n result[2] == 58u8,\n result[3] as int == 48 + (minute as int) / 10,\n result[4] as int == 48 + (minute as int) % 10,\n result[5] == 32u8,\n result[6] == if h24 >= 12 { 80u8 } else { 65u8 },\n result[7] == 77u8,\n {\n let (h12, is_pm) = Self::convert_hour(h24);\n let mut out = Vec::new();\n out.push(48u8 + h12 / 10);\n out.push(48u8 + h12 % 10);\n out.push(58u8);\n out.push(48u8 + minute / 10);\n out.push(48u8 + minute % 10);\n out.push(32u8);\n out.push(if is_pm { 80u8 } else { 65u8 });\n out.push(77u8);\n out\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn convert_hour(h24: u8) -> (u8, bool) {\n if h24 == 0 {\n (12, false)\n } else if h24 < 12 {\n (h24, false)\n } else if h24 == 12 {\n (12, true)\n } else {\n (h24 - 12, true)\n }\n }\n\n pub fn convert_time(h24: u8, minute: u8) -> Vec {\n let (h12, is_pm) = Self::convert_hour(h24);\n let mut out = Vec::new();\n out.push(48u8 + h12 / 10);\n out.push(48u8 + h12 % 10);\n out.push(58u8);\n out.push(48u8 + minute / 10);\n out.push(48u8 + minute % 10);\n out.push(32u8);\n out.push(if is_pm { 80u8 } else { 65u8 });\n out.push(77u8);\n out\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let s = it.next().unwrap();\n let bytes = s.as_bytes();\n \n let h: u8 = (bytes[0] - b'0') * 10 + (bytes[1] - b'0');\n let m: u8 = (bytes[3] - b'0') * 10 + (bytes[4] - b'0');\n let converted = Solution::convert_time(h, m);\n out.write_all(&converted).unwrap();\n writeln!(out).unwrap();\n tc = tc + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1950C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1950C/tests/mutated_testcases.jsonl"} {"problem": "cf1985C", "family": "codeforces", "description": "# Good Prefixes\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nAlex thinks some array is *good* if there exists some element that can be represented as the sum of all **other** elements (the sum of all other elements is $$$0$$$ if there are no other elements). For example, the array $$$[1,6,3,2]$$$ is good since $$$1+3+2=6$$$. Furthermore, the array $$$[0]$$$ is also good. However, the arrays $$$[1,2,3,4]$$$ and $$$[1]$$$ are not good.\n\nAlex has an array $$$a_1,a_2,\\ldots,a_n$$$. Help him count the number of good non-empty prefixes of the array $$$a$$$. In other words, count the number of integers $$$i$$$ ($$$1 \\le i \\le n$$$) such that the length $$$i$$$ prefix (i.e. $$$a_1,a_2,\\ldots,a_i$$$) is good.\n\n## Input\n\nThe first line of the input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) \u2014 the number of test cases.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of elements in the array.\n\nThe second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) \u2014 the elements of the array.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, output a single integer \u2014 the number of good non-empty prefixes of the array $$$a$$$.\n\n## Example\n\n**Input:**\n```\n7\n1\n0\n1\n1\n4\n1 1 2 0\n5\n0 1 2 1 4\n7\n1 1 0 3 5 2 12\n7\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 294967296\n10\n0 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 589934592\n```\n**Output:**\n```\n1\n0\n3\n3\n4\n1\n2\n```\n\n## Note\n\nIn the fourth test case, the array has five prefixes: \n - prefix $$$[0]$$$ is a good array, as mentioned in the statement; \n- prefix $$$[0, 1]$$$ is not a good array, since $$$0 \\ne 1$$$; \n- prefix $$$[0, 1, 2]$$$ is not a good array, since $$$0 \\ne 1 + 2$$$, $$$1 \\ne 0 + 2$$$ and $$$2 \\ne 0 + 1$$$; \n- prefix $$$[0, 1, 2, 1]$$$ is a good array, since $$$2 = 0 + 1 + 1$$$; \n- prefix $$$[0, 1, 2, 1, 4]$$$ is a good array, since $$$4 = 0 + 1 + 2 + 1$$$. \n\nAs you can see, three of them are good, so the answer is $$$3$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_good_prefixes_fn(a: Vec) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::count_good_prefixes_fn(a);\n writeln!(out, \"{}\", ans).unwrap();\n tc = tc + 1;\n }\n}\n```\n", "tags": "Rating: 1000\ngreedy\nSolved by: 40037 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn prefix_sum(a: Seq, end: int) -> int\n recommends\n 0 <= end <= a.len(),\n decreases end,\n{\n if end <= 0 {\n 0int\n } else {\n prefix_sum(a, end - 1) + a[end - 1] as int\n }\n}\n\npub open spec fn prefix_max(a: Seq, end: int) -> int\n recommends\n 1 <= end <= a.len(),\n decreases end,\n{\n if end <= 1 {\n a[0] as int\n } else {\n let prev = prefix_max(a, end - 1);\n let v = a[end - 1] as int;\n if v > prev { v } else { prev }\n }\n}\n\npub open spec fn count_good_prefixes(a: Seq, end: int) -> int\n recommends\n 0 <= end <= a.len(),\n decreases end,\n{\n if end <= 0 {\n 0int\n } else {\n let prev = count_good_prefixes(a, end - 1);\n let s = prefix_sum(a, end);\n let m = prefix_max(a, end);\n if 2 * m == s {\n prev + 1\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn count_good_prefixes_fn(a: Vec) -> (result: usize)\n requires\n 1 <= a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 1_000_000_000u64,\n ensures\n result as int == count_good_prefixes(a@, a.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_good_prefixes_fn(a: Vec) -> usize {\n let mut sum: u64 = 0;\n let mut max_val: u64 = 0;\n let mut count: usize = 0;\n let mut i: usize = 0;\n while i < a.len() {\n let ai = a[i];\n sum = sum + ai;\n if ai > max_val {\n max_val = ai;\n }\n if 2 * max_val == sum {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn prefix_sum(a: Seq, end: int) -> int\n recommends\n 0 <= end <= a.len(),\n decreases end,\n{\n if end <= 0 {\n 0int\n } else {\n prefix_sum(a, end - 1) + a[end - 1] as int\n }\n}\n\npub open spec fn prefix_max(a: Seq, end: int) -> int\n recommends\n 1 <= end <= a.len(),\n decreases end,\n{\n if end <= 1 {\n a[0] as int\n } else {\n let prev = prefix_max(a, end - 1);\n let v = a[end - 1] as int;\n if v > prev { v } else { prev }\n }\n}\n\npub open spec fn count_good_prefixes(a: Seq, end: int) -> int\n recommends\n 0 <= end <= a.len(),\n decreases end,\n{\n if end <= 0 {\n 0int\n } else {\n let prev = count_good_prefixes(a, end - 1);\n let s = prefix_sum(a, end);\n let m = prefix_max(a, end);\n if 2 * m == s {\n prev + 1\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn count_good_prefixes_fn(a: Vec) -> (result: usize)\n requires\n 1 <= a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 1_000_000_000u64,\n ensures\n result as int == count_good_prefixes(a@, a.len() as int),\n {\n let mut sum: u64 = 0;\n let mut max_val: u64 = 0;\n let mut count: usize = 0;\n let mut i: usize = 0;\n while i < a.len() {\n let ai = a[i];\n sum = sum + ai;\n if ai > max_val {\n max_val = ai;\n }\n if 2 * max_val == sum {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn prefix_sum(a: Seq, end: int) -> int\n recommends\n 0 <= end <= a.len(),\n decreases end,\n{\n if end <= 0 {\n 0int\n } else {\n prefix_sum(a, end - 1) + a[end - 1] as int\n }\n}\n\npub open spec fn prefix_max(a: Seq, end: int) -> int\n recommends\n 1 <= end <= a.len(),\n decreases end,\n{\n if end <= 1 {\n a[0] as int\n } else {\n let prev = prefix_max(a, end - 1);\n let v = a[end - 1] as int;\n if v > prev { v } else { prev }\n }\n}\n\npub open spec fn count_good_prefixes(a: Seq, end: int) -> int\n recommends\n 0 <= end <= a.len(),\n decreases end,\n{\n if end <= 0 {\n 0int\n } else {\n let prev = count_good_prefixes(a, end - 1);\n let s = prefix_sum(a, end);\n let m = prefix_max(a, end);\n if 2 * m == s {\n prev + 1\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn count_good_prefixes_fn(a: Vec) -> (result: usize)\n requires\n 1 <= a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 1_000_000_000u64,\n ensures\n result as int == count_good_prefixes(a@, a.len() as int),\n {\n let mut sum: u64 = 0;\n let mut max_val: u64 = 0;\n let mut count: usize = 0;\n let mut i: usize = 0;\n while i < a.len()\n invariant\n i <= a.len(),\n a.len() <= 200_000,\n forall|j: int| 0 <= j < a.len() ==> #[trigger] a[j] <= 1_000_000_000u64,\n sum as int == prefix_sum(a@, i as int),\n sum as int <= (i as int) * 1_000_000_000,\n i == 0 ==> max_val == 0,\n i > 0 ==> max_val as int == prefix_max(a@, i as int),\n max_val <= 1_000_000_000u64,\n count as int == count_good_prefixes(a@, i as int),\n count <= i,\n decreases a.len() - i,\n {\n proof {\n reveal_with_fuel(prefix_sum, 2);\n reveal_with_fuel(prefix_max, 2);\n reveal_with_fuel(count_good_prefixes, 2);\n assert(a[i as int] <= 1_000_000_000u64);\n assert((i as int + 1) * 1_000_000_000 == (i as int) * 1_000_000_000 + 1_000_000_000) by(nonlinear_arith);\n }\n let ai = a[i];\n sum = sum + ai;\n if ai > max_val {\n max_val = ai;\n }\n if 2 * max_val == sum {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_good_prefixes_fn(a: Vec) -> usize {\n let mut sum: u64 = 0;\n let mut max_val: u64 = 0;\n let mut count: usize = 0;\n let mut i: usize = 0;\n while i < a.len() {\n let ai = a[i];\n sum = sum + ai;\n if ai > max_val {\n max_val = ai;\n }\n if 2 * max_val == sum {\n count = count + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::count_good_prefixes_fn(a);\n writeln!(out, \"{}\", ans).unwrap();\n tc = tc + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1985C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1985C/tests/mutated_testcases.jsonl"} {"problem": "cf1989A", "family": "codeforces", "description": "# Catch the Coin\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nMonocarp visited a retro arcade club with arcade cabinets. There got curious about the \"Catch the Coin\" cabinet.\n\nThe game is pretty simple. The screen represents a coordinate grid such that: \n - the X-axis is directed from left to right; \n- the Y-axis is directed from bottom to top; \n- the center of the screen has coordinates $$$(0, 0)$$$. \n\nAt the beginning of the game, the character is located in the center, and $$$n$$$ coins appear on the screen\u00a0\u2014 the $$$i$$$-th coin is at coordinates $$$(x_i, y_i)$$$. The coordinates of all coins are different and not equal to $$$(0, 0)$$$.\n\nIn one second, Monocarp can move the character in one of eight directions. If the character is at coordinates $$$(x, y)$$$, then it can end up at any of the coordinates $$$(x, y + 1)$$$, $$$(x + 1, y + 1)$$$, $$$(x + 1, y)$$$, $$$(x + 1, y - 1)$$$, $$$(x, y - 1)$$$, $$$(x - 1, y - 1)$$$, $$$(x - 1, y)$$$, $$$(x - 1, y + 1)$$$.\n\nIf the character ends up at the coordinates with a coin, then Monocarp collects that coin.\n\nAfter Monocarp makes a move, all coins fall down by $$$1$$$, that is, they move from $$$(x, y)$$$ to $$$(x, y - 1)$$$. You can assume that the game field is infinite in all directions.\n\nMonocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it.\n\n## Input\n\nThe first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 500$$$)\u00a0\u2014 the number of coins.\n\nIn the $$$i$$$-th of the next $$$n$$$ lines, two integers $$$x_i$$$ and $$$y_i$$$ ($$$-50 \\le x_i, y_i \\le 50$$$) are written\u00a0\u2014 the coordinates of the $$$i$$$-th coin. The coordinates of all coins are different. No coin is located at $$$(0, 0)$$$.\n\n## Output\n\nFor each coin, print \"`YES`\" if Monocarp can collect it. Otherwise, print \"`NO`\".\n\n## Example\n\n**Input:**\n```\n5\n24 42\n-2 -1\n-1 -2\n0 -50\n15 0\n```\n**Output:**\n```\nYES\nYES\nNO\nNO\nYES\n```\n\n## Note\n\nPay attention to the second coin in the example. Monocarp can first move from $$$(0, 0)$$$ to $$$(-1, -1)$$$. Then the coin falls $$$1$$$ down and ends up at $$$(-2, -2)$$$. Finally, Monocarp moves to $$$(-2, -2)$$$ and collects the coin.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_catch_coin(x: i32, y: i32) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut i: usize = 0;\n while i < n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n let y: i32 = it.next().unwrap().parse().unwrap();\n if Solution::can_catch_coin(x, y) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n i = i + 1;\n }\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 29249 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn coin_catchable(y: int) -> bool {\n y >= -1\n }\n\n pub fn can_catch_coin(x: i32, y: i32) -> (ok: bool)\n requires\n -50 <= x <= 50,\n -50 <= y <= 50,\n !(x == 0 && y == 0),\n ensures\n ok == Self::coin_catchable(y as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_catch_coin(x: i32, y: i32) -> bool {\n let _ = x;\n y >= -1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn coin_catchable(y: int) -> bool {\n y >= -1\n }\n\n pub fn can_catch_coin(x: i32, y: i32) -> (ok: bool)\n requires\n -50 <= x <= 50,\n -50 <= y <= 50,\n !(x == 0 && y == 0),\n ensures\n ok == Self::coin_catchable(y as int),\n {\n let _ = x;\n y >= -1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn coin_catchable(y: int) -> bool {\n y >= -1\n }\n\n pub fn can_catch_coin(x: i32, y: i32) -> (ok: bool)\n requires\n -50 <= x <= 50,\n -50 <= y <= 50,\n !(x == 0 && y == 0),\n ensures\n ok == Self::coin_catchable(y as int),\n {\n let _ = x;\n proof {\n assert((y >= -1) == Self::coin_catchable(y as int));\n }\n y >= -1\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_catch_coin(x: i32, y: i32) -> bool {\n let _ = x;\n y >= -1\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut i: usize = 0;\n while i < n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n let y: i32 = it.next().unwrap().parse().unwrap();\n if Solution::can_catch_coin(x, y) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n i = i + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1989A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1989A/tests/mutated_testcases.jsonl"} {"problem": "cf1992D", "family": "codeforces", "description": "# Test of Love\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nErnKor is ready to do anything for Julen, even to swim through crocodile-infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $$$1$$$ meter and a length of $$$n$$$ meters.\n\nThe river is very cold. Therefore, **in total** (that is, throughout the entire swim from $$$0$$$ to $$$n+1$$$) ErnKor can swim in the water for no more than $$$k$$$ meters. For the sake of humanity, we have added not only crocodiles to the river, but also logs on which he can jump. Our test is as follows:\n\nInitially, ErnKor is on the left bank and needs to reach the right bank. They are located at the $$$0$$$ and $$$n+1$$$ meters respectively. The river can be represented as $$$n$$$ *segments*, each with a length of $$$1$$$ meter. Each *segment* contains either a log `'L'`, a crocodile `'C'`, or just water `'W'`. ErnKor can move as follows: \n - If he is on the surface (i.e., on the bank or on a log), he can jump forward for no more than $$$m$$$ ($$$1 \\le m \\le 10$$$) meters (he can jump on the bank, on a log, or in the water). \n- If he is in the water, he can only swim to the next river *segment* (or to the bank if he is at the $$$n$$$-th meter). \n- ErnKor cannot land in a *segment* with a crocodile in any way. \n\nDetermine if ErnKor can reach the right bank.\n\n## Input\n\nThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains three numbers $$$n, m, k$$$ ($$$0 \\le k \\le 2 \\cdot 10^5$$$, $$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 10$$$)\u00a0\u2014 the length of the river, the distance ErnKor can jump, and the number of meters ErnKor can swim without freezing.\n\nThe second line of each test case contains a string $$$a$$$ of length $$$n$$$. $$$a_i$$$ denotes the object located at the $$$i$$$-th meter. ($$$a_i \\in \\{$$$`'W'`,`'C'`,`'L'`$$$\\}$$$)\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, output \"`YES`\" if ErnKor can pass the test, and output \"`NO`\" otherwise.\n\nYou can output the answer in any case (upper or lower). For example, the strings \"`yEs`\", \"`yes`\", \"`Yes`\", and \"`YES`\" will be recognized as positive responses.\n\n## Example\n\n**Input:**\n```\n6\n6 2 0\nLWLLLW\n6 1 1\nLWLLLL\n6 1 1\nLWLLWL\n6 2 15\nLWLLCC\n6 10 0\nCCCCCC\n6 6 1\nWCCCCW\n```\n**Output:**\n```\nYES\nYES\nNO\nNO\nYES\nYES\n```\n\n## Note\n\nLet's consider examples: \n - First example: We jump from the shore to the first log ($$$0 \\rightarrow 1$$$), from the first log to the second ($$$1 \\rightarrow 3$$$), from the second to the fourth ($$$3 \\rightarrow 5$$$), and from the last log to the shore ($$$5 \\rightarrow 7$$$). So, we have $$$0 \\rightarrow 1 \\rightarrow 3 \\rightarrow 5 \\rightarrow 7$$$. Since we did not encounter a crocodile and swam no more than k meters, the answer is `\u00abYES\u00bb`. \n- Second example: $$$0 \\rightarrow 1$$$, we jump into the water from the first log ($$$1 \\rightarrow 2$$$), swim a cell to the log ($$$2 \\leadsto 3$$$), $$$3 \\rightarrow 4 \\rightarrow 5 \\rightarrow 6 \\rightarrow 7$$$. Since we did not encounter a crocodile and swam no more than k meters, the answer is `\u00abYES\u00bb`. \n- In the third example, ErnKor needs to swim two cells `'W'`, but can only swim one. Therefore, the answer is `\u00abNO\u00bb`. \n- Sixth example: We jump from the shore into the water ($$$0 \\rightarrow 6$$$) and swim one cell in the water ($$$6 \\leadsto 7$$$). Since we did not encounter a crocodile and swam no more than k meters, the answer is `\u00abYES\u00bb`.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_cross(a: Vec, m: usize, k: i64) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let m: usize = it.next().unwrap().parse().unwrap();\n let k: i64 = it.next().unwrap().parse().unwrap();\n let s_str = it.next().unwrap();\n let a: Vec = s_str.bytes().map(|b| match b {\n b'W' => 0u8,\n b'C' => 1u8,\n b'L' => 2u8,\n _ => 0u8,\n }).collect();\n let _ = n;\n let ans = Solution::can_cross(a, m, k);\n writeln!(out, \"{}\", if ans { \"YES\" } else { \"NO\" }).unwrap();\n tc = tc + 1;\n }\n}\n```\n", "tags": "Rating: 1200\ndp, greedy, implementation\nSolved by: 23973 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\n\npub open spec fn dp_inf() -> i64 {\n 300_000i64 \n}\n\npub open spec fn is_land_or_log(a: Seq, p: int) -> bool {\n p == 0 || p == a.len() + 1 || (1 <= p && p <= a.len() && a[p - 1] == 2u8)\n}\n\npub open spec fn is_water(a: Seq, p: int) -> bool {\n 1 <= p && p <= a.len() && a[p - 1] == 0u8\n}\n\npub open spec fn is_croc(a: Seq, p: int) -> bool {\n 1 <= p && p <= a.len() && a[p - 1] == 1u8\n}\n\npub open spec fn land_cost(a: Seq, p: int) -> int {\n if is_water(a, p) { 1int } else { 0int }\n}\n\n\n\n\n\n\n\n\npub open spec fn dp_min_pred(dp_prev: Seq, a: Seq, m: int, p: int, j_start: int, j_end: int) -> i64\n recommends\n dp_prev.len() == p,\n 0 <= j_start <= j_end <= p,\n m >= 1,\n decreases j_end - j_start,\n{\n if j_start >= j_end {\n dp_inf()\n } else {\n let prev = dp_min_pred(dp_prev, a, m, p, j_start + 1, j_end);\n let cur = dp_prev[j_start];\n let valid = if cur >= dp_inf() {\n false\n } else if is_land_or_log(a, j_start) {\n j_start + 1 <= p && p <= j_start + m\n } else if is_water(a, j_start) {\n p == j_start + 1\n } else {\n false\n };\n if valid {\n let cand = (cur as int + land_cost(a, p)) as i64;\n if cand < prev { cand } else { prev }\n } else {\n prev\n }\n }\n}\n\n\npub open spec fn dp_at(dp_prev: Seq, a: Seq, m: int, p: int) -> i64\n recommends\n dp_prev.len() == p,\n 0 <= p <= a.len() + 1,\n m >= 1,\n{\n if p == 0 {\n 0i64\n } else if is_croc(a, p) {\n dp_inf()\n } else {\n let lo = if p > m { p - m } else { 0int };\n dp_min_pred(dp_prev, a, m, p, lo, p)\n }\n}\n\n\npub open spec fn compute_dp_upto(a: Seq, m: int, k_max: int) -> Seq\n recommends\n 0 <= k_max <= a.len() + 1,\n m >= 1,\n decreases k_max,\n{\n if k_max <= 0 {\n seq![0i64]\n } else {\n let prev = compute_dp_upto(a, m, k_max - 1);\n prev.push(dp_at(prev, a, m, k_max))\n }\n}\n\npub open spec fn compute_dp(a: Seq, m: int) -> Seq\n recommends\n m >= 1,\n{\n compute_dp_upto(a, m, a.len() as int + 1)\n}\n\nimpl Solution {\n pub fn can_cross(a: Vec, m: usize, k: i64) -> (result: bool)\n requires\n 1 <= a.len() <= 200_000,\n 1 <= m <= 10,\n 0 <= k <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 2,\n ensures\n ({\n let dp = compute_dp(a@, m as int);\n let final_dp = dp[a.len() as int + 1];\n result == (final_dp < dp_inf() && final_dp <= k)\n }),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_cross(a: Vec, m: usize, k: i64) -> bool {\n let n = a.len();\n let inf: i64 = 300_000;\n let mut dp: Vec = Vec::with_capacity(n + 2);\n dp.push(0);\n let mut p: usize = 1;\n while p <= n + 1 {\n let val = if p <= n && a[p - 1] == 1 {\n inf\n } else {\n let lo: usize = if p > m { p - m } else { 0 };\n let mut best = inf;\n let mut jj: usize = p;\n while jj > lo {\n jj = jj - 1;\n let djv = dp[jj];\n if djv < inf {\n let valid = if jj == 0 || jj == n + 1 || (1 <= jj && jj <= n && a[jj - 1] == 2) {\n jj + 1 <= p && p <= jj + m\n } else if 1 <= jj && jj <= n && a[jj - 1] == 0 {\n p == jj + 1\n } else {\n false\n };\n if valid {\n let cost: i64 = if p >= 1 && p <= n && a[p - 1] == 0 { 1 } else { 0 };\n let cand = djv + cost;\n if cand < best {\n best = cand;\n }\n }\n }\n }\n best\n };\n dp.push(val);\n p = p + 1;\n }\n dp[n + 1] < inf && dp[n + 1] <= k\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn dp_inf() -> i64 {\n 300_000i64\n}\n\npub open spec fn is_land_or_log(a: Seq, p: int) -> bool {\n p == 0 || p == a.len() + 1 || (1 <= p && p <= a.len() && a[p - 1] == 2u8)\n}\n\npub open spec fn is_water(a: Seq, p: int) -> bool {\n 1 <= p && p <= a.len() && a[p - 1] == 0u8\n}\n\npub open spec fn is_croc(a: Seq, p: int) -> bool {\n 1 <= p && p <= a.len() && a[p - 1] == 1u8\n}\n\npub open spec fn land_cost(a: Seq, p: int) -> int {\n if is_water(a, p) { 1int } else { 0int }\n}\n\npub open spec fn dp_min_pred(dp_prev: Seq, a: Seq, m: int, p: int, j_start: int, j_end: int) -> i64\n recommends\n dp_prev.len() == p,\n 0 <= j_start <= j_end <= p,\n m >= 1,\n decreases j_end - j_start,\n{\n if j_start >= j_end {\n dp_inf()\n } else {\n let prev = dp_min_pred(dp_prev, a, m, p, j_start + 1, j_end);\n let cur = dp_prev[j_start];\n let valid = if cur >= dp_inf() {\n false\n } else if is_land_or_log(a, j_start) {\n j_start + 1 <= p && p <= j_start + m\n } else if is_water(a, j_start) {\n p == j_start + 1\n } else {\n false\n };\n if valid {\n let cand = (cur as int + land_cost(a, p)) as i64;\n if cand < prev { cand } else { prev }\n } else {\n prev\n }\n }\n}\n\npub open spec fn dp_at(dp_prev: Seq, a: Seq, m: int, p: int) -> i64\n recommends\n dp_prev.len() == p,\n 0 <= p <= a.len() + 1,\n m >= 1,\n{\n if p == 0 {\n 0i64\n } else if is_croc(a, p) {\n dp_inf()\n } else {\n let lo = if p > m { p - m } else { 0int };\n dp_min_pred(dp_prev, a, m, p, lo, p)\n }\n}\n\npub open spec fn compute_dp_upto(a: Seq, m: int, k_max: int) -> Seq\n recommends\n 0 <= k_max <= a.len() + 1,\n m >= 1,\n decreases k_max,\n{\n if k_max <= 0 {\n seq![0i64]\n } else {\n let prev = compute_dp_upto(a, m, k_max - 1);\n prev.push(dp_at(prev, a, m, k_max))\n }\n}\n\npub open spec fn compute_dp(a: Seq, m: int) -> Seq\n recommends\n m >= 1,\n{\n compute_dp_upto(a, m, a.len() as int + 1)\n}\n\nimpl Solution {\n pub fn can_cross(a: Vec, m: usize, k: i64) -> (result: bool)\n requires\n 1 <= a.len() <= 200_000,\n 1 <= m <= 10,\n 0 <= k <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 2,\n ensures\n ({\n let dp = compute_dp(a@, m as int);\n let final_dp = dp[a.len() as int + 1];\n result == (final_dp < dp_inf() && final_dp <= k)\n }),\n {\n let n = a.len();\n let inf: i64 = 300_000;\n let mut dp: Vec = Vec::with_capacity(n + 2);\n dp.push(0);\n let mut p: usize = 1;\n while p <= n + 1 {\n let val = if p <= n && a[p - 1] == 1 {\n inf\n } else {\n let lo: usize = if p > m { p - m } else { 0 };\n let mut best = inf;\n let mut jj: usize = p;\n while jj > lo {\n jj = jj - 1;\n let djv = dp[jj];\n if djv < inf {\n let valid = if jj == 0 || jj == n + 1 || (1 <= jj && jj <= n && a[jj - 1] == 2) {\n jj + 1 <= p && p <= jj + m\n } else if 1 <= jj && jj <= n && a[jj - 1] == 0 {\n p == jj + 1\n } else {\n false\n };\n if valid {\n let cost: i64 = if p >= 1 && p <= n && a[p - 1] == 0 { 1 } else { 0 };\n let cand = djv + cost;\n if cand < best {\n best = cand;\n }\n }\n }\n }\n best\n };\n dp.push(val);\n p = p + 1;\n }\n dp[n + 1] < inf && dp[n + 1] <= k\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn dp_inf() -> i64 {\n 300_000i64\n}\n\npub open spec fn is_land_or_log(a: Seq, p: int) -> bool {\n p == 0 || p == a.len() + 1 || (1 <= p && p <= a.len() && a[p - 1] == 2u8)\n}\n\npub open spec fn is_water(a: Seq, p: int) -> bool {\n 1 <= p && p <= a.len() && a[p - 1] == 0u8\n}\n\npub open spec fn is_croc(a: Seq, p: int) -> bool {\n 1 <= p && p <= a.len() && a[p - 1] == 1u8\n}\n\npub open spec fn land_cost(a: Seq, p: int) -> int {\n if is_water(a, p) { 1int } else { 0int }\n}\n\npub open spec fn dp_min_pred(dp_prev: Seq, a: Seq, m: int, p: int, j_start: int, j_end: int) -> i64\n recommends\n dp_prev.len() == p,\n 0 <= j_start <= j_end <= p,\n m >= 1,\n decreases j_end - j_start,\n{\n if j_start >= j_end {\n dp_inf()\n } else {\n let prev = dp_min_pred(dp_prev, a, m, p, j_start + 1, j_end);\n let cur = dp_prev[j_start];\n let valid = if cur >= dp_inf() {\n false\n } else if is_land_or_log(a, j_start) {\n j_start + 1 <= p && p <= j_start + m\n } else if is_water(a, j_start) {\n p == j_start + 1\n } else {\n false\n };\n if valid {\n let cand = (cur as int + land_cost(a, p)) as i64;\n if cand < prev { cand } else { prev }\n } else {\n prev\n }\n }\n}\n\npub open spec fn dp_at(dp_prev: Seq, a: Seq, m: int, p: int) -> i64\n recommends\n dp_prev.len() == p,\n 0 <= p <= a.len() + 1,\n m >= 1,\n{\n if p == 0 {\n 0i64\n } else if is_croc(a, p) {\n dp_inf()\n } else {\n let lo = if p > m { p - m } else { 0int };\n dp_min_pred(dp_prev, a, m, p, lo, p)\n }\n}\n\npub open spec fn compute_dp_upto(a: Seq, m: int, k_max: int) -> Seq\n recommends\n 0 <= k_max <= a.len() + 1,\n m >= 1,\n decreases k_max,\n{\n if k_max <= 0 {\n seq![0i64]\n } else {\n let prev = compute_dp_upto(a, m, k_max - 1);\n prev.push(dp_at(prev, a, m, k_max))\n }\n}\n\npub open spec fn compute_dp(a: Seq, m: int) -> Seq\n recommends\n m >= 1,\n{\n compute_dp_upto(a, m, a.len() as int + 1)\n}\n\n\nproof fn lemma_compute_dp_upto_len(a: Seq, m: int, k_max: int)\n requires\n 0 <= k_max,\n m >= 1,\n ensures\n compute_dp_upto(a, m, k_max).len() == k_max + 1,\n decreases k_max,\n{\n if k_max <= 0 {\n } else {\n lemma_compute_dp_upto_len(a, m, k_max - 1);\n }\n}\n\n\nproof fn lemma_compute_dp_upto_idx(a: Seq, m: int, k_max: int, idx: int)\n requires\n 0 <= k_max,\n 0 <= idx <= k_max,\n m >= 1,\n ensures\n compute_dp_upto(a, m, k_max)[idx] == compute_dp_upto(a, m, idx)[idx],\n decreases k_max - idx,\n{\n if k_max == idx {\n } else {\n lemma_compute_dp_upto_idx(a, m, k_max - 1, idx);\n lemma_compute_dp_upto_len(a, m, k_max - 1);\n }\n}\n\nimpl Solution {\n pub fn can_cross(a: Vec, m: usize, k: i64) -> (result: bool)\n requires\n 1 <= a.len() <= 200_000,\n 1 <= m <= 10,\n 0 <= k <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 2,\n ensures\n ({\n let dp = compute_dp(a@, m as int);\n let final_dp = dp[a.len() as int + 1];\n result == (final_dp < dp_inf() && final_dp <= k)\n }),\n {\n let n = a.len();\n let inf: i64 = 300_000;\n let mut dp: Vec = Vec::with_capacity(n + 2);\n dp.push(0);\n proof {\n assert(dp@ == seq![0i64]);\n assert(compute_dp_upto(a@, m as int, 0) == seq![0i64]);\n assert(dp@ == compute_dp_upto(a@, m as int, 0));\n }\n let mut p: usize = 1;\n while p <= n + 1\n invariant\n 1 <= p <= n + 2,\n n == a.len(),\n 1 <= n <= 200_000,\n 1 <= m <= 10,\n inf == 300_000i64,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 2,\n dp.len() == p,\n dp@ == compute_dp_upto(a@, m as int, (p - 1) as int),\n decreases (n + 2) - p,\n {\n let val = if p <= n && a[p - 1] == 1 {\n inf\n } else {\n let lo: usize = if p > m { p - m } else { 0 };\n let mut best = inf;\n let mut jj: usize = p;\n while jj > lo\n invariant\n lo <= jj <= p,\n n == a.len(),\n 1 <= n <= 200_000,\n 1 <= m <= 10,\n 1 <= p <= n + 1,\n lo as int == if (p as int) > (m as int) { (p as int) - (m as int) } else { 0int },\n inf == 300_000i64,\n forall|i: int| 0 <= i < a.len() ==> #[trigger] a[i] <= 2,\n dp.len() == p,\n dp@ == compute_dp_upto(a@, m as int, (p - 1) as int),\n best == dp_min_pred(dp@, a@, m as int, p as int, jj as int, p as int),\n decreases jj,\n {\n jj = jj - 1;\n proof {\n reveal_with_fuel(dp_min_pred, 2);\n }\n let djv = dp[jj];\n if djv < inf {\n let valid = if jj == 0 || jj == n + 1 || (1 <= jj && jj <= n && a[jj - 1] == 2) {\n jj + 1 <= p && p <= jj + m\n } else if 1 <= jj && jj <= n && a[jj - 1] == 0 {\n p == jj + 1\n } else {\n false\n };\n if valid {\n let cost: i64 = if p >= 1 && p <= n && a[p - 1] == 0 { 1 } else { 0 };\n let cand = djv + cost;\n if cand < best {\n best = cand;\n }\n }\n }\n }\n proof {\n assert(jj == lo);\n assert(best == dp_min_pred(dp@, a@, m as int, p as int, lo as int, p as int));\n if !(p as int <= a@.len() as int && a@[(p - 1) as int] == 1u8) {\n assert(!is_croc(a@, p as int));\n assert(dp_at(dp@, a@, m as int, p as int) == dp_min_pred(dp@, a@, m as int, p as int, lo as int, p as int));\n }\n }\n best\n };\n proof {\n if p <= n && a[p - 1] == 1 {\n assert(is_croc(a@, p as int));\n assert(dp_at(dp@, a@, m as int, p as int) == dp_inf());\n assert(val == dp_inf());\n } else {\n assert(!is_croc(a@, p as int));\n assert(val == dp_at(dp@, a@, m as int, p as int));\n }\n }\n dp.push(val);\n proof {\n reveal_with_fuel(compute_dp_upto, 2);\n let dp_old = compute_dp_upto(a@, m as int, (p - 1) as int);\n assert(compute_dp_upto(a@, m as int, p as int) == dp_old.push(dp_at(dp_old, a@, m as int, p as int)));\n assert(dp@ == compute_dp_upto(a@, m as int, p as int));\n }\n p = p + 1;\n }\n proof {\n assert(p == n + 2);\n assert(dp@ == compute_dp_upto(a@, m as int, (n + 1) as int));\n assert(compute_dp(a@, m as int) == compute_dp_upto(a@, m as int, n as int + 1));\n lemma_compute_dp_upto_len(a@, m as int, n as int + 1);\n assert(dp@.len() == n + 2);\n }\n dp[n + 1] < inf && dp[n + 1] <= k\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_cross(a: Vec, m: usize, k: i64) -> bool {\n let n = a.len();\n let inf: i64 = 300_000;\n let mut dp: Vec = Vec::with_capacity(n + 2);\n dp.push(0);\n let mut p: usize = 1;\n while p <= n + 1 {\n let val = if p <= n && a[p - 1] == 1 {\n inf\n } else {\n let lo: usize = if p > m { p - m } else { 0 };\n let mut best = inf;\n let mut jj: usize = p;\n while jj > lo {\n jj = jj - 1;\n let djv = dp[jj];\n if djv < inf {\n let valid = if jj == 0 || jj == n + 1 || (1 <= jj && jj <= n && a[jj - 1] == 2) {\n jj + 1 <= p && p <= jj + m\n } else if 1 <= jj && jj <= n && a[jj - 1] == 0 {\n p == jj + 1\n } else {\n false\n };\n if valid {\n let cost: i64 = if p >= 1 && p <= n && a[p - 1] == 0 { 1 } else { 0 };\n let cand = djv + cost;\n if cand < best {\n best = cand;\n }\n }\n }\n }\n best\n };\n dp.push(val);\n p = p + 1;\n }\n dp[n + 1] < inf && dp[n + 1] <= k\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let m: usize = it.next().unwrap().parse().unwrap();\n let k: i64 = it.next().unwrap().parse().unwrap();\n let s_str = it.next().unwrap();\n let a: Vec = s_str.bytes().map(|b| match b {\n b'W' => 0u8,\n b'C' => 1u8,\n b'L' => 2u8,\n _ => 0u8,\n }).collect();\n let _ = n;\n let ans = Solution::can_cross(a, m, k);\n writeln!(out, \"{}\", if ans { \"YES\" } else { \"NO\" }).unwrap();\n tc = tc + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf1992D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1992D/tests/mutated_testcases.jsonl"} {"problem": "cf1A", "family": "codeforces", "description": "# Theatre Square\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nTheatre Square in the capital city of Berland has a rectangular shape with the size $n\u2009\u00d7\u2009m$ meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size $a\u2009\u00d7\u2009a$.\n\nWhat is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.\n\n## Input\n\nThe input contains three positive integer numbers in the first line: $n,\u2009\u2009m$ and $a$ ($1\u2009\u2264\u2009\u2009n,\u2009m,\u2009a\u2009\u2264\u200910^9$).\n\n## Output\n\nWrite the needed number of flagstones.\n\n## Example\n\n**Input:**\n```\n6 6 4\n```\n**Output:**\n```\n4\n```\n\n## Starter Code\n\n```rust\nuse std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_flagstones(n: u64, m: u64, a: u64) -> u64 {\n \n }\n}\n\nfn main() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"read line\");\n let parts: Vec = line\n .split_whitespace()\n .map(|s| s.parse().expect(\"integer\"))\n .collect();\n let n = parts[0];\n let m = parts[1];\n let a = parts[2];\n println!(\"{}\", Solution::min_flagstones(n, m, a));\n}\n```\n", "tags": "Rating: 1000\nmath\nSolved by: 315793 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn ceil_div(n: int, a: int) -> int {\n (n + a - 1) / a\n}\n\nimpl Solution {\n pub fn min_flagstones(n: u64, m: u64, a: u64) -> (result: u64)\n requires\n 1 <= n <= 1_000_000_000,\n 1 <= m <= 1_000_000_000,\n 1 <= a <= 1_000_000_000,\n ensures\n result as int == ceil_div(n as int, a as int) * ceil_div(m as int, a as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_flagstones(n: u64, m: u64, a: u64) -> u64 {\n let rows = (n + a - 1) / a;\n let cols = (m + a - 1) / a;\n rows * cols\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn ceil_div(n: int, a: int) -> int {\n (n + a - 1) / a\n}\n\nimpl Solution {\n pub fn min_flagstones(n: u64, m: u64, a: u64) -> (result: u64)\n requires\n 1 <= n <= 1_000_000_000,\n 1 <= m <= 1_000_000_000,\n 1 <= a <= 1_000_000_000,\n ensures\n result as int == ceil_div(n as int, a as int) * ceil_div(m as int, a as int),\n {\n let rows = (n + a - 1) / a;\n let cols = (m + a - 1) / a;\n rows * cols\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn ceil_div(n: int, a: int) -> int {\n (n + a - 1) / a\n}\n\nimpl Solution {\n pub fn min_flagstones(n: u64, m: u64, a: u64) -> (result: u64)\n requires\n 1 <= n <= 1_000_000_000,\n 1 <= m <= 1_000_000_000,\n 1 <= a <= 1_000_000_000,\n ensures\n result as int == ceil_div(n as int, a as int) * ceil_div(m as int, a as int),\n {\n let rows = (n + a - 1) / a;\n let cols = (m + a - 1) / a;\n proof {\n assert(rows as int == (n as int + a as int - 1) / (a as int));\n assert(rows as int == ceil_div(n as int, a as int));\n assert(cols as int == (m as int + a as int - 1) / (a as int));\n assert(cols as int == ceil_div(m as int, a as int));\n assert(rows <= 2_000_000_000u64);\n assert(cols <= 2_000_000_000u64);\n assert(rows as int * cols as int <= 4_000_000_000_000_000_000) by (nonlinear_arith)\n requires\n rows as int <= 2_000_000_000,\n cols as int <= 2_000_000_000,\n {}\n assert(4_000_000_000_000_000_000 < 18446744073709551616) by (nonlinear_arith) {}\n }\n rows * cols\n }\n}\n\n}\n", "main": "use std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_flagstones(n: u64, m: u64, a: u64) -> u64 {\n let rows = (n + a - 1) / a;\n let cols = (m + a - 1) / a;\n rows * cols\n }\n}\n\nfn main() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"read line\");\n let parts: Vec = line\n .split_whitespace()\n .map(|s| s.parse().expect(\"integer\"))\n .collect();\n let n = parts[0];\n let m = parts[1];\n let a = parts[2];\n println!(\"{}\", Solution::min_flagstones(n, m, a));\n}\n", "checker": null, "testcases_path": "codeforces/cf1A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf1A/tests/mutated_testcases.jsonl"} {"problem": "cf2000D", "family": "codeforces", "description": "# Right Left Wrong\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nVlad found a strip of $$$n$$$ cells, numbered from left to right from $$$1$$$ to $$$n$$$. In the $$$i$$$-th cell, there is a positive integer $$$a_i$$$ and a letter $$$s_i$$$, where all $$$s_i$$$ are either '`L`' or '`R`'.\n\nVlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.\n\nIn one operation, you can choose two indices $$$l$$$ and $$$r$$$ ($$$1 \\le l \\lt r \\le n$$$) such that $$$s_l$$$ = '`L`' and $$$s_r$$$ = '`R`' and do the following:\n - add $$$a_l + a_{l + 1} + \\dots + a_{r - 1} + a_r$$$ points to the current score; \n- replace $$$s_i$$$ with '`.`' for all $$$l \\le i \\le r$$$, meaning you can no longer choose these indices. \n\nFor example, consider the following strip:\n $$$3$$$$$$5$$$$$$1$$$$$$4$$$$$$3$$$$$$2$$$`L``R``L``L``L``R` \nYou can first choose $$$l = 1$$$, $$$r = 2$$$ and add $$$3 + 5 = 8$$$ to your score.\n $$$3$$$$$$5$$$$$$1$$$$$$4$$$$$$3$$$$$$2$$$`.``.``L``L``L``R` \nThen choose $$$l = 3$$$, $$$r = 6$$$ and add $$$1 + 4 + 3 + 2 = 10$$$ to your score.\n $$$3$$$$$$5$$$$$$1$$$$$$4$$$$$$3$$$$$$2$$$`.``.``.``.``.``.` \nAs a result, it is impossible to perform another operation, and the final score is $$$18$$$.\n\nWhat is the maximum score that can be achieved?\n\n## Input\n\nThe first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the strip.\n\nThe second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$)\u00a0\u2014 the numbers written on the strip.\n\nThe third line of each test case contains a string $$$s$$$ of $$$n$$$ characters '`L`' and '`R`'.\n\nIt is guaranteed that the sum of the values of $$$n$$$ across all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, output one integer\u00a0\u2014 the maximum possible number of points that can be scored.\n\n## Example\n\n**Input:**\n```\n4\n6\n3 5 1 4 3 2\nLRLLLR\n2\n2 8\nLR\n2\n3 9\nRL\n5\n1 2 3 4 5\nLRLRR\n```\n**Output:**\n```\n18\n10\n0\n22\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_score(a: Vec, s: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let s_str = it.next().unwrap();\n let s: Vec = s_str.bytes().map(|b| match b {\n b'L' => 1u8,\n b'R' => 2u8,\n _ => 0u8,\n }).collect();\n let _ = n;\n let ans = Solution::max_score(a, s);\n writeln!(out, \"{}\", ans).unwrap();\n tc = tc + 1;\n }\n}\n```\n", "tags": "Rating: 1200\ngreedy, implementation, two pointers\nSolved by: 28778 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn pref_sum(a: Seq, idx: int) -> int\n recommends\n 0 <= idx <= a.len(),\n decreases idx,\n{\n if idx <= 0 {\n 0int\n } else {\n pref_sum(a, idx - 1) + a[idx - 1] as int\n }\n}\n\npub open spec fn leftmost_l(s: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi <= s.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n -1int\n } else if s[lo] == 1u8 {\n lo\n } else {\n leftmost_l(s, lo + 1, hi)\n }\n}\n\npub open spec fn rightmost_r(s: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi <= s.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n -1int\n } else if s[hi - 1] == 2u8 {\n hi - 1\n } else {\n rightmost_r(s, lo, hi - 1)\n }\n}\n\n\n\npub open spec fn greedy(a: Seq, s: Seq, lo: int, hi: int) -> int\n recommends\n a.len() == s.len(),\n 0 <= lo <= hi <= a.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n let l = leftmost_l(s, lo, hi);\n let r = rightmost_r(s, lo, hi);\n if l < 0 || r < 0 || l >= r {\n 0int\n } else {\n (pref_sum(a, r + 1) - pref_sum(a, l)) + greedy(a, s, l + 1, r)\n }\n}\n\n\nimpl Solution {\n pub fn max_score(a: Vec, s: Vec) -> (result: i64)\n requires\n 2 <= a.len() <= 200_000,\n a.len() == s.len(),\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 100_000,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] == 1u8 || s[i] == 2u8,\n ensures\n result as int == greedy(a@, s@, 0, a.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_score(a: Vec, s: Vec) -> i64 {\n let n = a.len();\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0);\n let mut i: usize = 0;\n while i < n {\n let next = prefix[i] + a[i];\n prefix.push(next);\n i = i + 1;\n }\n let mut total: i64 = 0;\n let mut lo: usize = 0;\n let mut hi: usize = n;\n let mut keep_going: bool = true;\n while keep_going {\n while lo < hi && s[lo] != 1 {\n lo = lo + 1;\n }\n while lo < hi && s[hi - 1] != 2 {\n hi = hi - 1;\n }\n if lo + 1 < hi {\n total = total + prefix[hi] - prefix[lo];\n lo = lo + 1;\n hi = hi - 1;\n } else {\n keep_going = false;\n }\n }\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn pref_sum(a: Seq, idx: int) -> int\n recommends\n 0 <= idx <= a.len(),\n decreases idx,\n{\n if idx <= 0 {\n 0int\n } else {\n pref_sum(a, idx - 1) + a[idx - 1] as int\n }\n}\n\npub open spec fn leftmost_l(s: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi <= s.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n -1int\n } else if s[lo] == 1u8 {\n lo\n } else {\n leftmost_l(s, lo + 1, hi)\n }\n}\n\npub open spec fn rightmost_r(s: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi <= s.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n -1int\n } else if s[hi - 1] == 2u8 {\n hi - 1\n } else {\n rightmost_r(s, lo, hi - 1)\n }\n}\n\n\n\npub open spec fn greedy(a: Seq, s: Seq, lo: int, hi: int) -> int\n recommends\n a.len() == s.len(),\n 0 <= lo <= hi <= a.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n let l = leftmost_l(s, lo, hi);\n let r = rightmost_r(s, lo, hi);\n if l < 0 || r < 0 || l >= r {\n 0int\n } else {\n (pref_sum(a, r + 1) - pref_sum(a, l)) + greedy(a, s, l + 1, r)\n }\n}\n\n\nimpl Solution {\n pub fn max_score(a: Vec, s: Vec) -> (result: i64)\n requires\n 2 <= a.len() <= 200_000,\n a.len() == s.len(),\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 100_000,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] == 1u8 || s[i] == 2u8,\n ensures\n result as int == greedy(a@, s@, 0, a.len() as int),\n {\n let n = a.len();\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0);\n let mut i: usize = 0;\n while i < n {\n let next = prefix[i] + a[i];\n prefix.push(next);\n i = i + 1;\n }\n let mut total: i64 = 0;\n let mut lo: usize = 0;\n let mut hi: usize = n;\n let mut keep_going: bool = true;\n while keep_going {\n while lo < hi && s[lo] != 1 {\n lo = lo + 1;\n }\n while lo < hi && s[hi - 1] != 2 {\n hi = hi - 1;\n }\n if lo + 1 < hi {\n total = total + prefix[hi] - prefix[lo];\n lo = lo + 1;\n hi = hi - 1;\n } else {\n keep_going = false;\n }\n }\n total\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn pref_sum(a: Seq, idx: int) -> int\n recommends\n 0 <= idx <= a.len(),\n decreases idx,\n{\n if idx <= 0 {\n 0int\n } else {\n pref_sum(a, idx - 1) + a[idx - 1] as int\n }\n}\n\npub open spec fn leftmost_l(s: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi <= s.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n -1int\n } else if s[lo] == 1u8 {\n lo\n } else {\n leftmost_l(s, lo + 1, hi)\n }\n}\n\npub open spec fn rightmost_r(s: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi <= s.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n -1int\n } else if s[hi - 1] == 2u8 {\n hi - 1\n } else {\n rightmost_r(s, lo, hi - 1)\n }\n}\n\n\nproof fn lemma_leftmost_l_bounds(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= s.len(),\n ensures\n leftmost_l(s, lo, hi) == -1int || (lo <= leftmost_l(s, lo, hi) < hi),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n } else if s[lo] == 1u8 {\n } else {\n lemma_leftmost_l_bounds(s, lo + 1, hi);\n }\n}\n\nproof fn lemma_rightmost_r_bounds(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= s.len(),\n ensures\n rightmost_r(s, lo, hi) == -1int || (lo <= rightmost_r(s, lo, hi) < hi),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n } else if s[hi - 1] == 2u8 {\n } else {\n lemma_rightmost_r_bounds(s, lo, hi - 1);\n }\n}\n\npub open spec fn greedy(a: Seq, s: Seq, lo: int, hi: int) -> int\n recommends\n a.len() == s.len(),\n 0 <= lo <= hi <= a.len(),\n decreases if hi > lo { hi - lo } else { 0int },\n when 0 <= lo <= hi <= s.len()\n via greedy_decreases\n{\n let l = leftmost_l(s, lo, hi);\n let r = rightmost_r(s, lo, hi);\n if l < 0 || r < 0 || l >= r {\n 0int\n } else {\n (pref_sum(a, r + 1) - pref_sum(a, l)) + greedy(a, s, l + 1, r)\n }\n}\n\n#[via_fn]\nproof fn greedy_decreases(a: Seq, s: Seq, lo: int, hi: int)\n{\n lemma_leftmost_l_bounds(s, lo, hi);\n lemma_rightmost_r_bounds(s, lo, hi);\n}\n\n\nproof fn lemma_leftmost_l_skip(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo < hi <= s.len(),\n s[lo] != 1u8,\n ensures\n leftmost_l(s, lo, hi) == leftmost_l(s, lo + 1, hi),\n{\n reveal_with_fuel(leftmost_l, 2);\n}\n\n\nproof fn lemma_greedy_skip_l(a: Seq, s: Seq, lo: int, hi: int)\n requires\n a.len() == s.len(),\n 0 <= lo < hi <= a.len(),\n s[lo] != 1u8,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] == 1u8 || s[i] == 2u8,\n ensures\n greedy(a, s, lo, hi) == greedy(a, s, lo + 1, hi),\n{\n reveal_with_fuel(greedy, 2);\n reveal_with_fuel(leftmost_l, 2);\n let l1 = leftmost_l(s, lo, hi);\n let l2 = leftmost_l(s, lo + 1, hi);\n assert(l1 == l2);\n let r1 = rightmost_r(s, lo, hi);\n let r2 = rightmost_r(s, lo + 1, hi);\n lemma_leftmost_l_neg(s, lo + 1, hi);\n if r2 >= 0 {\n lemma_rightmost_r_unaffected_by_lo(s, lo, hi);\n assert(r1 == r2);\n if l1 < 0 || r1 < 0 || l1 >= r1 {\n assert(greedy(a, s, lo, hi) == 0int);\n assert(greedy(a, s, lo + 1, hi) == 0int);\n } else {\n }\n } else {\n lemma_rightmost_r_neg(s, lo + 1, hi);\n assert(r2 == -1int);\n if s[lo] == 2u8 {\n lemma_rightmost_r_eq_lo(s, lo, hi);\n assert(r1 == lo);\n \n assert(greedy(a, s, lo + 1, hi) == 0int);\n \n if l1 < 0 {\n assert(greedy(a, s, lo, hi) == 0int);\n } else {\n \n assert(l1 >= lo + 1);\n assert(l1 > r1);\n assert(greedy(a, s, lo, hi) == 0int);\n }\n } else {\n assert(false);\n }\n }\n}\n\nproof fn lemma_rightmost_r_unaffected_by_lo(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo < hi <= s.len(),\n rightmost_r(s, lo + 1, hi) >= 0,\n ensures\n rightmost_r(s, lo, hi) == rightmost_r(s, lo + 1, hi),\n decreases hi - lo,\n{\n if hi == lo + 1 {\n \n reveal_with_fuel(rightmost_r, 1);\n } else {\n reveal_with_fuel(rightmost_r, 2);\n if s[hi - 1] == 2u8 {\n assert(rightmost_r(s, lo, hi) == hi - 1);\n assert(rightmost_r(s, lo + 1, hi) == hi - 1);\n } else {\n lemma_rightmost_r_unaffected_by_lo(s, lo, hi - 1);\n }\n }\n}\n\nproof fn lemma_rightmost_r_eq_lo(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo < hi <= s.len(),\n s[lo] == 2u8,\n rightmost_r(s, lo + 1, hi) == -1int,\n ensures\n rightmost_r(s, lo, hi) == lo,\n decreases hi - lo,\n{\n if hi == lo + 1 {\n reveal_with_fuel(rightmost_r, 1);\n assert(rightmost_r(s, lo, hi) == lo);\n } else {\n reveal_with_fuel(rightmost_r, 2);\n \n if s[hi - 1] == 2u8 {\n \n assert(rightmost_r(s, lo + 1, hi) == hi - 1);\n assert(false);\n }\n lemma_rightmost_r_eq_lo(s, lo, hi - 1);\n }\n}\n\n\nproof fn lemma_greedy_skip_r(a: Seq, s: Seq, lo: int, hi: int)\n requires\n a.len() == s.len(),\n 0 <= lo < hi <= a.len(),\n s[hi - 1] != 2u8,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] == 1u8 || s[i] == 2u8,\n ensures\n greedy(a, s, lo, hi) == greedy(a, s, lo, hi - 1),\n{\n reveal_with_fuel(greedy, 2);\n reveal_with_fuel(rightmost_r, 2);\n let l1 = leftmost_l(s, lo, hi);\n let l2 = leftmost_l(s, lo, hi - 1);\n let r1 = rightmost_r(s, lo, hi);\n let r2 = rightmost_r(s, lo, hi - 1);\n assert(r1 == r2);\n lemma_rightmost_r_neg(s, lo, hi - 1);\n if l2 >= 0 {\n lemma_leftmost_l_unaffected_by_hi(s, lo, hi);\n assert(l1 == l2);\n \n if l1 < 0 || r1 < 0 || l1 >= r1 {\n assert(greedy(a, s, lo, hi) == 0int);\n assert(greedy(a, s, lo, hi - 1) == 0int);\n } else {\n assert(greedy(a, s, lo, hi) == (pref_sum(a, r1 + 1) - pref_sum(a, l1)) + greedy(a, s, l1 + 1, r1));\n assert(greedy(a, s, lo, hi - 1) == (pref_sum(a, r2 + 1) - pref_sum(a, l2)) + greedy(a, s, l2 + 1, r2));\n }\n } else {\n lemma_leftmost_l_neg(s, lo, hi - 1);\n assert(l2 == -1int);\n if s[hi - 1] == 1u8 {\n lemma_leftmost_l_eq_hi_minus_1(s, lo, hi);\n assert(l1 == hi - 1);\n \n assert(greedy(a, s, lo, hi - 1) == 0int);\n \n if r1 < 0 {\n assert(greedy(a, s, lo, hi) == 0int);\n } else {\n \n assert(r1 < hi - 1);\n assert(l1 >= r1);\n assert(greedy(a, s, lo, hi) == 0int);\n }\n } else {\n assert(s[hi - 1] == 1u8 || s[hi - 1] == 2u8);\n assert(false);\n }\n }\n}\n\nproof fn lemma_leftmost_l_neg(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= s.len(),\n ensures\n leftmost_l(s, lo, hi) == -1int || (lo <= leftmost_l(s, lo, hi) < hi),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n } else if s[lo] == 1u8 {\n } else {\n lemma_leftmost_l_neg(s, lo + 1, hi);\n }\n}\n\nproof fn lemma_rightmost_r_neg(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= s.len(),\n ensures\n rightmost_r(s, lo, hi) == -1int || (lo <= rightmost_r(s, lo, hi) < hi),\n decreases if hi > lo { hi - lo } else { 0int },\n{\n if lo >= hi {\n } else if s[hi - 1] == 2u8 {\n } else {\n lemma_rightmost_r_neg(s, lo, hi - 1);\n }\n}\n\nproof fn lemma_leftmost_l_unaffected_by_hi(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo < hi <= s.len(),\n leftmost_l(s, lo, hi - 1) >= 0,\n ensures\n leftmost_l(s, lo, hi) == leftmost_l(s, lo, hi - 1),\n decreases hi - lo,\n{\n if hi - 1 == lo {\n reveal_with_fuel(leftmost_l, 1);\n } else {\n reveal_with_fuel(leftmost_l, 2);\n if s[lo] == 1u8 {\n assert(leftmost_l(s, lo, hi) == lo);\n assert(leftmost_l(s, lo, hi - 1) == lo);\n } else {\n lemma_leftmost_l_unaffected_by_hi(s, lo + 1, hi);\n }\n }\n}\n\nproof fn lemma_leftmost_l_eq_hi_minus_1(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo < hi <= s.len(),\n s[hi - 1] == 1u8,\n leftmost_l(s, lo, hi - 1) == -1int,\n ensures\n leftmost_l(s, lo, hi) == hi - 1,\n decreases hi - lo,\n{\n if hi - 1 == lo {\n reveal_with_fuel(leftmost_l, 2);\n } else {\n reveal_with_fuel(leftmost_l, 2);\n if s[lo] == 1u8 {\n assert(leftmost_l(s, lo, hi - 1) == lo);\n assert(false);\n }\n lemma_leftmost_l_eq_hi_minus_1(s, lo + 1, hi);\n }\n}\n\n\nproof fn lemma_greedy_zero(a: Seq, s: Seq, lo: int, hi: int)\n requires\n a.len() == s.len(),\n 0 <= lo <= hi <= a.len(),\n hi <= lo + 1,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] == 1u8 || s[i] == 2u8,\n ensures\n greedy(a, s, lo, hi) == 0int,\n{\n reveal_with_fuel(greedy, 1);\n if lo >= hi {\n reveal_with_fuel(leftmost_l, 1);\n reveal_with_fuel(rightmost_r, 1);\n assert(leftmost_l(s, lo, hi) == -1int);\n assert(rightmost_r(s, lo, hi) == -1int);\n } else {\n \n assert(s[lo] == 1u8 || s[lo] == 2u8);\n if s[lo] == 1u8 {\n reveal_with_fuel(leftmost_l, 1);\n assert(leftmost_l(s, lo, hi) == lo);\n reveal_with_fuel(rightmost_r, 2);\n \n reveal_with_fuel(rightmost_r, 1);\n assert(rightmost_r(s, lo, lo) == -1int);\n assert(rightmost_r(s, lo, hi) == -1int);\n } else {\n assert(s[lo] == 2u8);\n reveal_with_fuel(leftmost_l, 2);\n \n reveal_with_fuel(leftmost_l, 1);\n assert(leftmost_l(s, lo + 1, lo + 1) == -1int);\n assert(leftmost_l(s, lo, hi) == -1int);\n reveal_with_fuel(rightmost_r, 1);\n assert(rightmost_r(s, lo, hi) == lo);\n }\n }\n}\n\n\nproof fn lemma_rightmost_r_skip(s: Seq, lo: int, hi: int)\n requires\n 0 <= lo < hi <= s.len(),\n s[hi - 1] != 2u8,\n ensures\n rightmost_r(s, lo, hi) == rightmost_r(s, lo, hi - 1),\n{\n reveal_with_fuel(rightmost_r, 2);\n}\n\n\n\n\n\n\n\n\n\n\n\n\nproof fn lemma_pref_sum_bound(a: Seq, idx: int)\n requires\n 0 <= idx <= a.len(),\n a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 100_000,\n ensures\n 0 <= pref_sum(a, idx) <= idx * 100_000,\n decreases idx,\n{\n if idx <= 0 {\n } else {\n lemma_pref_sum_bound(a, idx - 1);\n assert(1 <= a[idx - 1] <= 100_000);\n }\n}\n\n\nproof fn lemma_pref_sum_monotonic(a: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= a.len(),\n a.len() <= 200_000,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 100_000,\n ensures\n pref_sum(a, hi) >= pref_sum(a, lo),\n pref_sum(a, hi) - pref_sum(a, lo) <= (hi - lo) * 100_000,\n decreases hi - lo,\n{\n if hi == lo {\n } else {\n lemma_pref_sum_monotonic(a, lo, hi - 1);\n assert(1 <= a[hi - 1] <= 100_000);\n }\n}\n\n\nproof fn lemma_prefix_step(a: Seq, prefix: Seq, i: int)\n requires\n 0 <= i < a.len(),\n prefix.len() == i + 1,\n forall|j: int| 0 <= j <= i ==> #[trigger] prefix[j] as int == pref_sum(a, j),\n ensures\n prefix[i] as int + a[i] as int == pref_sum(a, i + 1),\n{\n reveal_with_fuel(pref_sum, 2);\n}\n\nimpl Solution {\n pub fn max_score(a: Vec, s: Vec) -> (result: i64)\n requires\n 2 <= a.len() <= 200_000,\n a.len() == s.len(),\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 100_000,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] == 1u8 || s[i] == 2u8,\n ensures\n result as int == greedy(a@, s@, 0, a.len() as int),\n {\n let n = a.len();\n let ghost na = a@;\n let ghost ns = s@;\n \n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0);\n proof {\n assert(prefix[0] as int == pref_sum(a@, 0));\n }\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == a.len(),\n a.len() <= 200_000,\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j] <= 100_000,\n prefix.len() == i + 1,\n forall|j: int| 0 <= j <= i as int ==> #[trigger] prefix[j] as int == pref_sum(a@, j),\n forall|j: int| 0 <= j <= i as int ==> 0 <= #[trigger] prefix[j] <= j * 100_000,\n decreases n - i,\n {\n proof {\n lemma_pref_sum_bound(a@, i as int);\n lemma_pref_sum_bound(a@, (i + 1) as int);\n lemma_prefix_step(a@, prefix@, i as int);\n assert((i + 1) as int * 100_000 <= 200_000 * 100_000) by(nonlinear_arith) requires (i + 1) as int <= 200_000;\n }\n let next = prefix[i] + a[i];\n prefix.push(next);\n i = i + 1;\n }\n \n let mut total: i64 = 0;\n let mut lo: usize = 0;\n let mut hi: usize = n;\n let mut keep_going: bool = true;\n while keep_going\n invariant\n 0 <= lo <= hi <= n,\n n == a.len(),\n a.len() == s.len(),\n a.len() <= 200_000,\n a.len() >= 2,\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j] <= 100_000,\n forall|j: int| 0 <= j < s.len() ==> #[trigger] s[j] == 1u8 || s[j] == 2u8,\n prefix.len() == n + 1,\n forall|j: int| 0 <= j <= n as int ==> #[trigger] prefix[j] as int == pref_sum(a@, j),\n forall|j: int| 0 <= j <= n as int ==> 0 <= #[trigger] prefix[j] <= j * 100_000,\n total as int + greedy(a@, s@, lo as int, hi as int) == greedy(a@, s@, 0, n as int),\n 0 <= total as int,\n 2 * (total as int) <= ((lo as int) + ((n as int) - (hi as int))) * (n as int) * 100_000,\n !keep_going ==> hi <= lo + 1 && total as int == greedy(a@, s@, 0, n as int),\n decreases hi - lo + (if keep_going { 1int } else { 0int }),\n {\n \n let ghost orig_lo = lo;\n let ghost orig_hi_outer = hi;\n let ghost orig_total = total;\n while lo < hi && s[lo] != 1\n invariant\n orig_lo <= lo <= hi <= n,\n hi == orig_hi_outer,\n total == orig_total,\n n == s.len(),\n a.len() == s.len(),\n forall|j: int| 0 <= j < s.len() ==> #[trigger] s[j] == 1u8 || s[j] == 2u8,\n leftmost_l(s@, orig_lo as int, hi as int) == leftmost_l(s@, lo as int, hi as int),\n greedy(a@, s@, orig_lo as int, hi as int) == greedy(a@, s@, lo as int, hi as int),\n 2 * (total as int) <= ((lo as int) + ((n as int) - (hi as int))) * (n as int) * 100_000,\n decreases hi - lo,\n {\n proof {\n lemma_leftmost_l_skip(s@, lo as int, hi as int);\n lemma_greedy_skip_l(a@, s@, lo as int, hi as int);\n \n let cur_pos = (lo as int) + ((n as int) - (hi as int));\n let new_pos = ((lo as int) + 1) + ((n as int) - (hi as int));\n assert(new_pos == cur_pos + 1);\n assert(new_pos * (n as int) * 100_000 >= cur_pos * (n as int) * 100_000) by(nonlinear_arith)\n requires new_pos == cur_pos + 1, n as int >= 0;\n }\n lo = lo + 1;\n }\n proof {\n \n if lo == hi {\n \n reveal_with_fuel(leftmost_l, 1);\n assert(leftmost_l(s@, lo as int, hi as int) == -1int);\n } else {\n assert(s@[lo as int] == 1u8);\n reveal_with_fuel(leftmost_l, 1);\n assert(leftmost_l(s@, lo as int, hi as int) == lo as int);\n }\n assert(leftmost_l(s@, orig_lo as int, hi as int) == leftmost_l(s@, lo as int, hi as int));\n }\n \n let ghost orig_hi = hi;\n let ghost orig_lo_2 = lo;\n let ghost orig_total_2 = total;\n while lo < hi && s[hi - 1] != 2\n invariant\n lo <= hi <= orig_hi <= n,\n lo == orig_lo_2,\n total == orig_total_2,\n n == s.len(),\n a.len() == s.len(),\n forall|j: int| 0 <= j < s.len() ==> #[trigger] s[j] == 1u8 || s[j] == 2u8,\n rightmost_r(s@, lo as int, orig_hi as int) == rightmost_r(s@, lo as int, hi as int),\n greedy(a@, s@, lo as int, orig_hi as int) == greedy(a@, s@, lo as int, hi as int),\n 2 * (total as int) <= ((lo as int) + ((n as int) - (hi as int))) * (n as int) * 100_000,\n decreases hi,\n {\n proof {\n lemma_rightmost_r_skip(s@, lo as int, hi as int);\n lemma_greedy_skip_r(a@, s@, lo as int, hi as int);\n let cur_pos = (lo as int) + ((n as int) - (hi as int));\n let new_pos = (lo as int) + ((n as int) - ((hi as int) - 1));\n assert(new_pos == cur_pos + 1);\n assert(new_pos * (n as int) * 100_000 >= cur_pos * (n as int) * 100_000) by(nonlinear_arith)\n requires new_pos == cur_pos + 1, n as int >= 0;\n }\n hi = hi - 1;\n }\n proof {\n if lo == hi {\n \n reveal_with_fuel(rightmost_r, 1);\n assert(rightmost_r(s@, lo as int, hi as int) == -1int);\n } else {\n assert(s@[(hi - 1) as int] == 2u8);\n reveal_with_fuel(rightmost_r, 1);\n assert(rightmost_r(s@, lo as int, hi as int) == (hi - 1) as int);\n }\n assert(rightmost_r(s@, lo as int, orig_hi as int) == rightmost_r(s@, lo as int, hi as int));\n }\n \n if lo + 1 < hi {\n let ghost old_lo = lo;\n let ghost old_hi = hi;\n let ghost old_total = total;\n proof {\n reveal_with_fuel(greedy, 2);\n let l = leftmost_l(s@, lo as int, hi as int);\n let r = rightmost_r(s@, lo as int, hi as int);\n assert(l == lo as int);\n assert(r == (hi - 1) as int);\n assert(l < r);\n assert(greedy(a@, s@, lo as int, hi as int) ==\n (pref_sum(a@, r + 1) - pref_sum(a@, l)) + greedy(a@, s@, l + 1, r));\n assert(prefix[hi as int] as int == pref_sum(a@, hi as int));\n assert(prefix[lo as int] as int == pref_sum(a@, lo as int));\n assert(prefix[hi as int] <= hi as int * 100_000);\n assert((hi as int) * 100_000 <= 200_000 * 100_000) by(nonlinear_arith) requires hi as int <= 200_000;\n assert(prefix[lo as int] >= 0);\n \n \n assert(2 * (total as int) <= ((lo as int) + ((n as int) - (hi as int))) * (n as int) * 100_000);\n assert((lo as int) + ((n as int) - (hi as int)) <= (n as int)) by(nonlinear_arith)\n requires lo as int <= hi as int, hi as int <= n as int;\n assert(((lo as int) + ((n as int) - (hi as int))) * (n as int) <= (n as int) * (n as int)) by(nonlinear_arith)\n requires (lo as int) + ((n as int) - (hi as int)) <= (n as int), n as int >= 0;\n assert((n as int) * (n as int) <= 200_000 * 200_000) by(nonlinear_arith) requires n as int <= 200_000;\n assert(2 * (total as int) <= 200_000 * 200_000 * 100_000);\n assert(total as int <= 100_000 * 200_000 * 200_000);\n assert(total as int + prefix[hi as int] as int <= 100_000 * 200_000 * 200_000 + 200_000 * 100_000);\n }\n total = total + prefix[hi] - prefix[lo];\n lo = lo + 1;\n hi = hi - 1;\n proof {\n assert(total as int == old_total as int + prefix[old_hi as int] as int - prefix[old_lo as int] as int);\n assert(prefix[old_hi as int] as int == pref_sum(a@, old_hi as int));\n assert(prefix[old_lo as int] as int == pref_sum(a@, old_lo as int));\n lemma_pref_sum_monotonic(a@, old_lo as int, old_hi as int);\n assert(pref_sum(a@, old_hi as int) >= pref_sum(a@, old_lo as int));\n assert(0 <= total as int);\n assert(pref_sum(a@, old_hi as int) - pref_sum(a@, old_lo as int) <= (old_hi as int - old_lo as int) * 100_000);\n assert((old_hi as int - old_lo as int) * 100_000 <= (n as int) * 100_000) by(nonlinear_arith)\n requires old_hi as int - old_lo as int <= n as int;\n let old_pos = (old_lo as int) + (n as int) - (old_hi as int);\n let new_pos = (lo as int) + (n as int) - (hi as int);\n assert(new_pos == old_pos + 2);\n assert(2 * (total as int) == 2 * (old_total as int) + 2 * (prefix[old_hi as int] as int - prefix[old_lo as int] as int));\n assert(2 * (old_total as int) <= old_pos * (n as int) * 100_000);\n assert(2 * (total as int) <= old_pos * (n as int) * 100_000 + 2 * (n as int) * 100_000);\n assert((old_pos + 2) * (n as int) * 100_000 == old_pos * (n as int) * 100_000 + 2 * (n as int) * 100_000) by(nonlinear_arith);\n assert(2 * (total as int) <= new_pos * (n as int) * 100_000);\n \n reveal_with_fuel(greedy, 2);\n let l = leftmost_l(s@, old_lo as int, old_hi as int);\n let r = rightmost_r(s@, old_lo as int, old_hi as int);\n assert(l == old_lo as int);\n assert(r == (old_hi - 1) as int);\n assert(l < r);\n assert(greedy(a@, s@, old_lo as int, old_hi as int) ==\n (pref_sum(a@, r + 1) - pref_sum(a@, l)) + greedy(a@, s@, l + 1, r));\n assert(greedy(a@, s@, old_lo as int, old_hi as int) ==\n (pref_sum(a@, old_hi as int) - pref_sum(a@, old_lo as int)) + greedy(a@, s@, old_lo as int + 1, old_hi as int - 1));\n assert(lo as int == old_lo as int + 1);\n assert(hi as int == old_hi as int - 1);\n assert(greedy(a@, s@, lo as int, hi as int) == greedy(a@, s@, old_lo as int + 1, old_hi as int - 1));\n \n \n \n \n \n }\n } else {\n proof {\n assert(hi <= lo + 1);\n lemma_greedy_zero(a@, s@, lo as int, hi as int);\n assert(greedy(a@, s@, lo as int, hi as int) == 0int);\n }\n keep_going = false;\n }\n }\n proof {\n assert(!keep_going);\n assert(total as int == greedy(a@, s@, 0, n as int));\n }\n total\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_score(a: Vec, s: Vec) -> i64 {\n let n = a.len();\n let mut prefix: Vec = Vec::with_capacity(n + 1);\n prefix.push(0);\n let mut i: usize = 0;\n while i < n {\n let next = prefix[i] + a[i];\n prefix.push(next);\n i = i + 1;\n }\n let mut total: i64 = 0;\n let mut lo: usize = 0;\n let mut hi: usize = n;\n let mut keep_going: bool = true;\n while keep_going {\n while lo < hi && s[lo] != 1 {\n lo = lo + 1;\n }\n while lo < hi && s[hi - 1] != 2 {\n hi = hi - 1;\n }\n if lo + 1 < hi {\n total = total + prefix[hi] - prefix[lo];\n lo = lo + 1;\n hi = hi - 1;\n } else {\n keep_going = false;\n }\n }\n total\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut it = input.split_ascii_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut tc: usize = 0;\n while tc < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n a.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let s_str = it.next().unwrap();\n let s: Vec = s_str.bytes().map(|b| match b {\n b'L' => 1u8,\n b'R' => 2u8,\n _ => 0u8,\n }).collect();\n let _ = n;\n let ans = Solution::max_score(a, s);\n writeln!(out, \"{}\", ans).unwrap();\n tc = tc + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf2000D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf2000D/tests/mutated_testcases.jsonl"} {"problem": "cf2014D", "family": "codeforces", "description": "# Robert Hood and Mrs Hood\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\n*Impress thy brother, yet fret not thy mother.*\nRobin's brother and mother are visiting, and Robin gets to choose the start day for each visitor.\n\nAll days are numbered from $$$1$$$ to $$$n$$$. Visitors stay for $$$d$$$ continuous days, all of those $$$d$$$ days must be between day $$$1$$$ and $$$n$$$ inclusive.\n\nRobin has a total of $$$k$$$ risky 'jobs' planned. The $$$i$$$-th job takes place between days $$$l_i$$$ and $$$r_i$$$ inclusive, for $$$1 \\le i \\le k$$$. If a job takes place on any of the $$$d$$$ days, the visit overlaps with this job (the length of overlap is unimportant).\n\nRobin wants his brother's visit to overlap with the maximum number of **distinct jobs**, and his mother's the minimum.\n\nFind suitable start days for the visits of Robin's brother and mother. If there are multiple suitable days, choose the earliest one.\n\n## Input\n\nThe first line of the input contains a single integer $$$t$$$ ($$$1\\leq t \\leq 10^4$$$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case consists of three integers $$$n$$$, $$$d$$$, $$$k$$$ ($$$1 \\le n \\le 10^5, 1 \\le d, k \\le n$$$)\u00a0\u2014 the number of total days, duration of the visits, and the number of jobs. \n\nThen follow $$$k$$$ lines of each test case, each with two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$)\u00a0\u2014 the start and end day of each job.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, output two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $$$1$$$ and $$$n$$$ inclusive.\n\n## Example\n\n**Input:**\n```\n6\n2 1 1\n1 2\n4 1 2\n1 2\n2 4\n7 2 3\n1 2\n1 3\n6 7\n5 1 2\n1 2\n3 5\n9 2 1\n2 8\n9 2 4\n7 9\n4 8\n1 3\n2 3\n```\n**Output:**\n```\n1 1\n2 1\n1 4\n1 1\n1 1\n3 4\n```\n\n## Note\n\nIn the first test case, the only job fills all $$$2$$$ days, both should visit on day $$$1$$$.\n\nIn the second test case, day $$$2$$$ overlaps with $$$2$$$ jobs and day $$$1$$$ overlaps with only $$$1$$$.\n\nIn the third test case, Robert visits for days $$$[1,2]$$$, Mrs. Hood visits for days $$$[4,5]$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nfn next_i32<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nimpl Solution {\n pub fn best_start_days(n: i32, d: i32, left: Vec, right: Vec) -> (i32, i32) {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = match next_i32(&mut it) {\n Some(v) => v as usize,\n None => return,\n };\n\n let mut out = String::new();\n let mut case_id: usize = 0;\n while case_id < t {\n let n: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let d: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let k: usize = match next_i32(&mut it) {\n Some(v) => v as usize,\n None => return,\n };\n\n let mut left: Vec = Vec::new();\n let mut right: Vec = Vec::new();\n let mut i: usize = 0;\n while i < k {\n let l: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let r: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n left.push(l);\n right.push(r);\n i = i + 1;\n }\n\n let ans = Solution::best_start_days(n, d, left, right);\n out.push_str(&format!(\"{} {}\\n\", ans.0, ans.1));\n\n case_id = case_id + 1;\n }\n\n print!(\"{}\", out);\n}\n```\n", "tags": "Rating: 1400\nbrute force, data structures, greedy, sortings\nSolved by: N/A users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_overlaps_window(start: int, d: int, l: int, r: int) -> bool {\n start <= r && l <= start + d - 1\n }\n\n pub open spec fn spec_overlap_count_prefix(start: int, d: int, left: Seq, right: Seq, upto: int) -> int\n recommends\n 0 <= upto <= left.len(),\n left.len() == right.len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::spec_overlap_count_prefix(start, d, left, right, upto - 1)\n + if Self::spec_overlaps_window(start, d, left[upto - 1] as int, right[upto - 1] as int) {\n 1int\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn spec_overlap_count(start: int, d: int, left: Seq, right: Seq) -> int\n recommends\n left.len() == right.len(),\n {\n Self::spec_overlap_count_prefix(start, d, left, right, left.len() as int)\n }\n\n pub fn best_start_days(n: i32, d: i32, left: Vec, right: Vec) -> (res: (i32, i32))\n requires\n 1 <= n,\n n <= 100000,\n 1 <= d <= n,\n 1 <= left.len() <= n as nat,\n left.len() == right.len(),\n forall|j: int| 0 <= j < left.len() as int ==> 1 <= #[trigger] left[j] <= right[j] <= n,\n ensures\n 1 <= res.0 <= n - d + 1,\n 1 <= res.1 <= n - d + 1,\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n ==> Self::spec_overlap_count(res.0 as int, d as int, left@, right@)\n >= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n ==> Self::spec_overlap_count(res.1 as int, d as int, left@, right@)\n <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(res.0 as int, d as int, left@, right@)\n ==> res.0 as int <= s,\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(res.1 as int, d as int, left@, right@)\n ==> res.1 as int <= s,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn overlaps_window(start: i32, d: i32, l: i32, r: i32) -> bool\n {\n (start as i64) <= (r as i64) && (l as i64) <= (start as i64) + (d as i64) - 1\n }\n\n pub fn overlap_count(start: i32, d: i32, left: &Vec, right: &Vec) -> i32\n {\n let mut cnt: i32 = 0;\n let mut j: usize = 0;\n while j < left.len()\n {\n if Self::overlaps_window(start, d, left[j], right[j]) {\n cnt = cnt + 1;\n }\n j = j + 1;\n }\n cnt\n }\n\n pub fn best_start_days(n: i32, d: i32, left: Vec, right: Vec) -> (i32, i32)\n {\n let m = n - d + 1;\n let mm = m as usize;\n\n let mut diff: Vec = Vec::with_capacity(mm + 2);\n let mut p: usize = 0;\n while p < mm + 2\n {\n diff.push(0);\n p += 1;\n }\n\n let mut j: usize = 0;\n while j < left.len()\n {\n let l = left[j];\n let r = right[j];\n let lo = if l - d + 1 > 1 { l - d + 1 } else { 1 };\n let hi = if r < m { r } else { m };\n if lo <= hi {\n let li = lo as usize;\n let hi1 = (hi + 1) as usize;\n diff[li] = diff[li] + 1;\n diff[hi1] = diff[hi1] - 1;\n } \n j += 1;\n }\n\n let mut best_bro: i32 = 1;\n let mut best_mom: i32 = 1;\n let mut best_bro_count: i32 = i32::MIN;\n let mut best_mom_count: i32 = i32::MAX;\n\n let mut cur: i32 = 0;\n let mut start: usize = 1;\n while start <= mm\n {\n cur += diff[start];\n\n let old_best_bro = best_bro;\n let old_best_mom = best_mom;\n let old_best_bro_count = best_bro_count;\n let old_best_mom_count = best_mom_count;\n\n if cur > best_bro_count {\n best_bro_count = cur;\n best_bro = start as i32;\n }\n if cur < best_mom_count {\n best_mom_count = cur;\n best_mom = start as i32;\n }\n\n start += 1;\n }\n\n (best_bro, best_mom)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_overlaps_window(start: int, d: int, l: int, r: int) -> bool {\n start <= r && l <= start + d - 1\n }\n\n pub open spec fn spec_overlap_count_prefix(start: int, d: int, left: Seq, right: Seq, upto: int) -> int\n recommends\n 0 <= upto <= left.len(),\n left.len() == right.len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::spec_overlap_count_prefix(start, d, left, right, upto - 1)\n + if Self::spec_overlaps_window(start, d, left[upto - 1] as int, right[upto - 1] as int) {\n 1int\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn spec_overlap_count(start: int, d: int, left: Seq, right: Seq) -> int\n recommends\n left.len() == right.len(),\n {\n Self::spec_overlap_count_prefix(start, d, left, right, left.len() as int)\n }\n\n pub fn overlaps_window(start: i32, d: i32, l: i32, r: i32) -> (res: bool)\n requires\n 1 <= d,\n ensures\n res == Self::spec_overlaps_window(start as int, d as int, l as int, r as int),\n {\n (start as i64) <= (r as i64) && (l as i64) <= (start as i64) + (d as i64) - 1\n }\n\n pub fn overlap_count(start: i32, d: i32, left: &Vec, right: &Vec) -> (res: i32)\n requires\n 1 <= d,\n left.len() == right.len(),\n left.len() <= i32::MAX,\n forall|j: int| 0 <= j < left.len() as int ==> 1 <= #[trigger] left[j] <= right[j],\n ensures\n res as int == Self::spec_overlap_count(start as int, d as int, left@, right@),\n 0 <= res <= left.len() as i32,\n {\n let mut cnt: i32 = 0;\n let mut j: usize = 0;\n while j < left.len()\n {\n if Self::overlaps_window(start, d, left[j], right[j]) {\n cnt = cnt + 1;\n }\n j = j + 1;\n }\n cnt\n }\n\n pub fn best_start_days(n: i32, d: i32, left: Vec, right: Vec) -> (res: (i32, i32))\n requires\n 1 <= n,\n n <= 100000,\n 1 <= d <= n,\n 1 <= left.len() <= n as nat,\n left.len() == right.len(),\n forall|j: int| 0 <= j < left.len() as int ==> 1 <= #[trigger] left[j] <= right[j] <= n,\n ensures\n 1 <= res.0 <= n - d + 1,\n 1 <= res.1 <= n - d + 1,\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n ==> Self::spec_overlap_count(res.0 as int, d as int, left@, right@)\n >= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n ==> Self::spec_overlap_count(res.1 as int, d as int, left@, right@)\n <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(res.0 as int, d as int, left@, right@)\n ==> res.0 as int <= s,\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(res.1 as int, d as int, left@, right@)\n ==> res.1 as int <= s,\n {\n let m = n - d + 1;\n let mm = m as usize;\n\n let mut diff: Vec = Vec::with_capacity(mm + 2);\n let mut p: usize = 0;\n while p < mm + 2\n {\n diff.push(0);\n p += 1;\n }\n\n let mut j: usize = 0;\n while j < left.len()\n {\n let l = left[j];\n let r = right[j];\n let lo = if l - d + 1 > 1 { l - d + 1 } else { 1 };\n let hi = if r < m { r } else { m };\n if lo <= hi {\n let li = lo as usize;\n let hi1 = (hi + 1) as usize;\n diff[li] = diff[li] + 1;\n diff[hi1] = diff[hi1] - 1;\n } \n j += 1;\n }\n\n let mut best_bro: i32 = 1;\n let mut best_mom: i32 = 1;\n let mut best_bro_count: i32 = i32::MIN;\n let mut best_mom_count: i32 = i32::MAX;\n\n let mut cur: i32 = 0;\n let mut start: usize = 1;\n while start <= mm\n {\n cur += diff[start];\n\n let old_best_bro = best_bro;\n let old_best_mom = best_mom;\n let old_best_bro_count = best_bro_count;\n let old_best_mom_count = best_mom_count;\n\n if cur > best_bro_count {\n best_bro_count = cur;\n best_bro = start as i32;\n }\n if cur < best_mom_count {\n best_mom_count = cur;\n best_mom = start as i32;\n }\n\n start += 1;\n }\n\n (best_bro, best_mom)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_overlaps_window(start: int, d: int, l: int, r: int) -> bool {\n start <= r && l <= start + d - 1\n }\n\n pub open spec fn spec_overlap_count_prefix(start: int, d: int, left: Seq, right: Seq, upto: int) -> int\n recommends\n 0 <= upto <= left.len(),\n left.len() == right.len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::spec_overlap_count_prefix(start, d, left, right, upto - 1)\n + if Self::spec_overlaps_window(start, d, left[upto - 1] as int, right[upto - 1] as int) {\n 1int\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn spec_overlap_count(start: int, d: int, left: Seq, right: Seq) -> int\n recommends\n left.len() == right.len(),\n {\n Self::spec_overlap_count_prefix(start, d, left, right, left.len() as int)\n }\n\n pub open spec fn spec_diff_prefix_sum(diff: Seq, start: int) -> int\n decreases start,\n {\n if start <= 0 {\n 0\n } else {\n Self::spec_diff_prefix_sum(diff, start - 1) + diff[start] as int\n }\n }\n\n proof fn lemma_diff_prefix_sum_all_zero(diff: Seq, start: int)\n requires\n 0 <= start < diff.len(),\n forall|k: int| 0 <= k < diff.len() ==> diff[k] == 0,\n ensures\n Self::spec_diff_prefix_sum(diff, start) == 0,\n decreases start,\n {\n if start > 0 {\n Self::lemma_diff_prefix_sum_all_zero(diff, start - 1);\n }\n }\n\n proof fn lemma_diff_prefix_sum_point_update(old_diff: Seq, new_diff: Seq, li: int, hi1: int, start: int)\n requires\n 1 <= li < hi1 < old_diff.len(),\n new_diff.len() == old_diff.len(),\n 0 <= start < old_diff.len(),\n forall|p: int| #![trigger new_diff[p]] 0 <= p < old_diff.len() && p != li && p != hi1 ==> new_diff[p] == old_diff[p],\n new_diff[li] == old_diff[li] + 1,\n new_diff[hi1] == old_diff[hi1] - 1,\n ensures\n Self::spec_diff_prefix_sum(new_diff, start) ==\n Self::spec_diff_prefix_sum(old_diff, start)\n + (if li <= start < hi1 { 1int } else { 0int }),\n decreases start,\n {\n if start > 0 {\n Self::lemma_diff_prefix_sum_point_update(old_diff, new_diff, li, hi1, start - 1);\n if start == li {\n assert(new_diff[start] == old_diff[start] + 1);\n } else if start == hi1 {\n assert(new_diff[start] == old_diff[start] - 1);\n } else {\n assert(new_diff[start] == old_diff[start]);\n }\n }\n }\n\n proof fn lemma_overlap_count_prefix_bounds(start: int, d: int, left: Seq, right: Seq, upto: int)\n requires\n 0 <= upto <= left.len(),\n left.len() == right.len(),\n ensures\n 0 <= Self::spec_overlap_count_prefix(start, d, left, right, upto) <= upto,\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_overlap_count_prefix_bounds(start, d, left, right, upto - 1);\n }\n }\n\n pub fn overlaps_window(start: i32, d: i32, l: i32, r: i32) -> (res: bool)\n requires\n 1 <= d,\n ensures\n res == Self::spec_overlaps_window(start as int, d as int, l as int, r as int),\n {\n (start as i64) <= (r as i64) && (l as i64) <= (start as i64) + (d as i64) - 1\n }\n\n pub fn overlap_count(start: i32, d: i32, left: &Vec, right: &Vec) -> (res: i32)\n requires\n 1 <= d,\n left.len() == right.len(),\n left.len() <= i32::MAX,\n forall|j: int| 0 <= j < left.len() as int ==> 1 <= #[trigger] left[j] <= right[j],\n ensures\n res as int == Self::spec_overlap_count(start as int, d as int, left@, right@),\n 0 <= res <= left.len() as i32,\n {\n let mut cnt: i32 = 0;\n let mut j: usize = 0;\n while j < left.len()\n invariant\n 1 <= d,\n left.len() == right.len(),\n left.len() <= i32::MAX,\n forall|x: int| 0 <= x < left.len() as int ==> 1 <= #[trigger] left[x] <= right[x],\n 0 <= j <= left.len(),\n 0 <= cnt <= j as i32,\n cnt as int == Self::spec_overlap_count_prefix(start as int, d as int, left@, right@, j as int),\n decreases left.len() - j,\n {\n if Self::overlaps_window(start, d, left[j], right[j]) {\n cnt = cnt + 1;\n }\n proof {\n assert(Self::spec_overlap_count_prefix(start as int, d as int, left@, right@, j as int + 1)\n == Self::spec_overlap_count_prefix(start as int, d as int, left@, right@, j as int)\n + if Self::spec_overlaps_window(start as int, d as int, left[j as int] as int, right[j as int] as int) { 1int } else { 0int });\n }\n j = j + 1;\n }\n cnt\n }\n\n pub fn best_start_days(n: i32, d: i32, left: Vec, right: Vec) -> (res: (i32, i32))\n requires\n 1 <= n,\n n <= 100000,\n 1 <= d <= n,\n 1 <= left.len() <= n as nat,\n left.len() == right.len(),\n forall|j: int| 0 <= j < left.len() as int ==> 1 <= #[trigger] left[j] <= right[j] <= n,\n ensures\n 1 <= res.0 <= n - d + 1,\n 1 <= res.1 <= n - d + 1,\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n ==> Self::spec_overlap_count(res.0 as int, d as int, left@, right@)\n >= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n ==> Self::spec_overlap_count(res.1 as int, d as int, left@, right@)\n <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(res.0 as int, d as int, left@, right@)\n ==> res.0 as int <= s,\n forall|s: int|\n 1 <= s <= n as int - d as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(res.1 as int, d as int, left@, right@)\n ==> res.1 as int <= s,\n {\n let m = n - d + 1;\n let mm = m as usize;\n assert(1 <= m <= 100000);\n assert(mm <= 100000);\n\n let mut diff: Vec = Vec::with_capacity(mm + 2);\n let mut p: usize = 0;\n while p < mm + 2\n invariant\n mm <= 100000,\n p <= mm + 2,\n diff.len() == p,\n forall|k: int| 0 <= k < p as int ==> diff[k] == 0,\n decreases mm + 2 - p,\n {\n diff.push(0);\n p += 1;\n }\n\n proof {\n assert forall|start: int| 1 <= start <= mm as int implies\n Self::spec_diff_prefix_sum(diff@, start) == 0\n by {\n Self::lemma_diff_prefix_sum_all_zero(diff@, start);\n }\n }\n\n let mut j: usize = 0;\n while j < left.len()\n invariant\n 1 <= d <= n,\n n <= 100000,\n m == n - d + 1,\n mm == m as usize,\n 1 <= mm,\n left.len() == right.len(),\n 1 <= left.len() <= n as nat,\n forall|k: int| 0 <= k < left.len() as int ==> 1 <= #[trigger] left[k] <= right[k] <= n,\n diff.len() == mm + 2,\n 0 <= j <= left.len(),\n forall|k: int| 0 <= k < diff.len() as int ==> -(j as int) <= #[trigger] diff[k] <= j as int,\n forall|start: int| 1 <= start <= mm as int ==>\n Self::spec_diff_prefix_sum(diff@, start)\n == Self::spec_overlap_count_prefix(start, d as int, left@, right@, j as int),\n decreases left.len() - j,\n {\n let l = left[j];\n let r = right[j];\n let lo = if l - d + 1 > 1 { l - d + 1 } else { 1 };\n let hi = if r < m { r } else { m };\n if lo <= hi {\n let li = lo as usize;\n let hi1 = (hi + 1) as usize;\n let ghost old_diff = diff@;\n diff[li] = diff[li] + 1;\n diff[hi1] = diff[hi1] - 1;\n proof {\n assert forall|start: int| 1 <= start <= mm as int implies\n Self::spec_diff_prefix_sum(diff@, start)\n == Self::spec_overlap_count_prefix(start, d as int, left@, right@, j as int + 1)\n by {\n Self::lemma_diff_prefix_sum_point_update(old_diff, diff@, li as int, hi1 as int, start);\n }\n }\n } \n j += 1;\n }\n\n proof {\n assert forall|start: int| 1 <= start <= mm as int implies\n 0 <= #[trigger] Self::spec_overlap_count(start, d as int, left@, right@) <= left.len() as int\n by {\n Self::lemma_overlap_count_prefix_bounds(start, d as int, left@, right@, left.len() as int);\n }\n }\n assert(diff.len() == mm + 2);\n assert(forall|k: int| 0 <= k < diff.len() as int ==> -(left.len() as int) <= #[trigger] diff[k] <= left.len() as int);\n\n let mut best_bro: i32 = 1;\n let mut best_mom: i32 = 1;\n let mut best_bro_count: i32 = i32::MIN;\n let mut best_mom_count: i32 = i32::MAX;\n\n let mut cur: i32 = 0;\n let mut start: usize = 1;\n while start <= mm\n invariant\n mm == m as usize,\n 1 <= mm <= 100000,\n 1 <= left.len() <= 100000,\n diff.len() == mm + 2,\n 1 <= start <= mm + 1,\n forall|s: int| 1 <= s <= mm as int ==>\n Self::spec_diff_prefix_sum(diff@, s) == Self::spec_overlap_count(s, d as int, left@, right@),\n forall|s: int| 1 <= s <= mm as int ==>\n 0 <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@) <= left.len() as int,\n forall|k: int| 0 <= k < diff.len() as int ==> -(left.len() as int) <= #[trigger] diff[k] <= left.len() as int,\n cur as int == Self::spec_diff_prefix_sum(diff@, start as int - 1),\n (start == 1 && best_bro_count == i32::MIN && best_mom_count == i32::MAX && best_bro == 1 && best_mom == 1)\n ||\n (start > 1\n && 1 <= best_bro < start\n && 1 <= best_mom < start\n && best_bro_count as int == Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n && best_mom_count as int == Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n && (forall|s: int| 1 <= s < start as int ==>\n Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n >= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@))\n && (forall|s: int| 1 <= s < start as int ==>\n Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@))\n && (forall|s: int| 1 <= s < start as int\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n ==> best_bro as int <= s)\n && (forall|s: int| 1 <= s < start as int\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n ==> best_mom as int <= s)\n ),\n decreases mm + 1 - start,\n {\n proof {\n if start > 1 {\n assert(0 <= Self::spec_overlap_count(start as int - 1, d as int, left@, right@) <= left.len() as int);\n assert(Self::spec_diff_prefix_sum(diff@, start as int - 1)\n == Self::spec_overlap_count(start as int - 1, d as int, left@, right@));\n }\n assert(0 <= Self::spec_overlap_count(start as int, d as int, left@, right@) <= left.len() as int);\n assert(Self::spec_diff_prefix_sum(diff@, start as int)\n == Self::spec_overlap_count(start as int, d as int, left@, right@));\n assert(Self::spec_diff_prefix_sum(diff@, start as int)\n == Self::spec_diff_prefix_sum(diff@, start as int - 1) + diff@[start as int] as int);\n assert(diff@[start as int] as int\n == Self::spec_diff_prefix_sum(diff@, start as int) - Self::spec_diff_prefix_sum(diff@, start as int - 1));\n assert(-(left.len() as int) <= diff[start as int] <= left.len() as int);\n }\n cur += diff[start];\n\n proof {\n assert(cur as int == Self::spec_overlap_count(start as int, d as int, left@, right@));\n }\n\n let old_best_bro = best_bro;\n let old_best_mom = best_mom;\n let old_best_bro_count = best_bro_count;\n let old_best_mom_count = best_mom_count;\n\n if cur > best_bro_count {\n best_bro_count = cur;\n best_bro = start as i32;\n }\n if cur < best_mom_count {\n best_mom_count = cur;\n best_mom = start as i32;\n }\n\n proof {\n if start == 1 {\n assert(best_bro_count as int == Self::spec_overlap_count(best_bro as int, d as int, left@, right@));\n assert(best_mom_count as int == Self::spec_overlap_count(best_mom as int, d as int, left@, right@));\n assert forall|s: int| 1 <= s < start as int + 1 implies\n Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n >= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n by {\n assert(s == start as int);\n };\n assert forall|s: int| 1 <= s < start as int + 1 implies\n Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n by {\n assert(s == start as int);\n };\n assert forall|s: int| 1 <= s < start as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n implies best_bro as int <= s\n by {\n assert(s == start as int);\n };\n assert forall|s: int| 1 <= s < start as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n implies best_mom as int <= s\n by {\n assert(s == start as int);\n };\n } else {\n if cur > old_best_bro_count {\n assert(best_bro == start as i32);\n assert(best_bro_count == cur);\n } else {\n assert(best_bro == old_best_bro);\n assert(best_bro_count == old_best_bro_count);\n }\n if cur < old_best_mom_count {\n assert(best_mom == start as i32);\n assert(best_mom_count == cur);\n } else {\n assert(best_mom == old_best_mom);\n assert(best_mom_count == old_best_mom_count);\n }\n\n assert forall|s: int| 1 <= s < start as int + 1 implies\n Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n >= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n by {\n if s < start as int {\n assert(Self::spec_overlap_count(old_best_bro as int, d as int, left@, right@)\n >= Self::spec_overlap_count(s, d as int, left@, right@));\n } else {\n assert(s == start as int);\n }\n };\n assert forall|s: int| 1 <= s < start as int + 1 implies\n Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n <= #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n by {\n if s < start as int {\n assert(Self::spec_overlap_count(old_best_mom as int, d as int, left@, right@)\n <= Self::spec_overlap_count(s, d as int, left@, right@));\n } else {\n assert(s == start as int);\n }\n };\n assert forall|s: int| 1 <= s < start as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(best_bro as int, d as int, left@, right@)\n implies best_bro as int <= s\n by {\n if s < start as int {\n if cur > old_best_bro_count {\n assert(Self::spec_overlap_count(s, d as int, left@, right@)\n <= Self::spec_overlap_count(old_best_bro as int, d as int, left@, right@));\n assert(false);\n } else {\n assert(old_best_bro as int <= s);\n }\n }\n };\n assert forall|s: int| 1 <= s < start as int + 1\n && #[trigger] Self::spec_overlap_count(s, d as int, left@, right@)\n == Self::spec_overlap_count(best_mom as int, d as int, left@, right@)\n implies best_mom as int <= s\n by {\n if s < start as int {\n if cur < old_best_mom_count {\n assert(Self::spec_overlap_count(s, d as int, left@, right@)\n >= Self::spec_overlap_count(old_best_mom as int, d as int, left@, right@));\n assert(false);\n } else {\n assert(old_best_mom as int <= s);\n }\n }\n };\n }\n }\n\n start += 1;\n }\n\n (best_bro, best_mom)\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nfn next_i32<'a, I: Iterator>(it: &mut I) -> Option {\n it.next()?.parse().ok()\n}\n\nimpl Solution {\n pub fn overlaps_window(start: i32, d: i32, l: i32, r: i32) -> bool\n {\n (start as i64) <= (r as i64) && (l as i64) <= (start as i64) + (d as i64) - 1\n }\n\n pub fn overlap_count(start: i32, d: i32, left: &Vec, right: &Vec) -> i32\n {\n let mut cnt: i32 = 0;\n let mut j: usize = 0;\n while j < left.len()\n {\n if Self::overlaps_window(start, d, left[j], right[j]) {\n cnt = cnt + 1;\n }\n j = j + 1;\n }\n cnt\n }\n\n pub fn best_start_days(n: i32, d: i32, left: Vec, right: Vec) -> (i32, i32)\n {\n let m = n - d + 1;\n let mm = m as usize;\n\n let mut diff: Vec = Vec::with_capacity(mm + 2);\n let mut p: usize = 0;\n while p < mm + 2\n {\n diff.push(0);\n p += 1;\n }\n\n let mut j: usize = 0;\n while j < left.len()\n {\n let l = left[j];\n let r = right[j];\n let lo = if l - d + 1 > 1 { l - d + 1 } else { 1 };\n let hi = if r < m { r } else { m };\n if lo <= hi {\n let li = lo as usize;\n let hi1 = (hi + 1) as usize;\n diff[li] = diff[li] + 1;\n diff[hi1] = diff[hi1] - 1;\n } \n j += 1;\n }\n\n let mut best_bro: i32 = 1;\n let mut best_mom: i32 = 1;\n let mut best_bro_count: i32 = i32::MIN;\n let mut best_mom_count: i32 = i32::MAX;\n\n let mut cur: i32 = 0;\n let mut start: usize = 1;\n while start <= mm\n {\n cur += diff[start];\n\n let old_best_bro = best_bro;\n let old_best_mom = best_mom;\n let old_best_bro_count = best_bro_count;\n let old_best_mom_count = best_mom_count;\n\n if cur > best_bro_count {\n best_bro_count = cur;\n best_bro = start as i32;\n }\n if cur < best_mom_count {\n best_mom_count = cur;\n best_mom = start as i32;\n }\n\n start += 1;\n }\n\n (best_bro, best_mom)\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let t: usize = match next_i32(&mut it) {\n Some(v) => v as usize,\n None => return,\n };\n\n let mut out = String::new();\n let mut case_id: usize = 0;\n while case_id < t {\n let n: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let d: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let k: usize = match next_i32(&mut it) {\n Some(v) => v as usize,\n None => return,\n };\n\n let mut left: Vec = Vec::new();\n let mut right: Vec = Vec::new();\n let mut i: usize = 0;\n while i < k {\n let l: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n let r: i32 = match next_i32(&mut it) {\n Some(v) => v,\n None => return,\n };\n left.push(l);\n right.push(r);\n i = i + 1;\n }\n\n let ans = Solution::best_start_days(n, d, left, right);\n out.push_str(&format!(\"{} {}\\n\", ans.0, ans.1));\n\n case_id = case_id + 1;\n }\n\n print!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf2014D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf2014D/tests/mutated_testcases.jsonl"} {"problem": "cf2020B", "family": "codeforces", "description": "# Brightness Begins\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nImagine you have $$$n$$$ light bulbs numbered $$$1, 2, \\ldots, n$$$. **Initially, all bulbs are on**. To *flip* the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.\n\nNext, you do the following:\n - for each $$$i = 1, 2, \\ldots, n$$$, flip the state of all bulbs $$$j$$$ such that $$$j$$$ is divisible by $$$i^\\dagger$$$. \n\nAfter performing all operations, there will be several bulbs that are still on. Your goal is to make this number exactly $$$k$$$.\n\nFind the smallest suitable $$$n$$$ such that after performing the operations there will be exactly $$$k$$$ bulbs on. We can show that an answer always exists.\n\n$$$^\\dagger$$$ An integer $$$x$$$ is divisible by $$$y$$$ if there exists an integer $$$z$$$ such that $$$x = y\\cdot z$$$.\n\n## Input\n\nEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The description of the test cases follows.\n\nThe only line of each test case contains a single integer $$$k$$$ ($$$1 \\le k \\le 10^{18}$$$).\n\n## Output\n\nFor each test case, output $$$n$$$\u00a0\u2014 the minimum number of bulbs.\n\n## Example\n\n**Input:**\n```\n3\n1\n3\n8\n```\n**Output:**\n```\n2\n5\n11\n```\n\n## Note\n\nIn the first test case, the minimum number of bulbs is $$$2$$$. Let's denote the state of all bulbs with an array, where $$$1$$$ corresponds to a turned on bulb, and $$$0$$$ corresponds to a turned off bulb. Initially, the array is $$$[1, 1]$$$.\n - After performing the operation with $$$i = 1$$$, the array becomes $$$[\\underline{0}, \\underline{0}]$$$. \n- After performing the operation with $$$i = 2$$$, the array becomes $$$[0, \\underline{1}]$$$. \n\nIn the end, there are $$$k = 1$$$ bulbs on. We can also show that the answer cannot be less than $$$2$$$.\n\nIn the second test case, the minimum number of bulbs is $$$5$$$. Initially, the array is $$$[1, 1, 1, 1, 1]$$$. \n - After performing the operation with $$$i = 1$$$, the array becomes $$$[\\underline{0}, \\underline{0}, \\underline{0}, \\underline{0}, \\underline{0}]$$$. \n- After performing the operation with $$$i = 2$$$, the array becomes $$$[0, \\underline{1}, 0, \\underline{1}, 0]$$$. \n- After performing the operation with $$$i = 3$$$, the array becomes $$$[0, 1, \\underline{1}, 1, 0]$$$. \n- After performing the operation with $$$i = 4$$$, the array becomes $$$[0, 1, 1, \\underline{0}, 0]$$$. \n- After performing the operation with $$$i = 5$$$, the array becomes $$$[0, 1, 1, 0, \\underline{1}]$$$. \n\nIn the end, there are $$$k = 3$$$ bulbs on. We can also show that the answer cannot be smaller than $$$5$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_bulbs_n(k: u64) -> u64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut i = 0usize;\n while i < t {\n let k: u64 = it.next().unwrap().parse().unwrap();\n let ans = Solution::min_bulbs_n(k);\n println!(\"{}\", ans);\n i = i + 1;\n }\n}\n```\n", "tags": "Rating: 1200\nbinary search, math\nSolved by: 21266 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_floor_sqrt_rec(lo: int, hi: int, n: int) -> int\n recommends\n 0 <= lo < hi,\n lo * lo <= n,\n n < hi * hi,\n decreases hi - lo,\n {\n if lo + 1 >= hi {\n lo\n } else {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= n {\n Self::spec_floor_sqrt_rec(mid, hi, n)\n } else {\n Self::spec_floor_sqrt_rec(lo, mid, n)\n }\n }\n }\n\n pub open spec fn spec_floor_sqrt(n: int) -> int {\n if n <= 0 {\n 0int\n } else {\n Self::spec_floor_sqrt_rec(0, n + 1, n)\n }\n }\n\n pub open spec fn spec_bulbs_on(n: int) -> int {\n n - Self::spec_floor_sqrt(n)\n }\n\n pub fn min_bulbs_n(k: u64) -> (n: u64)\n requires\n 1 <= k <= 1_000_000_000_000_000_000u64,\n ensures\n Self::spec_bulbs_on(n as int) == k as int,\n forall|m: int|\n 1 <= m < (n as int) ==> #[trigger] Self::spec_bulbs_on(m) < k as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn floor_sqrt_u64(x: u64) -> u64 {\n let mut lo = 1u64;\n let mut hi = 2_000_000_001u64;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= x {\n lo = mid + 1;\n } else {\n hi = mid;\n }\n }\n let r = lo - 1;\n r\n }\n\n pub fn min_bulbs_n(k: u64) -> u64 {\n let ub = k + 2_000_000_000u64 + 1000u64;\n let mut lo = 1u64;\n let mut hi = ub;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let s = Self::floor_sqrt_u64(mid);\n let cnt = mid - s;\n if cnt >= k {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n lo\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_floor_sqrt_rec(lo: int, hi: int, n: int) -> int\n recommends\n 0 <= lo < hi,\n lo * lo <= n,\n n < hi * hi,\n decreases hi - lo,\n {\n if lo + 1 >= hi {\n lo\n } else {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= n {\n Self::spec_floor_sqrt_rec(mid, hi, n)\n } else {\n Self::spec_floor_sqrt_rec(lo, mid, n)\n }\n }\n }\n\n pub open spec fn spec_floor_sqrt(n: int) -> int {\n if n <= 0 {\n 0int\n } else {\n Self::spec_floor_sqrt_rec(0, n + 1, n)\n }\n }\n\n pub open spec fn spec_bulbs_on(n: int) -> int {\n n - Self::spec_floor_sqrt(n)\n }\n\n pub fn floor_sqrt_u64(x: u64) -> (r: u64)\n requires\n 1 <= x <= 1_000_000_002_000_001_000u64,\n ensures\n r as int == Self::spec_floor_sqrt(x as int),\n r <= 2_000_000_000u64,\n {\n let mut lo = 1u64;\n let mut hi = 2_000_000_001u64;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= x {\n lo = mid + 1;\n } else {\n hi = mid;\n }\n }\n let r = lo - 1;\n r\n }\n\n pub fn min_bulbs_n(k: u64) -> (n: u64)\n requires\n 1 <= k <= 1_000_000_000_000_000_000u64,\n ensures\n Self::spec_bulbs_on(n as int) == k as int,\n forall|m: int|\n 1 <= m < (n as int) ==> #[trigger] Self::spec_bulbs_on(m) < k as int,\n {\n let ub = k + 2_000_000_000u64 + 1000u64;\n let mut lo = 1u64;\n let mut hi = ub;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let s = Self::floor_sqrt_u64(mid);\n let cnt = mid - s;\n if cnt >= k {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n lo\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_floor_sqrt_rec(lo: int, hi: int, n: int) -> int\n recommends\n 0 <= lo < hi,\n lo * lo <= n,\n n < hi * hi,\n decreases hi - lo,\n {\n if lo + 1 >= hi {\n lo\n } else {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= n {\n Self::spec_floor_sqrt_rec(mid, hi, n)\n } else {\n Self::spec_floor_sqrt_rec(lo, mid, n)\n }\n }\n }\n\n pub open spec fn spec_floor_sqrt(n: int) -> int {\n if n <= 0 {\n 0int\n } else {\n Self::spec_floor_sqrt_rec(0, n + 1, n)\n }\n }\n\n pub open spec fn spec_bulbs_on(n: int) -> int {\n n - Self::spec_floor_sqrt(n)\n }\n\n pub fn floor_sqrt_u64(x: u64) -> (r: u64)\n requires\n 1 <= x <= 1_000_000_002_000_001_000u64,\n ensures\n r as int == Self::spec_floor_sqrt(x as int),\n r <= 2_000_000_000u64,\n {\n let mut lo = 1u64;\n let mut hi = 2_000_000_001u64;\n while lo < hi\n invariant\n 1u64 <= x <= 1_000_000_002_000_001_000u64,\n 1u64 <= lo <= hi <= 2_000_000_001u64,\n (lo - 1) * (lo - 1) <= x,\n x < hi * hi,\n decreases hi - lo,\n {\n let mid = lo + (hi - lo) / 2;\n proof {\n assert(lo <= mid) by (nonlinear_arith)\n requires\n lo < hi,\n mid == lo + (hi - lo) / 2,\n {\n }\n assert(mid < hi) by (nonlinear_arith)\n requires\n lo < hi,\n mid == lo + (hi - lo) / 2,\n {\n }\n assert(mid <= 2_000_000_000u64) by (nonlinear_arith)\n requires\n mid < hi,\n hi <= 2_000_000_001u64,\n {\n }\n }\n proof {\n assert(mid * mid <= 4_000_000_004_000_000_001u64) by (nonlinear_arith)\n requires\n mid <= 2_000_000_000u64,\n {\n }\n }\n if mid * mid <= x {\n lo = mid + 1;\n proof {\n assert((lo - 1) == mid);\n assert((lo - 1) * (lo - 1) <= x);\n }\n } else {\n hi = mid;\n proof {\n assert(x < hi * hi);\n }\n }\n }\n let r = lo - 1;\n proof {\n assert(lo == hi);\n assert(r <= 2_000_000_000u64) by (nonlinear_arith)\n requires\n lo <= 2_000_000_001u64,\n r == lo - 1,\n {\n }\n assert(r * r <= x);\n assert(x < (r + 1) * (r + 1));\n Self::lemma_exec_sqrt_matches_spec(x, r);\n }\n r\n }\n\n pub fn min_bulbs_n(k: u64) -> (n: u64)\n requires\n 1 <= k <= 1_000_000_000_000_000_000u64,\n ensures\n Self::spec_bulbs_on(n as int) == k as int,\n forall|m: int|\n 1 <= m < (n as int) ==> #[trigger] Self::spec_bulbs_on(m) < k as int,\n {\n proof {\n Self::lemma_ub_bulbs(k);\n }\n let ub = k + 2_000_000_000u64 + 1000u64;\n let mut lo = 1u64;\n let mut hi = ub;\n while lo < hi\n invariant\n 1 <= k <= 1_000_000_000_000_000_000u64,\n ub == k + 2_000_000_000u64 + 1000u64,\n 1u64 <= lo <= hi <= ub,\n Self::spec_bulbs_on(hi as int) >= k as int,\n lo == 1u64 || Self::spec_bulbs_on((lo - 1) as int) < k as int,\n decreases hi - lo,\n {\n let mid = lo + (hi - lo) / 2;\n proof {\n assert(lo <= mid) by (nonlinear_arith)\n requires\n lo < hi,\n mid == lo + (hi - lo) / 2,\n {\n }\n assert(mid < hi) by (nonlinear_arith)\n requires\n lo < hi,\n mid == lo + (hi - lo) / 2,\n {\n }\n assert(mid <= ub);\n assert(mid <= 1_000_000_002_000_001_000u64) by (nonlinear_arith)\n requires\n mid <= ub,\n ub == k + 2_000_000_000 + 1000,\n k <= 1_000_000_000_000_000_000,\n {\n }\n }\n let s = Self::floor_sqrt_u64(mid);\n proof {\n Self::lemma_spec_sqrt_bounds(mid as int);\n assert((s as int) * (s as int) <= mid as int);\n assert((s as int) <= mid as int) by (nonlinear_arith)\n requires\n (s as int) >= 0,\n mid as int >= 1,\n (s as int) * (s as int) <= mid as int,\n {\n }\n }\n let cnt = mid - s;\n if cnt >= k {\n hi = mid;\n proof {\n assert(s as int == Self::spec_floor_sqrt(mid as int));\n assert(Self::spec_bulbs_on(mid as int) == cnt as int);\n assert(Self::spec_bulbs_on(hi as int) >= k as int);\n assert(lo == 1u64 || Self::spec_bulbs_on((lo - 1) as int) < k as int);\n }\n } else {\n proof {\n Self::lemma_spec_sqrt_bounds(mid as int);\n assert(s as int == Self::spec_floor_sqrt(mid as int));\n assert(Self::spec_bulbs_on(mid as int) == cnt as int);\n assert(Self::spec_bulbs_on(mid as int) < k as int);\n Self::lemma_m_le_mid_impl(mid as int, k as int);\n }\n lo = mid + 1;\n proof {\n assert(Self::spec_bulbs_on((lo - 1) as int) < k as int);\n assert(Self::spec_bulbs_on(hi as int) >= k as int);\n }\n }\n }\n proof {\n assert(lo == hi);\n assert(Self::spec_bulbs_on(lo as int) >= k as int);\n assert(lo == 1u64 || Self::spec_bulbs_on((lo - 1) as int) < k as int);\n Self::lemma_bulbs_at_one();\n assert(lo != 1u64) by {\n if lo == 1u64 {\n assert(Self::spec_bulbs_on(1) >= k as int);\n assert(0 >= k as int);\n }\n }\n Self::lemma_bulbs_step((lo - 1) as int);\n assert(Self::spec_bulbs_on(lo as int) <= Self::spec_bulbs_on((lo - 1) as int) + 1);\n assert(Self::spec_bulbs_on(lo as int) == k as int);\n assert forall|m: int| 1 <= m < (lo as int) implies Self::spec_bulbs_on(m) < k as int by {\n assert(1 <= m && m < lo as int);\n assert(m <= (lo - 1) as int) by (nonlinear_arith)\n requires\n m < lo as int,\n lo as int >= 2,\n m >= 1,\n {\n }\n Self::lemma_m_le_mid_impl((lo - 1) as int, k as int);\n }\n }\n lo\n }\n\n proof fn lemma_spec_floor_sqrt_rec_bounds(lo: int, hi: int, n: int)\n requires\n 0 <= lo < hi,\n lo * lo <= n,\n n < hi * hi,\n ensures\n ({\n let r = Self::spec_floor_sqrt_rec(lo, hi, n);\n r >= 0 && r * r <= n && n < (r + 1) * (r + 1)\n }),\n decreases hi - lo,\n {\n if lo + 1 >= hi {\n assert(hi == lo + 1);\n let r = lo;\n assert(r >= 0);\n assert(r * r <= n);\n assert(n < (r + 1) * (r + 1));\n } else {\n let mid = lo + (hi - lo) / 2;\n assert(lo < mid && mid < hi);\n if mid * mid <= n {\n assert(0 <= mid < hi);\n assert(mid * mid <= n);\n assert(n < hi * hi);\n Self::lemma_spec_floor_sqrt_rec_bounds(mid, hi, n);\n let r = Self::spec_floor_sqrt_rec(mid, hi, n);\n assert(r >= 0 && r * r <= n && n < (r + 1) * (r + 1));\n } else {\n assert(mid * mid > n);\n assert(0 <= lo < mid);\n assert(lo * lo <= n);\n assert(n < mid * mid);\n Self::lemma_spec_floor_sqrt_rec_bounds(lo, mid, n);\n let r = Self::spec_floor_sqrt_rec(lo, mid, n);\n assert(r >= 0 && r * r <= n && n < (r + 1) * (r + 1));\n }\n }\n }\n\n proof fn lemma_spec_sqrt_bounds(n: int)\n requires\n n >= 1,\n ensures\n ({\n let r = Self::spec_floor_sqrt(n);\n r >= 0 && r * r <= n && n < (r + 1) * (r + 1)\n }),\n {\n assert(n < (n + 1) * (n + 1)) by (nonlinear_arith)\n requires\n n >= 1,\n {\n }\n assert(0 * 0 <= n);\n Self::lemma_spec_floor_sqrt_rec_bounds(0, n + 1, n);\n assert(Self::spec_floor_sqrt(n) == Self::spec_floor_sqrt_rec(0, n + 1, n));\n }\n\n proof fn lemma_bulbs_at_one()\n ensures\n Self::spec_bulbs_on(1) == 0,\n {\n Self::lemma_spec_sqrt_bounds(1);\n let r = Self::spec_floor_sqrt(1);\n assert(r * r <= 1);\n assert(1 < (r + 1) * (r + 1));\n assert(r != 0) by {\n if r == 0 {\n assert(1 < 1);\n }\n }\n assert(r < 2) by {\n if r >= 2 {\n assert(r * r >= 4) by (nonlinear_arith)\n requires\n r >= 2,\n {\n }\n assert(r * r <= 1);\n assert(false);\n }\n }\n assert(r == 1);\n assert(Self::spec_bulbs_on(1) == 0);\n }\n\n proof fn lemma_unique_sqrt(n: int, a: int, b: int)\n requires\n n >= 1,\n a >= 0,\n b >= 0,\n a * a <= n,\n n < (a + 1) * (a + 1),\n b * b <= n,\n n < (b + 1) * (b + 1),\n ensures\n a == b,\n {\n if a < b {\n assert(b >= a + 1);\n assert(b * b >= (a + 1) * (a + 1)) by (nonlinear_arith)\n requires\n b >= a + 1,\n a >= 0,\n b >= 0,\n {\n }\n assert(n >= b * b);\n assert(n >= (a + 1) * (a + 1));\n assert(n < (a + 1) * (a + 1));\n assert(false);\n } else if a > b {\n assert(a >= b + 1);\n assert(a * a >= (b + 1) * (b + 1)) by (nonlinear_arith)\n requires\n a >= b + 1,\n a >= 0,\n b >= 0,\n {\n }\n assert(n >= a * a);\n assert(n >= (b + 1) * (b + 1));\n assert(n < (b + 1) * (b + 1));\n assert(false);\n } else {\n assert(a == b);\n }\n }\n\n proof fn lemma_sqrt_upper_bound(x: int)\n requires\n 1 <= x <= 1_000_000_002_000_001_000,\n ensures\n Self::spec_floor_sqrt(x) <= 2_000_000_000,\n {\n assert(2_000_000_001 * 2_000_000_001 == 4_000_000_004_000_000_001);\n assert(x < 2_000_000_001 * 2_000_000_001);\n let r = Self::spec_floor_sqrt(x);\n Self::lemma_spec_sqrt_bounds(x);\n assert(r * r <= x);\n assert(r >= 0);\n assert(r < 2_000_000_001) by (nonlinear_arith)\n requires\n r * r <= x,\n x < 2_000_000_001 * 2_000_000_001,\n r >= 0,\n {\n }\n assert(r <= 2_000_000_000);\n }\n\n proof fn lemma_ub_bulbs(k: u64)\n requires\n 1 <= k <= 1_000_000_000_000_000_000u64,\n ensures\n Self::spec_bulbs_on((k + 2_000_000_000u64 + 1000u64) as int) >= k as int,\n {\n let ub = (k + 2_000_000_000u64 + 1000u64) as int;\n assert(ub <= 1_000_000_002_000_001_000);\n Self::lemma_sqrt_upper_bound(ub);\n assert(Self::spec_floor_sqrt(ub) <= 2_000_000_000);\n assert(ub - 2_000_000_000 >= k as int) by (nonlinear_arith)\n requires\n ub == k as int + 2_000_000_000 + 1000,\n k as int >= 1,\n k as int <= 1_000_000_000_000_000_000,\n {\n }\n assert(Self::spec_bulbs_on(ub) >= k as int);\n }\n\n proof fn lemma_bulbs_step(n: int)\n requires\n n >= 1,\n ensures\n Self::spec_bulbs_on(n + 1) >= Self::spec_bulbs_on(n),\n Self::spec_bulbs_on(n + 1) <= Self::spec_bulbs_on(n) + 1,\n {\n let sn = Self::spec_floor_sqrt(n);\n let sn1 = Self::spec_floor_sqrt(n + 1);\n Self::lemma_spec_sqrt_bounds(n);\n Self::lemma_spec_sqrt_bounds(n + 1);\n assert(sn >= 0);\n assert(sn1 >= 0);\n assert(sn * sn <= n);\n assert(n < (sn + 1) * (sn + 1));\n assert(sn1 * sn1 <= n + 1);\n assert(n + 1 < (sn1 + 1) * (sn1 + 1));\n assert(sn1 == sn || sn1 == sn + 1) by (nonlinear_arith)\n requires\n sn * sn <= n,\n n < (sn + 1) * (sn + 1),\n sn1 * sn1 <= n + 1,\n n + 1 < (sn1 + 1) * (sn1 + 1),\n sn >= 0,\n sn1 >= 0,\n {\n }\n if sn1 == sn + 1 {\n assert(Self::spec_bulbs_on(n + 1) == n + 1 - (sn + 1));\n assert(Self::spec_bulbs_on(n + 1) == Self::spec_bulbs_on(n));\n } else {\n assert(sn1 == sn);\n assert(Self::spec_bulbs_on(n + 1) == Self::spec_bulbs_on(n) + 1);\n }\n }\n\n proof fn lemma_bulbs_mono_range(a: int, b: int)\n requires\n 1 <= a <= b,\n ensures\n Self::spec_bulbs_on(a) <= Self::spec_bulbs_on(b),\n decreases b - a,\n {\n if a < b {\n Self::lemma_bulbs_step(a);\n assert(Self::spec_bulbs_on(a) <= Self::spec_bulbs_on(a + 1));\n Self::lemma_bulbs_mono_range(a + 1, b);\n assert(Self::spec_bulbs_on(a + 1) <= Self::spec_bulbs_on(b));\n }\n }\n\n proof fn lemma_m_le_mid_impl(mid: int, k: int)\n requires\n mid >= 1,\n Self::spec_bulbs_on(mid) < k,\n ensures\n forall|m: int| 1 <= m <= mid ==> #[trigger] Self::spec_bulbs_on(m) < k,\n {\n assert forall|m: int| 1 <= m <= mid implies Self::spec_bulbs_on(m) < k by {\n assert(1 <= m && m <= mid);\n Self::lemma_bulbs_mono_range(m, mid);\n assert(Self::spec_bulbs_on(m) <= Self::spec_bulbs_on(mid));\n assert(Self::spec_bulbs_on(m) < k);\n }\n }\n\n proof fn lemma_exec_sqrt_matches_spec(x: u64, r: u64)\n requires\n 1 <= x <= 1_000_000_002_000_001_000u64,\n r * r <= x,\n x < (r + 1) * (r + 1),\n r <= 2_000_000_000u64,\n ensures\n r as int == Self::spec_floor_sqrt(x as int),\n {\n let xi = x as int;\n let ri = r as int;\n Self::lemma_spec_sqrt_bounds(xi);\n let s = Self::spec_floor_sqrt(xi);\n Self::lemma_unique_sqrt(xi, ri, s);\n assert(ri == s);\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn floor_sqrt_u64(x: u64) -> u64 {\n let mut lo = 1u64;\n let mut hi = 2_000_000_001u64;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= x {\n lo = mid + 1;\n } else {\n hi = mid;\n }\n }\n let r = lo - 1;\n r\n }\n\n pub fn min_bulbs_n(k: u64) -> u64 {\n let ub = k + 2_000_000_000u64 + 1000u64;\n let mut lo = 1u64;\n let mut hi = ub;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let s = Self::floor_sqrt_u64(mid);\n let cnt = mid - s;\n if cnt >= k {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n lo\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut i = 0usize;\n while i < t {\n let k: u64 = it.next().unwrap().parse().unwrap();\n let ans = Solution::min_bulbs_n(k);\n println!(\"{}\", ans);\n i = i + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf2020B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf2020B/tests/mutated_testcases.jsonl"} {"problem": "cf2044C", "family": "codeforces", "description": "# Hard Problem\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nBall is the teacher in Paperfold University. The seats of his classroom are arranged in $$$2$$$ rows with $$$m$$$ seats each.\n\nBall is teaching $$$a + b + c$$$ monkeys, and he wants to assign as many monkeys to a seat as possible. Ball knows that $$$a$$$ of them only want to sit in row $$$1$$$, $$$b$$$ of them only want to sit in row $$$2$$$, and $$$c$$$ of them have no preference. Only one monkey may sit in each seat, and each monkey's preference must be followed if it is seated.\n\nWhat is the maximum number of monkeys that Ball can seat?\n\n## Input\n\nThe first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) \u2014 the number of test cases.\n\nEach test case contains four integers $$$m$$$, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \\leq m, a, b, c \\leq 10^8$$$).\n\n## Output\n\nFor each test case, output the maximum number of monkeys you can seat.\n\n## Example\n\n**Input:**\n```\n5\n10 5 5 10\n3 6 1 1\n15 14 12 4\n1 1 1 1\n420 6 9 69\n```\n**Output:**\n```\n20\n5\n30\n2\n84\n```\n\n## Note\n\nIn the second test case, $$$6$$$ monkeys want to sit in the front row, but only $$$3$$$ seats are available. The monkeys that have no preference and the monkeys who prefer sitting in the second row can sit in the second row together. Thus, the answer is $$$3+2=5$$$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_monkeys(m: u64, a: u64, b: u64, c: u64) -> u64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let m: u64 = iter.next().unwrap().parse().unwrap();\n let a: u64 = iter.next().unwrap().parse().unwrap();\n let b: u64 = iter.next().unwrap().parse().unwrap();\n let c: u64 = iter.next().unwrap().parse().unwrap();\n writeln!(out, \"{}\", Solution::max_monkeys(m, a, b, c)).unwrap();\n }\n}\n```\n", "tags": "Rating: 800\ngreedy, math\nSolved by: 45179 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn min_int(x: int, y: int) -> int {\n if x <= y { x } else { y }\n}\n\nimpl Solution {\n pub fn max_monkeys(m: u64, a: u64, b: u64, c: u64) -> (result: u64)\n requires\n 1 <= m <= 100000000,\n 1 <= a <= 100000000,\n 1 <= b <= 100000000,\n 1 <= c <= 100000000,\n ensures\n result as int == min_int(m as int, a as int) + min_int(m as int, b as int)\n + min_int(c as int, 2 * (m as int) - min_int(m as int, a as int) - min_int(m as int, b as int)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_monkeys(m: u64, a: u64, b: u64, c: u64) -> u64 {\n let row1: u64 = if m <= a { m } else { a };\n let row2: u64 = if m <= b { m } else { b };\n let remaining: u64 = 2 * m - row1 - row2;\n let extra: u64 = if c <= remaining { c } else { remaining };\n row1 + row2 + extra\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn min_int(x: int, y: int) -> int {\n if x <= y { x } else { y }\n}\n\nimpl Solution {\n pub fn max_monkeys(m: u64, a: u64, b: u64, c: u64) -> (result: u64)\n requires\n 1 <= m <= 100000000,\n 1 <= a <= 100000000,\n 1 <= b <= 100000000,\n 1 <= c <= 100000000,\n ensures\n result as int == min_int(m as int, a as int) + min_int(m as int, b as int)\n + min_int(c as int, 2 * (m as int) - min_int(m as int, a as int) - min_int(m as int, b as int)),\n {\n let row1: u64 = if m <= a { m } else { a };\n let row2: u64 = if m <= b { m } else { b };\n let remaining: u64 = 2 * m - row1 - row2;\n let extra: u64 = if c <= remaining { c } else { remaining };\n row1 + row2 + extra\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn min_int(x: int, y: int) -> int {\n if x <= y { x } else { y }\n}\n\nimpl Solution {\n pub fn max_monkeys(m: u64, a: u64, b: u64, c: u64) -> (result: u64)\n requires\n 1 <= m <= 100000000,\n 1 <= a <= 100000000,\n 1 <= b <= 100000000,\n 1 <= c <= 100000000,\n ensures\n result as int == min_int(m as int, a as int) + min_int(m as int, b as int)\n + min_int(c as int, 2 * (m as int) - min_int(m as int, a as int) - min_int(m as int, b as int)),\n {\n let row1: u64 = if m <= a { m } else { a };\n let row2: u64 = if m <= b { m } else { b };\n assert(row1 as int == min_int(m as int, a as int));\n assert(row2 as int == min_int(m as int, b as int));\n let remaining: u64 = 2 * m - row1 - row2;\n assert(remaining as int == 2 * (m as int) - min_int(m as int, a as int) - min_int(m as int, b as int));\n let extra: u64 = if c <= remaining { c } else { remaining };\n assert(extra as int == min_int(c as int, remaining as int));\n row1 + row2 + extra\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_monkeys(m: u64, a: u64, b: u64, c: u64) -> u64 {\n let row1: u64 = if m <= a { m } else { a };\n let row2: u64 = if m <= b { m } else { b };\n let remaining: u64 = 2 * m - row1 - row2;\n let extra: u64 = if c <= remaining { c } else { remaining };\n row1 + row2 + extra\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let t: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let m: u64 = iter.next().unwrap().parse().unwrap();\n let a: u64 = iter.next().unwrap().parse().unwrap();\n let b: u64 = iter.next().unwrap().parse().unwrap();\n let c: u64 = iter.next().unwrap().parse().unwrap();\n writeln!(out, \"{}\", Solution::max_monkeys(m, a, b, c)).unwrap();\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf2044C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf2044C/tests/mutated_testcases.jsonl"} {"problem": "cf2060D", "family": "codeforces", "description": "# Subtract Min Sort\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou are given a sequence $$$a$$$ consisting of $$$n$$$ positive integers.\n\nYou can perform the following operation any number of times.\n - Select an index $$$i$$$ ($$$1 \\le i \\lt n$$$), and subtract $$$\\min(a_i,a_{i+1})$$$ from both $$$a_i$$$ and $$$a_{i+1}$$$. \n\nDetermine if it is possible to make the sequence **non-decreasing** by using the operation any number of times.\n\n## Input\n\nEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$).\n\nThe second line of each test case contains $$$a_1,a_2,\\ldots,a_n$$$ ($$$1 \\le a_i \\le 10^9$$$).\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nIf it is possible to make the sequence **non-decreasing**, print \"`YES`\" on a new line. Otherwise, print \"`NO`\" on a new line.\n\nYou can output the answer in any case. For example, the strings \"`yEs`\", \"`yes`\", and \"`Yes`\" will also be recognized as positive responses.\n\n## Example\n\n**Input:**\n```\n5\n5\n1 2 3 4 5\n4\n4 3 2 1\n4\n4 5 2 3\n8\n4 5 4 5 4 5 4 5\n9\n9 9 8 2 4 4 3 5 3\n```\n**Output:**\n```\nYES\nNO\nYES\nYES\nNO\n```\n\n## Note\n\nIn the first test case, the array is already sorted.\n\nIn the second test case, we can show that it is impossible.\n\nIn the third test case, after performing an operation on $$$i=1$$$, the array becomes $$$[0,1,2,3]$$$, which is now in nondecreasing order.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_sort(a: Vec) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let t: usize = tokens.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case: usize = 0;\n while case < t {\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut a: Vec = Vec::with_capacity(n);\n let mut idx: usize = 0;\n while idx < n {\n a.push(tokens.next().expect(\"element\").parse().expect(\"valid i64\"));\n idx = idx + 1;\n }\n if Solution::can_sort(a) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n case = case + 1;\n }\n}\n```\n", "tags": "Rating: 1100\ngreedy\nSolved by: 26069 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn remaining_val(a: Seq, i: int) -> int\n recommends\n 0 <= i < a.len(),\n decreases i,\n {\n if i <= 0 {\n a[0] as int\n } else {\n let prev = Self::remaining_val(a, i - 1);\n if prev <= a[i] as int {\n a[i] as int - prev\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn is_sortable(a: Seq) -> bool {\n forall |i: int| 0 <= i < a.len() as int - 1 ==> Self::remaining_val(a, i) <= #[trigger] a[i + 1] as int\n }\n\n pub fn can_sort(a: Vec) -> (result: bool)\n requires\n 2 <= a.len() <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 1_000_000_000,\n ensures\n result == Self::is_sortable(a@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_sort(a: Vec) -> bool {\n let n = a.len();\n let mut cur: i64 = a[0];\n let mut i: usize = 0;\n while i < n - 1 {\n let next = a[i + 1];\n if cur > next {\n return false;\n }\n let new_cur = next - cur;\n cur = new_cur;\n i = i + 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn remaining_val(a: Seq, i: int) -> int\n recommends\n 0 <= i < a.len(),\n decreases i,\n {\n if i <= 0 {\n a[0] as int\n } else {\n let prev = Self::remaining_val(a, i - 1);\n if prev <= a[i] as int {\n a[i] as int - prev\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn is_sortable(a: Seq) -> bool {\n forall |i: int| 0 <= i < a.len() as int - 1 ==> Self::remaining_val(a, i) <= #[trigger] a[i + 1] as int\n }\n\n pub fn can_sort(a: Vec) -> (result: bool)\n requires\n 2 <= a.len() <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 1_000_000_000,\n ensures\n result == Self::is_sortable(a@),\n {\n let n = a.len();\n let mut cur: i64 = a[0];\n let mut i: usize = 0;\n while i < n - 1 {\n let next = a[i + 1];\n if cur > next {\n return false;\n }\n let new_cur = next - cur;\n cur = new_cur;\n i = i + 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn remaining_val(a: Seq, i: int) -> int\n recommends\n 0 <= i < a.len(),\n decreases i,\n {\n if i <= 0 {\n a[0] as int\n } else {\n let prev = Self::remaining_val(a, i - 1);\n if prev <= a[i] as int {\n a[i] as int - prev\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn is_sortable(a: Seq) -> bool {\n forall |i: int| 0 <= i < a.len() as int - 1 ==> Self::remaining_val(a, i) <= #[trigger] a[i + 1] as int\n }\n\n proof fn lemma_remaining_val_bounds(a: Seq, i: int)\n requires\n 0 <= i < a.len(),\n forall |k: int| 0 <= k < a.len() ==> 0 <= #[trigger] a[k] <= 1_000_000_000,\n ensures\n 0 <= Self::remaining_val(a, i) <= 1_000_000_000,\n decreases i,\n {\n if i == 0 {\n } else {\n Self::lemma_remaining_val_bounds(a, i - 1);\n }\n }\n\n pub fn can_sort(a: Vec) -> (result: bool)\n requires\n 2 <= a.len() <= 200_000,\n forall |k: int| 0 <= k < a.len() ==> 1 <= #[trigger] a[k] <= 1_000_000_000,\n ensures\n result == Self::is_sortable(a@),\n {\n let n = a.len();\n let ghost orig = a@;\n let mut cur: i64 = a[0];\n let mut i: usize = 0;\n\n proof {\n assert(cur as int == Self::remaining_val(orig, 0));\n Self::lemma_remaining_val_bounds(orig, 0);\n }\n\n while i < n - 1\n invariant\n n == orig.len(),\n a.len() == n,\n a@ == orig,\n n >= 2,\n n <= 200_000,\n 0 <= i <= n - 1,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] orig[k] <= 1_000_000_000,\n cur as int == Self::remaining_val(orig, i as int),\n 0 <= cur as int <= 1_000_000_000,\n forall |j: int| 0 <= j < i ==> Self::remaining_val(orig, j) <= #[trigger] orig[j + 1] as int,\n decreases n - 1 - i,\n {\n let next = a[i + 1];\n\n if cur > next {\n proof {\n assert(Self::remaining_val(orig, i as int) > orig[i as int + 1] as int);\n assert(!Self::is_sortable(orig));\n }\n return false;\n }\n\n let new_cur = next - cur;\n\n proof {\n assert(cur as int <= next as int);\n assert(Self::remaining_val(orig, i as int) <= orig[i as int + 1] as int);\n let rv = Self::remaining_val(orig, i as int);\n assert(rv <= orig[i as int + 1] as int);\n assert(Self::remaining_val(orig, i as int + 1) == orig[i as int + 1] as int - rv);\n assert(new_cur as int == next as int - cur as int);\n assert(new_cur as int == orig[i as int + 1] as int - Self::remaining_val(orig, i as int));\n assert(new_cur as int == Self::remaining_val(orig, i as int + 1));\n Self::lemma_remaining_val_bounds(orig, i as int + 1);\n assert forall |j: int| 0 <= j < i + 1 implies Self::remaining_val(orig, j) <= #[trigger] orig[j + 1] as int by {\n if j < i as int {\n } else {\n assert(j == i as int);\n }\n };\n }\n\n cur = new_cur;\n i = i + 1;\n }\n\n proof {\n assert(Self::is_sortable(orig));\n }\n true\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_sort(a: Vec) -> bool {\n let n = a.len();\n let mut cur: i64 = a[0];\n let mut i: usize = 0;\n while i < n - 1 {\n let next = a[i + 1];\n if cur > next {\n return false;\n }\n let new_cur = next - cur;\n cur = new_cur;\n i = i + 1;\n }\n true\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let t: usize = tokens.next().expect(\"t\").parse().expect(\"valid t\");\n let mut case: usize = 0;\n while case < t {\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut a: Vec = Vec::with_capacity(n);\n let mut idx: usize = 0;\n while idx < n {\n a.push(tokens.next().expect(\"element\").parse().expect(\"valid i64\"));\n idx = idx + 1;\n }\n if Solution::can_sort(a) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n case = case + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf2060D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf2060D/tests/mutated_testcases.jsonl"} {"problem": "cf2122B", "family": "codeforces", "description": "# Pile Shuffling\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou are given $$$n$$$ binary piles, the $$$i$$$-th of which consists of $$$a_i$$$ zeros on the top and $$$b_i$$$ ones on the bottom.\n\nIn one operation, you can take the **top element** of any pile and move it to any position in any pile, including the pile it was taken from. \n Solution of the first example test case. Calculate the minimum number of operations required to make it so that the $$$i$$$-th pile consists of $$$c_i$$$ zeros on the top and $$$d_i$$$ ones on the bottom.\n\n## Input\n\nEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The description of the test cases follows. \n\nThe first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$)\u00a0\u2014 the number of piles.\n\nThen $$$n$$$ lines follow, the $$$i$$$-th containing four integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$, $$$d_i$$$ ($$$0 \\leq a_i, b_i, c_i, d_i \\leq 10^9$$$)\u00a0\u2014 the original and target state of the $$$i$$$-th pile.\n\nIt is guaranteed that there exists a sequence of operations that transforms the piles into the target state.\n\nIt is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.\n\n## Output\n\nFor each test case, output one integer\u00a0\u2014 the minimum number of operations required to achieve the target state.\n\n## Example\n\n**Input:**\n```\n3\n2\n1 3 1 2\n1 1 1 2\n3\n2 0 2 2\n0 1 1 0\n1 1 0 0\n3\n1 2 1 2\n3 4 3 4\n0 0 0 0\n```\n**Output:**\n```\n2\n3\n0\n```\n\n## Note\n\nThe solution of the first test case is shown in the statement.\n\nIn the second test case, an optimal solution is to do the following: \n \n \n\nIn the third test case, all piles are already in their target state.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_pile_shuffle_operations(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut b: Vec = Vec::new();\n let mut c: Vec = Vec::new();\n let mut d: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let ai: i64 = it.next().unwrap().parse().unwrap();\n let bi: i64 = it.next().unwrap().parse().unwrap();\n let ci: i64 = it.next().unwrap().parse().unwrap();\n let di: i64 = it.next().unwrap().parse().unwrap();\n a.push(ai);\n b.push(bi);\n c.push(ci);\n d.push(di);\n j = j + 1;\n }\n let ans = Solution::min_pile_shuffle_operations(&a, &b, &c, &d);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n```\n", "tags": "Rating: 1100\ngreedy, math\nSolved by: 15044 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_contrib(a: int, b: int, c: int, d: int) -> int {\n let a_after = if a > c {\n c\n } else {\n a\n };\n let part1 = if a > c {\n a - c\n } else {\n 0\n };\n let part2 = if b > d {\n b - d + a_after\n } else {\n 0\n };\n part1 + part2\n}\n\npub open spec fn spec_ops_prefix(\n a: Seq,\n b: Seq,\n c: Seq,\n d: Seq,\n hi: int,\n) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n spec_ops_prefix(a, b, c, d, hi - 1) + spec_contrib(\n a[hi] as int,\n b[hi] as int,\n c[hi] as int,\n d[hi] as int,\n )\n }\n}\n\npub open spec fn spec_sum(nums: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n spec_sum(nums, hi - 1) + nums[hi] as int\n }\n}\n\nimpl Solution {\n pub fn min_pile_shuffle_operations(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> (result: i64)\n requires\n 1 <= a.len() <= 200_000,\n a.len() == b.len(),\n a.len() == c.len(),\n a.len() == d.len(),\n forall|j: int|\n #![trigger a[j]]\n 0 <= j && j < a.len() ==> 0 <= #[trigger] a[j] && a[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger b[j]]\n 0 <= j && j < b.len() ==> 0 <= #[trigger] b[j] && b[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger c[j]]\n 0 <= j && j < c.len() ==> 0 <= #[trigger] c[j] && c[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger d[j]]\n 0 <= j && j < d.len() ==> 0 <= #[trigger] d[j] && d[j] <= 1_000_000_000,\n spec_sum(a@, (a.len() as int) - 1) == spec_sum(c@, (c.len() as int) - 1),\n spec_sum(b@, (b.len() as int) - 1) == spec_sum(d@, (d.len() as int) - 1),\n ensures\n result as int == spec_ops_prefix(a@, b@, c@, d@, (a.len() as int) - 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_pile_shuffle_operations(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> i64 {\n let n = a.len();\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut av: i64 = a[i];\n let bv: i64 = b[i];\n let cv: i64 = c[i];\n let dv: i64 = d[i];\n if av > cv {\n ans = ans + (av - cv);\n av = cv;\n }\n if bv > dv {\n ans = ans + (bv - dv + av);\n }\n i = i + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_contrib(a: int, b: int, c: int, d: int) -> int {\n let a_after = if a > c {\n c\n } else {\n a\n };\n let part1 = if a > c {\n a - c\n } else {\n 0\n };\n let part2 = if b > d {\n b - d + a_after\n } else {\n 0\n };\n part1 + part2\n}\n\npub open spec fn spec_ops_prefix(\n a: Seq,\n b: Seq,\n c: Seq,\n d: Seq,\n hi: int,\n) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n spec_ops_prefix(a, b, c, d, hi - 1) + spec_contrib(\n a[hi] as int,\n b[hi] as int,\n c[hi] as int,\n d[hi] as int,\n )\n }\n}\n\npub open spec fn spec_sum(nums: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n spec_sum(nums, hi - 1) + nums[hi] as int\n }\n}\n\nimpl Solution {\n pub fn min_pile_shuffle_operations(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> (result: i64)\n requires\n 1 <= a.len() <= 200_000,\n a.len() == b.len(),\n a.len() == c.len(),\n a.len() == d.len(),\n forall|j: int|\n #![trigger a[j]]\n 0 <= j && j < a.len() ==> 0 <= #[trigger] a[j] && a[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger b[j]]\n 0 <= j && j < b.len() ==> 0 <= #[trigger] b[j] && b[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger c[j]]\n 0 <= j && j < c.len() ==> 0 <= #[trigger] c[j] && c[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger d[j]]\n 0 <= j && j < d.len() ==> 0 <= #[trigger] d[j] && d[j] <= 1_000_000_000,\n spec_sum(a@, (a.len() as int) - 1) == spec_sum(c@, (c.len() as int) - 1),\n spec_sum(b@, (b.len() as int) - 1) == spec_sum(d@, (d.len() as int) - 1),\n ensures\n result as int == spec_ops_prefix(a@, b@, c@, d@, (a.len() as int) - 1),\n {\n let n = a.len();\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut av: i64 = a[i];\n let bv: i64 = b[i];\n let cv: i64 = c[i];\n let dv: i64 = d[i];\n if av > cv {\n ans = ans + (av - cv);\n av = cv;\n }\n if bv > dv {\n ans = ans + (bv - dv + av);\n }\n i = i + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_contrib(a: int, b: int, c: int, d: int) -> int {\n let a_after = if a > c {\n c\n } else {\n a\n };\n let part1 = if a > c {\n a - c\n } else {\n 0\n };\n let part2 = if b > d {\n b - d + a_after\n } else {\n 0\n };\n part1 + part2\n}\n\npub open spec fn spec_ops_prefix(\n a: Seq,\n b: Seq,\n c: Seq,\n d: Seq,\n hi: int,\n) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n spec_ops_prefix(a, b, c, d, hi - 1) + spec_contrib(\n a[hi] as int,\n b[hi] as int,\n c[hi] as int,\n d[hi] as int,\n )\n }\n}\n\nproof fn lemma_spec_ops_prefix_succ(\n a: Seq,\n b: Seq,\n c: Seq,\n d: Seq,\n hi: int,\n)\n requires\n hi >= 0,\n ensures\n spec_ops_prefix(a, b, c, d, hi) == spec_ops_prefix(a, b, c, d, hi - 1) + spec_contrib(\n a[hi] as int,\n b[hi] as int,\n c[hi] as int,\n d[hi] as int,\n ),\n{\n}\n\nproof fn lemma_delta_matches_spec_contrib(av0: i64, bv: i64, cv: i64, dv: i64, delta: i64)\n requires\n av0 > cv ==> bv > dv ==> delta == (av0 - cv) + (bv - dv + cv),\n av0 > cv ==> !(bv > dv) ==> delta == av0 - cv,\n !(av0 > cv) ==> bv > dv ==> delta == bv - dv + av0,\n !(av0 > cv) ==> !(bv > dv) ==> delta == 0,\n ensures\n delta as int == spec_contrib(av0 as int, bv as int, cv as int, dv as int),\n{\n let ai = av0 as int;\n let bi = bv as int;\n let ci = cv as int;\n let di = dv as int;\n if av0 > cv {\n if bv > dv {\n assert(delta as int == (ai - ci) + (bi - di + ci));\n assert(spec_contrib(ai, bi, ci, di) == (ai - ci) + (bi - di + ci));\n } else {\n assert(delta as int == ai - ci);\n assert(spec_contrib(ai, bi, ci, di) == ai - ci);\n }\n } else {\n if bv > dv {\n assert(delta as int == bi - di + ai);\n assert(spec_contrib(ai, bi, ci, di) == bi - di + ai);\n } else {\n assert(delta as int == 0);\n assert(spec_contrib(ai, bi, ci, di) == 0);\n }\n }\n}\n\npub open spec fn spec_sum(nums: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n spec_sum(nums, hi - 1) + nums[hi] as int\n }\n}\n\nimpl Solution {\n pub fn min_pile_shuffle_operations(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> (result: i64)\n requires\n 1 <= a.len() <= 200_000,\n a.len() == b.len(),\n a.len() == c.len(),\n a.len() == d.len(),\n forall|j: int|\n #![trigger a[j]]\n 0 <= j && j < a.len() ==> 0 <= #[trigger] a[j] && a[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger b[j]]\n 0 <= j && j < b.len() ==> 0 <= #[trigger] b[j] && b[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger c[j]]\n 0 <= j && j < c.len() ==> 0 <= #[trigger] c[j] && c[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger d[j]]\n 0 <= j && j < d.len() ==> 0 <= #[trigger] d[j] && d[j] <= 1_000_000_000,\n spec_sum(a@, (a.len() as int) - 1) == spec_sum(c@, (c.len() as int) - 1),\n spec_sum(b@, (b.len() as int) - 1) == spec_sum(d@, (d.len() as int) - 1),\n ensures\n result as int == spec_ops_prefix(a@, b@, c@, d@, (a.len() as int) - 1),\n {\n let n = a.len();\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == a.len(),\n n == b.len(),\n n == c.len(),\n n == d.len(),\n i <= n,\n n <= 200_000,\n 0 <= ans,\n ans as int <= (i as int) * 3_000_000_000,\n ans as int == spec_ops_prefix(a@, b@, c@, d@, (i as int) - 1),\n forall|j: int|\n #![trigger a[j]]\n 0 <= j && j < a.len() ==> 0 <= #[trigger] a[j] && a[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger b[j]]\n 0 <= j && j < b.len() ==> 0 <= #[trigger] b[j] && b[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger c[j]]\n 0 <= j && j < c.len() ==> 0 <= #[trigger] c[j] && c[j] <= 1_000_000_000,\n forall|j: int|\n #![trigger d[j]]\n 0 <= j && j < d.len() ==> 0 <= #[trigger] d[j] && d[j] <= 1_000_000_000,\n decreases n - i\n {\n let ghost old_ans = ans;\n let mut av: i64 = a[i];\n let bv: i64 = b[i];\n let cv: i64 = c[i];\n let dv: i64 = d[i];\n let ghost av0 = a[i as int];\n if av > cv {\n proof {\n assert(i < n);\n assert(n <= 200_000);\n assert((i as int) < 200_000);\n assert((av - cv) as int <= 1_000_000_000);\n assert((ans + (av - cv)) as int <= (i as int) * 3_000_000_000 + 1_000_000_000);\n assert((i as int) * 3_000_000_000 + 1_000_000_000 < 9223372036854775807);\n }\n ans = ans + (av - cv);\n av = cv;\n }\n if bv > dv {\n proof {\n assert(i < n);\n assert(n <= 200_000);\n assert((i as int) < 200_000);\n assert((bv - dv + av) as int <= 3_000_000_000);\n assert((ans + (bv - dv + av)) as int <= (i as int) * 3_000_000_000 + 3_000_000_000);\n assert((i as int) * 3_000_000_000 + 3_000_000_000 < 9223372036854775807);\n }\n ans = ans + (bv - dv + av);\n }\n let ghost delta: i64 = (ans - old_ans) as i64;\n proof {\n assert(av0 == a@[i as int]);\n if av0 > cv {\n if bv > dv {\n assert(delta == (av0 - cv) + (bv - dv + cv));\n } else {\n assert(delta == av0 - cv);\n }\n } else {\n if bv > dv {\n assert(delta == bv - dv + av0);\n } else {\n assert(delta == 0);\n }\n }\n lemma_delta_matches_spec_contrib(av0, bv, cv, dv, delta);\n lemma_spec_ops_prefix_succ(a@, b@, c@, d@, i as int);\n assert(ans as int == old_ans as int + spec_contrib(a@[i as int] as int, b@[i as int] as int, c@[i as int] as int, d@[i as int] as int));\n assert(ans as int == spec_ops_prefix(a@, b@, c@, d@, i as int));\n }\n i = i + 1;\n }\n proof {\n assert(ans as int == spec_ops_prefix(a@, b@, c@, d@, (a.len() as int) - 1));\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_pile_shuffle_operations(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> i64 {\n let n = a.len();\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut av: i64 = a[i];\n let bv: i64 = b[i];\n let cv: i64 = c[i];\n let dv: i64 = d[i];\n if av > cv {\n ans = ans + (av - cv);\n av = cv;\n }\n if bv > dv {\n ans = ans + (bv - dv + av);\n }\n i = i + 1;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut k: usize = 0;\n while k < t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: Vec = Vec::new();\n let mut b: Vec = Vec::new();\n let mut c: Vec = Vec::new();\n let mut d: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let ai: i64 = it.next().unwrap().parse().unwrap();\n let bi: i64 = it.next().unwrap().parse().unwrap();\n let ci: i64 = it.next().unwrap().parse().unwrap();\n let di: i64 = it.next().unwrap().parse().unwrap();\n a.push(ai);\n b.push(bi);\n c.push(ci);\n d.push(di);\n j = j + 1;\n }\n let ans = Solution::min_pile_shuffle_operations(&a, &b, &c, &d);\n println!(\"{}\", ans);\n k = k + 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf2122B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf2122B/tests/mutated_testcases.jsonl"} {"problem": "cf227B", "family": "codeforces", "description": "# Effective Approach\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nOnce at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.\n\nAccording to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.\n\nVasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the $1$-st one (in this problem we consider the elements of the array indexed from $1$ to $n$) and ending with the $n$-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the $n$-th and ending with the $1$-st one. Sasha argues that the two approaches are equivalent.\n\nTo finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from $1$ to $n$, and generated $m$ queries of the form: find element with value $b_i$ in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.\n\nBut the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.\n\n## Input\n\nThe first line contains integer $n$ $(1\u2009\u2264\u2009n\u2009\u2264\u200910^5)$ \u2014 the number of elements in the array. The second line contains $n$ distinct space-separated integers $a_1,\u2009a_2,\u2009...,\u2009a_n$ $(1\u2009\u2264\u2009a_i\u2009\u2264\u2009n)$ \u2014 the elements of array. \n\nThe third line contains integer $m$ $(1\u2009\u2264\u2009m\u2009\u2264\u200910^5)$ \u2014 the number of queries. The last line contains $m$ space-separated integers $b_1,\u2009b_2,\u2009...,\u2009b_m$ $(1\u2009\u2264\u2009b_i\u2009\u2264\u2009n)$ \u2014 the search queries. Note that the queries can repeat.\n\n## Output\n\nPrint two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.\n\nPlease, do not use the `%lld` specifier to read or write 64-bit integers in \u0421++. It is preferred to use `cin`, `cout` streams or the `%I64d` specifier.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2\n1 2\n1\n1\n```\n**Output:**\n```\n1 2\n```\n\n### Example 2\n\n**Input:**\n```\n2\n2 1\n1\n1\n```\n**Output:**\n```\n2 1\n```\n\n### Example 3\n\n**Input:**\n```\n3\n3 1 2\n3\n1 2 3\n```\n**Output:**\n```\n6 6\n```\n\n## Note\n\nIn the first sample Vasya's approach will make one comparison (it starts with the $1$-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the $2$-nd array element, doesn't find the search item and compares with the $1$-st element).\n\nIn the second sample, on the contrary, Vasya's approach will need two comparisons (first with $1$-st element, and then with the $2$-nd), and Petya's approach will find the required value in one comparison (the first comparison with the $2$-nd element).\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn effective_approach(permutation: Vec, queries: Vec) -> (i64, i64) {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut permutation = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n permutation.push(it.next().unwrap().parse::().unwrap());\n i += 1;\n }\n let m: usize = it.next().unwrap().parse().unwrap();\n let mut queries = Vec::with_capacity(m);\n let mut j = 0usize;\n while j < m {\n queries.push(it.next().unwrap().parse::().unwrap());\n j += 1;\n }\n let (vasya, petya) = Solution::effective_approach(permutation, queries);\n println!(\"{} {}\", vasya, petya);\n}\n```\n", "tags": "Rating: 1100\nimplementation\nSolved by: 32149 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn value_occurs(permutation: Seq, value: int) -> bool {\n exists|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value\n}\n\npub open spec fn all_values_occur(permutation: Seq) -> bool {\n forall|value: int| 1 <= value <= permutation.len() ==> #[trigger] value_occurs(permutation, value)\n}\n\npub open spec fn is_valid_permutation(permutation: Seq) -> bool {\n 1 <= permutation.len() <= 100_000\n && forall|i: int| 0 <= i < permutation.len() ==> 1 <= #[trigger] permutation[i] <= permutation.len()\n && forall|i: int, j: int| 0 <= i < j < permutation.len() ==> permutation[i] != permutation[j]\n && all_values_occur(permutation)\n}\n\npub open spec fn are_valid_queries(queries: Seq, n: int) -> bool {\n 1 <= queries.len() <= 100_000\n && forall|i: int| 0 <= i < queries.len() ==> 1 <= #[trigger] queries[i] <= n\n}\n\npub open spec fn position_of(permutation: Seq, value: int) -> int\n recommends\n is_valid_permutation(permutation),\n 1 <= value <= permutation.len(),\n{\n choose|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value\n}\n\npub open spec fn total_vasya(permutation: Seq, queries: Seq, k: int) -> int\n recommends\n is_valid_permutation(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n total_vasya(permutation, queries, k - 1) + position_of(permutation, queries[k - 1] as int) + 1\n }\n}\n\npub open spec fn total_petya(permutation: Seq, queries: Seq, k: int) -> int\n recommends\n is_valid_permutation(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n total_petya(permutation, queries, k - 1) + permutation.len() - position_of(permutation, queries[k - 1] as int)\n }\n}\n\nimpl Solution {\n pub fn effective_approach(permutation: Vec, queries: Vec) -> (result: (i64, i64))\n requires\n is_valid_permutation(permutation@),\n all_values_occur(permutation@),\n are_valid_queries(queries@, permutation.len() as int),\n ensures\n result.0 as int == total_vasya(permutation@, queries@, queries.len() as int),\n result.1 as int == total_petya(permutation@, queries@, queries.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn effective_approach(permutation: Vec, queries: Vec) -> (i64, i64) {\n let n = permutation.len();\n let mut positions = Vec::new();\n let mut t = 0usize;\n while t <= n {\n positions.push(0i64);\n t += 1;\n }\n let mut i = 0usize;\n while i < n {\n let value = permutation[i] as usize;\n positions[value] = i as i64 + 1;\n i += 1;\n }\n let mut vasya = 0i64;\n let mut petya = 0i64;\n let mut j = 0usize;\n while j < queries.len() {\n let value = queries[j] as usize;\n let p = positions[value];\n vasya += p;\n petya += n as i64 - p + 1;\n j += 1;\n }\n (vasya, petya)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn value_occurs(permutation: Seq, value: int) -> bool {\n exists|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value\n}\n\npub open spec fn all_values_occur(permutation: Seq) -> bool {\n forall|value: int| 1 <= value <= permutation.len() ==> #[trigger] value_occurs(permutation, value)\n}\n\npub open spec fn is_valid_permutation(permutation: Seq) -> bool {\n 1 <= permutation.len() <= 100_000\n && forall|i: int| 0 <= i < permutation.len() ==> 1 <= #[trigger] permutation[i] <= permutation.len()\n && forall|i: int, j: int| 0 <= i < j < permutation.len() ==> permutation[i] != permutation[j]\n && all_values_occur(permutation)\n}\n\npub open spec fn are_valid_queries(queries: Seq, n: int) -> bool {\n 1 <= queries.len() <= 100_000\n && forall|i: int| 0 <= i < queries.len() ==> 1 <= #[trigger] queries[i] <= n\n}\n\npub open spec fn position_of(permutation: Seq, value: int) -> int\n recommends\n is_valid_permutation(permutation),\n 1 <= value <= permutation.len(),\n{\n choose|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value\n}\n\npub open spec fn total_vasya(permutation: Seq, queries: Seq, k: int) -> int\n recommends\n is_valid_permutation(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n total_vasya(permutation, queries, k - 1) + position_of(permutation, queries[k - 1] as int) + 1\n }\n}\n\npub open spec fn total_petya(permutation: Seq, queries: Seq, k: int) -> int\n recommends\n is_valid_permutation(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n total_petya(permutation, queries, k - 1) + permutation.len() - position_of(permutation, queries[k - 1] as int)\n }\n}\n\nimpl Solution {\n pub fn effective_approach(permutation: Vec, queries: Vec) -> (result: (i64, i64))\n requires\n is_valid_permutation(permutation@),\n all_values_occur(permutation@),\n are_valid_queries(queries@, permutation.len() as int),\n ensures\n result.0 as int == total_vasya(permutation@, queries@, queries.len() as int),\n result.1 as int == total_petya(permutation@, queries@, queries.len() as int),\n {\n let n = permutation.len();\n let mut positions = Vec::new();\n let mut t = 0usize;\n while t <= n {\n positions.push(0i64);\n t += 1;\n }\n let mut i = 0usize;\n while i < n {\n let value = permutation[i] as usize;\n positions.set(value, i as i64 + 1);\n i += 1;\n }\n let mut vasya = 0i64;\n let mut petya = 0i64;\n let mut j = 0usize;\n while j < queries.len() {\n let value = queries[j] as usize;\n let p = positions[value];\n vasya += p;\n petya += n as i64 - p + 1;\n j += 1;\n }\n (vasya, petya)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn value_occurs(permutation: Seq, value: int) -> bool {\n exists|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value\n}\n\npub open spec fn all_values_occur(permutation: Seq) -> bool {\n forall|value: int| 1 <= value <= permutation.len() ==> #[trigger] value_occurs(permutation, value)\n}\n\npub open spec fn is_valid_permutation(permutation: Seq) -> bool {\n 1 <= permutation.len() <= 100_000\n && forall|i: int| 0 <= i < permutation.len() ==> 1 <= #[trigger] permutation[i] <= permutation.len()\n && forall|i: int, j: int| 0 <= i < j < permutation.len() ==> permutation[i] != permutation[j]\n && all_values_occur(permutation)\n}\n\npub open spec fn are_valid_queries(queries: Seq, n: int) -> bool {\n 1 <= queries.len() <= 100_000\n && forall|i: int| 0 <= i < queries.len() ==> 1 <= #[trigger] queries[i] <= n\n}\n\npub open spec fn position_of(permutation: Seq, value: int) -> int\n recommends\n is_valid_permutation(permutation),\n 1 <= value <= permutation.len(),\n{\n choose|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value\n}\n\npub open spec fn total_vasya(permutation: Seq, queries: Seq, k: int) -> int\n recommends\n is_valid_permutation(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n total_vasya(permutation, queries, k - 1) + position_of(permutation, queries[k - 1] as int) + 1\n }\n}\n\npub open spec fn total_petya(permutation: Seq, queries: Seq, k: int) -> int\n recommends\n is_valid_permutation(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n total_petya(permutation, queries, k - 1) + permutation.len() - position_of(permutation, queries[k - 1] as int)\n }\n}\n\nproof fn lemma_position_of_properties(permutation: Seq, value: int)\n requires\n is_valid_permutation(permutation),\n all_values_occur(permutation),\n 1 <= value <= permutation.len(),\n ensures\n 0 <= position_of(permutation, value) < permutation.len(),\n permutation[position_of(permutation, value)] == value,\n forall|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value ==> pos == position_of(permutation, value),\n{\n assert(value_occurs(permutation, value)) by {\n reveal_with_fuel(all_values_occur, 1);\n assert(1 <= value <= permutation.len());\n }\n assert(exists|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value) by {\n assert(value_occurs(permutation, value));\n }\n let chosen = position_of(permutation, value);\n assert(0 <= chosen < permutation.len());\n assert(permutation[chosen] == value);\n assert forall|pos: int| 0 <= pos < permutation.len() && permutation[pos] == value implies pos == chosen by {\n if pos < chosen {\n assert(permutation[pos] != permutation[chosen]);\n } else if chosen < pos {\n assert(permutation[chosen] != permutation[pos]);\n }\n }\n}\n\nproof fn lemma_total_bounds(permutation: Seq, queries: Seq, k: int)\n requires\n is_valid_permutation(permutation),\n all_values_occur(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k <= queries.len(),\n ensures\n 0 <= total_vasya(permutation, queries, k) <= k * 100_000,\n 0 <= total_petya(permutation, queries, k) <= k * 100_000,\n decreases k,\n{\n if k > 0 {\n lemma_total_bounds(permutation, queries, k - 1);\n lemma_position_of_properties(permutation, queries[k - 1] as int);\n lemma_total_vasya_step(permutation, queries, k - 1);\n lemma_total_petya_step(permutation, queries, k - 1);\n assert(0 <= position_of(permutation, queries[k - 1] as int) + 1 <= 100_000);\n assert(0 <= permutation.len() - position_of(permutation, queries[k - 1] as int) <= 100_000);\n }\n}\n\nproof fn lemma_total_vasya_step(permutation: Seq, queries: Seq, k: int)\n requires\n is_valid_permutation(permutation),\n all_values_occur(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k < queries.len(),\n ensures\n total_vasya(permutation, queries, k + 1) == total_vasya(permutation, queries, k) + position_of(permutation, queries[k] as int) + 1,\n{\n reveal_with_fuel(total_vasya, 2);\n}\n\nproof fn lemma_total_petya_step(permutation: Seq, queries: Seq, k: int)\n requires\n is_valid_permutation(permutation),\n all_values_occur(permutation),\n are_valid_queries(queries, permutation.len() as int),\n 0 <= k < queries.len(),\n ensures\n total_petya(permutation, queries, k + 1) == total_petya(permutation, queries, k) + permutation.len() - position_of(permutation, queries[k] as int),\n{\n reveal_with_fuel(total_petya, 2);\n}\n\nimpl Solution {\n pub fn effective_approach(permutation: Vec, queries: Vec) -> (result: (i64, i64))\n requires\n is_valid_permutation(permutation@),\n all_values_occur(permutation@),\n are_valid_queries(queries@, permutation.len() as int),\n ensures\n result.0 as int == total_vasya(permutation@, queries@, queries.len() as int),\n result.1 as int == total_petya(permutation@, queries@, queries.len() as int),\n {\n let n = permutation.len();\n let mut positions = Vec::new();\n let mut t = 0usize;\n while t <= n\n invariant\n n == permutation.len(),\n is_valid_permutation(permutation@),\n all_values_occur(permutation@),\n are_valid_queries(queries@, permutation.len() as int),\n 0 <= t <= n + 1,\n positions.len() == t,\n forall|k: int| 0 <= k < positions.len() ==> positions[k] == 0,\n decreases n + 1 - t,\n {\n let ghost old_positions = positions@;\n positions.push(0i64);\n t += 1;\n proof {\n assert(positions@ == old_positions.push(0i64));\n assert forall|k: int| 0 <= k < positions.len() implies positions[k] == 0 by {\n if k < old_positions.len() {\n } else {\n assert(k == old_positions.len());\n }\n }\n }\n }\n let mut i = 0usize;\n while i < n\n invariant\n n == permutation.len(),\n is_valid_permutation(permutation@),\n all_values_occur(permutation@),\n are_valid_queries(queries@, permutation.len() as int),\n positions.len() == n + 1,\n 0 <= i <= n,\n forall|k: int| 0 <= k < i as int ==> #[trigger] positions[permutation[k] as int] == k + 1,\n forall|k: int| 0 <= k < positions.len() ==> 0 <= #[trigger] positions[k] <= n as int,\n decreases n - i,\n {\n let value = permutation[i] as usize;\n let ghost old_positions = positions@;\n positions.set(value, i as i64 + 1);\n proof {\n assert(1 <= permutation[i as int] <= n as int);\n assert(value == permutation[i as int] as usize);\n assert(positions@ == old_positions.update(value as int, (i + 1) as i64));\n assert(0 <= i as int + 1 <= n as int);\n assert forall|k: int| 0 <= k < i as int + 1 implies #[trigger] positions[permutation[k] as int] == k + 1 by {\n if k == i as int {\n assert(permutation[k] == permutation[i as int]);\n assert(positions[permutation[k] as int] == i as int + 1);\n } else {\n assert(0 <= k < i as int);\n assert(old_positions[permutation[k] as int] == k + 1);\n if permutation[k] as int == value as int {\n assert(k < i as int);\n assert(permutation[k] != permutation[i as int]);\n } else {\n assert(positions[permutation[k] as int] == old_positions[permutation[k] as int]);\n }\n }\n }\n assert forall|k: int| 0 <= k < positions.len() implies 0 <= #[trigger] positions[k] <= n as int by {\n if k == value as int {\n assert(positions[k] == i as int + 1);\n } else {\n assert(positions[k] == old_positions[k]);\n }\n }\n }\n i += 1;\n }\n proof {\n assert(i == n);\n assert forall|value: int| 1 <= value <= n as int implies #[trigger] positions[value] == position_of(permutation@, value) + 1 by {\n lemma_position_of_properties(permutation@, value);\n let pos = position_of(permutation@, value);\n assert(0 <= pos < i as int);\n assert(permutation[pos] == value);\n assert(positions[permutation[pos] as int] == pos + 1);\n }\n }\n let mut vasya = 0i64;\n let mut petya = 0i64;\n let mut j = 0usize;\n while j < queries.len()\n invariant\n n == permutation.len(),\n is_valid_permutation(permutation@),\n all_values_occur(permutation@),\n are_valid_queries(queries@, permutation.len() as int),\n positions.len() == n + 1,\n forall|value: int| 1 <= value <= n as int ==> #[trigger] positions[value] == position_of(permutation@, value) + 1,\n 0 <= j <= queries.len(),\n vasya as int == total_vasya(permutation@, queries@, j as int),\n petya as int == total_petya(permutation@, queries@, j as int),\n 0 <= vasya as int <= j as int * 100_000,\n 0 <= petya as int <= j as int * 100_000,\n decreases queries.len() - j,\n {\n let value = queries[j] as usize;\n let p = positions[value];\n proof {\n lemma_total_vasya_step(permutation@, queries@, j as int);\n lemma_total_petya_step(permutation@, queries@, j as int);\n lemma_position_of_properties(permutation@, queries[j as int] as int);\n lemma_total_bounds(permutation@, queries@, j as int + 1);\n assert(1 <= queries[j as int] <= n as int);\n assert(value == queries[j as int] as usize);\n assert(p as int == position_of(permutation@, queries[j as int] as int) + 1);\n assert(n as int - p as int + 1 == permutation.len() - position_of(permutation@, queries[j as int] as int));\n assert(0 <= p <= n as int);\n assert(vasya as int + p as int == total_vasya(permutation@, queries@, j as int + 1));\n assert(petya as int + (n as int - p as int + 1) == total_petya(permutation@, queries@, j as int + 1));\n assert((j as int + 1) * 100_000 <= 100_000 * 100_000);\n }\n vasya += p;\n petya += n as i64 - p + 1;\n j += 1;\n }\n (vasya, petya)\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn effective_approach(permutation: Vec, queries: Vec) -> (i64, i64) {\n let n = permutation.len();\n let mut positions = Vec::new();\n let mut t = 0usize;\n while t <= n {\n positions.push(0i64);\n t += 1;\n }\n let mut i = 0usize;\n while i < n {\n let value = permutation[i] as usize;\n positions[value] = i as i64 + 1;\n i += 1;\n }\n let mut vasya = 0i64;\n let mut petya = 0i64;\n let mut j = 0usize;\n while j < queries.len() {\n let value = queries[j] as usize;\n let p = positions[value];\n vasya += p;\n petya += n as i64 - p + 1;\n j += 1;\n }\n (vasya, petya)\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut permutation = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n permutation.push(it.next().unwrap().parse::().unwrap());\n i += 1;\n }\n let m: usize = it.next().unwrap().parse().unwrap();\n let mut queries = Vec::with_capacity(m);\n let mut j = 0usize;\n while j < m {\n queries.push(it.next().unwrap().parse::().unwrap());\n j += 1;\n }\n let (vasya, petya) = Solution::effective_approach(permutation, queries);\n println!(\"{} {}\", vasya, petya);\n}\n", "checker": null, "testcases_path": "codeforces/cf227B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf227B/tests/mutated_testcases.jsonl"} {"problem": "cf230B", "family": "codeforces", "description": "# T-primes\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nWe know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer $t$ \u0422-prime, if $t$ has exactly three distinct positive divisors.\n\nYou are given an array of $n$ positive integers. For each of them determine whether it is \u0422-prime or not.\n\n## Input\n\nThe first line contains a single positive integer, $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^5$), showing how many numbers are in the array. The next line contains $n$ space-separated integers $x_i$ ($1\u2009\u2264\u2009x_i\u2009\u2264\u200910^12$).\n\nPlease, do not use the `%lld` specifier to read or write 64-bit integers in \u0421++. It is advised to use the `cin`, `cout` streams or the `%I64d` specifier.\n\n## Output\n\nPrint $n$ lines: the $i$-th line should contain \"`YES`\" (without the quotes), if number $x_i$ is \u0422-prime, and \"`NO`\" (without the quotes), if it isn't.\n\n## Example\n\n**Input:**\n```\n3\n4 5 6\n```\n**Output:**\n```\nYES\nNO\nNO\n```\n\n## Note\n\nThe given test has three numbers. The first number 4 has exactly three divisors \u2014 1, 2 and 4, thus the answer for this number is \"`YES`\". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is \"`NO`\".\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn classify_t_primes(nums: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut nums = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n nums.push(tokens.next().expect(\"x\").parse::().expect(\"valid u64\"));\n i += 1;\n }\n let ans = Solution::classify_t_primes(nums);\n let mut out = String::new();\n i = 0;\n while i < ans.len() {\n if ans[i] {\n out.push_str(\"YES\\n\");\n } else {\n out.push_str(\"NO\\n\");\n }\n i += 1;\n }\n print!(\"{}\", out);\n}\n```\n", "tags": "Rating: 1300\nbinary search, implementation, math, number theory\nSolved by: 93954 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_prime(n: int) -> bool {\n 2 <= n && forall|d: int| 2 <= d && d <= n / d ==> #[trigger] (n % d) != 0\n}\n\npub open spec fn is_t_prime(x: int) -> bool {\n exists|p: int| 2 <= p && p * p == x && is_prime(p)\n}\n\nimpl Solution {\n pub fn classify_t_primes(nums: Vec) -> (res: Vec)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 1u64 <= #[trigger] nums[i] <= 1_000_000_000_000u64,\n ensures\n res.len() == nums.len(),\n forall|i: int| 0 <= i < res.len() ==> #[trigger] res[i] == is_t_prime(nums[i] as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn floor_sqrt(x: u64) -> u64 {\n let mut lo = 1u64;\n let mut hi = 1_000_001u64;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= x {\n lo = mid + 1;\n } else {\n hi = mid;\n }\n }\n let r = lo - 1;\n r\n }\n\n fn is_prime_runtime(n: u64) -> bool {\n if n < 2 {\n return false;\n }\n let mut d = 2u64;\n while d <= n / d {\n if n % d == 0 {\n return false;\n }\n d += 1;\n }\n true\n }\n\n pub fn classify_t_primes(nums: Vec) -> Vec {\n let mut res = Vec::new();\n let mut i = 0usize;\n while i < nums.len() {\n let x = nums[i];\n let root = Self::floor_sqrt(x);\n let answer = if root * root == x {\n Self::is_prime_runtime(root)\n } else {\n false\n };\n res.push(answer);\n i += 1;\n }\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_prime(n: int) -> bool {\n 2 <= n && forall|d: int| 2 <= d && d <= n / d ==> #[trigger] (n % d) != 0\n}\n\npub open spec fn is_t_prime(x: int) -> bool {\n exists|p: int| 2 <= p && p * p == x && is_prime(p)\n}\n\nimpl Solution {\n fn floor_sqrt(x: u64) -> (r: u64)\n requires\n 1u64 <= x <= 1_000_000_000_000u64,\n ensures\n r <= 1_000_000u64,\n r * r <= x,\n x < (r + 1) * (r + 1),\n {\n let mut lo = 1u64;\n let mut hi = 1_000_001u64;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= x {\n lo = mid + 1;\n } else {\n hi = mid;\n }\n }\n let r = lo - 1;\n r\n }\n\n fn is_prime_runtime(n: u64) -> (res: bool)\n requires\n n <= 1_000_000u64,\n ensures\n res == is_prime(n as int),\n {\n if n < 2 {\n return false;\n }\n let mut d = 2u64;\n while d <= n / d {\n if n % d == 0 {\n return false;\n }\n d += 1;\n }\n true\n }\n\n pub fn classify_t_primes(nums: Vec) -> (res: Vec)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 1u64 <= #[trigger] nums[i] <= 1_000_000_000_000u64,\n ensures\n res.len() == nums.len(),\n forall|i: int| 0 <= i < res.len() ==> #[trigger] res[i] == is_t_prime(nums[i] as int),\n {\n let mut res = Vec::new();\n let mut i = 0usize;\n while i < nums.len() {\n let x = nums[i];\n let root = Self::floor_sqrt(x);\n let answer = if root * root == x {\n Self::is_prime_runtime(root)\n } else {\n false\n };\n res.push(answer);\n i += 1;\n }\n res\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_prime(n: int) -> bool {\n 2 <= n && forall|d: int| 2 <= d && d <= n / d ==> #[trigger] (n % d) != 0\n}\n\npub open spec fn is_t_prime(x: int) -> bool {\n exists|p: int| 2 <= p && p * p == x && is_prime(p)\n}\n\nproof fn lemma_unique_square_root(x: int, r: int, p: int)\n requires\n 0 <= r,\n 0 <= p,\n r * r <= x,\n x < (r + 1) * (r + 1),\n p * p == x,\n ensures\n p == r,\n{\n assert(r <= p) by (nonlinear_arith)\n requires\n 0 <= r,\n 0 <= p,\n r * r <= p * p,\n {\n }\n if r < p {\n assert((r + 1) * (r + 1) <= p * p) by (nonlinear_arith)\n requires\n 0 <= r,\n 0 <= p,\n r < p,\n {\n }\n assert(false);\n }\n assert(p == r) by (nonlinear_arith)\n requires\n r <= p,\n !(r < p),\n {\n }\n}\n\nimpl Solution {\n fn floor_sqrt(x: u64) -> (r: u64)\n requires\n 1u64 <= x <= 1_000_000_000_000u64,\n ensures\n r <= 1_000_000u64,\n r * r <= x,\n x < (r + 1) * (r + 1),\n {\n let mut lo = 1u64;\n let mut hi = 1_000_001u64;\n while lo < hi\n invariant\n 1u64 <= x <= 1_000_000_000_000u64,\n 1u64 <= lo <= hi <= 1_000_001u64,\n (lo - 1) * (lo - 1) <= x,\n x < hi * hi,\n decreases hi - lo,\n {\n let mid = lo + (hi - lo) / 2;\n proof {\n assert(lo <= mid) by (nonlinear_arith)\n requires\n lo < hi,\n mid == lo + (hi - lo) / 2,\n {\n }\n assert(mid < hi) by (nonlinear_arith)\n requires\n lo < hi,\n mid == lo + (hi - lo) / 2,\n {\n }\n assert(mid <= 1_000_000u64) by (nonlinear_arith)\n requires\n mid < hi,\n hi <= 1_000_001u64,\n {\n }\n }\n proof {\n assert(mid * mid <= 18_446_744_073_709_551_615u64) by (nonlinear_arith)\n requires\n mid <= 1_000_000u64,\n {\n }\n }\n if mid * mid <= x {\n lo = mid + 1;\n proof {\n assert((lo - 1) == mid);\n assert((lo - 1) * (lo - 1) <= x);\n }\n } else {\n hi = mid;\n proof {\n assert(x < hi * hi);\n }\n }\n }\n let r = lo - 1;\n proof {\n assert(lo == hi);\n assert(r <= 1_000_000u64) by (nonlinear_arith)\n requires\n lo <= 1_000_001u64,\n r == lo - 1,\n {\n }\n assert(r * r <= x);\n assert(x < (r + 1) * (r + 1));\n }\n r\n }\n\n fn is_prime_runtime(n: u64) -> (res: bool)\n requires\n n <= 1_000_000u64,\n ensures\n res == is_prime(n as int),\n {\n if n < 2 {\n return false;\n }\n let mut d = 2u64;\n while d <= n / d\n invariant\n 2u64 <= n <= 1_000_000u64,\n 2u64 <= d <= n + 1,\n forall|e: int| 2 <= e < (d as int) ==> #[trigger] ((n as int) % e) != 0,\n decreases n - d + 1,\n {\n if n % d == 0 {\n proof {\n assert(!is_prime(n as int)) by {\n assert(2 <= d as int && d as int <= (n as int) / (d as int));\n assert((n as int) % (d as int) == 0);\n }\n }\n return false;\n }\n proof {\n assert forall|e: int| 2 <= e < (d as int) + 1 implies #[trigger] ((n as int) % e) != 0 by {\n if e < (d as int) {\n } else {\n assert(e == d as int);\n assert((n as int) % e == n % d);\n }\n }\n }\n d += 1;\n }\n proof {\n if !is_prime(n as int) {\n let e = choose|e: int| 2 <= e && e <= (n as int) / e && #[trigger] ((n as int) % e) == 0;\n assert(e < (d as int)) by (nonlinear_arith)\n requires\n 2 <= d as int,\n d as int > n as int / d as int,\n 2 <= e,\n e <= n as int / e,\n {\n }\n assert((n as int) % e != 0);\n assert(false);\n }\n }\n true\n }\n\n pub fn classify_t_primes(nums: Vec) -> (res: Vec)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 1u64 <= #[trigger] nums[i] <= 1_000_000_000_000u64,\n ensures\n res.len() == nums.len(),\n forall|i: int| 0 <= i < res.len() ==> #[trigger] res[i] == is_t_prime(nums[i] as int),\n {\n let mut res = Vec::new();\n let mut i = 0usize;\n while i < nums.len()\n invariant\n 1 <= nums.len() <= 100_000,\n forall|k: int| 0 <= k < nums.len() ==> 1u64 <= #[trigger] nums[k] <= 1_000_000_000_000u64,\n 0 <= i <= nums.len(),\n res.len() == i,\n forall|k: int| 0 <= k < i ==> #[trigger] res[k] == is_t_prime(nums[k] as int),\n decreases nums.len() - i,\n {\n let x = nums[i];\n let root = Self::floor_sqrt(x);\n let answer = if root * root == x {\n Self::is_prime_runtime(root)\n } else {\n false\n };\n let ghost old_res = res@;\n let ghost cur = i;\n proof {\n if root * root == x {\n assert(answer == is_prime(root as int));\n assert(answer == is_t_prime(x as int)) by {\n if answer {\n assert(exists|p: int| 2 <= p && p * p == x as int && is_prime(p)) by {\n let p = root as int;\n }\n } else {\n if is_t_prime(x as int) {\n let p = choose|p: int| 2 <= p && p * p == x as int && is_prime(p);\n lemma_unique_square_root(x as int, root as int, p);\n assert(root as int == p);\n assert(is_prime(root as int));\n assert(answer);\n assert(false);\n }\n }\n }\n } else {\n assert(answer == false);\n assert(!is_t_prime(x as int)) by {\n if is_t_prime(x as int) {\n let p = choose|p: int| 2 <= p && p * p == x as int && is_prime(p);\n lemma_unique_square_root(x as int, root as int, p);\n assert(root as int == p);\n assert(root * root == x);\n assert(false);\n }\n }\n }\n }\n res.push(answer);\n i += 1;\n proof {\n assert(res@ == old_res.push(answer));\n assert(res.len() == i);\n assert forall|k: int| 0 <= k < i implies #[trigger] res[k] == is_t_prime(nums[k] as int) by {\n if k < cur as int {\n assert(res[k] == old_res[k]);\n } else {\n assert(k == cur as int);\n assert(res[k] == answer);\n }\n }\n }\n }\n res\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n fn floor_sqrt(x: u64) -> u64 {\n let mut lo = 1u64;\n let mut hi = 1_000_001u64;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n if mid * mid <= x {\n lo = mid + 1;\n } else {\n hi = mid;\n }\n }\n let r = lo - 1;\n r\n }\n\n fn is_prime_runtime(n: u64) -> bool {\n if n < 2 {\n return false;\n }\n let mut d = 2u64;\n while d <= n / d {\n if n % d == 0 {\n return false;\n }\n d += 1;\n }\n true\n }\n\n pub fn classify_t_primes(nums: Vec) -> Vec {\n let mut res = Vec::new();\n let mut i = 0usize;\n while i < nums.len() {\n let x = nums[i];\n let root = Self::floor_sqrt(x);\n let answer = if root * root == x {\n Self::is_prime_runtime(root)\n } else {\n false\n };\n res.push(answer);\n i += 1;\n }\n res\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut nums = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n nums.push(tokens.next().expect(\"x\").parse::().expect(\"valid u64\"));\n i += 1;\n }\n let ans = Solution::classify_t_primes(nums);\n let mut out = String::new();\n i = 0;\n while i < ans.len() {\n if ans[i] {\n out.push_str(\"YES\\n\");\n } else {\n out.push_str(\"NO\\n\");\n }\n i += 1;\n }\n print!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf230B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf230B/tests/mutated_testcases.jsonl"} {"problem": "cf242B", "family": "codeforces", "description": "# Big Segment\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nA coordinate line has $n$ segments, the $i$-th segment starts at the position $l_i$ and ends at the position $r_i$. We will denote such a segment as $[l_i,\u2009r_i]$.\n\nYou have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn't exist, print -1.\n\nFormally we will assume that segment $[a,\u2009b]$ covers segment $[c,\u2009d]$, if they meet this condition $a\u2009\u2264\u2009c\u2009\u2264\u2009d\u2009\u2264\u2009b$.\n\n## Input\n\nThe first line contains integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^5$) \u2014 the number of segments. Next $n$ lines contain the descriptions of the segments. The $i$-th line contains two space-separated integers $l_i,\u2009r_i$ ($1\u2009\u2264\u2009l_i\u2009\u2264\u2009r_i\u2009\u2264\u200910^9$) \u2014 the borders of the $i$-th segment.\n\nIt is guaranteed that no two segments coincide.\n\n## Output\n\nPrint a single integer \u2014 the number of the segment that covers all other segments in the set. If there's no solution, print -1.\n\nThe segments are numbered starting from $1$ in the order in which they appear in the input.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n3\n1 1\n2 2\n3 3\n```\n**Output:**\n```\n-1\n```\n\n### Example 2\n\n**Input:**\n```\n6\n1 5\n2 3\n1 10\n7 10\n7 7\n10 10\n```\n**Output:**\n```\n3\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn find_covering_segment(left: Vec, right: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let n = it.next().expect(\"n\").parse::().expect(\"usize\");\n let mut left = Vec::with_capacity(n);\n let mut right = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n left.push(it.next().expect(\"left\").parse::().expect(\"i32\"));\n right.push(it.next().expect(\"right\").parse::().expect(\"i32\"));\n i += 1;\n }\n let ans = Solution::find_covering_segment(left, right);\n if ans == 0 {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}\n```\n", "tags": "Rating: 1100\nimplementation, sortings\nSolved by: 17216 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn segment_contains(left: Seq, right: Seq, i: int, j: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= i < left.len(),\n 0 <= j < left.len(),\n{\n left[i] <= left[j] && right[j] <= right[i]\n}\n\npub open spec fn covers_all_segments(left: Seq, right: Seq, i: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= i < left.len(),\n{\n forall|j: int| 0 <= j < left.len() ==> #[trigger] segment_contains(left, right, i, j)\n}\n\nimpl Solution {\n pub fn find_covering_segment(left: Vec, right: Vec) -> (ans: i32)\n requires\n left.len() == right.len(),\n 1 <= left.len() <= 100_000,\n forall|i: int| 0 <= i < left.len() ==> 1 <= #[trigger] left[i] <= right[i] <= 1_000_000_000,\n forall|i: int, j: int|\n 0 <= i < j < left.len() ==> left[i] != left[j] || right[i] != right[j],\n ensures\n 0 <= ans <= left.len(),\n ans != 0 ==> covers_all_segments(left@, right@, ans as int - 1),\n ans == 0 ==> forall|i: int| 0 <= i < left.len() ==> !covers_all_segments(left@, right@, i),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_covering_segment(left: Vec, right: Vec) -> i32 {\n let mut candidate = 0usize;\n let mut i = 1usize;\n while i < left.len() {\n if left[i] < left[candidate] || (left[i] == left[candidate] && right[candidate] < right[i]) {\n candidate = i;\n }\n i += 1;\n }\n\n let mut j = 0usize;\n while j < left.len() {\n if left[candidate] > left[j] || right[j] > right[candidate] {\n return 0;\n }\n j += 1;\n }\n\n candidate as i32 + 1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn segment_contains(left: Seq, right: Seq, i: int, j: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= i < left.len(),\n 0 <= j < left.len(),\n{\n left[i] <= left[j] && right[j] <= right[i]\n}\n\npub open spec fn covers_all_segments(left: Seq, right: Seq, i: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= i < left.len(),\n{\n forall|j: int| 0 <= j < left.len() ==> #[trigger] segment_contains(left, right, i, j)\n}\n\nimpl Solution {\n pub fn find_covering_segment(left: Vec, right: Vec) -> (ans: i32)\n requires\n left.len() == right.len(),\n 1 <= left.len() <= 100_000,\n forall|i: int| 0 <= i < left.len() ==> 1 <= #[trigger] left[i] <= right[i] <= 1_000_000_000,\n forall|i: int, j: int|\n 0 <= i < j < left.len() ==> left[i] != left[j] || right[i] != right[j],\n ensures\n 0 <= ans <= left.len(),\n ans != 0 ==> covers_all_segments(left@, right@, ans as int - 1),\n ans == 0 ==> forall|i: int| 0 <= i < left.len() ==> !covers_all_segments(left@, right@, i),\n {\n let mut candidate = 0usize;\n let mut i = 1usize;\n while i < left.len() {\n if left[i] < left[candidate] || (left[i] == left[candidate] && right[candidate] < right[i]) {\n candidate = i;\n }\n i += 1;\n }\n\n let mut j = 0usize;\n while j < left.len() {\n if left[candidate] > left[j] || right[j] > right[candidate] {\n return 0;\n }\n j += 1;\n }\n\n candidate as i32 + 1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn segment_contains(left: Seq, right: Seq, i: int, j: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= i < left.len(),\n 0 <= j < left.len(),\n{\n left[i] <= left[j] && right[j] <= right[i]\n}\n\npub open spec fn covers_all_segments(left: Seq, right: Seq, i: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= i < left.len(),\n{\n forall|j: int| 0 <= j < left.len() ==> #[trigger] segment_contains(left, right, i, j)\n}\n\npub open spec fn same_left(left: Seq, i: int, j: int) -> bool\n recommends\n 0 <= i < left.len(),\n 0 <= j < left.len(),\n{\n left[i] == left[j]\n}\n\npub open spec fn better_candidate(left: Seq, right: Seq, new_idx: int, old_idx: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= new_idx < left.len(),\n 0 <= old_idx < left.len(),\n{\n left[new_idx] < left[old_idx] || (left[new_idx] == left[old_idx] && right[old_idx] < right[new_idx])\n}\n\npub open spec fn candidate_is_min_left(left: Seq, candidate: int, bound: int) -> bool\n recommends\n 0 <= candidate < bound <= left.len(),\n{\n forall|j: int| 0 <= j < bound ==> left[candidate] <= #[trigger] left[j]\n}\n\npub open spec fn candidate_is_max_right_with_min_left(left: Seq, right: Seq, candidate: int, bound: int) -> bool\n recommends\n left.len() == right.len(),\n 0 <= candidate < bound <= left.len(),\n{\n forall|j: int| 0 <= j < bound && #[trigger] same_left(left, candidate, j) ==> right[j] <= right[candidate]\n}\n\nproof fn lemma_keep_candidate(left: Seq, right: Seq, candidate: int, bound: int)\n requires\n left.len() == right.len(),\n 0 <= candidate < bound < left.len(),\n candidate_is_min_left(left, candidate, bound),\n candidate_is_max_right_with_min_left(left, right, candidate, bound),\n !better_candidate(left, right, bound, candidate),\n ensures\n candidate_is_min_left(left, candidate, bound + 1),\n candidate_is_max_right_with_min_left(left, right, candidate, bound + 1),\n{\n assert forall|j: int| 0 <= j < bound + 1 implies left[candidate] <= #[trigger] left[j] by {\n if j < bound {\n } else {\n assert(j == bound);\n assert(!(left[bound] < left[candidate] || (left[bound] == left[candidate] && right[candidate] < right[bound])));\n if left[bound] < left[candidate] {\n assert(false);\n }\n }\n }\n assert forall|j: int| 0 <= j < bound + 1 && #[trigger] same_left(left, candidate, j) implies right[j] <= right[candidate] by {\n if j < bound {\n } else {\n assert(j == bound);\n assert(left[bound] == left[candidate]);\n assert(!(left[bound] < left[candidate] || (left[bound] == left[candidate] && right[candidate] < right[bound])));\n if right[candidate] < right[bound] {\n assert(left[bound] == left[candidate] && right[candidate] < right[bound]);\n assert(false);\n }\n }\n }\n}\n\nproof fn lemma_take_new_candidate(left: Seq, right: Seq, old_candidate: int, bound: int)\n requires\n left.len() == right.len(),\n 0 <= old_candidate < bound < left.len(),\n candidate_is_min_left(left, old_candidate, bound),\n candidate_is_max_right_with_min_left(left, right, old_candidate, bound),\n better_candidate(left, right, bound, old_candidate),\n ensures\n candidate_is_min_left(left, bound, bound + 1),\n candidate_is_max_right_with_min_left(left, right, bound, bound + 1),\n{\n assert forall|j: int| 0 <= j < bound + 1 implies left[bound] <= #[trigger] left[j] by {\n if j < bound {\n assert(left[old_candidate] <= left[j]);\n if left[bound] < left[old_candidate] {\n } else {\n assert(left[bound] == left[old_candidate]);\n }\n } else {\n assert(j == bound);\n }\n }\n assert forall|j: int| 0 <= j < bound + 1 && #[trigger] same_left(left, bound, j) implies right[j] <= right[bound] by {\n if j < bound {\n assert(left[j] == left[bound]);\n assert(left[old_candidate] <= left[j]);\n if left[bound] < left[old_candidate] {\n assert(left[old_candidate] <= left[bound]);\n assert(false);\n }\n assert(left[bound] == left[old_candidate]);\n assert(same_left(left, old_candidate, j));\n assert(right[j] <= right[old_candidate]);\n assert(better_candidate(left, right, bound, old_candidate));\n assert(right[old_candidate] < right[bound]);\n } else {\n assert(j == bound);\n }\n }\n}\n\nproof fn lemma_covering_implies_candidate_covering(left: Seq, right: Seq, candidate: int, big: int)\n requires\n left.len() == right.len(),\n 0 <= candidate < left.len(),\n 0 <= big < left.len(),\n candidate_is_min_left(left, candidate, left.len() as int),\n candidate_is_max_right_with_min_left(left, right, candidate, left.len() as int),\n covers_all_segments(left, right, big),\n ensures\n covers_all_segments(left, right, candidate),\n{\n assert(left[candidate] <= left[big]);\n assert(segment_contains(left, right, big, candidate));\n assert(left[big] <= left[candidate]);\n assert(left[big] == left[candidate]);\n assert(same_left(left, candidate, big));\n assert(right[big] <= right[candidate]);\n assert forall|j: int| 0 <= j < left.len() implies #[trigger] segment_contains(left, right, candidate, j) by {\n assert(segment_contains(left, right, big, j));\n assert(left[candidate] <= left[big]);\n assert(left[big] <= left[j]);\n assert(right[j] <= right[big]);\n assert(right[big] <= right[candidate]);\n }\n}\n\nimpl Solution {\n pub fn find_covering_segment(left: Vec, right: Vec) -> (ans: i32)\n requires\n left.len() == right.len(),\n 1 <= left.len() <= 100_000,\n forall|i: int| 0 <= i < left.len() ==> 1 <= #[trigger] left[i] <= right[i] <= 1_000_000_000,\n forall|i: int, j: int|\n 0 <= i < j < left.len() ==> left[i] != left[j] || right[i] != right[j],\n ensures\n 0 <= ans <= left.len(),\n ans != 0 ==> covers_all_segments(left@, right@, ans as int - 1),\n ans == 0 ==> forall|i: int| 0 <= i < left.len() ==> !covers_all_segments(left@, right@, i),\n {\n let mut candidate = 0usize;\n let mut i = 1usize;\n while i < left.len()\n invariant\n left.len() == right.len(),\n 1 <= left.len() <= 100_000,\n forall|k: int| 0 <= k < left.len() ==> 1 <= #[trigger] left[k] <= right[k] <= 1_000_000_000,\n forall|a: int, b: int| 0 <= a < b < left.len() ==> left[a] != left[b] || right[a] != right[b],\n 1 <= i <= left.len(),\n 0 <= candidate < i,\n candidate_is_min_left(left@, candidate as int, i as int),\n candidate_is_max_right_with_min_left(left@, right@, candidate as int, i as int),\n decreases left.len() - i,\n {\n let ghost old_candidate = candidate;\n if left[i] < left[candidate] || (left[i] == left[candidate] && right[candidate] < right[i]) {\n proof {\n lemma_take_new_candidate(left@, right@, candidate as int, i as int);\n }\n candidate = i;\n } else {\n proof {\n lemma_keep_candidate(left@, right@, candidate as int, i as int);\n }\n }\n i += 1;\n proof {\n if candidate == old_candidate {\n assert(candidate_is_min_left(left@, candidate as int, i as int));\n assert(candidate_is_max_right_with_min_left(left@, right@, candidate as int, i as int));\n } else {\n assert(candidate == i - 1);\n assert(candidate_is_min_left(left@, candidate as int, i as int));\n assert(candidate_is_max_right_with_min_left(left@, right@, candidate as int, i as int));\n }\n }\n }\n\n let mut j = 0usize;\n while j < left.len()\n invariant\n left.len() == right.len(),\n 1 <= left.len() <= 100_000,\n forall|k: int| 0 <= k < left.len() ==> 1 <= #[trigger] left[k] <= right[k] <= 1_000_000_000,\n forall|a: int, b: int| 0 <= a < b < left.len() ==> left[a] != left[b] || right[a] != right[b],\n 0 <= candidate < left.len(),\n candidate_is_min_left(left@, candidate as int, left.len() as int),\n candidate_is_max_right_with_min_left(left@, right@, candidate as int, left.len() as int),\n 0 <= j <= left.len(),\n forall|k: int| 0 <= k < j ==> #[trigger] segment_contains(left@, right@, candidate as int, k),\n decreases left.len() - j,\n {\n if left[candidate] > left[j] || right[j] > right[candidate] {\n proof {\n assert(left[candidate as int] <= left[j as int]);\n assert(!segment_contains(left@, right@, candidate as int, j as int));\n assert forall|k: int| 0 <= k < left.len() implies !covers_all_segments(left@, right@, k) by {\n if covers_all_segments(left@, right@, k) {\n lemma_covering_implies_candidate_covering(left@, right@, candidate as int, k);\n assert(segment_contains(left@, right@, candidate as int, j as int));\n }\n }\n }\n return 0;\n }\n proof {\n assert(segment_contains(left@, right@, candidate as int, j as int));\n }\n j += 1;\n }\n\n proof {\n assert(forall|k: int| 0 <= k < left.len() ==> #[trigger] segment_contains(left@, right@, candidate as int, k));\n assert(covers_all_segments(left@, right@, candidate as int));\n }\n candidate as i32 + 1\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn find_covering_segment(left: Vec, right: Vec) -> i32 {\n let mut candidate = 0usize;\n let mut i = 1usize;\n while i < left.len() {\n if left[i] < left[candidate] || (left[i] == left[candidate] && right[candidate] < right[i]) {\n candidate = i;\n }\n i += 1;\n }\n\n let mut j = 0usize;\n while j < left.len() {\n if left[candidate] > left[j] || right[j] > right[candidate] {\n return 0;\n }\n j += 1;\n }\n\n candidate as i32 + 1\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let n = it.next().expect(\"n\").parse::().expect(\"usize\");\n let mut left = Vec::with_capacity(n);\n let mut right = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n left.push(it.next().expect(\"left\").parse::().expect(\"i32\"));\n right.push(it.next().expect(\"right\").parse::().expect(\"i32\"));\n i += 1;\n }\n let ans = Solution::find_covering_segment(left, right);\n if ans == 0 {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf242B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf242B/tests/mutated_testcases.jsonl"} {"problem": "cf265A", "family": "codeforces", "description": "# Colorful Stones (Simplified Edition)\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nThere is a sequence of colorful stones. The color of each stone is one of red, green, or blue. You are given a string $s$. The $i$-th (1-based) character of $s$ represents the color of the $i$-th stone. If the character is \"`R`\", \"`G`\", or \"`B`\", the color of the corresponding stone is red, green, or blue, respectively.\n\nInitially Squirrel Liss is standing on the first stone. You perform instructions one or more times.\n\nEach instruction is one of the three types: \"`RED`\", \"`GREEN`\", or \"`BLUE`\". After an instruction $c$, if Liss is standing on a stone whose colors is $c$, Liss will move one stone forward, else she will not move.\n\nYou are given a string $t$. The number of instructions is equal to the length of $t$, and the $i$-th character of $t$ represents the $i$-th instruction.\n\nCalculate the final position of Liss (the number of the stone she is going to stand on in the end) after performing all the instructions, and print its 1-based position. It is guaranteed that Liss don't move out of the sequence.\n\n## Input\n\nThe input contains two lines. The first line contains the string $s$ ($1\u2009\u2264\u2009|s|\u2009\u2264\u200950$). The second line contains the string $t$ ($1\u2009\u2264\u2009|t|\u2009\u2264\u200950$). The characters of each string will be one of \"`R`\", \"`G`\", or \"`B`\". It is guaranteed that Liss don't move out of the sequence.\n\n## Output\n\nPrint the final 1-based position of Liss in a single line.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\nRGB\nRRR\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\nRRRBGBRBBB\nBBBRR\n```\n**Output:**\n```\n3\n```\n\n### Example 3\n\n**Input:**\n```\nBRRBGBRGRBGRGRRGGBGBGBRGBRGRGGGRBRRRBRBBBGRRRGGBBB\nBBRBGGRGRGBBBRBGRBRBBBBRBRRRBGBBGBBRRBBGGRBRRBRGRB\n```\n**Output:**\n```\n15\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn final_pos(s: Vec, t: Vec) -> usize {\n \n }\n}\n\nfn map_color(b: u8) -> u8 {\n match b {\n b'R' => 0u8,\n b'G' => 1u8,\n b'B' => 2u8,\n _ => 0u8,\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = input.lines().map(str::trim).filter(|l| !l.is_empty());\n let s_str = lines.next().unwrap_or(\"\");\n let t_str = lines.next().unwrap_or(\"\");\n let s: Vec = s_str.bytes().map(map_color).collect();\n let t: Vec = t_str.bytes().map(map_color).collect();\n let ans = Solution::final_pos(s, t);\n writeln!(out, \"{}\", ans).unwrap();\n out.flush().unwrap();\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 42604 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn final_position(s: Seq, t: Seq, k: int) -> int\n recommends\n 0 <= k <= t.len(),\n s.len() >= 1,\n decreases k,\n{\n if k <= 0 {\n 0int\n } else {\n let prev = final_position(s, t, k - 1);\n if 0 <= prev && prev < s.len() && s[prev] == t[k - 1] {\n prev + 1\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn final_pos(s: Vec, t: Vec) -> (result: usize)\n requires\n 1 <= s.len() <= 50,\n 1 <= t.len() <= 50,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] <= 2u8,\n forall|i: int| 0 <= i < t.len() ==> #[trigger] t[i] <= 2u8,\n final_position(s@, t@, t.len() as int) < s.len(),\n ensures\n result as int == final_position(s@, t@, t.len() as int) + 1,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn final_pos(s: Vec, t: Vec) -> usize {\n let mut pos: usize = 0;\n let m = t.len();\n let n = s.len();\n let mut i: usize = 0;\n while i < m {\n if pos < n && s[pos] == t[i] {\n pos = pos + 1;\n }\n i = i + 1;\n }\n pos + 1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn final_position(s: Seq, t: Seq, k: int) -> int\n recommends\n 0 <= k <= t.len(),\n s.len() >= 1,\n decreases k,\n{\n if k <= 0 {\n 0int\n } else {\n let prev = final_position(s, t, k - 1);\n if 0 <= prev && prev < s.len() && s[prev] == t[k - 1] {\n prev + 1\n } else {\n prev\n }\n }\n}\n\nimpl Solution {\n pub fn final_pos(s: Vec, t: Vec) -> (result: usize)\n requires\n 1 <= s.len() <= 50,\n 1 <= t.len() <= 50,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] <= 2u8,\n forall|i: int| 0 <= i < t.len() ==> #[trigger] t[i] <= 2u8,\n final_position(s@, t@, t.len() as int) < s.len(),\n ensures\n result as int == final_position(s@, t@, t.len() as int) + 1,\n {\n let mut pos: usize = 0;\n let m = t.len();\n let n = s.len();\n let mut i: usize = 0;\n while i < m {\n if pos < n && s[pos] == t[i] {\n pos = pos + 1;\n }\n i = i + 1;\n }\n pos + 1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn final_position(s: Seq, t: Seq, k: int) -> int\n recommends\n 0 <= k <= t.len(),\n s.len() >= 1,\n decreases k,\n{\n if k <= 0 {\n 0int\n } else {\n let prev = final_position(s, t, k - 1);\n if 0 <= prev && prev < s.len() && s[prev] == t[k - 1] {\n prev + 1\n } else {\n prev\n }\n }\n}\n\nproof fn lemma_final_position_bounds(s: Seq, t: Seq, k: int)\n requires\n 0 <= k <= t.len(),\n ensures\n 0 <= final_position(s, t, k) <= k,\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_final_position_bounds(s, t, k - 1);\n }\n}\n\nimpl Solution {\n pub fn final_pos(s: Vec, t: Vec) -> (result: usize)\n requires\n 1 <= s.len() <= 50,\n 1 <= t.len() <= 50,\n forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i] <= 2u8,\n forall|i: int| 0 <= i < t.len() ==> #[trigger] t[i] <= 2u8,\n final_position(s@, t@, t.len() as int) < s.len(),\n ensures\n result as int == final_position(s@, t@, t.len() as int) + 1,\n {\n let mut pos: usize = 0;\n let m = t.len();\n let n = s.len();\n let mut i: usize = 0;\n while i < m\n invariant\n 0 <= i <= m,\n m == t.len(),\n n == s.len(),\n 1 <= n <= 50,\n 1 <= m <= 50,\n pos as int == final_position(s@, t@, i as int),\n pos <= i,\n decreases m - i,\n {\n proof {\n lemma_final_position_bounds(s@, t@, i as int + 1);\n }\n if pos < n && s[pos] == t[i] {\n pos = pos + 1;\n }\n i = i + 1;\n }\n pos + 1\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn final_pos(s: Vec, t: Vec) -> usize {\n let mut pos: usize = 0;\n let m = t.len();\n let n = s.len();\n let mut i: usize = 0;\n while i < m {\n if pos < n && s[pos] == t[i] {\n pos = pos + 1;\n }\n i = i + 1;\n }\n pos + 1\n }\n}\n\nfn map_color(b: u8) -> u8 {\n match b {\n b'R' => 0u8,\n b'G' => 1u8,\n b'B' => 2u8,\n _ => 0u8,\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut lines = input.lines().map(str::trim).filter(|l| !l.is_empty());\n let s_str = lines.next().unwrap_or(\"\");\n let t_str = lines.next().unwrap_or(\"\");\n let s: Vec = s_str.bytes().map(map_color).collect();\n let t: Vec = t_str.bytes().map(map_color).collect();\n let ans = Solution::final_pos(s, t);\n writeln!(out, \"{}\", ans).unwrap();\n out.flush().unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf265A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf265A/tests/mutated_testcases.jsonl"} {"problem": "cf265B", "family": "codeforces", "description": "# Roadside Trees (Simplified Edition)\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nSquirrel Liss loves nuts. There are $n$ trees (numbered $1$ to $n$ from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree $i$ is $h_i$. Liss wants to eat all nuts.\n\nNow Liss is on the root of the tree with the number $1$. In one second Liss can perform one of the following actions:\n - Walk up or down one unit on a tree. \n- Eat a nut on the top of the current tree. \n- Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height $h$ of the tree $i$ ($1\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091$), she jumps to height $h$ of the tree $i\u2009+\u20091$. This action can't be performed if $h\u2009>\u2009h_i\u2009+\u20091$. \n\nCompute the minimal time (in seconds) required to eat all nuts.\n\n## Input\n\nThe first line contains an integer $n$ ($1\u2009\u2009\u2264\u2009\u2009n\u2009\u2264\u200910^5$) \u2014 the number of trees.\n\nNext $n$ lines contains the height of trees: $i$-th line contains an integer $h_i$ ($1\u2009\u2264\u2009h_i\u2009\u2264\u200910^4$) \u2014 the height of the tree with the number $i$.\n\n## Output\n\nPrint a single integer \u2014 the minimal time required to eat all nuts in seconds.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2\n1\n2\n```\n**Output:**\n```\n5\n```\n\n### Example 2\n\n**Input:**\n```\n5\n2\n1\n2\n1\n1\n```\n**Output:**\n```\n14\n```\n\n## Starter Code\n\n```rust\nuse std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_time(heights: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n let mut heights = Vec::new();\n for _ in 0..n {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let h: i32 = line.trim().parse().unwrap();\n heights.push(h);\n }\n println!(\"{}\", Solution::min_time(heights));\n}\n```\n", "tags": "Rating: 1000\ngreedy, implementation\nSolved by: 17884 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_time(heights: Seq, i: int, cur_h: int) -> int\n decreases heights.len() - i,\n{\n if i >= heights.len() {\n 0\n } else {\n let target = heights[i] as int;\n let climb = if target > cur_h { target - cur_h } else { cur_h - target };\n if i < heights.len() as int - 1 {\n let next_max = heights[i + 1] as int + 1;\n let extra = if target > next_max { target - next_max } else { 0int };\n let new_h = if target > next_max { next_max } else { target };\n climb + 1 + extra + 1 + spec_time(heights, i + 1, new_h)\n } else {\n climb + 1\n }\n }\n}\n\nimpl Solution {\n pub fn min_time(heights: Vec) -> (result: i64)\n requires\n 1 <= heights.len() <= 100000,\n forall|i: int| 0 <= i < heights.len() ==> 1 <= #[trigger] heights[i] <= 10000,\n ensures\n result as int == spec_time(heights@, 0, 0),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_time(heights: Vec) -> i64 {\n let n = heights.len();\n let mut time = 0i64;\n let mut current_height = 0i32;\n let mut i: usize = 0;\n while i < n {\n let target_height = heights[i];\n let climb: i64 = if target_height > current_height {\n (target_height - current_height) as i64\n } else {\n (current_height - target_height) as i64\n };\n time = time + climb;\n current_height = target_height;\n time = time + 1;\n if i < n - 1 {\n let next_tree_height = heights[i + 1];\n let max_jump_height = next_tree_height + 1;\n if current_height > max_jump_height {\n let climb_down = (current_height - max_jump_height) as i64;\n time = time + climb_down;\n current_height = max_jump_height;\n }\n time = time + 1;\n }\n i = i + 1;\n }\n time\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_time(heights: Seq, i: int, cur_h: int) -> int\n decreases heights.len() - i,\n{\n if i >= heights.len() {\n 0\n } else {\n let target = heights[i] as int;\n let climb = if target > cur_h { target - cur_h } else { cur_h - target };\n if i < heights.len() as int - 1 {\n let next_max = heights[i + 1] as int + 1;\n let extra = if target > next_max { target - next_max } else { 0int };\n let new_h = if target > next_max { next_max } else { target };\n climb + 1 + extra + 1 + spec_time(heights, i + 1, new_h)\n } else {\n climb + 1\n }\n }\n}\n\nimpl Solution {\n pub fn min_time(heights: Vec) -> (result: i64)\n requires\n 1 <= heights.len() <= 100000,\n forall|i: int| 0 <= i < heights.len() ==> 1 <= #[trigger] heights[i] <= 10000,\n ensures\n result as int == spec_time(heights@, 0, 0),\n {\n let n = heights.len();\n let mut time = 0i64;\n let mut current_height = 0i32;\n let mut i: usize = 0;\n while i < n {\n let target_height = heights[i];\n let climb: i64 = if target_height > current_height {\n (target_height - current_height) as i64\n } else {\n (current_height - target_height) as i64\n };\n time = time + climb;\n current_height = target_height;\n time = time + 1;\n if i < n - 1 {\n let next_tree_height = heights[i + 1];\n let max_jump_height = next_tree_height + 1;\n if current_height > max_jump_height {\n let climb_down = (current_height - max_jump_height) as i64;\n time = time + climb_down;\n current_height = max_jump_height;\n }\n time = time + 1;\n }\n i = i + 1;\n }\n time\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_time(heights: Seq, i: int, cur_h: int) -> int\n decreases heights.len() - i,\n{\n if i >= heights.len() {\n 0\n } else {\n let target = heights[i] as int;\n let climb = if target > cur_h { target - cur_h } else { cur_h - target };\n if i < heights.len() as int - 1 {\n let next_max = heights[i + 1] as int + 1;\n let extra = if target > next_max { target - next_max } else { 0int };\n let new_h = if target > next_max { next_max } else { target };\n climb + 1 + extra + 1 + spec_time(heights, i + 1, new_h)\n } else {\n climb + 1\n }\n }\n}\n\nproof fn lemma_spec_time_nonneg(heights: Seq, i: int, cur_h: int)\n requires\n i >= 0,\n cur_h >= 0,\n forall|j: int| 0 <= j < heights.len() ==> 1 <= #[trigger] heights[j] <= 10000,\n ensures\n spec_time(heights, i, cur_h) >= 0,\n decreases heights.len() - i,\n{\n if i < heights.len() as int {\n if i < heights.len() as int - 1 {\n let target = heights[i] as int;\n let next_max = heights[i + 1] as int + 1;\n let new_h: int = if target > next_max { next_max } else { target };\n lemma_spec_time_nonneg(heights, i + 1, new_h);\n }\n }\n}\n\nproof fn lemma_spec_time_upper(heights: Seq, i: int, cur_h: int)\n requires\n 0 <= i <= heights.len(),\n 0 <= cur_h <= 10001,\n heights.len() <= 100000,\n forall|j: int| 0 <= j < heights.len() ==> 1 <= #[trigger] heights[j] <= 10000,\n ensures\n spec_time(heights, i, cur_h) <= 20003 * (heights.len() as int - i),\n decreases heights.len() as int - i,\n{\n if i < heights.len() as int {\n if i < heights.len() as int - 1 {\n let target = heights[i] as int;\n let next_max = heights[i + 1] as int + 1;\n let new_h: int = if target > next_max { next_max } else { target };\n assert(0 <= new_h <= 10001);\n lemma_spec_time_upper(heights, i + 1, new_h);\n assert(20003 + 20003 * (heights.len() as int - i - 1)\n == 20003 * (heights.len() as int - i)) by (nonlinear_arith)\n requires i + 1 <= heights.len() as int;\n }\n }\n}\n\nimpl Solution {\n pub fn min_time(heights: Vec) -> (result: i64)\n requires\n 1 <= heights.len() <= 100000,\n forall|i: int| 0 <= i < heights.len() ==> 1 <= #[trigger] heights[i] <= 10000,\n ensures\n result as int == spec_time(heights@, 0, 0),\n {\n let n = heights.len();\n let mut time = 0i64;\n let mut current_height = 0i32;\n let mut i: usize = 0;\n\n proof {\n lemma_spec_time_upper(heights@, 0, 0);\n assert(spec_time(heights@, 0, 0) <= 2_000_300_000) by (nonlinear_arith)\n requires\n spec_time(heights@, 0, 0) <= 20003 * (heights@.len() as int),\n heights@.len() <= 100000;\n lemma_spec_time_nonneg(heights@, 0, 0);\n }\n\n let ghost total = spec_time(heights@, 0, 0);\n\n while i < n\n invariant\n 0 <= i <= n,\n n == heights.len(),\n 1 <= n <= 100000,\n 0 <= current_height <= 10001,\n 0 <= time,\n time as int == total - spec_time(heights@, i as int, current_height as int),\n total == spec_time(heights@, 0, 0),\n 0 <= total <= 2_000_300_000,\n forall|j: int| 0 <= j < heights.len() ==> 1 <= #[trigger] heights[j] <= 10000,\n decreases n - i,\n {\n proof {\n lemma_spec_time_nonneg(heights@, i as int, current_height as int);\n }\n\n let target_height = heights[i];\n let climb: i64 = if target_height > current_height {\n (target_height - current_height) as i64\n } else {\n (current_height - target_height) as i64\n };\n time = time + climb;\n current_height = target_height;\n time = time + 1;\n\n if i < n - 1 {\n let next_tree_height = heights[i + 1];\n let max_jump_height = next_tree_height + 1;\n if current_height > max_jump_height {\n let climb_down = (current_height - max_jump_height) as i64;\n time = time + climb_down;\n current_height = max_jump_height;\n }\n time = time + 1;\n }\n\n proof {\n assert(time as int == total\n - spec_time(heights@, (i + 1) as int, current_height as int));\n }\n\n i = i + 1;\n }\n\n time\n }\n}\n\n}\n", "main": "use std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_time(heights: Vec) -> i64 {\n let n = heights.len();\n let mut time = 0i64;\n let mut current_height = 0i32;\n let mut i: usize = 0;\n while i < n {\n let target_height = heights[i];\n let climb: i64 = if target_height > current_height {\n (target_height - current_height) as i64\n } else {\n (current_height - target_height) as i64\n };\n time = time + climb;\n current_height = target_height;\n time = time + 1;\n if i < n - 1 {\n let next_tree_height = heights[i + 1];\n let max_jump_height = next_tree_height + 1;\n if current_height > max_jump_height {\n let climb_down = (current_height - max_jump_height) as i64;\n time = time + climb_down;\n current_height = max_jump_height;\n }\n time = time + 1;\n }\n i = i + 1;\n }\n time\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n let mut heights = Vec::new();\n for _ in 0..n {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let h: i32 = line.trim().parse().unwrap();\n heights.push(h);\n }\n println!(\"{}\", Solution::min_time(heights));\n}\n", "checker": null, "testcases_path": "codeforces/cf265B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf265B/tests/mutated_testcases.jsonl"} {"problem": "cf287B", "family": "codeforces", "description": "# Pipeline\n\nTime limit: 0.4 seconds | Memory limit: 256 megabytes\n\nVova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly $n$ houses in Ultimate Thule, Vova wants the city to have exactly $n$ pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.\n\nA splitter is a construction that consists of one input (it can be connected to a water pipe) and $x$ output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.\n The figure shows a $4$-output splitter \nVova has one splitter of each kind: with $2$, $3$, $4$, ..., $k$ outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.\n\nVova needs the pipeline to have exactly $n$ pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.\n\n## Input\n\nThe first line contains two space-separated integers $n$ and $k$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^18$, $2\u2009\u2264\u2009k\u2009\u2264\u200910^9$).\n\nPlease, do not use the `%lld` specifier to read or write 64-bit integers in \u0421++. It is preferred to use the `cin`, `cout` streams or the `%I64d` specifier.\n\n## Output\n\nPrint a single integer \u2014 the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4 3\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n5 5\n```\n**Output:**\n```\n1\n```\n\n### Example 3\n\n**Input:**\n```\n8 4\n```\n**Output:**\n```\n-1\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_splitters(n: i128, k: i128) -> i128 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: i128 = it.next().unwrap().parse().unwrap();\n let k: i128 = it.next().unwrap().parse().unwrap();\n let ans = Solution::min_splitters(n, k);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 1700\nbinary search, math\nSolved by: 11422 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn max_additional(k: int, m: nat) -> int\n decreases m,\n{\n if m == 0 {\n 0\n } else {\n max_additional(k, (m - 1) as nat) + (k - m as int)\n }\n}\n\nimpl Solution {\n pub fn min_splitters(n: i128, k: i128) -> (res: i128)\n requires\n 1 <= n <= 1_000_000_000_000_000_000,\n 2 <= k <= 1_000_000_000,\n ensures\n n == 1 ==> res == 0,\n n > 1 && max_additional(k as int, (k - 1) as nat) < n as int - 1 ==> res == -1,\n n > 1 && max_additional(k as int, (k - 1) as nat) >= n as int - 1 ==> {\n 1 <= res < k\n && n as int - 1 <= max_additional(k as int, res as nat)\n && forall|m: int| 0 <= m < res as int ==> #[trigger] max_additional(k as int, m as nat) < n as int - 1\n },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn max_additional_exec(k: i128, m: i128) -> i128 {\n let res = m * (2 * k - m - 1) / 2;\n res\n }\n\n pub fn min_splitters(n: i128, k: i128) -> i128 {\n if n == 1 {\n return 0;\n }\n let need = n - 1;\n let total = Self::max_additional_exec(k, k - 1);\n if need > total {\n return -1;\n }\n let mut lo = 1i128;\n let mut hi = k - 1;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let max_mid = Self::max_additional_exec(k, mid);\n if max_mid >= need {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n lo\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn max_additional(k: int, m: nat) -> int\n decreases m,\n{\n if m == 0 {\n 0\n } else {\n max_additional(k, (m - 1) as nat) + (k - m as int)\n }\n}\n\nimpl Solution {\n fn max_additional_exec(k: i128, m: i128) -> (res: i128)\n requires\n 2 <= k <= 1_000_000_000,\n 0 <= m < k,\n ensures\n res as int == max_additional(k as int, m as nat),\n {\n let res = m * (2 * k - m - 1) / 2;\n res\n }\n\n pub fn min_splitters(n: i128, k: i128) -> (res: i128)\n requires\n 1 <= n <= 1_000_000_000_000_000_000,\n 2 <= k <= 1_000_000_000,\n ensures\n n == 1 ==> res == 0,\n n > 1 && max_additional(k as int, (k - 1) as nat) < n as int - 1 ==> res == -1,\n n > 1 && max_additional(k as int, (k - 1) as nat) >= n as int - 1 ==> {\n 1 <= res < k\n && n as int - 1 <= max_additional(k as int, res as nat)\n && forall|m: int| 0 <= m < res as int ==> #[trigger] max_additional(k as int, m as nat) < n as int - 1\n },\n {\n if n == 1 {\n return 0;\n }\n let need = n - 1;\n let total = Self::max_additional_exec(k, k - 1);\n if need > total {\n return -1;\n }\n let mut lo = 1i128;\n let mut hi = k - 1;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let max_mid = Self::max_additional_exec(k, mid);\n if max_mid >= need {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n lo\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn max_additional(k: int, m: nat) -> int\n decreases m,\n{\n if m == 0 {\n 0\n } else {\n max_additional(k, (m - 1) as nat) + (k - m as int)\n }\n}\n\nproof fn lemma_max_additional_step(k: int, m: nat)\n requires\n 2 <= k,\n m < k - 1,\n ensures\n max_additional(k, (m + 1) as nat) == max_additional(k, m) + (k - m as int - 1),\n{\n reveal_with_fuel(max_additional, 2);\n}\n\nproof fn lemma_max_additional_monotone(k: int, a: nat, b: nat)\n requires\n 2 <= k,\n a <= b < k,\n ensures\n max_additional(k, a) <= max_additional(k, b),\n decreases b - a,\n{\n if a < b {\n lemma_max_additional_monotone(k, a, (b - 1) as nat);\n lemma_max_additional_step(k, (b - 1) as nat);\n assert(k - b as int >= 0);\n }\n}\n\nproof fn lemma_max_additional_formula(k: int, m: nat)\n requires\n 2 <= k,\n m < k,\n ensures\n max_additional(k, m) == m as int * (2 * k - m as int - 1) / 2,\n decreases m,\n{\n if m == 0 {\n } else {\n lemma_max_additional_formula(k, (m - 1) as nat);\n lemma_max_additional_step(k, (m - 1) as nat);\n assert(max_additional(k, (m - 1) as nat) == (m as int - 1) * (2 * k - m as int) / 2);\n assert(max_additional(k, m) == max_additional(k, (m - 1) as nat) + (k - m as int));\n assert(max_additional(k, m) == m as int * (2 * k - m as int - 1) / 2) by (nonlinear_arith)\n requires\n max_additional(k, (m - 1) as nat) == (m as int - 1) * (2 * k - m as int) / 2,\n max_additional(k, m) == max_additional(k, (m - 1) as nat) + (k - m as int),\n m > 0;\n }\n}\n\nimpl Solution {\n fn max_additional_exec(k: i128, m: i128) -> (res: i128)\n requires\n 2 <= k <= 1_000_000_000,\n 0 <= m < k,\n ensures\n res as int == max_additional(k as int, m as nat),\n {\n proof {\n assert(0 <= m <= 999_999_999) by (nonlinear_arith)\n requires\n 2 <= k <= 1_000_000_000,\n 0 <= m < k;\n assert(1 <= 2 * k - m - 1 <= 1_999_999_999) by (nonlinear_arith)\n requires\n 2 <= k <= 1_000_000_000,\n 0 <= m < k;\n assert(m * (2 * k - m - 1) <= 2_000_000_000_000_000_000) by (nonlinear_arith)\n requires\n 0 <= m <= 999_999_999,\n 1 <= 2 * k - m - 1 <= 1_999_999_999;\n assert(m * (2 * k - m - 1) < 170141183460469231731687303715884105727) by (nonlinear_arith)\n requires\n m * (2 * k - m - 1) <= 2_000_000_000_000_000_000;\n }\n let res = m * (2 * k - m - 1) / 2;\n proof {\n lemma_max_additional_formula(k as int, m as nat);\n assert(res == m * (2 * k - m - 1) / 2);\n assert(res as int == m as int * (2 * k as int - m as int - 1) / 2);\n }\n res\n }\n\n pub fn min_splitters(n: i128, k: i128) -> (res: i128)\n requires\n 1 <= n <= 1_000_000_000_000_000_000,\n 2 <= k <= 1_000_000_000,\n ensures\n n == 1 ==> res == 0,\n n > 1 && max_additional(k as int, (k - 1) as nat) < n as int - 1 ==> res == -1,\n n > 1 && max_additional(k as int, (k - 1) as nat) >= n as int - 1 ==> {\n 1 <= res < k\n && n as int - 1 <= max_additional(k as int, res as nat)\n && forall|m: int| 0 <= m < res as int ==> #[trigger] max_additional(k as int, m as nat) < n as int - 1\n },\n {\n if n == 1 {\n return 0;\n }\n let need = n - 1;\n let total = Self::max_additional_exec(k, k - 1);\n if need > total {\n proof {\n assert forall|m: int| 0 <= m < k as int implies #[trigger] max_additional(k as int, m as nat) < n as int - 1 by {\n if m == k as int - 1 {\n assert(max_additional(k as int, m as nat) == max_additional(k as int, (k - 1) as nat));\n assert(max_additional(k as int, (k - 1) as nat) == total as int);\n } else {\n lemma_max_additional_monotone(k as int, m as nat, (k - 1) as nat);\n assert(max_additional(k as int, m as nat) <= max_additional(k as int, (k - 1) as nat));\n assert(max_additional(k as int, (k - 1) as nat) == total as int);\n }\n }\n }\n return -1;\n }\n let mut lo = 1i128;\n let mut hi = k - 1;\n while lo < hi\n invariant\n 1 <= n <= 1_000_000_000_000_000_000,\n 2 <= k <= 1_000_000_000,\n 1 <= need,\n 1 <= lo <= hi < k,\n need as int <= max_additional(k as int, hi as nat),\n lo == 1 || max_additional(k as int, (lo - 1) as nat) < need as int,\n decreases hi - lo,\n {\n let mid = lo + (hi - lo) / 2;\n proof {\n assert(lo <= mid < hi);\n }\n let max_mid = Self::max_additional_exec(k, mid);\n if max_mid >= need {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n proof {\n assert(lo == hi);\n assert(need as int <= max_additional(k as int, lo as nat));\n assert forall|m: int| 0 <= m < lo as int implies #[trigger] max_additional(k as int, m as nat) < n as int - 1 by {\n if lo == 1 {\n } else if m == lo as int - 1 {\n assert(max_additional(k as int, m as nat) == max_additional(k as int, (lo - 1) as nat));\n assert(max_additional(k as int, (lo - 1) as nat) < need as int);\n } else {\n lemma_max_additional_monotone(k as int, m as nat, (lo - 1) as nat);\n assert(max_additional(k as int, m as nat) <= max_additional(k as int, (lo - 1) as nat));\n assert(max_additional(k as int, (lo - 1) as nat) < need as int);\n }\n }\n }\n lo\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n fn max_additional_exec(k: i128, m: i128) -> i128 {\n let res = m * (2 * k - m - 1) / 2;\n res\n }\n\n pub fn min_splitters(n: i128, k: i128) -> i128 {\n if n == 1 {\n return 0;\n }\n let need = n - 1;\n let total = Self::max_additional_exec(k, k - 1);\n if need > total {\n return -1;\n }\n let mut lo = 1i128;\n let mut hi = k - 1;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let max_mid = Self::max_additional_exec(k, mid);\n if max_mid >= need {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n lo\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: i128 = it.next().unwrap().parse().unwrap();\n let k: i128 = it.next().unwrap().parse().unwrap();\n let ans = Solution::min_splitters(n, k);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf287B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf287B/tests/mutated_testcases.jsonl"} {"problem": "cf327A", "family": "codeforces", "description": "# Flipping Game\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nIahub got bored, so he invented a game to be played on paper. \n\nHe writes $n$ integers $a_1,\u2009a_2,\u2009...,\u2009a_n$. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices $i$ and $j$ ($1\u2009\u2264\u2009i\u2009\u2264\u2009j\u2009\u2264\u2009n$) and flips all values $a_k$ for which their positions are in range $[i,\u2009j]$ (that is $i\u2009\u2264\u2009k\u2009\u2264\u2009j$). Flip the value of $x$ means to apply operation $x\u2009=\u20091$ - $x$.\n\nThe goal of the game is that after **exactly** one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.\n\n## Input\n\nThe first line of the input contains an integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u2009100$). In the second line of the input there are $n$ integers: $a_1,\u2009a_2,\u2009...,\u2009a_n$. It is guaranteed that each of those $n$ values is either 0 or 1.\n\n## Output\n\nPrint an integer \u2014 the maximal number of 1s that can be obtained after exactly one move.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n1 0 0 1 0\n```\n**Output:**\n```\n4\n```\n\n### Example 2\n\n**Input:**\n```\n4\n1 0 0 1\n```\n**Output:**\n```\n4\n```\n\n## Note\n\nIn the first case, flip the segment from 2 to 5 $(i\u2009=\u20092,\u2009j\u2009=\u20095)$. That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].\n\nIn the second case, flipping only the second and the third element $(i\u2009=\u20092,\u2009j\u2009=\u20093)$ will turn all numbers into 1.\n\n## Starter Code\n\n```rust\nuse std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_ones_after_flip(a: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n \n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let a: Vec = input\n .trim()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n \n let result = Solution::max_ones_after_flip(a);\n println!(\"{}\", result);\n}\n```\n", "tags": "Rating: 1200\nbrute force, dp, implementation\nSolved by: 49232 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_ones(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n decreases end - start,\n {\n if start >= end {\n 0\n } else {\n Self::count_ones(nums, start + 1, end) + if nums[start] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn total_ones(nums: Seq) -> int {\n Self::count_ones(nums, 0, nums.len() as int)\n }\n\n pub open spec fn flip_value(x: i32) -> i32 {\n (1int - x as int) as i32\n }\n\n pub open spec fn after_flip(nums: Seq, i: int, j: int, k: int) -> i32\n recommends\n 0 <= i <= j < nums.len(),\n 0 <= k < nums.len(),\n {\n if i <= k && k <= j {\n Self::flip_value(nums[k])\n } else {\n nums[k]\n }\n }\n\n pub open spec fn count_ones_after_flip(nums: Seq, i: int, j: int, end: int) -> int\n recommends\n 0 <= i <= j < nums.len(),\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_ones_after_flip(nums, i, j, end - 1) + if Self::after_flip(nums, i, j, end - 1) == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn count_ones_after_flip_window(nums: Seq, i: int, j: int) -> int\n recommends\n 0 <= i <= j < nums.len(),\n {\n Self::count_ones_after_flip(nums, i, j, nums.len() as int)\n }\n\n pub open spec fn max_ones_over_windows(nums: Seq, i: int, j_end: int) -> int\n recommends\n 0 <= i <= j_end <= nums.len(),\n decreases j_end - i,\n {\n if i >= j_end {\n 0\n } else {\n let current = Self::count_ones_after_flip_window(nums, i, j_end - 1);\n let rest = Self::max_ones_over_windows(nums, i, j_end - 1);\n if current > rest { current } else { rest }\n }\n }\n\n pub open spec fn max_ones_all_windows(nums: Seq, i_end: int) -> int\n recommends\n 0 <= i_end <= nums.len(),\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n let current_row = Self::max_ones_over_windows(nums, i_end - 1, nums.len() as int);\n let rest = Self::max_ones_all_windows(nums, i_end - 1);\n if current_row > rest { current_row } else { rest }\n }\n }\n\n pub open spec fn max_ones_after_one_flip(nums: Seq) -> int {\n if nums.len() == 0 {\n 0\n } else {\n Self::max_ones_all_windows(nums, nums.len() as int)\n }\n }\n\n pub fn max_ones_after_flip(a: Vec) -> (result: i32)\n requires\n 1 <= a.len() <= 100,\n forall |k: int| 0 <= k < a.len() ==> (#[trigger] a[k] == 0 || a[k] == 1),\n ensures\n result == Self::max_ones_after_one_flip(a@),\n forall |i: int, j: int| 0 <= i <= j < a.len() ==>\n Self::count_ones_after_flip_window(a@, i, j) <= result,\n exists |i: int, j: int| 0 <= i <= j < a.len() &&\n Self::count_ones_after_flip_window(a@, i, j) == result,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_ones_after_flip(a: Vec) -> i32 {\n let n = a.len();\n let mut result = 0;\n let mut i = 0;\n while i < n {\n let mut j = i;\n while j < n {\n let mut count = 0;\n let mut k = 0;\n while k < n {\n let val = if i <= k && k <= j {\n 1 - a[k]\n } else {\n a[k]\n };\n if val == 1 {\n count = count + 1;\n }\n k = k + 1;\n }\n if count > result {\n result = count;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_ones(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n decreases end - start,\n {\n if start >= end {\n 0\n } else {\n Self::count_ones(nums, start + 1, end) + if nums[start] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn total_ones(nums: Seq) -> int {\n Self::count_ones(nums, 0, nums.len() as int)\n }\n\n pub open spec fn flip_value(x: i32) -> i32 {\n (1int - x as int) as i32\n }\n\n pub open spec fn after_flip(nums: Seq, i: int, j: int, k: int) -> i32\n recommends\n 0 <= i <= j < nums.len(),\n 0 <= k < nums.len(),\n {\n if i <= k && k <= j {\n Self::flip_value(nums[k])\n } else {\n nums[k]\n }\n }\n\n pub open spec fn count_ones_after_flip(nums: Seq, i: int, j: int, end: int) -> int\n recommends\n 0 <= i <= j < nums.len(),\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_ones_after_flip(nums, i, j, end - 1) + if Self::after_flip(nums, i, j, end - 1) == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn count_ones_after_flip_window(nums: Seq, i: int, j: int) -> int\n recommends\n 0 <= i <= j < nums.len(),\n {\n Self::count_ones_after_flip(nums, i, j, nums.len() as int)\n }\n\n pub open spec fn max_ones_over_windows(nums: Seq, i: int, j_end: int) -> int\n recommends\n 0 <= i <= j_end <= nums.len(),\n decreases j_end - i,\n {\n if i >= j_end {\n 0\n } else {\n let current = Self::count_ones_after_flip_window(nums, i, j_end - 1);\n let rest = Self::max_ones_over_windows(nums, i, j_end - 1);\n if current > rest { current } else { rest }\n }\n }\n\n pub open spec fn max_ones_all_windows(nums: Seq, i_end: int) -> int\n recommends\n 0 <= i_end <= nums.len(),\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n let current_row = Self::max_ones_over_windows(nums, i_end - 1, nums.len() as int);\n let rest = Self::max_ones_all_windows(nums, i_end - 1);\n if current_row > rest { current_row } else { rest }\n }\n }\n\n pub open spec fn max_ones_after_one_flip(nums: Seq) -> int {\n if nums.len() == 0 {\n 0\n } else {\n Self::max_ones_all_windows(nums, nums.len() as int)\n }\n }\n\n pub fn max_ones_after_flip(a: Vec) -> (result: i32)\n requires\n 1 <= a.len() <= 100,\n forall |k: int| 0 <= k < a.len() ==> (#[trigger] a[k] == 0 || a[k] == 1),\n ensures\n result == Self::max_ones_after_one_flip(a@),\n forall |i: int, j: int| 0 <= i <= j < a.len() ==>\n Self::count_ones_after_flip_window(a@, i, j) <= result,\n exists |i: int, j: int| 0 <= i <= j < a.len() &&\n Self::count_ones_after_flip_window(a@, i, j) == result,\n {\n let n = a.len();\n let mut result = 0;\n let mut i = 0;\n while i < n\n decreases n - i,\n {\n let mut j = i;\n while j < n\n decreases n - j,\n {\n let mut count = 0;\n let mut k = 0;\n while k < n\n decreases n - k,\n {\n let val = if i <= k && k <= j {\n 1 - a[k]\n } else {\n a[k]\n };\n if val == 1 {\n count = count + 1;\n }\n k = k + 1;\n }\n if count > result {\n result = count;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_ones(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n decreases end - start,\n {\n if start >= end {\n 0\n } else {\n Self::count_ones(nums, start + 1, end) + if nums[start] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn total_ones(nums: Seq) -> int {\n Self::count_ones(nums, 0, nums.len() as int)\n }\n\n pub open spec fn flip_value(x: i32) -> i32 {\n (1int - x as int) as i32\n }\n\n pub open spec fn after_flip(nums: Seq, i: int, j: int, k: int) -> i32\n recommends\n 0 <= i <= j < nums.len(),\n 0 <= k < nums.len(),\n {\n if i <= k && k <= j {\n Self::flip_value(nums[k])\n } else {\n nums[k]\n }\n }\n\n pub open spec fn count_ones_after_flip(nums: Seq, i: int, j: int, end: int) -> int\n recommends\n 0 <= i <= j < nums.len(),\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_ones_after_flip(nums, i, j, end - 1) + if Self::after_flip(nums, i, j, end - 1) == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn count_ones_after_flip_window(nums: Seq, i: int, j: int) -> int\n recommends\n 0 <= i <= j < nums.len(),\n {\n Self::count_ones_after_flip(nums, i, j, nums.len() as int)\n }\n\n pub open spec fn max_ones_over_windows(nums: Seq, i: int, j_end: int) -> int\n recommends\n 0 <= i <= j_end <= nums.len(),\n decreases j_end - i,\n {\n if i >= j_end {\n 0\n } else {\n let current = Self::count_ones_after_flip_window(nums, i, j_end - 1);\n let rest = Self::max_ones_over_windows(nums, i, j_end - 1);\n if current > rest { current } else { rest }\n }\n }\n\n pub open spec fn max_ones_all_windows(nums: Seq, i_end: int) -> int\n recommends\n 0 <= i_end <= nums.len(),\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n let current_row = Self::max_ones_over_windows(nums, i_end - 1, nums.len() as int);\n let rest = Self::max_ones_all_windows(nums, i_end - 1);\n if current_row > rest { current_row } else { rest }\n }\n }\n\n pub open spec fn max_ones_after_one_flip(nums: Seq) -> int {\n if nums.len() == 0 {\n 0\n } else {\n Self::max_ones_all_windows(nums, nums.len() as int)\n }\n }\n\n proof fn lemma_count_nonneg(nums: Seq, i: int, j: int, end: int)\n requires 0 <= end,\n ensures Self::count_ones_after_flip(nums, i, j, end) >= 0,\n decreases end,\n {\n if end > 0 {\n Self::lemma_count_nonneg(nums, i, j, end - 1);\n }\n }\n\n proof fn lemma_mow_bound(nums: Seq, i: int, j_end: int, j: int)\n requires\n 0 <= i <= j < j_end <= nums.len(),\n ensures\n Self::count_ones_after_flip_window(nums, i, j) <= Self::max_ones_over_windows(nums, i, j_end),\n decreases j_end - i,\n {\n if j < j_end - 1 {\n Self::lemma_mow_bound(nums, i, j_end - 1, j);\n }\n }\n\n proof fn lemma_maw_bound(nums: Seq, i_end: int, i: int, j: int)\n requires\n 0 <= i <= j < nums.len(),\n i < i_end <= nums.len(),\n ensures\n Self::count_ones_after_flip_window(nums, i, j) <= Self::max_ones_all_windows(nums, i_end),\n decreases i_end,\n {\n if i == i_end - 1 {\n Self::lemma_mow_bound(nums, i, nums.len() as int, j);\n } else {\n Self::lemma_maw_bound(nums, i_end - 1, i, j);\n }\n }\n\n proof fn lemma_mow_achieved(nums: Seq, i: int, j_end: int) -> (j: int)\n requires\n 0 <= i < j_end <= nums.len(),\n ensures\n i <= j < j_end,\n Self::count_ones_after_flip_window(nums, i, j) == Self::max_ones_over_windows(nums, i, j_end),\n decreases j_end - i,\n {\n if i == j_end - 1 {\n Self::lemma_count_nonneg(nums, i, j_end - 1, nums.len() as int);\n assert(Self::max_ones_over_windows(nums, i, j_end - 1) == 0);\n j_end - 1\n } else {\n let current = Self::count_ones_after_flip_window(nums, i, j_end - 1);\n let rest = Self::max_ones_over_windows(nums, i, j_end - 1);\n if current > rest {\n j_end - 1\n } else {\n Self::lemma_mow_achieved(nums, i, j_end - 1)\n }\n }\n }\n\n proof fn lemma_maw_achieved(nums: Seq, i_end: int) -> (pair: (int, int))\n requires\n 0 < i_end <= nums.len(),\n nums.len() >= 1,\n ensures\n 0 <= pair.0 <= pair.1 < nums.len(),\n Self::count_ones_after_flip_window(nums, pair.0, pair.1) == Self::max_ones_all_windows(nums, i_end),\n decreases i_end,\n {\n let current_row = Self::max_ones_over_windows(nums, i_end - 1, nums.len() as int);\n let rest = Self::max_ones_all_windows(nums, i_end - 1);\n if i_end == 1 || current_row > rest {\n let j = Self::lemma_mow_achieved(nums, i_end - 1, nums.len() as int);\n Self::lemma_count_nonneg(nums, i_end - 1, j, nums.len() as int);\n (i_end - 1, j)\n } else {\n Self::lemma_maw_achieved(nums, i_end - 1)\n }\n }\n\n pub fn max_ones_after_flip(a: Vec) -> (result: i32)\n requires\n 1 <= a.len() <= 100,\n forall |k: int| 0 <= k < a.len() ==> (#[trigger] a[k] == 0 || a[k] == 1),\n ensures\n result == Self::max_ones_after_one_flip(a@),\n forall |i: int, j: int| 0 <= i <= j < a.len() ==>\n Self::count_ones_after_flip_window(a@, i, j) <= result,\n exists |i: int, j: int| 0 <= i <= j < a.len() &&\n Self::count_ones_after_flip_window(a@, i, j) == result,\n {\n let n = a.len();\n let mut result = 0;\n let mut i = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == a.len(),\n 1 <= n <= 100,\n forall |idx: int| 0 <= idx < n as int ==> (#[trigger] a[idx] == 0 || a[idx] == 1),\n result as int == Self::max_ones_all_windows(a@, i as int),\n 0 <= result <= n as i32,\n decreases n - i,\n {\n let mut j = i;\n while j < n\n invariant\n 0 <= i < n,\n i <= j <= n,\n n == a.len(),\n 1 <= n <= 100,\n forall |idx: int| 0 <= idx < n as int ==> (#[trigger] a[idx] == 0 || a[idx] == 1),\n result as int == if Self::max_ones_all_windows(a@, i as int) > Self::max_ones_over_windows(a@, i as int, j as int) {\n Self::max_ones_all_windows(a@, i as int)\n } else {\n Self::max_ones_over_windows(a@, i as int, j as int)\n },\n 0 <= result <= n as i32,\n decreases n - j,\n {\n let mut count = 0;\n let mut k = 0;\n while k < n\n invariant\n 0 <= k <= n,\n n == a.len(),\n 0 <= i <= j < n,\n 1 <= n <= 100,\n forall |idx: int| 0 <= idx < n as int ==> (#[trigger] a[idx] == 0 || a[idx] == 1),\n 0 <= count <= k as i32,\n count as int == Self::count_ones_after_flip(a@, i as int, j as int, k as int),\n decreases n - k,\n {\n let val = if i <= k && k <= j {\n 1 - a[k]\n } else {\n a[k]\n };\n if val == 1 {\n count = count + 1;\n }\n k = k + 1;\n }\n if count > result {\n result = count;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n proof {\n assert forall |ii: int, jj: int| 0 <= ii <= jj < a@.len() implies\n Self::count_ones_after_flip_window(a@, ii, jj) <= result as int\n by {\n Self::lemma_maw_bound(a@, n as int, ii, jj);\n };\n let pair = Self::lemma_maw_achieved(a@, n as int);\n }\n result\n }\n}\n\n}\n", "main": "use std::io;\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_ones_after_flip(a: Vec) -> i32 {\n let n = a.len();\n let mut result = 0;\n let mut i = 0;\n while i < n {\n let mut j = i;\n while j < n {\n let mut count = 0;\n let mut k = 0;\n while k < n {\n let val = if i <= k && k <= j {\n 1 - a[k]\n } else {\n a[k]\n };\n if val == 1 {\n count = count + 1;\n }\n k = k + 1;\n }\n if count > result {\n result = count;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n result\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n \n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let a: Vec = input\n .trim()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n \n let result = Solution::max_ones_after_flip(a);\n println!(\"{}\", result);\n}\n", "checker": null, "testcases_path": "codeforces/cf327A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf327A/tests/mutated_testcases.jsonl"} {"problem": "cf330A", "family": "codeforces", "description": "# Cakeminator\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou are given a rectangular cake, represented as an $r\u2009\u00d7\u2009c$ grid. Each cell either has an evil strawberry, or is empty. For example, a $3\u2009\u00d7\u20094$ cake may look as follows:\n \nThe cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least one cake cell that has not been eaten before, and eats all the cake cells there. He may decide to eat any number of times.\n\nPlease output the maximum number of cake cells that the cakeminator can eat.\n\n## Input\n\nThe first line contains two integers $r$ and $c$ ($2\u2009\u2264\u2009r,\u2009c\u2009\u2264\u200910$), denoting the number of rows and the number of columns of the cake. The next $r$ lines each contains $c$ characters \u2014 the $j$-th character of the $i$-th line denotes the content of the cell at row $i$ and column $j$, and is either one of these: \n - '`.`' character denotes a cake cell with no evil strawberry; \n- '`S`' character denotes a cake cell with an evil strawberry.\n\n## Output\n\nOutput the maximum number of cake cells that the cakeminator can eat.\n\n## Example\n\n**Input:**\n```\n3 4\nS...\n....\n..S.\n```\n**Output:**\n```\n8\n```\n\n## Note\n\nFor the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats).\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn cakeminator(r: usize, c: usize, grid: Vec>) -> u64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let r: usize = iter.next().unwrap().parse().unwrap();\n let c: usize = iter.next().unwrap().parse().unwrap();\n let mut grid: Vec> = Vec::with_capacity(r);\n for _ in 0..r {\n let s: String = iter.next().unwrap().to_string();\n let mut row: Vec = Vec::with_capacity(c);\n for ch in s.chars() {\n row.push(if ch == 'S' { 1u8 } else { 0u8 });\n }\n grid.push(row);\n }\n writeln!(\n out,\n \"{}\",\n Solution::cakeminator(r, c, grid)\n )\n .unwrap();\n}\n```\n", "tags": "Rating: 800\nbrute force, implementation\nSolved by: 31408 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\npub open spec fn row_has_strawberry(grid: Seq>, i: int, c: int) -> bool {\n exists|j: int| 0 <= j < c && #[trigger] grid[i]@[j] == 1u8\n}\n\npub open spec fn col_has_strawberry(grid: Seq>, j: int, r: int) -> bool {\n exists|i: int| 0 <= i < r && #[trigger] grid[i]@[j] == 1u8\n}\n\npub open spec fn count_clean_rows(grid: Seq>, k: int, c: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0int\n } else if !row_has_strawberry(grid, k - 1, c) {\n count_clean_rows(grid, k - 1, c) + 1\n } else {\n count_clean_rows(grid, k - 1, c)\n }\n}\n\npub open spec fn count_clean_cols(grid: Seq>, k: int, r: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0int\n } else if !col_has_strawberry(grid, k - 1, r) {\n count_clean_cols(grid, k - 1, r) + 1\n } else {\n count_clean_cols(grid, k - 1, r)\n }\n}\n\nimpl Solution {\n pub fn cakeminator(r: usize, c: usize, grid: Vec>) -> (result: u64)\n requires\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n forall|i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == c,\n forall|i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> #[trigger] grid[i][j] == 0u8 || grid[i][j] == 1u8,\n ensures\n result as int == count_clean_rows(grid@, r as int, c as int) * (c as int)\n + count_clean_cols(grid@, c as int, r as int) * (r as int)\n - count_clean_rows(grid@, r as int, c as int) * count_clean_cols(grid@, c as int, r as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn cakeminator(r: usize, c: usize, grid: Vec>) -> u64 {\n let mut clean_rows: u64 = 0;\n let mut i: usize = 0;\n while i < r {\n let mut clean: bool = true;\n let mut j: usize = 0;\n while j < c {\n if grid[i][j] == 1 {\n clean = false;\n }\n j = j + 1;\n }\n if clean {\n clean_rows = clean_rows + 1;\n }\n i = i + 1;\n }\n let mut clean_cols: u64 = 0;\n let mut j: usize = 0;\n while j < c {\n let mut clean: bool = true;\n let mut i: usize = 0;\n while i < r {\n if grid[i][j] == 1 {\n clean = false;\n }\n i = i + 1;\n }\n if clean {\n clean_cols = clean_cols + 1;\n }\n j = j + 1;\n }\n clean_rows * (c as u64) + clean_cols * (r as u64) - clean_rows * clean_cols\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn row_has_strawberry(grid: Seq>, i: int, c: int) -> bool {\n exists|j: int| 0 <= j < c && #[trigger] grid[i]@[j] == 1u8\n}\n\npub open spec fn col_has_strawberry(grid: Seq>, j: int, r: int) -> bool {\n exists|i: int| 0 <= i < r && #[trigger] grid[i]@[j] == 1u8\n}\n\npub open spec fn count_clean_rows(grid: Seq>, k: int, c: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0int\n } else if !row_has_strawberry(grid, k - 1, c) {\n count_clean_rows(grid, k - 1, c) + 1\n } else {\n count_clean_rows(grid, k - 1, c)\n }\n}\n\npub open spec fn count_clean_cols(grid: Seq>, k: int, r: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0int\n } else if !col_has_strawberry(grid, k - 1, r) {\n count_clean_cols(grid, k - 1, r) + 1\n } else {\n count_clean_cols(grid, k - 1, r)\n }\n}\n\nimpl Solution {\n pub fn cakeminator(r: usize, c: usize, grid: Vec>) -> (result: u64)\n requires\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n forall|i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == c,\n forall|i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> #[trigger] grid[i][j] == 0u8 || grid[i][j] == 1u8,\n ensures\n result as int == count_clean_rows(grid@, r as int, c as int) * (c as int)\n + count_clean_cols(grid@, c as int, r as int) * (r as int)\n - count_clean_rows(grid@, r as int, c as int) * count_clean_cols(grid@, c as int, r as int),\n {\n let mut clean_rows: u64 = 0;\n let mut i: usize = 0;\n while i < r {\n let mut clean: bool = true;\n let mut j: usize = 0;\n while j < c {\n if grid[i][j] == 1 {\n clean = false;\n }\n j = j + 1;\n }\n if clean {\n clean_rows = clean_rows + 1;\n }\n i = i + 1;\n }\n let mut clean_cols: u64 = 0;\n let mut j: usize = 0;\n while j < c {\n let mut clean: bool = true;\n let mut i: usize = 0;\n while i < r {\n if grid[i][j] == 1 {\n clean = false;\n }\n i = i + 1;\n }\n if clean {\n clean_cols = clean_cols + 1;\n }\n j = j + 1;\n }\n clean_rows * (c as u64) + clean_cols * (r as u64) - clean_rows * clean_cols\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn row_has_strawberry(grid: Seq>, i: int, c: int) -> bool {\n exists|j: int| 0 <= j < c && #[trigger] grid[i]@[j] == 1u8\n}\n\npub open spec fn col_has_strawberry(grid: Seq>, j: int, r: int) -> bool {\n exists|i: int| 0 <= i < r && #[trigger] grid[i]@[j] == 1u8\n}\n\npub open spec fn count_clean_rows(grid: Seq>, k: int, c: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0int\n } else if !row_has_strawberry(grid, k - 1, c) {\n count_clean_rows(grid, k - 1, c) + 1\n } else {\n count_clean_rows(grid, k - 1, c)\n }\n}\n\npub open spec fn count_clean_cols(grid: Seq>, k: int, r: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0int\n } else if !col_has_strawberry(grid, k - 1, r) {\n count_clean_cols(grid, k - 1, r) + 1\n } else {\n count_clean_cols(grid, k - 1, r)\n }\n}\n\nproof fn lemma_count_rows_bound(grid: Seq>, k: int, c: int)\n requires\n 0 <= k,\n ensures\n 0 <= count_clean_rows(grid, k, c) <= k,\n decreases k,\n{\n if k > 0 {\n lemma_count_rows_bound(grid, k - 1, c);\n }\n}\n\nproof fn lemma_count_cols_bound(grid: Seq>, k: int, r: int)\n requires\n 0 <= k,\n ensures\n 0 <= count_clean_cols(grid, k, r) <= k,\n decreases k,\n{\n if k > 0 {\n lemma_count_cols_bound(grid, k - 1, r);\n }\n}\n\nimpl Solution {\n pub fn cakeminator(r: usize, c: usize, grid: Vec>) -> (result: u64)\n requires\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n forall|i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == c,\n forall|i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> #[trigger] grid[i][j] == 0u8 || grid[i][j] == 1u8,\n ensures\n result as int == count_clean_rows(grid@, r as int, c as int) * (c as int)\n + count_clean_cols(grid@, c as int, r as int) * (r as int)\n - count_clean_rows(grid@, r as int, c as int) * count_clean_cols(grid@, c as int, r as int),\n {\n let mut clean_rows: u64 = 0;\n let mut i: usize = 0;\n while i < r\n invariant\n 0 <= i <= r,\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n forall|k: int| 0 <= k < grid.len() ==> #[trigger] grid[k].len() == c,\n forall|k: int, j: int| 0 <= k < grid.len() && 0 <= j < grid[k].len() ==> #[trigger] grid[k][j] == 0u8 || grid[k][j] == 1u8,\n clean_rows as int == count_clean_rows(grid@, i as int, c as int),\n clean_rows <= i as u64,\n decreases r - i,\n {\n let mut clean: bool = true;\n let mut j: usize = 0;\n while j < c\n invariant\n 0 <= j <= c,\n 0 <= i < r,\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n grid[i as int].len() == c,\n forall|k: int| 0 <= k < grid.len() ==> #[trigger] grid[k].len() == c,\n forall|k: int, jj: int| 0 <= k < grid.len() && 0 <= jj < grid[k].len() ==> #[trigger] grid[k][jj] == 0u8 || grid[k][jj] == 1u8,\n clean == (forall|jj: int| 0 <= jj < j ==> #[trigger] grid@[i as int]@[jj] != 1u8),\n decreases c - j,\n {\n if grid[i][j] == 1 {\n clean = false;\n }\n j = j + 1;\n }\n proof {\n lemma_count_rows_bound(grid@, i as int, c as int);\n if clean {\n assert(forall|jj: int| 0 <= jj < c as int ==> #[trigger] grid@[i as int]@[jj] != 1u8);\n assert(!row_has_strawberry(grid@, i as int, c as int));\n } else {\n assert(exists|jj: int| 0 <= jj < c as int && #[trigger] grid@[i as int]@[jj] == 1u8);\n assert(row_has_strawberry(grid@, i as int, c as int));\n }\n }\n if clean {\n clean_rows = clean_rows + 1;\n }\n i = i + 1;\n }\n let mut clean_cols: u64 = 0;\n let mut j: usize = 0;\n while j < c\n invariant\n 0 <= j <= c,\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n forall|k: int| 0 <= k < grid.len() ==> #[trigger] grid[k].len() == c,\n forall|k: int, jj: int| 0 <= k < grid.len() && 0 <= jj < grid[k].len() ==> #[trigger] grid[k][jj] == 0u8 || grid[k][jj] == 1u8,\n clean_rows as int == count_clean_rows(grid@, r as int, c as int),\n clean_rows <= r as u64,\n clean_cols as int == count_clean_cols(grid@, j as int, r as int),\n clean_cols <= j as u64,\n decreases c - j,\n {\n let mut clean: bool = true;\n let mut i: usize = 0;\n while i < r\n invariant\n 0 <= i <= r,\n 0 <= j < c,\n 2 <= r <= 10,\n 2 <= c <= 10,\n grid.len() == r,\n forall|k: int| 0 <= k < grid.len() ==> #[trigger] grid[k].len() == c,\n forall|k: int, jj: int| 0 <= k < grid.len() && 0 <= jj < grid[k].len() ==> #[trigger] grid[k][jj] == 0u8 || grid[k][jj] == 1u8,\n clean == (forall|kk: int| 0 <= kk < i ==> #[trigger] grid@[kk]@[j as int] != 1u8),\n decreases r - i,\n {\n proof {\n assert(grid[i as int].len() == c);\n }\n if grid[i][j] == 1 {\n clean = false;\n }\n i = i + 1;\n }\n proof {\n lemma_count_cols_bound(grid@, j as int, r as int);\n if clean {\n assert(forall|kk: int| 0 <= kk < r as int ==> #[trigger] grid@[kk]@[j as int] != 1u8);\n assert(!col_has_strawberry(grid@, j as int, r as int));\n } else {\n assert(exists|kk: int| 0 <= kk < r as int && #[trigger] grid@[kk]@[j as int] == 1u8);\n assert(col_has_strawberry(grid@, j as int, r as int));\n }\n }\n if clean {\n clean_cols = clean_cols + 1;\n }\n j = j + 1;\n }\n proof {\n assert(clean_rows <= r as u64);\n assert(clean_cols <= c as u64);\n assert(clean_rows * (c as u64) <= 100u64) by (nonlinear_arith) requires clean_rows <= 10u64, c <= 10usize;\n assert(clean_cols * (r as u64) <= 100u64) by (nonlinear_arith) requires clean_cols <= 10u64, r <= 10usize;\n assert(clean_rows * clean_cols <= 100u64) by (nonlinear_arith) requires clean_rows <= 10u64, clean_cols <= 10u64;\n assert(clean_rows * (c as u64) + clean_cols * (r as u64) >= clean_rows * clean_cols) by (nonlinear_arith) requires\n clean_rows <= r as u64,\n clean_cols <= c as u64,\n r >= 2,\n c >= 2;\n }\n clean_rows * (c as u64) + clean_cols * (r as u64) - clean_rows * clean_cols\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn cakeminator(r: usize, c: usize, grid: Vec>) -> u64 {\n let mut clean_rows: u64 = 0;\n let mut i: usize = 0;\n while i < r {\n let mut clean: bool = true;\n let mut j: usize = 0;\n while j < c {\n if grid[i][j] == 1 {\n clean = false;\n }\n j = j + 1;\n }\n if clean {\n clean_rows = clean_rows + 1;\n }\n i = i + 1;\n }\n let mut clean_cols: u64 = 0;\n let mut j: usize = 0;\n while j < c {\n let mut clean: bool = true;\n let mut i: usize = 0;\n while i < r {\n if grid[i][j] == 1 {\n clean = false;\n }\n i = i + 1;\n }\n if clean {\n clean_cols = clean_cols + 1;\n }\n j = j + 1;\n }\n clean_rows * (c as u64) + clean_cols * (r as u64) - clean_rows * clean_cols\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let r: usize = iter.next().unwrap().parse().unwrap();\n let c: usize = iter.next().unwrap().parse().unwrap();\n let mut grid: Vec> = Vec::with_capacity(r);\n for _ in 0..r {\n let s: String = iter.next().unwrap().to_string();\n let mut row: Vec = Vec::with_capacity(c);\n for ch in s.chars() {\n row.push(if ch == 'S' { 1u8 } else { 0u8 });\n }\n grid.push(row);\n }\n writeln!(\n out,\n \"{}\",\n Solution::cakeminator(r, c, grid)\n )\n .unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf330A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf330A/tests/mutated_testcases.jsonl"} {"problem": "cf339A", "family": "codeforces", "description": "# Helpful Maths\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nXenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.\n\nThe teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.\n\nYou've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.\n\n## Input\n\nThe first line contains a non-empty string $s$ \u2014 the sum Xenia needs to count. String $s$ contains no spaces. It only contains digits and characters \"`+`\". Besides, string $s$ is a correct sum of numbers 1, 2 and 3. String $s$ is at most 100 characters long.\n\n## Output\n\nPrint the new sum that Xenia can count.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n3+2+1\n```\n**Output:**\n```\n1+2+3\n```\n\n### Example 2\n\n**Input:**\n```\n1+1+3+1+3\n```\n**Output:**\n```\n1+1+1+3+3\n```\n\n### Example 3\n\n**Input:**\n```\n2\n```\n**Output:**\n```\n2\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn sort_digits(nums: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut line = String::new();\n stdin.lock().read_line(&mut line).expect(\"read line\");\n let nums: Vec = line\n .trim()\n .split('+')\n .map(|s| s.parse::().expect(\"digit 1, 2, or 3\"))\n .collect();\n let sorted = Solution::sort_digits(nums);\n let out: Vec = sorted.into_iter().map(|d| d.to_string()).collect();\n println!(\"{}\", out.join(\"+\"));\n}\n```\n", "tags": "800\nimplementation, sortings, strings\n(solved count from API)\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_digit(seq: Seq, v: int, end: int) -> nat\n recommends\n 0 <= end <= seq.len(),\n decreases end,\n{\n if end <= 0 {\n 0nat\n } else {\n count_digit(seq, v, end - 1)\n + (if seq[end - 1] as int == v { 1nat } else { 0nat })\n }\n}\n\npub open spec fn sorted(seq: Seq) -> bool {\n forall|i: int, j: int|\n 0 <= i < j < seq.len() ==> #[trigger] seq[i] as int <= #[trigger] seq[j] as int\n}\n\npub open spec fn input_digits_valid(seq: Seq) -> bool {\n forall|i: int| 0 <= i < seq.len() ==> 1 <= #[trigger] seq[i] as int <= 3\n}\n\nimpl Solution {\n pub fn sort_digits(nums: Vec) -> (res: Vec)\n requires\n 1 <= nums.len() <= 50,\n input_digits_valid(nums@),\n ensures\n res@.len() == nums@.len(),\n sorted(res@),\n forall|v: int| 1 <= v <= 3 ==>\n count_digit(res@, v, res@.len() as int) == count_digit(nums@, v, nums@.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn sort_digits(nums: Vec) -> Vec {\n let n = nums.len();\n let mut c1 = 0usize;\n let mut c2 = 0usize;\n let mut c3 = 0usize;\n let mut i = 0usize;\n while i < n {\n if nums[i] == 1 {\n c1 += 1;\n } else if nums[i] == 2 {\n c2 += 1;\n } else {\n c3 += 1;\n }\n i += 1;\n }\n let mut res = Vec::new();\n let mut j = 0usize;\n while j < c1 {\n res.push(1u8);\n j += 1;\n }\n j = 0;\n while j < c2 {\n res.push(2u8);\n j += 1;\n }\n j = 0;\n while j < c3 {\n res.push(3u8);\n j += 1;\n }\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_digit(seq: Seq, v: int, end: int) -> nat\n recommends\n 0 <= end <= seq.len(),\n decreases end,\n{\n if end <= 0 {\n 0nat\n } else {\n count_digit(seq, v, end - 1)\n + (if seq[end - 1] as int == v { 1nat } else { 0nat })\n }\n}\n\npub open spec fn sorted(seq: Seq) -> bool {\n forall|i: int, j: int|\n 0 <= i < j < seq.len() ==> #[trigger] seq[i] as int <= #[trigger] seq[j] as int\n}\n\npub open spec fn input_digits_valid(seq: Seq) -> bool {\n forall|i: int| 0 <= i < seq.len() ==> 1 <= #[trigger] seq[i] as int <= 3\n}\n\nimpl Solution {\n pub fn sort_digits(nums: Vec) -> (res: Vec)\n requires\n 1 <= nums.len() <= 50,\n input_digits_valid(nums@),\n ensures\n res@.len() == nums@.len(),\n sorted(res@),\n forall|v: int| 1 <= v <= 3 ==>\n count_digit(res@, v, res@.len() as int) == count_digit(nums@, v, nums@.len() as int),\n {\n let n = nums.len();\n let mut c1 = 0usize;\n let mut c2 = 0usize;\n let mut c3 = 0usize;\n let mut i = 0usize;\n while i < n {\n if nums[i] == 1 {\n c1 += 1;\n } else if nums[i] == 2 {\n c2 += 1;\n } else {\n c3 += 1;\n }\n i += 1;\n }\n let mut res = Vec::new();\n let mut j = 0usize;\n while j < c1 {\n res.push(1u8);\n j += 1;\n }\n j = 0;\n while j < c2 {\n res.push(2u8);\n j += 1;\n }\n j = 0;\n while j < c3 {\n res.push(3u8);\n j += 1;\n }\n res\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_digit(seq: Seq, v: int, end: int) -> nat\n recommends\n 0 <= end <= seq.len(),\n decreases end,\n{\n if end <= 0 {\n 0nat\n } else {\n count_digit(seq, v, end - 1)\n + (if seq[end - 1] as int == v { 1nat } else { 0nat })\n }\n}\n\npub open spec fn sorted(seq: Seq) -> bool {\n forall|i: int, j: int|\n 0 <= i < j < seq.len() ==> #[trigger] seq[i] as int <= #[trigger] seq[j] as int\n}\n\nproof fn lemma_three_segment_sorted(seq: Seq, c1: int, c2: int, c3: int)\n requires\n 0 <= c1,\n 0 <= c2,\n 0 <= c3,\n seq.len() as int == c1 + c2 + c3,\n forall|i: int| 0 <= i < c1 ==> seq[i] as int == 1,\n forall|i: int| c1 <= i < c1 + c2 ==> seq[i] as int == 2,\n forall|i: int| c1 + c2 <= i < c1 + c2 + c3 ==> seq[i] as int == 3,\n ensures\n sorted(seq),\n{\n assert(forall|i: int, j: int|\n 0 <= i < j < seq.len() ==> seq[i] as int <= seq[j] as int);\n}\n\npub open spec fn input_digits_valid(seq: Seq) -> bool {\n forall|i: int| 0 <= i < seq.len() ==> 1 <= #[trigger] seq[i] as int <= 3\n}\n\npub open spec fn all_digits_1_2_3(seq: Seq, end: int) -> bool\n recommends 0 <= end <= seq.len(),\n decreases end,\n{\n if end <= 0 {\n true\n } else {\n all_digits_1_2_3(seq, end - 1) && 1 <= seq[end - 1] as int <= 3\n }\n}\n\nproof fn lemma_all_digits_at(seq: Seq, end: int, idx: int)\n requires\n 0 <= end <= seq.len(),\n all_digits_1_2_3(seq, end),\n 0 <= idx < end,\n ensures\n 1 <= seq[idx] as int <= 3,\n decreases end,\n{\n if end <= 0 {\n } else if idx == end - 1 {\n } else {\n lemma_all_digits_at(seq, end - 1, idx);\n }\n}\n\nproof fn lemma_input_implies_all_upto(seq: Seq, k: int)\n requires\n input_digits_valid(seq),\n 0 <= k <= seq.len() as int,\n ensures\n all_digits_1_2_3(seq, k),\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_input_implies_all_upto(seq, k - 1);\n assert(0 <= k - 1 && k - 1 < seq.len() as int);\n assert(1 <= seq[k - 1] as int <= 3);\n }\n}\n\nproof fn lemma_input_implies_all(seq: Seq)\n requires\n input_digits_valid(seq),\n ensures\n all_digits_1_2_3(seq, seq.len() as int),\n{\n lemma_input_implies_all_upto(seq, seq.len() as int);\n}\n\nproof fn lemma_count_digit_all(seq: Seq, v: int, len: int)\n requires\n 0 <= len <= seq.len(),\n forall|k: int| 0 <= k < len ==> seq[k] as int == v,\n ensures\n count_digit(seq, v, len) == len as nat,\n decreases len,\n{\n if len <= 0 {\n } else {\n lemma_count_digit_all(seq, v, len - 1);\n reveal_with_fuel(count_digit, 2);\n }\n}\n\nproof fn lemma_count_digit_none(seq: Seq, v: int, len: int)\n requires\n 0 <= len <= seq.len(),\n forall|k: int| 0 <= k < len ==> seq[k] as int != v,\n ensures\n count_digit(seq, v, len) == 0nat,\n decreases len,\n{\n if len <= 0 {\n } else {\n lemma_count_digit_none(seq, v, len - 1);\n reveal_with_fuel(count_digit, 2);\n }\n}\n\nproof fn lemma_count_digit_step(seq: Seq, v: int, end: int)\n requires\n 0 < end <= seq.len(),\n ensures\n count_digit(seq, v, end) == count_digit(seq, v, end - 1)\n + (if seq[end - 1] as int == v { 1nat } else { 0nat }),\n decreases end,\n{\n reveal_with_fuel(count_digit, 2);\n if end > 1 {\n lemma_count_digit_step(seq, v, end - 1);\n }\n}\n\nproof fn lemma_count_digit_same_prefix(seq1: Seq, seq2: Seq, v: int, end: int)\n requires\n 0 <= end <= seq1.len(),\n 0 <= end <= seq2.len(),\n forall|i: int| 0 <= i < end ==> seq1[i] == seq2[i],\n ensures\n count_digit(seq1, v, end) == count_digit(seq2, v, end),\n decreases end,\n{\n if end <= 0 {\n } else {\n lemma_count_digit_same_prefix(seq1, seq2, v, end - 1);\n reveal_with_fuel(count_digit, 2);\n }\n}\n\nimpl Solution {\n pub fn sort_digits(nums: Vec) -> (res: Vec)\n requires\n 1 <= nums.len() <= 50,\n input_digits_valid(nums@),\n ensures\n res@.len() == nums@.len(),\n sorted(res@),\n forall|v: int| 1 <= v <= 3 ==>\n count_digit(res@, v, res@.len() as int) == count_digit(nums@, v, nums@.len() as int),\n {\n let n = nums.len();\n proof {\n assert(input_digits_valid(nums@));\n lemma_input_implies_all(nums@);\n }\n let mut c1 = 0usize;\n let mut c2 = 0usize;\n let mut c3 = 0usize;\n let mut i = 0usize;\n while i < n\n invariant\n n == nums.len(),\n (nums@).len() == n as int,\n all_digits_1_2_3(nums@, n as int),\n i <= n,\n c1 + c2 + c3 == i,\n c1 as int == count_digit(nums@, 1, i as int),\n c2 as int == count_digit(nums@, 2, i as int),\n c3 as int == count_digit(nums@, 3, i as int),\n decreases n - i,\n {\n let ghost i_old = i;\n let ghost c1_old = c1;\n let ghost c2_old = c2;\n let ghost c3_old = c3;\n if nums[i] == 1 {\n c1 += 1;\n } else if nums[i] == 2 {\n c2 += 1;\n } else {\n c3 += 1;\n }\n i += 1;\n proof {\n lemma_count_digit_step(nums@, 1, i as int);\n lemma_count_digit_step(nums@, 2, i as int);\n lemma_count_digit_step(nums@, 3, i as int);\n assert(i as int == i_old as int + 1);\n if c1 as int == c1_old as int + 1 {\n assert((nums@)[i_old as int] as int == 1);\n assert(c1 as int == count_digit(nums@, 1, i as int));\n assert(c2 as int == count_digit(nums@, 2, i as int));\n assert(c3 as int == count_digit(nums@, 3, i as int));\n } else if c2 as int == c2_old as int + 1 {\n assert((nums@)[i_old as int] as int == 2);\n assert(c1 as int == count_digit(nums@, 1, i as int));\n assert(c2 as int == count_digit(nums@, 2, i as int));\n assert(c3 as int == count_digit(nums@, 3, i as int));\n } else {\n assert(c3 as int == c3_old as int + 1);\n lemma_all_digits_at(nums@, n as int, i_old as int);\n assert((nums@)[i_old as int] as int != 1);\n assert((nums@)[i_old as int] as int != 2);\n assert((nums@)[i_old as int] as int == 3);\n assert(c1 as int == count_digit(nums@, 1, i as int));\n assert(c2 as int == count_digit(nums@, 2, i as int));\n assert(c3 as int == count_digit(nums@, 3, i as int));\n }\n }\n }\n let mut res = Vec::new();\n let mut j = 0usize;\n while j < c1\n invariant\n j <= c1,\n res@.len() == j as int,\n forall|k: int| 0 <= k < j as int ==> res@[k] as int == 1,\n decreases c1 - j,\n {\n res.push(1u8);\n j += 1;\n }\n proof {\n assert(j == c1);\n assert(res@.len() == c1 as int);\n assert(forall|k: int| 0 <= k < c1 as int ==> res@[k] as int == 1);\n lemma_count_digit_all(res@, 1, c1 as int);\n assert(count_digit(res@, 1, res@.len() as int) == c1 as nat);\n lemma_count_digit_none(res@, 2, c1 as int);\n lemma_count_digit_none(res@, 3, c1 as int);\n }\n j = 0;\n while j < c2\n invariant\n j <= c2,\n res@.len() == c1 as int + j as int,\n count_digit(res@, 1, res@.len() as int) == c1 as nat,\n count_digit(res@, 2, res@.len() as int) == j as nat,\n count_digit(res@, 3, res@.len() as int) == 0nat,\n forall|i: int| 0 <= i < c1 as int ==> res@[i] as int == 1,\n forall|i: int| c1 as int <= i && i < c1 as int + j as int ==> res@[i] as int == 2,\n decreases c2 - j,\n {\n let ghost old_res = res@;\n res.push(2u8);\n j += 1;\n proof {\n assert(res@.len() as int == old_res.len() as int + 1);\n assert(forall|i: int| 0 <= i < old_res.len() as int ==> res@[i] == old_res[i]);\n lemma_count_digit_same_prefix(res@, old_res, 1, old_res.len() as int);\n lemma_count_digit_same_prefix(res@, old_res, 2, old_res.len() as int);\n lemma_count_digit_same_prefix(res@, old_res, 3, old_res.len() as int);\n assert(count_digit(res@, 1, old_res.len() as int) == c1 as nat);\n assert(count_digit(res@, 2, old_res.len() as int) == j as int - 1);\n lemma_count_digit_step(res@, 1, res@.len() as int);\n lemma_count_digit_step(res@, 2, res@.len() as int);\n lemma_count_digit_step(res@, 3, res@.len() as int);\n }\n }\n proof {\n assert(res@.len() == c1 as int + c2 as int);\n assert(count_digit(res@, 1, res@.len() as int) == c1 as nat);\n assert(count_digit(res@, 2, res@.len() as int) == c2 as nat);\n assert(count_digit(res@, 3, res@.len() as int) == 0nat);\n }\n j = 0;\n while j < c3\n invariant\n j <= c3,\n res@.len() == c1 as int + c2 as int + j as int,\n count_digit(res@, 1, res@.len() as int) == c1 as nat,\n count_digit(res@, 2, res@.len() as int) == c2 as nat,\n count_digit(res@, 3, res@.len() as int) == j as nat,\n forall|i: int| 0 <= i < c1 as int ==> res@[i] as int == 1,\n forall|i: int| c1 as int <= i && i < c1 as int + c2 as int ==> res@[i] as int == 2,\n forall|i: int| c1 as int + c2 as int <= i && i < c1 as int + c2 as int + j as int ==> res@[i] as int == 3,\n decreases c3 - j,\n {\n let ghost old_res = res@;\n res.push(3u8);\n j += 1;\n proof {\n assert(res@.len() as int == old_res.len() as int + 1);\n assert(forall|i: int| 0 <= i < old_res.len() as int ==> res@[i] == old_res[i]);\n lemma_count_digit_same_prefix(res@, old_res, 1, old_res.len() as int);\n lemma_count_digit_same_prefix(res@, old_res, 2, old_res.len() as int);\n lemma_count_digit_same_prefix(res@, old_res, 3, old_res.len() as int);\n assert(count_digit(res@, 1, old_res.len() as int) == c1 as nat);\n assert(count_digit(res@, 2, old_res.len() as int) == c2 as nat);\n assert(count_digit(res@, 3, old_res.len() as int) == j as int - 1);\n lemma_count_digit_step(res@, 1, res@.len() as int);\n lemma_count_digit_step(res@, 2, res@.len() as int);\n lemma_count_digit_step(res@, 3, res@.len() as int);\n }\n }\n proof {\n assert(c1 as int + c2 as int + c3 as int == n as int);\n assert(res@.len() == n as int);\n assert(count_digit(nums@, 1, n as int) == c1 as nat);\n assert(count_digit(nums@, 2, n as int) == c2 as nat);\n assert(count_digit(nums@, 3, n as int) == c3 as nat);\n assert(forall|v: int| 1 <= v <= 3 ==>\n count_digit(res@, v, res@.len() as int) == count_digit(nums@, v, nums@.len() as int));\n assert(forall|i: int| 0 <= i < c1 as int ==> res@[i] as int == 1);\n assert(forall|i: int| c1 as int <= i && i < c1 as int + c2 as int ==> res@[i] as int == 2);\n assert(forall|i: int| c1 as int + c2 as int <= i && i < res@.len() as int ==> res@[i] as int == 3);\n lemma_three_segment_sorted(res@, c1 as int, c2 as int, c3 as int);\n }\n res\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn sort_digits(nums: Vec) -> Vec {\n let n = nums.len();\n let mut c1 = 0usize;\n let mut c2 = 0usize;\n let mut c3 = 0usize;\n let mut i = 0usize;\n while i < n {\n if nums[i] == 1 {\n c1 += 1;\n } else if nums[i] == 2 {\n c2 += 1;\n } else {\n c3 += 1;\n }\n i += 1;\n }\n let mut res = Vec::new();\n let mut j = 0usize;\n while j < c1 {\n res.push(1u8);\n j += 1;\n }\n j = 0;\n while j < c2 {\n res.push(2u8);\n j += 1;\n }\n j = 0;\n while j < c3 {\n res.push(3u8);\n j += 1;\n }\n res\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut line = String::new();\n stdin.lock().read_line(&mut line).expect(\"read line\");\n let nums: Vec = line\n .trim()\n .split('+')\n .map(|s| s.parse::().expect(\"digit 1, 2, or 3\"))\n .collect();\n let sorted = Solution::sort_digits(nums);\n let out: Vec = sorted.into_iter().map(|d| d.to_string()).collect();\n println!(\"{}\", out.join(\"+\"));\n}\n", "checker": null, "testcases_path": "codeforces/cf339A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf339A/tests/mutated_testcases.jsonl"} {"problem": "cf365B", "family": "codeforces", "description": "# The Fibonacci Segment\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou have array $a_1,\u2009a_2,\u2009...,\u2009a_n$. Segment $[l,\u2009r]$ ($1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n$) is good if $a_i\u2009=\u2009a_i\u2009-\u20091\u2009+\u2009a_i\u2009-\u20092$, for all $i$ $(l\u2009+\u20092\u2009\u2264\u2009i\u2009\u2264\u2009r)$.\n\nLet's define $len([l,\u2009r])\u2009=\u2009r\u2009-\u2009l\u2009+\u20091$, $len([l,\u2009r])$ is the length of the segment $[l,\u2009r]$. Segment $[l_1,\u2009r_1]$, is longer than segment $[l_2,\u2009r_2]$, if $len([l_1,\u2009r_1])\u2009>\u2009len([l_2,\u2009r_2])$.\n\nYour task is to find a good segment of the maximum length in array $a$. Note that a segment of length $1$ or $2$ is always good.\n\n## Input\n\nThe first line contains a single integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^5$) \u2014 the number of elements in the array. The second line contains integers: $a_1,\u2009a_2,\u2009...,\u2009a_n$ ($0\u2009\u2264\u2009a_i\u2009\u2264\u200910^9$).\n\n## Output\n\nPrint the length of the longest good segment in array $a$.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n10\n1 2 3 5 8 13 21 34 55 89\n```\n**Output:**\n```\n10\n```\n\n### Example 2\n\n**Input:**\n```\n5\n1 1 1 1 1\n```\n**Output:**\n```\n2\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn longest_fibonacci_segment(nums: Vec) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut nums: Vec = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n nums.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::longest_fibonacci_segment(nums);\n println!(\"{}\", ans);\n}\n```\n", "tags": "1100\nimplementation\n15299\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn good_segment(nums: Seq, l: int, r: int) -> bool {\n &&& 0 <= l < r <= nums.len()\n &&& forall|t: int| l + 2 <= t < r ==> #[trigger] nums[t] == nums[t - 1] + nums[t - 2]\n }\n\n pub fn longest_fibonacci_segment(nums: Vec) -> (result: usize)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result <= nums.len(),\n exists|l: int, r: int|\n #[trigger] Self::good_segment(nums@, l, r) && result as int == r - l,\n forall|l: int, r: int|\n #[trigger] Self::good_segment(nums@, l, r) ==> r - l <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn longest_fibonacci_segment(nums: Vec) -> usize {\n let n = nums.len();\n if n <= 2 {\n return n;\n }\n\n let mut best = 2usize;\n let mut cur = 2usize;\n let mut i = 2usize;\n while i < n {\n if nums[i] == nums[i - 1] + nums[i - 2] {\n cur = cur + 1;\n } else {\n cur = 2;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn good_segment(nums: Seq, l: int, r: int) -> bool {\n &&& 0 <= l < r <= nums.len()\n &&& forall|t: int| l + 2 <= t < r ==> #[trigger] nums[t] == nums[t - 1] + nums[t - 2]\n }\n\n pub fn longest_fibonacci_segment(nums: Vec) -> (result: usize)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result <= nums.len(),\n exists|l: int, r: int|\n #[trigger] Self::good_segment(nums@, l, r) && result as int == r - l,\n forall|l: int, r: int|\n #[trigger] Self::good_segment(nums@, l, r) ==> r - l <= result as int,\n {\n let n = nums.len();\n if n <= 2 {\n return n;\n }\n\n let mut best = 2usize;\n let mut cur = 2usize;\n let mut i = 2usize;\n while i < n {\n if nums[i] == nums[i - 1] + nums[i - 2] {\n cur = cur + 1;\n } else {\n cur = 2;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn good_segment(nums: Seq, l: int, r: int) -> bool {\n &&& 0 <= l < r <= nums.len()\n &&& forall|t: int| l + 2 <= t < r ==> #[trigger] nums[t] == nums[t - 1] + nums[t - 2]\n }\n\n pub fn longest_fibonacci_segment(nums: Vec) -> (result: usize)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result <= nums.len(),\n exists|l: int, r: int|\n #[trigger] Self::good_segment(nums@, l, r) && result as int == r - l,\n forall|l: int, r: int|\n #[trigger] Self::good_segment(nums@, l, r) ==> r - l <= result as int,\n {\n let n = nums.len();\n if n <= 2 {\n proof {\n assert(Self::good_segment(nums@, 0, n as int)) by {\n assert forall|t: int| 0 + 2 <= t < n as int implies #[trigger] nums@[t] == nums@[t - 1] + nums@[t - 2] by {\n };\n };\n assert(exists|l: int, r: int| Self::good_segment(nums@, l, r) && n as int == r - l) by {\n let l = 0;\n let r = n as int;\n assert(Self::good_segment(nums@, l, r));\n };\n assert forall|l: int, r: int| Self::good_segment(nums@, l, r) implies r - l <= n as int by {\n assert(r <= n as int);\n assert(l >= 0);\n };\n }\n return n;\n }\n\n let mut best = 2usize;\n let ghost mut best_l: int = 0;\n let mut cur = 2usize;\n let mut i = 2usize;\n while i < n\n invariant\n n == nums.len(),\n 3 <= n <= 100_000,\n 2 <= i <= n,\n forall|j: int| 0 <= j < n ==> 0 <= #[trigger] nums@[j] <= 1_000_000_000,\n 2 <= cur <= i,\n Self::good_segment(nums@, i as int - cur as int, i as int),\n forall|l: int|\n 0 <= l < i as int && #[trigger] Self::good_segment(nums@, l, i as int) ==> i as int - l <= cur as int,\n 2 <= best <= i,\n 0 <= best_l && best_l + best as int <= i as int,\n Self::good_segment(nums@, best_l, best_l + best as int),\n forall|l: int, r: int|\n 0 <= l < r <= i as int && #[trigger] Self::good_segment(nums@, l, r) ==> r - l <= best as int,\n decreases n - i,\n {\n let ghost old_best = best as int;\n let ghost old_best_l = best_l;\n let ghost old_cur = cur as int;\n let ghost old_i = i as int;\n if nums[i] == nums[i - 1] + nums[i - 2] {\n proof {\n assert(Self::good_segment(nums@, old_i - old_cur, old_i + 1)) by {\n assert(0 <= old_i - old_cur < old_i + 1 <= nums@.len());\n assert forall|t: int| old_i - old_cur + 2 <= t < old_i + 1 implies #[trigger] nums@[t] == nums@[t - 1] + nums@[t - 2] by {\n if t < old_i {\n assert(Self::good_segment(nums@, old_i - old_cur, old_i));\n } else {\n assert(t == old_i);\n }\n };\n };\n assert forall|l: int|\n 0 <= l < old_i + 1 && #[trigger] Self::good_segment(nums@, l, old_i + 1) implies old_i + 1 - l <= old_cur + 1 by {\n if l <= old_i - 2 {\n assert(Self::good_segment(nums@, l, old_i)) by {\n assert(0 <= l < old_i <= nums@.len());\n assert forall|t: int| l + 2 <= t < old_i implies #[trigger] nums@[t] == nums@[t - 1] + nums@[t - 2] by {\n assert(Self::good_segment(nums@, l, old_i + 1));\n };\n };\n assert(old_i - l <= old_cur);\n } else if l == old_i - 1 {\n assert(old_i + 1 - l == 2);\n assert(2 <= old_cur + 1);\n } else {\n assert(l == old_i);\n assert(old_i + 1 - l == 1);\n }\n };\n }\n cur = cur + 1;\n } else {\n proof {\n assert(Self::good_segment(nums@, old_i - 1, old_i + 1)) by {\n assert(0 <= old_i - 1 < old_i + 1 <= nums@.len());\n assert forall|t: int| old_i - 1 + 2 <= t < old_i + 1 implies #[trigger] nums@[t] == nums@[t - 1] + nums@[t - 2] by {\n assert(false);\n };\n };\n assert forall|l: int|\n 0 <= l < old_i + 1 && #[trigger] Self::good_segment(nums@, l, old_i + 1) implies old_i + 1 - l <= 2 by {\n if l <= old_i - 2 {\n assert(old_i == l + 2 || old_i > l + 2);\n assert(nums@[old_i] == nums@[old_i - 1] + nums@[old_i - 2]);\n assert(false);\n } else if l == old_i - 1 {\n assert(old_i + 1 - l == 2);\n } else {\n assert(l == old_i);\n assert(old_i + 1 - l == 1);\n }\n };\n }\n cur = 2;\n }\n\n let ghost cur_start = old_i + 1 - cur as int;\n if cur > best {\n proof {\n best_l = cur_start;\n }\n best = cur;\n }\n i = i + 1;\n proof {\n assert(Self::good_segment(nums@, i as int - cur as int, i as int));\n if cur > old_best {\n assert(best == cur);\n assert(best_l == cur_start);\n assert(Self::good_segment(nums@, best_l, best_l + best as int));\n assert(best_l + best as int == i as int);\n } else {\n assert(best == old_best);\n assert(best_l == old_best_l);\n assert(Self::good_segment(nums@, best_l, best_l + best as int));\n }\n assert forall|l: int, r: int|\n 0 <= l < r <= i as int && #[trigger] Self::good_segment(nums@, l, r) implies r - l <= best as int by {\n if r < i as int {\n assert(r <= old_i);\n assert(r - l <= old_best);\n } else {\n assert(r == i as int);\n if nums@[old_i] == nums@[old_i - 1] + nums@[old_i - 2] {\n assert(i as int - l <= old_cur + 1);\n } else {\n assert(i as int - l <= 2);\n }\n if cur > old_best {\n assert(best as int == cur as int);\n } else {\n assert(cur as int <= old_best);\n assert(best as int == old_best);\n }\n }\n };\n }\n }\n\n proof {\n assert(exists|l: int, r: int| Self::good_segment(nums@, l, r) && best as int == r - l) by {\n let l = best_l;\n let r = best_l + best as int;\n assert(Self::good_segment(nums@, l, r));\n };\n }\n best\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn longest_fibonacci_segment(nums: Vec) -> usize {\n let n = nums.len();\n if n <= 2 {\n return n;\n }\n\n let mut best = 2usize;\n let mut cur = 2usize;\n let mut i = 2usize;\n while i < n {\n if nums[i] == nums[i - 1] + nums[i - 2] {\n cur = cur + 1;\n } else {\n cur = 2;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut nums: Vec = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n nums.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::longest_fibonacci_segment(nums);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf365B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf365B/tests/mutated_testcases.jsonl"} {"problem": "cf38A", "family": "codeforces", "description": "# Army\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nThe Berland Armed Forces System consists of $n$ ranks that are numbered using natural numbers from $1$ to $n$, where $1$ is the lowest rank and $n$ is the highest rank.\n\nOne needs exactly $d_i$ years to rise from rank $i$ to rank $i\u2009+\u20091$. Reaching a certain rank $i$ having not reached all the previous $i\u2009-\u20091$ ranks is impossible.\n\nVasya has just reached a new rank of $a$, but he dreams of holding the rank of $b$. Find for how many more years Vasya should serve in the army until he can finally realize his dream.\n\n## Input\n\nThe first input line contains an integer $n$ ($2\u2009\u2264\u2009n\u2009\u2264\u2009100$). The second line contains $n\u2009-\u20091$ integers $d_i$ ($1\u2009\u2264\u2009d_i\u2009\u2264\u2009100$). The third input line contains two integers $a$ and $b$ ($1\u2009\u2264\u2009a\u2009<\u2009b\u2009\u2264\u2009n$). The numbers on the lines are space-separated.\n\n## Output\n\nPrint the single number which is the number of years that Vasya needs to rise from rank $a$ to rank $b$.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n3\n5 6\n1 2\n```\n**Output:**\n```\n5\n```\n\n### Example 2\n\n**Input:**\n```\n3\n5 6\n1 3\n```\n**Output:**\n```\n11\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn years_needed(n: usize, d: Vec, a: usize, b: usize) -> u32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut d: Vec = Vec::with_capacity(n - 1);\n for _ in 0..(n - 1) {\n d.push(iter.next().unwrap().parse().unwrap());\n }\n let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n let result = Solution::years_needed(n, d, a, b);\n writeln!(out, \"{}\", result).unwrap();\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 26398 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_range(d: Seq, lo: int, hi: int) -> int\n decreases hi - lo,\n{\n if hi <= lo || lo < 0 || hi > d.len() {\n 0int\n } else {\n d[lo] as int + sum_range(d, lo + 1, hi)\n }\n}\n\nimpl Solution {\n pub fn years_needed(n: usize, d: Vec, a: usize, b: usize) -> (result: u32)\n requires\n 2 <= n <= 100,\n d.len() == n - 1,\n forall|i: int| 0 <= i < d.len() ==> 1 <= #[trigger] d[i] as int <= 100,\n 1 <= a < b <= n,\n ensures\n result as int == sum_range(d@, (a - 1) as int, (b - 1) as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn years_needed(n: usize, d: Vec, a: usize, b: usize) -> u32 {\n let mut sum: u32 = 0;\n let mut i: usize = a - 1;\n let lo: usize = a - 1;\n let hi: usize = b - 1;\n while i < hi {\n sum += d[i];\n i += 1;\n }\n sum\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_range(d: Seq, lo: int, hi: int) -> int\n decreases hi - lo,\n{\n if hi <= lo || lo < 0 || hi > d.len() {\n 0int\n } else {\n d[lo] as int + sum_range(d, lo + 1, hi)\n }\n}\n\nimpl Solution {\n pub fn years_needed(n: usize, d: Vec, a: usize, b: usize) -> (result: u32)\n requires\n 2 <= n <= 100,\n d.len() == n - 1,\n forall|i: int| 0 <= i < d.len() ==> 1 <= #[trigger] d[i] as int <= 100,\n 1 <= a < b <= n,\n ensures\n result as int == sum_range(d@, (a - 1) as int, (b - 1) as int),\n {\n let mut sum: u32 = 0;\n let mut i: usize = a - 1;\n let lo: usize = a - 1;\n let hi: usize = b - 1;\n while i < hi {\n sum += d[i];\n i += 1;\n }\n sum\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_range(d: Seq, lo: int, hi: int) -> int\n decreases hi - lo,\n{\n if hi <= lo || lo < 0 || hi > d.len() {\n 0int\n } else {\n d[lo] as int + sum_range(d, lo + 1, hi)\n }\n}\n\nproof fn lemma_sum_range_bound(d: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi <= d.len(),\n forall|i: int| 0 <= i < d.len() ==> 1 <= #[trigger] d[i] as int <= 100,\n ensures\n 0 <= sum_range(d, lo, hi) <= (hi - lo) * 100,\n decreases hi - lo,\n{\n if lo < hi {\n lemma_sum_range_bound(d, lo + 1, hi);\n }\n}\n\nproof fn lemma_sum_range_step(d: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi < d.len(),\n ensures\n sum_range(d, lo, hi + 1) == sum_range(d, lo, hi) + d[hi] as int,\n decreases hi - lo,\n{\n if lo == hi {\n assert(sum_range(d, lo, hi + 1) == d[lo] as int + sum_range(d, lo + 1, hi + 1));\n assert(sum_range(d, lo + 1, hi + 1) == 0);\n assert(sum_range(d, lo, hi) == 0);\n } else {\n lemma_sum_range_step(d, lo + 1, hi);\n }\n}\n\nimpl Solution {\n pub fn years_needed(n: usize, d: Vec, a: usize, b: usize) -> (result: u32)\n requires\n 2 <= n <= 100,\n d.len() == n - 1,\n forall|i: int| 0 <= i < d.len() ==> 1 <= #[trigger] d[i] as int <= 100,\n 1 <= a < b <= n,\n ensures\n result as int == sum_range(d@, (a - 1) as int, (b - 1) as int),\n {\n let mut sum: u32 = 0;\n let mut i: usize = a - 1;\n let lo: usize = a - 1;\n let hi: usize = b - 1;\n while i < hi\n invariant\n lo == a - 1,\n hi == b - 1,\n lo <= i <= hi,\n hi <= d.len(),\n hi - lo <= 99,\n forall|j: int| 0 <= j < d.len() ==> 1 <= #[trigger] d[j] as int <= 100,\n sum as int == sum_range(d@, lo as int, i as int),\n 0 <= sum as int <= (i - lo) * 100,\n decreases hi - i,\n {\n proof {\n lemma_sum_range_step(d@, lo as int, i as int);\n lemma_sum_range_bound(d@, lo as int, hi as int);\n }\n sum += d[i];\n i += 1;\n }\n sum\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn years_needed(n: usize, d: Vec, a: usize, b: usize) -> u32 {\n let mut sum: u32 = 0;\n let mut i: usize = a - 1;\n let lo: usize = a - 1;\n let hi: usize = b - 1;\n while i < hi {\n sum += d[i];\n i += 1;\n }\n sum\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut d: Vec = Vec::with_capacity(n - 1);\n for _ in 0..(n - 1) {\n d.push(iter.next().unwrap().parse().unwrap());\n }\n let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n let result = Solution::years_needed(n, d, a, b);\n writeln!(out, \"{}\", result).unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf38A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf38A/tests/mutated_testcases.jsonl"} {"problem": "cf432A", "family": "codeforces", "description": "# Choosing Teams\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nThe Saratov State University Olympiad Programmers Training Center (SSU OPTC) has $n$ students. For each student you know the number of times he/she has participated in the ACM ICPC world programming championship. According to the ACM ICPC rules, each person can participate in the world championship at most 5 times.\n\nThe head of the SSU OPTC is recently gathering teams to participate in the world championship. Each team must consist of exactly three people, at that, any person cannot be a member of two or more teams. What maximum number of teams can the head make if he wants each team to participate in the world championship with the same members at least $k$ times?\n\n## Input\n\nThe first line contains two integers, $n$ and $k$ $(1\u2009\u2264\u2009n\u2009\u2264\u20092000;\u00a01\u2009\u2264\u2009k\u2009\u2264\u20095)$. The next line contains $n$ integers: $y_1,\u2009y_2,\u2009...,\u2009y_n$ $(0\u2009\u2264\u2009y_i\u2009\u2264\u20095)$, where $y_i$ shows the number of times the $i$-th person participated in the ACM ICPC world championship.\n\n## Output\n\nPrint a single number \u2014 the answer to the problem.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5 2\n0 4 5 1 0\n```\n**Output:**\n```\n1\n```\n\n### Example 2\n\n**Input:**\n```\n6 4\n0 1 2 3 4 5\n```\n**Output:**\n```\n0\n```\n\n### Example 3\n\n**Input:**\n```\n6 5\n0 0 0 0 0 0\n```\n**Output:**\n```\n2\n```\n\n## Note\n\nIn the first sample only one team could be made: the first, the fourth and the fifth participants.\n\nIn the second sample no teams could be created.\n\nIn the third sample two teams could be created. Any partition into two teams fits.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_teams(n: usize, k: i32, y: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let first = lines.next().unwrap().unwrap();\n let mut parts = first.split_whitespace();\n let n: usize = parts.next().unwrap().parse().unwrap();\n let k: i32 = parts.next().unwrap().parse().unwrap();\n let y: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::max_teams(n, k, y);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 800\ngreedy, implementation, sortings\nSolved by: 68893 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_eligible_threshold(k: int) -> int {\n 5 - k\n}\n\npub open spec fn spec_is_eligible(y: int, k: int) -> bool {\n y <= spec_eligible_threshold(k)\n}\n\npub open spec fn spec_count_eligible(s: Seq, n: int, k: int) -> int\n recommends 0 <= n <= s.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n spec_count_eligible(s, n - 1, k)\n + if spec_is_eligible(s[n - 1] as int, k) {\n 1int\n } else {\n 0int\n }\n }\n}\n\nimpl Solution {\n pub fn max_teams(n: usize, k: i32, y: Vec) -> (result: i32)\n requires\n 1 <= n <= 2000,\n n == y.len(),\n 1 <= (k as int) <= 5,\n forall|i: int|\n 0 <= i < n as int ==> 0 <= (#[trigger] y[i] as int) <= 5,\n ensures\n result as int == spec_count_eligible(y@, n as int, k as int) / 3,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_teams(n: usize, k: i32, y: Vec) -> i32 {\n let mut cnt: usize = 0;\n let mut i: usize = 0;\n while i < n {\n if y[i] <= (5 - k) as i64 {\n cnt = cnt + 1;\n }\n i = i + 1;\n }\n (cnt / 3) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_eligible_threshold(k: int) -> int {\n 5 - k\n}\n\npub open spec fn spec_is_eligible(y: int, k: int) -> bool {\n y <= spec_eligible_threshold(k)\n}\n\npub open spec fn spec_count_eligible(s: Seq, n: int, k: int) -> int\n recommends 0 <= n <= s.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n spec_count_eligible(s, n - 1, k)\n + if spec_is_eligible(s[n - 1] as int, k) {\n 1int\n } else {\n 0int\n }\n }\n}\n\nimpl Solution {\n pub fn max_teams(n: usize, k: i32, y: Vec) -> (result: i32)\n requires\n 1 <= n <= 2000,\n n == y.len(),\n 1 <= (k as int) <= 5,\n forall|i: int|\n 0 <= i < n as int ==> 0 <= (#[trigger] y[i] as int) <= 5,\n ensures\n result as int == spec_count_eligible(y@, n as int, k as int) / 3,\n {\n let mut cnt: usize = 0;\n let mut i: usize = 0;\n while i < n {\n if y[i] <= (5 - k) as i64 {\n cnt = cnt + 1;\n }\n i = i + 1;\n }\n (cnt / 3) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_eligible_threshold(k: int) -> int {\n 5 - k\n}\n\npub open spec fn spec_is_eligible(y: int, k: int) -> bool {\n y <= spec_eligible_threshold(k)\n}\n\npub open spec fn spec_count_eligible(s: Seq, n: int, k: int) -> int\n recommends 0 <= n <= s.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n spec_count_eligible(s, n - 1, k)\n + if spec_is_eligible(s[n - 1] as int, k) {\n 1int\n } else {\n 0int\n }\n }\n}\n\nproof fn lemma_spec_count_eligible_step(s: Seq, i: int, k: int)\n requires\n 0 <= i < s.len(),\n 1 <= k <= 5,\n forall|t: int| 0 <= t < s.len() ==> 0 <= #[trigger] s[t] <= 5,\n ensures\n spec_count_eligible(s, i + 1, k)\n == spec_count_eligible(s, i, k)\n + if spec_is_eligible(s[i] as int, k) {\n 1int\n } else {\n 0int\n },\n{\n reveal_with_fuel(spec_count_eligible, 10);\n}\n\nproof fn lemma_spec_count_eligible_nonneg(s: Seq, n: int, k: int)\n requires\n 0 <= n <= s.len(),\n ensures\n spec_count_eligible(s, n, k) >= 0,\n decreases n,\n{\n if n > 0 {\n lemma_spec_count_eligible_nonneg(s, n - 1, k);\n reveal_with_fuel(spec_count_eligible, 2);\n } else {\n reveal_with_fuel(spec_count_eligible, 1);\n }\n}\n\nproof fn lemma_five_minus_k_int(k: i32)\n requires\n 1 <= (k as int) <= 5,\n ensures\n ((5 - k) as i64 as int) == (5 - k as int),\n{\n assert(0 <= 5 - k <= 4);\n}\n\nproof fn lemma_cmp_matches_spec(yv: int, kv: int)\n requires\n 1 <= kv <= 5,\n 0 <= yv <= 5,\n ensures\n (yv <= 5 - kv) == spec_is_eligible(yv, kv),\n{\n assert(spec_eligible_threshold(kv) == 5 - kv);\n}\n\nproof fn lemma_int_div_forall(c: int, r: int)\n requires\n c >= 0,\n r == c / 3,\n ensures\n forall|m: int|\n (0 <= m && #[trigger] (m * 3) <= c) ==> m <= r,\n{\n assert forall|m: int|\n (0 <= m && #[trigger] (m * 3) <= c) implies m <= r by {\n if (0 <= m && (m * 3) <= c) {\n assert(m * 3 <= c);\n assert(m <= c / 3) by (nonlinear_arith)\n requires\n m * 3 <= c,\n c >= 0,\n m >= 0,\n ;\n assert(r == c / 3);\n assert(m <= r);\n }\n };\n}\n\nimpl Solution {\n pub fn max_teams(n: usize, k: i32, y: Vec) -> (result: i32)\n requires\n 1 <= n <= 2000,\n n == y.len(),\n 1 <= (k as int) <= 5,\n forall|i: int|\n 0 <= i < n as int ==> 0 <= (#[trigger] y[i] as int) <= 5,\n ensures\n result as int == spec_count_eligible(y@, n as int, k as int) / 3,\n {\n proof {\n assert(1 <= (k as int) <= 5);\n }\n let mut cnt: usize = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == y.len(),\n 0 <= i <= n,\n 1 <= (k as int) <= 5,\n forall|u: int|\n 0 <= u < n as int ==> 0 <= (#[trigger] y[u] as int) <= 5,\n (cnt as int) <= (i as int),\n (cnt as int) <= (n as int),\n (cnt as int) == spec_count_eligible(y@, i as int, k as int),\n decreases n - i,\n {\n proof {\n assert((i as int) < (n as int));\n assert((i as int) < (y@.len() as int));\n lemma_spec_count_eligible_nonneg(y@, i as int, k as int);\n }\n if y[i] <= (5 - k) as i64 {\n proof {\n assert(0 <= (i as int) && (i as int) < (n as int));\n assert((y@[i as int] as int) >= 0 && (y@[i as int] as int) <= 5);\n assert(y@[i as int] == y[i as int]);\n assert(y[i as int] <= (5 - k) as i64);\n lemma_five_minus_k_int(k);\n assert((y[i as int] as int) <= ((5 - k) as i64 as int));\n assert((y[i as int] as int) <= 5 - k as int);\n lemma_cmp_matches_spec(y[i as int] as int, k as int);\n assert(spec_is_eligible(y[i as int] as int, k as int));\n lemma_spec_count_eligible_step(y@, i as int, k as int);\n assert(spec_count_eligible((y@), (i + 1) as int, k as int) == spec_count_eligible((y@), i as int, k as int) + 1);\n }\n cnt = cnt + 1;\n } else {\n proof {\n assert(0 <= (i as int) && (i as int) < (n as int));\n assert((y@[i as int] as int) >= 0 && (y@[i as int] as int) <= 5);\n assert(y@[i as int] == y[i as int]);\n assert(!(y[i as int] <= (5 - k) as i64));\n lemma_five_minus_k_int(k);\n assert(!((y[i as int] as int) <= ((5 - k) as i64 as int)));\n assert(!((y[i as int] as int) <= 5 - k as int));\n lemma_cmp_matches_spec(y[i as int] as int, k as int);\n assert(!spec_is_eligible(y[i as int] as int, k as int));\n lemma_spec_count_eligible_step(y@, i as int, k as int);\n assert(spec_count_eligible((y@), (i + 1) as int, k as int) == spec_count_eligible((y@), i as int, k as int));\n }\n }\n i = i + 1;\n proof {\n assert((cnt as int) == spec_count_eligible(y@, i as int, k as int));\n }\n }\n proof {\n lemma_spec_count_eligible_nonneg(y@, n as int, k as int);\n assert((cnt as int) == spec_count_eligible((y@), n as int, k as int));\n assert(((cnt / 3) as i32) as int == spec_count_eligible((y@), n as int, k as int) / 3);\n lemma_int_div_forall(spec_count_eligible((y@), n as int, k as int), (cnt / 3) as int);\n }\n (cnt / 3) as i32\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_teams(n: usize, k: i32, y: Vec) -> i32 {\n let mut cnt: usize = 0;\n let mut i: usize = 0;\n while i < n {\n if y[i] <= (5 - k) as i64 {\n cnt = cnt + 1;\n }\n i = i + 1;\n }\n (cnt / 3) as i32\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let first = lines.next().unwrap().unwrap();\n let mut parts = first.split_whitespace();\n let n: usize = parts.next().unwrap().parse().unwrap();\n let k: i32 = parts.next().unwrap().parse().unwrap();\n let y: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::max_teams(n, k, y);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf432A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf432A/tests/mutated_testcases.jsonl"} {"problem": "cf437C", "family": "codeforces", "description": "# The Child and Toy\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nOn Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.\n\nThe toy consists of $n$ parts and $m$ ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part $i$ as $v_i$. The child spend $v_f1\u2009+\u2009v_f2\u2009+\u2009...\u2009+\u2009v_fk$ energy for removing part $i$ where $f_1,\u2009f_2,\u2009...,\u2009f_k$ are the parts that are directly connected to the $i$-th and haven't been removed.\n\nHelp the child to find out, what is the minimum total energy he should spend to remove all $n$ parts.\n\n## Input\n\nThe first line contains two integers $n$ and $m$ ($1\u2009\u2264\u2009n\u2009\u2264\u20091000$; $0\u2009\u2264\u2009m\u2009\u2264\u20092000$). The second line contains $n$ integers: $v_1,\u2009v_2,\u2009...,\u2009v_n$ ($0\u2009\u2264\u2009v_i\u2009\u2264\u200910^5$). Then followed $m$ lines, each line contains two integers $x_i$ and $y_i$, representing a rope from part $x_i$ to part $y_i$ ($1\u2009\u2264\u2009x_i,\u2009y_i\u2009\u2264\u2009n;\u00a0x_i\u2009\u2260\u2009y_i$).\n\nConsider all the parts are numbered from $1$ to $n$.\n\n## Output\n\nOutput the minimum total energy the child should spend to remove all $n$ parts of the toy.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4 3\n10 20 30 40\n1 4\n1 2\n2 3\n```\n**Output:**\n```\n40\n```\n\n### Example 2\n\n**Input:**\n```\n4 4\n100 100 100 100\n1 2\n2 3\n2 4\n3 4\n```\n**Output:**\n```\n400\n```\n\n### Example 3\n\n**Input:**\n```\n7 10\n40 10 20 10 20 80 40\n1 5\n4 7\n4 5\n5 2\n5 7\n6 4\n1 6\n1 3\n4 3\n1 4\n```\n**Output:**\n```\n160\n```\n\n## Note\n\nOne of the optimal sequence of actions in the first sample is:\n - First, remove part $3$, cost of the action is $20$. \n- Then, remove part $2$, cost of the action is $10$. \n- Next, remove part $4$, cost of the action is $10$. \n- At last, remove part $1$, cost of the action is $0$. \n\nSo the total energy the child paid is $20\u2009+\u200910\u2009+\u200910\u2009+\u20090\u2009=\u200940$, which is the minimum.\n\nIn the second sample, the child will spend $400$ no matter in what order he will remove the parts.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_total_energy(weights: Vec, edges: Vec<(usize, usize)>) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read stdin\");\n let mut it = input.split_whitespace();\n let n = it.next().expect(\"n\").parse::().expect(\"usize\");\n let m = it.next().expect(\"m\").parse::().expect(\"usize\");\n let mut weights = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n weights.push(it.next().expect(\"weight\").parse::().expect(\"i64\"));\n i += 1;\n }\n let mut edges = Vec::with_capacity(m);\n let mut j = 0usize;\n while j < m {\n let x = it.next().expect(\"x\").parse::().expect(\"usize\");\n let y = it.next().expect(\"y\").parse::().expect(\"usize\");\n edges.push((x - 1, y - 1));\n j += 1;\n }\n let ans = Solution::min_total_energy(weights, edges);\n println!(\"{}\", ans);\n}\n```\n", "tags": "1400\ngraphs, greedy, sortings\n14879\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn same_undirected_edge(a: (usize, usize), b: (usize, usize)) -> bool {\n (a.0 == b.0 && a.1 == b.1) || (a.0 == b.1 && a.1 == b.0)\n}\n\npub open spec fn valid_edge(weights: Seq, edge: (usize, usize)) -> bool {\n edge.0 < weights.len() && edge.1 < weights.len() && edge.0 != edge.1\n}\n\npub open spec fn edge_energy(weights: Seq, edge: (usize, usize)) -> int {\n if valid_edge(weights, edge) {\n let wu = weights[edge.0 as int] as int;\n let wv = weights[edge.1 as int] as int;\n if wu <= wv { wu } else { wv }\n } else {\n 0\n }\n}\n\npub open spec fn total_energy_prefix(weights: Seq, edges: Seq<(usize, usize)>, end: nat) -> int\n recommends\n end <= edges.len(),\n decreases end,\n{\n if end == 0 {\n 0\n } else {\n total_energy_prefix(weights, edges, (end - 1) as nat) + edge_energy(weights, edges[end as int - 1])\n }\n}\n\npub open spec fn total_energy(weights: Seq, edges: Seq<(usize, usize)>) -> int {\n total_energy_prefix(weights, edges, edges.len())\n}\n\nimpl Solution {\n pub fn min_total_energy(weights: Vec, edges: Vec<(usize, usize)>) -> (res: i64)\n requires\n 1 <= weights.len() <= 1000,\n 0 <= edges.len() <= 2000,\n forall|i: int| 0 <= i < weights.len() ==> 0 <= #[trigger] weights@[i] && weights@[i] <= 100_000,\n forall|k: int| 0 <= k < edges.len() ==> #[trigger] valid_edge(weights@, edges@[k]),\n forall|i: int, j: int|\n 0 <= i < j < edges.len() ==> !same_undirected_edge(edges@[i], edges@[j]),\n ensures\n res as int == total_energy(weights@, edges@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_total_energy(weights: Vec, edges: Vec<(usize, usize)>) -> i64 {\n let mut total = 0i64;\n let mut i = 0usize;\n while i < edges.len() {\n let u = edges[i].0;\n let v = edges[i].1;\n let wu = weights[u];\n let wv = weights[v];\n let add = if wu <= wv { wu } else { wv };\n total += add;\n i += 1;\n }\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn same_undirected_edge(a: (usize, usize), b: (usize, usize)) -> bool {\n (a.0 == b.0 && a.1 == b.1) || (a.0 == b.1 && a.1 == b.0)\n}\n\npub open spec fn valid_edge(weights: Seq, edge: (usize, usize)) -> bool {\n edge.0 < weights.len() && edge.1 < weights.len() && edge.0 != edge.1\n}\n\npub open spec fn edge_energy(weights: Seq, edge: (usize, usize)) -> int {\n if valid_edge(weights, edge) {\n let wu = weights[edge.0 as int] as int;\n let wv = weights[edge.1 as int] as int;\n if wu <= wv { wu } else { wv }\n } else {\n 0\n }\n}\n\npub open spec fn total_energy_prefix(weights: Seq, edges: Seq<(usize, usize)>, end: nat) -> int\n recommends\n end <= edges.len(),\n decreases end,\n{\n if end == 0 {\n 0\n } else {\n total_energy_prefix(weights, edges, (end - 1) as nat) + edge_energy(weights, edges[end as int - 1])\n }\n}\n\npub open spec fn total_energy(weights: Seq, edges: Seq<(usize, usize)>) -> int {\n total_energy_prefix(weights, edges, edges.len())\n}\n\nimpl Solution {\n pub fn min_total_energy(weights: Vec, edges: Vec<(usize, usize)>) -> (res: i64)\n requires\n 1 <= weights.len() <= 1000,\n 0 <= edges.len() <= 2000,\n forall|i: int| 0 <= i < weights.len() ==> 0 <= #[trigger] weights@[i] && weights@[i] <= 100_000,\n forall|k: int| 0 <= k < edges.len() ==> #[trigger] valid_edge(weights@, edges@[k]),\n forall|i: int, j: int|\n 0 <= i < j < edges.len() ==> !same_undirected_edge(edges@[i], edges@[j]),\n ensures\n res as int == total_energy(weights@, edges@),\n {\n let mut total = 0i64;\n let mut i = 0usize;\n while i < edges.len() {\n let u = edges[i].0;\n let v = edges[i].1;\n let wu = weights[u];\n let wv = weights[v];\n let add = if wu <= wv { wu } else { wv };\n total += add;\n i += 1;\n }\n total\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn same_undirected_edge(a: (usize, usize), b: (usize, usize)) -> bool {\n (a.0 == b.0 && a.1 == b.1) || (a.0 == b.1 && a.1 == b.0)\n}\n\npub open spec fn valid_edge(weights: Seq, edge: (usize, usize)) -> bool {\n edge.0 < weights.len() && edge.1 < weights.len() && edge.0 != edge.1\n}\n\npub open spec fn edge_energy(weights: Seq, edge: (usize, usize)) -> int {\n if valid_edge(weights, edge) {\n let wu = weights[edge.0 as int] as int;\n let wv = weights[edge.1 as int] as int;\n if wu <= wv { wu } else { wv }\n } else {\n 0\n }\n}\n\npub open spec fn total_energy_prefix(weights: Seq, edges: Seq<(usize, usize)>, end: nat) -> int\n recommends\n end <= edges.len(),\n decreases end,\n{\n if end == 0 {\n 0\n } else {\n total_energy_prefix(weights, edges, (end - 1) as nat) + edge_energy(weights, edges[end as int - 1])\n }\n}\n\npub open spec fn total_energy(weights: Seq, edges: Seq<(usize, usize)>) -> int {\n total_energy_prefix(weights, edges, edges.len())\n}\n\nproof fn lemma_edge_energy_bound(weights: Seq, edge: (usize, usize))\n requires\n forall|i: int| 0 <= i < weights.len() ==> 0 <= #[trigger] weights[i] && weights[i] <= 100_000,\n valid_edge(weights, edge),\n ensures\n 0 <= edge_energy(weights, edge) <= 100_000,\n{\n assert(0 <= weights[edge.0 as int] && weights[edge.0 as int] <= 100_000);\n assert(0 <= weights[edge.1 as int] && weights[edge.1 as int] <= 100_000);\n}\n\nproof fn lemma_total_energy_prefix_bound(weights: Seq, edges: Seq<(usize, usize)>, end: nat)\n requires\n forall|i: int| 0 <= i < weights.len() ==> 0 <= #[trigger] weights[i] && weights[i] <= 100_000,\n forall|k: int| 0 <= k < edges.len() ==> #[trigger] valid_edge(weights, edges[k]),\n end <= edges.len(),\n ensures\n 0 <= total_energy_prefix(weights, edges, end) <= 100_000 * end as int,\n decreases end,\n{\n if end != 0 {\n lemma_total_energy_prefix_bound(weights, edges, (end - 1) as nat);\n lemma_edge_energy_bound(weights, edges[end as int - 1]);\n assert(total_energy_prefix(weights, edges, end) == total_energy_prefix(weights, edges, (end - 1) as nat) + edge_energy(weights, edges[end as int - 1]));\n }\n}\n\nimpl Solution {\n pub fn min_total_energy(weights: Vec, edges: Vec<(usize, usize)>) -> (res: i64)\n requires\n 1 <= weights.len() <= 1000,\n 0 <= edges.len() <= 2000,\n forall|i: int| 0 <= i < weights.len() ==> 0 <= #[trigger] weights@[i] && weights@[i] <= 100_000,\n forall|k: int| 0 <= k < edges.len() ==> #[trigger] valid_edge(weights@, edges@[k]),\n forall|i: int, j: int|\n 0 <= i < j < edges.len() ==> !same_undirected_edge(edges@[i], edges@[j]),\n ensures\n res as int == total_energy(weights@, edges@),\n {\n let mut total = 0i64;\n let mut i = 0usize;\n while i < edges.len()\n invariant\n 0 <= i <= edges.len(),\n edges.len() <= 2000,\n forall|j: int| 0 <= j < weights.len() ==> 0 <= #[trigger] weights@[j] && weights@[j] <= 100_000,\n forall|k: int| 0 <= k < edges.len() ==> #[trigger] valid_edge(weights@, edges@[k]),\n total as int == total_energy_prefix(weights@, edges@, i as nat),\n 0 <= total as int <= 100_000 * i as int,\n decreases edges.len() - i,\n {\n let u = edges[i].0;\n let v = edges[i].1;\n proof {\n assert(valid_edge(weights@, edges@[i as int]));\n assert(edges@[i as int].0 == u);\n assert(edges@[i as int].1 == v);\n assert(u < weights.len());\n assert(v < weights.len());\n }\n let wu = weights[u];\n let wv = weights[v];\n let add = if wu <= wv { wu } else { wv };\n proof {\n lemma_edge_energy_bound(weights@, edges@[i as int]);\n assert(weights@[u as int] == wu);\n assert(weights@[v as int] == wv);\n if wu <= wv {\n assert(add == wu);\n assert(edge_energy(weights@, edges@[i as int]) == weights@[u as int] as int);\n } else {\n assert(add == wv);\n assert(edge_energy(weights@, edges@[i as int]) == weights@[v as int] as int);\n }\n assert(add as int == edge_energy(weights@, edges@[i as int]));\n assert(total_energy_prefix(weights@, edges@, (i + 1) as nat) == total_energy_prefix(weights@, edges@, i as nat) + edge_energy(weights@, edges@[i as int]));\n assert(total as int + add as int == total_energy_prefix(weights@, edges@, (i + 1) as nat));\n assert((i as int) < edges.len() as int);\n assert(100_000 * ((i as int) + 1) <= 100_000 * edges.len() as int);\n assert(edges.len() <= 2000);\n assert(edges.len() as int <= 2000);\n assert(100_000 * edges.len() as int <= 100_000 * 2000);\n assert(100_000 * edges.len() as int <= 200_000_000);\n assert(0 <= total as int + add as int <= 200_000_000);\n }\n total += add;\n i += 1;\n }\n proof {\n assert(i == edges.len());\n assert(total as int == total_energy_prefix(weights@, edges@, edges.len() as nat));\n assert(total_energy(weights@, edges@) == total_energy_prefix(weights@, edges@, edges.len() as nat));\n }\n total\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_total_energy(weights: Vec, edges: Vec<(usize, usize)>) -> i64 {\n let mut total = 0i64;\n let mut i = 0usize;\n while i < edges.len() {\n let u = edges[i].0;\n let v = edges[i].1;\n let wu = weights[u];\n let wv = weights[v];\n let add = if wu <= wv { wu } else { wv };\n total += add;\n i += 1;\n }\n total\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read stdin\");\n let mut it = input.split_whitespace();\n let n = it.next().expect(\"n\").parse::().expect(\"usize\");\n let m = it.next().expect(\"m\").parse::().expect(\"usize\");\n let mut weights = Vec::with_capacity(n);\n let mut i = 0usize;\n while i < n {\n weights.push(it.next().expect(\"weight\").parse::().expect(\"i64\"));\n i += 1;\n }\n let mut edges = Vec::with_capacity(m);\n let mut j = 0usize;\n while j < m {\n let x = it.next().expect(\"x\").parse::().expect(\"usize\");\n let y = it.next().expect(\"y\").parse::().expect(\"usize\");\n edges.push((x - 1, y - 1));\n j += 1;\n }\n let ans = Solution::min_total_energy(weights, edges);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf437C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf437C/tests/mutated_testcases.jsonl"} {"problem": "cf456C", "family": "codeforces", "description": "# Boredom\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nAlex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.\n\nGiven a sequence $a$ consisting of $n$ integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it $a_k$) and delete it, at that all elements equal to $a_k\u2009+\u20091$ and $a_k\u2009-\u20091$ also must be deleted from the sequence. That step brings $a_k$ points to the player. \n\nAlex is a perfectionist, so he decided to get as many points as possible. Help him.\n\n## Input\n\nThe first line contains integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^5$) that shows how many numbers are in Alex's sequence. \n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1\u2009\u2264\u2009a_i\u2009\u2264\u200910^5$).\n\n## Output\n\nPrint a single integer \u2014 the maximum number of points that Alex can earn.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2\n1 2\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n3\n1 2 3\n```\n**Output:**\n```\n4\n```\n\n### Example 3\n\n**Input:**\n```\n9\n1 2 1 3 2 2 2 2 3\n```\n**Output:**\n```\n10\n```\n\n## Note\n\nConsider the third test example. At first step we need to choose any element equal to $2$. After that step our sequence looks like this $[2,\u20092,\u20092,\u20092]$. Then we do $4$ steps, on each step we choose any element equals to $2$. In total we earn $10$ points.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_boredom_points(nums: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let n: usize = it.next().expect(\"n\").parse().expect(\"n\");\n let mut nums: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let x: i32 = it.next().expect(\"a\").parse().expect(\"a\");\n nums.push(x);\n i = i + 1;\n }\n let answer = Solution::max_boredom_points(nums);\n println!(\"{}\", answer);\n}\n```\n", "tags": "Rating: 1500\ndp\nSolved by: N/A users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn freq_prefix(nums: Seq, v: int, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::freq_prefix(nums, v, end - 1) + if (nums[end - 1] as int) == v {\n 1int\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn freq_at(nums: Seq, v: int) -> int {\n Self::freq_prefix(nums, v, nums.len() as int)\n }\n\n pub open spec fn canonical_counts(nums: Seq) -> Seq {\n Seq::new(100_001, |i: int|\n if i == 0 {\n 0u64\n } else {\n Self::freq_at(nums, i) as u64\n }\n )\n }\n\n pub open spec fn dp_best(cnt: Seq, i: int) -> int\n recommends\n 0 <= i <= 100_000,\n cnt.len() == 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n decreases i,\n {\n if i <= 0 {\n 0\n } else {\n let prev2 = if i >= 2 {\n Self::dp_best(cnt, i - 2)\n } else {\n 0\n };\n let take = prev2 + i * (cnt[i] as int);\n let skip = Self::dp_best(cnt, i - 1);\n if take > skip {\n take\n } else {\n skip\n }\n }\n }\n\n pub fn max_boredom_points(nums: Vec) -> (result: i64)\n requires\n 1 <= nums.len() <= 100_000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100_000,\n ensures\n result as int == Self::dp_best(Self::canonical_counts(nums@), 100_000),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_boredom_points(nums: Vec) -> i64 {\n let mut cnt: Vec = Vec::new();\n let mut t: usize = 0;\n while t < 100_001 {\n cnt.push(0);\n t = t + 1;\n }\n let mut j: usize = 0;\n while j < nums.len() {\n let v = nums[j] as usize;\n let oldc = cnt[v];\n cnt[v] = oldc + 1;\n j = j + 1;\n }\n let mut dp_i_minus_2: i64 = 0;\n let mut dp_i_minus_1: i64 = 0;\n let mut i_val: usize = 1;\n while i_val <= 100_000 {\n let vi = i_val as i64;\n let take = dp_i_minus_2 + vi * (cnt[i_val] as i64);\n let cur = if take > dp_i_minus_1 {\n take\n } else {\n dp_i_minus_1\n };\n dp_i_minus_2 = dp_i_minus_1;\n dp_i_minus_1 = cur;\n i_val = i_val + 1;\n }\n dp_i_minus_1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn freq_prefix(nums: Seq, v: int, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::freq_prefix(nums, v, end - 1) + if (nums[end - 1] as int) == v {\n 1int\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn freq_at(nums: Seq, v: int) -> int {\n Self::freq_prefix(nums, v, nums.len() as int)\n }\n\n pub open spec fn canonical_counts(nums: Seq) -> Seq {\n Seq::new(100_001, |i: int|\n if i == 0 {\n 0u64\n } else {\n Self::freq_at(nums, i) as u64\n }\n )\n }\n\n pub open spec fn dp_best(cnt: Seq, i: int) -> int\n recommends\n 0 <= i <= 100_000,\n cnt.len() == 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n decreases i,\n {\n if i <= 0 {\n 0\n } else {\n let prev2 = if i >= 2 {\n Self::dp_best(cnt, i - 2)\n } else {\n 0\n };\n let take = prev2 + i * (cnt[i] as int);\n let skip = Self::dp_best(cnt, i - 1);\n if take > skip {\n take\n } else {\n skip\n }\n }\n }\n\n pub fn max_boredom_points(nums: Vec) -> (result: i64)\n requires\n 1 <= nums.len() <= 100_000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100_000,\n ensures\n result as int == Self::dp_best(Self::canonical_counts(nums@), 100_000),\n {\n let mut cnt: Vec = Vec::new();\n let mut t: usize = 0;\n while t < 100_001 {\n cnt.push(0);\n t = t + 1;\n }\n let mut j: usize = 0;\n while j < nums.len() {\n let v = nums[j] as usize;\n let oldc = cnt[v];\n cnt.set(v, oldc + 1);\n j = j + 1;\n }\n let mut dp_i_minus_2: i64 = 0;\n let mut dp_i_minus_1: i64 = 0;\n let mut i_val: usize = 1;\n while i_val <= 100_000 {\n let vi = i_val as i64;\n let take = dp_i_minus_2 + vi * (cnt[i_val] as i64);\n let cur = if take > dp_i_minus_1 {\n take\n } else {\n dp_i_minus_1\n };\n dp_i_minus_2 = dp_i_minus_1;\n dp_i_minus_1 = cur;\n i_val = i_val + 1;\n }\n dp_i_minus_1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn freq_prefix(nums: Seq, v: int, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::freq_prefix(nums, v, end - 1) + if (nums[end - 1] as int) == v {\n 1int\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn freq_at(nums: Seq, v: int) -> int {\n Self::freq_prefix(nums, v, nums.len() as int)\n }\n\n pub open spec fn canonical_counts(nums: Seq) -> Seq {\n Seq::new(100_001, |i: int|\n if i == 0 {\n 0u64\n } else {\n Self::freq_at(nums, i) as u64\n }\n )\n }\n\n pub open spec fn dp_best(cnt: Seq, i: int) -> int\n recommends\n 0 <= i <= 100_000,\n cnt.len() == 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n decreases i,\n {\n if i <= 0 {\n 0\n } else {\n let prev2 = if i >= 2 {\n Self::dp_best(cnt, i - 2)\n } else {\n 0\n };\n let take = prev2 + i * (cnt[i] as int);\n let skip = Self::dp_best(cnt, i - 1);\n if take > skip {\n take\n } else {\n skip\n }\n }\n }\n\n proof fn lemma_freq_prefix_split(nums: Seq, v: int, end: int)\n requires\n 0 < end <= nums.len(),\n ensures\n Self::freq_prefix(nums, v, end) == Self::freq_prefix(nums, v, end - 1) + if (nums[end - 1] as int) == v {\n 1int\n } else {\n 0int\n },\n {\n }\n\n proof fn lemma_freq_prefix_v_zero(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len(),\n forall |t: int| 0 <= t < end ==> 1 <= #[trigger] nums[t],\n ensures\n Self::freq_prefix(nums, 0, end) == 0,\n decreases end,\n {\n if end <= 0 {\n } else {\n Self::lemma_freq_prefix_v_zero(nums, end - 1);\n assert((nums[end - 1] as int) != 0);\n }\n }\n\n proof fn lemma_freq_prefix_le_len(nums: Seq, v: int, end: int)\n requires\n 0 <= end <= nums.len(),\n ensures\n Self::freq_prefix(nums, v, end) <= end,\n decreases end,\n {\n if end <= 0 {\n } else {\n Self::lemma_freq_prefix_le_len(nums, v, end - 1);\n reveal_with_fuel(Solution::freq_prefix, 2);\n }\n }\n\n proof fn lemma_cnt_eq_canonical_after_fill(\n nums: Seq,\n cnt: Seq,\n )\n requires\n nums.len() <= 100_000,\n 1 <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 1 <= #[trigger] nums[t] <= 100_000,\n cnt.len() == 100_001,\n forall |idx: int|\n 0 <= idx < 100_001 ==> #[trigger] cnt[idx] as int == Self::freq_prefix(nums, idx, nums.len() as int),\n ensures\n cnt == Self::canonical_counts(nums),\n {\n Self::lemma_freq_prefix_v_zero(nums, nums.len() as int);\n assert forall |idx: int|\n 0 <= idx < 100_001 implies cnt[idx] == Self::canonical_counts(nums)[idx] by {\n if idx == 0 {\n assert(Self::freq_prefix(nums, 0, nums.len() as int) == 0);\n assert(Self::canonical_counts(nums)[0] == 0u64);\n } else {\n assert(cnt[idx] as int == Self::freq_prefix(nums, idx, nums.len() as int));\n assert(Self::freq_at(nums, idx) == Self::freq_prefix(nums, idx, nums.len() as int));\n assert(Self::canonical_counts(nums)[idx] == Self::freq_at(nums, idx) as u64);\n }\n }\n assert(cnt == Self::canonical_counts(nums));\n }\n\n proof fn lemma_dp_step(cnt: Seq, i: int)\n requires\n 1 <= i <= 100_000,\n cnt.len() == 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n ensures\n Self::dp_best(cnt, i)\n == if Self::dp_best(cnt, i - 1)\n > (if i >= 2 {\n Self::dp_best(cnt, i - 2)\n } else {\n 0\n }) + i * (cnt[i] as int)\n {\n Self::dp_best(cnt, i - 1)\n } else {\n (if i >= 2 {\n Self::dp_best(cnt, i - 2)\n } else {\n 0\n }) + i * (cnt[i] as int)\n },\n {\n reveal_with_fuel(Solution::dp_best, 4);\n }\n\n proof fn lemma_dp_best_nonnegative(cnt: Seq, i: int)\n requires\n 0 <= i <= 100_000,\n cnt.len() == 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n ensures\n Self::dp_best(cnt, i) >= 0,\n decreases i,\n {\n if i <= 0 {\n } else {\n Self::lemma_dp_best_nonnegative(cnt, i - 1);\n if i >= 2 {\n Self::lemma_dp_best_nonnegative(cnt, i - 2);\n }\n reveal_with_fuel(Solution::dp_best, 4);\n }\n }\n\n proof fn lemma_dp_best_bound_crude(cnt: Seq, i: int)\n requires\n 0 <= i <= 100_000,\n cnt.len() == 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n ensures\n Self::dp_best(cnt, i) <= i * 100_000 * 100_000,\n decreases i,\n {\n let m: int = 10_000_000_000int;\n if i <= 0 {\n } else {\n Self::lemma_dp_best_bound_crude(cnt, i - 1);\n if i >= 2 {\n Self::lemma_dp_best_bound_crude(cnt, i - 2);\n }\n reveal_with_fuel(Solution::dp_best, 4);\n assert(100_000 * 100_000 == m);\n assert(0 <= i && i < cnt.len());\n assert((cnt[i] as int) <= 100_000);\n assert(0 <= (cnt[i] as int));\n assert(i * (cnt[i] as int) <= 100_000 * 100_000) by (nonlinear_arith)\n requires\n 0 <= i && i <= 100_000,\n 0 <= (cnt[i] as int) && (cnt[i] as int) <= 100_000,\n ;\n assert(i * (cnt[i] as int) <= m);\n assert(m > 0);\n assert(i * m == (i - 1) * m + m) by (nonlinear_arith)\n requires\n i >= 1,\n ;\n assert((i - 1) * m <= i * m);\n if i == 1 {\n assert(Self::dp_best(cnt, i - 1) == 0);\n assert((if i >= 2 {\n Self::dp_best(cnt, i - 2)\n } else {\n 0\n }) + i * (cnt[i] as int) <= m);\n assert(Self::dp_best(cnt, i) <= m);\n assert(i * m == m);\n } else {\n assert(i >= 2);\n assert(Self::dp_best(cnt, i - 1) <= (i - 1) * m);\n assert(Self::dp_best(cnt, i - 2) <= (i - 2) * m);\n assert(\n Self::dp_best(cnt, i - 2) + i * (cnt[i] as int) <= (i - 2) * m + m\n ) by (nonlinear_arith)\n requires\n Self::dp_best(cnt, i - 2) <= (i - 2) * m,\n i * (cnt[i] as int) <= m,\n ;\n assert((i - 2) * m + m == (i - 1) * m) by (nonlinear_arith)\n requires\n i >= 2,\n ;\n assert(Self::dp_best(cnt, i - 1) <= (i - 1) * m);\n assert((i - 1) * m <= i * m);\n }\n }\n }\n\n proof fn lemma_freq_inv_after_step(\n nums: Seq,\n old_cnt: Seq,\n new_cnt: Seq,\n j: int,\n v: int,\n )\n requires\n 0 <= j < nums.len(),\n nums.len() <= 100_000,\n forall |t: int| 0 <= t < nums.len() ==> 1 <= #[trigger] nums[t] <= 100_000,\n 1 <= v && v <= 100_000,\n (nums[j] as int) == v,\n old_cnt.len() == 100_001,\n new_cnt.len() == 100_001,\n forall |idx: int|\n 0 <= idx < 100_001 ==> #[trigger] old_cnt[idx] as int == Self::freq_prefix(nums, idx, j),\n forall |idx: int|\n 0 <= idx < 100_001 && idx != v ==> #[trigger] new_cnt[idx] == old_cnt[idx],\n new_cnt[v as int] == old_cnt[v as int] + 1,\n ensures\n forall |idx: int|\n 0 <= idx < 100_001 ==> #[trigger] new_cnt[idx] as int == Self::freq_prefix(nums, idx, j + 1),\n {\n assert forall |idx: int|\n 0 <= idx < 100_001 implies new_cnt[idx] as int == Self::freq_prefix(nums, idx, j + 1) by {\n Self::lemma_freq_prefix_split(nums, idx, j + 1);\n if idx == v {\n assert(Self::freq_prefix(nums, idx, j + 1) == Self::freq_prefix(nums, idx, j) + 1);\n assert(new_cnt[idx] as int == old_cnt[idx] as int + 1);\n } else {\n assert((nums[j] as int) != idx);\n assert(Self::freq_prefix(nums, idx, j + 1) == Self::freq_prefix(nums, idx, j));\n assert(new_cnt[idx] as int == old_cnt[idx] as int);\n }\n }\n }\n\n pub fn max_boredom_points(nums: Vec) -> (result: i64)\n requires\n 1 <= nums.len() <= 100_000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100_000,\n ensures\n result as int == Self::dp_best(Self::canonical_counts(nums@), 100_000),\n {\n let mut cnt: Vec = Vec::new();\n let mut t: usize = 0;\n while t < 100_001\n invariant\n t <= 100_001,\n cnt.len() == t,\n forall |k: int| 0 <= k < cnt.len() as int ==> #[trigger] cnt[k] == 0,\n decreases 100_001 - t,\n {\n cnt.push(0);\n t = t + 1;\n }\n let mut j: usize = 0;\n while j < nums.len()\n invariant\n nums.len() <= 100_000,\n cnt.len() == 100_001,\n j <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums@[k] <= 100_000,\n forall |idx: int|\n 0 <= idx < 100_001 ==> #[trigger] cnt@[idx] as int\n == Self::freq_prefix(nums@, idx, j as int),\n decreases nums.len() - j,\n {\n let v = nums[j] as usize;\n let oldc = cnt[v];\n let ghost cnt_before = cnt@;\n proof {\n assert(1 <= nums@[j as int] <= 100_000);\n assert(1 <= v && v <= 100_000);\n assert((nums@[j as int] as int) == v as int);\n assert(oldc as int == Self::freq_prefix(nums@, v as int, j as int));\n Self::lemma_freq_prefix_le_len(nums@, v as int, j as int);\n assert(oldc as int <= j as int);\n assert(j < nums.len());\n assert(oldc <= nums.len() as u64);\n assert(oldc <= 100_000);\n }\n cnt.set(v, oldc + 1);\n proof {\n Self::lemma_freq_inv_after_step(nums@, cnt_before, cnt@, j as int, v as int);\n }\n j = j + 1;\n }\n proof {\n Self::lemma_cnt_eq_canonical_after_fill(nums@, cnt@);\n assert(cnt@ == Self::canonical_counts(nums@));\n assert forall |k: int|\n 0 <= k < cnt.len() implies #[trigger] cnt@[k] <= 100_000 by {\n Self::lemma_freq_prefix_le_len(nums@, k, nums.len() as int);\n assert(cnt@[k] as int == Self::freq_prefix(nums@, k, nums.len() as int));\n }\n }\n let ghost nums_seq = nums@;\n let ghost cnt_seq = cnt@;\n let mut dp_i_minus_2: i64 = 0;\n let mut dp_i_minus_1: i64 = 0;\n let mut i_val: usize = 1;\n while i_val <= 100_000\n invariant\n cnt.len() == 100_001,\n nums@ == nums_seq,\n cnt@ == cnt_seq,\n cnt_seq == Self::canonical_counts(nums_seq),\n 1 <= i_val && i_val <= 100_001,\n forall |k: int| 0 <= k < cnt.len() ==> #[trigger] cnt[k] <= 100_000,\n dp_i_minus_2 as int == Self::dp_best(\n cnt_seq,\n if (i_val as int) <= 2 {\n 0\n } else {\n (i_val as int) - 2\n },\n ),\n dp_i_minus_1 as int == Self::dp_best(cnt_seq, (i_val as int) - 1),\n dp_i_minus_2 >= 0,\n dp_i_minus_1 >= 0,\n dp_i_minus_2 as int\n <= if (i_val as int) <= 2 {\n 0\n } else {\n ((i_val as int) - 2) * 100_000 * 100_000\n },\n dp_i_minus_1 as int <= ((i_val as int) - 1) * 100_000 * 100_000,\n decreases 100_001 - i_val,\n {\n let vi = i_val as i64;\n proof {\n Self::lemma_dp_best_nonnegative(\n cnt_seq,\n if (i_val as int) <= 2 {\n 0\n } else {\n (i_val as int) - 2\n },\n );\n Self::lemma_dp_best_nonnegative(cnt_seq, (i_val as int) - 1);\n Self::lemma_dp_best_bound_crude(\n cnt_seq,\n if (i_val as int) <= 2 {\n 0\n } else {\n (i_val as int) - 2\n },\n );\n Self::lemma_dp_best_bound_crude(cnt_seq, (i_val as int) - 1);\n assert(0 <= (i_val as int) && (i_val as int) <= 100_000);\n assert(\n 0 <= (cnt_seq[i_val as int] as int) && (cnt_seq[i_val as int] as int) <= 100_000\n );\n assert(\n (i_val as int) * (cnt_seq[i_val as int] as int) <= 10_000_000_000int\n ) by (nonlinear_arith)\n requires\n 0 <= (i_val as int) && (i_val as int) <= 100_000,\n 0 <= (cnt_seq[i_val as int] as int) && (cnt_seq[i_val as int] as int) <= 100_000,\n ;\n assert((vi as int) * (cnt_seq[i_val as int] as int) <= 10_000_000_000int);\n }\n let take = dp_i_minus_2 + vi * (cnt[i_val] as i64);\n let cur = if take > dp_i_minus_1 {\n take\n } else {\n dp_i_minus_1\n };\n proof {\n Self::lemma_dp_step(cnt_seq, i_val as int);\n assert(take as int == Self::dp_best(cnt_seq, if (i_val as int) <= 2 {\n 0\n } else {\n (i_val as int) - 2\n }) + (i_val as int) * (cnt_seq[i_val as int] as int));\n assert(dp_i_minus_2 as int == Self::dp_best(cnt_seq, if (i_val as int) <= 2 {\n 0\n } else {\n (i_val as int) - 2\n }));\n assert((vi * (cnt@[i_val as int] as i64)) as int == (i_val as int) * (cnt_seq[i_val as int] as int));\n assert(take as int == (if (i_val as int) >= 2 {\n Self::dp_best(cnt_seq, (i_val as int) - 2)\n } else {\n 0\n }) + (i_val as int) * (cnt_seq[i_val as int] as int));\n assert(cur as int == Self::dp_best(cnt_seq, i_val as int));\n Self::lemma_dp_best_nonnegative(cnt_seq, i_val as int);\n Self::lemma_dp_best_bound_crude(cnt_seq, i_val as int);\n }\n dp_i_minus_2 = dp_i_minus_1;\n dp_i_minus_1 = cur;\n i_val = i_val + 1;\n proof {\n assert(dp_i_minus_2 as int == Self::dp_best(cnt_seq, (i_val as int) - 2));\n assert(dp_i_minus_1 as int == Self::dp_best(cnt_seq, (i_val as int) - 1));\n }\n }\n proof {\n assert(i_val == 100_001);\n assert(dp_i_minus_1 as int == Self::dp_best(cnt_seq, 100_000));\n assert(Self::dp_best(cnt_seq, 100_000) == Self::dp_best(Self::canonical_counts(nums_seq), 100_000));\n }\n dp_i_minus_1\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_boredom_points(nums: Vec) -> i64 {\n let mut cnt: Vec = Vec::new();\n let mut t: usize = 0;\n while t < 100_001 {\n cnt.push(0);\n t = t + 1;\n }\n let mut j: usize = 0;\n while j < nums.len() {\n let v = nums[j] as usize;\n let oldc = cnt[v];\n cnt[v] = oldc + 1;\n j = j + 1;\n }\n let mut dp_i_minus_2: i64 = 0;\n let mut dp_i_minus_1: i64 = 0;\n let mut i_val: usize = 1;\n while i_val <= 100_000 {\n let vi = i_val as i64;\n let take = dp_i_minus_2 + vi * (cnt[i_val] as i64);\n let cur = if take > dp_i_minus_1 {\n take\n } else {\n dp_i_minus_1\n };\n dp_i_minus_2 = dp_i_minus_1;\n dp_i_minus_1 = cur;\n i_val = i_val + 1;\n }\n dp_i_minus_1\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let n: usize = it.next().expect(\"n\").parse().expect(\"n\");\n let mut nums: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let x: i32 = it.next().expect(\"a\").parse().expect(\"a\");\n nums.push(x);\n i = i + 1;\n }\n let answer = Solution::max_boredom_points(nums);\n println!(\"{}\", answer);\n}\n", "checker": null, "testcases_path": "codeforces/cf456C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf456C/tests/mutated_testcases.jsonl"} {"problem": "cf459B", "family": "codeforces", "description": "# Pashmak and Flowers\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nPashmak decided to give Parmida a pair of flowers from the garden. There are $n$ flowers in the garden and the $i$-th of them has a beauty number $b_i$. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!\n\nYour task is to write a program which calculates two things:\n - The maximum beauty difference of flowers that Pashmak can give to Parmida. \n- The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.\n\n## Input\n\nThe first line of the input contains $n$ $(2\u2009\u2264\u2009n\u2009\u2264\u20092\u00b710^5)$. In the next line there are $n$ space-separated integers $b_1$, $b_2$, ..., $b_n$ $(1\u2009\u2264\u2009b_i\u2009\u2264\u200910^9)$.\n\n## Output\n\nThe only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2\n1 2\n```\n**Output:**\n```\n1 1\n```\n\n### Example 2\n\n**Input:**\n```\n3\n1 4 5\n```\n**Output:**\n```\n4 1\n```\n\n### Example 3\n\n**Input:**\n```\n5\n3 1 2 3 1\n```\n**Output:**\n```\n2 4\n```\n\n## Note\n\nIn the third sample the maximum beauty difference is $2$ and there are $4$ ways to do this:\n - choosing the first and the second flowers; \n- choosing the first and the fifth flowers; \n- choosing the fourth and the second flowers; \n- choosing the fourth and the fifth flowers.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_beauty_and_pair_count(flowers: Vec) -> (i64, i64) {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let nums: Vec = input\n .split_whitespace()\n .map(|s| s.parse::().expect(\"integer\"))\n .collect();\n let n = nums[0] as usize;\n let flowers = nums[1..1 + n].to_vec();\n let ans = Solution::max_beauty_and_pair_count(flowers);\n println!(\"{} {}\", ans.0, ans.1);\n}\n```\n", "tags": "Rating: 1300\ncombinatorics, implementation, sortings\nSolved by: 44929 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn is_minimum(nums: Seq, mn: int) -> bool {\n nums.len() > 0\n && exists|i: int| 0 <= i < nums.len() && nums[i] as int == mn\n && forall|j: int| 0 <= j < nums.len() ==> mn <= #[trigger] nums[j] as int\n}\n\npub open spec fn is_maximum(nums: Seq, mx: int) -> bool {\n nums.len() > 0\n && exists|i: int| 0 <= i < nums.len() && nums[i] as int == mx\n && forall|j: int| 0 <= j < nums.len() ==> #[trigger] nums[j] as int <= mx\n}\n\npub open spec fn count_value_up_to(nums: Seq, value: int, end: int) -> nat\n recommends 0 <= end <= nums.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n count_value_up_to(nums, value, end - 1)\n + if nums[end - 1] as int == value { 1nat } else { 0nat }\n }\n}\n\npub open spec fn count_value(nums: Seq, value: int) -> nat {\n count_value_up_to(nums, value, nums.len() as int)\n}\n\npub open spec fn is_extremal_pair_for(nums: Seq, mn: int, mx: int, i: int, j: int) -> bool {\n &&& 0 <= i < j < nums.len()\n &&& (\n (nums[i] as int == mn && nums[j] as int == mx)\n || (nums[i] as int == mx && nums[j] as int == mn)\n )\n}\n\npub open spec fn count_extremal_pairs_for_with_right(nums: Seq, mn: int, mx: int, right: int, left_end: int) -> nat\n recommends 0 <= left_end <= right < nums.len(),\n decreases left_end,\n{\n if left_end <= 0 {\n 0\n } else {\n count_extremal_pairs_for_with_right(nums, mn, mx, right, left_end - 1)\n + if is_extremal_pair_for(nums, mn, mx, left_end - 1, right) { 1nat } else { 0nat }\n }\n}\n\npub open spec fn count_extremal_pairs_for_up_to(nums: Seq, mn: int, mx: int, end: int) -> nat\n recommends 0 <= end <= nums.len(),\n decreases end,\n{\n if end <= 1 {\n 0\n } else {\n count_extremal_pairs_for_up_to(nums, mn, mx, end - 1)\n + count_extremal_pairs_for_with_right(nums, mn, mx, end - 1, end - 1)\n }\n}\n\nimpl Solution {\n pub fn max_beauty_and_pair_count(flowers: Vec) -> (result: (i64, i64))\n requires\n 2 <= flowers.len() <= 200_000,\n forall|i: int| 0 <= i < flowers.len() ==> 1 <= #[trigger] flowers[i] <= 1_000_000_000,\n ensures\n exists|mn: int, mx: int| {\n &&& is_minimum(flowers@, mn)\n &&& is_maximum(flowers@, mx)\n &&& result.0 as int == mx - mn\n &&& result.1 as int == count_extremal_pairs_for_up_to(flowers@, mn, mx, flowers.len() as int)\n &&& forall|i: int, j: int|\n 0 <= i < j < flowers.len() ==> (\n #[trigger] is_extremal_pair_for(flowers@, mn, mx, i, j)\n <==> abs_diff(flowers[i] as int, flowers[j] as int) == result.0 as int\n )\n },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_beauty_and_pair_count(flowers: Vec) -> (i64, i64) {\n let mut min_val = flowers[0];\n let mut max_val = flowers[0];\n let mut min_count = 1i64;\n let mut max_count = 1i64;\n let mut i = 1usize;\n while i < flowers.len() {\n let x = flowers[i];\n if x < min_val {\n min_val = x;\n min_count = 1;\n } else if x == min_val {\n min_count += 1;\n }\n if x > max_val {\n max_val = x;\n max_count = 1;\n } else if x == max_val {\n max_count += 1;\n }\n i += 1;\n }\n let diff = max_val - min_val;\n let n = flowers.len() as i64;\n let pair_count = if min_val == max_val {\n n * (n - 1) / 2\n } else {\n min_count * max_count\n };\n (diff, pair_count)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn is_minimum(nums: Seq, mn: int) -> bool {\n nums.len() > 0\n && exists|i: int| 0 <= i < nums.len() && nums[i] as int == mn\n && forall|j: int| 0 <= j < nums.len() ==> mn <= #[trigger] nums[j] as int\n}\n\npub open spec fn is_maximum(nums: Seq, mx: int) -> bool {\n nums.len() > 0\n && exists|i: int| 0 <= i < nums.len() && nums[i] as int == mx\n && forall|j: int| 0 <= j < nums.len() ==> #[trigger] nums[j] as int <= mx\n}\n\npub open spec fn count_value_up_to(nums: Seq, value: int, end: int) -> nat\n recommends 0 <= end <= nums.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n count_value_up_to(nums, value, end - 1)\n + if nums[end - 1] as int == value { 1nat } else { 0nat }\n }\n}\n\npub open spec fn count_value(nums: Seq, value: int) -> nat {\n count_value_up_to(nums, value, nums.len() as int)\n}\n\npub open spec fn is_extremal_pair_for(nums: Seq, mn: int, mx: int, i: int, j: int) -> bool {\n &&& 0 <= i < j < nums.len()\n &&& (\n (nums[i] as int == mn && nums[j] as int == mx)\n || (nums[i] as int == mx && nums[j] as int == mn)\n )\n}\n\npub open spec fn count_extremal_pairs_for_with_right(nums: Seq, mn: int, mx: int, right: int, left_end: int) -> nat\n recommends 0 <= left_end <= right < nums.len(),\n decreases left_end,\n{\n if left_end <= 0 {\n 0\n } else {\n count_extremal_pairs_for_with_right(nums, mn, mx, right, left_end - 1)\n + if is_extremal_pair_for(nums, mn, mx, left_end - 1, right) { 1nat } else { 0nat }\n }\n}\n\npub open spec fn count_extremal_pairs_for_up_to(nums: Seq, mn: int, mx: int, end: int) -> nat\n recommends 0 <= end <= nums.len(),\n decreases end,\n{\n if end <= 1 {\n 0\n } else {\n count_extremal_pairs_for_up_to(nums, mn, mx, end - 1)\n + count_extremal_pairs_for_with_right(nums, mn, mx, end - 1, end - 1)\n }\n}\n\nimpl Solution {\n pub fn max_beauty_and_pair_count(flowers: Vec) -> (result: (i64, i64))\n requires\n 2 <= flowers.len() <= 200_000,\n forall|i: int| 0 <= i < flowers.len() ==> 1 <= #[trigger] flowers[i] <= 1_000_000_000,\n ensures\n exists|mn: int, mx: int| {\n &&& is_minimum(flowers@, mn)\n &&& is_maximum(flowers@, mx)\n &&& result.0 as int == mx - mn\n &&& result.1 as int == count_extremal_pairs_for_up_to(flowers@, mn, mx, flowers.len() as int)\n &&& forall|i: int, j: int|\n 0 <= i < j < flowers.len() ==> (\n #[trigger] is_extremal_pair_for(flowers@, mn, mx, i, j)\n <==> abs_diff(flowers[i] as int, flowers[j] as int) == result.0 as int\n )\n },\n {\n let mut min_val = flowers[0];\n let mut max_val = flowers[0];\n let mut min_count = 1i64;\n let mut max_count = 1i64;\n let mut i = 1usize;\n while i < flowers.len() {\n let x = flowers[i];\n if x < min_val {\n min_val = x;\n min_count = 1;\n } else if x == min_val {\n min_count += 1;\n }\n if x > max_val {\n max_val = x;\n max_count = 1;\n } else if x == max_val {\n max_count += 1;\n }\n i += 1;\n }\n let diff = max_val - min_val;\n let n = flowers.len() as i64;\n let pair_count = if min_val == max_val {\n n * (n - 1) / 2\n } else {\n min_count * max_count\n };\n (diff, pair_count)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn is_minimum(nums: Seq, mn: int) -> bool {\n nums.len() > 0\n && exists|i: int| 0 <= i < nums.len() && nums[i] as int == mn\n && forall|j: int| 0 <= j < nums.len() ==> mn <= #[trigger] nums[j] as int\n}\n\npub open spec fn is_maximum(nums: Seq, mx: int) -> bool {\n nums.len() > 0\n && exists|i: int| 0 <= i < nums.len() && nums[i] as int == mx\n && forall|j: int| 0 <= j < nums.len() ==> #[trigger] nums[j] as int <= mx\n}\n\npub open spec fn count_value_up_to(nums: Seq, value: int, end: int) -> nat\n recommends 0 <= end <= nums.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n count_value_up_to(nums, value, end - 1)\n + if nums[end - 1] as int == value { 1nat } else { 0nat }\n }\n}\n\npub open spec fn count_value(nums: Seq, value: int) -> nat {\n count_value_up_to(nums, value, nums.len() as int)\n}\n\npub open spec fn is_extremal_pair_for(nums: Seq, mn: int, mx: int, i: int, j: int) -> bool {\n &&& 0 <= i < j < nums.len()\n &&& (\n (nums[i] as int == mn && nums[j] as int == mx)\n || (nums[i] as int == mx && nums[j] as int == mn)\n )\n}\n\npub open spec fn count_extremal_pairs_for_with_right(nums: Seq, mn: int, mx: int, right: int, left_end: int) -> nat\n recommends 0 <= left_end <= right < nums.len(),\n decreases left_end,\n{\n if left_end <= 0 {\n 0\n } else {\n count_extremal_pairs_for_with_right(nums, mn, mx, right, left_end - 1)\n + if is_extremal_pair_for(nums, mn, mx, left_end - 1, right) { 1nat } else { 0nat }\n }\n}\n\npub open spec fn count_extremal_pairs_for_up_to(nums: Seq, mn: int, mx: int, end: int) -> nat\n recommends 0 <= end <= nums.len(),\n decreases end,\n{\n if end <= 1 {\n 0\n } else {\n count_extremal_pairs_for_up_to(nums, mn, mx, end - 1)\n + count_extremal_pairs_for_with_right(nums, mn, mx, end - 1, end - 1)\n }\n}\n\npub open spec fn choose2(n: nat) -> nat\n decreases n,\n{\n if n == 0 {\n 0\n } else {\n choose2((n - 1) as nat) + ((n - 1) as nat)\n }\n}\n\nproof fn lemma_choose2_closed_form(n: nat)\n ensures\n choose2(n) == n * (n - 1) / 2,\n decreases n,\n{\n if n == 0 {\n } else {\n lemma_choose2_closed_form((n - 1) as nat);\n assert(choose2(n) == choose2((n - 1) as nat) + (n - 1));\n assert((((n - 1) * (n - 2)) / 2) + (n - 1) == (n * (n - 1)) / 2) by (nonlinear_arith);\n }\n}\n\nproof fn lemma_count_value_zero_when_absent(nums: Seq, value: int, end: int)\n requires\n 0 <= end <= nums.len(),\n forall|i: int| 0 <= i < end ==> nums[i] as int != value,\n ensures\n count_value_up_to(nums, value, end) == 0,\n decreases end,\n{\n if end <= 0 {\n } else {\n lemma_count_value_zero_when_absent(nums, value, end - 1);\n assert(nums[end - 1] as int != value);\n }\n}\n\nproof fn lemma_all_equal_from_min_max(nums: Seq, mn: int, mx: int)\n requires\n is_minimum(nums, mn),\n is_maximum(nums, mx),\n mn == mx,\n ensures\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] as int == mn,\n{\n assert forall|i: int| 0 <= i < nums.len() implies #[trigger] nums[i] as int == mn by {\n assert(mn <= nums[i] as int);\n assert(nums[i] as int <= mx);\n }\n}\n\nproof fn lemma_count_pairs_with_right_all_equal(nums: Seq, mn: int, right: int, left_end: int)\n requires\n 0 <= left_end <= right < nums.len(),\n forall|i: int| 0 <= i <= right ==> nums[i] as int == mn,\n ensures\n count_extremal_pairs_for_with_right(nums, mn, mn, right, left_end) == left_end,\n decreases left_end,\n{\n if left_end <= 0 {\n } else {\n lemma_count_pairs_with_right_all_equal(nums, mn, right, left_end - 1);\n assert(is_extremal_pair_for(nums, mn, mn, left_end - 1, right));\n }\n}\n\nproof fn lemma_choose2_base(n: nat)\n requires n <= 1,\n ensures choose2(n) == 0,\n{\n reveal_with_fuel(choose2, 2);\n}\n\nproof fn lemma_count_pairs_all_equal(nums: Seq, mn: int, end: int)\n requires\n 0 <= end <= nums.len(),\n forall|i: int| 0 <= i < end ==> nums[i] as int == mn,\n ensures\n count_extremal_pairs_for_up_to(nums, mn, mn, end) == choose2(end as nat),\n decreases end,\n{\n if end <= 1 {\n assert(count_extremal_pairs_for_up_to(nums, mn, mn, end) == 0);\n lemma_choose2_base(end as nat);\n } else {\n lemma_count_pairs_all_equal(nums, mn, end - 1);\n lemma_count_pairs_with_right_all_equal(nums, mn, end - 1, end - 1);\n assert(count_extremal_pairs_for_up_to(nums, mn, mn, end - 1) == choose2((end - 1) as nat));\n assert(count_extremal_pairs_for_with_right(nums, mn, mn, end - 1, end - 1) == (end - 1) as nat);\n assert(count_extremal_pairs_for_up_to(nums, mn, mn, end)\n == count_extremal_pairs_for_up_to(nums, mn, mn, end - 1)\n + count_extremal_pairs_for_with_right(nums, mn, mn, end - 1, end - 1));\n assert(choose2(end as nat) == choose2((end - 1) as nat) + ((end - 1) as nat));\n assert(count_extremal_pairs_for_up_to(nums, mn, mn, end)\n == choose2((end - 1) as nat) + ((end - 1) as nat));\n }\n}\n\nproof fn lemma_count_pairs_with_right_distinct(nums: Seq, mn: int, mx: int, right: int, left_end: int)\n requires\n 0 <= left_end <= right < nums.len(),\n mn < mx,\n forall|i: int| 0 <= i <= right ==> mn <= #[trigger] nums[i] as int && nums[i] as int <= mx,\n ensures\n nums[right] as int == mn ==> count_extremal_pairs_for_with_right(nums, mn, mx, right, left_end) == count_value_up_to(nums, mx, left_end),\n nums[right] as int == mx ==> count_extremal_pairs_for_with_right(nums, mn, mx, right, left_end) == count_value_up_to(nums, mn, left_end),\n nums[right] as int != mn && nums[right] as int != mx ==> count_extremal_pairs_for_with_right(nums, mn, mx, right, left_end) == 0,\n decreases left_end,\n{\n if left_end <= 0 {\n } else {\n lemma_count_pairs_with_right_distinct(nums, mn, mx, right, left_end - 1);\n if nums[right] as int == mn {\n assert(nums[right] as int != mx);\n assert(is_extremal_pair_for(nums, mn, mx, left_end - 1, right) <==> nums[left_end - 1] as int == mx);\n } else if nums[right] as int == mx {\n assert(nums[right] as int != mn);\n assert(is_extremal_pair_for(nums, mn, mx, left_end - 1, right) <==> nums[left_end - 1] as int == mn);\n } else {\n assert(!is_extremal_pair_for(nums, mn, mx, left_end - 1, right));\n }\n }\n}\n\nproof fn lemma_count_pairs_distinct(nums: Seq, mn: int, mx: int, end: int)\n requires\n 0 <= end <= nums.len(),\n mn < mx,\n forall|i: int| 0 <= i < end ==> mn <= #[trigger] nums[i] as int && nums[i] as int <= mx,\n ensures\n count_extremal_pairs_for_up_to(nums, mn, mx, end)\n == count_value_up_to(nums, mn, end) * count_value_up_to(nums, mx, end),\n decreases end,\n{\n if end <= 1 {\n } else {\n lemma_count_pairs_distinct(nums, mn, mx, end - 1);\n lemma_count_pairs_with_right_distinct(nums, mn, mx, end - 1, end - 1);\n if nums[end - 1] as int == mn {\n assert(count_value_up_to(nums, mn, end) == count_value_up_to(nums, mn, end - 1) + 1);\n assert(count_value_up_to(nums, mx, end) == count_value_up_to(nums, mx, end - 1));\n assert(count_extremal_pairs_for_up_to(nums, mn, mx, end)\n == count_extremal_pairs_for_up_to(nums, mn, mx, end - 1)\n + count_value_up_to(nums, mx, end - 1));\n assert(count_value_up_to(nums, mn, end - 1) * count_value_up_to(nums, mx, end - 1)\n + count_value_up_to(nums, mx, end - 1)\n == (count_value_up_to(nums, mn, end - 1) + 1) * count_value_up_to(nums, mx, end - 1)) by (nonlinear_arith);\n } else if nums[end - 1] as int == mx {\n assert(count_value_up_to(nums, mn, end) == count_value_up_to(nums, mn, end - 1));\n assert(count_value_up_to(nums, mx, end) == count_value_up_to(nums, mx, end - 1) + 1);\n assert(count_extremal_pairs_for_up_to(nums, mn, mx, end)\n == count_extremal_pairs_for_up_to(nums, mn, mx, end - 1)\n + count_value_up_to(nums, mn, end - 1));\n assert(count_value_up_to(nums, mn, end - 1) * count_value_up_to(nums, mx, end - 1)\n + count_value_up_to(nums, mn, end - 1)\n == count_value_up_to(nums, mn, end - 1) * (count_value_up_to(nums, mx, end - 1) + 1)) by (nonlinear_arith);\n } else {\n assert(count_value_up_to(nums, mn, end) == count_value_up_to(nums, mn, end - 1));\n assert(count_value_up_to(nums, mx, end) == count_value_up_to(nums, mx, end - 1));\n assert(count_extremal_pairs_for_up_to(nums, mn, mx, end)\n == count_extremal_pairs_for_up_to(nums, mn, mx, end - 1));\n }\n }\n}\n\nproof fn lemma_extremal_pair_matches_max_difference(nums: Seq, mn: int, mx: int, i: int, j: int)\n requires\n is_minimum(nums, mn),\n is_maximum(nums, mx),\n 0 <= i < j < nums.len(),\n ensures\n is_extremal_pair_for(nums, mn, mx, i, j) <==> abs_diff(nums[i] as int, nums[j] as int) == mx - mn,\n{\n let a = nums[i] as int;\n let b = nums[j] as int;\n assert(mn <= a);\n assert(a <= mx);\n assert(mn <= b);\n assert(b <= mx);\n if is_extremal_pair_for(nums, mn, mx, i, j) {\n if a == mn && b == mx {\n assert(abs_diff(a, b) == mx - mn);\n } else {\n assert(a == mx && b == mn);\n assert(abs_diff(a, b) == mx - mn);\n }\n } else {\n if a >= b {\n assert(abs_diff(a, b) == a - b);\n if abs_diff(a, b) == mx - mn {\n assert((mx - a) + (b - mn) == (mx - mn) - (a - b));\n assert(mx - a == 0);\n assert(b - mn == 0);\n assert(a == mx && b == mn);\n assert(is_extremal_pair_for(nums, mn, mx, i, j));\n }\n } else {\n assert(abs_diff(a, b) == b - a);\n if abs_diff(a, b) == mx - mn {\n assert((mx - b) + (a - mn) == (mx - mn) - (b - a));\n assert(mx - b == 0);\n assert(a - mn == 0);\n assert(a == mn && b == mx);\n assert(is_extremal_pair_for(nums, mn, mx, i, j));\n }\n }\n }\n}\n\nimpl Solution {\n pub fn max_beauty_and_pair_count(flowers: Vec) -> (result: (i64, i64))\n requires\n 2 <= flowers.len() <= 200_000,\n forall|i: int| 0 <= i < flowers.len() ==> 1 <= #[trigger] flowers[i] <= 1_000_000_000,\n ensures\n exists|mn: int, mx: int| {\n &&& is_minimum(flowers@, mn)\n &&& is_maximum(flowers@, mx)\n &&& result.0 as int == mx - mn\n &&& result.1 as int == count_extremal_pairs_for_up_to(flowers@, mn, mx, flowers.len() as int)\n &&& forall|i: int, j: int|\n 0 <= i < j < flowers.len() ==> (\n #[trigger] is_extremal_pair_for(flowers@, mn, mx, i, j)\n <==> abs_diff(flowers[i] as int, flowers[j] as int) == result.0 as int\n )\n },\n {\n let mut min_val = flowers[0];\n let mut max_val = flowers[0];\n let mut min_count = 1i64;\n let mut max_count = 1i64;\n let mut i = 1usize;\n proof {\n assert(1 <= flowers[0] <= 1_000_000_000);\n reveal_with_fuel(count_value_up_to, 2);\n assert(count_value_up_to(flowers@, min_val as int, 1) == 1);\n assert(count_value_up_to(flowers@, max_val as int, 1) == 1);\n }\n while i < flowers.len()\n invariant\n 1 <= i <= flowers.len(),\n 1 <= min_count <= i as int,\n 1 <= max_count <= i as int,\n flowers.len() <= 200_000,\n exists|k: int| 0 <= k < i as int && flowers[k] == min_val,\n exists|k: int| 0 <= k < i as int && flowers[k] == max_val,\n forall|k: int| 0 <= k < i as int ==> min_val <= #[trigger] flowers[k],\n forall|k: int| 0 <= k < i as int ==> #[trigger] flowers[k] <= max_val,\n min_count as int == count_value_up_to(flowers@, min_val as int, i as int),\n max_count as int == count_value_up_to(flowers@, max_val as int, i as int),\n decreases flowers.len() - i,\n {\n let x = flowers[i];\n let ghost old_i = i as int;\n let ghost old_min = min_val as int;\n let ghost old_max = max_val as int;\n let ghost old_min_count = min_count as int;\n let ghost old_max_count = max_count as int;\n if x < min_val {\n min_val = x;\n min_count = 1;\n } else if x == min_val {\n assert(min_count as int <= i as int);\n assert((i as int) < flowers.len() as int);\n assert(flowers.len() as int <= 200_000);\n min_count += 1;\n }\n if x > max_val {\n max_val = x;\n max_count = 1;\n } else if x == max_val {\n assert(max_count as int <= i as int);\n assert((i as int) < flowers.len() as int);\n assert(flowers.len() as int <= 200_000);\n max_count += 1;\n }\n proof {\n if x < old_min as i64 {\n assert forall|k: int| 0 <= k < old_i implies flowers[k] as int != x as int by {\n assert(old_min <= flowers[k] as int);\n }\n lemma_count_value_zero_when_absent(flowers@, x as int, old_i);\n assert(count_value_up_to(flowers@, x as int, old_i + 1) == 1);\n } else if x == old_min as i64 {\n assert(count_value_up_to(flowers@, old_min, old_i + 1) == old_min_count + 1);\n } else {\n assert(x as int != old_min);\n assert(count_value_up_to(flowers@, old_min, old_i + 1) == old_min_count);\n }\n if x > old_max as i64 {\n assert forall|k: int| 0 <= k < old_i implies flowers[k] as int != x as int by {\n assert(flowers[k] as int <= old_max);\n }\n lemma_count_value_zero_when_absent(flowers@, x as int, old_i);\n assert(count_value_up_to(flowers@, x as int, old_i + 1) == 1);\n } else if x == old_max as i64 {\n assert(count_value_up_to(flowers@, old_max, old_i + 1) == old_max_count + 1);\n } else {\n assert(x as int != old_max);\n assert(count_value_up_to(flowers@, old_max, old_i + 1) == old_max_count);\n }\n if x < old_min as i64 {\n assert(min_val as int == x as int);\n assert(min_count as int == 1);\n assert(exists|k: int| 0 <= k < old_i + 1 && flowers[k] == min_val);\n assert forall|k: int| 0 <= k < old_i + 1 implies min_val <= #[trigger] flowers[k] by {\n if k < old_i {\n assert(old_min <= flowers[k] as int);\n }\n }\n } else {\n assert(min_val as int == old_min);\n assert(exists|k: int| 0 <= k < old_i + 1 && flowers[k] == min_val);\n assert forall|k: int| 0 <= k < old_i + 1 implies min_val <= #[trigger] flowers[k] by {\n if k < old_i {\n assert(old_min <= flowers[k] as int);\n }\n }\n }\n if x > old_max as i64 {\n assert(max_val as int == x as int);\n assert(max_count as int == 1);\n assert(exists|k: int| 0 <= k < old_i + 1 && flowers[k] == max_val);\n assert forall|k: int| 0 <= k < old_i + 1 implies #[trigger] flowers[k] <= max_val by {\n if k < old_i {\n assert(flowers[k] as int <= old_max);\n }\n }\n } else {\n assert(max_val as int == old_max);\n assert(exists|k: int| 0 <= k < old_i + 1 && flowers[k] == max_val);\n assert forall|k: int| 0 <= k < old_i + 1 implies #[trigger] flowers[k] <= max_val by {\n if k < old_i {\n assert(flowers[k] as int <= old_max);\n }\n }\n }\n }\n i += 1;\n }\n proof {\n assert(i == flowers.len());\n }\n let diff = max_val - min_val;\n let n = flowers.len() as i64;\n proof {\n let len = flowers.len() as int;\n let mc = min_count as int;\n let xc = max_count as int;\n assert(mc <= len);\n assert(xc <= len);\n assert(len <= 200_000);\n assert(len * len <= 200_000 * 200_000) by (nonlinear_arith)\n requires 0 <= len <= 200_000;\n assert(mc * xc <= 200_000 * 200_000) by (nonlinear_arith)\n requires 0 <= mc <= 200_000, 0 <= xc <= 200_000;\n assert(200_000 * 200_000 < 9_223_372_036_854_775_807int);\n assert(n as int == len);\n assert(n as int <= 200_000);\n assert((n as int) * ((n as int) - 1) <= 200_000 * 199_999) by (nonlinear_arith)\n requires n as int <= 200_000, n as int >= 2;\n assert((n as int) * ((n as int) - 1) / 2 <= 200_000 * 199_999 / 2);\n assert(200_000 * 199_999 / 2 < 9_223_372_036_854_775_807int);\n }\n let pair_count = if min_val == max_val {\n n * (n - 1) / 2\n } else {\n min_count * max_count\n };\n proof {\n let mn = min_val as int;\n let mx = max_val as int;\n assert(is_minimum(flowers@, mn));\n assert(is_maximum(flowers@, mx));\n assert(diff as int == mx - mn);\n if min_val == max_val {\n lemma_all_equal_from_min_max(flowers@, mn, mx);\n lemma_count_pairs_all_equal(flowers@, mn, flowers.len() as int);\n lemma_choose2_closed_form(flowers.len() as nat);\n assert(count_extremal_pairs_for_up_to(flowers@, mn, mx, flowers.len() as int) == choose2(flowers.len() as nat));\n assert(pair_count as int == choose2(flowers.len() as nat));\n } else {\n assert(mn < mx);\n lemma_count_pairs_distinct(flowers@, mn, mx, flowers.len() as int);\n assert(count_extremal_pairs_for_up_to(flowers@, mn, mx, flowers.len() as int)\n == count_value(flowers@, mn) * count_value(flowers@, mx));\n assert(pair_count as int == count_value(flowers@, mn) * count_value(flowers@, mx));\n }\n assert forall|a: int, b: int| 0 <= a < b < flowers.len() implies (\n #[trigger] is_extremal_pair_for(flowers@, mn, mx, a, b)\n <==> abs_diff(flowers[a] as int, flowers[b] as int) == diff as int\n ) by {\n lemma_extremal_pair_matches_max_difference(flowers@, mn, mx, a, b);\n }\n assert(exists|mn2: int, mx2: int| {\n &&& is_minimum(flowers@, mn2)\n &&& is_maximum(flowers@, mx2)\n &&& diff as int == mx2 - mn2\n &&& pair_count as int == count_extremal_pairs_for_up_to(flowers@, mn2, mx2, flowers.len() as int)\n &&& forall|a: int, b: int|\n 0 <= a < b < flowers.len() ==> (\n #[trigger] is_extremal_pair_for(flowers@, mn2, mx2, a, b)\n <==> abs_diff(flowers[a] as int, flowers[b] as int) == diff as int\n )\n });\n }\n (diff, pair_count)\n }\n}\n\n}", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_beauty_and_pair_count(flowers: Vec) -> (i64, i64) {\n let mut min_val = flowers[0];\n let mut max_val = flowers[0];\n let mut min_count = 1i64;\n let mut max_count = 1i64;\n let mut i = 1usize;\n while i < flowers.len() {\n let x = flowers[i];\n if x < min_val {\n min_val = x;\n min_count = 1;\n } else if x == min_val {\n min_count += 1;\n }\n if x > max_val {\n max_val = x;\n max_count = 1;\n } else if x == max_val {\n max_count += 1;\n }\n i += 1;\n }\n let diff = max_val - min_val;\n let n = flowers.len() as i64;\n let pair_count = if min_val == max_val {\n n * (n - 1) / 2\n } else {\n min_count * max_count\n };\n (diff, pair_count)\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let nums: Vec = input\n .split_whitespace()\n .map(|s| s.parse::().expect(\"integer\"))\n .collect();\n let n = nums[0] as usize;\n let flowers = nums[1..1 + n].to_vec();\n let ans = Solution::max_beauty_and_pair_count(flowers);\n println!(\"{} {}\", ans.0, ans.1);\n}\n", "checker": null, "testcases_path": "codeforces/cf459B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf459B/tests/mutated_testcases.jsonl"} {"problem": "cf466C", "family": "codeforces", "description": "# Number of Ways\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nYou've got array $a[1],\u2009a[2],\u2009...,\u2009a[n]$, consisting of $n$ integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. \n\nMore formally, you need to find the number of such pairs of indices $i,\u2009j$ $(2\u2009\u2264\u2009i\u2009\u2264\u2009j\u2009\u2264\u2009n\u2009-\u20091)$, that .\n\n## Input\n\nThe first line contains integer $n$ $(1\u2009\u2264\u2009n\u2009\u2264\u20095\u00b710^5)$, showing how many numbers are in the array. The second line contains $n$ integers $a[1]$, $a[2]$, ..., $a[n]$ $(|a[i]|\u2009\u2264\u2009\u200910^9)$ \u2014 the elements of array $a$.\n\n## Output\n\nPrint a single integer \u2014 the number of ways to split the array into three parts with the same sum.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n1 2 3 0 3\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n4\n0 1 -1 0\n```\n**Output:**\n```\n1\n```\n\n### Example 3\n\n**Input:**\n```\n2\n4 1\n```\n**Output:**\n```\n0\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_equal_sum_splits(nums: Vec) -> u64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut nums: Vec = Vec::with_capacity(n);\n let mut idx: usize = 0;\n while idx < n {\n nums.push(tokens.next().expect(\"array element\").parse().expect(\"valid i64\"));\n idx = idx + 1;\n }\n let answer = Solution::count_equal_sum_splits(nums);\n println!(\"{}\", answer);\n}\n```\n", "tags": "Rating: 1700\nbinary search, brute force, data structures, dp, two pointers\nSolved by: 41245 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn total_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, nums.len() as int)\n }\n\n pub open spec fn valid_split_pair(nums: Seq, i: int, j: int) -> bool {\n &&& 0 <= i < j < nums.len() - 1\n &&& {\n let s1 = Self::prefix_sum(nums, i + 1);\n let s2 = Self::prefix_sum(nums, j + 1) - Self::prefix_sum(nums, i + 1);\n let s3 = Self::total_sum(nums) - Self::prefix_sum(nums, j + 1);\n s1 == s2 && s2 == s3\n }\n }\n\n pub open spec fn count_valid_first_cuts(nums: Seq, j: int, i_end: int) -> nat\n recommends\n -1 <= j < nums.len() - 1,\n 0 <= i_end <= j,\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n Self::count_valid_first_cuts(nums, j, i_end - 1) + if Self::valid_split_pair(nums, i_end - 1, j) {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_valid_splits_upto(nums: Seq, j_end: int) -> nat\n recommends\n 0 <= j_end <= nums.len() - 1,\n decreases j_end,\n {\n if j_end <= 0 {\n 0\n } else {\n Self::count_valid_splits_upto(nums, j_end - 1)\n + Self::count_valid_first_cuts(nums, j_end - 1, j_end - 1)\n }\n }\n\n pub open spec fn count_valid_splits(nums: Seq) -> nat {\n if nums.len() < 3 {\n 0\n } else {\n Self::count_valid_splits_upto(nums, nums.len() - 1)\n }\n }\n\n pub fn count_equal_sum_splits(nums: Vec) -> (result: u64)\n requires\n 1 <= nums.len() <= 500_000,\n forall |k: int| 0 <= k < nums.len() ==> -1_000_000_000 <= #[trigger] nums[k] <= 1_000_000_000,\n ensures\n result as int == Self::count_valid_splits(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_equal_sum_splits(nums: Vec) -> u64 {\n let n = nums.len();\n if n < 3 {\n return 0;\n }\n\n let mut total: i128 = 0;\n let mut idx: usize = 0;\n while idx < n {\n total = total + nums[idx] as i128;\n idx = idx + 1;\n }\n\n let target = total / 3;\n if target * 3 != total {\n return 0;\n }\n\n let mut prefix: i128 = 0;\n let mut seen_targets: u64 = 0;\n let mut answer: u64 = 0;\n idx = 0;\n while idx < n - 1 {\n prefix = prefix + nums[idx] as i128;\n if prefix == target + target {\n answer = answer + seen_targets;\n }\n if prefix == target {\n seen_targets = seen_targets + 1;\n }\n idx = idx + 1;\n }\n answer\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn total_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, nums.len() as int)\n }\n\n pub open spec fn valid_split_pair(nums: Seq, i: int, j: int) -> bool {\n &&& 0 <= i < j < nums.len() - 1\n &&& {\n let s1 = Self::prefix_sum(nums, i + 1);\n let s2 = Self::prefix_sum(nums, j + 1) - Self::prefix_sum(nums, i + 1);\n let s3 = Self::total_sum(nums) - Self::prefix_sum(nums, j + 1);\n s1 == s2 && s2 == s3\n }\n }\n\n pub open spec fn count_valid_first_cuts(nums: Seq, j: int, i_end: int) -> nat\n recommends\n -1 <= j < nums.len() - 1,\n 0 <= i_end <= j,\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n Self::count_valid_first_cuts(nums, j, i_end - 1) + if Self::valid_split_pair(nums, i_end - 1, j) {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_valid_splits_upto(nums: Seq, j_end: int) -> nat\n recommends\n 0 <= j_end <= nums.len() - 1,\n decreases j_end,\n {\n if j_end <= 0 {\n 0\n } else {\n Self::count_valid_splits_upto(nums, j_end - 1)\n + Self::count_valid_first_cuts(nums, j_end - 1, j_end - 1)\n }\n }\n\n pub open spec fn count_valid_splits(nums: Seq) -> nat {\n if nums.len() < 3 {\n 0\n } else {\n Self::count_valid_splits_upto(nums, nums.len() - 1)\n }\n }\n\n pub fn count_equal_sum_splits(nums: Vec) -> (result: u64)\n requires\n 1 <= nums.len() <= 500_000,\n forall |k: int| 0 <= k < nums.len() ==> -1_000_000_000 <= #[trigger] nums[k] <= 1_000_000_000,\n ensures\n result as int == Self::count_valid_splits(nums@),\n {\n let n = nums.len();\n if n < 3 {\n return 0;\n }\n\n let mut total: i128 = 0;\n let mut idx: usize = 0;\n while idx < n {\n total = total + nums[idx] as i128;\n idx = idx + 1;\n }\n\n let target = total / 3;\n if target * 3 != total {\n return 0;\n }\n\n let mut prefix: i128 = 0;\n let mut seen_targets: u64 = 0;\n let mut answer: u64 = 0;\n idx = 0;\n while idx < n - 1 {\n prefix = prefix + nums[idx] as i128;\n if prefix == target + target {\n answer = answer + seen_targets;\n }\n if prefix == target {\n seen_targets = seen_targets + 1;\n }\n idx = idx + 1;\n }\n answer\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn total_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, nums.len() as int)\n }\n\n pub open spec fn valid_split_pair(nums: Seq, i: int, j: int) -> bool {\n &&& 0 <= i < j < nums.len() - 1\n &&& {\n let s1 = Self::prefix_sum(nums, i + 1);\n let s2 = Self::prefix_sum(nums, j + 1) - Self::prefix_sum(nums, i + 1);\n let s3 = Self::total_sum(nums) - Self::prefix_sum(nums, j + 1);\n s1 == s2 && s2 == s3\n }\n }\n\n pub open spec fn count_valid_first_cuts(nums: Seq, j: int, i_end: int) -> nat\n recommends\n -1 <= j < nums.len() - 1,\n 0 <= i_end <= j,\n decreases i_end,\n {\n if i_end <= 0 {\n 0\n } else {\n Self::count_valid_first_cuts(nums, j, i_end - 1) + if Self::valid_split_pair(nums, i_end - 1, j) {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_valid_splits_upto(nums: Seq, j_end: int) -> nat\n recommends\n 0 <= j_end <= nums.len() - 1,\n decreases j_end,\n {\n if j_end <= 0 {\n 0\n } else {\n Self::count_valid_splits_upto(nums, j_end - 1)\n + Self::count_valid_first_cuts(nums, j_end - 1, j_end - 1)\n }\n }\n\n pub open spec fn count_valid_splits(nums: Seq) -> nat {\n if nums.len() < 3 {\n 0\n } else {\n Self::count_valid_splits_upto(nums, nums.len() - 1)\n }\n }\n\n pub open spec fn count_target_prefixes(nums: Seq, target: int, end: int) -> nat\n recommends\n 0 <= end <= nums.len() - 1,\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_target_prefixes(nums, target, end - 1) + if Self::prefix_sum(nums, end) == target {\n 1nat\n } else {\n 0nat\n }\n }\n }\n\n pub open spec fn count_prefix_split_pairs(nums: Seq, target: int, end: int) -> nat\n recommends\n 0 <= end <= nums.len() - 1,\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_prefix_split_pairs(nums, target, end - 1) + if Self::prefix_sum(nums, end) == 2 * target {\n Self::count_target_prefixes(nums, target, end - 1)\n } else {\n 0nat\n }\n }\n }\n\n proof fn lemma_prefix_sum_step(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n ensures\n Self::prefix_sum(nums, i + 1) == Self::prefix_sum(nums, i) + nums[i] as int,\n {\n }\n\n proof fn lemma_prefix_targets_imply_valid_split_pair(nums: Seq, i: int, j: int, target: int)\n requires\n 0 <= i < j < nums.len() - 1,\n Self::prefix_sum(nums, i + 1) == target,\n Self::prefix_sum(nums, j + 1) == 2 * target,\n Self::total_sum(nums) == 3 * target,\n ensures\n Self::valid_split_pair(nums, i, j),\n {\n assert(Self::prefix_sum(nums, j + 1) - Self::prefix_sum(nums, i + 1) == target);\n assert(Self::total_sum(nums) - Self::prefix_sum(nums, j + 1) == target);\n }\n\n proof fn lemma_valid_split_pair_properties(nums: Seq, i: int, j: int)\n requires\n Self::valid_split_pair(nums, i, j),\n ensures\n Self::prefix_sum(nums, j + 1) == 2 * Self::prefix_sum(nums, i + 1),\n Self::total_sum(nums) == 3 * Self::prefix_sum(nums, i + 1),\n {\n let s1 = Self::prefix_sum(nums, i + 1);\n let s2 = Self::prefix_sum(nums, j + 1) - Self::prefix_sum(nums, i + 1);\n let s3 = Self::total_sum(nums) - Self::prefix_sum(nums, j + 1);\n assert(s1 == s2);\n assert(s2 == s3);\n assert(Self::prefix_sum(nums, j + 1) == s1 + s2);\n assert(Self::total_sum(nums) == s1 + s2 + s3);\n }\n\n proof fn lemma_valid_split_pair_iff_prefix_targets(nums: Seq, i: int, j: int, target: int)\n requires\n 0 <= i < j < nums.len() - 1,\n Self::total_sum(nums) == 3 * target,\n ensures\n Self::valid_split_pair(nums, i, j) == (Self::prefix_sum(nums, i + 1) == target && Self::prefix_sum(nums, j + 1) == 2 * target),\n {\n if Self::valid_split_pair(nums, i, j) {\n Self::lemma_valid_split_pair_properties(nums, i, j);\n assert(3 * Self::prefix_sum(nums, i + 1) == 3 * target);\n assert(Self::prefix_sum(nums, i + 1) == target);\n } else {\n if Self::prefix_sum(nums, i + 1) == target && Self::prefix_sum(nums, j + 1) == 2 * target {\n Self::lemma_prefix_targets_imply_valid_split_pair(nums, i, j, target);\n assert(false);\n }\n }\n }\n\n proof fn lemma_count_valid_first_cuts_match_target_prefixes(nums: Seq, target: int, j: int, i_end: int)\n requires\n 0 <= i_end <= j,\n j < nums.len() - 1,\n Self::total_sum(nums) == 3 * target,\n ensures\n Self::count_valid_first_cuts(nums, j, i_end) == if Self::prefix_sum(nums, j + 1) == 2 * target {\n Self::count_target_prefixes(nums, target, i_end)\n } else {\n 0nat\n },\n decreases i_end,\n {\n if i_end > 0 {\n Self::lemma_count_valid_first_cuts_match_target_prefixes(nums, target, j, i_end - 1);\n Self::lemma_valid_split_pair_iff_prefix_targets(nums, i_end - 1, j, target);\n }\n }\n\n proof fn lemma_count_valid_splits_match_prefix_counts(nums: Seq, target: int, end: int)\n requires\n 0 <= end <= nums.len() - 1,\n Self::total_sum(nums) == 3 * target,\n ensures\n Self::count_valid_splits_upto(nums, end) == Self::count_prefix_split_pairs(nums, target, end),\n decreases end,\n {\n if end > 0 {\n Self::lemma_count_valid_splits_match_prefix_counts(nums, target, end - 1);\n Self::lemma_count_valid_first_cuts_match_target_prefixes(nums, target, end - 1, end - 1);\n }\n }\n\n proof fn lemma_count_valid_first_cuts_positive_implies_exists(nums: Seq, j: int, i_end: int)\n requires\n 0 <= i_end <= j,\n j < nums.len() - 1,\n ensures\n Self::count_valid_first_cuts(nums, j, i_end) > 0 ==> exists |i: int| 0 <= i < i_end && #[trigger] Self::valid_split_pair(nums, i, j),\n decreases i_end,\n {\n if i_end > 0 {\n Self::lemma_count_valid_first_cuts_positive_implies_exists(nums, j, i_end - 1);\n if Self::count_valid_first_cuts(nums, j, i_end) > 0 {\n if Self::count_valid_first_cuts(nums, j, i_end - 1) > 0 {\n assert(exists |i: int| 0 <= i < i_end - 1 && Self::valid_split_pair(nums, i, j));\n let witness = choose |i: int| 0 <= i < i_end - 1 && Self::valid_split_pair(nums, i, j);\n assert(0 <= witness < i_end && Self::valid_split_pair(nums, witness, j));\n } else {\n assert(Self::valid_split_pair(nums, i_end - 1, j));\n assert(exists |i: int| 0 <= i < i_end && Self::valid_split_pair(nums, i, j));\n }\n }\n }\n }\n\n proof fn lemma_count_valid_splits_positive_implies_exists(nums: Seq, end: int)\n requires\n 0 <= end <= nums.len() - 1,\n ensures\n Self::count_valid_splits_upto(nums, end) > 0 ==> exists |i: int, j: int| 0 <= i < j < end && #[trigger] Self::valid_split_pair(nums, i, j),\n decreases end,\n {\n if end > 0 {\n Self::lemma_count_valid_splits_positive_implies_exists(nums, end - 1);\n Self::lemma_count_valid_first_cuts_positive_implies_exists(nums, end - 1, end - 1);\n if Self::count_valid_splits_upto(nums, end) > 0 {\n if Self::count_valid_splits_upto(nums, end - 1) > 0 {\n assert(exists |i: int, j: int| 0 <= i < j < end - 1 && Self::valid_split_pair(nums, i, j));\n assert(exists |i: int, j: int| 0 <= i < j < end && Self::valid_split_pair(nums, i, j)) by {\n let witness = choose |i: int, j: int| 0 <= i < j < end - 1 && Self::valid_split_pair(nums, i, j);\n assert(0 <= witness.0 < witness.1 < end);\n assert(Self::valid_split_pair(nums, witness.0, witness.1));\n };\n } else {\n assert(Self::count_valid_first_cuts(nums, end - 1, end - 1) > 0);\n assert(exists |i: int| 0 <= i < end - 1 && #[trigger] Self::valid_split_pair(nums, i, end - 1));\n let witness = choose |i: int| 0 <= i < end - 1 && #[trigger] Self::valid_split_pair(nums, i, end - 1);\n assert(0 <= witness < end - 1 < end && Self::valid_split_pair(nums, witness, end - 1));\n }\n }\n }\n }\n\n pub fn count_equal_sum_splits(nums: Vec) -> (result: u64)\n requires\n 1 <= nums.len() <= 500_000,\n forall |k: int| 0 <= k < nums.len() ==> -1_000_000_000 <= #[trigger] nums[k] <= 1_000_000_000,\n ensures\n result as int == Self::count_valid_splits(nums@),\n {\n let n = nums.len();\n if n < 3 {\n proof {\n assert(Self::count_valid_splits(nums@) == 0);\n }\n return 0;\n }\n\n let mut total: i128 = 0;\n let mut idx: usize = 0;\n while idx < n\n invariant\n n == nums.len(),\n 3 <= n <= 500_000,\n 0 <= idx <= n,\n forall |k: int| 0 <= k < n ==> -1_000_000_000 <= #[trigger] nums[k] <= 1_000_000_000,\n total as int == Self::prefix_sum(nums@, idx as int),\n -1_000_000_000 * idx as int <= total as int <= 1_000_000_000 * idx as int,\n decreases n - idx,\n {\n proof {\n Self::lemma_prefix_sum_step(nums@, idx as int);\n }\n total = total + nums[idx] as i128;\n idx = idx + 1;\n }\n\n let target = total / 3;\n if target * 3 != total {\n proof {\n assert(total as int == Self::total_sum(nums@));\n assert(!(exists |i: int, j: int| 0 <= i < j < nums@.len() - 1 && Self::valid_split_pair(nums@, i, j))) by {\n assert forall |i: int, j: int| 0 <= i < j < nums@.len() - 1 && Self::valid_split_pair(nums@, i, j) implies false by {\n Self::lemma_valid_split_pair_properties(nums@, i, j);\n assert(total as int == 3 * Self::prefix_sum(nums@, i + 1));\n assert(total == 3 * (Self::prefix_sum(nums@, i + 1) as i128));\n assert(target * 3 == total);\n }\n };\n assert(Self::count_valid_splits(nums@) == Self::count_valid_splits_upto(nums@, nums@.len() - 1));\n if Self::count_valid_splits(nums@) > 0 {\n Self::lemma_count_valid_splits_positive_implies_exists(nums@, nums@.len() - 1);\n assert(exists |i: int, j: int| 0 <= i < j < nums@.len() - 1 && Self::valid_split_pair(nums@, i, j));\n }\n assert(Self::count_valid_splits(nums@) == 0);\n }\n return 0;\n }\n\n proof {\n assert(total as int == Self::total_sum(nums@));\n assert(total as int == 3 * target as int);\n }\n\n let mut prefix: i128 = 0;\n let mut seen_targets: u64 = 0;\n let mut answer: u64 = 0;\n idx = 0;\n while idx < n - 1\n invariant\n n == nums.len(),\n 3 <= n <= 500_000,\n 0 <= idx <= n - 1,\n forall |k: int| 0 <= k < n ==> -1_000_000_000 <= #[trigger] nums[k] <= 1_000_000_000,\n total as int == Self::total_sum(nums@),\n total as int == 3 * target as int,\n prefix as int == Self::prefix_sum(nums@, idx as int),\n -1_000_000_000 * idx as int <= prefix as int <= 1_000_000_000 * idx as int,\n seen_targets as int == Self::count_target_prefixes(nums@, target as int, idx as int),\n seen_targets as int <= idx as int,\n answer as int == Self::count_prefix_split_pairs(nums@, target as int, idx as int),\n answer as int <= idx as int * idx as int,\n decreases n - 1 - idx,\n {\n let ghost prev_seen = seen_targets;\n let ghost prev_answer = answer;\n proof {\n Self::lemma_prefix_sum_step(nums@, idx as int);\n }\n prefix = prefix + nums[idx] as i128;\n if prefix == target + target {\n proof {\n assert(answer as int + seen_targets as int <= idx as int * idx as int + idx as int);\n assert(idx as int + 1 <= n as int);\n assert(idx as int * idx as int + idx as int <= n as int * n as int) by (nonlinear_arith)\n requires\n 0 <= idx as int,\n idx as int + 1 <= n as int,\n ;\n assert(n as int * n as int <= 250000000000) by (nonlinear_arith)\n requires\n 0 <= n as int,\n n as int <= 500000,\n ;\n }\n answer = answer + seen_targets;\n }\n if prefix == target {\n seen_targets = seen_targets + 1;\n }\n proof {\n assert(prefix as int == Self::prefix_sum(nums@, idx as int + 1));\n assert(Self::count_target_prefixes(nums@, target as int, idx as int + 1) == Self::count_target_prefixes(nums@, target as int, idx as int) + if Self::prefix_sum(nums@, idx as int + 1) == target as int {\n 1nat\n } else {\n 0nat\n });\n if prefix == target {\n assert(seen_targets == prev_seen + 1);\n assert(Self::prefix_sum(nums@, idx as int + 1) == target as int);\n } else {\n assert(seen_targets == prev_seen);\n assert(Self::prefix_sum(nums@, idx as int + 1) != target as int);\n }\n assert(Self::count_prefix_split_pairs(nums@, target as int, idx as int + 1) == Self::count_prefix_split_pairs(nums@, target as int, idx as int) + if Self::prefix_sum(nums@, idx as int + 1) == 2 * target as int {\n Self::count_target_prefixes(nums@, target as int, idx as int)\n } else {\n 0nat\n });\n if prefix == target + target {\n assert(answer as int == prev_answer as int + prev_seen as int);\n assert(prefix as int == 2 * target as int) by (nonlinear_arith)\n requires\n prefix == target + target,\n ;\n assert(Self::prefix_sum(nums@, idx as int + 1) == 2 * target as int);\n assert(answer as int <= idx as int * idx as int + idx as int);\n } else {\n assert(answer as int == prev_answer as int);\n assert(Self::prefix_sum(nums@, idx as int + 1) != 2 * target as int);\n assert(answer as int <= idx as int * idx as int + idx as int);\n }\n assert(seen_targets as int == Self::count_target_prefixes(nums@, target as int, idx as int + 1));\n assert(answer as int == Self::count_prefix_split_pairs(nums@, target as int, idx as int + 1));\n assert(seen_targets as int <= idx as int + 1);\n assert(answer as int <= (idx as int + 1) * (idx as int + 1)) by (nonlinear_arith)\n requires\n 0 <= idx as int,\n answer as int <= idx as int * idx as int + idx as int,\n ;\n }\n idx = idx + 1;\n }\n\n proof {\n Self::lemma_count_valid_splits_match_prefix_counts(nums@, target as int, n as int - 1);\n assert(Self::count_valid_splits(nums@) == Self::count_valid_splits_upto(nums@, n as int - 1));\n assert(answer as int == Self::count_valid_splits(nums@));\n }\n answer\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn count_equal_sum_splits(nums: Vec) -> u64 {\n let n = nums.len();\n if n < 3 {\n return 0;\n }\n\n let mut total: i128 = 0;\n let mut idx: usize = 0;\n while idx < n {\n total = total + nums[idx] as i128;\n idx = idx + 1;\n }\n\n let target = total / 3;\n if target * 3 != total {\n return 0;\n }\n\n let mut prefix: i128 = 0;\n let mut seen_targets: u64 = 0;\n let mut answer: u64 = 0;\n idx = 0;\n while idx < n - 1 {\n prefix = prefix + nums[idx] as i128;\n if prefix == target + target {\n answer = answer + seen_targets;\n }\n if prefix == target {\n seen_targets = seen_targets + 1;\n }\n idx = idx + 1;\n }\n answer\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut tokens = input.split_whitespace();\n let n: usize = tokens.next().expect(\"n\").parse().expect(\"valid n\");\n let mut nums: Vec = Vec::with_capacity(n);\n let mut idx: usize = 0;\n while idx < n {\n nums.push(tokens.next().expect(\"array element\").parse().expect(\"valid i64\"));\n idx = idx + 1;\n }\n let answer = Solution::count_equal_sum_splits(nums);\n println!(\"{}\", answer);\n}\n", "checker": null, "testcases_path": "codeforces/cf466C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf466C/tests/mutated_testcases.jsonl"} {"problem": "cf469A", "family": "codeforces", "description": "# I Wanna Be the Guy\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nThere is a game called \"I Wanna Be the Guy\", consisting of $n$ levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.\n\nLittle X can pass only $p$ levels of the game. And Little Y can pass only $q$ levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?\n\n## Input\n\nThe first line contains a single integer $n$ ($1\u2009\u2264\u2009\u2009n\u2009\u2264\u2009100$). \n\nThe next line contains an integer $p$ $(0\u2009\u2264\u2009p\u2009\u2264\u2009n)$ at first, then follows $p$ distinct integers $a_1,\u2009a_2,\u2009...,\u2009a_p$ $(1\u2009\u2264\u2009a_i\u2009\u2264\u2009n)$. These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to $n$.\n\n## Output\n\nIf they can pass all the levels, print \"`I become the guy.`\". If it's impossible, print \"`Oh, my keyboard!`\" (without the quotes).\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4\n3 1 2 3\n2 2 4\n```\n**Output:**\n```\nI become the guy.\n```\n\n### Example 2\n\n**Input:**\n```\n4\n3 1 2 3\n2 2 3\n```\n**Output:**\n```\nOh, my keyboard!\n```\n\n## Note\n\nIn the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.\n\nIn the second sample, no one can pass level 4.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_be_the_guy(n: i32, x_levels: Vec, y_levels: Vec) -> bool {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: i32 = lines.next().unwrap().unwrap().trim().parse().expect(\"n\");\n let line2: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n let p = line2[0] as usize;\n let x_levels: Vec = line2[1..1 + p].to_vec();\n let line3: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n let q = line3[0] as usize;\n let y_levels: Vec = line3[1..1 + q].to_vec();\n let ans = Solution::can_be_the_guy(n, x_levels, y_levels);\n if ans {\n println!(\"I become the guy.\");\n } else {\n println!(\"Oh, my keyboard!\");\n }\n}\n```\n", "tags": "800\nimplementation, math\n(solved count from API when available)\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn level_in_seq(seq: Seq, level: int) -> bool {\n exists|i: int| 0 <= i < seq.len() && #[trigger] seq[i] == level\n}\n\npub open spec fn level_passable(level: int, x_levels: Seq, y_levels: Seq) -> bool {\n level_in_seq(x_levels, level) || level_in_seq(y_levels, level)\n}\n\nimpl Solution {\n pub fn can_be_the_guy(n: i32, x_levels: Vec, y_levels: Vec) -> (res: bool)\n requires\n 1 <= n <= 100,\n x_levels.len() <= n as usize,\n y_levels.len() <= n as usize,\n forall|i: int| 0 <= i < x_levels.len() ==> 1 <= #[trigger] x_levels[i] && x_levels[i] <= n,\n forall|i: int| 0 <= i < y_levels.len() ==> 1 <= #[trigger] y_levels[i] && y_levels[i] <= n,\n forall|i: int, j: int| 0 <= i < j < x_levels.len() ==> x_levels[i] != x_levels[j],\n forall|i: int, j: int| 0 <= i < j < y_levels.len() ==> y_levels[i] != y_levels[j],\n ensures\n res == (forall|k: int| 1 <= k && k <= (n as int) ==> #[trigger] level_passable(k, x_levels@, y_levels@)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_be_the_guy(n: i32, x_levels: Vec, y_levels: Vec) -> bool {\n let mut k = 1i32;\n while k <= n {\n let x_len = x_levels.len();\n let y_len = y_levels.len();\n let mut found = false;\n let mut i = 0usize;\n while i < x_len && !found {\n if x_levels[i] == k {\n found = true;\n } else {\n i += 1;\n }\n }\n if !found {\n i = 0;\n while i < y_len && !found {\n if y_levels[i] == k {\n found = true;\n } else {\n i += 1;\n }\n }\n }\n if !found {\n return false;\n }\n k += 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn level_in_seq(seq: Seq, level: int) -> bool {\n exists|i: int| 0 <= i < seq.len() && #[trigger] seq[i] == level\n}\n\npub open spec fn level_passable(level: int, x_levels: Seq, y_levels: Seq) -> bool {\n level_in_seq(x_levels, level) || level_in_seq(y_levels, level)\n}\n\nimpl Solution {\n pub fn can_be_the_guy(n: i32, x_levels: Vec, y_levels: Vec) -> (res: bool)\n requires\n 1 <= n <= 100,\n x_levels.len() <= n as usize,\n y_levels.len() <= n as usize,\n forall|i: int| 0 <= i < x_levels.len() ==> 1 <= #[trigger] x_levels[i] && x_levels[i] <= n,\n forall|i: int| 0 <= i < y_levels.len() ==> 1 <= #[trigger] y_levels[i] && y_levels[i] <= n,\n forall|i: int, j: int| 0 <= i < j < x_levels.len() ==> x_levels[i] != x_levels[j],\n forall|i: int, j: int| 0 <= i < j < y_levels.len() ==> y_levels[i] != y_levels[j],\n ensures\n res == (forall|k: int| 1 <= k && k <= (n as int) ==> #[trigger] level_passable(k, x_levels@, y_levels@)),\n {\n let mut k = 1i32;\n while k <= n {\n let x_len = x_levels.len();\n let y_len = y_levels.len();\n let mut found = false;\n let mut i = 0usize;\n while i < x_len && !found {\n if x_levels[i] == k {\n found = true;\n } else {\n i += 1;\n }\n }\n if !found {\n i = 0;\n while i < y_len && !found {\n if y_levels[i] == k {\n found = true;\n } else {\n i += 1;\n }\n }\n }\n if !found {\n return false;\n }\n k += 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn level_in_seq(seq: Seq, level: int) -> bool {\n exists|i: int| 0 <= i < seq.len() && #[trigger] seq[i] == level\n}\n\npub open spec fn level_passable(level: int, x_levels: Seq, y_levels: Seq) -> bool {\n level_in_seq(x_levels, level) || level_in_seq(y_levels, level)\n}\n\nimpl Solution {\n pub fn can_be_the_guy(n: i32, x_levels: Vec, y_levels: Vec) -> (res: bool)\n requires\n 1 <= n <= 100,\n x_levels.len() <= n as usize,\n y_levels.len() <= n as usize,\n forall|i: int| 0 <= i < x_levels.len() ==> 1 <= #[trigger] x_levels[i] && x_levels[i] <= n,\n forall|i: int| 0 <= i < y_levels.len() ==> 1 <= #[trigger] y_levels[i] && y_levels[i] <= n,\n forall|i: int, j: int| 0 <= i < j < x_levels.len() ==> x_levels[i] != x_levels[j],\n forall|i: int, j: int| 0 <= i < j < y_levels.len() ==> y_levels[i] != y_levels[j],\n ensures\n res == (forall|k: int| 1 <= k && k <= (n as int) ==> #[trigger] level_passable(k, x_levels@, y_levels@)),\n {\n let mut k = 1i32;\n while k <= n\n invariant\n 1 <= k <= (n as int) + 1,\n (n as int) <= 100,\n forall|level: int| 1 <= level && level < k ==> #[trigger] level_passable(level, x_levels@, y_levels@),\n decreases\n (n as int) + 1 - k,\n {\n let x_len = x_levels.len();\n let y_len = y_levels.len();\n let mut found = false;\n let mut i = 0usize;\n while i < x_len && !found\n invariant\n 0 <= i <= x_len,\n x_len == x_levels.len(),\n forall|j: int| 0 <= j && j < (i as int) ==> (x_levels[j] as int) != (k as int),\n (i as int) >= (x_len as int) ==> (forall|j: int| 0 <= j && j < (x_len as int) ==> (x_levels[j] as int) != (k as int)),\n found ==> #[trigger] level_in_seq(x_levels@, k as int),\n decreases\n (if found { 0int } else { 1int }),\n (x_len - i) as int,\n {\n if x_levels[i] == k {\n proof {\n assert(x_levels@[i as int] == k as int);\n assert(level_in_seq(x_levels@, k as int));\n assert(level_passable(k as int, x_levels@, y_levels@));\n }\n found = true;\n } else {\n i += 1;\n }\n }\n proof {\n if found {\n assert(level_in_seq(x_levels@, k as int));\n assert(level_passable(k as int, x_levels@, y_levels@));\n }\n }\n if !found {\n proof {\n assert(!(i < x_len && !found));\n assert((i as int) >= (x_len as int) || found);\n assert((i as int) >= (x_len as int));\n assert(forall|j: int| 0 <= j && j < (x_len as int) ==> (x_levels[j] as int) != (k as int));\n }\n i = 0;\n while i < y_len && !found\n invariant\n 0 <= i <= y_len,\n y_len == y_levels.len(),\n x_len == x_levels.len(),\n forall|j: int| 0 <= j && j < (i as int) ==> (y_levels[j] as int) != (k as int),\n forall|j: int| 0 <= j && j < (x_len as int) ==> (x_levels[j] as int) != (k as int),\n (i as int) >= (y_len as int) ==> (forall|j: int| 0 <= j && j < (y_len as int) ==> (y_levels[j] as int) != (k as int)),\n found ==> #[trigger] level_passable(k as int, x_levels@, y_levels@),\n decreases\n (if found { 0int } else { 1int }),\n (y_len - i) as int,\n {\n if y_levels[i] == k {\n proof {\n assert(y_levels@[i as int] == k as int);\n assert(level_in_seq(y_levels@, k as int));\n assert(level_passable(k as int, x_levels@, y_levels@));\n }\n found = true;\n } else {\n i += 1;\n }\n }\n proof {\n assert(!(i < y_len && !found));\n assert((i as int) >= (y_len as int) || found);\n if !found {\n assert((i as int) >= (y_len as int));\n }\n }\n }\n if !found {\n proof {\n assert((i as int) >= (y_len as int));\n assert(forall|j: int| 0 <= j && j < (y_len as int) ==> (y_levels[j] as int) != (k as int));\n assert(!level_in_seq(x_levels@, k as int));\n assert(!level_in_seq(y_levels@, k as int));\n assert(!level_passable(k as int, x_levels@, y_levels@));\n }\n return false;\n }\n proof {\n assert(found);\n assert(level_passable(k as int, x_levels@, y_levels@));\n assert((k as int) <= (n as int));\n assert(1 <= k);\n assert((k as int) + 1 <= (n as int) + 1);\n assert((k as int) <= 100);\n }\n k += 1;\n }\n true\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn can_be_the_guy(n: i32, x_levels: Vec, y_levels: Vec) -> bool {\n let mut k = 1i32;\n while k <= n {\n let x_len = x_levels.len();\n let y_len = y_levels.len();\n let mut found = false;\n let mut i = 0usize;\n while i < x_len && !found {\n if x_levels[i] == k {\n found = true;\n } else {\n i += 1;\n }\n }\n if !found {\n i = 0;\n while i < y_len && !found {\n if y_levels[i] == k {\n found = true;\n } else {\n i += 1;\n }\n }\n }\n if !found {\n return false;\n }\n k += 1;\n }\n true\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: i32 = lines.next().unwrap().unwrap().trim().parse().expect(\"n\");\n let line2: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n let p = line2[0] as usize;\n let x_levels: Vec = line2[1..1 + p].to_vec();\n let line3: Vec = lines.next().unwrap().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();\n let q = line3[0] as usize;\n let y_levels: Vec = line3[1..1 + q].to_vec();\n let ans = Solution::can_be_the_guy(n, x_levels, y_levels);\n if ans {\n println!(\"I become the guy.\");\n } else {\n println!(\"Oh, my keyboard!\");\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf469A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf469A/tests/mutated_testcases.jsonl"} {"problem": "cf474D", "family": "codeforces", "description": "# Flowers\n\nTime limit: 1.5 seconds | Memory limit: 256 megabytes\n\nWe saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.\n\nBut, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size $k$.\n\nNow Marmot wonders in how many ways he can eat between $a$ and $b$ flowers. As the number of ways could be very large, print it modulo $1000000007$ ($10^9\u2009+\u20097$).\n\n## Input\n\nInput contains several test cases.\n\nThe first line contains two integers $t$ and $k$ ($1\u2009\u2264\u2009t,\u2009k\u2009\u2264\u200910^5$), where $t$ represents the number of test cases.\n\nThe next $t$ lines contain two integers $a_i$ and $b_i$ ($1\u2009\u2264\u2009a_i\u2009\u2264\u2009b_i\u2009\u2264\u200910^5$), describing the $i$-th test.\n\n## Output\n\nPrint $t$ lines to the standard output. The $i$-th line should contain the number of ways in which Marmot can eat between $a_i$ and $b_i$ flowers at dinner modulo $1000000007$ ($10^9\u2009+\u20097$).\n\n## Example\n\n**Input:**\n```\n3 2\n1 3\n2 3\n4 4\n```\n**Output:**\n```\n6\n5\n5\n```\n\n## Note\n\n- For $K$ = $2$ and length $1$ Marmot can eat ($R$). \n- For $K$ = $2$ and length $2$ Marmot can eat ($RR$) and ($WW$). \n- For $K$ = $2$ and length $3$ Marmot can eat ($RRR$), ($RWW$) and ($WWR$). \n- For $K$ = $2$ and length $4$ Marmot can eat, for example, ($WWWW$) or ($RWWR$), but for example he can't eat ($WWWR$).\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn solve_queries(k: i32, lefts: Vec, rights: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let k: i32 = it.next().unwrap().parse().unwrap();\n let mut lefts = Vec::with_capacity(t);\n let mut rights = Vec::with_capacity(t);\n let mut i = 0usize;\n while i < t {\n let left: i32 = it.next().unwrap().parse().unwrap();\n let right: i32 = it.next().unwrap().parse().unwrap();\n lefts.push(left);\n rights.push(right);\n i += 1;\n }\n let answers = Solution::solve_queries(k, lefts, rights);\n let mut i = 0usize;\n while i < answers.len() {\n println!(\"{}\", answers[i]);\n i += 1;\n }\n}\n```\n", "tags": "Rating: 1700\ndp\nSolved by: 30159 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn modulus() -> int {\n 1_000_000_007\n}\n\npub open spec fn sum_blocks(blocks: Seq) -> int\n decreases blocks.len(),\n{\n if blocks.len() == 0 {\n 0\n } else {\n sum_blocks(blocks.drop_last()) + blocks[blocks.len() - 1]\n }\n}\n\npub open spec fn valid_dinner_blocks(blocks: Seq, total: int, k: int) -> bool\n recommends\n 0 <= total,\n 1 <= k,\n{\n &&& sum_blocks(blocks) == total\n &&& forall|i: int| 0 <= i < blocks.len() ==> blocks[i] == 1 || blocks[i] == k\n}\n\npub open spec fn count_valid_dinners(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 1\n } else if k == 0 {\n 0\n } else if total < k {\n count_valid_dinners((total - 1) as nat, k)\n } else {\n count_valid_dinners((total - 1) as nat, k) + count_valid_dinners((total - k) as nat, k)\n }\n}\n\npub open spec fn count_valid_dinners_up_to(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 0\n } else {\n count_valid_dinners_up_to((total - 1) as nat, k) + count_valid_dinners(total, k)\n }\n}\n\npub open spec fn query_answer(left: int, right: int, k: int) -> int\n recommends\n 1 <= left <= right,\n 1 <= k,\n{\n (count_valid_dinners_up_to(right as nat, k as nat) - count_valid_dinners_up_to((left - 1) as nat, k as nat)) % modulus()\n}\n\nimpl Solution {\n pub fn solve_queries(k: i32, lefts: Vec, rights: Vec) -> (res: Vec)\n requires\n 1 <= k <= 100_000,\n 1 <= lefts.len() == rights.len() <= 100_000,\n forall|i: int|\n 0 <= i < lefts.len() ==> 1 <= #[trigger] lefts[i] <= rights[i] <= 100_000,\n ensures\n res.len() == lefts.len(),\n forall|i: int|\n 0 <= i < res.len() ==> 0 <= #[trigger] res[i] < modulus()\n && res[i] as int == query_answer(lefts[i] as int, rights[i] as int, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn add_mod(x: i32, y: i32) -> i32 {\n let sum = x + y;\n if sum >= 1_000_000_007i32 {\n sum - 1_000_000_007i32\n } else {\n sum\n }\n }\n\n fn sub_mod(x: i32, y: i32) -> i32 {\n if x >= y {\n x - y\n } else {\n x + 1_000_000_007i32 - y\n }\n }\n\n pub fn solve_queries(k: i32, lefts: Vec, rights: Vec) -> Vec {\n let max_n = 100_000usize;\n let mut ways = Vec::new();\n ways.push(1);\n let mut prefix = Vec::new();\n prefix.push(0);\n let mut len = 1usize;\n while len <= max_n {\n let prev = ways[len - 1];\n let next = if len < k as usize {\n prev\n } else {\n Self::add_mod(prev, ways[len - k as usize])\n };\n let next_prefix = Self::add_mod(prefix[len - 1], next);\n ways.push(next);\n prefix.push(next_prefix);\n len += 1;\n }\n let mut answers = Vec::new();\n let mut idx = 0usize;\n while idx < lefts.len() {\n let left = lefts[idx];\n let right = rights[idx];\n let answer = if left == 1 {\n prefix[right as usize]\n } else {\n Self::sub_mod(prefix[right as usize], prefix[left as usize - 1])\n };\n answers.push(answer);\n idx += 1;\n }\n answers\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn modulus() -> int {\n 1_000_000_007\n}\n\npub open spec fn sum_blocks(blocks: Seq) -> int\n decreases blocks.len(),\n{\n if blocks.len() == 0 {\n 0\n } else {\n sum_blocks(blocks.drop_last()) + blocks[blocks.len() - 1]\n }\n}\n\npub open spec fn valid_dinner_blocks(blocks: Seq, total: int, k: int) -> bool\n recommends\n 0 <= total,\n 1 <= k,\n{\n &&& sum_blocks(blocks) == total\n &&& forall|i: int| 0 <= i < blocks.len() ==> blocks[i] == 1 || blocks[i] == k\n}\n\npub open spec fn count_valid_dinners(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 1\n } else if k == 0 {\n 0\n } else if total < k {\n count_valid_dinners((total - 1) as nat, k)\n } else {\n count_valid_dinners((total - 1) as nat, k) + count_valid_dinners((total - k) as nat, k)\n }\n}\n\npub open spec fn count_valid_dinners_up_to(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 0\n } else {\n count_valid_dinners_up_to((total - 1) as nat, k) + count_valid_dinners(total, k)\n }\n}\n\npub open spec fn query_answer(left: int, right: int, k: int) -> int\n recommends\n 1 <= left <= right,\n 1 <= k,\n{\n (count_valid_dinners_up_to(right as nat, k as nat) - count_valid_dinners_up_to((left - 1) as nat, k as nat)) % modulus()\n}\n\npub open spec fn count_valid_dinners_mod(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 1\n } else if k == 0 {\n 0\n } else if total < k {\n count_valid_dinners_mod((total - 1) as nat, k)\n } else {\n (count_valid_dinners_mod((total - 1) as nat, k) + count_valid_dinners_mod((total - k) as nat, k)) % modulus()\n }\n}\n\npub open spec fn count_valid_dinners_up_to_mod(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 0\n } else {\n (count_valid_dinners_up_to_mod((total - 1) as nat, k) + count_valid_dinners_mod(total, k)) % modulus()\n }\n}\n\npub open spec fn sub_mod_int(x: int, y: int) -> int\n recommends\n 0 <= x < modulus(),\n 0 <= y < modulus(),\n{\n if x >= y {\n x - y\n } else {\n x + modulus() - y\n }\n}\n\npub open spec fn query_answer_mod(left: int, right: int, k: int) -> int\n recommends\n 1 <= left <= right,\n 1 <= k,\n{\n if left == 1 {\n count_valid_dinners_up_to_mod(right as nat, k as nat)\n } else {\n sub_mod_int(count_valid_dinners_up_to_mod(right as nat, k as nat), count_valid_dinners_up_to_mod((left - 1) as nat, k as nat))\n }\n}\n\nimpl Solution {\n fn add_mod(x: i32, y: i32) -> (res: i32)\n requires\n 0 <= x < modulus(),\n 0 <= y < modulus(),\n ensures\n 0 <= res < modulus(),\n res as int == ((x as int) + (y as int)) % modulus(),\n {\n let sum = x + y;\n if sum >= 1_000_000_007i32 {\n sum - 1_000_000_007i32\n } else {\n sum\n }\n }\n\n fn sub_mod(x: i32, y: i32) -> (res: i32)\n requires\n 0 <= x < modulus(),\n 0 <= y < modulus(),\n ensures\n 0 <= res < modulus(),\n res as int == sub_mod_int(x as int, y as int),\n {\n if x >= y {\n x - y\n } else {\n x + 1_000_000_007i32 - y\n }\n }\n\n pub fn solve_queries(k: i32, lefts: Vec, rights: Vec) -> (res: Vec)\n requires\n 1 <= k <= 100_000,\n 1 <= lefts.len() == rights.len() <= 100_000,\n forall|i: int|\n 0 <= i < lefts.len() ==> 1 <= #[trigger] lefts[i] <= rights[i] <= 100_000,\n ensures\n res.len() == lefts.len(),\n forall|i: int|\n 0 <= i < res.len() ==> 0 <= #[trigger] res[i] < modulus()\n && res[i] as int == query_answer(lefts[i] as int, rights[i] as int, k as int),\n {\n let max_n = 100_000usize;\n let mut ways = Vec::new();\n ways.push(1);\n let mut prefix = Vec::new();\n prefix.push(0);\n let mut len = 1usize;\n while len <= max_n {\n let prev = ways[len - 1];\n let current = len;\n let next = if len < k as usize {\n prev\n } else {\n Self::add_mod(prev, ways[len - k as usize])\n };\n let next_prefix = Self::add_mod(prefix[len - 1], next);\n ways.push(next);\n prefix.push(next_prefix);\n len += 1;\n }\n let mut answers = Vec::new();\n let mut idx = 0usize;\n while idx < lefts.len() {\n let left = lefts[idx];\n let right = rights[idx];\n let answer = if left == 1 {\n prefix[right as usize]\n } else {\n Self::sub_mod(prefix[right as usize], prefix[left as usize - 1])\n };\n answers.push(answer);\n idx += 1;\n }\n answers\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn modulus() -> int {\n 1_000_000_007\n}\n\npub open spec fn sum_blocks(blocks: Seq) -> int\n decreases blocks.len(),\n{\n if blocks.len() == 0 {\n 0\n } else {\n sum_blocks(blocks.drop_last()) + blocks[blocks.len() - 1]\n }\n}\n\npub open spec fn valid_dinner_blocks(blocks: Seq, total: int, k: int) -> bool\n recommends\n 0 <= total,\n 1 <= k,\n{\n &&& sum_blocks(blocks) == total\n &&& forall|i: int| 0 <= i < blocks.len() ==> blocks[i] == 1 || blocks[i] == k\n}\n\npub open spec fn count_valid_dinners(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 1\n } else if k == 0 {\n 0\n } else if total < k {\n count_valid_dinners((total - 1) as nat, k)\n } else {\n count_valid_dinners((total - 1) as nat, k) + count_valid_dinners((total - k) as nat, k)\n }\n}\n\npub open spec fn count_valid_dinners_up_to(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 0\n } else {\n count_valid_dinners_up_to((total - 1) as nat, k) + count_valid_dinners(total, k)\n }\n}\n\npub open spec fn query_answer(left: int, right: int, k: int) -> int\n recommends\n 1 <= left <= right,\n 1 <= k,\n{\n (count_valid_dinners_up_to(right as nat, k as nat) - count_valid_dinners_up_to((left - 1) as nat, k as nat)) % modulus()\n}\n\npub open spec fn count_valid_dinners_mod(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 1\n } else if k == 0 {\n 0\n } else if total < k {\n count_valid_dinners_mod((total - 1) as nat, k)\n } else {\n (count_valid_dinners_mod((total - 1) as nat, k) + count_valid_dinners_mod((total - k) as nat, k)) % modulus()\n }\n}\n\npub open spec fn count_valid_dinners_up_to_mod(total: nat, k: nat) -> int\n recommends\n 1 <= k,\n decreases total,\n{\n if total == 0 {\n 0\n } else {\n (count_valid_dinners_up_to_mod((total - 1) as nat, k) + count_valid_dinners_mod(total, k)) % modulus()\n }\n}\n\npub open spec fn sub_mod_int(x: int, y: int) -> int\n recommends\n 0 <= x < modulus(),\n 0 <= y < modulus(),\n{\n if x >= y {\n x - y\n } else {\n x + modulus() - y\n }\n}\n\npub open spec fn query_answer_mod(left: int, right: int, k: int) -> int\n recommends\n 1 <= left <= right,\n 1 <= k,\n{\n if left == 1 {\n count_valid_dinners_up_to_mod(right as nat, k as nat)\n } else {\n sub_mod_int(count_valid_dinners_up_to_mod(right as nat, k as nat), count_valid_dinners_up_to_mod((left - 1) as nat, k as nat))\n }\n}\n\nproof fn lemma_count_valid_dinners_nonnegative(total: nat, k: nat)\n requires\n 1 <= k,\n ensures\n 0 <= count_valid_dinners(total, k),\n decreases total,\n{\n if total > 0 {\n lemma_count_valid_dinners_nonnegative((total - 1) as nat, k);\n if total >= k {\n lemma_count_valid_dinners_nonnegative((total - k) as nat, k);\n }\n }\n}\n\nproof fn lemma_count_valid_dinners_up_to_nonnegative(total: nat, k: nat)\n requires\n 1 <= k,\n ensures\n 0 <= count_valid_dinners_up_to(total, k),\n decreases total,\n{\n if total > 0 {\n lemma_count_valid_dinners_up_to_nonnegative((total - 1) as nat, k);\n lemma_count_valid_dinners_nonnegative(total, k);\n }\n}\n\nproof fn lemma_mod_add(x: int, y: int)\n requires\n 0 <= x,\n 0 <= y,\n ensures\n ((x % modulus()) + (y % modulus())) % modulus() == (x + y) % modulus(),\n{\n}\n\nproof fn lemma_mod_sub(x: int, y: int)\n requires\n 0 <= y <= x,\n ensures\n sub_mod_int(x % modulus(), y % modulus()) == (x - y) % modulus(),\n{\n}\n\nproof fn lemma_count_valid_dinners_mod_correct(total: nat, k: nat)\n requires\n 1 <= k,\n ensures\n count_valid_dinners_mod(total, k) == count_valid_dinners(total, k) % modulus(),\n 0 <= count_valid_dinners_mod(total, k) < modulus(),\n decreases total,\n{\n lemma_count_valid_dinners_nonnegative(total, k);\n if total > 0 {\n lemma_count_valid_dinners_mod_correct((total - 1) as nat, k);\n if total >= k {\n lemma_count_valid_dinners_mod_correct((total - k) as nat, k);\n lemma_count_valid_dinners_nonnegative((total - 1) as nat, k);\n lemma_count_valid_dinners_nonnegative((total - k) as nat, k);\n assert(0 <= count_valid_dinners((total - 1) as nat, k));\n assert(0 <= count_valid_dinners((total - k) as nat, k));\n lemma_mod_add(count_valid_dinners((total - 1) as nat, k), count_valid_dinners((total - k) as nat, k));\n }\n }\n}\n\nproof fn lemma_count_valid_dinners_up_to_monotone(start: nat, end: nat, k: nat)\n requires\n start <= end,\n 1 <= k,\n ensures\n count_valid_dinners_up_to(start, k) <= count_valid_dinners_up_to(end, k),\n decreases end - start,\n{\n if start < end {\n lemma_count_valid_dinners_up_to_monotone(start, (end - 1) as nat, k);\n lemma_count_valid_dinners_nonnegative(end, k);\n }\n}\n\nproof fn lemma_count_valid_dinners_up_to_mod_correct(total: nat, k: nat)\n requires\n 1 <= k,\n ensures\n count_valid_dinners_up_to_mod(total, k) == count_valid_dinners_up_to(total, k) % modulus(),\n 0 <= count_valid_dinners_up_to_mod(total, k) < modulus(),\n decreases total,\n{\n if total > 0 {\n lemma_count_valid_dinners_up_to_mod_correct((total - 1) as nat, k);\n lemma_count_valid_dinners_mod_correct(total, k);\n lemma_count_valid_dinners_up_to_nonnegative((total - 1) as nat, k);\n lemma_count_valid_dinners_nonnegative(total, k);\n lemma_mod_add(count_valid_dinners_up_to((total - 1) as nat, k), count_valid_dinners(total, k));\n }\n}\n\nproof fn lemma_query_answer_mod_correct(left: int, right: int, k: int)\n requires\n 1 <= left <= right,\n 1 <= k,\n ensures\n query_answer_mod(left, right, k) == query_answer(left, right, k),\n 0 <= query_answer_mod(left, right, k) < modulus(),\n{\n lemma_count_valid_dinners_up_to_mod_correct(right as nat, k as nat);\n if left == 1 {\n } else {\n lemma_count_valid_dinners_up_to_mod_correct((left - 1) as nat, k as nat);\n lemma_count_valid_dinners_up_to_nonnegative((left - 1) as nat, k as nat);\n lemma_count_valid_dinners_up_to_monotone((left - 1) as nat, right as nat, k as nat);\n lemma_mod_sub(count_valid_dinners_up_to(right as nat, k as nat), count_valid_dinners_up_to((left - 1) as nat, k as nat));\n }\n}\n\nimpl Solution {\n fn add_mod(x: i32, y: i32) -> (res: i32)\n requires\n 0 <= x < modulus(),\n 0 <= y < modulus(),\n ensures\n 0 <= res < modulus(),\n res as int == ((x as int) + (y as int)) % modulus(),\n {\n let sum = x + y;\n if sum >= 1_000_000_007i32 {\n sum - 1_000_000_007i32\n } else {\n sum\n }\n }\n\n fn sub_mod(x: i32, y: i32) -> (res: i32)\n requires\n 0 <= x < modulus(),\n 0 <= y < modulus(),\n ensures\n 0 <= res < modulus(),\n res as int == sub_mod_int(x as int, y as int),\n {\n if x >= y {\n x - y\n } else {\n x + 1_000_000_007i32 - y\n }\n }\n\n pub fn solve_queries(k: i32, lefts: Vec, rights: Vec) -> (res: Vec)\n requires\n 1 <= k <= 100_000,\n 1 <= lefts.len() == rights.len() <= 100_000,\n forall|i: int|\n 0 <= i < lefts.len() ==> 1 <= #[trigger] lefts[i] <= rights[i] <= 100_000,\n ensures\n res.len() == lefts.len(),\n forall|i: int|\n 0 <= i < res.len() ==> 0 <= #[trigger] res[i] < modulus()\n && res[i] as int == query_answer(lefts[i] as int, rights[i] as int, k as int),\n {\n let max_n = 100_000usize;\n let mut ways = Vec::new();\n ways.push(1);\n let mut prefix = Vec::new();\n prefix.push(0);\n let mut len = 1usize;\n while len <= max_n\n invariant\n 1 <= k <= 100_000,\n 1 <= lefts.len() == rights.len() <= 100_000,\n forall|i: int|\n 0 <= i < lefts.len() ==> 1 <= #[trigger] lefts[i] <= rights[i] <= 100_000,\n max_n == 100_000usize,\n ways.len() == len,\n prefix.len() == len,\n 1 <= len <= max_n + 1,\n forall|j: int|\n 0 <= j < len ==> 0 <= #[trigger] ways[j] < modulus()\n && ways[j] as int == count_valid_dinners_mod(j as nat, k as nat),\n forall|j: int|\n 0 <= j < len ==> 0 <= #[trigger] prefix[j] < modulus()\n && prefix[j] as int == count_valid_dinners_up_to_mod(j as nat, k as nat),\n decreases max_n + 1 - len,\n {\n let prev = ways[len - 1];\n let current = len;\n let next = if len < k as usize {\n prev\n } else {\n Self::add_mod(prev, ways[len - k as usize])\n };\n let next_prefix = Self::add_mod(prefix[len - 1], next);\n proof {\n lemma_count_valid_dinners_mod_correct(current as nat, k as nat);\n lemma_count_valid_dinners_up_to_mod_correct(current as nat, k as nat);\n }\n let ghost old_ways = ways@;\n let ghost old_prefix = prefix@;\n ways.push(next);\n prefix.push(next_prefix);\n len += 1;\n proof {\n assert(ways@ == old_ways.push(next));\n assert(prefix@ == old_prefix.push(next_prefix));\n assert forall|j: int|\n 0 <= j < len implies 0 <= #[trigger] ways[j] < modulus()\n && ways[j] as int == count_valid_dinners_mod(j as nat, k as nat) by {\n if j == current as int {\n if current < k as usize {\n assert(next == prev);\n } else {\n assert(next as int == ((prev as int) + (ways[current - k as usize] as int)) % modulus());\n }\n } else {\n assert(j < current as int);\n assert(ways[j] == old_ways[j]);\n }\n }\n assert forall|j: int|\n 0 <= j < len implies 0 <= #[trigger] prefix[j] < modulus()\n && prefix[j] as int == count_valid_dinners_up_to_mod(j as nat, k as nat) by {\n if j == current as int {\n assert(next_prefix as int == ((prefix[current - 1] as int) + (next as int)) % modulus());\n } else {\n assert(j < current as int);\n assert(prefix[j] == old_prefix[j]);\n }\n }\n }\n }\n let mut answers = Vec::new();\n let mut idx = 0usize;\n while idx < lefts.len()\n invariant\n 1 <= k <= 100_000,\n 1 <= lefts.len() == rights.len() <= 100_000,\n forall|i: int|\n 0 <= i < lefts.len() ==> 1 <= #[trigger] lefts[i] <= rights[i] <= 100_000,\n max_n == 100_000usize,\n prefix.len() == max_n + 1,\n forall|j: int|\n 0 <= j < prefix.len() ==> 0 <= #[trigger] prefix[j] < modulus()\n && prefix[j] as int == count_valid_dinners_up_to_mod(j as nat, k as nat),\n idx <= lefts.len(),\n answers.len() == idx,\n forall|j: int|\n 0 <= j < answers.len() ==> 0 <= #[trigger] answers[j] < modulus()\n && answers[j] as int == query_answer(lefts[j] as int, rights[j] as int, k as int),\n decreases lefts.len() - idx,\n {\n let left = lefts[idx];\n let right = rights[idx];\n proof {\n assert(1 <= lefts[idx as int] <= rights[idx as int] <= 100_000);\n lemma_query_answer_mod_correct(left as int, right as int, k as int);\n assert(right <= 100_000);\n assert(max_n == 100_000usize);\n assert(prefix.len() == max_n + 1);\n assert((right as usize) < prefix.len());\n if left > 1 {\n assert(left <= 100_000);\n assert((left as usize) - 1 < prefix.len());\n }\n }\n let answer = if left == 1 {\n prefix[right as usize]\n } else {\n Self::sub_mod(prefix[right as usize], prefix[left as usize - 1])\n };\n let ghost old_answers = answers@;\n answers.push(answer);\n idx += 1;\n proof {\n assert(answers@ == old_answers.push(answer));\n assert forall|j: int|\n 0 <= j < answers.len() implies 0 <= #[trigger] answers[j] < modulus()\n && answers[j] as int == query_answer(lefts[j] as int, rights[j] as int, k as int) by {\n if j == idx as int - 1 {\n if left == 1 {\n assert(answer == prefix[right as int]);\n assert(answer as int == query_answer_mod(left as int, right as int, k as int));\n } else {\n assert(answer as int == sub_mod_int(prefix[right as int] as int, prefix[left as int - 1] as int));\n assert(answer as int == query_answer_mod(left as int, right as int, k as int));\n }\n assert(query_answer_mod(left as int, right as int, k as int) == query_answer(left as int, right as int, k as int));\n } else {\n assert(j < idx as int - 1);\n assert(answers[j] == old_answers[j]);\n }\n }\n }\n }\n answers\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n fn add_mod(x: i32, y: i32) -> i32 {\n let sum = x + y;\n if sum >= 1_000_000_007i32 {\n sum - 1_000_000_007i32\n } else {\n sum\n }\n }\n\n fn sub_mod(x: i32, y: i32) -> i32 {\n if x >= y {\n x - y\n } else {\n x + 1_000_000_007i32 - y\n }\n }\n\n pub fn solve_queries(k: i32, lefts: Vec, rights: Vec) -> Vec {\n let max_n = 100_000usize;\n let mut ways = Vec::new();\n ways.push(1);\n let mut prefix = Vec::new();\n prefix.push(0);\n let mut len = 1usize;\n while len <= max_n {\n let prev = ways[len - 1];\n let next = if len < k as usize {\n prev\n } else {\n Self::add_mod(prev, ways[len - k as usize])\n };\n let next_prefix = Self::add_mod(prefix[len - 1], next);\n ways.push(next);\n prefix.push(next_prefix);\n len += 1;\n }\n let mut answers = Vec::new();\n let mut idx = 0usize;\n while idx < lefts.len() {\n let left = lefts[idx];\n let right = rights[idx];\n let answer = if left == 1 {\n prefix[right as usize]\n } else {\n Self::sub_mod(prefix[right as usize], prefix[left as usize - 1])\n };\n answers.push(answer);\n idx += 1;\n }\n answers\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let t: usize = it.next().unwrap().parse().unwrap();\n let k: i32 = it.next().unwrap().parse().unwrap();\n let mut lefts = Vec::with_capacity(t);\n let mut rights = Vec::with_capacity(t);\n let mut i = 0usize;\n while i < t {\n let left: i32 = it.next().unwrap().parse().unwrap();\n let right: i32 = it.next().unwrap().parse().unwrap();\n lefts.push(left);\n rights.push(right);\n i += 1;\n }\n let answers = Solution::solve_queries(k, lefts, rights);\n let mut i = 0usize;\n while i < answers.len() {\n println!(\"{}\", answers[i]);\n i += 1;\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf474D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf474D/tests/mutated_testcases.jsonl"} {"problem": "cf478C", "family": "codeforces", "description": "# Table Decorations\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nYou have $r$ red, $g$ green and $b$ blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number $t$ of tables can be decorated if we know number of balloons of each color?\n\nYour task is to write a program that for given values $r$, $g$ and $b$ will find the maximum number $t$ of tables, that can be decorated in the required manner.\n\n## Input\n\nThe single line contains three integers $r$, $g$ and $b$ ($0\u2009\u2264\u2009r,\u2009g,\u2009b\u2009\u2264\u20092\u00b710^9$) \u2014 the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.\n\n## Output\n\nPrint a single integer $t$ \u2014 the maximum number of tables that can be decorated in the required manner.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5 4 3\n```\n**Output:**\n```\n4\n```\n\n### Example 2\n\n**Input:**\n```\n1 1 1\n```\n**Output:**\n```\n1\n```\n\n### Example 3\n\n**Input:**\n```\n2 3 3\n```\n**Output:**\n```\n2\n```\n\n## Note\n\nIn the first sample you can decorate the tables with the following balloon sets: \"`rgg`\", \"`gbb`\", \"`brr`\", \"`rrg`\", where \"`r`\", \"`g`\" and \"`b`\" represent the red, green and blue balls, respectively.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_decorated_tables(r: i64, g: i64, b: i64) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let r = it.next().expect(\"r\").parse::().expect(\"integer\");\n let g = it.next().expect(\"g\").parse::().expect(\"integer\");\n let b = it.next().expect(\"b\").parse::().expect(\"integer\");\n let ans = Solution::max_decorated_tables(r, g, b);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 1800\ngreedy\nSolved by: 20414 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn total_balloons(r: int, g: int, b: int) -> int {\n r + g + b\n}\n\npub open spec fn max_color(r: int, g: int, b: int) -> int {\n if r >= g {\n if r >= b { r } else { b }\n } else {\n if g >= b { g } else { b }\n }\n}\n\npub open spec fn feasible_tables(r: int, g: int, b: int, t: int) -> bool {\n &&& 0 <= t\n &&& 3 * t <= total_balloons(r, g, b)\n &&& t <= total_balloons(r, g, b) - max_color(r, g, b)\n}\n\nimpl Solution {\n pub fn max_decorated_tables(r: i64, g: i64, b: i64) -> (result: i64)\n requires\n 0 <= r <= 2_000_000_000,\n 0 <= g <= 2_000_000_000,\n 0 <= b <= 2_000_000_000,\n ensures\n feasible_tables(r as int, g as int, b as int, result as int),\n forall|t: int| t > result as int ==> !feasible_tables(r as int, g as int, b as int, t),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_decorated_tables(r: i64, g: i64, b: i64) -> i64 {\n let sum = r + g + b;\n let rg_max = if r >= g { r } else { g };\n let largest = if rg_max >= b { rg_max } else { b };\n let limit_by_total = sum / 3;\n let limit_by_dominant = sum - largest;\n if limit_by_total <= limit_by_dominant {\n limit_by_total\n } else {\n limit_by_dominant\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn total_balloons(r: int, g: int, b: int) -> int {\n r + g + b\n}\n\npub open spec fn max_color(r: int, g: int, b: int) -> int {\n if r >= g {\n if r >= b { r } else { b }\n } else {\n if g >= b { g } else { b }\n }\n}\n\npub open spec fn feasible_tables(r: int, g: int, b: int, t: int) -> bool {\n &&& 0 <= t\n &&& 3 * t <= total_balloons(r, g, b)\n &&& t <= total_balloons(r, g, b) - max_color(r, g, b)\n}\n\nimpl Solution {\n pub fn max_decorated_tables(r: i64, g: i64, b: i64) -> (result: i64)\n requires\n 0 <= r <= 2_000_000_000,\n 0 <= g <= 2_000_000_000,\n 0 <= b <= 2_000_000_000,\n ensures\n feasible_tables(r as int, g as int, b as int, result as int),\n forall|t: int| t > result as int ==> !feasible_tables(r as int, g as int, b as int, t),\n {\n let sum = r + g + b;\n let rg_max = if r >= g { r } else { g };\n let largest = if rg_max >= b { rg_max } else { b };\n let limit_by_total = sum / 3;\n let limit_by_dominant = sum - largest;\n if limit_by_total <= limit_by_dominant {\n limit_by_total\n } else {\n limit_by_dominant\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn total_balloons(r: int, g: int, b: int) -> int {\n r + g + b\n}\n\npub open spec fn max_color(r: int, g: int, b: int) -> int {\n if r >= g {\n if r >= b { r } else { b }\n } else {\n if g >= b { g } else { b }\n }\n}\n\npub open spec fn feasible_tables(r: int, g: int, b: int, t: int) -> bool {\n &&& 0 <= t\n &&& 3 * t <= total_balloons(r, g, b)\n &&& t <= total_balloons(r, g, b) - max_color(r, g, b)\n}\n\nproof fn lemma_max_color_bounds(r: int, g: int, b: int)\n requires\n 0 <= r,\n 0 <= g,\n 0 <= b,\n ensures\n max_color(r, g, b) <= total_balloons(r, g, b),\n 0 <= total_balloons(r, g, b) - max_color(r, g, b),\n{\n if r >= g {\n if r >= b {\n assert(max_color(r, g, b) == r);\n } else {\n assert(max_color(r, g, b) == b);\n }\n } else {\n if g >= b {\n assert(max_color(r, g, b) == g);\n } else {\n assert(max_color(r, g, b) == b);\n }\n }\n}\n\nproof fn lemma_impl_largest_matches_spec(r: int, g: int, b: int)\n ensures\n (if (if r >= g { r } else { g }) >= b { (if r >= g { r } else { g }) } else { b }) == max_color(r, g, b),\n{\n if r >= g {\n if r >= b {\n assert((if r >= g { r } else { g }) == r);\n assert((if (if r >= g { r } else { g }) >= b { (if r >= g { r } else { g }) } else { b }) == r);\n assert(max_color(r, g, b) == r);\n } else {\n assert((if r >= g { r } else { g }) == r);\n assert((if r >= g { r } else { g }) < b);\n assert((if (if r >= g { r } else { g }) >= b { (if r >= g { r } else { g }) } else { b }) == b);\n assert(max_color(r, g, b) == b);\n }\n } else {\n if g >= b {\n assert((if r >= g { r } else { g }) == g);\n assert((if (if r >= g { r } else { g }) >= b { (if r >= g { r } else { g }) } else { b }) == g);\n assert(max_color(r, g, b) == g);\n } else {\n assert((if r >= g { r } else { g }) == g);\n assert((if r >= g { r } else { g }) < b);\n assert((if (if r >= g { r } else { g }) >= b { (if r >= g { r } else { g }) } else { b }) == b);\n assert(max_color(r, g, b) == b);\n }\n }\n}\n\nproof fn lemma_div_by_3_bounds(n: int)\n requires\n 0 <= n,\n ensures\n 3 * (n / 3) <= n,\n n < 3 * ((n / 3) + 1),\n{\n}\n\nimpl Solution {\n pub fn max_decorated_tables(r: i64, g: i64, b: i64) -> (result: i64)\n requires\n 0 <= r <= 2_000_000_000,\n 0 <= g <= 2_000_000_000,\n 0 <= b <= 2_000_000_000,\n ensures\n feasible_tables(r as int, g as int, b as int, result as int),\n forall|t: int| t > result as int ==> !feasible_tables(r as int, g as int, b as int, t),\n {\n let sum = r + g + b;\n let rg_max = if r >= g { r } else { g };\n let largest = if rg_max >= b { rg_max } else { b };\n let limit_by_total = sum / 3;\n let limit_by_dominant = sum - largest;\n proof {\n let total = total_balloons(r as int, g as int, b as int);\n lemma_max_color_bounds(r as int, g as int, b as int);\n lemma_impl_largest_matches_spec(r as int, g as int, b as int);\n lemma_div_by_3_bounds(total);\n assert(sum as int == total);\n assert(rg_max as int == if r as int >= g as int { r as int } else { g as int });\n assert(largest as int == max_color(r as int, g as int, b as int));\n assert(limit_by_total as int == total / 3);\n assert(limit_by_dominant as int == total - max_color(r as int, g as int, b as int));\n }\n if limit_by_total <= limit_by_dominant {\n proof {\n let total = total_balloons(r as int, g as int, b as int);\n assert(0 <= limit_by_total as int);\n assert(3 * (limit_by_total as int) <= total);\n assert((limit_by_total as int) <= total - max_color(r as int, g as int, b as int));\n assert forall|t: int| t > limit_by_total as int implies !feasible_tables(r as int, g as int, b as int, t) by {\n assert(t >= limit_by_total as int + 1);\n assert(total < 3 * (limit_by_total as int + 1));\n assert(3 * t > total);\n }\n }\n limit_by_total\n } else {\n proof {\n let total = total_balloons(r as int, g as int, b as int);\n assert(0 <= limit_by_dominant as int);\n assert((limit_by_dominant as int) < (limit_by_total as int));\n assert(3 * (limit_by_dominant as int) < 3 * (limit_by_total as int));\n assert(3 * (limit_by_total as int) <= total);\n assert(3 * (limit_by_dominant as int) <= total);\n assert((limit_by_dominant as int) <= total - max_color(r as int, g as int, b as int));\n assert forall|t: int| t > limit_by_dominant as int implies !feasible_tables(r as int, g as int, b as int, t) by {\n assert(t > total - max_color(r as int, g as int, b as int));\n }\n }\n limit_by_dominant\n }\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_decorated_tables(r: i64, g: i64, b: i64) -> i64 {\n let sum = r + g + b;\n let rg_max = if r >= g { r } else { g };\n let largest = if rg_max >= b { rg_max } else { b };\n let limit_by_total = sum / 3;\n let limit_by_dominant = sum - largest;\n if limit_by_total <= limit_by_dominant {\n limit_by_total\n } else {\n limit_by_dominant\n }\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let r = it.next().expect(\"r\").parse::().expect(\"integer\");\n let g = it.next().expect(\"g\").parse::().expect(\"integer\");\n let b = it.next().expect(\"b\").parse::().expect(\"integer\");\n let ans = Solution::max_decorated_tables(r, g, b);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf478C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf478C/tests/mutated_testcases.jsonl"} {"problem": "cf486A", "family": "codeforces", "description": "# Calculating Function\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nFor a positive integer $n$ let's define a function $f$:\n\n$f(n)\u2009=\u2009\u2009-\u20091\u2009+\u20092\u2009-\u20093\u2009+\u2009..\u2009+\u2009(\u2009-\u20091)^nn$ \n\nYour task is to calculate $f(n)$ for a given integer $n$.\n\n## Input\n\nThe single line contains the positive integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^15$).\n\n## Output\n\nPrint $f(n)$ in a single line.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n5\n```\n**Output:**\n```\n-3\n```\n\n## Note\n\n$f(4)\u2009=\u2009\u2009-\u20091\u2009+\u20092\u2009-\u20093\u2009+\u20094\u2009=\u20092$\n\n$f(5)\u2009=\u2009\u2009-\u20091\u2009+\u20092\u2009-\u20093\u2009+\u20094\u2009-\u20095\u2009=\u2009\u2009-\u20093$\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn calculating_function(n: i64) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let n: i64 = input.trim().parse().unwrap();\n let result = Solution::calculating_function(n);\n writeln!(out, \"{}\", result).unwrap();\n}\n```\n", "tags": "Rating: 800\nimplementation, math\nSolved by: 153703 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn f_value(n: int) -> int\n decreases n,\n{\n if n <= 0 {\n 0int\n } else if n % 2 == 0 {\n f_value(n - 1) + n\n } else {\n f_value(n - 1) - n\n }\n}\n\nimpl Solution {\n pub fn calculating_function(n: i64) -> (result: i64)\n requires\n 1 <= n <= 1000000000000000,\n ensures\n result as int == f_value(n as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn calculating_function(n: i64) -> i64 {\n if n % 2 == 0 {\n n / 2\n } else {\n -((n + 1) / 2)\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn f_value(n: int) -> int\n decreases n,\n{\n if n <= 0 {\n 0int\n } else if n % 2 == 0 {\n f_value(n - 1) + n\n } else {\n f_value(n - 1) - n\n }\n}\n\nimpl Solution {\n pub fn calculating_function(n: i64) -> (result: i64)\n requires\n 1 <= n <= 1000000000000000,\n ensures\n result as int == f_value(n as int),\n {\n if n % 2 == 0 {\n n / 2\n } else {\n -((n + 1) / 2)\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn f_value(n: int) -> int\n decreases n,\n{\n if n <= 0 {\n 0int\n } else if n % 2 == 0 {\n f_value(n - 1) + n\n } else {\n f_value(n - 1) - n\n }\n}\n\nproof fn lemma_f_value(n: int)\n requires\n n >= 0,\n ensures\n n % 2 == 0 ==> f_value(n) == n / 2,\n n % 2 != 0 ==> f_value(n) == -((n + 1) / 2),\n decreases n,\n{\n if n == 0 {\n } else {\n lemma_f_value(n - 1);\n }\n}\n\nimpl Solution {\n pub fn calculating_function(n: i64) -> (result: i64)\n requires\n 1 <= n <= 1000000000000000,\n ensures\n result as int == f_value(n as int),\n {\n proof {\n lemma_f_value(n as int);\n }\n if n % 2 == 0 {\n n / 2\n } else {\n -((n + 1) / 2)\n }\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn calculating_function(n: i64) -> i64 {\n if n % 2 == 0 {\n n / 2\n } else {\n -((n + 1) / 2)\n }\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let n: i64 = input.trim().parse().unwrap();\n let result = Solution::calculating_function(n);\n writeln!(out, \"{}\", result).unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf486A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf486A/tests/mutated_testcases.jsonl"} {"problem": "cf507B", "family": "codeforces", "description": "# Amr and Pins\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nAmr loves Geometry. One day he came up with a very interesting problem.\n\nAmr has a circle of radius $r$ and center in point $(x,\u2009y)$. He wants the circle center to be in new position $(x',\u2009y')$.\n\nIn one step Amr can put a pin to the border of the circle in a certain point, then rotate the circle around that pin by any angle and finally remove the pin.\n\nHelp Amr to achieve his goal in minimum number of steps.\n\n## Input\n\nInput consists of 5 space-separated integers $r$, $x$, $y$, $x'$ $y'$ ($1\u2009\u2264\u2009r\u2009\u2264\u200910^5$, $\u2009-\u200910^5\u2009\u2264\u2009x,\u2009y,\u2009x',\u2009y'\u2009\u2264\u200910^5$), circle radius, coordinates of original center of the circle and coordinates of destination center of the circle respectively.\n\n## Output\n\nOutput a single integer \u2014 minimum number of steps required to move the center of the circle to the destination point.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2 0 0 0 4\n```\n**Output:**\n```\n1\n```\n\n### Example 2\n\n**Input:**\n```\n1 1 1 4 4\n```\n**Output:**\n```\n3\n```\n\n### Example 3\n\n**Input:**\n```\n4 5 6 5 6\n```\n**Output:**\n```\n0\n```\n\n## Note\n\nIn the first sample test the optimal way is to put a pin at point $(0,\u20092)$ and rotate the circle by $180$ degrees counter-clockwise (or clockwise, no matter).\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_steps_to_target(r: i128, x: i128, y: i128, x2: i128, y2: i128) -> i128 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let r: i128 = it.next().unwrap().parse().unwrap();\n let x: i128 = it.next().unwrap().parse().unwrap();\n let y: i128 = it.next().unwrap().parse().unwrap();\n let x2: i128 = it.next().unwrap().parse().unwrap();\n let y2: i128 = it.next().unwrap().parse().unwrap();\n let ans = Solution::min_steps_to_target(r, x, y, x2, y2);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 1400\ngeometry, math\nSolved by: 26558 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn square(x: int) -> int {\n x * x\n}\n\npub open spec fn distance_sq(x: int, y: int, x2: int, y2: int) -> int {\n square(x2 - x) + square(y2 - y)\n}\n\npub open spec fn jump_sq(r: int) -> int {\n square(2 * r)\n}\n\npub open spec fn can_reach_in_steps(r: int, x: int, y: int, x2: int, y2: int, steps: int) -> bool {\n 0 <= steps && distance_sq(x, y, x2, y2) <= jump_sq(r) * steps * steps\n}\n\nimpl Solution {\n pub fn min_steps_to_target(r: i128, x: i128, y: i128, x2: i128, y2: i128) -> (res: i128)\n requires\n 1 <= r <= 100000,\n -100000 <= x <= 100000,\n -100000 <= y <= 100000,\n -100000 <= x2 <= 100000,\n -100000 <= y2 <= 100000,\n ensures\n 0 <= res <= 200000,\n can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, res as int),\n forall|k: int|\n 0 <= k < res as int ==> !can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, k),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_steps_to_target(r: i128, x: i128, y: i128, x2: i128, y2: i128) -> i128 {\n let dx = x2 - x;\n let dy = y2 - y;\n let dist_sq = dx * dx + dy * dy;\n let two_r = 2 * r;\n let jump_sq_val = two_r * two_r;\n let mut ans = 0i128;\n while ans < 200000 && jump_sq_val * ans * ans < dist_sq {\n ans += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn square(x: int) -> int {\n x * x\n}\n\npub open spec fn distance_sq(x: int, y: int, x2: int, y2: int) -> int {\n square(x2 - x) + square(y2 - y)\n}\n\npub open spec fn jump_sq(r: int) -> int {\n square(2 * r)\n}\n\npub open spec fn can_reach_in_steps(r: int, x: int, y: int, x2: int, y2: int, steps: int) -> bool {\n 0 <= steps && distance_sq(x, y, x2, y2) <= jump_sq(r) * steps * steps\n}\n\nimpl Solution {\n pub fn min_steps_to_target(r: i128, x: i128, y: i128, x2: i128, y2: i128) -> (res: i128)\n requires\n 1 <= r <= 100000,\n -100000 <= x <= 100000,\n -100000 <= y <= 100000,\n -100000 <= x2 <= 100000,\n -100000 <= y2 <= 100000,\n ensures\n 0 <= res <= 200000,\n can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, res as int),\n forall|k: int|\n 0 <= k < res as int ==> !can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, k),\n {\n let dx = x2 - x;\n let dy = y2 - y;\n let dist_sq = dx * dx + dy * dy;\n let two_r = 2 * r;\n let jump_sq_val = two_r * two_r;\n let mut ans = 0i128;\n while ans < 200000 && jump_sq_val * ans * ans < dist_sq {\n ans += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn square(x: int) -> int {\n x * x\n}\n\npub open spec fn distance_sq(x: int, y: int, x2: int, y2: int) -> int {\n square(x2 - x) + square(y2 - y)\n}\n\npub open spec fn jump_sq(r: int) -> int {\n square(2 * r)\n}\n\npub open spec fn can_reach_in_steps(r: int, x: int, y: int, x2: int, y2: int, steps: int) -> bool {\n 0 <= steps && distance_sq(x, y, x2, y2) <= jump_sq(r) * steps * steps\n}\n\nproof fn lemma_max_steps_suffice(r: int, x: int, y: int, x2: int, y2: int)\n requires\n 1 <= r <= 100000,\n -100000 <= x <= 100000,\n -100000 <= y <= 100000,\n -100000 <= x2 <= 100000,\n -100000 <= y2 <= 100000,\n ensures\n can_reach_in_steps(r, x, y, x2, y2, 200000),\n{\n assert(-200000 <= x2 - x) by (nonlinear_arith)\n requires\n x <= 100000,\n -100000 <= x2,\n {\n }\n assert(x2 - x <= 200000) by (nonlinear_arith)\n requires\n -100000 <= x,\n x2 <= 100000,\n {\n }\n assert(-200000 <= y2 - y) by (nonlinear_arith)\n requires\n y <= 100000,\n -100000 <= y2,\n {\n }\n assert(y2 - y <= 200000) by (nonlinear_arith)\n requires\n -100000 <= y,\n y2 <= 100000,\n {\n }\n assert((x2 - x) * (x2 - x) <= 40000000000) by (nonlinear_arith)\n requires\n -200000 <= x2 - x,\n x2 - x <= 200000,\n {\n }\n assert((y2 - y) * (y2 - y) <= 40000000000) by (nonlinear_arith)\n requires\n -200000 <= y2 - y,\n y2 - y <= 200000,\n {\n }\n assert(distance_sq(x, y, x2, y2) <= 80000000000) by (nonlinear_arith)\n requires\n (x2 - x) * (x2 - x) <= 40000000000,\n (y2 - y) * (y2 - y) <= 40000000000,\n {\n }\n assert(80000000000 <= jump_sq(r) * 200000 * 200000) by (nonlinear_arith)\n requires\n 1 <= r,\n {\n }\n}\n\nimpl Solution {\n pub fn min_steps_to_target(r: i128, x: i128, y: i128, x2: i128, y2: i128) -> (res: i128)\n requires\n 1 <= r <= 100000,\n -100000 <= x <= 100000,\n -100000 <= y <= 100000,\n -100000 <= x2 <= 100000,\n -100000 <= y2 <= 100000,\n ensures\n 0 <= res <= 200000,\n can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, res as int),\n forall|k: int|\n 0 <= k < res as int ==> !can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, k),\n {\n proof {\n assert(-200000 <= x2 - x) by (nonlinear_arith)\n requires\n x <= 100000,\n -100000 <= x2,\n {\n }\n assert(x2 - x <= 200000) by (nonlinear_arith)\n requires\n -100000 <= x,\n x2 <= 100000,\n {\n }\n assert(-200000 <= y2 - y) by (nonlinear_arith)\n requires\n y <= 100000,\n -100000 <= y2,\n {\n }\n assert(y2 - y <= 200000) by (nonlinear_arith)\n requires\n -100000 <= y,\n y2 <= 100000,\n {\n }\n assert(0 <= (x2 - x) * (x2 - x) <= 40000000000) by (nonlinear_arith)\n requires\n -200000 <= x2 - x,\n x2 - x <= 200000,\n {\n }\n assert(0 <= (y2 - y) * (y2 - y) <= 40000000000) by (nonlinear_arith)\n requires\n -200000 <= y2 - y,\n y2 - y <= 200000,\n {\n }\n assert(0 <= 2 * r <= 200000) by (nonlinear_arith)\n requires\n 1 <= r <= 100000,\n {\n }\n }\n let dx = x2 - x;\n let dy = y2 - y;\n let dist_sq = dx * dx + dy * dy;\n let two_r = 2 * r;\n proof {\n assert(0 <= two_r <= 200000) by (nonlinear_arith)\n requires\n two_r == 2 * r,\n 1 <= r <= 100000,\n {\n }\n assert(0 <= two_r * two_r < 170141183460469231731687303715884105728) by (nonlinear_arith)\n requires\n 0 <= two_r <= 200000,\n {\n }\n }\n let jump_sq_val = two_r * two_r;\n proof {\n assert(0 <= dist_sq <= 80000000000) by (nonlinear_arith)\n requires\n dist_sq == dx * dx + dy * dy,\n 0 <= (x2 - x) * (x2 - x) <= 40000000000,\n 0 <= (y2 - y) * (y2 - y) <= 40000000000,\n dx == x2 - x,\n dy == y2 - y,\n {\n }\n assert(0 <= jump_sq_val <= 40000000000) by (nonlinear_arith)\n requires\n jump_sq_val == two_r * two_r,\n 0 <= two_r <= 200000,\n {\n }\n assert(dist_sq as int == distance_sq(x as int, y as int, x2 as int, y2 as int)) by (nonlinear_arith)\n requires\n dist_sq == dx * dx + dy * dy,\n dx == x2 - x,\n dy == y2 - y,\n {\n }\n assert(jump_sq_val as int == jump_sq(r as int)) by (nonlinear_arith)\n requires\n jump_sq_val == two_r * two_r,\n two_r == 2 * r,\n {\n }\n }\n let mut ans = 0i128;\n while ans < 200000 && jump_sq_val * ans * ans < dist_sq\n invariant\n 1 <= r <= 100000,\n -100000 <= x <= 100000,\n -100000 <= y <= 100000,\n -100000 <= x2 <= 100000,\n -100000 <= y2 <= 100000,\n 0 <= dist_sq <= 80000000000,\n 0 <= jump_sq_val <= 40000000000,\n dist_sq as int == distance_sq(x as int, y as int, x2 as int, y2 as int),\n jump_sq_val as int == jump_sq(r as int),\n 0 <= ans <= 200000,\n 0 <= jump_sq_val * ans <= 8000000000000000,\n 0 <= jump_sq_val * ans * ans <= 1600000000000000000000,\n forall|k: int|\n 0 <= k < ans as int ==> !can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, k),\n decreases 200000 - ans,\n {\n let ghost prev_ans = ans;\n proof {\n assert(!can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, prev_ans as int)) by {\n assert(0 <= prev_ans as int);\n assert(jump_sq(r as int) * (prev_ans as int) * (prev_ans as int)\n == jump_sq_val as int * (prev_ans as int) * (prev_ans as int)) by (nonlinear_arith)\n requires\n jump_sq_val as int == jump_sq(r as int),\n {\n }\n assert(jump_sq_val as int * (prev_ans as int) * (prev_ans as int) < dist_sq as int) by (nonlinear_arith)\n requires\n jump_sq_val * prev_ans * prev_ans < dist_sq,\n {\n }\n }\n }\n ans += 1;\n proof {\n assert(0 <= jump_sq_val * ans <= 8000000000000000) by (nonlinear_arith)\n requires\n 0 <= jump_sq_val <= 40000000000,\n 0 <= ans <= 200000,\n {\n }\n assert(0 <= jump_sq_val * ans * ans <= 1600000000000000000000) by (nonlinear_arith)\n requires\n 0 <= jump_sq_val <= 40000000000,\n 0 <= ans <= 200000,\n {\n }\n assert forall|k: int|\n 0 <= k < ans as int implies !can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, k) by {\n if k < prev_ans as int {\n } else {\n assert(k == prev_ans as int);\n }\n }\n }\n }\n proof {\n if ans == 200000 {\n lemma_max_steps_suffice(r as int, x as int, y as int, x2 as int, y2 as int);\n } else {\n assert(ans < 200000);\n assert(dist_sq as int <= jump_sq_val as int * (ans as int) * (ans as int)) by (nonlinear_arith)\n requires\n !(jump_sq_val * ans * ans < dist_sq),\n {\n }\n assert(can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, ans as int));\n }\n assert forall|k: int|\n 0 <= k < ans as int implies !can_reach_in_steps(r as int, x as int, y as int, x2 as int, y2 as int, k) by {\n }\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn min_steps_to_target(r: i128, x: i128, y: i128, x2: i128, y2: i128) -> i128 {\n let dx = x2 - x;\n let dy = y2 - y;\n let dist_sq = dx * dx + dy * dy;\n let two_r = 2 * r;\n let jump_sq_val = two_r * two_r;\n let mut ans = 0i128;\n while ans < 200000 && jump_sq_val * ans * ans < dist_sq {\n ans += 1;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let r: i128 = it.next().unwrap().parse().unwrap();\n let x: i128 = it.next().unwrap().parse().unwrap();\n let y: i128 = it.next().unwrap().parse().unwrap();\n let x2: i128 = it.next().unwrap().parse().unwrap();\n let y2: i128 = it.next().unwrap().parse().unwrap();\n let ans = Solution::min_steps_to_target(r, x, y, x2, y2);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf507B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf507B/tests/mutated_testcases.jsonl"} {"problem": "cf509A", "family": "codeforces", "description": "# Maximum in Table\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nAn $n\u2009\u00d7\u2009n$ table $a$ is defined as follows:\n - The first row and the first column contain ones, that is: $a_i,\u20091\u2009=\u2009a_1,\u2009i\u2009=\u20091$ for all $i\u2009=\u20091,\u20092,\u2009...,\u2009n$. \n- Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the formula $a_i,\u2009j\u2009=\u2009a_i\u2009-\u20091,\u2009j\u2009+\u2009a_i,\u2009j\u2009-\u20091$. \n\nThese conditions define all the values in the table.\n\nYou are given a number $n$. You need to determine the maximum value in the $n\u2009\u00d7\u2009n$ table defined by the rules above.\n\n## Input\n\nThe only line of input contains a positive integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910$) \u2014 the number of rows and columns of the table.\n\n## Output\n\nPrint a single line containing a positive integer $m$ \u2014 the maximum value in the table.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n1\n```\n**Output:**\n```\n1\n```\n\n### Example 2\n\n**Input:**\n```\n5\n```\n**Output:**\n```\n70\n```\n\n## Note\n\nIn the second test the rows of the table look as follows: \n${1,\u20091,\u20091,\u20091,\u20091},\u2009$ ${1,\u20092,\u20093,\u20094,\u20095},\u2009$ ${1,\u20093,\u20096,\u200910,\u200915},\u2009$ ${1,\u20094,\u200910,\u200920,\u200935},\u2009$ ${1,\u20095,\u200915,\u200935,\u200970}.$\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_in_table(n: u32) -> u32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let n: u32 = input.trim().parse().unwrap();\n let result = Solution::max_in_table(n);\n writeln!(out, \"{}\", result).unwrap();\n}\n```\n", "tags": "Rating: 800\nbrute force, implementation\nSolved by: 44053 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn table_value(n: int, i: int, j: int) -> int\n decreases i + j,\n{\n if i < 0 || j < 0 || i >= n || j >= n {\n 0int\n } else if i == 0 || j == 0 {\n 1int\n } else {\n table_value(n, i - 1, j) + table_value(n, i, j - 1)\n }\n}\n\nimpl Solution {\n pub fn max_in_table(n: u32) -> (result: u32)\n requires\n 1 <= n <= 10,\n ensures\n result as int == table_value(n as int, (n - 1) as int, (n - 1) as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_in_table(n: u32) -> u32 {\n let nu: usize = n as usize;\n let mut row: Vec = Vec::new();\n let mut k: usize = 0;\n while k < nu {\n row.push(1u32);\n k = k + 1;\n }\n let mut i: usize = 1;\n while i < nu {\n let mut j: usize = 1;\n while j < nu {\n let v: u32 = row[j] + row[j - 1];\n row[j] = v;\n j = j + 1;\n }\n i = i + 1;\n }\n row[nu - 1]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn table_value(n: int, i: int, j: int) -> int\n decreases i + j,\n{\n if i < 0 || j < 0 || i >= n || j >= n {\n 0int\n } else if i == 0 || j == 0 {\n 1int\n } else {\n table_value(n, i - 1, j) + table_value(n, i, j - 1)\n }\n}\n\nimpl Solution {\n pub fn max_in_table(n: u32) -> (result: u32)\n requires\n 1 <= n <= 10,\n ensures\n result as int == table_value(n as int, (n - 1) as int, (n - 1) as int),\n {\n let nu: usize = n as usize;\n let mut row: Vec = Vec::new();\n let mut k: usize = 0;\n while k < nu {\n row.push(1u32);\n k = k + 1;\n }\n let mut i: usize = 1;\n while i < nu {\n let mut j: usize = 1;\n while j < nu {\n let v: u32 = row[j] + row[j - 1];\n row.set(j, v);\n j = j + 1;\n }\n i = i + 1;\n }\n row[nu - 1]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn table_value(n: int, i: int, j: int) -> int\n decreases i + j,\n{\n if i < 0 || j < 0 || i >= n || j >= n {\n 0int\n } else if i == 0 || j == 0 {\n 1int\n } else {\n table_value(n, i - 1, j) + table_value(n, i, j - 1)\n }\n}\n\npub open spec fn pow2(k: int) -> int\n decreases k,\n{\n if k <= 0 { 1int } else { 2 * pow2(k - 1) }\n}\n\nproof fn lemma_pow2_pos(k: int)\n requires\n 0 <= k,\n ensures\n pow2(k) >= 1,\n decreases k,\n{\n if k > 0 {\n lemma_pow2_pos(k - 1);\n }\n}\n\n\nproof fn lemma_pow2_monotonic(k: int, m: int)\n requires\n 0 <= k <= m,\n ensures\n pow2(k) <= pow2(m),\n decreases m - k,\n{\n lemma_pow2_pos(k);\n lemma_pow2_pos(m);\n if k < m {\n lemma_pow2_monotonic(k, m - 1);\n assert(pow2(m) == 2 * pow2(m - 1));\n }\n}\n\nproof fn lemma_pow2_19()\n ensures\n pow2(19) == 524288,\n{\n reveal_with_fuel(pow2, 21);\n}\n\nproof fn lemma_pow2_bound(k: int)\n requires\n 0 <= k <= 19,\n ensures\n pow2(k) <= 524288,\n pow2(k) >= 1,\n{\n lemma_pow2_19();\n lemma_pow2_monotonic(k, 19);\n lemma_pow2_pos(k);\n}\n\nproof fn lemma_table_bound(n: int, i: int, j: int)\n requires\n 1 <= n <= 10,\n 0 <= i < n,\n 0 <= j < n,\n ensures\n 1 <= table_value(n, i, j) <= pow2(i + j + 1),\n decreases i + j,\n{\n if i == 0 || j == 0 {\n assert(table_value(n, i, j) == 1);\n assert(pow2(i + j + 1) >= 1) by {\n lemma_pow2_bound(i + j + 1);\n }\n } else {\n lemma_table_bound(n, i - 1, j);\n lemma_table_bound(n, i, j - 1);\n assert(pow2(i + j + 1) == 2 * pow2(i + j));\n assert(table_value(n, i - 1, j) <= pow2((i - 1) + j + 1));\n assert((i - 1) + j + 1 == i + j);\n assert(table_value(n, i, j - 1) <= pow2(i + (j - 1) + 1));\n assert(i + (j - 1) + 1 == i + j);\n }\n}\n\nimpl Solution {\n pub fn max_in_table(n: u32) -> (result: u32)\n requires\n 1 <= n <= 10,\n ensures\n result as int == table_value(n as int, (n - 1) as int, (n - 1) as int),\n {\n let nu: usize = n as usize;\n let mut row: Vec = Vec::new();\n let mut k: usize = 0;\n while k < nu\n invariant\n k <= nu,\n nu == n as usize,\n 1 <= n <= 10,\n row.len() == k,\n forall|t: int| 0 <= t < k ==> row[t] == 1u32,\n decreases nu - k,\n {\n row.push(1u32);\n k = k + 1;\n }\n proof {\n assert forall|t: int| 0 <= t < n as int implies row[t] as int == table_value(n as int, 0, t) by {\n assert(row[t] == 1u32);\n };\n }\n let mut i: usize = 1;\n while i < nu\n invariant\n 1 <= i <= nu,\n nu == n as usize,\n 1 <= n <= 10,\n row.len() == nu,\n forall|t: int| 0 <= t < nu as int ==> #[trigger] row[t] as int == table_value(n as int, (i - 1) as int, t),\n forall|t: int| 0 <= t < nu as int ==> 1 <= #[trigger] row[t] as int <= 524288,\n decreases nu - i,\n {\n let mut j: usize = 1;\n while j < nu\n invariant\n 1 <= j <= nu,\n 1 <= i < nu,\n nu == n as usize,\n 1 <= n <= 10,\n row.len() == nu,\n forall|t: int| 0 <= t < j as int ==> #[trigger] row[t] as int == table_value(n as int, i as int, t),\n forall|t: int| j as int <= t < nu as int ==> #[trigger] row[t] as int == table_value(n as int, (i - 1) as int, t),\n forall|t: int| 0 <= t < nu as int ==> 1 <= #[trigger] row[t] as int <= 524288,\n decreases nu - j,\n {\n proof {\n assert(row[j as int] as int == table_value(n as int, (i - 1) as int, j as int));\n assert(row[(j - 1) as int] as int == table_value(n as int, i as int, (j - 1) as int));\n assert(table_value(n as int, i as int, j as int) ==\n table_value(n as int, (i - 1) as int, j as int) +\n table_value(n as int, i as int, (j - 1) as int));\n lemma_table_bound(n as int, i as int, j as int);\n lemma_pow2_bound(i as int + j as int + 1);\n }\n let v: u32 = row[j] + row[j - 1];\n row.set(j, v);\n j = j + 1;\n }\n i = i + 1;\n }\n proof {\n assert(i == nu);\n assert(row[(nu - 1) as int] as int == table_value(n as int, (i - 1) as int, (nu - 1) as int));\n }\n row[nu - 1]\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_in_table(n: u32) -> u32 {\n let nu: usize = n as usize;\n let mut row: Vec = Vec::new();\n let mut k: usize = 0;\n while k < nu {\n row.push(1u32);\n k = k + 1;\n }\n let mut i: usize = 1;\n while i < nu {\n let mut j: usize = 1;\n while j < nu {\n let v: u32 = row[j] + row[j - 1];\n row[j] = v;\n j = j + 1;\n }\n i = i + 1;\n }\n row[nu - 1]\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let n: u32 = input.trim().parse().unwrap();\n let result = Solution::max_in_table(n);\n writeln!(out, \"{}\", result).unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf509A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf509A/tests/mutated_testcases.jsonl"} {"problem": "cf519B", "family": "codeforces", "description": "# A and B and Compilation Errors\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\n*A and B are preparing themselves for programming contests.*\n\nB loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.\n\nInitially, the compiler displayed $n$ compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.\n\nHowever, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared \u2014 the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.\n\nCan you help B find out exactly what two errors he corrected?\n\n## Input\n\nThe first line of the input contains integer $n$ ($3\u2009\u2264\u2009n\u2009\u2264\u200910^5$) \u2014 the initial number of compilation errors.\n\nThe second line contains $n$ space-separated integers $a_1,\u2009a_2,\u2009...,\u2009a_n$ ($1\u2009\u2264\u2009a_i\u2009\u2264\u200910^9$) \u2014 the errors the compiler displayed for the first time. \n\nThe third line contains $n\u2009-\u20091$ space-separated integers $b_1,\u2009b_2,\u2009...,\u2009b_n\u2009-\u20091$ \u2014 the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one. \n\nThe fourth line contains $n\u2009-\u20092$ space-separated integers $\u0441_1,\u2009\u0441_2,\u2009...,\u2009\u0441_n\u2009-\u20092$ \u2014 the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one.\n\n## Output\n\nPrint two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n1 5 8 123 7\n123 7 5 1\n5 1 7\n```\n**Output:**\n```\n8\n123\n```\n\n### Example 2\n\n**Input:**\n```\n6\n1 4 3 3 5 7\n3 7 5 4 3\n4 3 7 5\n```\n**Output:**\n```\n1\n3\n```\n\n## Note\n\nIn the first test sample B first corrects the error number 8, then the error number 123.\n\nIn the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn find_compilation_errors(first: Vec, second: Vec, third: Vec) -> (i64, i64) {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut first: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n first.push(it.next().unwrap().parse().unwrap());\n i += 1;\n }\n\n let mut second: Vec = Vec::with_capacity(n - 1);\n let mut j: usize = 0;\n while j + 1 < n {\n second.push(it.next().unwrap().parse().unwrap());\n j += 1;\n }\n\n let mut third: Vec = Vec::with_capacity(n - 2);\n let mut k: usize = 0;\n while k + 2 < n {\n third.push(it.next().unwrap().parse().unwrap());\n k += 1;\n }\n\n let answer = Solution::find_compilation_errors(first, second, third);\n println!(\"{}\", answer.0);\n println!(\"{}\", answer.1);\n}\n```\n", "tags": "Rating: 1100\ndata structures, implementation, sortings\nSolved by: 43400 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn all_errors_valid(s: Seq) -> bool {\n forall|i: int| 0 <= i < s.len() ==> 1 <= #[trigger] s[i] <= 1_000_000_000\n}\n\npub open spec fn count_value(s: Seq, value: i64) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n (if s[0] == value { 1int } else { 0int }) + count_value(s.subrange(1, s.len() as int), value)\n }\n}\n\npub open spec fn single_deletion(from: Seq, to: Seq, deleted: i64) -> bool {\n from.len() == to.len() + 1\n && forall|v: i64| #[trigger] count_value(from, v) == count_value(to, v) + if v == deleted { 1int } else { 0int }\n}\n\nimpl Solution {\n pub fn find_compilation_errors(first: Vec, second: Vec, third: Vec) -> (result: (i64, i64))\n requires\n 3 <= first.len() <= 100_000,\n all_errors_valid(first@),\n all_errors_valid(second@),\n all_errors_valid(third@),\n exists|x: i64| single_deletion(first@, second@, x),\n exists|y: i64| single_deletion(second@, third@, y),\n ensures\n single_deletion(first@, second@, result.0),\n single_deletion(second@, third@, result.1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_compilation_errors(first: Vec, second: Vec, third: Vec) -> (i64, i64) {\n let mut sum_first: i64 = 0;\n let mut i: usize = 0;\n while i < first.len() {\n sum_first = sum_first + first[i];\n i = i + 1;\n }\n\n let mut sum_second: i64 = 0;\n let mut j: usize = 0;\n while j < second.len() {\n sum_second = sum_second + second[j];\n j = j + 1;\n }\n\n let mut sum_third: i64 = 0;\n let mut k: usize = 0;\n while k < third.len() {\n sum_third = sum_third + third[k];\n k = k + 1;\n }\n\n let deleted_first = sum_first - sum_second;\n let deleted_second = sum_second - sum_third;\n (deleted_first, deleted_second)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn all_errors_valid(s: Seq) -> bool {\n forall|i: int| 0 <= i < s.len() ==> 1 <= #[trigger] s[i] <= 1_000_000_000\n}\n\npub open spec fn count_value(s: Seq, value: i64) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n (if s[0] == value { 1int } else { 0int }) + count_value(s.subrange(1, s.len() as int), value)\n }\n}\n\npub open spec fn single_deletion(from: Seq, to: Seq, deleted: i64) -> bool {\n from.len() == to.len() + 1\n && forall|v: i64| #[trigger] count_value(from, v) == count_value(to, v) + if v == deleted { 1int } else { 0int }\n}\n\nimpl Solution {\n pub fn find_compilation_errors(first: Vec, second: Vec, third: Vec) -> (result: (i64, i64))\n requires\n 3 <= first.len() <= 100_000,\n all_errors_valid(first@),\n all_errors_valid(second@),\n all_errors_valid(third@),\n exists|x: i64| single_deletion(first@, second@, x),\n exists|y: i64| single_deletion(second@, third@, y),\n ensures\n single_deletion(first@, second@, result.0),\n single_deletion(second@, third@, result.1),\n {\n let mut sum_first: i64 = 0;\n let mut i: usize = 0;\n while i < first.len() {\n sum_first = sum_first + first[i];\n i = i + 1;\n }\n\n let mut sum_second: i64 = 0;\n let mut j: usize = 0;\n while j < second.len() {\n sum_second = sum_second + second[j];\n j = j + 1;\n }\n\n let mut sum_third: i64 = 0;\n let mut k: usize = 0;\n while k < third.len() {\n sum_third = sum_third + third[k];\n k = k + 1;\n }\n\n let deleted_first = sum_first - sum_second;\n let deleted_second = sum_second - sum_third;\n (deleted_first, deleted_second)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn all_errors_valid(s: Seq) -> bool {\n forall|i: int| 0 <= i < s.len() ==> 1 <= #[trigger] s[i] <= 1_000_000_000\n}\n\npub open spec fn count_value(s: Seq, value: i64) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n (if s[0] == value { 1int } else { 0int }) + count_value(s.subrange(1, s.len() as int), value)\n }\n}\n\npub open spec fn seq_sum(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n s[0] as int + seq_sum(s.subrange(1, s.len() as int))\n }\n}\n\npub open spec fn single_deletion(from: Seq, to: Seq, deleted: i64) -> bool {\n from.len() == to.len() + 1\n && forall|v: i64| #[trigger] count_value(from, v) == count_value(to, v) + if v == deleted { 1int } else { 0int }\n}\n\npub open spec fn same_counts(left: Seq, right: Seq) -> bool {\n left.len() == right.len()\n && forall|v: i64| #[trigger] count_value(left, v) == count_value(right, v)\n}\n\npub open spec fn remove_at(s: Seq, idx: int) -> Seq\n recommends\n 0 <= idx < s.len(),\n decreases s.len(),\n{\n if s.len() == 0 {\n Seq::::empty()\n } else if idx <= 0 {\n s.subrange(1, s.len() as int)\n } else {\n seq![s[0]] + remove_at(s.subrange(1, s.len() as int), idx - 1)\n }\n}\n\nproof fn lemma_count_value_nonnegative(s: Seq, value: i64)\n ensures\n 0 <= count_value(s, value),\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_count_value_nonnegative(s.subrange(1, s.len() as int), value);\n }\n}\n\nproof fn lemma_seq_sum_concat(left: Seq, right: Seq)\n ensures\n seq_sum(left + right) == seq_sum(left) + seq_sum(right),\n decreases left.len(),\n{\n if left.len() == 0 {\n } else {\n let tail = left.subrange(1, left.len() as int);\n lemma_seq_sum_concat(tail, right);\n assert((left + right)[0] == left[0]);\n assert((left + right).subrange(1, (left + right).len() as int) =~= tail + right);\n assert(seq_sum(left + right) == left[0] as int + seq_sum(tail + right));\n assert(seq_sum(left) == left[0] as int + seq_sum(tail));\n assert(seq_sum(tail + right) == seq_sum(tail) + seq_sum(right));\n }\n}\n\nproof fn lemma_seq_sum_push(s: Seq, x: i64)\n ensures\n seq_sum(s.push(x)) == seq_sum(s) + x as int,\n{\n assert(s.push(x) =~= s + seq![x]);\n lemma_seq_sum_concat(s, seq![x]);\n reveal_with_fuel(seq_sum, 2);\n assert(seq![x][0] == x);\n assert(seq![x].subrange(1, seq![x].len() as int) =~= Seq::::empty());\n assert(seq_sum(seq![x]) == x as int);\n}\n\nproof fn lemma_remove_at_len(s: Seq, idx: int)\n requires\n 0 <= idx < s.len(),\n ensures\n remove_at(s, idx).len() + 1 == s.len(),\n decreases s.len(),\n{\n if idx > 0 {\n lemma_remove_at_len(s.subrange(1, s.len() as int), idx - 1);\n }\n}\n\nproof fn lemma_count_implies_index(s: Seq, value: i64)\n requires\n count_value(s, value) > 0,\n ensures\n exists|i: int| 0 <= i < s.len() && s[i] == value,\n decreases s.len(),\n{\n if s.len() == 0 {\n assert(false);\n } else if s[0] == value {\n assert(exists|i: int| 0 <= i < s.len() && s[i] == value);\n } else {\n assert(count_value(s, value) == count_value(s.subrange(1, s.len() as int), value));\n lemma_count_implies_index(s.subrange(1, s.len() as int), value);\n let i = choose|i: int| 0 <= i < s.subrange(1, s.len() as int).len() && s.subrange(1, s.len() as int)[i] == value;\n assert(0 <= i + 1 < s.len());\n assert(s[i + 1] == value);\n }\n}\n\nproof fn lemma_count_value_remove_at(s: Seq, idx: int, value: i64)\n requires\n 0 <= idx < s.len(),\n ensures\n count_value(s, value)\n == count_value(remove_at(s, idx), value) + if s[idx] == value { 1int } else { 0int },\n decreases s.len(),\n{\n if idx == 0 {\n } else {\n let tail = s.subrange(1, s.len() as int);\n lemma_count_value_remove_at(tail, idx - 1, value);\n assert(remove_at(s, idx).subrange(1, remove_at(s, idx).len() as int) =~= remove_at(tail, idx - 1));\n if s[0] == value {\n assert(count_value(s, value) == 1 + count_value(tail, value));\n assert(count_value(remove_at(s, idx), value) == 1 + count_value(remove_at(tail, idx - 1), value));\n } else {\n assert(count_value(s, value) == count_value(tail, value));\n assert(count_value(remove_at(s, idx), value) == count_value(remove_at(tail, idx - 1), value));\n }\n }\n}\n\nproof fn lemma_seq_sum_remove_at(s: Seq, idx: int)\n requires\n 0 <= idx < s.len(),\n ensures\n seq_sum(s) == seq_sum(remove_at(s, idx)) + s[idx] as int,\n decreases s.len(),\n{\n if idx == 0 {\n } else {\n let tail = s.subrange(1, s.len() as int);\n lemma_seq_sum_remove_at(tail, idx - 1);\n assert(remove_at(s, idx).subrange(1, remove_at(s, idx).len() as int) =~= remove_at(tail, idx - 1));\n assert(seq_sum(s) == s[0] as int + seq_sum(tail));\n assert(seq_sum(remove_at(s, idx)) == s[0] as int + seq_sum(remove_at(tail, idx - 1)));\n }\n}\n\nproof fn lemma_same_counts_implies_equal_sum(s: Seq, t: Seq)\n requires\n same_counts(s, t),\n ensures\n seq_sum(s) == seq_sum(t),\n decreases s.len(),\n{\n if s.len() == 0 {\n } else {\n let v = s[0];\n let s_tail = s.subrange(1, s.len() as int);\n assert(count_value(s, v) == count_value(s_tail, v) + 1);\n lemma_count_value_nonnegative(s_tail, v);\n assert(count_value(s, v) > 0);\n assert(count_value(t, v) == count_value(s, v));\n assert(count_value(t, v) > 0);\n lemma_count_implies_index(t, v);\n let j = choose|j: int| 0 <= j < t.len() && t[j] == v;\n let t_removed = remove_at(t, j);\n lemma_remove_at_len(t, j);\n lemma_count_value_remove_at(t, j, v);\n assert(s_tail.len() == t_removed.len());\n assert forall|x: i64| #[trigger] count_value(s_tail, x) == count_value(t_removed, x) by {\n lemma_count_value_remove_at(t, j, x);\n if x == v {\n assert(count_value(s, x) == count_value(s_tail, x) + 1);\n assert(count_value(t, x) == count_value(t_removed, x) + 1);\n } else {\n assert(count_value(s, x) == count_value(s_tail, x));\n assert(count_value(t, x) == count_value(t_removed, x));\n }\n }\n assert(same_counts(s_tail, t_removed));\n lemma_same_counts_implies_equal_sum(s_tail, t_removed);\n lemma_seq_sum_remove_at(t, j);\n assert(seq_sum(s) == v as int + seq_sum(s_tail));\n assert(seq_sum(t) == seq_sum(t_removed) + t[j] as int);\n assert(t[j] == v);\n }\n}\n\nproof fn lemma_single_deletion_sum(from: Seq, to: Seq, deleted: i64)\n requires\n single_deletion(from, to, deleted),\n ensures\n seq_sum(from) == seq_sum(to) + deleted as int,\n{\n assert(count_value(from, deleted) == count_value(to, deleted) + 1);\n lemma_count_value_nonnegative(to, deleted);\n assert(count_value(from, deleted) > 0);\n lemma_count_implies_index(from, deleted);\n let i = choose|i: int| 0 <= i < from.len() && from[i] == deleted;\n let reduced = remove_at(from, i);\n lemma_remove_at_len(from, i);\n assert(reduced.len() == to.len());\n assert forall|x: i64| #[trigger] count_value(reduced, x) == count_value(to, x) by {\n lemma_count_value_remove_at(from, i, x);\n if x == deleted {\n assert(count_value(from, x) == count_value(reduced, x) + 1);\n } else {\n assert(count_value(from, x) == count_value(reduced, x));\n }\n }\n assert(same_counts(reduced, to));\n lemma_same_counts_implies_equal_sum(reduced, to);\n lemma_seq_sum_remove_at(from, i);\n assert(seq_sum(from) == seq_sum(reduced) + from[i] as int);\n assert(from[i] == deleted);\n}\n\nimpl Solution {\n pub fn find_compilation_errors(first: Vec, second: Vec, third: Vec) -> (result: (i64, i64))\n requires\n 3 <= first.len() <= 100_000,\n all_errors_valid(first@),\n all_errors_valid(second@),\n all_errors_valid(third@),\n exists|x: i64| single_deletion(first@, second@, x),\n exists|y: i64| single_deletion(second@, third@, y),\n ensures\n single_deletion(first@, second@, result.0),\n single_deletion(second@, third@, result.1),\n {\n let ghost x0 = choose|x: i64| single_deletion(first@, second@, x);\n let ghost y0 = choose|y: i64| single_deletion(second@, third@, y);\n proof {\n assert(second.len() + 1 == first.len());\n assert(third.len() + 1 == second.len());\n }\n\n let mut sum_first: i64 = 0;\n let mut i: usize = 0;\n while i < first.len()\n invariant\n 3 <= first.len() <= 100_000,\n all_errors_valid(first@),\n 0 <= i <= first.len(),\n sum_first as int == seq_sum(first@.subrange(0, i as int)),\n 0 <= sum_first as int <= i as int * 1_000_000_000,\n decreases first.len() - i,\n {\n proof {\n let prefix = first@.subrange(0, i as int);\n lemma_seq_sum_push(prefix, first[i as int]);\n assert(first@.subrange(0, i as int + 1) =~= prefix.push(first[i as int]));\n assert(1 <= first[i as int] <= 1_000_000_000);\n assert(0 <= sum_first as int + first[i as int] as int <= (i as int + 1) * 1_000_000_000);\n }\n sum_first = sum_first + first[i];\n i = i + 1;\n }\n\n let mut sum_second: i64 = 0;\n let mut j: usize = 0;\n while j < second.len()\n invariant\n second.len() + 1 == first.len(),\n first.len() <= 100_000,\n all_errors_valid(second@),\n 0 <= j <= second.len(),\n sum_second as int == seq_sum(second@.subrange(0, j as int)),\n 0 <= sum_second as int <= j as int * 1_000_000_000,\n decreases second.len() - j,\n {\n proof {\n let prefix = second@.subrange(0, j as int);\n lemma_seq_sum_push(prefix, second[j as int]);\n assert(second@.subrange(0, j as int + 1) =~= prefix.push(second[j as int]));\n assert(1 <= second[j as int] <= 1_000_000_000);\n assert(0 <= sum_second as int + second[j as int] as int <= (j as int + 1) * 1_000_000_000);\n }\n sum_second = sum_second + second[j];\n j = j + 1;\n }\n\n let mut sum_third: i64 = 0;\n let mut k: usize = 0;\n while k < third.len()\n invariant\n third.len() + 1 == second.len(),\n second.len() + 1 == first.len(),\n first.len() <= 100_000,\n all_errors_valid(third@),\n 0 <= k <= third.len(),\n sum_third as int == seq_sum(third@.subrange(0, k as int)),\n 0 <= sum_third as int <= k as int * 1_000_000_000,\n decreases third.len() - k,\n {\n proof {\n let prefix = third@.subrange(0, k as int);\n lemma_seq_sum_push(prefix, third[k as int]);\n assert(third@.subrange(0, k as int + 1) =~= prefix.push(third[k as int]));\n assert(1 <= third[k as int] <= 1_000_000_000);\n assert(0 <= sum_third as int + third[k as int] as int <= (k as int + 1) * 1_000_000_000);\n }\n sum_third = sum_third + third[k];\n k = k + 1;\n }\n\n proof {\n assert(first@.subrange(0, first.len() as int) =~= first@);\n assert(second@.subrange(0, second.len() as int) =~= second@);\n assert(third@.subrange(0, third.len() as int) =~= third@);\n lemma_single_deletion_sum(first@, second@, x0);\n lemma_single_deletion_sum(second@, third@, y0);\n assert(sum_first as int == seq_sum(first@));\n assert(sum_second as int == seq_sum(second@));\n assert(sum_third as int == seq_sum(third@));\n assert(count_value(first@, x0) == count_value(second@, x0) + 1);\n lemma_count_value_nonnegative(second@, x0);\n assert(count_value(first@, x0) > 0);\n lemma_count_implies_index(first@, x0);\n let ix = choose|ix: int| 0 <= ix < first.len() && first[ix] == x0;\n assert(1 <= x0 <= 1_000_000_000);\n assert(count_value(second@, y0) == count_value(third@, y0) + 1);\n lemma_count_value_nonnegative(third@, y0);\n assert(count_value(second@, y0) > 0);\n lemma_count_implies_index(second@, y0);\n let iy = choose|iy: int| 0 <= iy < second.len() && second[iy] == y0;\n assert(1 <= y0 <= 1_000_000_000);\n }\n\n let deleted_first = sum_first - sum_second;\n let deleted_second = sum_second - sum_third;\n\n proof {\n assert(sum_first as int == seq_sum(first@));\n assert(sum_second as int == seq_sum(second@));\n assert(sum_third as int == seq_sum(third@));\n assert(deleted_first as int == seq_sum(first@) - seq_sum(second@));\n assert(deleted_second as int == seq_sum(second@) - seq_sum(third@));\n assert(deleted_first as int == x0 as int);\n assert(deleted_second as int == y0 as int);\n assert(deleted_first == x0);\n assert(deleted_second == y0);\n assert(single_deletion(first@, second@, deleted_first));\n assert(single_deletion(second@, third@, deleted_second));\n }\n\n (deleted_first, deleted_second)\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn find_compilation_errors(first: Vec, second: Vec, third: Vec) -> (i64, i64) {\n let mut sum_first: i64 = 0;\n let mut i: usize = 0;\n while i < first.len() {\n sum_first = sum_first + first[i];\n i = i + 1;\n }\n\n let mut sum_second: i64 = 0;\n let mut j: usize = 0;\n while j < second.len() {\n sum_second = sum_second + second[j];\n j = j + 1;\n }\n\n let mut sum_third: i64 = 0;\n let mut k: usize = 0;\n while k < third.len() {\n sum_third = sum_third + third[k];\n k = k + 1;\n }\n\n let deleted_first = sum_first - sum_second;\n let deleted_second = sum_second - sum_third;\n (deleted_first, deleted_second)\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut first: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n first.push(it.next().unwrap().parse().unwrap());\n i += 1;\n }\n\n let mut second: Vec = Vec::with_capacity(n - 1);\n let mut j: usize = 0;\n while j + 1 < n {\n second.push(it.next().unwrap().parse().unwrap());\n j += 1;\n }\n\n let mut third: Vec = Vec::with_capacity(n - 2);\n let mut k: usize = 0;\n while k + 2 < n {\n third.push(it.next().unwrap().parse().unwrap());\n k += 1;\n }\n\n let answer = Solution::find_compilation_errors(first, second, third);\n println!(\"{}\", answer.0);\n println!(\"{}\", answer.1);\n}\n", "checker": null, "testcases_path": "codeforces/cf519B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf519B/tests/mutated_testcases.jsonl"} {"problem": "cf519C", "family": "codeforces", "description": "# A and B and Team Training\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\n*A and B are preparing themselves for programming contests.*\n\nAn important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.\n\nA believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.\n\nHowever, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.\n\nAs a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.\n\nThere are $n$ experienced members and $m$ newbies on the training session. Can you calculate what maximum number of teams can be formed?\n\n## Input\n\nThe first line contains two integers $n$ and $m$ ($0\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20095\u00b710^5$) \u2014 the number of experienced participants and newbies that are present at the training session.\n\n## Output\n\nPrint the maximum number of teams that can be formed.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2 6\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n4 5\n```\n**Output:**\n```\n3\n```\n\n## Note\n\nLet's represent the experienced players as XP and newbies as NB.\n\nIn the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).\n\nIn the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_training_teams(n: i64, m: i64) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: i64 = it.next().unwrap().parse().unwrap();\n let m: i64 = it.next().unwrap().parse().unwrap();\n let ans = Solution::max_training_teams(n, m);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 1300\ngreedy, implementation, math, number theory\nSolved by: 20642 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn feasible_team_count(n: int, m: int, t: int) -> bool {\n 0 <= t <= n && 0 <= t <= m && t <= (n + m) / 3\n}\n\nimpl Solution {\n pub fn max_training_teams(n: i64, m: i64) -> (result: i64)\n requires\n 0 <= n <= 500000,\n 0 <= m <= 500000,\n ensures\n feasible_team_count(n as int, m as int, result as int),\n forall|t: int| feasible_team_count(n as int, m as int, t) ==> t <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_training_teams(n: i64, m: i64) -> i64 {\n let mut ans = if n < m { n } else { m };\n let by_total = (n + m) / 3;\n if by_total < ans {\n ans = by_total;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn feasible_team_count(n: int, m: int, t: int) -> bool {\n 0 <= t <= n && 0 <= t <= m && t <= (n + m) / 3\n}\n\nimpl Solution {\n pub fn max_training_teams(n: i64, m: i64) -> (result: i64)\n requires\n 0 <= n <= 500000,\n 0 <= m <= 500000,\n ensures\n feasible_team_count(n as int, m as int, result as int),\n forall|t: int| feasible_team_count(n as int, m as int, t) ==> t <= result as int,\n {\n let mut ans = if n < m { n } else { m };\n let by_total = (n + m) / 3;\n if by_total < ans {\n ans = by_total;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn feasible_team_count(n: int, m: int, t: int) -> bool {\n 0 <= t <= n && 0 <= t <= m && t <= (n + m) / 3\n}\n\nimpl Solution {\n pub fn max_training_teams(n: i64, m: i64) -> (result: i64)\n requires\n 0 <= n <= 500000,\n 0 <= m <= 500000,\n ensures\n feasible_team_count(n as int, m as int, result as int),\n forall|t: int| feasible_team_count(n as int, m as int, t) ==> t <= result as int,\n {\n let mut ans = if n < m { n } else { m };\n let by_total = (n + m) / 3;\n if by_total < ans {\n ans = by_total;\n }\n proof {\n assert(0 <= ans as int);\n if n < m {\n assert(ans as int == n as int || ans as int == by_total as int);\n } else {\n assert(ans as int == m as int || ans as int == by_total as int);\n }\n assert(ans as int <= n as int);\n assert(ans as int <= m as int);\n assert(ans as int <= (n as int + m as int) / 3);\n assert(feasible_team_count(n as int, m as int, ans as int));\n assert forall|t: int| feasible_team_count(n as int, m as int, t) implies t <= ans as int by {\n assert(t <= n as int);\n assert(t <= m as int);\n assert(t <= (n as int + m as int) / 3);\n }\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_training_teams(n: i64, m: i64) -> i64 {\n let mut ans = if n < m { n } else { m };\n let by_total = (n + m) / 3;\n if by_total < ans {\n ans = by_total;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: i64 = it.next().unwrap().parse().unwrap();\n let m: i64 = it.next().unwrap().parse().unwrap();\n let ans = Solution::max_training_teams(n, m);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf519C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf519C/tests/mutated_testcases.jsonl"} {"problem": "cf534C", "family": "codeforces", "description": "# Polycarpus' Dice\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nPolycarp has $n$ dice $d_1,\u2009d_2,\u2009...,\u2009d_n$. The $i$-th dice shows numbers from $1$ to $d_i$. Polycarp rolled all the dice and the sum of numbers they showed is $A$. Agrippina didn't see which dice showed what number, she knows only the sum $A$ and the values $d_1,\u2009d_2,\u2009...,\u2009d_n$. However, she finds it enough to make a series of statements of the following type: dice $i$ couldn't show number $r$. For example, if Polycarp had two six-faced dice and the total sum is $A\u2009=\u200911$, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).\n\nFor each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is $A$.\n\n## Input\n\nThe first line contains two integers $n,\u2009A$ ($1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b710^5,\u2009n\u2009\u2264\u2009A\u2009\u2264\u2009s$) \u2014 the number of dice and the sum of shown values where $s\u2009=\u2009d_1\u2009+\u2009d_2\u2009+\u2009...\u2009+\u2009d_n$.\n\nThe second line contains $n$ integers $d_1,\u2009d_2,\u2009...,\u2009d_n$ ($1\u2009\u2264\u2009d_i\u2009\u2264\u200910^6$), where $d_i$ is the maximum value that the $i$-th dice can show.\n\n## Output\n\nPrint $n$ integers $b_1,\u2009b_2,\u2009...,\u2009b_n$, where $b_i$ is the number of values for which it is guaranteed that the $i$-th dice couldn't show them.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2 8\n4 4\n```\n**Output:**\n```\n3 3\n```\n\n### Example 2\n\n**Input:**\n```\n1 3\n5\n```\n**Output:**\n```\n4\n```\n\n### Example 3\n\n**Input:**\n```\n2 3\n2 3\n```\n**Output:**\n```\n0 1\n```\n\n## Note\n\nIn the first sample from the statement $A$ equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.\n\nIn the second sample from the statement $A$ equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.\n\nIn the third sample from the statement $A$ equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn impossible_face_counts(total: i64, maxima: Vec) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let n: usize = it.next().expect(\"n\").parse().expect(\"parse n\");\n let total: i64 = it.next().expect(\"A\").parse().expect(\"parse A\");\n let mut maxima = Vec::new();\n let mut i = 0usize;\n while i < n {\n maxima.push(it.next().expect(\"di\").parse().expect(\"parse di\"));\n i += 1;\n }\n let ans = Solution::impossible_face_counts(total, maxima);\n let mut out = String::new();\n let mut j = 0usize;\n while j < ans.len() {\n if j > 0 {\n out.push(' ');\n }\n out.push_str(&ans[j].to_string());\n j += 1;\n }\n println!(\"{}\", out);\n}\n```\n", "tags": "Rating: 1600\nmath\nSolved by: 5250 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_maxima(maxima: Seq, end: int) -> int\n recommends 0 <= end <= maxima.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n sum_maxima(maxima, end - 1) + maxima[end - 1] as int\n }\n}\n\npub open spec fn other_sum_possible(maxima: Seq, i: int, rem: int) -> bool\n recommends 0 <= i < maxima.len(),\n{\n maxima.len() - 1 <= rem <= sum_maxima(maxima, maxima.len() as int) - maxima[i] as int\n}\n\npub open spec fn die_value_possible(maxima: Seq, total: int, i: int, x: int) -> bool\n recommends 0 <= i < maxima.len(),\n{\n 1 <= x <= maxima[i] as int && other_sum_possible(maxima, i, total - x)\n}\n\npub open spec fn impossible_count_upto(maxima: Seq, total: int, i: int, limit: int) -> int\n recommends 0 <= i < maxima.len(), 0 <= limit <= maxima[i] as int,\n decreases limit,\n{\n if limit <= 0 {\n 0\n } else {\n impossible_count_upto(maxima, total, i, limit - 1)\n + if die_value_possible(maxima, total, i, limit) { 0int } else { 1int }\n }\n}\n\npub open spec fn impossible_count(maxima: Seq, total: int, i: int) -> int\n recommends 0 <= i < maxima.len(),\n{\n impossible_count_upto(maxima, total, i, maxima[i] as int)\n}\n\nimpl Solution {\n pub fn impossible_face_counts(total: i64, maxima: Vec) -> (res: Vec)\n requires\n 1 <= maxima.len() <= 200_000,\n forall|i: int| 0 <= i < maxima.len() ==> 1 <= #[trigger] maxima[i] <= 1_000_000,\n maxima.len() as int <= total as int <= sum_maxima(maxima@, maxima.len() as int),\n ensures\n res.len() == maxima.len(),\n forall|i: int| 0 <= i < res.len() ==> res[i] as int == impossible_count(maxima@, total as int, i),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn impossible_face_counts(total: i64, maxima: Vec) -> Vec {\n let n = maxima.len();\n let mut sum_all = 0i64;\n let mut i = 0usize;\n while i < n {\n sum_all += maxima[i];\n i += 1;\n }\n let mut res = Vec::new();\n i = 0;\n while i < n {\n let mut lo = total - (sum_all - maxima[i]);\n if lo < 1 {\n lo = 1;\n }\n let mut hi = total - (n as i64 - 1);\n if hi > maxima[i] {\n hi = maxima[i];\n }\n let bad = if hi < lo {\n maxima[i]\n } else if hi == maxima[i] {\n lo - 1\n } else {\n lo - 1 + maxima[i] - hi\n };\n res.push(bad);\n i += 1;\n }\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_maxima(maxima: Seq, end: int) -> int\n recommends 0 <= end <= maxima.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n sum_maxima(maxima, end - 1) + maxima[end - 1] as int\n }\n}\n\npub open spec fn other_sum_possible(maxima: Seq, i: int, rem: int) -> bool\n recommends 0 <= i < maxima.len(),\n{\n maxima.len() - 1 <= rem <= sum_maxima(maxima, maxima.len() as int) - maxima[i] as int\n}\n\npub open spec fn die_value_possible(maxima: Seq, total: int, i: int, x: int) -> bool\n recommends 0 <= i < maxima.len(),\n{\n 1 <= x <= maxima[i] as int && other_sum_possible(maxima, i, total - x)\n}\n\npub open spec fn impossible_count_upto(maxima: Seq, total: int, i: int, limit: int) -> int\n recommends 0 <= i < maxima.len(), 0 <= limit <= maxima[i] as int,\n decreases limit,\n{\n if limit <= 0 {\n 0\n } else {\n impossible_count_upto(maxima, total, i, limit - 1)\n + if die_value_possible(maxima, total, i, limit) { 0int } else { 1int }\n }\n}\n\npub open spec fn impossible_count(maxima: Seq, total: int, i: int) -> int\n recommends 0 <= i < maxima.len(),\n{\n impossible_count_upto(maxima, total, i, maxima[i] as int)\n}\n\nimpl Solution {\n pub fn impossible_face_counts(total: i64, maxima: Vec) -> (res: Vec)\n requires\n 1 <= maxima.len() <= 200_000,\n forall|i: int| 0 <= i < maxima.len() ==> 1 <= #[trigger] maxima[i] <= 1_000_000,\n maxima.len() as int <= total as int <= sum_maxima(maxima@, maxima.len() as int),\n ensures\n res.len() == maxima.len(),\n forall|i: int| 0 <= i < res.len() ==> res[i] as int == impossible_count(maxima@, total as int, i),\n {\n let n = maxima.len();\n let mut sum_all = 0i64;\n let mut i = 0usize;\n while i < n\n {\n sum_all += maxima[i];\n i += 1;\n }\n let mut res = Vec::new();\n i = 0;\n while i < n\n {\n let mut lo = total - (sum_all - maxima[i]);\n if lo < 1 {\n lo = 1;\n }\n let mut hi = total - (n as i64 - 1);\n if hi > maxima[i] {\n hi = maxima[i];\n }\n let bad = if hi < lo {\n maxima[i]\n } else if hi == maxima[i] {\n lo - 1\n } else {\n lo - 1 + maxima[i] - hi\n };\n res.push(bad);\n i += 1;\n }\n res\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_maxima(maxima: Seq, end: int) -> int\n recommends 0 <= end <= maxima.len(),\n decreases end,\n{\n if end <= 0 {\n 0\n } else {\n sum_maxima(maxima, end - 1) + maxima[end - 1] as int\n }\n}\n\npub open spec fn other_sum_possible(maxima: Seq, i: int, rem: int) -> bool\n recommends 0 <= i < maxima.len(),\n{\n maxima.len() - 1 <= rem <= sum_maxima(maxima, maxima.len() as int) - maxima[i] as int\n}\n\npub open spec fn die_value_possible(maxima: Seq, total: int, i: int, x: int) -> bool\n recommends 0 <= i < maxima.len(),\n{\n 1 <= x <= maxima[i] as int && other_sum_possible(maxima, i, total - x)\n}\n\npub open spec fn feasible_low(maxima: Seq, total: int, i: int) -> int\n recommends 0 <= i < maxima.len(),\n{\n let raw = total - (sum_maxima(maxima, maxima.len() as int) - maxima[i] as int);\n if raw < 1 { 1 } else { raw }\n}\n\npub open spec fn feasible_high(maxima: Seq, total: int, i: int) -> int\n recommends 0 <= i < maxima.len(),\n{\n let raw = total - (maxima.len() as int - 1);\n if raw > maxima[i] as int { maxima[i] as int } else { raw }\n}\n\npub open spec fn impossible_count_upto(maxima: Seq, total: int, i: int, limit: int) -> int\n recommends 0 <= i < maxima.len(), 0 <= limit <= maxima[i] as int,\n decreases limit,\n{\n if limit <= 0 {\n 0\n } else {\n impossible_count_upto(maxima, total, i, limit - 1)\n + if die_value_possible(maxima, total, i, limit) { 0int } else { 1int }\n }\n}\n\npub open spec fn impossible_count(maxima: Seq, total: int, i: int) -> int\n recommends 0 <= i < maxima.len(),\n{\n impossible_count_upto(maxima, total, i, maxima[i] as int)\n}\n\npub open spec fn outside_interval_prefix(limit: int, lo: int, hi: int) -> int\n recommends 0 <= limit,\n decreases limit,\n{\n if limit <= 0 {\n 0\n } else {\n outside_interval_prefix(limit - 1, lo, hi)\n + if lo <= limit && limit <= hi { 0int } else { 1int }\n }\n}\n\npub open spec fn outside_interval_count(limit: int, lo: int, hi: int) -> int\n recommends 0 <= limit, 1 <= lo, 1 <= hi,\n{\n if hi < lo {\n limit\n } else if limit < lo {\n limit\n } else if limit <= hi {\n lo - 1\n } else {\n lo - 1 + (limit - hi)\n }\n}\n\nproof fn lemma_sum_maxima_step(maxima: Seq, end: int)\n requires\n 0 <= end < maxima.len(),\n ensures\n sum_maxima(maxima, end + 1) == sum_maxima(maxima, end) + maxima[end] as int,\n{\n reveal_with_fuel(sum_maxima, 2);\n}\n\nproof fn lemma_sum_maxima_bound(maxima: Seq, end: int)\n requires\n 0 <= end <= maxima.len(),\n forall|i: int| 0 <= i < maxima.len() ==> 1 <= #[trigger] maxima[i] <= 1_000_000,\n ensures\n 0 <= sum_maxima(maxima, end) <= end * 1_000_000,\n decreases end,\n{\n if end <= 0 {\n reveal_with_fuel(sum_maxima, 1);\n } else {\n lemma_sum_maxima_bound(maxima, end - 1);\n reveal_with_fuel(sum_maxima, 2);\n }\n}\n\nproof fn lemma_feasible_bounds(maxima: Seq, total: int, i: int)\n requires\n 1 <= maxima.len() <= 200_000,\n forall|j: int| 0 <= j < maxima.len() ==> 1 <= #[trigger] maxima[j] <= 1_000_000,\n maxima.len() <= total <= sum_maxima(maxima, maxima.len() as int),\n 0 <= i < maxima.len(),\n ensures\n 1 <= feasible_low(maxima, total, i) <= maxima[i] as int,\n 1 <= feasible_high(maxima, total, i) <= maxima[i] as int,\n{\n let sum_all = sum_maxima(maxima, maxima.len() as int);\n let raw_low = total - (sum_all - maxima[i] as int);\n let raw_high = total - (maxima.len() as int - 1);\n assert(raw_low <= maxima[i] as int) by {\n assert(total <= sum_all);\n }\n assert(1 <= raw_high) by {\n assert(maxima.len() <= total);\n }\n if raw_low < 1 {\n assert(feasible_low(maxima, total, i) == 1);\n } else {\n assert(feasible_low(maxima, total, i) == raw_low);\n }\n if raw_high > maxima[i] as int {\n assert(feasible_high(maxima, total, i) == maxima[i] as int);\n } else {\n assert(feasible_high(maxima, total, i) == raw_high);\n }\n}\n\nproof fn lemma_value_possible_interval(maxima: Seq, total: int, i: int, x: int)\n requires\n 1 <= maxima.len() <= 200_000,\n forall|j: int| 0 <= j < maxima.len() ==> 1 <= #[trigger] maxima[j] <= 1_000_000,\n maxima.len() <= total <= sum_maxima(maxima, maxima.len() as int),\n 0 <= i < maxima.len(),\n 1 <= x <= maxima[i] as int,\n ensures\n die_value_possible(maxima, total, i, x) <==> feasible_low(maxima, total, i) <= x <= feasible_high(maxima, total, i),\n{\n let sum_all = sum_maxima(maxima, maxima.len() as int);\n let raw_low = total - (sum_all - maxima[i] as int);\n let raw_high = total - (maxima.len() as int - 1);\n lemma_feasible_bounds(maxima, total, i);\n assert(die_value_possible(maxima, total, i, x) ==> feasible_low(maxima, total, i) <= x <= feasible_high(maxima, total, i)) by {\n if die_value_possible(maxima, total, i, x) {\n assert(other_sum_possible(maxima, i, total - x));\n assert(raw_low <= x) by {\n assert(total - x <= sum_all - maxima[i] as int);\n assert(raw_low == total - (sum_all - maxima[i] as int));\n }\n assert(x <= raw_high) by {\n assert(maxima.len() as int - 1 <= total - x);\n assert(raw_high == total - (maxima.len() as int - 1));\n }\n if raw_low < 1 {\n assert(feasible_low(maxima, total, i) == 1);\n } else {\n assert(feasible_low(maxima, total, i) == raw_low);\n }\n if raw_high > maxima[i] as int {\n assert(feasible_high(maxima, total, i) == maxima[i] as int);\n } else {\n assert(feasible_high(maxima, total, i) == raw_high);\n }\n }\n }\n assert(feasible_low(maxima, total, i) <= x <= feasible_high(maxima, total, i) ==> die_value_possible(maxima, total, i, x)) by {\n if feasible_low(maxima, total, i) <= x <= feasible_high(maxima, total, i) {\n if raw_low < 1 {\n assert(feasible_low(maxima, total, i) == 1);\n assert(raw_low <= 1);\n assert(raw_low <= x);\n } else {\n assert(feasible_low(maxima, total, i) == raw_low);\n assert(raw_low <= x);\n }\n if raw_high > maxima[i] as int {\n assert(feasible_high(maxima, total, i) == maxima[i] as int);\n assert(x <= maxima[i] as int);\n assert(x <= raw_high);\n } else {\n assert(feasible_high(maxima, total, i) == raw_high);\n assert(x <= raw_high);\n }\n assert(maxima.len() as int - 1 <= total - x) by {\n assert(x <= raw_high);\n assert(raw_high == total - (maxima.len() as int - 1));\n }\n assert(total - x <= sum_all - maxima[i] as int) by {\n assert(raw_low <= x);\n assert(raw_low == total - (sum_all - maxima[i] as int));\n }\n }\n }\n}\n\nproof fn lemma_impossible_count_interval(maxima: Seq, total: int, i: int, limit: int)\n requires\n 1 <= maxima.len() <= 200_000,\n forall|j: int| 0 <= j < maxima.len() ==> 1 <= #[trigger] maxima[j] <= 1_000_000,\n maxima.len() <= total <= sum_maxima(maxima, maxima.len() as int),\n 0 <= i < maxima.len(),\n 0 <= limit <= maxima[i] as int,\n ensures\n impossible_count_upto(maxima, total, i, limit)\n == outside_interval_prefix(limit, feasible_low(maxima, total, i), feasible_high(maxima, total, i)),\n decreases limit,\n{\n if limit <= 0 {\n reveal_with_fuel(impossible_count_upto, 1);\n reveal_with_fuel(outside_interval_prefix, 1);\n } else {\n lemma_impossible_count_interval(maxima, total, i, limit - 1);\n lemma_value_possible_interval(maxima, total, i, limit);\n reveal_with_fuel(impossible_count_upto, 2);\n reveal_with_fuel(outside_interval_prefix, 2);\n }\n}\n\nproof fn lemma_outside_interval_prefix_closed_form(limit: int, lo: int, hi: int)\n requires\n 0 <= limit,\n 1 <= lo,\n 1 <= hi,\n ensures\n outside_interval_prefix(limit, lo, hi) == outside_interval_count(limit, lo, hi),\n decreases limit,\n{\n if limit <= 0 {\n reveal_with_fuel(outside_interval_prefix, 1);\n } else {\n lemma_outside_interval_prefix_closed_form(limit - 1, lo, hi);\n reveal_with_fuel(outside_interval_prefix, 2);\n if hi < lo {\n assert(outside_interval_count(limit - 1, lo, hi) == limit - 1);\n assert(outside_interval_count(limit, lo, hi) == limit);\n } else if limit < lo {\n assert(outside_interval_count(limit - 1, lo, hi) == limit - 1);\n assert(outside_interval_count(limit, lo, hi) == limit);\n } else if limit == lo {\n assert(outside_interval_count(limit - 1, lo, hi) == limit - 1);\n assert(outside_interval_count(limit, lo, hi) == lo - 1);\n } else if limit <= hi {\n assert(outside_interval_count(limit - 1, lo, hi) == lo - 1);\n assert(outside_interval_count(limit, lo, hi) == lo - 1);\n } else if limit == hi + 1 {\n assert(outside_interval_count(limit - 1, lo, hi) == lo - 1);\n assert(outside_interval_count(limit, lo, hi) == lo - 1 + (limit - hi));\n } else {\n assert(hi + 1 < limit);\n assert(outside_interval_count(limit - 1, lo, hi) == lo - 1 + (limit - 1 - hi));\n assert(outside_interval_count(limit, lo, hi) == lo - 1 + (limit - hi));\n }\n }\n}\n\nimpl Solution {\n pub fn impossible_face_counts(total: i64, maxima: Vec) -> (res: Vec)\n requires\n 1 <= maxima.len() <= 200_000,\n forall|i: int| 0 <= i < maxima.len() ==> 1 <= #[trigger] maxima[i] <= 1_000_000,\n maxima.len() as int <= total as int <= sum_maxima(maxima@, maxima.len() as int),\n ensures\n res.len() == maxima.len(),\n forall|i: int| 0 <= i < res.len() ==> res[i] as int == impossible_count(maxima@, total as int, i),\n {\n let n = maxima.len();\n let mut sum_all = 0i64;\n let mut i = 0usize;\n while i < n\n invariant\n 1 <= maxima.len() <= 200_000,\n forall|j: int| 0 <= j < maxima.len() ==> 1 <= #[trigger] maxima[j] <= 1_000_000,\n maxima.len() as int <= total as int <= sum_maxima(maxima@, maxima.len() as int),\n n == maxima.len(),\n 0 <= i <= n,\n sum_all as int == sum_maxima(maxima@, i as int),\n 0 <= sum_all as int <= i as int * 1_000_000,\n decreases n - i,\n {\n proof {\n lemma_sum_maxima_step(maxima@, i as int);\n lemma_sum_maxima_bound(maxima@, i as int + 1);\n assert(sum_maxima(maxima@, i as int + 1) == sum_maxima(maxima@, i as int) + maxima[i as int] as int);\n }\n sum_all += maxima[i];\n i += 1;\n }\n proof {\n assert(sum_all as int == sum_maxima(maxima@, n as int));\n }\n let mut res = Vec::new();\n i = 0;\n while i < n\n invariant\n 1 <= maxima.len() <= 200_000,\n forall|j: int| 0 <= j < maxima.len() ==> 1 <= #[trigger] maxima[j] <= 1_000_000,\n maxima.len() as int <= total as int <= sum_maxima(maxima@, maxima.len() as int),\n n == maxima.len(),\n sum_all as int == sum_maxima(maxima@, n as int),\n i <= n,\n res.len() == i,\n forall|k: int| 0 <= k < i as int ==> res[k] as int == impossible_count(maxima@, total as int, k),\n decreases n - i,\n {\n let mut lo = total - (sum_all - maxima[i]);\n if lo < 1 {\n lo = 1;\n }\n let mut hi = total - (n as i64 - 1);\n if hi > maxima[i] {\n hi = maxima[i];\n }\n let bad = if hi < lo {\n maxima[i]\n } else if hi == maxima[i] {\n lo - 1\n } else {\n lo - 1 + maxima[i] - hi\n };\n proof {\n let lo_spec = feasible_low(maxima@, total as int, i as int);\n let hi_spec = feasible_high(maxima@, total as int, i as int);\n lemma_feasible_bounds(maxima@, total as int, i as int);\n lemma_impossible_count_interval(maxima@, total as int, i as int, maxima[i as int] as int);\n lemma_outside_interval_prefix_closed_form(maxima[i as int] as int, lo_spec, hi_spec);\n assert(lo as int == lo_spec) by {\n let raw_low = total as int - (sum_maxima(maxima@, n as int) - maxima[i as int] as int);\n if raw_low < 1 {\n assert(lo == 1);\n assert(lo_spec == 1);\n } else {\n assert(lo == raw_low as i64);\n assert(lo_spec == raw_low);\n }\n }\n assert(hi as int == hi_spec) by {\n let raw_high = total as int - (n as int - 1);\n if raw_high > maxima[i as int] as int {\n assert(hi == maxima[i as int]);\n assert(hi_spec == maxima[i as int] as int);\n } else {\n assert(hi == raw_high as i64);\n assert(hi_spec == raw_high);\n }\n }\n assert(lo_spec <= maxima[i as int] as int);\n assert(hi_spec <= maxima[i as int] as int);\n assert(bad as int == outside_interval_count(maxima[i as int] as int, lo_spec, hi_spec)) by {\n if hi_spec < lo_spec {\n assert(bad == maxima[i as int]);\n } else if hi_spec == maxima[i as int] as int {\n assert(bad == lo - 1);\n assert(outside_interval_count(maxima[i as int] as int, lo_spec, hi_spec) == lo_spec - 1);\n } else {\n assert(hi_spec < maxima[i as int] as int);\n assert(bad == lo - 1 + maxima[i as int] - hi);\n assert(outside_interval_count(maxima[i as int] as int, lo_spec, hi_spec)\n == lo_spec - 1 + (maxima[i as int] as int - hi_spec));\n }\n }\n assert(impossible_count(maxima@, total as int, i as int)\n == impossible_count_upto(maxima@, total as int, i as int, maxima[i as int] as int));\n assert(impossible_count(maxima@, total as int, i as int)\n == outside_interval_prefix(maxima[i as int] as int, lo_spec, hi_spec));\n assert(impossible_count(maxima@, total as int, i as int)\n == outside_interval_count(maxima[i as int] as int, lo_spec, hi_spec));\n }\n let ghost old_res = res@;\n res.push(bad);\n i += 1;\n proof {\n assert(res@ == old_res.push(bad));\n assert forall|k: int| 0 <= k < i as int implies res[k] as int == impossible_count(maxima@, total as int, k) by {\n if k == i as int - 1 {\n assert(res[k] == bad);\n } else {\n assert(k < i as int - 1);\n assert(res[k] == old_res[k]);\n }\n }\n }\n }\n res\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn impossible_face_counts(total: i64, maxima: Vec) -> Vec {\n let n = maxima.len();\n let mut sum_all = 0i64;\n let mut i = 0usize;\n while i < n {\n sum_all += maxima[i];\n i += 1;\n }\n let mut res = Vec::new();\n i = 0;\n while i < n {\n let mut lo = total - (sum_all - maxima[i]);\n if lo < 1 {\n lo = 1;\n }\n let mut hi = total - (n as i64 - 1);\n if hi > maxima[i] {\n hi = maxima[i];\n }\n let bad = if hi < lo {\n maxima[i]\n } else if hi == maxima[i] {\n lo - 1\n } else {\n lo - 1 + maxima[i] - hi\n };\n res.push(bad);\n i += 1;\n }\n res\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let mut it = input.split_whitespace();\n let n: usize = it.next().expect(\"n\").parse().expect(\"parse n\");\n let total: i64 = it.next().expect(\"A\").parse().expect(\"parse A\");\n let mut maxima = Vec::new();\n let mut i = 0usize;\n while i < n {\n maxima.push(it.next().expect(\"di\").parse().expect(\"parse di\"));\n i += 1;\n }\n let ans = Solution::impossible_face_counts(total, maxima);\n let mut out = String::new();\n let mut j = 0usize;\n while j < ans.len() {\n if j > 0 {\n out.push(' ');\n }\n out.push_str(&ans[j].to_string());\n j += 1;\n }\n println!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf534C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf534C/tests/mutated_testcases.jsonl"} {"problem": "cf545C", "family": "codeforces", "description": "# Woodcutters\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nLittle Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.\n\nThere are $n$ trees located along the road at points with coordinates $x_1,\u2009x_2,\u2009...,\u2009x_n$. Each tree has its height $h_i$. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments $[x_i\u2009-\u2009h_i,\u2009x_i]$ or $[x_i;x_i\u2009+\u2009h_i]$. The tree that is not cut down occupies a single point with coordinate $x_i$. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.\n\n## Input\n\nThe first line contains integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^5$) \u2014 the number of trees.\n\nNext $n$ lines contain pairs of integers $x_i,\u2009h_i$ ($1\u2009\u2264\u2009x_i,\u2009h_i\u2009\u2264\u200910^9$) \u2014 the coordinate and the height of the $\u0456$-th tree.\n\nThe pairs are given in the order of ascending $x_i$. No two trees are located at the point with the same coordinate.\n\n## Output\n\nPrint a single number \u2014 the maximum number of trees that you can cut down by the given rules.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n1 2\n2 1\n5 10\n10 9\n19 1\n```\n**Output:**\n```\n3\n```\n\n### Example 2\n\n**Input:**\n```\n5\n1 2\n2 1\n5 10\n10 9\n20 1\n```\n**Output:**\n```\n4\n```\n\n## Note\n\nIn the first sample you can fell the trees like that: \n - fell the $1$-st tree to the left \u2014 now it occupies segment $[\u2009-\u20091;1]$ \n- fell the $2$-nd tree to the right \u2014 now it occupies segment $[2;3]$ \n- leave the $3$-rd tree \u2014 it occupies point $5$ \n- leave the $4$-th tree \u2014 it occupies point $10$ \n- fell the $5$-th tree to the right \u2014 now it occupies segment $[19;20]$ \n\nIn the second sample you can also fell $4$-th tree to the right, after that it will occupy segment $[10;19]$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_felled_trees(x: Vec, h: Vec) -> i64 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut x: Vec = Vec::with_capacity(n);\n let mut h: Vec = Vec::with_capacity(n);\n let mut j: usize = 0;\n while j < n {\n let xi: i64 = it.next().unwrap().parse().unwrap();\n let hi: i64 = it.next().unwrap().parse().unwrap();\n x.push(xi);\n h.push(hi);\n j = j + 1;\n }\n let answer = Solution::max_felled_trees(x, h);\n println!(\"{}\", answer);\n}\n```\n", "tags": "Rating: 1500\ndp, greedy\nSolved by: 32465 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn x_at(x: Seq, j: int) -> int {\n x[j] as int\n }\n\n pub open spec fn middle_felled(x: Seq, h: Seq, i: int, last: int) -> int\n recommends\n x.len() == h.len(),\n 1 <= x.len(),\n 1 <= i < x.len(),\n decreases x.len() as int - i,\n {\n let n = x.len() as int;\n if i >= n - 1 {\n 0\n } else if Self::x_at(x, i) > last + Self::x_at(h, i) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n } else if Self::x_at(x, i) + Self::x_at(h, i) < Self::x_at(x, i + 1) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i) + Self::x_at(h, i))\n } else {\n Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n }\n }\n\n pub open spec fn woodcutters_answer(x: Seq, h: Seq) -> int {\n if x.len() <= 1 {\n x.len() as int\n } else {\n 2 + Self::middle_felled(x, h, 1, Self::x_at(x, 0))\n }\n }\n\n pub fn max_felled_trees(x: Vec, h: Vec) -> (result: i64)\n requires\n x.len() == h.len(),\n 1 <= x.len() <= 100_000,\n forall |j: int| 0 <= j < x.len() ==> 1 <= #[trigger] x@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < h.len() ==> 1 <= #[trigger] h@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < x.len() - 1 ==> x@[j] < #[trigger] x@[j + 1],\n ensures\n result as int == Self::woodcutters_answer(x@, h@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_felled_trees(x: Vec, h: Vec) -> i64 {\n let n = x.len();\n if n == 1 {\n return 1i64;\n }\n let mut ans: i64 = 2;\n let mut last: i64 = x[0];\n let mut i: usize = 1;\n while i < n - 1 {\n let xi = x[i];\n let hi = h[i];\n if xi > last + hi {\n ans = ans + 1;\n last = xi;\n } else if xi + hi < x[i + 1] {\n ans = ans + 1;\n last = xi + hi;\n } else {\n last = xi;\n }\n i = i + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn x_at(x: Seq, j: int) -> int {\n x[j] as int\n }\n\n pub open spec fn middle_felled(x: Seq, h: Seq, i: int, last: int) -> int\n recommends\n x.len() == h.len(),\n 1 <= x.len(),\n 1 <= i < x.len(),\n decreases x.len() as int - i,\n {\n let n = x.len() as int;\n if i >= n - 1 {\n 0\n } else if Self::x_at(x, i) > last + Self::x_at(h, i) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n } else if Self::x_at(x, i) + Self::x_at(h, i) < Self::x_at(x, i + 1) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i) + Self::x_at(h, i))\n } else {\n Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n }\n }\n\n pub open spec fn woodcutters_answer(x: Seq, h: Seq) -> int {\n if x.len() <= 1 {\n x.len() as int\n } else {\n 2 + Self::middle_felled(x, h, 1, Self::x_at(x, 0))\n }\n }\n\n pub fn max_felled_trees(x: Vec, h: Vec) -> (result: i64)\n requires\n x.len() == h.len(),\n 1 <= x.len() <= 100_000,\n forall |j: int| 0 <= j < x.len() ==> 1 <= #[trigger] x@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < h.len() ==> 1 <= #[trigger] h@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < x.len() - 1 ==> x@[j] < #[trigger] x@[j + 1],\n ensures\n result as int == Self::woodcutters_answer(x@, h@),\n {\n let n = x.len();\n if n == 1 {\n return 1i64;\n }\n let mut ans: i64 = 2;\n let mut last: i64 = x[0];\n let mut i: usize = 1;\n while i < n - 1 {\n let xi = x[i];\n let hi = h[i];\n if xi > last + hi {\n ans = ans + 1;\n last = xi;\n } else if xi + hi < x[i + 1] {\n ans = ans + 1;\n last = xi + hi;\n } else {\n last = xi;\n }\n i = i + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn x_at(x: Seq, j: int) -> int {\n x[j] as int\n }\n\n pub open spec fn middle_felled(x: Seq, h: Seq, i: int, last: int) -> int\n recommends\n x.len() == h.len(),\n 1 <= x.len(),\n 1 <= i < x.len(),\n decreases x.len() as int - i,\n {\n let n = x.len() as int;\n if i >= n - 1 {\n 0\n } else if Self::x_at(x, i) > last + Self::x_at(h, i) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n } else if Self::x_at(x, i) + Self::x_at(h, i) < Self::x_at(x, i + 1) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i) + Self::x_at(h, i))\n } else {\n Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n }\n }\n\n pub open spec fn woodcutters_answer(x: Seq, h: Seq) -> int {\n if x.len() <= 1 {\n x.len() as int\n } else {\n 2 + Self::middle_felled(x, h, 1, Self::x_at(x, 0))\n }\n }\n\n proof fn lemma_middle_felled_base(x: Seq, h: Seq, i: int, last: int)\n requires\n x.len() == h.len(),\n 1 <= x.len(),\n 1 <= i < x.len() as int,\n i >= x.len() as int - 1,\n ensures\n Self::middle_felled(x, h, i, last) == 0,\n {\n reveal_with_fuel(Solution::middle_felled, 2);\n }\n\n proof fn lemma_middle_felled_nonneg(x: Seq, h: Seq, i: int, last: int)\n requires\n x.len() == h.len(),\n 1 <= x.len(),\n 1 <= i < x.len(),\n ensures\n Self::middle_felled(x, h, i, last) >= 0,\n decreases x.len() as int - i,\n {\n reveal_with_fuel(Solution::middle_felled, 2);\n let n = x.len() as int;\n if i >= n - 1 {\n } else if Self::x_at(x, i) > last + Self::x_at(h, i) {\n Self::lemma_middle_felled_nonneg(x, h, i + 1, Self::x_at(x, i));\n } else if Self::x_at(x, i) + Self::x_at(h, i) < Self::x_at(x, i + 1) {\n Self::lemma_middle_felled_nonneg(x, h, i + 1, Self::x_at(x, i) + Self::x_at(h, i));\n } else {\n Self::lemma_middle_felled_nonneg(x, h, i + 1, Self::x_at(x, i));\n }\n }\n\n proof fn lemma_mf_upper(x: Seq, h: Seq, i: int, last: int)\n requires\n x.len() == h.len(),\n 2 <= x.len(),\n 1 <= i < x.len() as int,\n forall |j: int| 0 <= j < x.len() - 1 ==> x[j] < #[trigger] x[j + 1],\n ensures\n Self::middle_felled(x, h, i, last) <= x.len() as int - 1 - i,\n decreases x.len() as int - i,\n {\n reveal_with_fuel(Solution::middle_felled, 2);\n let n = x.len() as int;\n if i >= n - 1 {\n assert(Self::middle_felled(x, h, i, last) == 0);\n assert(x.len() as int - 1 - i == 0);\n } else if Self::x_at(x, i) > last + Self::x_at(h, i) {\n Self::lemma_mf_upper(x, h, i + 1, Self::x_at(x, i));\n assert((x.len() as int - 1 - (i + 1)) == (x.len() as int - 1 - i) - 1);\n } else if Self::x_at(x, i) + Self::x_at(h, i) < Self::x_at(x, i + 1) {\n Self::lemma_mf_upper(x, h, i + 1, Self::x_at(x, i) + Self::x_at(h, i));\n assert((x.len() as int - 1 - (i + 1)) == (x.len() as int - 1 - i) - 1);\n } else {\n Self::lemma_mf_upper(x, h, i + 1, Self::x_at(x, i));\n assert((x.len() as int - 1 - (i + 1)) == (x.len() as int - 1 - i) - 1);\n }\n }\n\n proof fn lemma_wa_le_len(x: Seq, h: Seq)\n requires\n x.len() == h.len(),\n 1 <= x.len(),\n forall |j: int| 0 <= j < x.len() - 1 ==> x[j] < #[trigger] x[j + 1],\n ensures\n Self::woodcutters_answer(x, h) <= x.len() as int,\n {\n if x.len() <= 1 {\n } else {\n Self::lemma_mf_upper(x, h, 1, Self::x_at(x, 0));\n assert(Self::woodcutters_answer(x, h) == 2 + Self::middle_felled(x, h, 1, Self::x_at(x, 0)));\n assert(Self::middle_felled(x, h, 1, Self::x_at(x, 0)) <= x.len() as int - 2);\n assert(2 + (x.len() as int - 2) == x.len() as int);\n }\n }\n\n proof fn lemma_middle_felled_step(\n x: Seq,\n h: Seq,\n i: int,\n last: int,\n )\n requires\n x.len() == h.len(),\n 2 <= x.len(),\n 1 <= i && i + 1 < x.len() as int,\n forall |j: int| 0 <= j < x.len() - 1 ==> x[j] < #[trigger] x[j + 1],\n ensures\n Self::middle_felled(x, h, i, last) == if Self::x_at(x, i) > last + Self::x_at(h, i) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n } else if Self::x_at(x, i) + Self::x_at(h, i) < Self::x_at(x, i + 1) {\n 1 + Self::middle_felled(x, h, i + 1, Self::x_at(x, i) + Self::x_at(h, i))\n } else {\n Self::middle_felled(x, h, i + 1, Self::x_at(x, i))\n },\n {\n reveal_with_fuel(Solution::middle_felled, 2);\n }\n\n pub fn max_felled_trees(x: Vec, h: Vec) -> (result: i64)\n requires\n x.len() == h.len(),\n 1 <= x.len() <= 100_000,\n forall |j: int| 0 <= j < x.len() ==> 1 <= #[trigger] x@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < h.len() ==> 1 <= #[trigger] h@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < x.len() - 1 ==> x@[j] < #[trigger] x@[j + 1],\n ensures\n result as int == Self::woodcutters_answer(x@, h@),\n {\n let n = x.len();\n if n == 1 {\n assert(Self::woodcutters_answer(x@, h@) == 1);\n return 1i64;\n }\n let mut ans: i64 = 2;\n let mut last: i64 = x[0];\n let mut i: usize = 1;\n assert(2 <= n);\n assert(Self::woodcutters_answer(x@, h@) == 2 + Self::middle_felled(x@, h@, 1, Self::x_at(x@, 0)));\n while i < n - 1\n invariant\n x.len() == n,\n h.len() == n,\n 2 <= n && n <= 100_000,\n 1 <= i && i <= n - 1,\n 1 <= (last as int) && (last as int) <= 2_000_000_000,\n forall |j: int| 0 <= j < x.len() ==> 1 <= #[trigger] x@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < h.len() ==> 1 <= #[trigger] h@[j] <= 1_000_000_000,\n forall |j: int| 0 <= j < x.len() - 1 ==> x@[j] < #[trigger] x@[j + 1],\n (ans as int) <= (n as int),\n ans as int + Self::middle_felled(x@, h@, i as int, last as int)\n == 2 + Self::middle_felled(x@, h@, 1, Self::x_at(x@, 0)),\n decreases n - 1 - i,\n {\n let xi = x[i];\n let hi = h[i];\n proof {\n assert(1 <= i as int);\n assert(i as int + 1 < x.len() as int);\n assert(x@[i as int] == xi);\n assert(h@[i as int] == hi);\n assert(1 <= #[trigger] x@[i as int] && #[trigger] x@[i as int] <= 1_000_000_000);\n assert(1 <= #[trigger] h@[i as int] && #[trigger] h@[i as int] <= 1_000_000_000);\n assert((last as int) + (hi as int) <= 3_000_000_000);\n assert((xi as int) + (hi as int) <= 2_000_000_000);\n }\n proof {\n Self::lemma_middle_felled_step(x@, h@, i as int, last as int);\n }\n if xi > last + hi {\n proof {\n assert((last as int) + (hi as int) == (last + hi) as int);\n assert((xi as int) > (last as int) + (hi as int));\n assert(Self::x_at(x@, i as int) == (xi as int));\n assert(Self::x_at(h@, i as int) == (hi as int));\n assert(Self::x_at(x@, i as int) > (last as int) + Self::x_at(h@, i as int));\n assert(Self::middle_felled(x@, h@, i as int, last as int) == 1 + Self::middle_felled(x@, h@, i as int + 1, xi as int));\n }\n assert((ans + 1) as int + Self::middle_felled(x@, h@, i as int + 1, xi as int)\n == ans as int + Self::middle_felled(x@, h@, i as int, last as int));\n ans = ans + 1;\n last = xi;\n } else if xi + hi < x[i + 1] {\n proof {\n assert(!(xi > last + hi));\n assert((xi as int) + (hi as int) == (xi + hi) as int);\n assert(Self::x_at(x@, i as int + 1) == (x[i + 1] as int));\n assert(!(Self::x_at(x@, i as int) > (last as int) + Self::x_at(h@, i as int)));\n assert(Self::x_at(x@, i as int) + Self::x_at(h@, i as int) < Self::x_at(x@, i as int + 1));\n assert(Self::middle_felled(x@, h@, i as int, last as int) == 1 + Self::middle_felled(x@, h@, i as int + 1, Self::x_at(x@, i as int) + Self::x_at(h@, i as int)));\n }\n assert((ans + 1) as int + Self::middle_felled(x@, h@, i as int + 1, Self::x_at(x@, i as int) + Self::x_at(h@, i as int))\n == ans as int + Self::middle_felled(x@, h@, i as int, last as int));\n ans = ans + 1;\n last = xi + hi;\n } else {\n proof {\n assert(!(xi > last + hi));\n assert(!(xi + hi < x[i + 1]));\n assert(!(Self::x_at(x@, i as int) > (last as int) + Self::x_at(h@, i as int)));\n assert(!(Self::x_at(x@, i as int) + Self::x_at(h@, i as int) < Self::x_at(x@, i as int + 1)));\n assert(Self::middle_felled(x@, h@, i as int, last as int) == Self::middle_felled(x@, h@, i as int + 1, xi as int));\n }\n assert(ans as int + Self::middle_felled(x@, h@, i as int + 1, xi as int)\n == ans as int + Self::middle_felled(x@, h@, i as int, last as int));\n last = xi;\n }\n i = i + 1;\n proof {\n assert(i as int <= n as int - 1);\n assert((ans as int) + Self::middle_felled(x@, h@, i as int, last as int) == Self::woodcutters_answer(x@, h@));\n Self::lemma_middle_felled_nonneg(x@, h@, i as int, last as int);\n Self::lemma_wa_le_len(x@, h@);\n assert((ans as int) <= Self::woodcutters_answer(x@, h@));\n assert(Self::woodcutters_answer(x@, h@) <= (n as int));\n assert((ans as int) <= (n as int));\n }\n }\n proof {\n assert(!(i < n - 1));\n assert(i >= n - 1);\n assert(i as int >= n as int - 1);\n Self::lemma_middle_felled_base(x@, h@, i as int, last as int);\n assert(Self::middle_felled(x@, h@, i as int, last as int) == 0);\n assert(ans as int == 2 + Self::middle_felled(x@, h@, 1, Self::x_at(x@, 0)));\n assert(ans as int == Self::woodcutters_answer(x@, h@));\n }\n ans\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_felled_trees(x: Vec, h: Vec) -> i64 {\n let n = x.len();\n if n == 1 {\n return 1i64;\n }\n let mut ans: i64 = 2;\n let mut last: i64 = x[0];\n let mut i: usize = 1;\n while i < n - 1 {\n let xi = x[i];\n let hi = h[i];\n if xi > last + hi {\n ans = ans + 1;\n last = xi;\n } else if xi + hi < x[i + 1] {\n ans = ans + 1;\n last = xi + hi;\n } else {\n last = xi;\n }\n i = i + 1;\n }\n ans\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut x: Vec = Vec::with_capacity(n);\n let mut h: Vec = Vec::with_capacity(n);\n let mut j: usize = 0;\n while j < n {\n let xi: i64 = it.next().unwrap().parse().unwrap();\n let hi: i64 = it.next().unwrap().parse().unwrap();\n x.push(xi);\n h.push(hi);\n j = j + 1;\n }\n let answer = Solution::max_felled_trees(x, h);\n println!(\"{}\", answer);\n}\n", "checker": null, "testcases_path": "codeforces/cf545C/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf545C/tests/mutated_testcases.jsonl"} {"problem": "cf546D", "family": "codeforces", "description": "# Soldier and Number Game\n\nTime limit: 3 seconds | Memory limit: 256 megabytes\n\nTwo soldiers are playing a game. At the beginning first of them chooses a positive integer $n$ and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer $x\u2009>\u20091$, such that $n$ is divisible by $x$ and replacing $n$ with $n\u2009/\u2009x$. When $n$ becomes equal to $1$ and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.\n\nTo make the game more interesting, first soldier chooses $n$ of form $a!\u2009/\u2009b!$ for some positive integer $a$ and $b$ ($a\u2009\u2265\u2009b$). Here by $k!$ we denote the *factorial* of $k$ that is defined as a product of all positive integers not large than $k$.\n\nWhat is the maximum possible score of the second soldier?\n\n## Input\n\nFirst line of input consists of single integer $t$ ($1\u2009\u2264\u2009t\u2009\u2264\u20091\u2009000\u2009000$) denoting number of games soldiers play.\n\nThen follow $t$ lines, each contains pair of integers $a$ and $b$ ($1\u2009\u2264\u2009b\u2009\u2264\u2009a\u2009\u2264\u20095\u2009000\u2009000$) defining the value of $n$ for a game.\n\n## Output\n\nFor each game output a maximum score that the second soldier can get.\n\n## Example\n\n**Input:**\n```\n2\n3 1\n6 3\n```\n**Output:**\n```\n2\n5\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_scores_for_games(queries: Vec<(i32, i32)>) -> Vec {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let nums: Vec = input\n .split_whitespace()\n .map(|s| s.parse::().expect(\"integer\"))\n .collect();\n\n let t = nums[0] as usize;\n let mut queries: Vec<(i32, i32)> = Vec::new();\n let mut i = 1usize;\n while i < 1 + 2 * t {\n queries.push((nums[i], nums[i + 1]));\n i += 2;\n }\n\n let ans = Solution::max_scores_for_games(queries);\n let mut out = String::new();\n let mut i = 0usize;\n while i < ans.len() {\n out.push_str(&format!(\"{}\\n\", ans[i]));\n i += 1;\n }\n print!(\"{}\", out);\n}\n```\n", "tags": "Rating: 1700\nconstructive algorithms, dp, math, number theory\nSolved by: 13612 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn divides(n: int, d: int) -> bool {\n 1 <= d && n % d == 0\n}\n\npub open spec fn has_factor_below(n: int, bound: int) -> bool\n recommends\n 2 <= bound,\n{\n exists|d: int| 2 <= d < bound && #[trigger] divides(n, d)\n}\n\npub open spec fn least_divisor(n: int, d: int) -> bool {\n &&& 2 <= d <= n\n &&& divides(n, d)\n &&& forall|e: int| 2 <= e < d && #[trigger] divides(n, e) ==> false\n}\n\npub open spec fn is_prime(n: int) -> bool {\n 2 <= n && forall|d: int| 2 <= d < n && #[trigger] divides(n, d) ==> false\n}\n\npub open spec fn smallest_divisor_from(n: int, d: int) -> int\n recommends\n 2 <= d <= n,\n decreases n - d,\n{\n if d >= n {\n n\n } else if n % d == 0 {\n d\n } else {\n smallest_divisor_from(n, d + 1)\n }\n}\n\npub open spec fn smallest_prime_divisor(n: int) -> int\n recommends\n 2 <= n,\n{\n smallest_divisor_from(n, 2)\n}\n\npub open spec fn omega(n: int) -> int\n recommends\n 1 <= n,\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n let q = n / smallest_prime_divisor(n);\n if q >= n {\n 1\n } else if q <= 1 {\n 1\n } else {\n 1 + omega(q)\n }\n }\n}\n\npub open spec fn omega_prefix(n: int) -> int\n recommends\n 0 <= n,\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n omega_prefix(n - 1) + omega(n)\n }\n}\n\npub open spec fn omega_interval_sum(lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi,\n decreases hi - lo,\n{\n if hi <= lo {\n 0\n } else {\n omega_interval_sum(lo, hi - 1) + omega(hi)\n }\n}\n\nimpl Solution {\n pub fn max_scores_for_games(queries: Vec<(i32, i32)>) -> (res: Vec)\n requires\n 1 <= queries.len() <= 1_000_000,\n forall|k: int|\n 0 <= k < queries.len() ==> {\n let (a, b) = #[trigger] queries[k];\n 1 <= b <= a <= 5_000_000\n },\n ensures\n res.len() == queries.len(),\n forall|k: int|\n 0 <= k < res.len() ==> #[trigger] res[k] as int == omega_interval_sum(\n queries[k].1 as int,\n queries[k].0 as int,\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_scores_for_games(queries: Vec<(i32, i32)>) -> Vec {\n let mut spf: Vec = Vec::new();\n let mut i: usize = 0;\n while i <= 5_000_000usize {\n spf.push(0usize);\n i = i + 1;\n }\n\n i = 2usize;\n while i <= 5_000_000usize {\n let mut j = i;\n while j <= 5_000_000usize {\n if spf[j] == 0usize {\n spf[j] = i;\n }\n j = j + i;\n }\n i = i + 1;\n }\n\n i = 2usize;\n while i <= 5_000_000usize {\n let d = spf[i];\n let q = i / d;\n if q <= 1 {\n spf[i] = 1usize;\n } else {\n let omega_val = spf[q] + 1;\n spf[i] = omega_val;\n }\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(0u64);\n prefix.push(0u64);\n\n i = 2usize;\n while i <= 5_000_000usize {\n let value = spf[i] as u64;\n let total = prefix[i - 1] + value;\n prefix.push(total);\n i = i + 1;\n }\n\n let mut res = Vec::new();\n i = 0usize;\n while i < queries.len() {\n let (a, b) = queries[i];\n let answer = prefix[a as usize] - prefix[b as usize];\n res.push(answer);\n i = i + 1;\n }\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn divides(n: int, d: int) -> bool {\n 1 <= d && n % d == 0\n}\n\npub open spec fn has_factor_below(n: int, bound: int) -> bool\n recommends\n 2 <= bound,\n{\n exists|d: int| 2 <= d < bound && #[trigger] divides(n, d)\n}\n\npub open spec fn least_divisor(n: int, d: int) -> bool {\n &&& 2 <= d <= n\n &&& divides(n, d)\n &&& forall|e: int| 2 <= e < d && #[trigger] divides(n, e) ==> false\n}\n\npub open spec fn is_prime(n: int) -> bool {\n 2 <= n && forall|d: int| 2 <= d < n && #[trigger] divides(n, d) ==> false\n}\n\npub open spec fn smallest_divisor_from(n: int, d: int) -> int\n recommends\n 2 <= d <= n,\n decreases n - d,\n{\n if d >= n {\n n\n } else if n % d == 0 {\n d\n } else {\n smallest_divisor_from(n, d + 1)\n }\n}\n\npub open spec fn smallest_prime_divisor(n: int) -> int\n recommends\n 2 <= n,\n{\n smallest_divisor_from(n, 2)\n}\n\npub open spec fn omega(n: int) -> int\n recommends\n 1 <= n,\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n let q = n / smallest_prime_divisor(n);\n if q >= n {\n 1\n } else if q <= 1 {\n 1\n } else {\n 1 + omega(q)\n }\n }\n}\n\npub open spec fn omega_prefix(n: int) -> int\n recommends\n 0 <= n,\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n omega_prefix(n - 1) + omega(n)\n }\n}\n\npub open spec fn omega_interval_sum(lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi,\n decreases hi - lo,\n{\n if hi <= lo {\n 0\n } else {\n omega_interval_sum(lo, hi - 1) + omega(hi)\n }\n}\n\nimpl Solution {\n pub fn max_scores_for_games(queries: Vec<(i32, i32)>) -> (res: Vec)\n requires\n 1 <= queries.len() <= 1_000_000,\n forall|k: int|\n 0 <= k < queries.len() ==> {\n let (a, b) = #[trigger] queries[k];\n 1 <= b <= a <= 5_000_000\n },\n ensures\n res.len() == queries.len(),\n forall|k: int|\n 0 <= k < res.len() ==> #[trigger] res[k] as int == omega_interval_sum(\n queries[k].1 as int,\n queries[k].0 as int,\n ),\n {\n let mut spf: Vec = Vec::new();\n let mut i: usize = 0;\n while i <= 5_000_000usize\n {\n spf.push(0usize);\n i = i + 1;\n }\n\n i = 2usize;\n while i <= 5_000_000usize\n {\n let mut j = i;\n while j <= 5_000_000usize\n {\n if spf[j] == 0usize {\n spf.set(j, i);\n }\n j = j + i;\n }\n i = i + 1;\n }\n\n i = 2usize;\n while i <= 5_000_000usize\n {\n let d = spf[i];\n let q = i / d;\n if q <= 1 {\n spf.set(i, 1usize);\n } else {\n let omega_val = spf[q] + 1;\n spf.set(i, omega_val);\n }\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(0u64);\n prefix.push(0u64);\n\n i = 2usize;\n while i <= 5_000_000usize\n {\n let value = spf[i] as u64;\n let total = prefix[i - 1] + value;\n prefix.push(total);\n i = i + 1;\n }\n\n let mut res = Vec::new();\n i = 0usize;\n while i < queries.len()\n {\n let (a, b) = queries[i];\n let answer = prefix[a as usize] - prefix[b as usize];\n res.push(answer);\n i = i + 1;\n }\n res\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn divides(n: int, d: int) -> bool {\n 1 <= d && n % d == 0\n}\n\npub open spec fn has_factor_below(n: int, bound: int) -> bool\n recommends\n 2 <= bound,\n{\n exists|d: int| 2 <= d < bound && #[trigger] divides(n, d)\n}\n\npub open spec fn least_divisor(n: int, d: int) -> bool {\n &&& 2 <= d <= n\n &&& divides(n, d)\n &&& forall|e: int| 2 <= e < d && #[trigger] divides(n, e) ==> false\n}\n\npub open spec fn is_prime(n: int) -> bool {\n 2 <= n && forall|d: int| 2 <= d < n && #[trigger] divides(n, d) ==> false\n}\n\npub open spec fn smallest_divisor_from(n: int, d: int) -> int\n recommends\n 2 <= d <= n,\n decreases n - d,\n{\n if d >= n {\n n\n } else if n % d == 0 {\n d\n } else {\n smallest_divisor_from(n, d + 1)\n }\n}\n\npub open spec fn smallest_prime_divisor(n: int) -> int\n recommends\n 2 <= n,\n{\n smallest_divisor_from(n, 2)\n}\n\npub open spec fn omega(n: int) -> int\n recommends\n 1 <= n,\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n let q = n / smallest_prime_divisor(n);\n if q >= n {\n 1\n } else if q <= 1 {\n 1\n } else {\n 1 + omega(q)\n }\n }\n}\n\npub open spec fn omega_prefix(n: int) -> int\n recommends\n 0 <= n,\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n omega_prefix(n - 1) + omega(n)\n }\n}\n\npub open spec fn omega_interval_sum(lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi,\n decreases hi - lo,\n{\n if hi <= lo {\n 0\n } else {\n omega_interval_sum(lo, hi - 1) + omega(hi)\n }\n}\n\nproof fn lemma_smallest_divisor_from_properties(n: int, d: int)\n requires\n 2 <= d <= n,\n ensures\n d <= smallest_divisor_from(n, d) <= n,\n divides(n, smallest_divisor_from(n, d)),\n forall|e: int| d <= e < smallest_divisor_from(n, d) && #[trigger] divides(n, e) ==> false,\n decreases n - d,\n{\n if d >= n {\n assert(n % n == 0) by (nonlinear_arith)\n requires 2 <= n,\n {\n }\n assert(divides(n, n));\n } else if n % d == 0 {\n } else {\n lemma_smallest_divisor_from_properties(n, d + 1);\n }\n}\n\nproof fn lemma_divisibility_transitive(n: int, d: int, e: int)\n requires\n 1 <= e <= d <= n,\n n % d == 0,\n d % e == 0,\n ensures\n n % e == 0,\n{\n let q1 = n / d;\n let q2 = d / e;\n assert(n == q1 * d) by (nonlinear_arith)\n requires q1 == n / d, n % d == 0, d >= 1,\n {\n }\n assert(d == q2 * e) by (nonlinear_arith)\n requires q2 == d / e, d % e == 0, e >= 1,\n {\n }\n assert(n == (q1 * q2) * e) by (nonlinear_arith)\n requires n == q1 * d, d == q2 * e,\n {\n }\n assert(n % e == 0) by (nonlinear_arith)\n requires n == (q1 * q2) * e, e >= 1,\n {\n }\n}\n\nproof fn lemma_smallest_prime_divisor_properties(n: int)\n requires\n 2 <= n,\n ensures\n least_divisor(n, smallest_prime_divisor(n)),\n is_prime(smallest_prime_divisor(n)),\n{\n lemma_smallest_divisor_from_properties(n, 2);\n let s = smallest_prime_divisor(n);\n assert(least_divisor(n, s));\n assert forall|e: int| 2 <= e < s && #[trigger] divides(s, e) implies false by {\n if divides(s, e) {\n lemma_divisibility_transitive(n, s, e);\n assert(divides(n, e));\n assert(false);\n }\n }\n}\n\nproof fn lemma_least_divisor_is_smallest(n: int, d: int)\n requires\n 2 <= n,\n least_divisor(n, d),\n ensures\n d == smallest_prime_divisor(n),\n{\n lemma_smallest_divisor_from_properties(n, 2);\n let s = smallest_prime_divisor(n);\n assert(n % s == 0);\n if s < d {\n assert(n % s != 0);\n assert(false);\n }\n if d < s {\n assert(n % d != 0);\n assert(false);\n }\n}\n\nproof fn lemma_omega_spd_bound(n: int)\n requires\n 2 <= n,\n ensures\n n / smallest_prime_divisor(n) < n,\n 1 <= n / smallest_prime_divisor(n),\n{\n lemma_smallest_prime_divisor_properties(n);\n let d = smallest_prime_divisor(n);\n assert(2 <= d <= n);\n assert(n % d == 0);\n let q = n / d;\n assert(1 <= q) by (nonlinear_arith)\n requires 2 <= d, d <= n, q == n / d, n % d == 0,\n {\n }\n assert(q < n) by (nonlinear_arith)\n requires 2 <= d, d <= n, q == n / d, n % d == 0,\n {\n }\n}\n\nproof fn lemma_omega_bound(n: int)\n requires\n 1 <= n,\n ensures\n 0 <= omega(n) <= n,\n decreases n,\n{\n if n > 1 {\n lemma_smallest_prime_divisor_properties(n);\n lemma_omega_spd_bound(n);\n let d = smallest_prime_divisor(n);\n let q = n / d;\n assert(1 <= q < n) by (nonlinear_arith)\n requires\n 2 <= d <= n,\n q == n / d,\n n % d == 0,\n {\n }\n lemma_omega_bound(q);\n assert(q < n);\n assert(omega(n) == 1 + omega(q));\n assert(1 + omega(q) <= n) by (nonlinear_arith)\n requires\n 1 <= q < n,\n omega(q) <= q,\n {\n }\n }\n}\n\nproof fn lemma_omega_prefix_bound(n: int)\n requires\n 0 <= n,\n ensures\n 0 <= omega_prefix(n) <= n * n,\n decreases n,\n{\n if n > 1 {\n lemma_omega_prefix_bound(n - 1);\n lemma_omega_bound(n);\n assert(omega_prefix(n) == omega_prefix(n - 1) + omega(n));\n assert(omega_prefix(n) <= n * n) by (nonlinear_arith)\n requires\n 0 <= omega_prefix(n - 1) <= (n - 1) * (n - 1),\n 0 <= omega(n) <= n,\n {\n }\n }\n}\n\nproof fn lemma_omega_prefix_difference(lo: int, hi: int)\n requires\n 0 <= lo <= hi,\n ensures\n omega_prefix(hi) - omega_prefix(lo) == omega_interval_sum(lo, hi),\n decreases hi - lo,\n{\n if hi > lo {\n lemma_omega_prefix_difference(lo, hi - 1);\n }\n}\n\nproof fn lemma_omega_interval_nonnegative(lo: int, hi: int)\n requires\n 0 <= lo <= hi,\n ensures\n 0 <= omega_interval_sum(lo, hi),\n decreases hi - lo,\n{\n if hi > lo {\n lemma_omega_interval_nonnegative(lo, hi - 1);\n lemma_omega_bound(hi);\n }\n}\n\nimpl Solution {\n pub fn max_scores_for_games(queries: Vec<(i32, i32)>) -> (res: Vec)\n requires\n 1 <= queries.len() <= 1_000_000,\n forall|k: int|\n 0 <= k < queries.len() ==> {\n let (a, b) = #[trigger] queries[k];\n 1 <= b <= a <= 5_000_000\n },\n ensures\n res.len() == queries.len(),\n forall|k: int|\n 0 <= k < res.len() ==> #[trigger] res[k] as int == omega_interval_sum(\n queries[k].1 as int,\n queries[k].0 as int,\n ),\n {\n let mut spf: Vec = Vec::new();\n let mut i: usize = 0;\n while i <= 5_000_000usize\n invariant\n i <= 5_000_001usize,\n spf.len() == i,\n forall|j: int| 0 <= j < i as int ==> #[trigger] spf[j] == 0usize,\n decreases 5_000_001usize - i,\n {\n spf.push(0usize);\n i = i + 1;\n }\n\n i = 2usize;\n proof {\n assert forall|m: int| 2 <= m <= 5_000_000 && spf[m] != 0usize implies #[trigger] least_divisor(m, spf[m] as int) by {\n assert(0 <= m && m < 5_000_001);\n assert(spf[m] == 0usize);\n }\n }\n while i <= 5_000_000usize\n invariant\n spf.len() == 5_000_001usize,\n 2usize <= i <= 5_000_001usize,\n forall|m: int| 2 <= m <= 5_000_000 && spf[m] != 0usize ==> #[trigger] least_divisor(m, spf[m] as int),\n forall|m: int| 2 <= m <= 5_000_000 && #[trigger] has_factor_below(m, i as int) ==> spf[m] != 0usize,\n decreases 5_000_001usize - i,\n {\n let mut j = i;\n while j <= 5_000_000usize\n invariant\n spf.len() == 5_000_001usize,\n 2usize <= i <= 5_000_000usize,\n i <= j <= 5_000_000usize + i,\n j as int % (i as int) == 0,\n forall|m: int| 2 <= m <= 5_000_000 && spf[m] != 0usize ==> #[trigger] least_divisor(m, spf[m] as int),\n forall|m: int| 2 <= m <= 5_000_000 && #[trigger] has_factor_below(m, i as int) ==> spf[m] != 0usize,\n forall|m: int| #![trigger spf[m]] i as int <= m < j as int && m % (i as int) == 0 ==> spf[m] != 0usize && least_divisor(m, spf[m] as int),\n decreases 5_000_000usize + i - j,\n {\n let ghost cur = j;\n let ghost old_spf = spf@;\n if spf[j] == 0usize {\n spf.set(j, i);\n proof {\n assert(old_spf[cur as int] == 0usize);\n assert(cur as int % (i as int) == 0);\n assert(divides(cur as int, i as int));\n assert(least_divisor(cur as int, i as int)) by {\n assert(2 <= i as int);\n assert(i as int <= cur as int);\n assert forall|e: int| 2 <= e < i as int && #[trigger] divides(cur as int, e) implies false by {\n if divides(cur as int, e) {\n assert(has_factor_below(cur as int, i as int));\n assert(old_spf[cur as int] != 0usize);\n assert(false);\n }\n }\n }\n assert(spf@ == old_spf.update(cur as int, i));\n assert forall|m: int| 2 <= m <= 5_000_000 && spf[m] != 0usize implies #[trigger] least_divisor(m, spf[m] as int) by {\n if m == cur as int {\n } else {\n assert(spf[m] == old_spf[m]);\n }\n }\n assert forall|m: int| #![trigger spf[m]] i as int <= m < cur as int && m % (i as int) == 0 implies spf[m] != 0usize && least_divisor(m, spf[m] as int) by {\n assert(m != cur as int);\n assert(spf[m] == old_spf[m]);\n assert(old_spf[m] != 0usize);\n assert(least_divisor(m, old_spf[m] as int));\n }\n }\n }\n let ghost old_j = j;\n j = j + i;\n proof {\n let ghost ji = j as int;\n let ghost oji = old_j as int;\n let ghost ii = i as int;\n assert(ji == oji + ii);\n assert(ji % ii == 0) by (nonlinear_arith)\n requires oji % ii == 0, ji == oji + ii, ii >= 2,\n {\n }\n assert forall|m: int| #![trigger spf[m]] i as int <= m < j as int && m % (i as int) == 0 implies spf[m] != 0usize && least_divisor(m, spf[m] as int) by {\n if m < old_j as int {\n } else if m == cur as int {\n assert(2 <= m && m <= 5_000_000);\n assert(spf[m] != 0usize);\n } else {\n let ghost ci = cur as int;\n let ghost ii = i as int;\n assert(ci % ii == 0);\n assert(m % ii == 0);\n assert(false) by (nonlinear_arith)\n requires\n ci < m,\n m < ci + ii,\n m % ii == 0,\n ci % ii == 0,\n ii >= 2,\n {\n }\n }\n }\n }\n }\n proof {\n assert forall|m: int| 2 <= m <= 5_000_000 && #[trigger] has_factor_below(m, i as int + 1) implies spf[m] != 0usize by {\n if has_factor_below(m, i as int) {\n } else {\n let d = choose|d: int| 2 <= d < i as int + 1 && divides(m, d);\n assert(2 <= d && d < i as int + 1);\n assert(!(d < i as int));\n assert(d == i as int);\n assert(divides(m, i as int));\n assert(m % (i as int) == 0);\n let ghost ii = i as int;\n assert(ii <= m) by (nonlinear_arith)\n requires 2 <= ii, 2 <= m, m % ii == 0,\n {\n }\n assert(i as int <= m);\n assert(m < j as int);\n let ghost _trigger = spf[m];\n assert(spf[m] != 0usize);\n }\n }\n }\n i = i + 1;\n }\n\n proof {\n assert forall|m: int| 2 <= m <= 5_000_000 implies spf[m] != 0usize by {\n assert(m % m == 0) by (nonlinear_arith)\n requires m >= 2,\n {\n }\n assert(divides(m, m));\n assert(2 <= m && m < 5_000_001);\n assert(has_factor_below(m, 5_000_001int));\n }\n }\n\n let ghost orig_spf = spf@;\n i = 2usize;\n while i <= 5_000_000usize\n invariant\n spf.len() == 5_000_001usize,\n 2usize <= i <= 5_000_001usize,\n forall|m: int| 2 <= m <= 5_000_000 ==> orig_spf[m] != 0usize && least_divisor(m, orig_spf[m] as int),\n forall|k: int| 2 <= k < i as int ==> #[trigger] spf[k] as int == omega(k) && spf[k] as int <= k,\n forall|m: int| i as int <= m <= 5_000_000 ==> #[trigger] spf[m] == orig_spf[m],\n decreases 5_000_001usize - i,\n {\n proof {\n assert(spf[i as int] == orig_spf[i as int]);\n assert(least_divisor(i as int, orig_spf[i as int] as int));\n lemma_least_divisor_is_smallest(i as int, orig_spf[i as int] as int);\n lemma_smallest_prime_divisor_properties(i as int);\n lemma_omega_spd_bound(i as int);\n lemma_omega_bound(i as int);\n }\n let d = spf[i];\n let q = i / d;\n proof {\n let ghost ii = i as int;\n let ghost di = d as int;\n let ghost qi = q as int;\n assert(di == smallest_prime_divisor(ii));\n assert(ii % di == 0);\n assert(1 <= qi && qi < ii) by (nonlinear_arith)\n requires 2 <= di, di <= ii, qi == ii / di, ii % di == 0,\n {\n }\n }\n if q <= 1 {\n spf.set(i, 1usize);\n proof {\n let ghost ii = i as int;\n let ghost qi = q as int;\n assert(qi == ii / smallest_prime_divisor(ii));\n assert(qi < ii);\n assert(qi <= 1);\n assert(omega(ii) == 1);\n assert forall|k: int| 2 <= k < ii + 1 implies #[trigger] spf[k] as int == omega(k) && spf[k] as int <= k by {\n if k < ii {\n } else {\n }\n }\n assert forall|m: int| ii + 1 <= m <= 5_000_000 implies #[trigger] spf[m] == orig_spf[m] by {\n }\n }\n } else {\n proof {\n let ghost qi = q as int;\n let ghost ii = i as int;\n assert(2 <= qi && qi < ii);\n assert(spf[qi] as int == omega(qi));\n assert(spf[qi] as int <= qi);\n }\n let omega_val = spf[q] + 1;\n spf.set(i, omega_val);\n proof {\n let ghost ii = i as int;\n let ghost qi = q as int;\n assert(qi == ii / smallest_prime_divisor(ii));\n assert(qi < ii);\n assert(qi > 1);\n assert(omega(ii) == 1 + omega(qi));\n assert(omega_val as int == omega(ii));\n assert(omega(ii) <= ii);\n assert forall|k: int| 2 <= k < ii + 1 implies #[trigger] spf[k] as int == omega(k) && spf[k] as int <= k by {\n if k < ii {\n } else {\n }\n }\n assert forall|m: int| ii + 1 <= m <= 5_000_000 implies #[trigger] spf[m] == orig_spf[m] by {\n }\n }\n }\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(0u64);\n prefix.push(0u64);\n\n i = 2usize;\n while i <= 5_000_000usize\n invariant\n spf.len() == 5_000_001usize,\n prefix.len() == i,\n 2usize <= i <= 5_000_001usize,\n forall|k: int| 2 <= k <= 5_000_000 ==> #[trigger] spf[k] as int == omega(k) && spf[k] as int <= k,\n prefix[0] == 0u64,\n prefix[1] == 0u64,\n forall|k: int| 0 <= k < i as int ==> #[trigger] prefix[k] as int == omega_prefix(k) && prefix[k] as int <= k * k,\n decreases 5_000_001usize - i,\n {\n let ghost ii = i as int;\n let ghost prefix_at_im1: int = prefix[i - 1] as int;\n proof {\n assert(prefix_at_im1 <= (ii - 1) * (ii - 1));\n lemma_omega_bound(ii);\n lemma_omega_prefix_bound(ii - 1);\n }\n let value = spf[i] as u64;\n proof {\n let ghost vi = value as int;\n assert(vi == omega(ii));\n assert(vi <= ii);\n assert(prefix_at_im1 + vi <= 25_000_005_000_000) by (nonlinear_arith)\n requires\n 0 <= prefix_at_im1,\n prefix_at_im1 <= (ii - 1) * (ii - 1),\n 0 <= vi,\n vi <= ii,\n 2 <= ii,\n ii <= 5_000_000,\n {\n }\n }\n let total = prefix[i - 1] + value;\n let ghost old_prefix = prefix@;\n prefix.push(total);\n proof {\n assert(total as int == prefix_at_im1 + value as int);\n assert(total as int == omega_prefix(ii)) by {\n assert(prefix_at_im1 == omega_prefix(ii - 1));\n assert(omega_prefix(ii) == omega_prefix(ii - 1) + omega(ii));\n }\n let ghost vi = value as int;\n let ghost ti = total as int;\n assert(ti <= ii * ii) by (nonlinear_arith)\n requires\n 0 <= prefix_at_im1,\n prefix_at_im1 <= (ii - 1) * (ii - 1),\n 0 <= vi,\n vi <= ii,\n 2 <= ii,\n ti == prefix_at_im1 + vi,\n {\n }\n assert(prefix@ == old_prefix.push(total));\n assert forall|k: int| 0 <= k < ii + 1 implies #[trigger] prefix[k] as int == omega_prefix(k) && prefix[k] as int <= k * k by {\n if k == ii {\n assert(prefix[k] == total);\n } else {\n assert(k < ii);\n assert(prefix[k] == old_prefix[k]);\n }\n }\n }\n i = i + 1;\n }\n\n let mut res = Vec::new();\n i = 0usize;\n while i < queries.len()\n invariant\n prefix.len() == 5_000_001usize,\n i <= queries.len(),\n res.len() == i,\n forall|k: int|\n 0 <= k < queries.len() ==> {\n let (a, b) = #[trigger] queries[k];\n 1 <= b <= a <= 5_000_000\n },\n forall|k: int| 0 <= k <= 5_000_000 ==> #[trigger] prefix[k] as int == omega_prefix(k),\n forall|k: int|\n 0 <= k < i as int ==> #[trigger] res[k] as int == omega_interval_sum(\n queries[k].1 as int,\n queries[k].0 as int,\n ),\n decreases queries.len() - i,\n {\n let ghost cur = i;\n let (a, b) = queries[i];\n proof {\n lemma_omega_prefix_difference(b as int, a as int);\n lemma_omega_interval_nonnegative(b as int, a as int);\n assert(prefix[a as int] as int - prefix[b as int] as int == omega_interval_sum(b as int, a as int));\n assert(prefix[b as int] as int <= prefix[a as int] as int);\n }\n let answer = prefix[a as usize] - prefix[b as usize];\n proof {\n assert(answer as int == omega_interval_sum(b as int, a as int)) by {\n assert(prefix[a as int] as int == omega_prefix(a as int));\n assert(prefix[b as int] as int == omega_prefix(b as int));\n }\n }\n let ghost old_res = res@;\n res.push(answer);\n i = i + 1;\n proof {\n assert(res@ == old_res.push(answer));\n assert forall|k: int|\n 0 <= k < i as int implies #[trigger] res[k] as int == omega_interval_sum(\n queries[k].1 as int,\n queries[k].0 as int,\n ) by {\n if k == cur as int {\n assert(res[k] == answer);\n } else {\n assert(k < cur as int);\n assert(res[k] == old_res[k]);\n }\n }\n }\n }\n res\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_scores_for_games(queries: Vec<(i32, i32)>) -> Vec {\n let mut spf: Vec = Vec::new();\n let mut i: usize = 0;\n while i <= 5_000_000usize {\n spf.push(0usize);\n i = i + 1;\n }\n\n i = 2usize;\n while i <= 5_000_000usize {\n let mut j = i;\n while j <= 5_000_000usize {\n if spf[j] == 0usize {\n spf[j] = i;\n }\n j = j + i;\n }\n i = i + 1;\n }\n\n i = 2usize;\n while i <= 5_000_000usize {\n let d = spf[i];\n let q = i / d;\n if q <= 1 {\n spf[i] = 1usize;\n } else {\n let omega_val = spf[q] + 1;\n spf[i] = omega_val;\n }\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(0u64);\n prefix.push(0u64);\n\n i = 2usize;\n while i <= 5_000_000usize {\n let value = spf[i] as u64;\n let total = prefix[i - 1] + value;\n prefix.push(total);\n i = i + 1;\n }\n\n let mut res = Vec::new();\n i = 0usize;\n while i < queries.len() {\n let (a, b) = queries[i];\n let answer = prefix[a as usize] - prefix[b as usize];\n res.push(answer);\n i = i + 1;\n }\n res\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).expect(\"read input\");\n let nums: Vec = input\n .split_whitespace()\n .map(|s| s.parse::().expect(\"integer\"))\n .collect();\n\n let t = nums[0] as usize;\n let mut queries: Vec<(i32, i32)> = Vec::new();\n let mut i = 1usize;\n while i < 1 + 2 * t {\n queries.push((nums[i], nums[i + 1]));\n i += 2;\n }\n\n let ans = Solution::max_scores_for_games(queries);\n let mut out = String::new();\n let mut i = 0usize;\n while i < ans.len() {\n out.push_str(&format!(\"{}\\n\", ans[i]));\n i += 1;\n }\n print!(\"{}\", out);\n}\n", "checker": null, "testcases_path": "codeforces/cf546D/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf546D/tests/mutated_testcases.jsonl"} {"problem": "cf580A", "family": "codeforces", "description": "# Kefa and First Steps\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nKefa decided to make some money doing business on the Internet for exactly $n$ days. He knows that on the $i$-th day ($1\u2009\u2264\u2009i\u2009\u2264\u2009n$) he makes $a_i$ money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence $a_i$. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.\n\nHelp Kefa cope with this task!\n\n## Input\n\nThe first line contains integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u200910^5$).\n\nThe second line contains $n$ integers $a_1,\u2009\u2009a_2,\u2009\u2009...,\u2009\u2009a_n$ ($1\u2009\u2264\u2009a_i\u2009\u2264\u200910^9$).\n\n## Output\n\nPrint a single integer \u2014 the length of the maximum non-decreasing subsegment of sequence $a$.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n6\n2 2 1 3 4 1\n```\n**Output:**\n```\n3\n```\n\n### Example 2\n\n**Input:**\n```\n3\n2 2 9\n```\n**Output:**\n```\n3\n```\n\n## Note\n\nIn the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.\n\nIn the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn longest_non_decreasing_run(a: Vec) -> usize {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let line = lines.next().unwrap().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for s in line.split_whitespace() {\n a.push(s.parse().unwrap());\n }\n let ans = Solution::longest_non_decreasing_run(a);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 900\nbrute force, dp, implementation\nSolved by: 103283 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_nd_contiguous(seq: Seq, l: int, r: int) -> bool {\n &&& 0 <= l <= r < seq.len()\n &&& forall|k: int| l <= k < r ==> #[trigger] seq[k] <= seq[k + 1]\n}\n\nimpl Solution {\n pub fn longest_non_decreasing_run(a: Vec) -> (result: usize)\n requires\n 1 <= a.len() <= 100_000,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n ensures\n 1 <= result as int <= a.len() as int,\n exists|l: int, r: int|\n 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r) && r - l + 1 == result as int,\n forall|l: int, r: int|\n 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r)\n ==> r - l + 1 <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn longest_non_decreasing_run(a: Vec) -> usize {\n let n = a.len();\n let mut best = 1usize;\n let mut cur = 1usize;\n let mut i = 1usize;\n while i < n {\n if a[i] >= a[i - 1] {\n cur = cur + 1;\n } else {\n cur = 1;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_nd_contiguous(seq: Seq, l: int, r: int) -> bool {\n &&& 0 <= l <= r < seq.len()\n &&& forall|k: int| l <= k < r ==> #[trigger] seq[k] <= seq[k + 1]\n}\n\nimpl Solution {\n pub fn longest_non_decreasing_run(a: Vec) -> (result: usize)\n requires\n 1 <= a.len() <= 100_000,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n ensures\n 1 <= result as int <= a.len() as int,\n exists|l: int, r: int|\n 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r) && r - l + 1 == result as int,\n forall|l: int, r: int|\n 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r)\n ==> r - l + 1 <= result as int,\n {\n let n = a.len();\n let mut best = 1usize;\n let mut cur = 1usize;\n let mut i = 1usize;\n while i < n {\n if a[i] >= a[i - 1] {\n cur = cur + 1;\n } else {\n cur = 1;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_nd_contiguous(seq: Seq, l: int, r: int) -> bool {\n &&& 0 <= l <= r < seq.len()\n &&& forall|k: int| l <= k < r ==> #[trigger] seq[k] <= seq[k + 1]\n}\n\npub open spec fn run_len_ending_at(seq: Seq, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= seq.len() {\n 0\n } else if i == 0 {\n 1\n } else if seq[i - 1] <= seq[i] {\n run_len_ending_at(seq, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_run_len_upto(seq: Seq, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = run_len_ending_at(seq, hi);\n let prev = max_run_len_upto(seq, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nproof fn lemma_run_len_nonneg(seq: Seq, i: int)\n requires\n 0 <= i < seq.len(),\n ensures\n run_len_ending_at(seq, i) >= 1,\n decreases i + 1,\n{\n if i == 0 {\n } else {\n lemma_run_len_nonneg(seq, i - 1);\n }\n}\n\nproof fn lemma_segment_le_run_len(seq: Seq, l: int, r: int)\n requires\n 0 <= l <= r < seq.len(),\n forall|k: int| l <= k < r ==> #[trigger] seq[k] <= seq[k + 1],\n ensures\n (r - l + 1) <= run_len_ending_at(seq, r),\n decreases r - l,\n{\n if l == r {\n lemma_run_len_nonneg(seq, r);\n assert(r - l + 1 == 1);\n } else {\n assert(l < r);\n assert(seq[r - 1] <= seq[r]);\n assert(forall|k: int| l <= k < r - 1 ==> #[trigger] seq[k] <= seq[k + 1]);\n lemma_segment_le_run_len(seq, l, r - 1);\n assert(run_len_ending_at(seq, r) == run_len_ending_at(seq, r - 1) + 1);\n assert((r - l + 1) <= run_len_ending_at(seq, r));\n }\n}\n\nproof fn lemma_run_len_le_index(seq: Seq, idx: int)\n requires\n 0 <= idx < seq.len(),\n ensures\n run_len_ending_at(seq, idx) <= idx + 1,\n decreases idx + 1,\n{\n if idx == 0 {\n assert(run_len_ending_at(seq, 0) == 1);\n } else {\n lemma_run_len_le_index(seq, idx - 1);\n if seq[idx - 1] <= seq[idx] {\n assert(run_len_ending_at(seq, idx) == run_len_ending_at(seq, idx - 1) + 1);\n assert(run_len_ending_at(seq, idx - 1) <= idx);\n assert(run_len_ending_at(seq, idx) <= idx + 1);\n } else {\n assert(run_len_ending_at(seq, idx) == 1);\n }\n }\n}\n\nproof fn lemma_max_upto_ge_run_len(seq: Seq, hi: int, j: int)\n requires\n 0 <= j <= hi,\n ensures\n run_len_ending_at(seq, j) <= max_run_len_upto(seq, hi),\n decreases hi - j,\n{\n if j == hi {\n assert(max_run_len_upto(seq, hi) >= run_len_ending_at(seq, hi));\n } else {\n lemma_max_upto_ge_run_len(seq, hi - 1, j);\n assert(max_run_len_upto(seq, hi) >= max_run_len_upto(seq, hi - 1));\n }\n}\n\nproof fn lemma_nd_of_run_ending_at(seq: Seq, i: int)\n requires\n 0 <= i < seq.len(),\n ensures\n is_nd_contiguous(seq, i - run_len_ending_at(seq, i) + 1, i),\n decreases i + 1,\n{\n let rl = run_len_ending_at(seq, i);\n let l = i - rl + 1;\n if i == 0 {\n assert(rl == 1);\n assert(l == 0);\n assert(is_nd_contiguous(seq, 0, 0));\n } else {\n if seq[i - 1] <= seq[i] {\n assert(rl == run_len_ending_at(seq, i - 1) + 1);\n assert(l == (i - 1) - run_len_ending_at(seq, i - 1) + 1);\n lemma_nd_of_run_ending_at(seq, i - 1);\n assert(is_nd_contiguous(seq, l, i - 1));\n assert(forall|k: int| l <= k < i - 1 ==> #[trigger] seq[k] <= seq[k + 1]);\n assert(seq[i - 1] <= seq[i]);\n assert(is_nd_contiguous(seq, l, i));\n } else {\n assert(rl == 1);\n assert(l == i);\n assert(is_nd_contiguous(seq, i, i));\n }\n }\n}\n\nproof fn witness_end_for_max(seq: Seq, hi: int) -> (j: int)\n requires\n 0 <= hi < seq.len(),\n ensures\n 0 <= j <= hi,\n run_len_ending_at(seq, j) == max_run_len_upto(seq, hi),\n decreases hi,\n{\n if hi == 0 {\n reveal_with_fuel(max_run_len_upto, 3);\n reveal_with_fuel(run_len_ending_at, 3);\n assert(run_len_ending_at(seq, 0) == 1);\n assert(max_run_len_upto(seq, 0) == run_len_ending_at(seq, 0));\n 0\n } else {\n let e = run_len_ending_at(seq, hi);\n let pm = max_run_len_upto(seq, hi - 1);\n if e > pm {\n assert(max_run_len_upto(seq, hi) == e);\n assert(run_len_ending_at(seq, hi) == max_run_len_upto(seq, hi));\n hi\n } else {\n let j0 = witness_end_for_max(seq, hi - 1);\n j0\n }\n }\n}\n\nimpl Solution {\n pub fn longest_non_decreasing_run(a: Vec) -> (result: usize)\n requires\n 1 <= a.len() <= 100_000,\n forall|i: int| 0 <= i < a.len() ==> 1 <= #[trigger] a[i] <= 1_000_000_000,\n ensures\n 1 <= result as int <= a.len() as int,\n exists|l: int, r: int|\n 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r) && r - l + 1 == result as int,\n forall|l: int, r: int|\n 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r)\n ==> r - l + 1 <= result as int,\n {\n let n = a.len();\n let mut best = 1usize;\n let mut cur = 1usize;\n let mut i = 1usize;\n proof {\n reveal_with_fuel(run_len_ending_at, 5);\n reveal_with_fuel(max_run_len_upto, 5);\n assert(run_len_ending_at(a@, 0) == 1);\n assert(max_run_len_upto(a@, 0) == 1);\n assert(cur as int == run_len_ending_at(a@, 0));\n assert(best as int == max_run_len_upto(a@, 0));\n }\n while i < n\n invariant\n 1 <= n <= 100_000,\n n == a.len(),\n forall|j: int| 0 <= j < a.len() ==> 1 <= #[trigger] a[j] <= 1_000_000_000,\n 1 <= i <= n,\n cur <= n,\n best <= n,\n cur as int == run_len_ending_at(a@, i - 1),\n best as int == max_run_len_upto(a@, i - 1),\n decreases n - i,\n {\n proof {\n lemma_run_len_nonneg(a@, i - 1);\n lemma_run_len_le_index(a@, i - 1);\n assert(i as int <= n as int);\n assert((cur as int) + 1 <= n as int);\n }\n if a[i] >= a[i - 1] {\n assert(a@[(i - 1) as int] <= a@[i as int]);\n proof {\n reveal_with_fuel(run_len_ending_at, 5);\n }\n assert(run_len_ending_at(a@, i as int) == run_len_ending_at(a@, i - 1) + 1);\n cur = cur + 1;\n } else {\n assert(!(a@[(i - 1) as int] <= a@[i as int]));\n proof {\n reveal_with_fuel(run_len_ending_at, 5);\n }\n assert(run_len_ending_at(a@, i as int) == 1);\n cur = 1;\n }\n proof {\n reveal_with_fuel(run_len_ending_at, 5);\n }\n assert(cur as int == run_len_ending_at(a@, i as int));\n let ghost prev_best = best;\n if cur > best {\n best = cur;\n } else {\n }\n proof {\n reveal_with_fuel(max_run_len_upto, 5);\n assert(cur as int == run_len_ending_at(a@, i as int));\n assert(max_run_len_upto(a@, i as int)\n == if run_len_ending_at(a@, i as int) > max_run_len_upto(a@, i - 1) {\n run_len_ending_at(a@, i as int)\n } else {\n max_run_len_upto(a@, i - 1)\n });\n if cur as int > prev_best as int {\n assert(best as int == cur as int);\n } else {\n assert(best as int == prev_best as int);\n }\n }\n assert(best as int == max_run_len_upto(a@, i as int));\n i = i + 1;\n }\n proof {\n assert(i == n);\n assert(best as int == max_run_len_upto(a@, n as int - 1));\n assert forall|l: int, r: int|\n (0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r))\n implies (r - l + 1) <= best as int by {\n assert forall|l: int, r: int|\n (0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r))\n implies (r - l + 1) <= best as int by {\n if 0 <= l <= r < a.len() as int && is_nd_contiguous(a@, l, r) {\n lemma_segment_le_run_len(a@, l, r);\n lemma_max_upto_ge_run_len(a@, n as int - 1, r);\n }\n };\n };\n let jm = witness_end_for_max(a@, n as int - 1);\n let rl = run_len_ending_at(a@, jm);\n let lw = jm - rl + 1;\n lemma_nd_of_run_ending_at(a@, jm);\n assert(is_nd_contiguous(a@, lw, jm));\n assert(jm - lw + 1 == rl);\n assert(rl == max_run_len_upto(a@, n as int - 1));\n assert(rl == best as int);\n }\n best\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn longest_non_decreasing_run(a: Vec) -> usize {\n let n = a.len();\n let mut best = 1usize;\n let mut cur = 1usize;\n let mut i = 1usize;\n while i < n {\n if a[i] >= a[i - 1] {\n cur = cur + 1;\n } else {\n cur = 1;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let line = lines.next().unwrap().unwrap();\n let mut a: Vec = Vec::with_capacity(n);\n for s in line.split_whitespace() {\n a.push(s.parse().unwrap());\n }\n let ans = Solution::longest_non_decreasing_run(a);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf580A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf580A/tests/mutated_testcases.jsonl"} {"problem": "cf688A", "family": "codeforces", "description": "# Opponents\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nArya has $n$ opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya.\n\nFor each opponent Arya knows his schedule\u00a0\u2014 whether or not he is going to present on each particular day. Tell him the maximum number of **consecutive** days that he will beat all present opponents.\n\nNote, that if some day there are no opponents present, Arya still considers he beats all the present opponents.\n\n## Input\n\nThe first line of the input contains two integers $n$ and $d$ ($1\u2009\u2264\u2009n,\u2009d\u2009\u2264\u2009100$)\u00a0\u2014 the number of opponents and the number of days, respectively.\n\nThe $i$-th of the following $d$ lines contains a string of length $n$ consisting of characters '`0`' and '`1`'. The $j$-th character of this string is '`0`' if the $j$-th opponent is going to be absent on the $i$-th day.\n\n## Output\n\nPrint the only integer\u00a0\u2014 the maximum number of consecutive days that Arya will beat all present opponents.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n2 2\n10\n00\n```\n**Output:**\n```\n2\n```\n\n### Example 2\n\n**Input:**\n```\n4 1\n0100\n```\n**Output:**\n```\n1\n```\n\n### Example 3\n\n**Input:**\n```\n4 5\n1101\n1111\n0110\n1011\n1111\n```\n**Output:**\n```\n2\n```\n\n## Note\n\nIn the first and the second samples, Arya will beat all present opponents each of the $d$ days.\n\nIn the third sample, Arya will beat his opponents on days $1$, $3$ and $4$ and his opponents will beat him on days $2$ and $5$. Thus, the maximum number of consecutive winning days is $2$, which happens on days $3$ and $4$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn max_consecutive_winning_days(n: usize, d: usize, days: &Vec>) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut lines = input.lines();\n let first = lines.next().unwrap();\n let mut it = first.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let d: usize = it.next().unwrap().parse().unwrap();\n let mut days: Vec> = Vec::new();\n let mut i = 0usize;\n while i < d {\n let line = lines.next().unwrap().trim();\n let mut row: Vec = Vec::new();\n let mut j = 0usize;\n while j < n {\n row.push(line.as_bytes()[j]);\n j = j + 1;\n }\n days.push(row);\n i = i + 1;\n }\n let mut t = 0usize;\n while t < d {\n assert!(days[t].len() == n);\n t = t + 1;\n }\n let ans = Solution::max_consecutive_winning_days(n, d, &days);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 14736 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn all_opponents_present(row: Seq) -> bool {\n forall|j: int| 0 <= j && j < row.len() ==> #[trigger] row[j] == 49u8\n}\n\npub open spec fn arya_beats(row: Seq) -> bool {\n !all_opponents_present(row)\n}\n\npub open spec fn win_interval(days: Seq>, l: int, r: int) -> bool {\n forall|t: int| l <= t && t <= r ==> #[trigger] arya_beats(days[t]@)\n}\n\npub open spec fn win_streak_ending_at(days: Seq>, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= days.len() {\n 0\n } else if !arya_beats(days[i]@) {\n 0\n } else if i == 0 {\n 1\n } else if arya_beats(days[i - 1]@) {\n win_streak_ending_at(days, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_win_streak_upto(days: Seq>, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = win_streak_ending_at(days, hi);\n let prev = max_win_streak_upto(days, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn max_consecutive_winning_days(n: usize, d: usize, days: &Vec>) -> (result: usize)\n requires\n 1 <= n && n <= 100,\n 1 <= d && d <= 100,\n days.len() == d,\n forall|i: int|\n 0 <= i && i < d ==> #[trigger] days@[i].len() == n,\n forall|i: int, j: int|\n 0 <= i && i < d && 0 <= j && j < n\n ==> (#[trigger] days@[i]@[j] == 48u8 || #[trigger] days@[i]@[j] == 49u8),\n ensures\n result as int <= d as int,\n result as int == max_win_streak_upto(days@, d as int - 1),\n (result as int == 0) || (exists|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) && r - l + 1 == result as int),\n forall|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) ==> r - l + 1 <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn is_win_row(row: &Vec) -> bool {\n let n = row.len();\n let mut j = 0usize;\n let mut found = false;\n while j < n {\n if row[j] == 48u8 {\n found = true;\n }\n j = j + 1;\n }\n found\n }\n\n pub fn max_consecutive_winning_days(n: usize, d: usize, days: &Vec>) -> usize {\n let _ = n;\n let mut best = 0usize;\n let mut cur = 0usize;\n let mut i = 0usize;\n while i < d {\n if Solution::is_win_row(&days[i]) {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn all_opponents_present(row: Seq) -> bool {\n forall|j: int| 0 <= j && j < row.len() ==> #[trigger] row[j] == 49u8\n}\n\npub open spec fn arya_beats(row: Seq) -> bool {\n !all_opponents_present(row)\n}\n\npub open spec fn win_interval(days: Seq>, l: int, r: int) -> bool {\n forall|t: int| l <= t && t <= r ==> #[trigger] arya_beats(days[t]@)\n}\n\npub open spec fn win_streak_ending_at(days: Seq>, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= days.len() {\n 0\n } else if !arya_beats(days[i]@) {\n 0\n } else if i == 0 {\n 1\n } else if arya_beats(days[i - 1]@) {\n win_streak_ending_at(days, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_win_streak_upto(days: Seq>, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = win_streak_ending_at(days, hi);\n let prev = max_win_streak_upto(days, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n fn is_win_row(row: &Vec) -> bool\n requires\n row.len() <= 100,\n forall|j: int| 0 <= j && j < row.len() ==> row[j] == 48u8 || row[j] == 49u8,\n returns\n arya_beats(row@),\n {\n let n = row.len();\n let mut j = 0usize;\n let mut found = false;\n while j < n\n decreases n - j,\n {\n if row[j] == 48u8 {\n found = true;\n }\n j = j + 1;\n }\n found\n }\n\n pub fn max_consecutive_winning_days(n: usize, d: usize, days: &Vec>) -> (result: usize)\n requires\n 1 <= n && n <= 100,\n 1 <= d && d <= 100,\n days.len() == d,\n forall|i: int|\n 0 <= i && i < d ==> #[trigger] days@[i].len() == n,\n forall|i: int, j: int|\n 0 <= i && i < d && 0 <= j && j < n\n ==> (#[trigger] days@[i]@[j] == 48u8 || #[trigger] days@[i]@[j] == 49u8),\n ensures\n result as int <= d as int,\n result as int == max_win_streak_upto(days@, d as int - 1),\n (result as int == 0) || (exists|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) && r - l + 1 == result as int),\n forall|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) ==> r - l + 1 <= result as int,\n {\n let _ = n;\n let mut best = 0usize;\n let mut cur = 0usize;\n let mut i = 0usize;\n while i < d\n decreases d - i,\n {\n if Solution::is_win_row(&days[i]) {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn all_opponents_present(row: Seq) -> bool {\n forall|j: int| 0 <= j && j < row.len() ==> #[trigger] row[j] == 49u8\n}\n\npub open spec fn arya_beats(row: Seq) -> bool {\n !all_opponents_present(row)\n}\n\npub open spec fn win_interval(days: Seq>, l: int, r: int) -> bool {\n forall|t: int| l <= t && t <= r ==> #[trigger] arya_beats(days[t]@)\n}\n\npub open spec fn win_streak_ending_at(days: Seq>, i: int) -> int\n decreases i + 1,\n{\n if i < 0 || i >= days.len() {\n 0\n } else if !arya_beats(days[i]@) {\n 0\n } else if i == 0 {\n 1\n } else if arya_beats(days[i - 1]@) {\n win_streak_ending_at(days, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn max_win_streak_upto(days: Seq>, hi: int) -> int\n decreases hi + 1,\n{\n if hi < 0 {\n 0\n } else {\n let e = win_streak_ending_at(days, hi);\n let prev = max_win_streak_upto(days, hi - 1);\n if e > prev {\n e\n } else {\n prev\n }\n }\n}\n\nproof fn lemma_exists_zero_iff_beats(row: Seq)\n requires\n row.len() >= 1,\n forall|j: int| 0 <= j && j < row.len() ==> row[j] == 48u8 || row[j] == 49u8,\n ensures\n arya_beats(row) == (exists|k: int| 0 <= k && k < row.len() && #[trigger] row[k] == 48u8),\n{\n assert(all_opponents_present(row) == (forall|j: int| 0 <= j && j < row.len() ==> row[j] == 49u8));\n if exists|k: int| 0 <= k && k < row.len() && row[k] == 48u8 {\n assert(!all_opponents_present(row));\n assert(arya_beats(row));\n } else {\n assert(forall|j: int| 0 <= j && j < row.len() ==> row[j] == 49u8);\n assert(all_opponents_present(row));\n assert(!arya_beats(row));\n }\n}\n\nproof fn lemma_win_streak_nonneg(days: Seq>, i: int)\n requires\n 0 <= i < days.len(),\n arya_beats(days[i]@),\n ensures\n win_streak_ending_at(days, i) >= 1,\n decreases i + 1,\n{\n if i == 0 {\n } else {\n if arya_beats(days[i - 1]@) {\n lemma_win_streak_nonneg(days, i - 1);\n } else {\n }\n }\n}\n\nproof fn lemma_segment_le_win_streak(days: Seq>, l: int, r: int)\n requires\n 0 <= l <= r < days.len(),\n win_interval(days, l, r),\n ensures\n (r - l + 1) <= win_streak_ending_at(days, r),\n decreases r - l,\n{\n if l == r {\n lemma_win_streak_nonneg(days, r);\n assert(r - l + 1 == 1);\n } else {\n assert(l < r);\n assert(arya_beats(days[r - 1]@));\n assert(arya_beats(days[r]@));\n assert(forall|t: int| l <= t && t <= r - 1 ==> arya_beats(#[trigger] days[t]@));\n lemma_segment_le_win_streak(days, l, r - 1);\n assert(win_streak_ending_at(days, r) == win_streak_ending_at(days, r - 1) + 1);\n assert((r - l + 1) <= win_streak_ending_at(days, r));\n }\n}\n\nproof fn lemma_win_streak_le_index(days: Seq>, idx: int)\n requires\n 0 <= idx < days.len(),\n ensures\n win_streak_ending_at(days, idx) <= idx + 1,\n decreases idx + 1,\n{\n if idx == 0 {\n assert(win_streak_ending_at(days, 0) == 0 || win_streak_ending_at(days, 0) == 1);\n } else {\n lemma_win_streak_le_index(days, idx - 1);\n if !arya_beats(days[idx]@) {\n assert(win_streak_ending_at(days, idx) == 0);\n } else if arya_beats(days[idx - 1]@) {\n assert(win_streak_ending_at(days, idx) == win_streak_ending_at(days, idx - 1) + 1);\n assert(win_streak_ending_at(days, idx - 1) <= idx);\n assert(win_streak_ending_at(days, idx) <= idx + 1);\n } else {\n assert(win_streak_ending_at(days, idx) == 1);\n }\n }\n}\n\nproof fn lemma_max_upto_ge_win_streak(days: Seq>, hi: int, j: int)\n requires\n 0 <= j <= hi,\n ensures\n win_streak_ending_at(days, j) <= max_win_streak_upto(days, hi),\n decreases hi - j,\n{\n if j == hi {\n assert(max_win_streak_upto(days, hi) >= win_streak_ending_at(days, hi));\n } else {\n lemma_max_upto_ge_win_streak(days, hi - 1, j);\n assert(max_win_streak_upto(days, hi) >= max_win_streak_upto(days, hi - 1));\n }\n}\n\nproof fn lemma_win_interval_of_streak_ending_at(days: Seq>, i: int)\n requires\n 0 <= i < days.len(),\n arya_beats(days[i]@),\n ensures\n win_interval(days, i - win_streak_ending_at(days, i) + 1, i),\n decreases i + 1,\n{\n let ws = win_streak_ending_at(days, i);\n let l = i - ws + 1;\n if i == 0 {\n assert(ws == 1);\n assert(l == 0);\n assert(win_interval(days, 0, 0));\n } else {\n if arya_beats(days[i - 1]@) {\n assert(ws == win_streak_ending_at(days, i - 1) + 1);\n assert(l == (i - 1) - win_streak_ending_at(days, i - 1) + 1);\n lemma_win_interval_of_streak_ending_at(days, i - 1);\n assert(win_interval(days, l, i - 1));\n assert(arya_beats(days[i]@));\n assert(win_interval(days, l, i));\n } else {\n assert(ws == 1);\n assert(l == i);\n assert(win_interval(days, i, i));\n }\n }\n}\n\nproof fn witness_end_for_max(days: Seq>, hi: int) -> (j: int)\n requires\n 0 <= hi < days.len(),\n ensures\n 0 <= j <= hi,\n win_streak_ending_at(days, j) == max_win_streak_upto(days, hi),\n decreases hi,\n{\n if hi == 0 {\n reveal_with_fuel(max_win_streak_upto, 3);\n reveal_with_fuel(win_streak_ending_at, 3);\n assert(win_streak_ending_at(days, 0) == 0 || win_streak_ending_at(days, 0) == 1);\n assert(max_win_streak_upto(days, 0) == win_streak_ending_at(days, 0));\n 0\n } else {\n let e = win_streak_ending_at(days, hi);\n let pm = max_win_streak_upto(days, hi - 1);\n if e > pm {\n assert(max_win_streak_upto(days, hi) == e);\n assert(win_streak_ending_at(days, hi) == max_win_streak_upto(days, hi));\n hi\n } else {\n let j0 = witness_end_for_max(days, hi - 1);\n j0\n }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n fn is_win_row(row: &Vec) -> bool\n requires\n row.len() <= 100,\n forall|j: int| 0 <= j && j < row.len() ==> row[j] == 48u8 || row[j] == 49u8,\n returns\n arya_beats(row@),\n {\n let n = row.len();\n let mut j = 0usize;\n let mut found = false;\n while j < n\n invariant\n n == row.len(),\n j <= n,\n found == (exists|k: int| 0 <= k && k < j && #[trigger] row[k] == 48u8),\n decreases n - j,\n {\n proof {\n assert(j < row.len());\n }\n if row[j] == 48u8 {\n found = true;\n }\n j = j + 1;\n }\n proof {\n if n >= 1 {\n lemma_exists_zero_iff_beats(row@);\n } else {\n assert(row@.len() == 0);\n assert(!arya_beats(row@));\n assert(!found);\n }\n }\n found\n }\n\n pub fn max_consecutive_winning_days(n: usize, d: usize, days: &Vec>) -> (result: usize)\n requires\n 1 <= n && n <= 100,\n 1 <= d && d <= 100,\n days.len() == d,\n forall|i: int|\n 0 <= i && i < d ==> #[trigger] days@[i].len() == n,\n forall|i: int, j: int|\n 0 <= i && i < d && 0 <= j && j < n\n ==> (#[trigger] days@[i]@[j] == 48u8 || #[trigger] days@[i]@[j] == 49u8),\n ensures\n result as int <= d as int,\n result as int == max_win_streak_upto(days@, d as int - 1),\n (result as int == 0) || (exists|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) && r - l + 1 == result as int),\n forall|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) ==> r - l + 1 <= result as int,\n {\n let _ = n;\n let mut best = 0usize;\n let mut cur = 0usize;\n let mut i = 0usize;\n proof {\n reveal_with_fuel(win_streak_ending_at, 5);\n reveal_with_fuel(max_win_streak_upto, 5);\n assert(win_streak_ending_at(days@, -1) == 0);\n assert(max_win_streak_upto(days@, -1) == 0);\n assert(cur as int == win_streak_ending_at(days@, i as int - 1));\n assert(best as int == max_win_streak_upto(days@, i as int - 1));\n }\n while i < d\n invariant\n 1 <= n && n <= 100,\n 1 <= d && d <= 100,\n days.len() == d,\n forall|ii: int|\n 0 <= ii && ii < d ==> #[trigger] days@[ii].len() == n,\n forall|ii: int, jj: int|\n 0 <= ii && ii < d && 0 <= jj && jj < n\n ==> (#[trigger] days@[ii]@[jj] == 48u8 || #[trigger] days@[ii]@[jj] == 49u8),\n 0 <= i && i <= d,\n cur as int == win_streak_ending_at(days@, i as int - 1),\n best as int == max_win_streak_upto(days@, i as int - 1),\n decreases d - i,\n {\n proof {\n if i > 0 {\n lemma_win_streak_le_index(days@, i as int - 1);\n assert((cur as int) + 1 <= d as int);\n }\n }\n if Solution::is_win_row(&days[i]) {\n proof {\n assert(arya_beats(days@[i as int]@));\n }\n cur = cur + 1;\n proof {\n reveal_with_fuel(win_streak_ending_at, 5);\n if i == 0 {\n assert(win_streak_ending_at(days@, 0) == 1);\n assert(cur as int == win_streak_ending_at(days@, i as int));\n } else {\n assert(arya_beats(days@[i as int]@));\n if arya_beats(days@[(i - 1) as int]@) {\n assert(win_streak_ending_at(days@, i as int) == win_streak_ending_at(days@, (i - 1) as int) + 1);\n } else {\n assert(win_streak_ending_at(days@, i as int) == 1);\n }\n assert(cur as int == win_streak_ending_at(days@, i as int));\n }\n }\n } else {\n proof {\n assert(!arya_beats(days@[i as int]@));\n reveal_with_fuel(win_streak_ending_at, 5);\n assert(win_streak_ending_at(days@, i as int) == 0);\n }\n cur = 0;\n proof {\n assert(cur as int == win_streak_ending_at(days@, i as int));\n }\n }\n proof {\n reveal_with_fuel(win_streak_ending_at, 5);\n }\n assert(cur as int == win_streak_ending_at(days@, i as int));\n let ghost prev_best = best;\n if cur > best {\n best = cur;\n } else {\n }\n proof {\n reveal_with_fuel(max_win_streak_upto, 5);\n assert(cur as int == win_streak_ending_at(days@, i as int));\n assert(\n max_win_streak_upto(days@, i as int)\n == if win_streak_ending_at(days@, i as int)\n > max_win_streak_upto(days@, i as int - 1)\n {\n win_streak_ending_at(days@, i as int)\n } else {\n max_win_streak_upto(days@, i as int - 1)\n }\n );\n if cur as int > prev_best as int {\n assert(best as int == cur as int);\n } else {\n assert(best as int == prev_best as int);\n }\n }\n assert(best as int == max_win_streak_upto(days@, i as int));\n i = i + 1;\n }\n proof {\n assert(i == d);\n assert(best as int == max_win_streak_upto(days@, d as int - 1));\n assert forall|l: int, r: int|\n (0 <= l && l <= r && r < d as int && win_interval(days@, l, r))\n implies (r - l + 1) <= best as int by {\n assert forall|l: int, r: int|\n (0 <= l && l <= r && r < d as int && win_interval(days@, l, r))\n implies (r - l + 1) <= best as int by {\n if 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) {\n lemma_segment_le_win_streak(days@, l, r);\n lemma_max_upto_ge_win_streak(days@, d as int - 1, r);\n }\n };\n };\n if best as int > 0 {\n let jm = witness_end_for_max(days@, d as int - 1);\n let rl = win_streak_ending_at(days@, jm);\n let lw = jm - rl + 1;\n lemma_win_streak_le_index(days@, jm);\n assert(rl <= jm + 1);\n assert(lw >= 0);\n assert(lw <= jm);\n assert(jm < d as int);\n lemma_win_interval_of_streak_ending_at(days@, jm);\n assert(win_interval(days@, lw, jm));\n assert(jm - lw + 1 == rl);\n assert(rl == max_win_streak_upto(days@, d as int - 1));\n assert(rl == best as int);\n assert(exists|l: int, r: int|\n 0 <= l && l <= r && r < d as int && win_interval(days@, l, r) && r - l + 1 == best as int);\n }\n assert(best as int <= d as int);\n }\n best\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n fn is_win_row(row: &Vec) -> bool {\n let n = row.len();\n let mut j = 0usize;\n let mut found = false;\n while j < n {\n if row[j] == 48u8 {\n found = true;\n }\n j = j + 1;\n }\n found\n }\n\n pub fn max_consecutive_winning_days(n: usize, d: usize, days: &Vec>) -> usize {\n let _ = n;\n let mut best = 0usize;\n let mut cur = 0usize;\n let mut i = 0usize;\n while i < d {\n if Solution::is_win_row(&days[i]) {\n cur = cur + 1;\n } else {\n cur = 0;\n }\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut lines = input.lines();\n let first = lines.next().unwrap();\n let mut it = first.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let d: usize = it.next().unwrap().parse().unwrap();\n let mut days: Vec> = Vec::new();\n let mut i = 0usize;\n while i < d {\n let line = lines.next().unwrap().trim();\n let mut row: Vec = Vec::new();\n let mut j = 0usize;\n while j < n {\n row.push(line.as_bytes()[j]);\n j = j + 1;\n }\n days.push(row);\n i = i + 1;\n }\n let mut t = 0usize;\n while t < d {\n assert!(days[t].len() == n);\n t = t + 1;\n }\n let ans = Solution::max_consecutive_winning_days(n, d, &days);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf688A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf688A/tests/mutated_testcases.jsonl"} {"problem": "cf69A", "family": "codeforces", "description": "# Young Physicist\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nA guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. \"Piece of cake\" \u2014 thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.\n\n## Input\n\nThe first line contains a positive integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u2009100$), then follow $n$ lines containing three integers each: the $x_i$ coordinate, the $y_i$ coordinate and the $z_i$ coordinate of the force vector, applied to the body ($\u2009-\u2009100\u2009\u2264\u2009x_i,\u2009y_i,\u2009z_i\u2009\u2264\u2009100$).\n\n## Output\n\nPrint the word \"`YES`\" if the body is in equilibrium, or the word \"`NO`\" if it is not.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n3\n4 1 7\n-2 4 -1\n1 -5 -3\n```\n**Output:**\n```\nNO\n```\n\n### Example 2\n\n**Input:**\n```\n3\n3 -1 7\n-5 2 -4\n2 -1 -3\n```\n**Output:**\n```\nYES\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_equilibrium(vec: Vec, n: usize) -> bool {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let mut vec: Vec = Vec::with_capacity(3 * n);\n for _ in 0..n {\n let line = lines.next().unwrap().unwrap();\n let parts: Vec = line.split_whitespace().map(|s| s.parse().unwrap()).collect();\n vec.push(parts[0]);\n vec.push(parts[1]);\n vec.push(parts[2]);\n }\n let ans = Solution::is_equilibrium(vec, n);\n println!(\"{}\", if ans { \"YES\" } else { \"NO\" });\n}\n```\n", "tags": "1200\nmath, implementation\n100000+\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_dim(vec: Seq, n: int, d: int) -> int\n recommends\n 0 <= d < 3,\n 0 <= n,\n 3 * n <= vec.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n vec[3 * (n - 1) + d] as int + sum_dim(vec, n - 1, d)\n }\n}\n\npub open spec fn forces_equilibrium(vec: Seq, n: int) -> bool\n recommends\n 1 <= n,\n 3 * n <= vec.len(),\n{\n forall|d: int| 0 <= d < 3 ==> #[trigger] sum_dim(vec, n, d) == 0\n}\n\nimpl Solution {\n pub fn is_equilibrium(vec: Vec, n: usize) -> (res: bool)\n requires\n 1 <= n <= 100,\n vec.len() == 3 * n,\n forall|i: int| 0 <= i < vec.len() ==> -100 <= #[trigger] vec[i] <= 100,\n ensures\n res == forces_equilibrium(vec@, n as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_equilibrium(vec: Vec, n: usize) -> bool {\n let mut sum_x = 0i64;\n let mut sum_y = 0i64;\n let mut sum_z = 0i64;\n let mut i = 0usize;\n while i < n {\n sum_x += vec[3 * i] as i64;\n sum_y += vec[3 * i + 1] as i64;\n sum_z += vec[3 * i + 2] as i64;\n i += 1;\n }\n sum_x == 0 && sum_y == 0 && sum_z == 0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_dim(vec: Seq, n: int, d: int) -> int\n recommends\n 0 <= d < 3,\n 0 <= n,\n 3 * n <= vec.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n vec[3 * (n - 1) + d] as int + sum_dim(vec, n - 1, d)\n }\n}\n\npub open spec fn forces_equilibrium(vec: Seq, n: int) -> bool\n recommends\n 1 <= n,\n 3 * n <= vec.len(),\n{\n forall|d: int| 0 <= d < 3 ==> #[trigger] sum_dim(vec, n, d) == 0\n}\n\nimpl Solution {\n pub fn is_equilibrium(vec: Vec, n: usize) -> (res: bool)\n requires\n 1 <= n <= 100,\n vec.len() == 3 * n,\n forall|i: int| 0 <= i < vec.len() ==> -100 <= #[trigger] vec[i] <= 100,\n ensures\n res == forces_equilibrium(vec@, n as int),\n {\n let mut sum_x = 0i64;\n let mut sum_y = 0i64;\n let mut sum_z = 0i64;\n let mut i = 0usize;\n while i < n {\n sum_x += vec[3 * i] as i64;\n sum_y += vec[3 * i + 1] as i64;\n sum_z += vec[3 * i + 2] as i64;\n i += 1;\n }\n sum_x == 0 && sum_y == 0 && sum_z == 0\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_dim(vec: Seq, n: int, d: int) -> int\n recommends\n 0 <= d < 3,\n 0 <= n,\n 3 * n <= vec.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n vec[3 * (n - 1) + d] as int + sum_dim(vec, n - 1, d)\n }\n}\n\npub open spec fn forces_equilibrium(vec: Seq, n: int) -> bool\n recommends\n 1 <= n,\n 3 * n <= vec.len(),\n{\n forall|d: int| 0 <= d < 3 ==> #[trigger] sum_dim(vec, n, d) == 0\n}\n\nproof fn lemma_sum_dim_step(vec: Seq, n: int, d: int)\n requires\n 0 <= d < 3,\n 0 <= n,\n 3 * (n + 1) <= vec.len(),\n ensures\n sum_dim(vec, n + 1, d) == sum_dim(vec, n, d) + vec[3 * n + d] as int,\n decreases n,\n{\n reveal_with_fuel(sum_dim, 3);\n}\n\nimpl Solution {\n pub fn is_equilibrium(vec: Vec, n: usize) -> (res: bool)\n requires\n 1 <= n <= 100,\n vec.len() == 3 * n,\n forall|i: int| 0 <= i < vec.len() ==> -100 <= #[trigger] vec[i] <= 100,\n ensures\n res == forces_equilibrium(vec@, n as int),\n {\n let mut sum_x = 0i64;\n let mut sum_y = 0i64;\n let mut sum_z = 0i64;\n let mut i = 0usize;\n while i < n\n invariant\n 1 <= n <= 100,\n vec.len() == 3 * n,\n forall|j: int| 0 <= j < vec.len() ==> -100 <= #[trigger] vec[j] <= 100,\n 0 <= i <= n,\n sum_x as int == sum_dim(vec@, i as int, 0),\n sum_y as int == sum_dim(vec@, i as int, 1),\n sum_z as int == sum_dim(vec@, i as int, 2),\n -100 * (i as int) <= sum_x as int <= 100 * (i as int),\n -100 * (i as int) <= sum_y as int <= 100 * (i as int),\n -100 * (i as int) <= sum_z as int <= 100 * (i as int),\n decreases n - i,\n {\n proof {\n lemma_sum_dim_step(vec@, i as int, 0);\n lemma_sum_dim_step(vec@, i as int, 1);\n lemma_sum_dim_step(vec@, i as int, 2);\n }\n sum_x += vec[3 * i] as i64;\n sum_y += vec[3 * i + 1] as i64;\n sum_z += vec[3 * i + 2] as i64;\n i += 1;\n }\n proof {\n assert(sum_x as int == sum_dim(vec@, n as int, 0));\n assert(sum_y as int == sum_dim(vec@, n as int, 1));\n assert(sum_z as int == sum_dim(vec@, n as int, 2));\n assert((sum_x == 0 && sum_y == 0 && sum_z == 0) == forces_equilibrium(vec@, n as int));\n }\n sum_x == 0 && sum_y == 0 && sum_z == 0\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_equilibrium(vec: Vec, n: usize) -> bool {\n let mut sum_x = 0i64;\n let mut sum_y = 0i64;\n let mut sum_z = 0i64;\n let mut i = 0usize;\n while i < n {\n sum_x += vec[3 * i] as i64;\n sum_y += vec[3 * i + 1] as i64;\n sum_z += vec[3 * i + 2] as i64;\n i += 1;\n }\n sum_x == 0 && sum_y == 0 && sum_z == 0\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let mut vec: Vec = Vec::with_capacity(3 * n);\n for _ in 0..n {\n let line = lines.next().unwrap().unwrap();\n let parts: Vec = line.split_whitespace().map(|s| s.parse().unwrap()).collect();\n vec.push(parts[0]);\n vec.push(parts[1]);\n vec.push(parts[2]);\n }\n let ans = Solution::is_equilibrium(vec, n);\n println!(\"{}\", if ans { \"YES\" } else { \"NO\" });\n}\n", "checker": null, "testcases_path": "codeforces/cf69A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf69A/tests/mutated_testcases.jsonl"} {"problem": "cf6A", "family": "codeforces", "description": "# Triangle\n\nTime limit: 2 seconds | Memory limit: 64 megabytes\n\nJohnny has a younger sister Anne, who is very clever and smart. As she came home from the kindergarten, she told his brother about the task that her kindergartener asked her to solve. The task was just to construct a triangle out of four sticks of different colours. Naturally, one of the sticks is extra. It is not allowed to break the sticks or use their partial length. Anne has perfectly solved this task, now she is asking Johnny to do the same.\n\nThe boy answered that he would cope with it without any difficulty. However, after a while he found out that different tricky things can occur. It can happen that it is impossible to construct a triangle of a positive area, but it is possible to construct a degenerate triangle. It can be so, that it is impossible to construct a degenerate triangle even. As Johnny is very lazy, he does not want to consider such a big amount of cases, he asks you to help him.\n\n## Input\n\nThe first line of the input contains four space-separated positive integer numbers not exceeding 100 \u2014 lengthes of the sticks.\n\n## Output\n\nOutput `TRIANGLE` if it is possible to construct a non-degenerate triangle. Output `SEGMENT` if the first case cannot take place and it is possible to construct a degenerate triangle. Output `IMPOSSIBLE` if it is impossible to construct any triangle. Remember that you are to use three sticks. It is not allowed to break the sticks or use their partial length.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4 2 1 3\n```\n**Output:**\n```\nTRIANGLE\n```\n\n### Example 2\n\n**Input:**\n```\n7 2 2 4\n```\n**Output:**\n```\nSEGMENT\n```\n\n### Example 3\n\n**Input:**\n```\n3 5 9 1\n```\n**Output:**\n```\nIMPOSSIBLE\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn has_triangle(sticks: Vec) -> bool {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let parts: Vec = line.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let sticks = vec![parts[0], parts[1], parts[2], parts[3]];\n let has_tri = Solution::has_triangle(sticks.clone());\n let has_seg = Solution::has_segment(sticks);\n if has_tri {\n println!(\"TRIANGLE\");\n } else if has_seg {\n println!(\"SEGMENT\");\n } else {\n println!(\"IMPOSSIBLE\");\n }\n}\n\nimpl Solution {\n pub fn has_segment(sticks: Vec) -> bool {\n let mut i = 0usize;\n while i < 4 {\n let mut j = 0usize;\n while j < 4 {\n let mut k = 0usize;\n while k < 4 {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b == c || a + c == b || b + c == a {\n return true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n false\n }\n}\n```\n", "tags": "Rating: 900\nbrute force, geometry\nSolved by: 22540 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn triangle_inequality(a: int, b: int, c: int) -> bool {\n a + b > c && a + c > b && b + c > a\n}\n\npub open spec fn segment_inequality(a: int, b: int, c: int) -> bool {\n a + b == c || a + c == b || b + c == a\n}\n\npub open spec fn valid_triple(i: int, j: int, k: int) -> bool {\n 0 <= i < 4 && 0 <= j < 4 && 0 <= k < 4 && i != j && i != k && j != k\n}\n\nimpl Solution {\n pub fn has_triangle(sticks: Vec) -> (res: bool)\n requires\n sticks.len() == 4,\n forall|i: int| 0 <= i < 4 ==> 1 <= #[trigger] sticks[i] as int <= 100,\n ensures\n res == exists|i: int, j: int, k: int|\n valid_triple(i, j, k)\n && triangle_inequality(sticks@[i] as int, sticks@[j] as int, sticks@[k] as int),\n {\n }\n\n pub fn has_segment(sticks: Vec) -> (res: bool)\n requires\n sticks.len() == 4,\n forall|i: int| 0 <= i < 4 ==> 1 <= #[trigger] sticks[i] as int <= 100,\n ensures\n res == exists|i: int, j: int, k: int|\n valid_triple(i, j, k)\n && segment_inequality(sticks@[i] as int, sticks@[j] as int, sticks@[k] as int),\n {\n }\n}\n\n}", "code": "impl Solution {\n pub fn has_triangle(sticks: Vec) -> bool {\n let mut found = false;\n let mut i = 0usize;\n while i < 4 {\n let mut j = 0usize;\n while j < 4 {\n let mut k = 0usize;\n while k < 4 {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b > c && a + c > b && b + c > a {\n found = true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n found\n }\n}", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn triangle_inequality(a: int, b: int, c: int) -> bool {\n a + b > c && a + c > b && b + c > a\n}\n\npub open spec fn segment_inequality(a: int, b: int, c: int) -> bool {\n a + b == c || a + c == b || b + c == a\n}\n\npub open spec fn valid_triple(i: int, j: int, k: int) -> bool {\n 0 <= i < 4 && 0 <= j < 4 && 0 <= k < 4 && i != j && i != k && j != k\n}\n\nimpl Solution {\n pub fn has_triangle(sticks: Vec) -> (res: bool)\n requires\n sticks.len() == 4,\n forall|i: int| 0 <= i < 4 ==> 1 <= #[trigger] sticks[i] as int <= 100,\n ensures\n res == exists|i: int, j: int, k: int|\n valid_triple(i, j, k)\n && triangle_inequality(sticks@[i] as int, sticks@[j] as int, sticks@[k] as int),\n {\n let mut found = false;\n let mut i = 0usize;\n while i < 4 {\n let mut j = 0usize;\n while j < 4 {\n let mut k = 0usize;\n while k < 4 {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b > c && a + c > b && b + c > a {\n found = true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n found\n }\n\n pub fn has_segment(sticks: Vec) -> (res: bool)\n requires\n sticks.len() == 4,\n forall|i: int| 0 <= i < 4 ==> 1 <= #[trigger] sticks[i] as int <= 100,\n ensures\n res == exists|i: int, j: int, k: int|\n valid_triple(i, j, k)\n && segment_inequality(sticks@[i] as int, sticks@[j] as int, sticks@[k] as int),\n {\n let mut found = false;\n let mut i = 0usize;\n while i < 4 {\n let mut j = 0usize;\n while j < 4 {\n let mut k = 0usize;\n while k < 4 {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b == c || a + c == b || b + c == a {\n found = true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n found\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn triangle_inequality(a: int, b: int, c: int) -> bool {\n a + b > c && a + c > b && b + c > a\n}\n\npub open spec fn segment_inequality(a: int, b: int, c: int) -> bool {\n a + b == c || a + c == b || b + c == a\n}\n\npub open spec fn valid_triple(i: int, j: int, k: int) -> bool {\n 0 <= i < 4 && 0 <= j < 4 && 0 <= k < 4 && i != j && i != k && j != k\n}\n\nimpl Solution {\n pub fn has_triangle(sticks: Vec) -> (res: bool)\n requires\n sticks.len() == 4,\n forall|i: int| 0 <= i < 4 ==> 1 <= #[trigger] sticks[i] as int <= 100,\n ensures\n res == exists|i: int, j: int, k: int|\n valid_triple(i, j, k)\n && triangle_inequality(sticks@[i] as int, sticks@[j] as int, sticks@[k] as int),\n {\n let mut found = false;\n let mut i = 0usize;\n while i < 4\n invariant\n sticks.len() == 4,\n forall|ii: int| 0 <= ii < 4 ==> 1 <= #[trigger] sticks[ii] as int <= 100,\n 0 <= i <= 4,\n found == exists|ii: int, jj: int, kk: int|\n (ii < i as int && valid_triple(ii, jj, kk) && triangle_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int)),\n decreases 4 - i,\n {\n let mut j = 0usize;\n while j < 4\n invariant\n sticks.len() == 4,\n forall|ii: int| 0 <= ii < 4 ==> 1 <= #[trigger] sticks[ii] as int <= 100,\n i < 4,\n 0 <= j <= 4,\n found == exists|ii: int, jj: int, kk: int|\n ((ii < i as int && valid_triple(ii, jj, kk)) || (ii == i as int && jj < j as int && valid_triple(ii, jj, kk))) &&\n triangle_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int),\n decreases 4 - j,\n {\n let mut k = 0usize;\n while k < 4\n invariant\n sticks.len() == 4,\n forall|ii: int| 0 <= ii < 4 ==> 1 <= #[trigger] sticks[ii] as int <= 100,\n i < 4,\n j < 4,\n 0 <= k <= 4,\n found == exists|ii: int, jj: int, kk: int|\n ((ii < i as int && valid_triple(ii, jj, kk)) ||\n (ii == i as int && jj < j as int && valid_triple(ii, jj, kk)) ||\n (ii == i as int && jj == j as int && kk < k as int && valid_triple(ii, jj, kk))) &&\n triangle_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int),\n decreases 4 - k,\n {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b > c && a + c > b && b + c > a {\n proof {\n assert(triangle_inequality(sticks@[i as int] as int, sticks@[j as int] as int, sticks@[k as int] as int));\n assert(valid_triple(i as int, j as int, k as int));\n assert(exists|ii: int, jj: int, kk: int|\n valid_triple(ii, jj, kk)\n && triangle_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int)) by {\n let ii = i as int;\n let jj = j as int;\n let kk = k as int;\n }\n }\n found = true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n proof {\n assert forall|ii: int, jj: int, kk: int|\n valid_triple(ii, jj, kk) implies\n (ii < 4 && jj < 4 && kk < 4) by {\n assert(0 <= ii < 4 && 0 <= jj < 4 && 0 <= kk < 4);\n }\n assert(i == 4);\n assert(found == exists|ii: int, jj: int, kk: int|\n (ii < 4 && valid_triple(ii, jj, kk) && triangle_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int)));\n assert(found == exists|ii: int, jj: int, kk: int|\n valid_triple(ii, jj, kk) && triangle_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int));\n }\n found\n }\n\n pub fn has_segment(sticks: Vec) -> (res: bool)\n requires\n sticks.len() == 4,\n forall|i: int| 0 <= i < 4 ==> 1 <= #[trigger] sticks[i] as int <= 100,\n ensures\n res == exists|i: int, j: int, k: int|\n valid_triple(i, j, k)\n && segment_inequality(sticks@[i] as int, sticks@[j] as int, sticks@[k] as int),\n {\n let mut found = false;\n let mut i = 0usize;\n while i < 4\n invariant\n sticks.len() == 4,\n forall|ii: int| 0 <= ii < 4 ==> 1 <= #[trigger] sticks[ii] as int <= 100,\n 0 <= i <= 4,\n found == exists|ii: int, jj: int, kk: int|\n (ii < i as int && valid_triple(ii, jj, kk) && segment_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int)),\n decreases 4 - i,\n {\n let mut j = 0usize;\n while j < 4\n invariant\n sticks.len() == 4,\n forall|ii: int| 0 <= ii < 4 ==> 1 <= #[trigger] sticks[ii] as int <= 100,\n i < 4,\n 0 <= j <= 4,\n found == exists|ii: int, jj: int, kk: int|\n ((ii < i as int && valid_triple(ii, jj, kk)) || (ii == i as int && jj < j as int && valid_triple(ii, jj, kk))) &&\n segment_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int),\n decreases 4 - j,\n {\n let mut k = 0usize;\n while k < 4\n invariant\n sticks.len() == 4,\n forall|ii: int| 0 <= ii < 4 ==> 1 <= #[trigger] sticks[ii] as int <= 100,\n i < 4,\n j < 4,\n 0 <= k <= 4,\n found == exists|ii: int, jj: int, kk: int|\n ((ii < i as int && valid_triple(ii, jj, kk)) ||\n (ii == i as int && jj < j as int && valid_triple(ii, jj, kk)) ||\n (ii == i as int && jj == j as int && kk < k as int && valid_triple(ii, jj, kk))) &&\n segment_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int),\n decreases 4 - k,\n {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b == c || a + c == b || b + c == a {\n proof {\n assert(segment_inequality(sticks@[i as int] as int, sticks@[j as int] as int, sticks@[k as int] as int));\n assert(valid_triple(i as int, j as int, k as int));\n assert(exists|ii: int, jj: int, kk: int|\n valid_triple(ii, jj, kk)\n && segment_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int)) by {\n let ii = i as int;\n let jj = j as int;\n let kk = k as int;\n }\n }\n found = true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n proof {\n assert forall|ii: int, jj: int, kk: int|\n valid_triple(ii, jj, kk) implies\n (ii < 4 && jj < 4 && kk < 4) by {\n assert(0 <= ii < 4 && 0 <= jj < 4 && 0 <= kk < 4);\n }\n assert(i == 4);\n assert(found == exists|ii: int, jj: int, kk: int|\n (ii < 4 && valid_triple(ii, jj, kk) && segment_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int)));\n assert(found == exists|ii: int, jj: int, kk: int|\n valid_triple(ii, jj, kk) && segment_inequality(sticks@[ii] as int, sticks@[jj] as int, sticks@[kk] as int));\n }\n found\n }\n}\n\n}", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn has_triangle(sticks: Vec) -> bool {\n let mut found = false;\n let mut i = 0usize;\n while i < 4 {\n let mut j = 0usize;\n while j < 4 {\n let mut k = 0usize;\n while k < 4 {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b > c && a + c > b && b + c > a {\n found = true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n found\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let parts: Vec = line.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let sticks = vec![parts[0], parts[1], parts[2], parts[3]];\n let has_tri = Solution::has_triangle(sticks.clone());\n let has_seg = Solution::has_segment(sticks);\n if has_tri {\n println!(\"TRIANGLE\");\n } else if has_seg {\n println!(\"SEGMENT\");\n } else {\n println!(\"IMPOSSIBLE\");\n }\n}\n\nimpl Solution {\n pub fn has_segment(sticks: Vec) -> bool {\n let mut i = 0usize;\n while i < 4 {\n let mut j = 0usize;\n while j < 4 {\n let mut k = 0usize;\n while k < 4 {\n if i != j && i != k && j != k {\n let a = sticks[i] as i64;\n let b = sticks[j] as i64;\n let c = sticks[k] as i64;\n if a + b == c || a + c == b || b + c == a {\n return true;\n }\n }\n k += 1;\n }\n j += 1;\n }\n i += 1;\n }\n false\n }\n}", "checker": null, "testcases_path": "codeforces/cf6A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf6A/tests/mutated_testcases.jsonl"} {"problem": "cf716A", "family": "codeforces", "description": "# Crazy Computer\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nZS the Coder is coding on a crazy computer. If you don't type in a word for a $c$ consecutive seconds, everything you typed disappear! \n\nMore formally, if you typed a word at second $a$ and then the next word at second $b$, then if $b\u2009-\u2009a\u2009\u2264\u2009c$, just the new word is appended to other words on the screen. If $b\u2009-\u2009a\u2009>\u2009c$, then everything on the screen disappears and after that the word you have typed appears on the screen.\n\nFor example, if $c\u2009=\u20095$ and you typed words at seconds $1,\u20093,\u20098,\u200914,\u200919,\u200920$ then at the second $8$ there will be $3$ words on the screen. After that, everything disappears at the second $13$ because nothing was typed. At the seconds $14$ and $19$ another two words are typed, and finally, at the second $20$, one more word is typed, and a total of $3$ words remain on the screen.\n\nYou're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.\n\n## Input\n\nThe first line contains two integers $n$ and $c$ ($1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000,\u20091\u2009\u2264\u2009c\u2009\u2264\u200910^9$)\u00a0\u2014 the number of words ZS the Coder typed and the crazy computer delay respectively.\n\nThe next line contains $n$ integers $t_1,\u2009t_2,\u2009...,\u2009t_n$ ($1\u2009\u2264\u2009t_1\u2009<\u2009t_2\u2009<\u2009...\u2009<\u2009t_n\u2009\u2264\u200910^9$), where $t_i$ denotes the second when ZS the Coder typed the $i$-th word.\n\n## Output\n\nPrint a single positive integer, the number of words that remain on the screen after all $n$ words was typed, in other words, at the second $t_n$.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n6 5\n1 3 8 14 19 20\n```\n**Output:**\n```\n3\n```\n\n### Example 2\n\n**Input:**\n```\n6 1\n1 3 5 7 9 10\n```\n**Output:**\n```\n2\n```\n\n## Note\n\nThe first sample is already explained in the problem statement.\n\nFor the second sample, after typing the first word at the second $1$, it disappears because the next word is typed at the second $3$ and $3\u2009-\u20091\u2009>\u20091$. Similarly, only $1$ word will remain at the second $9$. Then, a word is typed at the second $10$, so there will be two words on the screen, as the old word won't disappear because $10\u2009-\u20099\u2009\u2264\u20091$.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn remaining_words(n: usize, c: i64, t: Vec) -> usize {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let c: i64 = it.next().unwrap().parse().unwrap();\n let mut t: Vec = Vec::new();\n let mut i = 0usize;\n while i < n {\n t.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::remaining_words(n, c, t);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 800\nimplementation\nSolved by: 23964 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_screen(\n t: Seq, n: int, c: int, i: int, cnt: int,\n) -> int\n decreases n - i,\n{\n if i >= n {\n cnt\n } else {\n let gap = t[i] - t[i - 1];\n let new_cnt = if gap <= c { cnt + 1 } else { 1 };\n spec_screen(t, n, c, i + 1, new_cnt)\n }\n}\n\npub open spec fn spec_answer(t: Seq, n: int, c: int) -> int {\n spec_screen(t, n, c, 1, 1)\n}\n\nimpl Solution {\n pub fn remaining_words(n: usize, c: i64, t: Vec) -> (res: usize)\n requires\n 1 <= n <= 100_000,\n n == t.len(),\n 1 <= c <= 1_000_000_000,\n forall|u: int| 0 <= u < n as int - 1 ==> #[trigger] t[u] < t[u + 1],\n forall|u: int| 0 <= u < n as int ==> 1 <= #[trigger] t[u] <= 1_000_000_000,\n ensures\n res as int == spec_answer(t@, n as int, c as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn remaining_words(n: usize, c: i64, t: Vec) -> usize {\n let mut cnt = 1usize;\n let mut i = 1usize;\n while i < n {\n if t[i] - t[i - 1] <= c {\n cnt = cnt + 1;\n } else {\n cnt = 1;\n }\n i = i + 1;\n }\n cnt\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_screen(\n t: Seq, n: int, c: int, i: int, cnt: int,\n) -> int\n decreases n - i,\n{\n if i >= n {\n cnt\n } else {\n let gap = t[i] - t[i - 1];\n let new_cnt = if gap <= c { cnt + 1 } else { 1 };\n spec_screen(t, n, c, i + 1, new_cnt)\n }\n}\n\npub open spec fn spec_answer(t: Seq, n: int, c: int) -> int {\n spec_screen(t, n, c, 1, 1)\n}\n\nimpl Solution {\n pub fn remaining_words(n: usize, c: i64, t: Vec) -> (res: usize)\n requires\n 1 <= n <= 100_000,\n n == t.len(),\n 1 <= c <= 1_000_000_000,\n forall|u: int| 0 <= u < n as int - 1 ==> #[trigger] t[u] < t[u + 1],\n forall|u: int| 0 <= u < n as int ==> 1 <= #[trigger] t[u] <= 1_000_000_000,\n ensures\n res as int == spec_answer(t@, n as int, c as int),\n {\n let mut cnt = 1usize;\n let mut i = 1usize;\n while i < n {\n if t[i] - t[i - 1] <= c {\n cnt = cnt + 1;\n } else {\n cnt = 1;\n }\n i = i + 1;\n }\n cnt\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_screen(\n t: Seq, n: int, c: int, i: int, cnt: int,\n) -> int\n decreases n - i,\n{\n if i >= n {\n cnt\n } else {\n let gap = t[i] - t[i - 1];\n let new_cnt = if gap <= c { cnt + 1 } else { 1 };\n spec_screen(t, n, c, i + 1, new_cnt)\n }\n}\n\npub open spec fn spec_answer(t: Seq, n: int, c: int) -> int {\n spec_screen(t, n, c, 1, 1)\n}\n\nimpl Solution {\n pub fn remaining_words(n: usize, c: i64, t: Vec) -> (res: usize)\n requires\n 1 <= n <= 100_000,\n n == t.len(),\n 1 <= c <= 1_000_000_000,\n forall|u: int| 0 <= u < n as int - 1 ==> #[trigger] t[u] < t[u + 1],\n forall|u: int| 0 <= u < n as int ==> 1 <= #[trigger] t[u] <= 1_000_000_000,\n ensures\n res as int == spec_answer(t@, n as int, c as int),\n {\n let mut cnt = 1usize;\n let mut i = 1usize;\n while i < n\n invariant\n n == t.len(),\n 1 <= n <= 100_000,\n 1 <= c <= 1_000_000_000,\n 1 <= i <= n,\n 1 <= (cnt as int) && (cnt as int) <= (i as int),\n forall|u: int| 0 <= u < n as int - 1 ==> #[trigger] t[u] < t[u + 1],\n forall|u: int| 0 <= u < n as int ==> 1 <= #[trigger] t[u] <= 1_000_000_000,\n spec_screen(t@, n as int, c as int, i as int, cnt as int)\n == spec_answer(t@, n as int, c as int),\n decreases n - i,\n {\n proof {\n assert(i < n);\n assert(t[i as int] > t[(i as int) - 1]);\n assert(0 <= (i as int) - 1 && (i as int) < n as int);\n }\n if t[i] - t[i - 1] <= c {\n proof {\n assert((cnt as int) + 1 <= (i as int) + 1);\n assert((i as int) + 1 <= (n as int));\n }\n cnt = cnt + 1;\n } else {\n cnt = 1;\n }\n proof {\n assert((cnt as int) <= (i as int) + 1);\n assert((i as int) + 1 <= (n as int));\n }\n i = i + 1;\n proof {\n assert((cnt as int) <= (i as int));\n }\n }\n proof {\n assert(spec_screen(t@, n as int, c as int, n as int, cnt as int) == cnt as int);\n }\n cnt\n }\n}\n\n}\n", "main": "use std::io::{self, Read};\n\nstruct Solution;\n\nimpl Solution {\n pub fn remaining_words(n: usize, c: i64, t: Vec) -> usize {\n let mut cnt = 1usize;\n let mut i = 1usize;\n while i < n {\n if t[i] - t[i - 1] <= c {\n cnt = cnt + 1;\n } else {\n cnt = 1;\n }\n i = i + 1;\n }\n cnt\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let c: i64 = it.next().unwrap().parse().unwrap();\n let mut t: Vec = Vec::new();\n let mut i = 0usize;\n while i < n {\n t.push(it.next().unwrap().parse().unwrap());\n i = i + 1;\n }\n let ans = Solution::remaining_words(n, c, t);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf716A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf716A/tests/mutated_testcases.jsonl"} {"problem": "cf758A", "family": "codeforces", "description": "# Holiday Of Equality\n\nTime limit: 1 second | Memory limit: 256 megabytes\n\nIn Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. \n\nTotally in Berland there are $n$ citizens, the welfare of each of them is estimated as the integer in $a_i$ burles (burle is the currency in Berland).\n\nYou are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them.\n\n## Input\n\nThe first line contains the integer $n$ ($1\u2009\u2264\u2009n\u2009\u2264\u2009100$)\u00a0\u2014 the number of citizens in the kingdom.\n\nThe second line contains $n$ integers $a_1,\u2009a_2,\u2009...,\u2009a_n$, where $a_i$ ($0\u2009\u2264\u2009a_i\u2009\u2264\u200910^6$)\u00a0\u2014 the welfare of the $i$-th citizen.\n\n## Output\n\nIn the only line print the integer $S$\u00a0\u2014 the minimum number of burles which are had to spend.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n5\n0 1 2 3 4\n```\n**Output:**\n```\n10\n```\n\n### Example 2\n\n**Input:**\n```\n5\n1 1 0 1 1\n```\n**Output:**\n```\n1\n```\n\n### Example 3\n\n**Input:**\n```\n3\n1 3 1\n```\n**Output:**\n```\n4\n```\n\n### Example 4\n\n**Input:**\n```\n1\n12\n```\n**Output:**\n```\n0\n```\n\n## Note\n\nIn the first example if we add to the first citizen $4$ burles, to the second $3$, to the third $2$ and to the fourth $1$, then the welfare of all citizens will equal $4$.\n\nIn the second example it is enough to give one burle to the third citizen. \n\nIn the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal $3$.\n\nIn the fourth example it is possible to give nothing to everyone because all citizens have $12$ burles.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn holiday_equality_burles(n: usize, a: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::holiday_equality_burles(n, a);\n println!(\"{}\", ans);\n}\n```\n", "tags": "Rating: 800\nimplementation, math\nSolved by: 74431 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_max_of(a: Seq, n: int) -> int\n recommends 1 <= n <= a.len(),\n decreases n,\n{\n if n <= 1 {\n a[0] as int\n } else {\n let m = spec_max_of(a, n - 1);\n if (a[n - 1] as int) > m {\n a[n - 1] as int\n } else {\n m\n }\n }\n}\n\npub open spec fn spec_gap_sum(a: Seq, n: int, maxv: int) -> int\n recommends 0 <= n <= a.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n spec_gap_sum(a, n - 1, maxv) + (maxv - (a[n - 1] as int))\n }\n}\n\npub open spec fn spec_holiday_equality(a: Seq, n: int) -> int\n recommends 1 <= n <= a.len(),\n{\n let m = spec_max_of(a, n);\n spec_gap_sum(a, n, m)\n}\n\nimpl Solution {\n pub fn holiday_equality_burles(n: usize, a: Vec) -> (res: i32)\n requires\n 1 <= n <= 100,\n n == a.len(),\n forall|k: int|\n 0 <= k < n as int ==> 0 <= #[trigger] a[k] <= 1_000_000,\n ensures\n res as int == spec_holiday_equality(a@, n as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn holiday_equality_burles(n: usize, a: Vec) -> i32 {\n let mut maxv = a[0];\n let mut i = 1usize;\n while i < n {\n if a[i] > maxv {\n maxv = a[i];\n }\n i += 1;\n }\n let mut sum = 0i32;\n let mut j = 0usize;\n while j < n {\n sum += maxv - a[j];\n j += 1;\n }\n sum\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_max_of(a: Seq, n: int) -> int\n recommends 1 <= n <= a.len(),\n decreases n,\n{\n if n <= 1 {\n a[0] as int\n } else {\n let m = spec_max_of(a, n - 1);\n if (a[n - 1] as int) > m {\n a[n - 1] as int\n } else {\n m\n }\n }\n}\n\npub open spec fn spec_gap_sum(a: Seq, n: int, maxv: int) -> int\n recommends 0 <= n <= a.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n spec_gap_sum(a, n - 1, maxv) + (maxv - (a[n - 1] as int))\n }\n}\n\npub open spec fn spec_holiday_equality(a: Seq, n: int) -> int\n recommends 1 <= n <= a.len(),\n{\n let m = spec_max_of(a, n);\n spec_gap_sum(a, n, m)\n}\n\nimpl Solution {\n pub fn holiday_equality_burles(n: usize, a: Vec) -> (res: i32)\n requires\n 1 <= n <= 100,\n n == a.len(),\n forall|k: int|\n 0 <= k < n as int ==> 0 <= #[trigger] a[k] <= 1_000_000,\n ensures\n res as int == spec_holiday_equality(a@, n as int),\n {\n let mut maxv = a[0];\n let mut i = 1usize;\n while i < n {\n if a[i] > maxv {\n maxv = a[i];\n }\n i += 1;\n }\n let mut sum = 0i32;\n let mut j = 0usize;\n while j < n {\n sum += maxv - a[j];\n j += 1;\n }\n sum\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_max_of(a: Seq, n: int) -> int\n recommends 1 <= n <= a.len(),\n decreases n,\n{\n if n <= 1 {\n a[0] as int\n } else {\n let m = spec_max_of(a, n - 1);\n if (a[n - 1] as int) > m {\n a[n - 1] as int\n } else {\n m\n }\n }\n}\n\npub open spec fn spec_gap_sum(a: Seq, n: int, maxv: int) -> int\n recommends 0 <= n <= a.len(),\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n spec_gap_sum(a, n - 1, maxv) + (maxv - (a[n - 1] as int))\n }\n}\n\npub open spec fn spec_holiday_equality(a: Seq, n: int) -> int\n recommends 1 <= n <= a.len(),\n{\n let m = spec_max_of(a, n);\n spec_gap_sum(a, n, m)\n}\n\nproof fn lemma_spec_max_of_le_upper(a: Seq, n: int, cap: int)\n requires\n 1 <= n <= a.len(),\n forall|t: int| 0 <= t < n ==> (a[t] as int) <= cap,\n ensures\n spec_max_of(a, n) <= cap,\n decreases n,\n{\n if n <= 1 {\n reveal_with_fuel(spec_max_of, 2);\n assert(spec_max_of(a, 1) == a[0] as int);\n assert((a[0] as int) <= cap);\n } else {\n lemma_spec_max_of_le_upper(a, n - 1, cap);\n reveal_with_fuel(spec_max_of, 3);\n assert((a[n - 1] as int) <= cap);\n }\n}\n\nproof fn lemma_spec_max_of_next(a: Seq, i: int)\n requires\n 1 <= i < a.len(),\n ensures\n spec_max_of(a, i + 1) == if (a[i] as int) > spec_max_of(a, i) {\n a[i] as int\n } else {\n spec_max_of(a, i)\n },\n{\n reveal_with_fuel(spec_max_of, 20);\n}\n\nproof fn lemma_gap_sum_step(a: Seq, j: int, maxv: int)\n requires\n 0 <= j < a.len(),\n ensures\n spec_gap_sum(a, j + 1, maxv) == spec_gap_sum(a, j, maxv) + (maxv - (a[j] as int)),\n{\n reveal_with_fuel(spec_gap_sum, 20);\n}\n\nproof fn lemma_spec_max_ge_each(a: Seq, n: int, k: int)\n requires\n 1 <= n <= a.len(),\n 0 <= k < n,\n ensures\n spec_max_of(a, n) >= a[k] as int,\n decreases n,\n{\n if n <= 1 {\n assert(k == 0);\n reveal_with_fuel(spec_max_of, 2);\n assert(spec_max_of(a, 1) == a[0] as int);\n } else {\n reveal_with_fuel(spec_max_of, 3);\n if k == n - 1 {\n assert(spec_max_of(a, n) >= a[k] as int);\n } else {\n lemma_spec_max_ge_each(a, n - 1, k);\n assert(spec_max_of(a, n) >= spec_max_of(a, n - 1));\n assert(spec_max_of(a, n - 1) >= a[k] as int);\n }\n }\n}\n\nproof fn lemma_gap_sum_nonneg(a: Seq, n: int, maxv: int)\n requires\n 0 <= n <= a.len(),\n forall|i: int| 0 <= i < n ==> (a[i] as int) <= maxv,\n ensures\n spec_gap_sum(a, n, maxv) >= 0,\n decreases n,\n{\n if n <= 0 {\n reveal_with_fuel(spec_gap_sum, 1);\n } else {\n lemma_gap_sum_nonneg(a, n - 1, maxv);\n assert((a[n - 1] as int) <= maxv);\n reveal_with_fuel(spec_gap_sum, 2);\n }\n}\n\nproof fn lemma_gap_sum_ge_term(a: Seq, n: int, m: int, k: int)\n requires\n 1 <= n <= a.len(),\n 0 <= k < n,\n forall|i: int| 0 <= i < n ==> (a[i] as int) <= m,\n ensures\n spec_gap_sum(a, n, m) >= m - (a[k] as int),\n decreases n,\n{\n if n == 1 {\n assert(k == 0);\n reveal_with_fuel(spec_gap_sum, 2);\n assert(spec_gap_sum(a, 1, m) == m - (a[0] as int));\n } else {\n reveal_with_fuel(spec_gap_sum, 3);\n if k == n - 1 {\n lemma_gap_sum_nonneg(a, n - 1, m);\n assert(spec_gap_sum(a, n, m) == spec_gap_sum(a, n - 1, m) + (m - (a[n - 1] as int)));\n assert(spec_gap_sum(a, n - 1, m) >= 0);\n assert(spec_gap_sum(a, n, m) >= m - (a[k] as int));\n } else {\n lemma_gap_sum_ge_term(a, n - 1, m, k);\n assert((a[n - 1] as int) <= m);\n lemma_gap_sum_nonneg(a, n - 1, m);\n assert(spec_gap_sum(a, n, m) >= spec_gap_sum(a, n - 1, m));\n assert(spec_gap_sum(a, n - 1, m) >= m - (a[k] as int));\n }\n }\n}\n\nimpl Solution {\n pub fn holiday_equality_burles(n: usize, a: Vec) -> (res: i32)\n requires\n 1 <= n <= 100,\n n == a.len(),\n forall|k: int|\n 0 <= k < n as int ==> 0 <= #[trigger] a[k] <= 1_000_000,\n ensures\n res as int == spec_holiday_equality(a@, n as int),\n {\n let mut maxv = a[0];\n let mut i = 1usize;\n while i < n\n invariant\n n == a.len(),\n 1 <= n <= 100,\n forall|k: int|\n 0 <= k < n as int ==> 0 <= #[trigger] a[k] <= 1_000_000,\n 1 <= i <= n,\n maxv as int == spec_max_of(a@, i as int),\n decreases\n n - i,\n {\n proof {\n lemma_spec_max_of_next(a@, i as int);\n }\n if a[i] > maxv {\n maxv = a[i];\n }\n i += 1;\n proof {\n assert(maxv as int == spec_max_of(a@, i as int));\n }\n }\n proof {\n assert(i == n);\n assert(maxv as int == spec_max_of(a@, n as int));\n }\n let mut sum = 0i32;\n let mut j = 0usize;\n while j < n\n invariant\n n == a.len(),\n 1 <= n <= 100,\n forall|k: int|\n 0 <= k < n as int ==> 0 <= #[trigger] a[k] <= 1_000_000,\n j <= n,\n maxv as int == spec_max_of(a@, n as int),\n sum as int == spec_gap_sum(a@, j as int, maxv as int),\n sum as int <= (j as int) * 1_000_000,\n decreases\n n - j,\n {\n proof {\n lemma_gap_sum_step(a@, j as int, maxv as int);\n assert(forall|t: int|\n 0 <= t < n as int ==> (a@[t] as int) <= maxv as int) by {\n assert(maxv as int == spec_max_of(a@, n as int));\n assert forall|t: int|\n 0 <= t < n as int implies (a@[t] as int) <= maxv as int by {\n lemma_spec_max_ge_each(a@, n as int, t);\n };\n };\n lemma_spec_max_ge_each(a@, n as int, j as int);\n assert(maxv as int >= (a@[j as int] as int));\n lemma_spec_max_of_le_upper(a@, n as int, 1_000_000);\n assert(maxv as int <= 1_000_000);\n assert((a@[j as int] as int) >= 0);\n assert((maxv as int - (a@[j as int] as int)) <= 1_000_000);\n assert((sum as int) + (maxv as int - (a@[j as int] as int))\n <= ((j + 1) as int) * 1_000_000);\n assert(((j + 1) as int) * 1_000_000 <= 100 * 1_000_000);\n assert((sum as int) + (maxv as int - (a@[j as int] as int)) <= 100 * 1_000_000);\n assert(-2147483648 <= (sum as int) + (maxv as int - (a@[j as int] as int)));\n assert((sum as int) + (maxv as int - (a@[j as int] as int)) < 2147483648);\n }\n sum += maxv - a[j];\n j += 1;\n proof {\n assert(sum as int == spec_gap_sum(a@, j as int, maxv as int));\n }\n }\n proof {\n assert(j == n);\n assert(sum as int == spec_gap_sum(a@, n as int, maxv as int));\n assert(spec_holiday_equality(a@, n as int) == spec_gap_sum(a@, n as int, maxv as int));\n assert(sum as int == spec_holiday_equality(a@, n as int));\n assert forall|k: int|\n 0 <= k < n as int implies spec_max_of(a@, n as int) >= a@[k] by {\n lemma_spec_max_ge_each(a@, n as int, k);\n };\n assert forall|k: int|\n 0 <= k < n as int implies (sum as int) >= (spec_max_of(a@, n as int) - a@[k]) by {\n lemma_gap_sum_ge_term(a@, n as int, maxv as int, k);\n assert(maxv as int == spec_max_of(a@, n as int));\n };\n }\n sum\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn holiday_equality_burles(n: usize, a: Vec) -> i32 {\n let mut maxv = a[0];\n let mut i = 1usize;\n while i < n {\n if a[i] > maxv {\n maxv = a[i];\n }\n i += 1;\n }\n let mut sum = 0i32;\n let mut j = 0usize;\n while j < n {\n sum += maxv - a[j];\n j += 1;\n }\n sum\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let ans = Solution::holiday_equality_burles(n, a);\n println!(\"{}\", ans);\n}\n", "checker": null, "testcases_path": "codeforces/cf758A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf758A/tests/mutated_testcases.jsonl"} {"problem": "cf80A", "family": "codeforces", "description": "# Panoramix's Prediction\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\n*A prime number is a number which has exactly two distinct divisors: one and itself. For example, numbers $2*, 7$, $3$ are prime, and $1$, $6$, $4$ are not.\n\n*The next prime number after $x* is the **smallest** prime number greater than x$. For example, the next prime number after $2$ is $3$, and the next prime number after $3$ is $5$. Note that there is exactly one next prime number after each number. So $5$ **is\u00a0not** the next prime number for $2$.\n\nOne cold April morning Panoramix predicted that soon Kakofonix will break free from his straitjacket, and this will be a black day for the residents of the Gallic countryside.\n\nPanoramix's prophecy tells that if some day Asterix and Obelix beat exactly $x$ Roman soldiers, where $x$ is a prime number, and next day they beat exactly $y$ Roman soldiers, where $y$ is **the next prime number** after $x$, then it's time to wait for Armageddon, for nothing can shut Kakofonix up while he sings his infernal song.\n\nYesterday the Gauls beat $n$ Roman soldiers and it turned out that the number $n$ was prime! Today their victims were a troop of $m$ Romans ($m\u2009>\u2009n$). Determine whether the Gauls should wait for the black day after today's victory of Asterix and Obelix?\n\n## Input\n\nThe first and only input line contains two positive integers \u2014 $n$ and $m$ ($2\u2009\u2264\u2009n\u2009<\u2009m\u2009\u2264\u200950$). It is guaranteed that $n$ is prime.\n\nPretests contain all the cases with restrictions $2\u2009\u2264\u2009n\u2009<\u2009m\u2009\u2264\u20094$.\n\n## Output\n\nPrint `YES`, if $m$ is the next prime number after $n$, or `NO` otherwise.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n3 5\n```\n**Output:**\n```\nYES\n```\n\n### Example 2\n\n**Input:**\n```\n7 11\n```\n**Output:**\n```\nYES\n```\n\n### Example 3\n\n**Input:**\n```\n7 9\n```\n**Output:**\n```\nNO\n```\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_next_prime(n: u32, m: u32) -> bool {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: u32 = iter.next().unwrap().parse().unwrap();\n let m: u32 = iter.next().unwrap().parse().unwrap();\n writeln!(\n out,\n \"{}\",\n if Solution::is_next_prime(n, m) { \"YES\" } else { \"NO\" }\n )\n .unwrap();\n}\n```\n", "tags": "Rating: 800\nbrute force\nSolved by: 66159 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn divides(d: int, n: int) -> bool {\n d > 0 && n > 0 && n % d == 0\n}\n\npub open spec fn is_prime(n: int) -> bool {\n n >= 2 && (forall|d: int| 2 <= d < n ==> !divides(d, n))\n}\n\nimpl Solution {\n pub fn is_next_prime(n: u32, m: u32) -> (result: bool)\n requires\n 2 <= n < m <= 50,\n is_prime(n as int),\n ensures\n result == (is_prime(m as int) && (forall|k: int| n < k < m ==> !is_prime(k))),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_next_prime(n: u32, m: u32) -> bool {\n let mut x: u32 = n + 1;\n while x <= m {\n let mut prime: bool = true;\n let mut d: u32 = 2;\n while d < x {\n if x % d == 0 {\n prime = false;\n }\n d = d + 1;\n }\n if prime {\n if x == m {\n return true;\n } else {\n return false;\n }\n }\n x = x + 1;\n }\n false\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn divides(d: int, n: int) -> bool {\n d > 0 && n > 0 && n % d == 0\n}\n\npub open spec fn is_prime(n: int) -> bool {\n n >= 2 && (forall|d: int| 2 <= d < n ==> !divides(d, n))\n}\n\nimpl Solution {\n pub fn is_next_prime(n: u32, m: u32) -> (result: bool)\n requires\n 2 <= n < m <= 50,\n is_prime(n as int),\n ensures\n result == (is_prime(m as int) && (forall|k: int| n < k < m ==> !is_prime(k))),\n {\n let mut x: u32 = n + 1;\n while x <= m {\n let mut prime: bool = true;\n let mut d: u32 = 2;\n while d < x {\n if x % d == 0 {\n prime = false;\n }\n d = d + 1;\n }\n if prime {\n if x == m {\n return true;\n } else {\n return false;\n }\n }\n x = x + 1;\n }\n false\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn divides(d: int, n: int) -> bool {\n d > 0 && n > 0 && n % d == 0\n}\n\npub open spec fn is_prime(n: int) -> bool {\n n >= 2 && (forall|d: int| 2 <= d < n ==> !divides(d, n))\n}\n\nimpl Solution {\n pub fn is_next_prime(n: u32, m: u32) -> (result: bool)\n requires\n 2 <= n < m <= 50,\n is_prime(n as int),\n ensures\n result == (is_prime(m as int) && (forall|k: int| n < k < m ==> !is_prime(k))),\n {\n let mut x: u32 = n + 1;\n while x <= m\n invariant\n n + 1 <= x <= m + 1,\n 2 <= n < m <= 50,\n forall|k: int| n < k < x ==> !is_prime(k),\n decreases m + 1 - x,\n {\n let mut prime: bool = true;\n let mut d: u32 = 2;\n while d < x\n invariant\n 2 <= d <= x,\n x >= 2,\n prime == (forall|dd: int| 2 <= dd < d ==> !divides(dd, x as int)),\n decreases x - d,\n {\n if x % d == 0 {\n assert(divides(d as int, x as int));\n prime = false;\n }\n d = d + 1;\n }\n if prime {\n assert(is_prime(x as int));\n if x == m {\n return true;\n } else {\n \n assert(x < m);\n assert(is_prime(x as int));\n assert(n < x as int && (x as int) < m as int);\n return false;\n }\n }\n assert(!prime);\n \n assert(!is_prime(x as int)) by {\n if is_prime(x as int) {\n assert(forall|dd: int| 2 <= dd < x as int ==> !divides(dd, x as int));\n }\n };\n x = x + 1;\n }\n \n \n false\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn is_next_prime(n: u32, m: u32) -> bool {\n let mut x: u32 = n + 1;\n while x <= m {\n let mut prime: bool = true;\n let mut d: u32 = 2;\n while d < x {\n if x % d == 0 {\n prime = false;\n }\n d = d + 1;\n }\n if prime {\n if x == m {\n return true;\n } else {\n return false;\n }\n }\n x = x + 1;\n }\n false\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: u32 = iter.next().unwrap().parse().unwrap();\n let m: u32 = iter.next().unwrap().parse().unwrap();\n writeln!(\n out,\n \"{}\",\n if Solution::is_next_prime(n, m) { \"YES\" } else { \"NO\" }\n )\n .unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf80A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf80A/tests/mutated_testcases.jsonl"} {"problem": "cf92A", "family": "codeforces", "description": "# Chips\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nThere are $n$ walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number $2$ sits to the left of the walrus number $1$, the walrus number $3$ sits to the left of the walrus number $2$, ..., the walrus number $1$ sits to the left of the walrus number $n$.\n\nThe presenter has $m$ chips. The presenter stands in the middle of the circle and starts giving the chips to the walruses starting from walrus number $1$ and moving clockwise. The walrus number $i$ gets $i$ chips. If the presenter can't give the current walrus the required number of chips, then the presenter takes the remaining chips and the process ends. Determine by the given $n$ and $m$ how many chips the presenter will get in the end.\n\n## Input\n\nThe first line contains two integers $n$ and $m$ ($1\u2009\u2264\u2009n\u2009\u2264\u200950$, $1\u2009\u2264\u2009m\u2009\u2264\u200910^4$) \u2014 the number of walruses and the number of chips correspondingly.\n\n## Output\n\nPrint the number of chips the presenter ended up with.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n4 11\n```\n**Output:**\n```\n0\n```\n\n### Example 2\n\n**Input:**\n```\n17 107\n```\n**Output:**\n```\n2\n```\n\n### Example 3\n\n**Input:**\n```\n3 8\n```\n**Output:**\n```\n1\n```\n\n## Note\n\nIn the first sample the presenter gives one chip to the walrus number $1$, two chips to the walrus number $2$, three chips to the walrus number $3$, four chips to the walrus number $4$, then again one chip to the walrus number $1$. After that the presenter runs out of chips. He can't give anything to the walrus number $2$ and the process finishes.\n\nIn the third sample the presenter gives one chip to the walrus number $1$, two chips to the walrus number $2$, three chips to the walrus number $3$, then again one chip to the walrus number $1$. The presenter has one chip left and he can't give two chips to the walrus number $2$, that's why the presenter takes the last chip.\n\n## Starter Code\n\n```rust\nuse std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn presenter_chips(n: u32, m: u32) -> u32 {\n \n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: u32 = iter.next().unwrap().parse().unwrap();\n let m: u32 = iter.next().unwrap().parse().unwrap();\n writeln!(out, \"{}\", Solution::presenter_chips(n, m)).unwrap();\n}\n```\n", "tags": "Rating: 800\nimplementation, math\nSolved by: 21504 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn round_sum(n: int) -> int { n * (n + 1) / 2 }\n\n\n\n\n\npub open spec fn after_full_rounds(n: int, m: int) -> int {\n if round_sum(n) > 0 { m % round_sum(n) } else { m }\n}\n\npub open spec fn final_remainder(n: int, rem: int, walrus: int) -> int\n decreases (n + 1) - walrus,\n{\n if walrus < 1 || walrus > n || rem < walrus {\n rem\n } else {\n final_remainder(n, rem - walrus, walrus + 1)\n }\n}\n\nimpl Solution {\n pub fn presenter_chips(n: u32, m: u32) -> (result: u32)\n requires\n 1 <= n <= 50,\n 1 <= m <= 10000,\n ensures\n result as int == final_remainder(n as int, after_full_rounds(n as int, m as int), 1),\n result < n,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn presenter_chips(n: u32, m: u32) -> u32 {\n let r_sum: u32 = n * (n + 1) / 2;\n let mut remaining: u32 = m % r_sum;\n let mut walrus: u32 = 1;\n while walrus <= n && remaining >= walrus {\n remaining = remaining - walrus;\n walrus = walrus + 1;\n }\n remaining\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn round_sum(n: int) -> int { n * (n + 1) / 2 }\n\npub open spec fn after_full_rounds(n: int, m: int) -> int {\n if round_sum(n) > 0 { m % round_sum(n) } else { m }\n}\n\npub open spec fn final_remainder(n: int, rem: int, walrus: int) -> int\n decreases (n + 1) - walrus,\n{\n if walrus < 1 || walrus > n || rem < walrus {\n rem\n } else {\n final_remainder(n, rem - walrus, walrus + 1)\n }\n}\n\nimpl Solution {\n pub fn presenter_chips(n: u32, m: u32) -> (result: u32)\n requires\n 1 <= n <= 50,\n 1 <= m <= 10000,\n ensures\n result as int == final_remainder(n as int, after_full_rounds(n as int, m as int), 1),\n result < n,\n {\n let r_sum: u32 = n * (n + 1) / 2;\n let mut remaining: u32 = m % r_sum;\n let mut walrus: u32 = 1;\n while walrus <= n && remaining >= walrus {\n remaining = remaining - walrus;\n walrus = walrus + 1;\n }\n remaining\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn round_sum(n: int) -> int { n * (n + 1) / 2 }\n\npub open spec fn after_full_rounds(n: int, m: int) -> int {\n if round_sum(n) > 0 { m % round_sum(n) } else { m }\n}\n\npub open spec fn final_remainder(n: int, rem: int, walrus: int) -> int\n decreases (n + 1) - walrus,\n{\n if walrus < 1 || walrus > n || rem < walrus {\n rem\n } else {\n final_remainder(n, rem - walrus, walrus + 1)\n }\n}\n\nproof fn lemma_final_walrus_nonneg(n: int, rem: int, walrus: int)\n requires\n 1 <= n,\n 0 <= rem,\n 1 <= walrus,\n ensures\n final_remainder(n, rem, walrus) >= 0,\n decreases (n + 1) - walrus,\n{\n if walrus < 1 || walrus > n || rem < walrus {\n } else {\n lemma_final_walrus_nonneg(n, rem - walrus, walrus + 1);\n }\n}\n\nproof fn lemma_round_sum_pos(n: int)\n requires\n 1 <= n,\n ensures\n round_sum(n) >= 1,\n{\n assert(n * (n + 1) / 2 >= 1) by (nonlinear_arith) requires n >= 1;\n}\n\nimpl Solution {\n pub fn presenter_chips(n: u32, m: u32) -> (result: u32)\n requires\n 1 <= n <= 50,\n 1 <= m <= 10000,\n ensures\n result as int == final_remainder(n as int, after_full_rounds(n as int, m as int), 1),\n result < n,\n {\n proof {\n lemma_round_sum_pos(n as int);\n assert(n * (n + 1) / 2 <= 50 * 51 / 2) by (nonlinear_arith) requires n <= 50u32;\n }\n let r_sum: u32 = n * (n + 1) / 2;\n let mut remaining: u32 = m % r_sum;\n let ghost initial_remaining: u32 = remaining;\n let mut walrus: u32 = 1;\n let ghost mut subtracted: u32 = 0;\n proof {\n assert(remaining as int == after_full_rounds(n as int, m as int));\n assert(remaining < r_sum);\n }\n while walrus <= n && remaining >= walrus\n invariant\n 1 <= walrus <= n + 1,\n walrus <= 51,\n 1 <= n <= 50,\n remaining < r_sum,\n r_sum == n * (n + 1) / 2,\n subtracted as int == (walrus as int - 1) * walrus as int / 2,\n remaining as int + subtracted as int == initial_remaining as int,\n initial_remaining == m % r_sum,\n final_remainder(n as int, after_full_rounds(n as int, m as int), 1) ==\n final_remainder(n as int, remaining as int, walrus as int),\n decreases (n + 1) - walrus,\n {\n remaining = remaining - walrus;\n proof { subtracted = (subtracted + walrus) as u32; }\n walrus = walrus + 1;\n proof {\n assert(subtracted as int == (walrus as int - 1) * walrus as int / 2) by (nonlinear_arith)\n requires\n subtracted as int == (walrus as int - 2) * (walrus as int - 1) / 2 + (walrus as int - 1),\n walrus >= 2;\n }\n }\n proof {\n assert(remaining as int == final_remainder(n as int, remaining as int, walrus as int));\n \n \n \n \n \n if walrus > n as u32 {\n assert(walrus == n + 1);\n assert(subtracted as int == n as int * (n as int + 1) / 2);\n assert(subtracted == r_sum);\n assert(remaining as int + r_sum as int == initial_remaining as int);\n assert(initial_remaining < r_sum);\n assert(false);\n }\n assert(walrus <= n);\n assert(remaining < walrus);\n assert(remaining < n);\n }\n remaining\n }\n}\n\n}\n", "main": "use std::io::{self, Read, Write, BufWriter};\n\nstruct Solution;\n\nimpl Solution {\n pub fn presenter_chips(n: u32, m: u32) -> u32 {\n let r_sum: u32 = n * (n + 1) / 2;\n let mut remaining: u32 = m % r_sum;\n let mut walrus: u32 = 1;\n while walrus <= n && remaining >= walrus {\n remaining = remaining - walrus;\n walrus = walrus + 1;\n }\n remaining\n }\n}\n\nfn main() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let stdout = io::stdout();\n let mut out = BufWriter::new(stdout.lock());\n let mut iter = input.split_ascii_whitespace();\n let n: u32 = iter.next().unwrap().parse().unwrap();\n let m: u32 = iter.next().unwrap().parse().unwrap();\n writeln!(out, \"{}\", Solution::presenter_chips(n, m)).unwrap();\n}\n", "checker": null, "testcases_path": "codeforces/cf92A/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf92A/tests/mutated_testcases.jsonl"} {"problem": "cf935B", "family": "codeforces", "description": "# Fafa and the Gates\n\nTime limit: 2 seconds | Memory limit: 256 megabytes\n\nTwo neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.\n\nThe world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation $x\u2009=\u2009y$). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points $(0,\u20090)$, $(1,\u20091)$, $(2,\u20092)$, ...). The wall and the gates do not belong to any of the kingdoms. \n\nFafa is at the gate at position $(0,\u20090)$ and he wants to walk around in the two kingdoms. He knows the sequence $S$ of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are '`U`' (move one step up, from $(x,\u2009y)$ to $(x,\u2009y\u2009+\u20091)$) and '`R`' (move one step right, from $(x,\u2009y)$ to $(x\u2009+\u20091,\u2009y)$). \n\nFafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence $S$. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point $(0,\u20090)$, i.\u00a0e. he is initially on the side he needs.\n\n## Input\n\nThe first line of the input contains single integer $n$ $(1\u2009\u2264\u2009n\u2009\u2264\u200910^5)$ \u2014 the number of moves in the walking sequence.\n\nThe second line contains a string $S$ of length $n$ consisting of the characters '`U`' and '`R`' describing the required moves. Fafa will follow the sequence $S$ in order from left to right.\n\n## Output\n\nOn a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence $S$.\n\n## Examples\n\n### Example 1\n\n**Input:**\n```\n1\nU\n```\n**Output:**\n```\n0\n```\n\n### Example 2\n\n**Input:**\n```\n6\nRURUUR\n```\n**Output:**\n```\n1\n```\n\n### Example 3\n\n**Input:**\n```\n7\nURRRUUU\n```\n**Output:**\n```\n2\n```\n\n## Note\n\nThe figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins.\n\n## Starter Code\n\n```rust\nuse std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn fafa_and_gates(n: usize, s: Vec) -> i32 {\n \n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(line1)) = lines.next() {\n if let Ok(n) = line1.trim().parse::() {\n if let Some(Ok(line2)) = lines.next() {\n let moves: Vec = line2\n .trim()\n .chars()\n .map(|c| if c == 'R' { 1 } else { 0 })\n .collect();\n let ans = Solution::fafa_and_gates(n, moves);\n println!(\"{}\", ans);\n }\n }\n }\n}\n```\n", "tags": "Rating: 900\nimplementation\nSolved by: 12339 users\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn x_pos(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else { x_pos(s, k - 1) + if s[k - 1] == 1 { 1int } else { 0int } }\n}\n\npub open spec fn y_pos(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else { y_pos(s, k - 1) + if s[k - 1] == 0 { 1int } else { 0int } }\n}\n\npub open spec fn is_crossing(s: Seq, k: int) -> bool {\n 0 < k && k < s.len() && x_pos(s, k) == y_pos(s, k) && s[k - 1] == s[k]\n}\n\npub open spec fn num_crossings(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else {\n num_crossings(s, k - 1) + if is_crossing(s, k - 1) { 1int } else { 0int }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn fafa_and_gates(n: usize, s: Vec) -> (result: i32)\n requires\n n == s.len(),\n n > 0,\n n <= 100000,\n forall|j: int| 0 <= j && j < n ==> s@[j] == 0 || s@[j] == 1,\n ensures\n result as int == num_crossings(s@, n as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn fafa_and_gates(n: usize, s: Vec) -> i32 {\n let mut x = 0;\n let mut y = 0;\n let mut coins = 0;\n let mut i = 0;\n\n while i < n {\n let nxt = s[i];\n if i > 0 && x == y && s[i - 1] == nxt {\n coins += 1;\n }\n if nxt == 1 {\n x += 1;\n } else {\n y += 1;\n }\n i += 1;\n }\n\n coins\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn x_pos(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else { x_pos(s, k - 1) + if s[k - 1] == 1 { 1int } else { 0int } }\n}\n\npub open spec fn y_pos(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else { y_pos(s, k - 1) + if s[k - 1] == 0 { 1int } else { 0int } }\n}\n\npub open spec fn is_crossing(s: Seq, k: int) -> bool {\n 0 < k && k < s.len() && x_pos(s, k) == y_pos(s, k) && s[k - 1] == s[k]\n}\n\npub open spec fn num_crossings(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else {\n num_crossings(s, k - 1) + if is_crossing(s, k - 1) { 1int } else { 0int }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn fafa_and_gates(n: usize, s: Vec) -> (result: i32)\n requires\n n == s.len(),\n n > 0,\n n <= 100000,\n forall|j: int| 0 <= j && j < n ==> s@[j] == 0 || s@[j] == 1,\n ensures\n result as int == num_crossings(s@, n as int),\n {\n let mut x = 0;\n let mut y = 0;\n let mut coins = 0;\n let mut i = 0;\n\n while i < n {\n let nxt = s[i];\n if i > 0 && x == y && s[i - 1] == nxt {\n coins += 1;\n }\n if nxt == 1 {\n x += 1;\n } else {\n y += 1;\n }\n i += 1;\n }\n\n coins\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn x_pos(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else { x_pos(s, k - 1) + if s[k - 1] == 1 { 1int } else { 0int } }\n}\n\npub open spec fn y_pos(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else { y_pos(s, k - 1) + if s[k - 1] == 0 { 1int } else { 0int } }\n}\n\npub open spec fn is_crossing(s: Seq, k: int) -> bool {\n 0 < k && k < s.len() && x_pos(s, k) == y_pos(s, k) && s[k - 1] == s[k]\n}\n\npub open spec fn num_crossings(s: Seq, k: int) -> int\n decreases k\n{\n if k <= 0 { 0int }\n else {\n num_crossings(s, k - 1) + if is_crossing(s, k - 1) { 1int } else { 0int }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn fafa_and_gates(n: usize, s: Vec) -> (result: i32)\n requires\n n == s.len(),\n n > 0,\n n <= 100000,\n forall|j: int| 0 <= j && j < n ==> s@[j] == 0 || s@[j] == 1,\n ensures\n result as int == num_crossings(s@, n as int),\n {\n let mut x = 0;\n let mut y = 0;\n let mut coins = 0;\n let mut i = 0;\n\n while i < n\n invariant\n 0 <= i && i <= n,\n n <= 100000,\n n == s.len(),\n 0 <= x <= i as i32,\n 0 <= y <= i as i32,\n 0 <= coins <= i as i32,\n x == x_pos(s@, i as int),\n y == y_pos(s@, i as int),\n coins == num_crossings(s@, i as int),\n forall|j: int| 0 <= j && j < n ==> s@[j] == 0 || s@[j] == 1,\n decreases n - i\n {\n let nxt = s[i];\n \n if i > 0 && x == y && s[i - 1] == nxt {\n coins += 1;\n }\n if nxt == 1 {\n x += 1;\n } else {\n y += 1;\n }\n i += 1;\n }\n\n coins\n }\n}\n\n}\n", "main": "use std::io::{self, BufRead};\n\nstruct Solution;\n\nimpl Solution {\n pub fn fafa_and_gates(n: usize, s: Vec) -> i32 {\n let mut x = 0;\n let mut y = 0;\n let mut coins = 0;\n let mut i = 0;\n\n while i < n {\n let nxt = s[i];\n if i > 0 && x == y && s[i - 1] == nxt {\n coins += 1;\n }\n if nxt == 1 {\n x += 1;\n } else {\n y += 1;\n }\n i += 1;\n }\n\n coins\n }\n}\n\nfn main() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n if let Some(Ok(line1)) = lines.next() {\n if let Ok(n) = line1.trim().parse::() {\n if let Some(Ok(line2)) = lines.next() {\n let moves: Vec = line2\n .trim()\n .chars()\n .map(|c| if c == 'R' { 1 } else { 0 })\n .collect();\n let ans = Solution::fafa_and_gates(n, moves);\n println!(\"{}\", ans);\n }\n }\n }\n}\n", "checker": null, "testcases_path": "codeforces/cf935B/tests/testcases.jsonl", "mutated_testcases_path": "codeforces/cf935B/tests/mutated_testcases.jsonl"} {"problem": "lc1004", "family": "leetcode", "description": "# Max Consecutive Ones III\n\nGiven a binary array `nums` and an integer `k`, return the maximum number of consecutive `1`'s in the array if you can flip at most `k` `0`'s.\n\n## Example 1:\n\n> **Input:** nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2\n> **Output:** 6\n> **Explanation:** The underlined subarray `[1,1,1,0,0,1,1,1,1,1,1]` (with 2 zeros flipped) has the maximum length of 6.\n\n## Example 2:\n\n> **Input:** nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3\n> **Output:** 10\n> **Explanation:** The underlined subarray `[1,1,1,1,1,1,1,1,1,1]` (with 3 zeros flipped) has the maximum length of 10.\n\n## Constraints:\n\n- $1 \\leq$ `nums.length` $\\leq 10^5$\n- `nums[i]` is either `0` or `1`.\n- $0 \\leq$ `k` $\\leq$ `nums.length`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_ones(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Binary Search, Sliding Window, Prefix Sum\nAcceptance rate: 67.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_zeros(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n (if s[l] == 0 { 1int } else { 0int }) + Self::count_zeros(s, l + 1, r)\n }\n }\n\n pub fn longest_ones(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> nums[i] == 0 || nums[i] == 1,\n 0 <= k <= nums.len(),\n ensures\n 0 <= result <= nums.len(),\n result == 0 || exists|l: int, r: int|\n 0 <= l && r == l + result as int && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int,\n forall|l: int, r: int|\n 0 <= l && l <= r && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int ==> r - l\n <= result as int,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn longest_ones(nums: Vec, k: i32) -> i32 {\n let n = nums.len();\n let mut left: usize = 0;\n let mut zeros: i32 = 0;\n let mut result: i32 = 0;\n let mut right: usize = 0;\n\n while right < n {\n if nums[right] == 0 {\n zeros = zeros + 1;\n }\n right = right + 1;\n\n while zeros > k {\n if nums[left] == 0 {\n zeros = zeros - 1;\n }\n left = left + 1;\n }\n\n let window = (right - left) as i32;\n if window > result {\n result = window;\n }\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_zeros(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n (if s[l] == 0 { 1int } else { 0int }) + Self::count_zeros(s, l + 1, r)\n }\n }\n\n pub fn longest_ones(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> nums[i] == 0 || nums[i] == 1,\n 0 <= k <= nums.len(),\n ensures\n 0 <= result <= nums.len(),\n result == 0 || exists|l: int, r: int|\n 0 <= l && r == l + result as int && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int,\n forall|l: int, r: int|\n 0 <= l && l <= r && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int ==> r - l\n <= result as int,\n {\n let n = nums.len();\n let mut left: usize = 0;\n let mut zeros: i32 = 0;\n let mut result: i32 = 0;\n let mut right: usize = 0;\n\n while right < n {\n if nums[right] == 0 {\n zeros = zeros + 1;\n }\n right = right + 1;\n\n while zeros > k {\n if nums[left] == 0 {\n zeros = zeros - 1;\n }\n left = left + 1;\n }\n\n let window = (right - left) as i32;\n if window > result {\n result = window;\n }\n }\n\n result\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_zeros(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n (if s[l] == 0 { 1int } else { 0int }) + Self::count_zeros(s, l + 1, r)\n }\n }\n\n proof fn lemma_count_zeros_split(s: Seq, i: int, j: int, k: int)\n requires\n 0 <= i <= j <= k <= s.len(),\n ensures\n Self::count_zeros(s, i, k) == Self::count_zeros(s, i, j) + Self::count_zeros(s, j, k),\n decreases j - i,\n {\n if i < j {\n Self::lemma_count_zeros_split(s, i + 1, j, k);\n }\n }\n\n proof fn lemma_count_zeros_nonneg(s: Seq, i: int, j: int)\n requires\n 0 <= i <= j <= s.len(),\n ensures\n Self::count_zeros(s, i, j) >= 0,\n decreases j - i,\n {\n if i < j {\n Self::lemma_count_zeros_nonneg(s, i + 1, j);\n }\n }\n\n proof fn lemma_count_zeros_mono_left(s: Seq, l1: int, l2: int, r: int)\n requires\n 0 <= l1 <= l2 <= r <= s.len(),\n ensures\n Self::count_zeros(s, l1, r) >= Self::count_zeros(s, l2, r),\n decreases l2 - l1,\n {\n if l1 < l2 {\n Self::lemma_count_zeros_split(s, l1, l2, r);\n Self::lemma_count_zeros_nonneg(s, l1, l2);\n }\n }\n\n pub fn longest_ones(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> nums[i] == 0 || nums[i] == 1,\n 0 <= k <= nums.len(),\n ensures\n 0 <= result <= nums.len(),\n result == 0 || exists|l: int, r: int|\n 0 <= l && r == l + result as int && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int,\n forall|l: int, r: int|\n 0 <= l && l <= r && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int ==> r - l\n <= result as int,\n {\n let n = nums.len();\n let mut left: usize = 0;\n let mut zeros: i32 = 0;\n let mut result: i32 = 0;\n let ghost mut result_left: int = 0;\n let mut right: usize = 0;\n\n while right < n\n invariant\n 0 <= left <= right <= n,\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= k <= n,\n forall|i: int| 0 <= i < n ==> nums@[i] == 0 || nums@[i] == 1,\n zeros == Self::count_zeros(nums@, left as int, right as int),\n zeros >= 0,\n zeros <= k,\n 0 <= result,\n result as int <= n as int,\n result as int >= right as int - left as int,\n result == 0 || (\n 0 <= result_left && result_left + result as int <= right as int\n && Self::count_zeros(nums@, result_left, result_left + result as int)\n <= k as int\n ),\n forall|l: int, r: int|\n 0 <= l && l <= r && r <= right as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int ==> r - l\n <= result as int,\n left == 0\n || Self::count_zeros(nums@, left as int - 1, right as int) > k as int,\n decreases n - right,\n {\n proof {\n reveal_with_fuel(Solution::count_zeros, 2);\n Self::lemma_count_zeros_split(\n nums@,\n left as int,\n right as int,\n right as int + 1,\n );\n }\n if nums[right] == 0 {\n zeros = zeros + 1;\n }\n right = right + 1;\n\n while zeros > k\n invariant\n 0 <= left <= right <= n,\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= k <= n,\n forall|i: int| 0 <= i < n ==> nums@[i] == 0 || nums@[i] == 1,\n zeros == Self::count_zeros(nums@, left as int, right as int),\n zeros >= 0,\n 0 <= result,\n result as int <= n as int,\n result == 0 || (\n 0 <= result_left && (result_left + result as int) < right as int\n && Self::count_zeros(\n nums@,\n result_left,\n result_left + result as int,\n ) <= k as int\n ),\n forall|l: int, r: int|\n 0 <= l && l <= r && r < right as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int ==> r - l\n <= result as int,\n left == 0\n || Self::count_zeros(nums@, left as int - 1, right as int) > k as int,\n decreases right - left,\n {\n proof {\n reveal_with_fuel(Solution::count_zeros, 2);\n Self::lemma_count_zeros_split(\n nums@,\n left as int,\n left as int + 1,\n right as int,\n );\n }\n let ghost old_left: int = left as int;\n let ghost old_zeros: int = zeros as int;\n if nums[left] == 0 {\n zeros = zeros - 1;\n }\n left = left + 1;\n proof {\n assert(Self::count_zeros(nums@, old_left, right as int) == old_zeros);\n assert(Self::count_zeros(nums@, left as int - 1, right as int) > k as int);\n }\n }\n\n let window = (right - left) as i32;\n if window > result {\n proof {\n result_left = left as int;\n }\n result = window;\n }\n\n proof {\n assert forall|l: int, r: int|\n 0 <= l && l <= r && r <= right as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= k as int implies r - l\n <= result as int\n by {\n if r < right as int {\n } else {\n if l >= left as int {\n assert(r - l <= right as int - left as int);\n assert(right as int - left as int <= result as int);\n } else {\n if left > 0 {\n Self::lemma_count_zeros_mono_left(\n nums@,\n l,\n left as int - 1,\n right as int,\n );\n }\n }\n }\n };\n }\n }\n\n result\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1004/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1004/tests/mutated_testcases.jsonl"} {"problem": "lc1035", "family": "leetcode", "description": "# Uncrossed Lines\n\nYou are given two integer arrays `nums1` and `nums2`. We write the integers of `nums1` and `nums2` (in the order they are given) on two separate horizontal lines.\n\nWe may draw connecting lines: a straight line connecting two numbers `nums1[i]` and `nums2[j]` such that:\n\n- `nums1[i] == nums2[j]`, and\n- the line we draw does not intersect any other connecting (non-horizontal) line.\n\nNote that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).\n\nReturn the maximum number of connecting lines we can draw in this way.\n\n## Example 1:\n\n> **Input:** nums1 = [1,4,2], nums2 = [1,2,4]\n> **Output:** 2\n> **Explanation:** We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from nums1[1] = 4 to nums2[2] = 4 will intersect the line from nums1[2] = 2 to nums2[1] = 2.\n\n## Example 2:\n\n> **Input:** nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]\n> **Output:** 3\n\n## Example 3:\n\n> **Input:** nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]\n> **Output:** 2\n\n## Constraints:\n\n- `1 <= nums1.length, nums2.length <= 500`\n- `1 <= nums1[i], nums2[j] <= 2000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming\nAcceptance rate: 65.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_strictly_sorted(s: Seq) -> bool {\n forall |k: int, l: int| 0 <= k < l < s.len() ==> s[k] < s[l]\n}\n\npub open spec fn all_in_range(s: Seq, hi: int) -> bool {\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s[k] < hi\n}\n\npub open spec fn is_common_subseq(a: Seq, b: Seq, ia: Seq, ib: Seq) -> bool {\n ia.len() == ib.len()\n && is_strictly_sorted(ia)\n && is_strictly_sorted(ib)\n && all_in_range(ia, a.len() as int)\n && all_in_range(ib, b.len() as int)\n && (forall |k: int| 0 <= k < ia.len() ==> a[#[trigger] ia[k]] == b[#[trigger] ib[k]])\n}\n\nimpl Solution {\n pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 1 <= nums1.len() <= 500,\n 1 <= nums2.len() <= 500,\n forall |i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 2000,\n forall |i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 2000,\n ensures\n exists |ia: Seq, ib: Seq| #[trigger] is_common_subseq(nums1@, nums2@, ia, ib) && ia.len() == result as int,\n forall |ia: Seq, ib: Seq| #[trigger] is_common_subseq(nums1@, nums2@, ia, ib) ==> ia.len() <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 {\n let m = nums1.len();\n let n = nums2.len();\n let mut dp: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n {\n dp.push(0);\n k = k + 1;\n }\n let mut i: usize = 1;\n while i <= m {\n let mut prev: i32 = 0;\n let mut j: usize = 1;\n while j <= n {\n let curr = dp[j];\n if nums1[i - 1] == nums2[j - 1] {\n dp[j] = prev + 1;\n } else {\n let a = curr;\n let b = dp[j - 1];\n if a >= b {\n dp[j] = a;\n } else {\n dp[j] = b;\n }\n }\n prev = curr;\n j = j + 1;\n }\n i = i + 1;\n }\n dp[n]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_strictly_sorted(s: Seq) -> bool {\n forall |k: int, l: int| 0 <= k < l < s.len() ==> s[k] < s[l]\n}\n\npub open spec fn all_in_range(s: Seq, hi: int) -> bool {\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s[k] < hi\n}\n\npub open spec fn is_common_subseq(a: Seq, b: Seq, ia: Seq, ib: Seq) -> bool {\n ia.len() == ib.len()\n && is_strictly_sorted(ia)\n && is_strictly_sorted(ib)\n && all_in_range(ia, a.len() as int)\n && all_in_range(ib, b.len() as int)\n && (forall |k: int| 0 <= k < ia.len() ==> a[#[trigger] ia[k]] == b[#[trigger] ib[k]])\n}\n\npub open spec fn lcs(s1: Seq, s2: Seq, i: int, j: int) -> int\n decreases i, j\n{\n if i <= 0 || j <= 0 {\n 0\n } else if s1[i - 1] == s2[j - 1] {\n lcs(s1, s2, i - 1, j - 1) + 1\n } else {\n let a = lcs(s1, s2, i - 1, j);\n let b = lcs(s1, s2, i, j - 1);\n if a >= b { a } else { b }\n }\n}\n\nimpl Solution {\n pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 1 <= nums1.len() <= 500,\n 1 <= nums2.len() <= 500,\n forall |i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 2000,\n forall |i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 2000,\n ensures\n exists |ia: Seq, ib: Seq| #[trigger] is_common_subseq(nums1@, nums2@, ia, ib) && ia.len() == result as int,\n forall |ia: Seq, ib: Seq| #[trigger] is_common_subseq(nums1@, nums2@, ia, ib) ==> ia.len() <= result as int,\n {\n let m = nums1.len();\n let n = nums2.len();\n let mut dp: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n {\n dp.push(0);\n k = k + 1;\n }\n let mut i: usize = 1;\n while i <= m {\n let mut prev: i32 = 0;\n let mut j: usize = 1;\n while j <= n {\n let curr = dp[j];\n if nums1[i - 1] == nums2[j - 1] {\n dp.set(j, prev + 1);\n } else {\n let a = curr;\n let b = dp[j - 1];\n if a >= b {\n dp.set(j, a);\n } else {\n dp.set(j, b);\n }\n }\n prev = curr;\n j = j + 1;\n }\n i = i + 1;\n }\n dp[n]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\npub open spec fn is_strictly_sorted(s: Seq) -> bool {\n forall |k: int, l: int| 0 <= k < l < s.len() ==> s[k] < s[l]\n}\n\npub open spec fn all_in_range(s: Seq, hi: int) -> bool {\n forall |k: int| 0 <= k < s.len() ==> 0 <= #[trigger] s[k] < hi\n}\n\npub open spec fn is_common_subseq(a: Seq, b: Seq, ia: Seq, ib: Seq) -> bool {\n ia.len() == ib.len()\n && is_strictly_sorted(ia)\n && is_strictly_sorted(ib)\n && all_in_range(ia, a.len() as int)\n && all_in_range(ib, b.len() as int)\n && (forall |k: int| 0 <= k < ia.len() ==> a[#[trigger] ia[k]] == b[#[trigger] ib[k]])\n}\n\n\n\npub open spec fn lcs(s1: Seq, s2: Seq, i: int, j: int) -> int\n decreases i, j\n{\n if i <= 0 || j <= 0 {\n 0\n } else if s1[i - 1] == s2[j - 1] {\n lcs(s1, s2, i - 1, j - 1) + 1\n } else {\n let a = lcs(s1, s2, i - 1, j);\n let b = lcs(s1, s2, i, j - 1);\n if a >= b { a } else { b }\n }\n}\n\n\n\npub open spec fn lcs_wit_a(s1: Seq, s2: Seq, i: int, j: int) -> Seq\n decreases i, j\n{\n if i <= 0 || j <= 0 {\n Seq::empty()\n } else if s1[i - 1] == s2[j - 1] {\n lcs_wit_a(s1, s2, i - 1, j - 1).push(i - 1)\n } else if lcs(s1, s2, i - 1, j) >= lcs(s1, s2, i, j - 1) {\n lcs_wit_a(s1, s2, i - 1, j)\n } else {\n lcs_wit_a(s1, s2, i, j - 1)\n }\n}\n\npub open spec fn lcs_wit_b(s1: Seq, s2: Seq, i: int, j: int) -> Seq\n decreases i, j\n{\n if i <= 0 || j <= 0 {\n Seq::empty()\n } else if s1[i - 1] == s2[j - 1] {\n lcs_wit_b(s1, s2, i - 1, j - 1).push(j - 1)\n } else if lcs(s1, s2, i - 1, j) >= lcs(s1, s2, i, j - 1) {\n lcs_wit_b(s1, s2, i - 1, j)\n } else {\n lcs_wit_b(s1, s2, i, j - 1)\n }\n}\n\n\n\nproof fn sorted_push(s: Seq, val: int)\n requires\n is_strictly_sorted(s),\n all_in_range(s, val),\n ensures\n is_strictly_sorted(s.push(val)),\n{\n assert forall |k: int, l: int| 0 <= k < l < s.push(val).len()\n implies s.push(val)[k] < s.push(val)[l] by {\n if l < s.len() as int {\n } else {\n assert(s.push(val)[l] == val);\n assert(s.push(val)[k] == s[k]);\n }\n }\n}\n\nproof fn in_range_push(s: Seq, val: int, new_hi: int)\n requires\n all_in_range(s, val),\n 0 <= val < new_hi,\n ensures\n all_in_range(s.push(val), new_hi),\n{\n assert forall |k: int| 0 <= k < s.push(val).len()\n implies 0 <= #[trigger] s.push(val)[k] < new_hi by {\n if k < s.len() as int {\n assert(s.push(val)[k] == s[k]);\n }\n }\n}\n\nproof fn in_range_widen(s: Seq, lo: int, hi: int)\n requires\n all_in_range(s, lo),\n lo <= hi,\n ensures\n all_in_range(s, hi),\n{\n assert forall |k: int| 0 <= k < s.len()\n implies 0 <= #[trigger] s[k] < hi by {}\n}\n\nproof fn sorted_drop_last(s: Seq, hi: int)\n requires\n is_strictly_sorted(s),\n all_in_range(s, hi),\n s.len() > 0,\n ensures\n is_strictly_sorted(s.drop_last()),\n all_in_range(s.drop_last(), s.last()),\n{\n let dl = s.drop_last();\n assert forall |k: int, l: int| 0 <= k < l < dl.len()\n implies dl[k] < dl[l] by {\n assert(dl[k] == s[k]);\n assert(dl[l] == s[l]);\n }\n assert forall |k: int| 0 <= k < dl.len()\n implies 0 <= #[trigger] dl[k] < s.last() by {\n assert(dl[k] == s[k]);\n assert(s[k] < s[s.len() - 1]);\n }\n}\n\nproof fn matching_drop_last(s1: Seq, s2: Seq, ia: Seq, ib: Seq)\n requires\n ia.len() == ib.len(),\n ia.len() > 0,\n forall |k: int| 0 <= k < ia.len() ==> s1[#[trigger] ia[k]] == s2[#[trigger] ib[k]],\n ensures\n forall |k: int| 0 <= k < ia.drop_last().len() ==> s1[#[trigger] ia.drop_last()[k]] == s2[#[trigger] ib.drop_last()[k]],\n{\n assert forall |k: int| 0 <= k < ia.drop_last().len()\n implies s1[#[trigger] ia.drop_last()[k]] == s2[#[trigger] ib.drop_last()[k]] by {\n assert(ia.drop_last()[k] == ia[k]);\n assert(ib.drop_last()[k] == ib[k]);\n }\n}\n\n\n\nproof fn lcs_bounded(s1: Seq, s2: Seq, i: int, j: int)\n requires\n 0 <= i <= s1.len(),\n 0 <= j <= s2.len(),\n ensures\n 0 <= lcs(s1, s2, i, j) <= i,\n 0 <= lcs(s1, s2, i, j) <= j,\n decreases i, j\n{\n if i <= 0 || j <= 0 {\n } else if s1[i - 1] == s2[j - 1] {\n lcs_bounded(s1, s2, i - 1, j - 1);\n } else {\n lcs_bounded(s1, s2, i - 1, j);\n lcs_bounded(s1, s2, i, j - 1);\n }\n}\n\n\n\n\n\n\n\n\nproof fn lcs_mono_adj(s1: Seq, s2: Seq, i: int, j: int)\n requires\n 0 <= i <= s1.len(),\n 0 <= j <= s2.len(),\n ensures\n j < s2.len() ==> lcs(s1, s2, i, j) <= lcs(s1, s2, i, j + 1),\n i < s1.len() ==> lcs(s1, s2, i + 1, j) <= lcs(s1, s2, i, j) + 1,\n i < s1.len() ==> lcs(s1, s2, i, j) <= lcs(s1, s2, i + 1, j),\n j < s2.len() ==> lcs(s1, s2, i, j + 1) <= lcs(s1, s2, i, j) + 1,\n decreases i, j\n{\n if i == 0 || j == 0 {\n lcs_bounded(s1, s2, i, j);\n if i < s1.len() as int {\n lcs_bounded(s1, s2, i + 1, j);\n }\n if j < s2.len() as int {\n lcs_bounded(s1, s2, i, j + 1);\n }\n } else {\n \n lcs_mono_adj(s1, s2, i - 1, j - 1);\n\n \n if j >= 2 {\n lcs_mono_adj(s1, s2, i, j - 1);\n } else {\n \n if i < s1.len() as int {\n lcs_bounded(s1, s2, i + 1, 0);\n }\n }\n\n \n if i >= 2 {\n lcs_mono_adj(s1, s2, i - 1, j);\n } else {\n \n if j < s2.len() as int {\n lcs_bounded(s1, s2, 0, j + 1);\n }\n }\n\n \n if j < s2.len() as int {\n if s1[i - 1] == s2[j] && s1[i - 1] != s2[j - 1] {\n assert(lcs(s1, s2, i, j - 1) <= lcs(s1, s2, i - 1, j - 1) + 1);\n assert(lcs(s1, s2, i - 1, j - 1) <= lcs(s1, s2, i - 1, j));\n assert(lcs(s1, s2, i, j - 1) <= lcs(s1, s2, i - 1, j) + 1);\n }\n }\n if i < s1.len() as int {\n if s1[i] == s2[j - 1] && s1[i - 1] != s2[j - 1] {\n assert(lcs(s1, s2, i - 1, j) <= lcs(s1, s2, i - 1, j - 1) + 1);\n assert(lcs(s1, s2, i - 1, j - 1) <= lcs(s1, s2, i, j - 1));\n assert(lcs(s1, s2, i - 1, j) <= lcs(s1, s2, i, j - 1) + 1);\n }\n }\n }\n}\n\n\n\nproof fn lcs_mono(s1: Seq, s2: Seq, i1: int, j1: int, i2: int, j2: int)\n requires\n 0 <= i1 <= i2 <= s1.len(),\n 0 <= j1 <= j2 <= s2.len(),\n ensures\n lcs(s1, s2, i1, j1) <= lcs(s1, s2, i2, j2),\n decreases i2 - i1 + j2 - j1\n{\n if i1 == i2 && j1 == j2 {\n } else if j1 < j2 {\n lcs_mono(s1, s2, i1, j1, i2, j2 - 1);\n lcs_mono_adj(s1, s2, i2, j2 - 1);\n } else {\n lcs_mono(s1, s2, i1, j1, i2 - 1, j2);\n lcs_mono_adj(s1, s2, i2 - 1, j2);\n }\n}\n\n\n\nproof fn lcs_achievable(s1: Seq, s2: Seq, i: int, j: int)\n requires\n 0 <= i <= s1.len(),\n 0 <= j <= s2.len(),\n ensures\n lcs_wit_a(s1, s2, i, j).len() == lcs_wit_b(s1, s2, i, j).len(),\n is_strictly_sorted(lcs_wit_a(s1, s2, i, j)),\n is_strictly_sorted(lcs_wit_b(s1, s2, i, j)),\n all_in_range(lcs_wit_a(s1, s2, i, j), i),\n all_in_range(lcs_wit_b(s1, s2, i, j), j),\n lcs_wit_a(s1, s2, i, j).len() == lcs(s1, s2, i, j),\n forall |k: int| 0 <= k < lcs_wit_a(s1, s2, i, j).len()\n ==> s1[#[trigger] lcs_wit_a(s1, s2, i, j)[k]] == s2[#[trigger] lcs_wit_b(s1, s2, i, j)[k]],\n decreases i, j\n{\n if i <= 0 || j <= 0 {\n assert(lcs_wit_a(s1, s2, i, j) =~= Seq::::empty());\n assert(lcs_wit_b(s1, s2, i, j) =~= Seq::::empty());\n } else if s1[i - 1] == s2[j - 1] {\n lcs_achievable(s1, s2, i - 1, j - 1);\n let prev_a = lcs_wit_a(s1, s2, i - 1, j - 1);\n let prev_b = lcs_wit_b(s1, s2, i - 1, j - 1);\n sorted_push(prev_a, i - 1);\n sorted_push(prev_b, j - 1);\n in_range_push(prev_a, i - 1, i);\n in_range_push(prev_b, j - 1, j);\n assert forall |k: int| 0 <= k < prev_a.push(i - 1).len()\n implies s1[#[trigger] prev_a.push(i - 1)[k]] == s2[#[trigger] prev_b.push(j - 1)[k]] by {\n if k < prev_a.len() as int {\n assert(prev_a.push(i - 1)[k] == prev_a[k]);\n assert(prev_b.push(j - 1)[k] == prev_b[k]);\n } else {\n assert(prev_a.push(i - 1)[k] == i - 1);\n assert(prev_b.push(j - 1)[k] == j - 1);\n }\n }\n } else if lcs(s1, s2, i - 1, j) >= lcs(s1, s2, i, j - 1) {\n lcs_achievable(s1, s2, i - 1, j);\n in_range_widen(lcs_wit_a(s1, s2, i - 1, j), i - 1, i);\n } else {\n lcs_achievable(s1, s2, i, j - 1);\n in_range_widen(lcs_wit_b(s1, s2, i, j - 1), j - 1, j);\n }\n}\n\n\n\nproof fn lcs_optimal(s1: Seq, s2: Seq, i: int, j: int, ia: Seq, ib: Seq)\n requires\n 0 <= i <= s1.len(),\n 0 <= j <= s2.len(),\n ia.len() == ib.len(),\n is_strictly_sorted(ia),\n is_strictly_sorted(ib),\n all_in_range(ia, i),\n all_in_range(ib, j),\n forall |k: int| 0 <= k < ia.len() ==> s1[#[trigger] ia[k]] == s2[#[trigger] ib[k]],\n ensures\n ia.len() <= lcs(s1, s2, i, j),\n decreases ia.len()\n{\n if ia.len() == 0 {\n lcs_bounded(s1, s2, i, j);\n } else {\n let last_a = ia.last();\n let last_b = ib.last();\n sorted_drop_last(ia, i);\n sorted_drop_last(ib, j);\n matching_drop_last(s1, s2, ia, ib);\n lcs_optimal(s1, s2, last_a, last_b, ia.drop_last(), ib.drop_last());\n \n \n assert(s1[last_a] == s2[last_b]);\n assert(lcs(s1, s2, last_a + 1, last_b + 1) == lcs(s1, s2, last_a, last_b) + 1);\n \n lcs_mono(s1, s2, last_a + 1, last_b + 1, i, j);\n }\n}\n\nimpl Solution {\n pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 1 <= nums1.len() <= 500,\n 1 <= nums2.len() <= 500,\n forall |i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 2000,\n forall |i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 2000,\n ensures\n exists |ia: Seq, ib: Seq| #[trigger] is_common_subseq(nums1@, nums2@, ia, ib) && ia.len() == result as int,\n forall |ia: Seq, ib: Seq| #[trigger] is_common_subseq(nums1@, nums2@, ia, ib) ==> ia.len() <= result as int,\n {\n let m = nums1.len();\n let n = nums2.len();\n let mut dp: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= n\n invariant\n n <= 500,\n dp.len() == k,\n k <= n + 1,\n forall |c: int| 0 <= c < k as int ==> (#[trigger] dp@[c]) == 0i32,\n decreases (n + 1 - k),\n {\n dp.push(0);\n k = k + 1;\n }\n\n let mut i: usize = 1;\n while i <= m\n invariant\n m == nums1.len(),\n n == nums2.len(),\n 1 <= m <= 500,\n 1 <= n <= 500,\n 1 <= i <= m + 1,\n dp.len() == n + 1,\n forall |c: int| 0 <= c <= n as int ==>\n (#[trigger] dp@[c]) as int == lcs(nums1@, nums2@, (i - 1) as int, c),\n forall |idx: int| 0 <= idx < m as int ==> 1 <= #[trigger] nums1@[idx] <= 2000,\n forall |idx: int| 0 <= idx < n as int ==> 1 <= #[trigger] nums2@[idx] <= 2000,\n decreases (m + 1 - i),\n {\n let mut prev: i32 = 0;\n let mut j: usize = 1;\n while j <= n\n invariant\n m == nums1.len(),\n n == nums2.len(),\n 1 <= m <= 500,\n 1 <= n <= 500,\n 1 <= i <= m,\n 1 <= j <= n + 1,\n dp.len() == n + 1,\n prev as int == lcs(nums1@, nums2@, (i - 1) as int, (j - 1) as int),\n forall |c: int| 0 <= c < j as int ==>\n (#[trigger] dp@[c]) as int == lcs(nums1@, nums2@, i as int, c),\n forall |c: int| j as int <= c <= n as int ==>\n (#[trigger] dp@[c]) as int == lcs(nums1@, nums2@, (i - 1) as int, c),\n forall |idx: int| 0 <= idx < m as int ==> 1 <= #[trigger] nums1@[idx] <= 2000,\n forall |idx: int| 0 <= idx < n as int ==> 1 <= #[trigger] nums2@[idx] <= 2000,\n decreases (n + 1 - j),\n {\n let curr = dp[j];\n proof {\n lcs_bounded(nums1@, nums2@, (i - 1) as int, (j - 1) as int);\n }\n\n if nums1[i - 1] == nums2[j - 1] {\n dp.set(j, prev + 1);\n } else {\n let a = curr;\n let b = dp[j - 1];\n if a >= b {\n dp.set(j, a);\n } else {\n dp.set(j, b);\n }\n }\n prev = curr;\n j = j + 1;\n }\n i = i + 1;\n }\n\n proof {\n lcs_bounded(nums1@, nums2@, m as int, n as int);\n\n \n lcs_achievable(nums1@, nums2@, m as int, n as int);\n let wit_a = lcs_wit_a(nums1@, nums2@, m as int, n as int);\n let wit_b = lcs_wit_b(nums1@, nums2@, m as int, n as int);\n assert(is_common_subseq(nums1@, nums2@, wit_a, wit_b));\n assert(wit_a.len() == dp@[n as int] as int);\n\n \n assert forall |ia: Seq, ib: Seq|\n #[trigger] is_common_subseq(nums1@, nums2@, ia, ib)\n implies ia.len() <= dp@[n as int] as int by {\n lcs_optimal(nums1@, nums2@, m as int, n as int, ia, ib);\n }\n }\n\n dp[n]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1035/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1035/tests/mutated_testcases.jsonl"} {"problem": "lc1039", "family": "leetcode", "description": "# Minimum Score Triangulation of Polygon\n\nYou have a convex `n`-sided polygon where each vertex has an integer value. You are given an integer array `values` where `values[i]` is the value of the `i`th vertex in clockwise order.\n\nPolygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in `n - 2` triangles.\n\nYou will triangulate the polygon. For each triangle, the *weight* of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all `n - 2` triangles.\n\nReturn the minimum possible score that you can achieve with some triangulation of the polygon.\n\n## Example 1:\n\n> **Input:** values = [1,2,3]\n> **Output:** 6\n> **Explanation:** The polygon is already triangulated, and the score of the only triangle is 6.\n\n## Example 2:\n\n> **Input:** values = [3,7,4,5]\n> **Output:** 144\n> **Explanation:** There are two triangulations, with possible scores: 3\\*7\\*5 + 4\\*5\\*7 = 245, or 3\\*4\\*5 + 3\\*4\\*7 = 144. The minimum score is 144.\n\n## Example 3:\n\n> **Input:** values = [1,3,1,4,1,5]\n> **Output:** 13\n> **Explanation:** The minimum score triangulation is 1\\*1\\*3 + 1\\*1\\*4 + 1\\*1\\*5 + 1\\*1\\*1 = 13.\n\n## Constraints:\n\n- `n == values.length`\n- $3 \\le n \\le 50$\n- $1 \\le values[i] \\le 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_score_triangulation(values: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming\nAcceptance rate: 67.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\npub enum SplitTree {\n Leaf,\n Node(int, Box, Box),\n}\n\n\n\npub open spec fn split_tree_valid(tree: SplitTree, i: int, j: int) -> bool\n decreases tree,\n{\n match tree {\n SplitTree::Leaf => j - i < 2,\n SplitTree::Node(k, left, right) =>\n j - i >= 2 && i < k < j\n && split_tree_valid(*left, i, k)\n && split_tree_valid(*right, k, j),\n }\n}\n\n\n\npub open spec fn split_tree_score(\n values: Seq, tree: SplitTree, i: int, j: int,\n) -> int\n decreases tree,\n{\n match tree {\n SplitTree::Leaf => 0,\n SplitTree::Node(k, left, right) =>\n split_tree_score(values, *left, i, k)\n + values[i] as int * values[k] as int * values[j] as int\n + split_tree_score(values, *right, k, j),\n }\n}\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn min_score(values: Seq, i: int, j: int) -> int\n decreases j - i, j - i,\n {\n if j - i < 2 {\n 0\n } else {\n Self::min_score_k(values, i, j, i + 1)\n }\n }\n\n pub open spec fn min_score_k(values: Seq, i: int, j: int, k: int) -> int\n decreases j - i, j - k,\n {\n if k >= j || k <= i || j - i < 2 {\n i32::MAX as int\n } else {\n let cur = Self::min_score(values, i, k) + Self::min_score(values, k, j)\n + values[i] as int * values[k] as int * values[j] as int;\n if k + 1 >= j {\n cur\n } else {\n Self::min2(cur, Self::min_score_k(values, i, j, k + 1))\n }\n }\n }\n\n pub fn min_score_triangulation(values: Vec) -> (res: i32)\n requires\n 3 <= values.len() <= 50,\n forall|i: int| 0 <= i < values.len() ==> 1 <= #[trigger] values[i] <= 100,\n ensures\n exists|tree: SplitTree|\n #[trigger] split_tree_valid(tree, 0, (values.len() - 1) as int)\n && split_tree_score(values@, tree, 0, (values.len() - 1) as int) == res as int,\n forall|tree: SplitTree|\n split_tree_valid(tree, 0, (values.len() - 1) as int)\n ==> res as int <= #[trigger] split_tree_score(\n values@, tree, 0, (values.len() - 1) as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn triple_product(a: i32, b: i32, c: i32) -> i32 {\n let ab = a * b;\n ab * c\n }\n\n pub fn min_score_triangulation(values: Vec) -> i32 {\n let n = values.len();\n let nn = n * n;\n let mut dp: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < nn {\n dp.push(0i32);\n idx = idx + 1;\n }\n let mut gap = 2usize;\n while gap < n {\n let mut i = 0usize;\n while i + gap < n {\n let j = i + gap;\n let k_first: usize = i + 1;\n let dl = dp[i * n + k_first];\n let dr = dp[k_first * n + j];\n let prod = Self::triple_product(values[i], values[k_first], values[j]);\n let score_first = dl + dr + prod;\n let mut best: i32 = score_first;\n let mut k: usize = i + 2;\n while k < j {\n let dl2 = dp[i * n + k];\n let dr2 = dp[k * n + j];\n let prod2 = Self::triple_product(values[i], values[k], values[j]);\n let score = dl2 + dr2 + prod2;\n if score < best {\n best = score;\n }\n k = k + 1;\n }\n dp[i * n + j] = best;\n i = i + 1;\n }\n gap = gap + 1;\n }\n dp[n - 1]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\npub enum SplitTree {\n Leaf,\n Node(int, Box, Box),\n}\n\n\n\npub open spec fn split_tree_valid(tree: SplitTree, i: int, j: int) -> bool\n decreases tree,\n{\n match tree {\n SplitTree::Leaf => j - i < 2,\n SplitTree::Node(k, left, right) =>\n j - i >= 2 && i < k < j\n && split_tree_valid(*left, i, k)\n && split_tree_valid(*right, k, j),\n }\n}\n\n\n\npub open spec fn split_tree_score(\n values: Seq, tree: SplitTree, i: int, j: int,\n) -> int\n decreases tree,\n{\n match tree {\n SplitTree::Leaf => 0,\n SplitTree::Node(k, left, right) =>\n split_tree_score(values, *left, i, k)\n + values[i] as int * values[k] as int * values[j] as int\n + split_tree_score(values, *right, k, j),\n }\n}\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn min_score(values: Seq, i: int, j: int) -> int\n decreases j - i, j - i,\n {\n if j - i < 2 {\n 0\n } else {\n Self::min_score_k(values, i, j, i + 1)\n }\n }\n\n pub open spec fn min_score_k(values: Seq, i: int, j: int, k: int) -> int\n decreases j - i, j - k,\n {\n if k >= j || k <= i || j - i < 2 {\n i32::MAX as int\n } else {\n let cur = Self::min_score(values, i, k) + Self::min_score(values, k, j)\n + values[i] as int * values[k] as int * values[j] as int;\n if k + 1 >= j {\n cur\n } else {\n Self::min2(cur, Self::min_score_k(values, i, j, k + 1))\n }\n }\n }\n\n fn triple_product(a: i32, b: i32, c: i32) -> (r: i32)\n requires 1 <= a <= 100, 1 <= b <= 100, 1 <= c <= 100,\n ensures r as int == a as int * b as int * c as int,\n 1 <= r <= 1_000_000,\n {\n let ab = a * b;\n ab * c\n }\n\n pub fn min_score_triangulation(values: Vec) -> (res: i32)\n requires\n 3 <= values.len() <= 50,\n forall|i: int| 0 <= i < values.len() ==> 1 <= #[trigger] values[i] <= 100,\n ensures\n exists|tree: SplitTree|\n #[trigger] split_tree_valid(tree, 0, (values.len() - 1) as int)\n && split_tree_score(values@, tree, 0, (values.len() - 1) as int) == res as int,\n forall|tree: SplitTree|\n split_tree_valid(tree, 0, (values.len() - 1) as int)\n ==> res as int <= #[trigger] split_tree_score(\n values@, tree, 0, (values.len() - 1) as int),\n {\n let n = values.len();\n let nn = n * n;\n let mut dp: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < nn {\n dp.push(0i32);\n idx = idx + 1;\n }\n let mut gap = 2usize;\n while gap < n {\n let mut i = 0usize;\n while i + gap < n {\n let j = i + gap;\n let k_first: usize = i + 1;\n let dl = dp[i * n + k_first];\n let dr = dp[k_first * n + j];\n let prod = Self::triple_product(values[i], values[k_first], values[j]);\n let score_first = dl + dr + prod;\n let mut best: i32 = score_first;\n let mut k: usize = i + 2;\n while k < j {\n let dl2 = dp[i * n + k];\n let dr2 = dp[k * n + j];\n let prod2 = Self::triple_product(values[i], values[k], values[j]);\n let score = dl2 + dr2 + prod2;\n if score < best {\n best = score;\n }\n k = k + 1;\n }\n dp.set(i * n + j, best);\n i = i + 1;\n }\n gap = gap + 1;\n }\n dp[n - 1]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\npub enum SplitTree {\n Leaf,\n Node(int, Box, Box),\n}\n\n\n\npub open spec fn split_tree_valid(tree: SplitTree, i: int, j: int) -> bool\n decreases tree,\n{\n match tree {\n SplitTree::Leaf => j - i < 2,\n SplitTree::Node(k, left, right) =>\n j - i >= 2 && i < k < j\n && split_tree_valid(*left, i, k)\n && split_tree_valid(*right, k, j),\n }\n}\n\n\n\npub open spec fn split_tree_score(\n values: Seq, tree: SplitTree, i: int, j: int,\n) -> int\n decreases tree,\n{\n match tree {\n SplitTree::Leaf => 0,\n SplitTree::Node(k, left, right) =>\n split_tree_score(values, *left, i, k)\n + values[i] as int * values[k] as int * values[j] as int\n + split_tree_score(values, *right, k, j),\n }\n}\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn min_score(values: Seq, i: int, j: int) -> int\n decreases j - i, j - i,\n {\n if j - i < 2 {\n 0\n } else {\n Self::min_score_k(values, i, j, i + 1)\n }\n }\n\n pub open spec fn min_score_k(values: Seq, i: int, j: int, k: int) -> int\n decreases j - i, j - k,\n {\n if k >= j || k <= i || j - i < 2 {\n i32::MAX as int\n } else {\n let cur = Self::min_score(values, i, k) + Self::min_score(values, k, j)\n + values[i] as int * values[k] as int * values[j] as int;\n if k + 1 >= j {\n cur\n } else {\n Self::min2(cur, Self::min_score_k(values, i, j, k + 1))\n }\n }\n }\n\n proof fn lemma_min_assoc(a: int, b: int, c: int)\n ensures\n Self::min2(a, Self::min2(b, c)) == Self::min2(Self::min2(a, b), c),\n {\n }\n\n proof fn lemma_idx_bound(i: int, j: int, n: int)\n requires 0 <= i < n, 0 <= j < n, n >= 1,\n ensures 0 <= i * n + j < n * n,\n {\n assert(0 <= i * n + j) by (nonlinear_arith) requires 0 <= i, 0 <= j, n >= 1, {}\n assert(i * n + j < n * n) by (nonlinear_arith) requires 0 <= i < n, 0 <= j < n, {}\n }\n\n proof fn lemma_idx_injective(a: int, b: int, c: int, d: int, n: int)\n requires 0 <= a < n, 0 <= b < n, 0 <= c < n, 0 <= d < n, n >= 1,\n a * n + b == c * n + d,\n ensures a == c, b == d,\n {\n assert((a - c) * n == d - b) by (nonlinear_arith) requires a * n + b == c * n + d, {}\n assert(-n < d - b < n);\n if a > c {\n assert((a - c) * n >= n) by (nonlinear_arith) requires a > c, n >= 1, {}\n }\n if a < c {\n assert((a - c) * n <= -n) by (nonlinear_arith) requires a < c, n >= 1, {}\n }\n }\n\n proof fn lemma_min_score_bounds(values: Seq, i: int, j: int)\n requires\n 0 <= i <= j < values.len(), values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n ensures\n j - i < 2 ==> Self::min_score(values, i, j) == 0,\n j - i >= 2 ==> 1 <= Self::min_score(values, i, j) <= (j - i - 1) * 1_000_000,\n decreases j - i,\n {\n if j - i >= 2 {\n Self::lemma_min_score_k_bounds(values, i, j, i + 1);\n }\n }\n\n proof fn lemma_min_score_k_bounds(values: Seq, i: int, j: int, k: int)\n requires\n 0 <= i, i < k, k <= j, j < values.len(),\n j - i >= 2, values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n ensures\n k < j ==> 1 <= Self::min_score_k(values, i, j, k) <= (j - i - 1) * 1_000_000,\n k >= j ==> Self::min_score_k(values, i, j, k) == i32::MAX as int,\n decreases j - i, j - k,\n {\n if k >= j {\n } else {\n Self::lemma_min_score_bounds(values, i, k);\n Self::lemma_min_score_bounds(values, k, j);\n let left = Self::min_score(values, i, k);\n let right = Self::min_score(values, k, j);\n let vi = values[i] as int;\n let vk = values[k] as int;\n let vj = values[j] as int;\n let product = vi * vk * vj;\n let cur = left + right + product;\n assert(1 <= product <= 1_000_000) by (nonlinear_arith)\n requires 1 <= vi <= 100, 1 <= vk <= 100, 1 <= vj <= 100, product == vi * vk * vj,\n {}\n assert(left >= 0) by { if k - i < 2 { assert(left == 0); } }\n assert(right >= 0) by { if j - k < 2 { assert(right == 0); } }\n assert(cur >= 1);\n assert(left <= (k - i - 1) * 1_000_000) by {\n if k - i < 2 { assert(left == 0); assert(k - i >= 1); }\n }\n assert(right <= (j - k - 1) * 1_000_000) by {\n if j - k < 2 { assert(right == 0); assert(j - k >= 1); }\n }\n assert(cur <= (j - i - 1) * 1_000_000) by (nonlinear_arith)\n requires\n left <= (k - i - 1) * 1_000_000, right <= (j - k - 1) * 1_000_000,\n product <= 1_000_000, cur == left + right + product, k - i >= 1, j - k >= 1,\n {}\n if k + 1 < j {\n Self::lemma_min_score_k_bounds(values, i, j, k + 1);\n }\n }\n }\n\n proof fn lemma_dp_entry_bound(values: Seq, dp: Seq, n: int, a: int, b: int)\n requires\n 0 <= a < n, 0 <= b < n, n >= 3, n <= 50,\n 0 <= a * n + b < n * n,\n dp.len() == n * n, values.len() == n,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n dp[a * n + b] as int == Self::min_score(values, a, b),\n b >= a,\n ensures\n 0 <= dp[a * n + b] as int <= 48_000_000,\n {\n Self::lemma_min_score_bounds(values, a, b);\n if b - a < 2 {\n } else {\n assert(b - a - 1 <= 48);\n assert((b - a - 1) * 1_000_000 <= 48_000_000) by (nonlinear_arith)\n requires b - a - 1 <= 48, {}\n }\n }\n\n \n proof fn lemma_min_score_k_le_tree(\n values: Seq, i: int, j: int, k: int,\n tree: SplitTree, k0: int,\n left_tree: SplitTree, right_tree: SplitTree,\n )\n requires\n 0 <= i, j < values.len() as int, j - i >= 2, values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n i < k, k <= k0, k0 < j,\n split_tree_valid(left_tree, i, k0),\n split_tree_valid(right_tree, k0, j),\n tree == SplitTree::Node(k0, Box::new(left_tree), Box::new(right_tree)),\n Self::min_score(values, i, k0)\n <= split_tree_score(values, left_tree, i, k0),\n Self::min_score(values, k0, j)\n <= split_tree_score(values, right_tree, k0, j),\n ensures\n Self::min_score_k(values, i, j, k)\n <= split_tree_score(values, tree, i, j),\n decreases j - k,\n {\n let score = split_tree_score(values, tree, i, j);\n let cost_k0 = Self::min_score(values, i, k0)\n + Self::min_score(values, k0, j)\n + values[i] as int * values[k0] as int * values[j] as int;\n assert(score == split_tree_score(values, left_tree, i, k0)\n + values[i] as int * values[k0] as int * values[j] as int\n + split_tree_score(values, right_tree, k0, j));\n assert(score >= cost_k0);\n if k == k0 {\n let mk = Self::min_score_k(values, i, j, k);\n if k + 1 >= j {\n assert(mk == cost_k0);\n } else {\n assert(mk == Self::min2(cost_k0,\n Self::min_score_k(values, i, j, k + 1)));\n assert(mk <= cost_k0);\n }\n } else {\n Self::lemma_min_score_k_le_tree(\n values, i, j, k + 1, tree, k0, left_tree, right_tree);\n let rest = Self::min_score_k(values, i, j, k + 1);\n let cur_k = Self::min_score(values, i, k) + Self::min_score(values, k, j)\n + values[i] as int * values[k] as int * values[j] as int;\n assert(Self::min_score_k(values, i, j, k) == Self::min2(cur_k, rest));\n assert(Self::min2(cur_k, rest) <= rest);\n }\n }\n\n \n proof fn lemma_min_score_le_any(\n values: Seq, i: int, j: int, tree: SplitTree,\n )\n requires\n 0 <= i <= j, j < values.len() as int, values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n split_tree_valid(tree, i, j),\n ensures\n Self::min_score(values, i, j) <= split_tree_score(values, tree, i, j),\n decreases j - i,\n {\n match tree {\n SplitTree::Leaf => {\n assert(j - i < 2);\n },\n SplitTree::Node(k0, left_tree, right_tree) => {\n Self::lemma_min_score_le_any(values, i, k0, *left_tree);\n Self::lemma_min_score_le_any(values, k0, j, *right_tree);\n Self::lemma_min_score_k_le_tree(\n values, i, j, i + 1, tree, k0, *left_tree, *right_tree);\n },\n }\n }\n\n \n proof fn lemma_min_score_k_achieved(\n values: Seq, i: int, j: int, k: int,\n ) -> (best_k: int)\n requires\n 0 <= i, i < k, k < j, j < values.len() as int,\n j - i >= 2, values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n ensures\n i < best_k < j,\n Self::min_score_k(values, i, j, k) ==\n Self::min_score(values, i, best_k) + Self::min_score(values, best_k, j)\n + values[i] as int * values[best_k] as int * values[j] as int,\n decreases j - k,\n {\n let cur = Self::min_score(values, i, k) + Self::min_score(values, k, j)\n + values[i] as int * values[k] as int * values[j] as int;\n if k + 1 >= j {\n k\n } else {\n let rest = Self::min_score_k(values, i, j, k + 1);\n if cur <= rest {\n k\n } else {\n Self::lemma_min_score_k_achieved(values, i, j, k + 1)\n }\n }\n }\n\n \n proof fn lemma_min_score_k_val(\n values: Seq, i: int, j: int, start: int, k0: int,\n )\n requires\n 0 <= i, i < start, start <= k0, k0 < j, j < values.len() as int,\n j - i >= 2, values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n ensures\n Self::min_score_k(values, i, j, start) <=\n Self::min_score(values, i, k0) + Self::min_score(values, k0, j)\n + values[i] as int * values[k0] as int * values[j] as int,\n decreases j - start,\n {\n let cur = Self::min_score(values, i, start) + Self::min_score(values, start, j)\n + values[i] as int * values[start] as int * values[j] as int;\n if start == k0 {\n if start + 1 >= j {\n } else {\n assert(Self::min_score_k(values, i, j, start)\n == Self::min2(cur, Self::min_score_k(values, i, j, start + 1)));\n assert(Self::min2(cur, Self::min_score_k(values, i, j, start + 1)) <= cur);\n }\n } else {\n if start + 1 >= j {\n } else {\n Self::lemma_min_score_k_val(values, i, j, start + 1, k0);\n let rest = Self::min_score_k(values, i, j, start + 1);\n assert(Self::min_score_k(values, i, j, start)\n == Self::min2(cur, rest));\n assert(Self::min2(cur, rest) <= rest);\n }\n }\n }\n\n \n proof fn lemma_min_score_achieved(values: Seq, i: int, j: int)\n -> (tree: SplitTree)\n requires\n 0 <= i <= j, j < values.len() as int, values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n ensures\n split_tree_valid(tree, i, j),\n split_tree_score(values, tree, i, j) == Self::min_score(values, i, j),\n decreases j - i,\n {\n if j - i < 2 {\n SplitTree::Leaf\n } else {\n let best_k = Self::lemma_min_score_k_achieved(values, i, j, i + 1);\n let left_tree = Self::lemma_min_score_achieved(values, i, best_k);\n let right_tree = Self::lemma_min_score_achieved(values, best_k, j);\n SplitTree::Node(best_k, Box::new(left_tree), Box::new(right_tree))\n }\n }\n\n \n proof fn lemma_bridge(values: Seq)\n requires\n 3 <= values.len() <= 50,\n forall|kk: int| 0 <= kk < values.len() ==> 1 <= #[trigger] values[kk] <= 100,\n ensures\n exists|tree: SplitTree|\n #[trigger] split_tree_valid(tree, 0, (values.len() - 1) as int)\n && split_tree_score(values, tree, 0, (values.len() - 1) as int)\n == Self::min_score(values, 0, (values.len() - 1) as int),\n forall|tree: SplitTree|\n split_tree_valid(tree, 0, (values.len() - 1) as int)\n ==> Self::min_score(values, 0, (values.len() - 1) as int)\n <= #[trigger] split_tree_score(\n values, tree, 0, (values.len() - 1) as int),\n {\n let n = values.len() as int;\n let witness = Self::lemma_min_score_achieved(values, 0, n - 1);\n assert(split_tree_valid(witness, 0, n - 1));\n assert(split_tree_score(values, witness, 0, n - 1)\n == Self::min_score(values, 0, n - 1));\n assert forall|tree: SplitTree|\n split_tree_valid(tree, 0, n - 1)\n implies Self::min_score(values, 0, n - 1)\n <= #[trigger] split_tree_score(values, tree, 0, n - 1) by {\n Self::lemma_min_score_le_any(values, 0, n - 1, tree);\n };\n }\n\n fn triple_product(a: i32, b: i32, c: i32) -> (r: i32)\n requires 1 <= a <= 100, 1 <= b <= 100, 1 <= c <= 100,\n ensures r as int == a as int * b as int * c as int,\n 1 <= r <= 1_000_000,\n {\n proof {\n assert(a as int * b as int <= 10000) by (nonlinear_arith)\n requires 1 <= a as int <= 100, 1 <= b as int <= 100, {}\n assert(a as int * b as int >= 1) by (nonlinear_arith)\n requires a as int >= 1, b as int >= 1, {}\n }\n let ab = a * b;\n proof {\n assert(ab as int * c as int <= 1_000_000) by (nonlinear_arith)\n requires 1 <= ab as int <= 10000, 1 <= c as int <= 100, {}\n assert(ab as int * c as int >= 1) by (nonlinear_arith)\n requires ab as int >= 1, c as int >= 1, {}\n }\n ab * c\n }\n\n pub fn min_score_triangulation(values: Vec) -> (res: i32)\n requires\n 3 <= values.len() <= 50,\n forall|i: int| 0 <= i < values.len() ==> 1 <= #[trigger] values[i] <= 100,\n ensures\n exists|tree: SplitTree|\n #[trigger] split_tree_valid(tree, 0, (values.len() - 1) as int)\n && split_tree_score(values@, tree, 0, (values.len() - 1) as int) == res as int,\n forall|tree: SplitTree|\n split_tree_valid(tree, 0, (values.len() - 1) as int)\n ==> res as int <= #[trigger] split_tree_score(\n values@, tree, 0, (values.len() - 1) as int),\n {\n let n = values.len();\n assert(n * n <= 2500) by (nonlinear_arith) requires 3 <= n <= 50, {}\n let nn = n * n;\n let mut dp: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < nn\n invariant\n dp@.len() == idx as int, idx <= nn,\n nn == n * n, nn <= 2500, n == values.len(), 3 <= n <= 50,\n forall|i: int| 0 <= i < idx as int ==> dp@[i] == 0i32,\n decreases nn - idx,\n {\n dp.push(0i32);\n idx = idx + 1;\n }\n proof {\n assert forall|ii: int, jj: int|\n 0 <= ii < n as int && 0 <= jj < n as int && 0 <= jj - ii < 2\n implies dp@[ii * (n as int) + jj] as int == Self::min_score(values@, ii, jj) by {\n Self::lemma_idx_bound(ii, jj, n as int);\n };\n }\n let mut gap = 2usize;\n while gap < n\n invariant\n dp@.len() == nn as int, nn == n * n, nn <= 2500,\n n == values.len(), 3 <= n <= 50, 2 <= gap <= n,\n forall|ii: int| 0 <= ii < values@.len() ==> 1 <= #[trigger] values@[ii] <= 100,\n forall|ii: int, jj: int|\n 0 <= ii < n as int && 0 <= jj < n as int && 0 <= jj - ii < gap as int\n ==> dp@[ii * (n as int) + jj] as int == Self::min_score(values@, ii, jj),\n decreases n - gap,\n {\n let mut i = 0usize;\n while i + gap < n\n invariant\n dp@.len() == nn as int, nn == n * n, nn <= 2500,\n n == values.len(), 3 <= n <= 50,\n 2 <= gap < n, 0 <= i, i + gap <= n,\n forall|ii: int| 0 <= ii < values@.len() ==> 1 <= #[trigger] values@[ii] <= 100,\n forall|ii: int, jj: int|\n 0 <= ii < n as int && 0 <= jj < n as int && 0 <= jj - ii < gap as int\n ==> dp@[ii * (n as int) + jj] as int == Self::min_score(values@, ii, jj),\n forall|ii: int|\n 0 <= ii < i as int && ii + (gap as int) < n as int\n ==> #[trigger] dp@[ii * (n as int) + ii + gap as int] as int\n == Self::min_score(values@, ii, ii + gap as int),\n decreases n - gap - i,\n {\n let j = i + gap;\n let k_first: usize = i + 1;\n proof {\n let ii = i as int;\n let kf = k_first as int;\n let jj = j as int;\n let nn_i = n as int;\n Self::lemma_idx_bound(ii, kf, nn_i);\n Self::lemma_idx_bound(kf, jj, nn_i);\n assert(0 <= kf - ii < gap as int);\n assert(0 <= jj - kf < gap as int);\n assert(dp@[ii * nn_i + kf] as int == Self::min_score(values@, ii, kf));\n assert(dp@[kf * nn_i + jj] as int == Self::min_score(values@, kf, jj));\n Self::lemma_dp_entry_bound(values@, dp@, nn_i, ii, kf);\n Self::lemma_dp_entry_bound(values@, dp@, nn_i, kf, jj);\n }\n let dl = dp[i * n + k_first];\n let dr = dp[k_first * n + j];\n let prod = Self::triple_product(values[i], values[k_first], values[j]);\n let score_first = dl + dr + prod;\n let mut best: i32 = score_first;\n proof {\n let ii = i as int;\n let jj = j as int;\n let kk = k_first as int;\n let cur_spec = Self::min_score(values@, ii, kk) + Self::min_score(values@, kk, jj)\n + values@[ii] as int * values@[kk] as int * values@[jj] as int;\n assert(best as int == cur_spec);\n Self::lemma_min_score_k_bounds(values@, ii, jj, ii + 1);\n Self::lemma_min_score_bounds(values@, ii, jj);\n assert(1 <= Self::min_score(values@, ii, jj) <= (jj - ii - 1) * 1_000_000);\n if gap as int == 2 {\n assert(kk + 1 >= jj);\n assert(Self::min_score_k(values@, ii, jj, kk) == cur_spec);\n assert(Self::min_score(values@, ii, jj) == cur_spec);\n assert(Self::min_score_k(values@, ii, jj, kk + 1) == i32::MAX as int);\n assert(Self::min_score_k(values@, ii, jj, ii + 1) == Self::min2(\n best as int, Self::min_score_k(values@, ii, jj, kk + 1)));\n } else {\n assert(kk + 1 < jj);\n assert(Self::min_score_k(values@, ii, jj, kk) == Self::min2(\n cur_spec, Self::min_score_k(values@, ii, jj, kk + 1)));\n assert(Self::min_score_k(values@, ii, jj, ii + 1) == Self::min2(\n best as int, Self::min_score_k(values@, ii, jj, kk + 1)));\n }\n Self::lemma_min_score_bounds(values@, ii, kk);\n Self::lemma_min_score_bounds(values@, kk, jj);\n let ms_left = Self::min_score(values@, ii, kk);\n let ms_right = Self::min_score(values@, kk, jj);\n assert(ms_left == 0);\n assert(ms_right >= 0) by { if jj - kk < 2 { assert(ms_right == 0); } }\n assert(ms_right <= (jj - kk - 1) * 1_000_000) by {\n if jj - kk < 2 { assert(ms_right == 0); assert(jj - kk >= 1); }\n }\n assert(1 <= prod as int <= 1_000_000);\n assert(cur_spec >= 1);\n assert(cur_spec <= (jj - ii - 1) * 1_000_000) by (nonlinear_arith)\n requires\n ms_left == 0, ms_right <= (jj - kk - 1) * 1_000_000,\n prod as int <= 1_000_000, cur_spec == ms_left + ms_right + prod as int,\n kk == ii + 1, jj - kk >= 1,\n {}\n assert(1 <= best as int <= (jj - ii - 1) * 1_000_000);\n }\n let mut k: usize = i + 2;\n while k < j\n invariant\n i + 2 <= k <= j, j == i + gap,\n 2 <= gap < n, dp@.len() == nn as int,\n nn == n * n, nn <= 2500, n == values.len(), 3 <= n <= 50,\n 0 <= i, i + gap < n,\n forall|ii: int| 0 <= ii < values@.len() ==> 1 <= #[trigger] values@[ii] <= 100,\n forall|ii: int, jj: int|\n 0 <= ii < n as int && 0 <= jj < n as int && 0 <= jj - ii < gap as int\n ==> dp@[ii * (n as int) + jj] as int == Self::min_score(values@, ii, jj),\n Self::min_score_k(values@, i as int, j as int, i as int + 1)\n == Self::min2(best as int, Self::min_score_k(values@, i as int, j as int, k as int)),\n 1 <= best as int <= (gap as int - 1) * 1_000_000,\n decreases j - k,\n {\n proof {\n let ii = i as int;\n let kk = k as int;\n let jj = j as int;\n let nn_i = n as int;\n assert(0 < kk - ii < gap as int);\n assert(0 < jj - kk < gap as int);\n Self::lemma_idx_bound(ii, kk, nn_i);\n Self::lemma_idx_bound(kk, jj, nn_i);\n assert(dp@[ii * nn_i + kk] as int == Self::min_score(values@, ii, kk));\n assert(dp@[kk * nn_i + jj] as int == Self::min_score(values@, kk, jj));\n Self::lemma_dp_entry_bound(values@, dp@, nn_i, ii, kk);\n Self::lemma_dp_entry_bound(values@, dp@, nn_i, kk, jj);\n }\n let dl2 = dp[i * n + k];\n let dr2 = dp[k * n + j];\n let prod2 = Self::triple_product(values[i], values[k], values[j]);\n let score = dl2 + dr2 + prod2;\n let ghost old_best = best as int;\n if score < best {\n best = score;\n }\n proof {\n let ii = i as int;\n let jj = j as int;\n let kk = k as int;\n let new_best = best as int;\n let cur_spec = Self::min_score(values@, ii, kk) + Self::min_score(values@, kk, jj)\n + values@[ii] as int * values@[kk] as int * values@[jj] as int;\n assert(score as int == cur_spec);\n assert(new_best == Self::min2(old_best, score as int));\n if kk + 1 < jj {\n assert(Self::min_score_k(values@, ii, jj, kk) == Self::min2(\n cur_spec, Self::min_score_k(values@, ii, jj, kk + 1)));\n Self::lemma_min_assoc(\n old_best, cur_spec, Self::min_score_k(values@, ii, jj, kk + 1));\n assert(Self::min_score_k(values@, ii, jj, ii + 1) == Self::min2(\n new_best, Self::min_score_k(values@, ii, jj, kk + 1)));\n } else {\n assert(Self::min_score_k(values@, ii, jj, kk) == cur_spec);\n assert(Self::min_score_k(values@, ii, jj, kk + 1) == i32::MAX as int);\n assert(Self::min2(new_best, i32::MAX as int) == new_best);\n }\n Self::lemma_min_score_k_bounds(values@, ii, jj, ii + 1);\n Self::lemma_min_score_bounds(values@, ii, jj);\n }\n k = k + 1;\n }\n proof {\n let ii = i as int;\n let jj = j as int;\n assert(k as int == jj);\n assert(Self::min_score_k(values@, ii, jj, jj) == i32::MAX as int);\n assert(Self::min2(best as int, i32::MAX as int) == best as int);\n assert(best as int == Self::min_score(values@, ii, jj));\n Self::lemma_idx_bound(ii, jj, n as int);\n }\n let ghost old_dp = dp@;\n dp.set(i * n + j, best);\n proof {\n let ii = i as int;\n let jj = j as int;\n let nn_i = n as int;\n assert forall|a: int, b: int|\n 0 <= a < nn_i && 0 <= b < nn_i && 0 <= b - a < gap as int\n implies dp@[a * nn_i + b] as int == Self::min_score(values@, a, b) by {\n Self::lemma_idx_bound(a, b, nn_i);\n Self::lemma_idx_bound(ii, jj, nn_i);\n if a * nn_i + b == ii * nn_i + jj {\n Self::lemma_idx_injective(a, b, ii, jj, nn_i);\n assert(b - a == gap as int);\n } else {\n assert(dp@[a * nn_i + b] == old_dp[a * nn_i + b]);\n }\n };\n assert forall|a: int|\n 0 <= a < ii + 1 && a + (gap as int) < nn_i\n implies #[trigger] dp@[a * nn_i + a + gap as int] as int\n == Self::min_score(values@, a, a + gap as int) by {\n Self::lemma_idx_bound(a, a + gap as int, nn_i);\n Self::lemma_idx_bound(ii, jj, nn_i);\n if a == ii {\n assert(dp@[a * nn_i + a + gap as int] == best);\n } else {\n if a * nn_i + a + gap as int == ii * nn_i + jj {\n Self::lemma_idx_injective(a, a + gap as int, ii, jj, nn_i);\n } else {\n assert(dp@[a * nn_i + a + gap as int]\n == old_dp[a * nn_i + a + gap as int]);\n }\n }\n };\n }\n i = i + 1;\n }\n proof {\n let nn_i = n as int;\n let gg = gap as int;\n assert forall|ii: int, jj: int|\n 0 <= ii < nn_i && 0 <= jj < nn_i && 0 <= jj - ii < gg + 1\n implies dp@[ii * nn_i + jj] as int == Self::min_score(values@, ii, jj) by {\n Self::lemma_idx_bound(ii, jj, nn_i);\n if jj - ii < gg {} else {\n assert(jj - ii == gg);\n assert(jj == ii + gg);\n assert(ii + gg < nn_i);\n assert(ii < i as int) by {\n assert(i + gap >= n);\n assert(ii + (gap as int) < (n as int));\n }\n assert(dp@[ii * (n as int) + ii + gap as int] as int\n == Self::min_score(values@, ii, ii + gap as int));\n }\n };\n }\n gap = gap + 1;\n }\n proof {\n let nn_i = n as int;\n assert(nn_i - 1 < gap as int);\n Self::lemma_idx_bound(0, nn_i - 1, nn_i);\n assert(dp@[0 * nn_i + nn_i - 1] as int == Self::min_score(values@, 0, nn_i - 1));\n assert(0 * nn_i == 0) by (nonlinear_arith) {}\n Self::lemma_bridge(values@);\n }\n dp[n - 1]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1039/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1039/tests/mutated_testcases.jsonl"} {"problem": "lc1040", "family": "leetcode", "description": "# Moving Stones Until Consecutive II\n\nThere are some stones in different positions on the X-axis. You are given an integer array `stones`, the positions of the stones.\n\nCall a stone an **endpoint stone** if it has the smallest or largest position. In one move, you pick up an **endpoint stone** and move it to an unoccupied position so that it is no longer an **endpoint stone**.\n\n- In particular, if the stones are at say, `stones = [1,2,5]`, you cannot move the endpoint stone at position `5`, since moving it to any position (such as `0`, or `3`) will still keep that stone as an endpoint stone.\n\nThe game ends when you cannot make any more moves (i.e., the stones are in consecutive positions).\n\nReturn *an integer array* `answer` *of length* `2` *where*:\n\n- `answer[0]` *is the minimum number of moves you can play, and*\n- `answer[1]` *is the maximum number of moves you can play*.\n\n## Example 1:\n\n> **Input:** stones = [7,4,9]\n> **Output:** [1,2]\n> **Explanation:** We can move 4 -> 8 for one move to finish the game. Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.\n\n## Example 2:\n\n> **Input:** stones = [6,5,4,3,10]\n> **Output:** [2,3]\n> **Explanation:** We can move 3 -> 8 then 10 -> 7 to finish the game. Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game. Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.\n\n## Constraints:\n\n- `3 <= stones.length <= 10^4`\n- `1 <= stones[i] <= 10^9`\n- All the values of `stones` are **unique**.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_moves_stones_ii(stones: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Sliding Window, Sorting\nAcceptance rate: 58.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i < j < s.len() ==> s[i] < s[j]\n}\n\npub open spec fn max_of(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn min_of(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn same_multiset(left: Seq, right: Seq) -> bool {\n left.len() == right.len() && left.to_multiset() =~= right.to_multiset()\n}\n\n\n\n\n\n\n\npub open spec fn compute_max_moves(s: Seq) -> int {\n let n = s.len() as int;\n max_of(\n (s[n - 1] as int) - (s[1] as int) - n + 2,\n (s[n - 2] as int) - (s[0] as int) - n + 2,\n )\n}\n\n\n\n\n\npub open spec fn left_bound(s: Seq, j: int, start: int) -> int\n decreases j - start + 1\n{\n if start > j {\n j\n } else if (s[j] as int) - (s[start] as int) < (s.len() as int) {\n start\n } else {\n left_bound(s, j, start + 1)\n }\n}\n\n\n\n\n\npub open spec fn cost_at(s: Seq, j: int) -> int {\n let n = s.len() as int;\n let i = left_bound(s, j, 0);\n let count = j - i + 1;\n if count == n - 1 && (s[j] as int) - (s[i] as int) == n - 2 {\n 2int\n } else {\n n - count\n }\n}\n\n\n\n\n\n\n\n\n\npub open spec fn min_cost(s: Seq, j: int) -> int\n decreases j + 1\n{\n if j < 0 {\n s.len() as int\n } else {\n min_of(cost_at(s, j), min_cost(s, j - 1))\n }\n}\n\nimpl Solution {\n pub fn num_moves_stones_ii(stones: Vec) -> (result: Vec)\n requires\n stones@.len() >= 3,\n stones@.len() <= 10000,\n forall |i: int, j: int| 0 <= i < j < stones@.len() ==>\n stones@[i] != stones@[j],\n forall |i: int| 0 <= i < stones@.len() ==>\n 1 <= #[trigger] stones[i] <= 1_000_000_000i32,\n ensures\n result@.len() == 2,\n exists |sorted_s: Seq|\n is_sorted(sorted_s)\n && same_multiset(sorted_s, stones@)\n && result[0] as int == min_cost(sorted_s, sorted_s.len() as int - 1)\n && result[1] as int == compute_max_moves(sorted_s),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn num_moves_stones_ii(stones: Vec) -> Vec {\n let mut stones = stones;\n let n = stones.len();\n\n let mut si: usize = 1;\n while si < n {\n let mut sj: usize = si;\n while sj > 0 && stones[sj - 1] > stones[sj] {\n let tmp = stones[sj];\n stones[sj] = stones[sj - 1];\n stones[sj - 1] = tmp;\n sj = sj - 1;\n }\n si = si + 1;\n }\n\n let max_left = stones[n - 1] - stones[1] - n as i32 + 2;\n let max_right = stones[n - 2] - stones[0] - n as i32 + 2;\n let max_moves = if max_left >= max_right { max_left } else { max_right };\n\n let mut min_moves: i32 = n as i32;\n let mut i: usize = 0;\n let mut j: usize = 0;\n\n while j < n {\n while stones[j] - stones[i] >= n as i32 {\n i = i + 1;\n }\n let count = (j - i + 1) as i32;\n if count == n as i32 - 1 && stones[j] - stones[i] == n as i32 - 2 {\n if 2 < min_moves {\n min_moves = 2;\n }\n } else {\n let cost = n as i32 - count;\n if cost < min_moves {\n min_moves = cost;\n }\n }\n j = j + 1;\n }\n\n let mut result = Vec::new();\n result.push(min_moves);\n result.push(max_moves);\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i < j < s.len() ==> s[i] < s[j]\n}\n\npub open spec fn max_of(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn min_of(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn same_multiset(left: Seq, right: Seq) -> bool {\n left.len() == right.len() && left.to_multiset() =~= right.to_multiset()\n}\n\n\n\n\n\n\n\npub open spec fn compute_max_moves(s: Seq) -> int {\n let n = s.len() as int;\n max_of(\n (s[n - 1] as int) - (s[1] as int) - n + 2,\n (s[n - 2] as int) - (s[0] as int) - n + 2,\n )\n}\n\n\n\n\n\npub open spec fn left_bound(s: Seq, j: int, start: int) -> int\n decreases j - start + 1\n{\n if start > j {\n j\n } else if (s[j] as int) - (s[start] as int) < (s.len() as int) {\n start\n } else {\n left_bound(s, j, start + 1)\n }\n}\n\n\n\n\n\npub open spec fn cost_at(s: Seq, j: int) -> int {\n let n = s.len() as int;\n let i = left_bound(s, j, 0);\n let count = j - i + 1;\n if count == n - 1 && (s[j] as int) - (s[i] as int) == n - 2 {\n 2int\n } else {\n n - count\n }\n}\n\n\n\n\n\n\n\n\n\npub open spec fn min_cost(s: Seq, j: int) -> int\n decreases j + 1\n{\n if j < 0 {\n s.len() as int\n } else {\n min_of(cost_at(s, j), min_cost(s, j - 1))\n }\n}\n\nimpl Solution {\n pub fn num_moves_stones_ii(stones: Vec) -> (result: Vec)\n requires\n stones@.len() >= 3,\n stones@.len() <= 10000,\n forall |i: int, j: int| 0 <= i < j < stones@.len() ==>\n stones@[i] != stones@[j],\n forall |i: int| 0 <= i < stones@.len() ==>\n 1 <= #[trigger] stones[i] <= 1_000_000_000i32,\n ensures\n result@.len() == 2,\n exists |sorted_s: Seq|\n is_sorted(sorted_s)\n && same_multiset(sorted_s, stones@)\n && result[0] as int == min_cost(sorted_s, sorted_s.len() as int - 1)\n && result[1] as int == compute_max_moves(sorted_s),\n {\n let mut stones = stones;\n let n = stones.len();\n\n let mut si: usize = 1;\n while si < n {\n let mut sj: usize = si;\n while sj > 0 && stones[sj - 1] > stones[sj] {\n let tmp = stones[sj];\n stones.set(sj, stones[sj - 1]);\n stones.set(sj - 1, tmp);\n sj = sj - 1;\n }\n si = si + 1;\n }\n\n let max_left = stones[n - 1] - stones[1] - n as i32 + 2;\n let max_right = stones[n - 2] - stones[0] - n as i32 + 2;\n let max_moves = if max_left >= max_right { max_left } else { max_right };\n\n let mut min_moves: i32 = n as i32;\n let mut i: usize = 0;\n let mut j: usize = 0;\n\n while j < n {\n while stones[j] - stones[i] >= n as i32 {\n i = i + 1;\n }\n let count = (j - i + 1) as i32;\n if count == n as i32 - 1 && stones[j] - stones[i] == n as i32 - 2 {\n if 2 < min_moves {\n min_moves = 2;\n }\n } else {\n let cost = n as i32 - count;\n if cost < min_moves {\n min_moves = cost;\n }\n }\n j = j + 1;\n }\n\n let mut result = Vec::new();\n result.push(min_moves);\n result.push(max_moves);\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\nuse vstd::seq_lib::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i < j < s.len() ==> s[i] < s[j]\n}\n\npub open spec fn max_of(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn min_of(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn same_multiset(left: Seq, right: Seq) -> bool {\n left.len() == right.len() && left.to_multiset() =~= right.to_multiset()\n}\n\n\n\n\n\n\n\npub open spec fn compute_max_moves(s: Seq) -> int {\n let n = s.len() as int;\n max_of(\n (s[n - 1] as int) - (s[1] as int) - n + 2,\n (s[n - 2] as int) - (s[0] as int) - n + 2,\n )\n}\n\n\n\n\n\npub open spec fn left_bound(s: Seq, j: int, start: int) -> int\n decreases j - start + 1\n{\n if start > j {\n j\n } else if (s[j] as int) - (s[start] as int) < (s.len() as int) {\n start\n } else {\n left_bound(s, j, start + 1)\n }\n}\n\n\n\n\n\npub open spec fn cost_at(s: Seq, j: int) -> int {\n let n = s.len() as int;\n let i = left_bound(s, j, 0);\n let count = j - i + 1;\n if count == n - 1 && (s[j] as int) - (s[i] as int) == n - 2 {\n 2int\n } else {\n n - count\n }\n}\n\n\n\n\n\n\n\n\n\npub open spec fn min_cost(s: Seq, j: int) -> int\n decreases j + 1\n{\n if j < 0 {\n s.len() as int\n } else {\n min_of(cost_at(s, j), min_cost(s, j - 1))\n }\n}\n\nproof fn lemma_multiset_swap(seq: Seq, i: int, j: int)\n requires\n 0 <= i < seq.len(),\n 0 <= j < seq.len(),\n i != j,\n ensures\n seq.update(i, seq[j]).update(j, seq[i]).to_multiset() =~= seq.to_multiset(),\n{\n broadcast use group_to_multiset_ensures;\n let after_first = seq.update(i, seq[j]);\n assert(after_first.to_multiset() =~= seq.to_multiset().insert(seq[j]).remove(seq[i]));\n assert(after_first[j] == seq[j]);\n assert(after_first.update(j, seq[i]).to_multiset()\n =~= after_first.to_multiset().insert(seq[i]).remove(after_first[j]));\n}\n\nproof fn left_bound_skip(s: Seq, j: int, skip: int, start: int)\n requires\n 0 <= start <= skip <= j < s.len(),\n s.len() >= 3,\n forall |k: int| start <= k < skip ==>\n (s[j] as int) - (s[k] as int) >= (s.len() as int),\n ensures\n left_bound(s, j, start) == left_bound(s, j, skip),\n decreases skip - start\n{\n if start < skip {\n left_bound_skip(s, j, skip, start + 1);\n }\n}\n\nproof fn left_bound_in_range(s: Seq, j: int, start: int)\n requires\n is_sorted(s),\n 0 <= start <= j < s.len(),\n s.len() >= 1,\n ensures\n start <= left_bound(s, j, start) <= j,\n decreases j - start + 1\n{\n if !((s[j] as int) - (s[start] as int) < (s.len() as int)) {\n if start < j {\n left_bound_in_range(s, j, start + 1);\n }\n }\n}\n\nimpl Solution {\n pub fn num_moves_stones_ii(stones: Vec) -> (result: Vec)\n requires\n stones@.len() >= 3,\n stones@.len() <= 10000,\n forall |i: int, j: int| 0 <= i < j < stones@.len() ==>\n stones@[i] != stones@[j],\n forall |i: int| 0 <= i < stones@.len() ==>\n 1 <= #[trigger] stones[i] <= 1_000_000_000i32,\n ensures\n result@.len() == 2,\n exists |sorted_s: Seq|\n is_sorted(sorted_s)\n && same_multiset(sorted_s, stones@)\n && result[0] as int == min_cost(sorted_s, sorted_s.len() as int - 1)\n && result[1] as int == compute_max_moves(sorted_s),\n {\n let ghost original = stones@;\n let mut stones = stones;\n let n = stones.len();\n\n \n let mut si: usize = 1;\n while si < n\n invariant\n 1 <= si <= n,\n n == stones.len(),\n n >= 3,\n n <= 10000,\n stones@.to_multiset() =~= original.to_multiset(),\n forall |a: int, b: int| 0 <= a < b < si as int ==>\n stones@[a] < stones@[b],\n forall |a: int, b: int| 0 <= a < b < n as int ==>\n stones@[a] != stones@[b],\n forall |k: int| 0 <= k < n as int ==>\n 1 <= #[trigger] stones@[k] <= 1_000_000_000i32,\n decreases n - si\n {\n let mut sj: usize = si;\n while sj > 0 && stones[sj - 1] > stones[sj]\n invariant\n 0 <= sj <= si,\n si < n,\n n == stones.len(),\n n >= 3,\n n <= 10000,\n stones@.to_multiset() =~= original.to_multiset(),\n forall |a: int, b: int| 0 <= a < b < sj as int ==>\n stones@[a] < stones@[b],\n forall |a: int, b: int| sj as int <= a && a < b && b <= si as int ==>\n stones@[a] < stones@[b],\n forall |a: int, b: int|\n (0 <= a && a < (sj as int) && (sj as int) < b && b <= (si as int)) ==>\n stones@[a] < stones@[b],\n forall |a: int, b: int| 0 <= a < b < n as int ==>\n stones@[a] != stones@[b],\n forall |k: int| 0 <= k < n as int ==>\n 1 <= #[trigger] stones@[k] <= 1_000_000_000i32,\n decreases sj\n {\n proof {\n lemma_multiset_swap(stones@, sj as int, sj as int - 1);\n }\n let tmp = stones[sj];\n stones.set(sj, stones[sj - 1]);\n stones.set(sj - 1, tmp);\n sj = sj - 1;\n }\n\n proof {\n if sj > 0 {\n assert(stones@[(sj - 1) as int] != stones@[sj as int]);\n assert forall |a: int, b: int|\n 0 <= a && a < b && b <= si as int\n implies stones@[a] < stones@[b] by {\n if b < sj as int {\n } else if a >= sj as int {\n } else if b > sj as int {\n } else {\n assert(b == sj as int);\n if a < sj as int - 1 {\n assert(stones@[a] < stones@[(sj - 1) as int]);\n }\n }\n };\n }\n }\n\n si = si + 1;\n }\n\n proof {\n assert(is_sorted(stones@));\n assert(same_multiset(stones@, original));\n }\n\n let ghost s = stones@;\n\n let max_left = stones[n - 1] - stones[1] - n as i32 + 2;\n let max_right = stones[n - 2] - stones[0] - n as i32 + 2;\n let max_moves = if max_left >= max_right { max_left } else { max_right };\n\n let mut min_moves: i32 = n as i32;\n let mut i: usize = 0;\n let mut j: usize = 0;\n\n while j < n\n invariant\n 0 <= j <= n,\n 0 <= i <= j,\n n == stones@.len(),\n n >= 3,\n n <= 10000,\n s =~= stones@,\n is_sorted(s),\n forall |k: int| 0 <= k < stones@.len() ==>\n 1 <= #[trigger] stones[k] <= 1_000_000_000i32,\n min_moves as int == min_cost(s, j as int - 1),\n 0 <= min_moves <= n as i32,\n j < n ==> forall |k: int|\n 0 <= k < i as int ==>\n (s[j as int] as int) - (s[k] as int) >= (n as int),\n decreases n - j\n {\n while stones[j] - stones[i] >= n as i32\n invariant\n 0 <= i <= j,\n j < n,\n n == stones@.len(),\n n >= 3,\n n <= 10000,\n s =~= stones@,\n is_sorted(s),\n forall |k: int| 0 <= k < stones@.len() ==>\n 1 <= #[trigger] stones[k] <= 1_000_000_000i32,\n forall |k: int|\n 0 <= k < i as int ==>\n (s[j as int] as int) - (s[k] as int) >= (n as int),\n decreases j - i\n {\n i = i + 1;\n }\n\n proof {\n left_bound_skip(s, j as int, i as int, 0);\n assert(left_bound(s, j as int, 0) == i as int);\n }\n\n let count = (j - i + 1) as i32;\n\n if count == n as i32 - 1 && stones[j] - stones[i] == n as i32 - 2 {\n if 2 < min_moves {\n min_moves = 2;\n }\n } else {\n let cost = n as i32 - count;\n if cost < min_moves {\n min_moves = cost;\n }\n }\n\n proof {\n assert(min_moves as int == min_of(\n cost_at(s, j as int), min_cost(s, j as int - 1)));\n }\n\n j = j + 1;\n\n proof {\n if j < n {\n assert forall |k: int| 0 <= k < i as int\n implies (s[j as int] as int) - (s[k] as int) >= (n as int)\n by {\n assert(s[j as int] > s[(j - 1) as int]);\n }\n }\n }\n }\n\n let mut result = Vec::new();\n result.push(min_moves);\n result.push(max_moves);\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1040/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1040/tests/mutated_testcases.jsonl"} {"problem": "lc1073", "family": "leetcode", "description": "# Adding Two Negabinary Numbers\n\nGiven two numbers `arr1` and `arr2` in base **-2**, return the result of adding them together.\n\nEach number is given in *array format*: as an array of 0s and 1s, from most significant bit to least significant bit. For example, `arr = [1,1,0,1]` represents the number `(-2)^3 + (-2)^2 + (-2)^0 = -3`. A number `arr` in *array format* is also guaranteed to have no leading zeros: either `arr == [0]` or `arr[0] == 1`.\n\nReturn the result of adding `arr1` and `arr2` in the same format: as an array of 0s and 1s with no leading zeros.\n\n## Example 1:\n\n> **Input:** arr1 = [1,1,1,1,1], arr2 = [1,0,1]\n> **Output:** [1,0,0,0,0]\n> **Explanation:** arr1 represents 11, arr2 represents 5, the output represents 16.\n\n## Example 2:\n\n> **Input:** arr1 = [0], arr2 = [0]\n> **Output:** [0]\n\n## Example 3:\n\n> **Input:** arr1 = [0], arr2 = [1]\n> **Output:** [1]\n\n## Constraints:\n\n- `1 <= arr1.length, arr2.length <= 1000`\n- `arr1[i]` and `arr2[i]` are `0` or `1`\n- `arr1` and `arr2` have no leading zeros\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn add_negabinary(arr1: Vec, arr2: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math\nAcceptance rate: 37.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn negabinary_val(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0int\n } else {\n s.last() as int + (-2int) * negabinary_val(s.drop_last())\n }\n}\n\nimpl Solution {\n pub fn add_negabinary(arr1: Vec, arr2: Vec) -> (result: Vec)\n requires\n 1 <= arr1.len() <= 1000,\n 1 <= arr2.len() <= 1000,\n forall|i: int| 0 <= i < arr1.len() ==> (#[trigger] arr1[i] == 0 || arr1[i] == 1),\n forall|i: int| 0 <= i < arr2.len() ==> (#[trigger] arr2[i] == 0 || arr2[i] == 1),\n arr1.len() == 1 || arr1[0] == 1,\n arr2.len() == 1 || arr2[0] == 1,\n ensures\n result.len() >= 1,\n forall|i: int|\n 0 <= i < result.len() ==> (#[trigger] result[i] == 0 || result[i] == 1),\n result.len() == 1 || result[0] == 1,\n negabinary_val(result@) == negabinary_val(arr1@) + negabinary_val(arr2@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn add_negabinary(arr1: Vec, arr2: Vec) -> Vec {\n let n1 = arr1.len();\n let n2 = arr2.len();\n let max_len = if n1 >= n2 { n1 } else { n2 };\n let max_iters = max_len + 3;\n let mut res: Vec = Vec::new();\n let mut carry: i32 = 0;\n let mut k: usize = 0;\n while k < max_iters && (k < n1 || k < n2 || carry != 0) {\n let a = if k < n1 { arr1[n1 - 1 - k] } else { 0 };\n let b = if k < n2 { arr2[n2 - 1 - k] } else { 0 };\n let sum = carry + a + b;\n let bit: i32;\n let new_carry: i32;\n if sum >= 2 {\n bit = sum - 2;\n new_carry = -1;\n } else if sum < 0 {\n bit = sum + 2;\n new_carry = 1;\n } else {\n bit = sum;\n new_carry = 0;\n }\n res.push(bit);\n carry = new_carry;\n k = k + 1;\n }\n let mut end = res.len();\n while end > 1 && res[end - 1] == 0 {\n end = end - 1;\n }\n let mut result: Vec = Vec::new();\n let mut i = end;\n while i > 0 {\n i = i - 1;\n result.push(res[i]);\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn negabinary_val(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0int\n } else {\n s.last() as int + (-2int) * negabinary_val(s.drop_last())\n }\n}\n\nimpl Solution {\n pub fn add_negabinary(arr1: Vec, arr2: Vec) -> (result: Vec)\n requires\n 1 <= arr1.len() <= 1000,\n 1 <= arr2.len() <= 1000,\n forall|i: int| 0 <= i < arr1.len() ==> (#[trigger] arr1[i] == 0 || arr1[i] == 1),\n forall|i: int| 0 <= i < arr2.len() ==> (#[trigger] arr2[i] == 0 || arr2[i] == 1),\n arr1.len() == 1 || arr1[0] == 1,\n arr2.len() == 1 || arr2[0] == 1,\n ensures\n result.len() >= 1,\n forall|i: int|\n 0 <= i < result.len() ==> (#[trigger] result[i] == 0 || result[i] == 1),\n result.len() == 1 || result[0] == 1,\n negabinary_val(result@) == negabinary_val(arr1@) + negabinary_val(arr2@),\n {\n let n1 = arr1.len();\n let n2 = arr2.len();\n let max_len = if n1 >= n2 { n1 } else { n2 };\n let max_iters = max_len + 3;\n let mut res: Vec = Vec::new();\n let mut carry: i32 = 0;\n let mut k: usize = 0;\n while k < max_iters && (k < n1 || k < n2 || carry != 0) {\n let a = if k < n1 { arr1[n1 - 1 - k] } else { 0 };\n let b = if k < n2 { arr2[n2 - 1 - k] } else { 0 };\n let sum = carry + a + b;\n let bit: i32;\n let new_carry: i32;\n if sum >= 2 {\n bit = sum - 2;\n new_carry = -1;\n } else if sum < 0 {\n bit = sum + 2;\n new_carry = 1;\n } else {\n bit = sum;\n new_carry = 0;\n }\n res.push(bit);\n carry = new_carry;\n k = k + 1;\n }\n let mut end = res.len();\n while end > 1 && res[end - 1] == 0 {\n end = end - 1;\n }\n let mut result: Vec = Vec::new();\n let mut i = end;\n while i > 0 {\n i = i - 1;\n result.push(res[i]);\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn negabinary_val(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0int\n } else {\n s.last() as int + (-2int) * negabinary_val(s.drop_last())\n }\n}\n\npub open spec fn val_lsb(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0int\n } else {\n s[0] as int + (-2int) * val_lsb(s.subrange(1, s.len() as int))\n }\n}\n\npub open spec fn pow_neg2(n: nat) -> int\n decreases n,\n{\n if n == 0 {\n 1int\n } else {\n (-2int) * pow_neg2((n - 1) as nat)\n }\n}\n\npub open spec fn suffix_val(s: Seq, k: nat) -> int {\n negabinary_val(\n s.subrange(\n if k as int >= s.len() { 0int } else { s.len() as int - k as int },\n s.len() as int,\n ),\n )\n}\n\npub open spec fn get_digit(s: Seq, k: nat) -> int {\n if (k as int) < s.len() {\n s[s.len() - 1 - k as int] as int\n } else {\n 0int\n }\n}\n\nproof fn lemma_prepend_val(x: i32, s: Seq)\n ensures\n negabinary_val(seq![x] + s) == x as int * pow_neg2(s.len()) + negabinary_val(s),\n decreases s.len(),\n{\n let xs = seq![x] + s;\n if s.len() == 0 {\n assert(xs =~= seq![x]);\n assert(xs.drop_last() =~= Seq::::empty());\n assert(xs.last() == x);\n assert(negabinary_val(Seq::::empty()) == 0int);\n assert(negabinary_val(s) == 0int);\n assert(pow_neg2(s.len()) == 1int);\n } else {\n lemma_prepend_val(x, s.drop_last());\n assert(xs.last() == s.last());\n assert(xs.drop_last() =~= seq![x] + s.drop_last());\n let p = pow_neg2(s.len());\n let p_prev = pow_neg2((s.len() - 1) as nat);\n assert(p == (-2int) * p_prev);\n let inner = negabinary_val(s.drop_last());\n assert(negabinary_val(seq![x] + s.drop_last()) == x as int * p_prev + inner);\n assert((-2int) * (x as int * p_prev + inner) == x as int * p + (-2int) * inner) by (\n nonlinear_arith\n )\n requires\n p == (-2int) * p_prev,\n ;\n }\n}\n\nproof fn lemma_negabinary_val_head(s: Seq)\n requires\n s.len() > 0,\n ensures\n negabinary_val(s) == s[0] as int * pow_neg2((s.len() - 1) as nat) + negabinary_val(\n s.subrange(1, s.len() as int),\n ),\n{\n let tail = s.subrange(1, s.len() as int);\n assert(seq![s[0]] + tail =~= s);\n lemma_prepend_val(s[0], tail);\n}\n\nproof fn lemma_suffix_val_step(s: Seq, k: nat)\n ensures\n suffix_val(s, (k + 1) as nat) == get_digit(s, k) * pow_neg2(k) + suffix_val(s, k),\n{\n if (k as int) < s.len() {\n let suffix_new = s.subrange(s.len() as int - (k as int) - 1, s.len() as int);\n let suffix_old = s.subrange(s.len() as int - k as int, s.len() as int);\n assert(suffix_new.subrange(1, suffix_new.len() as int) =~= suffix_old);\n lemma_negabinary_val_head(suffix_new);\n }\n}\n\nproof fn lemma_suffix_val_full(s: Seq, k: nat)\n requires\n k as int >= s.len(),\n ensures\n suffix_val(s, k) == negabinary_val(s),\n{\n assert(s.subrange(0int, s.len() as int) =~= s);\n}\n\nproof fn lemma_val_lsb_push(s: Seq, d: i32)\n ensures\n val_lsb(s.push(d)) == val_lsb(s) + d as int * pow_neg2(s.len()),\n decreases s.len(),\n{\n if s.len() == 0 {\n assert(s.push(d) =~= seq![d]);\n assert(seq![d].subrange(1, 1int) =~= Seq::::empty());\n assert(val_lsb(Seq::::empty()) == 0int);\n assert(val_lsb(s) == 0int);\n assert(pow_neg2(s.len()) == 1int);\n } else {\n let tail = s.subrange(1, s.len() as int);\n let pk = pow_neg2(s.len());\n let pk_tail = pow_neg2(tail.len());\n lemma_val_lsb_push(tail, d);\n assert(s.push(d).subrange(1, s.push(d).len() as int) =~= tail.push(d));\n assert(pk == (-2int) * pk_tail);\n let vt = val_lsb(tail);\n let dpt = d as int * pk_tail;\n assert(val_lsb(tail.push(d)) == vt + dpt);\n assert((-2int) * (vt + dpt) == (-2int) * vt + (-2int) * dpt) by (nonlinear_arith);\n assert((-2int) * dpt == d as int * pk) by (nonlinear_arith)\n requires\n pk == (-2int) * pk_tail,\n dpt == d as int * pk_tail,\n ;\n assert(val_lsb(s) == s[0] as int + (-2int) * vt);\n assert(val_lsb(s.push(d)) == s[0] as int + (-2int) * (vt + dpt));\n }\n}\n\nproof fn lemma_reverse_val(s: Seq)\n ensures\n negabinary_val(s.reverse()) == val_lsb(s),\n decreases s.len(),\n{\n if s.len() == 0 {\n assert(s.reverse() =~= s);\n } else {\n let tail = s.subrange(1, s.len() as int);\n lemma_reverse_val(tail);\n assert(s.reverse().drop_last() =~= tail.reverse());\n assert(s.reverse().last() == s[0]);\n }\n}\n\nproof fn lemma_val_lsb_strip_trailing_zero(s: Seq)\n requires\n s.len() > 0,\n s.last() == 0,\n ensures\n val_lsb(s) == val_lsb(s.drop_last()),\n decreases s.len(),\n{\n if s.len() == 1 {\n assert(s.subrange(1, 1int) =~= Seq::::empty());\n assert(s[0] == s.last());\n assert(s[0] == 0int);\n assert(s.drop_last() =~= Seq::::empty());\n } else {\n let tail = s.subrange(1, s.len() as int);\n let dl = s.drop_last();\n assert(tail.last() == s.last());\n lemma_val_lsb_strip_trailing_zero(tail);\n assert(dl.subrange(1, dl.len() as int) =~= tail.drop_last());\n assert(dl[0] == s[0]);\n assert(val_lsb(s) == s[0] as int + (-2int) * val_lsb(tail));\n assert(val_lsb(dl) == dl[0] as int + (-2int) * val_lsb(dl.subrange(1, dl.len() as int)));\n assert(val_lsb(dl.subrange(1, dl.len() as int)) == val_lsb(tail.drop_last()));\n assert(val_lsb(tail) == val_lsb(tail.drop_last()));\n assert(val_lsb(s) == s[0] as int + (-2int) * val_lsb(tail.drop_last()));\n assert(val_lsb(dl) == s[0] as int + (-2int) * val_lsb(tail.drop_last()));\n assert(val_lsb(s) == val_lsb(s.drop_last()));\n }\n}\n\nproof fn lemma_val_lsb_strip_zeros(s: Seq, end: int)\n requires\n 1 <= end <= s.len(),\n forall|i: int| end <= i < s.len() ==> s[i] == 0,\n ensures\n val_lsb(s.subrange(0, end)) == val_lsb(s),\n decreases s.len() - end,\n{\n if end == s.len() {\n assert(s.subrange(0, end) =~= s);\n } else {\n assert(s.last() == 0);\n lemma_val_lsb_strip_trailing_zero(s);\n lemma_val_lsb_strip_zeros(s.drop_last(), end);\n assert(s.drop_last().subrange(0, end) =~= s.subrange(0, end));\n }\n}\n\nimpl Solution {\n pub fn add_negabinary(arr1: Vec, arr2: Vec) -> (result: Vec)\n requires\n 1 <= arr1.len() <= 1000,\n 1 <= arr2.len() <= 1000,\n forall|i: int| 0 <= i < arr1.len() ==> (#[trigger] arr1[i] == 0 || arr1[i] == 1),\n forall|i: int| 0 <= i < arr2.len() ==> (#[trigger] arr2[i] == 0 || arr2[i] == 1),\n arr1.len() == 1 || arr1[0] == 1,\n arr2.len() == 1 || arr2[0] == 1,\n ensures\n result.len() >= 1,\n forall|i: int|\n 0 <= i < result.len() ==> (#[trigger] result[i] == 0 || result[i] == 1),\n result.len() == 1 || result[0] == 1,\n negabinary_val(result@) == negabinary_val(arr1@) + negabinary_val(arr2@),\n {\n let n1 = arr1.len();\n let n2 = arr2.len();\n let max_len = if n1 >= n2 { n1 } else { n2 };\n let max_iters = max_len + 3;\n let mut res: Vec = Vec::new();\n let mut carry: i32 = 0;\n let mut k: usize = 0;\n\n while k < max_iters && (k < n1 || k < n2 || carry != 0)\n invariant\n n1 == arr1.len(),\n n2 == arr2.len(),\n 1 <= n1 <= 1000,\n 1 <= n2 <= 1000,\n max_len == (if n1 >= n2 { n1 } else { n2 }),\n max_iters == max_len + 3,\n k <= max_iters,\n k == res.len(),\n -1 <= carry <= 1,\n forall|i: int|\n 0 <= i < arr1.len() ==> (#[trigger] arr1[i] == 0 || arr1[i] == 1),\n forall|i: int|\n 0 <= i < arr2.len() ==> (#[trigger] arr2[i] == 0 || arr2[i] == 1),\n forall|i: int| 0 <= i < res.len() ==> (#[trigger] res[i] == 0 || res[i] == 1),\n val_lsb(res@) + carry as int * pow_neg2(k as nat)\n == suffix_val(arr1@, k as nat) + suffix_val(arr2@, k as nat),\n k as int > max_len as int ==> carry >= 0,\n k as int > max_len as int + 1 ==> carry == 0,\n decreases max_iters - k,\n {\n let a = if k < n1 { arr1[n1 - 1 - k] } else { 0 };\n let b = if k < n2 { arr2[n2 - 1 - k] } else { 0 };\n let sum = carry + a + b;\n\n let bit: i32;\n let new_carry: i32;\n if sum >= 2 {\n bit = sum - 2;\n new_carry = -1;\n } else if sum < 0 {\n bit = sum + 2;\n new_carry = 1;\n } else {\n bit = sum;\n new_carry = 0;\n }\n\n proof {\n let pk = pow_neg2(k as nat);\n assert(pow_neg2((k + 1) as nat) == (-2int) * pk);\n assert(sum as int == bit as int + (-2int) * new_carry as int);\n\n lemma_val_lsb_push(res@, bit);\n lemma_suffix_val_step(arr1@, k as nat);\n lemma_suffix_val_step(arr2@, k as nat);\n assert(get_digit(arr1@, k as nat) == a as int);\n assert(get_digit(arr2@, k as nat) == b as int);\n\n assert(new_carry as int * ((-2int) * pk) == (-2int) * new_carry as int * pk) by (\n nonlinear_arith\n );\n assert((bit as int + (-2int) * new_carry as int) * pk == bit as int * pk + (-2int)\n * new_carry as int * pk) by (nonlinear_arith);\n assert((carry as int + a as int + b as int) * pk == carry as int * pk + (a as int\n + b as int) * pk) by (nonlinear_arith);\n assert((a as int + b as int) * pk == a as int * pk + b as int * pk) by (\n nonlinear_arith\n );\n }\n\n res.push(bit);\n carry = new_carry;\n k = k + 1;\n }\n\n proof {\n if carry != 0 {\n assert(k as int > max_len as int + 1);\n assert(carry == 0);\n }\n lemma_suffix_val_full(arr1@, k as nat);\n lemma_suffix_val_full(arr2@, k as nat);\n }\n\n let mut end = res.len();\n while end > 1 && res[end - 1] == 0\n invariant\n 1 <= end <= res.len(),\n forall|i: int| 0 <= i < res.len() ==> (#[trigger] res[i] == 0 || res[i] == 1),\n forall|i: int| end as int <= i < res.len() ==> res[i] == 0,\n val_lsb(res@) == negabinary_val(arr1@) + negabinary_val(arr2@),\n decreases end,\n {\n end = end - 1;\n }\n\n proof {\n lemma_val_lsb_strip_zeros(res@, end as int);\n }\n\n let mut result: Vec = Vec::new();\n let mut i = end;\n while i > 0\n invariant\n 0 <= i <= end,\n 1 <= end <= res.len(),\n result.len() == end - i,\n forall|j: int|\n 0 <= j < result.len() ==> #[trigger] result[j] == res[(end - 1 - j) as int],\n forall|j: int| 0 <= j < res.len() ==> (res[j] == 0 || res[j] == 1),\n end > 1 ==> res[end - 1] != 0,\n decreases i,\n {\n i = i - 1;\n result.push(res[i]);\n }\n\n proof {\n assert(result@ =~= res@.subrange(0, end as int).reverse());\n lemma_reverse_val(res@.subrange(0, end as int));\n if result.len() > 1 {\n assert(result[0] == res[end - 1]);\n }\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1073/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1073/tests/mutated_testcases.jsonl"} {"problem": "lc1103", "family": "leetcode", "description": "# Distribute Candies to People\n\nWe distribute some number of `candies` to a row of `n = num_people` people in the following way:\n\nWe give 1 candy to the first person, 2 candies to the second person, and so on until we give `n` candies to the last person.\n\nThen, we go back to the start of the row, giving `n + 1` candies to the first person, `n + 2` candies to the second person, and so on until we give `2 * n` candies to the last person.\n\nThis process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).\n\nReturn an array (of length `num_people` and sum `candies`) that represents the final distribution of candies.\n\n## Example 1:\n\n> **Input:** candies = 7, num_people = 4\n> **Output:** [1,2,3,1]\n> **Explanation:**\n> On the first turn, ans[0] += 1, and the array is [1,0,0,0].\n> On the second turn, ans[1] += 2, and the array is [1,2,0,0].\n> On the third turn, ans[2] += 3, and the array is [1,2,3,0].\n> On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].\n\n## Example 2:\n\n> **Input:** candies = 10, num_people = 3\n> **Output:** [5,2,3]\n> **Explanation:**\n> On the first turn, ans[0] += 1, and the array is [1,0,0].\n> On the second turn, ans[1] += 2, and the array is [1,2,0].\n> On the third turn, ans[2] += 3, and the array is [1,2,3].\n> On the fourth turn, ans[0] += 4, and the final array is [5,2,3].\n\n## Constraints:\n\n- 1 <= candies <= 10^9\n- 1 <= num_people <= 1000\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn distribute_candies(candies: i32, num_people: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nMath, Simulation\nAcceptance rate: 67.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn distribute_helper(remaining: int, n: int, step: int, acc: Seq) -> Seq\n decreases remaining,\n {\n if remaining <= 0 || n <= 0 || step < 0 {\n acc\n } else {\n let give = if remaining < step + 1 { remaining } else { step + 1 };\n let person = step % n;\n Self::distribute_helper(\n remaining - give,\n n,\n step + 1,\n acc.update(person, acc[person] + give),\n )\n }\n }\n\n pub open spec fn distribute_spec(candies: int, n: int) -> Seq {\n Self::distribute_helper(candies, n, 0, Seq::new(n as nat, |_i: int| 0int))\n }\n\n pub fn distribute_candies(candies: i32, num_people: i32) -> (result: Vec)\n requires\n 1 <= candies <= 1_000_000_000,\n 1 <= num_people <= 1000,\n ensures\n result@.len() == num_people as int,\n forall |i: int| 0 <= i < num_people as int\n ==> (#[trigger] result@[i]) as int\n == Self::distribute_spec(candies as int, num_people as int)[i],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn distribute_candies(candies: i32, num_people: i32) -> Vec {\n let n = num_people as usize;\n let mut result: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n {\n result.push(0i32);\n idx = idx + 1;\n }\n let mut remaining = candies;\n let mut step: i32 = 0;\n while remaining > 0 {\n let give: i32 = if remaining < step + 1 { remaining } else { step + 1 };\n let person_idx: i32 = step % num_people;\n let person: usize = person_idx as usize;\n let old_val: i32 = result[person];\n result[person] = old_val + give;\n remaining = remaining - give;\n step = step + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn distribute_helper(remaining: int, n: int, step: int, acc: Seq) -> Seq\n decreases remaining,\n {\n if remaining <= 0 || n <= 0 || step < 0 {\n acc\n } else {\n let give = if remaining < step + 1 { remaining } else { step + 1 };\n let person = step % n;\n Self::distribute_helper(\n remaining - give,\n n,\n step + 1,\n acc.update(person, acc[person] + give),\n )\n }\n }\n\n pub open spec fn distribute_spec(candies: int, n: int) -> Seq {\n Self::distribute_helper(candies, n, 0, Seq::new(n as nat, |_i: int| 0int))\n }\n\n pub fn distribute_candies(candies: i32, num_people: i32) -> (result: Vec)\n requires\n 1 <= candies <= 1_000_000_000,\n 1 <= num_people <= 1000,\n ensures\n result@.len() == num_people as int,\n forall |i: int| 0 <= i < num_people as int\n ==> (#[trigger] result@[i]) as int\n == Self::distribute_spec(candies as int, num_people as int)[i],\n {\n let n = num_people as usize;\n let mut result: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n {\n result.push(0i32);\n idx = idx + 1;\n }\n let mut remaining = candies;\n let mut step: i32 = 0;\n while remaining > 0 {\n let give: i32 = if remaining < step + 1 { remaining } else { step + 1 };\n let person_idx: i32 = step % num_people;\n let person: usize = person_idx as usize;\n let old_val: i32 = result[person];\n result.set(person, old_val + give);\n remaining = remaining - give;\n step = step + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn distribute_helper(remaining: int, n: int, step: int, acc: Seq) -> Seq\n decreases remaining,\n {\n if remaining <= 0 || n <= 0 || step < 0 {\n acc\n } else {\n let give = if remaining < step + 1 { remaining } else { step + 1 };\n let person = step % n;\n Self::distribute_helper(\n remaining - give,\n n,\n step + 1,\n acc.update(person, acc[person] + give),\n )\n }\n }\n\n pub open spec fn distribute_spec(candies: int, n: int) -> Seq {\n Self::distribute_helper(candies, n, 0, Seq::new(n as nat, |_i: int| 0int))\n }\n\n pub fn distribute_candies(candies: i32, num_people: i32) -> (result: Vec)\n requires\n 1 <= candies <= 1_000_000_000,\n 1 <= num_people <= 1000,\n ensures\n result@.len() == num_people as int,\n forall |i: int| 0 <= i < num_people as int\n ==> (#[trigger] result@[i]) as int\n == Self::distribute_spec(candies as int, num_people as int)[i],\n {\n let n = num_people as usize;\n let mut result: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n\n invariant\n n == num_people as int,\n 1 <= num_people <= 1000,\n result@.len() == idx as int,\n 0 <= idx <= n,\n forall |k: int| 0 <= k < idx as int ==> (#[trigger] result@[k]) == 0i32,\n decreases n - idx,\n {\n result.push(0i32);\n idx = idx + 1;\n }\n\n proof {\n assert(Self::to_int_seq(result@) =~= Seq::new(num_people as nat, |_i: int| 0int));\n }\n\n let mut remaining = candies;\n let mut step: i32 = 0;\n\n while remaining > 0\n invariant\n 1 <= candies <= 1_000_000_000,\n 1 <= num_people <= 1000,\n n == num_people as int,\n result@.len() == num_people as int,\n 0 <= remaining <= candies,\n 0 <= step,\n step as int <= candies as int - remaining as int,\n forall |i: int| 0 <= i < num_people as int\n ==> 0 <= (#[trigger] result@[i]) as int\n <= candies as int - remaining as int,\n Self::distribute_helper(\n remaining as int,\n num_people as int,\n step as int,\n Self::to_int_seq(result@),\n ) == Self::distribute_spec(candies as int, num_people as int),\n decreases remaining as int,\n {\n let give: i32 = if remaining < step + 1 { remaining } else { step + 1 };\n let person_idx: i32 = step % num_people;\n let person: usize = person_idx as usize;\n\n let ghost old_acc = Self::to_int_seq(result@);\n\n proof {\n assert(result@[person as int] as int + give as int <= candies as int) by {\n assert(result@[person as int] as int <= candies as int - remaining as int);\n assert(give as int <= remaining as int);\n };\n }\n\n let old_val: i32 = result[person];\n result.set(person, old_val + give);\n remaining = remaining - give;\n step = step + 1;\n\n proof {\n assert(Self::to_int_seq(result@) =~= old_acc.update(\n person_idx as int,\n old_acc[person_idx as int] + give as int,\n ));\n }\n }\n\n proof {\n assert forall |i: int| 0 <= i < num_people as int\n implies (#[trigger] result@[i]) as int\n == Self::distribute_spec(candies as int, num_people as int)[i]\n by {\n assert(Self::to_int_seq(result@)[i]\n == Self::distribute_spec(candies as int, num_people as int)[i]);\n }\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1103/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1103/tests/mutated_testcases.jsonl"} {"problem": "lc1122", "family": "leetcode", "description": "# Relative Sort Array\n\nGiven two arrays `arr1` and `arr2`, the elements of `arr2` are distinct, and all elements in `arr2` are also in `arr1`.\n\nSort the elements of `arr1` such that the relative ordering of items in `arr1` are the same as in `arr2`. Elements that do not appear in `arr2` should be placed at the end of `arr1` in **ascending** order.\n\n## Example 1:\n\n> **Input:** arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]\n> **Output:** [2,2,2,1,4,3,3,9,6,7,19]\n\n## Example 2:\n\n> **Input:** arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]\n> **Output:** [22,28,8,6,17,44]\n\n## Constraints:\n\n- $1 \\leq arr1.length, arr2.length \\leq 1000$\n\n- $0 \\leq arr1[i], arr2[i] \\leq 1000$\n\n- All the elements of `arr2` are **distinct**.\n\n- Each `arr2[i]` is in `arr1`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn relative_sort_array(arr1: Vec, arr2: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Sorting, Counting Sort\nAcceptance rate: 75.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count(s: Seq, v: i32) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n (if s[0] == v { 1int } else { 0int }) + Self::count(s.subrange(1, s.len() as int), v)\n }\n }\n\n pub open spec fn index_of(v: i32, s: Seq) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n -1int\n } else if s[0] == v {\n 0int\n } else {\n let r = Self::index_of(v, s.subrange(1, s.len() as int));\n if r == -1 { -1int } else { 1 + r }\n }\n }\n\n pub open spec fn rank(v: i32, arr2: Seq) -> int {\n let idx = Self::index_of(v, arr2);\n if idx >= 0 {\n idx\n } else {\n arr2.len() + v as int\n }\n }\n\n pub fn relative_sort_array(arr1: Vec, arr2: Vec) -> (result: Vec)\n requires\n 1 <= arr1@.len() <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |i: int| 0 <= i < arr1@.len() ==> 0 <= #[trigger] arr1@[i] <= 1000,\n forall |i: int| 0 <= i < arr2@.len() ==> 0 <= #[trigger] arr2@[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < arr2@.len() ==> arr2@[i] != arr2@[j],\n forall |i: int| 0 <= i < arr2@.len() ==>\n Self::count(arr1@, arr2@[i]) >= 1,\n ensures\n result@.len() == arr1@.len(),\n forall |v: i32| Self::count(result@, v) == Self::count(arr1@, v),\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) < 0\n && Self::index_of(result@[j], arr2@) >= 0\n ==> false,\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) >= 0\n && Self::index_of(result@[j], arr2@) >= 0\n ==> Self::index_of(result@[i], arr2@) <= Self::index_of(result@[j], arr2@),\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) < 0\n && Self::index_of(result@[j], arr2@) < 0\n ==> result@[i] <= result@[j],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn relative_sort_array(arr1: Vec, arr2: Vec) -> Vec {\n let mut cnt: Vec = Vec::new();\n let mut i: usize = 0;\n while i < 1001 {\n cnt.push(0);\n i = i + 1;\n }\n\n let mut j: usize = 0;\n while j < arr1.len() {\n let v = arr1[j] as usize;\n cnt[v] = cnt[v] + 1;\n j = j + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut k: usize = 0;\n while k < arr2.len() {\n let v = arr2[k];\n while cnt[v as usize] > 0 {\n result.push(v);\n cnt[v as usize] = cnt[v as usize] - 1;\n }\n k = k + 1;\n }\n\n let mut m: usize = 0;\n while m < 1001 {\n while cnt[m] > 0 {\n result.push(m as i32);\n cnt[m] = cnt[m] - 1;\n }\n m = m + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count(s: Seq, v: i32) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n (if s[0] == v { 1int } else { 0int }) + Self::count(s.subrange(1, s.len() as int), v)\n }\n }\n\n pub open spec fn index_of(v: i32, s: Seq) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n -1int\n } else if s[0] == v {\n 0int\n } else {\n let r = Self::index_of(v, s.subrange(1, s.len() as int));\n if r == -1 { -1int } else { 1 + r }\n }\n }\n\n pub open spec fn rank(v: i32, arr2: Seq) -> int {\n let idx = Self::index_of(v, arr2);\n if idx >= 0 {\n idx\n } else {\n arr2.len() + v as int\n }\n }\n\n pub fn relative_sort_array(arr1: Vec, arr2: Vec) -> (result: Vec)\n requires\n 1 <= arr1@.len() <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |i: int| 0 <= i < arr1@.len() ==> 0 <= #[trigger] arr1@[i] <= 1000,\n forall |i: int| 0 <= i < arr2@.len() ==> 0 <= #[trigger] arr2@[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < arr2@.len() ==> arr2@[i] != arr2@[j],\n forall |i: int| 0 <= i < arr2@.len() ==>\n Self::count(arr1@, arr2@[i]) >= 1,\n ensures\n result@.len() == arr1@.len(),\n forall |v: i32| Self::count(result@, v) == Self::count(arr1@, v),\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) < 0\n && Self::index_of(result@[j], arr2@) >= 0\n ==> false,\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) >= 0\n && Self::index_of(result@[j], arr2@) >= 0\n ==> Self::index_of(result@[i], arr2@) <= Self::index_of(result@[j], arr2@),\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) < 0\n && Self::index_of(result@[j], arr2@) < 0\n ==> result@[i] <= result@[j],\n {\n let mut cnt: Vec = Vec::new();\n let mut i: usize = 0;\n while i < 1001 {\n cnt.push(0);\n i = i + 1;\n }\n\n let mut j: usize = 0;\n while j < arr1.len() {\n let v = arr1[j] as usize;\n cnt.set(v, cnt[v] + 1);\n j = j + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut k: usize = 0;\n while k < arr2.len() {\n let v = arr2[k];\n while cnt[v as usize] > 0 {\n result.push(v);\n cnt.set(v as usize, cnt[v as usize] - 1);\n }\n k = k + 1;\n }\n\n let mut m: usize = 0;\n while m < 1001 {\n while cnt[m] > 0 {\n result.push(m as i32);\n cnt.set(m, cnt[m] - 1);\n }\n m = m + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count(s: Seq, v: i32) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n (if s[0] == v { 1int } else { 0int }) + Self::count(s.subrange(1, s.len() as int), v)\n }\n }\n\n pub open spec fn index_of(v: i32, s: Seq) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n -1int\n } else if s[0] == v {\n 0int\n } else {\n let r = Self::index_of(v, s.subrange(1, s.len() as int));\n if r == -1 { -1int } else { 1 + r }\n }\n }\n\n pub open spec fn rank(v: i32, arr2: Seq) -> int {\n let idx = Self::index_of(v, arr2);\n if idx >= 0 {\n idx\n } else {\n arr2.len() + v as int\n }\n }\n\n pub open spec fn sum_range(cnt: Seq, lo: int, hi: int) -> int\n decreases hi - lo\n {\n if lo >= hi { 0 } else { cnt[lo] as int + Self::sum_range(cnt, lo + 1, hi) }\n }\n\n proof fn count_push(s: Seq, x: i32, v: i32)\n ensures\n Self::count(s.push(x), v) == Self::count(s, v) + if x == v { 1int } else { 0int }\n decreases s.len()\n {\n if s.len() == 0 {\n assert(Self::count(s, v) == 0);\n assert(s.push(x).len() == 1);\n assert(s.push(x)[0] == x);\n let rest = s.push(x).subrange(1, s.push(x).len() as int);\n assert(rest =~= Seq::::empty());\n assert(rest.len() == 0);\n assert(Self::count(rest, v) == 0);\n } else {\n let tail = s.subrange(1, s.len() as int);\n Self::count_push(tail, x, v);\n assert(s.push(x).len() > 0);\n assert(s.push(x)[0] == s[0]);\n let rest = s.push(x).subrange(1, s.push(x).len() as int);\n assert(rest =~= tail.push(x));\n assert(Self::count(rest, v) == Self::count(tail.push(x), v));\n assert(Self::count(s, v) == (if s[0] == v { 1int } else { 0int }) + Self::count(tail, v));\n }\n }\n\n proof fn count_le_len(s: Seq, v: i32)\n ensures\n Self::count(s, v) <= s.len()\n decreases s.len()\n {\n if s.len() > 0 {\n Self::count_le_len(s.subrange(1, s.len() as int), v);\n }\n }\n\n proof fn count_zero_not_present(s: Seq, v: i32)\n requires\n forall |i: int| 0 <= i < s.len() ==> s[i] != v\n ensures\n Self::count(s, v) == 0\n decreases s.len()\n {\n if s.len() > 0 {\n let sub = s.subrange(1, s.len() as int);\n assert forall |i: int| 0 <= i < sub.len() implies sub[i] != v by {\n assert(sub[i] == s[i + 1]);\n };\n Self::count_zero_not_present(sub, v);\n }\n }\n\n proof fn index_of_self(s: Seq, k: int)\n requires\n 0 <= k < s.len(),\n forall |i: int, j: int| 0 <= i < j < s.len() ==> s[i] != s[j],\n ensures\n Self::index_of(s[k], s) == k\n decreases k\n {\n if k > 0 {\n let sub = s.subrange(1, s.len() as int);\n assert(sub[k - 1] == s[k]);\n assert forall |i: int, j: int| 0 <= i < j < sub.len() implies sub[i] != sub[j] by {\n assert(sub[i] == s[i + 1]);\n assert(sub[j] == s[j + 1]);\n };\n Self::index_of_self(sub, k - 1);\n }\n }\n\n proof fn index_of_not_found(v: i32, s: Seq)\n requires\n forall |k: int| 0 <= k < s.len() ==> s[k] != v,\n ensures\n Self::index_of(v, s) == -1\n decreases s.len()\n {\n if s.len() > 0 {\n let sub = s.subrange(1, s.len() as int);\n assert forall |k: int| 0 <= k < sub.len() implies sub[k] != v by {\n assert(sub[k] == s[k + 1]);\n };\n Self::index_of_not_found(v, sub);\n }\n }\n\n proof fn index_of_bound(v: i32, s: Seq)\n ensures\n Self::index_of(v, s) < s.len() as int,\n Self::index_of(v, s) >= -1,\n decreases s.len()\n {\n if s.len() > 0 && s[0] != v {\n Self::index_of_bound(v, s.subrange(1, s.len() as int));\n }\n }\n\n proof fn sum_range_set(old_s: Seq, new_s: Seq, idx: int, lo: int, hi: int)\n requires\n old_s.len() == new_s.len(),\n lo <= idx < hi,\n 0 <= lo,\n hi <= old_s.len(),\n forall |k: int| lo <= k < hi && k != idx ==> new_s[k] == old_s[k],\n ensures\n Self::sum_range(new_s, lo, hi)\n == Self::sum_range(old_s, lo, hi) - old_s[idx] as int + new_s[idx] as int\n decreases hi - lo\n {\n if idx == lo {\n if lo + 1 < hi {\n Self::sum_range_same(old_s, new_s, lo + 1, hi);\n }\n assert(Self::sum_range(new_s, lo, hi) == new_s[lo] as int + Self::sum_range(new_s, lo + 1, hi));\n assert(Self::sum_range(old_s, lo, hi) == old_s[lo] as int + Self::sum_range(old_s, lo + 1, hi));\n } else {\n assert(new_s[lo] == old_s[lo]);\n Self::sum_range_set(old_s, new_s, idx, lo + 1, hi);\n }\n }\n\n proof fn sum_range_same(s1: Seq, s2: Seq, lo: int, hi: int)\n requires\n forall |k: int| lo <= k < hi ==> s1[k] == s2[k],\n 0 <= lo,\n ensures\n Self::sum_range(s1, lo, hi) == Self::sum_range(s2, lo, hi)\n decreases hi - lo\n {\n if lo < hi {\n Self::sum_range_same(s1, s2, lo + 1, hi);\n }\n }\n\n proof fn sum_range_all_zero(cnt: Seq, lo: int, hi: int)\n requires\n 0 <= lo,\n hi <= cnt.len(),\n forall |v: int| lo <= v < hi ==> cnt[v] == 0i32,\n ensures\n Self::sum_range(cnt, lo, hi) == 0\n decreases hi - lo\n {\n if lo < hi {\n Self::sum_range_all_zero(cnt, lo + 1, hi);\n }\n }\n\n pub fn relative_sort_array(arr1: Vec, arr2: Vec) -> (result: Vec)\n requires\n 1 <= arr1@.len() <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |i: int| 0 <= i < arr1@.len() ==> 0 <= #[trigger] arr1@[i] <= 1000,\n forall |i: int| 0 <= i < arr2@.len() ==> 0 <= #[trigger] arr2@[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < arr2@.len() ==> arr2@[i] != arr2@[j],\n forall |i: int| 0 <= i < arr2@.len() ==>\n Self::count(arr1@, arr2@[i]) >= 1,\n ensures\n result@.len() == arr1@.len(),\n forall |v: i32| Self::count(result@, v) == Self::count(arr1@, v),\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) < 0\n && Self::index_of(result@[j], arr2@) >= 0\n ==> false,\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) >= 0\n && Self::index_of(result@[j], arr2@) >= 0\n ==> Self::index_of(result@[i], arr2@) <= Self::index_of(result@[j], arr2@),\n \n forall |i: int, j: int| 0 <= i < j < result@.len()\n && Self::index_of(result@[i], arr2@) < 0\n && Self::index_of(result@[j], arr2@) < 0\n ==> result@[i] <= result@[j],\n {\n let mut cnt: Vec = Vec::new();\n let mut i: usize = 0;\n while i < 1001\n invariant\n cnt@.len() == i as int,\n 0 <= i <= 1001,\n forall |v: int| 0 <= v < i as int ==> cnt@[v] == 0i32,\n decreases 1001 - i\n {\n cnt.push(0);\n i = i + 1;\n }\n\n proof {\n Self::sum_range_all_zero(cnt@, 0, 1001);\n }\n\n let mut j: usize = 0;\n while j < arr1.len()\n invariant\n cnt@.len() == 1001,\n 0 <= j <= arr1@.len(),\n 1 <= arr1@.len() <= 1000,\n forall |ii: int| 0 <= ii < arr1@.len() ==> 0 <= #[trigger] arr1@[ii] <= 1000,\n forall |v: int| 0 <= v <= 1000 ==>\n cnt@[v] as int == Self::count(arr1@.subrange(0, j as int), v as i32),\n forall |v: int| 0 <= v <= 1000 ==> 0 <= #[trigger] cnt@[v],\n forall |v: int| 0 <= v <= 1000 ==> #[trigger] cnt@[v] as int <= j as int,\n Self::sum_range(cnt@, 0, 1001) == j as int,\n decreases arr1@.len() - j\n {\n let v = arr1[j] as usize;\n let ghost old_cnt = cnt@;\n\n proof {\n let prev = arr1@.subrange(0, j as int);\n Self::count_le_len(prev, v as i32);\n assert(prev.len() == j as int);\n assert(cnt@[v as int] as int <= j as int);\n assert(cnt@[v as int] < 1000);\n }\n cnt.set(v, cnt[v] + 1);\n\n proof {\n let prev = arr1@.subrange(0, j as int);\n let next = arr1@.subrange(0, j as int + 1);\n assert(next =~= prev.push(arr1@[j as int]));\n\n assert forall |w: int| 0 <= w <= 1000 implies\n cnt@[w] as int == Self::count(next, w as i32)\n by {\n Self::count_push(prev, arr1@[j as int], w as i32);\n };\n\n assert forall |w: int| 0 <= w <= 1000 implies\n #[trigger] cnt@[w] as int <= (j + 1) as int\n by {\n Self::count_le_len(next, w as i32);\n assert(next.len() == j as int + 1);\n };\n\n Self::sum_range_set(old_cnt, cnt@, v as int, 0, 1001);\n }\n\n j = j + 1;\n }\n\n proof {\n assert(arr1@.subrange(0, arr1@.len() as int) =~= arr1@);\n }\n\n let mut result: Vec = Vec::new();\n let mut k: usize = 0;\n while k < arr2.len()\n invariant\n cnt@.len() == 1001,\n 0 <= k <= arr2@.len(),\n 1 <= arr1@.len() <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |ii: int| 0 <= ii < arr1@.len() ==> 0 <= #[trigger] arr1@[ii] <= 1000,\n forall |ii: int| 0 <= ii < arr2@.len() ==> 0 <= #[trigger] arr2@[ii] <= 1000,\n forall |ii: int, jj: int| 0 <= ii < jj < arr2@.len() ==> arr2@[ii] != arr2@[jj],\n forall |v: int| 0 <= v <= 1000 ==>\n Self::count(result@, v as i32) + cnt@[v] as int == Self::count(arr1@, v as i32),\n forall |v: int| 0 <= v <= 1000 ==> 0 <= #[trigger] cnt@[v],\n result@.len() as int + Self::sum_range(cnt@, 0, 1001) == arr1@.len() as int,\n forall |jj: int| 0 <= jj < k as int ==> cnt@[arr2@[jj] as int] == 0i32,\n forall |ii: int, jj: int| 0 <= ii < jj < result@.len() ==>\n Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@),\n forall |ii: int| 0 <= ii < result@.len() ==>\n Self::rank(result@[ii], arr2@) < k as int,\n forall |ii: int| 0 <= ii < result@.len() ==> 0 <= #[trigger] result@[ii] <= 1000,\n decreases arr2@.len() - k\n {\n let v = arr2[k];\n\n proof {\n Self::index_of_self(arr2@, k as int);\n }\n\n while cnt[v as usize] > 0\n invariant\n cnt@.len() == 1001,\n 0 <= k < arr2@.len(),\n v == arr2@[k as int],\n 1 <= arr1@.len() <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |ii: int| 0 <= ii < arr2@.len() ==> 0 <= #[trigger] arr2@[ii] <= 1000,\n forall |ii: int, jj: int| 0 <= ii < jj < arr2@.len() ==> arr2@[ii] != arr2@[jj],\n 0 <= v <= 1000,\n forall |w: int| 0 <= w <= 1000 ==>\n Self::count(result@, w as i32) + cnt@[w] as int == Self::count(arr1@, w as i32),\n forall |w: int| 0 <= w <= 1000 ==> 0 <= #[trigger] cnt@[w],\n result@.len() as int + Self::sum_range(cnt@, 0, 1001) == arr1@.len() as int,\n forall |jj: int| 0 <= jj < k as int ==> cnt@[arr2@[jj] as int] == 0i32,\n forall |ii: int, jj: int| 0 <= ii < jj < result@.len() ==>\n Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@),\n forall |ii: int| 0 <= ii < result@.len() ==>\n Self::rank(result@[ii], arr2@) <= k as int,\n forall |ii: int| 0 <= ii < result@.len() ==> 0 <= #[trigger] result@[ii] <= 1000,\n Self::index_of(arr2@[k as int], arr2@) == k as int,\n decreases cnt@[v as int] as int\n {\n let ghost old_result = result@;\n let ghost old_cnt = cnt@;\n result.push(v);\n cnt.set(v as usize, cnt[v as usize] - 1);\n\n proof {\n assert forall |w: int| 0 <= w <= 1000 implies\n Self::count(result@, w as i32) + cnt@[w] as int == Self::count(arr1@, w as i32)\n by {\n Self::count_push(old_result, v, w as i32);\n };\n\n Self::sum_range_set(old_cnt, cnt@, v as int, 0, 1001);\n\n assert(Self::rank(v, arr2@) == k as int);\n\n assert forall |jj: int| 0 <= jj < k as int implies\n cnt@[arr2@[jj] as int] == 0i32\n by {\n assert(arr2@[jj] != arr2@[k as int]);\n };\n\n assert forall |ii: int, jj: int| 0 <= ii < jj < result@.len() implies\n Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@)\n by {\n if jj < old_result.len() as int {\n assert(result@[ii] == old_result[ii]);\n assert(result@[jj] == old_result[jj]);\n } else {\n if ii < old_result.len() as int {\n assert(result@[ii] == old_result[ii]);\n assert(Self::rank(result@[ii], arr2@) <= k as int);\n }\n assert(result@[jj] == v);\n }\n };\n\n assert forall |ii: int| 0 <= ii < result@.len() implies\n Self::rank(result@[ii], arr2@) <= k as int\n by {\n if ii < old_result.len() as int {\n assert(result@[ii] == old_result[ii]);\n } else {\n assert(result@[ii] == v);\n }\n };\n }\n }\n\n proof {\n assert forall |ii: int| 0 <= ii < result@.len() implies\n Self::rank(result@[ii], arr2@) < (k + 1) as int\n by {};\n\n assert forall |jj: int| 0 <= jj < (k + 1) as int implies\n cnt@[arr2@[jj] as int] == 0i32\n by {\n if jj < k as int {\n } else {\n assert(jj == k as int);\n assert(v == arr2@[k as int]);\n }\n };\n }\n\n k = k + 1;\n }\n\n let mut m: usize = 0;\n while m < 1001\n invariant\n cnt@.len() == 1001,\n 0 <= m <= 1001,\n 1 <= arr1@.len() <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |ii: int| 0 <= ii < arr2@.len() ==> 0 <= #[trigger] arr2@[ii] <= 1000,\n forall |ii: int, jj: int| 0 <= ii < jj < arr2@.len() ==> arr2@[ii] != arr2@[jj],\n forall |w: int| 0 <= w <= 1000 ==>\n Self::count(result@, w as i32) + cnt@[w] as int == Self::count(arr1@, w as i32),\n forall |w: int| 0 <= w <= 1000 ==> 0 <= #[trigger] cnt@[w],\n result@.len() as int + Self::sum_range(cnt@, 0, 1001) == arr1@.len() as int,\n forall |jj: int| 0 <= jj < arr2@.len() ==> cnt@[arr2@[jj] as int] == 0i32,\n forall |v: int| 0 <= v < m as int ==> cnt@[v] == 0i32,\n forall |ii: int, jj: int| 0 <= ii < jj < result@.len() ==>\n Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@),\n forall |ii: int| 0 <= ii < result@.len() ==>\n Self::rank(result@[ii], arr2@) < arr2@.len() + m as int,\n forall |ii: int| 0 <= ii < result@.len() ==> 0 <= #[trigger] result@[ii] <= 1000,\n decreases 1001 - m\n {\n while cnt[m] > 0\n invariant\n cnt@.len() == 1001,\n 0 <= m <= 1000,\n 1 <= arr2@.len() <= 1000,\n forall |ii: int| 0 <= ii < arr2@.len() ==> 0 <= #[trigger] arr2@[ii] <= 1000,\n forall |ii: int, jj: int| 0 <= ii < jj < arr2@.len() ==> arr2@[ii] != arr2@[jj],\n forall |w: int| 0 <= w <= 1000 ==>\n Self::count(result@, w as i32) + cnt@[w] as int == Self::count(arr1@, w as i32),\n forall |w: int| 0 <= w <= 1000 ==> 0 <= #[trigger] cnt@[w],\n result@.len() as int + Self::sum_range(cnt@, 0, 1001) == arr1@.len() as int,\n forall |jj: int| 0 <= jj < arr2@.len() ==> cnt@[arr2@[jj] as int] == 0i32,\n forall |v: int| 0 <= v < m as int ==> cnt@[v] == 0i32,\n forall |ii: int, jj: int| 0 <= ii < jj < result@.len() ==>\n Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@),\n forall |ii: int| 0 <= ii < result@.len() ==>\n Self::rank(result@[ii], arr2@) <= arr2@.len() + m as int,\n forall |ii: int| 0 <= ii < result@.len() ==> 0 <= #[trigger] result@[ii] <= 1000,\n decreases cnt@[m as int] as int\n {\n let ghost old_result = result@;\n let ghost old_cnt = cnt@;\n result.push(m as i32);\n cnt.set(m, cnt[m] - 1);\n\n proof {\n assert forall |jj: int| 0 <= jj < arr2@.len() implies arr2@[jj] != m as i32 by {\n if arr2@[jj] == m as i32 {\n assert(old_cnt[arr2@[jj] as int] == 0i32);\n assert(old_cnt[m as int] == 0i32);\n }\n };\n Self::index_of_not_found(m as i32, arr2@);\n assert(Self::rank(m as i32, arr2@) == arr2@.len() + m as int);\n\n assert forall |w: int| 0 <= w <= 1000 implies\n Self::count(result@, w as i32) + cnt@[w] as int == Self::count(arr1@, w as i32)\n by {\n Self::count_push(old_result, m as i32, w as i32);\n };\n\n Self::sum_range_set(old_cnt, cnt@, m as int, 0, 1001);\n\n assert forall |jj: int| 0 <= jj < arr2@.len() implies\n cnt@[arr2@[jj] as int] == 0i32\n by {\n assert(arr2@[jj] != m as i32);\n };\n\n assert forall |v: int| 0 <= v < m as int implies cnt@[v] == 0i32\n by {};\n\n assert forall |ii: int, jj: int| 0 <= ii < jj < result@.len() implies\n Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@)\n by {\n if jj < old_result.len() as int {\n assert(result@[ii] == old_result[ii]);\n assert(result@[jj] == old_result[jj]);\n } else {\n if ii < old_result.len() as int {\n assert(result@[ii] == old_result[ii]);\n assert(Self::rank(result@[ii], arr2@) <= arr2@.len() + m as int);\n }\n assert(result@[jj] == m as i32);\n }\n };\n\n assert forall |ii: int| 0 <= ii < result@.len() implies\n Self::rank(result@[ii], arr2@) <= arr2@.len() + m as int\n by {\n if ii < old_result.len() as int {\n assert(result@[ii] == old_result[ii]);\n } else {\n assert(result@[ii] == m as i32);\n }\n };\n }\n }\n\n proof {\n assert forall |ii: int| 0 <= ii < result@.len() implies\n Self::rank(result@[ii], arr2@) < arr2@.len() + (m + 1) as int\n by {};\n\n assert forall |v: int| 0 <= v < (m + 1) as int implies cnt@[v] == 0i32\n by {};\n }\n\n m = m + 1;\n }\n\n proof {\n Self::sum_range_all_zero(cnt@, 0, 1001);\n\n assert forall |v: i32| Self::count(result@, v) == Self::count(arr1@, v)\n by {\n if 0 <= v <= 1000 {\n assert(cnt@[v as int] == 0i32);\n } else {\n Self::count_zero_not_present(result@, v);\n Self::count_zero_not_present(arr1@, v);\n }\n };\n\n \n assert forall |ii: int, jj: int| 0 <= ii < jj < result@.len()\n && Self::index_of(result@[ii], arr2@) < 0\n && Self::index_of(result@[jj], arr2@) >= 0\n implies false\n by {\n let ri = result@[ii];\n let rj = result@[jj];\n let idx_j = Self::index_of(rj, arr2@);\n Self::index_of_bound(rj, arr2@);\n \n assert(Self::rank(rj, arr2@) == idx_j);\n \n assert(Self::rank(ri, arr2@) == arr2@.len() + ri as int);\n \n assert(Self::rank(ri, arr2@) >= arr2@.len() as int);\n \n assert(Self::rank(ri, arr2@) <= Self::rank(rj, arr2@));\n };\n\n assert forall |ii: int, jj: int| 0 <= ii < jj < result@.len()\n && Self::index_of(result@[ii], arr2@) >= 0\n && Self::index_of(result@[jj], arr2@) >= 0\n implies Self::index_of(result@[ii], arr2@) <= Self::index_of(result@[jj], arr2@)\n by {\n assert(Self::rank(result@[ii], arr2@) == Self::index_of(result@[ii], arr2@));\n assert(Self::rank(result@[jj], arr2@) == Self::index_of(result@[jj], arr2@));\n assert(Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@));\n };\n\n assert forall |ii: int, jj: int| 0 <= ii < jj < result@.len()\n && Self::index_of(result@[ii], arr2@) < 0\n && Self::index_of(result@[jj], arr2@) < 0\n implies result@[ii] <= result@[jj]\n by {\n assert(Self::rank(result@[ii], arr2@) == arr2@.len() + result@[ii] as int);\n assert(Self::rank(result@[jj], arr2@) == arr2@.len() + result@[jj] as int);\n assert(Self::rank(result@[ii], arr2@) <= Self::rank(result@[jj], arr2@));\n };\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1122/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1122/tests/mutated_testcases.jsonl"} {"problem": "lc1140", "family": "leetcode", "description": "# Stone Game II\n\nAlice and Bob continue their games with piles of stones. There are a number of piles **arranged in a row**, and each pile has a positive integer number of stones `piles[i]`. The objective of the game is to end with the most stones.\n\nAlice and Bob take turns, with Alice starting first.\n\nOn each player's turn, that player can take **all the stones** in the **first** `X` remaining piles, where `1 <= X <= 2M`. Then, we set `M = max(M, X)`. Initially, M = 1.\n\nThe game continues until all the stones have been taken.\n\nAssuming Alice and Bob play optimally, return the maximum number of stones Alice can get.\n\n## Example 1:\n\n> **Input:** piles = [2,7,9,4,4]\n> **Output:** 10\n> **Explanation:**\n> - If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get `2 + 4 + 4 = 10` stones in total.\n> - If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice gets `2 + 7 = 9` stones in total.\n> So we return 10 since it is larger.\n\n## Example 2:\n\n> **Input:** piles = [1,2,3,4,5,100]\n> **Output:** 104\n\n## Constraints:\n\n- `1 <= piles.length <= 100`\n- `1 <= piles[i] <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn stone_game_ii(piles: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Dynamic Programming, Prefix Sum, Game Theory\nAcceptance rate: 72.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn suffix_sum(piles: Seq, i: int) -> int\n decreases piles.len() - i\n {\n if i >= piles.len() {\n 0\n } else {\n piles[i] as int + Self::suffix_sum(piles, i + 1)\n }\n }\n\n pub open spec fn game(piles: Seq, i: int, m: int, k: int) -> int\n decreases piles.len() - i, k\n when m >= 1 && k >= 0 && i >= 0\n && (1 <= k && k <= 2 * m ==> i + 2 * m < piles.len())\n {\n if k > 2 * m {\n if i >= piles.len() {\n 0\n } else if i + 2 * m >= piles.len() {\n Self::suffix_sum(piles, i)\n } else {\n Self::game(piles, i, m, 2 * m)\n }\n } else if k == 0 {\n 0\n } else {\n let new_m = if k > m { k } else { m };\n let score_k = Self::suffix_sum(piles, i)\n - Self::game(piles, i + k, new_m, 2 * new_m + 1);\n if k == 1 {\n score_k\n } else {\n let prev = Self::game(piles, i, m, k - 1);\n if score_k >= prev { score_k } else { prev }\n }\n }\n }\n\n pub open spec fn optimal(piles: Seq, i: int, m: int) -> int\n {\n Self::game(piles, i, m, 2 * m + 1)\n }\n\n pub open spec fn max_score(piles: Seq, i: int, m: int, k: int) -> int\n {\n Self::game(piles, i, m, k)\n }\n\n pub fn stone_game_ii(piles: Vec) -> (result: i32)\n requires\n 1 <= piles.len() <= 100,\n forall |i: int| 0 <= i < piles.len() ==> 1 <= #[trigger] piles[i] <= 10000,\n ensures\n result as int == Self::optimal(piles@, 0, 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn stone_game_ii(piles: Vec) -> i32 {\n let n = piles.len();\n let stride = n + 1;\n let mut suffix_sums: Vec = Vec::new();\n let mut k: usize = 0;\n while k < stride {\n suffix_sums.push(0i32);\n k += 1;\n }\n let mut si = n;\n while si > 0 {\n si -= 1;\n suffix_sums[si] = suffix_sums[si + 1] + piles[si];\n }\n let total = stride * stride;\n let mut dp: Vec = Vec::new();\n let mut k2: usize = 0;\n while k2 < total {\n dp.push(0i32);\n k2 += 1;\n }\n let mut i: usize = n;\n while i > 0 {\n i -= 1;\n let mut m: usize = 1;\n while m <= n {\n if i + 2 * m >= n {\n dp[i * stride + m] = suffix_sums[i];\n } else {\n let mut best: i32 = suffix_sums[i] - dp[(i + 1) * stride + m];\n let mut x: usize = 2;\n while x <= 2 * m {\n let new_m: usize = if x > m { x } else { m };\n let score: i32 = suffix_sums[i] - dp[(i + x) * stride + new_m];\n if score > best {\n best = score;\n }\n x += 1;\n }\n dp[i * stride + m] = best;\n }\n m += 1;\n }\n }\n dp[1]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn suffix_sum(piles: Seq, i: int) -> int\n decreases piles.len() - i\n {\n if i >= piles.len() {\n 0\n } else {\n piles[i] as int + Self::suffix_sum(piles, i + 1)\n }\n }\n\n pub open spec fn game(piles: Seq, i: int, m: int, k: int) -> int\n decreases piles.len() - i, k\n when m >= 1 && k >= 0 && i >= 0\n && (1 <= k && k <= 2 * m ==> i + 2 * m < piles.len())\n {\n if k > 2 * m {\n if i >= piles.len() {\n 0\n } else if i + 2 * m >= piles.len() {\n Self::suffix_sum(piles, i)\n } else {\n Self::game(piles, i, m, 2 * m)\n }\n } else if k == 0 {\n 0\n } else {\n let new_m = if k > m { k } else { m };\n let score_k = Self::suffix_sum(piles, i)\n - Self::game(piles, i + k, new_m, 2 * new_m + 1);\n if k == 1 {\n score_k\n } else {\n let prev = Self::game(piles, i, m, k - 1);\n if score_k >= prev { score_k } else { prev }\n }\n }\n }\n\n pub open spec fn optimal(piles: Seq, i: int, m: int) -> int\n {\n Self::game(piles, i, m, 2 * m + 1)\n }\n\n pub open spec fn max_score(piles: Seq, i: int, m: int, k: int) -> int\n {\n Self::game(piles, i, m, k)\n }\n\n pub fn stone_game_ii(piles: Vec) -> (result: i32)\n requires\n 1 <= piles.len() <= 100,\n forall |i: int| 0 <= i < piles.len() ==> 1 <= #[trigger] piles[i] <= 10000,\n ensures\n result as int == Self::optimal(piles@, 0, 1),\n {\n let n = piles.len();\n let stride = n + 1;\n let mut suffix_sums: Vec = Vec::new();\n let mut k: usize = 0;\n while k < stride {\n suffix_sums.push(0i32);\n k += 1;\n }\n let mut si = n;\n while si > 0 {\n si -= 1;\n suffix_sums.set(si, suffix_sums[si + 1] + piles[si]);\n }\n let total = stride * stride;\n let mut dp: Vec = Vec::new();\n let mut k2: usize = 0;\n while k2 < total {\n dp.push(0i32);\n k2 += 1;\n }\n let mut i: usize = n;\n while i > 0 {\n i -= 1;\n let mut m: usize = 1;\n while m <= n {\n if i + 2 * m >= n {\n dp.set(i * stride + m, suffix_sums[i]);\n } else {\n let mut best: i32 = suffix_sums[i] - dp[(i + 1) * stride + m];\n let mut x: usize = 2;\n while x <= 2 * m {\n let new_m: usize = if x > m { x } else { m };\n let score: i32 = suffix_sums[i] - dp[(i + x) * stride + new_m];\n if score > best {\n best = score;\n }\n x += 1;\n }\n dp.set(i * stride + m, best);\n }\n m += 1;\n }\n }\n dp[1]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn suffix_sum(piles: Seq, i: int) -> int\n decreases piles.len() - i\n {\n if i >= piles.len() {\n 0\n } else {\n piles[i] as int + Self::suffix_sum(piles, i + 1)\n }\n }\n\n pub open spec fn game(piles: Seq, i: int, m: int, k: int) -> int\n decreases piles.len() - i, k\n when m >= 1 && k >= 0 && i >= 0\n && (1 <= k && k <= 2 * m ==> i + 2 * m < piles.len())\n {\n if k > 2 * m {\n if i >= piles.len() {\n 0\n } else if i + 2 * m >= piles.len() {\n Self::suffix_sum(piles, i)\n } else {\n Self::game(piles, i, m, 2 * m)\n }\n } else if k == 0 {\n 0\n } else {\n let new_m = if k > m { k } else { m };\n let score_k = Self::suffix_sum(piles, i)\n - Self::game(piles, i + k, new_m, 2 * new_m + 1);\n if k == 1 {\n score_k\n } else {\n let prev = Self::game(piles, i, m, k - 1);\n if score_k >= prev { score_k } else { prev }\n }\n }\n }\n\n pub open spec fn optimal(piles: Seq, i: int, m: int) -> int\n {\n Self::game(piles, i, m, 2 * m + 1)\n }\n\n pub open spec fn max_score(piles: Seq, i: int, m: int, k: int) -> int\n {\n Self::game(piles, i, m, k)\n }\n\n proof fn lemma_suffix_sum_bounds(piles: Seq, i: int)\n requires\n 0 <= i <= piles.len(),\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n ensures\n 0 <= Self::suffix_sum(piles, i) <= (piles.len() - i) * 10000,\n decreases piles.len() - i\n {\n if i < piles.len() {\n Self::lemma_suffix_sum_bounds(piles, i + 1);\n assert(10000 + (piles.len() - i - 1) * 10000 == (piles.len() - i) * 10000) by (nonlinear_arith)\n requires piles.len() - i >= 1 {}\n }\n }\n\n proof fn lemma_suffix_sum_monotone(piles: Seq, i: int, j: int)\n requires\n 0 <= i <= j <= piles.len(),\n forall |k: int| 0 <= k < piles.len() ==> 1 <= #[trigger] piles[k] <= 10000,\n ensures\n Self::suffix_sum(piles, j) <= Self::suffix_sum(piles, i),\n decreases j - i\n {\n if i < j {\n Self::lemma_suffix_sum_monotone(piles, i + 1, j);\n }\n }\n\n proof fn lemma_game_bounds(piles: Seq, i: int, m: int, k: int)\n requires\n m >= 1, k >= 0, i >= 0, i <= piles.len(),\n k <= 2 * m ==> i + 2 * m < piles.len(),\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n ensures\n 0 <= Self::game(piles, i, m, k) <= Self::suffix_sum(piles, i),\n decreases piles.len() - i, k\n {\n if k > 2 * m {\n if i >= piles.len() {\n } else if i + 2 * m >= piles.len() {\n Self::lemma_suffix_sum_bounds(piles, i);\n } else {\n Self::lemma_game_bounds(piles, i, m, 2 * m);\n }\n } else if k == 0 {\n Self::lemma_suffix_sum_bounds(piles, i);\n } else {\n let new_m = if k > m { k } else { m };\n assert(new_m >= 1);\n assert(i + k >= 1);\n assert(i + k <= i + 2 * m);\n assert(i + 2 * m < piles.len());\n assert(i + k <= piles.len());\n Self::lemma_game_bounds(piles, i + k, new_m, 2 * new_m + 1);\n Self::lemma_suffix_sum_monotone(piles, i, i + k);\n Self::lemma_suffix_sum_bounds(piles, i);\n if k > 1 {\n Self::lemma_game_bounds(piles, i, m, k - 1);\n }\n }\n }\n\n proof fn lemma_optimal_bounds(piles: Seq, i: int, m: int)\n requires\n 0 <= i <= piles.len(),\n m >= 1,\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n ensures\n 0 <= Self::optimal(piles, i, m) <= Self::suffix_sum(piles, i),\n {\n Self::lemma_game_bounds(piles, i, m, 2 * m + 1);\n }\n\n proof fn lemma_max_score_bounds(piles: Seq, i: int, m: int, k: int)\n requires\n 0 <= i < piles.len(),\n i + 2 * m < piles.len(),\n 1 <= k <= 2 * m,\n m >= 1,\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n ensures\n 0 <= Self::max_score(piles, i, m, k) <= Self::suffix_sum(piles, i),\n {\n Self::lemma_game_bounds(piles, i, m, k);\n }\n\n proof fn lemma_flat_idx_bound(i: int, m: int, n: int)\n requires\n 0 <= i <= n,\n 0 <= m <= n,\n n <= 100,\n ensures\n 0 <= i * (n + 1) + m,\n i * (n + 1) + m < (n + 1) * (n + 1),\n i * (n + 1) + m <= 10200,\n i * (n + 1) <= 10100,\n {\n assert(i * (n + 1) >= 0) by (nonlinear_arith) requires 0 <= i, 0 <= n {}\n assert(i * (n + 1) <= n * (n + 1)) by (nonlinear_arith) requires 0 <= i <= n, 0 <= n {}\n assert(n * (n + 1) <= 10100) by (nonlinear_arith) requires 0 <= n, n <= 100 {}\n assert(i * (n + 1) + m >= 0) by (nonlinear_arith) requires i * (n + 1) >= 0, m >= 0 {}\n assert(i * (n + 1) + m <= n * (n + 1) + n) by (nonlinear_arith) requires 0 <= i <= n, 0 <= m <= n, 0 <= n {}\n assert(n * (n + 1) + n == (n + 1) * (n + 1) - 1) by (nonlinear_arith) requires 0 <= n {}\n assert((n + 1) * (n + 1) - 1 <= 10200) by (nonlinear_arith) requires 0 <= n, n <= 100 {}\n }\n\n proof fn lemma_flat_idx_ne_higher_row(i1: int, m1: int, i2: int, m2: int, n: int)\n requires\n 0 <= i1 < i2 <= n,\n 0 <= m1 <= n,\n 0 <= m2 <= n,\n n <= 100,\n ensures\n i1 * (n + 1) + m1 != i2 * (n + 1) + m2,\n {\n assert(i2 * (n + 1) + m2 >= (i1 + 1) * (n + 1)) by (nonlinear_arith)\n requires i2 >= i1 + 1, m2 >= 0, n >= 0 {}\n assert((i1 + 1) * (n + 1) == i1 * (n + 1) + (n + 1)) by (nonlinear_arith)\n requires n >= 0 {}\n assert(i1 * (n + 1) + m1 <= i1 * (n + 1) + n) by (nonlinear_arith)\n requires m1 <= n {}\n assert(i1 * (n + 1) + n < i1 * (n + 1) + (n + 1)) by (nonlinear_arith)\n requires n >= 0 {}\n }\n\n proof fn lemma_flat_idx_ne_same_row(i: int, m1: int, m2: int, n: int)\n requires\n 0 <= i <= n,\n 0 <= m1 <= n,\n 0 <= m2 <= n,\n m1 != m2,\n n <= 100,\n ensures\n i * (n + 1) + m1 != i * (n + 1) + m2,\n {}\n\n spec fn flat(i: int, m: int, n: int) -> int {\n i * (n + 1) + m\n }\n\n pub fn stone_game_ii(piles: Vec) -> (result: i32)\n requires\n 1 <= piles.len() <= 100,\n forall |i: int| 0 <= i < piles.len() ==> 1 <= #[trigger] piles[i] <= 10000,\n ensures\n result as int == Self::optimal(piles@, 0, 1),\n {\n let n = piles.len();\n let stride = n + 1;\n let ghost ni = n as int;\n\n let mut suffix_sums: Vec = Vec::new();\n let mut k: usize = 0;\n while k < stride\n invariant\n k <= stride,\n stride == n + 1,\n suffix_sums.len() == k as int,\n forall |j: int| 0 <= j < k as int ==> suffix_sums@[j] == 0i32,\n decreases stride - k\n {\n suffix_sums.push(0i32);\n k += 1;\n }\n\n let mut si = n;\n while si > 0\n invariant\n 0 <= si <= n,\n n == piles.len(),\n suffix_sums.len() == stride as int,\n stride == n + 1,\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n forall |j: int| si as int <= j <= n as int\n ==> suffix_sums@[j] as int == Self::suffix_sum(piles@, j),\n decreases si\n {\n si -= 1;\n proof {\n Self::lemma_suffix_sum_bounds(piles@, si as int + 1);\n Self::lemma_suffix_sum_bounds(piles@, si as int);\n assert((piles@.len() - si as int) * 10000 <= 1_000_000) by (nonlinear_arith)\n requires piles@.len() <= 100, si as int >= 0 {}\n }\n suffix_sums.set(si, suffix_sums[si + 1] + piles[si]);\n }\n\n proof {\n assert(stride <= 101usize);\n assert((stride as int) * (stride as int) <= 10201) by (nonlinear_arith)\n requires stride as int >= 0, stride as int <= 101 {}\n }\n let total = stride * stride;\n\n let mut dp: Vec = Vec::new();\n let mut k2: usize = 0;\n while k2 < total\n invariant\n k2 <= total,\n total == stride * stride,\n stride == n + 1,\n n <= 100,\n dp.len() == k2 as int,\n forall |j: int| 0 <= j < k2 as int ==> dp@[j] == 0i32,\n decreases total - k2\n {\n dp.push(0i32);\n k2 += 1;\n }\n\n proof {\n assert(dp@.len() == (ni + 1) * (ni + 1)) by {\n assert(dp@.len() == total as int);\n assert(total as int == (stride as int) * (stride as int));\n assert(stride as int == ni + 1);\n assert((stride as int) * (stride as int) == (ni + 1) * (ni + 1));\n }\n \n assert forall |j: int, mm: int|\n ni <= j <= ni && 1 <= mm <= ni\n implies dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm)\n by {\n assert(j == ni);\n Self::lemma_flat_idx_bound(j, mm, ni);\n assert(Self::flat(j, mm, ni) < total as int) by {\n assert(j * (ni + 1) + mm < (ni + 1) * (ni + 1));\n assert(total as int == (stride as int) * (stride as int));\n assert(stride as int == ni + 1);\n }\n assert(dp@[Self::flat(j, mm, ni)] == 0i32);\n assert(ni == piles@.len());\n assert(j >= piles@.len());\n };\n }\n\n let mut i: usize = n;\n while i > 0\n invariant\n 0 <= i,\n i <= n,\n n == piles.len(),\n n >= 1,\n stride == n + 1,\n total == stride * stride,\n stride <= 101,\n ni == n as int,\n dp.len() == total as int,\n dp@.len() == (ni + 1) * (ni + 1),\n suffix_sums.len() == stride as int,\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n forall |j: int| 0 <= j <= n as int\n ==> suffix_sums@[j] as int == Self::suffix_sum(piles@, j),\n forall |j: int, mm: int|\n i as int <= j <= ni && 1 <= mm <= ni\n ==> dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm),\n decreases i\n {\n i -= 1;\n\n let mut m: usize = 1;\n\n while m <= n\n invariant\n 0 <= i,\n i < n,\n n == piles.len(),\n n >= 1,\n stride == n + 1,\n total == stride * stride,\n stride <= 101,\n ni == n as int,\n dp.len() == total as int,\n dp@.len() == (ni + 1) * (ni + 1),\n suffix_sums.len() == stride as int, 1 <= m,\n m <= n + 1,\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n forall |j: int| 0 <= j <= n as int\n ==> suffix_sums@[j] as int == Self::suffix_sum(piles@, j),\n forall |j: int, mm: int|\n (i as int + 1) <= j <= ni && 1 <= mm <= ni\n ==> dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm),\n forall |mm: int|\n 1 <= mm < m as int\n ==> dp@[Self::flat(i as int, mm, ni)] as int == Self::optimal(piles@, i as int, mm),\n decreases n - m + 1\n {\n proof {\n assert(i as int <= ni);\n assert(m as int <= ni);\n Self::lemma_flat_idx_bound(i as int, m as int, ni);\n }\n\n if i + 2 * m >= n {\n proof {\n Self::lemma_suffix_sum_bounds(piles@, i as int);\n assert((piles@.len() - i as int) * 10000 <= 1_000_000) by (nonlinear_arith)\n requires piles@.len() <= 100, i as int >= 0 {}\n assert(i as int + 2 * (m as int) >= piles@.len());\n }\n let ghost old_dp = dp@;\n dp.set(i * stride + m, suffix_sums[i]);\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n let ghost idx = Self::flat(ii, mi, ni);\n assert(idx == (i * stride + m) as int) by {\n assert(stride as int == ni + 1);\n }\n assert(dp@[idx] as int == suffix_sums@[ii] as int);\n assert(suffix_sums@[ii] as int == Self::suffix_sum(piles@, ii));\n assert(dp@[idx] as int == Self::optimal(piles@, ii, mi));\n assert forall |j: int, mm: int|\n (ii + 1) <= j <= ni && 1 <= mm <= ni\n implies dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm)\n by {\n Self::lemma_flat_idx_bound(j, mm, ni);\n Self::lemma_flat_idx_ne_higher_row(ii, mi, j, mm, ni);\n assert(Self::flat(j, mm, ni) != idx);\n assert(dp@[Self::flat(j, mm, ni)] == old_dp[Self::flat(j, mm, ni)]);\n }\n assert forall |mm: int|\n 1 <= mm < mi\n implies dp@[Self::flat(ii, mm, ni)] as int == Self::optimal(piles@, ii, mm)\n by {\n Self::lemma_flat_idx_bound(ii, mm, ni);\n Self::lemma_flat_idx_ne_same_row(ii, mm, mi, ni);\n assert(Self::flat(ii, mm, ni) != idx);\n assert(dp@[Self::flat(ii, mm, ni)] == old_dp[Self::flat(ii, mm, ni)]);\n }\n }\n } else {\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n Self::lemma_suffix_sum_bounds(piles@, ii);\n assert((piles@.len() - ii) * 10000 <= 1_000_000) by (nonlinear_arith)\n requires piles@.len() <= 100, ii >= 0 {}\n assert(ii + 1 <= ni);\n Self::lemma_optimal_bounds(piles@, ii + 1, mi);\n Self::lemma_suffix_sum_monotone(piles@, ii, ii + 1);\n Self::lemma_flat_idx_bound(ii + 1, mi, ni);\n assert(Self::flat(ii + 1, mi, ni) == ((i + 1) * stride + m) as int) by {\n assert(stride as int == ni + 1);\n }\n }\n let mut best: i32 = suffix_sums[i] - dp[(i + 1) * stride + m];\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n assert(dp@[Self::flat(ii + 1, mi, ni)] as int\n == Self::optimal(piles@, ii + 1, mi));\n assert(best as int == Self::suffix_sum(piles@, ii)\n - Self::optimal(piles@, ii + 1, mi));\n assert(best as int == Self::max_score(piles@, ii, mi, 1));\n Self::lemma_max_score_bounds(piles@, ii, mi, 1);\n }\n let mut x: usize = 2;\n while x <= 2 * m\n invariant\n 0 <= i,\n i < n,\n n == piles.len(),\n n >= 1,\n stride == n + 1,\n total == stride * stride,\n stride <= 101,\n ni == n as int,\n dp.len() == total as int,\n dp@.len() == (ni + 1) * (ni + 1),\n suffix_sums.len() == stride as int,\n 1 <= m,\n m <= n,\n i + 2 * m < n,\n 2 <= x,\n x <= 2 * m + 1,\n piles.len() <= 100,\n forall |j: int| 0 <= j < piles.len() ==> 1 <= #[trigger] piles[j] <= 10000,\n forall |j: int| 0 <= j <= n as int\n ==> suffix_sums@[j] as int == Self::suffix_sum(piles@, j),\n forall |j: int, mm: int|\n (i as int + 1) <= j <= ni && 1 <= mm <= ni\n ==> dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm),\n best as int == Self::max_score(piles@, i as int, m as int, (x - 1) as int),\n 0 <= best as int,\n best as int <= 1_000_000,\n decreases 2 * m - x + 1\n {\n let new_m: usize = if x > m { x } else { m };\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n let ghost xi = x as int;\n let ghost new_mi: int = if xi > mi { xi } else { mi };\n assert(new_m as int == new_mi);\n assert(new_mi >= 1);\n assert(xi <= 2 * mi);\n assert(new_mi <= 2 * mi);\n assert(ii >= 0);\n assert(ii + 2 * mi < ni);\n assert(2 * mi < ni);\n assert(new_mi < ni);\n assert(new_mi <= ni);\n assert(ii + xi <= ii + 2 * mi);\n assert(ii + xi < ni);\n assert(ii + xi <= ni);\n Self::lemma_flat_idx_bound(ii + xi, new_mi, ni);\n assert(Self::flat(ii + xi, new_mi, ni) == ((i + x) * stride + new_m) as int) by {\n assert(stride as int == ni + 1);\n }\n Self::lemma_suffix_sum_bounds(piles@, ii);\n assert((piles@.len() - ii) * 10000 <= 1_000_000) by (nonlinear_arith)\n requires piles@.len() <= 100, ii >= 0 {}\n Self::lemma_optimal_bounds(piles@, ii + xi, new_mi);\n Self::lemma_suffix_sum_monotone(piles@, ii, ii + xi);\n }\n let score: i32 = suffix_sums[i] - dp[(i + x) * stride + new_m];\n if score > best {\n best = score;\n }\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n let ghost xi = x as int;\n let ghost new_mi: int = if xi > mi { xi } else { mi };\n assert(dp@[Self::flat(ii + xi, new_mi, ni)] as int\n == Self::optimal(piles@, ii + xi, new_mi));\n let ghost score_x = Self::suffix_sum(piles@, ii)\n - Self::optimal(piles@, ii + xi, new_mi);\n assert(score as int == score_x);\n let ghost prev = Self::max_score(piles@, ii, mi, xi - 1);\n assert(best as int == if score_x >= prev { score_x } else { prev });\n assert(best as int == Self::max_score(piles@, ii, mi, xi));\n Self::lemma_max_score_bounds(piles@, ii, mi, xi);\n }\n x += 1;\n }\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n assert(best as int == Self::max_score(piles@, ii, mi, 2 * mi));\n assert(Self::optimal(piles@, ii, mi) == Self::max_score(piles@, ii, mi, 2 * mi));\n Self::lemma_flat_idx_bound(ii, mi, ni);\n assert(Self::flat(ii, mi, ni) == (i * stride + m) as int) by {\n assert(stride as int == ni + 1);\n }\n }\n let ghost old_dp = dp@;\n dp.set(i * stride + m, best);\n proof {\n let ghost ii = i as int;\n let ghost mi = m as int;\n let ghost idx = Self::flat(ii, mi, ni);\n assert(idx == (i * stride + m) as int) by {\n assert(stride as int == ni + 1);\n }\n assert(dp@[idx] as int == Self::optimal(piles@, ii, mi));\n assert forall |j: int, mm: int|\n (ii + 1) <= j <= ni && 1 <= mm <= ni\n implies dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm)\n by {\n Self::lemma_flat_idx_bound(j, mm, ni);\n Self::lemma_flat_idx_ne_higher_row(ii, mi, j, mm, ni);\n assert(Self::flat(j, mm, ni) != idx);\n assert(dp@[Self::flat(j, mm, ni)] == old_dp[Self::flat(j, mm, ni)]);\n }\n assert forall |mm: int|\n 1 <= mm < mi\n implies dp@[Self::flat(ii, mm, ni)] as int == Self::optimal(piles@, ii, mm)\n by {\n Self::lemma_flat_idx_bound(ii, mm, ni);\n Self::lemma_flat_idx_ne_same_row(ii, mm, mi, ni);\n assert(Self::flat(ii, mm, ni) != idx);\n assert(dp@[Self::flat(ii, mm, ni)] == old_dp[Self::flat(ii, mm, ni)]);\n }\n }\n }\n m += 1;\n }\n\n \n proof {\n let ghost ii = i as int;\n assert forall |j: int, mm: int|\n ii <= j <= ni && 1 <= mm <= ni\n implies dp@[Self::flat(j, mm, ni)] as int == Self::optimal(piles@, j, mm)\n by {\n Self::lemma_flat_idx_bound(j, mm, ni);\n if j > ii {\n assert((ii + 1) <= j);\n } else {\n assert(j == ii);\n assert(1 <= mm);\n assert(mm <= ni);\n }\n }\n }\n }\n proof {\n assert(i == 0);\n assert(ni >= 1);\n Self::lemma_flat_idx_bound(0, 1, ni);\n assert(Self::flat(0, 1, ni) == 1int);\n assert(dp@[1] as int == Self::optimal(piles@, 0, 1));\n }\n dp[1]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1140/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1140/tests/mutated_testcases.jsonl"} {"problem": "lc1157", "family": "leetcode", "description": "# Online Majority Element In Subarray\n\nGiven an array `arr`, find the **majority element** of a given subarray. The **majority element** of a subarray is an element that occurs `threshold` times or more in the subarray.\n\nGiven `arr`, `left`, `right`, and `threshold`, return the element in the subarray `arr[left...right]` that occurs at least `threshold` times, or `-1` if no such element exists.\n\n## Example 1:\n\n> **Input:** arr = [1, 1, 2, 2, 1, 1], left = 0, right = 5, threshold = 4\n> **Output:** 1\n\n## Example 2:\n\n> **Input:** arr = [1, 1, 2, 2, 1, 1], left = 0, right = 3, threshold = 3\n> **Output:** -1\n\n## Example 3:\n\n> **Input:** arr = [1, 1, 2, 2, 1, 1], left = 2, right = 3, threshold = 2\n> **Output:** 2\n\n## Constraints:\n\n- $1 \\leq arr.length \\leq 2 * 10^4$\n- $1 \\leq arr[i] \\leq 2 * 10^4$\n- $0 \\leq left \\leq right < arr.length$\n- $threshold \\leq right - left + 1$\n- $2 * threshold > right - left + 1$\n\n## Starter Code\n\n```rust\nstruct MajorityChecker {\n\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl MajorityChecker {\n\n fn new(arr: Vec) -> Self {\n \n }\n \n fn query(&self, left: i32, right: i32, threshold: i32) -> i32 {\n \n }\n}\n\n/**\n * Your MajorityChecker object will be instantiated and called as such:\n * let obj = MajorityChecker::new(arr);\n * let ret_1: i32 = obj.query(left, right, threshold);\n */\n```\n", "tags": "Hard\nArray, Binary Search, Design, Binary Indexed Tree, Segment Tree\nAcceptance rate: 40.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_occurrences(arr: Seq, left: int, right: int, val: i32) -> int\n decreases right - left + 1\n {\n if left > right {\n 0\n } else if arr[left] == val {\n 1 + Self::count_occurrences(arr, left + 1, right, val)\n } else {\n Self::count_occurrences(arr, left + 1, right, val)\n }\n }\n}\n\npub struct MajorityChecker {\n pub arr: Vec,\n}\n\nimpl MajorityChecker {\n pub fn new(arr: Vec) -> (result: Self)\n requires\n 1 <= arr.len() <= 20_000,\n forall |i: int| 0 <= i < arr@.len() ==> 1 <= #[trigger] arr@[i] <= 20_000,\n ensures\n result.arr@ == arr@,\n {\n }\n\n pub fn query(&self, left: i32, right: i32, threshold: i32) -> (result: i32)\n requires\n 1 <= self.arr.len() <= 20_000,\n forall |i: int| 0 <= i < self.arr@.len() ==> 1 <= #[trigger] self.arr@[i] <= 20_000,\n 0 <= left <= right,\n right < self.arr.len() as i32,\n threshold >= 1,\n threshold <= right - left + 1,\n 2 * threshold > right - left + 1,\n ensures\n result == -1 || (\n 1 <= result <= 20_000\n && Solution::count_occurrences(self.arr@, left as int, right as int, result) >= threshold as int\n ),\n result == -1 ==> forall |v: i32| 1 <= v <= 20_000 ==>\n Solution::count_occurrences(self.arr@, left as int, right as int, v) < threshold as int,\n {\n }\n}\n\n}\n", "code": "pub struct MajorityChecker {\n pub arr: Vec,\n}\n\nimpl MajorityChecker {\n pub fn new(arr: Vec) -> Self {\n MajorityChecker { arr }\n }\n\n pub fn query(&self, left: i32, right: i32, threshold: i32) -> i32 {\n let l = left as usize;\n let r = right as usize;\n let mut candidate: i32 = self.arr[l];\n let mut cnt: i32 = 0;\n let mut i: usize = l;\n while i <= r {\n if cnt == 0 {\n candidate = self.arr[i];\n cnt = 1;\n } else if self.arr[i] == candidate {\n cnt = cnt + 1;\n } else {\n cnt = cnt - 1;\n }\n i = i + 1;\n }\n let mut actual_count: i32 = 0;\n let mut j: usize = l;\n while j <= r {\n if self.arr[j] == candidate {\n actual_count = actual_count + 1;\n }\n j = j + 1;\n }\n if actual_count >= threshold {\n candidate\n } else {\n -1\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_occurrences(arr: Seq, left: int, right: int, val: i32) -> int\n decreases right - left + 1\n {\n if left > right {\n 0\n } else if arr[left] == val {\n 1 + Self::count_occurrences(arr, left + 1, right, val)\n } else {\n Self::count_occurrences(arr, left + 1, right, val)\n }\n }\n}\n\npub struct MajorityChecker {\n pub arr: Vec,\n}\n\nimpl MajorityChecker {\n pub fn new(arr: Vec) -> (result: Self)\n requires\n 1 <= arr.len() <= 20_000,\n forall |i: int| 0 <= i < arr@.len() ==> 1 <= #[trigger] arr@[i] <= 20_000,\n ensures\n result.arr@ == arr@,\n {\n MajorityChecker { arr }\n }\n\n pub fn query(&self, left: i32, right: i32, threshold: i32) -> (result: i32)\n requires\n 1 <= self.arr.len() <= 20_000,\n forall |i: int| 0 <= i < self.arr@.len() ==> 1 <= #[trigger] self.arr@[i] <= 20_000,\n 0 <= left <= right,\n right < self.arr.len() as i32,\n threshold >= 1,\n threshold <= right - left + 1,\n 2 * threshold > right - left + 1,\n ensures\n result == -1 || (\n 1 <= result <= 20_000\n && Solution::count_occurrences(self.arr@, left as int, right as int, result) >= threshold as int\n ),\n result == -1 ==> forall |v: i32| 1 <= v <= 20_000 ==>\n Solution::count_occurrences(self.arr@, left as int, right as int, v) < threshold as int,\n {\n let l = left as usize;\n let r = right as usize;\n let mut candidate: i32 = self.arr[l];\n let mut cnt: i32 = 0;\n let mut i: usize = l;\n while i <= r {\n if cnt == 0 {\n candidate = self.arr[i];\n cnt = 1;\n } else if self.arr[i] == candidate {\n cnt = cnt + 1;\n } else {\n cnt = cnt - 1;\n }\n i = i + 1;\n }\n let mut actual_count: i32 = 0;\n let mut j: usize = l;\n while j <= r {\n if self.arr[j] == candidate {\n actual_count = actual_count + 1;\n }\n j = j + 1;\n }\n if actual_count >= threshold {\n candidate\n } else {\n -1\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_occurrences(arr: Seq, left: int, right: int, val: i32) -> int\n decreases right - left + 1\n {\n if left > right {\n 0\n } else if arr[left] == val {\n 1 + Self::count_occurrences(arr, left + 1, right, val)\n } else {\n Self::count_occurrences(arr, left + 1, right, val)\n }\n }\n\n proof fn count_extend_right(arr: Seq, l: int, r: int, v: i32)\n requires\n 0 <= l <= r,\n r < arr.len(),\n ensures\n Self::count_occurrences(arr, l, r, v) ==\n Self::count_occurrences(arr, l, r - 1, v) + if arr[r] == v { 1int } else { 0int }\n decreases r - l\n {\n if l == r {\n assert(Self::count_occurrences(arr, l + 1, l, v) == 0);\n assert(Self::count_occurrences(arr, l, l - 1, v) == 0);\n } else {\n Self::count_extend_right(arr, l + 1, r, v);\n }\n }\n\n proof fn count_non_negative(arr: Seq, l: int, r: int, v: i32)\n requires\n 0 <= l,\n r < arr.len(),\n ensures\n Self::count_occurrences(arr, l, r, v) >= 0\n decreases r - l + 1\n {\n if l <= r {\n Self::count_non_negative(arr, l + 1, r, v);\n }\n }\n}\n\npub struct MajorityChecker {\n pub arr: Vec,\n}\n\nimpl MajorityChecker {\n pub fn new(arr: Vec) -> (result: Self)\n requires\n 1 <= arr.len() <= 20_000,\n forall |i: int| 0 <= i < arr@.len() ==> 1 <= #[trigger] arr@[i] <= 20_000,\n ensures\n result.arr@ == arr@,\n {\n MajorityChecker { arr }\n }\n\n pub fn query(&self, left: i32, right: i32, threshold: i32) -> (result: i32)\n requires\n 1 <= self.arr.len() <= 20_000,\n forall |i: int| 0 <= i < self.arr@.len() ==> 1 <= #[trigger] self.arr@[i] <= 20_000,\n 0 <= left <= right,\n right < self.arr.len() as i32,\n threshold >= 1,\n threshold <= right - left + 1,\n 2 * threshold > right - left + 1,\n ensures\n result == -1 || (\n 1 <= result <= 20_000\n && Solution::count_occurrences(self.arr@, left as int, right as int, result) >= threshold as int\n ),\n result == -1 ==> forall |v: i32| 1 <= v <= 20_000 ==>\n Solution::count_occurrences(self.arr@, left as int, right as int, v) < threshold as int,\n {\n let l = left as usize;\n let r = right as usize;\n let mut candidate: i32 = self.arr[l];\n let mut cnt: i32 = 0;\n let mut i: usize = l;\n while i <= r\n invariant\n l as int == left as int,\n r as int == right as int,\n 0 <= left <= right,\n right < self.arr.len() as i32,\n 1 <= self.arr.len() <= 20_000,\n forall |k: int| 0 <= k < self.arr@.len() ==> 1 <= #[trigger] self.arr@[k] <= 20_000,\n l <= i <= r + 1,\n cnt >= 0,\n cnt <= i as int - l as int,\n 1 <= candidate <= 20_000,\n forall |v: i32| 1 <= v <= 20_000 && v != candidate ==>\n 2 * (#[trigger] Solution::count_occurrences(self.arr@, l as int, (i as int) - 1, v))\n <= i as int - l as int - cnt as int,\n 2 * Solution::count_occurrences(self.arr@, l as int, (i as int) - 1, candidate)\n <= i as int - l as int + cnt as int,\n decreases r + 1 - i\n {\n let ghost old_candidate = candidate;\n let ghost old_cnt = cnt;\n\n if cnt == 0 {\n candidate = self.arr[i];\n cnt = 1;\n } else if self.arr[i] == candidate {\n cnt = cnt + 1;\n } else {\n cnt = cnt - 1;\n }\n\n proof {\n assert forall |v: i32| 1 <= v <= 20_000 && v != candidate implies\n 2 * (#[trigger] Solution::count_occurrences(self.arr@, l as int, i as int, v))\n <= (i + 1) as int - l as int - cnt as int\n by {\n Solution::count_extend_right(self.arr@, l as int, i as int, v);\n Solution::count_non_negative(self.arr@, l as int, (i as int) - 1, v);\n };\n\n Solution::count_extend_right(self.arr@, l as int, i as int, candidate);\n Solution::count_non_negative(self.arr@, l as int, (i as int) - 1, candidate);\n }\n\n i = i + 1;\n }\n\n let ghost bm_cnt = cnt;\n\n let mut actual_count: i32 = 0;\n let mut j: usize = l;\n while j <= r\n invariant\n l as int == left as int,\n r as int == right as int,\n 0 <= left <= right,\n right < self.arr.len() as i32,\n 1 <= self.arr.len() <= 20_000,\n forall |k: int| 0 <= k < self.arr@.len() ==> 1 <= #[trigger] self.arr@[k] <= 20_000,\n l <= j <= r + 1,\n 1 <= candidate <= 20_000,\n actual_count >= 0,\n actual_count as int == Solution::count_occurrences(self.arr@, l as int, (j as int) - 1, candidate),\n actual_count <= (j - l) as i32,\n cnt == bm_cnt,\n bm_cnt >= 0,\n forall |v: i32| 1 <= v <= 20_000 && v != candidate ==>\n 2 * (#[trigger] Solution::count_occurrences(self.arr@, l as int, r as int, v))\n <= r as int + 1 - l as int - bm_cnt as int,\n decreases r + 1 - j\n {\n proof {\n Solution::count_extend_right(self.arr@, l as int, j as int, candidate);\n }\n if self.arr[j] == candidate {\n actual_count = actual_count + 1;\n }\n j = j + 1;\n }\n\n if actual_count >= threshold {\n candidate\n } else {\n proof {\n assert forall |v: i32| 1 <= v <= 20_000 implies\n (#[trigger] Solution::count_occurrences(self.arr@, l as int, r as int, v)) < threshold as int\n by {\n if v == candidate {\n } else {\n Solution::count_non_negative(self.arr@, l as int, r as int, v);\n }\n };\n }\n -1\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1157/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1157/tests/mutated_testcases.jsonl"} {"problem": "lc1200", "family": "leetcode", "description": "# Minimum Absolute Difference\n\nGiven an array of **distinct** integers `arr`, find all pairs of elements with the minimum absolute difference of any two elements.\n\nReturn a list of pairs in ascending order (with respect to pairs), each pair `[a, b]` follows:\n\n- `a, b` are from `arr`\n- `a < b`\n- `b - a` equals to the minimum absolute difference of any two elements in `arr`\n\n## Example 1:\n\n> **Input:** arr = [4,2,1,3]\n> **Output:** [[1,2],[2,3],[3,4]]\n> **Explanation:** The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.\n\n## Example 2:\n\n> **Input:** arr = [1,3,6,10,15]\n> **Output:** [[1,3]]\n\n## Example 3:\n\n> **Input:** arr = [3,8,-10,23,19,-4,-14,27]\n> **Output:** [[-14,-10],[19,23],[23,27]]\n\n## Constraints:\n\n- $2 <= arr.length <= 10^5$\n- $-10^6 <= arr[i] <= 10^6$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_abs_difference(arr: Vec) -> Vec> {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sorting\nAcceptance rate: 75.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn seq_contains(s: Seq, v: i32) -> bool {\n exists |i: int| 0 <= i < s.len() && s[i] == v\n}\n\npub open spec fn consec_diff(s: Seq, k: int) -> int\n recommends 0 <= k < s.len() - 1,\n{\n s[k + 1] - s[k]\n}\n\npub open spec fn has_pair(res: Seq>, a: i32, b: i32) -> bool {\n exists |k: int| 0 <= k < res.len() && res[k][0] == a && res[k][1] == b\n}\n\nimpl Solution {\n pub fn minimum_abs_difference(arr: Vec) -> (res: Vec>)\n requires\n 2 <= arr.len() <= 100_000,\n forall |i: int| 0 <= i < arr.len() ==> -1_000_000 <= #[trigger] arr[i] <= 1_000_000,\n forall |i: int, j: int| 0 <= i < j < arr.len() ==> arr[i] != arr[j],\n ensures\n res.len() >= 1,\n forall |i: int| 0 <= i < res.len() ==> (#[trigger] res[i]).len() == 2,\n forall |i: int| 0 <= i < res.len() ==> (#[trigger] res[i])[0] < res[i][1],\n forall |i: int| 0 <= i < res.len() ==>\n #[trigger] seq_contains(arr@, res[i][0]),\n forall |i: int| 0 <= i < res.len() ==>\n #[trigger] seq_contains(arr@, res[i][1]),\n forall |i: int| 0 <= i < res.len() ==>\n ((#[trigger] res[i])[1] - res[i][0]) == (res[0][1] - res[0][0]),\n forall |p: int, q: int| 0 <= p < q < arr.len() ==>\n #[trigger] abs_diff(arr[p] as int, arr[q] as int) >= (res[0][1] - res[0][0]) as int,\n forall |i: int, j: int| 0 <= i < j < res.len() ==>\n (#[trigger] res[i])[0] < (#[trigger] res[j])[0],\n forall |p: int, q: int| 0 <= p < arr.len() && 0 <= q < arr.len()\n && arr[p] < arr[q]\n && #[trigger] abs_diff(arr[p] as int, arr[q] as int) == (res[0][1] - res[0][0]) as int\n ==> has_pair(res@, arr[p], arr[q]),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_abs_difference(arr: Vec) -> Vec> {\n let n = arr.len();\n let mut sorted = arr;\n\n let mut i: usize = 1;\n while i < n {\n let key = sorted[i];\n let mut j = i;\n while j > 0 && sorted[j - 1] > key {\n sorted[j] = sorted[j - 1];\n j = j - 1;\n }\n sorted[j] = key;\n i = i + 1;\n }\n\n let mut min_diff: i32 = sorted[1] - sorted[0];\n let mut i: usize = 2;\n while i < n {\n let diff = sorted[i] - sorted[i - 1];\n if diff < min_diff {\n min_diff = diff;\n }\n i = i + 1;\n }\n\n let mut result: Vec> = Vec::new();\n let mut i: usize = 1;\n while i < n {\n if sorted[i] - sorted[i - 1] == min_diff {\n let pair: Vec = vec![sorted[i - 1], sorted[i]];\n result.push(pair);\n }\n i = i + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn seq_contains(s: Seq, v: i32) -> bool {\n exists |i: int| 0 <= i < s.len() && s[i] == v\n}\n\npub open spec fn consec_diff(s: Seq, k: int) -> int\n recommends 0 <= k < s.len() - 1,\n{\n s[k + 1] - s[k]\n}\n\npub open spec fn has_pair(res: Seq>, a: i32, b: i32) -> bool {\n exists |k: int| 0 <= k < res.len() && res[k][0] == a && res[k][1] == b\n}\n\nimpl Solution {\n pub fn minimum_abs_difference(arr: Vec) -> (res: Vec>)\n requires\n 2 <= arr.len() <= 100_000,\n forall |i: int| 0 <= i < arr.len() ==> -1_000_000 <= #[trigger] arr[i] <= 1_000_000,\n forall |i: int, j: int| 0 <= i < j < arr.len() ==> arr[i] != arr[j],\n ensures\n res.len() >= 1,\n forall |i: int| 0 <= i < res.len() ==> (#[trigger] res[i]).len() == 2,\n forall |i: int| 0 <= i < res.len() ==> (#[trigger] res[i])[0] < res[i][1],\n forall |i: int| 0 <= i < res.len() ==>\n #[trigger] seq_contains(arr@, res[i][0]),\n forall |i: int| 0 <= i < res.len() ==>\n #[trigger] seq_contains(arr@, res[i][1]),\n forall |i: int| 0 <= i < res.len() ==>\n ((#[trigger] res[i])[1] - res[i][0]) == (res[0][1] - res[0][0]),\n forall |p: int, q: int| 0 <= p < q < arr.len() ==>\n #[trigger] abs_diff(arr[p] as int, arr[q] as int) >= (res[0][1] - res[0][0]) as int,\n forall |i: int, j: int| 0 <= i < j < res.len() ==>\n (#[trigger] res[i])[0] < (#[trigger] res[j])[0],\n forall |p: int, q: int| 0 <= p < arr.len() && 0 <= q < arr.len()\n && arr[p] < arr[q]\n && #[trigger] abs_diff(arr[p] as int, arr[q] as int) == (res[0][1] - res[0][0]) as int\n ==> has_pair(res@, arr[p], arr[q]),\n {\n let n = arr.len();\n let mut sorted = arr;\n\n let mut i: usize = 1;\n while i < n {\n let key = sorted[i];\n let mut j = i;\n while j > 0 && sorted[j - 1] > key {\n sorted.set(j, sorted[j - 1]);\n j = j - 1;\n }\n sorted.set(j, key);\n i = i + 1;\n }\n\n let mut min_diff: i32 = sorted[1] - sorted[0];\n let mut i: usize = 2;\n while i < n {\n let diff = sorted[i] - sorted[i - 1];\n if diff < min_diff {\n min_diff = diff;\n }\n i = i + 1;\n }\n\n let mut result: Vec> = Vec::new();\n let mut i: usize = 1;\n while i < n {\n if sorted[i] - sorted[i - 1] == min_diff {\n let pair: Vec = vec![sorted[i - 1], sorted[i]];\n result.push(pair);\n }\n i = i + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn seq_contains(s: Seq, v: i32) -> bool {\n exists |i: int| 0 <= i < s.len() && s[i] == v\n}\n\npub open spec fn consec_diff(s: Seq, k: int) -> int\n recommends 0 <= k < s.len() - 1,\n{\n s[k + 1] - s[k]\n}\n\npub open spec fn has_pair(res: Seq>, a: i32, b: i32) -> bool {\n exists |k: int| 0 <= k < res.len() && res[k][0] == a && res[k][1] == b\n}\n\nimpl Solution {\n pub fn minimum_abs_difference(arr: Vec) -> (res: Vec>)\n requires\n 2 <= arr.len() <= 100_000,\n forall |i: int| 0 <= i < arr.len() ==> -1_000_000 <= #[trigger] arr[i] <= 1_000_000,\n forall |i: int, j: int| 0 <= i < j < arr.len() ==> arr[i] != arr[j],\n ensures\n res.len() >= 1,\n forall |i: int| 0 <= i < res.len() ==> (#[trigger] res[i]).len() == 2,\n forall |i: int| 0 <= i < res.len() ==> (#[trigger] res[i])[0] < res[i][1],\n forall |i: int| 0 <= i < res.len() ==>\n #[trigger] seq_contains(arr@, res[i][0]),\n forall |i: int| 0 <= i < res.len() ==>\n #[trigger] seq_contains(arr@, res[i][1]),\n forall |i: int| 0 <= i < res.len() ==>\n ((#[trigger] res[i])[1] - res[i][0]) == (res[0][1] - res[0][0]),\n forall |p: int, q: int| 0 <= p < q < arr.len() ==>\n #[trigger] abs_diff(arr[p] as int, arr[q] as int) >= (res[0][1] - res[0][0]) as int,\n forall |i: int, j: int| 0 <= i < j < res.len() ==>\n (#[trigger] res[i])[0] < (#[trigger] res[j])[0],\n forall |p: int, q: int| 0 <= p < arr.len() && 0 <= q < arr.len()\n && arr[p] < arr[q]\n && #[trigger] abs_diff(arr[p] as int, arr[q] as int) == (res[0][1] - res[0][0]) as int\n ==> has_pair(res@, arr[p], arr[q]),\n {\n let ghost original_arr = arr@;\n let n = arr.len();\n let mut sorted = arr;\n\n let mut i: usize = 1;\n while i < n\n invariant\n n == sorted.len(),\n 2 <= n <= 100_000,\n 1 <= i <= n,\n forall |a: int, b: int| 0 <= a < b < i as int ==> sorted[a] < sorted[b],\n forall |a: int, b: int| 0 <= a < b < n as int ==> sorted[a] != sorted[b],\n forall |k: int| 0 <= k < n as int ==> -1_000_000 <= #[trigger] sorted[k] <= 1_000_000,\n forall |k: int| 0 <= k < n as int ==>\n #[trigger] seq_contains(original_arr, sorted[k]),\n forall |k: int| 0 <= k < n as int ==>\n seq_contains(sorted@, #[trigger] original_arr[k]),\n decreases n - i,\n {\n let key = sorted[i];\n let ghost pre_inner = sorted@;\n\n proof {\n assert(forall |a: int, b: int| 0 <= a < b < i as int ==> pre_inner[a] < pre_inner[b]);\n assert(forall |a: int, b: int| 0 <= a < b < n as int ==> pre_inner[a] != pre_inner[b]);\n }\n\n let mut j = i;\n while j > 0 && sorted[j - 1] > key\n invariant\n n == sorted.len(),\n 2 <= n <= 100_000,\n 0 <= j <= i,\n i < n,\n pre_inner.len() == n as int,\n key == pre_inner[i as int],\n -1_000_000 <= key <= 1_000_000,\n forall |a: int, b: int| 0 <= a < b < i as int ==> pre_inner[a] < pre_inner[b],\n forall |a: int, b: int| 0 <= a < b < n as int ==> pre_inner[a] != pre_inner[b],\n forall |k: int| 0 <= k < j as int ==> sorted[k] == pre_inner[k],\n forall |k: int| k > j as int ==> k <= i as int ==> sorted[k] == pre_inner[k - 1],\n forall |k: int| k > i as int ==> k < n as int ==> sorted[k] == pre_inner[k],\n j < i ==> sorted[j as int + 1] > key,\n forall |k: int| 0 <= k < n as int ==> -1_000_000 <= #[trigger] sorted[k] <= 1_000_000,\n decreases j,\n {\n sorted.set(j, sorted[j - 1]);\n j = j - 1;\n }\n sorted.set(j, key);\n\n proof {\n \n assert forall |a: int, b: int| 0 <= a < b < i as int + 1 implies sorted[a] < sorted[b] by {\n if a < j as int && b < j as int {\n assert(sorted[a] == pre_inner[a]);\n assert(sorted[b] == pre_inner[b]);\n } else if a < j as int && b == j as int {\n assert(sorted[a] == pre_inner[a]);\n assert(sorted[b] == key);\n if j as int > 0 {\n assert(pre_inner[j as int - 1] != pre_inner[i as int]);\n assert(pre_inner[a] <= pre_inner[j as int - 1]);\n assert(pre_inner[j as int - 1] < key);\n }\n } else if a == j as int && b > j as int {\n assert(sorted[a] == key);\n if j < i {\n \n assert(sorted[j as int + 1] > key);\n \n if b > j as int + 1 {\n assert(sorted[j as int + 1] == pre_inner[j as int]);\n assert(sorted[b] == pre_inner[b - 1]);\n assert(pre_inner[j as int] < pre_inner[b - 1]);\n assert(sorted[b] > key);\n }\n }\n } else if a > j as int && b > j as int {\n assert(sorted[a] == pre_inner[a - 1]);\n assert(sorted[b] == pre_inner[b - 1]);\n assert(a - 1 < b - 1);\n assert(pre_inner[a - 1] < pre_inner[b - 1]);\n } else if a < j as int && b > j as int {\n assert(sorted[a] == pre_inner[a]);\n if j as int > 0 {\n assert(pre_inner[j as int - 1] != pre_inner[i as int]);\n assert(pre_inner[a] <= pre_inner[j as int - 1]);\n assert(pre_inner[j as int - 1] < key);\n }\n assert(sorted[a] < key);\n assert(sorted[j as int + 1] > key);\n if b > j as int + 1 {\n assert(sorted[j as int + 1] == pre_inner[j as int]);\n assert(sorted[b] == pre_inner[b - 1]);\n assert(pre_inner[j as int] < pre_inner[b - 1]);\n }\n }\n };\n\n \n assert forall |a: int, b: int| 0 <= a < b < n as int implies sorted[a] != sorted[b] by {\n if a <= i as int && b <= i as int {\n \n if a < i as int + 1 && b < i as int + 1 {\n assert(sorted[a] < sorted[b]);\n } else {\n \n \n \n if a < j as int {\n assert(sorted[a] == pre_inner[a]);\n assert(sorted[b] == pre_inner[b]);\n assert(pre_inner[a] != pre_inner[b]);\n } else if a == j as int {\n assert(sorted[a] == key);\n assert(sorted[b] == pre_inner[b]);\n assert(key == pre_inner[i as int]);\n assert(pre_inner[i as int] != pre_inner[b]);\n } else {\n assert(sorted[a] == pre_inner[a - 1]);\n assert(sorted[b] == pre_inner[b]);\n assert(a - 1 < b);\n assert(pre_inner[a - 1] != pre_inner[b]);\n }\n }\n } else if a <= i as int && b > i as int {\n assert(sorted[b] == pre_inner[b]);\n if a < j as int {\n assert(sorted[a] == pre_inner[a]);\n assert(pre_inner[a] != pre_inner[b]);\n } else if a == j as int {\n assert(sorted[a] == key);\n assert(key == pre_inner[i as int]);\n assert(pre_inner[i as int] != pre_inner[b]);\n } else {\n assert(sorted[a] == pre_inner[a - 1]);\n assert(a - 1 < b);\n assert(pre_inner[a - 1] != pre_inner[b]);\n }\n } else {\n \n assert(sorted[a] == pre_inner[a]);\n assert(sorted[b] == pre_inner[b]);\n assert(pre_inner[a] != pre_inner[b]);\n }\n };\n\n assert forall |k: int| 0 <= k < n as int implies\n #[trigger] seq_contains(original_arr, sorted[k]) by {\n if k < j as int {\n assert(sorted[k] == pre_inner[k]);\n assert(seq_contains(original_arr, pre_inner[k]));\n } else if k == j as int {\n assert(sorted[k] == key);\n assert(key == pre_inner[i as int]);\n assert(seq_contains(original_arr, pre_inner[i as int]));\n } else if k <= i as int {\n assert(sorted[k] == pre_inner[k - 1]);\n assert(seq_contains(original_arr, pre_inner[k - 1]));\n } else {\n assert(sorted[k] == pre_inner[k]);\n assert(seq_contains(original_arr, pre_inner[k]));\n }\n };\n\n assert forall |k: int| 0 <= k < n as int implies\n seq_contains(sorted@, #[trigger] original_arr[k]) by {\n assert(seq_contains(pre_inner, original_arr[k]));\n let w = choose |l: int| 0 <= l < n as int && pre_inner[l] == original_arr[k];\n if w < j as int {\n assert(sorted[w] == pre_inner[w]);\n } else if w == i as int {\n assert(sorted[j as int] == pre_inner[i as int]);\n } else if w >= j as int && w < i as int {\n assert(sorted[w + 1] == pre_inner[w]);\n } else {\n assert(sorted[w] == pre_inner[w]);\n }\n };\n }\n\n i = i + 1;\n }\n\n let mut min_diff: i32 = sorted[1] - sorted[0];\n\n proof {\n assert(min_diff > 0) by {\n assert(sorted[0] < sorted[1]);\n };\n assert(consec_diff(sorted@, 0) == min_diff as int);\n }\n\n let mut i: usize = 2;\n while i < n\n invariant\n 2 <= i <= n,\n n == sorted.len(),\n 2 <= n <= 100_000,\n forall |a: int, b: int| 0 <= a < b < n as int ==> sorted[a] < sorted[b],\n forall |k: int| 0 <= k < n as int ==> -1_000_000 <= #[trigger] sorted[k] <= 1_000_000,\n min_diff > 0,\n forall |k: int| 0 <= k < i as int - 1 ==>\n #[trigger] consec_diff(sorted@, k) >= min_diff as int,\n exists |k: int| 0 <= k < i as int - 1 &&\n consec_diff(sorted@, k) == min_diff as int,\n decreases n - i,\n {\n let diff = sorted[i] - sorted[i - 1];\n proof {\n assert(diff == consec_diff(sorted@, i as int - 1));\n }\n if diff < min_diff {\n min_diff = diff;\n proof {\n assert(consec_diff(sorted@, i as int - 1) == min_diff as int);\n }\n }\n i = i + 1;\n }\n\n proof {\n assert forall |p: int, q: int| 0 <= p < q < n as int implies\n abs_diff(sorted[p] as int, sorted[q] as int) >= min_diff as int by {\n assert(sorted[p] < sorted[q]);\n assert(abs_diff(sorted[p] as int, sorted[q] as int) == sorted[q] as int - sorted[p] as int);\n assert(sorted[q] as int - sorted[p] as int >= sorted[p + 1] as int - sorted[p] as int);\n assert(consec_diff(sorted@, p) >= min_diff as int);\n };\n\n assert forall |p: int, q: int| 0 <= p < q < n as int implies\n #[trigger] abs_diff(original_arr[p] as int, original_arr[q] as int) >= min_diff as int by {\n assert(seq_contains(sorted@, original_arr[p]));\n assert(seq_contains(sorted@, original_arr[q]));\n let lp = choose |l: int| 0 <= l < n as int && sorted@[l] == original_arr[p];\n let lq = choose |l: int| 0 <= l < n as int && sorted@[l] == original_arr[q];\n if lp == lq {\n assert(original_arr[p] == original_arr[q]);\n assert(p != q);\n assert(original_arr[p] != original_arr[q]);\n } else if lp < lq {\n assert(abs_diff(sorted[lp] as int, sorted[lq] as int) >= min_diff as int);\n } else {\n assert(abs_diff(sorted[lq] as int, sorted[lp] as int) >= min_diff as int);\n }\n };\n }\n\n let mut result: Vec> = Vec::new();\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= i <= n,\n n == sorted.len(),\n 2 <= n <= 100_000,\n min_diff > 0,\n forall |a: int, b: int| 0 <= a < b < n as int ==> sorted[a] < sorted[b],\n forall |k: int| 0 <= k < n as int ==> -1_000_000 <= #[trigger] sorted[k] <= 1_000_000,\n forall |k: int| 0 <= k < n as int - 1 ==>\n #[trigger] consec_diff(sorted@, k) >= min_diff as int,\n exists |k: int| 0 <= k < n as int - 1 &&\n consec_diff(sorted@, k) == min_diff as int,\n forall |k: int| 0 <= k < n as int ==>\n #[trigger] seq_contains(original_arr, sorted[k]),\n forall |k: int| 0 <= k < result.len() ==> (#[trigger] result[k]).len() == 2,\n forall |k: int| 0 <= k < result.len() ==> (#[trigger] result[k])[0] < result[k][1],\n forall |k: int| 0 <= k < result.len() ==>\n ((#[trigger] result[k])[1] - result[k][0]) == min_diff,\n forall |k: int| 0 <= k < result.len() ==>\n #[trigger] seq_contains(original_arr, result[k][0]),\n forall |k: int| 0 <= k < result.len() ==>\n #[trigger] seq_contains(original_arr, result[k][1]),\n forall |k: int, l: int| 0 <= k < l < result.len() ==>\n (#[trigger] result[k])[0] < (#[trigger] result[l])[0],\n \n (exists |k: int| 0 <= k < i as int - 1 && consec_diff(sorted@, k) == min_diff as int) ==> result.len() > 0,\n \n result.len() > 0 ==> result[result.len() - 1 as int][0] < sorted[i as int - 1],\n forall |k: int| 0 <= k < n as int ==>\n seq_contains(sorted@, #[trigger] original_arr[k]),\n forall |kk: int| 0 <= kk < i as int - 1 ==>\n consec_diff(sorted@, kk) == min_diff as int ==>\n has_pair(result@, sorted[kk], sorted[kk + 1]),\n decreases n - i,\n {\n if sorted[i] - sorted[i - 1] == min_diff {\n let ghost old_result_len = result.len();\n let ghost old_result_seq = result@;\n\n proof {\n assert(consec_diff(sorted@, i as int - 1) == min_diff as int);\n }\n\n let pair: Vec = vec![sorted[i - 1], sorted[i]];\n result.push(pair);\n\n proof {\n assert(result[old_result_len as int][0] == sorted[i as int - 1]);\n assert(result[old_result_len as int][1] == sorted[i as int]);\n assert(result.len() == old_result_len + 1);\n\n \n assert forall |k: int, l: int| 0 <= k < l < result.len() implies\n (#[trigger] result[k])[0] < (#[trigger] result[l])[0] by {\n if l < old_result_len as int {\n \n } else if k < old_result_len as int {\n \n assert(result[l][0] == sorted[i as int - 1]);\n if old_result_len > 0 {\n \n if k < old_result_len as int - 1 {\n \n assert(result[k][0] < result[old_result_len as int - 1][0]);\n }\n \n assert(result[old_result_len as int - 1][0] < sorted[i as int - 1]);\n }\n }\n };\n\n \n assert forall |k: int| 0 <= k < result.len() implies\n #[trigger] seq_contains(original_arr, result[k][0]) by {\n if k < old_result_len as int {\n } else {\n assert(result[k][0] == sorted[i as int - 1]);\n assert(seq_contains(original_arr, sorted[i as int - 1]));\n }\n };\n\n assert forall |k: int| 0 <= k < result.len() implies\n #[trigger] seq_contains(original_arr, result[k][1]) by {\n if k < old_result_len as int {\n } else {\n assert(result[k][1] == sorted[i as int]);\n assert(seq_contains(original_arr, sorted[i as int]));\n }\n };\n\n \n assert(result[result.len() - 1 as int][0] == sorted[i as int - 1]);\n assert(sorted[i as int - 1] < sorted[i as int]);\n\n \n assert forall |kk: int| 0 <= kk < i as int\n && consec_diff(sorted@, kk) == min_diff as int\n implies has_pair(result@, sorted[kk], sorted[kk + 1]) by {\n if kk == i as int - 1 {\n assert(result@[old_result_len as int][0] == sorted[kk]);\n assert(result@[old_result_len as int][1] == sorted[kk + 1]);\n } else if consec_diff(sorted@, kk) == min_diff as int {\n assert(has_pair(old_result_seq, sorted[kk], sorted[kk + 1]));\n let w = choose |m: int| 0 <= m < old_result_seq.len()\n && old_result_seq[m][0] == sorted[kk]\n && old_result_seq[m][1] == sorted[kk + 1];\n assert(0 <= w < old_result_len as int);\n assert(w < result@.len());\n assert(result@[w] == old_result_seq[w]);\n assert(result@[w][0] == sorted[kk]);\n assert(result@[w][1] == sorted[kk + 1]);\n }\n };\n }\n } else {\n proof {\n assert(consec_diff(sorted@, i as int - 1) != min_diff as int) by {\n assert(sorted[i as int] - sorted[i as int - 1] != min_diff);\n };\n \n if result.len() > 0 {\n assert(result[result.len() - 1 as int][0] < sorted[i as int - 1]);\n assert(sorted[i as int - 1] < sorted[i as int]);\n }\n }\n }\n i = i + 1;\n }\n\n proof {\n \n \n \n let witness_k = choose |k: int| 0 <= k < n as int - 1 && consec_diff(sorted@, k) == min_diff as int;\n assert(0 <= witness_k < n as int - 1);\n assert(witness_k < i as int - 1);\n assert(consec_diff(sorted@, witness_k) == min_diff as int);\n assert(result.len() > 0);\n assert(result.len() >= 1);\n\n assert(result[0][1] - result[0][0] == min_diff);\n\n assert forall |i: int| 0 <= i < result.len() implies\n ((#[trigger] result[i])[1] - result[i][0]) == (result[0][1] - result[0][0]) by {\n assert((result[i][1] - result[i][0]) == min_diff);\n };\n\n assert forall |p: int, q: int| 0 <= p < q < n as int implies\n #[trigger] abs_diff(original_arr[p] as int, original_arr[q] as int) >= (result[0][1] - result[0][0]) as int by {\n assert(result[0][1] - result[0][0] == min_diff);\n };\n\n \n assert forall |p: int, q: int| 0 <= p < n as int && 0 <= q < n as int\n && original_arr[p] < original_arr[q]\n && #[trigger] abs_diff(original_arr[p] as int, original_arr[q] as int) == (result[0][1] - result[0][0]) as int\n implies has_pair(result@, original_arr[p], original_arr[q]) by {\n assert(result[0][1] - result[0][0] == min_diff);\n assert(abs_diff(original_arr[p] as int, original_arr[q] as int) == min_diff as int);\n \n assert((original_arr[p] as int) < (original_arr[q] as int));\n assert((original_arr[q] as int) - (original_arr[p] as int) == min_diff as int);\n\n \n assert(seq_contains(sorted@, original_arr[p]));\n assert(seq_contains(sorted@, original_arr[q]));\n let sp = choose |l: int| 0 <= l < n as int && sorted@[l] == original_arr[p];\n let sq = choose |l: int| 0 <= l < n as int && sorted@[l] == original_arr[q];\n assert(sorted[sp] == original_arr[p]);\n assert(sorted[sq] == original_arr[q]);\n assert(sorted[sp] < sorted[sq]);\n\n \n if sp >= sq {\n if sp > sq {\n assert(sorted[sq] < sorted[sp]); \n }\n assert(false);\n }\n assert(sp < sq);\n assert(sp < n as int - 1);\n\n \n \n assert(consec_diff(sorted@, sp) >= min_diff as int);\n \n \n assert(sorted[sp + 1] as int - sorted[sp] as int >= min_diff as int);\n assert(sorted[sq] as int - sorted[sp] as int == min_diff as int);\n assert(sorted[sp + 1] as int >= sorted[sq] as int);\n\n if sq > sp + 1 {\n \n assert(sorted[sp + 1] < sorted[sq]);\n assert(false);\n }\n assert(sq == sp + 1);\n\n \n assert(consec_diff(sorted@, sp) == min_diff as int);\n assert(has_pair(result@, sorted[sp], sorted[sp + 1]));\n };\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1200/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1200/tests/mutated_testcases.jsonl"} {"problem": "lc1223", "family": "leetcode", "description": "# Dice Roll Simulation\n\nA die simulator generates a random number from `1` to `6` for each roll. You introduced a constraint to the generator such that it cannot roll the number `i` more than `rollMax[i]` (**1-indexed**) consecutive times.\n\nGiven an array of integers `rollMax` and an integer `n`, return the number of distinct sequences that can be obtained with exactly `n` rolls. Since the answer may be too large, return it **modulo** $10^9 + 7$.\n\nTwo sequences are considered different if at least one element differs from each other.\n\n## Example 1:\n\n> **Input:** n = 2, rollMax = [1,1,2,2,2,3]\n> **Output:** 34\n> **Explanation:** There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36 - 2 = 34.\n\n## Example 2:\n\n> **Input:** n = 2, rollMax = [1,1,1,1,1,1]\n> **Output:** 30\n\n## Example 3:\n\n> **Input:** n = 3, rollMax = [1,1,1,2,2,3]\n> **Output:** 181\n\n## Constraints:\n\n- $1 \\leq n \\leq 5000$\n- `rollMax.length == 6`\n- $1 \\leq rollMax[i] \\leq 15$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn die_simulator(n: i32, roll_max: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Dynamic Programming\nAcceptance rate: 50.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n \n \n \n \n pub open spec fn dp_val(i: int, j: int, rm: Seq) -> int\n recommends 0 <= j < 6, rm.len() == 6,\n decreases i, 17int,\n {\n if i <= 0 { 0 }\n else if i == 1 { 1 }\n else {\n Self::dp_acc(i, j, 1, rm)\n }\n }\n\n \n \n pub open spec fn dp_acc(i: int, j: int, k: int, rm: Seq) -> int\n recommends 0 <= j < 6, rm.len() == 6,\n decreases i, 16int - k,\n {\n if k < 1 || k > rm[j] as int || k > i || k > 15 { 0 }\n else {\n let p = i - k;\n (\n (if p <= 0 { 1int } else { 0 })\n + Self::dp_val(p, 0, rm) + Self::dp_val(p, 1, rm)\n + Self::dp_val(p, 2, rm) + Self::dp_val(p, 3, rm)\n + Self::dp_val(p, 4, rm) + Self::dp_val(p, 5, rm)\n - Self::dp_val(p, j, rm)\n ) + Self::dp_acc(i, j, k + 1, rm)\n }\n }\n\n \n pub open spec fn total_val(i: int, rm: Seq) -> int\n recommends rm.len() == 6,\n {\n if i <= 0 { 1 }\n else {\n Self::dp_val(i, 0, rm) + Self::dp_val(i, 1, rm) + Self::dp_val(i, 2, rm)\n + Self::dp_val(i, 3, rm) + Self::dp_val(i, 4, rm) + Self::dp_val(i, 5, rm)\n }\n }\n\n pub fn die_simulator(n: i32, roll_max: Vec) -> (result: i32)\n requires\n 1 <= n <= 5000,\n roll_max.len() == 6,\n forall |j: int| 0 <= j < 6 ==> 1 <= #[trigger] roll_max[j] <= 15,\n ensures\n 0 <= result < 1_000_000_007,\n result as int == Solution::total_val(n as int, roll_max@) % 1_000_000_007,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn die_simulator(n: i32, roll_max: Vec) -> i32 {\n let modp: i64 = 1_000_000_007;\n let n_us = n as usize;\n let dp_size = (n_us + 1) * 6;\n let mut dp: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < dp_size {\n dp.push(0i64);\n idx = idx + 1;\n }\n let mut total: Vec = Vec::new();\n idx = 0;\n while idx <= n_us {\n total.push(0i64);\n idx = idx + 1;\n }\n total[0] = 1;\n let mut j = 0usize;\n while j < 6 {\n dp[6 + j] = 1;\n j = j + 1;\n }\n total[1] = 6;\n let mut i = 2usize;\n while i <= n_us {\n let mut j = 0usize;\n while j < 6 {\n let rm_j = roll_max[j] as usize;\n let bound = if rm_j < i { rm_j } else { i };\n let mut val: i64 = 0;\n let mut k = 1usize;\n while k <= bound {\n let prev = i - k;\n let diff = (total[prev] - dp[prev * 6 + j] + modp) % modp;\n val = (val + diff) % modp;\n k = k + 1;\n }\n dp[i * 6 + j] = val;\n j = j + 1;\n }\n let mut t: i64 = 0;\n let mut j2 = 0usize;\n while j2 < 6 {\n t = (t + dp[i * 6 + j2]) % modp;\n j2 = j2 + 1;\n }\n total[i] = t;\n i = i + 1;\n }\n (total[n_us] % modp) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n \n \n \n \n pub open spec fn dp_val(i: int, j: int, rm: Seq) -> int\n recommends 0 <= j < 6, rm.len() == 6,\n decreases i, 17int,\n {\n if i <= 0 { 0 }\n else if i == 1 { 1 }\n else {\n Self::dp_acc(i, j, 1, rm)\n }\n }\n\n \n \n pub open spec fn dp_acc(i: int, j: int, k: int, rm: Seq) -> int\n recommends 0 <= j < 6, rm.len() == 6,\n decreases i, 16int - k,\n {\n if k < 1 || k > rm[j] as int || k > i || k > 15 { 0 }\n else {\n let p = i - k;\n (\n (if p <= 0 { 1int } else { 0 })\n + Self::dp_val(p, 0, rm) + Self::dp_val(p, 1, rm)\n + Self::dp_val(p, 2, rm) + Self::dp_val(p, 3, rm)\n + Self::dp_val(p, 4, rm) + Self::dp_val(p, 5, rm)\n - Self::dp_val(p, j, rm)\n ) + Self::dp_acc(i, j, k + 1, rm)\n }\n }\n\n \n pub open spec fn total_val(i: int, rm: Seq) -> int\n recommends rm.len() == 6,\n {\n if i <= 0 { 1 }\n else {\n Self::dp_val(i, 0, rm) + Self::dp_val(i, 1, rm) + Self::dp_val(i, 2, rm)\n + Self::dp_val(i, 3, rm) + Self::dp_val(i, 4, rm) + Self::dp_val(i, 5, rm)\n }\n }\n\n pub fn die_simulator(n: i32, roll_max: Vec) -> (result: i32)\n requires\n 1 <= n <= 5000,\n roll_max.len() == 6,\n forall |j: int| 0 <= j < 6 ==> 1 <= #[trigger] roll_max[j] <= 15,\n ensures\n 0 <= result < 1_000_000_007,\n result as int == Solution::total_val(n as int, roll_max@) % 1_000_000_007,\n {\n let modp: i64 = 1_000_000_007;\n let n_us = n as usize;\n let dp_size = (n_us + 1) * 6;\n let mut dp: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < dp_size {\n dp.push(0i64);\n idx = idx + 1;\n }\n let mut total: Vec = Vec::new();\n idx = 0;\n while idx <= n_us {\n total.push(0i64);\n idx = idx + 1;\n }\n total.set(0, 1);\n let mut j = 0usize;\n while j < 6 {\n dp.set(6 + j, 1);\n j = j + 1;\n }\n total.set(1, 6);\n let mut i = 2usize;\n while i <= n_us {\n let mut j = 0usize;\n while j < 6 {\n let rm_j = roll_max[j] as usize;\n let bound = if rm_j < i { rm_j } else { i };\n let mut val: i64 = 0;\n let mut k = 1usize;\n while k <= bound {\n let prev = i - k;\n let diff = (total[prev] - dp[prev * 6 + j] + modp) % modp;\n val = (val + diff) % modp;\n k = k + 1;\n }\n dp.set(i * 6 + j, val);\n j = j + 1;\n }\n let mut t: i64 = 0;\n let mut j2 = 0usize;\n while j2 < 6 {\n t = (t + dp[i * 6 + j2]) % modp;\n j2 = j2 + 1;\n }\n total.set(i, t);\n i = i + 1;\n }\n (total[n_us] % modp) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::{lemma_fundamental_div_mod, lemma_mod_multiples_vanish};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n \n \n \n \n pub open spec fn dp_val(i: int, j: int, rm: Seq) -> int\n recommends 0 <= j < 6, rm.len() == 6,\n decreases i, 17int,\n {\n if i <= 0 { 0 }\n else if i == 1 { 1 }\n else {\n Self::dp_acc(i, j, 1, rm)\n }\n }\n\n \n \n pub open spec fn dp_acc(i: int, j: int, k: int, rm: Seq) -> int\n recommends 0 <= j < 6, rm.len() == 6,\n decreases i, 16int - k,\n {\n if k < 1 || k > rm[j] as int || k > i || k > 15 { 0 }\n else {\n let p = i - k;\n (\n (if p <= 0 { 1int } else { 0 })\n + Self::dp_val(p, 0, rm) + Self::dp_val(p, 1, rm)\n + Self::dp_val(p, 2, rm) + Self::dp_val(p, 3, rm)\n + Self::dp_val(p, 4, rm) + Self::dp_val(p, 5, rm)\n - Self::dp_val(p, j, rm)\n ) + Self::dp_acc(i, j, k + 1, rm)\n }\n }\n\n \n pub open spec fn total_val(i: int, rm: Seq) -> int\n recommends rm.len() == 6,\n {\n if i <= 0 { 1 }\n else {\n Self::dp_val(i, 0, rm) + Self::dp_val(i, 1, rm) + Self::dp_val(i, 2, rm)\n + Self::dp_val(i, 3, rm) + Self::dp_val(i, 4, rm) + Self::dp_val(i, 5, rm)\n }\n }\n\n pub open spec fn partial_face_sum(i: int, count: int, rm: Seq) -> int\n decreases count,\n {\n if count <= 0 { 0 }\n else { Self::partial_face_sum(i, count - 1, rm) + Self::dp_val(i, count - 1, rm) }\n }\n\n proof fn lemma_mod_add(a: int, b: int, m: int)\n requires m > 0, a >= 0, b >= 0,\n ensures (a % m + b % m) % m == (a + b) % m,\n {\n lemma_fundamental_div_mod(a, m);\n lemma_fundamental_div_mod(b, m);\n assert(a + b == m * (a / m + b / m) + (a % m + b % m)) by (nonlinear_arith)\n requires a == m * (a / m) + a % m, b == m * (b / m) + b % m;\n lemma_mod_multiples_vanish(a / m + b / m, a % m + b % m, m);\n }\n\n proof fn lemma_mod_sub(a: int, b: int, m: int)\n requires a >= 0, b >= 0, a >= b, m > 0,\n ensures (a % m - b % m + m) % m == (a - b) % m,\n {\n lemma_fundamental_div_mod(a, m);\n lemma_fundamental_div_mod(b, m);\n assert(a % m - b % m + m == (a - b) + m * (1 + b / m - a / m)) by (nonlinear_arith)\n requires a == m * (a / m) + a % m, b == m * (b / m) + b % m;\n lemma_mod_multiples_vanish(1 + b / m - a / m, a - b, m);\n }\n\n #[verifier::spinoff_prover]\n proof fn lemma_dp_val_nonneg(i: int, j: int, rm: Seq)\n requires 0 <= j < 6, rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n ensures Self::dp_val(i, j, rm) >= 0,\n decreases i, 17int,\n {\n if i <= 0 || i == 1 {}\n else { Self::lemma_dp_acc_nonneg(i, j, 1, rm); }\n }\n\n #[verifier::spinoff_prover]\n proof fn lemma_dp_acc_nonneg(i: int, j: int, k: int, rm: Seq)\n requires 0 <= j < 6, rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n k >= 1,\n ensures Self::dp_acc(i, j, k, rm) >= 0,\n decreases i, 16int - k,\n {\n if k < 1 || k > rm[j] as int || k > i || k > 15 {}\n else {\n let p = i - k;\n Self::lemma_dp_val_nonneg(p, 0, rm);\n Self::lemma_dp_val_nonneg(p, 1, rm);\n Self::lemma_dp_val_nonneg(p, 2, rm);\n Self::lemma_dp_val_nonneg(p, 3, rm);\n Self::lemma_dp_val_nonneg(p, 4, rm);\n Self::lemma_dp_val_nonneg(p, 5, rm);\n Self::lemma_dp_acc_nonneg(i, j, k + 1, rm);\n }\n }\n\n proof fn lemma_total_nonneg(i: int, rm: Seq)\n requires rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n ensures Self::total_val(i, rm) >= 0,\n {\n if i <= 0 {} else {\n Self::lemma_dp_val_nonneg(i, 0, rm);\n Self::lemma_dp_val_nonneg(i, 1, rm);\n Self::lemma_dp_val_nonneg(i, 2, rm);\n Self::lemma_dp_val_nonneg(i, 3, rm);\n Self::lemma_dp_val_nonneg(i, 4, rm);\n Self::lemma_dp_val_nonneg(i, 5, rm);\n }\n }\n\n proof fn lemma_total_ge_dp(i: int, j: int, rm: Seq)\n requires 0 <= j < 6, rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n ensures Self::total_val(i, rm) >= Self::dp_val(i, j, rm),\n {\n if i <= 0 {} else {\n Self::lemma_dp_val_nonneg(i, 0, rm);\n Self::lemma_dp_val_nonneg(i, 1, rm);\n Self::lemma_dp_val_nonneg(i, 2, rm);\n Self::lemma_dp_val_nonneg(i, 3, rm);\n Self::lemma_dp_val_nonneg(i, 4, rm);\n Self::lemma_dp_val_nonneg(i, 5, rm);\n }\n }\n\n #[verifier::spinoff_prover]\n proof fn lemma_dp_acc_partial_nonneg(i: int, j: int, from_k: int, to_k: int, rm: Seq)\n requires\n 0 <= j < 6, rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n 1 <= from_k, from_k <= to_k,\n ensures\n Self::dp_acc(i, j, from_k, rm) >= Self::dp_acc(i, j, to_k, rm),\n decreases to_k - from_k,\n {\n if from_k >= to_k {}\n else if from_k > rm[j] as int || from_k > i || from_k > 15 {\n } else {\n let p = i - from_k;\n Self::lemma_dp_val_nonneg(p, 0, rm);\n Self::lemma_dp_val_nonneg(p, 1, rm);\n Self::lemma_dp_val_nonneg(p, 2, rm);\n Self::lemma_dp_val_nonneg(p, 3, rm);\n Self::lemma_dp_val_nonneg(p, 4, rm);\n Self::lemma_dp_val_nonneg(p, 5, rm);\n Self::lemma_dp_acc_partial_nonneg(i, j, from_k + 1, to_k, rm);\n }\n }\n\n proof fn lemma_partial_nonneg(i: int, count: int, rm: Seq)\n requires 0 <= count <= 6, rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n ensures Self::partial_face_sum(i, count, rm) >= 0,\n decreases count,\n {\n if count <= 0 {}\n else {\n Self::lemma_partial_nonneg(i, count - 1, rm);\n Self::lemma_dp_val_nonneg(i, count - 1, rm);\n }\n }\n\n proof fn lemma_partial_eq_total(i: int, rm: Seq)\n requires i > 0, rm.len() == 6,\n ensures Self::partial_face_sum(i, 6, rm) == Self::total_val(i, rm),\n {\n reveal_with_fuel(Solution::partial_face_sum, 7);\n }\n\n proof fn lemma_dp_acc_unfold(i: int, j: int, k: int, rm: Seq)\n requires\n 0 <= j < 6, rm.len() == 6,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n k >= 1, k <= rm[j] as int, k <= i, k <= 15,\n ensures\n Self::dp_acc(i, j, k, rm) - Self::dp_acc(i, j, k + 1, rm)\n == Self::total_val(i - k, rm) - Self::dp_val(i - k, j, rm),\n {\n let p = i - k;\n if p <= 0 {\n assert(Self::dp_val(p, 0, rm) == 0);\n assert(Self::dp_val(p, 1, rm) == 0);\n assert(Self::dp_val(p, 2, rm) == 0);\n assert(Self::dp_val(p, 3, rm) == 0);\n assert(Self::dp_val(p, 4, rm) == 0);\n assert(Self::dp_val(p, 5, rm) == 0);\n assert(Self::total_val(p, rm) == 1);\n }\n }\n\n proof fn lemma_index_unique6(i1: int, j1: int, i2: int, j2: int)\n requires\n 0 <= j1 < 6, 0 <= j2 < 6,\n i1 * 6 + j1 == i2 * 6 + j2,\n ensures\n i1 == i2, j1 == j2,\n {\n assert(i1 == i2 && j1 == j2) by (nonlinear_arith)\n requires 0 <= j1 < 6, 0 <= j2 < 6, i1 * 6 + j1 == i2 * 6 + j2;\n }\n\n #[verifier::spinoff_prover]\n pub fn die_simulator(n: i32, roll_max: Vec) -> (result: i32)\n requires\n 1 <= n <= 5000,\n roll_max.len() == 6,\n forall |j: int| 0 <= j < 6 ==> 1 <= #[trigger] roll_max[j] <= 15,\n ensures\n 0 <= result < 1_000_000_007,\n result as int == Solution::total_val(n as int, roll_max@) % 1_000_000_007,\n {\n let modp: i64 = 1_000_000_007;\n let n_us = n as usize;\n let dp_size = (n_us + 1) * 6;\n let ghost rm = roll_max@;\n let ghost M: int = 1_000_000_007int;\n let mut dp: Vec = Vec::new();\n let mut idx = 0usize;\n while idx < dp_size\n invariant\n dp@.len() == idx as int,\n idx <= dp_size,\n dp_size == (n_us + 1) * 6,\n n_us == n as usize,\n 1 <= n <= 5000,\n forall |k: int| 0 <= k < idx as int ==> dp@[k] == 0i64,\n decreases dp_size - idx\n {\n dp.push(0i64);\n idx = idx + 1;\n }\n let mut total: Vec = Vec::new();\n idx = 0;\n while idx <= n_us\n invariant\n total@.len() == idx as int,\n idx <= n_us + 1,\n n_us == n as usize,\n 1 <= n <= 5000,\n forall |k: int| 0 <= k < idx as int ==> total@[k] == 0i64,\n decreases n_us + 1 - idx\n {\n total.push(0i64);\n idx = idx + 1;\n }\n total.set(0, 1);\n let mut j = 0usize;\n while j < 6\n invariant\n 0 <= j <= 6,\n dp@.len() == dp_size as int,\n dp_size == (n_us + 1) * 6,\n n_us >= 1,\n n_us == n as usize,\n 1 <= n <= 5000,\n total@.len() == n_us as int + 1,\n forall |jj: int| 0 <= jj < j as int ==> (#[trigger] dp@[6 + jj]) == 1i64,\n forall |k: int| 0 <= k < 6 ==> dp@[k] == 0i64,\n forall |jj: int| j as int <= jj < 6 ==> (#[trigger] dp@[6 + jj]) == 0i64,\n forall |k: int| 12 <= k < dp@.len() ==> dp@[k] == 0i64,\n total@[0] == 1i64,\n forall |k: int| 1 <= k < total@.len() ==> total@[k] == 0i64,\n decreases 6 - j\n {\n dp.set(6 + j, 1);\n j = j + 1;\n }\n total.set(1, 6);\n proof {\n assert forall |ii: int, jj: int| 0 <= ii < 2 && 0 <= jj < 6\n implies (#[trigger] dp@[ii * 6 + jj]) as int\n == Self::dp_val(ii, jj, rm) % M\n && 0 <= dp@[ii * 6 + jj]\n && dp@[ii * 6 + jj] < M\n by {\n assert(0 <= ii * 6 + jj && ii * 6 + jj < 12) by (nonlinear_arith)\n requires 0 <= ii < 2, 0 <= jj < 6;\n if ii == 0 {\n assert(dp@[jj] == 0i64);\n assert(Self::dp_val(0, jj, rm) == 0);\n } else {\n assert(dp@[6 + jj] == 1i64);\n assert(Self::dp_val(1, jj, rm) == 1);\n }\n };\n assert forall |ii: int| 0 <= ii < 2\n implies (#[trigger] total@[ii]) as int\n == Self::total_val(ii, rm) % M\n && 0 <= total@[ii]\n && total@[ii] < M\n by {\n if ii == 0 {\n assert(total@[0] == 1i64);\n assert(Self::total_val(0, rm) == 1);\n } else {\n assert(total@[1] == 6i64);\n assert(Self::dp_val(1, 0, rm) == 1);\n assert(Self::dp_val(1, 1, rm) == 1);\n assert(Self::dp_val(1, 2, rm) == 1);\n assert(Self::dp_val(1, 3, rm) == 1);\n assert(Self::dp_val(1, 4, rm) == 1);\n assert(Self::dp_val(1, 5, rm) == 1);\n }\n };\n }\n let mut i = 2usize;\n while i <= n_us\n invariant\n 2 <= i <= n_us + 1,\n n_us == n as usize,\n 1 <= n <= 5000,\n dp@.len() == dp_size as int,\n dp_size == (n_us + 1) * 6,\n total@.len() == n_us as int + 1,\n roll_max@.len() == 6,\n rm == roll_max@,\n M == 1_000_000_007int,\n modp == 1_000_000_007i64,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n forall |ii: int, jj: int| 0 <= ii < i as int && 0 <= jj < 6 ==>\n (#[trigger] dp@[ii * 6 + jj]) as int\n == Self::dp_val(ii, jj, rm) % M\n && 0 <= dp@[ii * 6 + jj]\n && dp@[ii * 6 + jj] < M,\n forall |ii: int| 0 <= ii < i as int ==>\n (#[trigger] total@[ii]) as int\n == Self::total_val(ii, rm) % M\n && 0 <= total@[ii]\n && total@[ii] < M,\n decreases n_us + 1 - i\n {\n let mut j = 0usize;\n while j < 6\n invariant\n 0 <= j <= 6,\n 2 <= i <= n_us,\n n_us == n as usize,\n 1 <= n <= 5000,\n dp@.len() == dp_size as int,\n dp_size == (n_us + 1) * 6,\n total@.len() == n_us as int + 1,\n roll_max@.len() == 6,\n rm == roll_max@,\n M == 1_000_000_007int,\n modp == 1_000_000_007i64,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n forall |ii: int, jj: int| 0 <= ii < i as int && 0 <= jj < 6 ==>\n (#[trigger] dp@[ii * 6 + jj]) as int\n == Self::dp_val(ii, jj, rm) % M\n && 0 <= dp@[ii * 6 + jj]\n && dp@[ii * 6 + jj] < M,\n forall |jj: int| 0 <= jj < j as int ==>\n (#[trigger] dp@[i as int * 6 + jj]) as int\n == Self::dp_val(i as int, jj, rm) % M\n && 0 <= dp@[i as int * 6 + jj]\n && dp@[i as int * 6 + jj] < M,\n forall |ii: int| 0 <= ii < i as int ==>\n (#[trigger] total@[ii]) as int\n == Self::total_val(ii, rm) % M\n && 0 <= total@[ii]\n && total@[ii] < M,\n decreases 6 - j\n {\n let rm_j = roll_max[j] as usize;\n let bound = if rm_j < i { rm_j } else { i };\n let mut val: i64 = 0;\n let mut k = 1usize;\n while k <= bound\n invariant\n 1 <= k <= bound + 1,\n bound == (if rm_j < i { rm_j } else { i }),\n rm_j == roll_max@[j as int] as usize,\n 0 <= j < 6,\n 2 <= i <= n_us,\n n_us == n as usize,\n 1 <= n <= 5000,\n dp@.len() == dp_size as int,\n dp_size == (n_us + 1) * 6,\n total@.len() == n_us as int + 1,\n roll_max@.len() == 6,\n rm == roll_max@,\n M == 1_000_000_007int,\n modp == 1_000_000_007i64,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n forall |ii: int, jj: int| 0 <= ii < i as int && 0 <= jj < 6 ==>\n (#[trigger] dp@[ii * 6 + jj]) as int\n == Self::dp_val(ii, jj, rm) % M\n && 0 <= dp@[ii * 6 + jj]\n && dp@[ii * 6 + jj] < M,\n forall |ii: int| 0 <= ii < i as int ==>\n (#[trigger] total@[ii]) as int\n == Self::total_val(ii, rm) % M\n && 0 <= total@[ii]\n && total@[ii] < M,\n 0 <= val < 1_000_000_007i64,\n val as int == (Self::dp_acc(i as int, j as int, 1, rm)\n - Self::dp_acc(i as int, j as int, k as int, rm)) % M,\n decreases bound + 1 - k\n {\n let prev = i - k;\n proof {\n let ii = i as int;\n let jj = j as int;\n let kk = k as int;\n let pp = prev as int;\n assert(pp == ii - kk);\n assert(0 <= pp && pp < ii);\n let dplen = dp@.len() as int;\n let nus = n_us as int;\n assert(0 <= pp * 6 + jj && pp * 6 + jj < dplen)\n by (nonlinear_arith)\n requires 0 <= pp, pp < ii, ii <= nus,\n 0 <= jj < 6, dplen == (nus + 1) * 6;\n let tv = Self::total_val(pp, rm);\n let dv = Self::dp_val(pp, jj, rm);\n Self::lemma_total_nonneg(pp, rm);\n Self::lemma_total_ge_dp(pp, jj, rm);\n Self::lemma_dp_val_nonneg(pp, jj, rm);\n assert(total@[pp] as int == tv % M);\n assert(dp@[pp * 6 + jj] as int == dv % M);\n Self::lemma_mod_sub(tv, dv, M);\n let old_diff = Self::dp_acc(ii, jj, 1, rm)\n - Self::dp_acc(ii, jj, kk, rm);\n Self::lemma_dp_acc_partial_nonneg(ii, jj, 1, kk, rm);\n assert(old_diff >= 0);\n assert(kk >= 1 && kk <= rm[jj] as int && kk <= ii && kk <= 15) by {\n assert(kk <= bound as int);\n if rm_j < i {\n assert(bound == rm_j);\n assert(kk <= rm_j as int);\n assert(rm_j == rm[jj] as usize);\n } else {\n assert(bound == i);\n assert(kk <= ii);\n }\n assert(rm[jj] <= 15);\n };\n Self::lemma_dp_acc_unfold(ii, jj, kk, rm);\n let step_val = tv - dv;\n assert(step_val >= 0);\n Self::lemma_mod_add(old_diff, step_val, M);\n assert(old_diff + step_val\n == Self::dp_acc(ii, jj, 1, rm)\n - Self::dp_acc(ii, jj, kk + 1, rm));\n }\n let diff = (total[prev] - dp[prev * 6 + j] + modp) % modp;\n val = (val + diff) % modp;\n k = k + 1;\n }\n proof {\n let ii = i as int;\n let jj = j as int;\n assert(Self::dp_acc(ii, jj, k as int, rm) == 0int);\n assert(val as int == Self::dp_acc(ii, jj, 1, rm) % M);\n assert(Self::dp_val(ii, jj, rm) == Self::dp_acc(ii, jj, 1, rm));\n assert(val as int == Self::dp_val(ii, jj, rm) % M);\n let dplen = dp@.len() as int;\n let nus = n_us as int;\n assert(0 <= ii * 6 + jj && ii * 6 + jj < dplen)\n by (nonlinear_arith)\n requires 0 <= ii, ii <= nus, 0 <= jj < 6,\n dplen == (nus + 1) * 6;\n }\n dp.set(i * 6 + j, val);\n proof {\n let ii = i as int;\n let jj = j as int;\n assert forall |i2: int, j2: int|\n 0 <= i2 < ii && 0 <= j2 < 6\n implies (#[trigger] dp@[i2 * 6 + j2]) as int\n == Self::dp_val(i2, j2, rm) % M\n && 0 <= dp@[i2 * 6 + j2]\n && dp@[i2 * 6 + j2] < M\n by {\n if i2 * 6 + j2 == ii * 6 + jj {\n Self::lemma_index_unique6(i2, j2, ii, jj);\n }\n };\n assert forall |j2: int| 0 <= j2 <= jj\n implies (#[trigger] dp@[ii * 6 + j2]) as int\n == Self::dp_val(ii, j2, rm) % M\n && 0 <= dp@[ii * 6 + j2]\n && dp@[ii * 6 + j2] < M\n by {\n if ii * 6 + j2 == ii * 6 + jj {\n assert(j2 == jj);\n }\n };\n }\n j = j + 1;\n }\n let mut t: i64 = 0;\n let mut j2 = 0usize;\n while j2 < 6\n invariant\n 0 <= j2 <= 6,\n 2 <= i <= n_us,\n n_us == n as usize,\n 1 <= n <= 5000,\n dp@.len() == dp_size as int,\n dp_size == (n_us + 1) * 6,\n total@.len() == n_us as int + 1,\n roll_max@.len() == 6,\n rm == roll_max@,\n M == 1_000_000_007int,\n modp == 1_000_000_007i64,\n forall |l: int| 0 <= l < 6 ==> 1 <= #[trigger] rm[l] <= 15,\n forall |jj: int| 0 <= jj < 6 ==>\n (#[trigger] dp@[i as int * 6 + jj]) as int\n == Self::dp_val(i as int, jj, rm) % M\n && 0 <= dp@[i as int * 6 + jj]\n && dp@[i as int * 6 + jj] < M,\n forall |ii: int, jj: int| 0 <= ii < i as int && 0 <= jj < 6 ==>\n (#[trigger] dp@[ii * 6 + jj]) as int\n == Self::dp_val(ii, jj, rm) % M\n && 0 <= dp@[ii * 6 + jj]\n && dp@[ii * 6 + jj] < M,\n forall |ii: int| 0 <= ii < i as int ==>\n (#[trigger] total@[ii]) as int\n == Self::total_val(ii, rm) % M\n && 0 <= total@[ii]\n && total@[ii] < M,\n 0 <= t < 1_000_000_007i64,\n t as int == Self::partial_face_sum(i as int, j2 as int, rm) % M,\n decreases 6 - j2\n {\n proof {\n let pfs = Self::partial_face_sum(i as int, j2 as int, rm);\n let dpv = Self::dp_val(i as int, j2 as int, rm);\n Self::lemma_partial_nonneg(i as int, j2 as int, rm);\n Self::lemma_dp_val_nonneg(i as int, j2 as int, rm);\n Self::lemma_mod_add(pfs, dpv, M);\n let ci = i as int;\n let cj2 = j2 as int;\n let dplen = dp@.len() as int;\n let nus = n_us as int;\n assert(0 <= ci * 6 + cj2 && ci * 6 + cj2 < dplen)\n by (nonlinear_arith)\n requires 0 <= ci, ci <= nus,\n 0 <= cj2 < 6,\n dplen == (nus + 1) * 6;\n }\n t = (t + dp[i * 6 + j2]) % modp;\n j2 = j2 + 1;\n }\n proof {\n Self::lemma_partial_eq_total(i as int, rm);\n assert(t as int == Self::total_val(i as int, rm) % M);\n }\n total.set(i, t);\n proof {\n let ii = i as int;\n assert forall |i2: int, j2: int|\n 0 <= i2 <= ii && 0 <= j2 < 6\n implies (#[trigger] dp@[i2 * 6 + j2]) as int\n == Self::dp_val(i2, j2, rm) % M\n && 0 <= dp@[i2 * 6 + j2]\n && dp@[i2 * 6 + j2] < M\n by {};\n assert forall |i2: int| 0 <= i2 <= ii\n implies (#[trigger] total@[i2]) as int\n == Self::total_val(i2, rm) % M\n && 0 <= total@[i2]\n && total@[i2] < M\n by {\n if i2 < ii {\n } else {\n assert(total@[ii] == t);\n }\n };\n }\n i = i + 1;\n }\n (total[n_us] % modp) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1223/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1223/tests/mutated_testcases.jsonl"} {"problem": "lc1260", "family": "leetcode", "description": "# Shift 2D Grid\n\nGiven a 2D `grid` of size `m x n` and an integer `k`. You need to shift the `grid` `k` times.\n\nIn one shift operation:\n\n- Element at `grid[i][j]` moves to `grid[i][j + 1]`.\n- Element at `grid[i][n - 1]` moves to `grid[i + 1][0]`.\n- Element at `grid[m - 1][n - 1]` moves to `grid[0][0]`.\n\nReturn the 2D grid after applying shift operation `k` times.\n\n## Example 1:\n\n> **Input:** grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1\n> **Output:** [[9,1,2],[3,4,5],[6,7,8]]\n\n## Example 2:\n\n> **Input:** grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4\n> **Output:** [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]\n\n## Example 3:\n\n> **Input:** grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9\n> **Output:** [[1,2,3],[4,5,6],[7,8,9]]\n\n## Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- $1 <= m <= 50$\n- $1 <= n <= 50$\n- $-1000 <= grid[i][j] <= 1000$\n- $0 <= k <= 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn shift_grid(grid: Vec>, k: i32) -> Vec> {\n \n }\n}\n```\n", "tags": "Easy\nArray, Matrix, Simulation\nAcceptance rate: 68.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn shifted_element(grid: Seq>, n: int, k: int, i: int, j: int) -> i32 {\n let total = (grid.len() as int) * n;\n let k_eff = k % total;\n let src = (i * n + j + total - k_eff) % total;\n grid[src / n][src % n]\n }\n\n pub fn shift_grid(grid: Vec>, k: i32) -> (result: Vec>)\n requires\n 1 <= grid.deep_view().len() <= 50,\n forall|i: int|\n 0 <= i < grid.deep_view().len() ==> 1 <= (#[trigger] grid.deep_view()[i]).len()\n <= 50,\n forall|i: int|\n 0 <= i < grid.deep_view().len() ==> (#[trigger] grid.deep_view()[i]).len()\n == grid.deep_view()[0].len(),\n forall|i: int, j: int|\n 0 <= i < grid.deep_view().len() && 0 <= j < grid.deep_view()[i].len() ==> -1000\n <= #[trigger] grid.deep_view()[i][j] <= 1000,\n 0 <= k <= 100,\n ensures\n result@.len() == grid.deep_view().len(),\n forall|i: int|\n 0 <= i < result@.len() ==> (#[trigger] result@[i])@.len()\n == grid.deep_view()[0].len(),\n forall|i: int, j: int|\n 0 <= i < result@.len() && 0 <= j < result@[i]@.len() ==> (#[trigger] result@[i]@[j])\n == Self::shifted_element(\n grid.deep_view(),\n grid.deep_view()[0].len() as int,\n k as int,\n i,\n j,\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn shift_grid(grid: Vec>, k: i32) -> Vec> {\n let m: usize = grid.len();\n let n: usize = grid[0].len();\n let total: usize = m * n;\n let k_eff: usize = (k as usize) % total;\n let mut result: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let linear: usize = i * n + j;\n let src: usize = (linear + total - k_eff) % total;\n let src_row: usize = src / n;\n let src_col: usize = src % n;\n row.push(grid[src_row][src_col]);\n j = j + 1;\n }\n result.push(row);\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn shifted_element(grid: Seq>, n: int, k: int, i: int, j: int) -> i32 {\n let total = (grid.len() as int) * n;\n let k_eff = k % total;\n let src = (i * n + j + total - k_eff) % total;\n grid[src / n][src % n]\n }\n\n pub fn shift_grid(grid: Vec>, k: i32) -> (result: Vec>)\n requires\n 1 <= grid.deep_view().len() <= 50,\n forall|i: int|\n 0 <= i < grid.deep_view().len() ==> 1 <= (#[trigger] grid.deep_view()[i]).len()\n <= 50,\n forall|i: int|\n 0 <= i < grid.deep_view().len() ==> (#[trigger] grid.deep_view()[i]).len()\n == grid.deep_view()[0].len(),\n forall|i: int, j: int|\n 0 <= i < grid.deep_view().len() && 0 <= j < grid.deep_view()[i].len() ==> -1000\n <= #[trigger] grid.deep_view()[i][j] <= 1000,\n 0 <= k <= 100,\n ensures\n result@.len() == grid.deep_view().len(),\n forall|i: int|\n 0 <= i < result@.len() ==> (#[trigger] result@[i])@.len()\n == grid.deep_view()[0].len(),\n forall|i: int, j: int|\n 0 <= i < result@.len() && 0 <= j < result@[i]@.len() ==> (#[trigger] result@[i]@[j])\n == Self::shifted_element(\n grid.deep_view(),\n grid.deep_view()[0].len() as int,\n k as int,\n i,\n j,\n ),\n {\n let m: usize = grid.len();\n let n: usize = grid[0].len();\n let total: usize = m * n;\n let k_eff: usize = (k as usize) % total;\n let mut result: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let linear: usize = i * n + j;\n let src: usize = (linear + total - k_eff) % total;\n let src_row: usize = src / n;\n let src_col: usize = src % n;\n row.push(grid[src_row][src_col]);\n j = j + 1;\n }\n result.push(row);\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn shifted_element(grid: Seq>, n: int, k: int, i: int, j: int) -> i32 {\n let total = (grid.len() as int) * n;\n let k_eff = k % total;\n let src = (i * n + j + total - k_eff) % total;\n grid[src / n][src % n]\n }\n\n proof fn lemma_div_less_than(a: int, b: int, c: int)\n requires\n 0 <= a,\n a < b * c,\n c > 0,\n b > 0,\n ensures\n a / c < b,\n {\n vstd::arithmetic::div_mod::lemma_fundamental_div_mod(a, c);\n assert(a / c < b) by (nonlinear_arith)\n requires\n a == c * (a / c) + a % c,\n 0 <= a % c < c,\n a < b * c,\n c > 0,\n b > 0,\n ;\n }\n\n pub fn shift_grid(grid: Vec>, k: i32) -> (result: Vec>)\n requires\n 1 <= grid.deep_view().len() <= 50,\n forall|i: int|\n 0 <= i < grid.deep_view().len() ==> 1 <= (#[trigger] grid.deep_view()[i]).len()\n <= 50,\n forall|i: int|\n 0 <= i < grid.deep_view().len() ==> (#[trigger] grid.deep_view()[i]).len()\n == grid.deep_view()[0].len(),\n forall|i: int, j: int|\n 0 <= i < grid.deep_view().len() && 0 <= j < grid.deep_view()[i].len() ==> -1000\n <= #[trigger] grid.deep_view()[i][j] <= 1000,\n 0 <= k <= 100,\n ensures\n result@.len() == grid.deep_view().len(),\n forall|i: int|\n 0 <= i < result@.len() ==> (#[trigger] result@[i])@.len()\n == grid.deep_view()[0].len(),\n forall|i: int, j: int|\n 0 <= i < result@.len() && 0 <= j < result@[i]@.len() ==> (#[trigger] result@[i]@[j])\n == Self::shifted_element(\n grid.deep_view(),\n grid.deep_view()[0].len() as int,\n k as int,\n i,\n j,\n ),\n {\n let m: usize = grid.len();\n let n: usize = grid[0].len();\n\n proof {\n assert(m as int == grid.deep_view().len() as int);\n assert(n as int == grid.deep_view()[0].len() as int);\n assert(1 <= m && m <= 50);\n assert(1 <= n && n <= 50);\n assert((m as int) * (n as int) <= 2500) by (nonlinear_arith)\n requires\n 1 <= m <= 50,\n 1 <= n <= 50,\n ;\n }\n\n let total: usize = m * n;\n\n proof {\n assert(total >= 1) by (nonlinear_arith)\n requires\n m >= 1,\n n >= 1,\n total == m * n,\n ;\n }\n\n let k_eff: usize = (k as usize) % total;\n\n let ghost grid_seq: Seq> = grid.deep_view();\n\n let mut result: Vec> = Vec::new();\n let mut i: usize = 0;\n\n while i < m\n invariant\n m == grid@.len(),\n grid_seq =~= grid.deep_view(),\n m as int == grid_seq.len(),\n 1 <= m <= 50,\n 1 <= n <= 50,\n total == m * n,\n 1 <= total <= 2500,\n 0 <= k_eff < total,\n k_eff as int == (k as int) % (total as int),\n n as int == grid_seq[0].len(),\n forall|ii: int|\n 0 <= ii < grid_seq.len() ==> (#[trigger] grid_seq[ii]).len() == n as int,\n 0 <= i <= m,\n result@.len() == i as int,\n forall|ii: int|\n 0 <= ii < i as int ==> (#[trigger] result@[ii])@.len() == n as int,\n forall|ii: int, jj: int|\n 0 <= ii < i as int && 0 <= jj < n as int ==> (#[trigger] result@[ii]@[jj])\n == Self::shifted_element(grid_seq, n as int, k as int, ii, jj),\n decreases m - i,\n {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n\n while j < n\n invariant\n m == grid@.len(),\n grid_seq =~= grid.deep_view(),\n m as int == grid_seq.len(),\n 1 <= m <= 50,\n 1 <= n <= 50,\n total == m * n,\n 1 <= total <= 2500,\n 0 <= k_eff < total,\n k_eff as int == (k as int) % (total as int),\n n as int == grid_seq[0].len(),\n forall|ii: int|\n 0 <= ii < grid_seq.len() ==> (#[trigger] grid_seq[ii]).len() == n as int,\n 0 <= i < m,\n 0 <= j <= n,\n row@.len() == j as int,\n forall|jj: int|\n 0 <= jj < j as int ==> (#[trigger] row@[jj]) == Self::shifted_element(\n grid_seq,\n n as int,\n k as int,\n i as int,\n jj,\n ),\n decreases n - j,\n {\n proof {\n assert((i as int) * (n as int) + (j as int) < (m as int) * (n as int))\n by (nonlinear_arith)\n requires\n i < m,\n j < n,\n n > 0,\n m > 0,\n ;\n }\n\n let linear: usize = i * n + j;\n\n proof {\n assert(linear < total);\n assert(total >= k_eff);\n assert((linear as int) + (total as int) < 5000) by (nonlinear_arith)\n requires\n linear < total,\n total <= 2500,\n ;\n }\n\n let src: usize = (linear + total - k_eff) % total;\n let src_row: usize = src / n;\n let src_col: usize = src % n;\n\n proof {\n assert(src < total);\n Self::lemma_div_less_than(src as int, m as int, n as int);\n assert(src_row < m);\n assert(src_col < n);\n assert(grid.deep_view()[src_row as int] =~= grid@[src_row as int]@);\n }\n\n row.push(grid[src_row][src_col]);\n\n proof {\n assert(row@[j as int] == grid@[src_row as int]@[src_col as int]);\n assert(grid_seq[src_row as int][src_col as int] == grid@[src_row as int]@[\n src_col as int]);\n assert(row@[j as int] == Self::shifted_element(\n grid_seq,\n n as int,\n k as int,\n i as int,\n j as int,\n ));\n }\n\n j = j + 1;\n }\n\n let ghost row_view: Seq = row@;\n result.push(row);\n\n proof {\n assert(result@[i as int]@ =~= row_view);\n assert(result@[i as int]@.len() == n as int);\n\n assert forall|ii: int|\n 0 <= ii < (i + 1) as int implies (#[trigger] result@[ii])@.len() == n as int by {\n if ii < i as int {\n } else {\n assert(result@[ii]@ =~= row_view);\n }\n };\n\n assert forall|ii: int, jj: int|\n 0 <= ii < (i + 1) as int && 0 <= jj < n as int implies (\n #[trigger] result@[ii]@[jj]) == Self::shifted_element(\n grid_seq,\n n as int,\n k as int,\n ii,\n jj,\n ) by {\n if ii < i as int {\n } else {\n assert(result@[ii]@ =~= row_view);\n assert(result@[ii]@[jj] == row_view[jj]);\n }\n };\n }\n\n i = i + 1;\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1260/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1260/tests/mutated_testcases.jsonl"} {"problem": "lc1262", "family": "leetcode", "description": "# Greatest Sum Divisible by Three\n\nGiven an integer array `nums`, return the **maximum possible sum** of elements of the array such that it is divisible by three.\n\n## Example 1:\n\n> **Input:** nums = [3,6,5,1,8]\n> **Output:** 18\n> **Explanation:** Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3).\n\n## Example 2:\n\n> **Input:** nums = [4]\n> **Output:** 0\n> **Explanation:** Since 4 is not divisible by 3, do not pick any number.\n\n## Example 3:\n\n> **Input:** nums = [1,2,3,4,4]\n> **Output:** 12\n> **Explanation:** Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).\n\n## Constraints:\n\n- `1 <= nums.length <= 4 * 10^4`\n- `1 <= nums[i] <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_sum_div_three(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming, Greedy, Sorting\nAcceptance rate: 57.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn subset_sum(nums: Seq, sel: Seq, hi: int) -> int\n decreases hi\n{\n if hi <= 0 { 0 }\n else {\n (if sel[hi - 1] { nums[hi - 1] as int } else { 0 })\n + subset_sum(nums, sel, hi - 1)\n }\n}\n\nimpl Solution {\n pub fn max_sum_div_three(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 40000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10000,\n ensures\n result >= 0,\n result as int % 3 == 0,\n exists |sel: Seq| sel.len() == nums.len() as int\n && subset_sum(nums@, sel, nums.len() as int) == result as int,\n forall |sel: Seq| sel.len() == nums.len() as int\n && subset_sum(nums@, sel, nums.len() as int) % 3 == 0\n ==> subset_sum(nums@, sel, nums.len() as int) <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_sum_div_three(nums: Vec) -> i32 {\n let n = nums.len();\n let mut dp0: i32 = 0;\n let mut dp1: i32 = 0;\n let mut dp2: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let a = nums[i];\n let old0 = dp0;\n let old1 = dp1;\n let old2 = dp2;\n let r = a % 3;\n if r == 0 {\n dp0 = old0 + a;\n dp1 = if old1 > 0 { old1 + a } else { old1 };\n dp2 = if old2 > 0 { old2 + a } else { old2 };\n } else if r == 1 {\n let new_dp0 = if old2 > 0 && old2 + a > old0 { old2 + a } else { old0 };\n let new_dp1 = if old0 + a > old1 { old0 + a } else { old1 };\n let new_dp2 = if old1 > 0 && old1 + a > old2 { old1 + a } else { old2 };\n dp0 = new_dp0;\n dp1 = new_dp1;\n dp2 = new_dp2;\n } else {\n let new_dp0 = if old1 > 0 && old1 + a > old0 { old1 + a } else { old0 };\n let new_dp1 = if old2 > 0 && old2 + a > old1 { old2 + a } else { old1 };\n let new_dp2 = if old0 + a > old2 { old0 + a } else { old2 };\n dp0 = new_dp0;\n dp1 = new_dp1;\n dp2 = new_dp2;\n }\n i += 1;\n }\n dp0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn subset_sum(nums: Seq, sel: Seq, hi: int) -> int\n decreases hi\n{\n if hi <= 0 { 0 }\n else {\n (if sel[hi - 1] { nums[hi - 1] as int } else { 0 })\n + subset_sum(nums, sel, hi - 1)\n }\n}\n\nimpl Solution {\n pub fn max_sum_div_three(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 40000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10000,\n ensures\n result >= 0,\n result as int % 3 == 0,\n exists |sel: Seq| sel.len() == nums.len() as int\n && subset_sum(nums@, sel, nums.len() as int) == result as int,\n forall |sel: Seq| sel.len() == nums.len() as int\n && subset_sum(nums@, sel, nums.len() as int) % 3 == 0\n ==> subset_sum(nums@, sel, nums.len() as int) <= result as int,\n {\n let n = nums.len();\n let mut dp0: i32 = 0;\n let mut dp1: i32 = 0;\n let mut dp2: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let a = nums[i];\n let old0 = dp0;\n let old1 = dp1;\n let old2 = dp2;\n let r = a % 3;\n if r == 0 {\n dp0 = old0 + a;\n dp1 = if old1 > 0 { old1 + a } else { old1 };\n dp2 = if old2 > 0 { old2 + a } else { old2 };\n } else if r == 1 {\n let new_dp0 = if old2 > 0 && old2 + a > old0 { old2 + a } else { old0 };\n let new_dp1 = if old0 + a > old1 { old0 + a } else { old1 };\n let new_dp2 = if old1 > 0 && old1 + a > old2 { old1 + a } else { old2 };\n dp0 = new_dp0;\n dp1 = new_dp1;\n dp2 = new_dp2;\n } else {\n let new_dp0 = if old1 > 0 && old1 + a > old0 { old1 + a } else { old0 };\n let new_dp1 = if old2 > 0 && old2 + a > old1 { old2 + a } else { old1 };\n let new_dp2 = if old0 + a > old2 { old0 + a } else { old2 };\n dp0 = new_dp0;\n dp1 = new_dp1;\n dp2 = new_dp2;\n }\n i += 1;\n }\n dp0\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn subset_sum(nums: Seq, sel: Seq, hi: int) -> int\n decreases hi\n{\n if hi <= 0 { 0 }\n else {\n (if sel[hi - 1] { nums[hi - 1] as int } else { 0 })\n + subset_sum(nums, sel, hi - 1)\n }\n}\n\n\npub open spec fn best(nums: Seq, i: int, r: int) -> int\n recommends 0 <= r < 3, 0 <= i <= nums.len() as int\n decreases i\n{\n if i <= 0 {\n 0\n } else {\n let a = nums[i - 1] as int;\n let ar = a % 3;\n let src = (r - ar + 3) % 3;\n let prev = best(nums, i - 1, r);\n let prev_src = best(nums, i - 1, src);\n let take = prev_src + a;\n if (prev_src > 0 || src == 0) && take > prev {\n take\n } else {\n prev\n }\n }\n}\n\nproof fn lemma_best_nonneg(nums: Seq, i: int, r: int)\n requires\n 0 <= r < 3,\n 0 <= i <= nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10000,\n ensures\n best(nums, i, r) >= 0,\n decreases i\n{\n if i > 0 {\n let a = nums[i - 1] as int;\n let ar = a % 3;\n let src = (r - ar + 3) % 3;\n assert(0 <= src < 3);\n lemma_best_nonneg(nums, i - 1, r);\n lemma_best_nonneg(nums, i - 1, src);\n }\n}\n\nproof fn lemma_best_upper_bound(nums: Seq, i: int, r: int)\n requires\n 0 <= r < 3,\n 0 <= i <= nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10000,\n ensures\n best(nums, i, r) <= 10000 * i,\n decreases i\n{\n if i > 0 {\n let a = nums[i - 1] as int;\n let ar = a % 3;\n let src = (r - ar + 3) % 3;\n assert(0 <= src < 3);\n lemma_best_upper_bound(nums, i - 1, r);\n lemma_best_upper_bound(nums, i - 1, src);\n lemma_best_nonneg(nums, i - 1, r);\n lemma_best_nonneg(nums, i - 1, src);\n }\n}\n\n\nproof fn lemma_best_remainder(nums: Seq, i: int, r: int)\n requires\n 0 <= r < 3,\n 0 <= i <= nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10000,\n ensures\n best(nums, i, r) % 3 == r || (best(nums, i, r) == 0 && r != 0),\n r == 0 ==> best(nums, i, r) % 3 == 0,\n decreases i\n{\n if i > 0 {\n let a = nums[i - 1] as int;\n let ar = a % 3;\n let src = (r - ar + 3) % 3;\n assert(0 <= src < 3);\n lemma_best_nonneg(nums, i - 1, r);\n lemma_best_nonneg(nums, i - 1, src);\n lemma_best_remainder(nums, i - 1, r);\n lemma_best_remainder(nums, i - 1, src);\n let prev = best(nums, i - 1, r);\n let prev_src = best(nums, i - 1, src);\n let take = prev_src + a;\n if (prev_src > 0 || src == 0) && take > prev {\n \n assert(prev_src % 3 == src || (prev_src == 0 && src != 0));\n if src == 0 {\n assert(prev_src % 3 == 0);\n assert(ar == r) by (nonlinear_arith)\n requires src == (r - ar + 3) % 3, src == 0, 0 <= r < 3, 0 <= ar < 3;\n assert((prev_src + a) % 3 == ar) by (nonlinear_arith)\n requires prev_src % 3 == 0, ar == a % 3, a >= 1, prev_src >= 0;\n } else {\n assert(prev_src > 0);\n assert(prev_src % 3 == src);\n assert((prev_src + a) % 3 == (src + ar) % 3) by (nonlinear_arith)\n requires prev_src % 3 == src, ar == a % 3, prev_src >= 1, a >= 1, 0 <= src < 3;\n assert((src + ar) % 3 == r) by (nonlinear_arith)\n requires src == (r - ar + 3) % 3, 0 <= r < 3, 0 <= ar < 3, 0 <= src < 3;\n }\n }\n }\n}\n\n\nproof fn lemma_best_achievable(nums: Seq, i: int, r: int)\n requires\n 0 <= r < 3,\n 0 <= i <= nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10000,\n best(nums, i, r) > 0 || r == 0,\n ensures\n exists |sel: Seq| sel.len() == nums.len()\n && subset_sum(nums, sel, i) == best(nums, i, r)\n && (forall |j: int| i <= j < nums.len() ==> (#[trigger] sel[j]) == false),\n decreases i\n{\n if i <= 0 {\n \n let sel = Seq::new(nums.len(), |_j: int| false);\n assert(sel.len() == nums.len());\n assert forall |j: int| 0 <= j < nums.len() implies (#[trigger] sel[j]) == false by {\n assert(sel[j] == false);\n };\n assert(subset_sum(nums, sel, 0) == 0);\n } else {\n let a = nums[i - 1] as int;\n let ar = a % 3;\n let src = (r - ar + 3) % 3;\n assert(0 <= src < 3);\n lemma_best_nonneg(nums, i - 1, r);\n lemma_best_nonneg(nums, i - 1, src);\n let prev = best(nums, i - 1, r);\n let prev_src = best(nums, i - 1, src);\n let take = prev_src + a;\n if (prev_src > 0 || src == 0) && take > prev {\n \n lemma_best_achievable(nums, i - 1, src);\n let sel_base: Seq = choose |sel: Seq| sel.len() == nums.len()\n && subset_sum(nums, sel, i - 1) == best(nums, i - 1, src)\n && (forall |j: int| i - 1 <= j < nums.len() ==> (#[trigger] sel[j]) == false);\n let sel = sel_base.update(i - 1, true);\n assert(sel.len() == nums.len());\n assert(sel[i - 1] == true);\n \n \n assert forall |j: int| 0 <= j < i - 1 implies sel[j] == sel_base[j] by {};\n lemma_subset_sum_agree(nums, sel, sel_base, i - 1);\n assert(subset_sum(nums, sel, i - 1) == subset_sum(nums, sel_base, i - 1));\n assert(subset_sum(nums, sel, i) == nums[i - 1] as int + subset_sum(nums, sel, i - 1));\n assert(forall |j: int| i <= j < nums.len() ==> (#[trigger] sel[j]) == false);\n } else {\n \n if prev > 0 || r == 0 {\n lemma_best_achievable(nums, i - 1, r);\n let sel: Seq = choose |sel: Seq| sel.len() == nums.len()\n && subset_sum(nums, sel, i - 1) == best(nums, i - 1, r)\n && (forall |j: int| i - 1 <= j < nums.len() ==> (#[trigger] sel[j]) == false);\n assert(sel[i - 1] == false);\n assert(subset_sum(nums, sel, i) == subset_sum(nums, sel, i - 1));\n assert(forall |j: int| i <= j < nums.len() ==> (#[trigger] sel[j]) == false);\n }\n }\n }\n}\n\nproof fn lemma_subset_sum_agree(nums: Seq, a: Seq, b: Seq, hi: int)\n requires\n a.len() == b.len(),\n hi >= 0,\n forall |j: int| 0 <= j < hi ==> a[j] == b[j],\n ensures\n subset_sum(nums, a, hi) == subset_sum(nums, b, hi),\n decreases hi\n{\n if hi > 0 {\n assert(a[hi - 1] == b[hi - 1]);\n lemma_subset_sum_agree(nums, a, b, hi - 1);\n }\n}\n\n\nproof fn lemma_best_optimal(nums: Seq, i: int, r: int, sel: Seq)\n requires\n 0 <= r < 3,\n 0 <= i <= nums.len(),\n sel.len() == nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10000,\n subset_sum(nums, sel, i) % 3 == r,\n forall |j: int| i <= j < nums.len() ==> (#[trigger] sel[j]) == false,\n ensures\n subset_sum(nums, sel, i) <= best(nums, i, r),\n decreases i\n{\n lemma_best_nonneg(nums, i, r);\n lemma_subset_sum_nonneg(nums, sel, i);\n if i <= 0 {\n } else if sel[i - 1] == false {\n \n assert(subset_sum(nums, sel, i) == subset_sum(nums, sel, i - 1));\n assert(forall |j: int| i - 1 <= j < nums.len() ==> (#[trigger] sel[j]) == false);\n lemma_best_nonneg(nums, i - 1, r);\n \n lemma_best_optimal(nums, i - 1, r, sel);\n \n } else {\n \n let a = nums[i - 1] as int;\n let ar = a % 3;\n let src = (r - ar + 3) % 3;\n assert(0 <= src < 3);\n lemma_best_nonneg(nums, i - 1, r);\n lemma_best_nonneg(nums, i - 1, src);\n\n let sub = subset_sum(nums, sel, i - 1);\n assert(subset_sum(nums, sel, i) == a + sub);\n lemma_subset_sum_nonneg(nums, sel, i - 1);\n\n \n assert(sub % 3 == src) by (nonlinear_arith)\n requires (a + sub) % 3 == r, ar == a % 3, src == (r - ar + 3) % 3,\n 0 <= r < 3, 0 <= ar < 3, 0 <= src < 3, a >= 1, sub >= 0;\n\n \n let sel2 = sel.update(i - 1, false);\n assert(forall |j: int| i - 1 <= j < nums.len() ==> (#[trigger] sel2[j]) == false);\n assert forall |j: int| 0 <= j < i - 1 implies sel2[j] == sel[j] by {};\n lemma_subset_sum_agree(nums, sel2, sel, i - 1);\n assert(subset_sum(nums, sel2, i - 1) == sub);\n\n \n lemma_best_optimal(nums, i - 1, src, sel2);\n assert(sub <= best(nums, i - 1, src));\n\n \n let prev_src = best(nums, i - 1, src);\n let prev = best(nums, i - 1, r);\n let take = prev_src + a;\n assert(subset_sum(nums, sel, i) <= take);\n\n \n if prev_src > 0 || src == 0 {\n \n if take > prev {\n assert(best(nums, i, r) == take);\n } else {\n assert(best(nums, i, r) == prev);\n assert(best(nums, i, r) >= take);\n }\n } else {\n \n assert(sub <= prev_src);\n assert(sub == 0);\n assert(false) by (nonlinear_arith)\n requires sub == 0, sub % 3 == src, src >= 1, src < 3;\n }\n }\n}\n\nproof fn lemma_subset_sum_nonneg(nums: Seq, sel: Seq, hi: int)\n requires\n forall |j: int| 0 <= j < hi ==> #[trigger] nums[j] >= 1,\n ensures\n subset_sum(nums, sel, hi) >= 0,\n decreases hi\n{\n if hi > 0 {\n lemma_subset_sum_nonneg(nums, sel, hi - 1);\n }\n}\n\nimpl Solution {\n pub fn max_sum_div_three(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 40000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10000,\n ensures\n result >= 0,\n result as int % 3 == 0,\n exists |sel: Seq| sel.len() == nums.len() as int\n && subset_sum(nums@, sel, nums.len() as int) == result as int,\n forall |sel: Seq| sel.len() == nums.len() as int\n && subset_sum(nums@, sel, nums.len() as int) % 3 == 0\n ==> subset_sum(nums@, sel, nums.len() as int) <= result as int,\n {\n let n = nums.len();\n let mut dp0: i32 = 0;\n let mut dp1: i32 = 0;\n let mut dp2: i32 = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n n <= 40000,\n forall |j: int| 0 <= j < n ==> 1 <= #[trigger] nums@[j] <= 10000,\n dp0 as int == best(nums@, i as int, 0),\n dp1 as int == best(nums@, i as int, 1),\n dp2 as int == best(nums@, i as int, 2),\n decreases n - i,\n {\n proof {\n lemma_best_upper_bound(nums@, i as int, 0);\n lemma_best_upper_bound(nums@, i as int, 1);\n lemma_best_upper_bound(nums@, i as int, 2);\n lemma_best_nonneg(nums@, i as int, 0);\n lemma_best_nonneg(nums@, i as int, 1);\n lemma_best_nonneg(nums@, i as int, 2);\n assert(dp0 as int <= 10000 * (i as int));\n assert(dp1 as int <= 10000 * (i as int));\n assert(dp2 as int <= 10000 * (i as int));\n assert(10000 * (i as int) <= 10000 * 40000);\n }\n\n let a = nums[i];\n let old0 = dp0;\n let old1 = dp1;\n let old2 = dp2;\n let r = a % 3;\n\n if r == 0 {\n dp0 = old0 + a;\n dp1 = if old1 > 0 { old1 + a } else { old1 };\n dp2 = if old2 > 0 { old2 + a } else { old2 };\n } else if r == 1 {\n let new_dp0 = if old2 > 0 && old2 + a > old0 { old2 + a } else { old0 };\n let new_dp1 = if old0 + a > old1 { old0 + a } else { old1 };\n let new_dp2 = if old1 > 0 && old1 + a > old2 { old1 + a } else { old2 };\n dp0 = new_dp0;\n dp1 = new_dp1;\n dp2 = new_dp2;\n } else {\n let new_dp0 = if old1 > 0 && old1 + a > old0 { old1 + a } else { old0 };\n let new_dp1 = if old2 > 0 && old2 + a > old1 { old2 + a } else { old1 };\n let new_dp2 = if old0 + a > old2 { old0 + a } else { old2 };\n dp0 = new_dp0;\n dp1 = new_dp1;\n dp2 = new_dp2;\n }\n\n i += 1;\n }\n\n proof {\n let n_int = n as int;\n \n lemma_best_nonneg(nums@, n_int, 0);\n\n \n lemma_best_remainder(nums@, n_int, 0);\n\n \n lemma_best_achievable(nums@, n_int, 0);\n let sel_witness: Seq = choose |sel: Seq| sel.len() == nums@.len()\n && subset_sum(nums@, sel, n_int) == best(nums@, n_int, 0)\n && (forall |j: int| n_int <= j < nums@.len() ==> (#[trigger] sel[j]) == false);\n\n \n assert forall |sel: Seq| sel.len() == n_int\n && subset_sum(nums@, sel, n_int) % 3 == 0\n implies subset_sum(nums@, sel, n_int) <= dp0 as int by {\n lemma_best_optimal(nums@, n_int, 0, sel);\n };\n }\n\n dp0\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1262/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1262/tests/mutated_testcases.jsonl"} {"problem": "lc1276", "family": "leetcode", "description": "# Number of Burgers with No Waste of Ingredients\n\nGiven two integers `tomatoSlices` and `cheeseSlices`. The ingredients of different burgers are as follows:\n\n- **Jumbo Burger:** 4 tomato slices and 1 cheese slice.\n- **Small Burger:** 2 tomato slices and 1 cheese slice.\n\nReturn `[total_jumbo, total_small]` so that the number of remaining `tomatoSlices` equal to 0 and the number of remaining `cheeseSlices` equal to 0. If it is not possible to make the remaining `tomatoSlices` and `cheeseSlices` equal to 0 return `[]`.\n\n## Example 1:\n\n> **Input:** tomatoSlices = 16, cheeseSlices = 7\n> **Output:** [1, 6]\n> **Explanation:** To make one jumbo burger and 6 small burgers we need 4\\*1 + 2\\*6 = 16 tomato and 1 + 6 = 7 cheese. There will be no remaining ingredients.\n\n## Example 2:\n\n> **Input:** tomatoSlices = 17, cheeseSlices = 4\n> **Output:** []\n> **Explanation:** There will be no way to use all ingredients to make small and jumbo burgers.\n\n## Example 3:\n\n> **Input:** tomatoSlices = 4, cheeseSlices = 17\n> **Output:** []\n> **Explanation:** Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.\n\n## Constraints:\n\n- 0 <= tomatoSlices, cheeseSlices <= 10^7\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nMath\nAcceptance rate: 50.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> (result: Vec)\n requires\n 0 <= tomato_slices <= 10_000_000,\n 0 <= cheese_slices <= 10_000_000,\n ensures\n result@.len() == 0 || result@.len() == 2,\n result@.len() == 2 ==> (\n result@[0] >= 0\n && result@[1] >= 0\n && 4 * (result@[0] as int) + 2 * (result@[1] as int) == tomato_slices as int\n && (result@[0] as int) + (result@[1] as int) == cheese_slices as int\n ),\n result@.len() == 2 <==> (\n tomato_slices as int % 2 == 0\n && 2 * (cheese_slices as int) <= tomato_slices as int\n && tomato_slices as int <= 4 * (cheese_slices as int)\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec {\n if tomato_slices % 2 != 0 {\n return Vec::new();\n }\n let jumbo = tomato_slices / 2 - cheese_slices;\n let small = cheese_slices - jumbo;\n if jumbo < 0 || small < 0 {\n return Vec::new();\n }\n let mut result = Vec::new();\n result.push(jumbo);\n result.push(small);\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> (result: Vec)\n requires\n 0 <= tomato_slices <= 10_000_000,\n 0 <= cheese_slices <= 10_000_000,\n ensures\n result@.len() == 0 || result@.len() == 2,\n result@.len() == 2 ==> (\n result@[0] >= 0\n && result@[1] >= 0\n && 4 * (result@[0] as int) + 2 * (result@[1] as int) == tomato_slices as int\n && (result@[0] as int) + (result@[1] as int) == cheese_slices as int\n ),\n result@.len() == 2 <==> (\n tomato_slices as int % 2 == 0\n && 2 * (cheese_slices as int) <= tomato_slices as int\n && tomato_slices as int <= 4 * (cheese_slices as int)\n ),\n {\n if tomato_slices % 2 != 0 {\n return Vec::new();\n }\n let jumbo = tomato_slices / 2 - cheese_slices;\n let small = cheese_slices - jumbo;\n if jumbo < 0 || small < 0 {\n return Vec::new();\n }\n let mut result = Vec::new();\n result.push(jumbo);\n result.push(small);\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> (result: Vec)\n requires\n 0 <= tomato_slices <= 10_000_000,\n 0 <= cheese_slices <= 10_000_000,\n ensures\n result@.len() == 0 || result@.len() == 2,\n result@.len() == 2 ==> (\n result@[0] >= 0\n && result@[1] >= 0\n && 4 * (result@[0] as int) + 2 * (result@[1] as int) == tomato_slices as int\n && (result@[0] as int) + (result@[1] as int) == cheese_slices as int\n ),\n result@.len() == 2 <==> (\n tomato_slices as int % 2 == 0\n && 2 * (cheese_slices as int) <= tomato_slices as int\n && tomato_slices as int <= 4 * (cheese_slices as int)\n ),\n {\n if tomato_slices % 2 != 0 {\n return Vec::new();\n }\n let jumbo = tomato_slices / 2 - cheese_slices;\n let small = cheese_slices - jumbo;\n if jumbo < 0 || small < 0 {\n return Vec::new();\n }\n let mut result = Vec::new();\n result.push(jumbo);\n result.push(small);\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1276/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1276/tests/mutated_testcases.jsonl"} {"problem": "lc1277", "family": "leetcode", "description": "# Count Square Submatrices with All Ones\n\nGiven a `m * n` matrix of ones and zeros, return how many **square** submatrices have all ones.\n\n## Example 1:\n\n> **Input:** matrix = [[0,1,1,1],[1,1,1,1],[0,1,1,1]]\n> **Output:** 15\n> **Explanation:**\n> There are **10** squares of side 1.\n> There are **4** squares of side 2.\n> There is **1** square of side 3.\n> Total number of squares = 10 + 4 + 1 = **15**.\n\n## Example 2:\n\n> **Input:** matrix = [[1,0,1],[1,1,0],[1,1,0]]\n> **Output:** 7\n> **Explanation:**\n> There are **6** squares of side 1.\n> There is **1** square of side 2.\n> Total number of squares = 6 + 1 = **7**.\n\n## Constraints:\n\n- `1 <= m <= 300`\n- `1 <= n <= 300`\n- `0 <= matrix[i][j] <= 1`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_squares(matrix: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming, Matrix\nAcceptance rate: 80.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\npub open spec fn is_all_ones_square(mat: Seq>, r: int, c: int, s: int) -> bool {\n &&& s >= 1\n &&& r >= 0 && r + s <= mat.len()\n &&& c >= 0 && c + s <= mat[0int].len()\n &&& forall|dr: int, dc: int|\n 0 <= dr < s && 0 <= dc < s ==> #[trigger] mat[r + dr][c + dc] == 1\n}\n\npub open spec fn min3(a: int, b: int, c: int) -> int {\n if a <= b && a <= c { a }\n else if b <= c { b }\n else { c }\n}\n\npub open spec fn dp_val(mat: Seq>, i: int, j: int) -> int\n decreases (if i > 0 { i } else { 0 }) + (if j > 0 { j } else { 0 })\n{\n if i < 0 || j < 0 { 0 }\n else if mat[i][j] != 1 { 0 }\n else if i == 0 || j == 0 { 1 }\n else { 1 + min3(dp_val(mat, i - 1, j), dp_val(mat, i, j - 1), dp_val(mat, i - 1, j - 1)) }\n}\n\npub open spec fn row_sum(mat: Seq>, i: int, j: int, n: int) -> int\n decreases (if j < n { n - j } else { 0 })\n{\n if j >= n { 0 }\n else { dp_val(mat, i, j) + row_sum(mat, i, j + 1, n) }\n}\n\npub open spec fn total_sum(mat: Seq>, i: int, m: int, n: int) -> int\n decreases (if i < m { m - i } else { 0 })\n{\n if i >= m { 0 }\n else { row_sum(mat, i, 0, n) + total_sum(mat, i + 1, m, n) }\n}\n\nimpl Solution {\n pub fn count_squares(matrix: Vec>) -> (result: i32)\n requires\n 1 <= matrix.len() <= 300,\n forall |i: int| 0 <= i < matrix.len() ==> 1 <= (#[trigger] matrix[i]).len() <= 300,\n forall |i: int| 0 <= i < matrix.len() ==> (#[trigger] matrix[i]).len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len()\n ==> #[trigger] matrix[i][j] == 0 || matrix[i][j] == 1,\n ensures\n result as int == total_sum(matrix@, 0, matrix.len() as int, matrix[0].len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn set_dp(dp: &mut Vec>, row: usize, col: usize, value: i32) {\n let mut current_row = dp[row].clone();\n current_row[col] = value;\n dp[row] = current_row;\n }\n\n pub fn count_squares(matrix: Vec>) -> i32 {\n let m = matrix.len();\n let n = matrix[0].len();\n let mut dp: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n row.push(0);\n j = j + 1;\n }\n dp.push(row);\n i = i + 1;\n }\n let mut ans: i32 = 0;\n i = 0;\n while i < m {\n let mut j: usize = 0;\n while j < n {\n if matrix[i][j] == 1 {\n if i == 0 || j == 0 {\n Self::set_dp(&mut dp, i, j, 1);\n } else {\n let a = dp[i - 1][j];\n let b = dp[i][j - 1];\n let c = dp[i - 1][j - 1];\n let min_val = if a <= b && a <= c { a } else if b <= c { b } else { c };\n Self::set_dp(&mut dp, i, j, 1 + min_val);\n }\n }\n ans = ans + dp[i][j];\n j = j + 1;\n }\n i = i + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\npub open spec fn is_all_ones_square(mat: Seq>, r: int, c: int, s: int) -> bool {\n &&& s >= 1\n &&& r >= 0 && r + s <= mat.len()\n &&& c >= 0 && c + s <= mat[0int].len()\n &&& forall|dr: int, dc: int|\n 0 <= dr < s && 0 <= dc < s ==> #[trigger] mat[r + dr][c + dc] == 1\n}\n\npub open spec fn min3(a: int, b: int, c: int) -> int {\n if a <= b && a <= c { a }\n else if b <= c { b }\n else { c }\n}\n\npub open spec fn dp_val(mat: Seq>, i: int, j: int) -> int\n decreases (if i > 0 { i } else { 0 }) + (if j > 0 { j } else { 0 })\n{\n if i < 0 || j < 0 { 0 }\n else if mat[i][j] != 1 { 0 }\n else if i == 0 || j == 0 { 1 }\n else { 1 + min3(dp_val(mat, i - 1, j), dp_val(mat, i, j - 1), dp_val(mat, i - 1, j - 1)) }\n}\n\npub open spec fn row_sum(mat: Seq>, i: int, j: int, n: int) -> int\n decreases (if j < n { n - j } else { 0 })\n{\n if j >= n { 0 }\n else { dp_val(mat, i, j) + row_sum(mat, i, j + 1, n) }\n}\n\npub open spec fn total_sum(mat: Seq>, i: int, m: int, n: int) -> int\n decreases (if i < m { m - i } else { 0 })\n{\n if i >= m { 0 }\n else { row_sum(mat, i, 0, n) + total_sum(mat, i + 1, m, n) }\n}\n\nimpl Solution {\n fn set_dp(dp: &mut Vec>, row: usize, col: usize, value: i32)\n requires\n row < old(dp)@.len(),\n col < old(dp)@[row as int].len(),\n ensures\n dp@.len() == old(dp)@.len(),\n forall |r: int| 0 <= r < dp@.len() ==> (#[trigger] dp@[r]).len() == old(dp)@[r].len(),\n forall |r: int, c: int|\n 0 <= r < dp@.len() && 0 <= c < dp@[r].len()\n ==> #[trigger] dp@[r][c] == if r == row as int && c == col as int { value } else { old(dp)@[r][c] },\n {\n let mut current_row = dp[row].clone();\n current_row.set(col, value);\n dp.set(row, current_row);\n }\n\n pub fn count_squares(matrix: Vec>) -> (result: i32)\n requires\n 1 <= matrix.len() <= 300,\n forall |i: int| 0 <= i < matrix.len() ==> 1 <= (#[trigger] matrix[i]).len() <= 300,\n forall |i: int| 0 <= i < matrix.len() ==> (#[trigger] matrix[i]).len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len()\n ==> #[trigger] matrix[i][j] == 0 || matrix[i][j] == 1,\n ensures\n result as int == total_sum(matrix@, 0, matrix.len() as int, matrix[0].len() as int),\n {\n let m = matrix.len();\n let n = matrix[0].len();\n let mut dp: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n row.push(0);\n j = j + 1;\n }\n dp.push(row);\n i = i + 1;\n }\n let mut ans: i32 = 0;\n i = 0;\n while i < m {\n let mut j: usize = 0;\n while j < n {\n if matrix[i][j] == 1 {\n if i == 0 || j == 0 {\n Self::set_dp(&mut dp, i, j, 1);\n } else {\n let a = dp[i - 1][j];\n let b = dp[i][j - 1];\n let c = dp[i - 1][j - 1];\n let min_val = if a <= b && a <= c { a } else if b <= c { b } else { c };\n Self::set_dp(&mut dp, i, j, 1 + min_val);\n }\n }\n ans = ans + dp[i][j];\n j = j + 1;\n }\n i = i + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\npub open spec fn is_all_ones_square(mat: Seq>, r: int, c: int, s: int) -> bool {\n &&& s >= 1\n &&& r >= 0 && r + s <= mat.len()\n &&& c >= 0 && c + s <= mat[0int].len()\n &&& forall|dr: int, dc: int|\n 0 <= dr < s && 0 <= dc < s ==> #[trigger] mat[r + dr][c + dc] == 1\n}\n\npub open spec fn min3(a: int, b: int, c: int) -> int {\n if a <= b && a <= c { a }\n else if b <= c { b }\n else { c }\n}\n\npub open spec fn dp_val(mat: Seq>, i: int, j: int) -> int\n decreases (if i > 0 { i } else { 0 }) + (if j > 0 { j } else { 0 })\n{\n if i < 0 || j < 0 { 0 }\n else if mat[i][j] != 1 { 0 }\n else if i == 0 || j == 0 { 1 }\n else { 1 + min3(dp_val(mat, i - 1, j), dp_val(mat, i, j - 1), dp_val(mat, i - 1, j - 1)) }\n}\n\npub open spec fn row_sum(mat: Seq>, i: int, j: int, n: int) -> int\n decreases (if j < n { n - j } else { 0 })\n{\n if j >= n { 0 }\n else { dp_val(mat, i, j) + row_sum(mat, i, j + 1, n) }\n}\n\npub open spec fn total_sum(mat: Seq>, i: int, m: int, n: int) -> int\n decreases (if i < m { m - i } else { 0 })\n{\n if i >= m { 0 }\n else { row_sum(mat, i, 0, n) + total_sum(mat, i + 1, m, n) }\n}\n\nproof fn dp_val_bounds(mat: Seq>, m: int, n: int, i: int, j: int)\n requires\n m == mat.len(),\n m >= 1,\n n >= 1,\n 0 <= i < m,\n 0 <= j < n,\n forall |r: int| 0 <= r < m ==> (#[trigger] mat[r]).len() == n,\n forall |r: int, c: int| 0 <= r < m && 0 <= c < n\n ==> #[trigger] mat[r][c] == 0 || mat[r][c] == 1,\n ensures\n 0 <= dp_val(mat, i, j),\n dp_val(mat, i, j) <= i + 1,\n dp_val(mat, i, j) <= j + 1,\n decreases i + j\n{\n if mat[i][j] != 1 {\n } else if i == 0 || j == 0 {\n } else {\n dp_val_bounds(mat, m, n, i - 1, j);\n dp_val_bounds(mat, m, n, i, j - 1);\n dp_val_bounds(mat, m, n, i - 1, j - 1);\n }\n}\n\nproof fn row_sum_split(mat: Seq>, i: int, a: int, b: int, n: int)\n requires\n a <= b,\n b <= n,\n ensures\n row_sum(mat, i, a, n) == row_sum(mat, i, a, b) + row_sum(mat, i, b, n),\n decreases b - a\n{\n if a < b {\n row_sum_split(mat, i, a + 1, b, n);\n }\n}\n\nproof fn total_sum_split(mat: Seq>, a: int, b: int, m: int, n: int)\n requires\n a <= b,\n b <= m,\n ensures\n total_sum(mat, a, m, n) == total_sum(mat, a, b, n) + total_sum(mat, b, m, n),\n decreases b - a\n{\n if a < b {\n total_sum_split(mat, a + 1, b, m, n);\n }\n}\n\nproof fn row_sum_nonneg(mat: Seq>, m: int, n: int, i: int, j: int)\n requires\n m == mat.len(),\n m >= 1,\n n >= 1,\n 0 <= i < m,\n 0 <= j,\n forall |r: int| 0 <= r < m ==> (#[trigger] mat[r]).len() == n,\n forall |r: int, c: int| 0 <= r < m && 0 <= c < n\n ==> #[trigger] mat[r][c] == 0 || mat[r][c] == 1,\n ensures\n row_sum(mat, i, j, n) >= 0,\n decreases (if j < n { n - j } else { 0 })\n{\n if j < n {\n dp_val_bounds(mat, m, n, i, j);\n row_sum_nonneg(mat, m, n, i, j + 1);\n }\n}\n\nproof fn total_sum_nonneg(mat: Seq>, m: int, n: int, i: int)\n requires\n m == mat.len(),\n m >= 1,\n n >= 1,\n 0 <= i,\n forall |r: int| 0 <= r < m ==> (#[trigger] mat[r]).len() == n,\n forall |r: int, c: int| 0 <= r < m && 0 <= c < n\n ==> #[trigger] mat[r][c] == 0 || mat[r][c] == 1,\n ensures\n total_sum(mat, i, m, n) >= 0,\n decreases (if i < m { m - i } else { 0 })\n{\n if i < m {\n row_sum_nonneg(mat, m, n, i, 0);\n total_sum_nonneg(mat, m, n, i + 1);\n }\n}\n\nproof fn row_sum_upper_bound(mat: Seq>, m: int, n: int, i: int, j: int)\n requires\n m == mat.len(),\n 1 <= m <= 300,\n 1 <= n <= 300,\n 0 <= i < m,\n 0 <= j,\n forall |r: int| 0 <= r < m ==> (#[trigger] mat[r]).len() == n,\n forall |r: int, c: int| 0 <= r < m && 0 <= c < n\n ==> #[trigger] mat[r][c] == 0 || mat[r][c] == 1,\n ensures\n row_sum(mat, i, j, n) <= (if j < n { (n - j) * 300 } else { 0 }),\n decreases (if j < n { n - j } else { 0 })\n{\n if j < n {\n dp_val_bounds(mat, m, n, i, j);\n row_sum_upper_bound(mat, m, n, i, j + 1);\n assert(dp_val(mat, i, j) <= 300) by {\n assert(dp_val(mat, i, j) <= i + 1);\n assert(i + 1 <= 300);\n }\n assert((n - j) * 300 >= 300 + (n - j - 1) * 300) by (nonlinear_arith)\n requires n > j;\n }\n}\n\nproof fn total_sum_upper_bound(mat: Seq>, m: int, n: int, i: int)\n requires\n m == mat.len(),\n 1 <= m <= 300,\n 1 <= n <= 300,\n 0 <= i,\n forall |r: int| 0 <= r < m ==> (#[trigger] mat[r]).len() == n,\n forall |r: int, c: int| 0 <= r < m && 0 <= c < n\n ==> #[trigger] mat[r][c] == 0 || mat[r][c] == 1,\n ensures\n total_sum(mat, i, m, n) <= (if i < m { (m - i) * n * 300 } else { 0 }),\n decreases (if i < m { m - i } else { 0 })\n{\n if i < m {\n row_sum_upper_bound(mat, m, n, i, 0);\n total_sum_upper_bound(mat, m, n, i + 1);\n assert(row_sum(mat, i, 0, n) <= n * 300);\n if i + 1 < m {\n assert(total_sum(mat, i + 1, m, n) <= (m - i - 1) * n * 300);\n }\n assert(n * 300 + (m - i - 1) * n * 300 <= (m - i) * n * 300) by (nonlinear_arith)\n requires m > i, n >= 1;\n }\n}\n\nimpl Solution {\n fn set_dp(dp: &mut Vec>, row: usize, col: usize, value: i32)\n requires\n row < old(dp)@.len(),\n col < old(dp)@[row as int].len(),\n ensures\n dp@.len() == old(dp)@.len(),\n forall |r: int| 0 <= r < dp@.len() ==> (#[trigger] dp@[r]).len() == old(dp)@[r].len(),\n forall |r: int, c: int|\n 0 <= r < dp@.len() && 0 <= c < dp@[r].len()\n ==> #[trigger] dp@[r][c] == if r == row as int && c == col as int { value } else { old(dp)@[r][c] },\n {\n let mut current_row = dp[row].clone();\n current_row.set(col, value);\n dp.set(row, current_row);\n }\n\n pub fn count_squares(matrix: Vec>) -> (result: i32)\n requires\n 1 <= matrix.len() <= 300,\n forall |i: int| 0 <= i < matrix.len() ==> 1 <= (#[trigger] matrix[i]).len() <= 300,\n forall |i: int| 0 <= i < matrix.len() ==> (#[trigger] matrix[i]).len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len()\n ==> #[trigger] matrix[i][j] == 0 || matrix[i][j] == 1,\n ensures\n result as int == total_sum(matrix@, 0, matrix.len() as int, matrix[0].len() as int),\n {\n let m = matrix.len();\n let n = matrix[0].len();\n\n let mut dp: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m\n invariant\n m == matrix.len(),\n n == matrix[0].len(),\n dp@.len() == i as int,\n 0 <= i <= m,\n forall |r: int| 0 <= r < i ==> (#[trigger] dp@[r]).len() == n as int,\n forall |r: int, c: int| 0 <= r < i && 0 <= c < n ==> #[trigger] dp@[r][c] == 0i32,\n decreases m - i\n {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n\n invariant\n n == matrix[0].len(),\n row@.len() == j as int,\n 0 <= j <= n,\n forall |c: int| 0 <= c < j ==> #[trigger] row@[c] == 0i32,\n decreases n - j\n {\n row.push(0);\n j = j + 1;\n }\n dp.push(row);\n i = i + 1;\n }\n\n proof {\n total_sum_upper_bound(matrix@, m as int, n as int, 0);\n assert(m as int * n as int * 300 <= 27_000_000) by (nonlinear_arith)\n requires 1 <= m as int <= 300, 1 <= n as int <= 300;\n total_sum_nonneg(matrix@, m as int, n as int, 0);\n }\n\n let mut ans: i32 = 0;\n i = 0;\n while i < m\n invariant\n m == matrix.len(),\n n == matrix[0].len(),\n 1 <= m <= 300,\n 1 <= n <= 300,\n dp@.len() == m as int,\n forall |r: int| 0 <= r < m ==> (#[trigger] dp@[r]).len() == n as int,\n forall |i2: int| 0 <= i2 < matrix.len() ==> 1 <= (#[trigger] matrix[i2]).len() <= 300,\n forall |i2: int| 0 <= i2 < matrix.len() ==> (#[trigger] matrix[i2]).len() == matrix[0].len(),\n forall |i2: int, j2: int| 0 <= i2 < matrix.len() && 0 <= j2 < matrix[0].len()\n ==> #[trigger] matrix[i2][j2] == 0 || matrix[i2][j2] == 1,\n 0 <= i <= m,\n forall |r: int, c: int| 0 <= r < i && 0 <= c < n\n ==> (#[trigger] dp@[r][c]) as int == dp_val(matrix@, r, c),\n forall |r: int, c: int| i <= r < m && 0 <= c < n\n ==> (#[trigger] dp@[r][c]) == 0i32,\n ans as int == total_sum(matrix@, 0, i as int, n as int),\n 0 <= ans as int <= 27_000_000,\n decreases m - i\n {\n let mut j: usize = 0;\n while j < n\n invariant\n m == matrix.len(),\n n == matrix[0].len(),\n 1 <= m <= 300,\n 1 <= n <= 300,\n dp@.len() == m as int,\n forall |r: int| 0 <= r < m ==> (#[trigger] dp@[r]).len() == n as int,\n forall |i2: int| 0 <= i2 < matrix.len() ==> 1 <= (#[trigger] matrix[i2]).len() <= 300,\n forall |i2: int| 0 <= i2 < matrix.len() ==> (#[trigger] matrix[i2]).len() == matrix[0].len(),\n forall |i2: int, j2: int| 0 <= i2 < matrix.len() && 0 <= j2 < matrix[0].len()\n ==> #[trigger] matrix[i2][j2] == 0 || matrix[i2][j2] == 1,\n 0 <= i < m,\n 0 <= j <= n,\n forall |r: int, c: int| 0 <= r < i && 0 <= c < n\n ==> (#[trigger] dp@[r][c]) as int == dp_val(matrix@, r, c),\n forall |c: int| 0 <= c < j\n ==> (#[trigger] dp@[i as int][c]) as int == dp_val(matrix@, i as int, c),\n forall |c: int| j <= c < n\n ==> (#[trigger] dp@[i as int][c]) == 0i32,\n forall |r: int, c: int| i + 1 <= r < m && 0 <= c < n\n ==> (#[trigger] dp@[r][c]) == 0i32,\n ans as int == total_sum(matrix@, 0, i as int, n as int) + row_sum(matrix@, i as int, 0, j as int),\n 0 <= ans as int <= 27_000_000,\n decreases n - j\n {\n let ghost ii = i as int;\n let ghost jj = j as int;\n\n if matrix[i][j] == 1 {\n if i == 0 || j == 0 {\n Self::set_dp(&mut dp, i, j, 1);\n } else {\n let a = dp[i - 1][j];\n let b = dp[i][j - 1];\n let c = dp[i - 1][j - 1];\n proof {\n assert(a as int == dp_val(matrix@, ii - 1, jj));\n assert(b as int == dp_val(matrix@, ii, jj - 1));\n assert(c as int == dp_val(matrix@, ii - 1, jj - 1));\n dp_val_bounds(matrix@, m as int, n as int, ii - 1, jj);\n dp_val_bounds(matrix@, m as int, n as int, ii, jj - 1);\n dp_val_bounds(matrix@, m as int, n as int, ii - 1, jj - 1);\n }\n let min_val = if a <= b && a <= c { a } else if b <= c { b } else { c };\n Self::set_dp(&mut dp, i, j, 1 + min_val);\n }\n }\n\n proof {\n dp_val_bounds(matrix@, m as int, n as int, ii, jj);\n\n assert(dp@[ii][jj] as int == dp_val(matrix@, ii, jj));\n\n assert(0 <= dp@[ii][jj] as int <= 300) by {\n assert(dp@[ii][jj] as int == dp_val(matrix@, ii, jj));\n assert(dp_val(matrix@, ii, jj) <= ii + 1);\n assert(ii + 1 <= 300);\n }\n\n row_sum_split(matrix@, ii, 0, jj, jj + 1);\n assert(row_sum(matrix@, ii, jj + 1, jj + 1) == 0int);\n assert(row_sum(matrix@, ii, jj, jj + 1) == dp_val(matrix@, ii, jj) + 0int);\n assert(row_sum(matrix@, ii, jj, jj + 1) == dp_val(matrix@, ii, jj));\n\n row_sum_split(matrix@, ii, 0, jj + 1, n as int);\n row_sum_nonneg(matrix@, m as int, n as int, ii, jj + 1);\n total_sum_split(matrix@, 0, ii, m as int, n as int);\n total_sum_split(matrix@, 0, ii + 1, m as int, n as int);\n total_sum_nonneg(matrix@, m as int, n as int, ii + 1);\n row_sum_nonneg(matrix@, m as int, n as int, ii, 0);\n\n assert(total_sum(matrix@, 0, ii + 1, n as int)\n == total_sum(matrix@, 0, ii, n as int) + row_sum(matrix@, ii, 0, n as int));\n assert(row_sum(matrix@, ii, 0, n as int)\n == row_sum(matrix@, ii, 0, jj + 1) + row_sum(matrix@, ii, jj + 1, n as int));\n assert(row_sum(matrix@, ii, jj + 1, n as int) >= 0);\n assert(total_sum(matrix@, ii + 1, m as int, n as int) >= 0);\n assert(total_sum(matrix@, 0, m as int, n as int)\n == total_sum(matrix@, 0, ii + 1, n as int) + total_sum(matrix@, ii + 1, m as int, n as int));\n\n total_sum_upper_bound(matrix@, m as int, n as int, 0);\n assert(m as int * n as int * 300 <= 27_000_000) by (nonlinear_arith)\n requires 1 <= m as int <= 300, 1 <= n as int <= 300;\n }\n\n ans = ans + dp[i][j];\n j = j + 1;\n }\n\n proof {\n let ii = i as int;\n total_sum_split(matrix@, 0, ii, ii + 1, n as int);\n assert(total_sum(matrix@, ii + 1, ii + 1, n as int) == 0int);\n assert(total_sum(matrix@, ii, ii + 1, n as int) == row_sum(matrix@, ii, 0, n as int) + 0int);\n assert(total_sum(matrix@, ii, ii + 1, n as int) == row_sum(matrix@, ii, 0, n as int));\n assert(ans as int == total_sum(matrix@, 0, ii + 1, n as int));\n\n assert forall |r: int, c: int| 0 <= r < ii + 1 && 0 <= c < n as int\n implies (#[trigger] dp@[r][c]) as int == dp_val(matrix@, r, c) by {};\n }\n\n i = i + 1;\n }\n\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1277/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1277/tests/mutated_testcases.jsonl"} {"problem": "lc1283", "family": "leetcode", "description": "# Find the Smallest Divisor Given a Threshold\n\nGiven an array of integers `nums` and an integer `threshold`, we will choose a positive integer `divisor`, divide all the array by it, and sum the division's result. Find the **smallest** `divisor` such that the result mentioned above is less than or equal to `threshold`.\n\nEach result of the division is rounded to the nearest integer greater than or equal to that element. (For example: `7 / 3 = 3` and `10 / 2 = 5`).\n\nThe test cases are generated so that there will be an answer.\n\n## Example 1:\n\n> **Input:** nums = [1,2,5,9], threshold = 6\n> **Output:** 5\n> **Explanation:** We can get a sum to 17 (1+2+5+9) if the divisor is 1.\n> If the divisor is 4 we can get a sum of 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).\n\n## Example 2:\n\n> **Input:** nums = [44,22,33,11,1], threshold = 5\n> **Output:** 44\n\n## Constraints:\n\n- `1 <= nums.length <= 5 * 10^4`\n- `1 <= nums[i] <= 10^6`\n- `nums.length <= threshold <= 10^6`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn smallest_divisor(nums: Vec, threshold: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nStaff, Array, Binary Search, Weekly Contest 166\nAcceptance rate: 65.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ceil_div(x: int, d: int) -> int {\n (x + d - 1) / d\n }\n\n pub open spec fn sum_prefix(nums: Seq, d: int, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else {\n Self::sum_prefix(nums, d, n - 1) + Self::ceil_div(nums[n - 1] as int, d)\n }\n }\n\n pub open spec fn sum_by_divisor(nums: Seq, d: int) -> int {\n Self::sum_prefix(nums, d, nums.len() as int)\n }\n\n fn sum_with_divisor(nums: &Vec, divisor: i32) -> (sum: i64)\n requires\n 1 <= nums.len() <= 50_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n 1 <= divisor <= 1_000_000,\n ensures\n sum as int == Self::sum_by_divisor(nums@, divisor as int),\n 0 <= sum <= 50_000 * 1_000_000,\n {\n \n }\n\n pub fn smallest_divisor(nums: Vec, threshold: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 50_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n nums.len() <= threshold <= 1_000_000,\n ensures\n 1 <= res <= 1_000_000,\n Self::sum_by_divisor(nums@, res as int) <= threshold as int,\n forall |d: int| 1 <= d < res ==> #[trigger] Self::sum_by_divisor(nums@, d) > threshold as int,\n {\n \n }\n}\n\n}\n", "code": "impl Solution {\n fn sum_with_divisor(nums: &Vec, divisor: i32) -> i64\n {\n let mut sum: i64 = 0;\n let mut i: usize = 0;\n while i < nums.len()\n {\n let n = nums[i];\n let term: i64 = (n as i64 + divisor as i64 - 1) / divisor as i64;\n sum += term;\n i += 1;\n }\n sum\n }\n\n pub fn smallest_divisor(nums: Vec, threshold: i32) -> i32\n {\n let mut left: i32 = 1;\n let mut right: i32 = 1_000_000;\n\n while left < right\n {\n let mid = left + (right - left) / 2;\n let sum = Self::sum_with_divisor(&nums, mid);\n if sum <= threshold as i64 {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n\n left\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ceil_div(x: int, d: int) -> int {\n (x + d - 1) / d\n }\n\n pub open spec fn sum_prefix(nums: Seq, d: int, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else {\n Self::sum_prefix(nums, d, n - 1) + Self::ceil_div(nums[n - 1] as int, d)\n }\n }\n\n pub open spec fn sum_by_divisor(nums: Seq, d: int) -> int {\n Self::sum_prefix(nums, d, nums.len() as int)\n }\n\n fn sum_with_divisor(nums: &Vec, divisor: i32) -> (sum: i64)\n requires\n 1 <= nums.len() <= 50_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n 1 <= divisor <= 1_000_000,\n ensures\n sum as int == Self::sum_by_divisor(nums@, divisor as int),\n 0 <= sum <= 50_000 * 1_000_000,\n {\n let mut sum: i64 = 0;\n let mut i: usize = 0;\n while i < nums.len()\n {\n let n = nums[i];\n let term: i64 = (n as i64 + divisor as i64 - 1) / divisor as i64;\n sum += term;\n i += 1;\n }\n sum\n }\n\n pub fn smallest_divisor(nums: Vec, threshold: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 50_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n nums.len() <= threshold <= 1_000_000,\n ensures\n 1 <= res <= 1_000_000,\n Self::sum_by_divisor(nums@, res as int) <= threshold as int,\n forall |d: int| 1 <= d < res ==> #[trigger] Self::sum_by_divisor(nums@, d) > threshold as int,\n {\n let mut left: i32 = 1;\n let mut right: i32 = 1_000_000;\n\n while left < right\n {\n let mid = left + (right - left) / 2;\n let sum = Self::sum_with_divisor(&nums, mid);\n if sum <= threshold as i64 {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n\n left\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ceil_div(x: int, d: int) -> int {\n (x + d - 1) / d\n }\n\n pub open spec fn sum_prefix(nums: Seq, d: int, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else {\n Self::sum_prefix(nums, d, n - 1) + Self::ceil_div(nums[n - 1] as int, d)\n }\n }\n\n pub open spec fn sum_by_divisor(nums: Seq, d: int) -> int {\n Self::sum_prefix(nums, d, nums.len() as int)\n }\n\n proof fn lemma_sum_prefix_upper_bound(nums: Seq, d: int, n: int)\n requires\n 1 <= d,\n 0 <= n <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n Self::sum_prefix(nums, d, n) <= n * 1_000_000,\n decreases n\n {\n if n > 0 {\n Self::lemma_sum_prefix_upper_bound(nums, d, n - 1);\n assert(Self::ceil_div(nums[n - 1] as int, d) <= nums[n - 1] as int) by (nonlinear_arith)\n requires\n 1 <= d,\n nums[n - 1] >= 1,\n {\n }\n }\n }\n\n proof fn lemma_ceil_div_monotonic(x: int, d1: int, d2: int)\n requires\n 1 <= x,\n 1 <= d1 <= d2,\n ensures\n Self::ceil_div(x, d2) <= Self::ceil_div(x, d1),\n {\n assert((x + d2 - 1) * d1 <= (x + d1 - 1) * d2) by (nonlinear_arith)\n requires\n 1 <= x,\n 1 <= d1 <= d2,\n {\n }\n assert((x + d2 - 1) / d2 <= (x + d1 - 1) / d1) by (nonlinear_arith)\n requires\n 1 <= d1,\n 1 <= d2,\n (x + d2 - 1) * d1 <= (x + d1 - 1) * d2,\n {\n }\n }\n\n proof fn lemma_sum_prefix_monotonic(nums: Seq, d1: int, d2: int, n: int)\n requires\n 0 <= n <= nums.len(),\n 1 <= d1 <= d2,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n Self::sum_prefix(nums, d2, n) <= Self::sum_prefix(nums, d1, n),\n decreases n\n {\n if n > 0 {\n Self::lemma_sum_prefix_monotonic(nums, d1, d2, n - 1);\n Self::lemma_ceil_div_monotonic(nums[n - 1] as int, d1, d2);\n }\n }\n\n proof fn lemma_sum_prefix_at_max(nums: Seq, n: int, k: int)\n requires\n 0 <= n <= nums.len(),\n k >= 1,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] as int <= k,\n ensures\n Self::sum_prefix(nums, k, n) == n,\n decreases n\n {\n if n > 0 {\n Self::lemma_sum_prefix_at_max(nums, n - 1, k);\n assert(Self::ceil_div(nums[n - 1] as int, k) == 1) by (nonlinear_arith)\n requires 1 <= nums[n - 1] as int <= k, k >= 1\n {\n }\n }\n }\n\n proof fn lemma_sum_by_divisor_monotonic(nums: Seq, d1: int, d2: int)\n requires\n 1 <= d1 <= d2,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n Self::sum_by_divisor(nums, d2) <= Self::sum_by_divisor(nums, d1),\n {\n Self::lemma_sum_prefix_monotonic(nums, d1, d2, nums.len() as int);\n }\n\n fn sum_with_divisor(nums: &Vec, divisor: i32) -> (sum: i64)\n requires\n 1 <= nums.len() <= 50_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n 1 <= divisor <= 1_000_000,\n ensures\n sum as int == Self::sum_by_divisor(nums@, divisor as int),\n 0 <= sum <= 50_000 * 1_000_000,\n {\n let mut sum: i64 = 0;\n let mut i: usize = 0;\n while i < nums.len()\n invariant\n 0 <= i <= nums.len(),\n 1 <= nums.len() <= 50_000,\n sum as int == Self::sum_prefix(nums@, divisor as int, i as int),\n 0 <= sum as int <= i as int * 1_000_000,\n 1 <= divisor <= 1_000_000,\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 1_000_000,\n decreases nums.len() - i\n {\n let n = nums[i];\n let term: i64 = (n as i64 + divisor as i64 - 1) / divisor as i64;\n proof {\n assert(term as int == Self::ceil_div(nums[i as int] as int, divisor as int)) by (nonlinear_arith)\n requires\n 1 <= nums[i as int] as int <= 1_000_000,\n 1 <= divisor as int <= 1_000_000,\n n as int == nums[i as int] as int,\n term as int == (n as int + divisor as int - 1) / divisor as int {}\n assert(1 <= term as int <= 1_000_000) by (nonlinear_arith)\n requires\n 1 <= nums[i as int] as int <= 1_000_000,\n 1 <= divisor as int,\n term as int == (nums[i as int] as int + divisor as int - 1) / divisor as int {}\n assert(sum as int + term as int <= (i as int + 1) * 1_000_000) by (nonlinear_arith)\n requires\n 0 <= sum as int <= i as int * 1_000_000,\n 1 <= term as int <= 1_000_000 {}\n assert((i as int + 1) * 1_000_000 <= 50_000 * 1_000_000) by (nonlinear_arith)\n requires i < 50_000 {}\n assert(50_000 * 1_000_000 < 9_223_372_036_854_775_807);\n }\n sum += term;\n i += 1;\n }\n sum\n }\n\n pub fn smallest_divisor(nums: Vec, threshold: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 50_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n nums.len() <= threshold <= 1_000_000,\n ensures\n 1 <= res <= 1_000_000,\n Self::sum_by_divisor(nums@, res as int) <= threshold as int,\n forall |d: int| 1 <= d < res ==> #[trigger] Self::sum_by_divisor(nums@, d) > threshold as int,\n {\n proof {\n Self::lemma_sum_prefix_at_max(nums@, nums.len() as int, 1_000_000);\n assert(Self::sum_by_divisor(nums@, 1_000_000) == nums.len() as int);\n }\n\n let mut left: i32 = 1;\n let mut right: i32 = 1_000_000;\n\n while left < right\n invariant\n 1 <= left <= right <= 1_000_000,\n 1 <= nums.len() <= 50_000,\n Self::sum_by_divisor(nums@, right as int) <= threshold as int,\n forall |d: int| 1 <= d < left ==> #[trigger] Self::sum_by_divisor(nums@, d) > threshold as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n nums.len() <= threshold <= 1_000_000,\n decreases right - left\n {\n let mid = left + (right - left) / 2;\n let sum = Self::sum_with_divisor(&nums, mid);\n if sum <= threshold as i64 {\n right = mid;\n } else {\n proof {\n assert(Self::sum_by_divisor(nums@, mid as int) > threshold as int) by (nonlinear_arith)\n requires\n sum as int == Self::sum_by_divisor(nums@, mid as int),\n sum > threshold as i64,\n {\n }\n assert forall |d: int| 1 <= d < mid + 1 implies Self::sum_by_divisor(nums@, d) > threshold as int by {\n if d < left {\n } else {\n assert(left <= d <= mid);\n Self::lemma_sum_by_divisor_monotonic(nums@, d, mid as int);\n assert(Self::sum_by_divisor(nums@, d) >= Self::sum_by_divisor(nums@, mid as int));\n }\n }\n }\n left = mid + 1;\n }\n }\n\n left\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1283/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1283/tests/mutated_testcases.jsonl"} {"problem": "lc1291", "family": "leetcode", "description": "# Sequential Digits\n\nAn integer has sequential digits if and only if each digit in the number is one more than the previous digit.\n\nReturn a sorted list of all the integers in the range `[low, high]` inclusive that have sequential digits.\n\n## Example 1:\n\n> **Input:** low = 100, high = 300\n> **Output:** [123,234]\n\n## Example 2:\n\n> **Input:** low = 1000, high = 13000\n> **Output:** [1234,2345,3456,4567,5678,6789,12345]\n\n## Constraints:\n\n- $10 <= low <= high <= 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn sequential_digits(low: i32, high: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nEnumeration\nAcceptance rate: 65.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub closed spec fn is_sequential_digits(n: int) -> bool\n decreases n\n {\n if n < 10 {\n 1 <= n && n <= 9\n } else {\n let last = n % 10;\n let rest = n / 10;\n let prev = rest % 10;\n 2 <= last && last <= 9 && prev == last - 1 && Self::is_sequential_digits(rest)\n }\n }\n\n pub fn sequential_digits(low: i32, high: i32) -> (result: Vec)\n requires\n 10 <= low <= high <= 1000000000,\n ensures\n forall|i: int| 0 <= i < result.len() ==> low <= #[trigger] result[i] <= high,\n forall|i: int| 0 <= i < result.len() ==> Self::is_sequential_digits(#[trigger] result[i] as int),\n forall|i: int, j: int| 0 <= i < j < result.len() ==> #[trigger] result[i] < #[trigger] result[j],\n forall|x: int| (low <= x <= high && Self::is_sequential_digits(x)) ==> exists|k: int| 0 <= k < result.len() && #[trigger] result[k] == x as i32,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn sequential_digits(low: i32, high: i32) -> Vec\n {\n let candidates = [\n 12i32, 23, 34, 45, 56, 67, 78, 89,\n 123, 234, 345, 456, 567, 678, 789,\n 1234, 2345, 3456, 4567, 5678, 6789,\n 12345, 23456, 34567, 45678, 56789,\n 123456, 234567, 345678, 456789,\n 1234567, 2345678, 3456789,\n 12345678, 23456789,\n 123456789,\n ];\n\n let mut result: Vec = Vec::new();\n let mut i = 0;\n while i < candidates.len()\n {\n let x = candidates[i];\n if low <= x && x <= high {\n result.push(x);\n }\n i += 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub closed spec fn is_sequential_digits(n: int) -> bool\n decreases n\n {\n if n < 10 {\n 1 <= n && n <= 9\n } else {\n let last = n % 10;\n let rest = n / 10;\n let prev = rest % 10;\n 2 <= last && last <= 9 && prev == last - 1 && Self::is_sequential_digits(rest)\n }\n }\n\n pub fn sequential_digits(low: i32, high: i32) -> (result: Vec)\n requires\n 10 <= low <= high <= 1000000000,\n ensures\n forall|i: int| 0 <= i < result.len() ==> low <= #[trigger] result[i] <= high,\n forall|i: int| 0 <= i < result.len() ==> Self::is_sequential_digits(#[trigger] result[i] as int),\n forall|i: int, j: int| 0 <= i < j < result.len() ==> #[trigger] result[i] < #[trigger] result[j],\n forall|x: int| (low <= x <= high && Self::is_sequential_digits(x)) ==> exists|k: int| 0 <= k < result.len() && #[trigger] result[k] == x as i32,\n {\n let candidates = [\n 12i32, 23, 34, 45, 56, 67, 78, 89,\n 123, 234, 345, 456, 567, 678, 789,\n 1234, 2345, 3456, 4567, 5678, 6789,\n 12345, 23456, 34567, 45678, 56789,\n 123456, 234567, 345678, 456789,\n 1234567, 2345678, 3456789,\n 12345678, 23456789,\n 123456789,\n ];\n\n let mut result: Vec = Vec::new();\n let mut i = 0;\n while i < candidates.len()\n {\n let x = candidates[i];\n if low <= x && x <= high {\n result.push(x);\n }\n i += 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub closed spec fn is_sequential_digits(n: int) -> bool\n decreases n\n {\n if n < 10 {\n 1 <= n && n <= 9\n } else {\n let last = n % 10;\n let rest = n / 10;\n let prev = rest % 10;\n 2 <= last && last <= 9 && prev == last - 1 && Self::is_sequential_digits(rest)\n }\n }\n\n proof fn sequential_digits_complete(x: int)\n requires\n 10 <= x <= 1000000000,\n Self::is_sequential_digits(x),\n ensures\n x == 12 || x == 23 || x == 34 || x == 45 || x == 56 || x == 67 || x == 78 || x == 89\n || x == 123 || x == 234 || x == 345 || x == 456 || x == 567 || x == 678 || x == 789\n || x == 1234 || x == 2345 || x == 3456 || x == 4567 || x == 5678 || x == 6789\n || x == 12345 || x == 23456 || x == 34567 || x == 45678 || x == 56789\n || x == 123456 || x == 234567 || x == 345678 || x == 456789\n || x == 1234567 || x == 2345678 || x == 3456789\n || x == 12345678 || x == 23456789\n || x == 123456789,\n decreases x,\n {\n reveal_with_fuel(Solution::is_sequential_digits, 2);\n let r = x / 10;\n let d = x % 10;\n if r < 10 {\n assert(x == 10 * r + d);\n if r == 1 { assert(x == 12); }\n else if r == 2 { assert(x == 23); }\n else if r == 3 { assert(x == 34); }\n else if r == 4 { assert(x == 45); }\n else if r == 5 { assert(x == 56); }\n else if r == 6 { assert(x == 67); }\n else if r == 7 { assert(x == 78); }\n else { assert(r == 8); assert(x == 89); }\n } else {\n Self::sequential_digits_complete(r);\n assert(x == 10 * r + d);\n if r == 12 { assert(x == 123); }\n else if r == 23 { assert(x == 234); }\n else if r == 34 { assert(x == 345); }\n else if r == 45 { assert(x == 456); }\n else if r == 56 { assert(x == 567); }\n else if r == 67 { assert(x == 678); }\n else if r == 78 { assert(x == 789); }\n else if r == 89 { assert(false); }\n else if r == 123 { assert(x == 1234); }\n else if r == 234 { assert(x == 2345); }\n else if r == 345 { assert(x == 3456); }\n else if r == 456 { assert(x == 4567); }\n else if r == 567 { assert(x == 5678); }\n else if r == 678 { assert(x == 6789); }\n else if r == 789 { assert(false); }\n else if r == 1234 { assert(x == 12345); }\n else if r == 2345 { assert(x == 23456); }\n else if r == 3456 { assert(x == 34567); }\n else if r == 4567 { assert(x == 45678); }\n else if r == 5678 { assert(x == 56789); }\n else if r == 6789 { assert(false); }\n else if r == 12345 { assert(x == 123456); }\n else if r == 23456 { assert(x == 234567); }\n else if r == 34567 { assert(x == 345678); }\n else if r == 45678 { assert(x == 456789); }\n else if r == 56789 { assert(false); }\n else if r == 123456 { assert(x == 1234567); }\n else if r == 234567 { assert(x == 2345678); }\n else if r == 345678 { assert(x == 3456789); }\n else if r == 456789 { assert(false); }\n else if r == 1234567 { assert(x == 12345678); }\n else if r == 2345678 { assert(x == 23456789); }\n else if r == 3456789 { assert(false); }\n else if r == 12345678 { assert(x == 123456789); }\n else if r == 23456789 { assert(false); }\n else { assert(r == 123456789); assert(false); }\n }\n }\n\n pub fn sequential_digits(low: i32, high: i32) -> (result: Vec)\n requires\n 10 <= low <= high <= 1000000000,\n ensures\n forall|i: int| 0 <= i < result.len() ==> low <= #[trigger] result[i] <= high,\n forall|i: int| 0 <= i < result.len() ==> Self::is_sequential_digits(#[trigger] result[i] as int),\n forall|i: int, j: int| 0 <= i < j < result.len() ==> #[trigger] result[i] < #[trigger] result[j],\n forall|x: int| (low <= x <= high && Self::is_sequential_digits(x)) ==> exists|k: int| 0 <= k < result.len() && #[trigger] result[k] == x as i32,\n {\n let candidates = [\n 12i32, 23, 34, 45, 56, 67, 78, 89,\n 123, 234, 345, 456, 567, 678, 789,\n 1234, 2345, 3456, 4567, 5678, 6789,\n 12345, 23456, 34567, 45678, 56789,\n 123456, 234567, 345678, 456789,\n 1234567, 2345678, 3456789,\n 12345678, 23456789,\n 123456789,\n ];\n\n proof {\n reveal_with_fuel(Solution::is_sequential_digits, 10);\n\n assert(Self::is_sequential_digits(12));\n assert(Self::is_sequential_digits(23));\n assert(Self::is_sequential_digits(34));\n assert(Self::is_sequential_digits(45));\n assert(Self::is_sequential_digits(56));\n assert(Self::is_sequential_digits(67));\n assert(Self::is_sequential_digits(78));\n assert(Self::is_sequential_digits(89));\n assert(Self::is_sequential_digits(123));\n assert(Self::is_sequential_digits(234));\n assert(Self::is_sequential_digits(345));\n assert(Self::is_sequential_digits(456));\n assert(Self::is_sequential_digits(567));\n assert(Self::is_sequential_digits(678));\n assert(Self::is_sequential_digits(789));\n assert(Self::is_sequential_digits(1234));\n assert(Self::is_sequential_digits(2345));\n assert(Self::is_sequential_digits(3456));\n assert(Self::is_sequential_digits(4567));\n assert(Self::is_sequential_digits(5678));\n assert(Self::is_sequential_digits(6789));\n assert(Self::is_sequential_digits(12345));\n assert(Self::is_sequential_digits(23456));\n assert(Self::is_sequential_digits(34567));\n assert(Self::is_sequential_digits(45678));\n assert(Self::is_sequential_digits(56789));\n assert(Self::is_sequential_digits(123456));\n assert(Self::is_sequential_digits(234567));\n assert(Self::is_sequential_digits(345678));\n assert(Self::is_sequential_digits(456789));\n assert(Self::is_sequential_digits(1234567));\n assert(Self::is_sequential_digits(2345678));\n assert(Self::is_sequential_digits(3456789));\n assert(Self::is_sequential_digits(12345678));\n assert(Self::is_sequential_digits(23456789));\n assert(Self::is_sequential_digits(123456789));\n\n assert(candidates@.len() == 36);\n\n assert forall|c: int| #![auto] 0 <= c < 36 implies\n Self::is_sequential_digits(candidates@[c] as int)\n by {\n if c == 0 { assert(candidates@[0] == 12); }\n else if c == 1 { assert(candidates@[1] == 23); }\n else if c == 2 { assert(candidates@[2] == 34); }\n else if c == 3 { assert(candidates@[3] == 45); }\n else if c == 4 { assert(candidates@[4] == 56); }\n else if c == 5 { assert(candidates@[5] == 67); }\n else if c == 6 { assert(candidates@[6] == 78); }\n else if c == 7 { assert(candidates@[7] == 89); }\n else if c == 8 { assert(candidates@[8] == 123); }\n else if c == 9 { assert(candidates@[9] == 234); }\n else if c == 10 { assert(candidates@[10] == 345); }\n else if c == 11 { assert(candidates@[11] == 456); }\n else if c == 12 { assert(candidates@[12] == 567); }\n else if c == 13 { assert(candidates@[13] == 678); }\n else if c == 14 { assert(candidates@[14] == 789); }\n else if c == 15 { assert(candidates@[15] == 1234); }\n else if c == 16 { assert(candidates@[16] == 2345); }\n else if c == 17 { assert(candidates@[17] == 3456); }\n else if c == 18 { assert(candidates@[18] == 4567); }\n else if c == 19 { assert(candidates@[19] == 5678); }\n else if c == 20 { assert(candidates@[20] == 6789); }\n else if c == 21 { assert(candidates@[21] == 12345); }\n else if c == 22 { assert(candidates@[22] == 23456); }\n else if c == 23 { assert(candidates@[23] == 34567); }\n else if c == 24 { assert(candidates@[24] == 45678); }\n else if c == 25 { assert(candidates@[25] == 56789); }\n else if c == 26 { assert(candidates@[26] == 123456); }\n else if c == 27 { assert(candidates@[27] == 234567); }\n else if c == 28 { assert(candidates@[28] == 345678); }\n else if c == 29 { assert(candidates@[29] == 456789); }\n else if c == 30 { assert(candidates@[30] == 1234567); }\n else if c == 31 { assert(candidates@[31] == 2345678); }\n else if c == 32 { assert(candidates@[32] == 3456789); }\n else if c == 33 { assert(candidates@[33] == 12345678); }\n else if c == 34 { assert(candidates@[34] == 23456789); }\n else { assert(candidates@[35] == 123456789); }\n }\n\n assert forall|c: int, d: int| 0 <= c < d < 36 implies\n candidates@[c] < candidates@[d]\n by {\n }\n }\n\n let mut result: Vec = Vec::new();\n let ghost mut last_idx: int = -1;\n let mut i = 0;\n while i < candidates.len()\n invariant\n 0 <= i <= 36,\n candidates@.len() == 36,\n forall|c: int| 0 <= c < 36 ==> Self::is_sequential_digits(#[trigger] candidates@[c] as int),\n forall|c: int, d: int| 0 <= c < d < 36 ==> #[trigger] candidates@[c] < #[trigger] candidates@[d],\n forall|j: int| 0 <= j < result.len() ==> low <= #[trigger] result[j] <= high,\n forall|j: int| 0 <= j < result.len() ==> Self::is_sequential_digits(#[trigger] result[j] as int),\n forall|j: int, k: int| 0 <= j < k < result.len() ==> #[trigger] result[j] < #[trigger] result[k],\n forall|c: int| (0 <= c < i as int && low <= candidates@[c] && candidates@[c] <= high) ==> exists|k: int| 0 <= k < result.len() && #[trigger] result[k] == candidates@[c],\n -1 <= last_idx < i as int,\n result.len() > 0 ==> 0 <= last_idx && last_idx < 36 && result[result.len() - 1] == candidates@[last_idx],\n decreases 36 - i\n {\n let x = candidates[i];\n if low <= x && x <= high {\n proof {\n assert(i < 36);\n assert(Self::is_sequential_digits(candidates@[i as int] as int));\n assert(x == candidates@[i as int]);\n if result.len() > 0 {\n assert(last_idx >= 0);\n assert(last_idx <= 35);\n assert(result[result.len() - 1] == candidates@[last_idx]);\n assert(last_idx < i as int);\n assert(candidates@[last_idx] < candidates@[i as int]);\n assert forall|j: int| 0 <= j < result.len() implies result[j] < x by {\n if j < result.len() - 1 {\n assert(result[j] < result[result.len() - 1]);\n }\n }\n }\n }\n let ghost old_result_view = result@;\n proof {\n assert forall|c: int| (0 <= c < i as int && low <= candidates@[c] && candidates@[c] <= high) implies exists|k: int| 0 <= k < old_result_view.len() && #[trigger] old_result_view[k] == candidates@[c] by {\n assert(result@[0] == result@[0]); // trigger\n };\n }\n result.push(x);\n proof {\n last_idx = i as int;\n assert(result@ =~= old_result_view.push(x));\n assert forall|c: int| (0 <= c < (i + 1) as int && low <= candidates@[c] && candidates@[c] <= high) implies exists|k: int| 0 <= k < result.len() && #[trigger] result[k] == candidates@[c] by {\n if c == i as int {\n assert(result[result.len() as int - 1] == candidates@[c]);\n } else {\n let kw = choose|k: int| 0 <= k < old_result_view.len() && old_result_view[k] == candidates@[c];\n assert(result[kw] == old_result_view[kw]);\n assert(result[kw] == candidates@[c]);\n }\n };\n }\n }\n i += 1;\n }\n\n proof {\n assert forall|x: int| (low <= x <= high && Self::is_sequential_digits(x)) implies exists|k: int| 0 <= k < result.len() && #[trigger] result[k] == x as i32 by {\n Self::sequential_digits_complete(x);\n if x == 12 { assert(candidates@[0] == 12i32); }\n else if x == 23 { assert(candidates@[1] == 23i32); }\n else if x == 34 { assert(candidates@[2] == 34i32); }\n else if x == 45 { assert(candidates@[3] == 45i32); }\n else if x == 56 { assert(candidates@[4] == 56i32); }\n else if x == 67 { assert(candidates@[5] == 67i32); }\n else if x == 78 { assert(candidates@[6] == 78i32); }\n else if x == 89 { assert(candidates@[7] == 89i32); }\n else if x == 123 { assert(candidates@[8] == 123i32); }\n else if x == 234 { assert(candidates@[9] == 234i32); }\n else if x == 345 { assert(candidates@[10] == 345i32); }\n else if x == 456 { assert(candidates@[11] == 456i32); }\n else if x == 567 { assert(candidates@[12] == 567i32); }\n else if x == 678 { assert(candidates@[13] == 678i32); }\n else if x == 789 { assert(candidates@[14] == 789i32); }\n else if x == 1234 { assert(candidates@[15] == 1234i32); }\n else if x == 2345 { assert(candidates@[16] == 2345i32); }\n else if x == 3456 { assert(candidates@[17] == 3456i32); }\n else if x == 4567 { assert(candidates@[18] == 4567i32); }\n else if x == 5678 { assert(candidates@[19] == 5678i32); }\n else if x == 6789 { assert(candidates@[20] == 6789i32); }\n else if x == 12345 { assert(candidates@[21] == 12345i32); }\n else if x == 23456 { assert(candidates@[22] == 23456i32); }\n else if x == 34567 { assert(candidates@[23] == 34567i32); }\n else if x == 45678 { assert(candidates@[24] == 45678i32); }\n else if x == 56789 { assert(candidates@[25] == 56789i32); }\n else if x == 123456 { assert(candidates@[26] == 123456i32); }\n else if x == 234567 { assert(candidates@[27] == 234567i32); }\n else if x == 345678 { assert(candidates@[28] == 345678i32); }\n else if x == 456789 { assert(candidates@[29] == 456789i32); }\n else if x == 1234567 { assert(candidates@[30] == 1234567i32); }\n else if x == 2345678 { assert(candidates@[31] == 2345678i32); }\n else if x == 3456789 { assert(candidates@[32] == 3456789i32); }\n else if x == 12345678 { assert(candidates@[33] == 12345678i32); }\n else if x == 23456789 { assert(candidates@[34] == 23456789i32); }\n else { assert(x == 123456789); assert(candidates@[35] == 123456789i32); }\n };\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1291/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1291/tests/mutated_testcases.jsonl"} {"problem": "lc1299", "family": "leetcode", "description": "# Replace Elements with Greatest Element on Right Side\n\nGiven an array `arr`, replace every element in that array with the greatest element among the elements to its right, and replace the last element with `-1`.\n\nAfter doing so, return the array.\n\n## Example 1:\n\n> **Input:** arr = [17,18,5,4,6,1]\n> **Output:** [18,6,6,6,1,-1]\n> **Explanation:**\n> - index 0 \u2192 the greatest element to the right of index 0 is index 1 (18).\n> - index 1 \u2192 the greatest element to the right of index 1 is index 4 (6).\n> - index 2 \u2192 the greatest element to the right of index 2 is index 4 (6).\n> - index 3 \u2192 the greatest element to the right of index 3 is index 4 (6).\n> - index 4 \u2192 the greatest element to the right of index 4 is index 5 (1).\n> - index 5 \u2192 there are no elements to the right of index 5, so we put -1.\n\n## Example 2:\n\n> **Input:** arr = [400]\n> **Output:** [-1]\n> **Explanation:** There are no elements to the right of index 0.\n\n## Constraints:\n\n- $1 <= arr.length <= 10^4$\n\n- $1 <= arr[i] <= 10^5$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn replace_elements(arr: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 72.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_max(s: Seq, start: int, end: int) -> int\n decreases end - start,\n {\n if start >= end {\n -1\n } else if end - start == 1 {\n s[start] as int\n } else {\n let rest_max = Self::spec_max(s, start + 1, end);\n if s[start] as int > rest_max { s[start] as int } else { rest_max }\n }\n }\n\n pub fn replace_elements(arr: Vec) -> (result: Vec)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n ensures\n result.len() == arr.len(),\n forall |i: int| 0 <= i < result.len() - 1 ==>\n result[i] as int == Self::spec_max(arr@, i + 1, arr.len() as int),\n arr.len() > 0 ==> result[result.len() - 1] == -1i32,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn replace_elements(arr: Vec) -> Vec {\n let mut result = arr;\n let n = result.len();\n let mut max_right: i32 = -1;\n let mut i: usize = n;\n while i > 0 {\n i = i - 1;\n let current = result[i];\n result[i] = max_right;\n if current > max_right {\n max_right = current;\n }\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_max(s: Seq, start: int, end: int) -> int\n decreases end - start,\n {\n if start >= end {\n -1\n } else if end - start == 1 {\n s[start] as int\n } else {\n let rest_max = Self::spec_max(s, start + 1, end);\n if s[start] as int > rest_max { s[start] as int } else { rest_max }\n }\n }\n\n pub fn replace_elements(arr: Vec) -> (result: Vec)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n ensures\n result.len() == arr.len(),\n forall |i: int| 0 <= i < result.len() - 1 ==>\n result[i] as int == Self::spec_max(arr@, i + 1, arr.len() as int),\n arr.len() > 0 ==> result[result.len() - 1] == -1i32,\n {\n let mut result = arr;\n let n = result.len();\n let mut max_right: i32 = -1;\n let mut i: usize = n;\n\n while i > 0 {\n i = i - 1;\n let current = result[i];\n result.set(i, max_right);\n if current > max_right {\n max_right = current;\n }\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_max(s: Seq, start: int, end: int) -> int\n decreases end - start,\n {\n if start >= end {\n -1\n } else if end - start == 1 {\n s[start] as int\n } else {\n let rest_max = Self::spec_max(s, start + 1, end);\n if s[start] as int > rest_max { s[start] as int } else { rest_max }\n }\n }\n\n proof fn spec_max_bounded(s: Seq, start: int, end: int)\n requires\n 0 <= start,\n end <= s.len(),\n forall |k: int| 0 <= k < s.len() ==> 1 <= #[trigger] s[k] <= 100_000,\n ensures\n -1 <= Self::spec_max(s, start, end) <= 100_000,\n decreases end - start,\n {\n if start >= end {\n } else if end - start == 1 {\n } else {\n Self::spec_max_bounded(s, start + 1, end);\n }\n }\n\n pub fn replace_elements(arr: Vec) -> (result: Vec)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n ensures\n result.len() == arr.len(),\n forall |i: int| 0 <= i < result.len() - 1 ==>\n result[i] as int == Self::spec_max(arr@, i + 1, arr.len() as int),\n arr.len() > 0 ==> result[result.len() - 1] == -1i32,\n {\n let ghost orig = arr@;\n let mut result = arr;\n let n = result.len();\n let mut max_right: i32 = -1;\n let mut i: usize = n;\n\n while i > 0\n invariant\n 0 <= i <= n,\n n == result.len(),\n result.len() == orig.len(),\n orig.len() <= 10_000,\n orig.len() >= 1,\n forall |k: int| 0 <= k < orig.len() ==> 1 <= #[trigger] orig[k] <= 100_000,\n max_right as int == Self::spec_max(orig, i as int, n as int),\n forall |k: int| i as int <= k < n as int - 1 ==>\n result[k] as int == Self::spec_max(orig, k + 1, n as int),\n i < n ==> result[n as int - 1] == -1i32,\n forall |k: int| 0 <= k < i as int ==> result[k] == #[trigger] orig[k],\n -1 <= max_right <= 100_000,\n decreases i,\n {\n i = i - 1;\n let current = result[i];\n\n proof {\n assert(current == orig[i as int]);\n }\n\n result.set(i, max_right);\n\n let ghost old_max = max_right as int;\n\n if current > max_right {\n max_right = current;\n }\n\n proof {\n Self::spec_max_bounded(orig, i as int, n as int);\n }\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1299/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1299/tests/mutated_testcases.jsonl"} {"problem": "lc1300", "family": "leetcode", "description": "# Sum of Mutated Array Closest to Target\n\nGiven an integer array `arr` and a target value `target`, return the integer `value` such that when we change all the integers larger than `value` in the given array to be equal to `value`, the sum of the array gets as close as possible (in absolute difference) to `target`.\n\nIn case of a tie, return the minimum such integer.\n\nNotice that the answer is not necessarily a number from `arr`.\n\n## Example 1:\n\n> **Input:** arr = [4,9,3], target = 10\n> **Output:** 3\n> **Explanation:** When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer.\n\n## Example 2:\n\n> **Input:** arr = [2,3,5], target = 10\n> **Output:** 5\n\n## Example 3:\n\n> **Input:** arr = [60864,25176,27249,21296,20204], target = 56803\n> **Output:** 11361\n\n## Constraints:\n\n- $1 \\leq \\text{arr.length} \\leq 10^4$\n- $1 \\leq \\text{arr}[i], \\text{target} \\leq 10^5$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_best_value(arr: Vec, target: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Binary Search, Sorting\nAcceptance rate: 46.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn min_spec(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn mutated_sum(arr: Seq, value: int) -> int\n decreases arr.len()\n{\n if arr.len() == 0 {\n 0\n } else {\n min_spec(arr[arr.len() - 1] as int, value) + mutated_sum(arr.drop_last(), value)\n }\n}\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\nfn compute_sum(arr: &Vec, value: i32) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 0 <= value <= 100_000,\n ensures\n result as int == mutated_sum(arr@, value as int),\n{\n}\n\nimpl Solution {\n pub fn find_best_value(arr: Vec, target: i32) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 1 <= target <= 100_000,\n ensures\n result >= 0,\n forall |v: int| #![trigger mutated_sum(arr@, v)] v >= 0 ==> abs_diff(mutated_sum(arr@, result as int), target as int) <= abs_diff(mutated_sum(arr@, v), target as int),\n forall |v: int| #![trigger mutated_sum(arr@, v)] (v >= 0 && abs_diff(mutated_sum(arr@, v), target as int) == abs_diff(mutated_sum(arr@, result as int), target as int)) ==> result as int <= v,\n {\n }\n}\n\n}\n", "code": "fn compute_sum(arr: &Vec, value: i32) -> i32 {\n let n = arr.len();\n let mut sum: i32 = 0;\n let mut j: usize = 0;\n while j < n {\n if arr[j] <= value {\n sum = sum + arr[j];\n } else {\n sum = sum + value;\n }\n j = j + 1;\n }\n sum\n}\n\nimpl Solution {\n pub fn find_best_value(arr: Vec, target: i32) -> i32 {\n let n = arr.len();\n let mut max_val: i32 = arr[0];\n let mut i: usize = 1;\n while i < n {\n if arr[i] > max_val {\n max_val = arr[i];\n }\n i = i + 1;\n }\n let total_sum = compute_sum(&arr, max_val);\n if total_sum <= target {\n return max_val;\n }\n let mut lo: i32 = 0;\n let mut hi: i32 = max_val;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let sum_mid = compute_sum(&arr, mid);\n if sum_mid >= target {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n let sum_lo = compute_sum(&arr, lo);\n let sum_prev = compute_sum(&arr, lo - 1);\n if sum_lo - target < target - sum_prev {\n lo\n } else {\n lo - 1\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn min_spec(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn mutated_sum(arr: Seq, value: int) -> int\n decreases arr.len()\n{\n if arr.len() == 0 {\n 0\n } else {\n min_spec(arr[arr.len() - 1] as int, value) + mutated_sum(arr.drop_last(), value)\n }\n}\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\nfn compute_sum(arr: &Vec, value: i32) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 0 <= value <= 100_000,\n ensures\n result as int == mutated_sum(arr@, value as int),\n{\n let n = arr.len();\n let mut sum: i32 = 0;\n let mut j: usize = 0;\n while j < n {\n if arr[j] <= value {\n sum = sum + arr[j];\n } else {\n sum = sum + value;\n }\n j = j + 1;\n }\n sum\n}\n\nimpl Solution {\n pub fn find_best_value(arr: Vec, target: i32) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 1 <= target <= 100_000,\n ensures\n result >= 0,\n forall |v: int| #![trigger mutated_sum(arr@, v)] v >= 0 ==> abs_diff(mutated_sum(arr@, result as int), target as int) <= abs_diff(mutated_sum(arr@, v), target as int),\n forall |v: int| #![trigger mutated_sum(arr@, v)] (v >= 0 && abs_diff(mutated_sum(arr@, v), target as int) == abs_diff(mutated_sum(arr@, result as int), target as int)) ==> result as int <= v,\n {\n let n = arr.len();\n let mut max_val: i32 = arr[0];\n let mut i: usize = 1;\n while i < n {\n if arr[i] > max_val {\n max_val = arr[i];\n }\n i = i + 1;\n }\n let total_sum = compute_sum(&arr, max_val);\n if total_sum <= target {\n return max_val;\n }\n let mut lo: i32 = 0;\n let mut hi: i32 = max_val;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let sum_mid = compute_sum(&arr, mid);\n if sum_mid >= target {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n let sum_lo = compute_sum(&arr, lo);\n let sum_prev = compute_sum(&arr, lo - 1);\n if sum_lo - target < target - sum_prev {\n lo\n } else {\n lo - 1\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn min_spec(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn mutated_sum(arr: Seq, value: int) -> int\n decreases arr.len()\n{\n if arr.len() == 0 {\n 0\n } else {\n min_spec(arr[arr.len() - 1] as int, value) + mutated_sum(arr.drop_last(), value)\n }\n}\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\nproof fn mutated_sum_monotone(arr: Seq, v1: int, v2: int)\n requires v1 <= v2\n ensures mutated_sum(arr, v1) <= mutated_sum(arr, v2)\n decreases arr.len()\n{\n if arr.len() > 0 {\n mutated_sum_monotone(arr.drop_last(), v1, v2);\n }\n}\n\nproof fn mutated_sum_at_zero(arr: Seq)\n requires forall |i: int| 0 <= i < arr.len() ==> arr[i] >= 1\n ensures mutated_sum(arr, 0) == 0\n decreases arr.len()\n{\n if arr.len() > 0 {\n mutated_sum_at_zero(arr.drop_last());\n }\n}\n\nproof fn mutated_sum_constant_above_max(arr: Seq, v1: int, v2: int)\n requires\n v1 <= v2,\n forall |i: int| 0 <= i < arr.len() ==> arr[i] as int <= v1,\n ensures\n mutated_sum(arr, v1) == mutated_sum(arr, v2)\n decreases arr.len()\n{\n if arr.len() > 0 {\n mutated_sum_constant_above_max(arr.drop_last(), v1, v2);\n }\n}\n\nproof fn mutated_sum_strict_monotone(arr: Seq, v: int)\n requires\n arr.len() > 0,\n forall |i: int| 0 <= i < arr.len() ==> arr[i] as int >= 1,\n exists |i: int| 0 <= i < arr.len() && arr[i] as int > v,\n v >= 0,\n ensures\n mutated_sum(arr, v) < mutated_sum(arr, v + 1)\n decreases arr.len()\n{\n let last = (arr.len() - 1) as int;\n if arr[last] as int > v {\n mutated_sum_monotone(arr.drop_last(), v, v + 1);\n } else {\n let witness = choose |i: int| 0 <= i < arr.len() && arr[i] as int > v;\n assert(witness != last);\n assert(arr.drop_last()[witness] == arr[witness]);\n mutated_sum_strict_monotone(arr.drop_last(), v);\n }\n}\n\nfn compute_sum(arr: &Vec, value: i32) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 0 <= value <= 100_000,\n ensures\n result as int == mutated_sum(arr@, value as int),\n{\n let n = arr.len();\n let mut sum: i32 = 0;\n let mut j: usize = 0;\n while j < n\n invariant\n 0 <= j <= n,\n n == arr.len(),\n n <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 0 <= value <= 100_000,\n sum as int == mutated_sum(arr@.subrange(0, j as int), value as int),\n 0 <= sum <= j as int * 100_000,\n decreases n - j,\n {\n proof {\n let s = arr@.subrange(0, (j + 1) as int);\n assert(s.len() == j + 1);\n assert(s[s.len() - 1] == arr@[j as int]);\n assert(s.drop_last() =~= arr@.subrange(0, j as int));\n }\n if arr[j] <= value {\n sum = sum + arr[j];\n } else {\n sum = sum + value;\n }\n j = j + 1;\n }\n proof {\n assert(arr@.subrange(0, n as int) =~= arr@);\n }\n sum\n}\n\nimpl Solution {\n pub fn find_best_value(arr: Vec, target: i32) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100_000,\n 1 <= target <= 100_000,\n ensures\n result >= 0,\n forall |v: int| #![trigger mutated_sum(arr@, v)] v >= 0 ==> abs_diff(mutated_sum(arr@, result as int), target as int) <= abs_diff(mutated_sum(arr@, v), target as int),\n forall |v: int| #![trigger mutated_sum(arr@, v)] (v >= 0 && abs_diff(mutated_sum(arr@, v), target as int) == abs_diff(mutated_sum(arr@, result as int), target as int)) ==> result as int <= v,\n {\n let n = arr.len();\n let mut max_val: i32 = arr[0];\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= n <= 10_000,\n n == arr.len(),\n 1 <= i <= n,\n 1 <= max_val <= 100_000,\n forall |j: int| 0 <= j < i as int ==> #[trigger] arr[j] <= max_val,\n exists |j: int| 0 <= j < i as int && arr[j] == max_val,\n forall |j: int| 0 <= j < arr.len() ==> 1 <= #[trigger] arr[j] <= 100_000,\n decreases n - i,\n {\n if arr[i] > max_val {\n max_val = arr[i];\n }\n i = i + 1;\n }\n\n let total_sum = compute_sum(&arr, max_val);\n\n if total_sum <= target {\n proof {\n assert forall |v: int| #![trigger mutated_sum(arr@, v)] v >= 0 implies abs_diff(mutated_sum(arr@, max_val as int), target as int) <= abs_diff(mutated_sum(arr@, v), target as int) by {\n if v <= max_val as int {\n mutated_sum_monotone(arr@, v, max_val as int);\n } else {\n mutated_sum_constant_above_max(arr@, max_val as int, v);\n }\n };\n assert forall |v: int| #![trigger mutated_sum(arr@, v)] (v >= 0 && abs_diff(mutated_sum(arr@, v), target as int) == abs_diff(mutated_sum(arr@, max_val as int), target as int)) implies max_val as int <= v by {\n if v < max_val as int {\n let w = choose |j: int| 0 <= j < arr@.len() && arr@[j] == max_val;\n assert(arr@[w] as int > v);\n mutated_sum_strict_monotone(arr@, v);\n if v + 1 < max_val as int {\n mutated_sum_monotone(arr@, v + 1, max_val as int);\n }\n }\n };\n }\n return max_val;\n }\n\n let mut lo: i32 = 0;\n let mut hi: i32 = max_val;\n while lo < hi\n invariant\n 0 <= lo <= hi <= max_val as int,\n lo as int > 0 ==> mutated_sum(arr@, lo as int - 1) < target as int,\n mutated_sum(arr@, hi as int) >= target as int,\n 1 <= arr.len() <= 10_000,\n 1 <= max_val <= 100_000,\n forall |j: int| 0 <= j < arr.len() ==> 1 <= #[trigger] arr[j] <= 100_000,\n forall |j: int| 0 <= j < arr.len() ==> arr[j] <= max_val,\n exists |j: int| 0 <= j < arr.len() && arr[j] == max_val,\n 1 <= target <= 100_000,\n decreases hi - lo,\n {\n let mid = lo + (hi - lo) / 2;\n let sum_mid = compute_sum(&arr, mid);\n if sum_mid >= target {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n\n proof {\n mutated_sum_at_zero(arr@);\n }\n assert(lo >= 1);\n\n let sum_lo = compute_sum(&arr, lo);\n let sum_prev = compute_sum(&arr, lo - 1);\n\n proof {\n mutated_sum_monotone(arr@, 0, lo as int - 1);\n }\n\n if sum_lo - target < target - sum_prev {\n proof {\n assert forall |v: int| #![trigger mutated_sum(arr@, v)] v >= 0 implies abs_diff(mutated_sum(arr@, lo as int), target as int) <= abs_diff(mutated_sum(arr@, v), target as int) by {\n if v >= lo as int {\n mutated_sum_monotone(arr@, lo as int, v);\n } else {\n mutated_sum_monotone(arr@, v, lo as int - 1);\n }\n };\n assert forall |v: int| #![trigger mutated_sum(arr@, v)] (v >= 0 && abs_diff(mutated_sum(arr@, v), target as int) == abs_diff(mutated_sum(arr@, lo as int), target as int)) implies lo as int <= v by {\n if v < lo as int {\n mutated_sum_monotone(arr@, v, lo as int - 1);\n }\n };\n }\n lo\n } else {\n proof {\n assert forall |v: int| #![trigger mutated_sum(arr@, v)] v >= 0 implies abs_diff(mutated_sum(arr@, lo as int - 1), target as int) <= abs_diff(mutated_sum(arr@, v), target as int) by {\n if v >= lo as int {\n mutated_sum_monotone(arr@, lo as int, v);\n } else {\n mutated_sum_monotone(arr@, v, lo as int - 1);\n }\n };\n assert forall |v: int| #![trigger mutated_sum(arr@, v)] (v >= 0 && abs_diff(mutated_sum(arr@, v), target as int) == abs_diff(mutated_sum(arr@, lo as int - 1), target as int)) implies lo as int - 1 <= v by {\n if v < lo as int - 1 {\n let w = choose |j: int| 0 <= j < arr@.len() && arr@[j] == max_val;\n assert(arr@[w] as int > v);\n mutated_sum_strict_monotone(arr@, v);\n if v + 1 < lo as int - 1 {\n mutated_sum_monotone(arr@, v + 1, lo as int - 1);\n }\n }\n };\n }\n lo - 1\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1300/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1300/tests/mutated_testcases.jsonl"} {"problem": "lc1314", "family": "leetcode", "description": "# Matrix Block Sum\n\nGiven a `m x n` matrix `mat` and an integer `k`, return a matrix `answer` where each `answer[i][j]` is the sum of all elements `mat[r][c]` for:\n\n- `i - k <= r <= i + k`,\n- `j - k <= c <= j + k`, and\n- `(r, c)` is a valid position in the matrix.\n\n## Example 1:\n\n> **Input:** mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1\n> **Output:** [[12,21,16],[27,45,33],[24,39,28]]\n\n## Example 2:\n\n> **Input:** mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2\n> **Output:** [[45,45,45],[45,45,45],[45,45,45]]\n\n## Constraints:\n\n- $m == mat.length$\n- $n == mat[i].length$\n- $1 <= m, n, k <= 100$\n- $1 <= mat[i][j] <= 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn matrix_block_sum(mat: Vec>, k: i32) -> Vec> {\n \n }\n}\n```\n", "tags": "Medium\nArray, Matrix, Prefix Sum\nAcceptance rate: 76.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn row_sum_in(mat: Seq>, r: int, c1: int, c2: int) -> int\n decreases c2 - c1\n {\n if c1 >= c2 {\n 0\n } else {\n mat[r][c1] as int + Self::row_sum_in(mat, r, c1 + 1, c2)\n }\n }\n\n pub open spec fn rect_sum(mat: Seq>, r1: int, r2: int, c1: int, c2: int) -> int\n decreases r2 - r1\n {\n if r1 >= r2 {\n 0\n } else {\n Self::row_sum_in(mat, r1, c1, c2) + Self::rect_sum(mat, r1 + 1, r2, c1, c2)\n }\n }\n\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a > b { a } else { b }\n }\n\n pub open spec fn spec_min(a: int, b: int) -> int {\n if a < b { a } else { b }\n }\n\n pub open spec fn block_sum(mat: Seq>, i: int, j: int, k: int, m: int, n: int) -> int {\n Self::rect_sum(\n mat,\n Self::spec_max(0, i - k),\n Self::spec_min(m, i + k + 1),\n Self::spec_max(0, j - k),\n Self::spec_min(n, j + k + 1),\n )\n }\n\n pub fn matrix_block_sum(mat: Vec>, k: i32) -> (answer: Vec>)\n requires\n 1 <= mat.len() <= 100,\n 1 <= mat[0].len() <= 100,\n forall |i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==>\n 1 <= #[trigger] mat[i][j] <= 100,\n 1 <= k <= 100,\n ensures\n answer.len() == mat.len(),\n forall |i: int| 0 <= i < answer.len() ==> #[trigger] answer[i].len() == mat[0].len(),\n forall |i: int, j: int| 0 <= i < answer.len() && 0 <= j < mat[0].len() ==>\n (#[trigger] answer[i][j]) as int == Self::block_sum(\n mat@, i, j, k as int, mat.len() as int, mat[0].len() as int,\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn matrix_block_sum(mat: Vec>, k: i32) -> Vec> {\n let m = mat.len();\n let n = mat[0].len();\n let ku = k as usize;\n\n let mut prefix: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let above: i32 = if i > 0 { prefix[i - 1][j] } else { 0 };\n let left: i32 = if j > 0 { row[j - 1] } else { 0 };\n let diag: i32 = if i > 0 && j > 0 { prefix[i - 1][j - 1] } else { 0 };\n let val: i32 = mat[i][j] + above + left - diag;\n row.push(val);\n j += 1;\n }\n prefix.push(row);\n i += 1;\n }\n\n let mut answer: Vec> = Vec::new();\n i = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let r1: usize = if i >= ku { i - ku } else { 0 };\n let r2: usize = if i + ku < m { i + ku } else { m - 1 };\n let c1: usize = if j >= ku { j - ku } else { 0 };\n let c2: usize = if j + ku < n { j + ku } else { n - 1 };\n\n let top_right: i32 = if r1 > 0 { prefix[r1 - 1][c2] } else { 0 };\n let bottom_left: i32 = if c1 > 0 { prefix[r2][c1 - 1] } else { 0 };\n let top_left: i32 = if r1 > 0 && c1 > 0 { prefix[r1 - 1][c1 - 1] } else { 0 };\n let val: i32 = prefix[r2][c2] - top_right - bottom_left + top_left;\n row.push(val);\n j += 1;\n }\n answer.push(row);\n i += 1;\n }\n\n answer\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn row_sum_in(mat: Seq>, r: int, c1: int, c2: int) -> int\n decreases c2 - c1\n {\n if c1 >= c2 {\n 0\n } else {\n mat[r][c1] as int + Self::row_sum_in(mat, r, c1 + 1, c2)\n }\n }\n\n pub open spec fn rect_sum(mat: Seq>, r1: int, r2: int, c1: int, c2: int) -> int\n decreases r2 - r1\n {\n if r1 >= r2 {\n 0\n } else {\n Self::row_sum_in(mat, r1, c1, c2) + Self::rect_sum(mat, r1 + 1, r2, c1, c2)\n }\n }\n\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a > b { a } else { b }\n }\n\n pub open spec fn spec_min(a: int, b: int) -> int {\n if a < b { a } else { b }\n }\n\n pub open spec fn block_sum(mat: Seq>, i: int, j: int, k: int, m: int, n: int) -> int {\n Self::rect_sum(\n mat,\n Self::spec_max(0, i - k),\n Self::spec_min(m, i + k + 1),\n Self::spec_max(0, j - k),\n Self::spec_min(n, j + k + 1),\n )\n }\n\n pub fn matrix_block_sum(mat: Vec>, k: i32) -> (answer: Vec>)\n requires\n 1 <= mat.len() <= 100,\n 1 <= mat[0].len() <= 100,\n forall |i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==>\n 1 <= #[trigger] mat[i][j] <= 100,\n 1 <= k <= 100,\n ensures\n answer.len() == mat.len(),\n forall |i: int| 0 <= i < answer.len() ==> #[trigger] answer[i].len() == mat[0].len(),\n forall |i: int, j: int| 0 <= i < answer.len() && 0 <= j < mat[0].len() ==>\n (#[trigger] answer[i][j]) as int == Self::block_sum(\n mat@, i, j, k as int, mat.len() as int, mat[0].len() as int,\n ),\n {\n let m = mat.len();\n let n = mat[0].len();\n let ku = k as usize;\n\n let mut prefix: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let above: i32 = if i > 0 { prefix[i - 1][j] } else { 0 };\n let left: i32 = if j > 0 { row[j - 1] } else { 0 };\n let diag: i32 = if i > 0 && j > 0 { prefix[i - 1][j - 1] } else { 0 };\n let val: i32 = mat[i][j] + above + left - diag;\n row.push(val);\n j += 1;\n }\n prefix.push(row);\n i += 1;\n }\n\n let mut answer: Vec> = Vec::new();\n i = 0;\n while i < m {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let r1: usize = if i >= ku { i - ku } else { 0 };\n let r2: usize = if i + ku < m { i + ku } else { m - 1 };\n let c1: usize = if j >= ku { j - ku } else { 0 };\n let c2: usize = if j + ku < n { j + ku } else { n - 1 };\n\n let top_right: i32 = if r1 > 0 { prefix[r1 - 1][c2] } else { 0 };\n let bottom_left: i32 = if c1 > 0 { prefix[r2][c1 - 1] } else { 0 };\n let top_left: i32 = if r1 > 0 && c1 > 0 { prefix[r1 - 1][c1 - 1] } else { 0 };\n let val: i32 = prefix[r2][c2] - top_right - bottom_left + top_left;\n row.push(val);\n j += 1;\n }\n answer.push(row);\n i += 1;\n }\n\n answer\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn row_sum_in(mat: Seq>, r: int, c1: int, c2: int) -> int\n decreases c2 - c1\n {\n if c1 >= c2 {\n 0\n } else {\n mat[r][c1] as int + Self::row_sum_in(mat, r, c1 + 1, c2)\n }\n }\n\n pub open spec fn rect_sum(mat: Seq>, r1: int, r2: int, c1: int, c2: int) -> int\n decreases r2 - r1\n {\n if r1 >= r2 {\n 0\n } else {\n Self::row_sum_in(mat, r1, c1, c2) + Self::rect_sum(mat, r1 + 1, r2, c1, c2)\n }\n }\n\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a > b { a } else { b }\n }\n\n pub open spec fn spec_min(a: int, b: int) -> int {\n if a < b { a } else { b }\n }\n\n pub open spec fn block_sum(mat: Seq>, i: int, j: int, k: int, m: int, n: int) -> int {\n Self::rect_sum(\n mat,\n Self::spec_max(0, i - k),\n Self::spec_min(m, i + k + 1),\n Self::spec_max(0, j - k),\n Self::spec_min(n, j + k + 1),\n )\n }\n\n proof fn lemma_row_sum_additive(mat: Seq>, r: int, a: int, b: int, c: int)\n requires\n 0 <= r < mat.len(),\n 0 <= a <= b <= c <= mat[r].len(),\n ensures\n Self::row_sum_in(mat, r, a, c) == Self::row_sum_in(mat, r, a, b) + Self::row_sum_in(mat, r, b, c),\n decreases c - a,\n {\n if a >= b {\n } else {\n Self::lemma_row_sum_additive(mat, r, a + 1, b, c);\n }\n }\n\n proof fn lemma_rect_sum_row_split(mat: Seq>, a: int, b: int, c: int, c1: int, c2: int)\n requires\n 0 <= a <= b <= c <= mat.len(),\n 0 <= c1 <= c2,\n forall |i: int| a <= i < c ==> c2 <= #[trigger] mat[i].len(),\n ensures\n Self::rect_sum(mat, a, c, c1, c2) == Self::rect_sum(mat, a, b, c1, c2) + Self::rect_sum(mat, b, c, c1, c2),\n decreases c - a,\n {\n if a >= b {\n } else {\n Self::lemma_rect_sum_row_split(mat, a + 1, b, c, c1, c2);\n }\n }\n\n proof fn lemma_rect_sum_col_split(mat: Seq>, r1: int, r2: int, a: int, b: int, c: int)\n requires\n 0 <= r1 <= r2 <= mat.len(),\n 0 <= a <= b <= c,\n forall |i: int| r1 <= i < r2 ==> c <= #[trigger] mat[i].len(),\n ensures\n Self::rect_sum(mat, r1, r2, a, c) == Self::rect_sum(mat, r1, r2, a, b) + Self::rect_sum(mat, r1, r2, b, c),\n decreases r2 - r1,\n {\n if r1 >= r2 {\n } else {\n Self::lemma_row_sum_additive(mat, r1, a, b, c);\n Self::lemma_rect_sum_col_split(mat, r1 + 1, r2, a, b, c);\n }\n }\n\n proof fn lemma_row_sum_bounds(mat: Seq>, r: int, c1: int, c2: int)\n requires\n 0 <= r < mat.len(),\n 0 <= c1 <= c2 <= mat[r].len(),\n forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 100,\n ensures\n 0 <= Self::row_sum_in(mat, r, c1, c2) <= (c2 - c1) * 100,\n decreases c2 - c1,\n {\n if c1 >= c2 {\n } else {\n Self::lemma_row_sum_bounds(mat, r, c1 + 1, c2);\n assert(1 <= mat[r][c1] <= 100);\n assert((c2 - c1 - 1) * 100 + 100 == (c2 - c1) * 100) by (nonlinear_arith);\n }\n }\n\n proof fn lemma_rect_sum_bounds(mat: Seq>, r1: int, r2: int, c1: int, c2: int)\n requires\n 0 <= r1 <= r2 <= mat.len(),\n 0 <= c1 <= c2,\n forall |i: int| r1 <= i < r2 ==> c2 <= #[trigger] mat[i].len(),\n forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 100,\n ensures\n 0 <= Self::rect_sum(mat, r1, r2, c1, c2) <= (r2 - r1) * (c2 - c1) * 100,\n decreases r2 - r1,\n {\n if r1 >= r2 {\n } else {\n Self::lemma_row_sum_bounds(mat, r1, c1, c2);\n Self::lemma_rect_sum_bounds(mat, r1 + 1, r2, c1, c2);\n assert((r2 - r1 - 1) * (c2 - c1) * 100 + (c2 - c1) * 100 == (r2 - r1) * (c2 - c1) * 100) by (nonlinear_arith);\n }\n }\n\n proof fn lemma_rect_sum_zero_cols(mat: Seq>, r1: int, r2: int, c: int)\n requires\n 0 <= r1 <= r2 <= mat.len(),\n 0 <= c,\n ensures\n Self::rect_sum(mat, r1, r2, c, c) == 0,\n decreases r2 - r1,\n {\n if r1 >= r2 {\n } else {\n Self::lemma_rect_sum_zero_cols(mat, r1 + 1, r2, c);\n }\n }\n\n proof fn lemma_prefix_recurrence(mat: Seq>, i: int, j: int)\n requires\n 0 <= i < mat.len(),\n 0 <= j < mat[i].len(),\n forall |ii: int| 0 <= ii <= i ==> j < #[trigger] mat[ii].len(),\n ensures\n Self::rect_sum(mat, 0, i + 1, 0, j + 1) ==\n Self::rect_sum(mat, 0, i, 0, j + 1) +\n Self::rect_sum(mat, 0, i + 1, 0, j) -\n Self::rect_sum(mat, 0, i, 0, j) +\n mat[i][j] as int,\n {\n Self::lemma_rect_sum_row_split(mat, 0, i, i + 1, 0, j + 1);\n assert(Self::rect_sum(mat, i + 1, i + 1, 0, j + 1) == 0int);\n assert(Self::rect_sum(mat, i, i + 1, 0, j + 1) == Self::row_sum_in(mat, i, 0, j + 1));\n\n Self::lemma_row_sum_additive(mat, i, 0, j, j + 1);\n assert(Self::row_sum_in(mat, i, j + 1, j + 1) == 0int);\n assert(Self::row_sum_in(mat, i, j, j + 1) == mat[i][j] as int);\n\n Self::lemma_rect_sum_row_split(mat, 0, i, i + 1, 0, j);\n assert(Self::rect_sum(mat, i + 1, i + 1, 0, j) == 0int);\n assert(Self::rect_sum(mat, i, i + 1, 0, j) == Self::row_sum_in(mat, i, 0, j));\n }\n\n proof fn lemma_inclusion_exclusion(mat: Seq>, r1: int, r2: int, c1: int, c2: int)\n requires\n 0 <= r1 <= r2 <= mat.len(),\n 0 <= c1 <= c2,\n forall |i: int| 0 <= i < r2 ==> c2 <= #[trigger] mat[i].len(),\n ensures\n Self::rect_sum(mat, r1, r2, c1, c2) ==\n Self::rect_sum(mat, 0, r2, 0, c2) -\n Self::rect_sum(mat, 0, r1, 0, c2) -\n Self::rect_sum(mat, 0, r2, 0, c1) +\n Self::rect_sum(mat, 0, r1, 0, c1),\n {\n Self::lemma_rect_sum_row_split(mat, 0, r1, r2, 0, c2);\n Self::lemma_rect_sum_row_split(mat, 0, r1, r2, 0, c1);\n Self::lemma_rect_sum_col_split(mat, r1, r2, 0, c1, c2);\n }\n\n pub fn matrix_block_sum(mat: Vec>, k: i32) -> (answer: Vec>)\n requires\n 1 <= mat.len() <= 100,\n 1 <= mat[0].len() <= 100,\n forall |i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==>\n 1 <= #[trigger] mat[i][j] <= 100,\n 1 <= k <= 100,\n ensures\n answer.len() == mat.len(),\n forall |i: int| 0 <= i < answer.len() ==> #[trigger] answer[i].len() == mat[0].len(),\n forall |i: int, j: int| 0 <= i < answer.len() && 0 <= j < mat[0].len() ==>\n (#[trigger] answer[i][j]) as int == Self::block_sum(\n mat@, i, j, k as int, mat.len() as int, mat[0].len() as int,\n ),\n {\n let m = mat.len();\n let n = mat[0].len();\n let ku = k as usize;\n\n let mut prefix: Vec> = Vec::new();\n let mut i: usize = 0;\n while i < m\n invariant\n m == mat.len(),\n n == mat[0].len(),\n 1 <= m <= 100,\n 1 <= n <= 100,\n ku == k as usize,\n 1 <= k <= 100,\n 0 <= i <= m,\n prefix@.len() == i as int,\n forall |ii: int| 0 <= ii < mat.len() ==> #[trigger] mat[ii].len() == n,\n forall |ii: int, jj: int| 0 <= ii < mat.len() && 0 <= jj < n as int ==>\n 1 <= #[trigger] mat[ii][jj] <= 100,\n forall |p: int| 0 <= p < i as int ==> #[trigger] prefix[p].len() == n,\n forall |p: int, q: int| 0 <= p < i as int && 0 <= q < n as int ==>\n (#[trigger] prefix[p][q]) as int == Self::rect_sum(mat@, 0, p + 1, 0, q + 1),\n forall |p: int, q: int| 0 <= p < i as int && 0 <= q < n as int ==>\n 0 <= #[trigger] prefix[p][q] <= 1_000_000,\n decreases m - i,\n {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n\n invariant\n m == mat.len(),\n n == mat[0].len(),\n 1 <= m <= 100,\n 1 <= n <= 100,\n 0 <= i < m,\n 0 <= j <= n,\n prefix@.len() == i as int,\n row@.len() == j as int,\n forall |ii: int| 0 <= ii < mat.len() ==> #[trigger] mat[ii].len() == n,\n forall |ii: int, jj: int| 0 <= ii < mat.len() && 0 <= jj < n as int ==>\n 1 <= #[trigger] mat[ii][jj] <= 100,\n forall |p: int| 0 <= p < i as int ==> #[trigger] prefix[p].len() == n,\n forall |p: int, q: int| 0 <= p < i as int && 0 <= q < n as int ==>\n (#[trigger] prefix[p][q]) as int == Self::rect_sum(mat@, 0, p + 1, 0, q + 1),\n forall |p: int, q: int| 0 <= p < i as int && 0 <= q < n as int ==>\n 0 <= #[trigger] prefix[p][q] <= 1_000_000,\n forall |q: int| 0 <= q < j as int ==>\n (#[trigger] row@[q]) as int == Self::rect_sum(mat@, 0, (i as int) + 1, 0, q + 1),\n forall |q: int| 0 <= q < j as int ==>\n 0 <= #[trigger] row@[q] <= 1_000_000,\n decreases n - j,\n {\n proof {\n assert(mat[i as int].len() == n);\n if i > 0 {\n assert(prefix[(i - 1) as int].len() == n);\n }\n }\n let above: i32 = if i > 0 { prefix[i - 1][j] } else { 0 };\n let left: i32 = if j > 0 { row[j - 1] } else { 0 };\n let diag: i32 = if i > 0 && j > 0 { prefix[i - 1][j - 1] } else { 0 };\n\n proof {\n if i > 0 {\n assert(above as int == Self::rect_sum(mat@, 0, i as int, 0, (j as int) + 1));\n assert(0 <= above <= 1_000_000);\n } else {\n assert(above == 0int);\n }\n\n if j > 0 {\n assert(left as int == Self::rect_sum(mat@, 0, (i as int) + 1, 0, j as int));\n assert(0 <= left <= 1_000_000);\n } else {\n assert(left == 0int);\n Self::lemma_rect_sum_zero_cols(mat@, 0, (i as int) + 1, 0);\n }\n\n if i > 0 && j > 0 {\n assert(diag as int == Self::rect_sum(mat@, 0, i as int, 0, j as int));\n assert(0 <= diag <= 1_000_000);\n } else if i == 0 {\n assert(diag == 0int);\n } else {\n assert(diag == 0int);\n Self::lemma_rect_sum_zero_cols(mat@, 0, i as int, 0);\n }\n\n Self::lemma_prefix_recurrence(mat@, i as int, j as int);\n Self::lemma_rect_sum_bounds(mat@, 0, (i as int) + 1, 0, (j as int) + 1);\n assert(((i as int) + 1) * ((j as int) + 1) * 100 <= 1_000_000) by (nonlinear_arith)\n requires i < 100, j < 100;\n assert(1 <= mat[i as int][j as int] <= 100);\n }\n\n let val: i32 = mat[i][j] + above + left - diag;\n\n proof {\n assert(val as int == Self::rect_sum(mat@, 0, (i as int) + 1, 0, (j as int) + 1));\n assert(0 <= val <= 1_000_000);\n }\n\n row.push(val);\n j += 1;\n }\n\n let ghost row_view: Seq = row@;\n prefix.push(row);\n\n proof {\n assert(prefix@.len() == (i + 1) as int);\n assert(prefix@[i as int]@ =~= row_view);\n assert(prefix[i as int].len() == n) by {\n assert(prefix@[i as int]@.len() == row_view.len());\n };\n assert forall |q: int| 0 <= q < n as int implies\n (#[trigger] prefix[i as int][q]) as int == Self::rect_sum(mat@, 0, (i as int) + 1, 0, q + 1) by {\n assert(prefix@[i as int]@[q] == row_view[q]);\n };\n assert forall |q: int| 0 <= q < n as int implies\n 0 <= #[trigger] prefix[i as int][q] <= 1_000_000 by {\n assert(prefix@[i as int]@[q] == row_view[q]);\n };\n assert forall |p: int| 0 <= p < (i + 1) as int implies\n #[trigger] prefix[p].len() == n by {\n if p < i as int {\n } else {\n assert(p == i as int);\n }\n };\n }\n\n i += 1;\n }\n\n let mut answer: Vec> = Vec::new();\n i = 0;\n while i < m\n invariant\n m == mat.len(),\n n == mat[0].len(),\n 1 <= m <= 100,\n 1 <= n <= 100,\n ku == k as usize,\n 1 <= k <= 100,\n 0 <= i <= m,\n prefix@.len() == m as int,\n answer@.len() == i as int,\n forall |ii: int| 0 <= ii < mat.len() ==> #[trigger] mat[ii].len() == n,\n forall |ii: int, jj: int| 0 <= ii < mat.len() && 0 <= jj < n as int ==>\n 1 <= #[trigger] mat[ii][jj] <= 100,\n forall |p: int| 0 <= p < m as int ==> #[trigger] prefix[p].len() == n,\n forall |p: int, q: int| 0 <= p < m as int && 0 <= q < n as int ==>\n (#[trigger] prefix[p][q]) as int == Self::rect_sum(mat@, 0, p + 1, 0, q + 1),\n forall |p: int, q: int| 0 <= p < m as int && 0 <= q < n as int ==>\n 0 <= #[trigger] prefix[p][q] <= 1_000_000,\n forall |p: int| 0 <= p < i as int ==> #[trigger] answer[p].len() == n,\n forall |p: int, q: int| 0 <= p < i as int && 0 <= q < n as int ==>\n (#[trigger] answer[p][q]) as int == Self::block_sum(\n mat@, p, q, k as int, m as int, n as int,\n ),\n decreases m - i,\n {\n let mut row: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n\n invariant\n m == mat.len(),\n n == mat[0].len(),\n 1 <= m <= 100,\n 1 <= n <= 100,\n ku == k as usize,\n 1 <= k <= 100,\n 0 <= i < m,\n 0 <= j <= n,\n prefix@.len() == m as int,\n row@.len() == j as int,\n forall |ii: int| 0 <= ii < mat.len() ==> #[trigger] mat[ii].len() == n,\n forall |ii: int, jj: int| 0 <= ii < mat.len() && 0 <= jj < n as int ==>\n 1 <= #[trigger] mat[ii][jj] <= 100,\n forall |p: int| 0 <= p < m as int ==> #[trigger] prefix[p].len() == n,\n forall |p: int, q: int| 0 <= p < m as int && 0 <= q < n as int ==>\n (#[trigger] prefix[p][q]) as int == Self::rect_sum(mat@, 0, p + 1, 0, q + 1),\n forall |p: int, q: int| 0 <= p < m as int && 0 <= q < n as int ==>\n 0 <= #[trigger] prefix[p][q] <= 1_000_000,\n forall |q: int| 0 <= q < j as int ==>\n (#[trigger] row@[q]) as int == Self::block_sum(\n mat@, i as int, q, k as int, m as int, n as int,\n ),\n decreases n - j,\n {\n let r1: usize = if i >= ku { i - ku } else { 0 };\n let r2: usize = if i + ku < m { i + ku } else { m - 1 };\n let c1: usize = if j >= ku { j - ku } else { 0 };\n let c2: usize = if j + ku < n { j + ku } else { n - 1 };\n\n proof {\n assert(prefix[r2 as int].len() == n);\n if r1 > 0 {\n assert(prefix[(r1 - 1) as int].len() == n);\n }\n }\n\n let top_right: i32 = if r1 > 0 { prefix[r1 - 1][c2] } else { 0 };\n let bottom_left: i32 = if c1 > 0 { prefix[r2][c1 - 1] } else { 0 };\n let top_left: i32 = if r1 > 0 && c1 > 0 { prefix[r1 - 1][c1 - 1] } else { 0 };\n\n proof {\n let ghost full_val = prefix[r2 as int][c2 as int] as int;\n assert(full_val == Self::rect_sum(mat@, 0, (r2 as int) + 1, 0, (c2 as int) + 1));\n assert(0 <= full_val <= 1_000_000);\n\n if r1 > 0 {\n assert(top_right as int == Self::rect_sum(mat@, 0, r1 as int, 0, (c2 as int) + 1));\n assert(0 <= top_right <= 1_000_000);\n } else {\n assert(top_right == 0int);\n }\n\n if c1 > 0 {\n assert(bottom_left as int == Self::rect_sum(mat@, 0, (r2 as int) + 1, 0, c1 as int));\n assert(0 <= bottom_left <= 1_000_000);\n } else {\n assert(bottom_left == 0int);\n Self::lemma_rect_sum_zero_cols(mat@, 0, (r2 as int) + 1, 0);\n }\n\n if r1 > 0 && c1 > 0 {\n assert(top_left as int == Self::rect_sum(mat@, 0, r1 as int, 0, c1 as int));\n assert(0 <= top_left <= 1_000_000);\n } else if r1 == 0 {\n assert(top_left == 0int);\n } else {\n assert(top_left == 0int);\n Self::lemma_rect_sum_zero_cols(mat@, 0, r1 as int, 0);\n }\n\n Self::lemma_inclusion_exclusion(mat@, r1 as int, (r2 as int) + 1, c1 as int, (c2 as int) + 1);\n\n assert(r1 as int == Self::spec_max(0, i as int - k as int));\n assert((r2 as int) + 1 == Self::spec_min(m as int, i as int + k as int + 1));\n assert(c1 as int == Self::spec_max(0, j as int - k as int));\n assert((c2 as int) + 1 == Self::spec_min(n as int, j as int + k as int + 1));\n\n Self::lemma_rect_sum_bounds(mat@, r1 as int, (r2 as int) + 1, c1 as int, (c2 as int) + 1);\n assert(r1 as int <= r2 as int);\n assert(c1 as int <= c2 as int);\n let ghost dr: int = (r2 as int) + 1 - r1 as int;\n let ghost dc: int = (c2 as int) + 1 - c1 as int;\n assert(1 <= dr && dr <= 100);\n assert(1 <= dc && dc <= 100);\n assert(dr * dc <= 10000) by (nonlinear_arith)\n requires 1 <= dr, dr <= 100, 1 <= dc, dc <= 100;\n assert(dr * dc * 100 <= 1_000_000) by (nonlinear_arith)\n requires dr * dc <= 10000;\n }\n\n let val: i32 = prefix[r2][c2] - top_right - bottom_left + top_left;\n\n proof {\n assert(val as int == Self::block_sum(\n mat@, i as int, j as int, k as int, m as int, n as int,\n ));\n }\n\n row.push(val);\n j += 1;\n }\n\n let ghost row_view: Seq = row@;\n answer.push(row);\n\n proof {\n assert(answer@.len() == (i + 1) as int);\n assert(answer@[i as int]@ =~= row_view);\n assert(answer[i as int].len() == n) by {\n assert(answer@[i as int]@.len() == row_view.len());\n };\n assert forall |q: int| 0 <= q < n as int implies\n (#[trigger] answer[i as int][q]) as int == Self::block_sum(\n mat@, i as int, q, k as int, m as int, n as int,\n ) by {\n assert(answer@[i as int]@[q] == row_view[q]);\n };\n assert forall |p: int| 0 <= p < (i + 1) as int implies\n #[trigger] answer[p].len() == n by {\n if p < i as int {\n } else {\n assert(p == i as int);\n }\n };\n }\n\n i += 1;\n }\n\n answer\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1314/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1314/tests/mutated_testcases.jsonl"} {"problem": "lc1318", "family": "leetcode", "description": "# Minimum Flips to Make a OR b Equal to c\n\nGiven 3 positive numbers `a`, `b` and `c`, return the minimum flips required in some bits of `a` and `b` to make `a OR b == c` (bitwise OR operation).\n\nA flip operation consists of changing **any** single bit 1 to 0 or changing the bit 0 to 1 in their binary representation.\n\n## Example 1:\n\n> **Input:** a = 2, b = 6, c = 5\n> **Output:** 3\n> **Explanation:** After flips a = 1, b = 4, c = 5 such that (a OR b == c)\n\n## Example 2:\n\n> **Input:** a = 4, b = 2, c = 7\n> **Output:** 1\n\n## Example 3:\n\n> **Input:** a = 1, b = 2, c = 3\n> **Output:** 0\n\n## Constraints:\n\n- $1 \\leq a \\leq 10^9$\n- $1 \\leq b \\leq 10^9$\n- $1 \\leq c \\leq 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_flips(a: i32, b: i32, c: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nBit Manipulation\nAcceptance rate: 71.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn flips_at_bit(a: i32, b: i32, c: i32, i: u32) -> int {\n let bit_a = ((a >> i) & 1i32) as int;\n let bit_b = ((b >> i) & 1i32) as int;\n let bit_c = ((c >> i) & 1i32) as int;\n if bit_c == 0 {\n bit_a + bit_b\n } else if bit_a == 0 && bit_b == 0 {\n 1int\n } else {\n 0int\n }\n}\n\npub open spec fn total_flips(a: i32, b: i32, c: i32, n: int) -> int\n decreases n\n{\n if n <= 0 {\n 0\n } else {\n total_flips(a, b, c, n - 1) + flips_at_bit(a, b, c, (n - 1) as u32)\n }\n}\n\nimpl Solution {\n pub fn min_flips(a: i32, b: i32, c: i32) -> (result: i32)\n requires\n 1 <= a <= 1_000_000_000,\n 1 <= b <= 1_000_000_000,\n 1 <= c <= 1_000_000_000,\n ensures\n result as int == total_flips(a, b, c, 31),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn min_flips(a: i32, b: i32, c: i32) -> i32 {\n let mut flips: i32 = 0;\n let mut i: i32 = 0;\n while i < 31 {\n let bit_a = (a >> (i as u32)) & 1;\n let bit_b = (b >> (i as u32)) & 1;\n let bit_c = (c >> (i as u32)) & 1;\n if bit_c == 0 {\n flips = flips + bit_a + bit_b;\n } else {\n if bit_a == 0 && bit_b == 0 {\n flips = flips + 1;\n }\n }\n i = i + 1;\n }\n flips\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn flips_at_bit(a: i32, b: i32, c: i32, i: u32) -> int {\n let bit_a = ((a >> i) & 1i32) as int;\n let bit_b = ((b >> i) & 1i32) as int;\n let bit_c = ((c >> i) & 1i32) as int;\n if bit_c == 0 {\n bit_a + bit_b\n } else if bit_a == 0 && bit_b == 0 {\n 1int\n } else {\n 0int\n }\n}\n\npub open spec fn total_flips(a: i32, b: i32, c: i32, n: int) -> int\n decreases n\n{\n if n <= 0 {\n 0\n } else {\n total_flips(a, b, c, n - 1) + flips_at_bit(a, b, c, (n - 1) as u32)\n }\n}\n\nimpl Solution {\n pub fn min_flips(a: i32, b: i32, c: i32) -> (result: i32)\n requires\n 1 <= a <= 1_000_000_000,\n 1 <= b <= 1_000_000_000,\n 1 <= c <= 1_000_000_000,\n ensures\n result as int == total_flips(a, b, c, 31),\n {\n let mut flips: i32 = 0;\n let mut i: i32 = 0;\n while i < 31 {\n let bit_a = (a >> (i as u32)) & 1;\n let bit_b = (b >> (i as u32)) & 1;\n let bit_c = (c >> (i as u32)) & 1;\n if bit_c == 0 {\n flips = flips + bit_a + bit_b;\n } else {\n if bit_a == 0 && bit_b == 0 {\n flips = flips + 1;\n }\n }\n i = i + 1;\n }\n flips\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn flips_at_bit(a: i32, b: i32, c: i32, i: u32) -> int {\n let bit_a = ((a >> i) & 1i32) as int;\n let bit_b = ((b >> i) & 1i32) as int;\n let bit_c = ((c >> i) & 1i32) as int;\n if bit_c == 0 {\n bit_a + bit_b\n } else if bit_a == 0 && bit_b == 0 {\n 1int\n } else {\n 0int\n }\n}\n\npub open spec fn total_flips(a: i32, b: i32, c: i32, n: int) -> int\n decreases n\n{\n if n <= 0 {\n 0\n } else {\n total_flips(a, b, c, n - 1) + flips_at_bit(a, b, c, (n - 1) as u32)\n }\n}\n\nproof fn flips_at_bit_bounds(a: i32, b: i32, c: i32, i: u32)\n requires\n 1 <= a <= 1_000_000_000,\n 1 <= b <= 1_000_000_000,\n 1 <= c <= 1_000_000_000,\n i <= 30,\n ensures\n 0 <= flips_at_bit(a, b, c, i) <= 2,\n{\n assert(0 <= (a >> i) & 1i32 <= 1) by(bit_vector)\n requires 1 <= a <= 1_000_000_000i32, i <= 30u32;\n assert(0 <= (b >> i) & 1i32 <= 1) by(bit_vector)\n requires 1 <= b <= 1_000_000_000i32, i <= 30u32;\n assert(0 <= (c >> i) & 1i32 <= 1) by(bit_vector)\n requires 1 <= c <= 1_000_000_000i32, i <= 30u32;\n}\n\nimpl Solution {\n pub fn min_flips(a: i32, b: i32, c: i32) -> (result: i32)\n requires\n 1 <= a <= 1_000_000_000,\n 1 <= b <= 1_000_000_000,\n 1 <= c <= 1_000_000_000,\n ensures\n result as int == total_flips(a, b, c, 31),\n {\n let mut flips: i32 = 0;\n let mut i: i32 = 0;\n while i < 31\n invariant\n 1 <= a <= 1_000_000_000,\n 1 <= b <= 1_000_000_000,\n 1 <= c <= 1_000_000_000,\n 0 <= i <= 31,\n 0 <= flips <= 2 * i,\n flips as int == total_flips(a, b, c, i as int),\n decreases 31 - i\n {\n let bit_a = (a >> (i as u32)) & 1;\n let bit_b = (b >> (i as u32)) & 1;\n let bit_c = (c >> (i as u32)) & 1;\n\n proof {\n assert(0 <= (a >> (i as u32)) & 1i32 <= 1) by(bit_vector)\n requires 1 <= a <= 1_000_000_000i32, 0 <= i <= 30i32;\n assert(0 <= (b >> (i as u32)) & 1i32 <= 1) by(bit_vector)\n requires 1 <= b <= 1_000_000_000i32, 0 <= i <= 30i32;\n assert(0 <= (c >> (i as u32)) & 1i32 <= 1) by(bit_vector)\n requires 1 <= c <= 1_000_000_000i32, 0 <= i <= 30i32;\n flips_at_bit_bounds(a, b, c, i as u32);\n }\n\n if bit_c == 0 {\n flips = flips + bit_a + bit_b;\n } else {\n if bit_a == 0 && bit_b == 0 {\n flips = flips + 1;\n }\n }\n\n i = i + 1;\n }\n flips\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1318/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1318/tests/mutated_testcases.jsonl"} {"problem": "lc1330", "family": "leetcode", "description": "# Reverse Subarray To Maximize Array Value\n\nYou are given an integer array `nums`. The *value* of this array is defined as the sum of `|nums[i] - nums[i + 1]|` for all `0 <= i < nums.length - 1`.\n\nYou are allowed to select any subarray of the given array and reverse it. You can perform this operation **only once**.\n\nFind the maximum possible value of the final array.\n\n## Example 1:\n\n> **Input:** nums = [2,3,1,5,4]\n> **Output:** 10\n> **Explanation:** By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.\n\n## Example 2:\n\n> **Input:** nums = [2,4,9,24,2,1,10]\n> **Output:** 68\n\n## Constraints:\n\n- 2 <= nums.length <= 3 * 10^4\n- -10^5 <= nums[i] <= 10^5\n- The answer is guaranteed to fit in a 32-bit integer.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_value_after_reverse(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Math, Greedy\nAcceptance rate: 44.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn array_value_upto(s: Seq, n: int) -> int\n decreases n\n{\n if n <= 0 { 0 }\n else { array_value_upto(s, n - 1) + abs_diff(s[n - 1] as int, s[n] as int) }\n}\n\npub open spec fn array_value(s: Seq) -> int {\n array_value_upto(s, s.len() as int - 1)\n}\n\npub open spec fn reversal_gain(s: Seq, l: int, r: int) -> int {\n let n = s.len() as int;\n let left_change = if l > 0 {\n abs_diff(s[l - 1] as int, s[r] as int) - abs_diff(s[l - 1] as int, s[l] as int)\n } else { 0int };\n let right_change = if r < n - 1 {\n abs_diff(s[l] as int, s[r + 1] as int) - abs_diff(s[r] as int, s[r + 1] as int)\n } else { 0int };\n left_change + right_change\n}\n\nimpl Solution {\n pub fn max_value_after_reverse(nums: Vec) -> (result: i32)\n requires\n 2 <= nums@.len() <= 30000,\n forall |i: int| 0 <= i < nums@.len() ==> -100000 <= #[trigger] nums@[i] <= 100000,\n forall |l: int, r: int| 0 <= l && l <= r && r < nums@.len() ==>\n array_value(nums@) + #[trigger] reversal_gain(nums@, l, r) <= i32::MAX as int,\n ensures\n forall |l: int, r: int| 0 <= l && l <= r && r < nums@.len() ==>\n result as int >= array_value(nums@) + #[trigger] reversal_gain(nums@, l, r),\n exists |l: int, r: int| 0 <= l && l <= r && r < nums@.len() &&\n result as int == array_value(nums@) + reversal_gain(nums@, l, r),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_value_after_reverse(nums: Vec) -> i32 {\n let n = nums.len();\n let mut total: i64 = 0;\n let mut gain: i64 = 0;\n let mut min_of_max: i64 = 200001;\n let mut max_of_min: i64 = -200001;\n let mut i: usize = 0;\n while i < n - 1 {\n let a = nums[i] as i64;\n let b = nums[i + 1] as i64;\n let diff: i64 = if a >= b { a - b } else { b - a };\n total = total + diff;\n let first = nums[0] as i64;\n let last = nums[n - 1] as i64;\n let g1: i64 = (if first >= b { first - b } else { b - first }) - diff;\n let g2: i64 = (if last >= a { last - a } else { a - last }) - diff;\n if g1 > gain {\n gain = g1;\n }\n if g2 > gain {\n gain = g2;\n }\n let pair_max: i64 = if a >= b { a } else { b };\n let pair_min: i64 = if a <= b { a } else { b };\n if pair_max < min_of_max {\n min_of_max = pair_max;\n }\n if pair_min > max_of_min {\n max_of_min = pair_min;\n }\n i = i + 1;\n }\n let interior: i64 = if max_of_min > min_of_max {\n 2 * (max_of_min - min_of_max)\n } else {\n 0\n };\n if interior > gain {\n gain = interior;\n }\n (total + gain) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn array_value_upto(s: Seq, n: int) -> int\n decreases n\n{\n if n <= 0 { 0 }\n else { array_value_upto(s, n - 1) + abs_diff(s[n - 1] as int, s[n] as int) }\n}\n\npub open spec fn array_value(s: Seq) -> int {\n array_value_upto(s, s.len() as int - 1)\n}\n\npub open spec fn reversal_gain(s: Seq, l: int, r: int) -> int {\n let n = s.len() as int;\n let left_change = if l > 0 {\n abs_diff(s[l - 1] as int, s[r] as int) - abs_diff(s[l - 1] as int, s[l] as int)\n } else { 0int };\n let right_change = if r < n - 1 {\n abs_diff(s[l] as int, s[r + 1] as int) - abs_diff(s[r] as int, s[r + 1] as int)\n } else { 0int };\n left_change + right_change\n}\n\nimpl Solution {\n pub fn max_value_after_reverse(nums: Vec) -> (result: i32)\n requires\n 2 <= nums@.len() <= 30000,\n forall |i: int| 0 <= i < nums@.len() ==> -100000 <= #[trigger] nums@[i] <= 100000,\n forall |l: int, r: int| 0 <= l && l <= r && r < nums@.len() ==>\n array_value(nums@) + #[trigger] reversal_gain(nums@, l, r) <= i32::MAX as int,\n ensures\n forall |l: int, r: int| 0 <= l && l <= r && r < nums@.len() ==>\n result as int >= array_value(nums@) + #[trigger] reversal_gain(nums@, l, r),\n exists |l: int, r: int| 0 <= l && l <= r && r < nums@.len() &&\n result as int == array_value(nums@) + reversal_gain(nums@, l, r),\n {\n let n = nums.len();\n let mut total: i64 = 0;\n let mut gain: i64 = 0;\n let mut min_of_max: i64 = 200001;\n let mut max_of_min: i64 = -200001;\n let mut i: usize = 0;\n while i < n - 1 {\n let a = nums[i] as i64;\n let b = nums[i + 1] as i64;\n let diff: i64 = if a >= b { a - b } else { b - a };\n total = total + diff;\n let first = nums[0] as i64;\n let last = nums[n - 1] as i64;\n let g1: i64 = (if first >= b { first - b } else { b - first }) - diff;\n let g2: i64 = (if last >= a { last - a } else { a - last }) - diff;\n if g1 > gain {\n gain = g1;\n }\n if g2 > gain {\n gain = g2;\n }\n let pair_max: i64 = if a >= b { a } else { b };\n let pair_min: i64 = if a <= b { a } else { b };\n if pair_max < min_of_max {\n min_of_max = pair_max;\n }\n if pair_min > max_of_min {\n max_of_min = pair_min;\n }\n i = i + 1;\n }\n let interior: i64 = if max_of_min > min_of_max {\n 2 * (max_of_min - min_of_max)\n } else {\n 0\n };\n if interior > gain {\n gain = interior;\n }\n (total + gain) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n}\n\npub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn array_value_upto(s: Seq, n: int) -> int\n decreases n\n{\n if n <= 0 { 0 }\n else { array_value_upto(s, n - 1) + abs_diff(s[n - 1] as int, s[n] as int) }\n}\n\npub open spec fn array_value(s: Seq) -> int {\n array_value_upto(s, s.len() as int - 1)\n}\n\npub open spec fn reversal_gain(s: Seq, l: int, r: int) -> int {\n let n = s.len() as int;\n let left_change = if l > 0 {\n abs_diff(s[l - 1] as int, s[r] as int) - abs_diff(s[l - 1] as int, s[l] as int)\n } else { 0int };\n let right_change = if r < n - 1 {\n abs_diff(s[l] as int, s[r + 1] as int) - abs_diff(s[r] as int, s[r + 1] as int)\n } else { 0int };\n left_change + right_change\n}\n\nproof fn four_number_bound(a: int, b: int, c: int, d: int)\n ensures\n abs_diff(a, c) + abs_diff(b, d) - abs_diff(a, b) - abs_diff(c, d)\n <= 2 * spec_max(0, spec_max(\n spec_min(a, b) - spec_max(c, d),\n spec_min(c, d) - spec_max(a, b)\n )),\n{\n}\n\nproof fn four_number_exact(a: int, b: int, c: int, d: int)\n requires\n spec_min(a, b) > spec_max(c, d),\n ensures\n abs_diff(a, c) + abs_diff(b, d) - abs_diff(a, b) - abs_diff(c, d)\n == 2 * (spec_min(a, b) - spec_max(c, d)),\n{\n}\n\nimpl Solution {\n pub fn max_value_after_reverse(nums: Vec) -> (result: i32)\n requires\n 2 <= nums@.len() <= 30000,\n forall |i: int| 0 <= i < nums@.len() ==> -100000 <= #[trigger] nums@[i] <= 100000,\n forall |l: int, r: int| 0 <= l && l <= r && r < nums@.len() ==>\n array_value(nums@) + #[trigger] reversal_gain(nums@, l, r) <= i32::MAX as int,\n ensures\n forall |l: int, r: int| 0 <= l && l <= r && r < nums@.len() ==>\n result as int >= array_value(nums@) + #[trigger] reversal_gain(nums@, l, r),\n exists |l: int, r: int| 0 <= l && l <= r && r < nums@.len() &&\n result as int == array_value(nums@) + reversal_gain(nums@, l, r),\n {\n let n = nums.len();\n let mut total: i64 = 0;\n let mut gain: i64 = 0;\n let mut min_of_max: i64 = 200001;\n let mut max_of_min: i64 = -200001;\n\n let ghost mut gl: int = 0;\n let ghost mut gr: int = 0;\n let ghost mut mom_idx: int = 0;\n let ghost mut mim_idx: int = 0;\n\n let mut i: usize = 0;\n while i < n - 1\n invariant\n 0 <= i <= n - 1,\n n == nums@.len(),\n n >= 2,\n n <= 30000,\n forall |j: int| 0 <= j < n as int ==> -100000 <= #[trigger] nums@[j] <= 100000,\n total as int == array_value_upto(nums@, i as int),\n total >= 0i64,\n total as int <= 200000 * (i as int),\n 0 <= gain <= 200_000i64,\n forall |k: int| 0 <= k < i as int ==>\n gain as int >= #[trigger] reversal_gain(nums@, 0, k),\n forall |k: int| 1 <= k && k <= i as int ==>\n gain as int >= #[trigger] reversal_gain(nums@, k, (n - 1) as int),\n 0 <= gl && gl <= gr && gr < n as int,\n gain as int == reversal_gain(nums@, gl, gr),\n forall |k: int| 0 <= k < i as int ==>\n min_of_max as int <= #[trigger] spec_max(nums@[k] as int, nums@[k + 1] as int),\n forall |k: int| 0 <= k < i as int ==>\n max_of_min as int >= #[trigger] spec_min(nums@[k] as int, nums@[k + 1] as int),\n i > 0 ==> (0 <= mim_idx < i as int &&\n min_of_max as int == spec_max(nums@[mim_idx] as int, nums@[mim_idx + 1] as int)),\n i > 0 ==> (0 <= mom_idx < i as int &&\n max_of_min as int == spec_min(nums@[mom_idx] as int, nums@[mom_idx + 1] as int)),\n i == 0 ==> (min_of_max == 200001i64 && max_of_min == -200001i64),\n decreases n - 1 - i,\n {\n let a = nums[i] as i64;\n let b = nums[i + 1] as i64;\n let diff: i64 = if a >= b { a - b } else { b - a };\n\n proof {\n assert(diff as int == abs_diff(nums@[i as int] as int, nums@[i as int + 1] as int));\n assert(diff as int <= 200000);\n assert(total as int + diff as int == array_value_upto(nums@, i as int + 1));\n assert(total as int + diff as int <= 200000 * (i as int) + 200000);\n assert(total as int + diff as int <= 6_000_000_000int) by(nonlinear_arith)\n requires\n total as int + diff as int <= 200000 * (i as int) + 200000,\n (i as int) < (n as int) - 1,\n (n as int) <= 30000;\n }\n\n total = total + diff;\n\n let first = nums[0] as i64;\n let last = nums[n - 1] as i64;\n let g1: i64 = (if first >= b { first - b } else { b - first }) - diff;\n let g2: i64 = (if last >= a { last - a } else { a - last }) - diff;\n\n proof {\n assert(g1 as int == reversal_gain(nums@, 0, i as int));\n assert(g2 as int == reversal_gain(nums@, (i + 1) as int, (n - 1) as int));\n }\n\n if g1 > gain {\n gain = g1;\n proof { gl = 0; gr = i as int; }\n }\n if g2 > gain {\n gain = g2;\n proof { gl = (i + 1) as int; gr = (n - 1) as int; }\n }\n\n let pair_max: i64 = if a >= b { a } else { b };\n let pair_min: i64 = if a <= b { a } else { b };\n\n proof {\n assert(pair_max as int == spec_max(nums@[i as int] as int, nums@[i as int + 1] as int));\n assert(pair_min as int == spec_min(nums@[i as int] as int, nums@[i as int + 1] as int));\n }\n\n if pair_max < min_of_max {\n min_of_max = pair_max;\n proof { mim_idx = i as int; }\n }\n if pair_min > max_of_min {\n max_of_min = pair_min;\n proof { mom_idx = i as int; }\n }\n\n i = i + 1;\n }\n\n proof {\n assert(i == n - 1);\n assert(total as int == array_value_upto(nums@, (n - 1) as int));\n assert(total as int == array_value(nums@));\n }\n\n let interior: i64 = if max_of_min > min_of_max {\n 2 * (max_of_min - min_of_max)\n } else {\n 0\n };\n\n let ghost old_gain = gain;\n\n if interior > gain {\n gain = interior;\n proof {\n assert(mom_idx != mim_idx) by {\n if mom_idx == mim_idx {\n assert(max_of_min as int == spec_min(nums@[mom_idx] as int, nums@[mom_idx + 1] as int));\n assert(min_of_max as int == spec_max(nums@[mim_idx] as int, nums@[mim_idx + 1] as int));\n assert(spec_min(nums@[mom_idx] as int, nums@[mom_idx + 1] as int)\n <= spec_max(nums@[mom_idx] as int, nums@[mom_idx + 1] as int));\n assert(max_of_min <= min_of_max);\n }\n }\n if mom_idx < mim_idx {\n gl = mom_idx + 1;\n gr = mim_idx;\n four_number_exact(\n nums@[mom_idx] as int, nums@[mom_idx + 1] as int,\n nums@[mim_idx] as int, nums@[mim_idx + 1] as int\n );\n assert(reversal_gain(nums@, gl, gr) == 2 * (max_of_min as int - min_of_max as int));\n } else {\n gl = mim_idx + 1;\n gr = mom_idx;\n four_number_exact(\n nums@[mom_idx] as int, nums@[mom_idx + 1] as int,\n nums@[mim_idx] as int, nums@[mim_idx + 1] as int\n );\n assert(abs_diff(nums@[mom_idx] as int, nums@[mim_idx] as int)\n == abs_diff(nums@[mim_idx] as int, nums@[mom_idx] as int));\n assert(abs_diff(nums@[mom_idx + 1] as int, nums@[mim_idx + 1] as int)\n == abs_diff(nums@[mim_idx + 1] as int, nums@[mom_idx + 1] as int));\n assert(reversal_gain(nums@, gl, gr) == 2 * (max_of_min as int - min_of_max as int));\n }\n assert(gain as int == reversal_gain(nums@, gl, gr));\n }\n }\n\n proof {\n assert forall |l: int, r: int| 0 <= l && l <= r && r < n as int implies\n gain as int >= #[trigger] reversal_gain(nums@, l, r) by {\n if l == r {\n assert(reversal_gain(nums@, l, r) == 0int);\n } else if l == 0 && r == n - 1 {\n assert(reversal_gain(nums@, l, r) == 0int);\n } else if l == 0 {\n assert(0 <= r && r < (n - 1) as int);\n assert(old_gain as int >= reversal_gain(nums@, 0, r));\n assert(gain >= old_gain);\n } else if r == n - 1 {\n assert(1 <= l && l <= (n - 1) as int);\n assert(old_gain as int >= reversal_gain(nums@, l, (n - 1) as int));\n assert(gain >= old_gain);\n } else {\n four_number_bound(\n nums@[l - 1] as int, nums@[l] as int,\n nums@[r] as int, nums@[r + 1] as int\n );\n let kl = l - 1;\n let kr = r;\n assert(0 <= kl && kl < (n - 1) as int);\n assert(0 <= kr && kr < (n - 1) as int);\n assert(max_of_min as int >= spec_min(nums@[kl] as int, nums@[kl + 1] as int));\n assert(min_of_max as int <= spec_max(nums@[kr] as int, nums@[kr + 1] as int));\n assert(max_of_min as int >= spec_min(nums@[kr] as int, nums@[kr + 1] as int));\n assert(min_of_max as int <= spec_max(nums@[kl] as int, nums@[kl + 1] as int));\n assert(spec_min(nums@[kl] as int, nums@[kl + 1] as int)\n - spec_max(nums@[kr] as int, nums@[kr + 1] as int)\n <= max_of_min as int - min_of_max as int);\n assert(spec_min(nums@[kr] as int, nums@[kr + 1] as int)\n - spec_max(nums@[kl] as int, nums@[kl + 1] as int)\n <= max_of_min as int - min_of_max as int);\n if max_of_min > min_of_max {\n assert(gain as int >= 2 * (max_of_min as int - min_of_max as int));\n }\n }\n };\n\n assert(total as int + gain as int >= 0) by {\n assert(total >= 0);\n assert(gain >= 0);\n }\n assert(total as int + gain as int <= i32::MAX as int) by {\n assert(total as int == array_value(nums@));\n assert(gain as int == reversal_gain(nums@, gl, gr));\n assert(0 <= gl && gl <= gr && gr < n as int);\n }\n }\n\n (total + gain) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1330/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1330/tests/mutated_testcases.jsonl"} {"problem": "lc1342", "family": "leetcode", "description": "# Number of Steps to Reduce a Number to Zero\n\nGiven an integer `num`, return *the number of steps to reduce it to zero*.\n\nIn one step, if the current number is even, you have to divide it by `2`, otherwise, you have to subtract `1` from it.\n\n## Example 1:\n\n> **Input:** num = 14\n> **Output:** 6\n> **Explanation:**\n> Step 1) 14 is even; divide by 2 and obtain 7.\n> Step 2) 7 is odd; subtract 1 and obtain 6.\n> Step 3) 6 is even; divide by 2 and obtain 3.\n> Step 4) 3 is odd; subtract 1 and obtain 2.\n> Step 5) 2 is even; divide by 2 and obtain 1.\n> Step 6) 1 is odd; subtract 1 and obtain 0.\n\n## Example 2:\n\n> **Input:** num = 8\n> **Output:** 4\n> **Explanation:**\n> Step 1) 8 is even; divide by 2 and obtain 4.\n> Step 2) 4 is even; divide by 2 and obtain 2.\n> Step 3) 2 is even; divide by 2 and obtain 1.\n> Step 4) 1 is odd; subtract 1 and obtain 0.\n\n## Example 3:\n\n> **Input:** num = 123\n> **Output:** 12\n\n## Constraints:\n\n- $0 \\leq num \\leq 10^6$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn number_of_steps(num: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Bit Manipulation\nAcceptance rate: 85.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_steps(n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else if n % 2 == 0 {\n 1 + Self::spec_steps(n / 2)\n } else {\n 1 + Self::spec_steps(n - 1)\n }\n }\n\n pub fn number_of_steps(num: i32) -> (result: i32)\n requires\n 0 <= num <= 1_000_000,\n ensures\n result == Self::spec_steps(num as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn number_of_steps(num: i32) -> i32 {\n let mut n = num;\n let mut steps = 0i32;\n while n > 0 {\n if n % 2 == 0 {\n n = n / 2;\n } else {\n n = n - 1;\n }\n steps = steps + 1;\n }\n steps\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_steps(n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else if n % 2 == 0 {\n 1 + Self::spec_steps(n / 2)\n } else {\n 1 + Self::spec_steps(n - 1)\n }\n }\n\n pub fn number_of_steps(num: i32) -> (result: i32)\n requires\n 0 <= num <= 1_000_000,\n ensures\n result == Self::spec_steps(num as int),\n {\n let mut n = num;\n let mut steps = 0i32;\n while n > 0 {\n if n % 2 == 0 {\n n = n / 2;\n } else {\n n = n - 1;\n }\n steps = steps + 1;\n }\n steps\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_steps(n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else if n % 2 == 0 {\n 1 + Self::spec_steps(n / 2)\n } else {\n 1 + Self::spec_steps(n - 1)\n }\n }\n\n proof fn spec_steps_nonneg(n: int)\n requires\n n >= 0,\n ensures\n Self::spec_steps(n) >= 0,\n decreases n,\n {\n if n > 0 {\n if n % 2 == 0 {\n Self::spec_steps_nonneg(n / 2);\n } else {\n Self::spec_steps_nonneg(n - 1);\n }\n }\n }\n\n proof fn spec_steps_bound(n: int)\n requires\n n >= 0,\n ensures\n Self::spec_steps(n) <= 2 * n,\n decreases n,\n {\n if n > 0 {\n if n % 2 == 0 {\n Self::spec_steps_bound(n / 2);\n } else {\n Self::spec_steps_bound(n - 1);\n }\n }\n }\n\n pub fn number_of_steps(num: i32) -> (result: i32)\n requires\n 0 <= num <= 1_000_000,\n ensures\n result == Self::spec_steps(num as int),\n {\n let mut n = num;\n let mut steps = 0i32;\n\n proof {\n Self::spec_steps_bound(num as int);\n }\n\n while n > 0\n invariant\n 0 <= n <= 1_000_000,\n 0 <= steps <= 2_000_000,\n steps as int + Self::spec_steps(n as int) == Self::spec_steps(num as int),\n Self::spec_steps(num as int) <= 2_000_000,\n decreases n,\n {\n proof {\n if n as int % 2 == 0 {\n Self::spec_steps_nonneg(n as int / 2);\n } else {\n Self::spec_steps_nonneg(n as int - 1);\n }\n }\n if n % 2 == 0 {\n n = n / 2;\n } else {\n n = n - 1;\n }\n steps = steps + 1;\n }\n steps\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1342/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1342/tests/mutated_testcases.jsonl"} {"problem": "lc1399", "family": "leetcode", "description": "# Count Largest Group\n\nYou are given an integer `n`.\n\nWe need to group the numbers from `1` to `n` according to the sum of its digits. For example, the numbers 14 and 5 belong to the **same** group, whereas 13 and 3 belong to **different** groups.\n\nReturn the number of groups that have the largest size, i.e. the **maximum** number of elements.\n\n \n\nExample 1:\n\n```\n**Input:** n = 13\n**Output:** 4\n**Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:\n[1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9].\nThere are 4 groups with largest size.\n```\n\nExample 2:\n\n```\n**Input:** n = 2\n**Output:** 2\n**Explanation:** There are 2 groups [1], [2] of size 1.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_largest_group(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nHash Table, Math, Counting\nAcceptance rate: 74.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(x: int) -> int\n decreases x,\n {\n if x <= 0 { 0 }\n else { x % 10 + Self::digit_sum(x / 10) }\n }\n\n pub open spec fn group_size(n: int, target: int) -> int\n decreases n,\n {\n if n <= 0 { 0 }\n else {\n (if Self::digit_sum(n) == target { 1int } else { 0int })\n + Self::group_size(n - 1, target)\n }\n }\n\n pub open spec fn max_group_size(n: int, s: int) -> int\n decreases s,\n {\n if s <= 0 { 0 }\n else {\n let prev = Self::max_group_size(n, s - 1);\n let cur = Self::group_size(n, s);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub open spec fn count_max_groups(n: int, s: int, max_sz: int) -> int\n decreases s,\n {\n if s <= 0 { 0 }\n else {\n (if Self::group_size(n, s) == max_sz { 1int } else { 0int })\n + Self::count_max_groups(n, s - 1, max_sz)\n }\n }\n\n pub fn count_largest_group(n: i32) -> (result: i32)\n requires 1 <= n <= 10000\n ensures\n result as int == Self::count_max_groups(\n n as int, 36, Self::max_group_size(n as int, 36),\n ),\n {\n }\n}\n\n}", "code": "impl Solution {\n pub fn count_largest_group(n: i32) -> i32 {\n let mut counts: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 37\n {\n counts.push(0);\n k = k + 1;\n }\n let mut i: i32 = 1;\n while i <= n\n {\n let mut ds: i32 = 0;\n let mut x: i32 = i;\n while x > 0\n {\n ds = ds + (x % 10) as i32;\n x = x / 10;\n }\n counts[ds as usize] = counts[ds as usize] + 1;\n i = i + 1;\n }\n let mut max_size: i32 = 0;\n let mut count: i32 = 0;\n let mut j: usize = 1;\n while j < 37\n {\n if counts[j] > max_size {\n max_size = counts[j];\n count = 1;\n } else if counts[j] == max_size && max_size > 0 {\n count = count + 1;\n }\n j = j + 1;\n }\n count\n }\n}", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(x: int) -> int\n decreases x,\n {\n if x <= 0 { 0 }\n else { x % 10 + Self::digit_sum(x / 10) }\n }\n\n pub open spec fn group_size(n: int, target: int) -> int\n decreases n,\n {\n if n <= 0 { 0 }\n else {\n (if Self::digit_sum(n) == target { 1int } else { 0int })\n + Self::group_size(n - 1, target)\n }\n }\n\n pub open spec fn max_group_size(n: int, s: int) -> int\n decreases s,\n {\n if s <= 0 { 0 }\n else {\n let prev = Self::max_group_size(n, s - 1);\n let cur = Self::group_size(n, s);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub open spec fn count_max_groups(n: int, s: int, max_sz: int) -> int\n decreases s,\n {\n if s <= 0 { 0 }\n else {\n (if Self::group_size(n, s) == max_sz { 1int } else { 0int })\n + Self::count_max_groups(n, s - 1, max_sz)\n }\n }\n\n pub fn count_largest_group(n: i32) -> (result: i32)\n requires 1 <= n <= 10000\n ensures\n result as int == Self::count_max_groups(\n n as int, 36, Self::max_group_size(n as int, 36),\n ),\n {\n let mut counts: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 37\n {\n counts.push(0);\n k = k + 1;\n }\n let mut i: i32 = 1;\n while i <= n\n {\n let mut ds: i32 = 0;\n let mut x: i32 = i;\n while x > 0\n {\n ds = ds + (x % 10) as i32;\n x = x / 10;\n }\n counts.set(ds as usize, counts[ds as usize] + 1);\n i = i + 1;\n }\n let mut max_size: i32 = 0;\n let mut count: i32 = 0;\n let mut j: usize = 1;\n while j < 37\n {\n if counts[j] > max_size {\n max_size = counts[j];\n count = 1;\n } else if counts[j] == max_size && max_size > 0 {\n count = count + 1;\n }\n j = j + 1;\n }\n count\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(x: int) -> int\n decreases x,\n {\n if x <= 0 { 0 }\n else { x % 10 + Self::digit_sum(x / 10) }\n }\n\n pub open spec fn group_size(n: int, target: int) -> int\n decreases n,\n {\n if n <= 0 { 0 }\n else {\n (if Self::digit_sum(n) == target { 1int } else { 0int })\n + Self::group_size(n - 1, target)\n }\n }\n\n pub open spec fn max_group_size(n: int, s: int) -> int\n decreases s,\n {\n if s <= 0 { 0 }\n else {\n let prev = Self::max_group_size(n, s - 1);\n let cur = Self::group_size(n, s);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub open spec fn count_max_groups(n: int, s: int, max_sz: int) -> int\n decreases s,\n {\n if s <= 0 { 0 }\n else {\n (if Self::group_size(n, s) == max_sz { 1int } else { 0int })\n + Self::count_max_groups(n, s - 1, max_sz)\n }\n }\n\n proof fn digit_sum_nonneg(x: int)\n ensures Self::digit_sum(x) >= 0\n decreases x\n {\n if x > 0 {\n Self::digit_sum_nonneg(x / 10);\n }\n }\n\n proof fn digit_sum_le_36(x: int)\n requires 0 <= x <= 10000\n ensures Self::digit_sum(x) <= 36\n decreases x\n {\n if x <= 0 { return; }\n Self::digit_sum_nonneg(x / 10);\n if x <= 9 {\n \n } else if x <= 99 {\n Self::digit_sum_le_9(x / 10);\n } else if x <= 999 {\n Self::digit_sum_le_18(x / 10);\n } else if x <= 9999 {\n Self::digit_sum_le_27(x / 10);\n } else {\n \n Self::digit_sum_le_36(x / 10);\n }\n }\n\n proof fn digit_sum_le_9(x: int)\n requires 0 <= x <= 9\n ensures Self::digit_sum(x) <= 9\n {\n if x > 0 {\n assert(x / 10 == 0) by(nonlinear_arith) requires 0 < x <= 9;\n assert(x % 10 == x) by(nonlinear_arith) requires 0 <= x <= 9;\n reveal_with_fuel(Solution::digit_sum, 2);\n }\n }\n\n proof fn digit_sum_le_18(x: int)\n requires 0 <= x <= 99\n ensures Self::digit_sum(x) <= 18\n decreases x\n {\n if x > 0 {\n assert(x / 10 <= 9) by(nonlinear_arith) requires x <= 99, x >= 0;\n Self::digit_sum_le_9(x / 10);\n }\n }\n\n proof fn digit_sum_le_27(x: int)\n requires 0 <= x <= 999\n ensures Self::digit_sum(x) <= 27\n decreases x\n {\n if x > 0 {\n assert(x / 10 <= 99) by(nonlinear_arith) requires x <= 999, x >= 0;\n Self::digit_sum_le_18(x / 10);\n }\n }\n\n proof fn digit_sum_pos(x: int)\n requires 1 <= x <= 10000\n ensures Self::digit_sum(x) >= 1\n decreases x\n {\n Self::digit_sum_nonneg(x / 10);\n if x % 10 >= 1 {\n } else {\n assert(x >= 10) by(nonlinear_arith) requires x >= 1, x % 10 == 0;\n assert(x / 10 >= 1) by(nonlinear_arith) requires x >= 10;\n assert(x / 10 <= 10000) by(nonlinear_arith) requires x <= 10000;\n Self::digit_sum_pos(x / 10);\n }\n }\n\n proof fn group_size_nonneg(n: int, target: int)\n ensures Self::group_size(n, target) >= 0\n decreases n\n {\n if n > 0 { Self::group_size_nonneg(n - 1, target); }\n }\n\n proof fn group_size_bounded(n: int, target: int)\n requires n >= 0\n ensures Self::group_size(n, target) <= n\n decreases n\n {\n if n > 0 { Self::group_size_bounded(n - 1, target); }\n }\n\n proof fn count_max_zero_above(n: int, s: int, max_sz: int)\n requires max_sz > Self::max_group_size(n, s)\n ensures Self::count_max_groups(n, s, max_sz) == 0\n decreases s\n {\n if s > 0 {\n Self::group_size_nonneg(n, s);\n Self::count_max_zero_above(n, s - 1, max_sz);\n }\n }\n\n proof fn count_max_nonneg(n: int, s: int, max_sz: int)\n ensures Self::count_max_groups(n, s, max_sz) >= 0\n decreases s\n {\n if s > 0 { Self::count_max_nonneg(n, s - 1, max_sz); }\n }\n\n proof fn count_max_bounded(n: int, s: int, max_sz: int)\n requires s >= 0\n ensures Self::count_max_groups(n, s, max_sz) <= s\n decreases s\n {\n if s > 0 { Self::count_max_bounded(n, s - 1, max_sz); }\n }\n\n proof fn group_size_pos_1(n: int)\n requires n >= 1\n ensures Self::group_size(n, 1) >= 1\n decreases n\n {\n Self::group_size_nonneg(n - 1, 1);\n if n == 1 {\n assert(1int / 10 == 0) by(nonlinear_arith);\n reveal_with_fuel(Solution::digit_sum, 2);\n assert(Self::digit_sum(1) == 1);\n } else if Self::digit_sum(n) == 1 {\n } else {\n Self::group_size_pos_1(n - 1);\n }\n }\n\n proof fn max_group_ge(n: int, s: int, t: int)\n requires 1 <= t <= s\n ensures Self::max_group_size(n, s) >= Self::group_size(n, t)\n decreases s\n {\n Self::group_size_nonneg(n, s);\n if s > t { Self::max_group_ge(n, s - 1, t); }\n }\n\n pub fn count_largest_group(n: i32) -> (result: i32)\n requires 1 <= n <= 10000\n ensures\n result as int == Self::count_max_groups(\n n as int, 36, Self::max_group_size(n as int, 36),\n ),\n {\n let mut counts: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 37\n invariant\n counts.len() == k, k <= 37,\n forall |idx: int| 0 <= idx < k as int ==> counts[idx] == 0i32,\n decreases 37 - k,\n {\n counts.push(0);\n k = k + 1;\n }\n\n let mut i: i32 = 1;\n while i <= n\n invariant\n 1 <= i <= n + 1, 1 <= n <= 10000,\n counts.len() == 37,\n forall |s: int| 0 <= s < 37\n ==> #[trigger] counts[s] as int == Self::group_size((i - 1) as int, s),\n forall |s: int| 0 <= s < 37 ==> 0 <= #[trigger] counts[s] <= 10000,\n decreases (n - i + 1),\n {\n let mut ds: i32 = 0;\n let mut x: i32 = i;\n while x > 0\n invariant\n 0 <= ds <= 36,\n 0 <= x <= 10000,\n 1 <= i <= 10000,\n ds as int + Self::digit_sum(x as int) == Self::digit_sum(i as int),\n decreases x,\n {\n proof {\n Self::digit_sum_nonneg(x as int / 10);\n Self::digit_sum_le_36(i as int);\n assert(ds as int <= Self::digit_sum(i as int));\n }\n ds = ds + (x % 10) as i32;\n x = x / 10;\n }\n proof {\n assert(ds as int == Self::digit_sum(i as int));\n Self::digit_sum_le_36(i as int);\n Self::digit_sum_pos(i as int);\n }\n counts.set(ds as usize, counts[ds as usize] + 1);\n proof {\n assert forall |s: int| 0 <= s < 37\n implies #[trigger] counts[s] as int == Self::group_size(i as int, s)\n by {\n Self::group_size_nonneg((i - 1) as int, s);\n };\n assert forall |s: int| 0 <= s < 37\n implies 0 <= #[trigger] counts[s] <= 10000\n by {\n Self::group_size_bounded(i as int, s);\n };\n }\n i = i + 1;\n }\n\n let mut max_size: i32 = 0;\n let mut count: i32 = 0;\n let mut j: usize = 1;\n while j < 37\n invariant\n 1 <= j <= 37,\n counts.len() == 37, 1 <= n <= 10000,\n forall |s: int| 0 <= s < 37\n ==> #[trigger] counts[s] as int == Self::group_size(n as int, s),\n forall |s: int| 0 <= s < 37 ==> 0 <= #[trigger] counts[s] <= 10000,\n max_size as int == Self::max_group_size(n as int, (j - 1) as int),\n 0 <= max_size <= 10000,\n max_size > 0 ==> count as int == Self::count_max_groups(\n n as int, (j - 1) as int, max_size as int,\n ),\n max_size == 0 ==> count == 0,\n 0 <= count <= 36,\n decreases 37 - j,\n {\n proof { Self::group_size_nonneg(n as int, j as int); }\n if counts[j] > max_size {\n proof {\n Self::count_max_zero_above(n as int, (j - 1) as int, counts[j as int] as int);\n }\n max_size = counts[j];\n count = 1;\n } else if counts[j] == max_size && max_size > 0 {\n proof {\n Self::count_max_nonneg(n as int, (j - 1) as int, max_size as int);\n Self::count_max_bounded(n as int, (j - 1) as int, max_size as int);\n }\n count = count + 1;\n }\n j = j + 1;\n }\n proof {\n Self::group_size_pos_1(n as int);\n Self::max_group_ge(n as int, 36, 1);\n assert(max_size > 0);\n }\n count\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc1399/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1399/tests/mutated_testcases.jsonl"} {"problem": "lc1401", "family": "leetcode", "description": "# Circle and Rectangle Overlapping\n\nYou are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle.\n\nReturn `true` if the circle and rectangle are overlapped otherwise return `false`. In other words, check if there is **any** point `(xi, yi)` that belongs to the circle and the rectangle at the same time.\n\n## Example 1:\n\n> **Input:** radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1\n> **Output:** true\n> **Explanation:** Circle and rectangle share the point (1,0).\n\n## Example 2:\n\n> **Input:** radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1\n> **Output:** false\n\n## Example 3:\n\n> **Input:** radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1\n> **Output:** true\n\n## Constraints:\n\n- `1 <= radius <= 2000`\n- `-10^4 <= xCenter, yCenter <= 10^4`\n- `-10^4 <= x1 < x2 <= 10^4`\n- `-10^4 <= y1 < y2 <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nMath, Geometry\nAcceptance rate: 49.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn clamp(val: int, lo: int, hi: int) -> int {\n if val < lo {\n lo\n } else if val > hi {\n hi\n } else {\n val\n }\n}\n\npub open spec fn overlaps(radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool {\n let nearest_x = clamp(x_center, x1, x2);\n let nearest_y = clamp(y_center, y1, y2);\n let dx = x_center - nearest_x;\n let dy = y_center - nearest_y;\n dx * dx + dy * dy <= radius * radius\n}\n\nimpl Solution {\n pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> (res: bool)\n requires\n 1 <= radius <= 2000,\n -10_000 <= x_center <= 10_000,\n -10_000 <= y_center <= 10_000,\n -10_000 <= x1 <= 10_000,\n -10_000 <= y1 <= 10_000,\n -10_000 <= x2 <= 10_000,\n -10_000 <= y2 <= 10_000,\n x1 < x2,\n y1 < y2,\n ensures\n res == overlaps(radius as int, x_center as int, y_center as int, x1 as int, y1 as int, x2 as int, y2 as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> bool {\n let nearest_x = if x_center < x1 { x1 } else if x_center > x2 { x2 } else { x_center };\n let nearest_y = if y_center < y1 { y1 } else if y_center > y2 { y2 } else { y_center };\n let dx = x_center - nearest_x;\n let dy = y_center - nearest_y;\n dx * dx + dy * dy <= radius * radius\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn clamp(val: int, lo: int, hi: int) -> int {\n if val < lo {\n lo\n } else if val > hi {\n hi\n } else {\n val\n }\n}\n\npub open spec fn overlaps(radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool {\n let nearest_x = clamp(x_center, x1, x2);\n let nearest_y = clamp(y_center, y1, y2);\n let dx = x_center - nearest_x;\n let dy = y_center - nearest_y;\n dx * dx + dy * dy <= radius * radius\n}\n\nimpl Solution {\n pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> (res: bool)\n requires\n 1 <= radius <= 2000,\n -10_000 <= x_center <= 10_000,\n -10_000 <= y_center <= 10_000,\n -10_000 <= x1 <= 10_000,\n -10_000 <= y1 <= 10_000,\n -10_000 <= x2 <= 10_000,\n -10_000 <= y2 <= 10_000,\n x1 < x2,\n y1 < y2,\n ensures\n res == overlaps(radius as int, x_center as int, y_center as int, x1 as int, y1 as int, x2 as int, y2 as int),\n {\n let nearest_x = if x_center < x1 { x1 } else if x_center > x2 { x2 } else { x_center };\n let nearest_y = if y_center < y1 { y1 } else if y_center > y2 { y2 } else { y_center };\n let dx = x_center - nearest_x;\n let dy = y_center - nearest_y;\n dx * dx + dy * dy <= radius * radius\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn clamp(val: int, lo: int, hi: int) -> int {\n if val < lo {\n lo\n } else if val > hi {\n hi\n } else {\n val\n }\n}\n\npub open spec fn overlaps(radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool {\n let nearest_x = clamp(x_center, x1, x2);\n let nearest_y = clamp(y_center, y1, y2);\n let dx = x_center - nearest_x;\n let dy = y_center - nearest_y;\n dx * dx + dy * dy <= radius * radius\n}\n\nimpl Solution {\n pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> (res: bool)\n requires\n 1 <= radius <= 2000,\n -10_000 <= x_center <= 10_000,\n -10_000 <= y_center <= 10_000,\n -10_000 <= x1 <= 10_000,\n -10_000 <= y1 <= 10_000,\n -10_000 <= x2 <= 10_000,\n -10_000 <= y2 <= 10_000,\n x1 < x2,\n y1 < y2,\n ensures\n res == overlaps(radius as int, x_center as int, y_center as int, x1 as int, y1 as int, x2 as int, y2 as int),\n {\n let nearest_x = if x_center < x1 { x1 } else if x_center > x2 { x2 } else { x_center };\n let nearest_y = if y_center < y1 { y1 } else if y_center > y2 { y2 } else { y_center };\n let dx = x_center - nearest_x;\n let dy = y_center - nearest_y;\n\n proof {\n assert(-20_000 <= (x_center - nearest_x) as int <= 20_000) by(nonlinear_arith)\n requires\n -10_000 <= x_center <= 10_000i32,\n -10_000 <= nearest_x <= 10_000i32,\n {}\n assert(-20_000 <= (y_center - nearest_y) as int <= 20_000) by(nonlinear_arith)\n requires\n -10_000 <= y_center <= 10_000i32,\n -10_000 <= nearest_y <= 10_000i32,\n {}\n assert(0 <= dx as int * dx as int <= 400_000_000) by(nonlinear_arith)\n requires\n -20_000 <= dx as int <= 20_000,\n {}\n assert(0 <= dy as int * dy as int <= 400_000_000) by(nonlinear_arith)\n requires\n -20_000 <= dy as int <= 20_000,\n {}\n assert(0 <= dx as int * dx as int + dy as int * dy as int <= 800_000_000) by(nonlinear_arith)\n requires\n 0 <= dx as int * dx as int <= 400_000_000,\n 0 <= dy as int * dy as int <= 400_000_000,\n {}\n assert(0 <= radius as int * radius as int <= 4_000_000) by(nonlinear_arith)\n requires\n 1 <= radius <= 2000i32,\n {}\n }\n\n dx * dx + dy * dy <= radius * radius\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1401/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1401/tests/mutated_testcases.jsonl"} {"problem": "lc1411", "family": "leetcode", "description": "# Number of Ways to Paint N \u00d7 3 Grid\n\nYou have a `grid` of size `n x 3` and you want to paint each cell of the grid with exactly one of the three colors: **Red**, **Yellow,** or **Green** while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color).\n\nGiven `n` the number of rows of the grid, return *the number of ways* you can paint this `grid`. As the answer may grow large, the answer **must be** computed modulo `10^9 + 7`.\n\n## Example 1:\n> **Input:** n = 1\n> **Output:** 12\n> **Explanation:** There are 12 possible ways to paint the grid as shown.\n\n## Example 2:\n> **Input:** n = 5000\n> **Output:** 30228214\n\n## Constraints:\n- `n == grid.length`\n- $1 <= n <= 5000$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_of_ways(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nDynamic Programming\nAcceptance rate: 80.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n pub open spec fn ways_mod(n: int, kind: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else if n == 1 {\n 6\n } else if kind == 0 {\n (3 * Self::ways_mod(n - 1, 0) + 2 * Self::ways_mod(n - 1, 1)) % 1_000_000_007\n } else {\n (2 * Self::ways_mod(n - 1, 0) + 2 * Self::ways_mod(n - 1, 1)) % 1_000_000_007\n }\n }\n\n pub fn num_of_ways(n: i32) -> (res: i32)\n requires\n 1 <= n <= 5000,\n ensures\n 0 <= res,\n res as int == (Self::ways_mod(n as int, 0) + Self::ways_mod(n as int, 1)) % 1_000_000_007,\n {\n }\n}\n\n} ", "code": "impl Solution {\n pub fn num_of_ways(n: i32) -> i32 {\n let modv: i64 = 1_000_000_007;\n let mut a121: i64 = 6;\n let mut a123: i64 = 6;\n let mut i: i32 = 1;\n while i < n {\n let new_a121 = (3 * a121 + 2 * a123) % modv;\n let new_a123 = (2 * a121 + 2 * a123) % modv;\n a121 = new_a121;\n a123 = new_a123;\n i = i + 1;\n }\n ((a121 + a123) % modv) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n pub open spec fn ways_mod(n: int, kind: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else if n == 1 {\n 6\n } else if kind == 0 {\n (3 * Self::ways_mod(n - 1, 0) + 2 * Self::ways_mod(n - 1, 1)) % 1_000_000_007\n } else {\n (2 * Self::ways_mod(n - 1, 0) + 2 * Self::ways_mod(n - 1, 1)) % 1_000_000_007\n }\n }\n\n pub fn num_of_ways(n: i32) -> (res: i32)\n requires\n 1 <= n <= 5000,\n ensures\n 0 <= res,\n res as int == (Self::ways_mod(n as int, 0) + Self::ways_mod(n as int, 1)) % 1_000_000_007,\n {\n let modv: i64 = 1_000_000_007;\n let mut a121: i64 = 6;\n let mut a123: i64 = 6;\n let mut i: i32 = 1;\n while i < n {\n let new_a121 = (3 * a121 + 2 * a123) % modv;\n let new_a123 = (2 * a121 + 2 * a123) % modv;\n a121 = new_a121;\n a123 = new_a123;\n i = i + 1;\n }\n ((a121 + a123) % modv) as i32\n }\n}\n\n} ", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n pub open spec fn ways_mod(n: int, kind: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else if n == 1 {\n 6\n } else if kind == 0 {\n (3 * Self::ways_mod(n - 1, 0) + 2 * Self::ways_mod(n - 1, 1)) % 1_000_000_007\n } else {\n (2 * Self::ways_mod(n - 1, 0) + 2 * Self::ways_mod(n - 1, 1)) % 1_000_000_007\n }\n }\n\n proof fn ways_mod_bounds(n: int, kind: int)\n ensures\n 0 <= Self::ways_mod(n, kind) < 1_000_000_007,\n decreases n,\n {\n if n <= 0 {\n } else if n == 1 {\n } else {\n Self::ways_mod_bounds(n - 1, 0);\n Self::ways_mod_bounds(n - 1, 1);\n }\n }\n\n pub fn num_of_ways(n: i32) -> (res: i32)\n requires\n 1 <= n <= 5000,\n ensures\n 0 <= res,\n res as int == (Self::ways_mod(n as int, 0) + Self::ways_mod(n as int, 1)) % 1_000_000_007,\n {\n let modv: i64 = 1_000_000_007;\n let mut a121: i64 = 6;\n let mut a123: i64 = 6;\n let mut i: i32 = 1;\n while i < n\n invariant\n 1 <= i <= n,\n 1 <= n <= 5000,\n modv == 1_000_000_007i64,\n 0 <= a121 < modv,\n 0 <= a123 < modv,\n a121 as int == Self::ways_mod(i as int, 0),\n a123 as int == Self::ways_mod(i as int, 1),\n decreases n - i,\n {\n let new_a121 = (3 * a121 + 2 * a123) % modv;\n let new_a123 = (2 * a121 + 2 * a123) % modv;\n a121 = new_a121;\n a123 = new_a123;\n i = i + 1;\n }\n proof {\n Self::ways_mod_bounds(n as int, 0);\n Self::ways_mod_bounds(n as int, 1);\n }\n ((a121 + a123) % modv) as i32\n }\n}\n\n} ", "main": null, "checker": null, "testcases_path": "leetcode/lc1411/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1411/tests/mutated_testcases.jsonl"} {"problem": "lc1423", "family": "leetcode", "description": "# Maximum Points You Can Obtain from Cards\n\nThere are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array `cardPoints`. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly `k` cards. Your score is the sum of the points of the cards you have taken.\n\nReturn the *maximum score* you can obtain.\n\n## Example 1:\n\n> **Input:** cardPoints = [1,2,3,4,5,6,1], k = 3 \n> **Output:** 12 \n> **Explanation:** After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.\n\n## Example 2:\n\n> **Input:** cardPoints = [2,2,2], k = 2 \n> **Output:** 4 \n> **Explanation:** Regardless of which two cards you take, your score will always be 4.\n\n## Example 3:\n\n> **Input:** cardPoints = [9,7,7,9,7,7,9], k = 7 \n> **Output:** 55 \n> **Explanation:** You have to take all the cards. Your score is the sum of points of all cards.\n\n## Constraints:\n\n- `1 <= cardPoints.length <= 10^5`\n- `1 <= cardPoints[i] <= 10^4`\n- `1 <= k <= cardPoints.length`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_score(card_points: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nSenior, Array, Sliding Window, Prefix Sum, Weekly Contest 186\nAcceptance rate: 57.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(cards: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 { 0 }\n else { Self::prefix_sum(cards, n - 1) + cards[n - 1] as int }\n }\n\n pub open spec fn suffix_sum(cards: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 { 0 }\n else { Self::suffix_sum(cards, n - 1) + cards[cards.len() - n] as int }\n }\n\n pub open spec fn max_score_spec(cards: Seq, k: int, i: int) -> int\n decreases k - i + 1\n {\n if i > k { 0 }\n else {\n let score = Self::prefix_sum(cards, i) + Self::suffix_sum(cards, k - i);\n let rest = Self::max_score_spec(cards, k, i + 1);\n if score >= rest { score } else { rest }\n }\n }\n \n pub fn max_score(card_points: Vec, k: i32) -> (res: i32)\n requires\n 1 <= card_points.len(),\n card_points.len() <= 100_000,\n forall |i: int| 0 <= i < card_points.len() ==> 1 <= #[trigger] card_points[i] <= 10_000,\n 1 <= k,\n k <= card_points.len(),\n ensures\n res as int == Self::max_score_spec(card_points@, k as int, 0),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_score(card_points: Vec, k: i32) -> i32\n {\n let n = card_points.len();\n let k = k as usize;\n let mut left_sum = 0i32;\n let mut right_sum = 0i32;\n let mut i = 0usize;\n while i < k\n {\n left_sum = left_sum + card_points[i];\n i = i + 1;\n }\n\n let mut best = left_sum;\n\n let mut i = 0usize;\n while i < k\n {\n let left_card_idx = k - 1 - i;\n let right_card_idx = n - 1 - i;\n left_sum = left_sum - card_points[left_card_idx];\n right_sum = right_sum + card_points[right_card_idx];\n\n let score = left_sum + right_sum;\n if left_sum + right_sum > best {\n best = left_sum + right_sum;\n }\n i = i + 1;\n }\n\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(cards: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 { 0 }\n else { Self::prefix_sum(cards, n - 1) + cards[n - 1] as int }\n }\n\n pub open spec fn suffix_sum(cards: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 { 0 }\n else { Self::suffix_sum(cards, n - 1) + cards[cards.len() - n] as int }\n }\n\n pub open spec fn max_score_spec(cards: Seq, k: int, i: int) -> int\n decreases k - i + 1\n {\n if i > k { 0 }\n else {\n let score = Self::prefix_sum(cards, i) + Self::suffix_sum(cards, k - i);\n let rest = Self::max_score_spec(cards, k, i + 1);\n if score >= rest { score } else { rest }\n }\n }\n \n pub fn max_score(card_points: Vec, k: i32) -> (res: i32)\n requires\n 1 <= card_points.len(),\n card_points.len() <= 100_000,\n forall |i: int| 0 <= i < card_points.len() ==> 1 <= #[trigger] card_points[i] <= 10_000,\n 1 <= k,\n k <= card_points.len(),\n ensures\n res as int == Self::max_score_spec(card_points@, k as int, 0),\n {\n let n = card_points.len();\n let k = k as usize;\n let mut left_sum = 0i32;\n let mut right_sum = 0i32;\n let mut i = 0usize;\n while i < k\n {\n left_sum = left_sum + card_points[i];\n i = i + 1;\n }\n\n let mut best = left_sum;\n\n let mut i = 0usize;\n while i < k\n {\n let left_card_idx = k - 1 - i;\n let right_card_idx = n - 1 - i;\n left_sum = left_sum - card_points[left_card_idx];\n right_sum = right_sum + card_points[right_card_idx];\n\n let score = left_sum + right_sum;\n if left_sum + right_sum > best {\n best = left_sum + right_sum;\n }\n i = i + 1;\n }\n\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_sum(cards: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 { 0 }\n else { Self::prefix_sum(cards, n - 1) + cards[n - 1] as int }\n }\n\n pub open spec fn suffix_sum(cards: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 { 0 }\n else { Self::suffix_sum(cards, n - 1) + cards[cards.len() - n] as int }\n }\n\n pub open spec fn max_score_spec(cards: Seq, k: int, i: int) -> int\n decreases k - i + 1\n {\n if i > k { 0 }\n else {\n let score = Self::prefix_sum(cards, i) + Self::suffix_sum(cards, k - i);\n let rest = Self::max_score_spec(cards, k, i + 1);\n if score >= rest { score } else { rest }\n }\n }\n\n proof fn lemma_prefix_sum_bounds(cards: Seq, n: int)\n requires\n 0 <= n <= cards.len(),\n forall |i: int| 0 <= i < cards.len() ==> 1 <= #[trigger] cards[i] <= 10_000,\n ensures\n Self::prefix_sum(cards, n) >= 0,\n Self::prefix_sum(cards, n) <= n * 10_000,\n decreases n\n {\n if n > 0 {\n Self::lemma_prefix_sum_bounds(cards, n - 1);\n }\n }\n\n proof fn lemma_suffix_sum_bounds(cards: Seq, n: int)\n requires\n 0 <= n <= cards.len(),\n forall |i: int| 0 <= i < cards.len() ==> 1 <= #[trigger] cards[i] <= 10_000,\n ensures\n Self::suffix_sum(cards, n) >= 0,\n Self::suffix_sum(cards, n) <= n * 10_000,\n decreases n\n {\n if n > 0 {\n Self::lemma_suffix_sum_bounds(cards, n - 1);\n }\n }\n\n proof fn lemma_max_score_spec_unfold(cards: Seq, k: int, m: int)\n requires\n 0 <= m <= k,\n 0 <= k <= cards.len(),\n forall |i: int| 0 <= i < cards.len() ==> 1 <= #[trigger] cards[i] <= 10_000,\n ensures\n Self::max_score_spec(cards, k, m) ==\n (if Self::prefix_sum(cards, m) + Self::suffix_sum(cards, k - m) >=\n Self::max_score_spec(cards, k, m + 1)\n {\n Self::prefix_sum(cards, m) + Self::suffix_sum(cards, k - m)\n } else {\n Self::max_score_spec(cards, k, m + 1)\n }),\n {\n }\n\n pub fn max_score(card_points: Vec, k: i32) -> (res: i32)\n requires\n 1 <= card_points.len(),\n card_points.len() <= 100_000,\n forall |i: int| 0 <= i < card_points.len() ==> 1 <= #[trigger] card_points[i] <= 10_000,\n 1 <= k,\n k <= card_points.len(),\n ensures\n res as int == Self::max_score_spec(card_points@, k as int, 0),\n {\n let n = card_points.len();\n let k = k as usize;\n let mut left_sum = 0i32;\n let mut right_sum = 0i32;\n let mut i = 0usize;\n while i < k\n invariant\n 0 <= i <= k,\n k <= n,\n n == card_points.len(),\n n <= 100_000,\n left_sum as int == Self::prefix_sum(card_points@, i as int),\n 0 <= left_sum as int,\n left_sum as int <= i * 10_000,\n forall |j: int| 0 <= j < card_points.len() ==> 1 <= #[trigger] card_points[j] <= 10_000,\n decreases k - i\n {\n proof {\n Self::lemma_prefix_sum_bounds(card_points@, i as int);\n assert(Self::prefix_sum(card_points@, i as int + 1) ==\n Self::prefix_sum(card_points@, i as int) + card_points@[i as int] as int);\n }\n left_sum = left_sum + card_points[i];\n i = i + 1;\n }\n\n let mut best = left_sum;\n proof {\n assert(Self::suffix_sum(card_points@, 0int) == 0);\n Self::lemma_prefix_sum_bounds(card_points@, k as int);\n assert(Self::max_score_spec(card_points@, k as int, k as int) ==\n Self::prefix_sum(card_points@, k as int) + Self::suffix_sum(card_points@, 0int)) by {\n assert(Self::max_score_spec(card_points@, k as int, k as int) ==\n (if Self::prefix_sum(card_points@, k as int) + Self::suffix_sum(card_points@, k as int - k as int) >=\n Self::max_score_spec(card_points@, k as int, k as int + 1)\n {\n Self::prefix_sum(card_points@, k as int) + Self::suffix_sum(card_points@, k as int - k as int)\n } else {\n Self::max_score_spec(card_points@, k as int, k as int + 1)\n }));\n assert(Self::max_score_spec(card_points@, k as int, k as int + 1) == 0int);\n assert(Self::suffix_sum(card_points@, k as int - k as int) == Self::suffix_sum(card_points@, 0int));\n Self::lemma_prefix_sum_bounds(card_points@, k as int);\n }\n }\n\n let mut i = 0usize;\n while i < k\n invariant\n 0 <= i <= k,\n k <= n,\n n == card_points.len(),\n n <= 100_000,\n left_sum as int == Self::prefix_sum(card_points@, (k - i) as int),\n right_sum as int == Self::suffix_sum(card_points@, i as int),\n best as int == Self::max_score_spec(card_points@, k as int, (k - i) as int),\n 0 <= left_sum as int,\n left_sum as int <= (k - i) * 10_000,\n 0 <= right_sum as int,\n right_sum as int <= i * 10_000,\n 0 <= best as int,\n best as int <= k * 10_000,\n forall |j: int| 0 <= j < card_points.len() ==> 1 <= #[trigger] card_points[j] <= 10_000,\n decreases k - i\n {\n let left_card_idx = k - 1 - i;\n let right_card_idx = n - 1 - i;\n\n proof {\n assert(Self::prefix_sum(card_points@, (k - i) as int) ==\n Self::prefix_sum(card_points@, (k - i - 1) as int) +\n card_points@[(k - i - 1) as int] as int);\n assert(Self::suffix_sum(card_points@, (i + 1) as int) ==\n Self::suffix_sum(card_points@, i as int) +\n card_points@[(card_points@.len() - (i + 1)) as int] as int);\n Self::lemma_prefix_sum_bounds(card_points@, (k - i) as int);\n Self::lemma_suffix_sum_bounds(card_points@, i as int);\n }\n\n left_sum = left_sum - card_points[left_card_idx];\n right_sum = right_sum + card_points[right_card_idx];\n\n proof {\n Self::lemma_prefix_sum_bounds(card_points@, (k - i - 1) as int);\n Self::lemma_suffix_sum_bounds(card_points@, (i + 1) as int);\n assert(left_sum as int + right_sum as int <= k * 10_000) by (nonlinear_arith)\n requires\n left_sum as int <= (k - i - 1) * 10_000,\n right_sum as int <= (i + 1) * 10_000,\n k <= 100_000,\n i + 1 <= k;\n }\n\n let score = left_sum + right_sum;\n\n proof {\n assert(Self::max_score_spec(card_points@, k as int, (k - i - 1) as int) ==\n (if Self::prefix_sum(card_points@, (k - i - 1) as int) +\n Self::suffix_sum(card_points@, (k as int - (k - i - 1) as int)) >=\n Self::max_score_spec(card_points@, k as int, (k - i) as int)\n {\n Self::prefix_sum(card_points@, (k - i - 1) as int) +\n Self::suffix_sum(card_points@, (k as int - (k - i - 1) as int))\n } else {\n Self::max_score_spec(card_points@, k as int, (k - i) as int)\n }));\n }\n\n if left_sum + right_sum > best {\n best = left_sum + right_sum;\n }\n i = i + 1;\n }\n\n best\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1423/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1423/tests/mutated_testcases.jsonl"} {"problem": "lc1424", "family": "leetcode", "description": "# Diagonal Traverse II\n\nGiven a 2D integer array `nums`, return all elements of `nums` in diagonal order as shown in the below images.\n\n## Example 1:\n\n> **Input:** nums = [[1,2,3],[4,5,6],[7,8,9]]\n> **Output:** [1,4,2,7,5,3,8,6,9]\n\n## Example 2:\n\n> **Input:** nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]\n> **Output:** [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]\n\n## Constraints:\n\n- $1 \\leq$ `nums.length` $\\leq 10^5$\n- $1 \\leq$ `nums[i].length` $\\leq 10^5$\n- $1 \\leq$ `sum(nums[i].length)` $\\leq 10^5$\n- $1 \\leq$ `nums[i][j]` $\\leq 10^5$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_diagonal_order(nums: Vec>) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Sorting, Heap (Priority Queue)\nAcceptance rate: 58.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_diag_val(nums: Seq>, i: int) -> int\n decreases nums.len() - i\n {\n if i >= nums.len() {\n 0int\n } else {\n let d = i + nums[i].len() - 1;\n let rest = Self::max_diag_val(nums, i + 1);\n if d > rest { d } else { rest }\n }\n }\n\n pub open spec fn diag_seg(nums: Seq>, d: int, hi: int, lo: int) -> Seq\n decreases (if hi >= lo && hi >= 0 { hi - lo + 1 } else { 0 }) as nat\n {\n if hi < lo || hi < 0 {\n Seq::::empty()\n } else {\n let j = d - hi;\n let head = if hi < nums.len() && 0 <= j && j < nums[hi].len() {\n seq![nums[hi][j]]\n } else {\n Seq::::empty()\n };\n head + Self::diag_seg(nums, d, hi - 1, lo)\n }\n }\n\n pub open spec fn total_len(nums: Seq>, i: int) -> int\n decreases i\n {\n if i <= 0 { 0int } else { Self::total_len(nums, i - 1) + nums[i - 1].len() as int }\n }\n\n pub open spec fn diag_order(nums: Seq>, max_d: int) -> Seq\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat\n {\n if max_d < 0 {\n Seq::::empty()\n } else {\n let m = nums.len() as int;\n let start_i = if max_d < m { max_d } else { m - 1 };\n Self::diag_order(nums, max_d - 1) + Self::diag_seg(nums, max_d, start_i, 0)\n }\n }\n\n pub fn find_diagonal_order(nums: Vec>) -> (result: Vec)\n requires\n 1 <= nums@.len() <= 100000,\n forall |i: int| 0 <= i < nums@.len() ==>\n 1 <= (#[trigger] nums@[i]).len() <= 100000,\n forall |i: int, j: int| 0 <= i < nums@.len() && 0 <= j < nums@[i].len() ==>\n 1 <= (#[trigger] nums@[i][j]) <= 100000,\n 1 <= Self::total_len(nums@, nums@.len() as int) <= 100000,\n ensures\n result@ == Self::diag_order(nums@, Self::max_diag_val(nums@, 0)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_diagonal_order(nums: Vec>) -> Vec {\n let m = nums.len();\n\n let mut max_d: usize = 0;\n let mut i: usize = m;\n while i > 0 {\n i = i - 1;\n let d = i + nums[i].len() - 1;\n if d > max_d {\n max_d = d;\n }\n }\n\n let mut count: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= max_d {\n count.push(0);\n k = k + 1;\n }\n\n let mut i2: usize = 0;\n while i2 < m {\n let row_len = nums[i2].len();\n let mut j: usize = 0;\n while j < row_len {\n let d = i2 + j;\n count[d] = count[d] + 1;\n j = j + 1;\n }\n i2 = i2 + 1;\n }\n\n let mut offset: Vec = Vec::new();\n offset.push(0);\n let mut k2: usize = 0;\n while k2 <= max_d {\n let next = offset[k2] + count[k2];\n offset.push(next);\n k2 = k2 + 1;\n }\n\n let total = offset[max_d + 1];\n\n let mut result: Vec = Vec::new();\n let mut z: usize = 0;\n while z < total {\n result.push(0);\n z = z + 1;\n }\n\n let mut cursor: Vec = Vec::new();\n let mut k3: usize = 0;\n while k3 <= max_d {\n cursor.push(offset[k3 + 1]);\n k3 = k3 + 1;\n }\n\n let mut i3: usize = 0;\n while i3 < m {\n let row_len3 = nums[i3].len();\n let mut j3: usize = 0;\n while j3 < row_len3 {\n let d = i3 + j3;\n let old_cursor_d = cursor[d];\n cursor[d] = cursor[d] - 1;\n let val = nums[i3][j3];\n result[cursor[d]] = val;\n j3 = j3 + 1;\n }\n i3 = i3 + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_diag_val(nums: Seq>, i: int) -> int\n decreases nums.len() - i\n {\n if i >= nums.len() {\n 0int\n } else {\n let d = i + nums[i].len() - 1;\n let rest = Self::max_diag_val(nums, i + 1);\n if d > rest { d } else { rest }\n }\n }\n\n pub open spec fn diag_seg(nums: Seq>, d: int, hi: int, lo: int) -> Seq\n decreases (if hi >= lo && hi >= 0 { hi - lo + 1 } else { 0 }) as nat\n {\n if hi < lo || hi < 0 {\n Seq::::empty()\n } else {\n let j = d - hi;\n let head = if hi < nums.len() && 0 <= j && j < nums[hi].len() {\n seq![nums[hi][j]]\n } else {\n Seq::::empty()\n };\n head + Self::diag_seg(nums, d, hi - 1, lo)\n }\n }\n\n pub open spec fn total_len(nums: Seq>, i: int) -> int\n decreases i\n {\n if i <= 0 { 0int } else { Self::total_len(nums, i - 1) + nums[i - 1].len() as int }\n }\n\n pub open spec fn diag_order(nums: Seq>, max_d: int) -> Seq\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat\n {\n if max_d < 0 {\n Seq::::empty()\n } else {\n let m = nums.len() as int;\n let start_i = if max_d < m { max_d } else { m - 1 };\n Self::diag_order(nums, max_d - 1) + Self::diag_seg(nums, max_d, start_i, 0)\n }\n }\n\n pub fn find_diagonal_order(nums: Vec>) -> (result: Vec)\n requires\n 1 <= nums@.len() <= 100000,\n forall |i: int| 0 <= i < nums@.len() ==>\n 1 <= (#[trigger] nums@[i]).len() <= 100000,\n forall |i: int, j: int| 0 <= i < nums@.len() && 0 <= j < nums@[i].len() ==>\n 1 <= (#[trigger] nums@[i][j]) <= 100000,\n 1 <= Self::total_len(nums@, nums@.len() as int) <= 100000,\n ensures\n result@ == Self::diag_order(nums@, Self::max_diag_val(nums@, 0)),\n {\n let m = nums.len();\n\n let mut max_d: usize = 0;\n let mut i: usize = m;\n while i > 0 {\n i = i - 1;\n let d = i + nums[i].len() - 1;\n if d > max_d {\n max_d = d;\n }\n }\n\n let mut count: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= max_d {\n count.push(0);\n k = k + 1;\n }\n\n let mut i2: usize = 0;\n while i2 < m {\n let row_len = nums[i2].len();\n let mut j: usize = 0;\n while j < row_len {\n let d = i2 + j;\n count[d] = count[d] + 1;\n j = j + 1;\n }\n i2 = i2 + 1;\n }\n\n let mut offset: Vec = Vec::new();\n offset.push(0);\n let mut k2: usize = 0;\n while k2 <= max_d {\n let next = offset[k2] + count[k2];\n offset.push(next);\n k2 = k2 + 1;\n }\n\n let total = offset[max_d + 1];\n\n let mut result: Vec = Vec::new();\n let mut z: usize = 0;\n while z < total {\n result.push(0);\n z = z + 1;\n }\n\n let mut cursor: Vec = Vec::new();\n let mut k3: usize = 0;\n while k3 <= max_d {\n cursor.push(offset[k3 + 1]);\n k3 = k3 + 1;\n }\n\n let mut i3: usize = 0;\n while i3 < m {\n let row_len3 = nums[i3].len();\n let mut j3: usize = 0;\n while j3 < row_len3 {\n let d = i3 + j3;\n let old_cursor_d = cursor[d];\n cursor[d] = cursor[d] - 1;\n let val = nums[i3][j3];\n result.set(cursor[d], val);\n j3 = j3 + 1;\n }\n i3 = i3 + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_diag_val(nums: Seq>, i: int) -> int\n decreases nums.len() - i\n {\n if i >= nums.len() {\n 0int\n } else {\n let d = i + nums[i].len() - 1;\n let rest = Self::max_diag_val(nums, i + 1);\n if d > rest { d } else { rest }\n }\n }\n\n pub open spec fn diag_seg(nums: Seq>, d: int, hi: int, lo: int) -> Seq\n decreases (if hi >= lo && hi >= 0 { hi - lo + 1 } else { 0 }) as nat\n {\n if hi < lo || hi < 0 {\n Seq::::empty()\n } else {\n let j = d - hi;\n let head = if hi < nums.len() && 0 <= j && j < nums[hi].len() {\n seq![nums[hi][j]]\n } else {\n Seq::::empty()\n };\n head + Self::diag_seg(nums, d, hi - 1, lo)\n }\n }\n\n pub open spec fn total_len(nums: Seq>, i: int) -> int\n decreases i\n {\n if i <= 0 { 0int } else { Self::total_len(nums, i - 1) + nums[i - 1].len() as int }\n }\n\n pub open spec fn diag_order(nums: Seq>, max_d: int) -> Seq\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat\n {\n if max_d < 0 {\n Seq::::empty()\n } else {\n let m = nums.len() as int;\n let start_i = if max_d < m { max_d } else { m - 1 };\n Self::diag_order(nums, max_d - 1) + Self::diag_seg(nums, max_d, start_i, 0)\n }\n }\n\n pub open spec fn diag_seg_fwd(nums: Seq>, d: int, lo: int, hi: int) -> Seq\n decreases (if hi >= lo { hi - lo + 1 } else { 0 }) as nat\n {\n if lo > hi {\n Seq::::empty()\n } else {\n let j = d - lo;\n let head = if 0 <= lo && lo < nums.len() && 0 <= j && j < nums[lo].len() {\n seq![nums[lo][j]]\n } else {\n Seq::::empty()\n };\n head + Self::diag_seg_fwd(nums, d, lo + 1, hi)\n }\n }\n\n proof fn lemma_diag_seg_fwd_snoc(nums: Seq>, d: int, lo: int, hi: int)\n requires\n hi >= lo,\n ensures\n Self::diag_seg_fwd(nums, d, lo, hi) == Self::diag_seg_fwd(nums, d, lo, hi - 1)\n + (if 0 <= hi && hi < nums.len() && 0 <= d - hi && d - hi < nums[hi].len() {\n seq![nums[hi][d - hi]]\n } else {\n Seq::::empty()\n }),\n decreases hi - lo,\n {\n if lo == hi {\n assert(Self::diag_seg_fwd(nums, d, lo + 1, hi) == Seq::::empty());\n assert(Self::diag_seg_fwd(nums, d, lo, hi - 1) == Seq::::empty());\n } else {\n Self::lemma_diag_seg_fwd_snoc(nums, d, lo + 1, hi);\n assert(Self::diag_seg_fwd(nums, d, lo, hi - 1) ==\n (if 0 <= lo && lo < nums.len() && 0 <= d - lo && d - lo < nums[lo].len() {\n seq![nums[lo][d - lo]]\n } else {\n Seq::::empty()\n }) + Self::diag_seg_fwd(nums, d, lo + 1, hi - 1));\n }\n }\n\n proof fn lemma_reverse_seg(nums: Seq>, d: int, lo: int, hi: int)\n requires\n lo >= 0,\n ensures\n Self::diag_seg(nums, d, hi, lo) == Self::diag_seg_fwd(nums, d, lo, hi).reverse(),\n decreases (if hi >= lo { hi - lo + 1 } else { 0 }) as nat,\n {\n if hi < lo {\n assert(Self::diag_seg(nums, d, hi, lo) == Seq::::empty());\n assert(Self::diag_seg_fwd(nums, d, lo, hi) == Seq::::empty());\n } else {\n Self::lemma_reverse_seg(nums, d, lo, hi - 1);\n Self::lemma_diag_seg_fwd_snoc(nums, d, lo, hi);\n let tail = if 0 <= hi && hi < nums.len() && 0 <= d - hi && d - hi < nums[hi].len() {\n seq![nums[hi][d - hi]]\n } else {\n Seq::::empty()\n };\n assert(Self::diag_seg(nums, d, hi, lo) == tail + Self::diag_seg(nums, d, hi - 1, lo));\n assert(Self::diag_seg_fwd(nums, d, lo, hi) == Self::diag_seg_fwd(nums, d, lo, hi - 1) + tail);\n assert(Self::diag_seg_fwd(nums, d, lo, hi).reverse() ==\n tail.reverse() + Self::diag_seg_fwd(nums, d, lo, hi - 1).reverse());\n assert(tail.reverse() == tail);\n }\n }\n\n proof fn lemma_diag_seg_fwd_trunc(nums: Seq>, d: int, hi1: int, hi2: int)\n requires\n 0 <= d <= hi2 <= hi1,\n ensures\n Self::diag_seg_fwd(nums, d, 0, hi1) == Self::diag_seg_fwd(nums, d, 0, hi2),\n decreases hi1 - hi2,\n {\n if hi1 > hi2 {\n Self::lemma_diag_seg_fwd_snoc(nums, d, 0, hi1);\n Self::lemma_diag_seg_fwd_trunc(nums, d, hi1 - 1, hi2);\n assert(!(0 <= hi1 && hi1 < nums.len() && 0 <= d - hi1 && d - hi1 < nums[hi1].len()));\n }\n }\n\n proof fn max_diag_bound(nums: Seq>, i: int)\n requires\n 0 <= i <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==>\n 1 <= (#[trigger] nums[k]).len() <= 100000,\n nums.len() <= 100000,\n ensures\n Self::max_diag_val(nums, i) <= 199999,\n Self::max_diag_val(nums, i) >= 0,\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n } else {\n Self::max_diag_bound(nums, i + 1);\n }\n }\n\n proof fn lemma_diag_seg_fwd_len_bound(nums: Seq>, d: int, hi: int)\n requires\n hi >= -1,\n ensures\n Self::diag_seg_fwd(nums, d, 0, hi).len() <= hi + 1,\n decreases (hi + 1) as nat,\n {\n if hi >= 0 {\n Self::lemma_diag_seg_fwd_snoc(nums, d, 0, hi);\n Self::lemma_diag_seg_fwd_len_bound(nums, d, hi - 1);\n } else {\n assert(Self::diag_seg_fwd(nums, d, 0, hi) =~= Seq::::empty());\n }\n }\n\n proof fn lemma_diag_seg_fwd_len_monotone(nums: Seq>, d: int, hi1: int, hi2: int)\n requires\n hi1 >= -1,\n hi1 <= hi2,\n ensures\n Self::diag_seg_fwd(nums, d, 0, hi1).len() <= Self::diag_seg_fwd(nums, d, 0, hi2).len(),\n decreases hi2 - hi1,\n {\n if hi1 < hi2 {\n Self::lemma_diag_seg_fwd_snoc(nums, d, 0, hi2);\n Self::lemma_diag_seg_fwd_len_monotone(nums, d, hi1, hi2 - 1);\n }\n }\n\n proof fn lemma_max_diag_is_upper_bound(nums: Seq>, i: int, row: int)\n requires\n 0 <= i <= row < nums.len(),\n ensures\n row + nums[row].len() as int - 1 <= Self::max_diag_val(nums, i),\n decreases row - i,\n {\n if i == row {\n } else {\n Self::lemma_max_diag_is_upper_bound(nums, i + 1, row);\n }\n }\n\n pub open spec fn count_of(nums: Seq>, d: int) -> int {\n let m = nums.len() as int;\n let start_i = if d < m { d } else { m - 1 };\n Self::diag_seg(nums, d, start_i, 0).len() as int\n }\n\n pub open spec fn offset_of(nums: Seq>, d: int) -> int\n decreases (if d >= 0 { d + 1 } else { 0 }) as nat\n {\n if d <= 0 { 0 } else { Self::offset_of(nums, d - 1) + Self::count_of(nums, d - 1) }\n }\n\n proof fn lemma_offset_of_eq_diag_order_len(nums: Seq>, d: int)\n requires\n d >= 0,\n ensures\n Self::offset_of(nums, d) == Self::diag_order(nums, d - 1).len(),\n decreases d,\n {\n if d > 0 {\n Self::lemma_offset_of_eq_diag_order_len(nums, d - 1);\n let m = nums.len() as int;\n let start_i = if d - 1 < m { d - 1 } else { m - 1 };\n assert(Self::diag_order(nums, d - 1) == Self::diag_order(nums, d - 2) + Self::diag_seg(nums, d - 1, start_i, 0));\n assert(Self::count_of(nums, d - 1) == Self::diag_seg(nums, d - 1, start_i, 0).len() as int);\n }\n }\n\n proof fn lemma_count_of_eq_fwd_len(nums: Seq>, d: int)\n requires\n 0 <= d,\n nums.len() >= 1,\n ensures\n Self::count_of(nums, d) == Self::diag_seg_fwd(nums, d, 0, nums.len() as int - 1).len() as int,\n {\n let m = nums.len() as int;\n let start_i = if d < m { d } else { m - 1 };\n Self::lemma_reverse_seg(nums, d, 0, start_i);\n assert(Self::diag_seg(nums, d, start_i, 0).len() == Self::diag_seg_fwd(nums, d, 0, start_i).reverse().len());\n assert(Self::diag_seg_fwd(nums, d, 0, start_i).reverse().len() == Self::diag_seg_fwd(nums, d, 0, start_i).len());\n if start_i < m - 1 {\n Self::lemma_diag_seg_fwd_trunc(nums, d, m - 1, start_i);\n }\n }\n\n pub open spec fn row_indicator_sum(nums: Seq>, r: int, max_d: int) -> int\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat\n {\n if max_d < 0 {\n 0\n } else {\n Self::row_indicator_sum(nums, r, max_d - 1)\n + (if 0 <= r < nums.len() && 0 <= max_d - r && max_d - r < nums[r].len() { 1int } else { 0int })\n }\n }\n\n proof fn lemma_row_indicator_sum_below_r(nums: Seq>, r: int, md: int)\n requires\n md >= -1,\n md < r,\n ensures\n Self::row_indicator_sum(nums, r, md) == 0,\n decreases (md + 1) as nat,\n {\n if md >= 0 {\n Self::lemma_row_indicator_sum_below_r(nums, r, md - 1);\n }\n }\n\n proof fn lemma_row_indicator_sum_general(nums: Seq>, r: int, max_d: int)\n requires\n 0 <= r < nums.len(),\n max_d >= r - 1,\n ensures\n Self::row_indicator_sum(nums, r, max_d) ==\n (if max_d - r + 1 <= nums[r].len() as int { max_d - r + 1 } else { nums[r].len() as int }),\n decreases (max_d - r + 1) as nat,\n {\n if max_d > r - 1 {\n Self::lemma_row_indicator_sum_general(nums, r, max_d - 1);\n assert(Self::row_indicator_sum(nums, r, max_d) == Self::row_indicator_sum(nums, r, max_d - 1)\n + (if 0 <= max_d - r && max_d - r < nums[r].len() { 1int } else { 0int }));\n } else {\n assert(max_d == r - 1);\n Self::lemma_row_indicator_sum_below_r(nums, r, max_d);\n }\n }\n\n proof fn lemma_row_indicator_sum_full(nums: Seq>, r: int, max_d: int)\n requires\n 0 <= r < nums.len(),\n max_d >= r + nums[r].len() as int - 1,\n ensures\n Self::row_indicator_sum(nums, r, max_d) == nums[r].len() as int,\n {\n Self::lemma_row_indicator_sum_general(nums, r, max_d);\n }\n\n pub open spec fn total_diag_len_upto(nums: Seq>, max_d: int, up_to: int) -> int\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat\n {\n if max_d < 0 {\n 0\n } else {\n Self::total_diag_len_upto(nums, max_d - 1, up_to)\n + Self::diag_seg_fwd(nums, max_d, 0, up_to - 1).len() as int\n }\n }\n\n proof fn lemma_total_diag_len_upto_zero(nums: Seq>, max_d: int)\n ensures\n Self::total_diag_len_upto(nums, max_d, 0) == 0,\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat,\n {\n if max_d >= 0 {\n Self::lemma_total_diag_len_upto_zero(nums, max_d - 1);\n assert(Self::diag_seg_fwd(nums, max_d, 0, -1).len() == 0);\n }\n }\n\n proof fn lemma_total_diag_len_upto_delta(nums: Seq>, max_d: int, up_to: int)\n requires\n 0 <= up_to < nums.len(),\n ensures\n Self::total_diag_len_upto(nums, max_d, up_to + 1)\n == Self::total_diag_len_upto(nums, max_d, up_to) + Self::row_indicator_sum(nums, up_to, max_d),\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat,\n {\n if max_d >= 0 {\n Self::lemma_total_diag_len_upto_delta(nums, max_d - 1, up_to);\n Self::lemma_diag_seg_fwd_snoc(nums, max_d, 0, up_to);\n assert(Self::diag_seg_fwd(nums, max_d, 0, up_to).len()\n == Self::diag_seg_fwd(nums, max_d, 0, up_to - 1).len()\n + (if 0 <= up_to && up_to < nums.len() && 0 <= max_d - up_to && max_d - up_to < nums[up_to].len() { 1int } else { 0int }));\n }\n }\n\n proof fn lemma_total_diag_len_upto_eq_total_len(nums: Seq>, max_d: int, up_to: int)\n requires\n 0 <= up_to <= nums.len(),\n forall |r: int| 0 <= r < up_to ==> max_d >= r + (#[trigger] nums[r]).len() as int - 1,\n ensures\n Self::total_diag_len_upto(nums, max_d, up_to) == Self::total_len(nums, up_to),\n decreases up_to,\n {\n if up_to == 0 {\n Self::lemma_total_diag_len_upto_zero(nums, max_d);\n } else {\n Self::lemma_total_diag_len_upto_eq_total_len(nums, max_d, up_to - 1);\n Self::lemma_total_diag_len_upto_delta(nums, max_d, up_to - 1);\n Self::lemma_row_indicator_sum_full(nums, up_to - 1, max_d);\n }\n }\n\n proof fn lemma_offset_of_eq_total_diag_len_upto(nums: Seq>, max_d: int)\n requires\n nums.len() >= 1,\n ensures\n Self::offset_of(nums, max_d + 1) == Self::total_diag_len_upto(nums, max_d, nums.len() as int),\n decreases (if max_d >= 0 { max_d + 1 } else { 0 }) as nat,\n {\n if max_d >= 0 {\n Self::lemma_offset_of_eq_total_diag_len_upto(nums, max_d - 1);\n Self::lemma_count_of_eq_fwd_len(nums, max_d);\n }\n }\n\n proof fn lemma_offset_of_le_total_len(nums: Seq>, d: int)\n requires\n 0 <= d,\n nums.len() >= 1,\n nums.len() <= 100000,\n forall |r: int| 0 <= r < nums.len() ==> 1 <= (#[trigger] nums[r]).len() <= 100000,\n 1 <= Self::total_len(nums, nums.len() as int) <= 100000,\n ensures\n 0 <= Self::offset_of(nums, d) <= 100000,\n {\n let m = nums.len() as int;\n Self::max_diag_bound(nums, 0);\n let max_d = Self::max_diag_val(nums, 0);\n assert forall |r: int| 0 <= r < m implies max_d >= r + (#[trigger] nums[r]).len() as int - 1 by {\n Self::lemma_max_diag_is_upper_bound(nums, 0, r);\n }\n Self::lemma_total_diag_len_upto_eq_total_len(nums, max_d, m);\n Self::lemma_offset_of_eq_total_diag_len_upto(nums, max_d);\n assert(Self::offset_of(nums, max_d + 1) == Self::total_len(nums, m));\n Self::lemma_offset_of_nonneg(nums, d);\n if d <= max_d + 1 {\n Self::lemma_offset_of_nondecr(nums, d, max_d + 1);\n } else {\n Self::lemma_offset_of_nondecr(nums, max_d + 1, d);\n Self::lemma_offset_of_stable_above_max(nums, max_d, d);\n }\n }\n\n proof fn lemma_offset_of_stable_above_max(nums: Seq>, max_d: int, d: int)\n requires\n nums.len() >= 1,\n max_d >= 0,\n d > max_d + 1,\n forall |r: int| 0 <= r < nums.len() ==> max_d >= r + (#[trigger] nums[r]).len() as int - 1,\n ensures\n Self::offset_of(nums, d) == Self::offset_of(nums, max_d + 1),\n decreases d - max_d,\n {\n if d > max_d + 2 {\n Self::lemma_offset_of_stable_above_max(nums, max_d, d - 1);\n Self::lemma_count_of_zero_above_max(nums, max_d, d - 1);\n } else {\n assert(d == max_d + 2);\n Self::lemma_count_of_zero_above_max(nums, max_d, max_d + 1);\n }\n }\n\n proof fn lemma_count_of_zero_above_max(nums: Seq>, max_d: int, d: int)\n requires\n nums.len() >= 1,\n max_d >= 0,\n d >= max_d + 1,\n d >= 0,\n forall |r: int| 0 <= r < nums.len() ==> max_d >= r + (#[trigger] nums[r]).len() as int - 1,\n ensures\n Self::count_of(nums, d) == 0,\n {\n Self::lemma_count_of_eq_fwd_len(nums, d);\n let m = nums.len() as int;\n assert(Self::diag_seg_fwd(nums, d, 0, m - 1).len() == 0) by {\n Self::lemma_diag_seg_fwd_all_empty(nums, d, m - 1, max_d);\n }\n }\n\n proof fn lemma_diag_seg_fwd_all_empty(nums: Seq>, d: int, hi: int, max_d: int)\n requires\n d >= max_d + 1,\n forall |r: int| 0 <= r < nums.len() ==> max_d >= r + (#[trigger] nums[r]).len() as int - 1,\n ensures\n Self::diag_seg_fwd(nums, d, 0, hi).len() == 0,\n decreases (if hi >= 0 { hi + 1 } else { 0 }) as nat,\n {\n if hi >= 0 {\n Self::lemma_diag_seg_fwd_all_empty(nums, d, hi - 1, max_d);\n Self::lemma_diag_seg_fwd_snoc(nums, d, 0, hi);\n if 0 <= hi < nums.len() {\n assert(max_d >= hi + nums[hi].len() as int - 1);\n assert(!(0 <= d - hi && d - hi < nums[hi].len()));\n }\n } else {\n assert(Self::diag_seg_fwd(nums, d, 0, hi) =~= Seq::::empty());\n }\n }\n\n proof fn lemma_count_of_bound(nums: Seq>, d: int)\n requires\n 0 <= d,\n nums.len() >= 1,\n ensures\n 0 <= Self::count_of(nums, d) <= nums.len() as int,\n {\n let m = nums.len() as int;\n let start_i = if d < m { d } else { m - 1 };\n Self::lemma_reverse_seg(nums, d, 0, start_i);\n Self::lemma_diag_seg_fwd_len_bound(nums, d, start_i);\n assert(Self::diag_seg(nums, d, start_i, 0).len() == Self::diag_seg_fwd(nums, d, 0, start_i).reverse().len());\n assert(Self::diag_seg_fwd(nums, d, 0, start_i).reverse().len() == Self::diag_seg_fwd(nums, d, 0, start_i).len());\n }\n\n proof fn lemma_offset_of_bound(nums: Seq>, d: int)\n requires\n 0 <= d,\n nums.len() >= 1,\n ensures\n 0 <= Self::offset_of(nums, d) <= d * (nums.len() as int),\n decreases d,\n {\n if d > 0 {\n Self::lemma_offset_of_bound(nums, d - 1);\n Self::lemma_count_of_bound(nums, d - 1);\n let m = nums.len() as int;\n assert(Self::offset_of(nums, d) <= d * m) by (nonlinear_arith)\n requires\n Self::offset_of(nums, d) == Self::offset_of(nums, d - 1) + Self::count_of(nums, d - 1),\n Self::offset_of(nums, d - 1) <= (d - 1) * m,\n Self::count_of(nums, d - 1) <= m;\n }\n }\n\n proof fn lemma_offset_of_nonneg(nums: Seq>, d: int)\n requires\n d >= 0,\n nums.len() >= 1,\n ensures\n Self::offset_of(nums, d) >= 0,\n decreases d,\n {\n if d > 0 {\n Self::lemma_offset_of_nonneg(nums, d - 1);\n Self::lemma_count_of_bound(nums, d - 1);\n }\n }\n\n proof fn lemma_offset_of_nondecr(nums: Seq>, d1: int, d2: int)\n requires\n 0 <= d1 <= d2,\n ensures\n Self::offset_of(nums, d1) <= Self::offset_of(nums, d2),\n decreases d2 - d1,\n {\n if d1 < d2 {\n Self::lemma_offset_of_nondecr(nums, d1 + 1, d2);\n assert(Self::count_of(nums, d1) >= 0);\n }\n }\n\n proof fn lemma_offset_seq_mono(offset: Seq, bound: int, i: int, j: int)\n requires\n 0 <= i <= j <= bound,\n bound < offset.len(),\n forall |k: int| 0 <= k < bound ==> #[trigger] offset[k] <= offset[k + 1],\n ensures\n offset[i] <= offset[j],\n decreases j - i,\n {\n if i < j {\n Self::lemma_offset_seq_mono(offset, bound, i, j - 1);\n let k = j - 1;\n assert(0 <= k < bound);\n assert(offset[k] <= offset[k + 1]);\n }\n }\n\n proof fn lemma_diag_order_from_result(nums: Seq>, result: Seq, offset: Seq, max_d: int)\n requires\n max_d >= -1,\n offset.len() >= max_d + 2,\n forall |dd: int| 0 <= dd <= max_d + 1 ==> (#[trigger] offset[dd]) as int == Self::offset_of(nums, dd),\n forall |dd: int| 0 <= dd <= max_d ==>\n result.subrange(#[trigger] offset[dd] as int, offset[dd + 1] as int)\n =~= Self::diag_seg(nums, dd, (if dd < nums.len() as int { dd } else { nums.len() as int - 1 }), 0),\n result.len() == offset[max_d + 1] as int,\n ensures\n result =~= Self::diag_order(nums, max_d),\n decreases max_d + 1,\n {\n if max_d < 0 {\n assert(offset[0] as int == Self::offset_of(nums, 0));\n assert(Self::offset_of(nums, 0) == 0);\n assert(offset[0] as int == 0);\n assert(offset[max_d + 1] as int == Self::offset_of(nums, max_d + 1));\n assert(max_d + 1 == 0);\n assert(result.len() == offset[max_d + 1] as int);\n assert(result.len() == 0);\n } else {\n Self::lemma_offset_of_nondecr(nums, 0, max_d);\n let prefix = result.subrange(0, offset[max_d] as int);\n assert forall |dd: int| 0 <= dd <= max_d - 1 implies\n prefix.subrange(#[trigger] offset[dd] as int, offset[dd + 1] as int)\n =~= Self::diag_seg(nums, dd, (if dd < nums.len() as int { dd } else { nums.len() as int - 1 }), 0) by {\n Self::lemma_offset_of_nondecr(nums, dd + 1, max_d);\n assert(prefix.subrange(offset[dd] as int, offset[dd + 1] as int)\n =~= result.subrange(offset[dd] as int, offset[dd + 1] as int));\n }\n Self::lemma_diag_order_from_result(nums, prefix, offset, max_d - 1);\n assert(prefix =~= Self::diag_order(nums, max_d - 1));\n assert(result =~= prefix + result.subrange(offset[max_d] as int, offset[max_d + 1] as int));\n let m = nums.len() as int;\n let start_i = if max_d < m { max_d } else { m - 1 };\n assert(Self::diag_order(nums, max_d) =~= Self::diag_order(nums, max_d - 1) + Self::diag_seg(nums, max_d, start_i, 0));\n }\n }\n\n pub fn find_diagonal_order(nums: Vec>) -> (result: Vec)\n requires\n 1 <= nums@.len() <= 100000,\n forall |i: int| 0 <= i < nums@.len() ==>\n 1 <= (#[trigger] nums@[i]).len() <= 100000,\n forall |i: int, j: int| 0 <= i < nums@.len() && 0 <= j < nums@[i].len() ==>\n 1 <= (#[trigger] nums@[i][j]) <= 100000,\n 1 <= Self::total_len(nums@, nums@.len() as int) <= 100000,\n ensures\n result@ == Self::diag_order(nums@, Self::max_diag_val(nums@, 0)),\n {\n let m = nums.len();\n\n let mut max_d: usize = 0;\n let mut i: usize = m;\n while i > 0\n invariant\n 0 <= i <= m,\n m == nums.len(),\n m <= 100000,\n forall |k: int| 0 <= k < nums@.len() ==>\n 1 <= (#[trigger] nums@[k]).len() <= 100000,\n max_d as int == Self::max_diag_val(nums@, i as int),\n max_d <= 199999,\n decreases i,\n {\n i = i - 1;\n proof {\n Self::max_diag_bound(nums@, i as int + 1);\n }\n let d = i + nums[i].len() - 1;\n if d > max_d {\n max_d = d;\n }\n }\n\n let mut count: Vec = Vec::new();\n let mut k: usize = 0;\n while k <= max_d\n invariant\n count.len() == k,\n k <= max_d + 1,\n max_d <= 199999,\n forall |dd: int| 0 <= dd < k ==> count@[dd] == 0,\n decreases max_d + 1 - k,\n {\n count.push(0);\n k = k + 1;\n }\n\n let mut i2: usize = 0;\n while i2 < m\n invariant\n count.len() == max_d + 1,\n 0 <= i2 <= m,\n m == nums.len(),\n m <= 100000,\n max_d as int == Self::max_diag_val(nums@, 0),\n max_d <= 199999,\n forall |kk: int| 0 <= kk < nums@.len() ==>\n 1 <= (#[trigger] nums@[kk]).len() <= 100000,\n forall |dd: int| 0 <= dd <= max_d as int ==>\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int - 1).len() as int,\n decreases m - i2,\n {\n let row_len = nums[i2].len();\n let mut j: usize = 0;\n proof {\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n (if dd < i2 as int {\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int).len() as int\n } else {\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int - 1).len() as int\n }) by {\n if dd < i2 as int {\n assert(!(0 <= i2 as int && (i2 as int) < nums@.len() && 0 <= dd - i2 as int && (dd - i2 as int) < nums@[i2 as int].len()));\n Self::lemma_diag_seg_fwd_snoc(nums@, dd, 0, i2 as int);\n }\n }\n }\n while j < row_len\n invariant\n count.len() == max_d + 1,\n 0 <= j <= row_len,\n row_len == nums@[i2 as int].len(),\n i2 < m,\n m == nums.len(),\n m <= 100000,\n max_d as int == Self::max_diag_val(nums@, 0),\n max_d <= 199999,\n forall |kk: int| 0 <= kk < nums@.len() ==>\n 1 <= (#[trigger] nums@[kk]).len() <= 100000,\n forall |dd: int| 0 <= dd <= max_d as int ==>\n (if dd < i2 as int + j as int {\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int).len() as int\n } else {\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int - 1).len() as int\n }),\n decreases row_len - j,\n {\n proof {\n assert(row_len <= 100000);\n assert(i2 < m && m <= 100000);\n assert(i2 + j <= 200000);\n }\n let d = i2 + j;\n let ghost count_before = count@;\n proof {\n Self::lemma_max_diag_is_upper_bound(nums@, 0, i2 as int);\n Self::lemma_diag_seg_fwd_snoc(nums@, d as int, 0, i2 as int);\n assert(d <= max_d);\n Self::lemma_diag_seg_fwd_len_bound(nums@, d as int, i2 as int - 1);\n assert(count@[d as int] as int <= i2 as int);\n assert(count@[d as int] as int <= 100000);\n }\n count[d] = count[d] + 1;\n proof {\n assert(count@ =~= count_before.update(d as int, (count_before[d as int] + 1) as usize));\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n (if dd < i2 as int + (j as int + 1) {\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int).len() as int\n } else {\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int - 1).len() as int\n }) by {\n if dd == d as int {\n assert(Self::diag_seg_fwd(nums@, dd, 0, i2 as int) =~= Self::diag_seg_fwd(nums@, dd, 0, i2 as int - 1)\n + (if 0 <= i2 as int && (i2 as int) < nums@.len() && 0 <= dd - i2 as int && (dd - i2 as int) < nums@[i2 as int].len() {\n seq![nums@[i2 as int][dd - i2 as int]]\n } else {\n Seq::::empty()\n }));\n } else {\n assert(count@[dd] == count_before[dd]);\n }\n }\n }\n j = j + 1;\n }\n proof {\n Self::lemma_max_diag_is_upper_bound(nums@, 0, i2 as int);\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n count@[dd] as int == Self::diag_seg_fwd(nums@, dd, 0, i2 as int).len() as int by {\n if dd >= i2 as int + row_len as int {\n assert(!(0 <= i2 as int && (i2 as int) < nums@.len() && 0 <= dd - i2 as int && (dd - i2 as int) < nums@[i2 as int].len()));\n Self::lemma_diag_seg_fwd_snoc(nums@, dd, 0, i2 as int);\n }\n }\n }\n i2 = i2 + 1;\n }\n\n proof {\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n count@[dd] as int == Self::count_of(nums@, dd) by {\n Self::lemma_count_of_eq_fwd_len(nums@, dd);\n }\n }\n\n let mut offset: Vec = Vec::new();\n offset.push(0);\n let mut k2: usize = 0;\n while k2 <= max_d\n invariant\n offset.len() == k2 + 1,\n k2 <= max_d + 1,\n max_d <= 199999,\n m == nums.len(),\n 1 <= m <= 100000,\n count.len() == max_d + 1,\n forall |kk: int| 0 <= kk < nums@.len() ==>\n 1 <= (#[trigger] nums@[kk]).len() <= 100000,\n 1 <= Self::total_len(nums@, nums@.len() as int) <= 100000,\n forall |dd: int| 0 <= dd <= max_d as int ==> count@[dd] as int == Self::count_of(nums@, dd),\n forall |dd: int| 0 <= dd <= k2 as int ==> offset@[dd] as int == Self::offset_of(nums@, dd),\n decreases max_d + 1 - k2,\n {\n proof {\n Self::lemma_offset_of_le_total_len(nums@, k2 as int);\n Self::lemma_count_of_bound(nums@, k2 as int);\n assert(offset@[k2 as int] as int <= 100000);\n assert(count@[k2 as int] as int <= m as int);\n assert(count@[k2 as int] as int <= 100000);\n }\n let next = offset[k2] + count[k2];\n proof {\n assert(next as int == Self::offset_of(nums@, k2 as int) + Self::count_of(nums@, k2 as int));\n assert(next as int == Self::offset_of(nums@, k2 as int + 1));\n }\n offset.push(next);\n k2 = k2 + 1;\n }\n\n let total = offset[max_d + 1];\n\n proof {\n Self::lemma_offset_of_eq_diag_order_len(nums@, max_d as int + 1);\n assert(total as int == offset@[max_d as int + 1] as int);\n assert(total as int == Self::diag_order(nums@, max_d as int).len());\n assert forall |dd: int| 0 <= dd <= max_d as int implies offset@[dd] <= #[trigger] offset@[dd + 1] by {\n assert(offset@[dd] as int == Self::offset_of(nums@, dd));\n assert(offset@[dd + 1] as int == Self::offset_of(nums@, dd + 1));\n Self::lemma_offset_of_nondecr(nums@, dd, dd + 1);\n }\n }\n\n let mut result: Vec = Vec::new();\n let mut z: usize = 0;\n while z < total\n invariant\n result.len() == z,\n z <= total,\n decreases total - z,\n {\n result.push(0);\n z = z + 1;\n }\n\n let mut cursor: Vec = Vec::new();\n let mut k3: usize = 0;\n while k3 <= max_d\n invariant\n cursor.len() == k3,\n k3 <= max_d + 1,\n offset.len() == max_d + 2,\n forall |dd: int| 0 <= dd < k3 ==> cursor@[dd] == offset@[dd + 1],\n decreases max_d + 1 - k3,\n {\n cursor.push(offset[k3 + 1]);\n k3 = k3 + 1;\n }\n\n let mut i3: usize = 0;\n proof {\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).reverse() by {\n assert(cursor@[dd] == offset@[dd + 1]);\n Self::lemma_offset_of_nondecr(nums@, dd + 1, max_d as int + 1);\n assert(offset@[dd + 1] as int == Self::offset_of(nums@, dd + 1));\n assert(offset@[max_d as int + 1] as int == Self::offset_of(nums@, max_d as int + 1));\n assert(offset@[dd + 1] as int <= offset@[max_d as int + 1] as int);\n assert(0 <= offset@[dd + 1] as int <= result@.len());\n assert(result@.subrange(offset@[dd + 1] as int, offset@[dd + 1] as int) =~= Seq::::empty());\n assert(Self::diag_seg_fwd(nums@, dd, 0, -1) =~= Seq::::empty());\n }\n }\n while i3 < m\n invariant\n cursor.len() == max_d + 1,\n offset.len() == max_d + 2,\n result.len() == total,\n total as int == offset@[max_d as int + 1] as int,\n 0 <= i3 <= m,\n m == nums.len(),\n m <= 100000,\n max_d as int == Self::max_diag_val(nums@, 0),\n max_d <= 199999,\n forall |kk: int| 0 <= kk < nums@.len() ==>\n 1 <= (#[trigger] nums@[kk]).len() <= 100000,\n forall |dd: int| 0 <= dd <= max_d as int + 1 ==> (#[trigger] offset@[dd]) as int == Self::offset_of(nums@, dd),\n forall |dd: int| 0 <= dd <= max_d as int ==>\n offset@[dd] as int <= #[trigger] cursor@[dd] as int <= offset@[dd + 1] as int,\n forall |dd: int| 0 <= dd <= max_d as int ==>\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).len() as int,\n forall |dd: int| 0 <= dd <= max_d as int ==>\n result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).reverse(),\n decreases m - i3,\n {\n let row_len3 = nums[i3].len();\n let mut j3: usize = 0;\n proof {\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n (if dd < i3 as int {\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int).len() as int\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int).reverse()\n } else {\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).len() as int\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).reverse()\n }) by {\n if dd < i3 as int {\n assert(!(0 <= i3 as int && (i3 as int) < nums@.len() && 0 <= dd - i3 as int && (dd - i3 as int) < nums@[i3 as int].len()));\n Self::lemma_diag_seg_fwd_snoc(nums@, dd, 0, i3 as int);\n }\n }\n }\n while j3 < row_len3\n invariant\n cursor.len() == max_d + 1,\n offset.len() == max_d + 2,\n result.len() == total,\n total as int == offset@[max_d as int + 1] as int,\n 0 <= j3 <= row_len3,\n row_len3 == nums@[i3 as int].len(),\n i3 < m,\n m == nums.len(),\n m <= 100000,\n max_d as int == Self::max_diag_val(nums@, 0),\n max_d <= 199999,\n forall |kk: int| 0 <= kk < nums@.len() ==>\n 1 <= (#[trigger] nums@[kk]).len() <= 100000,\n forall |dd: int| 0 <= dd <= max_d as int + 1 ==> (#[trigger] offset@[dd]) as int == Self::offset_of(nums@, dd),\n forall |dd: int| 0 <= dd <= max_d as int ==>\n offset@[dd] as int <= #[trigger] cursor@[dd] as int <= offset@[dd + 1] as int,\n forall |dd: int| 0 <= dd <= max_d as int ==>\n (if dd < i3 as int + j3 as int {\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int).len() as int\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int).reverse()\n } else {\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).len() as int\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).reverse()\n }),\n decreases row_len3 - j3,\n {\n let d = i3 + j3;\n proof {\n Self::lemma_max_diag_is_upper_bound(nums@, 0, i3 as int);\n Self::lemma_diag_seg_fwd_snoc(nums@, d as int, 0, i3 as int);\n assert(Self::diag_seg_fwd(nums@, d as int, 0, i3 as int).len()\n == Self::diag_seg_fwd(nums@, d as int, 0, i3 as int - 1).len() + 1);\n Self::lemma_diag_seg_fwd_len_monotone(nums@, d as int, i3 as int, m as int - 1);\n Self::lemma_count_of_eq_fwd_len(nums@, d as int);\n assert(Self::diag_seg_fwd(nums@, d as int, 0, i3 as int).len() <= Self::count_of(nums@, d as int));\n assert(offset@[d as int] as int == Self::offset_of(nums@, d as int));\n assert(offset@[d as int + 1] as int == Self::offset_of(nums@, d as int + 1));\n assert(Self::offset_of(nums@, d as int + 1) == Self::offset_of(nums@, d as int) + Self::count_of(nums@, d as int));\n assert(Self::count_of(nums@, d as int) == offset@[d as int + 1] as int - offset@[d as int] as int);\n assert((offset@[d as int] as int) < (cursor@[d as int] as int));\n assert(cursor@[d as int] as int >= 1);\n assert(cursor@[d as int] as int <= offset@[d as int + 1] as int);\n }\n let old_cursor_d = cursor[d];\n cursor[d] = cursor[d] - 1;\n proof {\n assert((cursor@[d as int] as int) < (offset@[d as int + 1] as int));\n Self::lemma_offset_of_nondecr(nums@, d as int + 1, max_d as int + 1);\n assert(offset@[d as int + 1] as int == Self::offset_of(nums@, d as int + 1));\n assert(offset@[max_d as int + 1] as int == Self::offset_of(nums@, max_d as int + 1));\n assert(offset@[d as int + 1] as int <= offset@[max_d as int + 1] as int);\n assert(total as int == offset@[max_d as int + 1] as int);\n assert((cursor@[d as int] as int) < (total as int));\n }\n let val = nums[i3][j3];\n let ghost result_before = result@;\n let ghost cursor_before = cursor@;\n result.set(cursor[d], val);\n proof {\n assert(result@ =~= result_before.update(old_cursor_d as int - 1, val));\n assert(cursor@ =~= cursor_before.update(d as int, (old_cursor_d - 1) as usize));\n let extra = if 0 <= i3 as int && (i3 as int) < nums@.len() && 0 <= d as int - i3 as int && (d as int - i3 as int) < nums@[i3 as int].len() {\n seq![nums@[i3 as int][d as int - i3 as int]]\n } else {\n Seq::::empty()\n };\n assert(Self::diag_seg_fwd(nums@, d as int, 0, i3 as int) =~= Self::diag_seg_fwd(nums@, d as int, 0, i3 as int - 1) + extra);\n assert(extra == seq![val]);\n assert(result@.subrange(cursor@[d as int] as int, offset@[d as int + 1] as int)\n =~= seq![val] + result_before.subrange(old_cursor_d as int, offset@[d as int + 1] as int));\n\n assert forall |dd: int| 0 <= dd < d as int implies\n (cursor@[dd] == cursor_before[dd]\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= result_before.subrange(cursor@[dd] as int, offset@[dd + 1] as int)) by {\n Self::lemma_offset_of_nondecr(nums@, dd + 1, d as int);\n assert(offset@[dd + 1] as int == Self::offset_of(nums@, dd + 1));\n assert(offset@[d as int] as int == Self::offset_of(nums@, d as int));\n assert(offset@[dd + 1] as int <= offset@[d as int] as int);\n assert(cursor@[d as int] as int >= offset@[d as int] as int);\n }\n assert forall |dd: int| (d as int) < dd && dd <= max_d as int implies\n (cursor@[dd] == cursor_before[dd]\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= result_before.subrange(cursor@[dd] as int, offset@[dd + 1] as int)) by {\n Self::lemma_offset_of_nondecr(nums@, d as int + 1, dd);\n assert(offset@[d as int + 1] as int == Self::offset_of(nums@, d as int + 1));\n assert(offset@[dd] as int == Self::offset_of(nums@, dd));\n assert(offset@[d as int + 1] as int <= offset@[dd] as int);\n assert((cursor@[d as int] as int) < (offset@[d as int + 1] as int));\n assert(offset@[dd] as int <= cursor@[dd] as int);\n assert((cursor@[d as int] as int) < (cursor@[dd] as int));\n assert(cursor@[dd] == cursor_before.update(d as int, (old_cursor_d - 1) as usize)[dd]);\n Self::lemma_offset_of_nondecr(nums@, dd + 1, max_d as int + 1);\n assert(offset@[max_d as int + 1] as int == Self::offset_of(nums@, max_d as int + 1));\n assert(offset@[dd + 1] as int <= offset@[max_d as int + 1] as int);\n assert(offset@[max_d as int + 1] as int == total as int);\n assert(total as int == result@.len() as int);\n assert(offset@[dd + 1] as int <= result@.len() as int);\n assert((old_cursor_d as int) - 1 < result@.len() as int);\n assert forall |kk: int| cursor@[dd] as int <= kk < offset@[dd + 1] as int implies\n result@[kk] == result_before[kk] by {\n assert(result@[kk] == result_before.update(old_cursor_d as int - 1, val)[kk]);\n assert(kk != old_cursor_d as int - 1);\n }\n assert(result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= result_before.subrange(cursor@[dd] as int, offset@[dd + 1] as int));\n }\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n (if dd < i3 as int + (j3 as int + 1) {\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int).len() as int\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int).reverse()\n } else {\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).len() as int\n && result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int - 1).reverse()\n }) by {\n if dd == d as int {\n } else if dd < d as int {\n } else {\n }\n }\n }\n j3 = j3 + 1;\n }\n proof {\n Self::lemma_max_diag_is_upper_bound(nums@, 0, i3 as int);\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n (offset@[dd + 1] as int - cursor@[dd] as int) == Self::diag_seg_fwd(nums@, dd, 0, i3 as int).len() as int by {\n if dd >= i3 as int + row_len3 as int {\n assert(!(0 <= i3 as int && (i3 as int) < nums@.len() && 0 <= dd - i3 as int && (dd - i3 as int) < nums@[i3 as int].len()));\n Self::lemma_diag_seg_fwd_snoc(nums@, dd, 0, i3 as int);\n }\n }\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n result@.subrange(cursor@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg_fwd(nums@, dd, 0, i3 as int).reverse() by {\n if dd >= i3 as int + row_len3 as int {\n assert(!(0 <= i3 as int && (i3 as int) < nums@.len() && 0 <= dd - i3 as int && (dd - i3 as int) < nums@[i3 as int].len()));\n Self::lemma_diag_seg_fwd_snoc(nums@, dd, 0, i3 as int);\n }\n }\n }\n i3 = i3 + 1;\n }\n\n proof {\n assert forall |dd: int| 0 <= dd <= max_d as int implies\n result@.subrange(#[trigger] offset@[dd] as int, offset@[dd + 1] as int)\n =~= Self::diag_seg(nums@, dd, (if dd < m as int { dd } else { m as int - 1 }), 0) by {\n Self::lemma_max_diag_is_upper_bound(nums@, 0, m as int - 1);\n assert(cursor@[dd] == offset@[dd]);\n Self::lemma_reverse_seg(nums@, dd, 0, (if dd < m as int { dd } else { m as int - 1 }));\n if dd < m as int {\n Self::lemma_diag_seg_fwd_trunc(nums@, dd, m as int - 1, dd);\n }\n }\n Self::lemma_diag_order_from_result(nums@, result@, offset@, max_d as int);\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1424/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1424/tests/mutated_testcases.jsonl"} {"problem": "lc1438", "family": "leetcode", "description": "# Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit\n\nGiven an array of integers `nums` and an integer `limit`, return the size of the longest non-empty subarray such that the absolute difference between any two elements in the subarray is less than or equal to `limit`.\n\n## Example 1:\n\n> **Input:** nums = [8,2,4,7], limit = 4\n> **Output:** 2\n> **Explanation:** The longest valid subarrays are [2,4] and [4,7], both of length 2.\n\n## Example 2:\n\n> **Input:** nums = [10,1,2,4,7,2], limit = 5\n> **Output:** 4\n> **Explanation:** The subarray [2,4,7,2] is the longest valid subarray since $|2 - 7| = 5$.\n\n## Example 3:\n\n> **Input:** nums = [4,2,2,2,4,4,2,2], limit = 0\n> **Output:** 3\n\n## Constraints:\n\n- $1 <= \\text{nums.length} <= 10^5$\n- $1 <= \\text{nums}[i] <= 10^9$\n- $0 <= \\text{limit} <= 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_subarray(nums: Vec, limit: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nStaff, Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue, Weekly Contest 187\nAcceptance rate: 57.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn window_ok(nums: Seq, limit: int, l: int, r: int) -> bool {\n &&& 0 <= l < r <= nums.len()\n &&& forall |i: int, j: int| l <= i < r && l <= j < r ==> (nums[i] as int - nums[j] as int) <= limit\n }\n\n pub fn longest_subarray(nums: Vec, limit: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result <= nums.len() as i32,\n exists |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) && result as int == r - l,\n forall |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) ==> r - l <= result as int,\n {\n }\n}\n\n}", "code": "impl Solution {\n pub fn longest_subarray(nums: Vec, limit: i32) -> i32\n {\n let n = nums.len();\n let mut max_q: Vec = Vec::new();\n let mut min_q: Vec = Vec::new();\n let mut init_i: usize = 0;\n while init_i < n {\n max_q.push(0usize);\n min_q.push(0usize);\n init_i += 1;\n }\n let mut max_head: usize = 0;\n let mut max_tail: usize = 0;\n let mut min_head: usize = 0;\n let mut min_tail: usize = 0;\n let mut left: usize = 0;\n let mut right: usize = 0;\n let mut best: usize = 0;\n\n while right < n\n {\n let right_num = nums[right];\n while max_head < max_tail && nums[max_q[max_tail - 1]] < right_num\n {\n let last_idx = max_q[max_tail - 1];\n let last_num = nums[last_idx];\n max_tail -= 1;\n }\n max_q[max_tail] = right;\n max_tail += 1;\n\n while min_head < min_tail && nums[min_q[min_tail - 1]] > right_num\n {\n let last_idx = min_q[min_tail - 1];\n let last_num = nums[last_idx];\n min_tail -= 1;\n }\n min_q[min_tail] = right;\n min_tail += 1;\n\n while nums[max_q[max_head]] - nums[min_q[min_head]] > limit\n {\n if max_q[max_head] == left {\n max_head += 1;\n }\n if min_q[min_head] == left {\n min_head += 1;\n }\n left += 1;\n }\n\n let len = right - left + 1;\n if best < len {\n best = len;\n }\n right += 1;\n }\n\n best as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn window_ok(nums: Seq, limit: int, l: int, r: int) -> bool {\n &&& 0 <= l < r <= nums.len()\n &&& forall |i: int, j: int| l <= i < r && l <= j < r ==> (nums[i] as int - nums[j] as int) <= limit\n }\n\n pub fn longest_subarray(nums: Vec, limit: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result <= nums.len() as i32,\n exists |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) && result as int == r - l,\n forall |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) ==> r - l <= result as int,\n {\n let n = nums.len();\n let mut max_q: Vec = Vec::new();\n let mut min_q: Vec = Vec::new();\n let mut init_i: usize = 0;\n while init_i < n {\n max_q.push(0usize);\n min_q.push(0usize);\n init_i += 1;\n }\n let mut max_head: usize = 0;\n let mut max_tail: usize = 0;\n let mut min_head: usize = 0;\n let mut min_tail: usize = 0;\n let mut left: usize = 0;\n let mut right: usize = 0;\n let mut best: usize = 0;\n\n while right < n\n {\n let right_num = nums[right];\n while max_head < max_tail && nums[max_q[max_tail - 1]] < right_num\n {\n let last_idx = max_q[max_tail - 1];\n let last_num = nums[last_idx];\n max_tail -= 1;\n }\n max_q.set(max_tail, right);\n max_tail += 1;\n\n while min_head < min_tail && nums[min_q[min_tail - 1]] > right_num\n {\n let last_idx = min_q[min_tail - 1];\n let last_num = nums[last_idx];\n min_tail -= 1;\n }\n min_q.set(min_tail, right);\n min_tail += 1;\n\n while nums[max_q[max_head]] - nums[min_q[min_head]] > limit\n {\n if max_q[max_head] == left {\n max_head += 1;\n }\n if min_q[min_head] == left {\n min_head += 1;\n }\n left += 1;\n }\n\n let len = right - left + 1;\n if best < len {\n best = len;\n }\n right += 1;\n }\n\n best as i32\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn window_ok(nums: Seq, limit: int, l: int, r: int) -> bool {\n &&& 0 <= l < r <= nums.len()\n &&& forall |i: int, j: int| l <= i < r && l <= j < r ==> (nums[i] as int - nums[j] as int) <= limit\n }\n\n pub open spec fn covered_by_max_queue(nums: Seq, q: Seq, head: int, tail: int, right: int, k: int) -> bool {\n exists |t: int|\n head <= t < tail\n && k <= (q[t] as int) < right\n && nums[k] as int <= nums[q[t] as int] as int\n }\n\n pub open spec fn covered_by_min_queue(nums: Seq, q: Seq, head: int, tail: int, right: int, k: int) -> bool {\n exists |t: int|\n head <= t < tail\n && k <= (q[t] as int) < right\n && nums[q[t] as int] as int <= nums[k] as int\n }\n\n pub open spec fn max_queue_valid(nums: Seq, q: Seq, head: int, tail: int, left: int, right: int) -> bool {\n &&& 0 <= left < right <= nums.len()\n &&& 0 <= head < tail <= q.len()\n &&& forall |t: int| head <= t < tail ==> left <= #[trigger] (q[t] as int) < right\n &&& forall |a: int, b: int| head <= a < b < tail ==> #[trigger] q[a] < #[trigger] q[b]\n &&& forall |a: int, b: int| head <= a <= b < tail ==> #[trigger] (nums[q[a] as int] as int) >= #[trigger] (nums[q[b] as int] as int)\n &&& forall |k: int| left <= k < right ==> #[trigger] Self::covered_by_max_queue(nums, q, head, tail, right, k)\n }\n\n pub open spec fn min_queue_valid(nums: Seq, q: Seq, head: int, tail: int, left: int, right: int) -> bool {\n &&& 0 <= left < right <= nums.len()\n &&& 0 <= head < tail <= q.len()\n &&& forall |t: int| head <= t < tail ==> left <= #[trigger] (q[t] as int) < right\n &&& forall |a: int, b: int| head <= a < b < tail ==> #[trigger] q[a] < #[trigger] q[b]\n &&& forall |a: int, b: int| head <= a <= b < tail ==> #[trigger] (nums[q[a] as int] as int) <= #[trigger] (nums[q[b] as int] as int)\n &&& forall |k: int| left <= k < right ==> #[trigger] Self::covered_by_min_queue(nums, q, head, tail, right, k)\n }\n\n proof fn lemma_window_ok_from_bounds(nums: Seq, limit: int, l: int, r: int, min_idx: int, max_idx: int)\n requires\n 0 <= l < r <= nums.len(),\n l <= min_idx < r,\n l <= max_idx < r,\n nums[max_idx] as int - nums[min_idx] as int <= limit,\n forall |k: int| l <= k < r ==> nums[min_idx] as int <= #[trigger] nums[k] as int,\n forall |k: int| l <= k < r ==> #[trigger] nums[k] as int <= nums[max_idx] as int,\n ensures\n Self::window_ok(nums, limit, l, r),\n {\n assert forall |i: int, j: int| l <= i < r && l <= j < r implies #[trigger] (nums[i] as int - nums[j] as int) <= limit by {\n assert(nums[i] as int <= nums[max_idx] as int);\n assert(nums[min_idx] as int <= nums[j] as int);\n assert(nums[i] as int - nums[j] as int <= nums[max_idx] as int - nums[min_idx] as int);\n };\n }\n\n proof fn lemma_window_ok_subwindow(nums: Seq, limit: int, l1: int, r1: int, l2: int, r2: int)\n requires\n Self::window_ok(nums, limit, l1, r1),\n l1 <= l2 < r2 <= r1,\n ensures\n Self::window_ok(nums, limit, l2, r2),\n {\n assert forall |i: int, j: int| l2 <= i < r2 && l2 <= j < r2 implies #[trigger] (nums[i] as int - nums[j] as int) <= limit by {\n assert(l1 <= i < r1);\n assert(l1 <= j < r1);\n };\n }\n\n proof fn lemma_violation_not_window_ok(nums: Seq, limit: int, l: int, r: int, i: int, j: int)\n requires\n 0 <= l < r <= nums.len(),\n l <= i < r,\n l <= j < r,\n nums[i] as int - nums[j] as int > limit,\n ensures\n !Self::window_ok(nums, limit, l, r),\n {\n assert(!Self::window_ok(nums, limit, l, r)) by {\n if Self::window_ok(nums, limit, l, r) {\n assert((nums[i] as int - nums[j] as int) <= limit);\n assert(false);\n }\n \n };\n }\n\n proof fn lemma_max_front_bounds(nums: Seq, q: Seq, head: int, tail: int, left: int, right: int)\n requires\n Self::max_queue_valid(nums, q, head, tail, left, right),\n ensures\n forall |k: int| left <= k < right ==> #[trigger] nums[k] as int <= nums[q[head] as int] as int,\n {\n assert forall |k: int| left <= k < right implies #[trigger] nums[k] as int <= nums[q[head] as int] as int by {\n assert(Self::covered_by_max_queue(nums, q, head, tail, right, k));\n let t = choose |t: int| head <= t < tail && k <= (q[t] as int) < right && nums[k] as int <= nums[q[t] as int] as int;\n assert(head <= t < tail);\n assert(nums[k] as int <= nums[q[t] as int] as int);\n assert(nums[q[head] as int] as int >= nums[q[t] as int] as int);\n };\n }\n\n proof fn lemma_min_front_bounds(nums: Seq, q: Seq, head: int, tail: int, left: int, right: int)\n requires\n Self::min_queue_valid(nums, q, head, tail, left, right),\n ensures\n forall |k: int| left <= k < right ==> nums[q[head] as int] as int <= #[trigger] nums[k] as int,\n {\n assert forall |k: int| left <= k < right implies nums[q[head] as int] as int <= #[trigger] nums[k] as int by {\n assert(Self::covered_by_min_queue(nums, q, head, tail, right, k));\n let t = choose |t: int| head <= t < tail && k <= (q[t] as int) < right && nums[q[t] as int] as int <= nums[k] as int;\n assert(head <= t < tail);\n assert(nums[q[t] as int] as int <= nums[k] as int);\n assert(nums[q[head] as int] as int <= nums[q[t] as int] as int);\n };\n }\n\n proof fn lemma_max_queue_advance_left(nums: Seq, q: Seq, head: int, tail: int, left: int, right: int, new_head: int)\n requires\n Self::max_queue_valid(nums, q, head, tail, left, right),\n left + 1 < right,\n new_head == if q[head] as int == left { head + 1 } else { head },\n ensures\n Self::max_queue_valid(nums, q, new_head, tail, left + 1, right),\n {\n if q[head] as int == left {\n let k = left + 1;\n assert(Self::covered_by_max_queue(nums, q, head, tail, right, k));\n let t = choose |t: int| head <= t < tail && k <= (q[t] as int) < right && nums[k] as int <= nums[q[t] as int] as int;\n assert(head <= t < tail);\n assert(t != head) by {\n if t == head {\n assert(k <= q[head] as int);\n assert(false);\n }\n };\n assert(head + 1 <= t < tail);\n assert(Self::max_queue_valid(nums, q, head + 1, tail, left + 1, right)) by {\n assert forall |u: int| head + 1 <= u < tail implies left + 1 <= #[trigger] (q[u] as int) < right by {\n assert(left <= (q[u] as int) < right);\n assert(q[head] < q[u]);\n assert(q[head] as int == left);\n };\n assert forall |a: int, b: int| head + 1 <= a < b < tail implies #[trigger] q[a] < #[trigger] q[b] by {\n };\n assert forall |a: int, b: int| head + 1 <= a <= b < tail implies #[trigger] nums[q[a] as int] as int >= #[trigger] nums[q[b] as int] as int by {\n };\n assert forall |k: int| left + 1 <= k < right implies #[trigger] Self::covered_by_max_queue(nums, q, head + 1, tail, right, k) by {\n assert(Self::covered_by_max_queue(nums, q, head, tail, right, k));\n let t0 = choose |t0: int| head <= t0 < tail && k <= (q[t0] as int) < right && nums[k] as int <= nums[q[t0] as int] as int;\n assert(head <= t0 < tail);\n assert(t0 != head) by {\n if t0 == head {\n assert(k <= q[head] as int);\n assert(false);\n }\n };\n assert(head + 1 <= t0 < tail);\n };\n };\n } else {\n assert(Self::max_queue_valid(nums, q, head, tail, left + 1, right)) by {\n assert forall |u: int| head <= u < tail implies left + 1 <= #[trigger] (q[u] as int) < right by {\n assert(left <= (q[u] as int) < right);\n if u == head {\n assert((q[head] as int) != left);\n }\n };\n assert forall |a: int, b: int| head <= a < b < tail implies #[trigger] q[a] < #[trigger] q[b] by {\n };\n assert forall |a: int, b: int| head <= a <= b < tail implies #[trigger] nums[q[a] as int] as int >= #[trigger] nums[q[b] as int] as int by {\n };\n assert forall |k: int| left + 1 <= k < right implies #[trigger] Self::covered_by_max_queue(nums, q, head, tail, right, k) by {\n assert(Self::covered_by_max_queue(nums, q, head, tail, right, k));\n let t0 = choose |t0: int| head <= t0 < tail && k <= (q[t0] as int) < right && nums[k] as int <= nums[q[t0] as int] as int;\n assert(head <= t0 < tail);\n };\n };\n }\n }\n\n proof fn lemma_min_queue_advance_left(nums: Seq, q: Seq, head: int, tail: int, left: int, right: int, new_head: int)\n requires\n Self::min_queue_valid(nums, q, head, tail, left, right),\n left + 1 < right,\n new_head == if q[head] as int == left { head + 1 } else { head },\n ensures\n Self::min_queue_valid(nums, q, new_head, tail, left + 1, right),\n {\n if q[head] as int == left {\n let k = left + 1;\n assert(Self::covered_by_min_queue(nums, q, head, tail, right, k));\n let t = choose |t: int| head <= t < tail && k <= (q[t] as int) < right && nums[q[t] as int] as int <= nums[k] as int;\n assert(head <= t < tail);\n assert(t != head) by {\n if t == head {\n assert(k <= q[head] as int);\n assert(false);\n }\n };\n assert(head + 1 <= t < tail);\n assert(Self::min_queue_valid(nums, q, head + 1, tail, left + 1, right)) by {\n assert forall |u: int| head + 1 <= u < tail implies left + 1 <= #[trigger] (q[u] as int) < right by {\n assert(left <= (q[u] as int) < right);\n assert(q[head] < q[u]);\n assert(q[head] as int == left);\n };\n assert forall |a: int, b: int| head + 1 <= a < b < tail implies #[trigger] q[a] < #[trigger] q[b] by {\n };\n assert forall |a: int, b: int| head + 1 <= a <= b < tail implies #[trigger] nums[q[a] as int] as int <= #[trigger] nums[q[b] as int] as int by {\n };\n assert forall |k: int| left + 1 <= k < right implies #[trigger] Self::covered_by_min_queue(nums, q, head + 1, tail, right, k) by {\n assert(Self::covered_by_min_queue(nums, q, head, tail, right, k));\n let t0 = choose |t0: int| head <= t0 < tail && k <= (q[t0] as int) < right && nums[q[t0] as int] as int <= nums[k] as int;\n assert(head <= t0 < tail);\n assert(t0 != head) by {\n if t0 == head {\n assert(k <= q[head] as int);\n assert(false);\n }\n };\n assert(head + 1 <= t0 < tail);\n };\n };\n } else {\n assert(Self::min_queue_valid(nums, q, head, tail, left + 1, right)) by {\n assert forall |u: int| head <= u < tail implies left + 1 <= #[trigger] (q[u] as int) < right by {\n assert(left <= (q[u] as int) < right);\n if u == head {\n assert((q[head] as int) != left);\n }\n };\n assert forall |a: int, b: int| head <= a < b < tail implies #[trigger] q[a] < #[trigger] q[b] by {\n };\n assert forall |a: int, b: int| head <= a <= b < tail implies #[trigger] nums[q[a] as int] as int <= #[trigger] nums[q[b] as int] as int by {\n };\n assert forall |k: int| left + 1 <= k < right implies #[trigger] Self::covered_by_min_queue(nums, q, head, tail, right, k) by {\n assert(Self::covered_by_min_queue(nums, q, head, tail, right, k));\n let t0 = choose |t0: int| head <= t0 < tail && k <= (q[t0] as int) < right && nums[q[t0] as int] as int <= nums[k] as int;\n assert(head <= t0 < tail);\n };\n };\n }\n }\n\n pub fn longest_subarray(nums: Vec, limit: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result <= nums.len() as i32,\n exists |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) && result as int == r - l,\n forall |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) ==> r - l <= result as int,\n {\n let n = nums.len();\n let mut max_q: Vec = Vec::new();\n let mut min_q: Vec = Vec::new();\n let mut init_i: usize = 0;\n while init_i < n\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n max_q.len() == init_i,\n min_q.len() == init_i,\n forall |j: int| 0 <= j < init_i ==> max_q@[j] < n,\n forall |j: int| 0 <= j < init_i ==> min_q@[j] < n,\n init_i <= n,\n decreases n - init_i,\n {\n max_q.push(0usize);\n min_q.push(0usize);\n init_i += 1;\n }\n let mut max_head: usize = 0;\n let mut max_tail: usize = 0;\n let mut min_head: usize = 0;\n let mut min_tail: usize = 0;\n let mut left: usize = 0;\n let mut right: usize = 0;\n let mut best: usize = 0;\n let ghost mut best_l: int = 0;\n let ghost mut best_r: int = 0;\n\n while right < n\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n max_q.len() == n,\n min_q.len() == n,\n forall |j: int| 0 <= j < n ==> max_q@[j] < n,\n forall |j: int| 0 <= j < n ==> min_q@[j] < n,\n 0 <= left <= right <= n,\n 0 <= max_head <= max_tail <= n,\n 0 <= min_head <= min_tail <= n,\n max_tail <= right,\n min_tail <= right,\n right == 0 ==> left == 0,\n right > 0 ==> left < right,\n right > 0 ==> Self::max_queue_valid(nums@, max_q@, max_head as int, max_tail as int, left as int, right as int),\n right > 0 ==> Self::min_queue_valid(nums@, min_q@, min_head as int, min_tail as int, left as int, right as int),\n right > 0 ==> Self::window_ok(nums@, limit as int, left as int, right as int),\n right > 0 ==> forall |l0: int| 0 <= l0 < left as int ==> !Self::window_ok(nums@, limit as int, l0, right as int),\n 0 <= best <= right,\n right == 0 ==> best == 0,\n right > 0 ==> 1 <= best,\n best > 0 ==> 0 <= best_l < best_r <= right as int,\n best > 0 ==> Self::window_ok(nums@, limit as int, best_l, best_r),\n best > 0 ==> best == (best_r - best_l) as usize,\n forall |l0: int, r0: int| 0 <= l0 < r0 <= right as int && Self::window_ok(nums@, limit as int, l0, r0) ==> r0 - l0 <= best as int,\n decreases n - right,\n {\n let ghost old_right = right as int;\n let ghost old_left = left as int;\n let ghost old_best = best as int;\n let ghost old_best_l = best_l;\n let ghost old_best_r = best_r;\n let ghost next_right = old_right + 1;\n let right_num = nums[right];\n proof {\n assert(old_right == right as int);\n assert(next_right == old_right + 1);\n }\n\n let ghost max_before = max_q@;\n let ghost max_tail_before = max_tail as int;\n while max_head < max_tail && nums[max_q[max_tail - 1]] < right_num\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n max_q.len() == n,\n forall |j: int| 0 <= j < n ==> max_q@[j] < n,\n max_q@ == max_before,\n 0 <= left <= right < n,\n 0 <= max_head <= max_tail <= max_tail_before as usize <= n,\n max_tail_before <= right as int,\n forall |t: int| (max_tail as int) <= t < max_tail_before ==> (nums[max_before[t] as int] as int) < (right_num as int),\n decreases max_tail,\n {\n let last_idx = max_q[max_tail - 1];\n let last_num = nums[last_idx];\n proof {\n let last = max_tail as int - 1;\n assert(last_num < right_num);\n assert(last_idx == max_q@[last]);\n assert(max_q@[last] == max_before[last]);\n assert forall |t: int| (max_tail as int - 1) <= t < max_tail_before implies (nums[max_before[t] as int] as int) < (right_num as int) by {\n if t == max_tail as int - 1 {\n assert(max_before[t] == last_idx);\n assert(last_num as int == nums[max_before[t] as int] as int);\n assert((last_num as int) < (right_num as int));\n }\n };\n }\n max_tail -= 1;\n }\n proof {\n if right > 0 && max_head < max_tail {\n assert((nums[max_q[max_tail as int - 1] as int] as int) >= (right_num as int));\n }\n }\n let ghost max_prefix_tail = max_tail as int;\n let ghost max_before_set = max_q@;\n max_q.set(max_tail, right);\n max_tail += 1;\n proof {\n assert(max_q@ == max_before_set.update(max_prefix_tail, right));\n assert(Self::max_queue_valid(nums@, max_q@, max_head as int, max_tail as int, left as int, next_right)) by {\n assert forall |t: int| max_head as int <= t < max_tail as int implies left as int <= max_q@[t] as int by {\n if t < max_prefix_tail {\n assert(max_q@[t] == max_before_set[t]);\n assert(left as int <= max_before_set[t] as int);\n } else {\n assert(t == max_prefix_tail);\n assert(max_q@[t] == right);\n }\n };\n assert forall |t: int| max_head as int <= t < max_tail as int implies max_q@[t] < right + 1 by {\n if t < max_prefix_tail {\n assert(max_q@[t] == max_before_set[t]);\n assert(max_before_set[t] < right);\n } else {\n assert(t == max_prefix_tail);\n assert(max_q@[t] == right);\n }\n };\n assert forall |a: int, b: int| max_head as int <= a < b < max_tail as int implies #[trigger] max_q@[a] < #[trigger] max_q@[b] by {\n if b < max_prefix_tail {\n assert(max_q@[a] == max_before_set[a]);\n assert(max_q@[b] == max_before_set[b]);\n } else {\n assert(b == max_prefix_tail);\n assert(max_q@[b] == right);\n assert(max_q@[a] == max_before_set[a]);\n assert((max_before_set[a] as int) < old_right);\n }\n };\n assert forall |a: int, b: int| max_head as int <= a <= b < max_tail as int implies #[trigger] nums[max_q@[a] as int] as int >= #[trigger] nums[max_q@[b] as int] as int by {\n if b < max_prefix_tail {\n assert(max_q@[a] == max_before_set[a]);\n assert(max_q@[b] == max_before_set[b]);\n } else {\n assert(b == max_prefix_tail);\n assert(max_q@[b] == right);\n if a < max_prefix_tail {\n assert(max_q@[a] == max_before_set[a]);\n if max_prefix_tail > max_head as int {\n assert(nums[max_before_set[a] as int] as int >= nums[max_before_set[max_prefix_tail - 1] as int] as int);\n assert((nums[max_before_set[max_prefix_tail - 1] as int] as int) >= (right_num as int));\n }\n }\n }\n };\n assert forall |k: int| left as int <= k < next_right implies #[trigger] Self::covered_by_max_queue(nums@, max_q@, max_head as int, max_tail as int, next_right, k) by {\n if k == right as int {\n let t = max_prefix_tail;\n assert(max_head as int <= t < max_tail as int);\n assert(k <= max_q[t] as int && max_q[t] < right + 1);\n assert(nums[k] as int <= nums[max_q[t] as int] as int);\n } else {\n assert(right > 0);\n assert(left as int <= k < old_right);\n assert(Self::max_queue_valid(nums@, max_before, max_head as int, max_tail_before, left as int, old_right));\n assert(Self::covered_by_max_queue(nums@, max_before, max_head as int, max_tail_before, old_right, k));\n let t0 = choose |t: int| max_head as int <= t < max_tail_before && k <= max_before[t] as int && max_before[t] < right && nums[k] as int <= nums[max_before[t] as int] as int;\n if t0 < max_prefix_tail {\n let t = t0;\n assert(max_head as int <= t < max_tail as int);\n assert(max_q[t] == max_before[t]);\n assert(k <= max_q[t] as int && max_q[t] < right + 1);\n assert(nums[k] as int <= nums[max_q[t] as int] as int);\n } else {\n let t = max_prefix_tail;\n assert(max_head as int <= t < max_tail as int);\n assert((nums[max_before[t0] as int] as int) < (right_num as int));\n assert(nums[k] as int <= nums[max_before[t0] as int] as int);\n assert(k <= max_q[t] as int && max_q[t] < right + 1);\n assert(nums[k] as int <= nums[max_q[t] as int] as int);\n }\n }\n };\n }\n }\n\n let ghost min_before = min_q@;\n let ghost min_tail_before = min_tail as int;\n while min_head < min_tail && nums[min_q[min_tail - 1]] > right_num\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n min_q.len() == n,\n forall |j: int| 0 <= j < n ==> min_q@[j] < n,\n min_q@ == min_before,\n 0 <= left <= right < n,\n 0 <= min_head <= min_tail <= min_tail_before as usize <= n,\n min_tail_before <= right as int,\n forall |t: int| min_tail as int <= t < min_tail_before ==> (nums[min_before[t] as int] as int) > (right_num as int),\n decreases min_tail,\n {\n let last_idx = min_q[min_tail - 1];\n let last_num = nums[last_idx];\n proof {\n let last = min_tail as int - 1;\n assert(last_num > right_num);\n assert(last_idx == min_q@[last]);\n assert(min_q@[last] == min_before[last]);\n assert forall |t: int| (min_tail as int - 1) <= t < min_tail_before implies (nums[min_before[t] as int] as int) > (right_num as int) by {\n if t == min_tail as int - 1 {\n assert(min_before[t] == last_idx);\n assert(last_num as int == nums[min_before[t] as int] as int);\n assert((last_num as int) > (right_num as int));\n }\n };\n }\n min_tail -= 1;\n }\n proof {\n if right > 0 && min_head < min_tail {\n assert((nums[min_q[min_tail as int - 1] as int] as int) <= (right_num as int));\n }\n }\n let ghost min_prefix_tail = min_tail as int;\n let ghost min_before_set = min_q@;\n min_q.set(min_tail, right);\n min_tail += 1;\n proof {\n assert(min_q@ == min_before_set.update(min_prefix_tail, right));\n assert(Self::min_queue_valid(nums@, min_q@, min_head as int, min_tail as int, left as int, next_right)) by {\n assert forall |t: int| min_head as int <= t < min_tail as int implies left as int <= min_q@[t] as int by {\n if t < min_prefix_tail {\n assert(min_q@[t] == min_before_set[t]);\n assert(left as int <= min_before_set[t] as int);\n } else {\n assert(t == min_prefix_tail);\n assert(min_q@[t] == right);\n }\n };\n assert forall |t: int| min_head as int <= t < min_tail as int implies min_q@[t] < right + 1 by {\n if t < min_prefix_tail {\n assert(min_q@[t] == min_before_set[t]);\n assert(min_before_set[t] < right);\n } else {\n assert(t == min_prefix_tail);\n assert(min_q@[t] == right);\n }\n };\n assert forall |a: int, b: int| min_head as int <= a < b < min_tail as int implies #[trigger] min_q@[a] < #[trigger] min_q@[b] by {\n if b < min_prefix_tail {\n assert(min_q@[a] == min_before_set[a]);\n assert(min_q@[b] == min_before_set[b]);\n } else {\n assert(b == min_prefix_tail);\n assert(min_q@[b] == right);\n assert(min_q@[a] == min_before_set[a]);\n assert((min_before_set[a] as int) < old_right);\n }\n };\n assert forall |a: int, b: int| min_head as int <= a <= b < min_tail as int implies #[trigger] nums[min_q@[a] as int] as int <= #[trigger] nums[min_q@[b] as int] as int by {\n if b < min_prefix_tail {\n assert(min_q@[a] == min_before_set[a]);\n assert(min_q@[b] == min_before_set[b]);\n } else {\n assert(b == min_prefix_tail);\n assert(min_q@[b] == right);\n if a < min_prefix_tail {\n assert(min_q@[a] == min_before_set[a]);\n if min_prefix_tail > min_head as int {\n assert(nums[min_before_set[a] as int] as int <= nums[min_before_set[min_prefix_tail - 1] as int] as int);\n assert((nums[min_before_set[min_prefix_tail - 1] as int] as int) <= (right_num as int));\n }\n }\n }\n };\n assert forall |k: int| left as int <= k < next_right implies #[trigger] Self::covered_by_min_queue(nums@, min_q@, min_head as int, min_tail as int, next_right, k) by {\n if k == right as int {\n let t = min_prefix_tail;\n assert(min_head as int <= t < min_tail as int);\n assert(k <= min_q[t] as int && min_q[t] < right + 1);\n assert(nums[min_q[t] as int] as int <= nums[k] as int);\n } else {\n assert(right > 0);\n assert(left as int <= k < old_right);\n assert(Self::min_queue_valid(nums@, min_before, min_head as int, min_tail_before, left as int, old_right));\n assert(Self::covered_by_min_queue(nums@, min_before, min_head as int, min_tail_before, old_right, k));\n let t0 = choose |t: int| min_head as int <= t < min_tail_before && k <= min_before[t] as int && min_before[t] < right && nums[min_before[t] as int] as int <= nums[k] as int;\n if t0 < min_prefix_tail {\n let t = t0;\n assert(min_head as int <= t < min_tail as int);\n assert(min_q[t] == min_before[t]);\n assert(k <= min_q[t] as int && min_q[t] < right + 1);\n assert(nums[min_q[t] as int] as int <= nums[k] as int);\n } else {\n let t = min_prefix_tail;\n assert(min_head as int <= t < min_tail as int);\n assert((nums[min_before[t0] as int] as int) > (right_num as int));\n assert((right_num as int) < (nums[min_before[t0] as int] as int));\n assert(nums[min_q[t] as int] as int <= nums[k] as int);\n assert(k <= min_q[t] as int && min_q[t] < right + 1);\n }\n }\n };\n }\n }\n\n proof {\n if old_right > 0 {\n assert forall |l0: int| 0 <= l0 < old_left implies !Self::window_ok(nums@, limit as int, l0, next_right) by {\n if Self::window_ok(nums@, limit as int, l0, next_right) {\n Self::lemma_window_ok_subwindow(nums@, limit as int, l0, next_right, l0, old_right);\n assert(false);\n }\n };\n }\n }\n\n while nums[max_q[max_head]] - nums[min_q[min_head]] > limit\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= limit <= 1_000_000_000,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n 0 <= left < next_right <= n,\n 0 <= max_head <= max_tail <= n,\n 0 <= min_head <= min_tail <= n,\n Self::max_queue_valid(nums@, max_q@, max_head as int, max_tail as int, left as int, next_right),\n Self::min_queue_valid(nums@, min_q@, min_head as int, min_tail as int, left as int, next_right),\n forall |l0: int| 0 <= l0 < old_left ==> !Self::window_ok(nums@, limit as int, l0, next_right),\n forall |l0: int| old_left <= l0 < left as int ==> !Self::window_ok(nums@, limit as int, l0, next_right),\n decreases next_right - left as int,\n {\n let ghost prev_left = left as int;\n let ghost prev_max_head = max_head as int;\n let ghost prev_min_head = min_head as int;\n proof {\n Self::lemma_max_front_bounds(nums@, max_q@, max_head as int, max_tail as int, left as int, next_right);\n Self::lemma_min_front_bounds(nums@, min_q@, min_head as int, min_tail as int, left as int, next_right);\n let max_idx = max_q@[max_head as int] as int;\n let min_idx = min_q@[min_head as int] as int;\n assert(left as int <= max_idx && max_idx < next_right);\n assert(left as int <= min_idx && min_idx < next_right);\n assert(nums[max_idx] as int - nums[min_idx] as int > limit as int);\n assert forall |l0: int| left as int <= l0 <= left as int implies !Self::window_ok(nums@, limit as int, l0, next_right) by {\n if l0 == left as int {\n Self::lemma_violation_not_window_ok(nums@, limit as int, l0, next_right, max_idx, min_idx);\n }\n };\n assert forall |l0: int| old_left <= l0 < ((left as int) + 1) implies !Self::window_ok(nums@, limit as int, l0, next_right) by {\n if l0 < left as int {\n } else {\n assert(l0 == left as int);\n Self::lemma_violation_not_window_ok(nums@, limit as int, l0, next_right, max_idx, min_idx);\n }\n };\n assert(prev_left + 1 < next_right) by {\n if !(prev_left + 1 < next_right) {\n assert(prev_left + 1 == next_right);\n assert(!Self::window_ok(nums@, limit as int, prev_left, next_right));\n assert(Self::window_ok(nums@, limit as int, prev_left, next_right)) by {\n assert forall |i: int, j: int| prev_left <= i < next_right && prev_left <= j < next_right implies #[trigger] (nums[i] as int - nums[j] as int) <= limit as int by {\n assert(prev_left <= i < prev_left + 1);\n assert(prev_left <= j < prev_left + 1);\n assert(i == prev_left);\n assert(j == prev_left);\n };\n };\n assert(false);\n }\n };\n }\n if max_q[max_head] == left {\n max_head += 1;\n }\n if min_q[min_head] == left {\n min_head += 1;\n }\n left += 1;\n proof {\n assert(left as int == prev_left + 1);\n assert((next_right - left as int) < (next_right - prev_left));\n Self::lemma_max_queue_advance_left(nums@, max_q@, prev_max_head, max_tail as int, prev_left, next_right, max_head as int);\n Self::lemma_min_queue_advance_left(nums@, min_q@, prev_min_head, min_tail as int, prev_left, next_right, min_head as int);\n }\n }\n\n proof {\n if old_right > 0 {\n assert forall |l0: int| 0 <= l0 < old_left implies !Self::window_ok(nums@, limit as int, l0, next_right) by {\n if Self::window_ok(nums@, limit as int, l0, next_right) {\n Self::lemma_window_ok_subwindow(nums@, limit as int, l0, next_right, l0, old_right);\n assert(false);\n }\n };\n }\n Self::lemma_max_front_bounds(nums@, max_q@, max_head as int, max_tail as int, left as int, next_right);\n Self::lemma_min_front_bounds(nums@, min_q@, min_head as int, min_tail as int, left as int, next_right);\n let max_idx = max_q@[max_head as int] as int;\n let min_idx = min_q@[min_head as int] as int;\n assert(nums[max_idx] as int - nums[min_idx] as int <= limit as int);\n Self::lemma_window_ok_from_bounds(nums@, limit as int, left as int, next_right, min_idx, max_idx);\n }\n\n let len = right - left + 1;\n if best < len {\n proof {\n best_l = left as int;\n best_r = next_right;\n }\n best = len;\n }\n proof {\n if old_best > 0 && !(old_best < len as int) {\n assert(best == old_best as usize);\n assert(best_l == old_best_l);\n assert(best_r == old_best_r);\n }\n assert forall |l0: int, r0: int| 0 <= l0 < r0 <= next_right && Self::window_ok(nums@, limit as int, l0, r0) implies r0 - l0 <= best as int by {\n if r0 <= old_right {\n assert(r0 - l0 <= old_best);\n } else {\n assert(r0 == next_right);\n if l0 < left as int {\n assert(!Self::window_ok(nums@, limit as int, l0, r0));\n assert(false);\n }\n assert(l0 >= left as int);\n assert(r0 - l0 <= next_right - left as int);\n assert(next_right - left as int == len as int);\n assert(len as int <= best as int);\n }\n };\n if best > 0 {\n assert(0 <= best_l < best_r <= next_right);\n assert(Self::window_ok(nums@, limit as int, best_l, best_r));\n assert(best == (best_r - best_l) as usize);\n }\n if right == 0 {\n assert(best == 1);\n }\n }\n right += 1;\n }\n\n proof {\n assert(best > 0);\n assert(0 <= best_l < best_r <= n as int);\n assert(Self::window_ok(nums@, limit as int, best_l, best_r));\n assert(best == (best_r - best_l) as usize);\n assert(best <= 100_000);\n assert(exists |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) && best as int == r - l) by {\n let l = best_l;\n let r = best_r;\n assert(Self::window_ok(nums@, limit as int, l, r));\n assert(best as int == r - l);\n };\n assert forall |l: int, r: int| Self::window_ok(nums@, limit as int, l, r) implies r - l <= best as int by {\n };\n }\n\n best as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1438/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1438/tests/mutated_testcases.jsonl"} {"problem": "lc1439", "family": "leetcode", "description": "# Find the Kth Smallest Sum of a Matrix With Sorted Rows\n\nYou are given an `m x n` matrix `mat` that has its rows sorted in non-decreasing order and an integer `k`.\n\nYou are allowed to choose **exactly one element** from each row to form an array.\n\nReturn the `k`th smallest array sum among all possible arrays.\n\n## Example 1:\n\n> **Input:** mat = [[1,3,11],[2,4,6]], k = 5\n> **Output:** 7\n> **Explanation:** Choosing one element from each row, the first k smallest sum are: [1,2], [1,4], [3,2], [3,4], [1,6]. Where the 5th sum is 7.\n\n## Example 2:\n\n> **Input:** mat = [[1,3,11],[2,4,6]], k = 9\n> **Output:** 17\n\n## Example 3:\n\n> **Input:** mat = [[1,10,10],[1,4,5],[2,3,6]], k = 7\n> **Output:** 9\n> **Explanation:** Choosing one element from each row, the first k smallest sum are: [1,1,2], [1,1,3], [1,4,2], [1,4,3], [1,1,6], [1,5,2], [1,5,3]. Where the 7th sum is 9.\n\n## Constraints:\n\n- `m == mat.length`\n- `n == mat[i].length`\n- `1 <= m, n <= 40`\n- `1 <= mat[i][j] <= 5000`\n- `1 <= k <= min(200, n^m)`\n- `mat[i]` is a non-decreasing array.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn kth_smallest(mat: Vec>, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Binary Search, Heap (Priority Queue), Matrix\nAcceptance rate: 62.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_sums(mat: Seq>, row: int, col: int, remaining: int) -> nat\n decreases mat.len() - row, (if 0 <= row < mat.len() as int { mat[row].len() - col } else { 0 })\n{\n if row >= mat.len() as int {\n if remaining >= 0 { 1 } else { 0 }\n } else if row < 0 || col >= mat[row].len() as int {\n 0\n } else {\n count_sums(mat, row + 1, 0, remaining - mat[row][col] as int) +\n count_sums(mat, row, col + 1, remaining)\n }\n}\n\npub open spec fn total_combos(mat: Seq>, row: int) -> int\n decreases mat.len() - row\n{\n if row >= mat.len() as int { 1 }\n else { mat[row].len() as int * total_combos(mat, row + 1) }\n}\n\nimpl Solution {\n pub fn kth_smallest(mat: Vec>, k: i32) -> (result: i32)\n requires\n 1 <= mat.len() <= 40,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() >= 1 && mat[i].len() <= 40,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n 1 <= k <= 200,\n k as int <= total_combos(mat@, 0),\n ensures\n count_sums(mat@, 0, 0, result as int) >= k as int,\n count_sums(mat@, 0, 0, result as int - 1) < k as int,\n {\n }\n}\n\n}\n", "code": "fn merge_capped_exec(a: &Vec, b: &Vec, cap: usize) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while result.len() < cap && (i < a.len() || j < b.len()) {\n if j >= b.len() || (i < a.len() && a[i] <= b[j]) {\n result.push(a[i]);\n i += 1;\n } else {\n result.push(b[j]);\n j += 1;\n }\n }\n result\n}\n\nfn shift_exec(s: &Vec, shift: i32) -> Vec {\n let mut result: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < s.len() {\n result.push(s[idx] + shift);\n idx += 1;\n }\n result\n}\n\nfn fold_cols_exec(mat: &Vec>, row: usize, tail: &Vec, cap: usize) -> Vec {\n let n = mat[row].len();\n let mut acc: Vec = Vec::new();\n let mut c: usize = n;\n while c > 0 {\n c -= 1;\n let shifted = shift_exec(tail, mat[row][c]);\n acc = merge_capped_exec(&shifted, &acc, cap);\n }\n acc\n}\n\nfn capped_sums_exec(mat: &Vec>, cap: usize) -> Vec {\n let mut tail: Vec = Vec::new();\n if cap >= 1 {\n tail.push(0);\n }\n let m = mat.len();\n let mut row: usize = m;\n while row > 0 {\n row -= 1;\n tail = fold_cols_exec(mat, row, &tail, cap);\n }\n tail\n}\n\nimpl Solution {\n pub fn kth_smallest(mat: Vec>, k: i32) -> i32 {\n let l = capped_sums_exec(&mat, k as usize);\n let ans = l[(k - 1) as usize];\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_sums(mat: Seq>, row: int, col: int, remaining: int) -> nat\n decreases mat.len() - row, (if 0 <= row < mat.len() as int { mat[row].len() - col } else { 0 })\n{\n if row >= mat.len() as int {\n if remaining >= 0 { 1 } else { 0 }\n } else if row < 0 || col >= mat[row].len() as int {\n 0\n } else {\n count_sums(mat, row + 1, 0, remaining - mat[row][col] as int) +\n count_sums(mat, row, col + 1, remaining)\n }\n}\n\npub open spec fn total_combos(mat: Seq>, row: int) -> int\n decreases mat.len() - row\n{\n if row >= mat.len() as int { 1 }\n else { mat[row].len() as int * total_combos(mat, row + 1) }\n}\n\npub open spec fn min_spec(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn sorted_asc(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n}\n\npub open spec fn merge_seq(a: Seq, b: Seq) -> Seq\n decreases a.len() + b.len()\n{\n if a.len() == 0 {\n b\n } else if b.len() == 0 {\n a\n } else if a[0] <= b[0] {\n seq![a[0]] + merge_seq(a.drop_first(), b)\n } else {\n seq![b[0]] + merge_seq(a, b.drop_first())\n }\n}\n\npub open spec fn all_between(s: Seq, lo: int, hi: int) -> bool {\n forall|i: int| 0 <= i < s.len() ==> lo <= #[trigger] s[i] <= hi\n}\n\npub open spec fn merge_capped(a: Seq, b: Seq, cap: int) -> Seq {\n merge_seq(a, b).take(min_spec(cap, merge_seq(a, b).len() as int))\n}\n\npub open spec fn shift_seq(s: Seq, shift: int) -> Seq {\n s.map_values(|e: int| shift + e)\n}\n\npub open spec fn fold_cols(mat: Seq>, row: int, col: int, tail: Seq, cap: int) -> Seq\n decreases (if 0 <= row < mat.len() && col <= mat[row].len() { mat[row].len() - col } else { 0 }),\n{\n if !(0 <= row < mat.len()) || col >= mat[row].len() {\n Seq::::empty()\n } else {\n merge_capped(shift_seq(tail, mat[row][col] as int), fold_cols(mat, row, col + 1, tail, cap), cap)\n }\n}\n\npub open spec fn capped_sums(mat: Seq>, row: int, cap: int) -> Seq\n decreases mat.len() - row,\n{\n if row >= mat.len() {\n seq![0int].take(min_spec(cap, 1))\n } else {\n fold_cols(mat, row, 0, capped_sums(mat, row + 1, cap), cap)\n }\n}\n\npub open spec fn to_int_seq(s: Seq) -> Seq {\n s.map_values(|x: i32| x as int)\n}\n\nfn merge_capped_exec(a: &Vec, b: &Vec, cap: usize) -> (result: Vec)\n requires\n sorted_asc(to_int_seq(a@)),\n sorted_asc(to_int_seq(b@)),\n a.len() <= 1000,\n b.len() <= 1000,\n ensures\n to_int_seq(result@) == merge_capped(to_int_seq(a@), to_int_seq(b@), cap as int),\n{\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while result.len() < cap && (i < a.len() || j < b.len())\n {\n if j >= b.len() || (i < a.len() && a[i] <= b[j]) {\n result.push(a[i]);\n i += 1;\n } else {\n result.push(b[j]);\n j += 1;\n }\n }\n result\n}\n\nfn shift_exec(s: &Vec, shift: i32) -> (result: Vec)\n requires\n all_between(to_int_seq(s@), 0, 1_000_000),\n 0 <= shift <= 5000,\n ensures\n to_int_seq(result@) == shift_seq(to_int_seq(s@), shift as int),\n{\n let mut result: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < s.len()\n {\n result.push(s[idx] + shift);\n idx += 1;\n }\n result\n}\n\nfn fold_cols_exec(mat: &Vec>, row: usize, tail: &Vec, cap: usize) -> (result: Vec)\n requires\n row < mat.len(),\n mat.len() <= 40,\n cap <= 200,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() >= 1 && mat[i].len() <= 40,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n to_int_seq(tail@) == capped_sums(mat@, row as int + 1, cap as int),\n ensures\n to_int_seq(result@) == fold_cols(mat@, row as int, 0, to_int_seq(tail@), cap as int),\n{\n let n = mat[row].len();\n let mut acc: Vec = Vec::new();\n let mut c: usize = n;\n while c > 0\n {\n c -= 1;\n let shifted = shift_exec(tail, mat[row][c]);\n acc = merge_capped_exec(&shifted, &acc, cap);\n }\n acc\n}\n\nfn capped_sums_exec(mat: &Vec>, cap: usize) -> (result: Vec)\n requires\n 1 <= mat.len() <= 40,\n cap <= 200,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() >= 1 && mat[i].len() <= 40,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n ensures\n to_int_seq(result@) == capped_sums(mat@, 0, cap as int),\n{\n let mut tail: Vec = Vec::new();\n if cap >= 1 {\n tail.push(0);\n }\n let m = mat.len();\n let mut row: usize = m;\n while row > 0\n {\n row -= 1;\n tail = fold_cols_exec(mat, row, &tail, cap);\n }\n tail\n}\n\nimpl Solution {\n pub fn kth_smallest(mat: Vec>, k: i32) -> (result: i32)\n requires\n 1 <= mat.len() <= 40,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() >= 1 && mat[i].len() <= 40,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n 1 <= k <= 200,\n k as int <= total_combos(mat@, 0),\n ensures\n count_sums(mat@, 0, 0, result as int) >= k as int,\n count_sums(mat@, 0, 0, result as int - 1) < k as int,\n {\n let l = capped_sums_exec(&mat, k as usize);\n let ans = l[(k - 1) as usize];\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_sums(mat: Seq>, row: int, col: int, remaining: int) -> nat\n decreases mat.len() - row, (if 0 <= row < mat.len() as int { mat[row].len() - col } else { 0 })\n{\n if row >= mat.len() as int {\n if remaining >= 0 { 1 } else { 0 }\n } else if row < 0 || col >= mat[row].len() as int {\n 0\n } else {\n count_sums(mat, row + 1, 0, remaining - mat[row][col] as int) +\n count_sums(mat, row, col + 1, remaining)\n }\n}\n\npub open spec fn total_combos(mat: Seq>, row: int) -> int\n decreases mat.len() - row\n{\n if row >= mat.len() as int { 1 }\n else { mat[row].len() as int * total_combos(mat, row + 1) }\n}\n\npub open spec fn min_spec(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn sorted_asc(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n}\n\npub open spec fn count_le(s: Seq, v: int) -> int\n decreases s.len()\n{\n if s.len() == 0 {\n 0\n } else if s[0] <= v {\n 1 + count_le(s.drop_first(), v)\n } else {\n count_le(s.drop_first(), v)\n }\n}\n\npub open spec fn merge_seq(a: Seq, b: Seq) -> Seq\n decreases a.len() + b.len()\n{\n if a.len() == 0 {\n b\n } else if b.len() == 0 {\n a\n } else if a[0] <= b[0] {\n seq![a[0]] + merge_seq(a.drop_first(), b)\n } else {\n seq![b[0]] + merge_seq(a, b.drop_first())\n }\n}\n\npub open spec fn all_ge(s: Seq, lo: int) -> bool {\n forall|i: int| 0 <= i < s.len() ==> s[i] >= lo\n}\n\npub open spec fn all_between(s: Seq, lo: int, hi: int) -> bool {\n forall|i: int| 0 <= i < s.len() ==> lo <= #[trigger] s[i] <= hi\n}\n\npub open spec fn rows_bounded(mat: Seq>) -> bool {\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000\n}\n\nproof fn lemma_count_le_cons(x: int, rest: Seq, v: int)\n ensures count_le(seq![x] + rest, v) == (if x <= v { 1int } else { 0int }) + count_le(rest, v),\n{\n assert((seq![x] + rest).drop_first() =~= rest);\n assert((seq![x] + rest)[0] == x);\n}\n\nproof fn lemma_count_le_nonneg(s: Seq, v: int)\n ensures count_le(s, v) >= 0,\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_count_le_nonneg(s.drop_first(), v);\n }\n}\n\nproof fn lemma_count_le_bound(s: Seq, v: int)\n ensures count_le(s, v) <= s.len(),\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_count_le_bound(s.drop_first(), v);\n }\n}\n\nproof fn lemma_count_le_zero_when_gt(s: Seq, v: int)\n requires\n sorted_asc(s),\n s.len() > 0 ==> s[0] > v,\n ensures\n count_le(s, v) == 0,\n decreases s.len(),\n{\n if s.len() == 0 {\n } else {\n assert(s[0] > v);\n if s.len() > 1 {\n assert(s.drop_first()[0] == s[1]);\n assert(s[0] <= s[1]);\n assert(s[1] > v);\n lemma_sorted_drop_first(s);\n lemma_count_le_zero_when_gt(s.drop_first(), v);\n }\n assert(count_le(s, v) == count_le(s.drop_first(), v));\n }\n}\n\nproof fn lemma_merge_seq_len(a: Seq, b: Seq)\n ensures merge_seq(a, b).len() == a.len() + b.len(),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n } else if b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_merge_seq_len(a.drop_first(), b);\n } else {\n lemma_merge_seq_len(a, b.drop_first());\n }\n}\n\nproof fn lemma_merge_seq_count_le(a: Seq, b: Seq, v: int)\n ensures count_le(merge_seq(a, b), v) == count_le(a, v) + count_le(b, v),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n } else if b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_merge_seq_count_le(a.drop_first(), b, v);\n lemma_count_le_cons(a[0], merge_seq(a.drop_first(), b), v);\n lemma_count_le_cons(a[0], a.drop_first(), v);\n } else {\n lemma_merge_seq_count_le(a, b.drop_first(), v);\n lemma_count_le_cons(b[0], merge_seq(a, b.drop_first()), v);\n lemma_count_le_cons(b[0], b.drop_first(), v);\n }\n}\n\nproof fn lemma_merge_seq_all_ge(a: Seq, b: Seq, lo: int)\n requires all_ge(a, lo), all_ge(b, lo),\n ensures all_ge(merge_seq(a, b), lo),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n } else if b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_merge_seq_all_ge(a.drop_first(), b, lo);\n } else {\n lemma_merge_seq_all_ge(a, b.drop_first(), lo);\n }\n}\n\nproof fn lemma_sorted_drop_first(s: Seq)\n requires sorted_asc(s), s.len() > 0,\n ensures sorted_asc(s.drop_first()), all_ge(s.drop_first(), s[0]),\n{\n assert forall|i: int, j: int| 0 <= i <= j < s.drop_first().len() implies\n s.drop_first()[i] <= s.drop_first()[j] by {\n assert(s.drop_first()[i] == s[i + 1]);\n assert(s.drop_first()[j] == s[j + 1]);\n }\n assert forall|i: int| 0 <= i < s.drop_first().len() implies s.drop_first()[i] >= s[0] by {\n assert(s.drop_first()[i] == s[i + 1]);\n }\n}\n\nproof fn lemma_sorted_cons(x: int, rest: Seq)\n requires all_ge(rest, x), sorted_asc(rest),\n ensures sorted_asc(seq![x] + rest),\n{\n assert forall|i: int, j: int| 0 <= i <= j < (seq![x] + rest).len() implies\n (seq![x] + rest)[i] <= (seq![x] + rest)[j] by {\n if i == 0 {\n if j > 0 {\n assert((seq![x] + rest)[j] == rest[j - 1]);\n assert(rest[j - 1] >= x);\n }\n } else {\n assert((seq![x] + rest)[i] == rest[i - 1]);\n assert((seq![x] + rest)[j] == rest[j - 1]);\n }\n }\n}\n\nproof fn lemma_merge_seq_sorted(a: Seq, b: Seq)\n requires sorted_asc(a), sorted_asc(b),\n ensures sorted_asc(merge_seq(a, b)),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n } else if b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_sorted_drop_first(a);\n lemma_merge_seq_sorted(a.drop_first(), b);\n lemma_merge_seq_all_ge(a.drop_first(), b, a[0]);\n lemma_sorted_cons(a[0], merge_seq(a.drop_first(), b));\n } else {\n lemma_sorted_drop_first(b);\n lemma_merge_seq_sorted(a, b.drop_first());\n lemma_merge_seq_all_ge(a, b.drop_first(), b[0]);\n lemma_sorted_cons(b[0], merge_seq(a, b.drop_first()));\n }\n}\n\nproof fn lemma_take_unfold(s: Seq, cap: int)\n requires 0 < cap <= s.len(),\n ensures s.take(cap) =~= seq![s[0]] + s.drop_first().take(cap - 1),\n{\n}\n\nproof fn lemma_min_arith(cap: int, x: int, y: int)\n requires cap >= 0, x >= 0, y >= 0,\n ensures min_spec(cap, min_spec(cap, x) + min_spec(cap, y)) == min_spec(cap, x + y),\n{\n if x <= cap && y <= cap {\n assert(min_spec(cap, x) == x);\n assert(min_spec(cap, y) == y);\n if x + y <= cap {\n assert(min_spec(cap, x + y) == x + y);\n assert(min_spec(cap, x + y) == x + y);\n } else {\n assert(min_spec(cap, x + y) == cap);\n }\n } else if x > cap {\n assert(min_spec(cap, x) == cap);\n assert(min_spec(cap, x + y) == cap);\n } else {\n assert(min_spec(cap, y) == cap);\n assert(min_spec(cap, x + y) == cap);\n }\n}\n\nproof fn lemma_min_step(cap: int, d: int, r: int)\n requires cap >= 1, 0 <= d <= 1, r >= 0, d == 0 ==> r == 0,\n ensures d + min_spec(cap - 1, r) == min_spec(cap, d + r),\n{\n if d == 1 {\n if r <= cap - 1 {\n assert(min_spec(cap - 1, r) == r);\n assert(min_spec(cap, d + r) == d + r);\n } else {\n assert(min_spec(cap - 1, r) == cap - 1);\n assert(min_spec(cap, d + r) == cap);\n }\n } else {\n assert(r == 0);\n assert(min_spec(cap - 1, r) == 0);\n assert(min_spec(cap, d + r) == 0);\n }\n}\n\nproof fn lemma_sorted_take_count_le(s: Seq, cap: int, v: int)\n requires sorted_asc(s), 0 <= cap <= s.len(),\n ensures count_le(s.take(cap), v) == min_spec(cap, count_le(s, v)),\n decreases s.len(),\n{\n if cap == 0 {\n assert(s.take(0) =~= Seq::::empty());\n lemma_count_le_nonneg(s, v);\n } else {\n lemma_take_unfold(s, cap);\n lemma_count_le_cons(s[0], s.drop_first().take(cap - 1), v);\n lemma_sorted_drop_first(s);\n lemma_sorted_take_count_le(s.drop_first(), cap - 1, v);\n lemma_count_le_nonneg(s.drop_first(), v);\n lemma_count_le_cons(s[0], s.drop_first(), v);\n let d: int = if s[0] <= v { 1int } else { 0int };\n let r = count_le(s.drop_first(), v);\n assert(count_le(s.take(cap), v) == d + min_spec(cap - 1, r));\n assert(count_le(s, v) == d + r);\n if s[0] > v {\n lemma_count_le_zero_when_gt(s.drop_first(), v);\n assert(r == 0);\n }\n lemma_min_step(cap, d, r);\n assert(d + min_spec(cap - 1, r) == min_spec(cap, d + r));\n assert(count_le(s.take(cap), v) == min_spec(cap, count_le(s, v)));\n }\n}\n\npub open spec fn merge_capped(a: Seq, b: Seq, cap: int) -> Seq {\n merge_seq(a, b).take(min_spec(cap, merge_seq(a, b).len() as int))\n}\n\nproof fn lemma_merge_capped_count_le(a: Seq, b: Seq, cap: int, v: int)\n requires sorted_asc(a), sorted_asc(b), cap >= 0,\n ensures count_le(merge_capped(a, b, cap), v) == min_spec(cap, count_le(a, v) + count_le(b, v)),\n{\n lemma_merge_seq_sorted(a, b);\n lemma_merge_seq_len(a, b);\n lemma_merge_seq_count_le(a, b, v);\n let cap_eff = min_spec(cap, merge_seq(a, b).len() as int);\n lemma_sorted_take_count_le(merge_seq(a, b), cap_eff, v);\n lemma_count_le_nonneg(a, v);\n lemma_count_le_nonneg(b, v);\n lemma_count_le_bound(a, v);\n lemma_count_le_bound(b, v);\n assert(count_le(a, v) + count_le(b, v) <= merge_seq(a, b).len());\n}\n\nproof fn lemma_merge_capped_sorted(a: Seq, b: Seq, cap: int)\n requires sorted_asc(a), sorted_asc(b), cap >= 0,\n ensures sorted_asc(merge_capped(a, b, cap)),\n{\n lemma_merge_seq_sorted(a, b);\n}\n\nproof fn lemma_merge_capped_len(a: Seq, b: Seq, cap: int)\n requires cap >= 0,\n ensures merge_capped(a, b, cap).len() == min_spec(cap, a.len() as int + b.len() as int),\n{\n lemma_merge_seq_len(a, b);\n}\n\nproof fn lemma_merge_seq_all_between(a: Seq, b: Seq, lo: int, hi: int)\n requires all_between(a, lo, hi), all_between(b, lo, hi),\n ensures all_between(merge_seq(a, b), lo, hi),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n } else if b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_merge_seq_all_between(a.drop_first(), b, lo, hi);\n } else {\n lemma_merge_seq_all_between(a, b.drop_first(), lo, hi);\n }\n}\n\nproof fn lemma_take_all_between(s: Seq, cap: int, lo: int, hi: int)\n requires all_between(s, lo, hi), 0 <= cap <= s.len(),\n ensures all_between(s.take(cap), lo, hi),\n{\n assert forall|i: int| 0 <= i < s.take(cap).len() implies lo <= #[trigger] s.take(cap)[i] <= hi by {\n assert(s.take(cap)[i] == s[i]);\n }\n}\n\nproof fn lemma_merge_capped_all_between(a: Seq, b: Seq, cap: int, lo: int, hi: int)\n requires all_between(a, lo, hi), all_between(b, lo, hi), cap >= 0,\n ensures all_between(merge_capped(a, b, cap), lo, hi),\n{\n lemma_merge_seq_all_between(a, b, lo, hi);\n lemma_merge_seq_len(a, b);\n let cap_eff = min_spec(cap, merge_seq(a, b).len() as int);\n lemma_take_all_between(merge_seq(a, b), cap_eff, lo, hi);\n}\n\nproof fn lemma_shift_seq_all_between(s: Seq, shift: int, lo: int, hi: int)\n requires all_between(s, lo, hi),\n ensures all_between(shift_seq(s, shift), lo + shift, hi + shift),\n{\n assert forall|i: int| 0 <= i < shift_seq(s, shift).len() implies\n lo + shift <= #[trigger] shift_seq(s, shift)[i] <= hi + shift by {\n assert(shift_seq(s, shift)[i] == shift + s[i]);\n }\n}\n\npub open spec fn shift_seq(s: Seq, shift: int) -> Seq {\n s.map_values(|e: int| shift + e)\n}\n\nproof fn lemma_shift_seq_len(s: Seq, shift: int)\n ensures shift_seq(s, shift).len() == s.len(),\n{\n}\n\nproof fn lemma_shift_seq_sorted(s: Seq, shift: int)\n requires sorted_asc(s),\n ensures sorted_asc(shift_seq(s, shift)),\n{\n assert forall|i: int, j: int| 0 <= i <= j < shift_seq(s, shift).len() implies\n shift_seq(s, shift)[i] <= shift_seq(s, shift)[j] by {\n assert(shift_seq(s, shift)[i] == shift + s[i]);\n assert(shift_seq(s, shift)[j] == shift + s[j]);\n }\n}\n\nproof fn lemma_count_le_shift(s: Seq, shift: int, v: int)\n ensures count_le(shift_seq(s, shift), v) == count_le(s, v - shift),\n decreases s.len(),\n{\n if s.len() == 0 {\n } else {\n assert(shift_seq(s, shift).drop_first() =~= shift_seq(s.drop_first(), shift));\n assert(shift_seq(s, shift)[0] == shift + s[0]);\n assert(shift_seq(s, shift) =~= seq![shift + s[0]] + shift_seq(s.drop_first(), shift));\n lemma_count_le_shift(s.drop_first(), shift, v);\n lemma_count_le_cons(shift + s[0], shift_seq(s.drop_first(), shift), v);\n lemma_count_le_cons(s[0], s.drop_first(), v - shift);\n }\n}\n\npub open spec fn fold_cols(mat: Seq>, row: int, col: int, tail: Seq, cap: int) -> Seq\n decreases (if 0 <= row < mat.len() && col <= mat[row].len() { mat[row].len() - col } else { 0 }),\n{\n if !(0 <= row < mat.len()) || col >= mat[row].len() {\n Seq::::empty()\n } else {\n merge_capped(shift_seq(tail, mat[row][col] as int), fold_cols(mat, row, col + 1, tail, cap), cap)\n }\n}\n\npub open spec fn capped_sums(mat: Seq>, row: int, cap: int) -> Seq\n decreases mat.len() - row,\n{\n if row >= mat.len() {\n seq![0int].take(min_spec(cap, 1))\n } else {\n fold_cols(mat, row, 0, capped_sums(mat, row + 1, cap), cap)\n }\n}\n\npub open spec fn rows_sorted(mat: Seq>) -> bool {\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1]\n}\n\npub open spec fn rows_nonempty(mat: Seq>) -> bool {\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() >= 1\n}\n\nproof fn lemma_fold_cols_correct(mat: Seq>, row: int, col: int, cap: int, v: int)\n requires\n 0 <= row < mat.len(),\n 0 <= col <= mat[row].len(),\n cap >= 0,\n rows_sorted(mat),\n rows_nonempty(mat),\n rows_bounded(mat),\n ensures\n count_le(fold_cols(mat, row, col, capped_sums(mat, row + 1, cap), cap), v) ==\n min_spec(cap, count_sums(mat, row, col, v) as int),\n sorted_asc(fold_cols(mat, row, col, capped_sums(mat, row + 1, cap), cap)),\n fold_cols(mat, row, col, capped_sums(mat, row + 1, cap), cap).len() ==\n min_spec(cap, (mat[row].len() as int - col) * capped_sums(mat, row + 1, cap).len() as int),\n all_between(fold_cols(mat, row, col, capped_sums(mat, row + 1, cap), cap), 0,\n (mat.len() as int - row) * 5000),\n decreases mat.len() - row, mat[row].len() - col,\n{\n let tail = capped_sums(mat, row + 1, cap);\n lemma_capped_sums_sorted(mat, row + 1, cap);\n lemma_capped_sums_len(mat, row + 1, cap);\n lemma_capped_sums_bound(mat, row + 1, cap);\n if col >= mat[row].len() {\n assert(fold_cols(mat, row, col, tail, cap) =~= Seq::::empty());\n assert(col == mat[row].len());\n assert((mat[row].len() as int - col) * tail.len() as int == 0);\n } else {\n lemma_fold_cols_correct(mat, row, col + 1, cap, v);\n let val = mat[row][col] as int;\n lemma_capped_sums_correct(mat, row + 1, cap, v - val);\n lemma_shift_seq_sorted(tail, val);\n lemma_shift_seq_len(tail, val);\n lemma_merge_capped_count_le(shift_seq(tail, val), fold_cols(mat, row, col + 1, tail, cap), cap, v);\n lemma_merge_capped_sorted(shift_seq(tail, val), fold_cols(mat, row, col + 1, tail, cap), cap);\n lemma_merge_capped_len(shift_seq(tail, val), fold_cols(mat, row, col + 1, tail, cap), cap);\n lemma_count_le_shift(tail, val, v);\n assert(count_le(shift_seq(tail, val), v) == min_spec(cap, count_sums(mat, row + 1, 0, v - val) as int));\n assert(count_le(fold_cols(mat, row, col + 1, tail, cap), v) ==\n min_spec(cap, count_sums(mat, row, col + 1, v) as int));\n let x = count_sums(mat, row + 1, 0, v - val) as int;\n let y = count_sums(mat, row, col + 1, v) as int;\n lemma_min_arith(cap, x, y);\n assert(count_le(fold_cols(mat, row, col, tail, cap), v) == min_spec(cap, x + y));\n assert(count_sums(mat, row, col, v) as int == x + y);\n\n assert(tail.len() <= cap);\n lemma_min_arith(cap, tail.len() as int, (mat[row].len() as int - col - 1) * tail.len() as int);\n assert(fold_cols(mat, row, col, tail, cap).len() ==\n min_spec(cap, tail.len() as int + min_spec(cap, (mat[row].len() as int - col - 1) * tail.len() as int)));\n assert(tail.len() as int + (mat[row].len() as int - col - 1) * tail.len() as int ==\n (mat[row].len() as int - col) * tail.len() as int) by (nonlinear_arith);\n\n assert(1 <= val <= 5000);\n assert(all_between(tail, 0, (mat.len() as int - row - 1) * 5000));\n lemma_shift_seq_all_between(tail, val, 0, (mat.len() as int - row - 1) * 5000);\n assert(val + (mat.len() as int - row - 1) * 5000 <= (mat.len() as int - row) * 5000) by (nonlinear_arith)\n requires 1 <= val <= 5000;\n assert(all_between(shift_seq(tail, val), 0, (mat.len() as int - row) * 5000)) by {\n assert forall|idx: int| 0 <= idx < shift_seq(tail, val).len() implies\n 0 <= #[trigger] shift_seq(tail, val)[idx] <= (mat.len() as int - row) * 5000 by {\n assert(val <= shift_seq(tail, val)[idx] <= val + (mat.len() as int - row - 1) * 5000);\n }\n }\n lemma_merge_capped_all_between(shift_seq(tail, val), fold_cols(mat, row, col + 1, tail, cap), cap,\n 0, (mat.len() as int - row) * 5000);\n }\n}\n\nproof fn lemma_capped_sums_correct(mat: Seq>, row: int, cap: int, v: int)\n requires\n 0 <= row <= mat.len(),\n cap >= 0,\n rows_sorted(mat),\n rows_nonempty(mat),\n rows_bounded(mat),\n ensures\n count_le(capped_sums(mat, row, cap), v) == min_spec(cap, count_sums(mat, row, 0, v) as int),\n decreases mat.len() - row, (if row < mat.len() { mat[row].len() + 1 } else { 0int }) as int,\n{\n if row >= mat.len() {\n if cap >= 1 {\n assert(seq![0int].take(min_spec(cap, 1)) =~= seq![0int]);\n assert(seq![0int] =~= seq![0int] + Seq::::empty());\n lemma_count_le_cons(0int, Seq::::empty(), v);\n } else {\n assert(seq![0int].take(min_spec(cap, 1)) =~= Seq::::empty());\n }\n } else {\n lemma_fold_cols_correct(mat, row, 0, cap, v);\n }\n}\n\nproof fn lemma_capped_sums_sorted(mat: Seq>, row: int, cap: int)\n requires\n 0 <= row <= mat.len(),\n cap >= 0,\n rows_sorted(mat),\n rows_nonempty(mat),\n rows_bounded(mat),\n ensures\n sorted_asc(capped_sums(mat, row, cap)),\n decreases mat.len() - row, (if row < mat.len() { mat[row].len() + 1 } else { 0int }) as int,\n{\n if row >= mat.len() {\n } else {\n lemma_fold_cols_correct(mat, row, 0, cap, 0);\n }\n}\n\nproof fn lemma_capped_sums_bound(mat: Seq>, row: int, cap: int)\n requires\n 0 <= row <= mat.len(),\n cap >= 0,\n rows_sorted(mat),\n rows_nonempty(mat),\n rows_bounded(mat),\n ensures\n all_between(capped_sums(mat, row, cap), 0, (mat.len() as int - row) * 5000),\n decreases mat.len() - row, (if row < mat.len() { mat[row].len() + 1 } else { 0int }) as int,\n{\n if row >= mat.len() {\n if cap >= 1 {\n assert(seq![0int].take(min_spec(cap, 1)) =~= seq![0int]);\n } else {\n assert(seq![0int].take(min_spec(cap, 1)) =~= Seq::::empty());\n }\n } else {\n lemma_fold_cols_correct(mat, row, 0, cap, 0);\n }\n}\n\nproof fn lemma_min_mult(cap: int, k: int, t: int)\n requires cap >= 0, k >= 1, t >= 0,\n ensures min_spec(cap, k * min_spec(cap, t)) == min_spec(cap, k * t),\n{\n if t <= cap {\n } else {\n assert(min_spec(cap, t) == cap);\n assert(k * cap >= cap) by (nonlinear_arith)\n requires k >= 1, cap >= 0;\n assert(min_spec(cap, k * cap) == cap);\n assert(k * t >= t) by (nonlinear_arith)\n requires k >= 1, t >= 0;\n assert(k * t > cap);\n assert(min_spec(cap, k * t) == cap);\n }\n}\n\nproof fn lemma_capped_sums_len(mat: Seq>, row: int, cap: int)\n requires\n 0 <= row <= mat.len(),\n cap >= 0,\n rows_sorted(mat),\n rows_nonempty(mat),\n rows_bounded(mat),\n ensures\n capped_sums(mat, row, cap).len() == min_spec(cap, total_combos(mat, row)),\n decreases mat.len() - row, (if row < mat.len() { mat[row].len() + 1 } else { 0int }) as int,\n{\n if row >= mat.len() {\n if cap >= 1 {\n assert(seq![0int].take(min_spec(cap, 1)) =~= seq![0int]);\n } else {\n assert(seq![0int].take(min_spec(cap, 1)) =~= Seq::::empty());\n }\n } else {\n lemma_fold_cols_correct(mat, row, 0, cap, 0);\n lemma_capped_sums_len(mat, row + 1, cap);\n assert(mat[row].len() as int * capped_sums(mat, row + 1, cap).len() as int ==\n mat[row].len() as int * min_spec(cap, total_combos(mat, row + 1)));\n lemma_min_mult(cap, mat[row].len() as int, total_combos(mat, row + 1));\n assert(total_combos(mat, row) == mat[row].len() as int * total_combos(mat, row + 1));\n }\n}\n\nproof fn lemma_count_le_full(s: Seq, v: int)\n requires forall|i: int| 0 <= i < s.len() ==> s[i] <= v,\n ensures count_le(s, v) == s.len(),\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_count_le_full(s.drop_first(), v);\n }\n}\n\nproof fn lemma_count_le_lt_len_when_last_gt(s: Seq, v: int)\n requires sorted_asc(s), s.len() > 0, s[s.len() - 1] > v,\n ensures count_le(s, v) < s.len(),\n decreases s.len(),\n{\n if s.len() == 1 {\n assert(s[0] == s[s.len() - 1]);\n assert(s.drop_first() =~= Seq::::empty());\n assert(count_le(s, v) == count_le(s.drop_first(), v));\n } else {\n lemma_sorted_drop_first(s);\n assert(s.drop_first()[s.drop_first().len() - 1] == s[s.len() - 1]);\n lemma_count_le_lt_len_when_last_gt(s.drop_first(), v);\n lemma_count_le_cons(s[0], s.drop_first(), v);\n }\n}\n\nproof fn lemma_kth_smallest_correct(mat: Seq>, k: int)\n requires\n 1 <= mat.len(),\n rows_sorted(mat),\n rows_nonempty(mat),\n rows_bounded(mat),\n 1 <= k,\n k <= total_combos(mat, 0),\n ensures\n capped_sums(mat, 0, k).len() == k,\n count_sums(mat, 0, 0, capped_sums(mat, 0, k)[k - 1]) >= k,\n count_sums(mat, 0, 0, (capped_sums(mat, 0, k)[k - 1] - 1) as int) < k,\n all_between(capped_sums(mat, 0, k), 0, mat.len() as int * 5000),\n{\n lemma_capped_sums_len(mat, 0, k);\n lemma_capped_sums_sorted(mat, 0, k);\n lemma_capped_sums_bound(mat, 0, k);\n let l = capped_sums(mat, 0, k);\n assert(l.len() == k);\n let last = l[k - 1];\n let below = last - 1;\n lemma_count_le_full(l, last);\n lemma_capped_sums_correct(mat, 0, k, last);\n assert(min_spec(k, count_sums(mat, 0, 0, last) as int) == k);\n assert(count_sums(mat, 0, 0, last) as int >= k);\n\n lemma_count_le_lt_len_when_last_gt(l, below);\n lemma_capped_sums_correct(mat, 0, k, below);\n assert(min_spec(k, count_sums(mat, 0, 0, below) as int) < k);\n assert((count_sums(mat, 0, 0, below) as int) < k);\n}\n\n\npub open spec fn to_int_seq(s: Seq) -> Seq {\n s.map_values(|x: i32| x as int)\n}\n\nproof fn lemma_merge_seq_skip_step_a(a: Seq, b: Seq, i: int, j: int)\n requires 0 <= i < a.len(), 0 <= j <= b.len(), (j >= b.len() || a[i] <= b[j]),\n ensures merge_seq(a.skip(i), b.skip(j)) =~= seq![a[i]] + merge_seq(a.skip(i + 1), b.skip(j)),\n{\n assert(a.skip(i)[0] == a[i]);\n assert(a.skip(i).drop_first() =~= a.skip(i + 1));\n if j < b.len() {\n assert(b.skip(j)[0] == b[j]);\n assert(a.skip(i)[0] <= b.skip(j)[0]);\n } else {\n assert(b.skip(j).len() == 0);\n }\n}\n\nproof fn lemma_merge_seq_skip_step_b(a: Seq, b: Seq, i: int, j: int)\n requires 0 <= i <= a.len(), 0 <= j < b.len(), (i >= a.len() || b[j] < a[i]),\n ensures merge_seq(a.skip(i), b.skip(j)) =~= seq![b[j]] + merge_seq(a.skip(i), b.skip(j + 1)),\n{\n assert(b.skip(j)[0] == b[j]);\n assert(b.skip(j).drop_first() =~= b.skip(j + 1));\n if i < a.len() {\n assert(a.skip(i)[0] == a[i]);\n assert(b.skip(j)[0] < a.skip(i)[0]);\n } else {\n assert(a.skip(i).len() == 0);\n }\n}\n\nproof fn lemma_prefix_take(x: Seq, y: Seq)\n ensures (x + y).take(x.len() as int) =~= x,\n{\n}\n\nfn merge_capped_exec(a: &Vec, b: &Vec, cap: usize) -> (result: Vec)\n requires\n sorted_asc(to_int_seq(a@)),\n sorted_asc(to_int_seq(b@)),\n a.len() <= 1000,\n b.len() <= 1000,\n ensures\n to_int_seq(result@) == merge_capped(to_int_seq(a@), to_int_seq(b@), cap as int),\n{\n let ghost av = to_int_seq(a@);\n let ghost bv = to_int_seq(b@);\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n proof {\n assert(av.skip(0) =~= av);\n assert(bv.skip(0) =~= bv);\n assert(to_int_seq(result@) =~= Seq::::empty());\n }\n while result.len() < cap && (i < a.len() || j < b.len())\n invariant\n i <= a.len(),\n j <= b.len(),\n result.len() == i + j,\n result.len() <= cap,\n to_int_seq(a@) == av,\n to_int_seq(b@) == bv,\n to_int_seq(result@) + merge_seq(av.skip(i as int), bv.skip(j as int)) == merge_seq(av, bv),\n decreases (a.len() - i) + (b.len() - j),\n {\n if j >= b.len() || (i < a.len() && a[i] <= b[j]) {\n proof {\n lemma_merge_seq_skip_step_a(av, bv, i as int, j as int);\n assert(to_int_seq(result@).push(a@[i as int] as int) =~= to_int_seq(result@) + seq![a@[i as int] as int]);\n }\n result.push(a[i]);\n i += 1;\n } else {\n proof {\n lemma_merge_seq_skip_step_b(av, bv, i as int, j as int);\n assert(to_int_seq(result@).push(b@[j as int] as int) =~= to_int_seq(result@) + seq![b@[j as int] as int]);\n }\n result.push(b[j]);\n j += 1;\n }\n }\n proof {\n if i >= a.len() && j >= b.len() {\n assert(av.skip(i as int).len() == 0);\n assert(bv.skip(j as int).len() == 0);\n assert(merge_seq(av.skip(i as int), bv.skip(j as int)) =~= Seq::::empty());\n assert(to_int_seq(result@) == merge_seq(av, bv));\n lemma_merge_seq_len(av, bv);\n assert(merge_seq(av, bv).len() == result.len());\n assert(merge_seq(av, bv).len() as int <= cap as int);\n assert(merge_capped(av, bv, cap as int) == merge_seq(av, bv).take(min_spec(cap as int, merge_seq(av, bv).len() as int)));\n assert(min_spec(cap as int, merge_seq(av, bv).len() as int) == merge_seq(av, bv).len() as int);\n assert(merge_seq(av, bv).take(merge_seq(av, bv).len() as int) =~= merge_seq(av, bv));\n } else {\n assert(result.len() == cap);\n lemma_merge_seq_len(av, bv);\n lemma_prefix_take(to_int_seq(result@), merge_seq(av.skip(i as int), bv.skip(j as int)));\n assert(merge_capped(av, bv, cap as int) == merge_seq(av, bv).take(min_spec(cap as int, merge_seq(av, bv).len() as int)));\n assert(min_spec(cap as int, merge_seq(av, bv).len() as int) == cap as int);\n }\n }\n result\n}\n\nfn shift_exec(s: &Vec, shift: i32) -> (result: Vec)\n requires\n all_between(to_int_seq(s@), 0, 1_000_000),\n 0 <= shift <= 5000,\n ensures\n to_int_seq(result@) == shift_seq(to_int_seq(s@), shift as int),\n{\n let ghost sv = to_int_seq(s@);\n let mut result: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < s.len()\n invariant\n idx <= s.len(),\n result.len() == idx,\n to_int_seq(s@) == sv,\n all_between(sv, 0, 1_000_000),\n 0 <= shift <= 5000,\n forall|k: int| 0 <= k < idx ==> #[trigger] result@[k] as int == shift as int + sv[k],\n decreases s.len() - idx,\n {\n assert(sv[idx as int] == s@[idx as int] as int);\n assert(0 <= sv[idx as int] <= 1_000_000);\n assert(0 <= s@[idx as int] as int <= 1_000_000);\n assert(0 <= shift as int <= 5000);\n assert(s@[idx as int] as int + shift as int <= 1_005_000);\n result.push(s[idx] + shift);\n idx += 1;\n }\n assert(to_int_seq(result@) =~= shift_seq(sv, shift as int)) by {\n assert forall|k: int| 0 <= k < result.len() as int implies\n #[trigger] to_int_seq(result@)[k] == shift_seq(sv, shift as int)[k] by {\n assert(to_int_seq(result@)[k] == result@[k] as int);\n assert(shift_seq(sv, shift as int)[k] == shift as int + sv[k]);\n }\n }\n result\n}\n\nfn fold_cols_exec(mat: &Vec>, row: usize, tail: &Vec, cap: usize) -> (result: Vec)\n requires\n row < mat.len(),\n mat.len() <= 40,\n cap <= 200,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() >= 1 && mat[i].len() <= 40,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n to_int_seq(tail@) == capped_sums(mat@, row as int + 1, cap as int),\n ensures\n to_int_seq(result@) == fold_cols(mat@, row as int, 0, to_int_seq(tail@), cap as int),\n{\n proof {\n lemma_capped_sums_sorted(mat@, row as int + 1, cap as int);\n lemma_capped_sums_len(mat@, row as int + 1, cap as int);\n lemma_capped_sums_bound(mat@, row as int + 1, cap as int);\n assert((mat.len() as int - (row as int + 1)) * 5000 <= 200000) by (nonlinear_arith)\n requires mat.len() as int <= 40, row as int >= 0;\n }\n let ghost tv = to_int_seq(tail@);\n let n = mat[row].len();\n let mut acc: Vec = Vec::new();\n let mut c: usize = n;\n while c > 0\n invariant\n c <= n,\n n == mat[row as int].len(),\n row < mat.len(),\n mat.len() <= 40,\n cap <= 200,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() >= 1 && mat[i].len() <= 40,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n to_int_seq(tail@) == tv,\n tv == capped_sums(mat@, row as int + 1, cap as int),\n sorted_asc(tv),\n all_between(tv, 0, 200000),\n tv.len() <= cap,\n to_int_seq(acc@) == fold_cols(mat@, row as int, c as int, tv, cap as int),\n sorted_asc(to_int_seq(acc@)),\n acc.len() <= cap,\n decreases c,\n {\n c -= 1;\n proof {\n assert(1 <= mat@[row as int][c as int] <= 5000);\n }\n let shifted = shift_exec(tail, mat[row][c]);\n proof {\n lemma_shift_seq_sorted(tv, mat@[row as int][c as int] as int);\n lemma_shift_seq_len(tv, mat@[row as int][c as int] as int);\n assert(to_int_seq(shifted@).len() == shift_seq(tv, mat@[row as int][c as int] as int).len());\n assert(shifted.len() == shifted@.len());\n assert(to_int_seq(shifted@).len() == shifted@.len());\n assert(shifted.len() == tv.len());\n }\n acc = merge_capped_exec(&shifted, &acc, cap);\n proof {\n lemma_fold_cols_correct(mat@, row as int, c as int, cap as int, 0);\n assert(acc.len() as int <= cap as int);\n }\n }\n acc\n}\n\nfn capped_sums_exec(mat: &Vec>, cap: usize) -> (result: Vec)\n requires\n 1 <= mat.len() <= 40,\n cap <= 200,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() >= 1 && mat[i].len() <= 40,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n ensures\n to_int_seq(result@) == capped_sums(mat@, 0, cap as int),\n{\n let mut tail: Vec = Vec::new();\n if cap >= 1 {\n tail.push(0);\n }\n proof {\n if cap >= 1 {\n assert(seq![0int].take(min_spec(cap as int, 1)) =~= seq![0int]);\n } else {\n assert(seq![0int].take(min_spec(cap as int, 1)) =~= Seq::::empty());\n }\n assert(to_int_seq(tail@) =~= capped_sums(mat@, mat.len() as int, cap as int));\n }\n let m = mat.len();\n let mut row: usize = m;\n while row > 0\n invariant\n row <= m,\n m == mat.len(),\n 1 <= m <= 40,\n cap <= 200,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() >= 1 && mat[i].len() <= 40,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n sorted_asc(to_int_seq(tail@)),\n all_between(to_int_seq(tail@), 0, (mat.len() as int - row as int) * 5000),\n tail.len() <= cap,\n to_int_seq(tail@) == capped_sums(mat@, row as int, cap as int),\n decreases row,\n {\n proof {\n lemma_capped_sums_len(mat@, row as int, cap as int);\n lemma_capped_sums_bound(mat@, row as int, cap as int);\n assert((mat.len() as int - row as int) * 5000 <= 200000) by (nonlinear_arith)\n requires mat.len() as int <= 40, row as int >= 0;\n assert(all_between(to_int_seq(tail@), 0, 200000));\n }\n row -= 1;\n tail = fold_cols_exec(mat, row, &tail, cap);\n proof {\n lemma_capped_sums_bound(mat@, row as int, cap as int);\n lemma_capped_sums_sorted(mat@, row as int, cap as int);\n lemma_capped_sums_len(mat@, row as int, cap as int);\n }\n }\n tail\n}\n\nimpl Solution {\n pub fn kth_smallest(mat: Vec>, k: i32) -> (result: i32)\n requires\n 1 <= mat.len() <= 40,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() >= 1 && mat[i].len() <= 40,\n forall|i: int| 0 <= i < mat.len() ==> (#[trigger] mat[i]).len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==>\n 1 <= #[trigger] mat[i][j] <= 5000,\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() - 1 ==>\n #[trigger] mat[i][j] <= mat[i][j + 1],\n 1 <= k <= 200,\n k as int <= total_combos(mat@, 0),\n ensures\n count_sums(mat@, 0, 0, result as int) >= k as int,\n count_sums(mat@, 0, 0, result as int - 1) < k as int,\n {\n proof {\n lemma_kth_smallest_correct(mat@, k as int);\n }\n let l = capped_sums_exec(&mat, k as usize);\n let ans = l[(k - 1) as usize];\n proof {\n assert(ans as int == capped_sums(mat@, 0, k as int)[k as int - 1]);\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1439/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1439/tests/mutated_testcases.jsonl"} {"problem": "lc1450", "family": "leetcode", "description": "# Number of Students Doing Homework at a Given Time\n\nGiven two integer arrays `startTime` and `endTime` and given an integer `queryTime`.\n\nThe `ith` student started doing their homework at the time `startTime[i]` and finished it at time `endTime[i]`.\n\nReturn *the number of students* doing their homework at time `queryTime`. More formally, return the number of students where `queryTime` lays in the interval `[startTime[i], endTime[i]]` inclusive.\n\n \n\nExample 1:\n\n```\n**Input:** startTime = [1,2,3], endTime = [3,2,7], queryTime = 4\n**Output:** 1\n**Explanation:** We have 3 students where:\nThe first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.\nThe second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.\nThe third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.\n```\n\nExample 2:\n\n```\n**Input:** startTime = [4], endTime = [4], queryTime = 4\n**Output:** 1\n**Explanation:** The only student was doing their homework at the queryTime.\n```\n\n \n\n**Constraints:**\n\n\t- `startTime.length == endTime.length`\n\n\t- `1 <= startTime.length <= 100`\n\n\t- `1 <= startTime[i] <= endTime[i] <= 1000`\n\n\t- `1 <= queryTime <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 75.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_busy(start_time: Seq, end_time: Seq, query_time: i32, n: int) -> int\n decreases n\n{\n if n <= 0 {\n 0\n } else {\n count_busy(start_time, end_time, query_time, n - 1)\n + if start_time[n - 1] <= query_time && query_time <= end_time[n - 1] { 1 as int } else { 0 as int }\n }\n}\n\nimpl Solution {\n pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> (res: i32)\n requires\n start_time.len() == end_time.len(),\n 1 <= start_time.len() <= 100,\n forall |i: int| 0 <= i < start_time.len() ==>\n 1 <= #[trigger] start_time[i] <= end_time[i] <= 1000,\n 1 <= query_time <= 1000,\n ensures\n res as int == count_busy(start_time@, end_time@, query_time, start_time.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> i32 {\n let mut count: i32 = 0;\n let n = start_time.len();\n let mut i: usize = 0;\n while i < n {\n if start_time[i] <= query_time && query_time <= end_time[i] {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_busy(start_time: Seq, end_time: Seq, query_time: i32, n: int) -> int\n decreases n\n{\n if n <= 0 {\n 0\n } else {\n count_busy(start_time, end_time, query_time, n - 1)\n + if start_time[n - 1] <= query_time && query_time <= end_time[n - 1] { 1 as int } else { 0 as int }\n }\n}\n\nimpl Solution {\n pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> (res: i32)\n requires\n start_time.len() == end_time.len(),\n 1 <= start_time.len() <= 100,\n forall |i: int| 0 <= i < start_time.len() ==>\n 1 <= #[trigger] start_time[i] <= end_time[i] <= 1000,\n 1 <= query_time <= 1000,\n ensures\n res as int == count_busy(start_time@, end_time@, query_time, start_time.len() as int),\n {\n let mut count: i32 = 0;\n let n = start_time.len();\n let mut i: usize = 0;\n while i < n {\n if start_time[i] <= query_time && query_time <= end_time[i] {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_busy(start_time: Seq, end_time: Seq, query_time: i32, n: int) -> int\n decreases n\n{\n if n <= 0 {\n 0\n } else {\n count_busy(start_time, end_time, query_time, n - 1)\n + if start_time[n - 1] <= query_time && query_time <= end_time[n - 1] { 1 as int } else { 0 as int }\n }\n}\n\nimpl Solution {\n pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> (res: i32)\n requires\n start_time.len() == end_time.len(),\n 1 <= start_time.len() <= 100,\n forall |i: int| 0 <= i < start_time.len() ==>\n 1 <= #[trigger] start_time[i] <= end_time[i] <= 1000,\n 1 <= query_time <= 1000,\n ensures\n res as int == count_busy(start_time@, end_time@, query_time, start_time.len() as int),\n {\n let mut count: i32 = 0;\n let n = start_time.len();\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == start_time.len(),\n n == end_time.len(),\n 1 <= n <= 100,\n forall |j: int| 0 <= j < start_time.len() ==>\n 1 <= #[trigger] start_time[j] <= end_time[j] <= 1000,\n 1 <= query_time <= 1000,\n count as int == count_busy(start_time@, end_time@, query_time, i as int),\n 0 <= count as int <= i as int,\n decreases n - i,\n {\n if start_time[i] <= query_time && query_time <= end_time[i] {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1450/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1450/tests/mutated_testcases.jsonl"} {"problem": "lc1460", "family": "leetcode", "description": "# Make Two Arrays Equal by Reversing Subarrays\n\nYou are given two integer arrays of equal length `target` and `arr`. In one step, you can select any **non-empty subarray** of `arr` and reverse it. You are allowed to make any number of steps.\n\nReturn `true` if you can make `arr` equal to `target`, or `false` otherwise.\n\n## Example 1:\n\n> **Input:** target = [1,2,3,4], arr = [2,4,1,3]\n> **Output:** true\n> **Explanation:** You can follow the next steps to convert arr to target:\n> 1- Reverse subarray [2,4,1], arr becomes [1,4,2,3]\n> 2- Reverse subarray [4,2], arr becomes [1,2,4,3]\n> 3- Reverse subarray [4,3], arr becomes [1,2,3,4]\n> There are multiple ways to convert arr to target, this is not the only way to do so.\n\n## Example 2:\n\n> **Input:** target = [7], arr = [7]\n> **Output:** true\n> **Explanation:** arr is equal to target without any reverses.\n\n## Example 3:\n\n> **Input:** target = [3,7,9], arr = [3,7,11]\n> **Output:** false\n> **Explanation:** arr does not have value 9 and it can never be converted to target.\n\n## Constraints:\n\n- `target.length == arr.length`\n- `1 <= target.length <= 1000`\n- `1 <= target[i] <= 1000`\n- `1 <= arr[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn can_be_equal(target: Vec, arr: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Sorting\nAcceptance rate: 75.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count(s: Seq, v: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int }) + Self::count(s.drop_last(), v)\n }\n }\n\n pub fn can_be_equal(target: Vec, arr: Vec) -> (res: bool)\n requires\n target.len() == arr.len(),\n 1 <= target.len() <= 1000,\n forall |i: int| 0 <= i < target.len() ==> 1 <= #[trigger] target[i] <= 1000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 1000,\n ensures\n res == (forall |v: i32| Self::count(target@, v) == Self::count(arr@, v)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_be_equal(target: Vec, arr: Vec) -> bool {\n let n = target.len();\n let mut counts: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001 {\n counts.push(0i32);\n k = k + 1;\n }\n let mut i: usize = 0;\n while i < n {\n let tv = target[i];\n let av = arr[i];\n counts[tv as usize] = counts[tv as usize] + 1;\n counts[av as usize] = counts[av as usize] - 1;\n i = i + 1;\n }\n let mut k2: usize = 0;\n while k2 < 1001 {\n if counts[k2] != 0 {\n return false;\n }\n k2 = k2 + 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count(s: Seq, v: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int }) + Self::count(s.drop_last(), v)\n }\n }\n\n pub fn can_be_equal(target: Vec, arr: Vec) -> (res: bool)\n requires\n target.len() == arr.len(),\n 1 <= target.len() <= 1000,\n forall |i: int| 0 <= i < target.len() ==> 1 <= #[trigger] target[i] <= 1000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 1000,\n ensures\n res == (forall |v: i32| Self::count(target@, v) == Self::count(arr@, v)),\n {\n let n = target.len();\n let mut counts: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001 {\n counts.push(0i32);\n k = k + 1;\n }\n let mut i: usize = 0;\n while i < n {\n let tv = target[i];\n let av = arr[i];\n counts.set(tv as usize, counts[tv as usize] + 1);\n counts.set(av as usize, counts[av as usize] - 1);\n i = i + 1;\n }\n let mut k2: usize = 0;\n while k2 < 1001 {\n if counts[k2] != 0 {\n return false;\n }\n k2 = k2 + 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count(s: Seq, v: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int }) + Self::count(s.drop_last(), v)\n }\n }\n\n proof fn count_nonneg(s: Seq, v: i32)\n ensures\n Self::count(s, v) >= 0,\n decreases s.len(),\n {\n if s.len() > 0 {\n Self::count_nonneg(s.drop_last(), v);\n }\n }\n\n proof fn count_take_step(s: Seq, v: i32, n: int)\n requires\n 0 <= n < s.len() as int,\n ensures\n Self::count(s.take(n + 1), v)\n == Self::count(s.take(n), v)\n + (if s[n] == v { 1int } else { 0int }),\n {\n assert(s.take(n + 1).drop_last() =~= s.take(n));\n }\n\n proof fn count_zero_for_absent(s: Seq, v: i32)\n requires\n forall|i: int| 0 <= i < s.len() ==> s[i] != v,\n ensures\n Self::count(s, v) == 0,\n decreases s.len(),\n {\n if s.len() > 0 {\n Self::count_zero_for_absent(s.drop_last(), v);\n }\n }\n\n proof fn count_upper_bound(s: Seq, v: i32)\n ensures\n Self::count(s, v) <= s.len(),\n decreases s.len(),\n {\n if s.len() > 0 {\n Self::count_upper_bound(s.drop_last(), v);\n }\n }\n\n pub fn can_be_equal(target: Vec, arr: Vec) -> (res: bool)\n requires\n target.len() == arr.len(),\n 1 <= target.len() <= 1000,\n forall |i: int| 0 <= i < target.len() ==> 1 <= #[trigger] target[i] <= 1000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 1000,\n ensures\n res == (forall |v: i32| Self::count(target@, v) == Self::count(arr@, v)),\n {\n let n = target.len();\n let mut counts: Vec = Vec::new();\n let mut k: usize = 0;\n while k < 1001\n invariant\n 0 <= k <= 1001,\n counts.len() == k,\n forall |j: int| 0 <= j < k ==> counts[j] == 0i32,\n decreases 1001 - k,\n {\n counts.push(0i32);\n k = k + 1;\n }\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == target.len(),\n n == arr.len(),\n n <= 1000,\n counts.len() == 1001,\n forall |j: int| 0 <= j < target.len() ==> 1 <= #[trigger] target[j] <= 1000,\n forall |j: int| 0 <= j < arr.len() ==> 1 <= #[trigger] arr[j] <= 1000,\n forall |v: int| 0 <= v <= 1000 ==>\n counts[v] == Self::count(target@.take(i as int), v as i32)\n - Self::count(arr@.take(i as int), v as i32),\n decreases n - i,\n {\n let tv = target[i];\n let av = arr[i];\n\n proof {\n Self::count_nonneg(target@.take(i as int), tv);\n Self::count_upper_bound(target@.take(i as int), tv);\n Self::count_nonneg(arr@.take(i as int), tv);\n Self::count_upper_bound(arr@.take(i as int), tv);\n Self::count_nonneg(target@.take(i as int), av);\n Self::count_upper_bound(target@.take(i as int), av);\n Self::count_nonneg(arr@.take(i as int), av);\n Self::count_upper_bound(arr@.take(i as int), av);\n\n assert forall |v: int| 0 <= v <= 1000 implies\n (#[trigger] Self::count(target@.take(i as int + 1), v as i32))\n == Self::count(target@.take(i as int), v as i32)\n + (if target@[i as int] == v as i32 { 1int } else { 0int })\n by {\n Self::count_take_step(target@, v as i32, i as int);\n };\n assert forall |v: int| 0 <= v <= 1000 implies\n (#[trigger] Self::count(arr@.take(i as int + 1), v as i32))\n == Self::count(arr@.take(i as int), v as i32)\n + (if arr@[i as int] == v as i32 { 1int } else { 0int })\n by {\n Self::count_take_step(arr@, v as i32, i as int);\n };\n }\n\n counts.set(tv as usize, counts[tv as usize] + 1);\n counts.set(av as usize, counts[av as usize] - 1);\n i = i + 1;\n }\n\n assert(target@.take(n as int) =~= target@);\n assert(arr@.take(n as int) =~= arr@);\n\n let mut k2: usize = 0;\n while k2 < 1001\n invariant\n 0 <= k2 <= 1001,\n counts.len() == 1001,\n forall |v: int| 0 <= v <= 1000 ==>\n counts[v] == Self::count(target@, v as i32)\n - Self::count(arr@, v as i32),\n forall |v: int| 0 <= v < k2 ==> counts[v] == 0i32,\n decreases 1001 - k2,\n {\n if counts[k2] != 0 {\n return false;\n }\n k2 = k2 + 1;\n }\n\n proof {\n assert forall |v: i32| Self::count(target@, v) == Self::count(arr@, v) by {\n if 0 <= v <= 1000 {\n assert(counts[v as int] == 0i32);\n } else {\n Self::count_zero_for_absent(target@, v);\n Self::count_zero_for_absent(arr@, v);\n }\n };\n }\n\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1460/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1460/tests/mutated_testcases.jsonl"} {"problem": "lc1463", "family": "leetcode", "description": "# Cherry Pickup II\n\nYou are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell.\n\nYou have two robots that can collect cherries for you:\n\n- **Robot #1** is located at the **top-left corner** `(0, 0)`, and\n- **Robot #2** is located at the **top-right corner** `(0, cols - 1)`.\n\nReturn the maximum number of cherries collection using both robots by following the rules below:\n\n- From a cell `(i, j)`, robots can move to cell `(i + 1, j - 1)`, `(i + 1, j)`, or `(i + 1, j + 1)`.\n- When any robot passes through a cell, it picks up all cherries, and the cell becomes an empty cell.\n- When both robots stay in the same cell, only one takes the cherries.\n- Both robots cannot move outside of the grid at any moment.\n- Both robots should reach the bottom row in `grid`.\n\n## Example 1:\n\n> **Input:** grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]\n> **Output:** 24\n> **Explanation:** Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24.\n\n## Example 2:\n\n> **Input:** grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]\n> **Output:** 28\n> **Explanation:** Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17. Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. Total of cherries: 17 + 11 = 28.\n\n## Constraints:\n\n- `rows == grid.length`\n- `cols == grid[i].length`\n- `2 <= rows, cols <= 70`\n- `0 <= grid[i][j] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn cherry_pickup(grid: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Dynamic Programming, Matrix\nAcceptance rate: 72.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max2(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n \n \n pub open spec fn cherry_value(grid: Seq>, row: int, c1: int, c2: int) -> int {\n if c1 == c2 {\n grid[row][c1] as int\n } else {\n grid[row][c1] as int + grid[row][c2] as int\n }\n }\n\n \n \n \n \n \n \n \n pub open spec fn dp_val(grid: Seq>, row: int, c1: int, c2: int) -> int\n decreases grid.len() - row\n {\n let n = grid[0].len() as int;\n if row < 0 || row >= grid.len() || c1 < 0 || c1 >= n || c2 < 0 || c2 >= n {\n 0\n } else if row >= grid.len() - 1 {\n Self::cherry_value(grid, row, c1, c2)\n } else {\n Self::cherry_value(grid, row, c1, c2) + Self::max2(\n Self::max2(\n Self::max2(\n Self::dp_val(grid, row + 1, c1 - 1, c2 - 1),\n Self::dp_val(grid, row + 1, c1 - 1, c2)\n ),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 - 1, c2 + 1),\n Self::dp_val(grid, row + 1, c1, c2 - 1)\n )\n ),\n Self::max2(\n Self::max2(\n Self::dp_val(grid, row + 1, c1, c2),\n Self::dp_val(grid, row + 1, c1, c2 + 1)\n ),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 + 1, c2 - 1),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 + 1, c2),\n Self::dp_val(grid, row + 1, c1 + 1, c2 + 1)\n )\n )\n )\n )\n }\n }\n\n pub fn cherry_pickup(grid: Vec>) -> (result: i32)\n requires\n 2 <= grid.len() <= 70,\n 2 <= grid[0].len() <= 70,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> 0 <= #[trigger] grid[i][j] <= 100,\n ensures\n result as int == Self::dp_val(grid@, 0, 0, grid[0].len() as int - 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn max_i32(a: i32, b: i32) -> i32 {\n if a >= b { a } else { b }\n }\n\n pub fn cherry_pickup(grid: Vec>) -> i32 {\n let rows = grid.len();\n let cols = grid[0].len();\n let n = cols;\n let mut dp: Vec> = Vec::new();\n let mut c1: usize = 0;\n while c1 < n {\n let mut row_vec: Vec = Vec::new();\n let mut c2: usize = 0;\n while c2 < n {\n let val: i32 = if c1 == c2 {\n grid[rows - 1][c1]\n } else {\n grid[rows - 1][c1] + grid[rows - 1][c2]\n };\n row_vec.push(val);\n c2 += 1;\n }\n dp.push(row_vec);\n c1 += 1;\n }\n let mut ri: usize = 1;\n while ri < rows {\n let r = rows - 1 - ri;\n let mut new_dp: Vec> = Vec::new();\n let mut c1: usize = 0;\n while c1 < n {\n let mut row_vec: Vec = Vec::new();\n let mut c2: usize = 0;\n while c2 < n {\n let cherries: i32 = if c1 == c2 { grid[r][c1] } else { grid[r][c1] + grid[r][c2] };\n let v_1_1 = if c1 > 0 && c2 > 0 { dp[c1 - 1][c2 - 1] } else { 0 };\n let v_10 = if c1 > 0 { dp[c1 - 1][c2] } else { 0 };\n let v_11 = if c1 > 0 && c2 + 1 < n { dp[c1 - 1][c2 + 1] } else { 0 };\n let v0_1 = if c2 > 0 { dp[c1][c2 - 1] } else { 0 };\n let v00 = dp[c1][c2];\n let v01 = if c2 + 1 < n { dp[c1][c2 + 1] } else { 0 };\n let v1_1 = if c1 + 1 < n && c2 > 0 { dp[c1 + 1][c2 - 1] } else { 0 };\n let v10 = if c1 + 1 < n { dp[c1 + 1][c2] } else { 0 };\n let v11 = if c1 + 1 < n && c2 + 1 < n { dp[c1 + 1][c2 + 1] } else { 0 };\n let best = Self::max_i32(\n Self::max_i32(\n Self::max_i32(v_1_1, v_10),\n Self::max_i32(v_11, v0_1)\n ),\n Self::max_i32(\n Self::max_i32(v00, v01),\n Self::max_i32(v1_1, Self::max_i32(v10, v11))\n )\n );\n row_vec.push(cherries + best);\n c2 += 1;\n }\n new_dp.push(row_vec);\n c1 += 1;\n }\n dp = new_dp;\n ri += 1;\n }\n dp[0][n - 1]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max2(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n \n \n pub open spec fn cherry_value(grid: Seq>, row: int, c1: int, c2: int) -> int {\n if c1 == c2 {\n grid[row][c1] as int\n } else {\n grid[row][c1] as int + grid[row][c2] as int\n }\n }\n\n \n \n \n \n \n \n \n pub open spec fn dp_val(grid: Seq>, row: int, c1: int, c2: int) -> int\n decreases grid.len() - row\n {\n let n = grid[0].len() as int;\n if row < 0 || row >= grid.len() || c1 < 0 || c1 >= n || c2 < 0 || c2 >= n {\n 0\n } else if row >= grid.len() - 1 {\n Self::cherry_value(grid, row, c1, c2)\n } else {\n Self::cherry_value(grid, row, c1, c2) + Self::max2(\n Self::max2(\n Self::max2(\n Self::dp_val(grid, row + 1, c1 - 1, c2 - 1),\n Self::dp_val(grid, row + 1, c1 - 1, c2)\n ),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 - 1, c2 + 1),\n Self::dp_val(grid, row + 1, c1, c2 - 1)\n )\n ),\n Self::max2(\n Self::max2(\n Self::dp_val(grid, row + 1, c1, c2),\n Self::dp_val(grid, row + 1, c1, c2 + 1)\n ),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 + 1, c2 - 1),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 + 1, c2),\n Self::dp_val(grid, row + 1, c1 + 1, c2 + 1)\n )\n )\n )\n )\n }\n }\n\n fn max_i32(a: i32, b: i32) -> (res: i32)\n ensures\n res as int == Self::max2(a as int, b as int),\n {\n if a >= b { a } else { b }\n }\n\n pub fn cherry_pickup(grid: Vec>) -> (result: i32)\n requires\n 2 <= grid.len() <= 70,\n 2 <= grid[0].len() <= 70,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> 0 <= #[trigger] grid[i][j] <= 100,\n ensures\n result as int == Self::dp_val(grid@, 0, 0, grid[0].len() as int - 1),\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let n = cols;\n let mut dp: Vec> = Vec::new();\n let mut c1: usize = 0;\n while c1 < n {\n let mut row_vec: Vec = Vec::new();\n let mut c2: usize = 0;\n while c2 < n {\n let val: i32 = if c1 == c2 {\n grid[rows - 1][c1]\n } else {\n grid[rows - 1][c1] + grid[rows - 1][c2]\n };\n row_vec.push(val);\n c2 += 1;\n }\n dp.push(row_vec);\n c1 += 1;\n }\n let mut ri: usize = 1;\n while ri < rows {\n let r = rows - 1 - ri;\n let mut new_dp: Vec> = Vec::new();\n let mut c1: usize = 0;\n while c1 < n {\n let mut row_vec: Vec = Vec::new();\n let mut c2: usize = 0;\n while c2 < n {\n let cherries: i32 = if c1 == c2 { grid[r][c1] } else { grid[r][c1] + grid[r][c2] };\n let v_1_1 = if c1 > 0 && c2 > 0 { dp[c1 - 1][c2 - 1] } else { 0 };\n let v_10 = if c1 > 0 { dp[c1 - 1][c2] } else { 0 };\n let v_11 = if c1 > 0 && c2 + 1 < n { dp[c1 - 1][c2 + 1] } else { 0 };\n let v0_1 = if c2 > 0 { dp[c1][c2 - 1] } else { 0 };\n let v00 = dp[c1][c2];\n let v01 = if c2 + 1 < n { dp[c1][c2 + 1] } else { 0 };\n let v1_1 = if c1 + 1 < n && c2 > 0 { dp[c1 + 1][c2 - 1] } else { 0 };\n let v10 = if c1 + 1 < n { dp[c1 + 1][c2] } else { 0 };\n let v11 = if c1 + 1 < n && c2 + 1 < n { dp[c1 + 1][c2 + 1] } else { 0 };\n let best = Self::max_i32(\n Self::max_i32(\n Self::max_i32(v_1_1, v_10),\n Self::max_i32(v_11, v0_1)\n ),\n Self::max_i32(\n Self::max_i32(v00, v01),\n Self::max_i32(v1_1, Self::max_i32(v10, v11))\n )\n );\n row_vec.push(cherries + best);\n c2 += 1;\n }\n new_dp.push(row_vec);\n c1 += 1;\n }\n dp = new_dp;\n ri += 1;\n }\n dp[0][n - 1]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max2(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n \n \n pub open spec fn cherry_value(grid: Seq>, row: int, c1: int, c2: int) -> int {\n if c1 == c2 {\n grid[row][c1] as int\n } else {\n grid[row][c1] as int + grid[row][c2] as int\n }\n }\n\n \n \n \n \n \n \n \n pub open spec fn dp_val(grid: Seq>, row: int, c1: int, c2: int) -> int\n decreases grid.len() - row\n {\n let n = grid[0].len() as int;\n if row < 0 || row >= grid.len() || c1 < 0 || c1 >= n || c2 < 0 || c2 >= n {\n 0\n } else if row >= grid.len() - 1 {\n Self::cherry_value(grid, row, c1, c2)\n } else {\n Self::cherry_value(grid, row, c1, c2) + Self::max2(\n Self::max2(\n Self::max2(\n Self::dp_val(grid, row + 1, c1 - 1, c2 - 1),\n Self::dp_val(grid, row + 1, c1 - 1, c2)\n ),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 - 1, c2 + 1),\n Self::dp_val(grid, row + 1, c1, c2 - 1)\n )\n ),\n Self::max2(\n Self::max2(\n Self::dp_val(grid, row + 1, c1, c2),\n Self::dp_val(grid, row + 1, c1, c2 + 1)\n ),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 + 1, c2 - 1),\n Self::max2(\n Self::dp_val(grid, row + 1, c1 + 1, c2),\n Self::dp_val(grid, row + 1, c1 + 1, c2 + 1)\n )\n )\n )\n )\n }\n }\n\n proof fn lemma_dp_val_bounded(grid: Seq>, row: int)\n requires\n grid.len() >= 2,\n grid[0].len() >= 2,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> 0 <= #[trigger] grid[i][j] <= 100,\n 0 <= row <= grid.len() as int,\n ensures\n forall |c1: int, c2: int| 0 <= c1 < grid[0].len() && 0 <= c2 < grid[0].len() ==>\n 0 <= #[trigger] Self::dp_val(grid, row, c1, c2) <= 200 * (grid.len() - row),\n decreases grid.len() - row,\n {\n let n = grid[0].len() as int;\n if row >= grid.len() as int {\n \n } else if row == grid.len() - 1 {\n \n assert forall |c1: int, c2: int| 0 <= c1 < n && 0 <= c2 < n implies\n 0 <= #[trigger] Self::dp_val(grid, row, c1, c2) <= 200 * (grid.len() - row)\n by {\n assert(grid[row].len() == n);\n assert(0 <= Self::cherry_value(grid, row, c1, c2) <= 200);\n };\n } else {\n \n Self::lemma_dp_val_bounded(grid, row + 1);\n assert forall |c1: int, c2: int| 0 <= c1 < n && 0 <= c2 < n implies\n 0 <= #[trigger] Self::dp_val(grid, row, c1, c2) <= 200 * (grid.len() - row)\n by {\n assert(grid[row].len() == n);\n assert(0 <= Self::cherry_value(grid, row, c1, c2) <= 200);\n \n \n let rr = row + 1;\n if c1 - 1 >= 0 && c2 - 1 >= 0 {\n assert(0 <= Self::dp_val(grid, rr, c1 - 1, c2 - 1) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1 - 1, c2 - 1) == 0);\n }\n if c1 - 1 >= 0 {\n assert(0 <= Self::dp_val(grid, rr, c1 - 1, c2) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1 - 1, c2) == 0);\n }\n if c1 - 1 >= 0 && c2 + 1 < n {\n assert(0 <= Self::dp_val(grid, rr, c1 - 1, c2 + 1) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1 - 1, c2 + 1) == 0);\n }\n if c2 - 1 >= 0 {\n assert(0 <= Self::dp_val(grid, rr, c1, c2 - 1) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1, c2 - 1) == 0);\n }\n assert(0 <= Self::dp_val(grid, rr, c1, c2) <= 200 * (grid.len() - rr));\n if c2 + 1 < n {\n assert(0 <= Self::dp_val(grid, rr, c1, c2 + 1) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1, c2 + 1) == 0);\n }\n if c1 + 1 < n && c2 - 1 >= 0 {\n assert(0 <= Self::dp_val(grid, rr, c1 + 1, c2 - 1) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1 + 1, c2 - 1) == 0);\n }\n if c1 + 1 < n {\n assert(0 <= Self::dp_val(grid, rr, c1 + 1, c2) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1 + 1, c2) == 0);\n }\n if c1 + 1 < n && c2 + 1 < n {\n assert(0 <= Self::dp_val(grid, rr, c1 + 1, c2 + 1) <= 200 * (grid.len() - rr));\n } else {\n assert(Self::dp_val(grid, rr, c1 + 1, c2 + 1) == 0);\n }\n assert(200 + 200 * (grid.len() - row - 1) == 200 * (grid.len() - row))\n by(nonlinear_arith) requires grid.len() > row {};\n };\n }\n }\n\n fn max_i32(a: i32, b: i32) -> (res: i32)\n ensures\n res as int == Self::max2(a as int, b as int),\n {\n if a >= b { a } else { b }\n }\n\n pub fn cherry_pickup(grid: Vec>) -> (result: i32)\n requires\n 2 <= grid.len() <= 70,\n 2 <= grid[0].len() <= 70,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid[i].len() ==> 0 <= #[trigger] grid[i][j] <= 100,\n ensures\n result as int == Self::dp_val(grid@, 0, 0, grid[0].len() as int - 1),\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let n = cols;\n let mut dp: Vec> = Vec::new();\n let mut c1: usize = 0;\n while c1 < n\n invariant\n c1 <= n,\n n == cols,\n cols == grid[0].len(),\n rows == grid.len(),\n 2 <= rows <= 70,\n 2 <= n <= 70,\n forall |i: int| 0 <= i < rows as int ==> #[trigger] grid[i].len() == n as int,\n forall |i: int, j: int|\n 0 <= i < rows as int && 0 <= j < n as int ==> 0 <= #[trigger] grid[i][j] <= 100,\n dp@.len() == c1 as int,\n forall |a: int| 0 <= a < c1 as int ==> (#[trigger] dp@[a]).len() == n as int,\n forall |a: int, b: int| 0 <= a < c1 as int && 0 <= b < n as int ==>\n dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, (rows - 1) as int, a, b)),\n decreases n - c1,\n {\n let mut row_vec: Vec = Vec::new();\n let mut c2: usize = 0;\n while c2 < n\n invariant\n c2 <= n,\n c1 < n,\n n == cols,\n cols == grid[0].len(),\n rows == grid.len(),\n 2 <= rows <= 70,\n 2 <= n <= 70,\n forall |i: int| 0 <= i < rows as int ==> #[trigger] grid[i].len() == n as int,\n forall |i: int, j: int|\n 0 <= i < rows as int && 0 <= j < n as int ==> 0 <= #[trigger] grid[i][j] <= 100,\n row_vec@.len() == c2 as int,\n forall |b: int| 0 <= b < c2 as int ==>\n row_vec@[b] as int == (#[trigger] Self::dp_val(grid@, (rows - 1) as int, c1 as int, b)),\n decreases n - c2,\n {\n proof {\n assert(grid@[(rows - 1) as int].len() == n as int);\n }\n let val: i32 = if c1 == c2 {\n grid[rows - 1][c1]\n } else {\n grid[rows - 1][c1] + grid[rows - 1][c2]\n };\n proof {\n assert(val as int == Self::cherry_value(grid@, (rows - 1) as int, c1 as int, c2 as int));\n assert((rows - 1) as int >= grid@.len() - 1);\n assert(val as int == Self::dp_val(grid@, (rows - 1) as int, c1 as int, c2 as int));\n }\n row_vec.push(val);\n c2 += 1;\n }\n dp.push(row_vec);\n proof {\n assert(dp@.len() == c1 as int + 1);\n assert forall |a: int| 0 <= a < c1 as int + 1 implies\n (#[trigger] dp@[a]).len() == n as int\n by {\n if a == c1 as int {} else {}\n };\n assert forall |a: int, b: int| 0 <= a < c1 as int + 1 && 0 <= b < n as int implies\n dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, (rows - 1) as int, a, b))\n by {\n if a == c1 as int {} else {}\n };\n }\n c1 += 1;\n }\n let mut ri: usize = 1;\n while ri < rows\n invariant\n 1 <= ri <= rows,\n n == cols,\n cols == grid[0].len(),\n rows == grid.len(),\n 2 <= rows <= 70,\n 2 <= n <= 70,\n forall |i: int| 0 <= i < rows as int ==> #[trigger] grid[i].len() == n as int,\n forall |i: int, j: int|\n 0 <= i < rows as int && 0 <= j < n as int ==> 0 <= #[trigger] grid[i][j] <= 100,\n dp@.len() == n as int,\n forall |a: int| 0 <= a < n as int ==> (#[trigger] dp@[a]).len() == n as int,\n forall |a: int, b: int| 0 <= a < n as int && 0 <= b < n as int ==>\n dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, (rows - ri) as int, a, b)),\n decreases rows - ri,\n {\n let r = rows - 1 - ri;\n proof {\n Self::lemma_dp_val_bounded(grid@, (r + 1) as int);\n }\n let mut new_dp: Vec> = Vec::new();\n let mut c1: usize = 0;\n while c1 < n\n invariant\n c1 <= n,\n n == cols,\n cols == grid[0].len(),\n rows == grid.len(),\n 2 <= rows <= 70,\n 2 <= n <= 70,\n 1 <= ri < rows,\n r == rows - 1 - ri,\n r < rows - 1,\n forall |i: int| 0 <= i < rows as int ==> #[trigger] grid[i].len() == n as int,\n forall |i: int, j: int|\n 0 <= i < rows as int && 0 <= j < n as int ==> 0 <= #[trigger] grid[i][j] <= 100,\n dp@.len() == n as int,\n forall |a: int| 0 <= a < n as int ==> (#[trigger] dp@[a]).len() == n as int,\n forall |a: int, b: int| 0 <= a < n as int && 0 <= b < n as int ==>\n dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, (r + 1) as int, a, b)),\n forall |c1i: int, c2i: int| 0 <= c1i < n as int && 0 <= c2i < n as int ==>\n 0 <= (#[trigger] Self::dp_val(grid@, (r + 1) as int, c1i, c2i)) <= 200 * (rows as int - r as int - 1),\n new_dp@.len() == c1 as int,\n forall |a: int| 0 <= a < c1 as int ==> (#[trigger] new_dp@[a]).len() == n as int,\n forall |a: int, b: int| 0 <= a < c1 as int && 0 <= b < n as int ==>\n new_dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, r as int, a, b)),\n decreases n - c1,\n {\n let mut row_vec: Vec = Vec::new();\n let mut c2: usize = 0;\n while c2 < n\n invariant\n c2 <= n,\n c1 < n,\n n == cols,\n cols == grid[0].len(),\n rows == grid.len(),\n 2 <= rows <= 70,\n 2 <= n <= 70,\n 1 <= ri < rows,\n r == rows - 1 - ri,\n r < rows - 1,\n forall |i: int| 0 <= i < rows as int ==> #[trigger] grid[i].len() == n as int,\n forall |i: int, j: int|\n 0 <= i < rows as int && 0 <= j < n as int ==> 0 <= #[trigger] grid[i][j] <= 100,\n dp@.len() == n as int,\n forall |a: int| 0 <= a < n as int ==> (#[trigger] dp@[a]).len() == n as int,\n forall |a: int, b: int| 0 <= a < n as int && 0 <= b < n as int ==>\n dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, (r + 1) as int, a, b)),\n forall |c1i: int, c2i: int| 0 <= c1i < n as int && 0 <= c2i < n as int ==>\n 0 <= (#[trigger] Self::dp_val(grid@, (r + 1) as int, c1i, c2i)) <= 200 * (rows as int - r as int - 1),\n row_vec@.len() == c2 as int,\n forall |b: int| 0 <= b < c2 as int ==>\n row_vec@[b] as int == (#[trigger] Self::dp_val(grid@, r as int, c1 as int, b)),\n decreases n - c2,\n {\n proof {\n assert(grid@[r as int].len() == n as int);\n }\n let cherries: i32 = if c1 == c2 { grid[r][c1] } else { grid[r][c1] + grid[r][c2] };\n let v_1_1 = if c1 > 0 && c2 > 0 { dp[c1 - 1][c2 - 1] } else { 0 };\n let v_10 = if c1 > 0 { dp[c1 - 1][c2] } else { 0 };\n let v_11 = if c1 > 0 && c2 + 1 < n { dp[c1 - 1][c2 + 1] } else { 0 };\n let v0_1 = if c2 > 0 { dp[c1][c2 - 1] } else { 0 };\n let v00 = dp[c1][c2];\n let v01 = if c2 + 1 < n { dp[c1][c2 + 1] } else { 0 };\n let v1_1 = if c1 + 1 < n && c2 > 0 { dp[c1 + 1][c2 - 1] } else { 0 };\n let v10 = if c1 + 1 < n { dp[c1 + 1][c2] } else { 0 };\n let v11 = if c1 + 1 < n && c2 + 1 < n { dp[c1 + 1][c2 + 1] } else { 0 };\n proof {\n let ri_ = (r + 1) as int;\n let c1_ = c1 as int;\n let c2_ = c2 as int;\n assert(v_1_1 as int == Self::dp_val(grid@, ri_, c1_ - 1, c2_ - 1));\n assert(v_10 as int == Self::dp_val(grid@, ri_, c1_ - 1, c2_));\n assert(v_11 as int == Self::dp_val(grid@, ri_, c1_ - 1, c2_ + 1));\n assert(v0_1 as int == Self::dp_val(grid@, ri_, c1_, c2_ - 1));\n assert(v00 as int == Self::dp_val(grid@, ri_, c1_, c2_));\n assert(v01 as int == Self::dp_val(grid@, ri_, c1_, c2_ + 1));\n assert(v1_1 as int == Self::dp_val(grid@, ri_, c1_ + 1, c2_ - 1));\n assert(v10 as int == Self::dp_val(grid@, ri_, c1_ + 1, c2_));\n assert(v11 as int == Self::dp_val(grid@, ri_, c1_ + 1, c2_ + 1));\n assert(0 <= v_1_1 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v_10 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v_11 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v0_1 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v00 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v01 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v1_1 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v10 as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= v11 as int <= 200 * (rows as int - r as int - 1));\n }\n let best = Self::max_i32(\n Self::max_i32(\n Self::max_i32(v_1_1, v_10),\n Self::max_i32(v_11, v0_1)\n ),\n Self::max_i32(\n Self::max_i32(v00, v01),\n Self::max_i32(v1_1, Self::max_i32(v10, v11))\n )\n );\n proof {\n let ri_ = (r + 1) as int;\n let c1_ = c1 as int;\n let c2_ = c2 as int;\n assert(0 <= best as int <= 200 * (rows as int - r as int - 1));\n assert(0 <= cherries as int <= 200);\n assert(200 * (rows as int - r as int - 1) <= 200 * 69) by(nonlinear_arith)\n requires rows as int - r as int - 1 <= 69 {};\n assert(cherries as int + best as int <= i32::MAX);\n assert(cherries as int + best as int >= i32::MIN);\n assert(cherries as int == Self::cherry_value(grid@, r as int, c1_, c2_));\n assert(best as int == Self::max2(\n Self::max2(\n Self::max2(\n Self::dp_val(grid@, ri_, c1_ - 1, c2_ - 1),\n Self::dp_val(grid@, ri_, c1_ - 1, c2_)\n ),\n Self::max2(\n Self::dp_val(grid@, ri_, c1_ - 1, c2_ + 1),\n Self::dp_val(grid@, ri_, c1_, c2_ - 1)\n )\n ),\n Self::max2(\n Self::max2(\n Self::dp_val(grid@, ri_, c1_, c2_),\n Self::dp_val(grid@, ri_, c1_, c2_ + 1)\n ),\n Self::max2(\n Self::dp_val(grid@, ri_, c1_ + 1, c2_ - 1),\n Self::max2(\n Self::dp_val(grid@, ri_, c1_ + 1, c2_),\n Self::dp_val(grid@, ri_, c1_ + 1, c2_ + 1)\n )\n )\n )\n ));\n assert((cherries + best) as int == Self::dp_val(grid@, r as int, c1_, c2_));\n }\n row_vec.push(cherries + best);\n c2 += 1;\n }\n new_dp.push(row_vec);\n proof {\n assert(new_dp@.len() == c1 as int + 1);\n assert forall |a: int| 0 <= a < c1 as int + 1 implies\n (#[trigger] new_dp@[a]).len() == n as int\n by {\n if a == c1 as int {} else {}\n };\n assert forall |a: int, b: int| 0 <= a < c1 as int + 1 && 0 <= b < n as int implies\n new_dp@[a][b] as int == (#[trigger] Self::dp_val(grid@, r as int, a, b))\n by {\n if a == c1 as int {} else {}\n };\n }\n c1 += 1;\n }\n dp = new_dp;\n proof {\n assert((rows - (ri + 1)) as int == r as int);\n }\n ri += 1;\n }\n proof {\n assert(ri == rows);\n assert((rows - ri) as int == 0int);\n \n assert(dp@[0int].len() == n as int);\n assert(dp@[0int][(n - 1) as int] as int == Self::dp_val(grid@, 0int, 0int, (n - 1) as int));\n }\n dp[0][n - 1]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1463/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1463/tests/mutated_testcases.jsonl"} {"problem": "lc1473", "family": "leetcode", "description": "# Paint House III\n\nThere is a row of `m` houses in a small city, each house must be painted with one of the `n` colors (labeled from `1` to `n`), some houses that have been painted last summer should not be painted again.\n\nA neighborhood is a maximal group of continuous houses that are painted with the same color.\n\n- For example: `houses = [1,2,2,3,3,2,1,1]` contains `5` neighborhoods `[{1}, {2,2}, {3,3}, {2}, {1,1}]`.\n\nGiven an array `houses`, an `m x n` matrix `cost` and an integer `target` where:\n\n- `houses[i]`: is the color of the house `i`, and `0` if the house is not painted yet.\n- `cost[i][j]`: is the cost of paint the house `i` with the color `j + 1`.\n\nReturn the minimum cost of painting all the remaining houses in such a way that there are exactly `target` neighborhoods. If it is not possible, return `-1`.\n\n## Example 1:\n\n> **Input:** houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3\n> **Output:** 9\n> **Explanation:** Paint houses of this way [1,2,2,1,1]. This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9.\n\n## Example 2:\n\n> **Input:** houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3\n> **Output:** 11\n> **Explanation:** Some houses are already painted. Paint the houses of this way [2,2,1,2,2]. This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11.\n\n## Example 3:\n\n> **Input:** houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3\n> **Output:** -1\n> **Explanation:** Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3.\n\n## Constraints:\n\n- `m == houses.length == cost.length`\n- `n == cost[i].length`\n- $1 \\leq m \\leq 100$\n- $1 \\leq n \\leq 20$\n- $1 \\leq \\text{target} \\leq m$\n- $0 \\leq \\text{houses}[i] \\leq n$\n- $1 \\leq \\text{cost}[i][j] \\leq 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_cost(houses: Vec, cost: Vec>, m: i32, n: i32, target: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Dynamic Programming\nAcceptance rate: 61.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\npub open spec fn valid_coloring(houses: Seq, colors: Seq, n: int) -> bool {\n houses.len() == colors.len() &&\n forall|i: int| 0 <= i < houses.len() as int ==> (\n 1 <= #[trigger] colors[i] <= n &&\n (houses[i] != 0 ==> colors[i] == houses[i] as int)\n )\n}\n\n\npub open spec fn count_neighborhoods(colors: Seq) -> int\n decreases colors.len(),\n{\n if colors.len() <= 0 {\n 0\n } else if colors.len() == 1 {\n 1\n } else {\n count_neighborhoods(colors.drop_last()) +\n if colors.last() != colors[colors.len() - 2] { 1int } else { 0int }\n }\n}\n\n\npub open spec fn total_paint_cost(\n houses: Seq,\n cost: Seq>,\n colors: Seq,\n len: int,\n) -> int\n decreases len,\n{\n if len <= 0 {\n 0int\n } else {\n total_paint_cost(houses, cost, colors, len - 1) +\n if houses[len - 1] != 0i32 { 0int } else { cost[len - 1]@[colors[len - 1] - 1] as int }\n }\n}\n\n\n\n\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn dp_spec(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n j: int,\n k: int,\n) -> int\n decreases i, 0int,\n{\n if j < 1 || j > n || k < 1 || k > i + 1 {\n 1_000_001int\n } else if i == 0 {\n if k != 1 {\n 1_000_001int\n } else if houses[0] as int != 0 && houses[0] as int != j {\n 1_000_001int\n } else if houses[0] as int != 0 {\n 0int\n } else {\n cost[0]@[j - 1] as int\n }\n } else if houses[i] as int != 0 && houses[i] as int != j {\n 1_000_001int\n } else {\n let paint_cost: int = if houses[i] as int != 0 {\n 0int\n } else {\n cost[i]@[j - 1] as int\n };\n let same = dp_spec(houses, cost, n, i - 1, j, k);\n let diff = min_excluding(houses, cost, n, i - 1, j, k - 1, 1);\n let best = spec_min(same, diff);\n if best >= 1_000_001int || paint_cost + best >= 1_000_001int {\n 1_000_001int\n } else {\n paint_cost + best\n }\n }\n}\n\n\npub open spec fn min_excluding(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n exclude: int,\n k: int,\n from_c: int,\n) -> int\n decreases i, n - from_c + 1,\n{\n if from_c > n {\n 1_000_001int\n } else if from_c == exclude {\n min_excluding(houses, cost, n, i, exclude, k, from_c + 1)\n } else {\n spec_min(\n dp_spec(houses, cost, n, i, from_c, k),\n min_excluding(houses, cost, n, i, exclude, k, from_c + 1),\n )\n }\n}\n\n\npub open spec fn min_final(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n target: int,\n from_c: int,\n) -> int\n decreases n - from_c + 1,\n{\n if from_c > n {\n 1_000_001int\n } else {\n spec_min(\n dp_spec(houses, cost, n, m - 1, from_c, target),\n min_final(houses, cost, n, m, target, from_c + 1),\n )\n }\n}\n\n\n\npub open spec fn answer_spec(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n target: int,\n) -> int {\n let min_val = min_final(houses, cost, n, m, target, 1);\n if min_val >= 1_000_001int {\n -1int\n } else {\n min_val\n }\n}\n\nimpl Solution {\n pub fn min_cost(\n houses: Vec,\n cost: Vec>,\n m: i32,\n n: i32,\n target: i32,\n ) -> (result: i32)\n requires\n m as int == houses@.len(),\n m as int == cost@.len(),\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n forall|i: int| 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len() == n as int,\n forall|i: int, j: int|\n 0 <= i < m as int && 0 <= j < n as int ==> 1 <= #[trigger] cost@[i]@[j]\n <= 10_000,\n ensures\n result as int == answer_spec(houses@, cost@, n as int, m as int, target as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_cost(\n houses: Vec,\n cost: Vec>,\n m: i32,\n n: i32,\n target: i32,\n ) -> i32 {\n let m_us = m as usize;\n let n_us = n as usize;\n let target_us = target as usize;\n let stride: usize = target_us + 1;\n let dp_size: usize = (n_us + 1) * stride;\n\n let mut prev_dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < dp_size {\n prev_dp.push(1_000_001i32);\n idx += 1;\n }\n\n let mut j: usize = 1;\n while j <= n_us {\n if houses[0] != 0 {\n if houses[0] as usize == j {\n prev_dp[j * stride + 1] = 0i32;\n }\n } else {\n prev_dp[j * stride + 1] = cost[0][j - 1];\n }\n j += 1;\n }\n\n let mut i: usize = 1;\n while i < m_us {\n let mut curr_dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < dp_size {\n curr_dp.push(1_000_001i32);\n idx += 1;\n }\n\n let mut j: usize = 1;\n while j <= n_us {\n if houses[i] == 0 || houses[i] as usize == j {\n let paint_cost: i32 =\n if houses[i] != 0 { 0i32 } else { cost[i][j - 1] };\n\n let mut k: usize = 1;\n while k <= target_us {\n let same: i32 = prev_dp[j * stride + k];\n\n let mut diff: i32 = 1_000_001i32;\n let mut c: usize = 1;\n while c <= n_us {\n if c != j {\n let val: i32 = prev_dp[c * stride + (k - 1)];\n if val < diff {\n diff = val;\n }\n }\n c += 1;\n }\n\n let best: i32 = if same <= diff { same } else { diff };\n if best < 1_000_001i32 {\n let total: i32 = paint_cost + best;\n if total < 1_000_001i32 {\n curr_dp[j * stride + k] = total;\n }\n }\n\n k += 1;\n }\n }\n j += 1;\n }\n\n prev_dp = curr_dp;\n i += 1;\n }\n\n let mut ans: i32 = 1_000_001i32;\n let mut j: usize = 1;\n while j <= n_us {\n let val: i32 = prev_dp[j * stride + target_us];\n if val < ans {\n ans = val;\n }\n j += 1;\n }\n\n if ans >= 1_000_001i32 { -1i32 } else { ans }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\npub open spec fn valid_coloring(houses: Seq, colors: Seq, n: int) -> bool {\n houses.len() == colors.len() &&\n forall|i: int| 0 <= i < houses.len() as int ==> (\n 1 <= #[trigger] colors[i] <= n &&\n (houses[i] != 0 ==> colors[i] == houses[i] as int)\n )\n}\n\n\npub open spec fn count_neighborhoods(colors: Seq) -> int\n decreases colors.len(),\n{\n if colors.len() <= 0 {\n 0\n } else if colors.len() == 1 {\n 1\n } else {\n count_neighborhoods(colors.drop_last()) +\n if colors.last() != colors[colors.len() - 2] { 1int } else { 0int }\n }\n}\n\n\npub open spec fn total_paint_cost(\n houses: Seq,\n cost: Seq>,\n colors: Seq,\n len: int,\n) -> int\n decreases len,\n{\n if len <= 0 {\n 0int\n } else {\n total_paint_cost(houses, cost, colors, len - 1) +\n if houses[len - 1] != 0i32 { 0int } else { cost[len - 1]@[colors[len - 1] - 1] as int }\n }\n}\n\n\n\n\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn dp_spec(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n j: int,\n k: int,\n) -> int\n decreases i, 0int,\n{\n if j < 1 || j > n || k < 1 || k > i + 1 {\n 1_000_001int\n } else if i == 0 {\n if k != 1 {\n 1_000_001int\n } else if houses[0] as int != 0 && houses[0] as int != j {\n 1_000_001int\n } else if houses[0] as int != 0 {\n 0int\n } else {\n cost[0]@[j - 1] as int\n }\n } else if houses[i] as int != 0 && houses[i] as int != j {\n 1_000_001int\n } else {\n let paint_cost: int = if houses[i] as int != 0 {\n 0int\n } else {\n cost[i]@[j - 1] as int\n };\n let same = dp_spec(houses, cost, n, i - 1, j, k);\n let diff = min_excluding(houses, cost, n, i - 1, j, k - 1, 1);\n let best = spec_min(same, diff);\n if best >= 1_000_001int || paint_cost + best >= 1_000_001int {\n 1_000_001int\n } else {\n paint_cost + best\n }\n }\n}\n\n\npub open spec fn min_excluding(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n exclude: int,\n k: int,\n from_c: int,\n) -> int\n decreases i, n - from_c + 1,\n{\n if from_c > n {\n 1_000_001int\n } else if from_c == exclude {\n min_excluding(houses, cost, n, i, exclude, k, from_c + 1)\n } else {\n spec_min(\n dp_spec(houses, cost, n, i, from_c, k),\n min_excluding(houses, cost, n, i, exclude, k, from_c + 1),\n )\n }\n}\n\n\npub open spec fn min_final(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n target: int,\n from_c: int,\n) -> int\n decreases n - from_c + 1,\n{\n if from_c > n {\n 1_000_001int\n } else {\n spec_min(\n dp_spec(houses, cost, n, m - 1, from_c, target),\n min_final(houses, cost, n, m, target, from_c + 1),\n )\n }\n}\n\n\n\npub open spec fn answer_spec(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n target: int,\n) -> int {\n let min_val = min_final(houses, cost, n, m, target, 1);\n if min_val >= 1_000_001int {\n -1int\n } else {\n min_val\n }\n}\n\nimpl Solution {\n pub fn min_cost(\n houses: Vec,\n cost: Vec>,\n m: i32,\n n: i32,\n target: i32,\n ) -> (result: i32)\n requires\n m as int == houses@.len(),\n m as int == cost@.len(),\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n forall|i: int| 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len() == n as int,\n forall|i: int, j: int|\n 0 <= i < m as int && 0 <= j < n as int ==> 1 <= #[trigger] cost@[i]@[j]\n <= 10_000,\n ensures\n result as int == answer_spec(houses@, cost@, n as int, m as int, target as int),\n {\n let m_us = m as usize;\n let n_us = n as usize;\n let target_us = target as usize;\n let stride: usize = target_us + 1;\n let dp_size: usize = (n_us + 1) * stride;\n\n let mut prev_dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < dp_size {\n prev_dp.push(1_000_001i32);\n idx += 1;\n }\n\n let mut j: usize = 1;\n while j <= n_us {\n if houses[0] != 0 {\n if houses[0] as usize == j {\n prev_dp.set(j * stride + 1, 0i32);\n }\n } else {\n prev_dp.set(j * stride + 1, cost[0][j - 1]);\n }\n j += 1;\n }\n\n let mut i: usize = 1;\n while i < m_us {\n let mut curr_dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < dp_size {\n curr_dp.push(1_000_001i32);\n idx += 1;\n }\n\n let mut j: usize = 1;\n while j <= n_us {\n if houses[i] == 0 || houses[i] as usize == j {\n let paint_cost: i32 =\n if houses[i] != 0 { 0i32 } else { cost[i][j - 1] };\n\n let mut k: usize = 1;\n while k <= target_us {\n let same: i32 = prev_dp[j * stride + k];\n\n let mut diff: i32 = 1_000_001i32;\n let mut c: usize = 1;\n while c <= n_us {\n if c != j {\n let val: i32 = prev_dp[c * stride + (k - 1)];\n if val < diff {\n diff = val;\n }\n }\n c += 1;\n }\n\n let best: i32 = if same <= diff { same } else { diff };\n if best < 1_000_001i32 {\n let total: i32 = paint_cost + best;\n if total < 1_000_001i32 {\n curr_dp.set(j * stride + k, total);\n }\n }\n\n k += 1;\n }\n }\n j += 1;\n }\n\n prev_dp = curr_dp;\n i += 1;\n }\n\n let mut ans: i32 = 1_000_001i32;\n let mut j: usize = 1;\n while j <= n_us {\n let val: i32 = prev_dp[j * stride + target_us];\n if val < ans {\n ans = val;\n }\n j += 1;\n }\n\n if ans >= 1_000_001i32 { -1i32 } else { ans }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\n\n\n\npub open spec fn valid_coloring(houses: Seq, colors: Seq, n: int) -> bool {\n houses.len() == colors.len() &&\n forall|i: int| 0 <= i < houses.len() as int ==> (\n 1 <= #[trigger] colors[i] <= n &&\n (houses[i] != 0 ==> colors[i] == houses[i] as int)\n )\n}\n\n\npub open spec fn count_neighborhoods(colors: Seq) -> int\n decreases colors.len(),\n{\n if colors.len() <= 0 {\n 0\n } else if colors.len() == 1 {\n 1\n } else {\n count_neighborhoods(colors.drop_last()) +\n if colors.last() != colors[colors.len() - 2] { 1int } else { 0int }\n }\n}\n\n\npub open spec fn total_paint_cost(\n houses: Seq,\n cost: Seq>,\n colors: Seq,\n len: int,\n) -> int\n decreases len,\n{\n if len <= 0 {\n 0int\n } else {\n total_paint_cost(houses, cost, colors, len - 1) +\n if houses[len - 1] != 0i32 { 0int } else { cost[len - 1]@[colors[len - 1] - 1] as int }\n }\n}\n\n\n\n\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn dp_spec(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n j: int,\n k: int,\n) -> int\n decreases i, 0int,\n{\n if j < 1 || j > n || k < 1 || k > i + 1 {\n 1_000_001int\n } else if i == 0 {\n if k != 1 {\n 1_000_001int\n } else if houses[0] as int != 0 && houses[0] as int != j {\n 1_000_001int\n } else if houses[0] as int != 0 {\n 0int\n } else {\n cost[0]@[j - 1] as int\n }\n } else if houses[i] as int != 0 && houses[i] as int != j {\n 1_000_001int\n } else {\n let paint_cost: int = if houses[i] as int != 0 {\n 0int\n } else {\n cost[i]@[j - 1] as int\n };\n let same = dp_spec(houses, cost, n, i - 1, j, k);\n let diff = min_excluding(houses, cost, n, i - 1, j, k - 1, 1);\n let best = spec_min(same, diff);\n if best >= 1_000_001int || paint_cost + best >= 1_000_001int {\n 1_000_001int\n } else {\n paint_cost + best\n }\n }\n}\n\n\npub open spec fn min_excluding(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n exclude: int,\n k: int,\n from_c: int,\n) -> int\n decreases i, n - from_c + 1,\n{\n if from_c > n {\n 1_000_001int\n } else if from_c == exclude {\n min_excluding(houses, cost, n, i, exclude, k, from_c + 1)\n } else {\n spec_min(\n dp_spec(houses, cost, n, i, from_c, k),\n min_excluding(houses, cost, n, i, exclude, k, from_c + 1),\n )\n }\n}\n\n\npub open spec fn min_final(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n target: int,\n from_c: int,\n) -> int\n decreases n - from_c + 1,\n{\n if from_c > n {\n 1_000_001int\n } else {\n spec_min(\n dp_spec(houses, cost, n, m - 1, from_c, target),\n min_final(houses, cost, n, m, target, from_c + 1),\n )\n }\n}\n\n\n\npub open spec fn answer_spec(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n target: int,\n) -> int {\n let min_val = min_final(houses, cost, n, m, target, 1);\n if min_val >= 1_000_001int {\n -1int\n } else {\n min_val\n }\n}\n\npub open spec fn dp_idx(j: int, k: int, stride: int) -> int {\n j * stride + k\n}\n\nproof fn lemma_dp_spec_base(\n houses: Seq,\n cost: Seq>,\n n: int,\n jj: int,\n kk: int,\n)\n requires\n houses.len() >= 1,\n cost.len() >= 1,\n cost[0]@.len() == n,\n 1 <= n,\n ensures\n (jj == 0 || kk == 0 || kk > 1 || jj < 1 || jj > n) ==> dp_spec(houses, cost, n, 0, jj,\n kk) == 1_000_001int,\n (1 <= jj <= n && kk == 1 && houses[0] as int != 0 && houses[0] as int == jj) ==> dp_spec(\n houses,\n cost,\n n,\n 0,\n jj,\n kk,\n ) == 0int,\n (1 <= jj <= n && kk == 1 && houses[0] as int != 0 && houses[0] as int != jj) ==> dp_spec(\n houses,\n cost,\n n,\n 0,\n jj,\n kk,\n ) == 1_000_001int,\n (1 <= jj <= n && kk == 1 && houses[0] as int == 0) ==> dp_spec(houses, cost, n, 0, jj,\n kk) == cost[0]@[jj - 1] as int,\n{\n}\n\nproof fn lemma_spec_min_assoc(a: int, b: int, c: int)\n ensures\n spec_min(a, spec_min(b, c)) == spec_min(spec_min(a, b), c),\n{\n}\n\nproof fn lemma_dp_idx_disjoint_j(j1: int, k1: int, j2: int, k2: int, stride: int)\n requires\n stride > 0,\n 0 <= k1 < stride,\n 0 <= k2 < stride,\n j1 >= 0,\n j2 >= 0,\n j1 != j2,\n ensures\n j1 * stride + k1 != j2 * stride + k2,\n{\n assert(j1 * stride + k1 != j2 * stride + k2) by (nonlinear_arith)\n requires stride > 0, 0 <= k1 < stride, 0 <= k2 < stride, j1 >= 0, j2 >= 0, j1 != j2;\n}\n\nproof fn lemma_dp_spec_bound(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n i: int,\n j: int,\n k: int,\n)\n requires\n 1 <= m,\n 1 <= n,\n i < m,\n cost.len() >= m,\n houses.len() >= m,\n forall|ii: int| 0 <= ii < m ==> (#[trigger] cost[ii])@.len() == n,\n forall|ii: int, jj: int| 0 <= ii < m && 0 <= jj < n ==> 1 <= #[trigger] cost[ii]@[jj] <= 10_000,\n ensures\n dp_spec(houses, cost, n, i, j, k) <= 1_000_001,\n decreases i, 0int,\n{\n if j < 1 || j > n || k < 1 || k > i + 1 {\n } else if i == 0 {\n if k != 1 {\n } else if houses[0] as int != 0 && houses[0] as int != j {\n } else if houses[0] as int != 0 {\n } else {\n assert(cost[0]@[j - 1] as int <= 10_000);\n }\n } else if houses[i] as int != 0 && houses[i] as int != j {\n } else {\n lemma_dp_spec_bound(houses, cost, n, m, i - 1, j, k);\n lemma_min_excluding_bound(houses, cost, n, m, i - 1, j, k - 1, 1);\n }\n}\n\nproof fn lemma_min_excluding_bound(\n houses: Seq,\n cost: Seq>,\n n: int,\n m: int,\n i: int,\n exclude: int,\n k: int,\n from_c: int,\n)\n requires\n 1 <= m,\n 1 <= n,\n i < m,\n cost.len() >= m,\n houses.len() >= m,\n forall|ii: int| 0 <= ii < m ==> (#[trigger] cost[ii])@.len() == n,\n forall|ii: int, jj: int| 0 <= ii < m && 0 <= jj < n ==> 1 <= #[trigger] cost[ii]@[jj] <= 10_000,\n ensures\n min_excluding(houses, cost, n, i, exclude, k, from_c) <= 1_000_001,\n decreases i, n - from_c + 1,\n{\n if from_c > n {\n } else if from_c == exclude {\n lemma_min_excluding_bound(houses, cost, n, m, i, exclude, k, from_c + 1);\n } else {\n lemma_dp_spec_bound(houses, cost, n, m, i, from_c, k);\n lemma_min_excluding_bound(houses, cost, n, m, i, exclude, k, from_c + 1);\n }\n}\n\nproof fn lemma_min_excluding_all_large(\n houses: Seq,\n cost: Seq>,\n n: int,\n i: int,\n exclude: int,\n k: int,\n from_c: int,\n)\n requires\n forall|c: int| from_c <= c <= n && c != exclude ==> dp_spec(houses, cost, n, i, c, k) >= 1_000_001int,\n ensures\n min_excluding(houses, cost, n, i, exclude, k, from_c) >= 1_000_001int,\n decreases n - from_c + 1,\n{\n if from_c > n {\n } else if from_c == exclude {\n lemma_min_excluding_all_large(houses, cost, n, i, exclude, k, from_c + 1);\n } else {\n assert(dp_spec(houses, cost, n, i, from_c, k) >= 1_000_001int);\n lemma_min_excluding_all_large(houses, cost, n, i, exclude, k, from_c + 1);\n }\n}\n\nimpl Solution {\n pub fn min_cost(\n houses: Vec,\n cost: Vec>,\n m: i32,\n n: i32,\n target: i32,\n ) -> (result: i32)\n requires\n m as int == houses@.len(),\n m as int == cost@.len(),\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n forall|i: int| 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len() == n as int,\n forall|i: int, j: int|\n 0 <= i < m as int && 0 <= j < n as int ==> 1 <= #[trigger] cost@[i]@[j]\n <= 10_000,\n ensures\n result as int == answer_spec(houses@, cost@, n as int, m as int, target as int),\n {\n let m_us = m as usize;\n let n_us = n as usize;\n let target_us = target as usize;\n let stride: usize = target_us + 1;\n\n assert((n_us as int + 1) * (stride as int) <= 21 * 101) by (nonlinear_arith)\n requires\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n n_us == n as usize,\n target_us == target as usize,\n ;\n\n let dp_size: usize = (n_us + 1) * stride;\n\n assert(dp_size <= 21 * 101) by (nonlinear_arith)\n requires\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n n_us == n as usize,\n target_us == target as usize,\n ;\n\n let mut prev_dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < dp_size\n invariant\n idx <= dp_size,\n prev_dp@.len() == idx as int,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n forall|ii: int| 0 <= ii < idx as int ==> prev_dp@[ii] == 1_000_001i32,\n decreases dp_size - idx,\n {\n prev_dp.push(1_000_001i32);\n idx += 1;\n }\n\n assert(prev_dp@.len() == (dp_size as int));\n\n proof {\n assert forall|jj: int, kk: int|\n 1 <= jj <= n as int && 0 <= kk <= target as int\n implies prev_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32\n by {\n assert(dp_idx(jj, kk, stride as int) >= 0) by (nonlinear_arith)\n requires 1 <= jj, 0 <= kk, stride as int >= 0;\n assert(dp_idx(jj, kk, stride as int) < dp_size as int) by (nonlinear_arith)\n requires\n 1 <= jj <= n as int,\n 0 <= kk <= target as int,\n stride as int == target as int + 1,\n dp_size as int == (n as int + 1) * stride as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n ;\n }\n assert forall|kk: int|\n 0 <= kk <= target as int\n implies prev_dp@[kk as int] == 1_000_001i32\n by {\n assert(0 <= kk);\n assert(kk < dp_size as int) by (nonlinear_arith)\n requires\n 0 <= kk <= target as int,\n stride as int == target as int + 1,\n dp_size as int == (n as int + 1) * stride as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n ;\n }\n }\n\n let mut j: usize = 1;\n while j <= n_us\n invariant\n 1 <= j <= n_us + 1,\n prev_dp@.len() == dp_size as int,\n m_us == m as usize,\n n_us == n as usize,\n target_us == target as usize,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n m as int == houses@.len(),\n m as int == cost@.len(),\n forall|i: int| 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len() == n as int,\n forall|i: int, jj: int|\n 0 <= i < m as int && 0 <= jj < n as int ==> 1\n <= #[trigger] cost@[i]@[jj] <= 10_000,\n forall|jj: int, kk: int|\n 1 <= jj < j as int && 0 <= kk <= target as int ==> prev_dp@[dp_idx(\n jj,\n kk,\n stride as int,\n )] as int == dp_spec(houses@, cost@, n as int, 0, jj, kk),\n forall|jj: int, kk: int|\n j as int <= jj <= n as int && 0 <= kk <= target as int\n ==> prev_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32,\n forall|kk: int|\n 0 <= kk <= target as int ==> prev_dp@[kk as int] == 1_000_001i32,\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] prev_dp@[ii] <= 1_000_001,\n decreases n_us + 1 - j,\n {\n assert((j as int) * (stride as int) + (target as int) < (dp_size as int))\n by (nonlinear_arith)\n requires\n 1 <= j as int <= n as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n target_us == target as usize,\n dp_size == (n_us + 1) * stride,\n n_us == n as usize,\n ;\n if houses[0] != 0 {\n if houses[0] as usize == j {\n prev_dp.set(j * stride + 1, 0i32);\n proof {\n lemma_dp_spec_base(houses@, cost@, n as int, j as int, 1);\n assert(dp_spec(houses@, cost@, n as int, 0, j as int, 1) == 0int);\n assert forall|kk: int|\n 0 <= kk <= target as int implies prev_dp@[dp_idx(\n j as int,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n 0,\n j as int,\n kk,\n ) by {\n if kk == 1 {\n } else {\n lemma_dp_spec_base(\n houses@,\n cost@,\n n as int,\n j as int,\n kk,\n );\n }\n }\n assert forall|jj: int, kk: int|\n 1 <= jj < j as int && 0 <= kk <= target as int\n implies prev_dp@[dp_idx(jj, kk, stride as int)] as int == dp_spec(houses@, cost@, n as int, 0, jj, kk)\n by {\n assert(jj * (stride as int) <= (j as int - 1) * (stride as int)) by (nonlinear_arith)\n requires jj <= j as int - 1, jj >= 1, stride as int >= 2;\n assert((j as int - 1) * (stride as int) + target as int == (j as int) * (stride as int) - 1) by (nonlinear_arith)\n requires stride as int == target as int + 1;\n assert(jj * (stride as int) + kk <= (j as int) * (stride as int) - 1);\n }\n assert forall|jj: int, kk: int|\n j as int + 1 <= jj && jj <= n as int && 0 <= kk <= target as int\n implies prev_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32\n by {\n assert(dp_idx(jj, kk, stride as int) == jj * (stride as int) + kk);\n assert(jj * (stride as int) >= (j as int + 1) * (stride as int)) by (nonlinear_arith)\n requires jj >= j as int + 1, stride as int >= 2;\n assert((j as int + 1) * (stride as int) >= (j as int) * (stride as int) + 2) by (nonlinear_arith)\n requires stride as int >= 2;\n assert(jj * (stride as int) + kk >= (j as int) * (stride as int) + 2);\n assert(dp_idx(jj, kk, stride as int) != (j as int) * (stride as int) + 1);\n assert(0 <= dp_idx(jj, kk, stride as int)) by (nonlinear_arith)\n requires 0 <= jj, 0 <= kk, stride as int >= 0;\n assert(dp_idx(jj, kk, stride as int) < dp_size as int) by (nonlinear_arith)\n requires 1 <= jj, jj <= n as int, 0 <= kk, kk <= target as int,\n stride as int == target as int + 1, dp_size as int == (n as int + 1) * (stride as int);\n }\n assert forall|kk: int|\n 0 <= kk <= target as int\n implies prev_dp@[kk] == 1_000_001i32\n by {\n assert(kk != (j as int) * (stride as int) + 1) by (nonlinear_arith)\n requires 0 <= kk <= target as int, stride as int == target as int + 1, 1 <= j as int;\n }\n }\n } else {\n proof {\n assert forall|kk: int|\n 0 <= kk <= target as int implies prev_dp@[dp_idx(\n j as int,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n 0,\n j as int,\n kk,\n ) by {\n lemma_dp_spec_base(houses@, cost@, n as int, j as int, kk);\n }\n }\n }\n } else {\n prev_dp.set(j * stride + 1, cost[0][j - 1]);\n proof {\n lemma_dp_spec_base(houses@, cost@, n as int, j as int, 1);\n assert(dp_spec(houses@, cost@, (n as int), 0, (j as int), 1) == (cost@[0]@[(j as int) - 1] as int));\n assert forall|kk: int|\n 0 <= kk <= target as int implies prev_dp@[dp_idx(\n j as int,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n 0,\n j as int,\n kk,\n ) by {\n if kk == 1 {\n } else {\n lemma_dp_spec_base(houses@, cost@, n as int, j as int, kk);\n }\n }\n assert forall|jj: int, kk: int|\n 1 <= jj < j as int && 0 <= kk <= target as int\n implies prev_dp@[dp_idx(jj, kk, stride as int)] as int == dp_spec(houses@, cost@, n as int, 0, jj, kk)\n by {\n assert(jj * (stride as int) <= (j as int - 1) * (stride as int)) by (nonlinear_arith)\n requires jj <= j as int - 1, jj >= 1, stride as int >= 2;\n assert((j as int - 1) * (stride as int) + target as int == (j as int) * (stride as int) - 1) by (nonlinear_arith)\n requires stride as int == target as int + 1;\n assert(jj * (stride as int) + kk <= (j as int) * (stride as int) - 1);\n }\n assert forall|jj: int, kk: int|\n j as int + 1 <= jj && jj <= n as int && 0 <= kk <= target as int\n implies prev_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32\n by {\n assert(dp_idx(jj, kk, stride as int) == jj * (stride as int) + kk);\n assert(jj * (stride as int) >= (j as int + 1) * (stride as int)) by (nonlinear_arith)\n requires jj >= j as int + 1, stride as int >= 2;\n assert((j as int + 1) * (stride as int) >= (j as int) * (stride as int) + 2) by (nonlinear_arith)\n requires stride as int >= 2;\n assert(jj * (stride as int) + kk >= (j as int) * (stride as int) + 2);\n assert(dp_idx(jj, kk, stride as int) != (j as int) * (stride as int) + 1);\n assert(0 <= dp_idx(jj, kk, stride as int)) by (nonlinear_arith)\n requires 0 <= jj, 0 <= kk, stride as int >= 0;\n assert(dp_idx(jj, kk, stride as int) < dp_size as int) by (nonlinear_arith)\n requires 1 <= jj, jj <= n as int, 0 <= kk, kk <= target as int,\n stride as int == target as int + 1, dp_size as int == (n as int + 1) * (stride as int);\n }\n assert forall|kk: int|\n 0 <= kk <= target as int\n implies prev_dp@[kk] == 1_000_001i32\n by {\n assert(kk != (j as int) * (stride as int) + 1) by (nonlinear_arith)\n requires 0 <= kk <= target as int, stride as int == target as int + 1, 1 <= j as int;\n }\n }\n }\n j += 1;\n }\n\n proof {\n assert forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int implies prev_dp@[dp_idx(\n jj,\n kk,\n stride as int,\n )] as int == dp_spec(houses@, cost@, n as int, 0, jj, kk) by {\n if jj == 0 {\n lemma_dp_spec_base(houses@, cost@, n as int, 0, kk);\n }\n }\n }\n\n let ghost mut house_idx: int = 0;\n\n let mut i: usize = 1;\n while i < m_us\n invariant\n 1 <= i <= m_us,\n house_idx == i as int - 1,\n prev_dp@.len() == dp_size as int,\n m_us == m as usize,\n n_us == n as usize,\n target_us == target as usize,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n m as int == houses@.len(),\n m as int == cost@.len(),\n forall|i: int| 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len() == n as int,\n forall|i: int, jj: int|\n 0 <= i < m as int && 0 <= jj < n as int ==> 1\n <= #[trigger] cost@[i]@[jj] <= 10_000,\n forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int ==> prev_dp@[dp_idx(\n jj,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n jj,\n kk,\n ),\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] prev_dp@[ii] <= 1_000_001,\n decreases m_us - i,\n {\n let mut curr_dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < dp_size\n invariant\n idx <= dp_size,\n curr_dp@.len() == idx as int,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n forall|ii: int| 0 <= ii < idx as int ==> curr_dp@[ii] == 1_000_001i32,\n decreases dp_size - idx,\n {\n curr_dp.push(1_000_001i32);\n idx += 1;\n }\n\n proof {\n assert forall|jj: int, kk: int|\n 1 <= jj <= n as int && 0 <= kk <= target as int\n implies curr_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32\n by {\n assert(dp_idx(jj, kk, stride as int) >= 0) by (nonlinear_arith)\n requires 1 <= jj, 0 <= kk, stride as int >= 0;\n assert(dp_idx(jj, kk, stride as int) < dp_size as int) by (nonlinear_arith)\n requires\n 1 <= jj <= n as int,\n 0 <= kk <= target as int,\n stride as int == target as int + 1,\n dp_size as int == (n as int + 1) * stride as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n ;\n }\n assert forall|kk: int|\n 0 <= kk <= target as int\n implies curr_dp@[kk as int] == 1_000_001i32\n by {\n assert(0 <= kk);\n assert(kk < dp_size as int) by (nonlinear_arith)\n requires\n 0 <= kk <= target as int,\n stride as int == target as int + 1,\n dp_size as int == (n as int + 1) * stride as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n ;\n }\n }\n\n let mut j: usize = 1;\n while j <= n_us\n invariant\n 1 <= j <= n_us + 1,\n curr_dp@.len() == dp_size as int,\n prev_dp@.len() == dp_size as int,\n m_us == m as usize,\n n_us == n as usize,\n target_us == target as usize,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= i < m_us,\n house_idx == i as int - 1,\n m as int == houses@.len(),\n m as int == cost@.len(),\n forall|i: int| 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len() == n as int,\n forall|i: int, jj: int|\n 0 <= i < m as int && 0 <= jj < n as int ==> 1\n <= #[trigger] cost@[i]@[jj] <= 10_000,\n forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int\n ==> prev_dp@[dp_idx(jj, kk, stride as int)] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n jj,\n kk,\n ),\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] prev_dp@[ii] <= 1_000_001,\n forall|jj: int, kk: int|\n 1 <= jj < j as int && 0 <= kk <= target as int\n ==> curr_dp@[dp_idx(jj, kk, stride as int)] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n jj,\n kk,\n ),\n forall|jj: int, kk: int|\n j as int <= jj <= n as int && 0 <= kk <= target as int\n ==> curr_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32,\n forall|kk: int|\n 0 <= kk <= target as int ==> curr_dp@[kk as int] == 1_000_001i32,\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] curr_dp@[ii] <= 1_000_001,\n decreases n_us + 1 - j,\n {\n assert((j as int) * (stride as int) + (target as int) < (dp_size as int))\n by (nonlinear_arith)\n requires\n 1 <= j as int <= n as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n target_us == target as usize,\n dp_size == (n_us + 1) * stride,\n n_us == n as usize,\n ;\n\n if houses[i] == 0 || houses[i] as usize == j {\n let paint_cost: i32 =\n if houses[i] != 0 { 0i32 } else { cost[i][j - 1] };\n\n proof {\n assert(0 <= paint_cost <= 10_000) by {\n if houses@[i as int] as int != 0 {\n } else {\n assert(cost@[i as int]@[j as int - 1] == paint_cost);\n }\n }\n }\n\n let mut k: usize = 1;\n while k <= target_us\n invariant\n 1 <= k <= target_us + 1,\n curr_dp@.len() == dp_size as int,\n prev_dp@.len() == dp_size as int,\n m_us == m as usize,\n n_us == n as usize,\n target_us == target as usize,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= i < m_us,\n 1 <= j as int <= n as int,\n house_idx == i as int - 1,\n 0 <= paint_cost <= 10_000,\n m as int == houses@.len(),\n m as int == cost@.len(),\n houses@[i as int] == 0 || houses@[i as int] as int == j as int,\n forall|i: int|\n 0 <= i < m as int ==> 0 <= #[trigger] houses@[i] <= n,\n forall|i: int|\n 0 <= i < m as int ==> (#[trigger] cost@[i])@.len()\n == n as int,\n forall|i: int, jj: int|\n 0 <= i < m as int && 0 <= jj < n as int ==> 1\n <= #[trigger] cost@[i]@[jj] <= 10_000,\n forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int\n ==> prev_dp@[dp_idx(jj, kk, stride as int)] as int\n == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n jj,\n kk,\n ),\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] prev_dp@[ii] <= 1_000_001,\n paint_cost as int == (if houses@[i as int] as int != 0 {\n 0int\n } else {\n cost@[i as int]@[j as int - 1] as int\n }),\n forall|kk: int|\n 1 <= kk < k as int ==> curr_dp@[dp_idx(\n j as int,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n j as int,\n kk,\n ),\n forall|kk: int|\n k as int <= kk <= target as int ==> curr_dp@[dp_idx(\n j as int,\n kk,\n stride as int,\n )] == 1_000_001i32,\n curr_dp@[dp_idx(j as int, 0, stride as int)]\n == 1_000_001i32,\n forall|jj: int, kk: int|\n 1 <= jj < j as int && 0 <= kk <= target as int\n ==> curr_dp@[dp_idx(jj, kk, stride as int)] as int\n == dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n jj,\n kk,\n ),\n forall|jj: int, kk: int|\n (j as int) < jj <= n as int && 0 <= kk <= target as int\n ==> curr_dp@[dp_idx(jj, kk, stride as int)]\n == 1_000_001i32,\n forall|kk: int|\n 0 <= kk <= target as int ==> curr_dp@[kk as int]\n == 1_000_001i32,\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] curr_dp@[ii]\n <= 1_000_001,\n decreases target_us + 1 - k,\n {\n assert((j as int) * (stride as int) + (k as int) < (dp_size as int))\n by (nonlinear_arith)\n requires\n 1 <= j as int <= n as int,\n 1 <= k as int <= target as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n target_us == target as usize,\n dp_size == (n_us + 1) * stride,\n n_us == n as usize,\n ;\n\n let same: i32 = prev_dp[j * stride + k];\n\n let mut diff: i32 = 1_000_001i32;\n let mut c: usize = 1;\n proof {\n lemma_min_excluding_bound(houses@, cost@, n as int, m as int, house_idx, j as int, k as int - 1, 1);\n }\n while c <= n_us\n invariant\n 1 <= c <= n_us + 1,\n prev_dp@.len() == dp_size as int,\n m_us == m as usize,\n n_us == n as usize,\n target_us == target as usize,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= i < m_us,\n 1 <= j as int <= n as int,\n 1 <= k as int <= target as int,\n house_idx == i as int - 1,\n forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int\n ==> prev_dp@[dp_idx(jj, kk, stride as int)]\n as int == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n jj,\n kk,\n ),\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] prev_dp@[ii]\n <= 1_000_001,\n 0 <= diff <= 1_000_001,\n spec_min(\n diff as int,\n min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int,\n ),\n ) == min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n 1,\n ),\n decreases n_us + 1 - c,\n {\n assert(c as int * stride as int + (k as int - 1) < dp_size\n as int) by (nonlinear_arith)\n requires\n 1 <= c as int <= n as int,\n 1 <= k as int <= target as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n target_us == target as usize,\n dp_size == (n_us + 1) * stride,\n n_us == n as usize,\n ;\n\n if c != j {\n let val: i32 = prev_dp[c * stride + (k - 1)];\n proof {\n assert(val as int == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n c as int,\n k as int - 1,\n ));\n let me_old = min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int,\n );\n let me_rest = min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int + 1,\n );\n assert(me_old == spec_min(val as int, me_rest));\n }\n if val < diff {\n proof {\n let me_rest = min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int + 1,\n );\n lemma_spec_min_assoc(\n diff as int,\n val as int,\n me_rest,\n );\n assert(spec_min(diff as int, spec_min(\n val as int,\n me_rest,\n )) == spec_min(\n spec_min(diff as int, val as int),\n me_rest,\n ));\n assert(spec_min((diff as int), (val as int))\n == (val as int));\n }\n diff = val;\n } else {\n proof {\n let me_rest = min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int + 1,\n );\n lemma_spec_min_assoc(\n diff as int,\n val as int,\n me_rest,\n );\n assert(spec_min((diff as int), (val as int))\n == (diff as int));\n }\n }\n } else {\n proof {\n assert(min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int,\n ) == min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n c as int + 1,\n ));\n }\n }\n c += 1;\n }\n\n proof {\n assert(min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n n as int + 1,\n ) == 1_000_001int);\n assert(diff as int == min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n 1,\n ));\n }\n\n let best: i32 = if same <= diff { same } else { diff };\n\n proof {\n assert(same as int == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int,\n ));\n assert(diff as int == min_excluding(\n houses@,\n cost@,\n n as int,\n house_idx,\n j as int,\n k as int - 1,\n 1,\n ));\n assert((best as int) == spec_min((same as int), (diff as int)));\n }\n\n if best < 1_000_001i32 {\n let total: i32 = paint_cost + best;\n if total < 1_000_001i32 {\n curr_dp.set(j * stride + k, total);\n proof {\n assert((total as int) == (paint_cost as int) + (best as int));\n assert(house_idx == i as int - 1);\n let same_spec = dp_spec(houses@, cost@, n as int, i as int - 1, j as int, k as int);\n let diff_spec = min_excluding(houses@, cost@, n as int, i as int - 1, j as int, k as int - 1, 1);\n assert(same as int == same_spec);\n assert(diff as int == diff_spec);\n assert(best as int == spec_min(same_spec, diff_spec));\n assert(spec_min(same_spec, diff_spec) < 1_000_001int);\n assert(paint_cost as int + spec_min(same_spec, diff_spec) < 1_000_001int);\n \n assert(k as int <= i as int + 1) by {\n if k as int > i as int + 1 {\n \n assert(dp_spec(houses@, cost@, n as int, i as int - 1, j as int, k as int) == 1_000_001int);\n assert(same_spec == 1_000_001int);\n assert forall|c: int| 1 <= c <= n as int && c != j as int\n implies #[trigger] dp_spec(houses@, cost@, n as int, i as int - 1, c, k as int - 1) >= 1_000_001int\n by {\n assert(k as int - 1 > i as int) by (nonlinear_arith)\n requires k as int > i as int + 1;\n assert(k as int - 1 > (i as int - 1) + 1) by (nonlinear_arith)\n requires k as int - 1 > i as int;\n };\n lemma_min_excluding_all_large(houses@, cost@, n as int, i as int - 1, j as int, k as int - 1, 1);\n assert(diff_spec >= 1_000_001int);\n lemma_min_excluding_bound(houses@, cost@, n as int, m as int, i as int - 1, j as int, k as int - 1, 1);\n assert(diff_spec == 1_000_001int);\n assert(spec_min(same_spec, diff_spec) == 1_000_001int);\n }\n };\n let dp_val = dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n j as int,\n k as int,\n );\n assert(dp_val == (total as int));\n \n assert forall|kk: int|\n k as int + 1 <= kk && kk <= target as int\n implies curr_dp@[dp_idx(j as int, kk, stride as int)] == 1_000_001i32\n by {\n assert(dp_idx(j as int, kk, stride as int) == (j as int) * (stride as int) + kk);\n assert(kk != k as int);\n assert((j as int) * (stride as int) + kk != (j as int) * (stride as int) + (k as int));\n assert(0 <= dp_idx(j as int, kk, stride as int)) by (nonlinear_arith)\n requires 0 <= j as int, 0 <= kk, stride as int >= 0;\n assert(dp_idx(j as int, kk, stride as int) < dp_size as int) by (nonlinear_arith)\n requires 1 <= j as int, j as int <= n as int, 0 <= kk, kk <= target as int,\n stride as int == target as int + 1, dp_size as int == (n as int + 1) * (stride as int);\n }\n \n assert forall|jj: int, kk: int|\n 1 <= jj < j as int && 0 <= kk <= target as int\n implies curr_dp@[dp_idx(jj, kk, stride as int)] as int == dp_spec(houses@, cost@, n as int, i as int, jj, kk)\n by {\n assert(jj * (stride as int) <= (j as int - 1) * (stride as int)) by (nonlinear_arith)\n requires jj <= j as int - 1, jj >= 1, stride as int >= 2;\n assert((j as int - 1) * (stride as int) + target as int == (j as int) * (stride as int) - 1) by (nonlinear_arith)\n requires stride as int == target as int + 1;\n assert(jj * (stride as int) + kk <= (j as int) * (stride as int) - 1);\n assert((j as int) * (stride as int) + (k as int) >= (j as int) * (stride as int) + 1);\n }\n \n assert forall|jj: int, kk: int|\n (j as int) < jj <= n as int && 0 <= kk <= target as int\n implies curr_dp@[dp_idx(jj, kk, stride as int)] == 1_000_001i32\n by {\n assert(dp_idx(jj, kk, stride as int) == jj * (stride as int) + kk);\n assert(jj * (stride as int) + kk >= (j as int) * (stride as int) + stride as int) by (nonlinear_arith)\n requires jj >= j as int + 1, stride as int >= 2, kk >= 0;\n assert(stride as int == target as int + 1);\n assert(k as int <= target as int);\n assert((j as int) * (stride as int) + stride as int > (j as int) * (stride as int) + (k as int));\n assert(dp_idx(jj, kk, stride as int) > (j as int) * (stride as int) + (k as int));\n assert(dp_idx(jj, kk, stride as int) != (j as int) * (stride as int) + (k as int));\n assert(0 <= dp_idx(jj, kk, stride as int)) by (nonlinear_arith)\n requires 0 <= jj, 0 <= kk, stride as int >= 0;\n assert(dp_idx(jj, kk, stride as int) < dp_size as int) by (nonlinear_arith)\n requires 1 <= jj, jj <= n as int, 0 <= kk, kk <= target as int,\n stride as int == target as int + 1, dp_size as int == (n as int + 1) * (stride as int);\n }\n \n assert forall|kk: int|\n 0 <= kk <= target as int\n implies curr_dp@[kk] == 1_000_001i32\n by {\n assert(0 <= kk);\n assert(kk < dp_size as int) by (nonlinear_arith)\n requires kk <= target as int, stride as int == target as int + 1,\n dp_size as int == (n as int + 1) * (stride as int), n as int >= 1;\n assert((j as int) * (stride as int) >= stride as int) by (nonlinear_arith)\n requires 1 <= j as int, stride as int >= 0;\n assert(stride as int == target as int + 1);\n assert(k as int >= 1);\n assert((j as int) * (stride as int) + (k as int) >= target as int + 2);\n assert(kk <= target as int);\n assert(kk != (j as int) * (stride as int) + (k as int));\n }\n \n assert(curr_dp@[dp_idx(j as int, 0, stride as int)] == 1_000_001i32) by {\n assert((j as int) * (stride as int) + 0 != (j as int) * (stride as int) + (k as int));\n }\n }\n } else {\n proof {\n let dp_val = dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n j as int,\n k as int,\n );\n assert(dp_val == 1_000_001int);\n }\n }\n } else {\n proof {\n let dp_val = dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n j as int,\n k as int,\n );\n assert(dp_val == 1_000_001int);\n }\n }\n\n k += 1;\n }\n\n } else {\n proof {\n assert forall|kk: int|\n 0 <= kk <= target as int implies curr_dp@[dp_idx(\n j as int,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n j as int,\n kk,\n ) by {\n assert((houses@[(i as int)] as int) != 0);\n assert((houses@[(i as int)] as int) != (j as int));\n assert(dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n j as int,\n kk,\n ) == 1_000_001int);\n }\n }\n }\n j += 1;\n }\n\n proof {\n assert forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int\n implies curr_dp@[dp_idx(jj, kk, stride as int)] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n i as int,\n jj,\n kk,\n ) by {\n if jj == 0 {\n assert(dp_idx(0, kk, stride as int) == kk);\n assert(dp_spec(houses@, cost@, n as int, i as int, 0, kk)\n == 1_000_001int);\n }\n }\n }\n\n prev_dp = curr_dp;\n proof {\n house_idx = house_idx + 1;\n }\n i += 1;\n }\n\n proof {\n assert(house_idx == m as int - 1);\n }\n\n let mut ans: i32 = 1_000_001i32;\n let mut j: usize = 1;\n while j <= n_us\n invariant\n 1 <= j <= n_us + 1,\n prev_dp@.len() == dp_size as int,\n m_us == m as usize,\n n_us == n as usize,\n target_us == target as usize,\n stride == target_us + 1,\n dp_size == (n_us + 1) * stride,\n dp_size <= 21 * 101,\n 1 <= m <= 100,\n 1 <= n <= 20,\n 1 <= target <= m,\n house_idx == m as int - 1,\n forall|jj: int, kk: int|\n 0 <= jj <= n as int && 0 <= kk <= target as int ==> prev_dp@[dp_idx(\n jj,\n kk,\n stride as int,\n )] as int == dp_spec(\n houses@,\n cost@,\n n as int,\n house_idx,\n jj,\n kk,\n ),\n 0 <= ans <= 1_000_001,\n forall|ii: int|\n 0 <= ii < dp_size as int ==> 0 <= #[trigger] prev_dp@[ii] <= 1_000_001,\n ans as int == min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int,\n ) ==> ans as int == min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int,\n ),\n spec_min(\n ans as int,\n min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int,\n ),\n ) == min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n 1,\n ),\n decreases n_us + 1 - j,\n {\n assert((j as int) * (stride as int) + (target as int) < (dp_size as int))\n by (nonlinear_arith)\n requires\n 1 <= j as int <= n as int,\n 1 <= n <= 20,\n 1 <= target <= m,\n 1 <= m <= 100,\n stride == target_us + 1,\n target_us == target as usize,\n dp_size == (n_us + 1) * stride,\n n_us == n as usize,\n ;\n\n let val: i32 = prev_dp[j * stride + target_us];\n\n proof {\n assert(val as int == dp_spec(\n houses@,\n cost@,\n n as int,\n m as int - 1,\n j as int,\n target as int,\n ));\n let mf_cur = min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int,\n );\n let mf_rest = min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int + 1,\n );\n assert(mf_cur == spec_min(val as int, mf_rest));\n }\n\n if val < ans {\n proof {\n let mf_rest = min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int + 1,\n );\n lemma_spec_min_assoc(ans as int, val as int, mf_rest);\n assert(spec_min((ans as int), (val as int)) == (val as int));\n }\n ans = val;\n } else {\n proof {\n let mf_rest = min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n j as int + 1,\n );\n lemma_spec_min_assoc(ans as int, val as int, mf_rest);\n assert(spec_min((ans as int), (val as int)) == (ans as int));\n }\n }\n j += 1;\n }\n\n proof {\n assert(min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n n as int + 1,\n ) == 1_000_001int);\n assert(ans as int == min_final(\n houses@,\n cost@,\n n as int,\n m as int,\n target as int,\n 1,\n ));\n if ans >= 1_000_001i32 {\n assert(answer_spec(houses@, cost@, (n as int), (m as int), (target as int))\n == -1int);\n } else {\n assert(answer_spec(houses@, cost@, (n as int), (m as int), (target as int))\n == (ans as int));\n }\n }\n\n if ans >= 1_000_001i32 { -1i32 } else { ans }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1473/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1473/tests/mutated_testcases.jsonl"} {"problem": "lc1480", "family": "leetcode", "description": "# Running Sum of 1d Array\n\nGiven an array `nums`, we define a running sum of an array as `runningSum[i] = sum(nums[0]...nums[i])`.\n\nReturn the running sum of `nums`.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,4]\n> **Output:** [1,3,6,10]\n> **Explanation:** Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].\n\n## Example 2:\n\n> **Input:** nums = [1,1,1,1,1]\n> **Output:** [1,2,3,4,5]\n> **Explanation:** Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].\n\n## Example 3:\n\n> **Input:** nums = [3,1,2,10,1]\n> **Output:** [3,4,6,16,17]\n\n## Constraints:\n\n- $1 <= nums.length <= 1000$\n\n- $-10^6 <= nums[i] <= 10^6$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn running_sum(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Prefix Sum\nAcceptance rate: 86.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_sum(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::spec_sum(s, end - 1) + s[end - 1] as int\n }\n }\n\n pub fn running_sum(nums: Vec) -> (result: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> -1_000_000 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() as int ==> result[i] == Self::spec_sum(nums@, i + 1),\n {\n \n }\n}\n\n}\n", "code": "impl Solution {\n pub fn running_sum(nums: Vec) -> Vec {\n let mut result = nums;\n let n = result.len();\n let mut i: usize = 1;\n while i < n {\n result[i] = result[i] + result[i - 1];\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_sum(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::spec_sum(s, end - 1) + s[end - 1] as int\n }\n }\n\n pub fn running_sum(nums: Vec) -> (result: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> -1_000_000 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() as int ==> result[i] == Self::spec_sum(nums@, i + 1),\n {\n let mut result = nums;\n let n = result.len();\n let mut i: usize = 1;\n while i < n\n {\n result.set(i, result[i] + result[i - 1]);\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_sum(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::spec_sum(s, end - 1) + s[end - 1] as int\n }\n }\n\n proof fn spec_sum_bound(s: Seq, end: int)\n requires\n 0 <= end <= s.len(),\n s.len() <= 1000,\n forall |i: int| 0 <= i < s.len() ==> -1_000_000 <= #[trigger] s[i] <= 1_000_000,\n ensures\n -end * 1_000_000 <= Self::spec_sum(s, end) <= end * 1_000_000,\n decreases end,\n {\n if end > 0 {\n Self::spec_sum_bound(s, end - 1);\n }\n }\n\n pub fn running_sum(nums: Vec) -> (result: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> -1_000_000 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() as int ==> result[i] == Self::spec_sum(nums@, i + 1),\n {\n let ghost orig = nums@;\n let mut result = nums;\n let n = result.len();\n proof {\n assert(result@ =~= orig);\n assert(Self::spec_sum(orig, 0int) == 0);\n }\n let mut i: usize = 1;\n while i < n\n invariant\n n == result.len(),\n n == orig.len(),\n 1 <= n <= 1000,\n 1 <= i <= n,\n forall |k: int| 0 <= k < orig.len() ==> -1_000_000 <= #[trigger] orig[k] <= 1_000_000,\n forall |j: int| 0 <= j < i as int ==> result[j] == Self::spec_sum(orig, j + 1),\n forall |j: int| #![auto] i as int <= j < n as int ==> result[j] == orig[j],\n decreases n - i,\n {\n proof {\n Self::spec_sum_bound(orig, i as int);\n assert(i as int * 1_000_000 <= 1_000_000_000) by(nonlinear_arith)\n requires i as int <= 1000;\n assert(-1_000_000_000 <= Self::spec_sum(orig, i as int) <= 1_000_000_000);\n assert(result[i as int] == orig[i as int]);\n assert(-1_000_000 <= orig[i as int] <= 1_000_000);\n assert(-1_000_000 <= result[i as int] <= 1_000_000);\n assert(-1_000_000_000 <= result[(i - 1) as int] <= 1_000_000_000);\n }\n result.set(i, result[i] + result[i - 1]);\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1480/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1480/tests/mutated_testcases.jsonl"} {"problem": "lc1493", "family": "leetcode", "description": "# Longest Subarray of 1's After Deleting One Element\n\nGiven a binary array `nums`, you should delete one element from it.\n\nReturn the size of the longest non-empty subarray containing only `1`'s in the resulting array. Return `0` if there is no such subarray.\n\n## Example 1:\n\n> **Input:** nums = [1,1,0,1]\n> **Output:** 3\n> **Explanation:** After deleting the number at position 2, [1,1,1] contains 3 numbers with value 1.\n\n## Example 2:\n\n> **Input:** nums = [0,1,1,1,0,1,1,0,1]\n> **Output:** 5\n> **Explanation:** After deleting the number at position 4, [0,1,1,1,1,1,0,1] has a longest subarray of 1's equal to [1,1,1,1,1].\n\n## Example 3:\n\n> **Input:** nums = [1,1,1]\n> **Output:** 2\n> **Explanation:** You must delete one element.\n\n## Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `nums[i]` is either `0` or `1`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_subarray(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nStaff, Array, Dynamic Programming, Sliding Window, Biweekly Contest 29\nAcceptance rate: 71.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_zeros(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n (if s[l] == 0 { 1int } else { 0int }) + Self::count_zeros(s, l + 1, r)\n }\n }\n\n pub fn longest_subarray(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> nums[i] == 0 || nums[i] == 1,\n ensures\n 0 <= result,\n (result as int) < (nums.len() as int),\n result == 0 || exists|l: int, r: int|\n 0 <= l && l < r && r <= nums.len() as int\n && r == l + result as int + 1\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1,\n forall|l: int, r: int|\n 0 <= l && l < r && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1 ==> r - l - 1\n <= result as int,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn longest_subarray(nums: Vec) -> i32 {\n let n = nums.len();\n let mut left: usize = 0;\n let mut zeros: i32 = 0;\n let mut result: i32 = 0;\n let mut right: usize = 0;\n\n while right < n {\n if nums[right] == 0 {\n zeros = zeros + 1;\n }\n right = right + 1;\n\n while zeros > 1 {\n if nums[left] == 0 {\n zeros = zeros - 1;\n }\n left = left + 1;\n }\n\n let window = if right > left {\n (right - left) as i32 - 1\n } else {\n 0\n };\n if window > result {\n result = window;\n }\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_zeros(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n (if s[l] == 0 { 1int } else { 0int }) + Self::count_zeros(s, l + 1, r)\n }\n }\n\n pub fn longest_subarray(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> nums[i] == 0 || nums[i] == 1,\n ensures\n 0 <= result,\n (result as int) < (nums.len() as int),\n result == 0 || exists|l: int, r: int|\n 0 <= l && l < r && r <= nums.len() as int\n && r == l + result as int + 1\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1,\n forall|l: int, r: int|\n 0 <= l && l < r && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1 ==> r - l - 1\n <= result as int,\n {\n let n = nums.len();\n let mut left: usize = 0;\n let mut zeros: i32 = 0;\n let mut result: i32 = 0;\n let mut right: usize = 0;\n\n while right < n {\n if nums[right] == 0 {\n zeros = zeros + 1;\n }\n right = right + 1;\n\n while zeros > 1 {\n if nums[left] == 0 {\n zeros = zeros - 1;\n }\n left = left + 1;\n }\n\n let window = if right > left {\n (right - left) as i32 - 1\n } else {\n 0\n };\n if window > result {\n result = window;\n }\n }\n\n result\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_zeros(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n (if s[l] == 0 { 1int } else { 0int }) + Self::count_zeros(s, l + 1, r)\n }\n }\n\n proof fn lemma_count_zeros_split(s: Seq, i: int, j: int, k: int)\n requires\n 0 <= i <= j <= k <= s.len(),\n ensures\n Self::count_zeros(s, i, k) == Self::count_zeros(s, i, j) + Self::count_zeros(s, j, k),\n decreases j - i,\n {\n if i < j {\n Self::lemma_count_zeros_split(s, i + 1, j, k);\n }\n }\n\n proof fn lemma_count_zeros_nonneg(s: Seq, i: int, j: int)\n requires\n 0 <= i <= j <= s.len(),\n ensures\n Self::count_zeros(s, i, j) >= 0,\n decreases j - i,\n {\n if i < j {\n Self::lemma_count_zeros_nonneg(s, i + 1, j);\n }\n }\n\n proof fn lemma_count_zeros_mono_left(s: Seq, l1: int, l2: int, r: int)\n requires\n 0 <= l1 <= l2 <= r <= s.len(),\n ensures\n Self::count_zeros(s, l1, r) >= Self::count_zeros(s, l2, r),\n decreases l2 - l1,\n {\n if l1 < l2 {\n Self::lemma_count_zeros_split(s, l1, l2, r);\n Self::lemma_count_zeros_nonneg(s, l1, l2);\n }\n }\n\n pub fn longest_subarray(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> nums[i] == 0 || nums[i] == 1,\n ensures\n 0 <= result,\n (result as int) < (nums.len() as int),\n result == 0 || exists|l: int, r: int|\n 0 <= l && l < r && r <= nums.len() as int\n && r == l + result as int + 1\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1,\n forall|l: int, r: int|\n 0 <= l && l < r && r <= nums.len() as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1 ==> r - l - 1\n <= result as int,\n {\n let n = nums.len();\n let mut left: usize = 0;\n let mut zeros: i32 = 0;\n let mut result: i32 = 0;\n let ghost mut result_left: int = 0;\n let mut right: usize = 0;\n\n while right < n\n invariant\n 0 <= left <= right <= n,\n n == nums.len(),\n 1 <= n <= 100_000,\n forall|i: int| 0 <= i < n ==> nums@[i] == 0 || nums@[i] == 1,\n zeros == Self::count_zeros(nums@, left as int, right as int),\n zeros >= 0,\n zeros <= 1,\n 0 <= result,\n (result as int) < (n as int),\n result as int >= right as int - left as int - 1,\n result == 0 || (\n 0 <= result_left && result_left + result as int + 1 <= right as int\n && Self::count_zeros(nums@, result_left, result_left + result as int + 1)\n <= 1\n ),\n forall|l: int, r: int|\n 0 <= l && l < r && r <= right as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1 ==> r - l - 1\n <= result as int,\n left == 0 || Self::count_zeros(nums@, left as int - 1, right as int) > 1,\n decreases n - right,\n {\n proof {\n reveal_with_fuel(Solution::count_zeros, 2);\n Self::lemma_count_zeros_split(nums@, left as int, right as int, right as int + 1);\n if left > 0 {\n Self::lemma_count_zeros_split(\n nums@,\n left as int - 1,\n right as int,\n right as int + 1,\n );\n Self::lemma_count_zeros_nonneg(nums@, right as int, right as int + 1);\n }\n }\n if nums[right] == 0 {\n zeros = zeros + 1;\n }\n right = right + 1;\n\n while zeros > 1\n invariant\n 0 <= left <= right <= n,\n n == nums.len(),\n 1 <= n <= 100_000,\n forall|i: int| 0 <= i < n ==> nums@[i] == 0 || nums@[i] == 1,\n zeros == Self::count_zeros(nums@, left as int, right as int),\n zeros >= 0,\n 0 <= result,\n (result as int) < (n as int),\n result == 0 || (\n 0 <= result_left && result_left + result as int + 1 < right as int\n && Self::count_zeros(\n nums@,\n result_left,\n result_left + result as int + 1,\n ) <= 1\n ),\n forall|l: int, r: int|\n 0 <= l && l < r && r < right as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1 ==> r - l - 1\n <= result as int,\n left == 0 || Self::count_zeros(nums@, left as int - 1, right as int) > 1,\n decreases right - left,\n {\n proof {\n reveal_with_fuel(Solution::count_zeros, 2);\n Self::lemma_count_zeros_split(\n nums@,\n left as int,\n left as int + 1,\n right as int,\n );\n }\n let ghost old_left: int = left as int;\n let ghost old_zeros: int = zeros as int;\n if nums[left] == 0 {\n zeros = zeros - 1;\n }\n left = left + 1;\n proof {\n assert(Self::count_zeros(nums@, old_left, right as int) == old_zeros);\n assert(Self::count_zeros(nums@, left as int - 1, right as int) > 1);\n }\n }\n\n let window = if right > left {\n (right - left) as i32 - 1\n } else {\n 0\n };\n proof {\n if right > left {\n assert(window as int == right as int - left as int - 1);\n } else {\n assert(window == 0);\n }\n }\n if window > result {\n proof {\n result_left = left as int;\n }\n result = window;\n }\n\n proof {\n assert forall|l: int, r: int|\n 0 <= l && l < r && r <= right as int\n && #[trigger] Self::count_zeros(nums@, l, r) <= 1 implies r - l - 1\n <= result as int\n by {\n if r < right as int {\n } else {\n if l >= left as int {\n assert((left as int) < (right as int));\n assert(r - l - 1 <= right as int - left as int - 1);\n assert(right as int - left as int - 1 <= result as int);\n } else {\n if left > 0 {\n Self::lemma_count_zeros_mono_left(\n nums@,\n l,\n left as int - 1,\n right as int,\n );\n }\n }\n }\n };\n }\n }\n\n result\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1493/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1493/tests/mutated_testcases.jsonl"} {"problem": "lc1503", "family": "leetcode", "description": "# Last Moment Before All Ants Fall Out of a Plank\n\nWe have a wooden plank of the length `n` **units**. Some ants are walking on the plank, each ant moves with a speed of **1 unit per second**. Some of the ants move to the **left**, the other move to the **right**.\n\nWhen two ants moving in two **different** directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.\n\nWhen an ant reaches **one end** of the plank at a time `t`, it falls out of the plank immediately.\n\nGiven an integer `n` and two integer arrays `left` and `right`, the positions of the ants moving to the left and the right, return *the moment when the last ant(s) fall out of the plank*.\n\n## Example 1:\n\n> **Input:** n = 4, left = [4,3], right = [0,1]\n> **Output:** 4\n> **Explanation:** The ant at index 0 is named A and going to the right. The ant at index 1 is named B and going to the right. The ant at index 3 is named C and going to the left. The ant at index 4 is named D and going to the left. The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank).\n\n## Example 2:\n\n> **Input:** n = 7, left = [], right = [0,1,2,3,4,5,6,7]\n> **Output:** 7\n> **Explanation:** All ants are going to the right, the ant at index 0 needs 7 seconds to fall.\n\n## Example 3:\n\n> **Input:** n = 7, left = [0,1,2,3,4,5,6,7], right = []\n> **Output:** 7\n> **Explanation:** All ants are going to the left, the ant at index 7 needs 7 seconds to fall.\n\n## Constraints:\n\n- $1 \\leq n \\leq 10^4$\n- $0 \\leq \\text{left.length} \\leq n + 1$\n- $0 \\leq \\text{left}[i] \\leq n$\n- $0 \\leq \\text{right.length} \\leq n + 1$\n- $0 \\leq \\text{right}[i] \\leq n$\n- $1 \\leq \\text{left.length} + \\text{right.length} \\leq n + 1$\n- All values of `left` and `right` are unique, and each value can appear **only in one** of the two arrays.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn get_last_moment(n: i32, left: Vec, right: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Brainteaser, Simulation\nAcceptance rate: 68.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn get_last_moment(n: i32, left: Vec, right: Vec) -> (res: i32)\n requires\n 1 <= n <= 10_000,\n left.len() + right.len() >= 1,\n left.len() + right.len() <= n + 1,\n forall |i: int| 0 <= i < left.len() ==> 0 <= #[trigger] left[i] <= n,\n forall |i: int| 0 <= i < right.len() ==> 0 <= #[trigger] right[i] <= n,\n forall |i: int, j: int| 0 <= i < j < left.len() ==> left[i] != left[j],\n forall |i: int, j: int| 0 <= i < j < right.len() ==> right[i] != right[j],\n forall |i: int, j: int| 0 <= i < left.len() && 0 <= j < right.len() ==> left[i] != right[j],\n ensures\n 0 <= res <= n,\n forall |i: int| 0 <= i < left.len() ==> res >= #[trigger] left[i],\n forall |i: int| 0 <= i < right.len() ==> res >= n - #[trigger] right[i],\n (exists |k: int| 0 <= k < left.len() && res == left[k])\n || (exists |k: int| 0 <= k < right.len() && res == n - right[k]),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn get_last_moment(n: i32, left: Vec, right: Vec) -> i32 {\n let mut max_left: i32 = 0;\n let mut i: usize = 0;\n while i < left.len() {\n if left[i] >= max_left {\n max_left = left[i];\n }\n i = i + 1;\n }\n let mut min_right: i32 = n;\n let mut j: usize = 0;\n while j < right.len() {\n if right[j] <= min_right {\n min_right = right[j];\n }\n j = j + 1;\n }\n if max_left >= n - min_right {\n max_left\n } else {\n n - min_right\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn get_last_moment(n: i32, left: Vec, right: Vec) -> (res: i32)\n requires\n 1 <= n <= 10_000,\n left.len() + right.len() >= 1,\n left.len() + right.len() <= n + 1,\n forall |i: int| 0 <= i < left.len() ==> 0 <= #[trigger] left[i] <= n,\n forall |i: int| 0 <= i < right.len() ==> 0 <= #[trigger] right[i] <= n,\n forall |i: int, j: int| 0 <= i < j < left.len() ==> left[i] != left[j],\n forall |i: int, j: int| 0 <= i < j < right.len() ==> right[i] != right[j],\n forall |i: int, j: int| 0 <= i < left.len() && 0 <= j < right.len() ==> left[i] != right[j],\n ensures\n 0 <= res <= n,\n forall |i: int| 0 <= i < left.len() ==> res >= #[trigger] left[i],\n forall |i: int| 0 <= i < right.len() ==> res >= n - #[trigger] right[i],\n (exists |k: int| 0 <= k < left.len() && res == left[k])\n || (exists |k: int| 0 <= k < right.len() && res == n - right[k]),\n {\n let mut max_left: i32 = 0;\n let mut i: usize = 0;\n while i < left.len() {\n if left[i] >= max_left {\n max_left = left[i];\n }\n i = i + 1;\n }\n let mut min_right: i32 = n;\n let mut j: usize = 0;\n while j < right.len() {\n if right[j] <= min_right {\n min_right = right[j];\n }\n j = j + 1;\n }\n if max_left >= n - min_right {\n max_left\n } else {\n n - min_right\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn get_last_moment(n: i32, left: Vec, right: Vec) -> (res: i32)\n requires\n 1 <= n <= 10_000,\n left.len() + right.len() >= 1,\n left.len() + right.len() <= n + 1,\n forall |i: int| 0 <= i < left.len() ==> 0 <= #[trigger] left[i] <= n,\n forall |i: int| 0 <= i < right.len() ==> 0 <= #[trigger] right[i] <= n,\n forall |i: int, j: int| 0 <= i < j < left.len() ==> left[i] != left[j],\n forall |i: int, j: int| 0 <= i < j < right.len() ==> right[i] != right[j],\n forall |i: int, j: int| 0 <= i < left.len() && 0 <= j < right.len() ==> left[i] != right[j],\n ensures\n 0 <= res <= n,\n forall |i: int| 0 <= i < left.len() ==> res >= #[trigger] left[i],\n forall |i: int| 0 <= i < right.len() ==> res >= n - #[trigger] right[i],\n (exists |k: int| 0 <= k < left.len() && res == left[k])\n || (exists |k: int| 0 <= k < right.len() && res == n - right[k]),\n {\n let mut max_left: i32 = 0;\n let ghost mut ml_idx: int = -1;\n let mut i: usize = 0;\n\n while i < left.len()\n invariant\n 0 <= i <= left.len(),\n 0 <= max_left <= n,\n 1 <= n <= 10_000,\n forall |k: int| 0 <= k < left.len() ==> 0 <= #[trigger] left[k] <= n,\n forall |k: int| 0 <= k < i ==> max_left >= left[k],\n i == 0 ==> (max_left == 0 && ml_idx == -1),\n i > 0 ==> (0 <= ml_idx < i && max_left == left[ml_idx]),\n decreases left.len() - i,\n {\n if left[i] >= max_left {\n max_left = left[i];\n proof { ml_idx = i as int; }\n }\n i = i + 1;\n }\n\n let mut min_right: i32 = n;\n let ghost mut mr_idx: int = -1;\n let mut j: usize = 0;\n\n while j < right.len()\n invariant\n 0 <= j <= right.len(),\n 0 <= min_right <= n,\n 1 <= n <= 10_000,\n forall |k: int| 0 <= k < right.len() ==> 0 <= #[trigger] right[k] <= n,\n forall |k: int| 0 <= k < j ==> min_right <= right[k],\n j == 0 ==> (min_right == n && mr_idx == -1),\n j > 0 ==> (0 <= mr_idx < j && min_right == right[mr_idx]),\n decreases right.len() - j,\n {\n if right[j] <= min_right {\n min_right = right[j];\n proof { mr_idx = j as int; }\n }\n j = j + 1;\n }\n\n if max_left >= n - min_right {\n proof {\n assert forall |k: int| 0 <= k < right.len() implies max_left >= n - #[trigger] right[k] by {\n assert(min_right <= right[k]);\n }\n if left.len() > 0 {\n assert(0 <= ml_idx < left.len() && max_left == left[ml_idx]);\n } else {\n assert(right.len() > 0);\n assert(max_left == 0);\n assert(min_right == n);\n assert(0 <= mr_idx < right.len());\n assert(max_left == n - right[mr_idx]);\n }\n }\n max_left\n } else {\n proof {\n assert forall |k: int| 0 <= k < left.len() implies n - min_right >= #[trigger] left[k] by {\n assert(max_left >= left[k]);\n }\n assert(right.len() > 0);\n assert(0 <= mr_idx < right.len());\n assert(n - min_right == n - right[mr_idx]);\n }\n n - min_right\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1503/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1503/tests/mutated_testcases.jsonl"} {"problem": "lc1509", "family": "leetcode", "description": "# Minimum Difference Between Largest and Smallest Value in Three Moves\n\nYou are given an integer array `nums`.\n\nIn one move, you can choose one element of `nums` and change it to **any value**.\n\nReturn *the minimum difference between the largest and smallest value of `nums` after performing at most three moves*.\n\n## Example 1:\n\n> **Input:** nums = [5,3,2,4]\n> **Output:** 0\n> **Explanation:** We can make at most 3 moves.\n> In the first move, change 2 to 3. nums becomes [5,3,3,4].\n> In the second move, change 4 to 3. nums becomes [5,3,3,3].\n> In the third move, change 5 to 3. nums becomes [3,3,3,3].\n> After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.\n\n## Example 2:\n\n> **Input:** nums = [1,5,0,10,14]\n> **Output:** 1\n> **Explanation:** We can make at most 3 moves.\n> In the first move, change 5 to 0. nums becomes [1,0,0,10,14].\n> In the second move, change 10 to 0. nums becomes [1,0,0,0,14].\n> In the third move, change 14 to 1. nums becomes [1,0,0,0,1].\n> After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.\n> It can be shown that there is no way to make the difference 0 in 3 moves.\n\n## Example 3:\n\n> **Input:** nums = [3,100,20]\n> **Output:** 0\n> **Explanation:** We can make at most 3 moves.\n> In the first move, change 100 to 7. nums becomes [3,7,20].\n> In the second move, change 20 to 7. nums becomes [3,7,7].\n> In the third move, change 3 to 7. nums becomes [7,7,7].\n> After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.\n\n## Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_difference(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Greedy, Sorting\nAcceptance rate: 59.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn num_changes(a: Seq, b: Seq) -> int\n decreases a.len(),\n {\n if a.len() == 0 || a.len() != b.len() { 0 }\n else {\n (if a.last() != b.last() { 1int } else { 0int })\n + Self::num_changes(a.drop_last(), b.drop_last())\n }\n }\n\n pub open spec fn seq_max(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() <= 0 { i32::MIN as int }\n else if s.len() == 1 { s[0] as int }\n else {\n let rest = Self::seq_max(s.drop_last());\n if s.last() as int >= rest { s.last() as int } else { rest }\n }\n }\n\n pub open spec fn seq_min(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() <= 0 { i32::MAX as int }\n else if s.len() == 1 { s[0] as int }\n else {\n let rest = Self::seq_min(s.drop_last());\n if (s.last() as int) <= rest { s.last() as int } else { rest }\n }\n }\n\n pub fn min_difference(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n nums.len() <= 4 ==> res == 0,\n nums.len() > 4 ==> (\n exists |m: Seq|\n m.len() == nums.len()\n && Self::num_changes(nums@, m) <= 3\n && res as int == Self::seq_max(m) - Self::seq_min(m)\n && forall |m2: Seq|\n m2.len() == nums.len()\n && Self::num_changes(nums@, m2) <= 3\n ==> Self::seq_max(m2) - Self::seq_min(m2) >= res as int\n ),\n {\n }\n}\n\n}", "code": "impl Solution {\n fn ms_merge(a: &Vec, b: &Vec) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort(input: &Vec) -> Vec {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n {\n right.push(input[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n result\n }\n\n pub fn min_difference(nums: Vec) -> i32 {\n let n = nums.len();\n if n <= 4 { return 0; }\n\n let nums = Self::ms_sort(&nums);\n\n let d1 = nums[n - 1] - nums[3];\n let d2 = nums[n - 2] - nums[2];\n let d3 = nums[n - 3] - nums[1];\n let d4 = nums[n - 4] - nums[0];\n let mut result = d1;\n if d2 < result {\n result = d2;\n }\n if d3 < result {\n result = d3;\n }\n if d4 < result {\n result = d4;\n }\n result\n }\n}", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn num_changes(a: Seq, b: Seq) -> int\n decreases a.len(),\n {\n if a.len() == 0 || a.len() != b.len() { 0 }\n else {\n (if a.last() != b.last() { 1int } else { 0int })\n + Self::num_changes(a.drop_last(), b.drop_last())\n }\n }\n\n pub open spec fn seq_max(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() <= 0 { i32::MIN as int }\n else if s.len() == 1 { s[0] as int }\n else {\n let rest = Self::seq_max(s.drop_last());\n if s.last() as int >= rest { s.last() as int } else { rest }\n }\n }\n\n pub open spec fn seq_min(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() <= 0 { i32::MAX as int }\n else if s.len() == 1 { s[0] as int }\n else {\n let rest = Self::seq_min(s.drop_last());\n if (s.last() as int) <= rest { s.last() as int } else { rest }\n }\n }\n\n pub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_permutation(a: Seq, b: Seq) -> bool {\n forall |v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n fn ms_merge(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::is_sorted(a@), Self::is_sorted(b@)\n ensures\n Self::is_sorted(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_permutation(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort(input: &Vec) -> (result: Vec)\n ensures\n Self::is_sorted(result@),\n result@.len() == input@.len(),\n Self::is_permutation(result@, input@),\n decreases input.len(),\n {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n {\n right.push(input[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n result\n }\n\n pub fn min_difference(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n nums.len() <= 4 ==> res == 0,\n nums.len() > 4 ==> (\n exists |m: Seq|\n m.len() == nums.len()\n && Self::num_changes(nums@, m) <= 3\n && res as int == Self::seq_max(m) - Self::seq_min(m)\n && forall |m2: Seq|\n m2.len() == nums.len()\n && Self::num_changes(nums@, m2) <= 3\n ==> Self::seq_max(m2) - Self::seq_min(m2) >= res as int\n ),\n {\n let n = nums.len();\n if n <= 4 { return 0; }\n\n let nums = Self::ms_sort(&nums);\n\n let d1 = nums[n - 1] - nums[3];\n let d2 = nums[n - 2] - nums[2];\n let d3 = nums[n - 3] - nums[1];\n let d4 = nums[n - 4] - nums[0];\n let mut result = d1;\n if d2 < result {\n result = d2;\n }\n if d3 < result {\n result = d3;\n }\n if d4 < result {\n result = d4;\n }\n result\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn num_changes(a: Seq, b: Seq) -> int\n decreases a.len(),\n {\n if a.len() == 0 || a.len() != b.len() { 0 }\n else {\n (if a.last() != b.last() { 1int } else { 0int })\n + Self::num_changes(a.drop_last(), b.drop_last())\n }\n }\n\n pub open spec fn seq_max(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() <= 0 { i32::MIN as int }\n else if s.len() == 1 { s[0] as int }\n else {\n let rest = Self::seq_max(s.drop_last());\n if s.last() as int >= rest { s.last() as int } else { rest }\n }\n }\n\n pub open spec fn seq_min(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() <= 0 { i32::MAX as int }\n else if s.len() == 1 { s[0] as int }\n else {\n let rest = Self::seq_min(s.drop_last());\n if (s.last() as int) <= rest { s.last() as int } else { rest }\n }\n }\n\n pub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn min4(a: int, b: int, c: int, d: int) -> int {\n let m1 = if a <= b { a } else { b };\n let m2 = if c <= d { c } else { d };\n if m1 <= m2 { m1 } else { m2 }\n }\n\n proof fn seq_max_ge_elem(s: Seq, k: int)\n requires 0 <= k < s.len()\n ensures Self::seq_max(s) >= s[k] as int\n decreases s.len()\n {\n if s.len() > 1 && k < s.len() - 1 {\n Self::seq_max_ge_elem(s.drop_last(), k);\n }\n }\n\n proof fn seq_min_le_elem(s: Seq, k: int)\n requires 0 <= k < s.len()\n ensures Self::seq_min(s) <= s[k] as int\n decreases s.len()\n {\n if s.len() > 1 && k < s.len() - 1 {\n Self::seq_min_le_elem(s.drop_last(), k);\n }\n }\n\n proof fn seq_max_bounded(s: Seq, bound: int)\n requires s.len() > 0, forall |i: int| 0 <= i < s.len() ==> s[i] as int <= bound\n ensures Self::seq_max(s) <= bound\n decreases s.len()\n {\n if s.len() > 1 { Self::seq_max_bounded(s.drop_last(), bound); }\n }\n\n proof fn seq_min_bounded(s: Seq, bound: int)\n requires s.len() > 0, forall |i: int| 0 <= i < s.len() ==> s[i] as int >= bound\n ensures Self::seq_min(s) >= bound\n decreases s.len()\n {\n if s.len() > 1 { Self::seq_min_bounded(s.drop_last(), bound); }\n }\n\n proof fn num_changes_nonneg(a: Seq, b: Seq)\n ensures Self::num_changes(a, b) >= 0\n decreases a.len()\n {\n if a.len() > 0 && a.len() == b.len() {\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n }\n }\n\n proof fn num_changes_ge_1(a: Seq, b: Seq, p: int)\n requires a.len() == b.len(), 0 <= p < a.len(), a[p] != b[p]\n ensures Self::num_changes(a, b) >= 1\n decreases a.len()\n {\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n if p < a.len() as int - 1 {\n Self::num_changes_ge_1(a.drop_last(), b.drop_last(), p);\n }\n }\n\n proof fn num_changes_ge_2(a: Seq, b: Seq, p0: int, p1: int)\n requires a.len() == b.len(), 0 <= p0 < a.len(), 0 <= p1 < a.len(),\n p0 != p1, a[p0] != b[p0], a[p1] != b[p1]\n ensures Self::num_changes(a, b) >= 2\n decreases a.len()\n {\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n let last = a.len() as int - 1;\n if last == p0 || last == p1 {\n let q = if last == p0 { p1 } else { p0 };\n Self::num_changes_ge_1(a.drop_last(), b.drop_last(), q);\n } else {\n Self::num_changes_ge_2(a.drop_last(), b.drop_last(), p0, p1);\n }\n }\n\n proof fn num_changes_ge_3(a: Seq, b: Seq, p0: int, p1: int, p2: int)\n requires a.len() == b.len(), 0 <= p0 < a.len(), 0 <= p1 < a.len(),\n 0 <= p2 < a.len(), p0 != p1, p0 != p2, p1 != p2,\n a[p0] != b[p0], a[p1] != b[p1], a[p2] != b[p2]\n ensures Self::num_changes(a, b) >= 3\n decreases a.len()\n {\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n let last = a.len() as int - 1;\n if last == p0 || last == p1 || last == p2 {\n let (q0, q1) = if last == p0 { (p1, p2) }\n else if last == p1 { (p0, p2) } else { (p0, p1) };\n Self::num_changes_ge_2(a.drop_last(), b.drop_last(), q0, q1);\n } else {\n Self::num_changes_ge_3(a.drop_last(), b.drop_last(), p0, p1, p2);\n }\n }\n\n proof fn num_changes_ge_4(a: Seq, b: Seq, p0: int, p1: int, p2: int, p3: int)\n requires a.len() == b.len(), 0 <= p0 < a.len(), 0 <= p1 < a.len(),\n 0 <= p2 < a.len(), 0 <= p3 < a.len(),\n p0 != p1, p0 != p2, p0 != p3, p1 != p2, p1 != p3, p2 != p3,\n a[p0] != b[p0], a[p1] != b[p1], a[p2] != b[p2], a[p3] != b[p3]\n ensures Self::num_changes(a, b) >= 4\n decreases a.len()\n {\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n let last = a.len() as int - 1;\n if last == p0 || last == p1 || last == p2 || last == p3 {\n let (q0, q1, q2) = if last == p0 { (p1, p2, p3) }\n else if last == p1 { (p0, p2, p3) }\n else if last == p2 { (p0, p1, p3) }\n else { (p0, p1, p2) };\n Self::num_changes_ge_3(a.drop_last(), b.drop_last(), q0, q1, q2);\n } else {\n Self::num_changes_ge_4(a.drop_last(), b.drop_last(), p0, p1, p2, p3);\n }\n }\n\n proof fn num_changes_le_3(a: Seq, b: Seq, c0: int, c1: int, c2: int)\n requires\n a.len() == b.len(), 0 <= c0 < a.len(), 0 <= c1 < a.len(), 0 <= c2 < a.len(),\n c0 != c1, c0 != c2, c1 != c2,\n forall |p: int| 0 <= p < a.len() && p != c0 && p != c1 && p != c2 ==> a[p] == b[p],\n ensures Self::num_changes(a, b) <= 3\n decreases a.len()\n {\n if a.len() == 0 { return; }\n let last = a.len() as int - 1;\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n if last == c0 || last == c1 || last == c2 {\n let (q0, q1) = if last == c0 { (c1, c2) }\n else if last == c1 { (c0, c2) } else { (c0, c1) };\n assert forall |p: int| 0 <= p < last && p != q0 && p != q1\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_2(a.drop_last(), b.drop_last(), q0, q1);\n } else {\n assert(a.last() == b.last());\n assert forall |p: int| 0 <= p < last && p != c0 && p != c1 && p != c2\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_3(a.drop_last(), b.drop_last(), c0, c1, c2);\n }\n }\n\n proof fn num_changes_le_2(a: Seq, b: Seq, c0: int, c1: int)\n requires\n a.len() == b.len(), 0 <= c0 < a.len(), 0 <= c1 < a.len(), c0 != c1,\n forall |p: int| 0 <= p < a.len() && p != c0 && p != c1 ==> a[p] == b[p],\n ensures Self::num_changes(a, b) <= 2\n decreases a.len()\n {\n if a.len() == 0 { return; }\n let last = a.len() as int - 1;\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n if last == c0 || last == c1 {\n let q = if last == c0 { c1 } else { c0 };\n assert forall |p: int| 0 <= p < last && p != q\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_1(a.drop_last(), b.drop_last(), q);\n } else {\n assert(a.last() == b.last());\n assert forall |p: int| 0 <= p < last && p != c0 && p != c1\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_2(a.drop_last(), b.drop_last(), c0, c1);\n }\n }\n\n proof fn num_changes_le_1(a: Seq, b: Seq, c0: int)\n requires a.len() == b.len(), 0 <= c0 < a.len(),\n forall |p: int| 0 <= p < a.len() && p != c0 ==> a[p] == b[p],\n ensures Self::num_changes(a, b) <= 1\n decreases a.len()\n {\n if a.len() == 0 { return; }\n let last = a.len() as int - 1;\n Self::num_changes_nonneg(a.drop_last(), b.drop_last());\n if last == c0 {\n assert forall |p: int| 0 <= p < last\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_0(a.drop_last(), b.drop_last());\n } else {\n assert(a.last() == b.last());\n assert forall |p: int| 0 <= p < last && p != c0\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_1(a.drop_last(), b.drop_last(), c0);\n }\n }\n\n proof fn num_changes_le_0(a: Seq, b: Seq)\n requires a.len() == b.len(), forall |p: int| 0 <= p < a.len() ==> a[p] == b[p]\n ensures Self::num_changes(a, b) == 0\n decreases a.len()\n {\n if a.len() > 0 {\n assert forall |p: int| 0 <= p < a.len() - 1\n implies a.drop_last()[p] == b.drop_last()[p] by {};\n Self::num_changes_le_0(a.drop_last(), b.drop_last());\n }\n }\n\n \n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_permutation(a: Seq, b: Seq) -> bool {\n forall |v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n proof fn count_occ_nonneg(s: Seq, val: i32)\n ensures Self::count_occ(s, val) >= 0\n decreases s.len()\n {\n if s.len() > 0 { Self::count_occ_nonneg(s.drop_last(), val); }\n }\n\n proof fn count_occ_push(s: Seq, val: i32, query: i32)\n ensures Self::count_occ(s.push(val), query)\n == Self::count_occ(s, query) + if val == query { 1int } else { 0int }\n {\n assert(s.push(val).drop_last() =~= s);\n }\n\n proof fn count_occ_append(a: Seq, b: Seq, val: i32)\n ensures Self::count_occ(a + b, val)\n == Self::count_occ(a, val) + Self::count_occ(b, val)\n decreases b.len()\n {\n if b.len() == 0 {\n assert(a + b =~= a);\n } else {\n let ab = a + b;\n assert(ab.drop_last() =~= a + b.drop_last());\n Self::count_occ_append(a, b.drop_last(), val);\n }\n }\n\n proof fn count_occ_contains(s: Seq, idx: int)\n requires 0 <= idx < s.len()\n ensures Self::count_occ(s, s[idx]) >= 1\n decreases s.len()\n {\n Self::count_occ_nonneg(s.drop_last(), s[idx]);\n if idx < s.len() - 1 {\n Self::count_occ_contains(s.drop_last(), idx);\n }\n }\n\n proof fn perm_refl(s: Seq)\n ensures Self::is_permutation(s, s)\n {}\n\n proof fn perm_trans(a: Seq, b: Seq, c: Seq)\n requires Self::is_permutation(a, b), Self::is_permutation(b, c)\n ensures Self::is_permutation(a, c)\n {}\n\n proof fn perm_append(a1: Seq, a2: Seq, b1: Seq, b2: Seq)\n requires Self::is_permutation(a1, b1), Self::is_permutation(a2, b2)\n ensures Self::is_permutation(a1 + a2, b1 + b2)\n {\n assert forall |v: i32|\n Self::count_occ(a1 + a2, v) == Self::count_occ(b1 + b2, v) by {\n Self::count_occ_append(a1, a2, v);\n Self::count_occ_append(b1, b2, v);\n };\n }\n\n fn ms_merge(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::is_sorted(a@), Self::is_sorted(b@)\n ensures\n Self::is_sorted(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_permutation(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n invariant\n 0 <= i <= a.len(), 0 <= j <= b.len(),\n Self::is_sorted(a@), Self::is_sorted(b@),\n Self::is_sorted(result@),\n result@.len() == i + j,\n Self::is_permutation(result@,\n a@.subrange(0, i as int) + b@.subrange(0, j as int)),\n i < a.len() ==> (forall |k: int|\n 0 <= k < result@.len() ==> result[k] <= a[i as int]),\n j < b.len() ==> (forall |k: int|\n 0 <= k < result@.len() ==> result[k] <= b[j as int]),\n decreases a.len() - i + b.len() - j,\n {\n let ghost old_result = result@;\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n proof {\n assert(Self::is_sorted(result@)) by {\n assert forall |p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q] by {\n if q < old_result.len() as int {\n } else if p < old_result.len() as int {\n assert(result[q] == a[i as int]);\n }\n };\n };\n let new_a_prefix = a@.subrange(0, (i + 1) as int);\n let old_a_prefix = a@.subrange(0, i as int);\n let b_prefix = b@.subrange(0, j as int);\n assert(new_a_prefix =~= old_a_prefix.push(a[i as int]));\n assert(result@ =~= old_result.push(a[i as int]));\n assert forall |v: i32|\n Self::count_occ(result@, v)\n == Self::count_occ(new_a_prefix + b_prefix, v) by {\n Self::count_occ_push(old_result, a[i as int], v);\n Self::count_occ_append(new_a_prefix, b_prefix, v);\n Self::count_occ_push(old_a_prefix, a[i as int], v);\n Self::count_occ_append(old_a_prefix, b_prefix, v);\n };\n }\n i = i + 1;\n } else {\n result.push(b[j]);\n proof {\n assert(Self::is_sorted(result@)) by {\n assert forall |p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q] by {\n if q < old_result.len() as int {\n } else if p < old_result.len() as int {\n assert(result[q] == b[j as int]);\n }\n };\n };\n let a_prefix = a@.subrange(0, i as int);\n let new_b_prefix = b@.subrange(0, (j + 1) as int);\n let old_b_prefix = b@.subrange(0, j as int);\n assert(new_b_prefix =~= old_b_prefix.push(b[j as int]));\n assert(result@ =~= old_result.push(b[j as int]));\n assert forall |v: i32|\n Self::count_occ(result@, v)\n == Self::count_occ(a_prefix + new_b_prefix, v) by {\n Self::count_occ_push(old_result, b[j as int], v);\n Self::count_occ_append(a_prefix, new_b_prefix, v);\n Self::count_occ_push(old_b_prefix, b[j as int], v);\n Self::count_occ_append(a_prefix, old_b_prefix, v);\n };\n }\n j = j + 1;\n }\n }\n proof {\n assert(a@.subrange(0, a@.len() as int) =~= a@);\n assert(b@.subrange(0, b@.len() as int) =~= b@);\n }\n result\n }\n\n fn ms_sort(input: &Vec) -> (result: Vec)\n ensures\n Self::is_sorted(result@),\n result@.len() == input@.len(),\n Self::is_permutation(result@, input@),\n decreases input.len(),\n {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n proof { assert(result@ =~= input@); Self::perm_refl(input@); }\n } else {\n proof { assert(result@ =~= input@); Self::perm_refl(input@); }\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n invariant\n 0 <= i <= mid, mid <= n, n == input.len(),\n left.len() == i,\n forall |k: int| 0 <= k < i as int ==> left[k] == input[k],\n decreases mid - i,\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n invariant\n mid <= j <= n, n == input.len(),\n right.len() == j - mid,\n forall |k: int| 0 <= k < (j - mid) as int\n ==> right[k] == input[k + mid as int],\n decreases n - j,\n {\n right.push(input[j]);\n j = j + 1;\n }\n proof {\n assert(left@ =~= input@.subrange(0, mid as int));\n assert(right@ =~= input@.subrange(mid as int, n as int));\n assert(input@ =~= left@ + right@);\n Self::perm_refl(input@);\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n proof {\n Self::perm_append(sorted_left@, sorted_right@, left@, right@);\n Self::perm_trans(result@, sorted_left@ + sorted_right@, left@ + right@);\n assert(left@ + right@ =~= input@);\n }\n result\n }\n\n \n pub open spec fn seq_remove(s: Seq, idx: int) -> Seq {\n s.subrange(0, idx) + s.subrange(idx + 1, s.len() as int)\n }\n\n proof fn seq_remove_index(s: Seq, idx: int, k: int)\n requires 0 <= idx < s.len(), 0 <= k < s.len() - 1\n ensures Self::seq_remove(s, idx)[k]\n == if k < idx { s[k] } else { s[k + 1] }\n {\n let r = Self::seq_remove(s, idx);\n let left = s.subrange(0, idx);\n let right = s.subrange(idx + 1, s.len() as int);\n assert(r =~= left + right);\n if k < idx { assert(r[k] == left[k]); }\n else { assert(r[k] == right[k - idx]); }\n }\n\n proof fn count_occ_remove(s: Seq, idx: int, v: i32)\n requires 0 <= idx < s.len()\n ensures Self::count_occ(s, v)\n == Self::count_occ(Self::seq_remove(s, idx), v)\n + if s[idx] == v { 1int } else { 0int }\n decreases s.len()\n {\n let n = s.len();\n if idx == n - 1 {\n assert(Self::seq_remove(s, idx) =~= s.drop_last());\n } else {\n Self::count_occ_remove(s.drop_last(), idx, v);\n let inner = Self::seq_remove(s.drop_last(), idx);\n let full_rem = Self::seq_remove(s, idx);\n assert(full_rem =~= inner.push(s[n - 1]));\n Self::count_occ_push(inner, s[n - 1], v);\n }\n }\n\n proof fn find_occ_pos(s: Seq, val: i32) -> (pos: int)\n requires Self::count_occ(s, val) >= 1\n ensures 0 <= pos < s.len(), s[pos] == val\n decreases s.len()\n {\n Self::count_occ_nonneg(s.drop_last(), val);\n if s.last() == val { (s.len() - 1) as int }\n else { Self::find_occ_pos(s.drop_last(), val) }\n }\n\n proof fn perm_to_bijection(sorted: Seq, original: Seq)\n -> (result: (Seq, Seq))\n requires\n sorted.len() == original.len(),\n Self::is_permutation(sorted, original),\n ensures ({\n let (perm, inv) = result;\n perm.len() == sorted.len()\n && inv.len() == sorted.len()\n && (forall |k: int| 0 <= k < sorted.len()\n ==> 0 <= #[trigger] perm[k] < sorted.len())\n && (forall |k: int, l: int| 0 <= k < l < sorted.len()\n ==> perm[k] != perm[l])\n && (forall |k: int| 0 <= k < sorted.len()\n ==> sorted[k] == original[#[trigger] perm[k]])\n && (forall |p: int| 0 <= p < sorted.len()\n ==> 0 <= #[trigger] inv[p] < sorted.len())\n && (forall |p: int| 0 <= p < sorted.len()\n ==> perm[#[trigger] inv[p]] == p)\n && (forall |k: int| 0 <= k < sorted.len()\n ==> inv[#[trigger] perm[k]] == k)\n })\n decreases sorted.len()\n {\n let n = sorted.len();\n if n == 0 {\n return (Seq::::empty(), Seq::::empty());\n }\n\n let val = sorted[n - 1];\n Self::count_occ_nonneg(sorted.drop_last(), val);\n assert(Self::count_occ(sorted, val) >= 1);\n let pos = Self::find_occ_pos(original, val);\n\n let sorted2 = sorted.drop_last();\n let original2 = Self::seq_remove(original, pos);\n\n assert forall |v: i32|\n Self::count_occ(sorted2, v) == Self::count_occ(original2, v) by {\n Self::count_occ_remove(original, pos, v);\n };\n\n let (sub_perm, sub_inv) = Self::perm_to_bijection(sorted2, original2);\n\n let perm = Seq::new(n as nat, |k: int|\n if k == n - 1 { pos }\n else { let sk = sub_perm[k]; if sk >= pos { sk + 1 } else { sk } }\n );\n let inv = Seq::new(n as nat, |p: int|\n if p == pos { n as int - 1 }\n else { sub_inv[if p > pos { p - 1 } else { p }] }\n );\n\n assert forall |k: int| 0 <= k < n\n implies 0 <= #[trigger] perm[k] < n by {\n if k != n - 1 { let sk = sub_perm[k]; assert(0 <= sk < n - 1); }\n };\n\n assert forall |k: int| 0 <= k < n\n implies sorted[k] == original[#[trigger] perm[k]] by {\n if k == n - 1 {\n } else {\n let sk = sub_perm[k];\n assert(sorted2[k] == original2[sk]);\n Self::seq_remove_index(original, pos, sk);\n }\n };\n\n assert forall |k: int, l: int| 0 <= k < l < n\n implies perm[k] != perm[l] by {\n if l == n - 1 {\n let sk = sub_perm[k]; assert(0 <= sk < n - 1);\n } else if k == n - 1 {\n } else {\n let sk = sub_perm[k]; let sl = sub_perm[l]; assert(sk != sl);\n }\n };\n\n assert forall |p: int| 0 <= p < n\n implies 0 <= #[trigger] inv[p] < n by {\n if p != pos {\n let up = if p > pos { (p - 1) as int } else { p };\n assert(0 <= up < n - 1);\n }\n };\n\n assert forall |p: int| 0 <= p < n\n implies perm[#[trigger] inv[p]] == p by {\n if p == pos {\n } else {\n let up = if p > pos { (p - 1) as int } else { p };\n assert(0 <= up < n - 1);\n let si = sub_inv[up];\n assert(sub_perm[si] == up);\n if p > pos {\n assert(up == p - 1); assert(up >= pos);\n } else {\n assert(up == p); assert(up < pos);\n }\n }\n };\n\n assert forall |k: int| 0 <= k < n\n implies inv[#[trigger] perm[k]] == k by {\n if k == n - 1 {\n } else {\n let sk = sub_perm[k];\n let pk = perm[k];\n assert(pk != pos) by {\n if sk >= pos { assert(pk == sk + 1); }\n else { assert(pk == sk); }\n };\n let up_pk = if pk > pos { (pk - 1) as int } else { pk };\n assert(up_pk == sk) by {\n if pk > pos { assert(sk >= pos); assert(pk == sk + 1); }\n else { assert(sk < pos); assert(pk == sk); }\n };\n assert(inv[pk] == sub_inv[sk]);\n }\n };\n\n (perm, inv)\n }\n\n proof fn perm_preserves_bounds(a: Seq, b: Seq, lo: int, hi: int)\n requires\n Self::is_permutation(a, b),\n forall |i: int| 0 <= i < b.len() ==> lo <= #[trigger] b[i] as int <= hi,\n ensures\n forall |i: int| 0 <= i < a.len() ==> lo <= #[trigger] a[i] as int <= hi,\n {\n assert forall |i: int| 0 <= i < a.len()\n implies lo <= #[trigger] a[i] as int <= hi by {\n Self::count_occ_contains(a, i);\n let j = Self::find_occ_pos(b, a[i]);\n };\n }\n\n pub fn min_difference(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n nums.len() <= 4 ==> res == 0,\n nums.len() > 4 ==> (\n exists |m: Seq|\n m.len() == nums.len()\n && Self::num_changes(nums@, m) <= 3\n && res as int == Self::seq_max(m) - Self::seq_min(m)\n && forall |m2: Seq|\n m2.len() == nums.len()\n && Self::num_changes(nums@, m2) <= 3\n ==> Self::seq_max(m2) - Self::seq_min(m2) >= res as int\n ),\n {\n let n = nums.len();\n if n <= 4 { return 0; }\n let ghost original = nums@;\n\n let nums = Self::ms_sort(&nums);\n\n let ghost perm: Seq;\n let ghost inv: Seq;\n proof {\n let (p, i) = Self::perm_to_bijection(nums@, original);\n perm = p;\n inv = i;\n Self::perm_preserves_bounds(nums@, original,\n -1_000_000_000, 1_000_000_000);\n }\n\n let d1 = nums[n - 1] - nums[3];\n let d2 = nums[n - 2] - nums[2];\n let d3 = nums[n - 3] - nums[1];\n let d4 = nums[n - 4] - nums[0];\n let mut result = d1;\n let mut best_j: Ghost = Ghost(3);\n if d2 < result {\n result = d2;\n proof { best_j@ = 2; }\n }\n if d3 < result {\n result = d3;\n proof { best_j@ = 1; }\n }\n if d4 < result {\n result = d4;\n proof { best_j@ = 0; }\n }\n\n proof {\n let j = best_j@;\n let nn = n as int;\n let sorted = nums@;\n assert(0 <= j <= 3);\n assert(result as int == sorted[j + nn - 4] as int - sorted[j] as int);\n let lo = sorted[j];\n let hi = sorted[j + nn - 4];\n\n let (cp0, cp1, cp2) = if j == 0 {\n (perm[nn - 3], perm[nn - 2], perm[nn - 1])\n } else if j == 1 {\n (perm[0], perm[nn - 2], perm[nn - 1])\n } else if j == 2 {\n (perm[0], perm[1], perm[nn - 1])\n } else {\n (perm[0], perm[1], perm[2])\n };\n assert(cp0 != cp1 && cp0 != cp2 && cp1 != cp2);\n\n let m = Seq::new(nn as nat, |p: int|\n if p == cp0 || p == cp1 || p == cp2 { lo } else { original[p] }\n );\n\n assert forall |p: int| 0 <= p < nn && p != cp0 && p != cp1 && p != cp2\n implies original[p] == m[p] by {};\n Self::num_changes_le_3(original, m, cp0, cp1, cp2);\n\n assert forall |p: int| 0 <= p < nn\n implies lo as int <= #[trigger] m[p] as int <= hi as int by {\n if p == cp0 || p == cp1 || p == cp2 {\n assert(Self::is_sorted(sorted));\n } else {\n let si = inv[p];\n assert(perm[si] == p);\n assert(original[p] == original[perm[si]]);\n assert(original[perm[si]] == sorted[si]);\n if j == 0 {\n assert(si != nn - 3 && si != nn - 2 && si != nn - 1) by {\n if si == nn - 3 { assert(perm[si] == perm[nn - 3]); assert(p == cp0); }\n if si == nn - 2 { assert(perm[si] == perm[nn - 2]); assert(p == cp1); }\n if si == nn - 1 { assert(perm[si] == perm[nn - 1]); assert(p == cp2); }\n };\n assert(0 <= si <= nn - 4);\n } else if j == 1 {\n assert(si != 0 && si != nn - 2 && si != nn - 1) by {\n if si == 0 { assert(perm[si] == perm[0]); assert(p == cp0); }\n if si == nn - 2 { assert(perm[si] == perm[nn - 2]); assert(p == cp1); }\n if si == nn - 1 { assert(perm[si] == perm[nn - 1]); assert(p == cp2); }\n };\n assert(1 <= si <= nn - 3);\n } else if j == 2 {\n assert(si != 0 && si != 1 && si != nn - 1) by {\n if si == 0 { assert(perm[si] == perm[0]); assert(p == cp0); }\n if si == 1 { assert(perm[si] == perm[1]); assert(p == cp1); }\n if si == nn - 1 { assert(perm[si] == perm[nn - 1]); assert(p == cp2); }\n };\n assert(2 <= si <= nn - 2);\n } else {\n assert(si != 0 && si != 1 && si != 2) by {\n if si == 0 { assert(perm[si] == perm[0]); assert(p == cp0); }\n if si == 1 { assert(perm[si] == perm[1]); assert(p == cp1); }\n if si == 2 { assert(perm[si] == perm[2]); assert(p == cp2); }\n };\n assert(3 <= si <= nn - 1);\n }\n assert(j <= si && si <= j + nn - 4);\n assert(Self::is_sorted(sorted));\n }\n };\n\n Self::seq_max_bounded(m, hi as int);\n Self::seq_min_bounded(m, lo as int);\n\n let ph = perm[j + nn - 4];\n assert(m[ph] == original[ph]) by {\n assert(j + nn - 4 != (if j == 0 { nn - 3 } else if j == 1 { 0 } else if j == 2 { 0 } else { 0 }));\n if ph == cp0 || ph == cp1 || ph == cp2 {\n if j == 0 { assert(j+nn-4 != nn-3 && j+nn-4 != nn-2 && j+nn-4 != nn-1); }\n else if j == 1 { assert(j+nn-4 != 0 && j+nn-4 != nn-2 && j+nn-4 != nn-1); }\n else if j == 2 { assert(j+nn-4 != 0 && j+nn-4 != 1 && j+nn-4 != nn-1); }\n else { assert(j+nn-4 != 0 && j+nn-4 != 1 && j+nn-4 != 2); }\n }\n };\n assert(m[ph] == sorted[j + nn - 4]);\n Self::seq_max_ge_elem(m, ph);\n\n let pl = perm[j];\n assert(m[pl] == original[pl]) by {\n if pl == cp0 || pl == cp1 || pl == cp2 {\n if j == 0 { assert(0 != nn-3 && 0 != nn-2 && 0 != nn-1); }\n else if j == 1 { assert(1 != 0 && 1 != nn-2 && 1 != nn-1); }\n else if j == 2 { assert(2 != 0 && 2 != 1 && 2 != nn-1); }\n else { assert(3 != 0 && 3 != 1 && 3 != 2); }\n }\n };\n assert(m[pl] == sorted[j]);\n Self::seq_min_le_elem(m, pl);\n\n assert(Self::seq_max(m) == hi as int);\n assert(Self::seq_min(m) == lo as int);\n\n \n assert forall |m2: Seq|\n m2.len() == nn && Self::num_changes(original, m2) <= 3\n implies Self::seq_max(m2) - Self::seq_min(m2) >= result as int\n by {\n let bot_si: int = if m2[perm[0]] == original[perm[0]] { 0 }\n else if m2[perm[1]] == original[perm[1]] { 1 }\n else if m2[perm[2]] == original[perm[2]] { 2 }\n else {\n if m2[perm[3]] != original[perm[3]] {\n Self::num_changes_ge_4(original, m2, perm[0], perm[1], perm[2], perm[3]);\n }\n 3\n };\n assert(m2[perm[bot_si]] == original[perm[bot_si]]);\n assert(original[perm[bot_si]] == sorted[bot_si]);\n Self::seq_min_le_elem(m2, perm[bot_si]);\n\n let top_si: int = if bot_si == 0 {\n if m2[perm[nn-1]] == original[perm[nn-1]] { nn - 1 }\n else if m2[perm[nn-2]] == original[perm[nn-2]] { nn - 2 }\n else if m2[perm[nn-3]] == original[perm[nn-3]] { nn - 3 }\n else {\n if m2[perm[nn-4]] != original[perm[nn-4]] {\n Self::num_changes_ge_4(original, m2, perm[nn-4], perm[nn-3], perm[nn-2], perm[nn-1]);\n }\n nn - 4\n }\n } else if bot_si == 1 {\n if m2[perm[nn-1]] == original[perm[nn-1]] { nn - 1 }\n else if m2[perm[nn-2]] == original[perm[nn-2]] { nn - 2 }\n else {\n if m2[perm[nn-3]] != original[perm[nn-3]] {\n Self::num_changes_ge_4(original, m2, perm[0], perm[nn-3], perm[nn-2], perm[nn-1]);\n }\n nn - 3\n }\n } else if bot_si == 2 {\n if m2[perm[nn-1]] == original[perm[nn-1]] { nn - 1 }\n else {\n if m2[perm[nn-2]] != original[perm[nn-2]] {\n Self::num_changes_ge_4(original, m2, perm[0], perm[1], perm[nn-2], perm[nn-1]);\n }\n nn - 2\n }\n } else {\n if m2[perm[nn-1]] != original[perm[nn-1]] {\n Self::num_changes_ge_4(original, m2, perm[0], perm[1], perm[2], perm[nn-1]);\n }\n nn - 1\n };\n\n assert(m2[perm[top_si]] == original[perm[top_si]]);\n assert(original[perm[top_si]] == sorted[top_si]);\n Self::seq_max_ge_elem(m2, perm[top_si]);\n\n assert(top_si >= bot_si + nn - 4);\n assert(sorted[top_si] as int >= sorted[bot_si + nn - 4] as int) by {\n assert(Self::is_sorted(sorted));\n };\n };\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1509/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1509/tests/mutated_testcases.jsonl"} {"problem": "lc1512", "family": "leetcode", "description": "# 1512. Number of Good Pairs\n\nGiven an array of integers `nums`, return the number of **good pairs**.\n\nA pair `(i, j)` is called good if `nums[i] == nums[j]` and `i < j`.\n\n## Example 1\n\n- **Input:** `nums = [1,2,3,1,1,3]`\n- **Output:** `4`\n- **Explanation:** There are 4 good pairs `(0,3)`, `(0,4)`, `(3,4)`, `(2,5)` (0-indexed).\n\n## Example 2\n\n- **Input:** `nums = [1,1,1,1]`\n- **Output:** `6`\n- **Explanation:** Each pair in the array is good.\n\n## Example 3\n\n- **Input:** `nums = [1,2,3]`\n- **Output:** `0`\n\n## Constraints\n\n- $1 \\leq nums.length \\leq 100$\n- $1 \\leq nums[i] \\leq 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_identical_pairs(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\n89.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_good_pair(nums: Seq, i: int, j: int) -> bool {\n 0 <= i < j < nums.len()\n && nums[i] == nums[j]\n}\n\npub open spec fn count_inner(nums: Seq, i: int, j: int) -> int\n decreases j - i - 1,\n{\n if j <= i + 1 {\n 0\n } else {\n count_inner(nums, i, j - 1)\n + if is_good_pair(nums, i, j - 1) { 1int } else { 0int }\n }\n}\n\npub open spec fn count_all(nums: Seq, i: int) -> int\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n 0\n } else {\n count_all(nums, i + 1) + count_inner(nums, i, nums.len() as int)\n }\n}\n\nimpl Solution {\n pub fn num_identical_pairs(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n res as int == count_all(nums@, 0),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn num_identical_pairs(nums: Vec) -> i32 {\n let n = nums.len();\n let mut count: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = i + 1;\n while j < n {\n if nums[i] == nums[j] {\n count = count + 1;\n }\n j += 1;\n }\n i += 1;\n }\n count as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_good_pair(nums: Seq, i: int, j: int) -> bool {\n 0 <= i < j < nums.len()\n && nums[i] == nums[j]\n}\n\npub open spec fn count_inner(nums: Seq, i: int, j: int) -> int\n decreases j - i - 1,\n{\n if j <= i + 1 {\n 0\n } else {\n count_inner(nums, i, j - 1)\n + if is_good_pair(nums, i, j - 1) { 1int } else { 0int }\n }\n}\n\npub open spec fn count_all(nums: Seq, i: int) -> int\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n 0\n } else {\n count_all(nums, i + 1) + count_inner(nums, i, nums.len() as int)\n }\n}\n\nimpl Solution {\n pub fn num_identical_pairs(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n res as int == count_all(nums@, 0),\n {\n let n = nums.len();\n let mut count: i64 = 0;\n let mut i: usize = 0;\n\n while i < n\n {\n let mut j: usize = i + 1;\n\n while j < n\n {\n if nums[i] == nums[j] {\n count = count + 1;\n }\n j += 1;\n }\n i += 1;\n }\n\n count as i32\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_good_pair(nums: Seq, i: int, j: int) -> bool {\n 0 <= i < j < nums.len()\n && nums[i] == nums[j]\n}\n\npub open spec fn count_inner(nums: Seq, i: int, j: int) -> int\n decreases j - i - 1,\n{\n if j <= i + 1 {\n 0\n } else {\n count_inner(nums, i, j - 1)\n + if is_good_pair(nums, i, j - 1) { 1int } else { 0int }\n }\n}\n\npub open spec fn count_all(nums: Seq, i: int) -> int\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n 0\n } else {\n count_all(nums, i + 1) + count_inner(nums, i, nums.len() as int)\n }\n}\n\nproof fn lemma_count_inner_bound(nums: Seq, i: int, j: int)\n requires\n 0 <= i,\n j <= nums.len(),\n ensures\n 0 <= count_inner(nums, i, j) <= if j > i + 1 { j - i - 1 } else { 0 },\n decreases j - i - 1,\n{\n if j <= i + 1 {\n } else {\n lemma_count_inner_bound(nums, i, j - 1);\n }\n}\n\nproof fn lemma_count_inner_mono(nums: Seq, i: int, j1: int, j2: int)\n requires\n 0 <= i,\n j1 <= j2,\n j2 <= nums.len(),\n ensures\n count_inner(nums, i, j1) <= count_inner(nums, i, j2),\n decreases j2 - j1,\n{\n if j1 >= j2 {\n } else {\n lemma_count_inner_mono(nums, i, j1, j2 - 1);\n }\n}\n\nproof fn lemma_count_all_bound(nums: Seq, i: int)\n requires\n 0 <= i,\n nums.len() <= 100,\n ensures\n 0 <= count_all(nums, i),\n count_all(nums, i) <= if i < nums.len() {\n (nums.len() - i) * nums.len()\n } else {\n 0int\n },\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n } else {\n lemma_count_all_bound(nums, i + 1);\n lemma_count_inner_bound(nums, i, nums.len() as int);\n assert(count_inner(nums, i, nums.len() as int) <= nums.len() - i - 1);\n assert(count_all(nums, i + 1) <= if (i + 1) < nums.len() {\n (nums.len() - (i + 1)) * nums.len()\n } else {\n 0int\n });\n assert((nums.len() - i - 1) + (nums.len() - (i + 1)) * nums.len() <= (nums.len() - i) * nums.len()) by(nonlinear_arith)\n requires\n 0 <= i < nums.len() <= 100,\n {}\n }\n}\n\nimpl Solution {\n pub fn num_identical_pairs(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n res as int == count_all(nums@, 0),\n {\n let n = nums.len();\n let mut count: i64 = 0;\n let mut i: usize = 0;\n\n proof {\n lemma_count_all_bound(nums@, 0);\n assert(count_all(nums@, 0) <= nums.len() * nums.len());\n assert(nums.len() * nums.len() <= 10000) by(nonlinear_arith)\n requires nums.len() <= 100;\n }\n\n while i < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 100,\n forall |idx: int| 0 <= idx < nums.len() ==> 1 <= #[trigger] nums[idx] <= 100,\n 0 <= i <= n,\n 0 <= count <= 10000,\n count == count_all(nums@, 0) - count_all(nums@, i as int),\n decreases n - i,\n {\n let mut j: usize = i + 1;\n\n proof {\n lemma_count_inner_bound(nums@, i as int, (i + 1) as int);\n }\n\n while j < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 100,\n forall |idx: int| 0 <= idx < nums.len() ==> 1 <= #[trigger] nums[idx] <= 100,\n 0 <= i < n,\n i + 1 <= j <= n,\n 0 <= count <= 10000,\n count == count_all(nums@, 0) - count_all(nums@, i as int)\n + count_inner(nums@, i as int, j as int),\n decreases n - j,\n {\n proof {\n lemma_count_inner_bound(nums@, i as int, (j + 1) as int);\n lemma_count_all_bound(nums@, i as int);\n lemma_count_all_bound(nums@, (i + 1) as int);\n lemma_count_inner_mono(nums@, i as int, (j + 1) as int, nums.len() as int);\n assert(count_all(nums@, i as int) == count_all(nums@, (i + 1) as int) + count_inner(nums@, i as int, nums.len() as int));\n }\n\n if nums[i] == nums[j] {\n proof {\n assert(is_good_pair(nums@, i as int, j as int));\n }\n count = count + 1;\n } else {\n assert(!is_good_pair(nums@, i as int, j as int));\n }\n\n assert(count == count_all(nums@, 0) - count_all(nums@, i as int)\n + count_inner(nums@, i as int, (j + 1) as int));\n\n proof {\n lemma_count_all_bound(nums@, 0);\n lemma_count_all_bound(nums@, (i + 1) as int);\n assert(count_inner(nums@, i as int, (j + 1) as int) <= count_inner(nums@, i as int, nums.len() as int));\n assert(count_all(nums@, (i + 1) as int) >= 0);\n assert(count_all(nums@, 0) <= nums.len() * nums.len());\n assert(nums.len() * nums.len() <= 10000) by(nonlinear_arith)\n requires nums.len() <= 100;\n }\n\n j += 1;\n }\n\n assert(count == count_all(nums@, 0) - count_all(nums@, i as int)\n + count_inner(nums@, i as int, n as int));\n assert(count == count_all(nums@, 0) - count_all(nums@, (i + 1) as int));\n\n proof {\n lemma_count_all_bound(nums@, (i + 1) as int);\n }\n\n i += 1;\n }\n\n assert(count_all(nums@, n as int) == 0int);\n assert(count == count_all(nums@, 0));\n\n count as i32\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1512/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1512/tests/mutated_testcases.jsonl"} {"problem": "lc1523", "family": "leetcode", "description": "# Count Odd Numbers in an Interval Range\n\nGiven two non-negative integers `low` and `high`, return the count of odd numbers between `low` and `high` (inclusive).\n\n## Example 1:\n\n> **Input:** low = 3, high = 7\n> **Output:** 3\n> **Explanation:** The odd numbers between 3 and 7 are [3,5,7].\n\n## Example 2:\n\n> **Input:** low = 8, high = 10\n> **Output:** 1\n> **Explanation:** The odd numbers between 8 and 10 are [9].\n\n## Constraints:\n\n- $0 \\leq low \\leq high \\leq 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_odds(low: i32, high: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 54.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_odds_up_to(n: int) -> int {\n (n + 1) / 2\n}\n\nimpl Solution {\n pub fn count_odds(low: i32, high: i32) -> (res: i32)\n requires\n 0 <= low <= high <= 1_000_000_000,\n ensures\n 0 <= res,\n res == count_odds_up_to(high as int) - count_odds_up_to(low as int - 1),\n {\n\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_odds(low: i32, high: i32) -> i32 {\n (high + 1) / 2 - low / 2\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_odds_up_to(n: int) -> int {\n (n + 1) / 2\n}\n\nimpl Solution {\n pub fn count_odds(low: i32, high: i32) -> (res: i32)\n requires\n 0 <= low <= high <= 1_000_000_000,\n ensures\n 0 <= res,\n res == count_odds_up_to(high as int) - count_odds_up_to(low as int - 1),\n {\n (high + 1) / 2 - low / 2\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_odds_up_to(n: int) -> int {\n (n + 1) / 2\n}\n\nimpl Solution {\n pub fn count_odds(low: i32, high: i32) -> (res: i32)\n requires\n 0 <= low <= high <= 1_000_000_000,\n ensures\n 0 <= res,\n res == count_odds_up_to(high as int) - count_odds_up_to(low as int - 1),\n {\n (high + 1) / 2 - low / 2\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1523/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1523/tests/mutated_testcases.jsonl"} {"problem": "lc1524", "family": "leetcode", "description": "# Number of Sub-arrays With Odd Sum\n\nGiven an array of integers `arr`, return the number of subarrays with an **odd** sum.\n\nSince the answer can be very large, return it modulo $10^9 + 7$.\n\n## Example 1:\n\n> **Input:** arr = [1,3,5]\n> **Output:** 4\n> **Explanation:** All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]. All sub-array sums are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4.\n\n## Example 2:\n\n> **Input:** arr = [2,4,6]\n> **Output:** 0\n> **Explanation:** All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]. All sub-array sums are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0.\n\n## Example 3:\n\n> **Input:** arr = [1,2,3,4,5,6,7]\n> **Output:** 16\n\n## Constraints:\n\n- $1 \\leq arr.length \\leq 10^5$\n- $1 \\leq arr[i] \\leq 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_of_subarrays(arr: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Dynamic Programming, Prefix Sum\nAcceptance rate: 55.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn subarray_sum(s: Seq, i: int, j: int) -> int\n decreases j - i + 1,\n{\n if i > j { 0 }\n else { subarray_sum(s, i, j - 1) + s[j] as int }\n}\n\npub open spec fn count_odd_ending(s: Seq, end_idx: int, k: int) -> int\n decreases k + 1,\n{\n if k < 0 { 0 }\n else {\n count_odd_ending(s, end_idx, k - 1)\n + if subarray_sum(s, k, end_idx) % 2 != 0 { 1 as int } else { 0 as int }\n }\n}\n\npub open spec fn count_odd_subarrays(s: Seq, n: int) -> int\n decreases n,\n{\n if n <= 0 { 0 }\n else {\n count_odd_subarrays(s, n - 1) + count_odd_ending(s, n - 1, n - 1)\n }\n}\n\nimpl Solution {\n pub fn num_of_subarrays(arr: Vec) -> (res: i32)\n requires\n 1 <= arr.len() <= 100_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100,\n ensures\n 0 <= res < 1_000_000_007,\n res as int == count_odd_subarrays(arr@, arr.len() as int) % 1_000_000_007,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn num_of_subarrays(arr: Vec) -> i32 {\n let mut odd: i64 = 0;\n let mut even: i64 = 0;\n let mut res: i64 = 0;\n let mut i: usize = 0;\n while i < arr.len() {\n if arr[i] % 2 == 0 {\n even = even + 1;\n } else {\n let tmp = odd;\n odd = even + 1;\n even = tmp;\n }\n res = (res + odd) % 1_000_000_007i64;\n i = i + 1;\n }\n res as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn subarray_sum(s: Seq, i: int, j: int) -> int\n decreases j - i + 1,\n{\n if i > j { 0 }\n else { subarray_sum(s, i, j - 1) + s[j] as int }\n}\n\npub open spec fn count_odd_ending(s: Seq, end_idx: int, k: int) -> int\n decreases k + 1,\n{\n if k < 0 { 0 }\n else {\n count_odd_ending(s, end_idx, k - 1)\n + if subarray_sum(s, k, end_idx) % 2 != 0 { 1 as int } else { 0 as int }\n }\n}\n\npub open spec fn count_odd_subarrays(s: Seq, n: int) -> int\n decreases n,\n{\n if n <= 0 { 0 }\n else {\n count_odd_subarrays(s, n - 1) + count_odd_ending(s, n - 1, n - 1)\n }\n}\n\nimpl Solution {\n pub fn num_of_subarrays(arr: Vec) -> (res: i32)\n requires\n 1 <= arr.len() <= 100_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100,\n ensures\n 0 <= res < 1_000_000_007,\n res as int == count_odd_subarrays(arr@, arr.len() as int) % 1_000_000_007,\n {\n let mut odd: i64 = 0;\n let mut even: i64 = 0;\n let mut res: i64 = 0;\n let mut i: usize = 0;\n while i < arr.len() {\n if arr[i] % 2 == 0 {\n even = even + 1;\n } else {\n let tmp = odd;\n odd = even + 1;\n even = tmp;\n }\n res = (res + odd) % 1_000_000_007i64;\n i = i + 1;\n }\n res as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::{lemma_fundamental_div_mod, lemma_mod_multiples_vanish};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn subarray_sum(s: Seq, i: int, j: int) -> int\n decreases j - i + 1,\n{\n if i > j { 0 }\n else { subarray_sum(s, i, j - 1) + s[j] as int }\n}\n\npub open spec fn count_odd_ending(s: Seq, end_idx: int, k: int) -> int\n decreases k + 1,\n{\n if k < 0 { 0 }\n else {\n count_odd_ending(s, end_idx, k - 1)\n + if subarray_sum(s, k, end_idx) % 2 != 0 { 1 as int } else { 0 as int }\n }\n}\n\npub open spec fn count_even_ending(s: Seq, end_idx: int, k: int) -> int\n decreases k + 1,\n{\n if k < 0 { 0 }\n else {\n count_even_ending(s, end_idx, k - 1)\n + if subarray_sum(s, k, end_idx) % 2 == 0 { 1 as int } else { 0 as int }\n }\n}\n\npub open spec fn count_odd_subarrays(s: Seq, n: int) -> int\n decreases n,\n{\n if n <= 0 { 0 }\n else {\n count_odd_subarrays(s, n - 1) + count_odd_ending(s, n - 1, n - 1)\n }\n}\n\nproof fn lemma_subarray_sum_last(s: Seq, i: int, j: int)\n requires\n 0 <= i <= j,\n j < s.len() as int,\n ensures\n subarray_sum(s, i, j) == subarray_sum(s, i, j - 1) + s[j] as int,\n{\n}\n\nproof fn lemma_subarray_sum_positive(s: Seq, i: int, j: int)\n requires\n 0 <= i <= j,\n j < s.len() as int,\n forall |k: int| 0 <= k < s.len() ==> 1 <= #[trigger] s[k] <= 100,\n ensures\n subarray_sum(s, i, j) >= 1,\n decreases j - i + 1,\n{\n if i < j {\n lemma_subarray_sum_positive(s, i, j - 1);\n } else {\n assert(subarray_sum(s, j, j - 1) == 0);\n }\n}\n\nproof fn lemma_shift_even(s: Seq, j: int, k: int)\n requires\n 0 < j,\n j < s.len() as int,\n -1 <= k <= j - 1,\n s[j] as int % 2 == 0,\n forall |idx: int| 0 <= idx < s.len() ==> 1 <= #[trigger] s[idx] <= 100,\n ensures\n count_odd_ending(s, j, k) == count_odd_ending(s, j - 1, k),\n count_even_ending(s, j, k) == count_even_ending(s, j - 1, k),\n decreases k + 1,\n{\n if k >= 0 {\n lemma_shift_even(s, j, k - 1);\n lemma_subarray_sum_last(s, k, j);\n lemma_subarray_sum_positive(s, k, j - 1);\n assert((subarray_sum(s, k, j - 1) + s[j] as int) % 2\n == subarray_sum(s, k, j - 1) % 2) by(nonlinear_arith)\n requires\n subarray_sum(s, k, j - 1) >= 1,\n s[j] as int >= 1,\n s[j] as int % 2 == 0,\n ;\n }\n}\n\nproof fn lemma_shift_odd(s: Seq, j: int, k: int)\n requires\n 0 < j,\n j < s.len() as int,\n -1 <= k <= j - 1,\n s[j] as int % 2 != 0,\n forall |idx: int| 0 <= idx < s.len() ==> 1 <= #[trigger] s[idx] <= 100,\n ensures\n count_odd_ending(s, j, k) == count_even_ending(s, j - 1, k),\n count_even_ending(s, j, k) == count_odd_ending(s, j - 1, k),\n decreases k + 1,\n{\n if k >= 0 {\n lemma_shift_odd(s, j, k - 1);\n lemma_subarray_sum_last(s, k, j);\n lemma_subarray_sum_positive(s, k, j - 1);\n assert((subarray_sum(s, k, j - 1) + s[j] as int) % 2\n != subarray_sum(s, k, j - 1) % 2) by(nonlinear_arith)\n requires\n subarray_sum(s, k, j - 1) >= 1,\n s[j] as int >= 1,\n s[j] as int % 2 != 0,\n ;\n }\n}\n\nproof fn lemma_transition_even(s: Seq, j: int)\n requires\n 0 < j,\n j < s.len() as int,\n s[j] as int % 2 == 0,\n forall |idx: int| 0 <= idx < s.len() ==> 1 <= #[trigger] s[idx] <= 100,\n ensures\n count_odd_ending(s, j, j) == count_odd_ending(s, j - 1, j - 1),\n count_even_ending(s, j, j) == count_even_ending(s, j - 1, j - 1) + 1,\n{\n lemma_shift_even(s, j, j - 1);\n assert(subarray_sum(s, j, j) == subarray_sum(s, j, j - 1) + s[j] as int);\n assert(subarray_sum(s, j, j - 1) == 0);\n}\n\nproof fn lemma_transition_odd(s: Seq, j: int)\n requires\n 0 < j,\n j < s.len() as int,\n s[j] as int % 2 != 0,\n forall |idx: int| 0 <= idx < s.len() ==> 1 <= #[trigger] s[idx] <= 100,\n ensures\n count_odd_ending(s, j, j) == count_even_ending(s, j - 1, j - 1) + 1,\n count_even_ending(s, j, j) == count_odd_ending(s, j - 1, j - 1),\n{\n lemma_shift_odd(s, j, j - 1);\n assert(subarray_sum(s, j, j) == subarray_sum(s, j, j - 1) + s[j] as int);\n assert(subarray_sum(s, j, j - 1) == 0);\n}\n\nproof fn lemma_odd_even_sum(s: Seq, end_idx: int, k: int)\n requires\n -1 <= k <= end_idx,\n end_idx < s.len() as int,\n ensures\n count_odd_ending(s, end_idx, k) + count_even_ending(s, end_idx, k) == k + 1,\n decreases k + 1,\n{\n if k >= 0 {\n lemma_odd_even_sum(s, end_idx, k - 1);\n }\n}\n\nproof fn lemma_count_nonneg(s: Seq, end_idx: int, k: int)\n ensures\n count_odd_ending(s, end_idx, k) >= 0,\n count_even_ending(s, end_idx, k) >= 0,\n decreases k + 1,\n{\n if k >= 0 {\n lemma_count_nonneg(s, end_idx, k - 1);\n }\n}\n\nproof fn lemma_count_odd_subarrays_nonneg(s: Seq, n: int)\n ensures\n count_odd_subarrays(s, n) >= 0,\n decreases n,\n{\n if n > 0 {\n lemma_count_odd_subarrays_nonneg(s, n - 1);\n lemma_count_nonneg(s, n - 1, n - 1);\n }\n}\n\nproof fn lemma_mod_add(a: int, b: int, m: int)\n requires\n m > 0,\n a >= 0,\n b >= 0,\n ensures\n (a % m + b) % m == (a + b) % m,\n{\n lemma_fundamental_div_mod(a, m);\n lemma_mod_multiples_vanish(a / m, a % m + b, m);\n}\n\nimpl Solution {\n pub fn num_of_subarrays(arr: Vec) -> (res: i32)\n requires\n 1 <= arr.len() <= 100_000,\n forall |i: int| 0 <= i < arr.len() ==> 1 <= #[trigger] arr[i] <= 100,\n ensures\n 0 <= res < 1_000_000_007,\n res as int == count_odd_subarrays(arr@, arr.len() as int) % 1_000_000_007,\n {\n let mut odd: i64 = 0;\n let mut even: i64 = 0;\n let mut res: i64 = 0;\n let mut i: usize = 0;\n while i < arr.len()\n invariant\n 0 <= i <= arr.len(),\n 1 <= arr.len() <= 100_000,\n forall |k: int| 0 <= k < arr.len() ==> 1 <= #[trigger] arr[k] <= 100,\n i == 0 ==> (odd == 0 && even == 0),\n i > 0 ==> odd as int == count_odd_ending(arr@, (i - 1) as int, (i - 1) as int),\n i > 0 ==> even as int == count_even_ending(arr@, (i - 1) as int, (i - 1) as int),\n 0 <= odd as int <= i as int,\n 0 <= even as int <= i as int,\n odd as int + even as int == i as int,\n 0 <= res < 1_000_000_007,\n res as int == count_odd_subarrays(arr@, i as int) % 1_000_000_007,\n decreases arr.len() - i,\n {\n if arr[i] % 2 == 0 {\n even = even + 1;\n proof {\n if i > 0 {\n lemma_transition_even(arr@, i as int);\n } else {\n assert(subarray_sum(arr@, 0, -1 as int) == 0);\n assert(count_odd_ending(arr@, 0, -1 as int) == 0);\n assert(count_even_ending(arr@, 0, -1 as int) == 0);\n }\n assert(odd as int == count_odd_ending(arr@, i as int, i as int));\n assert(even as int == count_even_ending(arr@, i as int, i as int));\n }\n } else {\n let tmp = odd;\n odd = even + 1;\n even = tmp;\n proof {\n if i > 0 {\n lemma_transition_odd(arr@, i as int);\n } else {\n assert(subarray_sum(arr@, 0, -1 as int) == 0);\n assert(count_odd_ending(arr@, 0, -1 as int) == 0);\n assert(count_even_ending(arr@, 0, -1 as int) == 0);\n }\n assert(odd as int == count_odd_ending(arr@, i as int, i as int));\n assert(even as int == count_even_ending(arr@, i as int, i as int));\n }\n }\n proof {\n lemma_odd_even_sum(arr@, i as int, i as int);\n lemma_count_nonneg(arr@, i as int, i as int);\n lemma_count_odd_subarrays_nonneg(arr@, i as int);\n assert(0 <= odd as int <= i as int + 1);\n assert(0 <= even as int <= i as int + 1);\n assert(odd as int + even as int == i as int + 1);\n lemma_mod_add(\n count_odd_subarrays(arr@, i as int),\n count_odd_ending(arr@, i as int, i as int),\n 1_000_000_007 as int,\n );\n }\n res = (res + odd) % 1_000_000_007i64;\n i = i + 1;\n }\n res as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1524/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1524/tests/mutated_testcases.jsonl"} {"problem": "lc153", "family": "leetcode", "description": "# Find Minimum in Rotated Sorted Array\n\nSuppose an array of length $n$ sorted in ascending order is rotated between $1$ and $n$ times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:\n\n- `[4,5,6,7,0,1,2]` if it was rotated $4$ times.\n- `[0,1,2,4,5,6,7]` if it was rotated $7$ times.\n\nNotice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.\n\nGiven the sorted rotated array `nums` of **unique** elements, return the minimum element of this array.\n\nYou must write an algorithm that runs in $O(\\log n)$ time.\n\n## Example 1:\n\n> **Input:** nums = [3,4,5,1,2]\n> **Output:** 1\n> **Explanation:** The original array was [1,2,3,4,5] rotated 3 times.\n\n## Example 2:\n\n> **Input:** nums = [4,5,6,7,0,1,2]\n> **Output:** 0\n> **Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.\n\n## Example 3:\n\n> **Input:** nums = [11,13,15,17]\n> **Output:** 11\n> **Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.\n\n## Constraints:\n\n- $n == nums.length$\n- $1 \\leq n \\leq 5000$\n- $-5000 \\leq nums[i] \\leq 5000$\n- All the integers of `nums` are **unique**.\n- `nums` is sorted and rotated between $1$ and $n$ times.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_min(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Binary Search\nAcceptance rate: 49.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_rotation_point(nums: Seq, k: int) -> bool {\n 0 <= k < nums.len()\n && (forall |a: int, b: int| k <= a < b < nums.len() ==> nums[a] < nums[b])\n && (forall |a: int, b: int| 0 <= a < b < k ==> nums[a] < nums[b])\n && (k == 0 || forall |a: int, b: int| 0 <= a < k && k <= b < nums.len() ==> nums[a] > nums[b])\n }\n\n pub fn find_min(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> -5000 <= #[trigger] nums[i] <= 5000,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n exists |k: int| Self::is_rotation_point(nums@, k),\n ensures\n forall |i: int| 0 <= i < nums.len() ==> result <= nums[i],\n exists |i: int| 0 <= i < nums.len() && result == nums[i],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_min(nums: Vec) -> i32 {\n let mut left: usize = 0;\n let mut right: usize = nums.len() - 1;\n while left < right {\n let mid = left + (right - left) / 2;\n if nums[mid] > nums[right] {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n nums[left]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_rotation_point(nums: Seq, k: int) -> bool {\n 0 <= k < nums.len()\n && (forall |a: int, b: int| k <= a < b < nums.len() ==> nums[a] < nums[b])\n && (forall |a: int, b: int| 0 <= a < b < k ==> nums[a] < nums[b])\n && (k == 0 || forall |a: int, b: int| 0 <= a < k && k <= b < nums.len() ==> nums[a] > nums[b])\n }\n\n pub fn find_min(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> -5000 <= #[trigger] nums[i] <= 5000,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n exists |k: int| Self::is_rotation_point(nums@, k),\n ensures\n forall |i: int| 0 <= i < nums.len() ==> result <= nums[i],\n exists |i: int| 0 <= i < nums.len() && result == nums[i],\n {\n let mut left: usize = 0;\n let mut right: usize = nums.len() - 1;\n\n while left < right\n {\n let mid = left + (right - left) / 2;\n\n if nums[mid] > nums[right] {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n\n nums[left]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_rotation_point(nums: Seq, k: int) -> bool {\n 0 <= k < nums.len()\n && (forall |a: int, b: int| k <= a < b < nums.len() ==> nums[a] < nums[b])\n && (forall |a: int, b: int| 0 <= a < b < k ==> nums[a] < nums[b])\n && (k == 0 || forall |a: int, b: int| 0 <= a < k && k <= b < nums.len() ==> nums[a] > nums[b])\n }\n\n pub fn find_min(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> -5000 <= #[trigger] nums[i] <= 5000,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n exists |k: int| Self::is_rotation_point(nums@, k),\n ensures\n forall |i: int| 0 <= i < nums.len() ==> result <= nums[i],\n exists |i: int| 0 <= i < nums.len() && result == nums[i],\n {\n let ghost k = choose |k: int| Self::is_rotation_point(nums@, k);\n\n let mut left: usize = 0;\n let mut right: usize = nums.len() - 1;\n\n while left < right\n invariant\n 0 <= left <= right < nums.len(),\n left as int <= k <= right as int,\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> -5000 <= #[trigger] nums[i] <= 5000,\n Self::is_rotation_point(nums@, k),\n decreases right - left,\n {\n let mid = left + (right - left) / 2;\n\n if nums[mid] > nums[right] {\n proof {\n \n \n if k <= mid as int {\n assert(nums[mid as int] < nums[right as int]);\n }\n }\n left = mid + 1;\n } else {\n proof {\n \n \n if k > mid as int {\n assert(nums[mid as int] > nums[right as int]);\n }\n }\n right = mid;\n }\n }\n\n proof {\n assert(left as int == k);\n assert forall |i: int| 0 <= i < nums.len() implies nums[left as int] <= nums[i] by {\n if i >= k {\n if i > k {\n assert(nums[k] < nums[i]);\n }\n } else {\n assert(nums[i] > nums[k]);\n }\n };\n }\n\n nums[left]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc153/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc153/tests/mutated_testcases.jsonl"} {"problem": "lc1558", "family": "leetcode", "description": "# Minimum Numbers of Function Calls to Make Target Array\n\nYou are given an integer array `nums`. You have an integer array `arr` of the same length with all values set to `0` initially. You also have the following `modify` function:\n\n```\nmodify(arr, op, idx):\n if op == 0:\n arr[idx] += 1\n if op == 1:\n for i from 0 to arr.length:\n arr[i] *= 2\n```\n\nYou want to use the modify function to convert `arr` to `nums` using the minimum number of calls.\n\nReturn the minimum number of function calls to make `nums` from `arr`.\n\nThe test cases are generated so that the answer fits in a **32-bit** signed integer.\n\n## Example 1:\n\n> **Input:** nums = [1,5]\n> **Output:** 5\n> **Explanation:** Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation). Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations). Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations). Total of operations: 1 + 2 + 2 = 5.\n\n## Example 2:\n\n> **Input:** nums = [2,2]\n> **Output:** 3\n> **Explanation:** Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations). Double all the elements: [1, 1] -> [2, 2] (1 operation). Total of operations: 2 + 1 = 3.\n\n## Example 3:\n\n> **Input:** nums = [4,2,5]\n> **Output:** 6\n> **Explanation:** (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5] (nums).\n\n## Constraints:\n\n- $1 <= nums.length <= 10^5$\n- $0 <= nums[i] <= 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Greedy, Bit Manipulation\nAcceptance rate: 62.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n pub open spec fn popcount(n: int) -> int\n decreases n,\n {\n if n <= 0 { 0 } else { (n % 2) + Self::popcount(n / 2) }\n }\n\n \n \n pub open spec fn bit_length(n: int) -> int\n decreases n,\n {\n if n <= 0 { 0 } else { 1 + Self::bit_length(n / 2) }\n }\n\n pub open spec fn sum_popcount(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 { 0 } else { Self::sum_popcount(s, end - 1) + Self::popcount(s[end - 1] as int) }\n }\n\n pub open spec fn max_bit_length(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let prev = Self::max_bit_length(s, end - 1);\n let curr = Self::bit_length(s[end - 1] as int);\n if curr > prev { curr } else { prev }\n }\n }\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n pub fn min_operations(nums: Vec) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==> 0 <= #[trigger] nums@[i] <= 1_000_000_000,\n ensures\n result as int == Self::sum_popcount(nums@, nums@.len() as int) +\n if Self::max_bit_length(nums@, nums@.len() as int) > 0 {\n Self::max_bit_length(nums@, nums@.len() as int) - 1\n } else {\n 0int\n },\n {\n }\n}\n\n}", "code": "impl Solution {\n pub fn min_operations(nums: Vec) -> i32 {\n let mut ones: i32 = 0;\n let mut max_len: i32 = 0;\n let n = nums.len();\n let mut i: usize = 0;\n while i < n {\n let mut val = nums[i];\n let mut bits: i32 = 0;\n let mut len: i32 = 0;\n while val > 0 {\n if val % 2 == 1 {\n bits = bits + 1;\n }\n val = val / 2;\n len = len + 1;\n }\n ones = ones + bits;\n if len > max_len {\n max_len = len;\n }\n i = i + 1;\n }\n if max_len > 0 {\n ones + max_len - 1\n } else {\n ones\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n pub open spec fn popcount(n: int) -> int\n decreases n,\n {\n if n <= 0 { 0 } else { (n % 2) + Self::popcount(n / 2) }\n }\n\n \n \n pub open spec fn bit_length(n: int) -> int\n decreases n,\n {\n if n <= 0 { 0 } else { 1 + Self::bit_length(n / 2) }\n }\n\n pub open spec fn sum_popcount(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 { 0 } else { Self::sum_popcount(s, end - 1) + Self::popcount(s[end - 1] as int) }\n }\n\n pub open spec fn max_bit_length(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let prev = Self::max_bit_length(s, end - 1);\n let curr = Self::bit_length(s[end - 1] as int);\n if curr > prev { curr } else { prev }\n }\n }\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n pub fn min_operations(nums: Vec) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==> 0 <= #[trigger] nums@[i] <= 1_000_000_000,\n ensures\n result as int == Self::sum_popcount(nums@, nums@.len() as int) +\n if Self::max_bit_length(nums@, nums@.len() as int) > 0 {\n Self::max_bit_length(nums@, nums@.len() as int) - 1\n } else {\n 0int\n },\n {\n let mut ones: i32 = 0;\n let mut max_len: i32 = 0;\n let n = nums.len();\n let mut i: usize = 0;\n while i < n {\n let mut val = nums[i];\n let mut bits: i32 = 0;\n let mut len: i32 = 0;\n while val > 0 {\n if val % 2 == 1 {\n bits = bits + 1;\n }\n val = val / 2;\n len = len + 1;\n }\n ones = ones + bits;\n if len > max_len {\n max_len = len;\n }\n i = i + 1;\n }\n if max_len > 0 {\n ones + max_len - 1\n } else {\n ones\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n pub open spec fn popcount(n: int) -> int\n decreases n,\n {\n if n <= 0 { 0 } else { (n % 2) + Self::popcount(n / 2) }\n }\n\n \n \n pub open spec fn bit_length(n: int) -> int\n decreases n,\n {\n if n <= 0 { 0 } else { 1 + Self::bit_length(n / 2) }\n }\n\n pub open spec fn sum_popcount(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 { 0 } else { Self::sum_popcount(s, end - 1) + Self::popcount(s[end - 1] as int) }\n }\n\n pub open spec fn max_bit_length(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n let prev = Self::max_bit_length(s, end - 1);\n let curr = Self::bit_length(s[end - 1] as int);\n if curr > prev { curr } else { prev }\n }\n }\n\n pub open spec fn pow2_spec(k: nat) -> nat\n decreases k,\n {\n if k == 0 { 1 } else { 2 * Self::pow2_spec((k - 1) as nat) }\n }\n\n proof fn popcount_nonneg(n: int)\n requires\n n >= 0,\n ensures\n Self::popcount(n) >= 0,\n decreases n,\n {\n if n > 0 {\n Self::popcount_nonneg(n / 2);\n }\n }\n\n proof fn bit_length_nonneg(n: int)\n requires\n n >= 0,\n ensures\n Self::bit_length(n) >= 0,\n decreases n,\n {\n if n > 0 {\n Self::bit_length_nonneg(n / 2);\n }\n }\n\n proof fn popcount_le_bit_length(n: int)\n requires\n n >= 0,\n ensures\n Self::popcount(n) <= Self::bit_length(n),\n decreases n,\n {\n if n > 0 {\n Self::popcount_le_bit_length(n / 2);\n }\n }\n\n proof fn bit_length_lt_pow2(n: int, k: nat)\n requires\n 0 <= n,\n n < Self::pow2_spec(k) as int,\n ensures\n Self::bit_length(n) <= k as int,\n decreases n,\n {\n if n > 0 {\n Self::bit_length_lt_pow2(n / 2, (k - 1) as nat);\n }\n }\n\n proof fn bit_length_le_30(n: int)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n Self::bit_length(n) <= 30,\n {\n assert(Self::pow2_spec(30) == 1_073_741_824nat) by {\n reveal_with_fuel(Solution::pow2_spec, 32);\n };\n Self::bit_length_lt_pow2(n, 30);\n }\n\n proof fn sum_popcount_bound(s: Seq, end: int)\n requires\n end >= 0,\n end <= s.len(),\n forall |j: int| 0 <= j < end ==> 0 <= #[trigger] s[j] <= 1_000_000_000,\n ensures\n Self::sum_popcount(s, end) <= 30 * end,\n decreases end,\n {\n if end > 0 {\n Self::sum_popcount_bound(s, end - 1);\n Self::popcount_le_bit_length(s[end - 1] as int);\n Self::bit_length_le_30(s[end - 1] as int);\n }\n }\n\n proof fn sum_popcount_nonneg(s: Seq, end: int)\n requires\n end >= 0,\n end <= s.len(),\n forall |j: int| 0 <= j < end ==> #[trigger] s[j] >= 0,\n ensures\n Self::sum_popcount(s, end) >= 0,\n decreases end,\n {\n if end > 0 {\n Self::sum_popcount_nonneg(s, end - 1);\n Self::popcount_nonneg(s[end - 1] as int);\n }\n }\n\n proof fn max_bit_length_nonneg(s: Seq, end: int)\n requires\n end >= 0,\n end <= s.len(),\n forall |j: int| 0 <= j < end ==> #[trigger] s[j] >= 0,\n ensures\n Self::max_bit_length(s, end) >= 0,\n decreases end,\n {\n if end > 0 {\n Self::max_bit_length_nonneg(s, end - 1);\n Self::bit_length_nonneg(s[end - 1] as int);\n }\n }\n\n proof fn max_bit_length_bound(s: Seq, end: int)\n requires\n end >= 0,\n end <= s.len(),\n forall |j: int| 0 <= j < end ==> 0 <= #[trigger] s[j] <= 1_000_000_000,\n ensures\n Self::max_bit_length(s, end) <= 30,\n decreases end,\n {\n if end > 0 {\n Self::max_bit_length_bound(s, end - 1);\n Self::bit_length_le_30(s[end - 1] as int);\n }\n }\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n pub fn min_operations(nums: Vec) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==> 0 <= #[trigger] nums@[i] <= 1_000_000_000,\n ensures\n result as int == Self::sum_popcount(nums@, nums@.len() as int) +\n if Self::max_bit_length(nums@, nums@.len() as int) > 0 {\n Self::max_bit_length(nums@, nums@.len() as int) - 1\n } else {\n 0int\n },\n {\n let mut ones: i32 = 0;\n let mut max_len: i32 = 0;\n let n = nums.len();\n let mut i: usize = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums@.len(),\n 1 <= n <= 100_000,\n forall |j: int| 0 <= j < n ==> 0 <= #[trigger] nums@[j] <= 1_000_000_000,\n ones as int == Self::sum_popcount(nums@, i as int),\n max_len as int == Self::max_bit_length(nums@, i as int),\n 0 <= ones <= 30 * i as int,\n 0 <= max_len <= 30,\n decreases n - i,\n {\n let mut val = nums[i];\n let mut bits: i32 = 0;\n let mut len: i32 = 0;\n\n proof {\n Self::bit_length_le_30(nums@[i as int] as int);\n Self::popcount_le_bit_length(nums@[i as int] as int);\n Self::popcount_nonneg(nums@[i as int] as int);\n Self::bit_length_nonneg(nums@[i as int] as int);\n }\n\n while val > 0\n invariant\n 0 <= i < n,\n n == nums@.len(),\n val >= 0,\n bits >= 0,\n len >= 0,\n bits as int + Self::popcount(val as int) == Self::popcount(nums@[i as int] as int),\n len as int + Self::bit_length(val as int) == Self::bit_length(nums@[i as int] as int),\n bits as int <= len as int,\n Self::bit_length(nums@[i as int] as int) <= 30,\n Self::popcount(nums@[i as int] as int) <= Self::bit_length(nums@[i as int] as int),\n decreases val,\n {\n proof {\n Self::popcount_nonneg(val as int / 2);\n Self::bit_length_nonneg(val as int / 2);\n }\n if val % 2 == 1 {\n bits = bits + 1;\n }\n val = val / 2;\n len = len + 1;\n }\n\n ones = ones + bits;\n if len > max_len {\n max_len = len;\n }\n i = i + 1;\n }\n\n if max_len > 0 {\n ones + max_len - 1\n } else {\n ones\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1558/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1558/tests/mutated_testcases.jsonl"} {"problem": "lc1560", "family": "leetcode", "description": "# Most Visited Sector in a Circular Track\n\nGiven an integer `n` and an integer array `rounds`. We have a circular track which consists of `n` sectors labeled from `1` to `n`. A marathon will be held on this track, the marathon consists of `m` rounds. The `i`-th round starts at sector `rounds[i - 1]` and ends at sector `rounds[i]`. For example, round 1 starts at sector `rounds[0]` and ends at sector `rounds[1]`.\n\nReturn *an array of the most visited sectors* sorted in **ascending** order.\n\nNotice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).\n\n## Example 1:\n\n> **Input:** n = 4, rounds = [1,3,1,2]\n> **Output:** [1,2]\n> **Explanation:** The marathon starts at sector 1. The order of the visited sectors is as follows:\n> 1 --> 2 --> 3 (end of round 1) --> 4 --> 1 (end of round 2) --> 2 (end of round 3 and the marathon)\n> We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.\n\n## Example 2:\n\n> **Input:** n = 2, rounds = [2,1,2,1,2,1,2,1,2]\n> **Output:** [2]\n\n## Example 3:\n\n> **Input:** n = 7, rounds = [1,3,5,7]\n> **Output:** [1,2,3,4,5,6,7]\n\n## Constraints:\n\n- `2 <= n <= 100`\n- `1 <= m <= 100`\n- `rounds.length == m + 1`\n- `1 <= rounds[i] <= n`\n- `rounds[i] != rounds[i + 1]` for `0 <= i < m`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn most_visited(n: i32, rounds: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Simulation\nAcceptance rate: 59.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn in_circular_range(s: int, start: int, end: int, n: int) -> bool {\n if start <= end {\n start <= s && s <= end\n } else {\n (1 <= s && s <= end) || (start <= s && s <= n)\n }\n }\n\n pub fn most_visited(n: i32, rounds: Vec) -> (result: Vec)\n requires\n 2 <= n <= 100,\n rounds.len() >= 2,\n rounds.len() <= 101,\n forall |i: int| 0 <= i < rounds.len() ==> 1 <= #[trigger] rounds[i] <= n,\n forall |i: int| 0 <= i < rounds.len() - 1 ==> (#[trigger] rounds[i]) != rounds[i + 1],\n ensures\n result@.len() >= 1,\n forall |i: int| 0 <= i < result@.len() ==> 1 <= #[trigger] result@[i] <= n,\n forall |i: int, j: int| 0 <= i < j < result@.len() ==> result@[i] < result@[j],\n forall |i: int| 0 <= i < result@.len() ==>\n Self::in_circular_range(\n (#[trigger] result@[i]) as int,\n rounds[0] as int,\n rounds[rounds.len() - 1] as int,\n n as int,\n ),\n forall |s: int| 1 <= s <= n as int && (#[trigger] Self::in_circular_range(\n s,\n rounds[0] as int,\n rounds[rounds.len() - 1] as int,\n n as int,\n )) ==> exists |i: int| 0 <= i < result@.len() && result@[i] as int == s,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn most_visited(n: i32, rounds: Vec) -> Vec {\n let start = rounds[0];\n let end_val = rounds[rounds.len() - 1];\n let mut result: Vec = Vec::new();\n if start <= end_val {\n let mut s = start;\n while s <= end_val {\n result.push(s);\n s = s + 1;\n }\n } else {\n let mut s: i32 = 1;\n while s <= end_val {\n result.push(s);\n s = s + 1;\n }\n s = start;\n while s <= n {\n result.push(s);\n s = s + 1;\n }\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn in_circular_range(s: int, start: int, end: int, n: int) -> bool {\n if start <= end {\n start <= s && s <= end\n } else {\n (1 <= s && s <= end) || (start <= s && s <= n)\n }\n }\n\n pub fn most_visited(n: i32, rounds: Vec) -> (result: Vec)\n requires\n 2 <= n <= 100,\n rounds.len() >= 2,\n rounds.len() <= 101,\n forall |i: int| 0 <= i < rounds.len() ==> 1 <= #[trigger] rounds[i] <= n,\n forall |i: int| 0 <= i < rounds.len() - 1 ==> (#[trigger] rounds[i]) != rounds[i + 1],\n ensures\n result@.len() >= 1,\n forall |i: int| 0 <= i < result@.len() ==> 1 <= #[trigger] result@[i] <= n,\n forall |i: int, j: int| 0 <= i < j < result@.len() ==> result@[i] < result@[j],\n forall |i: int| 0 <= i < result@.len() ==>\n Self::in_circular_range(\n (#[trigger] result@[i]) as int,\n rounds[0] as int,\n rounds[rounds.len() - 1] as int,\n n as int,\n ),\n forall |s: int| 1 <= s <= n as int && (#[trigger] Self::in_circular_range(\n s,\n rounds[0] as int,\n rounds[rounds.len() - 1] as int,\n n as int,\n )) ==> exists |i: int| 0 <= i < result@.len() && result@[i] as int == s,\n {\n let start = rounds[0];\n let end_val = rounds[rounds.len() - 1];\n let mut result: Vec = Vec::new();\n if start <= end_val {\n let mut s = start;\n while s <= end_val {\n result.push(s);\n s = s + 1;\n }\n } else {\n let mut s: i32 = 1;\n while s <= end_val {\n result.push(s);\n s = s + 1;\n }\n s = start;\n while s <= n {\n result.push(s);\n s = s + 1;\n }\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn in_circular_range(s: int, start: int, end: int, n: int) -> bool {\n if start <= end {\n start <= s && s <= end\n } else {\n (1 <= s && s <= end) || (start <= s && s <= n)\n }\n }\n\n pub fn most_visited(n: i32, rounds: Vec) -> (result: Vec)\n requires\n 2 <= n <= 100,\n rounds.len() >= 2,\n rounds.len() <= 101,\n forall |i: int| 0 <= i < rounds.len() ==> 1 <= #[trigger] rounds[i] <= n,\n forall |i: int| 0 <= i < rounds.len() - 1 ==> (#[trigger] rounds[i]) != rounds[i + 1],\n ensures\n result@.len() >= 1,\n forall |i: int| 0 <= i < result@.len() ==> 1 <= #[trigger] result@[i] <= n,\n forall |i: int, j: int| 0 <= i < j < result@.len() ==> result@[i] < result@[j],\n forall |i: int| 0 <= i < result@.len() ==>\n Self::in_circular_range(\n (#[trigger] result@[i]) as int,\n rounds[0] as int,\n rounds[rounds.len() - 1] as int,\n n as int,\n ),\n forall |s: int| 1 <= s <= n as int && (#[trigger] Self::in_circular_range(\n s,\n rounds[0] as int,\n rounds[rounds.len() - 1] as int,\n n as int,\n )) ==> exists |i: int| 0 <= i < result@.len() && result@[i] as int == s,\n {\n let start = rounds[0];\n let end_val = rounds[rounds.len() - 1];\n let mut result: Vec = Vec::new();\n if start <= end_val {\n let mut s = start;\n while s <= end_val\n invariant\n 1 <= start <= end_val <= n <= 100,\n start <= s <= end_val + 1,\n result@.len() == (s - start) as int,\n forall |i: int| 0 <= i < result@.len() ==> (#[trigger] result@[i]) as int == start as int + i,\n decreases end_val - s + 1,\n {\n result.push(s);\n s = s + 1;\n }\n proof {\n assert forall |i: int, j: int| 0 <= i < j < result@.len() implies result@[i] < result@[j] by {}\n\n assert forall |i: int| 0 <= i < result@.len() implies\n Self::in_circular_range(\n (#[trigger] result@[i]) as int,\n start as int,\n end_val as int,\n n as int,\n ) by {}\n\n assert forall |sv: int| 1 <= sv <= n as int && Self::in_circular_range(\n sv, start as int, end_val as int, n as int,\n ) implies exists |i: int| 0 <= i < result@.len() && result@[i] as int == sv by {\n let idx = sv - start as int;\n assert(result@[idx] as int == sv);\n }\n }\n } else {\n let mut s: i32 = 1;\n while s <= end_val\n invariant\n 1 <= end_val < start <= n <= 100,\n 1 <= s <= end_val + 1,\n result@.len() == (s - 1) as int,\n forall |i: int| 0 <= i < result@.len() ==> (#[trigger] result@[i]) as int == i + 1,\n decreases end_val - s + 1,\n {\n result.push(s);\n s = s + 1;\n }\n let ghost first_part_len: int = result@.len() as int;\n s = start;\n while s <= n\n invariant\n 1 <= end_val < start <= n <= 100,\n start <= s <= n + 1,\n first_part_len == end_val as int,\n result@.len() as int == first_part_len + (s - start) as int,\n forall |i: int| 0 <= i < first_part_len ==> (#[trigger] result@[i]) as int == i + 1,\n forall |i: int| first_part_len <= i < result@.len() ==> (#[trigger] result@[i]) as int == start as int + (i - first_part_len),\n decreases n - s + 1,\n {\n result.push(s);\n s = s + 1;\n }\n\n proof {\n assert forall |i: int, j: int| 0 <= i < j < result@.len() implies result@[i] < result@[j] by {\n if i < first_part_len && j < first_part_len {\n } else if i >= first_part_len && j >= first_part_len {\n } else {\n assert(result@[i] as int <= end_val as int);\n assert(result@[j] as int >= start as int);\n }\n }\n\n assert forall |i: int| 0 <= i < result@.len() implies\n Self::in_circular_range(\n (#[trigger] result@[i]) as int,\n start as int,\n end_val as int,\n n as int,\n ) by {\n if i < first_part_len {\n assert(1 <= result@[i] as int && result@[i] as int <= end_val as int);\n } else {\n assert(start as int <= result@[i] as int && result@[i] as int <= n as int);\n }\n }\n\n assert forall |sv: int| 1 <= sv <= n as int && Self::in_circular_range(\n sv, start as int, end_val as int, n as int,\n ) implies exists |i: int| 0 <= i < result@.len() && result@[i] as int == sv by {\n if sv <= end_val as int {\n let idx = sv - 1;\n assert(result@[idx] as int == sv);\n } else {\n let idx = first_part_len + (sv - start as int);\n assert(result@[idx] as int == sv);\n }\n }\n }\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1560/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1560/tests/mutated_testcases.jsonl"} {"problem": "lc1561", "family": "leetcode", "description": "# Maximum Number of Coins You Can Get\n\nThere are `3n` piles of coins of varying size, you and your friends will take piles of coins as follows:\n\n- In each step, you will choose **any** `3` piles of coins (not necessarily consecutive).\n- Of your choice, Alice will pick the pile with the maximum number of coins.\n- You will pick the next pile with the maximum number of coins.\n- Your friend Bob will pick the last pile.\n- Repeat until there are no more piles of coins.\n\nGiven an array of integers `piles` where `piles[i]` is the number of coins in the `i`th pile.\n\nReturn the maximum number of coins that you can have.\n\n## Example 1:\n\n> **Input:** piles = [2,4,1,2,7,8]\n> **Output:** 9\n> **Explanation:** Choose the triplet (2, 7, 8), Alice picks the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice picks the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal.\n\n## Example 2:\n\n> **Input:** piles = [2,4,5]\n> **Output:** 4\n\n## Example 3:\n\n> **Input:** piles = [9,8,7,6,5,1,2,3,4]\n> **Output:** 18\n\n## Constraints:\n\n- `3 <= piles.length <= 10^5`\n- `piles.length % 3 == 0`\n- `1 <= piles[i] <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_coins(piles: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Greedy, Sorting, Game Theory\nAcceptance rate: 84.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sorted(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i < j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm(a: Seq, b: Seq) -> bool {\n a.len() == b.len() && forall|v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n pub open spec fn even_index_sum(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() < 1 {\n 0int\n } else if s.len() < 2 {\n s[0] as int\n } else {\n s[0] as int + Self::even_index_sum(s.subrange(2, s.len() as int))\n }\n }\n\n pub fn max_coins(piles: Vec) -> (result: i32)\n requires\n 3 <= piles.len() <= 100000,\n piles.len() % 3 == 0,\n forall|i: int| 0 <= i < piles.len() ==> 1 <= #[trigger] piles[i] <= 10000,\n ensures\n exists|sorted_piles: Seq|\n Self::sorted(sorted_piles)\n && sorted_piles.len() == piles.len()\n && Self::is_perm(sorted_piles, piles@)\n && result as int == Self::even_index_sum(\n sorted_piles.subrange(\n (sorted_piles.len() / 3) as int,\n sorted_piles.len() as int,\n ),\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn ms_merge(a: &Vec, b: &Vec) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort(nums: &Vec) -> Vec {\n let n = nums.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(nums[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n {\n left.push(nums[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n {\n right.push(nums[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n result\n }\n\n pub fn max_coins(piles: Vec) -> i32 {\n let sorted = Self::ms_sort(&piles);\n let n = sorted.len();\n\n let mut sum: i32 = 0;\n let third = n / 3;\n let mut k = third;\n while k < n {\n sum = sum + sorted[k];\n k = k + 2;\n }\n\n sum\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sorted(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i < j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm(a: Seq, b: Seq) -> bool {\n a.len() == b.len() && forall|v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n pub open spec fn even_index_sum(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() < 1 {\n 0int\n } else if s.len() < 2 {\n s[0] as int\n } else {\n s[0] as int + Self::even_index_sum(s.subrange(2, s.len() as int))\n }\n }\n\n fn ms_merge(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::sorted(a@), Self::sorted(b@)\n ensures\n Self::sorted(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_perm(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort(nums: &Vec) -> (result: Vec)\n ensures\n Self::sorted(result@),\n Self::is_perm(result@, nums@),\n {\n let n = nums.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(nums[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n {\n left.push(nums[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n {\n right.push(nums[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n result\n }\n\n pub fn max_coins(piles: Vec) -> (result: i32)\n requires\n 3 <= piles.len() <= 100000,\n piles.len() % 3 == 0,\n forall|i: int| 0 <= i < piles.len() ==> 1 <= #[trigger] piles[i] <= 10000,\n ensures\n exists|sorted_piles: Seq|\n Self::sorted(sorted_piles)\n && sorted_piles.len() == piles.len()\n && Self::is_perm(sorted_piles, piles@)\n && result as int == Self::even_index_sum(\n sorted_piles.subrange(\n (sorted_piles.len() / 3) as int,\n sorted_piles.len() as int,\n ),\n ),\n {\n let sorted = Self::ms_sort(&piles);\n let n = sorted.len();\n\n let mut sum: i32 = 0;\n let third = n / 3;\n let mut k = third;\n while k < n {\n sum = sum + sorted[k];\n k = k + 2;\n }\n\n sum\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sorted(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i < j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len(),\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm(a: Seq, b: Seq) -> bool {\n a.len() == b.len() && forall|v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n pub open spec fn even_index_sum(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() < 1 {\n 0int\n } else if s.len() < 2 {\n s[0] as int\n } else {\n s[0] as int + Self::even_index_sum(s.subrange(2, s.len() as int))\n }\n }\n\n proof fn count_occ_nonneg(s: Seq, val: i32)\n ensures Self::count_occ(s, val) >= 0\n decreases s.len()\n {\n if s.len() > 0 { Self::count_occ_nonneg(s.drop_last(), val); }\n }\n\n proof fn count_occ_append(a: Seq, b: Seq, val: i32)\n ensures Self::count_occ(a + b, val) == Self::count_occ(a, val) + Self::count_occ(b, val)\n decreases b.len()\n {\n if b.len() == 0 {\n assert(a + b =~= a);\n } else {\n assert((a + b).drop_last() =~= a + b.drop_last());\n Self::count_occ_append(a, b.drop_last(), val);\n }\n }\n\n proof fn count_occ_push(s: Seq, val: i32, query: i32)\n ensures Self::count_occ(s.push(val), query)\n == Self::count_occ(s, query) + if val == query { 1int } else { 0int }\n {\n assert(s.push(val).drop_last() =~= s);\n }\n\n proof fn perm_append(a1: Seq, a2: Seq, b1: Seq, b2: Seq)\n requires Self::is_perm(a1, b1), Self::is_perm(a2, b2)\n ensures Self::is_perm(a1 + a2, b1 + b2)\n {\n assert forall|v: i32|\n Self::count_occ(a1 + a2, v) == Self::count_occ(b1 + b2, v)\n by {\n Self::count_occ_append(a1, a2, v);\n Self::count_occ_append(b1, b2, v);\n };\n }\n\n proof fn count_occ_elem(s: Seq, idx: int)\n requires 0 <= idx < s.len()\n ensures Self::count_occ(s, s[idx]) >= 1\n decreases s.len()\n {\n Self::count_occ_nonneg(s.drop_last(), s[idx]);\n if idx == s.len() - 1 {\n } else {\n Self::count_occ_elem(s.drop_last(), idx);\n assert(s.drop_last()[idx] == s[idx]);\n }\n }\n\n proof fn count_occ_positive_means_present(s: Seq, val: i32)\n requires Self::count_occ(s, val) >= 1\n ensures exists|i: int| 0 <= i < s.len() && s[i] == val\n decreases s.len()\n {\n if s.len() == 0 { return; }\n if s.last() == val { return; }\n Self::count_occ_positive_means_present(s.drop_last(), val);\n let i = choose|i: int| 0 <= i < s.drop_last().len() && s.drop_last()[i] == val;\n assert(s[i] == val);\n }\n\n proof fn perm_preserves_bounds(a: Seq, b: Seq, lo: int, hi: int)\n requires\n Self::is_perm(a, b),\n forall|i: int| 0 <= i < b.len() ==> lo <= #[trigger] b[i] as int <= hi,\n ensures\n forall|i: int| 0 <= i < a.len() ==> lo <= #[trigger] a[i] as int <= hi,\n {\n assert forall|i: int| 0 <= i < a.len() implies lo <= #[trigger] a[i] as int <= hi by {\n Self::count_occ_elem(a, i);\n Self::count_occ_positive_means_present(b, a[i]);\n };\n }\n\n fn ms_merge(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::sorted(a@), Self::sorted(b@)\n ensures\n Self::sorted(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_perm(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n invariant\n 0 <= i <= a.len(), 0 <= j <= b.len(),\n Self::sorted(a@), Self::sorted(b@),\n Self::sorted(result@),\n result@.len() == i + j,\n Self::is_perm(result@, a@.subrange(0, i as int) + b@.subrange(0, j as int)),\n i < a.len() ==> (forall|k: int| 0 <= k < result@.len() ==> result[k] <= a[i as int]),\n j < b.len() ==> (forall|k: int| 0 <= k < result@.len() ==> result[k] <= b[j as int]),\n decreases a.len() - i + b.len() - j,\n {\n let ghost old_result = result@;\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n proof {\n assert(Self::sorted(result@)) by {\n assert forall|p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q]\n by {\n if q < old_result.len() as int { }\n else if p < old_result.len() as int { assert(result[q] == a[i as int]); }\n };\n };\n let new_ap = a@.subrange(0, (i + 1) as int);\n let old_ap = a@.subrange(0, i as int);\n let bp = b@.subrange(0, j as int);\n assert(new_ap =~= old_ap.push(a[i as int]));\n assert(result@ =~= old_result.push(a[i as int]));\n assert forall|v: i32|\n Self::count_occ(result@, v) == Self::count_occ(new_ap + bp, v)\n by {\n Self::count_occ_push(old_result, a[i as int], v);\n Self::count_occ_append(new_ap, bp, v);\n Self::count_occ_push(old_ap, a[i as int], v);\n Self::count_occ_append(old_ap, bp, v);\n };\n }\n i = i + 1;\n } else {\n result.push(b[j]);\n proof {\n assert(Self::sorted(result@)) by {\n assert forall|p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q]\n by {\n if q < old_result.len() as int { }\n else if p < old_result.len() as int { assert(result[q] == b[j as int]); }\n };\n };\n let ap = a@.subrange(0, i as int);\n let new_bp = b@.subrange(0, (j + 1) as int);\n let old_bp = b@.subrange(0, j as int);\n assert(new_bp =~= old_bp.push(b[j as int]));\n assert(result@ =~= old_result.push(b[j as int]));\n assert forall|v: i32|\n Self::count_occ(result@, v) == Self::count_occ(ap + new_bp, v)\n by {\n Self::count_occ_push(old_result, b[j as int], v);\n Self::count_occ_append(ap, new_bp, v);\n Self::count_occ_push(old_bp, b[j as int], v);\n Self::count_occ_append(ap, old_bp, v);\n };\n }\n j = j + 1;\n }\n }\n proof {\n assert(a@.subrange(0, a@.len() as int) =~= a@);\n assert(b@.subrange(0, b@.len() as int) =~= b@);\n }\n result\n }\n\n fn ms_sort(nums: &Vec) -> (result: Vec)\n ensures\n Self::sorted(result@),\n Self::is_perm(result@, nums@),\n decreases nums.len(),\n {\n let n = nums.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(nums[0]);\n proof { assert(result@ =~= nums@); }\n } else {\n proof { assert(result@ =~= nums@); }\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n invariant\n 0 <= i <= mid, mid <= n, n == nums.len(),\n left.len() == i,\n forall|k: int| 0 <= k < i as int ==> left[k] == nums[k],\n decreases mid - i,\n {\n left.push(nums[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n invariant\n mid <= j <= n, n == nums.len(),\n right.len() == j - mid,\n forall|k: int| 0 <= k < (j - mid) as int ==> right[k] == nums[k + mid as int],\n decreases n - j,\n {\n right.push(nums[j]);\n j = j + 1;\n }\n proof {\n assert(left@ =~= nums@.subrange(0, mid as int));\n assert(right@ =~= nums@.subrange(mid as int, n as int));\n assert(nums@ =~= left@ + right@);\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n proof {\n Self::perm_append(sorted_left@, sorted_right@, left@, right@);\n assert forall|v: i32|\n Self::count_occ(result@, v) == Self::count_occ(nums@, v)\n by {\n assert(Self::count_occ(result@, v)\n == Self::count_occ(sorted_left@ + sorted_right@, v));\n assert(Self::count_occ(sorted_left@ + sorted_right@, v)\n == Self::count_occ(left@ + right@, v));\n assert(left@ + right@ =~= nums@);\n };\n }\n result\n }\n\n proof fn even_index_sum_extend_by_two(s: Seq, k: int)\n requires\n 0 <= k,\n k % 2 == 0,\n k + 2 <= s.len(),\n ensures\n Self::even_index_sum(s.subrange(0, k + 2))\n == s[k] as int + Self::even_index_sum(s.subrange(0, k)),\n decreases k,\n {\n if k == 0 {\n let sub2 = s.subrange(0, 2);\n assert(sub2.len() == 2);\n assert(Self::even_index_sum(sub2)\n == sub2[0] as int + Self::even_index_sum(sub2.subrange(2, 2)));\n assert(sub2.subrange(2, 2).len() == 0);\n assert(Self::even_index_sum(sub2.subrange(2, 2)) == 0);\n assert(Self::even_index_sum(s.subrange(0, 0)) == 0) by {\n assert(s.subrange(0, 0).len() == 0);\n }\n assert(sub2[0] == s[0]) by {\n assert(sub2[0] == s.subrange(0, 2)[0]);\n }\n } else {\n let s2 = s.subrange(2, s.len() as int);\n assert(s.subrange(2, k + 2) =~= s2.subrange(0, k));\n assert(s.subrange(2, k) =~= s2.subrange(0, k - 2));\n Self::even_index_sum_extend_by_two(s2, k - 2);\n let sk2 = s.subrange(0, k + 2);\n assert(Self::even_index_sum(sk2)\n == sk2[0] as int + Self::even_index_sum(sk2.subrange(2, sk2.len() as int)));\n assert(sk2.subrange(2, sk2.len() as int) =~= s.subrange(2, k + 2));\n let sk = s.subrange(0, k);\n assert(Self::even_index_sum(sk)\n == sk[0] as int + Self::even_index_sum(sk.subrange(2, sk.len() as int)));\n assert(sk.subrange(2, sk.len() as int) =~= s.subrange(2, k));\n }\n }\n\n proof fn even_index_sum_extend_by_one(s: Seq, k: int)\n requires\n 0 <= k,\n k % 2 == 0,\n k + 1 <= s.len(),\n ensures\n Self::even_index_sum(s.subrange(0, k + 1))\n == s[k] as int + Self::even_index_sum(s.subrange(0, k)),\n decreases k,\n {\n if k == 0 {\n let sub1 = s.subrange(0, 1);\n assert(sub1.len() == 1);\n assert(Self::even_index_sum(sub1) == sub1[0] as int);\n assert(Self::even_index_sum(s.subrange(0, 0)) == 0) by {\n assert(s.subrange(0, 0).len() == 0);\n }\n assert(sub1[0] == s[0]);\n } else {\n let s2 = s.subrange(2, s.len() as int);\n assert(s.subrange(2, k + 1) =~= s2.subrange(0, k - 1));\n assert(s.subrange(2, k) =~= s2.subrange(0, k - 2));\n Self::even_index_sum_extend_by_one(s2, k - 2);\n let sk1 = s.subrange(0, k + 1);\n assert(Self::even_index_sum(sk1)\n == sk1[0] as int + Self::even_index_sum(sk1.subrange(2, sk1.len() as int)));\n assert(sk1.subrange(2, sk1.len() as int) =~= s.subrange(2, k + 1));\n let sk = s.subrange(0, k);\n assert(Self::even_index_sum(sk)\n == sk[0] as int + Self::even_index_sum(sk.subrange(2, sk.len() as int)));\n assert(sk.subrange(2, sk.len() as int) =~= s.subrange(2, k));\n }\n }\n\n pub fn max_coins(piles: Vec) -> (result: i32)\n requires\n 3 <= piles.len() <= 100000,\n piles.len() % 3 == 0,\n forall|i: int| 0 <= i < piles.len() ==> 1 <= #[trigger] piles[i] <= 10000,\n ensures\n exists|sorted_piles: Seq|\n Self::sorted(sorted_piles)\n && sorted_piles.len() == piles.len()\n && Self::is_perm(sorted_piles, piles@)\n && result as int == Self::even_index_sum(\n sorted_piles.subrange(\n (sorted_piles.len() / 3) as int,\n sorted_piles.len() as int,\n ),\n ),\n {\n let sorted = Self::ms_sort(&piles);\n let n = sorted.len();\n\n proof {\n Self::perm_preserves_bounds(sorted@, piles@, 1, 10000);\n }\n\n let mut sum: i32 = 0;\n let third = n / 3;\n let mut k = third;\n let ghost tail = sorted@.subrange(third as int, n as int);\n let ghost tail_len = tail.len();\n\n while k < n\n invariant\n third <= k <= n,\n n == sorted.len(),\n n <= 100000,\n n % 3 == 0,\n third == n / 3,\n (k - third) % 2 == 0,\n forall|j: int| 0 <= j < n ==> 1 <= #[trigger] sorted[j] <= 10000,\n Self::sorted(sorted@),\n tail == sorted@.subrange(third as int, n as int),\n tail_len == n - third,\n sum as int == Self::even_index_sum(tail.subrange(0, (k - third) as int)),\n 0 <= sum <= ((k - third) / 2) as int * 10000,\n decreases n - k,\n {\n let ghost idx = (k - third) as int;\n proof {\n assert(tail[idx] == sorted@[k as int]) by {\n assert(sorted@.subrange(third as int, n as int)[idx] == sorted@[third as int + idx]);\n }\n if (k + 1) < n {\n Self::even_index_sum_extend_by_two(tail, idx);\n } else {\n Self::even_index_sum_extend_by_one(tail, idx);\n }\n }\n sum = sum + sorted[k];\n k = k + 2;\n }\n\n proof {\n assert(tail.subrange(0, tail_len as int) =~= tail);\n assert(Self::sorted(sorted@));\n assert(sorted@.len() == piles@.len());\n assert(Self::is_perm(sorted@, piles@));\n assert(sum as int == Self::even_index_sum(\n sorted@.subrange((sorted@.len() / 3) as int, sorted@.len() as int)));\n }\n\n sum\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1561/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1561/tests/mutated_testcases.jsonl"} {"problem": "lc1589", "family": "leetcode", "description": "# Maximum Sum Obtained of Any Permutation\n\nWe have an array of integers, `nums`, and an array of `requests` where `requests[i] = [start_i, end_i]`. The `i`-th request asks for the sum of `nums[start_i] + nums[start_i + 1] + ... + nums[end_i - 1] + nums[end_i]`. Both `start_i` and `end_i` are 0-indexed.\n\nReturn the maximum total sum of all requests among all permutations of `nums`.\n\nSince the answer may be too large, return it **modulo** $10^9 + 7$.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,4,5], requests = [[1,3],[0,1]]\n> **Output:** 19\n> **Explanation:** One permutation of nums is [2,1,3,4,5] with the following result:\n> requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8\n> requests[1] -> nums[0] + nums[1] = 2 + 1 = 3\n> Total sum: 8 + 3 = 11.\n> A permutation with a higher total sum is [3,5,4,2,1] with the following result:\n> requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11\n> requests[1] -> nums[0] + nums[1] = 3 + 5 = 8\n> Total sum: 11 + 8 = 19, which is the best that you can do.\n\n## Example 2:\n\n> **Input:** nums = [1,2,3,4,5,6], requests = [[0,1]]\n> **Output:** 11\n> **Explanation:** A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].\n\n## Example 3:\n\n> **Input:** nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]\n> **Output:** 47\n> **Explanation:** A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].\n\n## Constraints:\n\n- `n == nums.length`\n- $1 \\leq n \\leq 10^5$\n- $0 \\leq nums[i] \\leq 10^5$\n- $1 \\leq requests.length \\leq 10^5$\n- `requests[i].length == 2`\n- $0 \\leq start_i \\leq end_i < n$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_sum_range_query(nums: Vec, requests: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Greedy, Sorting, Prefix Sum\nAcceptance rate: 40.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn freq_at(requests: Seq>, idx: int, k: int) -> int\n decreases requests.len() - k\n {\n if k >= requests.len() as int {\n 0\n } else {\n (if requests[k]@[0] as int <= idx && idx <= requests[k]@[1] as int {\n 1int\n } else {\n 0int\n }) + Self::freq_at(requests, idx, k + 1)\n }\n }\n\n pub open spec fn freq_vec(requests: Seq>, n: int) -> Seq {\n Seq::new(n as nat, |i: int| Self::freq_at(requests, i, 0))\n }\n\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn dot_product_int(a: Seq, b: Seq, k: int) -> int\n decreases a.len() - k\n {\n if k >= a.len() as int {\n 0\n } else {\n a[k] * b[k] + Self::dot_product_int(a, b, k + 1)\n }\n }\n\n pub open spec fn is_sorted_int(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_int(s: Seq, v: int) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int })\n + Self::count_int(s.drop_last(), v)\n }\n }\n\n pub open spec fn is_perm_int(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: int| Self::count_int(a, v) == Self::count_int(b, v)\n }\n\n pub fn max_sum_range_query(nums: Vec, requests: Vec>) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==>\n 0 <= #[trigger] nums@[i] <= 100_000,\n 1 <= requests@.len() <= 100_000,\n forall |i: int| 0 <= i < requests@.len() ==> (\n (#[trigger] requests@[i])@.len() == 2\n && 0 <= requests@[i]@[0]\n && requests@[i]@[0] <= requests@[i]@[1]\n && (requests@[i]@[1] as int) < nums@.len() as int\n ),\n ensures\n 0 <= result < 1_000_000_007,\n exists |sv: Seq, sf: Seq|\n sv.len() == nums@.len()\n && sf.len() == nums@.len()\n && Self::is_sorted_int(sv)\n && Self::is_sorted_int(sf)\n && Self::is_perm_int(sv, Self::to_int_seq(nums@))\n && Self::is_perm_int(sf, Self::freq_vec(requests@, nums@.len() as int))\n && result as int\n == Self::dot_product_int(sv, sf, 0) % 1_000_000_007\n && forall |c: Seq|\n c.len() == sv.len() && Self::is_perm_int(c, sv)\n ==> Self::dot_product_int(sv, sf, 0)\n >= Self::dot_product_int(c, sf, 0),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn ms_merge(a: &Vec, b: &Vec) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len() {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort(input: &Vec) -> Vec {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n {\n right.push(input[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n result\n }\n\n fn ms_merge_i64(a: &Vec, b: &Vec) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len() {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort_i64(input: &Vec) -> Vec {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n {\n right.push(input[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort_i64(&left);\n let sorted_right = Self::ms_sort_i64(&right);\n let result = Self::ms_merge_i64(&sorted_left, &sorted_right);\n result\n }\n\n pub fn max_sum_range_query(nums: Vec, requests: Vec>) -> i32 {\n let n = nums.len();\n let m = requests.len();\n let modval: i64 = 1_000_000_007;\n let mut diff: Vec = Vec::new();\n let mut z: usize = 0;\n while z < n {\n diff.push(0);\n z = z + 1;\n }\n let mut r: usize = 0;\n while r < m {\n let s = requests[r][0] as usize;\n let e = requests[r][1] as usize;\n diff[s] = diff[s] + 1;\n if e + 1 < n {\n diff[e + 1] = diff[e + 1] - 1;\n }\n r = r + 1;\n }\n let mut count: Vec = Vec::new();\n let mut running: i64 = 0;\n let mut idx: usize = 0;\n while idx < n {\n running = running + diff[idx];\n count.push(running);\n idx = idx + 1;\n }\n let nums = Self::ms_sort(&nums);\n let sorted_count = Self::ms_sort_i64(&count);\n let mut result: i64 = 0;\n let mut k: usize = 0;\n while k < n {\n result = (result + nums[k] as i64 * sorted_count[k]) % modval;\n k = k + 1;\n }\n result as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn freq_at(requests: Seq>, idx: int, k: int) -> int\n decreases requests.len() - k\n {\n if k >= requests.len() as int {\n 0\n } else {\n (if requests[k]@[0] as int <= idx && idx <= requests[k]@[1] as int {\n 1int\n } else {\n 0int\n }) + Self::freq_at(requests, idx, k + 1)\n }\n }\n\n pub open spec fn freq_vec(requests: Seq>, n: int) -> Seq {\n Seq::new(n as nat, |i: int| Self::freq_at(requests, i, 0))\n }\n\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn dot_product_int(a: Seq, b: Seq, k: int) -> int\n decreases a.len() - k\n {\n if k >= a.len() as int {\n 0\n } else {\n a[k] * b[k] + Self::dot_product_int(a, b, k + 1)\n }\n }\n\n pub open spec fn is_sorted_int(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_int(s: Seq, v: int) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int })\n + Self::count_int(s.drop_last(), v)\n }\n }\n\n pub open spec fn is_perm_int(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: int| Self::count_int(a, v) == Self::count_int(b, v)\n }\n\n pub open spec fn to_int_seq_i64(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len()\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n pub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_occ_i64(s: Seq, val: i64) -> int\n decreases s.len()\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ_i64(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm_i64(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: i64| Self::count_occ_i64(a, v) == Self::count_occ_i64(b, v)\n }\n\n pub open spec fn is_sorted_i64(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n fn ms_merge(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::is_sorted(a@), Self::is_sorted(b@)\n ensures\n Self::is_sorted(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_perm(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort(input: &Vec) -> (result: Vec)\n ensures\n Self::is_sorted(result@),\n result@.len() == input@.len(),\n Self::is_perm(result@, input@),\n decreases input.len(),\n {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n {\n right.push(input[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n result\n }\n\n fn ms_merge_i64(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::is_sorted_i64(a@), Self::is_sorted_i64(b@)\n ensures\n Self::is_sorted_i64(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_perm_i64(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n {\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n i = i + 1;\n } else {\n result.push(b[j]);\n j = j + 1;\n }\n }\n result\n }\n\n fn ms_sort_i64(input: &Vec) -> (result: Vec)\n ensures\n Self::is_sorted_i64(result@),\n result@.len() == input@.len(),\n Self::is_perm_i64(result@, input@),\n decreases input.len(),\n {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n {\n right.push(input[j]);\n j = j + 1;\n }\n let sorted_left = Self::ms_sort_i64(&left);\n let sorted_right = Self::ms_sort_i64(&right);\n let result = Self::ms_merge_i64(&sorted_left, &sorted_right);\n result\n }\n\n pub fn max_sum_range_query(nums: Vec, requests: Vec>) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==>\n 0 <= #[trigger] nums@[i] <= 100_000,\n 1 <= requests@.len() <= 100_000,\n forall |i: int| 0 <= i < requests@.len() ==> (\n (#[trigger] requests@[i])@.len() == 2\n && 0 <= requests@[i]@[0]\n && requests@[i]@[0] <= requests@[i]@[1]\n && (requests@[i]@[1] as int) < nums@.len() as int\n ),\n ensures\n 0 <= result < 1_000_000_007,\n exists |sv: Seq, sf: Seq|\n sv.len() == nums@.len()\n && sf.len() == nums@.len()\n && Self::is_sorted_int(sv)\n && Self::is_sorted_int(sf)\n && Self::is_perm_int(sv, Self::to_int_seq(nums@))\n && Self::is_perm_int(sf, Self::freq_vec(requests@, nums@.len() as int))\n && result as int\n == Self::dot_product_int(sv, sf, 0) % 1_000_000_007\n && forall |c: Seq|\n c.len() == sv.len() && Self::is_perm_int(c, sv)\n ==> Self::dot_product_int(sv, sf, 0)\n >= Self::dot_product_int(c, sf, 0),\n {\n let n = nums.len();\n let m = requests.len();\n let modval: i64 = 1_000_000_007;\n let mut diff: Vec = Vec::new();\n let mut z: usize = 0;\n while z < n {\n diff.push(0);\n z = z + 1;\n }\n let mut r: usize = 0;\n while r < m {\n let s = requests[r][0] as usize;\n let e = requests[r][1] as usize;\n diff.set(s, diff[s] + 1);\n if e + 1 < n {\n diff.set(e + 1, diff[e + 1] - 1);\n }\n r = r + 1;\n }\n let mut count: Vec = Vec::new();\n let mut running: i64 = 0;\n let mut idx: usize = 0;\n while idx < n {\n running = running + diff[idx];\n count.push(running);\n idx = idx + 1;\n }\n let nums = Self::ms_sort(&nums);\n let sorted_count = Self::ms_sort_i64(&count);\n let mut result: i64 = 0;\n let mut k: usize = 0;\n while k < n {\n result = (result + nums[k] as i64 * sorted_count[k]) % modval;\n k = k + 1;\n }\n result as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn freq_at(requests: Seq>, idx: int, k: int) -> int\n decreases requests.len() - k\n {\n if k >= requests.len() as int {\n 0\n } else {\n (if requests[k]@[0] as int <= idx && idx <= requests[k]@[1] as int {\n 1int\n } else {\n 0int\n }) + Self::freq_at(requests, idx, k + 1)\n }\n }\n\n pub open spec fn freq_vec(requests: Seq>, n: int) -> Seq {\n Seq::new(n as nat, |i: int| Self::freq_at(requests, i, 0))\n }\n\n pub open spec fn diff_delta(requests: Seq>, pos: int, i: int) -> int {\n (if requests[i]@[0] as int == pos { 1int } else { 0int })\n - (if requests[i]@[1] as int + 1 == pos { 1int } else { 0int })\n }\n\n pub open spec fn diff_at(requests: Seq>, pos: int, r: int) -> int\n decreases r\n {\n if r <= 0 { 0int } else { Self::diff_at(requests, pos, r - 1) + Self::diff_delta(requests, pos, r - 1) }\n }\n\n pub open spec fn prefix_diff(requests: Seq>, idx: int, r: int) -> int\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 { 0int } else { Self::prefix_diff(requests, idx - 1, r) + Self::diff_at(requests, idx, r) }\n }\n\n pub open spec fn prefix_delta_sum(requests: Seq>, idx: int, i: int) -> int\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 { 0int } else { Self::prefix_delta_sum(requests, idx - 1, i) + Self::diff_delta(requests, idx, i) }\n }\n\n pub open spec fn prefix_point_indicator(idx: int, c: int) -> int\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 { 0int } else { Self::prefix_point_indicator(idx - 1, c) + (if idx == c { 1int } else { 0int }) }\n }\n\n pub open spec fn freq_at_prefix(requests: Seq>, idx: int, r: int) -> int\n decreases r\n {\n if r <= 0 {\n 0int\n } else {\n Self::freq_at_prefix(requests, idx, r - 1)\n + (if requests[r - 1]@[0] as int <= idx && idx <= requests[r - 1]@[1] as int { 1int } else { 0int })\n }\n }\n\n proof fn lemma_freq_prefix_eq(requests: Seq>, idx: int, r: int)\n requires 0 <= r <= requests.len(),\n ensures Self::freq_at_prefix(requests, idx, r) + Self::freq_at(requests, idx, r) == Self::freq_at(requests, idx, 0),\n decreases r\n {\n if r == 0 {\n } else {\n Self::lemma_freq_prefix_eq(requests, idx, r - 1);\n }\n }\n\n proof fn lemma_prefix_point_indicator(idx: int, c: int)\n requires c >= 0,\n ensures Self::prefix_point_indicator(idx, c) == (if c <= idx { 1int } else { 0int }),\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 {\n } else {\n Self::lemma_prefix_point_indicator(idx - 1, c);\n }\n }\n\n proof fn lemma_prefix_delta_sum_eq(requests: Seq>, idx: int, i: int)\n requires 0 <= i < requests.len(),\n ensures Self::prefix_delta_sum(requests, idx, i)\n == Self::prefix_point_indicator(idx, requests[i]@[0] as int)\n - Self::prefix_point_indicator(idx, requests[i]@[1] as int + 1),\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 {\n } else {\n Self::lemma_prefix_delta_sum_eq(requests, idx - 1, i);\n }\n }\n\n proof fn lemma_single_spike(requests: Seq>, idx: int, i: int)\n requires\n 0 <= i < requests.len(),\n 0 <= idx,\n 0 <= requests[i]@[0] as int <= requests[i]@[1] as int,\n ensures\n Self::prefix_delta_sum(requests, idx, i) ==\n (if requests[i]@[0] as int <= idx && idx <= requests[i]@[1] as int { 1int } else { 0int }),\n {\n Self::lemma_prefix_delta_sum_eq(requests, idx, i);\n Self::lemma_prefix_point_indicator(idx, requests[i]@[0] as int);\n Self::lemma_prefix_point_indicator(idx, requests[i]@[1] as int + 1);\n }\n\n proof fn lemma_prefix_diff_split(requests: Seq>, idx: int, r: int)\n requires r >= 1, idx >= -1,\n ensures Self::prefix_diff(requests, idx, r) ==\n Self::prefix_diff(requests, idx, r - 1) + Self::prefix_delta_sum(requests, idx, r - 1),\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 {\n } else {\n Self::lemma_prefix_diff_split(requests, idx - 1, r);\n }\n }\n\n proof fn lemma_prefix_diff_zero(requests: Seq>, idx: int)\n requires idx >= -1,\n ensures Self::prefix_diff(requests, idx, 0) == 0,\n decreases (if idx >= 0 { idx + 1 } else { 0 }) as nat\n {\n if idx < 0 {\n } else {\n Self::lemma_prefix_diff_zero(requests, idx - 1);\n }\n }\n\n proof fn lemma_prefix_diff_eq(requests: Seq>, idx: int, r: int)\n requires\n 0 <= idx,\n 0 <= r <= requests.len(),\n forall |i: int| 0 <= i < requests.len() ==>\n 0 <= (#[trigger] requests[i])@[0] as int <= requests[i]@[1] as int,\n ensures Self::prefix_diff(requests, idx, r) == Self::freq_at_prefix(requests, idx, r),\n decreases r\n {\n if r == 0 {\n Self::lemma_prefix_diff_zero(requests, idx);\n } else {\n Self::lemma_prefix_diff_eq(requests, idx, r - 1);\n Self::lemma_prefix_diff_split(requests, idx, r);\n Self::lemma_single_spike(requests, idx, r - 1);\n }\n }\n\n proof fn lemma_prefix_diff_freq(requests: Seq>, idx: int)\n requires\n 0 <= idx,\n forall |i: int| 0 <= i < requests.len() ==>\n 0 <= (#[trigger] requests[i])@[0] as int <= requests[i]@[1] as int,\n ensures Self::prefix_diff(requests, idx, requests.len() as int) == Self::freq_at(requests, idx, 0),\n {\n Self::lemma_prefix_diff_eq(requests, idx, requests.len() as int);\n Self::lemma_freq_prefix_eq(requests, idx, requests.len() as int);\n }\n\n proof fn lemma_freq_at_prefix_nonneg(requests: Seq>, idx: int, r: int)\n requires 0 <= r <= requests.len(),\n ensures Self::freq_at_prefix(requests, idx, r) >= 0,\n decreases r\n {\n if r > 0 {\n Self::lemma_freq_at_prefix_nonneg(requests, idx, r - 1);\n }\n }\n\n proof fn lemma_diff_at_bound(requests: Seq>, pos: int, r: int)\n requires 0 <= r,\n ensures -r <= Self::diff_at(requests, pos, r) <= r,\n decreases r\n {\n if r > 0 {\n Self::lemma_diff_at_bound(requests, pos, r - 1);\n }\n }\n\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn dot_product_int(a: Seq, b: Seq, k: int) -> int\n decreases a.len() - k\n {\n if k >= a.len() as int {\n 0\n } else {\n a[k] * b[k] + Self::dot_product_int(a, b, k + 1)\n }\n }\n\n pub open spec fn is_sorted_int(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_int(s: Seq, v: int) -> int\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int })\n + Self::count_int(s.drop_last(), v)\n }\n }\n\n pub open spec fn is_perm_int(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: int| Self::count_int(a, v) == Self::count_int(b, v)\n }\n\n pub open spec fn to_int_seq_i64(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn count_occ(s: Seq, val: i32) -> int\n decreases s.len()\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: i32| Self::count_occ(a, v) == Self::count_occ(b, v)\n }\n\n pub open spec fn is_sorted(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_occ_i64(s: Seq, val: i64) -> int\n decreases s.len()\n {\n if s.len() == 0 { 0 }\n else {\n (if s.last() == val { 1int } else { 0int })\n + Self::count_occ_i64(s.drop_last(), val)\n }\n }\n\n pub open spec fn is_perm_i64(a: Seq, b: Seq) -> bool {\n a.len() == b.len()\n && forall |v: i64| Self::count_occ_i64(a, v) == Self::count_occ_i64(b, v)\n }\n\n pub open spec fn is_sorted_i64(s: Seq) -> bool {\n forall |i: int, j: int| 0 <= i <= j < s.len() ==> s[i] <= s[j]\n }\n\n pub open spec fn count_in_range_int(s: Seq, v: int, start: int, end: int) -> int\n decreases end - start when start <= end\n {\n if start >= end { 0 }\n else {\n (if s[start] == v { 1int } else { 0int })\n + Self::count_in_range_int(s, v, start + 1, end)\n }\n }\n\n \n\n proof fn freq_at_bounded(requests: Seq>, idx: int, k: int)\n requires\n 0 <= k,\n forall |i: int| 0 <= i < requests.len() ==> (\n (#[trigger] requests[i])@.len() == 2\n && 0 <= requests[i]@[0]\n && requests[i]@[0] <= requests[i]@[1]\n ),\n ensures\n 0 <= Self::freq_at(requests, idx, k),\n k <= requests.len() as int ==> Self::freq_at(requests, idx, k) <= requests.len() as int - k,\n decreases requests.len() - k\n {\n if k < requests.len() as int {\n Self::freq_at_bounded(requests, idx, k + 1);\n }\n }\n\n proof fn count_int_nonneg(s: Seq, v: int)\n ensures Self::count_int(s, v) >= 0\n decreases s.len()\n {\n if s.len() > 0 {\n Self::count_int_nonneg(s.drop_last(), v);\n }\n }\n\n proof fn count_int_positive_exists(s: Seq, v: int)\n requires Self::count_int(s, v) >= 1\n ensures exists |k: int| 0 <= k < s.len() && s[k] == v\n decreases s.len()\n {\n if s.len() == 0 {\n } else {\n Self::count_int_nonneg(s.drop_last(), v);\n if s.last() == v {\n assert(s[s.len() as int - 1] == v);\n } else {\n Self::count_int_positive_exists(s.drop_last(), v);\n let k = choose |k: int| 0 <= k < s.drop_last().len() && s.drop_last()[k] == v;\n assert(s[k] == v);\n }\n }\n }\n\n proof fn count_in_range_additive_int(s: Seq, v: int, a: int, b: int, c: int)\n requires a <= b, b <= c\n ensures Self::count_in_range_int(s, v, a, c)\n == Self::count_in_range_int(s, v, a, b) + Self::count_in_range_int(s, v, b, c)\n decreases b - a\n {\n if a < b {\n Self::count_in_range_additive_int(s, v, a + 1, b, c);\n }\n }\n\n proof fn count_in_range_same_int(s1: Seq, s2: Seq, v: int, start: int, end: int)\n requires\n start <= end,\n forall |i: int| start <= i < end ==> s1[i] == s2[i]\n ensures Self::count_in_range_int(s1, v, start, end)\n == Self::count_in_range_int(s2, v, start, end)\n decreases end - start\n {\n if start < end {\n Self::count_in_range_same_int(s1, s2, v, start + 1, end);\n }\n }\n\n proof fn count_int_eq_count_in_range(s: Seq, v: int)\n ensures Self::count_int(s, v) == Self::count_in_range_int(s, v, 0, s.len() as int)\n decreases s.len()\n {\n if s.len() > 0 {\n Self::count_int_eq_count_in_range(s.drop_last(), v);\n Self::count_in_range_additive_int(s, v, 0, s.len() as int - 1, s.len() as int);\n Self::count_in_range_same_int(s.drop_last(), s, v, 0, s.len() as int - 1);\n assert(Self::count_in_range_int(s, v, s.len() as int, s.len() as int) == 0);\n }\n }\n\n proof fn swap_perm_int(c: Seq, a: Seq, i: int, j: int)\n requires\n Self::is_perm_int(c, a),\n 0 <= i < c.len(),\n 0 <= j < c.len(),\n ensures\n Self::is_perm_int(c.update(i, c[j]).update(j, c[i]), a)\n {\n let c_prime = c.update(i, c[j]).update(j, c[i]);\n if i == j {\n assert(c_prime =~= c);\n } else {\n let n = c.len() as int;\n let lo = if i < j { i } else { j };\n let hi = if i < j { j } else { i };\n assert forall |v: int| Self::count_int(c_prime, v) == Self::count_int(a, v) by {\n Self::count_int_eq_count_in_range(c, v);\n Self::count_int_eq_count_in_range(c_prime, v);\n Self::count_in_range_additive_int(c, v, 0, lo, lo + 1);\n Self::count_in_range_additive_int(c, v, 0, lo + 1, hi);\n Self::count_in_range_additive_int(c, v, 0, hi, hi + 1);\n Self::count_in_range_additive_int(c, v, 0, hi + 1, n);\n Self::count_in_range_additive_int(c_prime, v, 0, lo, lo + 1);\n Self::count_in_range_additive_int(c_prime, v, 0, lo + 1, hi);\n Self::count_in_range_additive_int(c_prime, v, 0, hi, hi + 1);\n Self::count_in_range_additive_int(c_prime, v, 0, hi + 1, n);\n Self::count_in_range_same_int(c, c_prime, v, 0, lo);\n Self::count_in_range_same_int(c, c_prime, v, lo + 1, hi);\n Self::count_in_range_same_int(c, c_prime, v, hi + 1, n);\n assert(Self::count_in_range_int(c, v, 0, lo)\n == Self::count_in_range_int(c_prime, v, 0, lo));\n assert(Self::count_in_range_int(c, v, lo + 1, hi)\n == Self::count_in_range_int(c_prime, v, lo + 1, hi));\n assert(Self::count_in_range_int(c, v, hi + 1, n)\n == Self::count_in_range_int(c_prime, v, hi + 1, n));\n assert(Self::count_in_range_int(c, v, 0, n)\n == Self::count_in_range_int(c, v, 0, lo)\n + Self::count_in_range_int(c, v, lo, lo + 1)\n + Self::count_in_range_int(c, v, lo + 1, hi)\n + Self::count_in_range_int(c, v, hi, hi + 1)\n + Self::count_in_range_int(c, v, hi + 1, n));\n assert(Self::count_in_range_int(c_prime, v, 0, n)\n == Self::count_in_range_int(c_prime, v, 0, lo)\n + Self::count_in_range_int(c_prime, v, lo, lo + 1)\n + Self::count_in_range_int(c_prime, v, lo + 1, hi)\n + Self::count_in_range_int(c_prime, v, hi, hi + 1)\n + Self::count_in_range_int(c_prime, v, hi + 1, n));\n assert(Self::count_in_range_int(c, v, lo + 1, lo + 1) == 0);\n assert(Self::count_in_range_int(c, v, hi + 1, hi + 1) == 0);\n assert(Self::count_in_range_int(c_prime, v, lo + 1, lo + 1) == 0);\n assert(Self::count_in_range_int(c_prime, v, hi + 1, hi + 1) == 0);\n };\n }\n }\n\n proof fn drop_last_perm_int(c: Seq, a: Seq)\n requires\n Self::is_perm_int(c, a),\n c.len() > 0,\n c.last() == a.last(),\n ensures\n Self::is_perm_int(c.drop_last(), a.drop_last())\n {\n assert forall |v: int| Self::count_int(c.drop_last(), v)\n == Self::count_int(a.drop_last(), v) by {\n Self::count_int_nonneg(c.drop_last(), v);\n Self::count_int_nonneg(a.drop_last(), v);\n assert(Self::count_int(c, v)\n == (if c.last() == v { 1int } else { 0int })\n + Self::count_int(c.drop_last(), v));\n assert(Self::count_int(a, v)\n == (if a.last() == v { 1int } else { 0int })\n + Self::count_int(a.drop_last(), v));\n };\n }\n\n proof fn dot_product_drop_last_helper(a: Seq, b: Seq, k: int)\n requires\n a.len() > 0,\n a.len() == b.len(),\n 0 <= k,\n ensures\n Self::dot_product_int(a, b, k)\n == Self::dot_product_int(a.drop_last(), b.drop_last(), k)\n + if k < a.len() as int { a.last() * b.last() } else { 0int }\n decreases a.len() - k\n {\n if k >= a.len() as int {\n } else if k == a.len() as int - 1 {\n assert(a.last() == a[k]);\n assert(b.last() == b[k]);\n assert(a[k] * b[k] == a.last() * b.last()) by(nonlinear_arith)\n requires a.last() == a[k], b.last() == b[k];\n assert(Self::dot_product_int(a, b, k + 1) == 0);\n assert(Self::dot_product_int(a.drop_last(), b.drop_last(), k) == 0);\n } else {\n assert(a.drop_last()[k] == a[k]);\n assert(b.drop_last()[k] == b[k]);\n assert(a.drop_last()[k] * b.drop_last()[k] == a[k] * b[k]) by(nonlinear_arith)\n requires a.drop_last()[k] == a[k], b.drop_last()[k] == b[k];\n Self::dot_product_drop_last_helper(a, b, k + 1);\n }\n }\n\n proof fn dot_product_drop_last_int(a: Seq, b: Seq)\n requires a.len() > 0, a.len() == b.len()\n ensures Self::dot_product_int(a, b, 0)\n == Self::dot_product_int(a.drop_last(), b.drop_last(), 0) + a.last() * b.last()\n {\n Self::dot_product_drop_last_helper(a, b, 0);\n }\n\n proof fn dot_product_update_int(s: Seq, t: Seq, pos: int, val: int, start: int)\n requires s.len() == t.len(), 0 <= pos < s.len(), 0 <= start\n ensures Self::dot_product_int(s.update(pos, val), t, start)\n == Self::dot_product_int(s, t, start)\n + if start <= pos { (val - s[pos]) * t[pos] } else { 0int }\n decreases s.len() - start\n {\n if start >= s.len() as int {\n } else {\n if start == pos {\n assert(val * t[start]\n == s[start] * t[start] + (val - s[start]) * t[start]) by(nonlinear_arith);\n }\n Self::dot_product_update_int(s, t, pos, val, start + 1);\n }\n }\n\n proof fn element_has_positive_count(s: Seq, k: int)\n requires 0 <= k < s.len()\n ensures Self::count_int(s, s[k]) >= 1\n decreases s.len()\n {\n Self::count_int_nonneg(s.drop_last(), s[k]);\n if k < s.len() as int - 1 {\n Self::element_has_positive_count(s.drop_last(), k);\n }\n }\n\n proof fn perm_max(a: Seq, c: Seq)\n requires Self::is_sorted_int(a), Self::is_perm_int(c, a), a.len() > 0\n ensures forall |k: int| 0 <= k < a.len() ==> c[k] <= a.last()\n {\n assert forall |k: int| 0 <= k < a.len() implies c[k] <= a.last() by {\n Self::element_has_positive_count(c, k);\n if c[k] > a.last() {\n Self::count_int_positive_exists(a, c[k]);\n }\n };\n }\n\n proof fn rearrangement_ineq(a: Seq, b: Seq, c: Seq)\n requires\n a.len() == b.len(), a.len() == c.len(),\n Self::is_sorted_int(a), Self::is_sorted_int(b),\n Self::is_perm_int(c, a),\n ensures Self::dot_product_int(a, b, 0) >= Self::dot_product_int(c, b, 0)\n decreases a.len()\n {\n if a.len() == 0 {\n assert(Self::dot_product_int(a, b, 0) == 0);\n assert(Self::dot_product_int(c, b, 0) == 0);\n } else if a.len() == 1 {\n Self::element_has_positive_count(a, 0);\n assert(Self::count_int(c, a[0]) >= 1);\n Self::count_int_positive_exists(c, a[0]);\n let w = choose |w: int| 0 <= w < c.len() && c[w] == a[0];\n assert(c[0] == a[0]);\n assert(a[0] * b[0] == c[0] * b[0]) by(nonlinear_arith)\n requires a[0] == c[0];\n assert(Self::dot_product_int(a, b, 1) == 0);\n assert(Self::dot_product_int(c, b, 1) == 0);\n assert(Self::dot_product_int(a, b, 0) == a[0] * b[0]);\n assert(Self::dot_product_int(c, b, 0) == c[0] * b[0]);\n } else {\n let n = a.len() as int;\n Self::count_int_nonneg(c, a.last());\n assert(Self::count_int(c, a.last()) >= 1) by {\n assert(Self::count_int(a, a.last()) >= 1) by {\n Self::count_int_nonneg(a.drop_last(), a.last());\n };\n };\n Self::count_int_positive_exists(c, a.last());\n let k = choose |k: int| 0 <= k < n && c[k] == a.last();\n if k == n - 1 {\n Self::drop_last_perm_int(c, a);\n Self::dot_product_drop_last_int(a, b);\n Self::dot_product_drop_last_int(c, b);\n Self::rearrangement_ineq(a.drop_last(), b.drop_last(), c.drop_last());\n assert(a.last() * b.last() == c.last() * b.last()) by(nonlinear_arith)\n requires c.last() == a.last();\n assert(Self::dot_product_int(a.drop_last(), b.drop_last(), 0)\n >= Self::dot_product_int(c.drop_last(), b.drop_last(), 0));\n assert(Self::dot_product_int(a, b, 0) ==\n Self::dot_product_int(a.drop_last(), b.drop_last(), 0) + a.last() * b.last());\n assert(Self::dot_product_int(c, b, 0) ==\n Self::dot_product_int(c.drop_last(), b.drop_last(), 0) + c.last() * b.last());\n assert(Self::dot_product_int(a, b, 0) >= Self::dot_product_int(c, b, 0));\n } else {\n let c_prime = c.update(k, c[n - 1]).update(n - 1, c[k]);\n Self::dot_product_update_int(c, b, k, c[n - 1], 0);\n Self::dot_product_update_int(c.update(k, c[n - 1]), b, n - 1, c[k], 0);\n Self::perm_max(a, c);\n assert(c[n - 1] <= a[n - 1]);\n assert(c[k] == a[n - 1]);\n assert(b[n - 1] >= b[k]);\n assert((a[n - 1] - c[n - 1]) * (b[n - 1] - b[k]) >= 0) by(nonlinear_arith)\n requires a[n - 1] >= c[n - 1], b[n - 1] >= b[k];\n assert(Self::dot_product_int(c_prime, b, 0) >= Self::dot_product_int(c, b, 0)) by {\n assert(Self::dot_product_int(c_prime, b, 0)\n == Self::dot_product_int(c, b, 0)\n + (c[n - 1] - c[k]) * b[k]\n + (c[k] - c.update(k, c[n - 1])[n - 1]) * b[n - 1]);\n assert(c.update(k, c[n - 1])[n - 1] == c[n - 1]);\n assert(Self::dot_product_int(c_prime, b, 0)\n == Self::dot_product_int(c, b, 0)\n + (c[n - 1] - a[n - 1]) * b[k]\n + (a[n - 1] - c[n - 1]) * b[n - 1]);\n assert((c[n - 1] - a[n - 1]) * b[k] + (a[n - 1] - c[n - 1]) * b[n - 1]\n == (a[n - 1] - c[n - 1]) * (b[n - 1] - b[k])) by(nonlinear_arith);\n assert((a[n - 1] - c[n - 1]) * (b[n - 1] - b[k]) >= 0) by(nonlinear_arith)\n requires a[n - 1] >= c[n - 1], b[n - 1] >= b[k];\n };\n Self::swap_perm_int(c, a, k, n - 1);\n assert(c_prime.last() == a.last());\n Self::drop_last_perm_int(c_prime, a);\n Self::dot_product_drop_last_int(a, b);\n Self::dot_product_drop_last_int(c_prime, b);\n Self::rearrangement_ineq(a.drop_last(), b.drop_last(), c_prime.drop_last());\n assert(a.last() * b.last() == c_prime.last() * b.last()) by(nonlinear_arith)\n requires c_prime.last() == a.last();\n assert(Self::dot_product_int(a.drop_last(), b.drop_last(), 0)\n >= Self::dot_product_int(c_prime.drop_last(), b.drop_last(), 0));\n assert(Self::dot_product_int(a, b, 0) ==\n Self::dot_product_int(a.drop_last(), b.drop_last(), 0) + a.last() * b.last());\n assert(Self::dot_product_int(c_prime, b, 0) ==\n Self::dot_product_int(c_prime.drop_last(), b.drop_last(), 0) + c_prime.last() * b.last());\n assert(Self::dot_product_int(a, b, 0) >= Self::dot_product_int(c_prime, b, 0));\n assert(Self::dot_product_int(c_prime, b, 0) >= Self::dot_product_int(c, b, 0));\n assert(Self::dot_product_int(a, b, 0) >= Self::dot_product_int(c, b, 0));\n }\n }\n }\n\n proof fn dot_product_nonneg(a: Seq, b: Seq, k: int)\n requires\n a.len() == b.len(),\n forall |i: int| 0 <= i < a.len() ==> a[i] >= 0,\n forall |i: int| 0 <= i < b.len() ==> b[i] >= 0,\n 0 <= k,\n ensures Self::dot_product_int(a, b, k) >= 0\n decreases a.len() - k\n {\n if k < a.len() as int {\n Self::dot_product_nonneg(a, b, k + 1);\n }\n }\n\n proof fn mod_add_lemma(x: int, y: int, m: int)\n requires m > 0, 0 <= x < m, 0 <= y\n ensures (x + y) % m >= 0, (x + y) % m < m\n {}\n\n \n\n proof fn count_occ_nonneg(s: Seq, val: i32)\n ensures Self::count_occ(s, val) >= 0\n decreases s.len()\n {\n if s.len() > 0 { Self::count_occ_nonneg(s.drop_last(), val); }\n }\n\n proof fn count_occ_push(s: Seq, val: i32, query: i32)\n ensures Self::count_occ(s.push(val), query)\n == Self::count_occ(s, query) + if val == query { 1int } else { 0int }\n {\n assert(s.push(val).drop_last() =~= s);\n }\n\n proof fn count_occ_append(a: Seq, b: Seq, val: i32)\n ensures Self::count_occ(a + b, val)\n == Self::count_occ(a, val) + Self::count_occ(b, val)\n decreases b.len()\n {\n if b.len() == 0 {\n assert(a + b =~= a);\n } else {\n assert((a + b).drop_last() =~= a + b.drop_last());\n Self::count_occ_append(a, b.drop_last(), val);\n }\n }\n\n proof fn count_occ_contains(s: Seq, idx: int)\n requires 0 <= idx < s.len()\n ensures Self::count_occ(s, s[idx]) >= 1\n decreases s.len()\n {\n Self::count_occ_nonneg(s.drop_last(), s[idx]);\n if idx < s.len() - 1 {\n Self::count_occ_contains(s.drop_last(), idx);\n }\n }\n\n proof fn find_occ_pos(s: Seq, val: i32) -> (pos: int)\n requires Self::count_occ(s, val) >= 1\n ensures 0 <= pos < s.len(), s[pos] == val\n decreases s.len()\n {\n Self::count_occ_nonneg(s.drop_last(), val);\n if s.last() == val { (s.len() - 1) as int }\n else { Self::find_occ_pos(s.drop_last(), val) }\n }\n\n proof fn perm_refl(s: Seq)\n ensures Self::is_perm(s, s)\n {}\n\n proof fn perm_trans(a: Seq, b: Seq, c: Seq)\n requires Self::is_perm(a, b), Self::is_perm(b, c)\n ensures Self::is_perm(a, c)\n {}\n\n proof fn perm_append(a1: Seq, a2: Seq, b1: Seq, b2: Seq)\n requires Self::is_perm(a1, b1), Self::is_perm(a2, b2)\n ensures Self::is_perm(a1 + a2, b1 + b2)\n {\n assert forall |v: i32|\n Self::count_occ(a1 + a2, v) == Self::count_occ(b1 + b2, v) by {\n Self::count_occ_append(a1, a2, v);\n Self::count_occ_append(b1, b2, v);\n };\n }\n\n proof fn perm_preserves_bounds(a: Seq, b: Seq, lo: i32, hi: i32)\n requires\n Self::is_perm(a, b),\n forall |i: int| 0 <= i < b.len() ==> lo <= #[trigger] b[i] <= hi,\n ensures\n forall |i: int| 0 <= i < a.len() ==> lo <= #[trigger] a[i] <= hi,\n {\n assert forall |i: int| 0 <= i < a.len()\n implies lo <= #[trigger] a[i] <= hi by {\n Self::count_occ_contains(a, i);\n let _ = Self::find_occ_pos(b, a[i]);\n };\n }\n\n \n\n proof fn count_occ_i64_nonneg(s: Seq, val: i64)\n ensures Self::count_occ_i64(s, val) >= 0\n decreases s.len()\n {\n if s.len() > 0 { Self::count_occ_i64_nonneg(s.drop_last(), val); }\n }\n\n proof fn count_occ_i64_push(s: Seq, val: i64, query: i64)\n ensures Self::count_occ_i64(s.push(val), query)\n == Self::count_occ_i64(s, query) + if val == query { 1int } else { 0int }\n {\n assert(s.push(val).drop_last() =~= s);\n }\n\n proof fn count_occ_i64_append(a: Seq, b: Seq, val: i64)\n ensures Self::count_occ_i64(a + b, val)\n == Self::count_occ_i64(a, val) + Self::count_occ_i64(b, val)\n decreases b.len()\n {\n if b.len() == 0 {\n assert(a + b =~= a);\n } else {\n assert((a + b).drop_last() =~= a + b.drop_last());\n Self::count_occ_i64_append(a, b.drop_last(), val);\n }\n }\n\n proof fn count_occ_i64_contains(s: Seq, idx: int)\n requires 0 <= idx < s.len()\n ensures Self::count_occ_i64(s, s[idx]) >= 1\n decreases s.len()\n {\n Self::count_occ_i64_nonneg(s.drop_last(), s[idx]);\n if idx < s.len() - 1 {\n Self::count_occ_i64_contains(s.drop_last(), idx);\n }\n }\n\n proof fn find_occ_pos_i64(s: Seq, val: i64) -> (pos: int)\n requires Self::count_occ_i64(s, val) >= 1\n ensures 0 <= pos < s.len(), s[pos] == val\n decreases s.len()\n {\n Self::count_occ_i64_nonneg(s.drop_last(), val);\n if s.last() == val { (s.len() - 1) as int }\n else { Self::find_occ_pos_i64(s.drop_last(), val) }\n }\n\n proof fn perm_i64_refl(s: Seq)\n ensures Self::is_perm_i64(s, s)\n {}\n\n proof fn perm_i64_trans(a: Seq, b: Seq, c: Seq)\n requires Self::is_perm_i64(a, b), Self::is_perm_i64(b, c)\n ensures Self::is_perm_i64(a, c)\n {}\n\n proof fn perm_i64_append(a1: Seq, a2: Seq, b1: Seq, b2: Seq)\n requires Self::is_perm_i64(a1, b1), Self::is_perm_i64(a2, b2)\n ensures Self::is_perm_i64(a1 + a2, b1 + b2)\n {\n assert forall |v: i64|\n Self::count_occ_i64(a1 + a2, v) == Self::count_occ_i64(b1 + b2, v) by {\n Self::count_occ_i64_append(a1, a2, v);\n Self::count_occ_i64_append(b1, b2, v);\n };\n }\n\n proof fn perm_i64_preserves_bounds(a: Seq, b: Seq, lo: i64, hi: i64)\n requires\n Self::is_perm_i64(a, b),\n forall |i: int| 0 <= i < b.len() ==> lo <= #[trigger] b[i] <= hi,\n ensures\n forall |i: int| 0 <= i < a.len() ==> lo <= #[trigger] a[i] <= hi,\n {\n assert forall |i: int| 0 <= i < a.len()\n implies lo <= #[trigger] a[i] <= hi by {\n Self::count_occ_i64_contains(a, i);\n let _ = Self::find_occ_pos_i64(b, a[i]);\n };\n }\n\n \n\n proof fn count_relates_i32(s: Seq, v: int)\n ensures Self::count_int(Self::to_int_seq(s), v) ==\n (if i32::MIN as int <= v <= i32::MAX as int {\n Self::count_occ(s, v as i32)\n } else { 0int })\n decreases s.len()\n {\n if s.len() > 0 {\n Self::count_relates_i32(s.drop_last(), v);\n assert(Self::to_int_seq(s).drop_last() =~= Self::to_int_seq(s.drop_last()));\n }\n }\n\n proof fn perm_implies_perm_int(a: Seq, b: Seq)\n requires Self::is_perm(a, b)\n ensures Self::is_perm_int(Self::to_int_seq(a), Self::to_int_seq(b))\n {\n assert forall |v: int|\n Self::count_int(Self::to_int_seq(a), v)\n == Self::count_int(Self::to_int_seq(b), v)\n by {\n Self::count_relates_i32(a, v);\n Self::count_relates_i32(b, v);\n };\n }\n\n proof fn count_relates_i64(s: Seq, v: int)\n ensures Self::count_int(Self::to_int_seq_i64(s), v) ==\n (if i64::MIN as int <= v <= i64::MAX as int {\n Self::count_occ_i64(s, v as i64)\n } else { 0int })\n decreases s.len()\n {\n if s.len() > 0 {\n Self::count_relates_i64(s.drop_last(), v);\n assert(Self::to_int_seq_i64(s).drop_last() =~= Self::to_int_seq_i64(s.drop_last()));\n }\n }\n\n proof fn perm_i64_implies_perm_int(a: Seq, b: Seq)\n requires Self::is_perm_i64(a, b)\n ensures Self::is_perm_int(Self::to_int_seq_i64(a), Self::to_int_seq_i64(b))\n {\n assert forall |v: int|\n Self::count_int(Self::to_int_seq_i64(a), v)\n == Self::count_int(Self::to_int_seq_i64(b), v)\n by {\n Self::count_relates_i64(a, v);\n Self::count_relates_i64(b, v);\n };\n }\n\n \n\n fn ms_merge(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::is_sorted(a@), Self::is_sorted(b@)\n ensures\n Self::is_sorted(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_perm(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n invariant\n 0 <= i <= a.len(), 0 <= j <= b.len(),\n Self::is_sorted(a@), Self::is_sorted(b@),\n Self::is_sorted(result@),\n result@.len() == i + j,\n Self::is_perm(result@,\n a@.subrange(0, i as int) + b@.subrange(0, j as int)),\n i < a.len() ==> (forall |k: int|\n 0 <= k < result@.len() ==> result[k] <= a[i as int]),\n j < b.len() ==> (forall |k: int|\n 0 <= k < result@.len() ==> result[k] <= b[j as int]),\n decreases a.len() - i + b.len() - j,\n {\n let ghost old_result = result@;\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n proof {\n assert(Self::is_sorted(result@)) by {\n assert forall |p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q] by {\n if q < old_result.len() as int {\n } else if p < old_result.len() as int {\n assert(result[q] == a[i as int]);\n }\n };\n };\n let new_a_prefix = a@.subrange(0, (i + 1) as int);\n let old_a_prefix = a@.subrange(0, i as int);\n let b_prefix = b@.subrange(0, j as int);\n assert(new_a_prefix =~= old_a_prefix.push(a[i as int]));\n assert(result@ =~= old_result.push(a[i as int]));\n assert forall |v: i32|\n Self::count_occ(result@, v)\n == Self::count_occ(new_a_prefix + b_prefix, v) by {\n Self::count_occ_push(old_result, a[i as int], v);\n Self::count_occ_append(new_a_prefix, b_prefix, v);\n Self::count_occ_push(old_a_prefix, a[i as int], v);\n Self::count_occ_append(old_a_prefix, b_prefix, v);\n };\n }\n i = i + 1;\n } else {\n result.push(b[j]);\n proof {\n assert(Self::is_sorted(result@)) by {\n assert forall |p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q] by {\n if q < old_result.len() as int {\n } else if p < old_result.len() as int {\n assert(result[q] == b[j as int]);\n }\n };\n };\n let a_prefix = a@.subrange(0, i as int);\n let new_b_prefix = b@.subrange(0, (j + 1) as int);\n let old_b_prefix = b@.subrange(0, j as int);\n assert(new_b_prefix =~= old_b_prefix.push(b[j as int]));\n assert(result@ =~= old_result.push(b[j as int]));\n assert forall |v: i32|\n Self::count_occ(result@, v)\n == Self::count_occ(a_prefix + new_b_prefix, v) by {\n Self::count_occ_push(old_result, b[j as int], v);\n Self::count_occ_append(a_prefix, new_b_prefix, v);\n Self::count_occ_push(old_b_prefix, b[j as int], v);\n Self::count_occ_append(a_prefix, old_b_prefix, v);\n };\n }\n j = j + 1;\n }\n }\n proof {\n assert(a@.subrange(0, a@.len() as int) =~= a@);\n assert(b@.subrange(0, b@.len() as int) =~= b@);\n }\n result\n }\n\n fn ms_sort(input: &Vec) -> (result: Vec)\n ensures\n Self::is_sorted(result@),\n result@.len() == input@.len(),\n Self::is_perm(result@, input@),\n decreases input.len(),\n {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n proof { assert(result@ =~= input@); Self::perm_refl(input@); }\n } else {\n proof { assert(result@ =~= input@); Self::perm_refl(input@); }\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n invariant\n 0 <= i <= mid, mid <= n, n == input.len(),\n left.len() == i,\n forall |k: int| 0 <= k < i as int ==> left[k] == input[k],\n decreases mid - i,\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n invariant\n mid <= j <= n, n == input.len(),\n right.len() == j - mid,\n forall |k: int| 0 <= k < (j - mid) as int\n ==> right[k] == input[k + mid as int],\n decreases n - j,\n {\n right.push(input[j]);\n j = j + 1;\n }\n proof {\n assert(left@ =~= input@.subrange(0, mid as int));\n assert(right@ =~= input@.subrange(mid as int, n as int));\n assert(input@ =~= left@ + right@);\n Self::perm_refl(input@);\n }\n let sorted_left = Self::ms_sort(&left);\n let sorted_right = Self::ms_sort(&right);\n let result = Self::ms_merge(&sorted_left, &sorted_right);\n proof {\n Self::perm_append(sorted_left@, sorted_right@, left@, right@);\n Self::perm_trans(result@, sorted_left@ + sorted_right@, left@ + right@);\n assert(left@ + right@ =~= input@);\n }\n result\n }\n\n \n\n fn ms_merge_i64(a: &Vec, b: &Vec) -> (result: Vec)\n requires Self::is_sorted_i64(a@), Self::is_sorted_i64(b@)\n ensures\n Self::is_sorted_i64(result@),\n result@.len() == a@.len() + b@.len(),\n Self::is_perm_i64(result@, a@ + b@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len()\n invariant\n 0 <= i <= a.len(), 0 <= j <= b.len(),\n Self::is_sorted_i64(a@), Self::is_sorted_i64(b@),\n Self::is_sorted_i64(result@),\n result@.len() == i + j,\n Self::is_perm_i64(result@,\n a@.subrange(0, i as int) + b@.subrange(0, j as int)),\n i < a.len() ==> (forall |k: int|\n 0 <= k < result@.len() ==> result[k] <= a[i as int]),\n j < b.len() ==> (forall |k: int|\n 0 <= k < result@.len() ==> result[k] <= b[j as int]),\n decreases a.len() - i + b.len() - j,\n {\n let ghost old_result = result@;\n if i < a.len() && (j >= b.len() || a[i] <= b[j]) {\n result.push(a[i]);\n proof {\n assert(Self::is_sorted_i64(result@)) by {\n assert forall |p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q] by {\n if q < old_result.len() as int {\n } else if p < old_result.len() as int {\n assert(result[q] == a[i as int]);\n }\n };\n };\n let new_a_prefix = a@.subrange(0, (i + 1) as int);\n let old_a_prefix = a@.subrange(0, i as int);\n let b_prefix = b@.subrange(0, j as int);\n assert(new_a_prefix =~= old_a_prefix.push(a[i as int]));\n assert(result@ =~= old_result.push(a[i as int]));\n assert forall |v: i64|\n Self::count_occ_i64(result@, v)\n == Self::count_occ_i64(new_a_prefix + b_prefix, v) by {\n Self::count_occ_i64_push(old_result, a[i as int], v);\n Self::count_occ_i64_append(new_a_prefix, b_prefix, v);\n Self::count_occ_i64_push(old_a_prefix, a[i as int], v);\n Self::count_occ_i64_append(old_a_prefix, b_prefix, v);\n };\n }\n i = i + 1;\n } else {\n result.push(b[j]);\n proof {\n assert(Self::is_sorted_i64(result@)) by {\n assert forall |p: int, q: int| 0 <= p <= q < result@.len()\n implies result[p] <= result[q] by {\n if q < old_result.len() as int {\n } else if p < old_result.len() as int {\n assert(result[q] == b[j as int]);\n }\n };\n };\n let a_prefix = a@.subrange(0, i as int);\n let new_b_prefix = b@.subrange(0, (j + 1) as int);\n let old_b_prefix = b@.subrange(0, j as int);\n assert(new_b_prefix =~= old_b_prefix.push(b[j as int]));\n assert(result@ =~= old_result.push(b[j as int]));\n assert forall |v: i64|\n Self::count_occ_i64(result@, v)\n == Self::count_occ_i64(a_prefix + new_b_prefix, v) by {\n Self::count_occ_i64_push(old_result, b[j as int], v);\n Self::count_occ_i64_append(a_prefix, new_b_prefix, v);\n Self::count_occ_i64_push(old_b_prefix, b[j as int], v);\n Self::count_occ_i64_append(a_prefix, old_b_prefix, v);\n };\n }\n j = j + 1;\n }\n }\n proof {\n assert(a@.subrange(0, a@.len() as int) =~= a@);\n assert(b@.subrange(0, b@.len() as int) =~= b@);\n }\n result\n }\n\n fn ms_sort_i64(input: &Vec) -> (result: Vec)\n ensures\n Self::is_sorted_i64(result@),\n result@.len() == input@.len(),\n Self::is_perm_i64(result@, input@),\n decreases input.len(),\n {\n let n = input.len();\n if n <= 1 {\n let mut result = Vec::new();\n if n == 1 {\n result.push(input[0]);\n proof { assert(result@ =~= input@); Self::perm_i64_refl(input@); }\n } else {\n proof { assert(result@ =~= input@); Self::perm_i64_refl(input@); }\n }\n return result;\n }\n let mid = n / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n invariant\n 0 <= i <= mid, mid <= n, n == input.len(),\n left.len() == i,\n forall |k: int| 0 <= k < i as int ==> left[k] == input[k],\n decreases mid - i,\n {\n left.push(input[i]);\n i = i + 1;\n }\n let mut right: Vec = Vec::new();\n let mut j: usize = mid;\n while j < n\n invariant\n mid <= j <= n, n == input.len(),\n right.len() == j - mid,\n forall |k: int| 0 <= k < (j - mid) as int\n ==> right[k] == input[k + mid as int],\n decreases n - j,\n {\n right.push(input[j]);\n j = j + 1;\n }\n proof {\n assert(left@ =~= input@.subrange(0, mid as int));\n assert(right@ =~= input@.subrange(mid as int, n as int));\n assert(input@ =~= left@ + right@);\n Self::perm_i64_refl(input@);\n }\n let sorted_left = Self::ms_sort_i64(&left);\n let sorted_right = Self::ms_sort_i64(&right);\n let result = Self::ms_merge_i64(&sorted_left, &sorted_right);\n proof {\n Self::perm_i64_append(sorted_left@, sorted_right@, left@, right@);\n Self::perm_i64_trans(result@, sorted_left@ + sorted_right@, left@ + right@);\n assert(left@ + right@ =~= input@);\n }\n result\n }\n\n \n\n pub fn max_sum_range_query(nums: Vec, requests: Vec>) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==>\n 0 <= #[trigger] nums@[i] <= 100_000,\n 1 <= requests@.len() <= 100_000,\n forall |i: int| 0 <= i < requests@.len() ==> (\n (#[trigger] requests@[i])@.len() == 2\n && 0 <= requests@[i]@[0]\n && requests@[i]@[0] <= requests@[i]@[1]\n && (requests@[i]@[1] as int) < nums@.len() as int\n ),\n ensures\n 0 <= result < 1_000_000_007,\n exists |sv: Seq, sf: Seq|\n sv.len() == nums@.len()\n && sf.len() == nums@.len()\n && Self::is_sorted_int(sv)\n && Self::is_sorted_int(sf)\n && Self::is_perm_int(sv, Self::to_int_seq(nums@))\n && Self::is_perm_int(sf, Self::freq_vec(requests@, nums@.len() as int))\n && result as int\n == Self::dot_product_int(sv, sf, 0) % 1_000_000_007\n && forall |c: Seq|\n c.len() == sv.len() && Self::is_perm_int(c, sv)\n ==> Self::dot_product_int(sv, sf, 0)\n >= Self::dot_product_int(c, sf, 0),\n {\n let ghost orig_nums_int = Self::to_int_seq(nums@);\n let n = nums.len();\n let m = requests.len();\n let modval: i64 = 1_000_000_007;\n\n\n let mut diff: Vec = Vec::new();\n let mut z: usize = 0;\n while z < n\n invariant\n diff@.len() == z as int,\n 0 <= z <= n,\n forall |pos: int| 0 <= pos < z as int ==>\n (#[trigger] diff@[pos]) as int == Self::diff_at(requests@, pos, 0),\n decreases n - z,\n {\n diff.push(0);\n z = z + 1;\n }\n\n let mut r: usize = 0;\n while r < m\n invariant\n n == nums@.len(),\n m == requests@.len(),\n diff@.len() == n as int,\n 0 <= r <= m,\n 1 <= n <= 100_000,\n 1 <= m <= 100_000,\n forall |pos: int| 0 <= pos < n as int ==>\n (#[trigger] diff@[pos]) as int == Self::diff_at(requests@, pos, r as int),\n forall |pos: int| 0 <= pos < n as int ==> -100_000 <= #[trigger] diff@[pos] <= 100_000,\n forall |ii: int| 0 <= ii < requests@.len() ==> (\n (#[trigger] requests@[ii])@.len() == 2\n && 0 <= requests@[ii]@[0]\n && requests@[ii]@[0] <= requests@[ii]@[1]\n && (requests@[ii]@[1] as int) < nums@.len() as int\n ),\n decreases m - r,\n {\n let s = requests[r][0] as usize;\n let e = requests[r][1] as usize;\n let ghost diff_before = diff@;\n diff.set(s, diff[s] + 1);\n proof {\n assert(diff@ =~= diff_before.update(s as int, (diff_before[s as int] + 1) as i64));\n }\n if e + 1 < n {\n let ghost diff_before2 = diff@;\n diff.set(e + 1, diff[e + 1] - 1);\n proof {\n assert(diff@ =~= diff_before2.update(e as int + 1, (diff_before2[e as int + 1] - 1) as i64));\n }\n }\n proof {\n assert forall |pos: int| 0 <= pos < n as int implies\n (#[trigger] diff@[pos]) as int == Self::diff_at(requests@, pos, r as int + 1) by {\n assert(Self::diff_at(requests@, pos, r as int + 1)\n == Self::diff_at(requests@, pos, r as int) + Self::diff_delta(requests@, pos, r as int));\n if pos == s as int {\n assert(requests@[r as int]@[0] as int == pos);\n } else if pos == e as int + 1 {\n assert(requests@[r as int]@[1] as int + 1 == pos);\n assert(requests@[r as int]@[0] as int != pos);\n } else {\n assert(requests@[r as int]@[0] as int != pos);\n assert(requests@[r as int]@[1] as int + 1 != pos);\n }\n }\n assert forall |pos: int| 0 <= pos < n as int implies\n -100_000 <= #[trigger] diff@[pos] <= 100_000 by {\n Self::lemma_diff_at_bound(requests@, pos, r as int + 1);\n }\n }\n r = r + 1;\n }\n\n let mut count: Vec = Vec::new();\n let mut running: i64 = 0;\n let mut idx: usize = 0;\n while idx < n\n invariant\n n == nums@.len(),\n m == requests@.len(),\n diff@.len() == n as int,\n count@.len() == idx as int,\n 0 <= idx <= n,\n 1 <= n <= 100_000,\n 1 <= m <= 100_000,\n forall |pos: int| 0 <= pos < n as int ==>\n (#[trigger] diff@[pos]) as int == Self::diff_at(requests@, pos, m as int),\n running as int == Self::prefix_diff(requests@, idx as int - 1, m as int),\n 0 <= running <= m as i64,\n forall |k: int| 0 <= k < idx as int ==>\n (#[trigger] count@[k]) as int == Self::prefix_diff(requests@, k, m as int),\n forall |ii: int| 0 <= ii < requests@.len() ==> (\n (#[trigger] requests@[ii])@.len() == 2\n && 0 <= requests@[ii]@[0]\n && requests@[ii]@[0] <= requests@[ii]@[1]\n && (requests@[ii]@[1] as int) < nums@.len() as int\n ),\n decreases n - idx,\n {\n proof {\n assert(Self::prefix_diff(requests@, idx as int, m as int)\n == Self::prefix_diff(requests@, idx as int - 1, m as int) + Self::diff_at(requests@, idx as int, m as int));\n Self::lemma_prefix_diff_freq(requests@, idx as int);\n Self::freq_at_bounded(requests@, idx as int, 0);\n assert(0 <= Self::prefix_diff(requests@, idx as int, m as int) <= m as int);\n }\n running = running + diff[idx];\n count.push(running);\n idx = idx + 1;\n }\n proof {\n assert(count@.len() == n as int);\n assert forall |k: int| 0 <= k < n as int implies\n (#[trigger] count@[k]) as int == Self::freq_at(requests@, k, 0) by {\n Self::lemma_prefix_diff_freq(requests@, k);\n }\n assert forall |k: int| 0 <= k < n as int implies\n 0 <= (#[trigger] count@[k]) as int <= m as int by {\n Self::lemma_prefix_diff_freq(requests@, k);\n Self::freq_at_bounded(requests@, k, 0);\n }\n assert forall |k: int| 0 <= k < n as int implies\n Self::to_int_seq_i64(count@)[k] == Self::freq_vec(requests@, n as int)[k] by {};\n assert(Self::to_int_seq_i64(count@) =~= Self::freq_vec(requests@, n as int));\n }\n let ghost orig_count_int = Self::to_int_seq_i64(count@);\n\n \n let ghost pre_sort_nums = nums@;\n let nums = Self::ms_sort(&nums);\n\n \n let sorted_count = Self::ms_sort_i64(&count);\n\n let ghost sv = Self::to_int_seq(nums@);\n let ghost sf = Self::to_int_seq_i64(sorted_count@);\n\n proof {\n \n assert(Self::is_sorted_int(sv)) by {\n assert forall |a: int, b: int| 0 <= a <= b < sv.len()\n implies sv[a] <= sv[b] by {};\n };\n \n assert(Self::is_sorted_int(sf)) by {\n assert forall |a: int, b: int| 0 <= a <= b < sf.len()\n implies sf[a] <= sf[b] by {};\n };\n\n \n assert(sv.len() == n as int);\n assert(sf.len() == n as int);\n\n \n Self::perm_implies_perm_int(nums@, pre_sort_nums);\n assert(Self::to_int_seq(pre_sort_nums) == orig_nums_int);\n\n \n Self::perm_i64_implies_perm_int(sorted_count@, count@);\n assert(Self::to_int_seq_i64(count@) == orig_count_int);\n assert(orig_count_int =~= Self::freq_vec(requests@, n as int));\n\n \n Self::perm_preserves_bounds(nums@, pre_sort_nums, 0i32, 100_000i32);\n Self::perm_i64_preserves_bounds(sorted_count@, count@, 0i64, m as i64);\n\n assert forall |ii: int| 0 <= ii < n as int implies sv[ii] <= 100_000 by {};\n assert forall |ii: int| 0 <= ii < n as int implies sf[ii] <= 100_000 by {\n assert(sorted_count@[ii] <= m as i64);\n };\n assert forall |ii: int| 0 <= ii < n as int\n implies (nums@[ii] as i64 as int) * (#[trigger] sorted_count@[ii] as int) <= 10_000_000_000 by {\n assert(0 <= nums@[ii] as int <= 100_000);\n assert(sv[ii] == nums@[ii] as int);\n assert(sf[ii] == sorted_count@[ii] as int);\n assert(0 <= sf[ii] <= 100_000);\n assert(nums@[ii] as i64 as int == nums@[ii] as int);\n assert((nums@[ii] as i64 as int) * (sorted_count@[ii] as int) <= 10_000_000_000) by(nonlinear_arith)\n requires 0 <= nums@[ii] as i64 as int <= 100_000, 0 <= sorted_count@[ii] as int <= 100_000;\n };\n }\n\n \n let mut result: i64 = 0;\n let mut k: usize = 0;\n while k < n\n invariant\n n == nums@.len(),\n n == sorted_count@.len(),\n 0 <= k <= n,\n 0 <= result < 1_000_000_007,\n modval == 1_000_000_007i64,\n 1 <= m <= 100_000,\n sv == Self::to_int_seq(nums@),\n sf == Self::to_int_seq_i64(sorted_count@),\n sv.len() == n as int,\n sf.len() == n as int,\n result as int == (Self::dot_product_int(sv, sf, 0)\n - Self::dot_product_int(sv, sf, k as int)) % 1_000_000_007,\n Self::dot_product_int(sv, sf, 0)\n - Self::dot_product_int(sv, sf, k as int) >= 0,\n forall |ii: int| 0 <= ii < n as int ==> #[trigger] sv[ii] >= 0,\n forall |ii: int| 0 <= ii < n as int ==> #[trigger] sf[ii] >= 0,\n forall |ii: int| 0 <= ii < n as int ==> sv[ii] <= 100_000,\n forall |ii: int| 0 <= ii < n as int ==> sf[ii] <= 100_000,\n forall |ii: int| 0 <= ii < n as int ==> 0 <= #[trigger] nums@[ii] <= 100_000,\n forall |ii: int| 0 <= ii < n as int ==> 0 <= #[trigger] sorted_count@[ii] <= m as i64,\n forall |ii: int| 0 <= ii < n as int ==> (nums@[ii] as i64 as int) * (#[trigger] sorted_count@[ii] as int) <= 10_000_000_000,\n decreases n - k,\n {\n proof {\n assert(sv[k as int] >= 0);\n assert(sf[k as int] >= 0);\n assert(sv[k as int] <= 100_000);\n assert(sf[k as int] <= 100_000);\n assert(sv[k as int] * sf[k as int] >= 0) by(nonlinear_arith)\n requires sv[k as int] >= 0, sf[k as int] >= 0;\n assert(sv[k as int] * sf[k as int] <= 100_000 * 100_000) by(nonlinear_arith)\n requires 0 <= sv[k as int] <= 100_000, 0 <= sf[k as int] <= 100_000;\n let partial_before = Self::dot_product_int(sv, sf, 0)\n - Self::dot_product_int(sv, sf, k as int);\n let term = sv[k as int] * sf[k as int];\n assert(term >= 0);\n assert(term <= 10_000_000_000);\n assert(Self::dot_product_int(sv, sf, k as int) == term + Self::dot_product_int(sv, sf, k as int + 1));\n Self::dot_product_nonneg(sv, sf, k as int + 1);\n assert(sv[k as int] == nums@[k as int] as int);\n assert(sf[k as int] == sorted_count@[k as int] as int);\n assert(nums@[k as int] as i64 as int == sv[k as int]);\n assert(0 <= (nums@[k as int] as i64 as int) * (sorted_count@[k as int] as int)) by(nonlinear_arith)\n requires nums@[k as int] as i64 as int >= 0, sorted_count@[k as int] as int >= 0;\n assert((nums@[k as int] as i64 as int) * (sorted_count@[k as int] as int) <= 10_000_000_000) by(nonlinear_arith)\n requires 0 <= nums@[k as int] as i64 as int <= 100_000,\n 0 <= sorted_count@[k as int] as int <= 100_000;\n assert(result as int + (nums@[k as int] as i64 as int) * (sorted_count@[k as int] as int) >= 0);\n assert(result as int + (nums@[k as int] as i64 as int) * (sorted_count@[k as int] as int) <= 11_000_000_006);\n assert((nums@[k as int] as i64 as int) * (sorted_count@[k as int] as int) <= i64::MAX as int);\n assert(result as int + (nums@[k as int] as i64 as int) * (sorted_count@[k as int] as int) <= i64::MAX as int);\n assert(result as int + term\n == partial_before % 1_000_000_007 + term);\n assert((partial_before % 1_000_000_007 + term) % 1_000_000_007\n == (partial_before + term) % 1_000_000_007) by(nonlinear_arith)\n requires 0 <= partial_before % 1_000_000_007 < 1_000_000_007,\n 0 <= term <= 10_000_000_000i64 as int,\n partial_before >= 0;\n }\n result = (result + nums[k] as i64 * sorted_count[k]) % modval;\n k = k + 1;\n }\n proof {\n assert(Self::dot_product_int(sv, sf, n as int) == 0);\n assert(result as int == Self::dot_product_int(sv, sf, 0) % 1_000_000_007);\n assert forall |c: Seq| c.len() == sv.len() && Self::is_perm_int(c, sv)\n implies Self::dot_product_int(sv, sf, 0) >= Self::dot_product_int(c, sf, 0) by {\n Self::rearrangement_ineq(sv, sf, c);\n };\n }\n result as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1589/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1589/tests/mutated_testcases.jsonl"} {"problem": "lc1648", "family": "leetcode", "description": "# Sell Diminishing-Valued Colored Balls\n\nYou have an `inventory` of different colored balls, and there is a customer that wants `orders` balls of **any** color.\n\nThe customer weirdly values the colored balls. Each colored ball's value is the number of balls **of that color** you currently have in your `inventory`. For example, if you own `6` yellow balls, the customer would pay `6` for the first yellow ball. After the transaction, there are only `5` yellow balls left, so the next yellow ball is then valued at `5` (i.e., the value of the balls decreases as you sell more to the customer).\n\nYou are given an integer array `inventory`, where `inventory[i]` represents the number of balls of the `i`th color that you initially own. You are also given an integer `orders`, which represents the total number of balls that the customer wants. You can sell the balls **in any order**.\n\nReturn the **maximum** total value that you can attain after selling `orders` colored balls. As the answer may be too large, return it **modulo** $10^9 + 7$.\n\n## Example 1:\n\n> **Input:** inventory = [2,5], orders = 4\n> **Output:** 14\n> **Explanation:** Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3). The maximum total value is 2 + 5 + 4 + 3 = 14.\n\n## Example 2:\n\n> **Input:** inventory = [3,5], orders = 6\n> **Output:** 19\n> **Explanation:** Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2). The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.\n\n## Constraints:\n\n- $1 \\leq \\text{inventory.length} \\leq 10^5$\n- $1 \\leq \\text{inventory}[i] \\leq 10^9$\n- $1 \\leq \\text{orders} \\leq \\min(\\sum \\text{inventory}[i], 10^9)$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_profit(inventory: Vec, orders: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Binary Search, Greedy, Sorting, Heap (Priority Queue)\nAcceptance rate: 30.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn to_int_seq(s: Seq) -> Seq {\n s.map(|_idx: int, x: i32| x as int)\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\npub open spec fn count_above(s: Seq, t: int) -> int\n decreases s.len()\n{\n if s.len() == 0 { 0 }\n else {\n count_above(s.drop_last(), t)\n + if s.last() > t { s.last() - t } else { 0 }\n }\n}\n\npub open spec fn value_above(s: Seq, t: int) -> int\n decreases s.len()\n{\n if s.len() == 0 { 0 }\n else {\n value_above(s.drop_last(), t)\n + if s.last() > t { (s.last() + t + 1) * (s.last() - t) / 2 } else { 0 }\n }\n}\n\nimpl Solution {\n pub fn max_profit(inventory: Vec, orders: i32) -> (result: i32)\n requires\n 1 <= inventory.len() <= 100_000,\n forall |i: int| 0 <= i < inventory.len() ==> 1 <= #[trigger] inventory[i] <= 1_000_000_000,\n 1 <= orders <= 1_000_000_000,\n orders as int <= count_above(to_int_seq(inventory@), 0),\n ensures\n exists |t: int| {\n &&& 0 <= t\n &&& count_above(to_int_seq(inventory@), t) <= orders as int\n &&& (t == 0 || count_above(to_int_seq(inventory@), t - 1) > orders as int)\n &&& result as int == (value_above(to_int_seq(inventory@), t)\n + (orders as int - count_above(to_int_seq(inventory@), t)) * t)\n % 1_000_000_007\n },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn count_above_exec(inventory: &Vec, threshold: i64) -> i64 {\n let n = inventory.len();\n let mut count: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n if inventory[j] as i64 > threshold {\n count += inventory[j] as i64 - threshold;\n }\n j += 1;\n }\n count\n }\n\n pub fn max_profit(inventory: Vec, orders: i32) -> i32 {\n let n = inventory.len();\n let orders_i64 = orders as i64;\n let modulo: i128 = 1_000_000_007;\n\n let mut max_inv: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n if inventory[i] as i64 > max_inv {\n max_inv = inventory[i] as i64;\n }\n i += 1;\n }\n\n let mut lo: i64 = 0;\n let mut hi: i64 = max_inv;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let count = Self::count_above_exec(&inventory, mid);\n if count <= orders_i64 {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n\n let threshold = lo;\n let mut total: i128 = 0;\n let mut sold: i64 = 0;\n let mut k: usize = 0;\n while k < n {\n let inv = inventory[k] as i64;\n if inv > threshold {\n sold += inv - threshold;\n let inv128 = inv as i128;\n let thr128 = threshold as i128;\n let a = inv128 + thr128 + 1;\n let b = inv128 - thr128;\n let contrib = (inv128 + thr128 + 1) * (inv128 - thr128) / 2;\n total += contrib;\n }\n k += 1;\n }\n\n let rem128 = (orders_i64 - sold) as i128;\n let thr_rem = threshold as i128;\n\n total += rem128 * thr_rem;\n\n (total % modulo) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn to_int_seq(s: Seq) -> Seq {\n s.map(|_idx: int, x: i32| x as int)\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\npub open spec fn count_above(s: Seq, t: int) -> int\n decreases s.len()\n{\n if s.len() == 0 { 0 }\n else {\n count_above(s.drop_last(), t)\n + if s.last() > t { s.last() - t } else { 0 }\n }\n}\n\npub open spec fn value_above(s: Seq, t: int) -> int\n decreases s.len()\n{\n if s.len() == 0 { 0 }\n else {\n value_above(s.drop_last(), t)\n + if s.last() > t { (s.last() + t + 1) * (s.last() - t) / 2 } else { 0 }\n }\n}\n\nimpl Solution {\n fn count_above_exec(inventory: &Vec, threshold: i64) -> (count: i64)\n requires\n inventory.len() <= 100_000,\n forall |i: int| 0 <= i < inventory.len() ==> 1 <= #[trigger] inventory[i] <= 1_000_000_000,\n 0 <= threshold <= 1_000_000_000,\n ensures\n count as int == count_above(to_int_seq(inventory@), threshold as int),\n count >= 0,\n {\n let n = inventory.len();\n let mut count: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n if inventory[j] as i64 > threshold {\n count += inventory[j] as i64 - threshold;\n }\n j += 1;\n }\n count\n }\n\n pub fn max_profit(inventory: Vec, orders: i32) -> (result: i32)\n requires\n 1 <= inventory.len() <= 100_000,\n forall |i: int| 0 <= i < inventory.len() ==> 1 <= #[trigger] inventory[i] <= 1_000_000_000,\n 1 <= orders <= 1_000_000_000,\n orders as int <= count_above(to_int_seq(inventory@), 0),\n ensures\n exists |t: int| {\n &&& 0 <= t\n &&& count_above(to_int_seq(inventory@), t) <= orders as int\n &&& (t == 0 || count_above(to_int_seq(inventory@), t - 1) > orders as int)\n &&& result as int == (value_above(to_int_seq(inventory@), t)\n + (orders as int - count_above(to_int_seq(inventory@), t)) * t)\n % 1_000_000_007\n },\n {\n let n = inventory.len();\n let orders_i64 = orders as i64;\n let modulo: i128 = 1_000_000_007;\n\n let mut max_inv: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n if inventory[i] as i64 > max_inv {\n max_inv = inventory[i] as i64;\n }\n i += 1;\n }\n\n let mut lo: i64 = 0;\n let mut hi: i64 = max_inv;\n while lo < hi {\n let mid = lo + (hi - lo) / 2;\n let count = Self::count_above_exec(&inventory, mid);\n if count <= orders_i64 {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n\n let threshold = lo;\n let mut total: i128 = 0;\n let mut sold: i64 = 0;\n let mut k: usize = 0;\n while k < n {\n let inv = inventory[k] as i64;\n if inv > threshold {\n sold += inv - threshold;\n let inv128 = inv as i128;\n let thr128 = threshold as i128;\n let a = inv128 + thr128 + 1;\n let b = inv128 - thr128;\n let contrib = (inv128 + thr128 + 1) * (inv128 - thr128) / 2;\n total += contrib;\n }\n k += 1;\n }\n\n let rem128 = (orders_i64 - sold) as i128;\n let thr_rem = threshold as i128;\n\n total += rem128 * thr_rem;\n\n (total % modulo) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn to_int_seq(s: Seq) -> Seq {\n s.map(|_idx: int, x: i32| x as int)\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\npub open spec fn count_above(s: Seq, t: int) -> int\n decreases s.len()\n{\n if s.len() == 0 { 0 }\n else {\n count_above(s.drop_last(), t)\n + if s.last() > t { s.last() - t } else { 0 }\n }\n}\n\npub open spec fn value_above(s: Seq, t: int) -> int\n decreases s.len()\n{\n if s.len() == 0 { 0 }\n else {\n value_above(s.drop_last(), t)\n + if s.last() > t { (s.last() + t + 1) * (s.last() - t) / 2 } else { 0 }\n }\n}\n\nproof fn lemma_count_above_nonneg(s: Seq, t: int)\n ensures count_above(s, t) >= 0\n decreases s.len()\n{\n if s.len() > 0 { lemma_count_above_nonneg(s.drop_last(), t); }\n}\n\nproof fn lemma_count_above_monotone(s: Seq, t1: int, t2: int)\n requires t1 <= t2\n ensures count_above(s, t1) >= count_above(s, t2)\n decreases s.len()\n{\n if s.len() > 0 { lemma_count_above_monotone(s.drop_last(), t1, t2); }\n}\n\nproof fn lemma_count_above_at_max(s: Seq, m: int)\n requires forall |i: int| 0 <= i < s.len() ==> s[i] <= m\n ensures count_above(s, m) == 0\n decreases s.len()\n{\n if s.len() > 0 {\n assert forall |i: int| 0 <= i < s.drop_last().len() implies s.drop_last()[i] <= m by {\n assert(s.drop_last()[i] == s[i]);\n }\n lemma_count_above_at_max(s.drop_last(), m);\n }\n}\n\nproof fn lemma_count_above_prefix_le(s: Seq, k: int, t: int)\n requires 0 <= k <= s.len()\n ensures count_above(s.take(k), t) <= count_above(s, t)\n decreases s.len()\n{\n lemma_count_above_nonneg(s.take(k), t);\n if s.len() > 0 && k < s.len() {\n lemma_count_above_prefix_le(s.drop_last(), k, t);\n assert(s.drop_last().take(k) =~= s.take(k));\n } else if s.len() > 0 {\n assert(s.take(k) =~= s);\n }\n}\n\nproof fn lemma_take_drop_last(s: Seq, k: int)\n requires 0 < k <= s.len()\n ensures\n s.take(k).drop_last() =~= s.take(k - 1),\n s.take(k).last() == s[k - 1],\n s.take(k).len() == k,\n{\n assert(s.take(k).drop_last() =~= s.take(k - 1));\n}\n\nproof fn lemma_value_above_nonneg(s: Seq, t: int)\n requires t >= 0\n ensures value_above(s, t) >= 0\n decreases s.len()\n{\n if s.len() > 0 {\n lemma_value_above_nonneg(s.drop_last(), t);\n if s.last() > t {\n assert((s.last() + t + 1) * (s.last() - t) >= 0) by(nonlinear_arith)\n requires s.last() > t, t >= 0;\n assert((s.last() + t + 1) * (s.last() - t) / 2 >= 0) by(nonlinear_arith)\n requires (s.last() + t + 1) * (s.last() - t) >= 0;\n }\n }\n}\n\nimpl Solution {\n fn count_above_exec(inventory: &Vec, threshold: i64) -> (count: i64)\n requires\n inventory.len() <= 100_000,\n forall |i: int| 0 <= i < inventory.len() ==> 1 <= #[trigger] inventory[i] <= 1_000_000_000,\n 0 <= threshold <= 1_000_000_000,\n ensures\n count as int == count_above(to_int_seq(inventory@), threshold as int),\n count >= 0,\n {\n let ghost sinv = to_int_seq(inventory@);\n let n = inventory.len();\n let mut count: i64 = 0;\n let mut j: usize = 0;\n while j < n\n invariant\n 0 <= j <= n,\n n == inventory.len(),\n n <= 100_000,\n sinv == to_int_seq(inventory@),\n count as int == count_above(sinv.take(j as int), threshold as int),\n count >= 0,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] inventory[i] <= 1_000_000_000,\n 0 <= threshold <= 1_000_000_000,\n count <= (j as int) * 1_000_000_000,\n decreases n - j,\n {\n proof {\n lemma_take_drop_last(sinv, (j + 1) as int);\n assert(sinv[j as int] == inventory[j as int] as int);\n }\n if inventory[j] as i64 > threshold {\n count += inventory[j] as i64 - threshold;\n }\n proof {\n assert(count as int == count_above(sinv.take((j + 1) as int), threshold as int));\n assert(count <= ((j + 1) as int) * 1_000_000_000) by(nonlinear_arith)\n requires\n count as int <= (j as int) * 1_000_000_000 + 1_000_000_000,\n 0 <= j,\n j < n,\n n <= 100_000;\n }\n j += 1;\n }\n proof { assert(sinv.take(n as int) =~= sinv); }\n count\n }\n\n pub fn max_profit(inventory: Vec, orders: i32) -> (result: i32)\n requires\n 1 <= inventory.len() <= 100_000,\n forall |i: int| 0 <= i < inventory.len() ==> 1 <= #[trigger] inventory[i] <= 1_000_000_000,\n 1 <= orders <= 1_000_000_000,\n orders as int <= count_above(to_int_seq(inventory@), 0),\n ensures\n exists |t: int| {\n &&& 0 <= t\n &&& count_above(to_int_seq(inventory@), t) <= orders as int\n &&& (t == 0 || count_above(to_int_seq(inventory@), t - 1) > orders as int)\n &&& result as int == (value_above(to_int_seq(inventory@), t)\n + (orders as int - count_above(to_int_seq(inventory@), t)) * t)\n % 1_000_000_007\n },\n {\n let ghost sinv = to_int_seq(inventory@);\n let n = inventory.len();\n let orders_i64 = orders as i64;\n let modulo: i128 = 1_000_000_007;\n\n let mut max_inv: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == inventory.len(),\n 0 <= max_inv <= 1_000_000_000,\n forall |j: int| 0 <= j < i ==> inventory[j] as i64 <= max_inv,\n forall |j: int| 0 <= j < n ==> 1 <= #[trigger] inventory[j] <= 1_000_000_000,\n decreases n - i,\n {\n if inventory[i] as i64 > max_inv {\n max_inv = inventory[i] as i64;\n }\n i += 1;\n }\n\n proof {\n assert forall |j: int| 0 <= j < sinv.len() implies sinv[j] <= max_inv as int by {\n assert(sinv[j] == inventory[j] as int);\n assert(inventory[j] as i64 <= max_inv);\n };\n lemma_count_above_at_max(sinv, max_inv as int);\n }\n\n let mut lo: i64 = 0;\n let mut hi: i64 = max_inv;\n while lo < hi\n invariant\n 0 <= lo <= hi <= max_inv,\n max_inv <= 1_000_000_000,\n count_above(sinv, hi as int) <= orders_i64 as int,\n lo == 0 || count_above(sinv, lo as int - 1) > orders_i64 as int,\n sinv == to_int_seq(inventory@),\n n == inventory.len(),\n inventory.len() <= 100_000,\n forall |j: int| 0 <= j < n ==> 1 <= #[trigger] inventory[j] <= 1_000_000_000,\n orders_i64 == orders as i64,\n 1 <= orders_i64 <= 1_000_000_000,\n decreases hi - lo,\n {\n let mid = lo + (hi - lo) / 2;\n let count = Self::count_above_exec(&inventory, mid);\n if count <= orders_i64 {\n hi = mid;\n } else {\n proof {\n if mid + 1 > 1 {\n lemma_count_above_monotone(sinv, mid as int, mid as int);\n }\n }\n lo = mid + 1;\n }\n }\n\n let threshold = lo;\n\n let mut total: i128 = 0;\n let mut sold: i64 = 0;\n let mut k: usize = 0;\n while k < n\n invariant\n 0 <= k <= n,\n n == inventory.len(),\n n <= 100_000,\n sinv == to_int_seq(inventory@),\n total as int == value_above(sinv.take(k as int), threshold as int),\n sold as int == count_above(sinv.take(k as int), threshold as int),\n 0 <= threshold <= max_inv,\n max_inv <= 1_000_000_000,\n forall |j: int| 0 <= j < n ==> 1 <= #[trigger] inventory[j] <= 1_000_000_000,\n sold >= 0,\n sold <= orders_i64,\n total >= 0,\n (total as int) <= (k as int) * 2_000_000_001_000_000_000int,\n orders_i64 == orders as i64,\n 1 <= orders_i64 <= 1_000_000_000,\n count_above(sinv, threshold as int) <= orders_i64 as int,\n forall |j: int| 0 <= j < sinv.len() ==> sinv[j] <= max_inv as int,\n decreases n - k,\n {\n let inv = inventory[k] as i64;\n\n proof {\n lemma_take_drop_last(sinv, (k + 1) as int);\n assert(sinv[k as int] == inventory[k as int] as int);\n }\n\n if inv > threshold {\n proof {\n lemma_count_above_prefix_le(sinv, (k + 1) as int, threshold as int);\n }\n\n sold += inv - threshold;\n\n let inv128 = inv as i128;\n let thr128 = threshold as i128;\n\n assert(1 <= inv128 && inv128 <= 1_000_000_000);\n assert(0 <= thr128 && thr128 <= 1_000_000_000);\n assert(inv128 > thr128);\n\n let a = inv128 + thr128 + 1;\n let b = inv128 - thr128;\n\n assert(2 <= a && a <= 2_000_000_001);\n assert(1 <= b && b <= 1_000_000_000);\n\n assert(a * b <= 2_000_000_001_000_000_000i128) by(nonlinear_arith)\n requires a <= 2_000_000_001, b <= 1_000_000_000, a >= 0, b >= 0;\n\n let contrib = (inv128 + thr128 + 1) * (inv128 - thr128) / 2;\n\n proof {\n assert(contrib == a * b / 2);\n assert(a * b >= 0);\n assert(contrib >= 0);\n assert(contrib <= a * b);\n assert(contrib <= 2_000_000_001_000_000_000i128);\n assert((total as int) + (contrib as int) <= ((k + 1) as int) * 2_000_000_001_000_000_000int)\n by(nonlinear_arith)\n requires\n (total as int) <= (k as int) * 2_000_000_001_000_000_000int,\n (contrib as int) <= 2_000_000_001_000_000_000int,\n total >= 0i128,\n contrib >= 0i128;\n }\n\n total += contrib;\n }\n\n proof {\n assert(total as int == value_above(sinv.take((k + 1) as int), threshold as int));\n assert(sold as int == count_above(sinv.take((k + 1) as int), threshold as int));\n assert(sold <= orders_i64) by {\n lemma_count_above_prefix_le(sinv, (k + 1) as int, threshold as int);\n }\n lemma_value_above_nonneg(sinv.take((k + 1) as int), threshold as int);\n }\n\n k += 1;\n }\n\n proof { assert(sinv.take(n as int) =~= sinv); }\n\n assert(sold <= orders_i64) by {\n assert(sold as int == count_above(sinv, threshold as int));\n assert(count_above(sinv, threshold as int) <= orders_i64 as int);\n }\n\n let rem128 = (orders_i64 - sold) as i128;\n let thr_rem = threshold as i128;\n\n assert(0 <= rem128 && rem128 <= 1_000_000_000);\n assert(0 <= thr_rem && thr_rem <= 1_000_000_000);\n assert(rem128 * thr_rem <= 1_000_000_000_000_000_000i128) by(nonlinear_arith)\n requires rem128 >= 0, rem128 <= 1_000_000_000, thr_rem >= 0, thr_rem <= 1_000_000_000;\n\n total += rem128 * thr_rem;\n\n proof {\n assert(total as int == value_above(sinv, threshold as int)\n + (orders as int - count_above(sinv, threshold as int)) * threshold as int);\n lemma_value_above_nonneg(sinv, threshold as int);\n assert(total >= 0);\n }\n\n (total % modulo) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1648/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1648/tests/mutated_testcases.jsonl"} {"problem": "lc1652", "family": "leetcode", "description": "# Defuse the Bomb\n\nYou have a bomb to defuse, and your time is running out! Your informer will provide you with a **circular** array `code` of length of `n` and a key `k`.\n\nTo decrypt the code, you must replace every number. All the numbers are replaced **simultaneously**.\n\n- If `k > 0`, replace the `i`-th number with the sum of the **next** `k` numbers.\n- If `k < 0`, replace the `i`-th number with the sum of the **previous** `-k` numbers.\n- If `k == 0`, replace the `i`-th number with `0`.\n\nAs `code` is circular, the next element of `code[n-1]` is `code[0]`, and the previous element of `code[0]` is `code[n-1]`.\n\nGiven the **circular** array `code` and an integer key `k`, return the decrypted code to defuse the bomb!\n\n## Example 1:\n\n> **Input:** code = [5,7,1,4], k = 3\n> **Output:** [12,10,16,13]\n> **Explanation:** Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.\n\n## Example 2:\n\n> **Input:** code = [1,2,3,4], k = 0\n> **Output:** [0,0,0,0]\n> **Explanation:** When k is zero, the numbers are replaced by 0.\n\n## Example 3:\n\n> **Input:** code = [2,4,9,3], k = -2\n> **Output:** [12,5,6,13]\n> **Explanation:** The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the **previous** numbers.\n\n## Constraints:\n\n- `n == code.length`\n- $1 \\leq n \\leq 100$\n- $1 \\leq \\text{code}[i] \\leq 100$\n- $-(n - 1) \\leq k \\leq n - 1$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn decrypt(code: Vec, k: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sliding Window\nAcceptance rate: 79.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn circular_sum(code: Seq, n: int, start: int, count: int) -> int\n decreases count,\n {\n if count <= 0 || n <= 0 {\n 0\n } else {\n code[start % n] as int + Self::circular_sum(code, n, start + 1, count - 1)\n }\n }\n\n pub open spec fn decrypt_val(code: Seq, n: int, k: int, i: int) -> int {\n if k > 0 {\n Self::circular_sum(code, n, i + 1, k)\n } else if k < 0 {\n Self::circular_sum(code, n, i + n + k, -k)\n } else {\n 0\n }\n }\n\n pub fn decrypt(code: Vec, k: i32) -> (result: Vec)\n requires\n 1 <= code@.len() <= 100,\n forall|i: int| 0 <= i < code@.len() ==> 1 <= #[trigger] code@[i] <= 100,\n -(code@.len() as int - 1) <= k as int <= code@.len() as int - 1,\n ensures\n result@.len() == code@.len(),\n forall|i: int|\n 0 <= i < code@.len() ==> (#[trigger] result@[i]) as int == Self::decrypt_val(\n code@,\n code@.len() as int,\n k as int,\n i,\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn decrypt(code: Vec, k: i32) -> Vec {\n let n = code.len();\n let mut result: Vec = Vec::new();\n if k == 0 {\n let mut i: usize = 0;\n while i < n {\n result.push(0);\n i = i + 1;\n }\n return result;\n }\n let mut sum: i32 = 0;\n if k > 0 {\n let mut j: usize = 0;\n while j < k as usize {\n sum = sum + code[(1 + j) % n];\n j = j + 1;\n }\n result.push(sum);\n let mut i: usize = 1;\n while i < n {\n sum = sum - code[i % n] + code[(i + k as usize) % n];\n result.push(sum);\n i = i + 1;\n }\n } else {\n let abs_k: usize = (-k) as usize;\n let mut j: usize = 0;\n while j < abs_k {\n sum = sum + code[(n - abs_k + j) % n];\n j = j + 1;\n }\n result.push(sum);\n let mut i: usize = 1;\n while i < n {\n sum = sum - code[(i + n - abs_k - 1) % n] + code[(i + n - 1) % n];\n result.push(sum);\n i = i + 1;\n }\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn circular_sum(code: Seq, n: int, start: int, count: int) -> int\n decreases count,\n {\n if count <= 0 || n <= 0 {\n 0\n } else {\n code[start % n] as int + Self::circular_sum(code, n, start + 1, count - 1)\n }\n }\n\n pub open spec fn decrypt_val(code: Seq, n: int, k: int, i: int) -> int {\n if k > 0 {\n Self::circular_sum(code, n, i + 1, k)\n } else if k < 0 {\n Self::circular_sum(code, n, i + n + k, -k)\n } else {\n 0\n }\n }\n\n pub fn decrypt(code: Vec, k: i32) -> (result: Vec)\n requires\n 1 <= code@.len() <= 100,\n forall|i: int| 0 <= i < code@.len() ==> 1 <= #[trigger] code@[i] <= 100,\n -(code@.len() as int - 1) <= k as int <= code@.len() as int - 1,\n ensures\n result@.len() == code@.len(),\n forall|i: int|\n 0 <= i < code@.len() ==> (#[trigger] result@[i]) as int == Self::decrypt_val(\n code@,\n code@.len() as int,\n k as int,\n i,\n ),\n {\n let n = code.len();\n let mut result: Vec = Vec::new();\n if k == 0 {\n let mut i: usize = 0;\n while i < n {\n result.push(0);\n i = i + 1;\n }\n return result;\n }\n let mut sum: i32 = 0;\n if k > 0 {\n let mut j: usize = 0;\n while j < k as usize {\n sum = sum + code[(1 + j) % n];\n j = j + 1;\n }\n result.push(sum);\n let mut i: usize = 1;\n while i < n {\n sum = sum - code[i % n] + code[(i + k as usize) % n];\n result.push(sum);\n i = i + 1;\n }\n } else {\n let abs_k: usize = (-k) as usize;\n let mut j: usize = 0;\n while j < abs_k {\n sum = sum + code[(n - abs_k + j) % n];\n j = j + 1;\n }\n result.push(sum);\n let mut i: usize = 1;\n while i < n {\n sum = sum - code[(i + n - abs_k - 1) % n] + code[(i + n - 1) % n];\n result.push(sum);\n i = i + 1;\n }\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn circular_sum(code: Seq, n: int, start: int, count: int) -> int\n decreases count,\n {\n if count <= 0 || n <= 0 {\n 0\n } else {\n code[start % n] as int + Self::circular_sum(code, n, start + 1, count - 1)\n }\n }\n\n pub open spec fn decrypt_val(code: Seq, n: int, k: int, i: int) -> int {\n if k > 0 {\n Self::circular_sum(code, n, i + 1, k)\n } else if k < 0 {\n Self::circular_sum(code, n, i + n + k, -k)\n } else {\n 0\n }\n }\n\n proof fn circular_sum_append(code: Seq, n: int, start: int, count: int)\n requires\n n > 0,\n count >= 0,\n code.len() == n,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] code[i] <= 100,\n ensures\n Self::circular_sum(code, n, start, count + 1) == Self::circular_sum(\n code,\n n,\n start,\n count,\n ) + code[(start + count) % n] as int,\n decreases count,\n {\n reveal_with_fuel(Solution::circular_sum, 2);\n if count > 0 {\n Self::circular_sum_append(code, n, start + 1, count - 1);\n }\n }\n\n proof fn circular_sum_slide(code: Seq, n: int, start: int, count: int)\n requires\n n > 0,\n count > 0,\n code.len() == n,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] code[i] <= 100,\n ensures\n Self::circular_sum(code, n, start + 1, count)\n == Self::circular_sum(code, n, start, count)\n - code[start % n] as int\n + code[(start + count) % n] as int,\n {\n Self::circular_sum_append(code, n, start + 1, count - 1);\n }\n\n proof fn circular_sum_bounds(code: Seq, n: int, start: int, count: int)\n requires\n n > 0,\n count >= 0,\n code.len() == n,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] code[i] <= 100,\n ensures\n count <= Self::circular_sum(code, n, start, count) <= count * 100,\n decreases count,\n {\n if count > 0 {\n Self::circular_sum_bounds(code, n, start + 1, count - 1);\n }\n }\n\n pub fn decrypt(code: Vec, k: i32) -> (result: Vec)\n requires\n 1 <= code@.len() <= 100,\n forall|i: int| 0 <= i < code@.len() ==> 1 <= #[trigger] code@[i] <= 100,\n -(code@.len() as int - 1) <= k as int <= code@.len() as int - 1,\n ensures\n result@.len() == code@.len(),\n forall|i: int|\n 0 <= i < code@.len() ==> (#[trigger] result@[i]) as int == Self::decrypt_val(\n code@,\n code@.len() as int,\n k as int,\n i,\n ),\n {\n let n = code.len();\n let mut result: Vec = Vec::new();\n if k == 0 {\n let mut i: usize = 0;\n while i < n\n invariant\n n == code@.len(),\n 1 <= n <= 100,\n k == 0,\n 0 <= i <= n,\n result@.len() == i as int,\n forall|idx: int|\n 0 <= idx < i as int ==> (#[trigger] result@[idx]) as int == 0,\n decreases n - i,\n {\n result.push(0);\n i = i + 1;\n }\n proof {\n assert forall|idx: int|\n 0 <= idx < code@.len() implies (#[trigger] result@[idx]) as int\n == Self::decrypt_val(code@, n as int, k as int, idx)\n by {\n assert(result@[idx] == 0i32);\n }\n }\n return result;\n }\n let mut sum: i32 = 0;\n if k > 0 {\n let mut j: usize = 0;\n while j < k as usize\n invariant\n n == code@.len(),\n 1 <= n <= 100,\n k > 0,\n k as int <= n as int - 1,\n 0 <= j <= k as usize,\n forall|m: int| 0 <= m < n as int ==> 1 <= #[trigger] code@[m] <= 100,\n sum as int == Self::circular_sum(code@, n as int, 1, j as int),\n j as int <= sum as int <= j as int * 100,\n decreases k as usize - j,\n {\n proof {\n Self::circular_sum_append(code@, n as int, 1, j as int);\n }\n sum = sum + code[(1 + j) % n];\n j = j + 1;\n }\n proof {\n assert(sum as int == Self::circular_sum(code@, n as int, 1, k as int));\n assert(sum as int == Self::decrypt_val(code@, n as int, k as int, 0));\n }\n result.push(sum);\n let mut i: usize = 1;\n while i < n\n invariant\n n == code@.len(),\n 1 <= n <= 100,\n k > 0,\n k as int <= n as int - 1,\n 1 <= i <= n,\n forall|m: int| 0 <= m < n as int ==> 1 <= #[trigger] code@[m] <= 100,\n result@.len() == i as int,\n sum as int == Self::circular_sum(code@, n as int, i as int, k as int),\n k as int <= sum as int <= k as int * 100,\n forall|idx: int|\n 0 <= idx < i as int ==> (#[trigger] result@[idx]) as int\n == Self::decrypt_val(code@, n as int, k as int, idx),\n decreases n - i,\n {\n proof {\n Self::circular_sum_slide(code@, n as int, i as int, k as int);\n Self::circular_sum_bounds(code@, n as int, (i + 1) as int, k as int);\n }\n sum = sum - code[i % n] + code[(i + k as usize) % n];\n proof {\n assert(sum as int == Self::circular_sum(\n code@,\n n as int,\n (i + 1) as int,\n k as int,\n ));\n assert(sum as int == Self::decrypt_val(\n code@,\n n as int,\n k as int,\n i as int,\n ));\n }\n result.push(sum);\n i = i + 1;\n }\n } else {\n let abs_k: usize = (-k) as usize;\n let mut j: usize = 0;\n while j < abs_k\n invariant\n n == code@.len(),\n 1 <= n <= 100,\n k < 0,\n -(n as int - 1) <= k as int,\n abs_k == (-k) as usize,\n 1 <= abs_k <= n - 1,\n 0 <= j <= abs_k,\n forall|m: int| 0 <= m < n as int ==> 1 <= #[trigger] code@[m] <= 100,\n sum as int == Self::circular_sum(\n code@,\n n as int,\n (n - abs_k) as int,\n j as int,\n ),\n j as int <= sum as int <= j as int * 100,\n decreases abs_k - j,\n {\n proof {\n Self::circular_sum_append(\n code@,\n n as int,\n (n - abs_k) as int,\n j as int,\n );\n }\n sum = sum + code[(n - abs_k + j) % n];\n j = j + 1;\n }\n proof {\n assert(abs_k as int == -(k as int));\n assert((n - abs_k) as int == 0int + n as int + k as int);\n assert(sum as int == Self::circular_sum(\n code@,\n n as int,\n 0int + n as int + k as int,\n abs_k as int,\n ));\n assert(sum as int == Self::decrypt_val(code@, n as int, k as int, 0));\n assert(0int == 1int - 1 + 0);\n }\n result.push(sum);\n let mut i: usize = 1;\n while i < n\n invariant\n n == code@.len(),\n 1 <= n <= 100,\n k < 0,\n -(n as int - 1) <= k as int,\n abs_k == (-k) as usize,\n 1 <= abs_k <= n - 1,\n 1 <= i <= n,\n forall|m: int| 0 <= m < n as int ==> 1 <= #[trigger] code@[m] <= 100,\n result@.len() == i as int,\n sum as int == Self::circular_sum(\n code@,\n n as int,\n (i as int - 1 + n as int + k as int),\n abs_k as int,\n ),\n abs_k as int <= sum as int <= abs_k as int * 100,\n forall|idx: int|\n 0 <= idx < i as int ==> (#[trigger] result@[idx]) as int\n == Self::decrypt_val(code@, n as int, k as int, idx),\n decreases n - i,\n {\n proof {\n let prev_start = i as int - 1 + n as int + k as int;\n Self::circular_sum_slide(\n code@,\n n as int,\n prev_start,\n abs_k as int,\n );\n Self::circular_sum_bounds(\n code@,\n n as int,\n prev_start + 1,\n abs_k as int,\n );\n assert(prev_start == i as int + n as int - abs_k as int - 1);\n assert(prev_start + abs_k as int == i as int + n as int - 1);\n }\n sum = sum - code[(i + n - abs_k - 1) % n] + code[(i + n - 1) % n];\n proof {\n let new_start = i as int + n as int + k as int;\n assert(new_start == i as int - 1 + n as int + k as int + 1);\n assert(sum as int == Self::circular_sum(\n code@,\n n as int,\n new_start,\n abs_k as int,\n ));\n assert(-(k as int) == abs_k as int);\n assert(sum as int == Self::decrypt_val(\n code@,\n n as int,\n k as int,\n i as int,\n ));\n }\n result.push(sum);\n i = i + 1;\n }\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1652/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1652/tests/mutated_testcases.jsonl"} {"problem": "lc1664", "family": "leetcode", "description": "# Ways to Make a Fair Array\n\nYou are given an integer array `nums`. You can choose **exactly one** index (**0-indexed**) and remove the element. Notice that the index of the elements may change after the removal.\n\nFor example, if `nums = [6,1,7,4,1]`:\n\n- Choosing to remove index `1` results in `nums = [6,7,4,1]`.\n- Choosing to remove index `2` results in `nums = [6,1,4,1]`.\n- Choosing to remove index `4` results in `nums = [6,1,7,4]`.\n\nAn array is **fair** if the sum of the odd-indexed values equals the sum of the even-indexed values.\n\nReturn the number of indices that you could choose such that after the removal, `nums` is fair.\n\n## Example 1:\n\n> **Input:** nums = [2,1,6,4]\n> **Output:** 1\n> **Explanation:**\n> Remove index 0: [1,6,4] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair.\n> Remove index 1: [2,6,4] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair.\n> Remove index 2: [2,1,4] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair.\n> Remove index 3: [2,1,6] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair.\n> There is 1 index that you can remove to make nums fair.\n\n## Example 2:\n\n> **Input:** nums = [1,1,1]\n> **Output:** 3\n> **Explanation:** You can remove any index and the remaining array is fair.\n\n## Example 3:\n\n> **Input:** nums = [1,2,3]\n> **Output:** 0\n> **Explanation:** You cannot make a fair array after removing any index.\n\n## Constraints:\n\n- $1 \\leq \\text{nums.length} \\leq 10^5$\n- $1 \\leq \\text{nums}[i] \\leq 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn ways_to_make_fair(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Prefix Sum\nAcceptance rate: 66.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_even(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if start % 2 == 0 {\n s[start] as int + sum_even(s, start + 1, end)\n } else {\n sum_even(s, start + 1, end)\n }\n}\n\npub open spec fn sum_odd(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if start % 2 == 1 {\n s[start] as int + sum_odd(s, start + 1, end)\n } else {\n sum_odd(s, start + 1, end)\n }\n}\n\npub open spec fn even_sum_after_removal(s: Seq, i: int) -> int {\n sum_even(s, 0, i) + sum_odd(s, i + 1, s.len() as int)\n}\n\npub open spec fn odd_sum_after_removal(s: Seq, i: int) -> int {\n sum_odd(s, 0, i) + sum_even(s, i + 1, s.len() as int)\n}\n\npub open spec fn is_fair_after_removal(s: Seq, i: int) -> bool {\n even_sum_after_removal(s, i) == odd_sum_after_removal(s, i)\n}\n\npub open spec fn count_fair(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if is_fair_after_removal(s, start) {\n 1 + count_fair(s, start + 1, end)\n } else {\n count_fair(s, start + 1, end)\n }\n}\n\nimpl Solution {\n pub fn ways_to_make_fair(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10_000,\n ensures\n res == count_fair(nums@, 0, nums@.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn ways_to_make_fair(nums: Vec) -> i32 {\n let n = nums.len();\n let mut right_even: i64 = 0;\n let mut right_odd: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n if i % 2 == 0 {\n right_even = right_even + nums[i] as i64;\n } else {\n right_odd = right_odd + nums[i] as i64;\n }\n i = i + 1;\n }\n let mut left_even: i64 = 0;\n let mut left_odd: i64 = 0;\n let mut result: i32 = 0;\n i = 0;\n while i < n {\n if i % 2 == 0 {\n right_even = right_even - nums[i] as i64;\n } else {\n right_odd = right_odd - nums[i] as i64;\n }\n if left_even + right_odd == left_odd + right_even {\n result = result + 1;\n }\n if i % 2 == 0 {\n left_even = left_even + nums[i] as i64;\n } else {\n left_odd = left_odd + nums[i] as i64;\n }\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_even(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if start % 2 == 0 {\n s[start] as int + sum_even(s, start + 1, end)\n } else {\n sum_even(s, start + 1, end)\n }\n}\n\npub open spec fn sum_odd(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if start % 2 == 1 {\n s[start] as int + sum_odd(s, start + 1, end)\n } else {\n sum_odd(s, start + 1, end)\n }\n}\n\npub open spec fn even_sum_after_removal(s: Seq, i: int) -> int {\n sum_even(s, 0, i) + sum_odd(s, i + 1, s.len() as int)\n}\n\npub open spec fn odd_sum_after_removal(s: Seq, i: int) -> int {\n sum_odd(s, 0, i) + sum_even(s, i + 1, s.len() as int)\n}\n\npub open spec fn is_fair_after_removal(s: Seq, i: int) -> bool {\n even_sum_after_removal(s, i) == odd_sum_after_removal(s, i)\n}\n\npub open spec fn count_fair(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if is_fair_after_removal(s, start) {\n 1 + count_fair(s, start + 1, end)\n } else {\n count_fair(s, start + 1, end)\n }\n}\n\nimpl Solution {\n pub fn ways_to_make_fair(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10_000,\n ensures\n res == count_fair(nums@, 0, nums@.len() as int),\n {\n let n = nums.len();\n let mut right_even: i64 = 0;\n let mut right_odd: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n if i % 2 == 0 {\n right_even = right_even + nums[i] as i64;\n } else {\n right_odd = right_odd + nums[i] as i64;\n }\n i = i + 1;\n }\n let mut left_even: i64 = 0;\n let mut left_odd: i64 = 0;\n let mut result: i32 = 0;\n i = 0;\n while i < n {\n if i % 2 == 0 {\n right_even = right_even - nums[i] as i64;\n } else {\n right_odd = right_odd - nums[i] as i64;\n }\n if left_even + right_odd == left_odd + right_even {\n result = result + 1;\n }\n if i % 2 == 0 {\n left_even = left_even + nums[i] as i64;\n } else {\n left_odd = left_odd + nums[i] as i64;\n }\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sum_even(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if start % 2 == 0 {\n s[start] as int + sum_even(s, start + 1, end)\n } else {\n sum_even(s, start + 1, end)\n }\n}\n\npub open spec fn sum_odd(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if start % 2 == 1 {\n s[start] as int + sum_odd(s, start + 1, end)\n } else {\n sum_odd(s, start + 1, end)\n }\n}\n\npub open spec fn even_sum_after_removal(s: Seq, i: int) -> int {\n sum_even(s, 0, i) + sum_odd(s, i + 1, s.len() as int)\n}\n\npub open spec fn odd_sum_after_removal(s: Seq, i: int) -> int {\n sum_odd(s, 0, i) + sum_even(s, i + 1, s.len() as int)\n}\n\npub open spec fn is_fair_after_removal(s: Seq, i: int) -> bool {\n even_sum_after_removal(s, i) == odd_sum_after_removal(s, i)\n}\n\npub open spec fn count_fair(s: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else if is_fair_after_removal(s, start) {\n 1 + count_fair(s, start + 1, end)\n } else {\n count_fair(s, start + 1, end)\n }\n}\n\nproof fn lemma_sum_even_bounds(s: Seq, start: int, end: int)\n requires\n 0 <= start <= end <= s.len(),\n forall |i: int| start <= i < end ==> 1 <= #[trigger] s[i] <= 10_000,\n ensures\n 0 <= sum_even(s, start, end) <= (end - start) * 10_000,\n decreases end - start,\n{\n if start < end {\n lemma_sum_even_bounds(s, start + 1, end);\n assert((end - start - 1) * 10_000 + 10_000 <= (end - start) * 10_000) by(nonlinear_arith)\n requires end - start >= 1;\n }\n}\n\nproof fn lemma_sum_odd_bounds(s: Seq, start: int, end: int)\n requires\n 0 <= start <= end <= s.len(),\n forall |i: int| start <= i < end ==> 1 <= #[trigger] s[i] <= 10_000,\n ensures\n 0 <= sum_odd(s, start, end) <= (end - start) * 10_000,\n decreases end - start,\n{\n if start < end {\n lemma_sum_odd_bounds(s, start + 1, end);\n assert((end - start - 1) * 10_000 + 10_000 <= (end - start) * 10_000) by(nonlinear_arith)\n requires end - start >= 1;\n }\n}\n\nproof fn lemma_sum_even_split(s: Seq, start: int, mid: int, end: int)\n requires\n 0 <= start <= mid <= end <= s.len(),\n ensures\n sum_even(s, start, end) == sum_even(s, start, mid) + sum_even(s, mid, end),\n decreases mid - start,\n{\n if start < mid {\n lemma_sum_even_split(s, start + 1, mid, end);\n }\n}\n\nproof fn lemma_sum_odd_split(s: Seq, start: int, mid: int, end: int)\n requires\n 0 <= start <= mid <= end <= s.len(),\n ensures\n sum_odd(s, start, end) == sum_odd(s, start, mid) + sum_odd(s, mid, end),\n decreases mid - start,\n{\n if start < mid {\n lemma_sum_odd_split(s, start + 1, mid, end);\n }\n}\n\nproof fn lemma_sum_even_single(s: Seq, k: int)\n requires\n 0 <= k,\n k + 1 <= s.len(),\n ensures\n sum_even(s, k, k + 1) == if k % 2 == 0 { s[k] as int } else { 0 as int },\n{\n assert(sum_even(s, k + 1, k + 1) == 0 as int);\n}\n\nproof fn lemma_sum_odd_single(s: Seq, k: int)\n requires\n 0 <= k,\n k + 1 <= s.len(),\n ensures\n sum_odd(s, k, k + 1) == if k % 2 == 1 { s[k] as int } else { 0 as int },\n{\n assert(sum_odd(s, k + 1, k + 1) == 0 as int);\n}\n\nproof fn lemma_count_fair_nonneg(s: Seq, start: int, end: int)\n ensures\n count_fair(s, start, end) >= 0,\n decreases end - start,\n{\n if start < end {\n lemma_count_fair_nonneg(s, start + 1, end);\n }\n}\n\nproof fn lemma_count_fair_upper(s: Seq, start: int, end: int)\n requires\n start <= end,\n ensures\n count_fair(s, start, end) <= end - start,\n decreases end - start,\n{\n if start < end {\n lemma_count_fair_upper(s, start + 1, end);\n }\n}\n\nimpl Solution {\n pub fn ways_to_make_fair(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10_000,\n ensures\n res == count_fair(nums@, 0, nums@.len() as int),\n {\n let n = nums.len();\n let mut right_even: i64 = 0;\n let mut right_odd: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n n <= 100_000,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] nums[k] <= 10_000,\n right_even == sum_even(nums@, 0, i as int),\n right_odd == sum_odd(nums@, 0, i as int),\n 0 <= right_even <= n as int * 10_000,\n 0 <= right_odd <= n as int * 10_000,\n decreases n - i,\n {\n proof {\n lemma_sum_even_single(nums@, i as int);\n lemma_sum_odd_single(nums@, i as int);\n lemma_sum_even_split(nums@, 0, i as int, i as int + 1);\n lemma_sum_odd_split(nums@, 0, i as int, i as int + 1);\n lemma_sum_even_bounds(nums@, 0, i as int + 1);\n lemma_sum_odd_bounds(nums@, 0, i as int + 1);\n assert((i as int + 1) * 10_000 <= n as int * 10_000) by(nonlinear_arith)\n requires 0 <= i as int + 1 <= n as int;\n }\n if i % 2 == 0 {\n right_even = right_even + nums[i] as i64;\n } else {\n right_odd = right_odd + nums[i] as i64;\n }\n i = i + 1;\n }\n let mut left_even: i64 = 0;\n let mut left_odd: i64 = 0;\n let mut result: i32 = 0;\n i = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n n <= 100_000,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] nums[k] <= 10_000,\n left_even == sum_even(nums@, 0, i as int),\n left_odd == sum_odd(nums@, 0, i as int),\n right_even == sum_even(nums@, i as int, n as int),\n right_odd == sum_odd(nums@, i as int, n as int),\n result as int + count_fair(nums@, i as int, n as int) == count_fair(nums@, 0, n as int),\n 0 <= left_even <= n as int * 10_000,\n 0 <= left_odd <= n as int * 10_000,\n 0 <= right_even <= n as int * 10_000,\n 0 <= right_odd <= n as int * 10_000,\n 0 <= result,\n result as int <= n as int,\n decreases n - i,\n {\n proof {\n lemma_sum_even_single(nums@, i as int);\n lemma_sum_odd_single(nums@, i as int);\n lemma_sum_even_split(nums@, i as int, i as int + 1, n as int);\n lemma_sum_odd_split(nums@, i as int, i as int + 1, n as int);\n lemma_sum_even_bounds(nums@, i as int + 1, n as int);\n lemma_sum_odd_bounds(nums@, i as int + 1, n as int);\n assert((n as int - i as int - 1) * 10_000 <= n as int * 10_000) by(nonlinear_arith)\n requires 0 <= n as int - i as int - 1 <= n as int;\n }\n if i % 2 == 0 {\n right_even = right_even - nums[i] as i64;\n } else {\n right_odd = right_odd - nums[i] as i64;\n }\n proof {\n assert(right_even == sum_even(nums@, i as int + 1, n as int));\n assert(right_odd == sum_odd(nums@, i as int + 1, n as int));\n assert(even_sum_after_removal(nums@, i as int) == left_even + right_odd);\n assert(odd_sum_after_removal(nums@, i as int) == left_odd + right_even);\n lemma_count_fair_nonneg(nums@, i as int + 1, n as int);\n lemma_count_fair_upper(nums@, i as int + 1, n as int);\n }\n if left_even + right_odd == left_odd + right_even {\n proof {\n assert(is_fair_after_removal(nums@, i as int));\n assert(count_fair(nums@, i as int, n as int) == 1 + count_fair(nums@, i as int + 1, n as int));\n }\n result = result + 1;\n } else {\n proof {\n assert(!is_fair_after_removal(nums@, i as int));\n assert(count_fair(nums@, i as int, n as int) == count_fair(nums@, i as int + 1, n as int));\n }\n }\n proof {\n lemma_sum_even_split(nums@, 0, i as int, i as int + 1);\n lemma_sum_odd_split(nums@, 0, i as int, i as int + 1);\n lemma_sum_even_bounds(nums@, 0, i as int + 1);\n lemma_sum_odd_bounds(nums@, 0, i as int + 1);\n assert((i as int + 1) * 10_000 <= n as int * 10_000) by(nonlinear_arith)\n requires 0 <= i as int + 1 <= n as int;\n }\n if i % 2 == 0 {\n left_even = left_even + nums[i] as i64;\n } else {\n left_odd = left_odd + nums[i] as i64;\n }\n proof {\n lemma_count_fair_upper(nums@, 0, n as int);\n }\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1664/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1664/tests/mutated_testcases.jsonl"} {"problem": "lc1671", "family": "leetcode", "description": "# Minimum Number of Removals to Make Mountain Array\n\nYou may recall that an array `arr` is a **mountain array** if and only if:\n\n- `arr.length >= 3`\n- There exists some index `i` (**0-indexed**) with `0 < i < arr.length - 1` such that:\n - `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`\n - `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`\n\nGiven an integer array `nums`, return the **minimum** number of elements to remove to make `nums` a **mountain array**.\n\n## Example 1:\n\n> **Input:** nums = [1,3,1]\n> **Output:** 0\n> **Explanation:** The array itself is a mountain array so we do not need to remove any elements.\n\n## Example 2:\n\n> **Input:** nums = [2,1,1,5,6,2,3,1]\n> **Output:** 3\n> **Explanation:** One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].\n\n## Constraints:\n\n- 3 <= nums.length <= 1000\n- 1 <= nums[i] <= $10^9$\n- It is guaranteed that you can make a mountain array out of `nums`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_mountain_removals(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Binary Search, Dynamic Programming, Greedy\nAcceptance rate: 54.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_subseq(nums: Seq, indices: Seq, peak: int) -> bool {\n &&& indices.len() >= 3\n &&& 0 < peak < indices.len() - 1\n &&& (forall |k: int| 0 <= k < indices.len() ==> 0 <= (#[trigger] indices[k]) < nums.len())\n &&& (forall |k: int| 0 <= k < peak ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) < nums[indices[k + 1]])\n &&& (forall |k: int| peak <= k < indices.len() - 1 ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) > nums[indices[k + 1]])\n }\n\n pub fn minimum_mountain_removals(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000i32,\n exists |a: int, b: int, c: int| 0 <= a < b < c < nums.len() as int\n && nums[a] < nums[b] && nums[b] > nums[c],\n ensures\n result >= 0,\n exists |indices: Seq, peak: int| Self::is_mountain_subseq(nums@, indices, peak)\n && indices.len() == nums.len() - result as int,\n forall |indices: Seq, peak: int| Self::is_mountain_subseq(nums@, indices, peak)\n ==> indices.len() <= nums.len() - result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_mountain_removals(nums: Vec) -> i32 {\n let n = nums.len();\n\n let mut lis: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n {\n lis.push(1i32);\n idx = idx + 1;\n }\n\n let mut i: usize = 1;\n while i < n {\n let mut j: usize = 0;\n while j < i {\n if nums[j] < nums[i] {\n if lis[j] + 1 > lis[i] {\n lis[i] = lis[j] + 1;\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n\n let mut lds: Vec = Vec::new();\n idx = 0;\n while idx < n {\n lds.push(1i32);\n idx = idx + 1;\n }\n\n let mut k: usize = 1;\n while k < n {\n let i_idx: usize = n - 1 - k;\n let mut j: usize = i_idx + 1;\n while j < n {\n if nums[j] < nums[i_idx] {\n if lds[j] + 1 > lds[i_idx] {\n lds[i_idx] = lds[j] + 1;\n }\n }\n j = j + 1;\n }\n k = k + 1;\n }\n\n let mut result: i32 = n as i32;\n let mut i2: usize = 0;\n while i2 < n {\n if lis[i2] > 1 && lds[i2] > 1 {\n let mountain_len: i32 = lis[i2] + lds[i2] - 1;\n let removals: i32 = n as i32 - mountain_len;\n if removals < result {\n result = removals;\n }\n }\n i2 = i2 + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_subseq(nums: Seq, indices: Seq, peak: int) -> bool {\n &&& indices.len() >= 3\n &&& 0 < peak < indices.len() - 1\n &&& (forall |k: int| 0 <= k < indices.len() ==> 0 <= (#[trigger] indices[k]) < nums.len())\n &&& (forall |k: int| 0 <= k < peak ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) < nums[indices[k + 1]])\n &&& (forall |k: int| peak <= k < indices.len() - 1 ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) > nums[indices[k + 1]])\n }\n\n pub fn minimum_mountain_removals(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000i32,\n exists |a: int, b: int, c: int| 0 <= a < b < c < nums.len() as int\n && nums[a] < nums[b] && nums[b] > nums[c],\n ensures\n result >= 0,\n exists |indices: Seq, peak: int| Self::is_mountain_subseq(nums@, indices, peak)\n && indices.len() == nums.len() - result as int,\n forall |indices: Seq, peak: int| Self::is_mountain_subseq(nums@, indices, peak)\n ==> indices.len() <= nums.len() - result as int,\n {\n let n = nums.len();\n\n let mut lis: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n {\n lis.push(1i32);\n idx = idx + 1;\n }\n\n let mut i: usize = 1;\n while i < n {\n let mut j: usize = 0;\n while j < i {\n if nums[j] < nums[i] {\n if lis[j] + 1 > lis[i] {\n lis.set(i, lis[j] + 1);\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n\n let mut lds: Vec = Vec::new();\n idx = 0;\n while idx < n {\n lds.push(1i32);\n idx = idx + 1;\n }\n\n let mut k: usize = 1;\n while k < n {\n let i_idx: usize = n - 1 - k;\n let mut j: usize = i_idx + 1;\n while j < n {\n if nums[j] < nums[i_idx] {\n if lds[j] + 1 > lds[i_idx] {\n lds.set(i_idx, lds[j] + 1);\n }\n }\n j = j + 1;\n }\n k = k + 1;\n }\n\n let mut result: i32 = n as i32;\n let mut i2: usize = 0;\n while i2 < n {\n if lis[i2] > 1 && lds[i2] > 1 {\n let mountain_len: i32 = lis[i2] + lds[i2] - 1;\n let removals: i32 = n as i32 - mountain_len;\n if removals < result {\n result = removals;\n }\n }\n i2 = i2 + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\npub open spec fn is_inc_subseq_ending_at(nums: Seq, indices: Seq, i: int) -> bool {\n &&& indices.len() >= 1\n &&& indices[indices.len() - 1] == i\n &&& (forall |k: int| 0 <= k < indices.len() ==> 0 <= (#[trigger] indices[k]) < nums.len())\n &&& (forall |k: int| 0 <= k < indices.len() - 1 ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) < nums[indices[k + 1]])\n}\n\n\npub open spec fn is_dec_subseq_starting_at(nums: Seq, indices: Seq, i: int) -> bool {\n &&& indices.len() >= 1\n &&& indices[0] == i\n &&& (forall |k: int| 0 <= k < indices.len() ==> 0 <= (#[trigger] indices[k]) < nums.len())\n &&& (forall |k: int| 0 <= k < indices.len() - 1 ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) > nums[indices[k + 1]])\n}\n\nimpl Solution {\n pub open spec fn is_mountain_subseq(nums: Seq, indices: Seq, peak: int) -> bool {\n &&& indices.len() >= 3\n &&& 0 < peak < indices.len() - 1\n &&& (forall |k: int| 0 <= k < indices.len() ==> 0 <= (#[trigger] indices[k]) < nums.len())\n &&& (forall |k: int| 0 <= k < peak ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) < nums[indices[k + 1]])\n &&& (forall |k: int| peak <= k < indices.len() - 1 ==>\n indices[k] < indices[k + 1] && (#[trigger] nums[indices[k]]) > nums[indices[k + 1]])\n }\n\n \n pub open spec fn lis_max_scan(nums: Seq, idx: int, bound: int) -> int\n decreases idx, bound\n {\n if bound <= 0 { 0 }\n else {\n let rest = Self::lis_max_scan(nums, idx, bound - 1);\n if bound - 1 < idx && nums[bound - 1] < nums[idx] {\n let v = Self::lis_at(nums, bound - 1);\n if v > rest { v } else { rest }\n } else {\n rest\n }\n }\n }\n\n pub open spec fn lis_at(nums: Seq, i: int) -> int\n decreases i, i + 1\n {\n if i <= 0 { 1 }\n else {\n 1 + Self::lis_max_scan(nums, i, i)\n }\n }\n\n pub open spec fn lds_max_scan(nums: Seq, idx: int, bound: int) -> int\n decreases nums.len() - idx, bound - idx\n {\n if bound <= idx || bound >= nums.len() as int { 0 }\n else {\n let rest = Self::lds_max_scan(nums, idx, bound - 1);\n if nums[bound] < nums[idx] {\n let v = Self::lds_at(nums, bound);\n if v > rest { v } else { rest }\n } else {\n rest\n }\n }\n }\n\n pub open spec fn lds_at(nums: Seq, i: int) -> int\n decreases nums.len() - i, nums.len() - i + 1\n {\n if i < 0 || i >= nums.len() as int - 1 { 1 }\n else {\n 1 + Self::lds_max_scan(nums, i, nums.len() as int - 1)\n }\n }\n\n pub open spec fn mountain_len_at(nums: Seq, i: int) -> int {\n let lis = Self::lis_at(nums, i);\n let lds = Self::lds_at(nums, i);\n if lis > 1 && lds > 1 { lis + lds - 1 } else { 0 }\n }\n\n pub open spec fn max_mountain_len(nums: Seq, i: int) -> int\n decreases i + 1\n {\n if i < 0 { 0 }\n else {\n let cur = Self::mountain_len_at(nums, i);\n let rest = Self::max_mountain_len(nums, i - 1);\n if cur > rest { cur } else { rest }\n }\n }\n\n pub open spec fn min_removals(nums: Seq) -> int {\n nums.len() as int - Self::max_mountain_len(nums, nums.len() as int - 1)\n }\n\n \n\n proof fn lemma_lis_max_scan_nonneg(nums: Seq, idx: int, bound: int)\n ensures Self::lis_max_scan(nums, idx, bound) >= 0,\n decreases idx, bound,\n {\n reveal_with_fuel(Solution::lis_max_scan, 2);\n if bound > 0 {\n Self::lemma_lis_max_scan_nonneg(nums, idx, bound - 1);\n if bound - 1 < idx && bound - 1 >= 0 && (bound - 1) < nums.len()\n && idx < nums.len() && nums[bound - 1] < nums[idx] {\n Self::lemma_lis_at_pos(nums, bound - 1);\n }\n }\n }\n\n proof fn lemma_lis_at_pos(nums: Seq, i: int)\n requires 0 <= i < nums.len(),\n ensures Self::lis_at(nums, i) >= 1,\n decreases i, i + 1,\n {\n reveal_with_fuel(Solution::lis_at, 2);\n if i > 0 {\n Self::lemma_lis_max_scan_nonneg(nums, i, i);\n }\n }\n\n proof fn lemma_lis_max_scan_bound(nums: Seq, idx: int, bound: int)\n requires 0 <= idx < nums.len(), 0 <= bound <= idx,\n ensures Self::lis_max_scan(nums, idx, bound) <= idx,\n decreases idx, bound,\n {\n reveal_with_fuel(Solution::lis_max_scan, 2);\n if bound > 0 {\n Self::lemma_lis_max_scan_bound(nums, idx, bound - 1);\n if bound - 1 < idx && nums[bound - 1] < nums[idx] {\n Self::lemma_lis_at_bound(nums, bound - 1);\n }\n }\n }\n\n proof fn lemma_lis_at_bound(nums: Seq, i: int)\n requires 0 <= i < nums.len(),\n ensures Self::lis_at(nums, i) <= i + 1,\n decreases i, i + 1,\n {\n reveal_with_fuel(Solution::lis_at, 2);\n if i > 0 {\n Self::lemma_lis_max_scan_bound(nums, i, i);\n }\n }\n\n proof fn lemma_lis_max_scan_includes(nums: Seq, idx: int, bound: int, j: int)\n requires\n 0 <= j < bound,\n j < idx,\n 0 <= idx < nums.len(),\n 0 < bound <= idx,\n nums[j] < nums[idx],\n ensures\n Self::lis_max_scan(nums, idx, bound) >= Self::lis_at(nums, j),\n decreases idx, bound,\n {\n reveal_with_fuel(Solution::lis_max_scan, 2);\n if j == bound - 1 {\n Self::lemma_lis_max_scan_nonneg(nums, idx, bound - 1);\n } else {\n Self::lemma_lis_max_scan_includes(nums, idx, bound - 1, j);\n if bound - 1 < idx && nums[bound - 1] < nums[idx] {\n Self::lemma_lis_at_pos(nums, bound - 1);\n }\n }\n }\n\n proof fn lemma_lis_max_scan_witness(nums: Seq, idx: int, bound: int) -> (j: int)\n requires\n 0 <= idx < nums.len(),\n 0 <= bound <= idx,\n Self::lis_max_scan(nums, idx, bound) > 0,\n ensures\n 0 <= j < bound,\n j < idx,\n nums[j] < nums[idx],\n Self::lis_at(nums, j) == Self::lis_max_scan(nums, idx, bound),\n decreases idx, bound,\n {\n reveal_with_fuel(Solution::lis_max_scan, 2);\n let rest = Self::lis_max_scan(nums, idx, bound - 1);\n if bound - 1 < idx && nums[bound - 1] < nums[idx] {\n let v = Self::lis_at(nums, bound - 1);\n if v > rest {\n bound - 1\n } else if rest > 0 {\n Self::lemma_lis_max_scan_witness(nums, idx, bound - 1)\n } else {\n bound - 1\n }\n } else {\n Self::lemma_lis_max_scan_witness(nums, idx, bound - 1)\n }\n }\n\n proof fn lemma_extend_inc_subseq(nums: Seq, w: Seq, new_idx: int)\n requires\n w.len() >= 1,\n is_inc_subseq_ending_at(nums, w, w[w.len() - 1]),\n 0 <= new_idx < nums.len(),\n w[w.len() - 1] < new_idx,\n nums[w[w.len() - 1]] < nums[new_idx],\n ensures\n is_inc_subseq_ending_at(nums, w.push(new_idx), new_idx),\n {\n let ext = w.push(new_idx);\n assert forall |k: int| 0 <= k < ext.len() implies\n 0 <= (#[trigger] ext[k]) < nums.len() by {\n if k < w.len() as int { assert(ext[k] == w[k]); }\n else { assert(ext[k] == new_idx); }\n }\n \n assert forall |k: int| 0 <= k < ext.len() - 1 implies\n (#[trigger] ext[k]) < ext[k + 1] by {\n if k < w.len() as int - 1 {\n assert(ext[k] == w[k]);\n assert(ext[k + 1] == w[k + 1]);\n \n assert(nums[w[k]] < nums[w[k + 1]]);\n } else {\n assert(ext[k] == w[w.len() - 1]);\n assert(ext[k + 1] == new_idx);\n }\n }\n \n assert forall |k: int| 0 <= k < ext.len() - 1 implies\n (#[trigger] nums[ext[k]]) < nums[ext[k + 1]] by {\n if k < w.len() as int - 1 {\n assert(ext[k] == w[k]);\n assert(ext[k + 1] == w[k + 1]);\n assert(nums[w[k]] < nums[w[k + 1]]);\n } else {\n assert(ext[k] == w[w.len() - 1]);\n assert(ext[k + 1] == new_idx);\n }\n }\n \n assert forall |k: int| 0 <= k < ext.len() - 1 implies\n ext[k] < ext[k + 1]\n && (#[trigger] nums[ext[k]]) < nums[ext[k + 1]] by {\n assert(ext[k] < ext[k + 1]);\n assert(nums[ext[k]] < nums[ext[k + 1]]);\n }\n }\n\n proof fn lemma_lis_achievable(nums: Seq, i: int) -> (w: Seq)\n requires 0 <= i < nums.len(),\n ensures\n is_inc_subseq_ending_at(nums, w, i),\n w.len() == Self::lis_at(nums, i),\n decreases i, i + 1,\n {\n reveal_with_fuel(Solution::lis_at, 2);\n if i <= 0 {\n let w: Seq = seq![i];\n assert(is_inc_subseq_ending_at(nums, w, i));\n w\n } else {\n Self::lemma_lis_max_scan_nonneg(nums, i, i);\n let mlb = Self::lis_max_scan(nums, i, i);\n if mlb == 0 {\n let w: Seq = seq![i];\n assert(is_inc_subseq_ending_at(nums, w, i));\n w\n } else {\n let j = Self::lemma_lis_max_scan_witness(nums, i, i);\n let prev_w = Self::lemma_lis_achievable(nums, j);\n Self::lemma_extend_inc_subseq(nums, prev_w, i);\n prev_w.push(i)\n }\n }\n }\n\n proof fn lemma_lis_optimal(nums: Seq, i: int, indices: Seq)\n requires\n is_inc_subseq_ending_at(nums, indices, i),\n 0 <= i < nums.len(),\n ensures\n indices.len() <= Self::lis_at(nums, i),\n decreases indices.len(),\n {\n reveal_with_fuel(Solution::lis_at, 2);\n Self::lemma_lis_at_pos(nums, i);\n if indices.len() > 1 {\n let prev_idx = indices[indices.len() - 2];\n let prev_seq = indices.subrange(0, indices.len() - 1);\n assert(prev_seq[prev_seq.len() - 1] == prev_idx);\n assert forall |k: int| 0 <= k < prev_seq.len() implies\n 0 <= (#[trigger] prev_seq[k]) < nums.len() by {\n assert(prev_seq[k] == indices[k]);\n }\n \n assert forall |k: int| 0 <= k < prev_seq.len() - 1 implies\n (#[trigger] prev_seq[k]) < prev_seq[k + 1] by {\n assert(prev_seq[k] == indices[k]);\n assert(prev_seq[k + 1] == indices[k + 1]);\n assert(nums[indices[k]] < nums[indices[k + 1]]);\n }\n assert forall |k: int| 0 <= k < prev_seq.len() - 1 implies\n (#[trigger] nums[prev_seq[k]]) < nums[prev_seq[k + 1]] by {\n assert(prev_seq[k] == indices[k]);\n assert(prev_seq[k + 1] == indices[k + 1]);\n }\n assert forall |k: int| 0 <= k < prev_seq.len() - 1 implies\n prev_seq[k] < prev_seq[k + 1]\n && (#[trigger] nums[prev_seq[k]]) < nums[prev_seq[k + 1]] by {\n assert(prev_seq[k] < prev_seq[k + 1]);\n assert(nums[prev_seq[k]] < nums[prev_seq[k + 1]]);\n }\n assert(is_inc_subseq_ending_at(nums, prev_seq, prev_idx));\n Self::lemma_lis_optimal(nums, prev_idx, prev_seq);\n assert(indices[indices.len() - 2] < indices[indices.len() - 1]);\n assert(nums[indices[indices.len() - 2]] < nums[indices[indices.len() - 1]]);\n Self::lemma_lis_max_scan_includes(nums, i, i, prev_idx);\n }\n }\n\n \n\n proof fn lemma_lds_max_scan_nonneg(nums: Seq, idx: int, bound: int)\n requires 0 <= idx < nums.len(),\n ensures Self::lds_max_scan(nums, idx, bound) >= 0,\n decreases nums.len() - idx, bound - idx,\n {\n reveal_with_fuel(Solution::lds_max_scan, 2);\n if bound > idx && bound < nums.len() as int {\n Self::lemma_lds_max_scan_nonneg(nums, idx, bound - 1);\n if nums[bound] < nums[idx] {\n Self::lemma_lds_at_pos(nums, bound);\n }\n }\n }\n\n proof fn lemma_lds_at_pos(nums: Seq, i: int)\n requires 0 <= i < nums.len(),\n ensures Self::lds_at(nums, i) >= 1,\n decreases nums.len() - i, nums.len() - i + 1,\n {\n reveal_with_fuel(Solution::lds_at, 2);\n if i < nums.len() as int - 1 {\n Self::lemma_lds_max_scan_nonneg(nums, i, nums.len() as int - 1);\n }\n }\n\n proof fn lemma_lds_max_scan_bound(nums: Seq, idx: int, bound: int)\n requires 0 <= idx < nums.len(), idx < bound, bound < nums.len() as int,\n ensures Self::lds_max_scan(nums, idx, bound) <= nums.len() - 1 - idx,\n decreases nums.len() - idx, bound - idx,\n {\n reveal_with_fuel(Solution::lds_max_scan, 2);\n if bound > idx && bound < nums.len() as int {\n if bound - 1 > idx {\n Self::lemma_lds_max_scan_bound(nums, idx, bound - 1);\n }\n if nums[bound] < nums[idx] {\n Self::lemma_lds_at_bound(nums, bound);\n }\n }\n }\n\n proof fn lemma_lds_at_bound(nums: Seq, i: int)\n requires 0 <= i < nums.len(),\n ensures Self::lds_at(nums, i) <= nums.len() - i,\n decreases nums.len() - i, nums.len() - i + 1,\n {\n reveal_with_fuel(Solution::lds_at, 2);\n if i < nums.len() as int - 1 {\n Self::lemma_lds_max_scan_bound(nums, i, nums.len() as int - 1);\n }\n }\n\n proof fn lemma_lds_max_scan_includes(nums: Seq, idx: int, bound: int, j: int)\n requires\n idx < j,\n j <= bound,\n 0 <= idx < nums.len(),\n bound < nums.len() as int,\n nums[j] < nums[idx],\n ensures\n Self::lds_max_scan(nums, idx, bound) >= Self::lds_at(nums, j),\n decreases nums.len() - idx, bound - idx,\n {\n reveal_with_fuel(Solution::lds_max_scan, 2);\n if j == bound {\n if bound - 1 > idx {\n Self::lemma_lds_max_scan_nonneg(nums, idx, bound - 1);\n }\n } else {\n Self::lemma_lds_max_scan_includes(nums, idx, bound - 1, j);\n if nums[bound] < nums[idx] {\n Self::lemma_lds_at_pos(nums, bound);\n }\n }\n }\n\n proof fn lemma_lds_max_scan_witness(nums: Seq, idx: int, bound: int) -> (j: int)\n requires\n 0 <= idx < nums.len(),\n idx < bound,\n bound < nums.len() as int,\n Self::lds_max_scan(nums, idx, bound) > 0,\n ensures\n idx < j <= bound,\n j < nums.len(),\n nums[j] < nums[idx],\n Self::lds_at(nums, j) == Self::lds_max_scan(nums, idx, bound),\n decreases nums.len() - idx, bound - idx,\n {\n reveal_with_fuel(Solution::lds_max_scan, 2);\n let rest = if bound - 1 > idx { Self::lds_max_scan(nums, idx, bound - 1) } else { 0 };\n if nums[bound] < nums[idx] {\n let v = Self::lds_at(nums, bound);\n if v > rest {\n bound\n } else if rest > 0 && bound - 1 > idx {\n Self::lemma_lds_max_scan_witness(nums, idx, bound - 1)\n } else {\n bound\n }\n } else {\n Self::lemma_lds_max_scan_witness(nums, idx, bound - 1)\n }\n }\n\n proof fn lemma_prepend_dec_subseq(nums: Seq, w: Seq, new_idx: int)\n requires\n w.len() >= 1,\n is_dec_subseq_starting_at(nums, w, w[0]),\n 0 <= new_idx < nums.len(),\n new_idx < w[0],\n nums[new_idx] > nums[w[0]],\n ensures\n is_dec_subseq_starting_at(nums, seq![new_idx] + w, new_idx),\n {\n let ext = seq![new_idx] + w;\n assert forall |k: int| 0 <= k < ext.len() implies\n 0 <= (#[trigger] ext[k]) < nums.len() by {\n if k == 0 { assert(ext[k] == new_idx); }\n else { assert(ext[k] == w[k - 1]); }\n }\n assert forall |k: int| 0 <= k < ext.len() - 1 implies\n (#[trigger] ext[k]) < ext[k + 1] by {\n if k == 0 {\n assert(ext[0] == new_idx);\n assert(ext[1] == w[0]);\n } else {\n assert(ext[k] == w[k - 1]);\n assert(ext[k + 1] == w[k]);\n assert(nums[w[k - 1]] > nums[w[k]]);\n }\n }\n assert forall |k: int| 0 <= k < ext.len() - 1 implies\n (#[trigger] nums[ext[k]]) > nums[ext[k + 1]] by {\n if k == 0 {\n assert(ext[0] == new_idx);\n assert(ext[1] == w[0]);\n } else {\n assert(ext[k] == w[k - 1]);\n assert(ext[k + 1] == w[k]);\n }\n }\n assert forall |k: int| 0 <= k < ext.len() - 1 implies\n ext[k] < ext[k + 1]\n && (#[trigger] nums[ext[k]]) > nums[ext[k + 1]] by {\n assert(ext[k] < ext[k + 1]);\n assert(nums[ext[k]] > nums[ext[k + 1]]);\n }\n }\n\n proof fn lemma_lds_achievable(nums: Seq, i: int) -> (w: Seq)\n requires 0 <= i < nums.len(),\n ensures\n is_dec_subseq_starting_at(nums, w, i),\n w.len() == Self::lds_at(nums, i),\n decreases nums.len() - i, nums.len() - i + 1,\n {\n reveal_with_fuel(Solution::lds_at, 2);\n if i >= nums.len() as int - 1 {\n let w: Seq = seq![i];\n assert(is_dec_subseq_starting_at(nums, w, i));\n w\n } else {\n Self::lemma_lds_max_scan_nonneg(nums, i, nums.len() as int - 1);\n let mds = Self::lds_max_scan(nums, i, nums.len() as int - 1);\n if mds == 0 {\n let w: Seq = seq![i];\n assert(is_dec_subseq_starting_at(nums, w, i));\n w\n } else {\n let j = Self::lemma_lds_max_scan_witness(nums, i, nums.len() as int - 1);\n let next_w = Self::lemma_lds_achievable(nums, j);\n Self::lemma_prepend_dec_subseq(nums, next_w, i);\n seq![i] + next_w\n }\n }\n }\n\n proof fn lemma_lds_optimal(nums: Seq, i: int, indices: Seq)\n requires\n is_dec_subseq_starting_at(nums, indices, i),\n 0 <= i < nums.len(),\n ensures\n indices.len() <= Self::lds_at(nums, i),\n decreases indices.len(),\n {\n reveal_with_fuel(Solution::lds_at, 2);\n Self::lemma_lds_at_pos(nums, i);\n if indices.len() > 1 {\n let next_idx = indices[1];\n let rest_seq = indices.subrange(1, indices.len() as int);\n assert(rest_seq[0] == next_idx);\n assert forall |k: int| 0 <= k < rest_seq.len() implies\n 0 <= (#[trigger] rest_seq[k]) < nums.len() by {\n assert(rest_seq[k] == indices[k + 1]);\n }\n assert forall |k: int| 0 <= k < rest_seq.len() - 1 implies\n (#[trigger] rest_seq[k]) < rest_seq[k + 1] by {\n assert(rest_seq[k] == indices[k + 1]);\n assert(rest_seq[k + 1] == indices[k + 2]);\n assert(nums[indices[k + 1]] > nums[indices[k + 2]]);\n }\n assert forall |k: int| 0 <= k < rest_seq.len() - 1 implies\n (#[trigger] nums[rest_seq[k]]) > nums[rest_seq[k + 1]] by {\n assert(rest_seq[k] == indices[k + 1]);\n assert(rest_seq[k + 1] == indices[k + 2]);\n }\n assert forall |k: int| 0 <= k < rest_seq.len() - 1 implies\n rest_seq[k] < rest_seq[k + 1]\n && (#[trigger] nums[rest_seq[k]]) > nums[rest_seq[k + 1]] by {\n assert(rest_seq[k] < rest_seq[k + 1]);\n assert(nums[rest_seq[k]] > nums[rest_seq[k + 1]]);\n }\n assert(is_dec_subseq_starting_at(nums, rest_seq, next_idx));\n Self::lemma_lds_optimal(nums, next_idx, rest_seq);\n assert(indices[0] < indices[1]);\n assert(nums[indices[0]] > nums[indices[1]]);\n Self::lemma_lds_max_scan_includes(nums, i, nums.len() as int - 1, next_idx);\n }\n }\n\n \n\n proof fn lemma_max_mountain_len_nonneg(nums: Seq, i: int)\n ensures Self::max_mountain_len(nums, i) >= 0,\n decreases i + 1,\n {\n reveal_with_fuel(Solution::max_mountain_len, 2);\n if i >= 0 {\n Self::lemma_max_mountain_len_nonneg(nums, i - 1);\n }\n }\n\n proof fn lemma_max_mountain_len_witness(nums: Seq, i: int) -> (p: int)\n requires\n Self::max_mountain_len(nums, i) > 0,\n 0 <= i < nums.len(),\n ensures\n 0 <= p <= i,\n Self::mountain_len_at(nums, p) == Self::max_mountain_len(nums, i),\n decreases i + 1,\n {\n reveal_with_fuel(Solution::max_mountain_len, 2);\n let cur = Self::mountain_len_at(nums, i);\n let rest = Self::max_mountain_len(nums, i - 1);\n Self::lemma_max_mountain_len_nonneg(nums, i - 1);\n if cur >= rest {\n if cur > 0 {\n i\n } else {\n \n assert(Self::max_mountain_len(nums, i) == cur);\n i\n }\n } else {\n \n if i > 0 {\n Self::lemma_max_mountain_len_witness(nums, i - 1)\n } else {\n \n \n i\n }\n }\n }\n\n proof fn lemma_max_mountain_len_includes(nums: Seq, i: int, p: int)\n requires 0 <= p <= i,\n ensures Self::max_mountain_len(nums, i) >= Self::mountain_len_at(nums, p),\n decreases i + 1,\n {\n reveal_with_fuel(Solution::max_mountain_len, 2);\n if p == i {\n Self::lemma_max_mountain_len_nonneg(nums, i - 1);\n } else if i >= 0 {\n Self::lemma_max_mountain_len_includes(nums, i - 1, p);\n }\n }\n\n proof fn lemma_mountain_achievable(nums: Seq, n: int)\n requires\n n == nums.len(),\n n >= 3,\n Self::max_mountain_len(nums, n - 1) >= 3,\n ensures\n exists |indices: Seq, peak: int| Self::is_mountain_subseq(nums, indices, peak)\n && indices.len() == Self::max_mountain_len(nums, n - 1),\n {\n let p = Self::lemma_max_mountain_len_witness(nums, n - 1);\n let lis_len = Self::lis_at(nums, p);\n let lds_len = Self::lds_at(nums, p);\n assert(lis_len > 1 && lds_len > 1);\n assert(Self::mountain_len_at(nums, p) == lis_len + lds_len - 1);\n\n let inc_w = Self::lemma_lis_achievable(nums, p);\n let dec_w = Self::lemma_lds_achievable(nums, p);\n\n \n let dec_tail = dec_w.subrange(1, dec_w.len() as int);\n let mountain = inc_w + dec_tail;\n let peak_pos = (inc_w.len() - 1) as int;\n\n assert(mountain.len() == lis_len + lds_len - 1);\n\n assert forall |k: int| 0 <= k < mountain.len() implies\n 0 <= (#[trigger] mountain[k]) < nums.len() by {\n if k < inc_w.len() as int {\n assert(mountain[k] == inc_w[k]);\n } else {\n let dk = k - inc_w.len() as int;\n assert(mountain[k] == dec_tail[dk]);\n assert(dec_tail[dk] == dec_w[dk + 1]);\n }\n }\n\n \n assert forall |k: int| 0 <= k < peak_pos implies\n (#[trigger] mountain[k]) < mountain[k + 1] by {\n assert(mountain[k] == inc_w[k]);\n assert(mountain[k + 1] == inc_w[k + 1]);\n assert(nums[inc_w[k]] < nums[inc_w[k + 1]]);\n }\n assert forall |k: int| 0 <= k < peak_pos implies\n (#[trigger] nums[mountain[k]]) < nums[mountain[k + 1]] by {\n assert(mountain[k] == inc_w[k]);\n assert(mountain[k + 1] == inc_w[k + 1]);\n }\n assert forall |k: int| 0 <= k < peak_pos implies\n mountain[k] < mountain[k + 1]\n && (#[trigger] nums[mountain[k]]) < nums[mountain[k + 1]] by {\n assert(mountain[k] < mountain[k + 1]);\n assert(nums[mountain[k]] < nums[mountain[k + 1]]);\n }\n\n \n assert forall |k: int| peak_pos <= k < mountain.len() - 1 implies\n (#[trigger] mountain[k]) < mountain[k + 1] by {\n if k == peak_pos {\n assert(mountain[k] == inc_w[inc_w.len() - 1]);\n assert(inc_w[inc_w.len() - 1] == p);\n assert(mountain[k + 1] == dec_tail[0]);\n assert(dec_tail[0] == dec_w[1]);\n assert(dec_w[0] == p);\n assert(nums[dec_w[0]] > nums[dec_w[1]]);\n } else {\n let dk = k - inc_w.len() as int;\n assert(mountain[k] == dec_tail[dk]);\n assert(mountain[k + 1] == dec_tail[dk + 1]);\n assert(dec_tail[dk] == dec_w[dk + 1]);\n assert(dec_tail[dk + 1] == dec_w[dk + 2]);\n assert(nums[dec_w[dk + 1]] > nums[dec_w[dk + 2]]);\n }\n }\n assert forall |k: int| peak_pos <= k < mountain.len() - 1 implies\n (#[trigger] nums[mountain[k]]) > nums[mountain[k + 1]] by {\n if k == peak_pos {\n assert(mountain[k] == inc_w[inc_w.len() - 1]);\n assert(inc_w[inc_w.len() - 1] == p);\n assert(mountain[k + 1] == dec_tail[0]);\n assert(dec_tail[0] == dec_w[1]);\n assert(dec_w[0] == p);\n } else {\n let dk = k - inc_w.len() as int;\n assert(mountain[k] == dec_tail[dk]);\n assert(mountain[k + 1] == dec_tail[dk + 1]);\n assert(dec_tail[dk] == dec_w[dk + 1]);\n assert(dec_tail[dk + 1] == dec_w[dk + 2]);\n }\n }\n assert forall |k: int| peak_pos <= k < mountain.len() - 1 implies\n mountain[k] < mountain[k + 1]\n && (#[trigger] nums[mountain[k]]) > nums[mountain[k + 1]] by {\n assert(mountain[k] < mountain[k + 1]);\n assert(nums[mountain[k]] > nums[mountain[k + 1]]);\n }\n\n assert(Self::is_mountain_subseq(nums, mountain, peak_pos));\n }\n\n proof fn lemma_mountain_optimal(nums: Seq, indices: Seq, peak: int)\n requires Self::is_mountain_subseq(nums, indices, peak),\n ensures indices.len() <= Self::max_mountain_len(nums, nums.len() as int - 1),\n {\n let peak_arr_idx = indices[peak];\n let asc = indices.subrange(0, peak + 1);\n let desc = indices.subrange(peak, indices.len() as int);\n\n \n assert(asc[asc.len() - 1] == peak_arr_idx);\n assert forall |k: int| 0 <= k < asc.len() implies\n 0 <= (#[trigger] asc[k]) < nums.len() by { assert(asc[k] == indices[k]); }\n assert forall |k: int| 0 <= k < asc.len() - 1 implies\n (#[trigger] asc[k]) < asc[k + 1] by {\n assert(asc[k] == indices[k]);\n assert(asc[k + 1] == indices[k + 1]);\n assert(nums[indices[k]] < nums[indices[k + 1]]);\n }\n assert forall |k: int| 0 <= k < asc.len() - 1 implies\n (#[trigger] nums[asc[k]]) < nums[asc[k + 1]] by {\n assert(asc[k] == indices[k]);\n assert(asc[k + 1] == indices[k + 1]);\n }\n assert forall |k: int| 0 <= k < asc.len() - 1 implies\n asc[k] < asc[k + 1]\n && (#[trigger] nums[asc[k]]) < nums[asc[k + 1]] by {\n assert(asc[k] < asc[k + 1]);\n assert(nums[asc[k]] < nums[asc[k + 1]]);\n }\n assert(is_inc_subseq_ending_at(nums, asc, peak_arr_idx));\n Self::lemma_lis_optimal(nums, peak_arr_idx, asc);\n\n \n assert(desc[0] == peak_arr_idx);\n assert forall |k: int| 0 <= k < desc.len() implies\n 0 <= (#[trigger] desc[k]) < nums.len() by { assert(desc[k] == indices[k + peak]); }\n assert forall |k: int| 0 <= k < desc.len() - 1 implies\n (#[trigger] desc[k]) < desc[k + 1] by {\n assert(desc[k] == indices[k + peak]);\n assert(desc[k + 1] == indices[k + peak + 1]);\n assert(nums[indices[k + peak]] > nums[indices[k + peak + 1]]);\n }\n assert forall |k: int| 0 <= k < desc.len() - 1 implies\n (#[trigger] nums[desc[k]]) > nums[desc[k + 1]] by {\n assert(desc[k] == indices[k + peak]);\n assert(desc[k + 1] == indices[k + peak + 1]);\n }\n assert forall |k: int| 0 <= k < desc.len() - 1 implies\n desc[k] < desc[k + 1]\n && (#[trigger] nums[desc[k]]) > nums[desc[k + 1]] by {\n assert(desc[k] < desc[k + 1]);\n assert(nums[desc[k]] > nums[desc[k + 1]]);\n }\n assert(is_dec_subseq_starting_at(nums, desc, peak_arr_idx));\n Self::lemma_lds_optimal(nums, peak_arr_idx, desc);\n\n assert(indices.len() == asc.len() + desc.len() - 1);\n Self::lemma_max_mountain_len_includes(nums, nums.len() as int - 1, peak_arr_idx);\n }\n\n proof fn lemma_precondition_implies_mountain_exists(nums: Seq, a: int, b: int, c: int)\n requires\n 0 <= a < b < c < nums.len(),\n nums[a] < nums[b],\n nums[b] > nums[c],\n ensures Self::max_mountain_len(nums, nums.len() as int - 1) >= 3,\n {\n Self::lemma_lis_at_pos(nums, a);\n Self::lemma_lis_at_pos(nums, b);\n Self::lemma_lds_at_pos(nums, b);\n Self::lemma_lds_at_pos(nums, c);\n Self::lemma_lis_max_scan_includes(nums, b, b, a);\n \n \n reveal_with_fuel(Solution::lis_at, 2);\n assert(Self::lis_at(nums, b) >= 2);\n Self::lemma_lds_max_scan_includes(nums, b, nums.len() as int - 1, c);\n \n \n reveal_with_fuel(Solution::lds_at, 2);\n assert(Self::lds_at(nums, b) >= 2);\n assert(Self::mountain_len_at(nums, b) >= 3);\n Self::lemma_max_mountain_len_includes(nums, nums.len() as int - 1, b);\n }\n\n pub fn minimum_mountain_removals(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000i32,\n exists |a: int, b: int, c: int| 0 <= a < b < c < nums.len() as int\n && nums[a] < nums[b] && nums[b] > nums[c],\n ensures\n result >= 0,\n exists |indices: Seq, peak: int| Self::is_mountain_subseq(nums@, indices, peak)\n && indices.len() == nums.len() - result as int,\n forall |indices: Seq, peak: int| Self::is_mountain_subseq(nums@, indices, peak)\n ==> indices.len() <= nums.len() - result as int,\n {\n let n = nums.len();\n\n let mut lis: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n\n invariant\n 0 <= idx <= n,\n lis.len() == idx,\n forall |k: int| 0 <= k < idx as int ==> lis[k] == 1i32,\n decreases n - idx,\n {\n lis.push(1i32);\n idx = idx + 1;\n }\n\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= i <= n,\n lis.len() == n,\n n == nums.len(),\n 3 <= n <= 1000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 1_000_000_000i32,\n forall |k: int| 0 <= k < i as int ==> lis[k] as int == Self::lis_at(nums@, k),\n forall |k: int| i as int <= k < n as int ==> lis[k] == 1i32,\n forall |k: int| 0 <= k < i as int ==> 1 <= #[trigger] lis[k] as int <= k + 1,\n decreases n - i,\n {\n let mut j: usize = 0;\n while j < i\n invariant\n 0 <= j <= i,\n 1 <= i < n,\n lis.len() == n,\n n == nums.len(),\n 3 <= n <= 1000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 1_000_000_000i32,\n forall |k: int| 0 <= k < i as int ==> lis[k] as int == Self::lis_at(nums@, k),\n forall |k: int| i as int + 1 <= k && k < n as int ==> lis[k] == 1i32,\n forall |k: int| 0 <= k < i as int ==> 1 <= #[trigger] lis[k] as int <= k + 1,\n lis[i as int] as int - 1 == Self::lis_max_scan(nums@, i as int, j as int),\n 1 <= lis[i as int] as int <= i as int + 1,\n decreases i - j,\n {\n if nums[j] < nums[i] {\n if lis[j] + 1 > lis[i] {\n lis.set(i, lis[j] + 1);\n }\n }\n proof {\n reveal_with_fuel(Solution::lis_max_scan, 2);\n }\n j = j + 1;\n }\n proof {\n reveal_with_fuel(Solution::lis_at, 2);\n }\n i = i + 1;\n }\n\n let mut lds: Vec = Vec::new();\n idx = 0;\n while idx < n\n invariant\n 0 <= idx <= n,\n lds.len() == idx,\n forall |k: int| 0 <= k < idx as int ==> lds[k] == 1i32,\n decreases n - idx,\n {\n lds.push(1i32);\n idx = idx + 1;\n }\n\n let mut k: usize = 1;\n while k < n\n invariant\n 1 <= k <= n,\n lds.len() == n,\n lis.len() == n,\n n == nums.len(),\n 3 <= n <= 1000,\n forall |m: int| 0 <= m < nums.len() ==> 1 <= #[trigger] nums[m] <= 1_000_000_000i32,\n forall |m: int| (n - k) as int <= m < n as int ==> lds[m] as int == Self::lds_at(nums@, m),\n forall |m: int| 0 <= m < (n - k) as int ==> lds[m] == 1i32,\n forall |m: int| (n - k) as int <= m < n as int ==> 1 <= #[trigger] lds[m] as int <= n as int - m,\n forall |m: int| 0 <= m < n as int ==> lis[m] as int == Self::lis_at(nums@, m),\n forall |m: int| 0 <= m < n as int ==> 1 <= #[trigger] lis[m] as int <= m + 1,\n decreases n - k,\n {\n let i_idx: usize = n - 1 - k;\n let mut j: usize = i_idx + 1;\n while j < n\n invariant\n i_idx + 1 <= j <= n,\n i_idx == n - 1 - k,\n i_idx < n - 1,\n 1 <= k < n,\n lds.len() == n,\n n == nums.len(),\n 3 <= n <= 1000,\n forall |m: int| 0 <= m < nums.len() ==> 1 <= #[trigger] nums[m] <= 1_000_000_000i32,\n forall |m: int| i_idx as int + 1 <= m && m < n as int ==> lds[m] as int == Self::lds_at(nums@, m),\n forall |m: int| 0 <= m < i_idx as int ==> lds[m] == 1i32,\n forall |m: int| i_idx as int + 1 <= m && m < n as int ==> 1 <= #[trigger] lds[m] as int <= n as int - m,\n lds[i_idx as int] as int - 1 == Self::lds_max_scan(nums@, i_idx as int, (j - 1) as int),\n 1 <= lds[i_idx as int] as int <= n as int - i_idx as int,\n decreases n - j,\n {\n if nums[j] < nums[i_idx] {\n if lds[j] + 1 > lds[i_idx] {\n lds.set(i_idx, lds[j] + 1);\n }\n }\n proof {\n reveal_with_fuel(Solution::lds_max_scan, 2);\n }\n j = j + 1;\n }\n proof {\n reveal_with_fuel(Solution::lds_at, 2);\n }\n k = k + 1;\n }\n\n let mut result: i32 = n as i32;\n let mut i2: usize = 0;\n while i2 < n\n invariant\n 0 <= i2 <= n,\n lis.len() == n,\n lds.len() == n,\n n == nums.len(),\n 3 <= n <= 1000,\n forall |m: int| 0 <= m < n as int ==> lis[m] as int == Self::lis_at(nums@, m),\n forall |m: int| 0 <= m < n as int ==> lds[m] as int == Self::lds_at(nums@, m),\n forall |m: int| 0 <= m < n as int ==> 1 <= #[trigger] lis[m] as int <= m + 1,\n forall |m: int| 0 <= m < n as int ==> 1 <= #[trigger] lds[m] as int <= n as int - m,\n result as int == n as int - Self::max_mountain_len(nums@, i2 as int - 1),\n 0 <= result <= n as i32,\n decreases n - i2,\n {\n if lis[i2] > 1 && lds[i2] > 1 {\n let mountain_len: i32 = lis[i2] + lds[i2] - 1;\n let removals: i32 = n as i32 - mountain_len;\n if removals < result {\n result = removals;\n }\n }\n proof {\n reveal_with_fuel(Solution::max_mountain_len, 2);\n Self::lemma_max_mountain_len_nonneg(nums@, i2 as int - 1);\n }\n i2 = i2 + 1;\n }\n\n \n proof {\n assert(result as int == n as int - Self::max_mountain_len(nums@, n as int - 1));\n\n \n let (a, b, c): (int, int, int) = choose |a: int, b: int, c: int|\n 0 <= a < b < c < nums.len() as int\n && nums@[a] < nums@[b] && nums@[b] > nums@[c];\n Self::lemma_precondition_implies_mountain_exists(nums@, a, b, c);\n\n \n Self::lemma_max_mountain_len_nonneg(nums@, n as int - 1);\n\n \n Self::lemma_mountain_achievable(nums@, n as int);\n\n \n assert forall |indices: Seq, peak: int|\n Self::is_mountain_subseq(nums@, indices, peak)\n implies indices.len() <= nums.len() - result as int by {\n Self::lemma_mountain_optimal(nums@, indices, peak);\n }\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1671/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1671/tests/mutated_testcases.jsonl"} {"problem": "lc1685", "family": "leetcode", "description": "# Sum of Absolute Differences in a Sorted Array\n\nYou are given an integer array `nums` sorted in **non-decreasing** order.\n\nBuild and return an integer array `result` with the same length as `nums` such that `result[i]` is equal to the **summation of absolute differences** between `nums[i]` and all the other elements in the array.\n\nIn other words, `result[i]` is equal to `sum(|nums[i] - nums[j]|)` where `0 <= j < nums.length` and `j != i` (0-indexed).\n\n## Example 1:\n\n> **Input:** nums = [2,3,5]\n> **Output:** [4,3,5]\n> **Explanation:** Assuming the arrays are 0-indexed, then\n> result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,\n> result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,\n> result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.\n\n## Example 2:\n\n> **Input:** nums = [1,4,6,8,10]\n> **Output:** [24,15,13,15,21]\n\n## Constraints:\n\n- $2 <= nums.length <= 10^5$\n- $1 <= nums[i] <= nums[i + 1] <= 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn get_sum_absolute_differences(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Prefix Sum\nAcceptance rate: 68.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_abs(x: int) -> int {\n if x >= 0 { x } else { -x }\n}\n\npub open spec fn sum_abs_diffs(nums: Seq, i: int, j: int) -> int\n decreases nums.len() - j,\n{\n if j >= nums.len() {\n 0\n } else {\n spec_abs(nums[i] as int - nums[j] as int) + sum_abs_diffs(nums, i, j + 1)\n }\n}\n\nimpl Solution {\n pub fn get_sum_absolute_differences(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==> 1 <= #[trigger] nums@[i] <= 10_000,\n forall |i: int, j: int| 0 <= i <= j < nums@.len() ==> nums@[i] <= nums@[j],\n ensures\n result@.len() == nums@.len(),\n forall |i: int| 0 <= i < result@.len() ==> #[trigger] result@[i] as int == sum_abs_diffs(nums@, i, 0),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn get_sum_absolute_differences(nums: Vec) -> Vec {\n let n = nums.len() as i32;\n let mut total_sum: i32 = 0;\n let mut i: i32 = 0;\n\n while i < n {\n total_sum = total_sum + nums[i as usize];\n i = i + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut prefix: i32 = 0;\n i = 0;\n\n while i < n {\n let left = i * nums[i as usize] - prefix;\n let suffix = total_sum - prefix - nums[i as usize];\n let right = suffix - (n - 1 - i) * nums[i as usize];\n result.push(left + right);\n prefix = prefix + nums[i as usize];\n i = i + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_abs(x: int) -> int {\n if x >= 0 { x } else { -x }\n}\n\npub open spec fn sum_abs_diffs(nums: Seq, i: int, j: int) -> int\n decreases nums.len() - j,\n{\n if j >= nums.len() {\n 0\n } else {\n spec_abs(nums[i] as int - nums[j] as int) + sum_abs_diffs(nums, i, j + 1)\n }\n}\n\nimpl Solution {\n pub fn get_sum_absolute_differences(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==> 1 <= #[trigger] nums@[i] <= 10_000,\n forall |i: int, j: int| 0 <= i <= j < nums@.len() ==> nums@[i] <= nums@[j],\n ensures\n result@.len() == nums@.len(),\n forall |i: int| 0 <= i < result@.len() ==> #[trigger] result@[i] as int == sum_abs_diffs(nums@, i, 0),\n {\n let n = nums.len() as i32;\n let mut total_sum: i32 = 0;\n let mut i: i32 = 0;\n\n while i < n {\n total_sum = total_sum + nums[i as usize];\n i = i + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut prefix: i32 = 0;\n i = 0;\n\n while i < n {\n let left = i * nums[i as usize] - prefix;\n let suffix = total_sum - prefix - nums[i as usize];\n let right = suffix - (n - 1 - i) * nums[i as usize];\n result.push(left + right);\n prefix = prefix + nums[i as usize];\n i = i + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_abs(x: int) -> int {\n if x >= 0 { x } else { -x }\n}\n\npub open spec fn sum_abs_diffs(nums: Seq, i: int, j: int) -> int\n decreases nums.len() - j,\n{\n if j >= nums.len() {\n 0\n } else {\n spec_abs(nums[i] as int - nums[j] as int) + sum_abs_diffs(nums, i, j + 1)\n }\n}\n\npub open spec fn spec_sum(nums: Seq, start: int, end: int) -> int\n decreases end - start,\n{\n if start >= end {\n 0\n } else {\n nums[start] as int + spec_sum(nums, start + 1, end)\n }\n}\n\nproof fn spec_sum_split(nums: Seq, lo: int, mid: int, hi: int)\n requires\n 0 <= lo <= mid <= hi <= nums.len(),\n ensures\n spec_sum(nums, lo, hi) == spec_sum(nums, lo, mid) + spec_sum(nums, mid, hi),\n decreases mid - lo,\n{\n if lo < mid {\n spec_sum_split(nums, lo + 1, mid, hi);\n }\n}\n\nproof fn spec_sum_upper_bound(nums: Seq, lo: int, hi: int, ub: int)\n requires\n 0 <= lo <= hi <= nums.len(),\n forall |k: int| lo <= k < hi ==> #[trigger] nums[k] as int <= ub,\n ensures\n spec_sum(nums, lo, hi) <= (hi - lo) * ub,\n decreases hi - lo,\n{\n if lo < hi {\n spec_sum_upper_bound(nums, lo + 1, hi, ub);\n assert(spec_sum(nums, lo, hi) == nums[lo] as int + spec_sum(nums, lo + 1, hi));\n assert(nums[lo] as int <= ub);\n assert(spec_sum(nums, lo + 1, hi) <= (hi - lo - 1) * ub);\n assert(spec_sum(nums, lo, hi) <= (hi - lo) * ub) by (nonlinear_arith)\n requires\n spec_sum(nums, lo, hi) == nums[lo] as int + spec_sum(nums, lo + 1, hi),\n nums[lo] as int <= ub,\n spec_sum(nums, lo + 1, hi) <= (hi - lo - 1) * ub;\n } else {\n assert(spec_sum(nums, lo, hi) == 0int);\n assert(hi - lo == 0int);\n }\n}\n\nproof fn spec_sum_lower_bound(nums: Seq, lo: int, hi: int, lb: int)\n requires\n 0 <= lo <= hi <= nums.len(),\n forall |k: int| lo <= k < hi ==> #[trigger] nums[k] as int >= lb,\n ensures\n spec_sum(nums, lo, hi) >= (hi - lo) * lb,\n decreases hi - lo,\n{\n if lo < hi {\n spec_sum_lower_bound(nums, lo + 1, hi, lb);\n assert(spec_sum(nums, lo, hi) == nums[lo] as int + spec_sum(nums, lo + 1, hi));\n assert(nums[lo] as int >= lb);\n assert(spec_sum(nums, lo + 1, hi) >= (hi - lo - 1) * lb);\n assert(spec_sum(nums, lo, hi) >= (hi - lo) * lb) by (nonlinear_arith)\n requires\n spec_sum(nums, lo, hi) == nums[lo] as int + spec_sum(nums, lo + 1, hi),\n nums[lo] as int >= lb,\n spec_sum(nums, lo + 1, hi) >= (hi - lo - 1) * lb;\n } else {\n assert(spec_sum(nums, lo, hi) == 0int);\n assert(hi - lo == 0int);\n }\n}\n\nproof fn sad_left(nums: Seq, idx: int, j: int)\n requires\n 0 <= j <= idx < nums.len(),\n forall |k: int, l: int| 0 <= k <= l < nums.len() ==> #[trigger] nums[k] <= #[trigger] nums[l],\n ensures\n sum_abs_diffs(nums, idx, j) ==\n (idx - j) * (nums[idx] as int) - spec_sum(nums, j, idx) + sum_abs_diffs(nums, idx, idx),\n decreases idx - j,\n{\n if j < idx {\n sad_left(nums, idx, j + 1);\n assert(nums[j] <= nums[idx]);\n assert(spec_abs(nums[idx] as int - nums[j] as int) == nums[idx] as int - nums[j] as int);\n assert(sum_abs_diffs(nums, idx, j) ==\n spec_abs(nums[idx] as int - nums[j] as int) + sum_abs_diffs(nums, idx, j + 1));\n assert(spec_sum(nums, j, idx) == nums[j] as int + spec_sum(nums, j + 1, idx));\n assert(sum_abs_diffs(nums, idx, j) ==\n (idx - j) * (nums[idx] as int) - spec_sum(nums, j, idx) + sum_abs_diffs(nums, idx, idx))\n by (nonlinear_arith)\n requires\n sum_abs_diffs(nums, idx, j) ==\n (nums[idx] as int - nums[j] as int) + sum_abs_diffs(nums, idx, j + 1),\n sum_abs_diffs(nums, idx, j + 1) ==\n (idx - j - 1) * (nums[idx] as int) - spec_sum(nums, j + 1, idx) + sum_abs_diffs(nums, idx, idx),\n spec_sum(nums, j, idx) == nums[j] as int + spec_sum(nums, j + 1, idx);\n } else {\n assert(j == idx);\n assert(idx - j == 0int);\n assert(spec_sum(nums, j, idx) == 0int);\n assert((idx - j) * (nums[idx] as int) == 0int);\n }\n}\n\nproof fn sad_right(nums: Seq, idx: int, j: int)\n requires\n 0 <= idx,\n idx < j,\n j <= nums.len(),\n forall |k: int, l: int| 0 <= k <= l < nums.len() ==> #[trigger] nums[k] <= #[trigger] nums[l],\n ensures\n sum_abs_diffs(nums, idx, j) ==\n spec_sum(nums, j, nums.len() as int) - (nums.len() - j) * (nums[idx] as int),\n decreases nums.len() - j,\n{\n if j < nums.len() {\n sad_right(nums, idx, j + 1);\n assert(nums[idx] <= nums[j]);\n assert(spec_abs(nums[idx] as int - nums[j] as int) == nums[j] as int - nums[idx] as int);\n assert(sum_abs_diffs(nums, idx, j) ==\n spec_abs(nums[idx] as int - nums[j] as int) + sum_abs_diffs(nums, idx, j + 1));\n assert(spec_sum(nums, j, nums.len() as int) ==\n nums[j] as int + spec_sum(nums, j + 1, nums.len() as int));\n assert(sum_abs_diffs(nums, idx, j) ==\n spec_sum(nums, j, nums.len() as int) - (nums.len() - j) * (nums[idx] as int))\n by (nonlinear_arith)\n requires\n sum_abs_diffs(nums, idx, j) ==\n (nums[j] as int - nums[idx] as int) + sum_abs_diffs(nums, idx, j + 1),\n sum_abs_diffs(nums, idx, j + 1) ==\n spec_sum(nums, j + 1, nums.len() as int) - (nums.len() - j - 1) * (nums[idx] as int),\n spec_sum(nums, j, nums.len() as int) ==\n nums[j] as int + spec_sum(nums, j + 1, nums.len() as int);\n } else {\n assert(sum_abs_diffs(nums, idx, j) == 0int);\n assert(spec_sum(nums, j, nums.len() as int) == 0int);\n assert(nums.len() - j == 0int);\n }\n}\n\nproof fn sad_formula(nums: Seq, idx: int, n: int)\n requires\n n == nums.len(),\n 0 <= idx < n,\n forall |k: int, l: int| 0 <= k <= l < n ==> #[trigger] nums[k] <= #[trigger] nums[l],\n ensures\n sum_abs_diffs(nums, idx, 0) ==\n (2 * idx - n) * (nums[idx] as int)\n + spec_sum(nums, 0, n) - 2 * spec_sum(nums, 0, idx),\n{\n sad_left(nums, idx, 0);\n assert(spec_abs(nums[idx] as int - nums[idx] as int) == 0);\n assert(sum_abs_diffs(nums, idx, idx) ==\n spec_abs(nums[idx] as int - nums[idx] as int) + sum_abs_diffs(nums, idx, idx + 1));\n assert(sum_abs_diffs(nums, idx, idx) == sum_abs_diffs(nums, idx, idx + 1));\n sad_right(nums, idx, idx + 1);\n spec_sum_split(nums, 0, idx, n);\n spec_sum_split(nums, idx, idx + 1, n);\n assert(spec_sum(nums, idx, idx + 1) == nums[idx] as int + spec_sum(nums, idx + 1, idx + 1));\n assert(spec_sum(nums, idx + 1, idx + 1) == 0int);\n assert(sum_abs_diffs(nums, idx, 0) ==\n (2 * idx - n) * (nums[idx] as int)\n + spec_sum(nums, 0, n) - 2 * spec_sum(nums, 0, idx))\n by (nonlinear_arith)\n requires\n sum_abs_diffs(nums, idx, 0) ==\n idx * (nums[idx] as int) - spec_sum(nums, 0, idx)\n + sum_abs_diffs(nums, idx, idx + 1),\n sum_abs_diffs(nums, idx, idx + 1) ==\n spec_sum(nums, idx + 1, n) - (n - idx - 1) * (nums[idx] as int),\n spec_sum(nums, 0, n) == spec_sum(nums, 0, idx)\n + nums[idx] as int + spec_sum(nums, idx + 1, n);\n}\n\nproof fn sad_nonneg(nums: Seq, idx: int, j: int)\n requires\n 0 <= idx < nums.len(),\n 0 <= j,\n ensures\n sum_abs_diffs(nums, idx, j) >= 0,\n decreases nums.len() - j,\n{\n if j < nums.len() {\n sad_nonneg(nums, idx, j + 1);\n } else {\n assert(sum_abs_diffs(nums, idx, j) == 0int);\n }\n}\n\nproof fn sad_upper_bound(nums: Seq, idx: int, j: int)\n requires\n 0 <= idx < nums.len(),\n 0 <= j <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 10_000,\n ensures\n sum_abs_diffs(nums, idx, j) <= (nums.len() - j) * 9999,\n decreases nums.len() - j,\n{\n if j < nums.len() {\n sad_upper_bound(nums, idx, j + 1);\n assert(1 <= nums[idx] <= 10_000);\n assert(1 <= nums[j] <= 10_000);\n assert(spec_abs(nums[idx] as int - nums[j] as int) <= 9999);\n assert(sum_abs_diffs(nums, idx, j) ==\n spec_abs(nums[idx] as int - nums[j] as int) + sum_abs_diffs(nums, idx, j + 1));\n assert(sum_abs_diffs(nums, idx, j) <= (nums.len() - j) * 9999) by (nonlinear_arith)\n requires\n sum_abs_diffs(nums, idx, j) ==\n spec_abs(nums[idx] as int - nums[j] as int) + sum_abs_diffs(nums, idx, j + 1),\n spec_abs(nums[idx] as int - nums[j] as int) <= 9999,\n sum_abs_diffs(nums, idx, j + 1) <= (nums.len() - j - 1) * 9999;\n } else {\n assert(sum_abs_diffs(nums, idx, j) == 0int);\n assert(j == nums.len());\n }\n}\n\nimpl Solution {\n pub fn get_sum_absolute_differences(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums@.len() <= 100_000,\n forall |i: int| 0 <= i < nums@.len() ==> 1 <= #[trigger] nums@[i] <= 10_000,\n forall |i: int, j: int| 0 <= i <= j < nums@.len() ==> nums@[i] <= nums@[j],\n ensures\n result@.len() == nums@.len(),\n forall |i: int| 0 <= i < result@.len() ==> #[trigger] result@[i] as int == sum_abs_diffs(nums@, i, 0),\n {\n let n = nums.len() as i32;\n let mut total_sum: i32 = 0;\n let mut i: i32 = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 2 <= n <= 100_000,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] nums@[k] <= 10_000,\n total_sum as int == spec_sum(nums@, 0, i as int),\n 0 <= total_sum <= i * 10_000,\n decreases n - i,\n {\n proof {\n assert(1 <= nums@[i as int] <= 10_000);\n spec_sum_split(nums@, 0, i as int, (i + 1) as int);\n assert(spec_sum(nums@, i as int, (i + 1) as int) ==\n nums@[i as int] as int + spec_sum(nums@, (i + 1) as int, (i + 1) as int));\n assert(spec_sum(nums@, (i + 1) as int, (i + 1) as int) == 0int);\n spec_sum_upper_bound(nums@, 0, (i + 1) as int, 10_000);\n assert((i + 1) * 10_000 <= 1_000_000_000) by (nonlinear_arith)\n requires i + 1 <= 100_000;\n }\n total_sum = total_sum + nums[i as usize];\n i = i + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut prefix: i32 = 0;\n i = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 2 <= n <= 100_000,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] nums@[k] <= 10_000,\n forall |k: int, l: int| 0 <= k <= l < n ==> nums@[k] <= nums@[l],\n result@.len() == i as nat,\n prefix as int == spec_sum(nums@, 0, i as int),\n total_sum as int == spec_sum(nums@, 0, n as int),\n 0 <= prefix <= i * 10_000,\n 0 <= total_sum <= n * 10_000,\n forall |k: int| 0 <= k < i ==> #[trigger] result@[k] as int == sum_abs_diffs(nums@, k, 0),\n decreases n - i,\n {\n proof {\n sad_formula(nums@, i as int, n as int);\n sad_nonneg(nums@, i as int, 0);\n sad_upper_bound(nums@, i as int, 0);\n\n spec_sum_upper_bound(nums@, 0, i as int, nums@[i as int] as int);\n spec_sum_lower_bound(nums@, (i + 1) as int, n as int, nums@[i as int] as int);\n spec_sum_split(nums@, 0, i as int, n as int);\n spec_sum_split(nums@, i as int, (i + 1) as int, n as int);\n assert(spec_sum(nums@, i as int, (i + 1) as int) ==\n nums@[i as int] as int + spec_sum(nums@, (i + 1) as int, (i + 1) as int));\n assert(spec_sum(nums@, (i + 1) as int, (i + 1) as int) == 0int);\n\n assert(0 <= i * (nums@[i as int] as int) <= 999_990_000) by (nonlinear_arith)\n requires\n 0 <= i <= 99_999,\n 1 <= nums@[i as int] as int <= 10_000,\n {};\n\n assert(0 <= (n - 1 - i) * (nums@[i as int] as int) <= 999_990_000) by (nonlinear_arith)\n requires\n 0 <= n - 1 - i <= 99_999,\n 1 <= nums@[i as int] as int <= 10_000,\n {};\n\n assert(sum_abs_diffs(nums@, i as int, 0) <= 100_000 * 9999);\n }\n\n let left = i * nums[i as usize] - prefix;\n let suffix = total_sum - prefix - nums[i as usize];\n let right = suffix - (n - 1 - i) * nums[i as usize];\n\n proof {\n assert((left + right) as int == sum_abs_diffs(nums@, i as int, 0)) by (nonlinear_arith)\n requires\n left as int == i as int * (nums@[i as int] as int) - prefix as int,\n suffix as int == total_sum as int - prefix as int - nums@[i as int] as int,\n right as int == suffix as int - (n as int - 1 - i as int) * (nums@[i as int] as int),\n sum_abs_diffs(nums@, i as int, 0) ==\n (2 * (i as int) - n as int) * (nums@[i as int] as int)\n + total_sum as int - 2 * prefix as int;\n }\n\n result.push(left + right);\n\n proof {\n assert(result@[i as int] as int == sum_abs_diffs(nums@, i as int, 0));\n }\n\n proof {\n assert(1 <= nums@[i as int] <= 10_000);\n spec_sum_split(nums@, 0, i as int, (i + 1) as int);\n assert(spec_sum(nums@, i as int, (i + 1) as int) ==\n nums@[i as int] as int + spec_sum(nums@, (i + 1) as int, (i + 1) as int));\n assert(spec_sum(nums@, (i + 1) as int, (i + 1) as int) == 0int);\n spec_sum_upper_bound(nums@, 0, (i + 1) as int, 10_000);\n assert((i + 1) * 10_000 <= 1_000_000_000) by (nonlinear_arith)\n requires i + 1 <= 100_000;\n }\n prefix = prefix + nums[i as usize];\n i = i + 1;\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1685/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1685/tests/mutated_testcases.jsonl"} {"problem": "lc1690", "family": "leetcode", "description": "# Stone Game VII\n\nAlice and Bob take turns playing a game, with **Alice starting first**.\n\nThere are `n` stones arranged in a row. On each player's turn, they can **remove** either the leftmost stone or the rightmost stone from the row and receive points equal to the **sum** of the remaining stones' values in the row. The winner is the one with the higher score when there are no stones left to remove.\n\nBob found that he will always lose this game (poor Bob, he always loses), so he decided to **minimize the score's difference**. Alice's goal is to **maximize the difference** in the score.\n\nGiven an array of integers `stones` where `stones[i]` represents the value of the `i`th stone **from the left**, return *the **difference** in Alice and Bob's score if they both play **optimally**.*\n\n## Example 1:\n\n> **Input:** stones = [5,3,1,4,2]\n> **Output:** 6\n> **Explanation:**\n> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4].\n> - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4].\n> - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4].\n> - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4].\n> - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = [].\n> The score difference is 18 - 12 = 6.\n\n## Example 2:\n\n> **Input:** stones = [7,90,5,1,100,10,10,2]\n> **Output:** 122\n\n## Constraints:\n\n- `n == stones.length`\n- $2 \\leq n \\leq 1000$\n- $1 \\leq stones[i] \\leq 1000$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn stone_game_vii(stones: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Dynamic Programming, Game Theory\nAcceptance rate: 58.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_sum(stones: Seq, i: int, j: int) -> int\n decreases (if j >= i { j - i + 1 } else { 0 }),\n{\n if i > j {\n 0\n } else {\n stones[i] as int + spec_sum(stones, i + 1, j)\n }\n}\n\npub open spec fn spec_optimal_diff(stones: Seq, i: int, j: int) -> int\n decreases (if j > i { j - i } else { 0 }),\n{\n if i >= j {\n 0\n } else {\n let left = spec_sum(stones, i + 1, j) - spec_optimal_diff(stones, i + 1, j);\n let right = spec_sum(stones, i, j - 1) - spec_optimal_diff(stones, i, j - 1);\n if left >= right { left } else { right }\n }\n}\n\nimpl Solution {\n pub fn stone_game_vii(stones: Vec) -> (res: i32)\n requires\n 2 <= stones.len() <= 1000,\n forall |i: int| 0 <= i < stones.len() ==> 1 <= #[trigger] stones[i] <= 1000,\n ensures\n res as int == spec_optimal_diff(stones@, 0, stones@.len() - 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn stone_game_vii(stones: Vec) -> i32\n {\n let n = stones.len();\n let mut dp: Vec = Vec::new();\n let mut k: usize = 0;\n while k < n {\n dp.push(0i32);\n k = k + 1;\n }\n let mut i: i32 = n as i32 - 2;\n while i >= 0 {\n let mut total: i32 = stones[i as usize];\n let mut j: usize = (i + 1) as usize;\n while j < n {\n total = total + stones[j];\n let left = total - stones[i as usize] - dp[j];\n let right = total - stones[j] - dp[j - 1];\n if left >= right {\n dp[j] = left;\n } else {\n dp[j] = right;\n }\n j = j + 1;\n }\n i = i - 1;\n }\n dp[n - 1]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_sum(stones: Seq, i: int, j: int) -> int\n decreases (if j >= i { j - i + 1 } else { 0 }),\n{\n if i > j {\n 0\n } else {\n stones[i] as int + spec_sum(stones, i + 1, j)\n }\n}\n\npub open spec fn spec_optimal_diff(stones: Seq, i: int, j: int) -> int\n decreases (if j > i { j - i } else { 0 }),\n{\n if i >= j {\n 0\n } else {\n let left = spec_sum(stones, i + 1, j) - spec_optimal_diff(stones, i + 1, j);\n let right = spec_sum(stones, i, j - 1) - spec_optimal_diff(stones, i, j - 1);\n if left >= right { left } else { right }\n }\n}\n\nimpl Solution {\n pub fn stone_game_vii(stones: Vec) -> (res: i32)\n requires\n 2 <= stones.len() <= 1000,\n forall |i: int| 0 <= i < stones.len() ==> 1 <= #[trigger] stones[i] <= 1000,\n ensures\n res as int == spec_optimal_diff(stones@, 0, stones@.len() - 1),\n {\n let n = stones.len();\n let mut dp: Vec = Vec::new();\n let mut k: usize = 0;\n while k < n {\n dp.push(0i32);\n k = k + 1;\n }\n let mut i: i32 = n as i32 - 2;\n while i >= 0 {\n let mut total: i32 = stones[i as usize];\n let mut j: usize = (i + 1) as usize;\n while j < n {\n total = total + stones[j];\n let left = total - stones[i as usize] - dp[j];\n let right = total - stones[j] - dp[j - 1];\n if left >= right {\n dp.set(j, left);\n } else {\n dp.set(j, right);\n }\n j = j + 1;\n }\n i = i - 1;\n }\n dp[n - 1]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_sum(stones: Seq, i: int, j: int) -> int\n decreases (if j >= i { j - i + 1 } else { 0 }),\n{\n if i > j {\n 0\n } else {\n stones[i] as int + spec_sum(stones, i + 1, j)\n }\n}\n\npub open spec fn spec_optimal_diff(stones: Seq, i: int, j: int) -> int\n decreases (if j > i { j - i } else { 0 }),\n{\n if i >= j {\n 0\n } else {\n let left = spec_sum(stones, i + 1, j) - spec_optimal_diff(stones, i + 1, j);\n let right = spec_sum(stones, i, j - 1) - spec_optimal_diff(stones, i, j - 1);\n if left >= right { left } else { right }\n }\n}\n\nproof fn lemma_spec_sum_snoc(stones: Seq, i: int, j: int)\n requires\n 0 <= i <= j,\n j < stones.len(),\n ensures\n spec_sum(stones, i, j) == spec_sum(stones, i, j - 1) + stones[j] as int,\n decreases j - i,\n{\n if i == j {\n assert(spec_sum(stones, i + 1, j) == 0);\n assert(spec_sum(stones, i, j - 1) == 0);\n } else {\n lemma_spec_sum_snoc(stones, i + 1, j);\n }\n}\n\nproof fn lemma_spec_sum_bounds(stones: Seq, i: int, j: int)\n requires\n 0 <= i,\n j < stones.len(),\n forall |k: int| 0 <= k < stones.len() ==> 1 <= #[trigger] stones[k] <= 1000,\n ensures\n i > j ==> spec_sum(stones, i, j) == 0,\n i <= j ==> (j - i + 1) <= spec_sum(stones, i, j) <= 1000 * (j - i + 1),\n decreases (if j >= i { j - i + 1 } else { 0 }),\n{\n if i <= j {\n lemma_spec_sum_bounds(stones, i + 1, j);\n }\n}\n\nproof fn lemma_spec_optimal_diff_bounds(stones: Seq, i: int, j: int)\n requires\n 0 <= i,\n j < stones.len(),\n forall |k: int| 0 <= k < stones.len() ==> 1 <= #[trigger] stones[k] <= 1000,\n ensures\n 0 <= spec_optimal_diff(stones, i, j),\n i <= j ==> spec_optimal_diff(stones, i, j) <= spec_sum(stones, i, j),\n i > j ==> spec_optimal_diff(stones, i, j) == 0,\n decreases (if j > i { j - i } else { 0 }),\n{\n if i < j {\n lemma_spec_optimal_diff_bounds(stones, i + 1, j);\n lemma_spec_optimal_diff_bounds(stones, i, j - 1);\n lemma_spec_sum_bounds(stones, i + 1, j);\n lemma_spec_sum_bounds(stones, i, j - 1);\n lemma_spec_sum_bounds(stones, i, j);\n lemma_spec_sum_snoc(stones, i, j);\n assert(spec_sum(stones, i, j) == stones[i] as int + spec_sum(stones, i + 1, j));\n assert(spec_sum(stones, i, j) == spec_sum(stones, i, j - 1) + stones[j] as int);\n }\n}\n\nimpl Solution {\n pub fn stone_game_vii(stones: Vec) -> (res: i32)\n requires\n 2 <= stones.len() <= 1000,\n forall |i: int| 0 <= i < stones.len() ==> 1 <= #[trigger] stones[i] <= 1000,\n ensures\n res as int == spec_optimal_diff(stones@, 0, stones@.len() - 1),\n {\n let n = stones.len();\n let mut dp: Vec = Vec::new();\n let mut k: usize = 0;\n while k < n\n invariant\n dp.len() == k,\n k <= n,\n forall |idx: int| 0 <= idx < k as int ==> dp@[idx] == 0i32,\n decreases n - k,\n {\n dp.push(0i32);\n k = k + 1;\n }\n let mut i: i32 = n as i32 - 2;\n while i >= 0\n invariant\n -1 <= i <= n as i32 - 2,\n dp@.len() == n as int,\n n == stones@.len(),\n 2 <= n <= 1000,\n forall |k: int| 0 <= k < stones@.len() ==> 1 <= #[trigger] stones@[k] <= 1000,\n forall |j: int| (i + 1) as int <= j < n as int ==>\n dp@[j] as int == spec_optimal_diff(stones@, (i + 1) as int, j),\n forall |j: int| 0 <= j <= i as int ==> dp@[j] == 0i32,\n forall |j: int| 0 <= j < n as int ==> 0 <= #[trigger] dp@[j] <= 1_000_000,\n decreases i + 1,\n {\n let mut total: i32 = stones[i as usize];\n let mut j: usize = (i + 1) as usize;\n proof {\n assert(spec_sum(stones@, (i + 1) as int, i as int) == 0);\n assert(spec_optimal_diff(stones@, i as int, i as int) == 0);\n }\n while j < n\n invariant\n dp@.len() == n as int,\n n == stones@.len(),\n 2 <= n <= 1000,\n 0 <= i <= n as i32 - 2,\n (i + 1) as int <= j as int <= n as int,\n forall |k: int| 0 <= k < stones@.len() ==> 1 <= #[trigger] stones@[k] <= 1000,\n total as int == spec_sum(stones@, i as int, (j - 1) as int),\n 1 <= total <= 1_000_000,\n forall |j2: int| i as int <= j2 < j as int ==>\n #[trigger] dp@[j2] as int == spec_optimal_diff(stones@, i as int, j2),\n forall |j2: int| j as int <= j2 < n as int ==>\n dp@[j2] as int == spec_optimal_diff(stones@, (i + 1) as int, j2),\n forall |j2: int| 0 <= j2 < i as int ==> dp@[j2] == 0i32,\n forall |j2: int| 0 <= j2 < n as int ==> 0 <= #[trigger] dp@[j2] <= 1_000_000,\n decreases n - j,\n {\n proof {\n lemma_spec_sum_snoc(stones@, i as int, j as int);\n lemma_spec_sum_bounds(stones@, i as int, j as int);\n }\n total = total + stones[j];\n proof {\n lemma_spec_sum_bounds(stones@, (i + 1) as int, j as int);\n lemma_spec_sum_bounds(stones@, i as int, (j - 1) as int);\n lemma_spec_optimal_diff_bounds(stones@, (i + 1) as int, j as int);\n lemma_spec_optimal_diff_bounds(stones@, i as int, (j - 1) as int);\n }\n let left = total - stones[i as usize] - dp[j];\n let right = total - stones[j] - dp[j - 1];\n if left >= right {\n dp.set(j, left);\n } else {\n dp.set(j, right);\n }\n j = j + 1;\n }\n i = i - 1;\n }\n dp[n - 1]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1690/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1690/tests/mutated_testcases.jsonl"} {"problem": "lc1780", "family": "leetcode", "description": "# Check if Number is a Sum of Powers of Three\n\nGiven an integer `n`, return `true` if it is possible to represent `n` as the sum of distinct powers of three. Otherwise, return `false`.\n\nAn integer `y` is a power of three if there exists an integer `x` such that `y == 3^x`.\n\n## Example 1:\n\n> **Input:** n = 12\n> **Output:** true\n> **Explanation:** 12 = 3^1 + 3^2\n\n## Example 2:\n\n> **Input:** n = 91\n> **Output:** true\n> **Explanation:** 91 = 3^0 + 3^2 + 3^4\n\n## Example 3:\n\n> **Input:** n = 21\n> **Output:** false\n\n## Constraints:\n\n- $1 <= n <= 10^7$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn check_powers_of_three(n: i32) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nMath\nAcceptance rate: 79.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_sum_of_distinct_pow3(n: nat) -> bool\n decreases n,\n {\n if n == 0 {\n true\n } else {\n n % 3 != 2 && Solution::is_sum_of_distinct_pow3(n / 3)\n }\n }\n\n pub fn check_powers_of_three(n: i32) -> (result: bool)\n requires\n 1 <= n <= 10_000_000,\n ensures\n result == Solution::is_sum_of_distinct_pow3(n as nat),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn check_powers_of_three(n: i32) -> bool {\n let mut cur = n;\n while cur > 0 {\n if cur % 3 == 2 {\n return false;\n }\n cur = cur / 3;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_sum_of_distinct_pow3(n: nat) -> bool\n decreases n,\n {\n if n == 0 {\n true\n } else {\n n % 3 != 2 && Solution::is_sum_of_distinct_pow3(n / 3)\n }\n }\n\n pub fn check_powers_of_three(n: i32) -> (result: bool)\n requires\n 1 <= n <= 10_000_000,\n ensures\n result == Solution::is_sum_of_distinct_pow3(n as nat),\n {\n let mut cur = n;\n\n while cur > 0\n decreases cur,\n {\n if cur % 3 == 2 {\n return false;\n }\n cur = cur / 3;\n }\n true\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_sum_of_distinct_pow3(n: nat) -> bool\n decreases n,\n {\n if n == 0 {\n true\n } else {\n n % 3 != 2 && Solution::is_sum_of_distinct_pow3(n / 3)\n }\n }\n\n pub fn check_powers_of_three(n: i32) -> (result: bool)\n requires\n 1 <= n <= 10_000_000,\n ensures\n result == Solution::is_sum_of_distinct_pow3(n as nat),\n {\n let mut cur = n;\n\n while cur > 0\n invariant\n 0 <= cur <= n,\n Solution::is_sum_of_distinct_pow3(n as nat) == Solution::is_sum_of_distinct_pow3(cur as nat),\n decreases cur,\n {\n if cur % 3 == 2 {\n return false;\n }\n cur = cur / 3;\n }\n true\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1780/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1780/tests/mutated_testcases.jsonl"} {"problem": "lc1785", "family": "leetcode", "description": "# Minimum Elements to Add to Form a Given Sum\n\nYou are given an integer array `nums` and two integers `limit` and `goal`. The array `nums` has an interesting property that `abs(nums[i]) <= limit`.\n\nReturn the minimum number of elements you need to add to make the sum of the array equal to `goal`. The array must maintain its property that `abs(nums[i]) <= limit`.\n\nNote that `abs(x)` equals `x` if `x >= 0`, and `-x` otherwise.\n\n## Example 1:\n\n> **Input:** nums = [1,-1,1], limit = 3, goal = -4\n> **Output:** 2\n> **Explanation:** You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.\n\n## Example 2:\n\n> **Input:** nums = [1,-10,9,1], limit = 100, goal = 0\n> **Output:** 1\n\n## Constraints:\n\n- $1 \\leq \\text{nums.length} \\leq 10^5$\n- $1 \\leq \\text{limit} \\leq 10^6$\n- $-\\text{limit} \\leq \\text{nums[i]} \\leq \\text{limit}$\n- $-10^9 \\leq \\text{goal} \\leq 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_elements(nums: Vec, limit: i32, goal: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Greedy\nAcceptance rate: 45.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn seq_sum(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n seq_sum(s.subrange(0, s.len() - 1)) + s[s.len() - 1] as int\n }\n}\n\npub open spec fn abs_int(x: int) -> int {\n if x >= 0 { x } else { -x }\n}\n\npub open spec fn min_elements_spec(diff: int, limit: int) -> int {\n (diff + limit - 1) / limit\n}\n\nimpl Solution {\n pub fn min_elements(nums: Vec, limit: i32, goal: i32) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n 1 <= limit <= 1_000_000,\n forall |i: int| 0 <= i < nums@.len() ==> -limit <= (#[trigger] nums@[i]) && nums@[i] <= limit,\n -1_000_000_000 <= goal <= 1_000_000_000,\n min_elements_spec(abs_int(seq_sum(nums@) - goal as int), limit as int) <= i32::MAX as int,\n ensures\n result as int == min_elements_spec(abs_int(seq_sum(nums@) - goal as int), limit as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_elements(nums: Vec, limit: i32, goal: i32) -> i32 {\n let n = nums.len();\n let mut sum: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n sum = sum + nums[i] as i64;\n i = i + 1;\n }\n let diff: i64 = if sum >= goal as i64 { sum - goal as i64 } else { goal as i64 - sum };\n let result: i64 = (diff + limit as i64 - 1) / limit as i64;\n result as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn seq_sum(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n seq_sum(s.subrange(0, s.len() - 1)) + s[s.len() - 1] as int\n }\n}\n\npub open spec fn abs_int(x: int) -> int {\n if x >= 0 { x } else { -x }\n}\n\npub open spec fn min_elements_spec(diff: int, limit: int) -> int {\n (diff + limit - 1) / limit\n}\n\nimpl Solution {\n pub fn min_elements(nums: Vec, limit: i32, goal: i32) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n 1 <= limit <= 1_000_000,\n forall |i: int| 0 <= i < nums@.len() ==> -limit <= (#[trigger] nums@[i]) && nums@[i] <= limit,\n -1_000_000_000 <= goal <= 1_000_000_000,\n min_elements_spec(abs_int(seq_sum(nums@) - goal as int), limit as int) <= i32::MAX as int,\n ensures\n result as int == min_elements_spec(abs_int(seq_sum(nums@) - goal as int), limit as int),\n {\n let n = nums.len();\n let mut sum: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n sum = sum + nums[i] as i64;\n i = i + 1;\n }\n let diff: i64 = if sum >= goal as i64 { sum - goal as i64 } else { goal as i64 - sum };\n let result: i64 = (diff + limit as i64 - 1) / limit as i64;\n result as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn seq_sum(s: Seq) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n seq_sum(s.subrange(0, s.len() - 1)) + s[s.len() - 1] as int\n }\n}\n\npub open spec fn abs_int(x: int) -> int {\n if x >= 0 { x } else { -x }\n}\n\npub open spec fn min_elements_spec(diff: int, limit: int) -> int {\n (diff + limit - 1) / limit\n}\n\nproof fn seq_sum_extend(s: Seq, n: int)\n requires\n 0 <= n < s.len(),\n ensures\n seq_sum(s.subrange(0, n + 1)) == seq_sum(s.subrange(0, n)) + s[n] as int,\n{\n let sub = s.subrange(0, n + 1);\n assert(sub.subrange(0, sub.len() - 1) =~= s.subrange(0, n));\n}\n\nimpl Solution {\n pub fn min_elements(nums: Vec, limit: i32, goal: i32) -> (result: i32)\n requires\n 1 <= nums@.len() <= 100_000,\n 1 <= limit <= 1_000_000,\n forall |i: int| 0 <= i < nums@.len() ==> -limit <= (#[trigger] nums@[i]) && nums@[i] <= limit,\n -1_000_000_000 <= goal <= 1_000_000_000,\n min_elements_spec(abs_int(seq_sum(nums@) - goal as int), limit as int) <= i32::MAX as int,\n ensures\n result as int == min_elements_spec(abs_int(seq_sum(nums@) - goal as int), limit as int),\n {\n let n = nums.len();\n let mut sum: i64 = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums@.len(),\n n <= 100_000,\n 1 <= limit <= 1_000_000,\n forall |j: int| 0 <= j < n as int ==> -limit <= (#[trigger] nums@[j]) && nums@[j] <= limit,\n sum as int == seq_sum(nums@.subrange(0, i as int)),\n -((i as int) * (limit as int)) <= sum as int,\n sum as int <= (i as int) * (limit as int),\n decreases n - i,\n {\n proof {\n seq_sum_extend(nums@, i as int);\n assert((i as int + 1) * (limit as int) == (i as int) * (limit as int) + (limit as int)) by(nonlinear_arith)\n requires 0 <= i as int, 0 <= limit as int;\n assert((i as int + 1) * (limit as int) <= 100_000_000_000) by(nonlinear_arith)\n requires 0 <= i as int, i as int + 1 <= 100_000, 1 <= limit as int, limit as int <= 1_000_000;\n }\n sum = sum + nums[i] as i64;\n i = i + 1;\n }\n\n proof {\n assert(nums@.subrange(0, n as int) =~= nums@);\n assert((n as int) * (limit as int) <= 100_000_000_000) by(nonlinear_arith)\n requires 0 <= n as int, n as int <= 100_000, 1 <= limit as int, limit as int <= 1_000_000;\n }\n\n let diff: i64 = if sum >= goal as i64 { sum - goal as i64 } else { goal as i64 - sum };\n let result: i64 = (diff + limit as i64 - 1) / limit as i64;\n result as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1785/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1785/tests/mutated_testcases.jsonl"} {"problem": "lc1822", "family": "leetcode", "description": "# 1822. Sign of the Product of an Array\n\nThere is a function `signFunc(x)` that returns:\n\n- `1` if `x` is positive.\n- `-1` if `x` is negative.\n- `0` if `x` is equal to `0`.\n\nYou are given an integer array `nums`. Let `product` be the product of all values in the array `nums`.\n\nReturn `signFunc(product)`.\n\n## Example 1\n\n- **Input:** `nums = [-1,-2,-3,-4,3,2,1]`\n- **Output:** `1`\n- **Explanation:** The product of all values in the array is `144`, and `signFunc(144) = 1`.\n\n## Example 2\n\n- **Input:** `nums = [1,5,0,2,-3]`\n- **Output:** `0`\n- **Explanation:** The product of all values in the array is `0`, and `signFunc(0) = 0`.\n\n## Example 3\n\n- **Input:** `nums = [-1,1,-1,1,-1]`\n- **Output:** `-1`\n- **Explanation:** The product of all values in the array is `-1`, and `signFunc(-1) = -1`.\n\n## Constraints\n\n- $1 \\leq nums.length \\leq 1000$\n- $-100 \\leq nums[i] \\leq 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn array_sign(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\n64.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sign_func(x: int) -> int {\n if x > 0 {\n 1int\n } else if x < 0 {\n -1int\n } else {\n 0int\n }\n}\n\npub open spec fn product_sign(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1int\n } else {\n sign_func(nums[i - 1] as int) * product_sign(nums, i - 1)\n }\n}\n\nimpl Solution {\n pub fn array_sign(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n res == product_sign(nums@, nums.len() as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn array_sign(nums: Vec) -> i32 {\n let n = nums.len();\n let mut sign: i32 = 1;\n let mut i: usize = 0;\n while i < n {\n if nums[i] == 0 {\n sign = 0;\n } else if nums[i] < 0 {\n sign = sign * -1;\n }\n i += 1;\n }\n sign\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sign_func(x: int) -> int {\n if x > 0 {\n 1int\n } else if x < 0 {\n -1int\n } else {\n 0int\n }\n}\n\npub open spec fn product_sign(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1int\n } else {\n sign_func(nums[i - 1] as int) * product_sign(nums, i - 1)\n }\n}\n\nimpl Solution {\n pub fn array_sign(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n res == product_sign(nums@, nums.len() as int),\n {\n let n = nums.len();\n let mut sign: i32 = 1;\n let mut i: usize = 0;\n\n while i < n\n {\n if nums[i] == 0 {\n sign = 0;\n } else if nums[i] < 0 {\n sign = sign * -1;\n }\n i += 1;\n }\n\n sign\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn sign_func(x: int) -> int {\n if x > 0 {\n 1int\n } else if x < 0 {\n -1int\n } else {\n 0int\n }\n}\n\npub open spec fn product_sign(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1int\n } else {\n sign_func(nums[i - 1] as int) * product_sign(nums, i - 1)\n }\n}\n\nproof fn lemma_product_sign_range(nums: Seq, i: int)\n requires\n 0 <= i <= nums.len(),\n ensures\n product_sign(nums, i) == -1\n || product_sign(nums, i) == 0\n || product_sign(nums, i) == 1,\n decreases i,\n{\n if i <= 0 {\n } else {\n lemma_product_sign_range(nums, i - 1);\n let s = sign_func(nums[i - 1] as int);\n let p = product_sign(nums, i - 1);\n assert(s == -1 || s == 0 || s == 1);\n assert(p == -1 || p == 0 || p == 1);\n \n }\n}\n\nproof fn lemma_sign_zero_absorbs(s: int, p: int)\n requires\n s == 0 || p == 0,\n ensures\n s * p == 0,\n{}\n\nproof fn lemma_sign_mul(s: int, p: int)\n requires\n (s == -1 || s == 0 || s == 1),\n (p == -1 || p == 0 || p == 1),\n ensures\n s * p == -1 || s * p == 0 || s * p == 1,\n s == 0 ==> s * p == 0,\n p == 0 ==> s * p == 0,\n (s == 1 && p == 1) ==> s * p == 1,\n (s == -1 && p == -1) ==> s * p == 1,\n (s == 1 && p == -1) ==> s * p == -1,\n (s == -1 && p == 1) ==> s * p == -1,\n{}\n\nimpl Solution {\n pub fn array_sign(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n res == product_sign(nums@, nums.len() as int),\n {\n let n = nums.len();\n let mut sign: i32 = 1;\n let mut i: usize = 0;\n\n while i < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 1000,\n forall |k: int| 0 <= k < nums.len() ==> -100 <= #[trigger] nums[k] <= 100,\n 0 <= i <= n,\n sign == product_sign(nums@, i as int),\n sign == -1 || sign == 0 || sign == 1,\n decreases n - i,\n {\n proof {\n \n assert(product_sign(nums@, (i + 1) as int)\n == sign_func(nums[i as int] as int) * product_sign(nums@, i as int));\n lemma_product_sign_range(nums@, i as int);\n }\n\n if nums[i] == 0 {\n proof {\n assert(sign_func(nums[i as int] as int) == 0);\n lemma_sign_mul(0, sign as int);\n }\n sign = 0;\n } else if nums[i] < 0 {\n proof {\n assert(sign_func(nums[i as int] as int) == -1int);\n lemma_sign_mul(-1, sign as int);\n }\n sign = sign * -1;\n } else {\n proof {\n assert(sign_func(nums[i as int] as int) == 1);\n lemma_sign_mul(1, sign as int);\n }\n }\n\n proof {\n lemma_product_sign_range(nums@, (i + 1) as int);\n }\n\n i += 1;\n }\n\n sign\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1822/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1822/tests/mutated_testcases.jsonl"} {"problem": "lc1827", "family": "leetcode", "description": "# Minimum Operations to Make the Array Increasing\n\nYou are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.\n\n\n\n\t- For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.\n\n\n\n\nReturn *the **minimum** number of operations needed to make* `nums` ***strictly** **increasing**.*\n\n\n\nAn array `nums` is **strictly increasing** if `nums[i] < nums[i+1]` for all `0 <= i < nums.length - 1`. An array of length `1` is trivially strictly increasing.\n\n\n\n \n\n\nExample 1:\n\n\n\n```\n**Input:** nums = [1,1,1]\n**Output:** 3\n**Explanation:** You can do the following operations:\n1) Increment nums[2], so nums becomes [1,1,**2**].\n2) Increment nums[1], so nums becomes [1,**2**,2].\n3) Increment nums[2], so nums becomes [1,2,**3**].\n```\n\n\n\nExample 2:\n\n\n\n```\n**Input:** nums = [1,5,2,4,1]\n**Output:** 14\n```\n\n\n\nExample 3:\n\n\n\n```\n**Input:** nums = [8]\n**Output:** 0\n```\n\n\n\n \n\n\n**Constraints:**\n\n\n\n\t- `1 <= nums.length <= 5000`\n\n\t- `1 <= nums[i] <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Greedy\nAcceptance rate: 81.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn adjusted_val(nums: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n nums[0] as int\n } else {\n let prev = Self::adjusted_val(nums, i - 1);\n if (nums[i] as int) <= prev {\n prev + 1\n } else {\n nums[i] as int\n }\n }\n }\n\n pub open spec fn total_ops(nums: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n 0int\n } else {\n Self::total_ops(nums, i - 1) + (Self::adjusted_val(nums, i) - nums[i] as int)\n }\n }\n\n pub fn min_operations(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10_000,\n ensures\n res >= 0,\n forall |i: int| 0 <= i < nums.len() - 1 ==>\n #[trigger] Self::adjusted_val(nums@, i) < Self::adjusted_val(nums@, i + 1),\n forall |i: int| 0 <= i < nums.len() ==>\n #[trigger] Self::adjusted_val(nums@, i) >= nums[i] as int,\n res as int == Self::total_ops(nums@, (nums.len() - 1) as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn min_operations(nums: Vec) -> i32 {\n let n = nums.len();\n let mut ops: i64 = 0;\n let mut prev: i64 = nums[0] as i64;\n let mut i: usize = 1;\n while i < n {\n if nums[i] as i64 <= prev {\n ops = ops + (prev + 1 - nums[i] as i64);\n prev = prev + 1;\n } else {\n prev = nums[i] as i64;\n }\n i += 1;\n }\n ops as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn adjusted_val(nums: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n nums[0] as int\n } else {\n let prev = Self::adjusted_val(nums, i - 1);\n if (nums[i] as int) <= prev {\n prev + 1\n } else {\n nums[i] as int\n }\n }\n }\n\n pub open spec fn total_ops(nums: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n 0int\n } else {\n Self::total_ops(nums, i - 1) + (Self::adjusted_val(nums, i) - nums[i] as int)\n }\n }\n\n pub fn min_operations(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10_000,\n ensures\n res >= 0,\n forall |i: int| 0 <= i < nums.len() - 1 ==>\n #[trigger] Self::adjusted_val(nums@, i) < Self::adjusted_val(nums@, i + 1),\n forall |i: int| 0 <= i < nums.len() ==>\n #[trigger] Self::adjusted_val(nums@, i) >= nums[i] as int,\n res as int == Self::total_ops(nums@, (nums.len() - 1) as int),\n {\n let n = nums.len();\n let mut ops: i64 = 0;\n let mut prev: i64 = nums[0] as i64;\n let mut i: usize = 1;\n\n while i < n\n {\n if nums[i] as i64 <= prev {\n ops = ops + (prev + 1 - nums[i] as i64);\n prev = prev + 1;\n } else {\n prev = nums[i] as i64;\n }\n i += 1;\n }\n\n ops as i32\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n \n \n pub open spec fn adjusted_val(nums: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n nums[0] as int\n } else {\n let prev = Self::adjusted_val(nums, i - 1);\n if (nums[i] as int) <= prev {\n prev + 1\n } else {\n nums[i] as int\n }\n }\n }\n\n \n pub open spec fn total_ops(nums: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n 0int\n } else {\n Self::total_ops(nums, i - 1) + (Self::adjusted_val(nums, i) - nums[i] as int)\n }\n }\n\n \n proof fn lemma_adjusted_val_bound(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10_000,\n ensures\n 1 <= Self::adjusted_val(nums, i) <= 10_000 + i,\n decreases i,\n {\n if i <= 0 {\n assert(Self::adjusted_val(nums, 0) == nums[0] as int);\n } else {\n Self::lemma_adjusted_val_bound(nums, i - 1);\n assert(Self::adjusted_val(nums, i) == {\n let p = Self::adjusted_val(nums, i - 1);\n if (nums[i] as int) <= p { p + 1 } else { nums[i] as int }\n });\n }\n }\n\n \n proof fn lemma_total_ops_bound(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10_000,\n ensures\n 0 <= Self::total_ops(nums, i) <= (10_000 + i) * i,\n decreases i,\n {\n if i <= 0 {\n assert(Self::total_ops(nums, 0) == 0);\n } else {\n Self::lemma_total_ops_bound(nums, i - 1);\n Self::lemma_adjusted_val_bound(nums, i);\n assert(Self::total_ops(nums, i) ==\n Self::total_ops(nums, i - 1) +\n (Self::adjusted_val(nums, i) - nums[i] as int));\n \n \n \n \n \n assert(Self::adjusted_val(nums, i) - nums[i] as int <= 10_000 + i - 1);\n assert(Self::total_ops(nums, i - 1) <= (10_000 + i - 1) * (i - 1));\n assert((10_000 + i - 1) * (i - 1) + (10_000 + i - 1) <= (10_000 + i) * i) by(nonlinear_arith)\n requires i >= 1, 0 <= 10_000 + i - 1;\n }\n }\n\n pub fn min_operations(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 5000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10_000,\n ensures\n res >= 0,\n \n forall |i: int| 0 <= i < nums.len() - 1 ==>\n #[trigger] Self::adjusted_val(nums@, i) < Self::adjusted_val(nums@, i + 1),\n \n forall |i: int| 0 <= i < nums.len() ==>\n #[trigger] Self::adjusted_val(nums@, i) >= nums[i] as int,\n \n res as int == Self::total_ops(nums@, (nums.len() - 1) as int),\n {\n let n = nums.len();\n let mut ops: i64 = 0;\n let mut prev: i64 = nums[0] as i64;\n let mut i: usize = 1;\n\n proof {\n assert(Self::adjusted_val(nums@, 0) == nums[0] as int);\n assert(Self::total_ops(nums@, 0) == 0);\n }\n\n while i < n\n invariant\n 1 <= nums.len() <= 5000,\n n == nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10_000,\n 1 <= i <= n,\n prev as int == Self::adjusted_val(nums@, (i - 1) as int),\n ops as int == Self::total_ops(nums@, (i - 1) as int),\n 0 <= ops <= 75_000_000,\n 1 <= prev <= 15_000,\n decreases n - i,\n {\n proof {\n \n assert(Self::adjusted_val(nums@, i as int) == {\n let p = Self::adjusted_val(nums@, (i - 1) as int);\n if (nums@[i as int] as int) <= p { p + 1 } else { nums@[i as int] as int }\n });\n assert(Self::total_ops(nums@, i as int) ==\n Self::total_ops(nums@, (i - 1) as int) +\n (Self::adjusted_val(nums@, i as int) - nums@[i as int] as int));\n \n Self::lemma_adjusted_val_bound(nums@, i as int);\n Self::lemma_total_ops_bound(nums@, i as int);\n \n assert(Self::adjusted_val(nums@, i as int) <= 10_000 + i);\n assert(i <= n - 1 <= 4999);\n \n assert((10_000 + i) * i <= 75_000_000) by(nonlinear_arith)\n requires 1 <= i <= 4999;\n }\n\n if nums[i] as i64 <= prev {\n ops = ops + (prev + 1 - nums[i] as i64);\n prev = prev + 1;\n } else {\n prev = nums[i] as i64;\n }\n i += 1;\n }\n\n proof {\n assert forall |j: int| 0 <= j < nums@.len() - 1 implies\n #[trigger] Self::adjusted_val(nums@, j) < Self::adjusted_val(nums@, j + 1)\n by {\n assert(Self::adjusted_val(nums@, j + 1) == {\n let p = Self::adjusted_val(nums@, j);\n if (nums@[j + 1] as int) <= p { p + 1 } else { nums@[j + 1] as int }\n });\n }\n\n assert forall |j: int| 0 <= j < nums@.len() implies\n #[trigger] Self::adjusted_val(nums@, j) >= nums@[j] as int\n by {\n if j == 0 {\n assert(Self::adjusted_val(nums@, 0) == nums@[0] as int);\n } else {\n assert(Self::adjusted_val(nums@, j) == {\n let p = Self::adjusted_val(nums@, j - 1);\n if (nums@[j] as int) <= p { p + 1 } else { nums@[j] as int }\n });\n }\n }\n\n Self::lemma_total_ops_bound(nums@, (nums@.len() - 1) as int);\n }\n\n ops as i32\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1827/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1827/tests/mutated_testcases.jsonl"} {"problem": "lc1828", "family": "leetcode", "description": "# Queries on Number of Points Inside a Circle\n\nYou are given an array `points` where `points[i] = [x_i, y_i]` is the coordinates of the `i`th point on a 2D plane. Multiple points can have the same coordinates.\n\nYou are also given an array `queries` where `queries[j] = [x_j, y_j, r_j]` describes a circle centered at `(x_j, y_j)` with radius `r_j`.\n\nFor each query `queries[j]`, compute the number of points inside the `j`th circle. Points on the border of the circle are considered inside.\n\nReturn an array `answer`, where `answer[j]` is the answer to the `j`th query.\n\n## Example 1:\n\n> **Input:** points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]\n> **Output:** [3,2,2]\n> **Explanation:** The points and circles are shown above. `queries[0]` is the green circle, `queries[1]` is the red circle, and `queries[2]` is the blue circle.\n\n## Example 2:\n\n> **Input:** points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]\n> **Output:** [2,3,2,4]\n> **Explanation:** The points and circles are shown above. `queries[0]` is green, `queries[1]` is red, `queries[2]` is blue, and `queries[3]` is purple.\n\n## Constraints:\n\n- `1 <= points.length <= 500`\n- `points[i].length == 2`\n- `0 <= x_i, y_i <= 500`\n- `1 <= queries.length <= 500`\n- `queries[j].length == 3`\n- `0 <= x_j, y_j <= 500`\n- `1 <= r_j <= 500`\n- All coordinates are integers.\n\n## Follow up:\n\nCould you find the answer for each query in better complexity than `O(n)`?\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_points(points: Vec>, queries: Vec>) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nSenior, Array, Math, Geometry, Biweekly Contest 50\nAcceptance rate: 86.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn point_in_circle(point: Vec, qx: i32, qy: i32, r: i32) -> bool {\n let dx = point[0] - qx;\n let dy = point[1] - qy;\n dx * dx + dy * dy <= r * r\n }\n\n pub open spec fn count_points_in_query(points: Seq>, qx: i32, qy: i32, r: i32, i: int) -> int\n decreases points.len() - i\n {\n if i >= points.len() {\n 0\n } else {\n (if Self::point_in_circle(points[i], qx, qy, r) { 1int } else { 0int })\n + Self::count_points_in_query(points, qx, qy, r, i + 1)\n }\n }\n\n pub fn count_points(points: Vec>, queries: Vec>) -> (answer: Vec)\n requires\n 1 <= points.len() <= 500,\n forall |i: int| 0 <= i < points.len() ==> #[trigger] points[i].len() == 2,\n forall |i: int|\n 0 <= i < points.len() ==> 0 <= #[trigger] points[i][0] <= 500 && 0 <= points[i][1] <= 500,\n 1 <= queries.len() <= 500,\n forall |j: int| 0 <= j < queries.len() ==> #[trigger] queries[j].len() == 3,\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] queries[j][0] <= 500 && 0 <= queries[j][1] <= 500\n && 1 <= queries[j][2] <= 500,\n ensures\n answer.len() == queries.len(),\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] answer[j] <= points.len()\n && answer[j] == Self::count_points_in_query(\n points@,\n queries[j][0],\n queries[j][1],\n queries[j][2],\n 0,\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_points(points: Vec>, queries: Vec>) -> Vec {\n let mut answer: Vec = Vec::new();\n let mut qi: usize = 0;\n while qi < queries.len() {\n let qx = queries[qi][0];\n let qy = queries[qi][1];\n let r = queries[qi][2];\n let rr = r as i128;\n let rsq = rr * rr;\n let mut count: i32 = 0;\n let mut pi: usize = 0;\n while pi < points.len() {\n let px = points[pi][0] as i128;\n let py = points[pi][1] as i128;\n let dx = px - qx as i128;\n let dy = py - qy as i128;\n let dx2 = dx * dx;\n let dy2 = dy * dy;\n let dist2 = dx2 + dy2;\n let inside = dist2 <= rsq;\n if inside {\n count += 1;\n }\n pi += 1;\n }\n answer.push(count);\n qi += 1;\n }\n answer\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn point_in_circle(point: Vec, qx: i32, qy: i32, r: i32) -> bool {\n let dx = point[0] - qx;\n let dy = point[1] - qy;\n dx * dx + dy * dy <= r * r\n }\n\n pub open spec fn count_points_in_query(points: Seq>, qx: i32, qy: i32, r: i32, i: int) -> int\n decreases points.len() - i\n {\n if i >= points.len() {\n 0\n } else {\n (if Self::point_in_circle(points[i], qx, qy, r) { 1int } else { 0int })\n + Self::count_points_in_query(points, qx, qy, r, i + 1)\n }\n }\n\n pub fn count_points(points: Vec>, queries: Vec>) -> (answer: Vec)\n requires\n 1 <= points.len() <= 500,\n forall |i: int| 0 <= i < points.len() ==> #[trigger] points[i].len() == 2,\n forall |i: int|\n 0 <= i < points.len() ==> 0 <= #[trigger] points[i][0] <= 500 && 0 <= points[i][1] <= 500,\n 1 <= queries.len() <= 500,\n forall |j: int| 0 <= j < queries.len() ==> #[trigger] queries[j].len() == 3,\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] queries[j][0] <= 500 && 0 <= queries[j][1] <= 500\n && 1 <= queries[j][2] <= 500,\n ensures\n answer.len() == queries.len(),\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] answer[j] <= points.len()\n && answer[j] == Self::count_points_in_query(\n points@,\n queries[j][0],\n queries[j][1],\n queries[j][2],\n 0,\n ),\n {\n let mut answer: Vec = Vec::new();\n let mut qi: usize = 0;\n while qi < queries.len() {\n let qx = queries[qi][0];\n let qy = queries[qi][1];\n let r = queries[qi][2];\n let rr = r as i128;\n let rsq = rr * rr;\n let mut count: i32 = 0;\n let mut pi: usize = 0;\n while pi < points.len() {\n let px = points[pi][0] as i128;\n let py = points[pi][1] as i128;\n let dx = px - qx as i128;\n let dy = py - qy as i128;\n let dx2 = dx * dx;\n let dy2 = dy * dy;\n let dist2 = dx2 + dy2;\n let inside = dist2 <= rsq;\n if inside {\n count += 1;\n }\n pi += 1;\n }\n answer.push(count);\n qi += 1;\n }\n answer\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n proof fn diff_bound_500(a: i32, b: i32)\n requires\n 0 <= a <= 500,\n 0 <= b <= 500,\n ensures\n -500 <= a as i128 - b as i128 <= 500,\n {\n }\n\n proof fn square_bound_500(x: i128)\n requires\n -500 <= x <= 500,\n ensures\n 0 <= x * x <= 250000,\n {\n assert(0 <= x * x <= 250000) by (nonlinear_arith)\n requires -500 <= x <= 500;\n }\n\n pub open spec fn point_in_circle(point: Vec, qx: i32, qy: i32, r: i32) -> bool {\n let dx = point[0] - qx;\n let dy = point[1] - qy;\n dx * dx + dy * dy <= r * r\n }\n\n pub open spec fn count_points_in_query(points: Seq>, qx: i32, qy: i32, r: i32, i: int) -> int\n decreases points.len() - i\n {\n if i >= points.len() {\n 0\n } else {\n (if Self::point_in_circle(points[i], qx, qy, r) { 1int } else { 0int })\n + Self::count_points_in_query(points, qx, qy, r, i + 1)\n }\n }\n\n pub fn count_points(points: Vec>, queries: Vec>) -> (answer: Vec)\n requires\n 1 <= points.len() <= 500,\n forall |i: int| 0 <= i < points.len() ==> #[trigger] points[i].len() == 2,\n forall |i: int|\n 0 <= i < points.len() ==> 0 <= #[trigger] points[i][0] <= 500 && 0 <= points[i][1] <= 500,\n 1 <= queries.len() <= 500,\n forall |j: int| 0 <= j < queries.len() ==> #[trigger] queries[j].len() == 3,\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] queries[j][0] <= 500 && 0 <= queries[j][1] <= 500\n && 1 <= queries[j][2] <= 500,\n ensures\n answer.len() == queries.len(),\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] answer[j] <= points.len()\n && answer[j] == Self::count_points_in_query(\n points@,\n queries[j][0],\n queries[j][1],\n queries[j][2],\n 0,\n ),\n {\n let mut answer: Vec = Vec::new();\n let mut qi: usize = 0;\n while qi < queries.len()\n invariant\n 0 <= qi <= queries.len(),\n 1 <= points.len() <= 500,\n 1 <= queries.len() <= 500,\n forall |i: int| 0 <= i < points.len() ==> #[trigger] points[i].len() == 2,\n forall |i: int|\n 0 <= i < points.len() ==> 0 <= #[trigger] points[i][0] <= 500 && 0 <= points[i][1] <= 500,\n forall |j: int| 0 <= j < queries.len() ==> #[trigger] queries[j].len() == 3,\n forall |j: int|\n 0 <= j < queries.len() ==> 0 <= #[trigger] queries[j][0] <= 500 && 0 <= queries[j][1] <= 500\n && 1 <= queries[j][2] <= 500,\n answer.len() == qi,\n forall |j: int|\n 0 <= j < qi ==> 0 <= #[trigger] answer[j] <= points.len()\n && answer[j] == Self::count_points_in_query(\n points@,\n queries[j][0],\n queries[j][1],\n queries[j][2],\n 0,\n ),\n decreases queries.len() - qi,\n {\n proof {\n assert(queries[qi as int].len() == 3);\n }\n let qx = queries[qi][0];\n let qy = queries[qi][1];\n let r = queries[qi][2];\n let rr = r as i128;\n proof {\n Self::square_bound_500(rr);\n }\n let rsq = rr * rr;\n let mut count: i32 = 0;\n let mut pi: usize = 0;\n while pi < points.len()\n invariant\n 0 <= qi < queries.len(),\n 0 <= pi <= points.len(),\n 0 <= qx <= 500,\n 0 <= qy <= 500,\n 1 <= r <= 500,\n rr == r as i128,\n rsq == rr * rr,\n 1 <= points.len() <= 500,\n forall |i: int| 0 <= i < points.len() ==> #[trigger] points[i].len() == 2,\n forall |i: int|\n 0 <= i < points.len() ==> 0 <= #[trigger] points[i][0] <= 500 && 0 <= points[i][1] <= 500,\n count == Self::count_points_in_query(points@, qx, qy, r, 0)\n - Self::count_points_in_query(points@, qx, qy, r, pi as int),\n 0 <= count <= pi,\n decreases points.len() - pi,\n {\n proof {\n assert(points[pi as int].len() == 2);\n assert(0 <= points[pi as int][0] <= 500);\n assert(0 <= points[pi as int][1] <= 500);\n }\n let px = points[pi][0] as i128;\n let py = points[pi][1] as i128;\n let dx = px - qx as i128;\n let dy = py - qy as i128;\n proof {\n Self::diff_bound_500(points[pi as int][0], qx);\n Self::diff_bound_500(points[pi as int][1], qy);\n Self::square_bound_500(dx);\n Self::square_bound_500(dy);\n }\n let dx2 = dx * dx;\n let dy2 = dy * dy;\n proof {\n assert(0 <= dx2 <= 250000);\n assert(0 <= dy2 <= 250000);\n assert(0 <= dx2 + dy2 <= 500000) by (nonlinear_arith)\n requires 0 <= dx2 <= 250000, 0 <= dy2 <= 250000;\n }\n let dist2 = dx2 + dy2;\n let inside = dist2 <= rsq;\n if inside {\n proof {\n assert(count <= pi);\n assert(pi < points.len());\n assert(points.len() <= 500);\n assert(count < 2147483647) by (nonlinear_arith)\n requires count <= pi, pi < points.len(), points.len() <= 500;\n }\n count += 1;\n }\n proof {\n assert(Self::count_points_in_query(points@, qx, qy, r, pi as int)\n == (if Self::point_in_circle(points@[pi as int], qx, qy, r) { 1int } else { 0int })\n + Self::count_points_in_query(points@, qx, qy, r, pi as int + 1));\n assert(Self::point_in_circle(points@[pi as int], qx, qy, r)\n == inside);\n }\n pi += 1;\n }\n proof {\n assert(count == Self::count_points_in_query(points@, qx, qy, r, 0));\n }\n let ghost answer_prev = answer@;\n answer.push(count);\n proof {\n assert(answer@ == answer_prev.push(count));\n assert forall |j: int|\n 0 <= j < qi\n implies 0 <= answer[j] <= points.len()\n && answer[j] == Self::count_points_in_query(\n points@,\n queries[j][0],\n queries[j][1],\n queries[j][2],\n 0,\n )\n by {\n assert(answer@[j] == answer_prev[j]);\n };\n }\n qi += 1;\n }\n answer\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1828/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1828/tests/mutated_testcases.jsonl"} {"problem": "lc1829", "family": "leetcode", "description": "# Maximum XOR for Each Query\n\nYou are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:\n\n1. Find a non-negative integer `k < 2^maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `i`th query.\n2. Remove the **last** element from the current array `nums`.\n\nReturn an array `answer`, where `answer[i]` is the answer to the `i`th query.\n\n## Example 1:\n\n> **Input:** nums = [0,1,1,3], maximumBit = 2\n> **Output:** [0,3,2,3]\n> **Explanation:** The queries are answered as follows:\n> 1st query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.\n> 2nd query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.\n> 3rd query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.\n> 4th query: nums = [0], k = 3 since 0 XOR 3 = 3.\n\n## Example 2:\n\n> **Input:** nums = [2,3,4,7], maximumBit = 3\n> **Output:** [5,2,6,5]\n> **Explanation:** The queries are answered as follows:\n> 1st query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.\n> 2nd query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.\n> 3rd query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.\n> 4th query: nums = [2], k = 5 since 2 XOR 5 = 7.\n\n## Example 3:\n\n> **Input:** nums = [0,1,2,2,5,7], maximumBit = 3\n> **Output:** [4,3,6,4,6,7]\n\n## Constraints:\n\n- `nums.length == n`\n- $1 <= n <= 10^5$\n- $1 <= maximumBit <= 20$\n- $0 <= nums[i] < 2^{maximumBit}$\n- `nums` is sorted in **ascending** order.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn get_maximum_xor(nums: Vec, maximum_bit: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Bit Manipulation, Prefix Sum\nAcceptance rate: 84.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn prefix_xor(nums: Seq, len: int) -> i32\n decreases len,\n{\n if len <= 0 {\n 0i32\n } else {\n prefix_xor(nums, len - 1) ^ nums[len - 1]\n }\n}\n\npub open spec fn mask_for(mb: i32) -> i32 {\n !(!0i32 << (mb as u32))\n}\n\nimpl Solution {\n pub fn get_maximum_xor(nums: Vec, maximum_bit: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100_000,\n 1 <= maximum_bit <= 20,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= mask_for(maximum_bit),\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] <= nums[j],\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() ==>\n 0 <= #[trigger] result[i] <= mask_for(maximum_bit),\n forall |i: int| 0 <= i < result.len() ==>\n prefix_xor(nums@, (nums@.len() - i) as int) ^ #[trigger] result[i]\n == mask_for(maximum_bit),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn get_maximum_xor(nums: Vec, maximum_bit: i32) -> Vec {\n let mask = !(!0i32 << (maximum_bit as u32));\n let n = nums.len();\n let mut xor_all: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n xor_all = xor_all ^ nums[i];\n i = i + 1;\n }\n let mut result: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n result.push(xor_all ^ mask);\n xor_all = xor_all ^ nums[n - 1 - j];\n j = j + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn prefix_xor(nums: Seq, len: int) -> i32\n decreases len,\n{\n if len <= 0 {\n 0i32\n } else {\n prefix_xor(nums, len - 1) ^ nums[len - 1]\n }\n}\n\npub open spec fn mask_for(mb: i32) -> i32 {\n !(!0i32 << (mb as u32))\n}\n\nimpl Solution {\n pub fn get_maximum_xor(nums: Vec, maximum_bit: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100_000,\n 1 <= maximum_bit <= 20,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= mask_for(maximum_bit),\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] <= nums[j],\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() ==>\n 0 <= #[trigger] result[i] <= mask_for(maximum_bit),\n forall |i: int| 0 <= i < result.len() ==>\n prefix_xor(nums@, (nums@.len() - i) as int) ^ #[trigger] result[i]\n == mask_for(maximum_bit),\n {\n let mask = !(!0i32 << (maximum_bit as u32));\n let n = nums.len();\n let mut xor_all: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n xor_all = xor_all ^ nums[i];\n i = i + 1;\n }\n let mut result: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n result.push(xor_all ^ mask);\n xor_all = xor_all ^ nums[n - 1 - j];\n j = j + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn prefix_xor(nums: Seq, len: int) -> i32\n decreases len,\n{\n if len <= 0 {\n 0i32\n } else {\n prefix_xor(nums, len - 1) ^ nums[len - 1]\n }\n}\n\npub open spec fn mask_for(mb: i32) -> i32 {\n !(!0i32 << (mb as u32))\n}\n\nproof fn prefix_xor_remove_last(nums: Seq, k: int)\n requires\n 0 < k <= nums.len(),\n ensures\n prefix_xor(nums, k) ^ nums[k - 1] == prefix_xor(nums, k - 1),\n{\n let a = prefix_xor(nums, k - 1);\n let b = nums[k - 1];\n assert((a ^ b) ^ b == a) by(bit_vector);\n}\n\nproof fn prefix_xor_bounded(nums: Seq, k: int, mb: i32)\n requires\n 0 <= k <= nums.len(),\n 1 <= mb <= 20,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= mask_for(mb),\n ensures\n 0 <= prefix_xor(nums, k) <= mask_for(mb),\n decreases k,\n{\n let m = mask_for(mb);\n if k <= 0 {\n assert(0i32 <= !(!0i32 << (mb as u32))) by(bit_vector)\n requires 1 <= mb <= 20;\n } else {\n prefix_xor_bounded(nums, k - 1, mb);\n let a = prefix_xor(nums, k - 1);\n let b = nums[k - 1];\n assert(0 <= (a ^ b) && (a ^ b) <= m) by(bit_vector)\n requires\n 0 <= a <= m,\n 0 <= b <= m,\n 1 <= mb <= 20,\n m == !(!0i32 << (mb as u32));\n }\n}\n\nproof fn xor_mask_bounded(a: i32, mb: i32)\n requires\n 1 <= mb <= 20,\n 0 <= a <= mask_for(mb),\n ensures\n 0 <= (a ^ mask_for(mb)) <= mask_for(mb),\n{\n let m = mask_for(mb);\n assert(0 <= (a ^ m) && (a ^ m) <= m) by(bit_vector)\n requires\n 0 <= a <= m,\n 1 <= mb <= 20,\n m == !(!0i32 << (mb as u32));\n}\n\nproof fn mask_nonneg(mb: i32)\n requires\n 1 <= mb <= 20,\n ensures\n mask_for(mb) >= 0i32,\n{\n assert(!(!0i32 << (mb as u32)) >= 0i32) by(bit_vector)\n requires 1 <= mb <= 20;\n}\n\nimpl Solution {\n pub fn get_maximum_xor(nums: Vec, maximum_bit: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100_000,\n 1 <= maximum_bit <= 20,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= mask_for(maximum_bit),\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] <= nums[j],\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() ==>\n 0 <= #[trigger] result[i] <= mask_for(maximum_bit),\n forall |i: int| 0 <= i < result.len() ==>\n prefix_xor(nums@, (nums@.len() - i) as int) ^ #[trigger] result[i]\n == mask_for(maximum_bit),\n {\n proof { mask_nonneg(maximum_bit); }\n\n let mask = !(!0i32 << (maximum_bit as u32));\n let n = nums.len();\n\n let mut xor_all: i32 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n mask == mask_for(maximum_bit),\n 1 <= maximum_bit <= 20,\n forall |j: int| 0 <= j < nums.len() ==> 0 <= #[trigger] nums[j] <= mask_for(maximum_bit),\n xor_all == prefix_xor(nums@, i as int),\n 0 <= xor_all <= mask_for(maximum_bit),\n decreases n - i,\n {\n proof {\n prefix_xor_bounded(nums@, (i + 1) as int, maximum_bit);\n }\n xor_all = xor_all ^ nums[i];\n i = i + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n\n invariant\n 0 <= j <= n,\n n == nums.len(),\n mask == mask_for(maximum_bit),\n 1 <= maximum_bit <= 20,\n forall |k: int| 0 <= k < nums.len() ==> 0 <= #[trigger] nums[k] <= mask_for(maximum_bit),\n xor_all == prefix_xor(nums@, (n - j) as int),\n 0 <= xor_all <= mask_for(maximum_bit),\n result.len() == j,\n forall |k: int| 0 <= k < j ==>\n 0 <= #[trigger] result[k] <= mask_for(maximum_bit),\n forall |k: int| 0 <= k < j ==>\n prefix_xor(nums@, (n - k) as int) ^ #[trigger] result[k]\n == mask_for(maximum_bit),\n decreases n - j,\n {\n proof {\n xor_mask_bounded(xor_all, maximum_bit);\n }\n let ghost pxor = prefix_xor(nums@, (n - j) as int);\n result.push(xor_all ^ mask);\n proof {\n assert(pxor ^ (pxor ^ mask) == mask) by(bit_vector);\n assert(prefix_xor(nums@, (n - j) as int) ^ result@[j as int]\n == mask_for(maximum_bit));\n prefix_xor_remove_last(nums@, (n - j) as int);\n prefix_xor_bounded(nums@, (n - j - 1) as int, maximum_bit);\n }\n xor_all = xor_all ^ nums[n - 1 - j];\n j = j + 1;\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1829/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1829/tests/mutated_testcases.jsonl"} {"problem": "lc1837", "family": "leetcode", "description": "# Sum of Digits in Base K\n\nGiven an integer `n` (in base `10`) and a base `k`, return the **sum** of the digits of `n` **after** converting `n` from base `10` to base `k`.\n\nAfter converting, each digit should be interpreted as a base `10` number, and the sum should be returned in base `10`.\n\n## Example 1:\n\n> **Input:** n = 34, k = 6\n> **Output:** 9\n> **Explanation:** 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.\n\n## Example 2:\n\n> **Input:** n = 10, k = 10\n> **Output:** 1\n> **Explanation:** n is already in base 10. 1 + 0 = 1.\n\n## Constraints:\n\n- `1 <= n <= 100`\n- `2 <= k <= 10`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn sum_base(n: i32, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 78.6%\n", "spec": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::lemma_div_decreases;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn digit_sum_base(n: nat, k: nat) -> nat\n decreases n\n{\n if n == 0 {\n 0\n } else {\n (n % k) + digit_sum_base(n / k, k)\n }\n}\n\n\nimpl Solution {\n pub fn sum_base(n: i32, k: i32) -> (result: i32)\n requires\n 1 <= n <= 100,\n 2 <= k <= 10,\n ensures\n result == digit_sum_base(n as nat, k as nat) as i32,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn sum_base(n: i32, k: i32) -> i32 {\n let mut sum: i32 = 0;\n let mut cur: i32 = n;\n while cur > 0 {\n sum = sum + cur % k;\n cur = cur / k;\n }\n sum\n }\n}\n", "code_spec": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::lemma_div_decreases;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn digit_sum_base(n: nat, k: nat) -> nat\n decreases n\n{\n if n == 0 {\n 0\n } else {\n (n % k) + digit_sum_base(n / k, k)\n }\n}\n\n\nimpl Solution {\n pub fn sum_base(n: i32, k: i32) -> (result: i32)\n requires\n 1 <= n <= 100,\n 2 <= k <= 10,\n ensures\n result == digit_sum_base(n as nat, k as nat) as i32,\n {\n let mut sum: i32 = 0;\n let mut cur: i32 = n;\n while cur > 0 {\n sum = sum + cur % k;\n cur = cur / k;\n }\n sum\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::{lemma_fundamental_div_mod, lemma_div_decreases};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn digit_sum_base(n: nat, k: nat) -> nat\n decreases n\n when k >= 2\n via digit_sum_base_decreases\n{\n if n == 0 {\n 0\n } else {\n (n % k) + digit_sum_base(n / k, k)\n }\n}\n\n#[verifier::decreases_by]\nproof fn digit_sum_base_decreases(n: nat, k: nat) {\n if n > 0 {\n lemma_div_decreases(n as int, k as int);\n }\n}\n\nproof fn lemma_digit_sum_bound(n: nat, k: nat)\n requires\n k >= 2,\n ensures\n digit_sum_base(n, k) <= n,\n decreases n,\n{\n if n > 0 {\n lemma_div_decreases(n as int, k as int);\n lemma_digit_sum_bound(n / k, k);\n lemma_fundamental_div_mod(n as int, k as int);\n assert(n % k + n / k <= n) by (nonlinear_arith)\n requires\n n as int == k as int * ((n as int) / (k as int)) + (n as int) % (k as int),\n (n as int) / (k as int) >= 0,\n (n as int) % (k as int) >= 0,\n k as int >= 2,\n ;\n }\n}\n\nimpl Solution {\n pub fn sum_base(n: i32, k: i32) -> (result: i32)\n requires\n 1 <= n <= 100,\n 2 <= k <= 10,\n ensures\n result == digit_sum_base(n as nat, k as nat) as i32,\n {\n let mut sum: i32 = 0;\n let mut cur: i32 = n;\n proof {\n lemma_digit_sum_bound(n as nat, k as nat);\n }\n while cur > 0\n invariant\n 0 <= cur <= n,\n 0 <= sum <= n,\n 2 <= k <= 10,\n 1 <= n <= 100,\n sum + digit_sum_base(cur as nat, k as nat) == digit_sum_base(n as nat, k as nat),\n digit_sum_base(n as nat, k as nat) <= n as nat,\n decreases cur,\n {\n proof {\n lemma_div_decreases(cur as int, k as int);\n lemma_digit_sum_bound(cur as nat / (k as nat), k as nat);\n }\n sum = sum + cur % k;\n cur = cur / k;\n }\n sum\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1837/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1837/tests/mutated_testcases.jsonl"} {"problem": "lc1862", "family": "leetcode", "description": "# Sum of Floored Pairs\n\nGiven an integer array `nums`, return the sum of `floor(nums[i] / nums[j])` for all pairs of indices `0 <= i, j < nums.length` in the array. Since the answer may be too large, return it **modulo** `10^9 + 7`.\n\nThe `floor()` function returns the integer part of the division.\n\n## Example 1:\n\n> **Input:** nums = [2,5,9]\n> **Output:** 10\n> **Explanation:**\n> floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0\n> floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1\n> floor(5 / 2) = 2\n> floor(9 / 2) = 4\n> floor(9 / 5) = 1\n> We calculate the floor of the division for every pair of indices in the array then sum them up.\n\n## Example 2:\n\n> **Input:** nums = [7,7,7,7,7,7,7]\n> **Output:** 49\n\n## Constraints:\n\n- $1 <= nums.length <= 10^5$\n- $1 <= nums[i] <= 10^5$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn sum_of_floored_pairs(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Math, Binary Search, Counting, Enumeration, Prefix Sum\nAcceptance rate: 30.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn inner_sum(nums: Seq, i: int, end_j: int) -> int\n decreases end_j,\n{\n if end_j <= 0 {\n 0\n } else {\n inner_sum(nums, i, end_j - 1) + (nums[i] as int) / (nums[end_j - 1] as int)\n }\n}\n\npub open spec fn outer_sum(nums: Seq, end_i: int) -> int\n decreases end_i,\n{\n if end_i <= 0 {\n 0\n } else {\n outer_sum(nums, end_i - 1) + inner_sum(nums, end_i - 1, nums.len() as int)\n }\n}\n\nimpl Solution {\n pub fn sum_of_floored_pairs(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result as int == outer_sum(nums@, nums.len() as int) % 1_000_000_007,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn sum_of_floored_pairs(nums: Vec) -> i32 {\n let n = nums.len();\n let modulo: i64 = 1_000_000_007;\n\n let mut count: Vec = Vec::new();\n let mut vi: usize = 0;\n while vi <= 100_000 {\n count.push(0);\n vi = vi + 1;\n }\n\n let mut i: usize = 0;\n while i < n {\n let val = nums[i] as usize;\n count[val] = count[val] + 1;\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(count[0]);\n let mut v1: usize = 1;\n while v1 <= 100_000 {\n let next = prefix[v1 - 1] + count[v1];\n prefix.push(next);\n v1 = v1 + 1;\n }\n\n let mut gval: Vec = Vec::new();\n gval.push(0);\n let mut v2: usize = 1;\n while v2 <= 100_000 {\n let kmax_bound: usize = 100_000 / v2;\n let mut g: i64 = 0;\n let mut k: usize = 1;\n while k <= kmax_bound {\n let lo = k * v2;\n let k1 = k + 1;\n let hi_raw = k1 * v2 - 1;\n let hi: usize = if hi_raw > 100_000 { 100_000 } else { hi_raw };\n let range_count = prefix[hi] - prefix[lo - 1];\n g = g + (k as i64) * range_count;\n k = k + 1;\n }\n gval.push(g);\n v2 = v2 + 1;\n }\n\n let mut total: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n let val = nums[j] as usize;\n total = (total + gval[val]) % modulo;\n j = j + 1;\n }\n (total % modulo) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn inner_sum(nums: Seq, i: int, end_j: int) -> int\n decreases end_j,\n{\n if end_j <= 0 {\n 0\n } else {\n inner_sum(nums, i, end_j - 1) + (nums[i] as int) / (nums[end_j - 1] as int)\n }\n}\n\npub open spec fn outer_sum(nums: Seq, end_i: int) -> int\n decreases end_i,\n{\n if end_i <= 0 {\n 0\n } else {\n outer_sum(nums, end_i - 1) + inner_sum(nums, end_i - 1, nums.len() as int)\n }\n}\n\nimpl Solution {\n pub fn sum_of_floored_pairs(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result as int == outer_sum(nums@, nums.len() as int) % 1_000_000_007,\n {\n let n = nums.len();\n let modulo: i64 = 1_000_000_007;\n\n let mut count: Vec = Vec::new();\n let mut vi: usize = 0;\n while vi <= 100_000 {\n count.push(0);\n vi = vi + 1;\n }\n\n let mut i: usize = 0;\n while i < n {\n let val = nums[i] as usize;\n count.set(val, count[val] + 1);\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(count[0]);\n let mut v1: usize = 1;\n while v1 <= 100_000 {\n let next = prefix[v1 - 1] + count[v1];\n prefix.push(next);\n v1 = v1 + 1;\n }\n\n let mut gval: Vec = Vec::new();\n gval.push(0);\n let mut v2: usize = 1;\n while v2 <= 100_000 {\n let kmax_bound: usize = 100_000 / v2;\n let mut g: i64 = 0;\n let mut k: usize = 1;\n while k <= kmax_bound {\n let lo = k * v2;\n let k1 = k + 1;\n let hi_raw = k1 * v2 - 1;\n let hi: usize = if hi_raw > 100_000 { 100_000 } else { hi_raw };\n let range_count = prefix[hi] - prefix[lo - 1];\n g = g + (k as i64) * range_count;\n k = k + 1;\n }\n gval.push(g);\n v2 = v2 + 1;\n }\n\n let mut total: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n let val = nums[j] as usize;\n total = (total + gval[val]) % modulo;\n j = j + 1;\n }\n (total % modulo) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::div_mod::{lemma_fundamental_div_mod, lemma_mod_multiples_vanish, lemma_mod_bound};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn inner_sum(nums: Seq, i: int, end_j: int) -> int\n decreases end_j,\n{\n if end_j <= 0 {\n 0\n } else {\n inner_sum(nums, i, end_j - 1) + (nums[i] as int) / (nums[end_j - 1] as int)\n }\n}\n\npub open spec fn outer_sum(nums: Seq, end_i: int) -> int\n decreases end_i,\n{\n if end_i <= 0 {\n 0\n } else {\n outer_sum(nums, end_i - 1) + inner_sum(nums, end_i - 1, nums.len() as int)\n }\n}\n\nproof fn lemma_inner_sum_nonneg(nums: Seq, i: int, end_j: int)\n requires\n 0 <= i < nums.len(),\n 0 <= end_j <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] >= 1,\n ensures\n inner_sum(nums, i, end_j) >= 0,\n decreases end_j,\n{\n if end_j > 0 {\n lemma_inner_sum_nonneg(nums, i, end_j - 1);\n }\n}\n\nproof fn lemma_outer_sum_nonneg(nums: Seq, end_i: int)\n requires\n 0 <= end_i <= nums.len(),\n nums.len() >= 1,\n forall|k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] >= 1,\n ensures\n outer_sum(nums, end_i) >= 0,\n decreases end_i,\n{\n if end_i > 0 {\n lemma_outer_sum_nonneg(nums, end_i - 1);\n lemma_inner_sum_nonneg(nums, end_i - 1, nums.len() as int);\n }\n}\n\nproof fn lemma_mod_add(a: int, b: int, m: int)\n requires\n a >= 0,\n b >= 0,\n m > 0,\n ensures\n (a % m + b) % m == (a + b) % m,\n{\n lemma_fundamental_div_mod(a, m);\n let q = a / m;\n assert(a % m + b == (a + b) + (-q) * m) by(nonlinear_arith)\n requires(a == m * q + a % m);\n lemma_mod_multiples_vanish(-q, a + b, m);\n}\n\n\n\n\n\n\n\n\npub open spec fn count_range(nums: Seq, end: int, lo: int, hi: int) -> int\n decreases end\n{\n if end <= 0 {\n 0\n } else {\n count_range(nums, end - 1, lo, hi)\n + (if lo <= nums[end - 1] && nums[end - 1] as int <= hi { 1int } else { 0int })\n }\n}\n\nproof fn count_range_step(nums: Seq, end: int, lo: int, hi: int)\n requires 0 <= end < nums.len(),\n ensures count_range(nums, end + 1, lo, hi)\n == count_range(nums, end, lo, hi)\n + (if lo <= nums[end] && nums[end] as int <= hi { 1int } else { 0int }),\n{\n}\n\nproof fn count_range_nonneg(nums: Seq, end: int, lo: int, hi: int)\n requires 0 <= end <= nums.len(),\n ensures 0 <= count_range(nums, end, lo, hi) <= end,\n decreases end\n{\n if end > 0 {\n count_range_nonneg(nums, end - 1, lo, hi);\n }\n}\n\nproof fn count_range_empty(nums: Seq, end: int, lo: int, hi: int)\n requires 0 <= end <= nums.len(), lo > hi,\n ensures count_range(nums, end, lo, hi) == 0,\n decreases end\n{\n if end > 0 {\n count_range_empty(nums, end - 1, lo, hi);\n }\n}\n\nproof fn count_range_split(nums: Seq, end: int, lo: int, mid: int, hi: int)\n requires 0 <= end <= nums.len(), lo <= mid + 1, mid <= hi,\n ensures count_range(nums, end, lo, hi)\n == count_range(nums, end, lo, mid) + count_range(nums, end, mid + 1, hi),\n decreases end\n{\n if end > 0 {\n count_range_split(nums, end - 1, lo, mid, hi);\n let x = nums[end - 1];\n if lo <= x && x as int <= hi {\n if x as int <= mid {\n assert(lo <= x && x as int <= mid);\n assert(!(mid + 1 <= x && x as int <= hi));\n } else {\n assert(mid + 1 <= x && x as int <= hi);\n assert(!(lo <= x && x as int <= mid));\n }\n } else {\n assert(!(lo <= x && x as int <= mid));\n assert(!(mid + 1 <= x && x as int <= hi));\n }\n }\n}\n\nproof fn count_range_cap(nums: Seq, end: int, lo: int, hi1: int, hi2: int)\n requires\n 0 <= end <= nums.len(),\n hi1 <= hi2,\n forall |k: int| 0 <= k < end ==> nums[k] as int <= hi1,\n ensures count_range(nums, end, lo, hi1) == count_range(nums, end, lo, hi2),\n decreases end\n{\n if end > 0 {\n count_range_cap(nums, end - 1, lo, hi1, hi2);\n }\n}\n\npub open spec fn col_sum(nums: Seq, j: int, end_i: int) -> int\n decreases end_i\n{\n if end_i <= 0 {\n 0\n } else {\n col_sum(nums, j, end_i - 1) + (nums[end_i - 1] as int) / (nums[j] as int)\n }\n}\n\npub open spec fn partial_sum(nums: Seq, ei: int, ej: int) -> int\n decreases ei\n{\n if ei <= 0 {\n 0\n } else {\n partial_sum(nums, ei - 1, ej) + inner_sum(nums, ei - 1, ej)\n }\n}\n\nproof fn partial_sum_eq_outer(nums: Seq, ei: int)\n requires 0 <= ei <= nums.len(),\n ensures partial_sum(nums, ei, nums.len() as int) == outer_sum(nums, ei),\n decreases ei\n{\n if ei > 0 {\n partial_sum_eq_outer(nums, ei - 1);\n }\n}\n\nproof fn partial_sum_step(nums: Seq, ei: int, ej: int)\n requires 0 <= ei <= nums.len(), 0 <= ej < nums.len(),\n ensures partial_sum(nums, ei, ej + 1) == partial_sum(nums, ei, ej) + col_sum(nums, ej, ei),\n decreases ei\n{\n if ei > 0 {\n partial_sum_step(nums, ei - 1, ej);\n }\n}\n\npub open spec fn total_by_col_upto(nums: Seq, end_j: int, ei: int) -> int\n decreases end_j\n{\n if end_j <= 0 {\n 0\n } else {\n total_by_col_upto(nums, end_j - 1, ei) + col_sum(nums, end_j - 1, ei)\n }\n}\n\nproof fn partial_sum_zero_ej(nums: Seq, ei: int)\n requires 0 <= ei <= nums.len(),\n ensures partial_sum(nums, ei, 0) == 0,\n decreases ei\n{\n if ei > 0 {\n partial_sum_zero_ej(nums, ei - 1);\n }\n}\n\nproof fn partial_sum_eq_total_by_col(nums: Seq, ei: int, ej: int)\n requires 0 <= ei <= nums.len(), 0 <= ej <= nums.len(),\n ensures partial_sum(nums, ei, ej) == total_by_col_upto(nums, ej, ei),\n decreases ej\n{\n if ej <= 0 {\n partial_sum_zero_ej(nums, ei);\n } else {\n partial_sum_eq_total_by_col(nums, ei, ej - 1);\n partial_sum_step(nums, ei, ej - 1);\n assert(partial_sum(nums, ei, ej) == partial_sum(nums, ei, ej - 1) + col_sum(nums, ej - 1, ei));\n assert(total_by_col_upto(nums, ej, ei) == total_by_col_upto(nums, ej - 1, ei) + col_sum(nums, ej - 1, ei));\n assert(partial_sum(nums, ei, ej) == total_by_col_upto(nums, ej, ei));\n }\n}\n\npub open spec fn point_indicator_sum(x: int, v: int, kmax: int) -> int\n decreases kmax\n{\n if kmax <= 0 {\n 0\n } else {\n point_indicator_sum(x, v, kmax - 1)\n + kmax * (if kmax * v <= x && x <= (kmax + 1) * v - 1 { 1int } else { 0int })\n }\n}\n\nproof fn floor_bucket_exists(x: int, v: int)\n requires 1 <= x, v >= 1,\n ensures (x / v) * v <= x, x <= (x / v + 1) * v - 1,\n{\n lemma_fundamental_div_mod(x, v);\n lemma_mod_bound(x, v);\n assert(v * (x / v) == (x / v) * v) by (nonlinear_arith);\n assert((x / v + 1) * v == (x / v) * v + v) by (nonlinear_arith);\n}\n\nproof fn floor_bucket_unique(x: int, v: int, k: int)\n requires 1 <= x, v >= 1, k >= 0, k != x / v,\n ensures !(k * v <= x && x <= (k + 1) * v - 1),\n{\n lemma_fundamental_div_mod(x, v);\n lemma_mod_bound(x, v);\n if k < x / v {\n assert((k + 1) * v <= (x / v) * v) by (nonlinear_arith)\n requires k + 1 <= x / v, v >= 1;\n assert((x / v) * v <= x);\n assert((k + 1) * v - 1 < x);\n } else {\n assert(k * v >= (x / v + 1) * v) by (nonlinear_arith)\n requires k >= x / v + 1, v >= 1;\n assert((x / v + 1) * v == (x / v) * v + v) by (nonlinear_arith);\n assert(x < (x / v) * v + v);\n assert(k * v > x);\n }\n}\n\nproof fn div_monotone(a: int, b: int, v: int)\n requires 0 <= a <= b, v >= 1,\n ensures a / v <= b / v,\n{\n lemma_fundamental_div_mod(a, v);\n lemma_fundamental_div_mod(b, v);\n lemma_mod_bound(a, v);\n lemma_mod_bound(b, v);\n if a / v > b / v {\n assert((a / v) * v >= (b / v + 1) * v) by (nonlinear_arith)\n requires a / v >= b / v + 1, v >= 1;\n assert((b / v + 1) * v == (b / v) * v + v) by (nonlinear_arith);\n assert(b < (b / v) * v + v);\n assert(v * (a / v) == (a / v) * v) by (nonlinear_arith);\n assert(a >= (a / v) * v);\n assert(a > b);\n }\n}\n\nproof fn point_indicator_sum_eq(x: int, v: int, kmax: int)\n requires 1 <= x, v >= 1, kmax >= x / v, kmax >= 0,\n ensures point_indicator_sum(x, v, kmax) == x / v,\n decreases kmax\n{\n if kmax <= 0 {\n } else if kmax == x / v {\n floor_bucket_exists(x, v);\n point_indicator_sum_zero(x, v, kmax - 1);\n } else {\n point_indicator_sum_eq(x, v, kmax - 1);\n floor_bucket_unique(x, v, kmax);\n }\n}\n\nproof fn point_indicator_sum_zero(x: int, v: int, kmax: int)\n requires 1 <= x, v >= 1, kmax >= 0, kmax < x / v,\n ensures point_indicator_sum(x, v, kmax) == 0,\n decreases kmax\n{\n if kmax > 0 {\n point_indicator_sum_zero(x, v, kmax - 1);\n floor_bucket_unique(x, v, kmax);\n }\n}\n\npub open spec fn g_upto_ei(nums: Seq, v: int, kmax: int, end_i: int) -> int\n decreases kmax\n{\n if kmax <= 0 {\n 0\n } else {\n g_upto_ei(nums, v, kmax - 1, end_i) + kmax * count_range(nums, end_i, kmax * v, (kmax + 1) * v - 1)\n }\n}\n\nproof fn g_upto_ei_step(nums: Seq, v: int, kmax: int, end_i: int)\n requires 0 <= end_i < nums.len(), v >= 1, kmax >= 0,\n ensures g_upto_ei(nums, v, kmax, end_i + 1)\n == g_upto_ei(nums, v, kmax, end_i) + point_indicator_sum(nums[end_i] as int, v, kmax),\n decreases kmax\n{\n if kmax > 0 {\n g_upto_ei_step(nums, v, kmax - 1, end_i);\n count_range_step(nums, end_i, kmax * v, (kmax + 1) * v - 1);\n let x = nums[end_i] as int;\n assert(g_upto_ei(nums, v, kmax, end_i + 1)\n == g_upto_ei(nums, v, kmax - 1, end_i + 1) + kmax * count_range(nums, end_i + 1, kmax * v, (kmax + 1) * v - 1));\n assert(g_upto_ei(nums, v, kmax, end_i)\n == g_upto_ei(nums, v, kmax - 1, end_i) + kmax * count_range(nums, end_i, kmax * v, (kmax + 1) * v - 1));\n assert(count_range(nums, end_i + 1, kmax * v, (kmax + 1) * v - 1)\n == count_range(nums, end_i, kmax * v, (kmax + 1) * v - 1)\n + (if kmax * v <= x && x <= (kmax + 1) * v - 1 { 1int } else { 0int }));\n assert(point_indicator_sum(x, v, kmax)\n == point_indicator_sum(x, v, kmax - 1)\n + kmax * (if kmax * v <= x && x <= (kmax + 1) * v - 1 { 1int } else { 0int }));\n let ind = if kmax * v <= x && x <= (kmax + 1) * v - 1 { 1int } else { 0int };\n let d = count_range(nums, end_i, kmax * v, (kmax + 1) * v - 1);\n assert(kmax * (d + ind) == kmax * d + kmax * ind) by (nonlinear_arith);\n assert(g_upto_ei(nums, v, kmax, end_i + 1) == g_upto_ei(nums, v, kmax, end_i) + point_indicator_sum(x, v, kmax));\n }\n}\n\nproof fn g_upto_ei_zero(nums: Seq, v: int, kmax: int)\n requires kmax >= 0,\n ensures g_upto_ei(nums, v, kmax, 0) == 0,\n decreases kmax\n{\n if kmax > 0 {\n g_upto_ei_zero(nums, v, kmax - 1);\n assert(count_range(nums, 0, kmax * v, (kmax + 1) * v - 1) == 0);\n }\n}\n\nproof fn g_upto_ei_bound(nums: Seq, v: int, kmax: int, end_i: int)\n requires 0 <= end_i <= nums.len(), v >= 1, kmax >= 0,\n ensures 0 <= g_upto_ei(nums, v, kmax, end_i) <= kmax * kmax * end_i,\n decreases kmax\n{\n if kmax > 0 {\n g_upto_ei_bound(nums, v, kmax - 1, end_i);\n count_range_nonneg(nums, end_i, kmax * v, (kmax + 1) * v - 1);\n assert(kmax * count_range(nums, end_i, kmax * v, (kmax + 1) * v - 1) <= kmax * end_i) by (nonlinear_arith)\n requires 0 <= count_range(nums, end_i, kmax * v, (kmax + 1) * v - 1) <= end_i, kmax >= 0;\n assert((kmax - 1) * (kmax - 1) * end_i + kmax * end_i <= kmax * kmax * end_i) by (nonlinear_arith)\n requires kmax >= 1, end_i >= 0;\n }\n}\n\nproof fn col_sum_eq_g(nums: Seq, j: int, end_i: int, v: int, kmax: int)\n requires\n 0 <= end_i <= nums.len(),\n 0 <= j < nums.len(),\n v == nums[j] as int,\n v >= 1,\n kmax >= 100_000int / v,\n kmax >= 0,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100_000,\n ensures col_sum(nums, j, end_i) == g_upto_ei(nums, v, kmax, end_i),\n decreases end_i\n{\n if end_i <= 0 {\n g_upto_ei_zero(nums, v, kmax);\n } else {\n col_sum_eq_g(nums, j, end_i - 1, v, kmax);\n g_upto_ei_step(nums, v, kmax, end_i - 1);\n assert(1 <= nums[end_i - 1] <= 100_000);\n div_monotone(nums[end_i - 1] as int, 100_000int, v);\n point_indicator_sum_eq(nums[end_i - 1] as int, v, kmax);\n assert(col_sum(nums, j, end_i) == col_sum(nums, j, end_i - 1) + (nums[end_i - 1] as int) / v);\n assert(point_indicator_sum(nums[end_i - 1] as int, v, kmax) == (nums[end_i - 1] as int) / v);\n assert(g_upto_ei(nums, v, kmax, end_i)\n == g_upto_ei(nums, v, kmax, end_i - 1) + point_indicator_sum(nums[end_i - 1] as int, v, kmax));\n assert(col_sum(nums, j, end_i) == g_upto_ei(nums, v, kmax, end_i));\n }\n}\n\nproof fn outer_sum_eq_total_by_col(nums: Seq)\n ensures outer_sum(nums, nums.len() as int) == total_by_col_upto(nums, nums.len() as int, nums.len() as int),\n{\n partial_sum_eq_outer(nums, nums.len() as int);\n partial_sum_eq_total_by_col(nums, nums.len() as int, nums.len() as int);\n}\n\nproof fn total_by_col_upto_nonneg(nums: Seq, end_j: int, ei: int)\n requires\n 0 <= end_j <= nums.len(),\n 0 <= ei <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] >= 1,\n ensures total_by_col_upto(nums, end_j, ei) >= 0,\n decreases end_j\n{\n if end_j > 0 {\n total_by_col_upto_nonneg(nums, end_j - 1, ei);\n col_sum_nonneg(nums, end_j - 1, ei);\n }\n}\n\nproof fn col_sum_nonneg(nums: Seq, j: int, end_i: int)\n requires\n 0 <= j < nums.len(),\n 0 <= end_i <= nums.len(),\n forall |k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] >= 1,\n ensures col_sum(nums, j, end_i) >= 0,\n decreases end_i\n{\n if end_i > 0 {\n col_sum_nonneg(nums, j, end_i - 1);\n }\n}\n\nimpl Solution {\n pub fn sum_of_floored_pairs(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result as int == outer_sum(nums@, nums.len() as int) % 1_000_000_007,\n {\n let n = nums.len();\n let modulo: i64 = 1_000_000_007;\n\n let mut count: Vec = Vec::new();\n let mut vi: usize = 0;\n while vi <= 100_000\n invariant\n count@.len() == vi as int,\n 0 <= vi <= 100_001,\n forall |v: int| 0 <= v < vi as int ==> (#[trigger] count@[v]) as int == count_range(nums@, 0, v, v),\n decreases 100_001 - vi,\n {\n count.push(0);\n vi = vi + 1;\n }\n\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 1 <= nums.len() <= 100_000,\n count@.len() == 100_001,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums@[k] <= 100_000,\n forall |v: int| 0 <= v <= 100_000 ==> (#[trigger] count@[v]) as int == count_range(nums@, i as int, v, v),\n forall |v: int| 0 <= v <= 100_000 ==> 0 <= #[trigger] count@[v] <= i as i64,\n decreases n - i,\n {\n let val = nums[i] as usize;\n proof {\n count_range_step(nums@, i as int, val as int, val as int);\n assert forall |v: int| 0 <= v <= 100_000 && v != val as int implies\n (#[trigger] count@[v]) as int == count_range(nums@, i as int + 1, v, v) by {\n count_range_step(nums@, i as int, v, v);\n assert(!(v <= nums@[i as int] && nums@[i as int] as int <= v));\n }\n }\n let ghost count_before = count@;\n count.set(val, count[val] + 1);\n proof {\n assert(count@ =~= count_before.update(val as int, (count_before[val as int] + 1) as i64));\n }\n i = i + 1;\n }\n\n let mut prefix: Vec = Vec::new();\n prefix.push(count[0]);\n let mut v1: usize = 1;\n while v1 <= 100_000\n invariant\n prefix@.len() == v1 as int,\n 1 <= v1 <= 100_001,\n n == nums.len(),\n 1 <= nums.len() <= 100_000,\n count@.len() == 100_001,\n forall |v: int| 0 <= v <= 100_000 ==> (#[trigger] count@[v]) as int == count_range(nums@, n as int, v, v),\n forall |v: int| 0 <= v < v1 as int ==> (#[trigger] prefix@[v]) as int == count_range(nums@, n as int, 0, v),\n forall |v: int| 0 <= v < v1 as int ==> 0 <= #[trigger] prefix@[v] <= n as i64,\n decreases 100_001 - v1,\n {\n proof {\n count_range_nonneg(nums@, n as int, 0, v1 as int);\n count_range_split(nums@, n as int, 0, v1 as int - 1, v1 as int);\n assert(prefix@[v1 as int - 1] as int + count@[v1 as int] as int <= n as int);\n }\n let next = prefix[v1 - 1] + count[v1];\n prefix.push(next);\n v1 = v1 + 1;\n }\n\n let mut gval: Vec = Vec::new();\n gval.push(0);\n let mut v2: usize = 1;\n while v2 <= 100_000\n invariant\n gval@.len() == v2 as int,\n 1 <= v2 <= 100_001,\n n == nums.len(),\n 1 <= nums.len() <= 100_000,\n prefix@.len() == 100_001,\n forall |kk: int| 0 <= kk < nums.len() ==> 1 <= #[trigger] nums@[kk] <= 100_000,\n forall |v: int| 0 <= v <= 100_000 ==> (#[trigger] prefix@[v]) as int == count_range(nums@, n as int, 0, v),\n forall |v: int| 0 <= v <= 100_000 ==> 0 <= #[trigger] prefix@[v] <= n as i64,\n forall |v: int| 1 <= v < v2 as int ==> (#[trigger] gval@[v]) as int == g_upto_ei(nums@, v, 100_000int / v, n as int),\n forall |v: int| 1 <= v < v2 as int ==> 0 <= #[trigger] gval@[v] <= 2_000_000_000_000_000i64,\n decreases 100_001 - v2,\n {\n let kmax_bound: usize = 100_000 / v2;\n proof {\n lemma_fundamental_div_mod(100_000, v2 as int);\n lemma_mod_bound(100_000, v2 as int);\n assert(kmax_bound as int == 100_000int / (v2 as int));\n assert(100_000int == (v2 as int) * (kmax_bound as int) + 100_000int % (v2 as int));\n assert((kmax_bound as int) * (v2 as int) <= 100_000) by (nonlinear_arith)\n requires 100_000int == (v2 as int) * (kmax_bound as int) + 100_000int % (v2 as int),\n 0 <= 100_000int % (v2 as int);\n }\n let mut g: i64 = 0;\n let mut k: usize = 1;\n while k <= kmax_bound\n invariant\n 1 <= v2 <= 100_000,\n 1 <= k <= kmax_bound + 1,\n kmax_bound <= 100_000,\n (kmax_bound as int) * (v2 as int) <= 100_000,\n n == nums.len(),\n 1 <= nums.len() <= 100_000,\n prefix@.len() == 100_001,\n forall |kk: int| 0 <= kk < nums.len() ==> 1 <= #[trigger] nums@[kk] <= 100_000,\n forall |v: int| 0 <= v <= 100_000 ==> (#[trigger] prefix@[v]) as int == count_range(nums@, n as int, 0, v),\n forall |v: int| 0 <= v <= 100_000 ==> 0 <= #[trigger] prefix@[v] <= n as i64,\n g as int == g_upto_ei(nums@, v2 as int, k as int - 1, n as int),\n 0 <= g <= 2_000_000_000_000_000i64,\n decreases kmax_bound + 1 - k,\n {\n proof {\n assert((k as int) * (v2 as int) <= (kmax_bound as int) * (v2 as int)) by (nonlinear_arith)\n requires k as int <= kmax_bound as int, v2 as int >= 1;\n assert((k as int) * (v2 as int) <= 100_000);\n assert(((k as int) + 1) * (v2 as int) <= 100_000 + v2 as int) by (nonlinear_arith)\n requires (k as int) * (v2 as int) <= 100_000;\n }\n let lo = k * v2;\n let k1 = k + 1;\n proof {\n assert(k1 as int == k as int + 1);\n assert((k1 as int) * (v2 as int) <= 100_000 + v2 as int);\n assert((k1 as int) * (v2 as int) >= 1) by (nonlinear_arith)\n requires k1 as int >= 1, v2 as int >= 1;\n }\n let hi_raw = k1 * v2 - 1;\n let hi: usize = if hi_raw > 100_000 { 100_000 } else { hi_raw };\n proof {\n assert(0 <= lo as int <= 100_000);\n assert(lo as int - 1 >= 0) by (nonlinear_arith)\n requires lo as int == k as int * v2 as int, k as int >= 1, v2 as int >= 1;\n assert(hi_raw as int - lo as int == v2 as int - 1) by (nonlinear_arith)\n requires hi_raw as int == (k1 as int) * (v2 as int) - 1,\n lo as int == (k as int) * (v2 as int), k1 as int == k as int + 1;\n assert(lo as int <= hi_raw as int) by (nonlinear_arith)\n requires hi_raw as int - lo as int == v2 as int - 1, v2 as int >= 1;\n assert(lo as int <= hi as int);\n count_range_nonneg(nums@, n as int, 0, hi as int);\n count_range_nonneg(nums@, n as int, 0, lo as int - 1);\n count_range_split(nums@, n as int, 0, lo as int - 1, hi as int);\n assert forall |kk: int| 0 <= kk < n as int implies 1 <= #[trigger] nums@[kk] <= 100_000 by {\n assert(0 <= kk < nums@.len());\n }\n if hi_raw > 100_000 {\n assert(hi as int == 100_000);\n count_range_cap(nums@, n as int, lo as int, 100_000, hi_raw as int);\n } else {\n assert(hi as int == hi_raw as int);\n }\n assert(count_range(nums@, n as int, lo as int, hi as int)\n == count_range(nums@, n as int, lo as int, hi_raw as int));\n assert(count_range(nums@, n as int, lo as int, hi_raw as int)\n == prefix@[hi as int] as int - prefix@[lo as int - 1] as int);\n assert(g_upto_ei(nums@, v2 as int, k as int, n as int)\n == g_upto_ei(nums@, v2 as int, k as int - 1, n as int)\n + k * count_range(nums@, n as int, k as int * v2 as int, (k as int + 1) * v2 as int - 1));\n count_range_nonneg(nums@, n as int, lo as int, hi_raw as int);\n assert(0 <= prefix@[hi as int] as int - prefix@[lo as int - 1] as int <= n as int);\n g_upto_ei_bound(nums@, v2 as int, k as int, n as int);\n assert(0 <= k as int <= kmax_bound as int + 1 <= 100_001);\n assert(0 <= n as int <= 100_000);\n assert((k as int) * (k as int) <= 100_001 * 100_001) by (nonlinear_arith)\n requires 0 <= k as int <= 100_001;\n assert((k as int) * (k as int) * (n as int) <= (100_001 * 100_001) * 100_000) by (nonlinear_arith)\n requires (k as int) * (k as int) <= 100_001 * 100_001, 0 <= (k as int) * (k as int), 0 <= n as int <= 100_000;\n assert(g_upto_ei(nums@, v2 as int, k as int, n as int) <= 1_000_020_000_100_000);\n assert(g as int + (k as int) * (prefix@[hi as int] as int - prefix@[lo as int - 1] as int)\n == g_upto_ei(nums@, v2 as int, k as int, n as int));\n assert(0 <= g as int + (k as int) * (prefix@[hi as int] as int - prefix@[lo as int - 1] as int)\n <= 2_000_000_000_000_000);\n }\n let range_count = prefix[hi] - prefix[lo - 1];\n g = g + (k as i64) * range_count;\n k = k + 1;\n }\n proof {\n assert(k as int == kmax_bound as int + 1);\n assert(g as int == g_upto_ei(nums@, v2 as int, kmax_bound as int, n as int));\n assert(g as int == g_upto_ei(nums@, v2 as int, 100_000int / (v2 as int), n as int));\n }\n gval.push(g);\n v2 = v2 + 1;\n }\n\n proof {\n outer_sum_eq_total_by_col(nums@);\n }\n let mut total: i64 = 0;\n let mut j: usize = 0;\n while j < n\n invariant\n 0 <= j <= n,\n n == nums.len(),\n 1 <= nums.len() <= 100_000,\n modulo == 1_000_000_007,\n gval@.len() == 100_001,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums@[k] <= 100_000,\n forall |v: int| 1 <= v <= 100_000 ==> (#[trigger] gval@[v]) as int == g_upto_ei(nums@, v, 100_000int / v, n as int),\n forall |v: int| 1 <= v <= 100_000 ==> 0 <= #[trigger] gval@[v] <= 2_000_000_000_000_000i64,\n total as int == total_by_col_upto(nums@, j as int, n as int) % 1_000_000_007,\n 0 <= total < 1_000_000_007,\n total_by_col_upto(nums@, j as int, n as int) >= 0,\n decreases n - j,\n {\n let val = nums[j] as usize;\n proof {\n assert(1 <= val <= 100_000);\n assert(val as int == nums@[j as int] as int);\n assert(0 <= gval@[val as int] <= 2_000_000_000_000_000i64);\n assert(0 <= total as int + gval@[val as int] as int <= 3_000_000_000_000_000);\n }\n proof {\n col_sum_eq_g(nums@, j as int, n as int, val as int, 100_000int / val as int);\n assert(gval@[val as int] as int == col_sum(nums@, j as int, n as int));\n col_sum_nonneg(nums@, j as int, n as int);\n total_by_col_upto_nonneg(nums@, j as int, n as int);\n let total_val = total_by_col_upto(nums@, j as int, n as int);\n assert(total_by_col_upto(nums@, j as int + 1, n as int) == total_val + col_sum(nums@, j as int, n as int));\n lemma_mod_add(total_val, gval@[val as int] as int, 1_000_000_007);\n assert((total_val % 1_000_000_007 + gval@[val as int] as int) % 1_000_000_007\n == (total_val + gval@[val as int] as int) % 1_000_000_007);\n assert(total as int == total_val % 1_000_000_007);\n assert((total as int + gval@[val as int] as int) % 1_000_000_007\n == total_by_col_upto(nums@, j as int + 1, n as int) % 1_000_000_007);\n }\n total = (total + gval[val]) % modulo;\n j = j + 1;\n }\n (total % modulo) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1862/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1862/tests/mutated_testcases.jsonl"} {"problem": "lc1863", "family": "leetcode", "description": "# Sum of All Subset XOR Totals\n\nThe **XOR total** of an array is defined as the bitwise `XOR` of **all its elements**, or `0` if the array is **empty**.\n\n- For example, the **XOR total** of the array `[2,5,6]` is `2 XOR 5 XOR 6 = 1`.\n\nGiven an array `nums`, return *the **sum** of all **XOR totals** for every **subset** of* `nums`.\n\n**Note:** Subsets with the **same** elements should be counted **multiple** times.\n\nAn array `a` is a **subset** of an array `b` if `a` can be obtained from `b` by deleting some (possibly zero) elements of `b`.\n\n## Example 1:\n\n> **Input:** nums = [1,3]\n> **Output:** 6\n> **Explanation:** The 4 subsets of [1,3] are:\n> - The empty subset has an XOR total of 0.\n> - [1] has an XOR total of 1.\n> - [3] has an XOR total of 3.\n> - [1,3] has an XOR total of 1 XOR 3 = 2.\n> 0 + 1 + 3 + 2 = 6\n\n## Example 2:\n\n> **Input:** nums = [5,1,6]\n> **Output:** 28\n> **Explanation:** The 8 subsets of [5,1,6] are:\n> - The empty subset has an XOR total of 0.\n> - [5] has an XOR total of 5.\n> - [1] has an XOR total of 1.\n> - [6] has an XOR total of 6.\n> - [5,1] has an XOR total of 5 XOR 1 = 4.\n> - [5,6] has an XOR total of 5 XOR 6 = 3.\n> - [1,6] has an XOR total of 1 XOR 6 = 7.\n> - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.\n> 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28\n\n## Example 3:\n\n> **Input:** nums = [3,4,5,6,7,8]\n> **Output:** 480\n> **Explanation:** The sum of all XOR totals for every subset is 480.\n\n## Constraints:\n\n- `1 <= nums.length <= 12`\n- `1 <= nums[i] <= 20`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn subset_xor_sum(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration\nAcceptance rate: 90.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn xor_sum_rec(nums: Seq, idx: int, current_xor: i32) -> int\n decreases nums.len() - idx,\n{\n if idx >= nums.len() {\n current_xor as int\n } else {\n xor_sum_rec(nums, idx + 1, current_xor ^ nums[idx])\n + xor_sum_rec(nums, idx + 1, current_xor)\n }\n}\n\nimpl Solution {\n pub fn subset_xor_sum(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 12,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 20,\n ensures\n result as int == xor_sum_rec(nums@, 0, 0i32),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn subset_xor_sum(nums: Vec) -> i32 {\n Self::dfs(&nums, 0, 0)\n }\n\n fn dfs(nums: &Vec, idx: usize, current_xor: i32) -> i32 {\n if idx == nums.len() {\n return current_xor;\n }\n let include = Self::dfs(nums, idx + 1, current_xor ^ nums[idx]);\n let exclude = Self::dfs(nums, idx + 1, current_xor);\n include + exclude\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn pow2(n: int) -> int\n decreases n,\n{\n if n <= 0 { 1 } else { 2 * pow2(n - 1) }\n}\n\npub open spec fn xor_sum_rec(nums: Seq, idx: int, current_xor: i32) -> int\n decreases nums.len() - idx,\n{\n if idx >= nums.len() {\n current_xor as int\n } else {\n xor_sum_rec(nums, idx + 1, current_xor ^ nums[idx])\n + xor_sum_rec(nums, idx + 1, current_xor)\n }\n}\n\nimpl Solution {\n pub fn subset_xor_sum(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 12,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 20,\n ensures\n result as int == xor_sum_rec(nums@, 0, 0i32),\n {\n Self::dfs(&nums, 0, 0)\n }\n\n fn dfs(nums: &Vec, idx: usize, current_xor: i32) -> (result: i32)\n requires\n idx <= nums.len() <= 12,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 20,\n 0 <= current_xor <= 31,\n ensures\n result as int == xor_sum_rec(nums@, idx as int, current_xor),\n 0 <= result as int <= 31 * pow2((nums.len() - idx) as int),\n decreases nums.len() - idx,\n {\n if idx == nums.len() {\n return current_xor;\n }\n let include = Self::dfs(nums, idx + 1, current_xor ^ nums[idx]);\n let exclude = Self::dfs(nums, idx + 1, current_xor);\n include + exclude\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn pow2(n: int) -> int\n decreases n,\n{\n if n <= 0 { 1 } else { 2 * pow2(n - 1) }\n}\n\npub open spec fn xor_sum_rec(nums: Seq, idx: int, current_xor: i32) -> int\n decreases nums.len() - idx,\n{\n if idx >= nums.len() {\n current_xor as int\n } else {\n xor_sum_rec(nums, idx + 1, current_xor ^ nums[idx])\n + xor_sum_rec(nums, idx + 1, current_xor)\n }\n}\n\nproof fn lemma_pow2_positive(n: int)\n ensures pow2(n) >= 1,\n decreases n,\n{\n if n > 0 {\n lemma_pow2_positive(n - 1);\n }\n}\n\nproof fn lemma_pow2_monotone(a: int, b: int)\n requires 0 <= a <= b,\n ensures pow2(a) <= pow2(b),\n decreases b - a,\n{\n if a < b {\n lemma_pow2_monotone(a, b - 1);\n lemma_pow2_positive(b - 1);\n }\n}\n\nproof fn lemma_xor_bound(a: i32, b: i32)\n requires\n 0 <= a <= 31,\n 0 <= b <= 20,\n ensures\n 0 <= (a ^ b) <= 31,\n{\n assert(0 <= a ^ b <= 31) by(bit_vector)\n requires 0 <= a <= 31i32, 0 <= b <= 20i32;\n}\n\nimpl Solution {\n pub fn subset_xor_sum(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 12,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 20,\n ensures\n result as int == xor_sum_rec(nums@, 0, 0i32),\n {\n Self::dfs(&nums, 0, 0)\n }\n\n fn dfs(nums: &Vec, idx: usize, current_xor: i32) -> (result: i32)\n requires\n idx <= nums.len() <= 12,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 20,\n 0 <= current_xor <= 31,\n ensures\n result as int == xor_sum_rec(nums@, idx as int, current_xor),\n 0 <= result as int <= 31 * pow2((nums.len() - idx) as int),\n decreases nums.len() - idx,\n {\n if idx == nums.len() {\n return current_xor;\n }\n proof { lemma_xor_bound(current_xor, nums[idx as int]); }\n let include = Self::dfs(nums, idx + 1, current_xor ^ nums[idx]);\n let exclude = Self::dfs(nums, idx + 1, current_xor);\n proof {\n assert(pow2((nums.len() - idx) as int) == 2 * pow2((nums.len() - idx - 1) as int));\n lemma_pow2_monotone((nums.len() - idx) as int, 12);\n reveal_with_fuel(pow2, 14);\n assert(pow2(12) == 4096);\n }\n include + exclude\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1863/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1863/tests/mutated_testcases.jsonl"} {"problem": "lc1893", "family": "leetcode", "description": "# Check if All the Integers in a Range Are Covered\n\nYou are given a 2D integer array `ranges` and two integers `left` and `right`. Each `ranges[i] = [start_i, end_i]` represents an **inclusive** interval between `start_i` and `end_i`.\n\nReturn `true` *if each integer in the inclusive range* `[left, right]` *is covered by **at least one** interval in* `ranges`. Return `false` *otherwise*.\n\nAn integer `x` is covered by an interval `ranges[i] = [start_i, end_i]` if `start_i <= x <= end_i`.\n\n## Example 1:\n\n> **Input:** ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5 \n> **Output:** true \n> **Explanation:** Every integer between 2 and 5 is covered: \n> - 2 is covered by the first range. \n> - 3 and 4 are covered by the second range. \n> - 5 is covered by the third range.\n\n## Example 2:\n\n> **Input:** ranges = [[1,10],[10,20]], left = 21, right = 21 \n> **Output:** false \n> **Explanation:** 21 is not covered by any range.\n\n## Constraints:\n\n- `1 <= ranges.length <= 50`\n- `1 <= start_i <= end_i <= 50`\n- `1 <= left <= right <= 50`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_covered(ranges: Vec>, left: i32, right: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Prefix Sum\nAcceptance rate: 50.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn is_covered_spec(ranges: Seq>, x: int) -> bool {\n exists |j: int| 0 <= j < ranges.len() && #[trigger] ranges[j][0] <= x && x <= ranges[j][1]\n}\n\nimpl Solution {\n pub fn is_covered(ranges: Vec>, left: i32, right: i32) -> (result: bool)\n requires\n 1 <= ranges.len() <= 50,\n 1 <= left <= right <= 50,\n forall |j: int| 0 <= j < ranges.len() ==> #[trigger] ranges[j]@.len() == 2,\n forall |j: int| 0 <= j < ranges.len() ==> 1 <= #[trigger] ranges[j][0] && ranges[j][0] <= ranges[j][1] && ranges[j][1] <= 50,\n ensures\n result <==> forall |i: int| left <= i <= right ==> #[trigger] is_covered_spec(ranges@, i),\n {\n }\n}\n}\n", "code": "impl Solution {\n pub fn is_covered(ranges: Vec>, left: i32, right: i32) -> bool {\n let mut i = left;\n while i <= right {\n let mut covered = false;\n let mut j = 0;\n while j < ranges.len() {\n if ranges[j][0] <= i && i <= ranges[j][1] {\n covered = true;\n }\n j += 1;\n }\n if !covered {\n return false;\n }\n i += 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn is_covered_spec(ranges: Seq>, x: int) -> bool {\n exists |j: int| 0 <= j < ranges.len() && #[trigger] ranges[j][0] <= x && x <= ranges[j][1]\n}\n\nimpl Solution {\n pub fn is_covered(ranges: Vec>, left: i32, right: i32) -> (result: bool)\n requires\n 1 <= ranges.len() <= 50,\n 1 <= left <= right <= 50,\n forall |j: int| 0 <= j < ranges.len() ==> #[trigger] ranges[j]@.len() == 2,\n forall |j: int| 0 <= j < ranges.len() ==> 1 <= #[trigger] ranges[j][0] && ranges[j][0] <= ranges[j][1] && ranges[j][1] <= 50,\n ensures\n result <==> forall |i: int| left <= i <= right ==> #[trigger] is_covered_spec(ranges@, i),\n {\n let mut i = left;\n while i <= right {\n let mut covered = false;\n let mut j = 0;\n while j < ranges.len() {\n if ranges[j][0] <= i && i <= ranges[j][1] {\n covered = true;\n }\n j += 1;\n }\n if !covered {\n return false;\n }\n i += 1;\n }\n true\n }\n}\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn is_covered_spec(ranges: Seq>, x: int) -> bool {\n exists |j: int| 0 <= j < ranges.len() && #[trigger] ranges[j][0] <= x && x <= ranges[j][1]\n}\n\nimpl Solution {\n pub fn is_covered(ranges: Vec>, left: i32, right: i32) -> (result: bool)\n requires\n 1 <= ranges.len() <= 50,\n 1 <= left <= right <= 50,\n forall |j: int| 0 <= j < ranges.len() ==> #[trigger] ranges[j]@.len() == 2,\n forall |j: int| 0 <= j < ranges.len() ==> 1 <= #[trigger] ranges[j][0] && ranges[j][0] <= ranges[j][1] && ranges[j][1] <= 50,\n ensures\n result <==> forall |i: int| left <= i <= right ==> #[trigger] is_covered_spec(ranges@, i),\n {\n let mut i = left;\n while i <= right\n invariant\n left <= i <= right + 1,\n left <= right,\n forall |ii: int| left <= ii < i ==> #[trigger] is_covered_spec(ranges@, ii),\n 1 <= ranges.len() <= 50,\n forall |j: int| 0 <= j < ranges.len() ==> #[trigger] ranges[j]@.len() == 2,\n forall |j: int| 0 <= j < ranges.len() ==> 1 <= #[trigger] ranges[j][0] && ranges[j][0] <= ranges[j][1] && ranges[j][1] <= 50\n decreases right - i + 1\n {\n let mut covered = false;\n let mut j = 0;\n while j < ranges.len()\n invariant\n 0 <= j <= ranges.len(),\n 1 <= ranges.len() <= 50,\n left <= i <= right,\n forall |k: int| 0 <= k < ranges.len() ==> #[trigger] ranges[k]@.len() == 2,\n covered <==> exists |k: int| 0 <= k < j && #[trigger] ranges[k][0] <= i && i <= ranges[k][1]\n decreases ranges.len() - j\n {\n if ranges[j][0] <= i && i <= ranges[j][1] {\n covered = true;\n }\n j += 1;\n }\n if !covered {\n proof {\n assert(!is_covered_spec(ranges@, i as int));\n }\n return false;\n }\n proof {\n assert(is_covered_spec(ranges@, i as int));\n }\n i += 1;\n }\n true\n }\n}\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1893/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1893/tests/mutated_testcases.jsonl"} {"problem": "lc1900", "family": "leetcode", "description": "# The Earliest and Latest Rounds Where Players Compete\n\nThere is a tournament where `n` players are participating. The players are standing in a single row and are numbered from `1` to `n` based on their **initial** standing position (player `1` is the first player in the row, player `2` is the second player in the row, etc.).\n\nThe tournament consists of multiple rounds (starting from round number `1`). In each round, the `i`-th player from the front of the row competes against the `i`-th player from the end of the row, and the winner advances to the next round. When the number of players is odd for the current round, the player in the middle automatically advances to the next round.\n\n- For example, if the row consists of players `1, 2, 4, 6, 7`:\n - Player `1` competes against player `7`.\n - Player `2` competes against player `6`.\n - Player `4` automatically advances to the next round.\n\nAfter each round is over, the winners are lined back up in the row based on the **original ordering** assigned to them initially (ascending order).\n\nThe players numbered `firstPlayer` and `secondPlayer` are the best in the tournament. They can win against any other player before they compete against each other. If any two other players compete against each other, either of them might win, and thus you may **choose** the outcome of this round.\n\nGiven the integers `n`, `firstPlayer`, and `secondPlayer`, return an integer array containing two values, the **earliest** possible round number and the **latest** possible round number in which these two players will compete against each other, respectively.\n\n## Example 1:\n\n> **Input:** n = 11, firstPlayer = 2, secondPlayer = 4\n> **Output:** [3, 4]\n> **Explanation:**\n> One possible scenario which leads to the earliest round number:\n> First round: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\n> Second round: 2, 3, 4, 5, 6, 11\n> Third round: 2, 3, 4\n> One possible scenario which leads to the latest round number:\n> First round: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\n> Second round: 1, 2, 3, 4, 5, 6\n> Third round: 1, 2, 4\n> Fourth round: 2, 4\n\n## Example 2:\n\n> **Input:** n = 5, firstPlayer = 1, secondPlayer = 5\n> **Output:** [1, 1]\n> **Explanation:** The players numbered 1 and 5 compete in the first round. There is no way to make them compete in any other round.\n\n## Constraints:\n\n- 2 <= n <= 28\n- 1 <= firstPlayer < secondPlayer <= n\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn earliest_and_latest(n: i32, first_player: i32, second_player: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Hard\nDynamic Programming, Memoization\nAcceptance rate: 72.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn scan_j_early(l: int, r: int, m: int, i: int, j: int, j_hi: int) -> int\n decreases m, 0int, spec_max(j_hi - j + 1, 0)\n{\n if j > j_hi || j < 1 || m < 3 {\n 30\n } else {\n let nxt = (m + 1) / 2;\n let ii = spec_min(i, j);\n let jj = spec_max(i, j);\n let cur = 1 + spec_earliest(ii, jj, nxt);\n let rest = scan_j_early(l, r, m, i, j + 1, j_hi);\n spec_min(cur, rest)\n }\n}\n\npub open spec fn scan_i_early(l: int, r: int, m: int, i: int) -> int\n decreases m, 1int, spec_max(l - i + 1, 0)\n{\n if i > l || i < 1 || m < 3 {\n 30\n } else {\n let nxt = (m + 1) / 2;\n let j_lo = spec_max(l - i + 1, spec_max(l + r - m / 2 - i, 1));\n let j_hi = spec_min(r - i, nxt - i);\n let cur = scan_j_early(l, r, m, i, j_lo, j_hi);\n let rest = scan_i_early(l, r, m, i + 1);\n spec_min(cur, rest)\n }\n}\n\npub open spec fn spec_earliest(l: int, r: int, m: int) -> int\n decreases m, 2int, 0int\n{\n let ll = spec_min(l, r);\n let rr = spec_max(l, r);\n if m < 2 || ll < 1 || rr < 1 || ll + rr > m {\n 30\n } else if ll == rr {\n 1\n } else {\n scan_i_early(ll, rr, m, 1)\n }\n}\n\npub open spec fn scan_j_late(l: int, r: int, m: int, i: int, j: int, j_hi: int) -> int\n decreases m, 0int, spec_max(j_hi - j + 1, 0)\n{\n if j > j_hi || j < 1 || m < 3 {\n 0\n } else {\n let nxt = (m + 1) / 2;\n let ii = spec_min(i, j);\n let jj = spec_max(i, j);\n let cur = 1 + spec_latest(ii, jj, nxt);\n let rest = scan_j_late(l, r, m, i, j + 1, j_hi);\n spec_max(cur, rest)\n }\n}\n\npub open spec fn scan_i_late(l: int, r: int, m: int, i: int) -> int\n decreases m, 1int, spec_max(l - i + 1, 0)\n{\n if i > l || i < 1 || m < 3 {\n 0\n } else {\n let nxt = (m + 1) / 2;\n let j_lo = spec_max(l - i + 1, spec_max(l + r - m / 2 - i, 1));\n let j_hi = spec_min(r - i, nxt - i);\n let cur = scan_j_late(l, r, m, i, j_lo, j_hi);\n let rest = scan_i_late(l, r, m, i + 1);\n spec_max(cur, rest)\n }\n}\n\npub open spec fn spec_latest(l: int, r: int, m: int) -> int\n decreases m, 2int, 0int\n{\n let ll = spec_min(l, r);\n let rr = spec_max(l, r);\n if m < 2 || ll < 1 || rr < 1 || ll + rr > m {\n 0\n } else if ll == rr {\n 1\n } else {\n scan_i_late(ll, rr, m, 1)\n }\n}\n\nimpl Solution {\n pub fn earliest_and_latest(n: i32, first_player: i32, second_player: i32) -> (result: Vec)\n requires\n 2 <= n <= 28,\n 1 <= first_player < second_player <= n,\n ensures\n result@.len() == 2,\n result@[0] == spec_earliest(first_player as int, (n - second_player + 1) as int, n as int),\n result@[1] == spec_latest(first_player as int, (n - second_player + 1) as int, n as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n fn dp(l: i32, r: i32, m: i32) -> (i32, i32) {\n if l == r {\n return (1, 1);\n }\n let nxt = (m + 1) / 2;\n let mut best_min = 30i32;\n let mut best_max = 0i32;\n let mut i = 1i32;\n while i <= l {\n let j_lo_a = l - i + 1;\n let j_lo_b = l + r - m / 2 - i;\n let mut j_lo = if j_lo_a >= j_lo_b { j_lo_a } else { j_lo_b };\n if j_lo < 1 {\n j_lo = 1;\n }\n let j_hi_a = r - i;\n let j_hi_b = nxt - i;\n let j_hi = if j_hi_a <= j_hi_b { j_hi_a } else { j_hi_b };\n let mut local_min = 30i32;\n let mut local_max = 0i32;\n let mut j = j_lo;\n while j <= j_hi {\n let ii = if i <= j { i } else { j };\n let jj = if i <= j { j } else { i };\n let (sub_min, sub_max) = Self::dp(ii, jj, nxt);\n if sub_min + 1 < local_min {\n local_min = sub_min + 1;\n }\n if sub_max + 1 > local_max {\n local_max = sub_max + 1;\n }\n j += 1;\n }\n if local_min < best_min {\n best_min = local_min;\n }\n if local_max > best_max {\n best_max = local_max;\n }\n i += 1;\n }\n (best_min, best_max)\n }\n\n pub fn earliest_and_latest(n: i32, first_player: i32, second_player: i32) -> Vec {\n let l0 = first_player;\n let r0 = n - second_player + 1;\n let ll = if l0 <= r0 { l0 } else { r0 };\n let rr = if l0 <= r0 { r0 } else { l0 };\n let (e, la) = Self::dp(ll, rr, n);\n vec![e, la]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn scan_j_early(l: int, r: int, m: int, i: int, j: int, j_hi: int) -> int\n decreases m, 0int, spec_max(j_hi - j + 1, 0)\n{\n if j > j_hi || j < 1 || m < 3 {\n 30\n } else {\n let nxt = (m + 1) / 2;\n let ii = spec_min(i, j);\n let jj = spec_max(i, j);\n let cur = 1 + spec_earliest(ii, jj, nxt);\n let rest = scan_j_early(l, r, m, i, j + 1, j_hi);\n spec_min(cur, rest)\n }\n}\n\npub open spec fn scan_i_early(l: int, r: int, m: int, i: int) -> int\n decreases m, 1int, spec_max(l - i + 1, 0)\n{\n if i > l || i < 1 || m < 3 {\n 30\n } else {\n let nxt = (m + 1) / 2;\n let j_lo = spec_max(l - i + 1, spec_max(l + r - m / 2 - i, 1));\n let j_hi = spec_min(r - i, nxt - i);\n let cur = scan_j_early(l, r, m, i, j_lo, j_hi);\n let rest = scan_i_early(l, r, m, i + 1);\n spec_min(cur, rest)\n }\n}\n\npub open spec fn spec_earliest(l: int, r: int, m: int) -> int\n decreases m, 2int, 0int\n{\n let ll = spec_min(l, r);\n let rr = spec_max(l, r);\n if m < 2 || ll < 1 || rr < 1 || ll + rr > m {\n 30\n } else if ll == rr {\n 1\n } else {\n scan_i_early(ll, rr, m, 1)\n }\n}\n\npub open spec fn scan_j_late(l: int, r: int, m: int, i: int, j: int, j_hi: int) -> int\n decreases m, 0int, spec_max(j_hi - j + 1, 0)\n{\n if j > j_hi || j < 1 || m < 3 {\n 0\n } else {\n let nxt = (m + 1) / 2;\n let ii = spec_min(i, j);\n let jj = spec_max(i, j);\n let cur = 1 + spec_latest(ii, jj, nxt);\n let rest = scan_j_late(l, r, m, i, j + 1, j_hi);\n spec_max(cur, rest)\n }\n}\n\npub open spec fn scan_i_late(l: int, r: int, m: int, i: int) -> int\n decreases m, 1int, spec_max(l - i + 1, 0)\n{\n if i > l || i < 1 || m < 3 {\n 0\n } else {\n let nxt = (m + 1) / 2;\n let j_lo = spec_max(l - i + 1, spec_max(l + r - m / 2 - i, 1));\n let j_hi = spec_min(r - i, nxt - i);\n let cur = scan_j_late(l, r, m, i, j_lo, j_hi);\n let rest = scan_i_late(l, r, m, i + 1);\n spec_max(cur, rest)\n }\n}\n\npub open spec fn spec_latest(l: int, r: int, m: int) -> int\n decreases m, 2int, 0int\n{\n let ll = spec_min(l, r);\n let rr = spec_max(l, r);\n if m < 2 || ll < 1 || rr < 1 || ll + rr > m {\n 0\n } else if ll == rr {\n 1\n } else {\n scan_i_late(ll, rr, m, 1)\n }\n}\n\nimpl Solution {\n fn dp(l: i32, r: i32, m: i32) -> (result: (i32, i32))\n requires\n 1 <= l <= r,\n l + r <= m,\n 2 <= m <= 28,\n ensures\n result.0 as int == spec_earliest(l as int, r as int, m as int),\n result.1 as int == spec_latest(l as int, r as int, m as int),\n 0 <= result.0 <= 30,\n 0 <= result.1 <= m,\n decreases m,\n {\n if l == r {\n return (1, 1);\n }\n let nxt = (m + 1) / 2;\n let mut best_min = 30i32;\n let mut best_max = 0i32;\n let mut i = 1i32;\n while i <= l {\n let j_lo_a = l - i + 1;\n let j_lo_b = l + r - m / 2 - i;\n let mut j_lo = if j_lo_a >= j_lo_b { j_lo_a } else { j_lo_b };\n if j_lo < 1 {\n j_lo = 1;\n }\n let j_hi_a = r - i;\n let j_hi_b = nxt - i;\n let j_hi = if j_hi_a <= j_hi_b { j_hi_a } else { j_hi_b };\n let mut local_min = 30i32;\n let mut local_max = 0i32;\n let mut j = j_lo;\n while j <= j_hi {\n let ii = if i <= j { i } else { j };\n let jj = if i <= j { j } else { i };\n let (sub_min, sub_max) = Self::dp(ii, jj, nxt);\n if sub_min + 1 < local_min {\n local_min = sub_min + 1;\n }\n if sub_max + 1 > local_max {\n local_max = sub_max + 1;\n }\n j += 1;\n }\n if local_min < best_min {\n best_min = local_min;\n }\n if local_max > best_max {\n best_max = local_max;\n }\n i += 1;\n }\n (best_min, best_max)\n }\n\n pub fn earliest_and_latest(n: i32, first_player: i32, second_player: i32) -> (result: Vec)\n requires\n 2 <= n <= 28,\n 1 <= first_player < second_player <= n,\n ensures\n result@.len() == 2,\n result@[0] == spec_earliest(first_player as int, (n - second_player + 1) as int, n as int),\n result@[1] == spec_latest(first_player as int, (n - second_player + 1) as int, n as int),\n {\n let l0 = first_player;\n let r0 = n - second_player + 1;\n let ll = if l0 <= r0 { l0 } else { r0 };\n let rr = if l0 <= r0 { r0 } else { l0 };\n let (e, la) = Self::dp(ll, rr, n);\n vec![e, la]\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n}\n\npub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn scan_j_early(l: int, r: int, m: int, i: int, j: int, j_hi: int) -> int\n decreases m, 0int, spec_max(j_hi - j + 1, 0)\n{\n if j > j_hi || j < 1 || m < 3 {\n 30\n } else {\n let nxt = (m + 1) / 2;\n let ii = spec_min(i, j);\n let jj = spec_max(i, j);\n let cur = 1 + spec_earliest(ii, jj, nxt);\n let rest = scan_j_early(l, r, m, i, j + 1, j_hi);\n spec_min(cur, rest)\n }\n}\n\npub open spec fn scan_i_early(l: int, r: int, m: int, i: int) -> int\n decreases m, 1int, spec_max(l - i + 1, 0)\n{\n if i > l || i < 1 || m < 3 {\n 30\n } else {\n let nxt = (m + 1) / 2;\n let j_lo = spec_max(l - i + 1, spec_max(l + r - m / 2 - i, 1));\n let j_hi = spec_min(r - i, nxt - i);\n let cur = scan_j_early(l, r, m, i, j_lo, j_hi);\n let rest = scan_i_early(l, r, m, i + 1);\n spec_min(cur, rest)\n }\n}\n\npub open spec fn spec_earliest(l: int, r: int, m: int) -> int\n decreases m, 2int, 0int\n{\n let ll = spec_min(l, r);\n let rr = spec_max(l, r);\n if m < 2 || ll < 1 || rr < 1 || ll + rr > m {\n 30\n } else if ll == rr {\n 1\n } else {\n scan_i_early(ll, rr, m, 1)\n }\n}\n\npub open spec fn scan_j_late(l: int, r: int, m: int, i: int, j: int, j_hi: int) -> int\n decreases m, 0int, spec_max(j_hi - j + 1, 0)\n{\n if j > j_hi || j < 1 || m < 3 {\n 0\n } else {\n let nxt = (m + 1) / 2;\n let ii = spec_min(i, j);\n let jj = spec_max(i, j);\n let cur = 1 + spec_latest(ii, jj, nxt);\n let rest = scan_j_late(l, r, m, i, j + 1, j_hi);\n spec_max(cur, rest)\n }\n}\n\npub open spec fn scan_i_late(l: int, r: int, m: int, i: int) -> int\n decreases m, 1int, spec_max(l - i + 1, 0)\n{\n if i > l || i < 1 || m < 3 {\n 0\n } else {\n let nxt = (m + 1) / 2;\n let j_lo = spec_max(l - i + 1, spec_max(l + r - m / 2 - i, 1));\n let j_hi = spec_min(r - i, nxt - i);\n let cur = scan_j_late(l, r, m, i, j_lo, j_hi);\n let rest = scan_i_late(l, r, m, i + 1);\n spec_max(cur, rest)\n }\n}\n\npub open spec fn spec_latest(l: int, r: int, m: int) -> int\n decreases m, 2int, 0int\n{\n let ll = spec_min(l, r);\n let rr = spec_max(l, r);\n if m < 2 || ll < 1 || rr < 1 || ll + rr > m {\n 0\n } else if ll == rr {\n 1\n } else {\n scan_i_late(ll, rr, m, 1)\n }\n}\n\nproof fn lemma_scan_j_early_bound(l: int, r: int, m: int, i: int, j: int, j_hi: int)\n ensures\n scan_j_early(l, r, m, i, j, j_hi) <= 30,\n decreases m, 0int, spec_max(j_hi - j + 1, 0),\n{\n reveal_with_fuel(scan_j_early, 2);\n if j > j_hi || j < 1 || m < 3 {\n } else {\n lemma_scan_j_early_bound(l, r, m, i, j + 1, j_hi);\n }\n}\n\nproof fn lemma_scan_i_early_bound(l: int, r: int, m: int, i: int)\n ensures\n scan_i_early(l, r, m, i) <= 30,\n decreases m, 1int, spec_max(l - i + 1, 0),\n{\n reveal_with_fuel(scan_i_early, 2);\n if i > l || i < 1 || m < 3 {\n } else {\n lemma_scan_i_early_bound(l, r, m, i + 1);\n }\n}\n\nproof fn lemma_scan_j_late_bound(l: int, r: int, m: int, i: int, j: int, j_hi: int)\n ensures\n scan_j_late(l, r, m, i, j, j_hi) >= 0,\n decreases m, 0int, spec_max(j_hi - j + 1, 0),\n{\n reveal_with_fuel(scan_j_late, 2);\n if j > j_hi || j < 1 || m < 3 {\n } else {\n lemma_scan_j_late_bound(l, r, m, i, j + 1, j_hi);\n }\n}\n\nproof fn lemma_scan_i_late_bound(l: int, r: int, m: int, i: int)\n ensures\n scan_i_late(l, r, m, i) >= 0,\n decreases m, 1int, spec_max(l - i + 1, 0),\n{\n reveal_with_fuel(scan_i_late, 2);\n if i > l || i < 1 || m < 3 {\n } else {\n lemma_scan_i_late_bound(l, r, m, i + 1);\n }\n}\n\nimpl Solution {\n fn dp(l: i32, r: i32, m: i32) -> (result: (i32, i32))\n requires\n 1 <= l <= r,\n l + r <= m,\n 2 <= m <= 28,\n ensures\n result.0 as int == spec_earliest(l as int, r as int, m as int),\n result.1 as int == spec_latest(l as int, r as int, m as int),\n 0 <= result.0 <= 30,\n 0 <= result.1 <= m,\n decreases m,\n {\n proof {\n reveal_with_fuel(spec_earliest, 2);\n reveal_with_fuel(spec_latest, 2);\n }\n if l == r {\n return (1, 1);\n }\n let nxt = (m + 1) / 2;\n let mut best_min = 30i32;\n let mut best_max = 0i32;\n let mut i = 1i32;\n\n proof {\n lemma_scan_i_early_bound(l as int, r as int, m as int, 1);\n lemma_scan_i_late_bound(l as int, r as int, m as int, 1);\n }\n\n while i <= l\n invariant\n 1 <= l <= r,\n l < r,\n l + r <= m,\n 3 <= m <= 28,\n nxt == (m + 1) / 2,\n 1 <= i <= l + 1,\n 0 <= best_max <= m,\n 0 <= best_min <= 30,\n spec_min(best_min as int, scan_i_early(l as int, r as int, m as int, i as int))\n == scan_i_early(l as int, r as int, m as int, 1),\n spec_max(best_max as int, scan_i_late(l as int, r as int, m as int, i as int))\n == scan_i_late(l as int, r as int, m as int, 1),\n decreases l - i + 1,\n {\n let j_lo_a = l - i + 1;\n let j_lo_b = l + r - m / 2 - i;\n let mut j_lo = if j_lo_a >= j_lo_b { j_lo_a } else { j_lo_b };\n if j_lo < 1 {\n j_lo = 1;\n }\n let j_hi_a = r - i;\n let j_hi_b = nxt - i;\n let j_hi = if j_hi_a <= j_hi_b { j_hi_a } else { j_hi_b };\n\n let mut local_min = 30i32;\n let mut local_max = 0i32;\n let mut j = j_lo;\n\n proof {\n lemma_scan_j_early_bound(l as int, r as int, m as int, i as int, j_lo as int, j_hi as int);\n lemma_scan_j_late_bound(l as int, r as int, m as int, i as int, j_lo as int, j_hi as int);\n }\n\n while j <= j_hi\n invariant\n 1 <= l <= r,\n l < r,\n l + r <= m,\n 3 <= m <= 28,\n nxt == (m + 1) / 2,\n 1 <= i <= l,\n j_lo <= j <= j_hi + 1,\n j_lo as int == spec_max((l - i + 1) as int, spec_max((l + r - m / 2 - i) as int, 1)),\n j_hi as int == spec_min((r - i) as int, (nxt - i) as int),\n 0 <= local_max <= m,\n 0 <= local_min <= 30,\n spec_min(local_min as int, scan_j_early(l as int, r as int, m as int, i as int, j as int, j_hi as int))\n == scan_j_early(l as int, r as int, m as int, i as int, j_lo as int, j_hi as int),\n spec_max(local_max as int, scan_j_late(l as int, r as int, m as int, i as int, j as int, j_hi as int))\n == scan_j_late(l as int, r as int, m as int, i as int, j_lo as int, j_hi as int),\n decreases j_hi - j + 1,\n {\n let ii = if i <= j { i } else { j };\n let jj = if i <= j { j } else { i };\n\n proof {\n assert(1 <= ii <= jj);\n assert(ii + jj == i + j);\n assert(j <= nxt - i) by {\n assert(j <= j_hi);\n assert(j_hi as int <= (nxt - i) as int);\n }\n assert(ii + jj <= nxt);\n assert(m >= 3) by {\n assert(l >= 1 && r >= 2 && l + r <= m);\n }\n assert(2 <= nxt <= 14) by {\n assert(nxt == (m + 1) / 2);\n assert(3 <= m <= 28);\n }\n }\n\n let (sub_min, sub_max) = Self::dp(ii, jj, nxt);\n\n proof {\n reveal_with_fuel(scan_j_early, 2);\n reveal_with_fuel(scan_j_late, 2);\n assert(sub_min as int == spec_earliest(ii as int, jj as int, nxt as int));\n assert(sub_max as int == spec_latest(ii as int, jj as int, nxt as int));\n assert(0 <= sub_min <= 30);\n assert(0 <= sub_max <= nxt);\n assert(sub_max + 1 <= nxt + 1 <= m) by {\n assert(nxt == (m + 1) / 2);\n assert(m >= 3);\n }\n lemma_scan_j_early_bound(l as int, r as int, m as int, i as int, (j + 1) as int, j_hi as int);\n lemma_scan_j_late_bound(l as int, r as int, m as int, i as int, (j + 1) as int, j_hi as int);\n }\n\n if sub_min + 1 < local_min {\n local_min = sub_min + 1;\n }\n if sub_max + 1 > local_max {\n local_max = sub_max + 1;\n }\n j += 1;\n }\n\n proof {\n reveal_with_fuel(scan_j_early, 2);\n reveal_with_fuel(scan_j_late, 2);\n assert(local_min as int == scan_j_early(l as int, r as int, m as int, i as int, j_lo as int, j_hi as int));\n assert(local_max as int == scan_j_late(l as int, r as int, m as int, i as int, j_lo as int, j_hi as int));\n reveal_with_fuel(scan_i_early, 2);\n reveal_with_fuel(scan_i_late, 2);\n lemma_scan_i_early_bound(l as int, r as int, m as int, (i + 1) as int);\n lemma_scan_i_late_bound(l as int, r as int, m as int, (i + 1) as int);\n }\n\n if local_min < best_min {\n best_min = local_min;\n }\n if local_max > best_max {\n best_max = local_max;\n }\n\n i += 1;\n }\n\n proof {\n reveal_with_fuel(scan_i_early, 2);\n reveal_with_fuel(scan_i_late, 2);\n }\n\n (best_min, best_max)\n }\n\n pub fn earliest_and_latest(n: i32, first_player: i32, second_player: i32) -> (result: Vec)\n requires\n 2 <= n <= 28,\n 1 <= first_player < second_player <= n,\n ensures\n result@.len() == 2,\n result@[0] == spec_earliest(first_player as int, (n - second_player + 1) as int, n as int),\n result@[1] == spec_latest(first_player as int, (n - second_player + 1) as int, n as int),\n {\n let l0 = first_player;\n let r0 = n - second_player + 1;\n let ll = if l0 <= r0 { l0 } else { r0 };\n let rr = if l0 <= r0 { r0 } else { l0 };\n\n proof {\n reveal_with_fuel(spec_earliest, 2);\n reveal_with_fuel(spec_latest, 2);\n }\n\n let (e, la) = Self::dp(ll, rr, n);\n vec![e, la]\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1900/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1900/tests/mutated_testcases.jsonl"} {"problem": "lc1920", "family": "leetcode", "description": "# Build Array from Permutation\n\nGiven a **zero-based permutation** `nums` (**0-indexed**), build an array `ans` of the **same length** where `ans[i] = nums[nums[i]]` for each `0 <= i < nums.length` and return it.\n\nA **zero-based permutation** `nums` is an array of **distinct** integers from `0` to `nums.length - 1` (inclusive).\n\n## Example 1:\n\n> **Input:** nums = [0,2,1,5,3,4]\n> **Output:** [0,1,2,4,5,3]\n> **Explanation:** ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]] = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]] = [0,1,2,4,5,3]\n\n## Example 2:\n\n> **Input:** nums = [5,0,1,2,3,4]\n> **Output:** [4,5,0,1,2,3]\n> **Explanation:** ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]] = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]] = [4,5,0,1,2,3]\n\n## Constraints:\n\n- `1 <= nums.length <= 1000`\n- `0 <= nums[i] < nums.length`\n- The elements in `nums` are **distinct**.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn build_array(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Simulation\nAcceptance rate: 91.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn build_array(nums: Vec) -> (ans: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < nums.len(),\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n ans.len() == nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] ans[i] == nums[nums[i] as int],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn build_array(nums: Vec) -> Vec {\n let n = nums.len();\n let mut ans = vec![0i32; n];\n let mut i: usize = 0;\n while i < n {\n ans[i] = nums[nums[i] as usize];\n i += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn build_array(nums: Vec) -> (ans: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < nums.len(),\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n ans.len() == nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] ans[i] == nums[nums[i] as int],\n {\n let n = nums.len();\n let mut ans = vec![0i32; n];\n let mut i: usize = 0;\n while i < n {\n ans.set(i, nums[nums[i] as usize]);\n i += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn build_array(nums: Vec) -> (ans: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < nums.len(),\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n ans.len() == nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] ans[i] == nums[nums[i] as int],\n {\n let n = nums.len();\n let mut ans = vec![0i32; n];\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 1 <= n <= 1000,\n ans.len() == n,\n forall|j: int| 0 <= j < nums.len() ==> 0 <= #[trigger] nums[j] < nums.len(),\n forall|j: int| 0 <= j < i ==> #[trigger] ans[j] == nums[nums[j] as int],\n decreases n - i,\n {\n ans.set(i, nums[nums[i] as usize]);\n i += 1;\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1920/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1920/tests/mutated_testcases.jsonl"} {"problem": "lc1922", "family": "leetcode", "description": "# 1922. Count Good Numbers\n\nA digit string is **good** if the digits **(0-indexed)** at **even** indices are **even** and the digits at **odd** indices are **prime** (`2`, `3`, `5`, or `7`).\n\n- For example, `\"2582\"` is good because the digits (`2` and `8`) at even positions are even and the digits (`5` and `2`) at odd positions are prime. However, `\"3245\"` is **not** good because `3` is at an even index but is not even.\n\nGiven an integer `n`, return *the **total** number of good digit strings of length* `n`. Since the answer may be large, **return it modulo** `10**9 + 7`.\n\nA **digit string** is a string consisting of digits `0` through `9` that may contain leading zeros.\n\n## Example 1:\n```\nInput: n = 1\nOutput: 5\nExplanation: The good numbers of length 1 are \"0\", \"2\", \"4\", \"6\", \"8\".\n```\n\n## Example 2:\n```\nInput: n = 4\nOutput: 400\n```\n\n## Example 3:\n```\nInput: n = 50\nOutput: 564908303\n```\n\n## Constraints:\n\n- `1 <= n <= 10**15`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_good_numbers(n: i64) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath\nRecursion\nAcceptance rate: 57.2%", "spec": "use vstd::arithmetic::power::pow;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub const M: u64 = 1_000_000_007;\n\n pub open spec fn count_good_numbers_spec_inner(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 1\n } else if n % 2 == 1 {\n 5 * Self::count_good_numbers_spec_inner((n - 1) as nat)\n } else {\n 4 * Self::count_good_numbers_spec_inner((n - 1) as nat)\n }\n }\n\n pub open spec fn count_good_numbers_spec(n: nat) -> nat {\n Self::count_good_numbers_spec_inner(n) % Self::M as nat\n }\n\n pub fn count_good_numbers(n: i64) -> (res: i32)\n requires\n 1 <= n <= pow(10, 15),\n ensures\n res == Self::count_good_numbers_spec(n as nat) as i32,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub const M: u64 = 1_000_000_007;\n \n fn mod_pow(base: u64, exp: u64, modulus: u64) -> u64 {\n if modulus == 1 {\n return 0\n }\n let mut result = 1;\n let mut base_pow = base % modulus;\n let mut i: u64 = 0;\n let mut mut_exp = exp;\n while mut_exp > 0 {\n if mut_exp % 2 != 0 {\n result = result * base_pow % modulus;\n }\n base_pow = base_pow * base_pow % modulus;\n mut_exp >>= 1;\n i += 1;\n }\n result\n }\n\n pub fn count_good_numbers(n: i64) -> i32 {\n ((Self::mod_pow(4 * 5, n as u64 / 2, Self::M) * if n % 2 == 1 {\n 5\n } else {\n 1\n }) % Self::M) as i32\n }\n}\n", "code_spec": "use vstd::arithmetic::power::pow;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub const M: u64 = 1_000_000_007;\n\n pub open spec fn count_good_numbers_spec_inner(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 1\n } else if n % 2 == 1 {\n 5 * Self::count_good_numbers_spec_inner((n - 1) as nat)\n } else {\n 4 * Self::count_good_numbers_spec_inner((n - 1) as nat)\n }\n }\n\n pub open spec fn count_good_numbers_spec(n: nat) -> nat {\n Self::count_good_numbers_spec_inner(n) % Self::M as nat\n }\n\n fn mod_pow(base: u64, exp: u64, modulus: u64) -> (res: u64)\n requires\n 0 < modulus <= u32::MAX + 1,\n ensures\n res == (pow(base as int, exp as nat) % modulus as int) as u64,\n {\n if modulus == 1 {\n return 0\n }\n let mut result = 1;\n let mut base_pow = base % modulus;\n let mut i: u64 = 0;\n let mut mut_exp = exp;\n while mut_exp > 0 {\n if mut_exp % 2 != 0 {\n result = result * base_pow % modulus;\n }\n base_pow = base_pow * base_pow % modulus;\n mut_exp >>= 1;\n i += 1;\n }\n result\n }\n\n pub fn count_good_numbers(n: i64) -> (res: i32)\n requires\n 1 <= n <= pow(10, 15),\n ensures\n res == Self::count_good_numbers_spec(n as nat) as i32,\n {\n ((Self::mod_pow(4 * 5, n as u64 / 2, Self::M) * if n % 2 == 1 {\n 5\n } else {\n 1\n }) % Self::M) as i32\n }\n}\n\n} \n", "verified": "use vstd::arithmetic::div_mod::{\n lemma_div_is_ordered, lemma_div_non_zero, lemma_fundamental_div_mod, lemma_mul_mod_noop,\n lemma_small_mod,\n};\nuse vstd::arithmetic::mul::lemma_mul_inequality;\nuse vstd::arithmetic::power::{\n lemma_pow0, lemma_pow1, lemma_pow_adds, lemma_pow_multiplies, lemma_square_is_pow2, pow,\n};\nuse vstd::arithmetic::power2::{\n lemma2_to64, lemma2_to64_rest, lemma_pow2, lemma_pow2_pos, lemma_pow2_unfold, pow2,\n};\nuse vstd::bits::{\n lemma_u64_low_bits_mask_is_mod, lemma_u64_shl_is_mul, lemma_u64_shr_is_div, low_bits_mask,\n};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub proof fn lemma_mul_is_zero(x: int, y: int)\n by (nonlinear_arith)\n requires\n x * y == 0,\n ensures\n x == 0 || y == 0,\n{\n}\n\npub proof fn lemma_div_is_zero(x: int, d: int)\n requires\n d > 0,\n ensures\n 0 <= x < d <==> x / d == 0,\n{\n lemma_fundamental_div_mod(x, d);\n if 0 <= x < d {\n lemma_small_mod(x as nat, d as nat);\n lemma_mul_is_zero(d, x / d);\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub const M: u64 = 1_000_000_007;\n\n pub open spec fn count_good_numbers_spec_inner(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 1\n } else if n % 2 == 1 {\n 5 * Self::count_good_numbers_spec_inner((n - 1) as nat)\n } else {\n 4 * Self::count_good_numbers_spec_inner((n - 1) as nat)\n }\n }\n\n pub open spec fn count_good_numbers_spec(n: nat) -> nat {\n Self::count_good_numbers_spec_inner(n) % Self::M as nat\n }\n\n pub proof fn lemma_count_good_numbers_pow(n: nat)\n ensures\n n % 2 == 0 ==> Self::count_good_numbers_spec_inner(n) == pow(20, n / 2),\n n % 2 == 1 ==> Self::count_good_numbers_spec_inner(n) == 5 * pow(\n 20,\n ((n - 1) / 2) as nat,\n ),\n decreases n,\n {\n lemma_pow0(20);\n reveal_with_fuel(Solution::count_good_numbers_spec_inner, 2);\n if n > 1 {\n Self::lemma_count_good_numbers_pow((n - 2) as nat);\n lemma_pow1(20);\n lemma_pow_adds(20, 1, ((n - 2) / 2) as nat);\n }\n }\n\n #[verifier::spinoff_prover]\n fn mod_pow(base: u64, exp: u64, modulus: u64) -> (res: u64)\n requires\n 0 < modulus <= u32::MAX + 1,\n ensures\n res == (pow(base as int, exp as nat) % modulus as int) as u64,\n {\n if modulus == 1 {\n return 0\n }\n let mut result = 1;\n let mut base_pow = base % modulus;\n let mut i: u64 = 0;\n let mut mut_exp = exp;\n proof {\n assert(mut_exp == exp >> i) by (bit_vector)\n requires\n i == 0,\n mut_exp == exp,\n ;\n lemma_u64_shr_is_div(exp, i);\n lemma2_to64();\n lemma_pow1(base as int);\n lemma_pow0(base as int);\n lemma_small_mod(1, modulus as nat);\n }\n while mut_exp > 0\n invariant\n 0 < modulus <= u32::MAX + 1,\n base_pow < modulus,\n 0 <= result < modulus,\n i < 64 ==> (mut_exp == exp >> i == exp as nat / pow2(i as nat)),\n i == 64 ==> mut_exp == 0,\n 0 <= i <= 64,\n result == pow(base as int, exp as nat % pow2(i as nat)) % modulus as int,\n base_pow == pow(base as int, pow2(i as nat)) % modulus as int,\n decreases mut_exp,\n {\n proof {\n assert(result == pow(base as int, exp as nat % pow2(i as nat)) % modulus as int);\n assert(exp & ((1u64 << i + 1) - 1) as u64 == (exp & (1u64 << i)) + (exp & (((1u64\n << i) - 1) as u64))) by (bit_vector);\n assert((mut_exp & 1) << i == exp & (1u64 << i)) by (bit_vector)\n requires\n mut_exp == exp >> i,\n ;\n lemma2_to64();\n lemma2_to64_rest();\n if i + 1 <= 63 {\n assert(pow2(i as nat + 1) < u64::MAX);\n lemma_u64_shl_is_mul(1, (i + 1) as u64);\n lemma_u64_low_bits_mask_is_mod(exp, i as nat + 1);\n } else {\n assert(exp & ((1u64 << i + 1) - 1) as u64 == exp) by (bit_vector)\n requires\n i + 1 == 64,\n ;\n lemma_small_mod(exp as nat, pow2(64));\n }\n lemma_u64_low_bits_mask_is_mod(mut_exp, 1);\n assert(mut_exp & 1 == mut_exp % 2);\n assert(pow2(i as nat) < u64::MAX);\n assert(mut_exp & 1 <= 1);\n lemma_u64_shl_is_mul(mut_exp & 1, i);\n lemma_u64_shl_is_mul(1, i);\n lemma_u64_low_bits_mask_is_mod(exp, i as nat);\n if mut_exp % 2 == 0 {\n } else {\n lemma_pow_adds(base as int, pow2(i as nat), exp as nat % pow2(i as nat));\n lemma_mul_mod_noop(\n pow(base as int, exp as nat % pow2(i as nat)),\n pow(base as int, pow2(i as nat)),\n modulus as int,\n );\n }\n }\n if mut_exp % 2 != 0 {\n proof {\n lemma_mul_inequality(base_pow as int, u32::MAX as int + 1, result as int);\n }\n result = result * base_pow % modulus;\n }\n proof {\n lemma_mul_inequality(base_pow as int, u32::MAX as int + 1, base_pow as int);\n lemma_u64_shr_is_div(mut_exp, 1);\n lemma_u64_shr_is_div(exp, i);\n if i == 63 {\n assert(u64::MAX >> i == 1) by (bit_vector)\n requires\n i == 63,\n ;\n lemma_u64_shr_is_div(u64::MAX, i);\n lemma_pow2_pos(i as nat);\n lemma_div_is_ordered(exp as int, u64::MAX as int, pow2(i as nat) as int);\n } else {\n assert(mut_exp >> 1 == exp >> (i + 1)) by (bit_vector)\n requires\n mut_exp == exp >> i,\n ;\n lemma_u64_shr_is_div(exp, (i + 1) as u64);\n }\n lemma_square_is_pow2(base_pow as int);\n lemma_mul_mod_noop(\n pow(base as int, pow2(i as nat)),\n pow(base as int, pow2(i as nat)),\n modulus as int,\n );\n lemma_pow_multiplies(base as int, pow2(i as nat), 2);\n lemma_square_is_pow2(pow(base as int, pow2(i as nat)));\n lemma_pow2_unfold(i as nat + 1);\n }\n base_pow = base_pow * base_pow % modulus;\n mut_exp >>= 1;\n i += 1;\n }\n proof {\n assert(result == pow(base as int, exp as nat % pow2(i as nat)) % modulus as int);\n if i == 64 {\n lemma2_to64();\n } else {\n lemma_pow2_pos(i as nat);\n lemma_div_is_zero(exp as int, pow2(i as nat) as int);\n }\n lemma_small_mod(exp as nat, pow2(i as nat));\n }\n result\n }\n\n pub fn count_good_numbers(n: i64) -> (res: i32)\n requires\n 1 <= n <= pow(10, 15),\n ensures\n res == Self::count_good_numbers_spec(n as nat) as i32,\n {\n proof {\n Self::lemma_count_good_numbers_pow(n as nat);\n }\n ((Self::mod_pow(4 * 5, n as u64 / 2, Self::M) * if n % 2 == 1 {\n 5\n } else {\n 1\n }) % Self::M) as i32\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc1922/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1922/tests/mutated_testcases.jsonl"} {"problem": "lc1925", "family": "leetcode", "description": "# Count Square Sum Triples\n\nA **square triple** `(a, b, c)` is a triple where `a`, `b`, and `c` are **integers** and `a^2 + b^2 = c^2`.\n\nGiven an integer `n`, return the number of **square triples** such that `1 <= a, b, c <= n`.\n\n## Example 1:\n\n> **Input:** n = 5\n> **Output:** 2\n> **Explanation:** The square triples are (3,4,5) and (4,3,5).\n\n## Example 2:\n\n> **Input:** n = 10\n> **Output:** 4\n> **Explanation:** The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).\n\n## Constraints:\n\n- `1 <= n <= 250`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_triples(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Enumeration\nAcceptance rate: 77.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_square_triple(a: int, b: int, c: int) -> bool {\n a * a + b * b == c * c\n}\n\npub open spec fn count_for_c(n: int, a: int, b: int, c: int) -> int\n decreases n - c + 1,\n{\n if c > n {\n 0\n } else {\n (if is_square_triple(a, b, c) { 1int } else { 0int }) + count_for_c(n, a, b, c + 1)\n }\n}\n\npub open spec fn count_for_b(n: int, a: int, b: int) -> int\n decreases n - b + 1,\n{\n if b > n {\n 0\n } else {\n count_for_c(n, a, b, 1) + count_for_b(n, a, b + 1)\n }\n}\n\npub open spec fn count_for_a(n: int, a: int) -> int\n decreases n - a + 1,\n{\n if a > n {\n 0\n } else {\n count_for_b(n, a, 1) + count_for_a(n, a + 1)\n }\n}\n\nimpl Solution {\n pub fn count_triples(n: i32) -> (result: i32)\n requires\n 1 <= n <= 250,\n ensures\n result == count_for_a(n as int, 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_triples(n: i32) -> i32\n {\n let max_sq: i32 = n * n;\n let mut is_sq: Vec = Vec::new();\n let mut idx: i32 = 0;\n while idx <= max_sq\n {\n is_sq.push(false);\n idx = idx + 1;\n }\n let mut c: i32 = 1;\n while c <= n\n {\n is_sq[(c * c) as usize] = true;\n c = c + 1;\n }\n let mut count: i32 = 0;\n let mut a: i32 = 1;\n while a <= n\n {\n let mut b: i32 = 1;\n while b <= n\n {\n let s: i32 = a * a + b * b;\n if s <= max_sq && is_sq[s as usize] {\n count = count + 1;\n }\n b = b + 1;\n }\n a = a + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_square_triple(a: int, b: int, c: int) -> bool {\n a * a + b * b == c * c\n}\n\npub open spec fn count_for_c(n: int, a: int, b: int, c: int) -> int\n decreases n - c + 1,\n{\n if c > n {\n 0\n } else {\n (if is_square_triple(a, b, c) { 1int } else { 0int }) + count_for_c(n, a, b, c + 1)\n }\n}\n\npub open spec fn count_for_b(n: int, a: int, b: int) -> int\n decreases n - b + 1,\n{\n if b > n {\n 0\n } else {\n count_for_c(n, a, b, 1) + count_for_b(n, a, b + 1)\n }\n}\n\npub open spec fn count_for_a(n: int, a: int) -> int\n decreases n - a + 1,\n{\n if a > n {\n 0\n } else {\n count_for_b(n, a, 1) + count_for_a(n, a + 1)\n }\n}\n\nimpl Solution {\n pub fn count_triples(n: i32) -> (result: i32)\n requires\n 1 <= n <= 250,\n ensures\n result == count_for_a(n as int, 1),\n {\n let max_sq: i32 = n * n;\n let mut is_sq: Vec = Vec::new();\n let mut idx: i32 = 0;\n while idx <= max_sq\n {\n is_sq.push(false);\n idx = idx + 1;\n }\n let mut c: i32 = 1;\n while c <= n\n {\n is_sq.set((c * c) as usize, true);\n c = c + 1;\n }\n let mut count: i32 = 0;\n let mut a: i32 = 1;\n while a <= n\n {\n let mut b: i32 = 1;\n while b <= n\n {\n let s: i32 = a * a + b * b;\n if s <= max_sq && is_sq[s as usize] {\n count = count + 1;\n }\n b = b + 1;\n }\n a = a + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_square_triple(a: int, b: int, c: int) -> bool {\n a * a + b * b == c * c\n}\n\npub open spec fn sq_val(c: int) -> int { c * c }\n\npub open spec fn count_for_c(n: int, a: int, b: int, c: int) -> int\n decreases n - c + 1,\n{\n if c > n {\n 0\n } else {\n (if is_square_triple(a, b, c) { 1int } else { 0int }) + count_for_c(n, a, b, c + 1)\n }\n}\n\npub open spec fn count_for_b(n: int, a: int, b: int) -> int\n decreases n - b + 1,\n{\n if b > n {\n 0\n } else {\n count_for_c(n, a, b, 1) + count_for_b(n, a, b + 1)\n }\n}\n\npub open spec fn count_for_a(n: int, a: int) -> int\n decreases n - a + 1,\n{\n if a > n {\n 0\n } else {\n count_for_b(n, a, 1) + count_for_a(n, a + 1)\n }\n}\n\nproof fn square_strict_mono(c1: int, c2: int)\n requires\n c1 >= 1,\n c2 > c1,\n ensures\n c2 * c2 > c1 * c1,\n{\n assert(c2 * c2 - c1 * c1 == (c2 - c1) * (c2 + c1)) by(nonlinear_arith);\n assert((c2 - c1) * (c2 + c1) > 0) by(nonlinear_arith)\n requires c2 - c1 > 0, c2 + c1 > 0;\n}\n\nproof fn count_for_c_zero_no_match(n: int, a: int, b: int, c: int, witness: int)\n requires\n n >= 1,\n c >= 1,\n witness >= 1,\n witness < c,\n is_square_triple(a, b, witness),\n ensures\n count_for_c(n, a, b, c) == 0,\n decreases n - c + 1,\n{\n if c > n {\n } else {\n square_strict_mono(witness, c);\n assert(!is_square_triple(a, b, c));\n count_for_c_zero_no_match(n, a, b, c + 1, witness);\n }\n}\n\nproof fn count_for_c_at_most_one(n: int, a: int, b: int, c: int)\n requires\n n >= 1,\n c >= 1,\n ensures\n count_for_c(n, a, b, c) <= 1,\n decreases n - c + 1,\n{\n if c > n {\n } else {\n count_for_c_at_most_one(n, a, b, c + 1);\n if is_square_triple(a, b, c) {\n count_for_c_zero_no_match(n, a, b, c + 1, c);\n } else {\n count_for_c_bound(n, a, b, c + 1);\n }\n }\n}\n\nproof fn count_for_c_has_match(n: int, a: int, b: int, c: int, target: int)\n requires\n n >= 1,\n 1 <= c <= target,\n target <= n,\n is_square_triple(a, b, target),\n ensures\n count_for_c(n, a, b, c) >= 1,\n decreases target - c,\n{\n count_for_c_bound(n, a, b, c + 1);\n if c == target {\n } else {\n count_for_c_has_match(n, a, b, c + 1, target);\n }\n}\n\nproof fn count_for_c_no_match(n: int, a: int, b: int, c: int)\n requires\n n >= 1,\n c >= 1,\n forall|c_: int| c <= c_ <= n ==> !is_square_triple(a, b, c_),\n ensures\n count_for_c(n, a, b, c) == 0,\n decreases n - c + 1,\n{\n if c > n {\n } else {\n count_for_c_no_match(n, a, b, c + 1);\n }\n}\n\nproof fn count_for_c_with_is_sq(n: int, a: int, b: int, is_sq: Seq)\n requires\n n >= 1,\n a >= 1,\n b >= 1,\n a <= n,\n b <= n,\n is_sq.len() == n * n + 1,\n forall|v: int| 0 <= v <= n * n ==>\n (#[trigger] is_sq[v] == (exists|c_: int| 1 <= c_ <= n && #[trigger] sq_val(c_) == v)),\n ensures\n count_for_c(n, a, b, 1) == (\n if a * a + b * b <= n * n && is_sq[a * a + b * b] { 1int } else { 0int }\n ),\n{\n let s = a * a + b * b;\n assert(a * a >= 1) by(nonlinear_arith) requires a >= 1;\n assert(b * b >= 1) by(nonlinear_arith) requires b >= 1;\n\n if s <= n * n && is_sq[s] {\n let c_ = choose|c_: int| 1 <= c_ <= n && sq_val(c_) == s;\n count_for_c_has_match(n, a, b, 1, c_);\n count_for_c_at_most_one(n, a, b, 1);\n } else {\n if s <= n * n {\n assert(!is_sq[s]);\n assert(!(exists|c_: int| 1 <= c_ <= n && sq_val(c_) == s));\n assert forall|c_: int| 1 <= c_ <= n implies !is_square_triple(a, b, c_) by {\n if is_square_triple(a, b, c_) {\n assert(sq_val(c_) == s);\n assert(1 <= c_ <= n && sq_val(c_) == s);\n }\n }\n count_for_c_no_match(n, a, b, 1);\n } else {\n assert(s > n * n);\n assert forall|c_: int| 1 <= c_ <= n implies !is_square_triple(a, b, c_) by {\n if is_square_triple(a, b, c_) {\n assert(sq_val(c_) == s);\n assert(c_ * c_ <= n * n) by(nonlinear_arith) requires 1 <= c_ <= n;\n assert(false);\n }\n }\n count_for_c_no_match(n, a, b, 1);\n }\n }\n}\n\nproof fn count_for_c_bound(n: int, a: int, b: int, c: int)\n requires\n n >= 1,\n ensures\n count_for_c(n, a, b, c) >= 0,\n 1 <= c <= n + 1 ==> count_for_c(n, a, b, c) <= n - c + 1,\n decreases n - c + 1,\n{\n if c > n {\n } else {\n count_for_c_bound(n, a, b, c + 1);\n }\n}\n\nproof fn count_for_b_bound(n: int, a: int, b: int)\n requires\n n >= 1,\n ensures\n count_for_b(n, a, b) >= 0,\n 1 <= b <= n + 1 ==> count_for_b(n, a, b) <= n * (n - b + 1),\n decreases n - b + 1,\n{\n if b > n {\n } else {\n count_for_b_bound(n, a, b + 1);\n count_for_c_bound(n, a, b, 1);\n assert(n + n * (n - b) == n * (n - b + 1)) by (nonlinear_arith);\n }\n}\n\nproof fn count_for_a_bound(n: int, a: int)\n requires\n n >= 1,\n ensures\n count_for_a(n, a) >= 0,\n 1 <= a <= n + 1 ==> count_for_a(n, a) <= n * n * (n - a + 1),\n decreases n - a + 1,\n{\n if a > n {\n } else {\n count_for_a_bound(n, a + 1);\n count_for_b_bound(n, a, 1);\n assert(n * n + n * n * (n - a) == n * n * (n - a + 1)) by (nonlinear_arith);\n }\n}\n\nimpl Solution {\n pub fn count_triples(n: i32) -> (result: i32)\n requires\n 1 <= n <= 250,\n ensures\n result == count_for_a(n as int, 1),\n {\n proof {\n count_for_a_bound(n as int, 1);\n assert((n as int) * (n as int) * (n as int) <= 250 * 250 * 250) by (nonlinear_arith)\n requires 1 <= n <= 250;\n assert((n as int) * (n as int) <= 250 * 250) by(nonlinear_arith)\n requires 1 <= n <= 250;\n }\n\n let max_sq: i32 = n * n;\n\n let mut is_sq: Vec = Vec::new();\n let mut idx: i32 = 0;\n while idx <= max_sq\n invariant\n 1 <= n <= 250,\n max_sq == n * n,\n 0 <= max_sq <= 62500,\n 0 <= idx <= max_sq + 1,\n is_sq@.len() == idx as int,\n forall|v: int| 0 <= v < idx as int ==> #[trigger] is_sq@[v] == false,\n decreases max_sq - idx + 1,\n {\n is_sq.push(false);\n idx = idx + 1;\n }\n\n let mut c: i32 = 1;\n while c <= n\n invariant\n 1 <= n <= 250,\n max_sq == n * n,\n 0 <= max_sq <= 62500,\n 1 <= c <= n + 1,\n is_sq@.len() == max_sq as int + 1,\n forall|v: int| 0 <= v <= max_sq as int ==>\n (#[trigger] is_sq@[v] == (exists|c_: int| 1 <= c_ < c as int && #[trigger] sq_val(c_) == v)),\n decreases n - c + 1,\n {\n proof {\n assert((c as int) * (c as int) <= max_sq as int) by(nonlinear_arith)\n requires 1 <= c <= n, max_sq == (n as int) * (n as int);\n assert((c as int) * (c as int) >= 0) by(nonlinear_arith)\n requires c >= 1;\n }\n is_sq.set((c * c) as usize, true);\n proof {\n assert forall|v: int| 0 <= v <= max_sq as int implies\n #[trigger] is_sq@[v] == (exists|c_: int| 1 <= c_ < (c as int) + 1 && #[trigger] sq_val(c_) == v)\n by {\n if v == (c as int) * (c as int) {\n assert(is_sq@[v] == true);\n assert(sq_val(c as int) == v);\n } else {\n assert(is_sq@[v] == (exists|c_: int| 1 <= c_ < c as int && #[trigger] sq_val(c_) == v));\n assert((exists|c_: int| 1 <= c_ < c as int && #[trigger] sq_val(c_) == v)\n == (exists|c_: int| 1 <= c_ < (c as int) + 1 && #[trigger] sq_val(c_) == v)) by {\n if exists|c_: int| 1 <= c_ < (c as int) + 1 && #[trigger] sq_val(c_) == v {\n let c_ = choose|c_: int| 1 <= c_ < (c as int) + 1 && #[trigger] sq_val(c_) == v;\n if c_ == c as int {\n assert(c_ * c_ == (c as int) * (c as int));\n assert(v == (c as int) * (c as int));\n assert(false);\n }\n assert(c_ < c as int);\n }\n }\n }\n }\n }\n c = c + 1;\n }\n\n proof {\n assert forall|v: int| 0 <= v <= max_sq as int implies\n #[trigger] is_sq@[v] == (exists|c_: int| 1 <= c_ <= n as int && #[trigger] sq_val(c_) == v)\n by {\n assert(is_sq@[v] == (exists|c_: int| 1 <= c_ < (n as int + 1) && #[trigger] sq_val(c_) == v));\n }\n }\n\n let mut count: i32 = 0;\n let mut a: i32 = 1;\n while a <= n\n invariant\n 1 <= n <= 250,\n max_sq == n * n,\n 0 <= max_sq <= 62500,\n 1 <= a <= n + 1,\n is_sq@.len() == max_sq as int + 1,\n forall|v: int| 0 <= v <= max_sq as int ==>\n (#[trigger] is_sq@[v] == (exists|c_: int| 1 <= c_ <= n as int && #[trigger] sq_val(c_) == v)),\n count as int + count_for_a(n as int, a as int) == count_for_a(n as int, 1),\n count_for_a(n as int, a as int) >= 0,\n count_for_a(n as int, 1) <= 250 * 250 * 250,\n 0 <= count,\n decreases\n n - a + 1,\n {\n proof {\n count_for_a_bound(n as int, (a + 1) as int);\n count_for_b_bound(n as int, a as int, 1);\n }\n let mut b: i32 = 1;\n while b <= n\n invariant\n 1 <= n <= 250,\n max_sq == n * n,\n 0 <= max_sq <= 62500,\n 1 <= a <= n,\n 1 <= b <= n + 1,\n is_sq@.len() == max_sq as int + 1,\n forall|v: int| 0 <= v <= max_sq as int ==>\n (#[trigger] is_sq@[v] == (exists|c_: int| 1 <= c_ <= n as int && #[trigger] sq_val(c_) == v)),\n count as int + count_for_b(n as int, a as int, b as int)\n + count_for_a(n as int, (a + 1) as int)\n == count_for_a(n as int, 1),\n count_for_b(n as int, a as int, b as int) >= 0,\n count_for_a(n as int, (a + 1) as int) >= 0,\n count_for_a(n as int, 1) <= 250 * 250 * 250,\n 0 <= count,\n decreases\n n - b + 1,\n {\n proof {\n count_for_b_bound(n as int, a as int, (b + 1) as int);\n count_for_c_bound(n as int, a as int, b as int, 1);\n assert((a as int) * (a as int) <= 250 * 250) by (nonlinear_arith)\n requires 1 <= a <= 250;\n assert((b as int) * (b as int) <= 250 * 250) by (nonlinear_arith)\n requires 1 <= b <= 250;\n }\n let s: i32 = a * a + b * b;\n proof {\n count_for_c_with_is_sq(n as int, a as int, b as int, is_sq@);\n }\n if s <= max_sq && is_sq[s as usize] {\n count = count + 1;\n }\n b = b + 1;\n }\n a = a + 1;\n }\n count\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1925/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1925/tests/mutated_testcases.jsonl"} {"problem": "lc1952", "family": "leetcode", "description": "# Three Divisors\n\nGiven an integer `n`, return `true` if `n` has **exactly three positive divisors**. Otherwise, return `false`.\n\nAn integer `m` is a **divisor** of `n` if there exists an integer `k` such that `n = k * m`.\n\n## Example 1:\n\n> **Input:** n = 2\n> **Output:** false\n> **Explanation:** 2 has only two divisors: 1 and 2.\n\n## Example 2:\n\n> **Input:** n = 4\n> **Output:** true\n> **Explanation:** 4 has three divisors: 1, 2, and 4.\n\n## Constraints:\n\n- $1 <= n <= 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_three(n: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMath, Enumeration, Number Theory\nAcceptance rate: 64.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_divisors_up_to(n: int, k: int) -> int\n decreases k\n{\n if k <= 0 {\n 0\n } else {\n count_divisors_up_to(n, k - 1) + if n % k == 0 { 1int } else { 0int }\n }\n}\n\nimpl Solution {\n pub fn is_three(n: i32) -> (res: bool)\n requires\n 1 <= n <= 10_000,\n ensures\n res == (count_divisors_up_to(n as int, n as int) == 3),\n {\n\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_three(n: i32) -> bool {\n let mut count: i32 = 0;\n let mut i: i32 = 1;\n while i <= n {\n if n % i == 0 {\n count = count + 1;\n }\n i = i + 1;\n }\n count == 3\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_divisors_up_to(n: int, k: int) -> int\n decreases k\n{\n if k <= 0 {\n 0\n } else {\n count_divisors_up_to(n, k - 1) + if n % k == 0 { 1int } else { 0int }\n }\n}\n\nimpl Solution {\n pub fn is_three(n: i32) -> (res: bool)\n requires\n 1 <= n <= 10_000,\n ensures\n res == (count_divisors_up_to(n as int, n as int) == 3),\n {\n let mut count: i32 = 0;\n let mut i: i32 = 1;\n while i <= n {\n if n % i == 0 {\n count = count + 1;\n }\n i = i + 1;\n }\n count == 3\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_divisors_up_to(n: int, k: int) -> int\n decreases k\n{\n if k <= 0 {\n 0\n } else {\n count_divisors_up_to(n, k - 1) + if n % k == 0 { 1int } else { 0int }\n }\n}\n\nimpl Solution {\n pub fn is_three(n: i32) -> (res: bool)\n requires\n 1 <= n <= 10_000,\n ensures\n res == (count_divisors_up_to(n as int, n as int) == 3),\n {\n let mut count: i32 = 0;\n let mut i: i32 = 1;\n while i <= n\n invariant\n 1 <= n <= 10_000,\n 1 <= i <= n + 1,\n 0 <= count <= i - 1,\n count as int == count_divisors_up_to(n as int, (i - 1) as int),\n decreases n - i + 1,\n {\n if n % i == 0 {\n count = count + 1;\n }\n i = i + 1;\n }\n count == 3\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1952/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1952/tests/mutated_testcases.jsonl"} {"problem": "lc1979", "family": "leetcode", "description": "# Find Greatest Common Divisor of Array\n\nGiven an integer array `nums`, return the **greatest common divisor** of the smallest number and largest number in `nums`.\n\nThe **greatest common divisor** of two numbers is the largest positive integer that evenly divides both numbers.\n\n## Example 1:\n\n> **Input:** nums = [2,5,6,9,10]\n> **Output:** 2\n> **Explanation:**\n> The smallest number in nums is 2.\n> The largest number in nums is 10.\n> The greatest common divisor of 2 and 10 is 2.\n\n## Example 2:\n\n> **Input:** nums = [7,5,6,8,3]\n> **Output:** 1\n> **Explanation:**\n> The smallest number in nums is 3.\n> The largest number in nums is 8.\n> The greatest common divisor of 3 and 8 is 1.\n\n## Example 3:\n\n> **Input:** nums = [3,3]\n> **Output:** 3\n> **Explanation:**\n> The smallest number in nums is 3.\n> The largest number in nums is 3.\n> The greatest common divisor of 3 and 3 is 3.\n\n## Constraints:\n\n- `2 <= nums.length <= 1000`\n- `1 <= nums[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_gcd(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Math, Number Theory\nAcceptance rate: 79.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_spec(s: Seq, hi: int) -> int\n decreases hi\n {\n if hi <= 1 { s[0] as int }\n else {\n let rest = Self::min_spec(s, hi - 1);\n if (s[hi - 1] as int) < rest { s[hi - 1] as int } else { rest }\n }\n }\n\n pub open spec fn max_spec(s: Seq, hi: int) -> int\n decreases hi\n {\n if hi <= 1 { s[0] as int }\n else {\n let rest = Self::max_spec(s, hi - 1);\n if (s[hi - 1] as int) > rest { s[hi - 1] as int } else { rest }\n }\n }\n\n pub open spec fn divides(d: int, n: int) -> bool\n recommends d > 0\n {\n n % d == 0\n }\n\n pub fn find_gcd(nums: Vec) -> (res: i32)\n requires\n 2 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n res >= 1,\n Self::divides(res as int, Self::min_spec(nums@, nums.len() as int)),\n Self::divides(res as int, Self::max_spec(nums@, nums.len() as int)),\n forall |d: int| 1 <= d <= 1000\n && Self::divides(d, Self::min_spec(nums@, nums.len() as int))\n && Self::divides(d, Self::max_spec(nums@, nums.len() as int))\n ==> d <= res as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_gcd(nums: Vec) -> i32 {\n let n = nums.len();\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n while i < n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i += 1;\n }\n let mut a = max_v;\n let mut b = min_v;\n while b > 0 {\n let temp = b;\n b = a % b;\n a = temp;\n }\n a\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_spec(s: Seq, hi: int) -> int\n decreases hi\n {\n if hi <= 1 { s[0] as int }\n else {\n let rest = Self::min_spec(s, hi - 1);\n if (s[hi - 1] as int) < rest { s[hi - 1] as int } else { rest }\n }\n }\n\n pub open spec fn max_spec(s: Seq, hi: int) -> int\n decreases hi\n {\n if hi <= 1 { s[0] as int }\n else {\n let rest = Self::max_spec(s, hi - 1);\n if (s[hi - 1] as int) > rest { s[hi - 1] as int } else { rest }\n }\n }\n\n pub open spec fn divides(d: int, n: int) -> bool\n recommends d > 0\n {\n n % d == 0\n }\n\n pub fn find_gcd(nums: Vec) -> (res: i32)\n requires\n 2 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n res >= 1,\n Self::divides(res as int, Self::min_spec(nums@, nums.len() as int)),\n Self::divides(res as int, Self::max_spec(nums@, nums.len() as int)),\n forall |d: int| 1 <= d <= 1000\n && Self::divides(d, Self::min_spec(nums@, nums.len() as int))\n && Self::divides(d, Self::max_spec(nums@, nums.len() as int))\n ==> d <= res as int,\n {\n let n = nums.len();\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n while i < n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i += 1;\n }\n let mut a = max_v;\n let mut b = min_v;\n while b > 0 {\n let temp = b;\n b = a % b;\n a = temp;\n }\n a\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_spec(s: Seq, hi: int) -> int\n decreases hi\n {\n if hi <= 1 { s[0] as int }\n else {\n let rest = Self::min_spec(s, hi - 1);\n if (s[hi - 1] as int) < rest { s[hi - 1] as int } else { rest }\n }\n }\n\n pub open spec fn max_spec(s: Seq, hi: int) -> int\n decreases hi\n {\n if hi <= 1 { s[0] as int }\n else {\n let rest = Self::max_spec(s, hi - 1);\n if (s[hi - 1] as int) > rest { s[hi - 1] as int } else { rest }\n }\n }\n\n pub open spec fn divides(d: int, n: int) -> bool\n recommends d > 0\n {\n n % d == 0\n }\n\n \n pub open spec fn gcd_rec(a: int, b: int) -> int\n decreases b\n {\n if b <= 0 { a }\n else { Self::gcd_rec(b, a % b) }\n }\n\n proof fn lemma_gcd_rec_positive(a: int, b: int)\n requires a >= 1, b >= 0,\n ensures Self::gcd_rec(a, b) >= 1,\n decreases b\n {\n if b > 0 {\n Self::lemma_gcd_rec_positive(b, a % b);\n }\n }\n\n proof fn lemma_gcd_rec_divides(a: int, b: int)\n requires a >= 1, b >= 0,\n ensures\n Self::divides(Self::gcd_rec(a, b), a),\n Self::divides(Self::gcd_rec(a, b), b),\n decreases b\n {\n if b <= 0 {\n assert(a % a == 0) by (nonlinear_arith) requires a >= 1;\n assert(0int % a == 0) by (nonlinear_arith) requires a >= 1;\n } else {\n Self::lemma_gcd_rec_divides(b, a % b);\n Self::lemma_gcd_rec_positive(b, a % b);\n let g = Self::gcd_rec(a, b);\n assert(b % g == 0);\n assert((a % b) % g == 0);\n \n \n let qb = b / g;\n let qr = (a % b) / g;\n let q = a / b;\n assert(b == g * qb) by (nonlinear_arith)\n requires g >= 1, b % g == 0, qb == b / g;\n assert(a % b == g * qr) by (nonlinear_arith)\n requires g >= 1, (a % b) % g == 0, qr == (a % b) / g;\n assert(a == q * b + a % b) by (nonlinear_arith)\n requires b >= 1, q == a / b;\n assert(a == g * (q * qb + qr)) by (nonlinear_arith)\n requires a == q * b + a % b, b == g * qb, a % b == g * qr;\n assert(a % g == 0) by (nonlinear_arith)\n requires g >= 1, a == g * (q * qb + qr);\n }\n }\n\n proof fn lemma_gcd_rec_greatest(a: int, b: int, d: int)\n requires a >= 1, b >= 0, d >= 1, Self::divides(d, a), Self::divides(d, b),\n ensures d <= Self::gcd_rec(a, b),\n decreases b\n {\n if b <= 0 {\n assert(a % d == 0);\n let qa = a / d;\n assert(a == d * qa) by (nonlinear_arith)\n requires d >= 1, a % d == 0, qa == a / d;\n assert(d <= a) by (nonlinear_arith)\n requires d >= 1, a >= 1, a == d * qa;\n } else {\n assert(a % d == 0);\n assert(b % d == 0);\n let qa = a / d;\n let qb = b / d;\n let q = a / b;\n assert(a == d * qa) by (nonlinear_arith)\n requires d >= 1, a % d == 0, qa == a / d;\n assert(b == d * qb) by (nonlinear_arith)\n requires d >= 1, b % d == 0, qb == b / d;\n assert(a == q * b + a % b) by (nonlinear_arith)\n requires b >= 1, q == a / b;\n assert(a % b == d * (qa - q * qb)) by (nonlinear_arith)\n requires a == q * b + a % b, a == d * qa, b == d * qb;\n assert((a % b) % d == 0) by (nonlinear_arith)\n requires d >= 1, a % b == d * (qa - q * qb);\n assert(Self::divides(d, a % b));\n Self::lemma_gcd_rec_greatest(b, a % b, d);\n }\n }\n\n pub fn find_gcd(nums: Vec) -> (res: i32)\n requires\n 2 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n res >= 1,\n Self::divides(res as int, Self::min_spec(nums@, nums.len() as int)),\n Self::divides(res as int, Self::max_spec(nums@, nums.len() as int)),\n forall |d: int| 1 <= d <= 1000\n && Self::divides(d, Self::min_spec(nums@, nums.len() as int))\n && Self::divides(d, Self::max_spec(nums@, nums.len() as int))\n ==> d <= res as int,\n {\n let n = nums.len();\n let ghost s = nums@;\n let ghost n_int = n as int;\n\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n\n while i < n\n invariant\n 1 <= i <= n,\n n == nums.len(),\n s == nums@,\n n_int == n as int,\n 2 <= n <= 1000,\n forall |j: int| 0 <= j < n_int ==> 1 <= #[trigger] s[j] <= 1000,\n min_v as int == Self::min_spec(s, i as int),\n max_v as int == Self::max_spec(s, i as int),\n 1 <= min_v <= 1000,\n 1 <= max_v <= 1000,\n decreases n - i\n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i += 1;\n }\n\n let ghost the_min = min_v as int;\n let ghost the_max = max_v as int;\n\n proof {\n Self::lemma_gcd_rec_positive(max_v as int, min_v as int);\n }\n\n let mut a = max_v;\n let mut b = min_v;\n\n while b > 0\n invariant\n Self::gcd_rec(a as int, b as int) == Self::gcd_rec(the_max, the_min),\n a >= 1,\n b >= 0,\n a <= 1000,\n b <= 1000,\n the_min == min_v as int,\n the_max == max_v as int,\n decreases b as int\n {\n let temp = b;\n b = a % b;\n a = temp;\n }\n\n \n proof {\n \n assert(a as int == Self::gcd_rec(the_max, the_min));\n Self::lemma_gcd_rec_divides(the_max, the_min);\n Self::lemma_gcd_rec_positive(the_max, the_min);\n\n \n assert(Self::divides(a as int, the_max));\n assert(Self::divides(a as int, the_min));\n\n \n assert forall |d: int| d >= 1\n && Self::divides(d, the_min)\n && Self::divides(d, the_max)\n implies d <= a as int by {\n Self::lemma_gcd_rec_greatest(the_max, the_min, d);\n };\n }\n\n a\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc1979/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc1979/tests/mutated_testcases.jsonl"} {"problem": "lc2006", "family": "leetcode", "description": "# Count Number of Pairs With Absolute Difference K\n\nGiven an integer array `nums` and an integer `k`, return the number of pairs `(i, j)` where `i < j` such that `|nums[i] - nums[j]| == k`.\n\nThe value `|x|` is defined as:\n- `x` if `x >= 0`.\n- `-x` if `x < 0`.\n\n## Example 1:\n> **Input:** nums = [1,2,2,1], k = 1\n> **Output:** 4\n> **Explanation:**\n> The pairs with an absolute difference of 1 are:\n> - [**1**,**2**,2,1]\n> - [**1**,2,**2**,1]\n> - [1,**2**,2,**1**]\n> - [1,2,**2**,**1**]\n\n## Example 2:\n> **Input:** nums = [1,3], k = 3\n> **Output:** 0\n> **Explanation:**\n> There are no pairs with an absolute difference of 3.\n\n## Example 3:\n> **Input:** nums = [3,2,1,5,4], k = 2\n> **Output:** 3\n> **Explanation:**\n> The pairs with an absolute difference of 2 are:\n> - [**3**,2,**1**,5,4]\n> - [**3**,2,1,**5**,4]\n> - [3,**2**,1,5,**4**]\n\n## Constraints:\n\n- $1 \\leq nums.length \\leq 200$\n\n- $1 \\leq nums[i] \\leq 100$\n\n- $1 \\leq k \\leq 99$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_k_difference(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Counting\nAcceptance rate: 82.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn abs_diff_is_k(a: i32, b: i32, k: i32) -> bool {\n a - b == k || b - a == k\n }\n\n pub open spec fn count_pairs_from(nums: Seq, k: i32, i: int, j: int) -> int\n decreases nums.len() - i, nums.len() - j\n {\n if i >= nums.len() {\n 0\n } else if j >= nums.len() {\n Self::count_pairs_from(nums, k, i + 1, i + 2)\n } else {\n (if Self::abs_diff_is_k(nums[i], nums[j], k) { 1int } else { 0int })\n + Self::count_pairs_from(nums, k, i, j + 1)\n }\n }\n\n pub fn count_k_difference(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 200,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 99,\n ensures\n result == Self::count_pairs_from(nums@, k, 0, 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_k_difference(nums: Vec, k: i32) -> i32 {\n let n = nums.len();\n let mut count: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = i + 1;\n while j < n {\n let diff = nums[i] - nums[j];\n if diff == k || diff == -k {\n count = count + 1;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn abs_diff_is_k(a: i32, b: i32, k: i32) -> bool {\n a - b == k || b - a == k\n }\n\n pub open spec fn count_pairs_from(nums: Seq, k: i32, i: int, j: int) -> int\n decreases nums.len() - i, nums.len() - j\n {\n if i >= nums.len() {\n 0\n } else if j >= nums.len() {\n Self::count_pairs_from(nums, k, i + 1, i + 2)\n } else {\n (if Self::abs_diff_is_k(nums[i], nums[j], k) { 1int } else { 0int })\n + Self::count_pairs_from(nums, k, i, j + 1)\n }\n }\n\n pub fn count_k_difference(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 200,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 99,\n ensures\n result == Self::count_pairs_from(nums@, k, 0, 1),\n {\n let n = nums.len();\n let mut count: i32 = 0;\n let mut i: usize = 0;\n while i < n\n {\n let mut j: usize = i + 1;\n while j < n\n {\n let diff = nums[i] - nums[j];\n if diff == k || diff == -k {\n count = count + 1;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn abs_diff_is_k(a: i32, b: i32, k: i32) -> bool {\n a - b == k || b - a == k\n }\n\n pub open spec fn count_pairs_from(nums: Seq, k: i32, i: int, j: int) -> int\n decreases nums.len() - i, nums.len() - j\n {\n if i >= nums.len() {\n 0\n } else if j >= nums.len() {\n Self::count_pairs_from(nums, k, i + 1, i + 2)\n } else {\n (if Self::abs_diff_is_k(nums[i], nums[j], k) { 1int } else { 0int })\n + Self::count_pairs_from(nums, k, i, j + 1)\n }\n }\n\n proof fn count_pairs_from_nonneg(nums: Seq, k: i32, i: int, j: int)\n ensures\n Self::count_pairs_from(nums, k, i, j) >= 0,\n decreases nums.len() - i, nums.len() - j\n {\n if i >= nums.len() {\n } else if j >= nums.len() {\n Self::count_pairs_from_nonneg(nums, k, i + 1, i + 2);\n } else {\n Self::count_pairs_from_nonneg(nums, k, i, j + 1);\n }\n }\n\n proof fn count_pairs_from_bound(nums: Seq, k: i32, i: int, j: int)\n requires\n 0 <= i < nums.len(),\n i + 1 <= j <= nums.len(),\n nums.len() <= 200,\n ensures\n Self::count_pairs_from(nums, k, i, j) <= (nums.len() - i) * nums.len() - (j - i - 1),\n decreases nums.len() - i, nums.len() - j,\n {\n let n = nums.len();\n if j >= n {\n if i + 1 < n {\n Self::count_pairs_from_bound(nums, k, i + 1, i + 2);\n \n \n \n assert((n - i - 1) * n <= (n - i) * n - (n - i - 1)) by(nonlinear_arith)\n requires n >= 1, 0 <= i, i + 1 < n;\n } else {\n \n assert(Self::count_pairs_from(nums, k, i + 1, i + 2) == 0int);\n }\n } else {\n Self::count_pairs_from_bound(nums, k, i, j + 1);\n \n \n \n Self::count_pairs_from_nonneg(nums, k, i, j + 1);\n assert(Self::count_pairs_from(nums, k, i, j) <=\n 1 + Self::count_pairs_from(nums, k, i, j + 1));\n }\n }\n\n pub fn count_k_difference(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 200,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 99,\n ensures\n result == Self::count_pairs_from(nums@, k, 0, 1),\n {\n let n = nums.len();\n let mut count: i32 = 0;\n let mut i: usize = 0;\n\n proof {\n Self::count_pairs_from_nonneg(nums@, k, 0int, 1int);\n Self::count_pairs_from_bound(nums@, k, 0int, 1int);\n let sn = nums@.len();\n assert(sn * sn <= 40000) by(nonlinear_arith)\n requires sn <= 200;\n }\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 1 <= nums.len() <= 200,\n forall |idx: int| 0 <= idx < nums.len() ==> 1 <= #[trigger] nums[idx] <= 100,\n 1 <= k <= 99,\n 0 <= count <= 40000,\n count == Self::count_pairs_from(nums@, k, 0, 1) - Self::count_pairs_from(nums@, k, i as int, (i + 1) as int),\n 0 <= Self::count_pairs_from(nums@, k, 0, 1) <= 40000,\n decreases n - i,\n {\n let mut j: usize = i + 1;\n\n proof {\n Self::count_pairs_from_nonneg(nums@, k, i as int, (i + 1) as int);\n }\n\n while j < n\n invariant\n 0 <= i < n,\n i + 1 <= j <= n,\n n == nums.len(),\n 1 <= nums.len() <= 200,\n forall |idx: int| 0 <= idx < nums.len() ==> 1 <= #[trigger] nums[idx] <= 100,\n 1 <= k <= 99,\n 0 <= count <= 40000,\n count == Self::count_pairs_from(nums@, k, 0, 1) - Self::count_pairs_from(nums@, k, i as int, j as int),\n 0 <= Self::count_pairs_from(nums@, k, 0, 1) <= 40000,\n Self::count_pairs_from(nums@, k, i as int, j as int) >= 0,\n decreases n - j,\n {\n proof {\n Self::count_pairs_from_nonneg(nums@, k, i as int, (j + 1) as int);\n }\n let diff = nums[i] - nums[j];\n if diff == k || diff == -k {\n count = count + 1;\n }\n j = j + 1;\n }\n\n proof {\n Self::count_pairs_from_nonneg(nums@, k, (i + 1) as int, (i + 2) as int);\n }\n\n i = i + 1;\n }\n count\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2006/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2006/tests/mutated_testcases.jsonl"} {"problem": "lc2073", "family": "leetcode", "description": "# Time Needed to Buy Tickets\n\nThere are `n` people in a line queuing to buy tickets, where the `0^th` person is at the **front** of the line and the `(n - 1)^th` person is at the **back** of the line.\n\nYou are given a **0-indexed** integer array `tickets` of length `n` where the number of tickets that the `i^th` person would like to buy is `tickets[i]`.\n\nEach person takes **exactly 1 second** to buy a ticket. A person can only buy **1 ticket at a time** and has to go back to **the end** of the line (which happens **instantaneously**) in order to buy more tickets. If a person does not have any tickets left to buy, the person will **leave **the line.\n\nReturn the **time taken** for the person **initially** at position **k**** **(0-indexed) to finish buying tickets.\n\n \n\nExample 1:\n\n**Input:** tickets = [2,3,2], k = 2\n\n**Output:** 6\n\n**Explanation:**\n\n\t- The queue starts as [2,3,2], where the kth person is underlined.\n\n\t- After the person at the front has bought a ticket, the queue becomes [3,2,1] at 1 second.\n\n\t- Continuing this process, the queue becomes [2,1,2] at 2 seconds.\n\n\t- Continuing this process, the queue becomes [1,2,1] at 3 seconds.\n\n\t- Continuing this process, the queue becomes [2,1] at 4 seconds. Note: the person at the front left the queue.\n\n\t- Continuing this process, the queue becomes [1,1] at 5 seconds.\n\n\t- Continuing this process, the queue becomes [1] at 6 seconds. The kth person has bought all their tickets, so return 6.\n\nExample 2:\n\n**Input:** tickets = [5,1,1,1], k = 0\n\n**Output:** 8\n\n**Explanation:**\n\n\t- The queue starts as [5,1,1,1], where the kth person is underlined.\n\n\t- After the person at the front has bought a ticket, the queue becomes [1,1,1,4] at 1 second.\n\n\t- Continuing this process for 3 seconds, the queue becomes [4] at 4 seconds.\n\n\t- Continuing this process for 4 seconds, the queue becomes [] at 8 seconds. The kth person has bought all their tickets, so return 8.\n\n \n\n**Constraints:**\n\n\t- `n == tickets.length`\n\n\t- `1 <= n <= 100`\n\n\t- `1 <= tickets[i] <= 100`\n\n\t- `0 <= k < n`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn time_required_to_buy(tickets: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Queue, Simulation\nAcceptance rate: 72.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn buy_for_index(tickets: Seq, k: int, i: int) -> int\n recommends\n 0 <= k < tickets.len(),\n 0 <= i < tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n {\n if i <= k {\n Self::min2(tickets[i] as int, tickets[k] as int)\n } else {\n Self::min2(tickets[i] as int, tickets[k] as int - 1)\n }\n }\n\n pub open spec fn total_upto(tickets: Seq, k: int, upto: int) -> int\n recommends\n 0 <= k < tickets.len(),\n 0 <= upto <= tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::total_upto(tickets, k, upto - 1) + Self::buy_for_index(tickets, k, upto - 1)\n }\n }\n\n pub fn time_required_to_buy(tickets: Vec, k: i32) -> (result: i32)\n requires\n 1 <= tickets.len() <= 100,\n 0 <= k < tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n ensures\n result as int == Self::total_upto(tickets@, k as int, tickets.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn time_required_to_buy(tickets: Vec, k: i32) -> i32 {\n let n = tickets.len();\n let kk = k as usize;\n let target = tickets[kk];\n let mut total = 0;\n let mut i: usize = 0;\n\n while i < n {\n let buy = if i <= kk { target } else { target - 1 };\n if tickets[i] < buy {\n total = total + tickets[i];\n } else {\n total = total + buy;\n }\n i = i + 1;\n }\n\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn buy_for_index(tickets: Seq, k: int, i: int) -> int\n recommends\n 0 <= k < tickets.len(),\n 0 <= i < tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n {\n if i <= k {\n Self::min2(tickets[i] as int, tickets[k] as int)\n } else {\n Self::min2(tickets[i] as int, tickets[k] as int - 1)\n }\n }\n\n pub open spec fn total_upto(tickets: Seq, k: int, upto: int) -> int\n recommends\n 0 <= k < tickets.len(),\n 0 <= upto <= tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::total_upto(tickets, k, upto - 1) + Self::buy_for_index(tickets, k, upto - 1)\n }\n }\n\n pub fn time_required_to_buy(tickets: Vec, k: i32) -> (result: i32)\n requires\n 1 <= tickets.len() <= 100,\n 0 <= k < tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n ensures\n result as int == Self::total_upto(tickets@, k as int, tickets.len() as int),\n {\n let n = tickets.len();\n let kk = k as usize;\n let target = tickets[kk];\n let mut total = 0;\n let mut i: usize = 0;\n\n while i < n {\n let buy = if i <= kk { target } else { target - 1 };\n if tickets[i] < buy {\n total = total + tickets[i];\n } else {\n total = total + buy;\n }\n i = i + 1;\n }\n\n total\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn buy_for_index(tickets: Seq, k: int, i: int) -> int\n recommends\n 0 <= k < tickets.len(),\n 0 <= i < tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n {\n if i <= k {\n Self::min2(tickets[i] as int, tickets[k] as int)\n } else {\n Self::min2(tickets[i] as int, tickets[k] as int - 1)\n }\n }\n\n pub open spec fn total_upto(tickets: Seq, k: int, upto: int) -> int\n recommends\n 0 <= k < tickets.len(),\n 0 <= upto <= tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::total_upto(tickets, k, upto - 1) + Self::buy_for_index(tickets, k, upto - 1)\n }\n }\n\n pub fn time_required_to_buy(tickets: Vec, k: i32) -> (result: i32)\n requires\n 1 <= tickets.len() <= 100,\n 0 <= k < tickets.len(),\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n ensures\n result as int == Self::total_upto(tickets@, k as int, tickets.len() as int),\n {\n let n = tickets.len();\n let kk = k as usize;\n let target = tickets[kk];\n let mut total = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n n == tickets.len(),\n 1 <= n <= 100,\n 0 <= kk < n,\n target == tickets@[kk as int],\n 0 <= i <= n,\n 1 <= target <= 100,\n forall |j: int| 0 <= j < tickets.len() ==> 1 <= #[trigger] tickets[j] <= 100,\n total as int == Self::total_upto(tickets@, kk as int, i as int),\n 0 <= total as int <= i as int * 100,\n decreases n - i,\n {\n let buy = if i <= kk { target } else { target - 1 };\n let ghost ti = tickets[i as int];\n if tickets[i] < buy {\n total = total + tickets[i];\n } else {\n total = total + buy;\n }\n proof {\n if i <= kk {\n if ti < buy {\n assert(Self::buy_for_index(tickets@, kk as int, i as int) == ti as int);\n } else {\n assert(Self::buy_for_index(tickets@, kk as int, i as int) == buy as int);\n }\n } else {\n if ti < buy {\n assert(Self::buy_for_index(tickets@, kk as int, i as int) == ti as int);\n } else {\n assert(Self::buy_for_index(tickets@, kk as int, i as int) == buy as int);\n }\n }\n }\n i = i + 1;\n }\n\n total\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2073/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2073/tests/mutated_testcases.jsonl"} {"problem": "lc2110", "family": "leetcode", "description": "# Number of Smooth Descent Periods of a Stock\n\nYou are given an integer array `prices` representing the daily price history of a stock, where `prices[i]` is the stock price on the `i`th day.\n\nA **smooth descent period** of a stock consists of **one or more contiguous** days such that the price on each day is **lower** than the price on the **preceding** day by **exactly** `1`. The first day of the period is exempted from this rule.\n\nReturn *the number of **smooth descent periods***.\n\n**Example 1:**\n\n**Input:** prices = [3,2,1,4]\n**Output:** 7\n**Explanation:** There are 7 smooth descent periods:\n[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]\nNote that a period with one day is a smooth descent period by the definition.\n\n**Example 2:**\n\n**Input:** prices = [8,6,7,7]\n**Output:** 4\n**Explanation:** There are 4 smooth descent periods: [8], [6], [7], and [7]\nNote that [8,6] is not a smooth descent period as 8 - 6 \u2260 1.\n\n**Example 3:**\n\n**Input:** prices = [1]\n**Output:** 1\n**Explanation:** There is 1 smooth descent period: [1]\n\n**Constraints:**\n\n- `1 <= prices.length <= 10^5`\n- `1 <= prices[i] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn get_descent_periods(prices: Vec) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nArray\nMath\nDynamic Programming\n67.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn run_length(prices: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if i >= prices.len() {\n 0\n } else if prices[i - 1] - prices[i] == 1 {\n run_length(prices, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn total_periods(prices: Seq, n: int) -> int\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n total_periods(prices, n - 1) + run_length(prices, n - 1)\n }\n}\n\nimpl Solution {\n pub fn get_descent_periods(prices: Vec) -> (total: i64)\n requires\n 1 <= prices.len() <= 100_000,\n forall |i: int| 0 <= i < prices.len() ==> 1 <= #[trigger] prices[i] <= 100_000,\n ensures\n total == total_periods(prices@, prices.len() as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn get_descent_periods(prices: Vec) -> i64 {\n let n = prices.len();\n let mut total: i64 = 0;\n let mut run_len: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n if i > 0 && prices[i - 1] - prices[i] == 1 {\n run_len += 1;\n } else {\n run_len = 1;\n }\n total += run_len;\n i += 1;\n }\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn run_length(prices: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if i >= prices.len() {\n 0\n } else if prices[i - 1] - prices[i] == 1 {\n run_length(prices, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn total_periods(prices: Seq, n: int) -> int\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n total_periods(prices, n - 1) + run_length(prices, n - 1)\n }\n}\n\nimpl Solution {\n pub fn get_descent_periods(prices: Vec) -> (total: i64)\n requires\n 1 <= prices.len() <= 100_000,\n forall |i: int| 0 <= i < prices.len() ==> 1 <= #[trigger] prices[i] <= 100_000,\n ensures\n total == total_periods(prices@, prices.len() as int),\n {\n let n = prices.len();\n let mut total: i64 = 0;\n let mut run_len: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n if i > 0 && prices[i - 1] - prices[i] == 1 {\n run_len += 1;\n } else {\n run_len = 1;\n }\n total += run_len;\n i += 1;\n }\n total\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn run_length(prices: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if i >= prices.len() {\n 0\n } else if prices[i - 1] - prices[i] == 1 {\n run_length(prices, i - 1) + 1\n } else {\n 1\n }\n}\n\npub open spec fn total_periods(prices: Seq, n: int) -> int\n decreases n,\n{\n if n <= 0 {\n 0\n } else {\n total_periods(prices, n - 1) + run_length(prices, n - 1)\n }\n}\n\nproof fn run_length_bounds(prices: Seq, i: int)\n requires\n 0 <= i < prices.len(),\n ensures\n 1 <= run_length(prices, i) <= i + 1,\n decreases i,\n{\n if i <= 0 {\n \n } else if prices[i - 1] - prices[i] == 1 {\n run_length_bounds(prices, i - 1);\n } else {\n \n }\n}\n\nproof fn total_periods_bounds(prices: Seq, n: int)\n requires\n 0 <= n <= prices.len(),\n n <= 100_000,\n ensures\n 0 <= total_periods(prices, n),\n total_periods(prices, n) <= n * (n + 1) / 2,\n decreases n,\n{\n if n <= 0 {\n \n } else {\n total_periods_bounds(prices, n - 1);\n run_length_bounds(prices, n - 1);\n assert(total_periods(prices, n) == total_periods(prices, n - 1) + run_length(prices, n - 1));\n assert(total_periods(prices, n) >= 0) by (nonlinear_arith)\n requires\n total_periods(prices, n - 1) >= 0,\n run_length(prices, n - 1) >= 1,\n total_periods(prices, n) == total_periods(prices, n - 1) + run_length(prices, n - 1),\n {}\n assert(total_periods(prices, n) <= n * (n + 1) / 2) by (nonlinear_arith)\n requires\n total_periods(prices, n - 1) <= (n - 1) * n / 2,\n run_length(prices, n - 1) <= n,\n total_periods(prices, n) == total_periods(prices, n - 1) + run_length(prices, n - 1),\n {}\n }\n}\n\nimpl Solution {\n pub fn get_descent_periods(prices: Vec) -> (total: i64)\n requires\n 1 <= prices.len() <= 100_000,\n forall |i: int| 0 <= i < prices.len() ==> 1 <= #[trigger] prices[i] <= 100_000,\n ensures\n total == total_periods(prices@, prices.len() as int),\n {\n let n = prices.len();\n let mut total: i64 = 0;\n let mut run_len: i64 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == prices.len(),\n 1 <= n <= 100_000,\n forall |k: int| 0 <= k < prices.len() ==> 1 <= #[trigger] prices[k] <= 100_000,\n 0 <= i <= n,\n i > 0 ==> run_len == run_length(prices@, (i - 1) as int),\n i == 0 ==> run_len == 0,\n i > 0 ==> 1 <= run_len <= i as i64,\n total == total_periods(prices@, i as int),\n 0 <= total <= i as i64 * (i as i64 + 1) / 2,\n decreases n - i,\n {\n if i > 0 && prices[i - 1] - prices[i] == 1 {\n run_len += 1;\n } else {\n run_len = 1;\n }\n\n proof {\n \n assert(run_len == run_length(prices@, i as int));\n run_length_bounds(prices@, i as int);\n }\n\n \n \n \n assert(total + run_len <= 5_000_050_001i64) by (nonlinear_arith)\n requires\n 0 <= total <= (i as i64) * (i as i64 + 1) / 2,\n 1 <= run_len <= i as i64 + 1,\n (i as i64) < 100_000i64,\n {}\n\n total += run_len;\n\n proof {\n \n assert(total_periods(prices@, (i + 1) as int) == \n total_periods(prices@, i as int) + run_length(prices@, i as int));\n assert(total == total_periods(prices@, (i + 1) as int));\n total_periods_bounds(prices@, (i + 1) as int);\n }\n\n i += 1;\n }\n total\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc2110/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2110/tests/mutated_testcases.jsonl"} {"problem": "lc213", "family": "leetcode", "description": "# House Robber II\n\nYou are a professional robber planning to rob houses along a street. All houses at this place are **arranged in a circle**. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatically contact the police if two adjacent houses were broken into on the same night**.\n\nGiven an integer array `nums` representing the amount of money of each house, return the maximum amount of money you can rob tonight **without alerting the police**.\n\n## Example 1:\n\n> **Input:** nums = [2,3,2]\n> **Output:** 3\n> **Explanation:** You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.\n\n## Example 2:\n\n> **Input:** nums = [1,2,3,1]\n> **Output:** 4\n> **Explanation:** Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.\n\n## Example 3:\n\n> **Input:** nums = [1,2,3]\n> **Output:** 3\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `0 <= nums[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn rob(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming\n42.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn rob_spec(nums: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi < nums.len(),\n decreases hi - lo,\n {\n if hi < lo {\n 0\n } else if hi == lo {\n nums[lo] as int\n } else if hi == lo + 1 {\n if nums[lo] as int > nums[hi] as int { nums[lo] as int } else { nums[hi] as int }\n } else {\n let skip = Self::rob_spec(nums, lo, hi - 1);\n let take = Self::rob_spec(nums, lo, hi - 2) + nums[hi] as int;\n if take > skip { take } else { skip }\n }\n }\n\n pub fn rob(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1000,\n ensures\n result == if nums@.len() == 1 {\n nums@[0] as int\n } else {\n let n = nums@.len() as int;\n let r1 = Self::rob_spec(nums@, 0, n - 2);\n let r2 = Self::rob_spec(nums@, 1, n - 1);\n if r1 > r2 { r1 } else { r2 }\n },\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn rob(nums: Vec) -> i32 {\n let n = nums.len();\n if n == 1 {\n return nums[0];\n }\n let hi1: usize = n - 2;\n let rob1: i32;\n if hi1 == 0 {\n rob1 = nums[0];\n } else {\n let mut a: i32 = nums[0];\n let mut b: i32 = if nums[0] > nums[1] { nums[0] } else { nums[1] };\n let mut i: usize = 2;\n while i <= hi1 {\n let c = if a + nums[i] > b { a + nums[i] } else { b };\n a = b;\n b = c;\n i = i + 1;\n }\n rob1 = b;\n }\n let hi2: usize = n - 1;\n let rob2: i32;\n if n == 2 {\n rob2 = nums[1];\n } else {\n let mut a: i32 = nums[1];\n let mut b: i32 = if nums[1] > nums[2] { nums[1] } else { nums[2] };\n let mut i: usize = 3;\n while i <= hi2 {\n let c = if a + nums[i] > b { a + nums[i] } else { b };\n a = b;\n b = c;\n i = i + 1;\n }\n rob2 = b;\n }\n if rob1 > rob2 { rob1 } else { rob2 }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn rob_spec(nums: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi < nums.len(),\n decreases hi - lo,\n {\n if hi < lo {\n 0\n } else if hi == lo {\n nums[lo] as int\n } else if hi == lo + 1 {\n if nums[lo] as int > nums[hi] as int { nums[lo] as int } else { nums[hi] as int }\n } else {\n let skip = Self::rob_spec(nums, lo, hi - 1);\n let take = Self::rob_spec(nums, lo, hi - 2) + nums[hi] as int;\n if take > skip { take } else { skip }\n }\n }\n\n pub fn rob(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1000,\n ensures\n result == if nums@.len() == 1 {\n nums@[0] as int\n } else {\n let n = nums@.len() as int;\n let r1 = Self::rob_spec(nums@, 0, n - 2);\n let r2 = Self::rob_spec(nums@, 1, n - 1);\n if r1 > r2 { r1 } else { r2 }\n },\n {\n let n = nums.len();\n if n == 1 {\n return nums[0];\n }\n let hi1: usize = n - 2;\n let rob1: i32;\n if hi1 == 0 {\n rob1 = nums[0];\n } else {\n let mut a: i32 = nums[0];\n let mut b: i32 = if nums[0] > nums[1] { nums[0] } else { nums[1] };\n let mut i: usize = 2;\n while i <= hi1 {\n let c = if a + nums[i] > b { a + nums[i] } else { b };\n a = b;\n b = c;\n i = i + 1;\n }\n rob1 = b;\n }\n let hi2: usize = n - 1;\n let rob2: i32;\n if n == 2 {\n rob2 = nums[1];\n } else {\n let mut a: i32 = nums[1];\n let mut b: i32 = if nums[1] > nums[2] { nums[1] } else { nums[2] };\n let mut i: usize = 3;\n while i <= hi2 {\n let c = if a + nums[i] > b { a + nums[i] } else { b };\n a = b;\n b = c;\n i = i + 1;\n }\n rob2 = b;\n }\n if rob1 > rob2 { rob1 } else { rob2 }\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn rob_spec(nums: Seq, lo: int, hi: int) -> int\n recommends\n 0 <= lo <= hi < nums.len(),\n decreases hi - lo,\n {\n if hi < lo {\n 0\n } else if hi == lo {\n nums[lo] as int\n } else if hi == lo + 1 {\n if nums[lo] as int > nums[hi] as int { nums[lo] as int } else { nums[hi] as int }\n } else {\n let skip = Self::rob_spec(nums, lo, hi - 1);\n let take = Self::rob_spec(nums, lo, hi - 2) + nums[hi] as int;\n if take > skip { take } else { skip }\n }\n }\n\n proof fn rob_bounds(nums: Seq, lo: int, hi: int)\n requires\n 0 <= lo <= hi < nums.len(),\n forall|j: int| 0 <= j < nums.len() ==> 0 <= #[trigger] nums[j] <= 1000,\n ensures\n 0 <= Self::rob_spec(nums, lo, hi) <= 1000 * (hi - lo + 1),\n decreases hi - lo,\n {\n if hi > lo + 1 {\n Self::rob_bounds(nums, lo, hi - 1);\n Self::rob_bounds(nums, lo, hi - 2);\n }\n }\n\n pub fn rob(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1000,\n ensures\n result == if nums@.len() == 1 {\n nums@[0] as int\n } else {\n let n = nums@.len() as int;\n let r1 = Self::rob_spec(nums@, 0, n - 2);\n let r2 = Self::rob_spec(nums@, 1, n - 1);\n if r1 > r2 { r1 } else { r2 }\n },\n {\n let n = nums.len();\n if n == 1 {\n return nums[0];\n }\n let hi1: usize = n - 2;\n let rob1: i32;\n if hi1 == 0 {\n rob1 = nums[0];\n } else {\n proof {\n Self::rob_bounds(nums@, 0, 0);\n Self::rob_bounds(nums@, 0, 1);\n }\n let mut a: i32 = nums[0];\n let mut b: i32 = if nums[0] > nums[1] { nums[0] } else { nums[1] };\n let mut i: usize = 2;\n while i <= hi1\n invariant\n 2 <= i <= hi1 + 1,\n hi1 == n - 2,\n n == nums.len(),\n 1 <= n <= 100,\n forall|j: int| 0 <= j < n ==> 0 <= #[trigger] nums@[j] <= 1000,\n a as int == Self::rob_spec(nums@, 0, (i - 2) as int),\n b as int == Self::rob_spec(nums@, 0, (i - 1) as int),\n 0 <= a <= 100000,\n 0 <= b <= 100000,\n decreases hi1 + 1 - i,\n {\n proof {\n Self::rob_bounds(nums@, 0, i as int);\n Self::rob_bounds(nums@, 0, (i - 1) as int);\n Self::rob_bounds(nums@, 0, (i - 2) as int);\n }\n let c = if a + nums[i] > b { a + nums[i] } else { b };\n a = b;\n b = c;\n i = i + 1;\n }\n rob1 = b;\n }\n let hi2: usize = n - 1;\n let rob2: i32;\n if n == 2 {\n rob2 = nums[1];\n } else {\n proof {\n Self::rob_bounds(nums@, 1, 1);\n Self::rob_bounds(nums@, 1, 2);\n }\n let mut a: i32 = nums[1];\n let mut b: i32 = if nums[1] > nums[2] { nums[1] } else { nums[2] };\n let mut i: usize = 3;\n while i <= hi2\n invariant\n 3 <= i <= hi2 + 1,\n hi2 == n - 1,\n n == nums.len(),\n 1 <= n <= 100,\n forall|j: int| 0 <= j < n ==> 0 <= #[trigger] nums@[j] <= 1000,\n a as int == Self::rob_spec(nums@, 1, (i - 2) as int),\n b as int == Self::rob_spec(nums@, 1, (i - 1) as int),\n 0 <= a <= 100000,\n 0 <= b <= 100000,\n decreases hi2 + 1 - i,\n {\n proof {\n Self::rob_bounds(nums@, 1, i as int);\n Self::rob_bounds(nums@, 1, (i - 1) as int);\n Self::rob_bounds(nums@, 1, (i - 2) as int);\n }\n let c = if a + nums[i] > b { a + nums[i] } else { b };\n a = b;\n b = c;\n i = i + 1;\n }\n rob2 = b;\n }\n if rob1 > rob2 { rob1 } else { rob2 }\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc213/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc213/tests/mutated_testcases.jsonl"} {"problem": "lc2139", "family": "leetcode", "description": "# Minimum Moves to Reach Target Score\n\nYou are playing a game with integers. You start with the integer `1` and you want to reach the integer `target`.\n\nIn one move, you can either:\n\n\t- **Increment** the current integer by one (i.e., `x = x + 1`).\n\n\t- **Double** the current integer (i.e., `x = 2 * x`).\n\nYou can use the **increment** operation **any** number of times, however, you can only use the **double** operation **at most** `maxDoubles` times.\n\nGiven the two integers `target` and `maxDoubles`, return *the minimum number of moves needed to reach *`target`* starting with *`1`.\n\n \n\nExample 1:\n\n```\n**Input:** target = 5, maxDoubles = 0\n**Output:** 4\n**Explanation:** Keep incrementing by 1 until you reach target.\n```\n\nExample 2:\n\n```\n**Input:** target = 19, maxDoubles = 2\n**Output:** 7\n**Explanation:** Initially, x = 1\nIncrement 3 times so x = 4\nDouble once so x = 8\nIncrement once so x = 9\nDouble again so x = 18\nIncrement once so x = 19\n```\n\nExample 3:\n\n```\n**Input:** target = 10, maxDoubles = 4\n**Output:** 4\n**Explanation:**** **Initially, x = 1\nIncrement once so x = 2\nDouble once so x = 4\nIncrement once so x = 5\nDouble again so x = 10\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= target <= 10^9`\n\n\t- `0 <= maxDoubles <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_moves(target: i32, max_doubles: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Greedy\nAcceptance rate: 52.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_moves_spec(target: int, max_doubles: int) -> int\n decreases target,\n {\n if target <= 1 {\n 0\n } else if max_doubles <= 0 {\n target - 1\n } else if target % 2 == 0 {\n 1 + Self::min_moves_spec(target / 2, max_doubles - 1)\n } else {\n 1 + Self::min_moves_spec(target - 1, max_doubles)\n }\n }\n\n pub fn min_moves(target: i32, max_doubles: i32) -> (result: i32)\n requires\n 1 <= target <= 1_000_000_000,\n 0 <= max_doubles <= 100,\n ensures\n result as int == Self::min_moves_spec(target as int, max_doubles as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_moves(target: i32, max_doubles: i32) -> i32 {\n let mut t = target;\n let mut k = max_doubles;\n let mut moves = 0;\n while t > 1 && k > 0 {\n if t % 2 == 0 {\n t = t / 2;\n k = k - 1;\n } else {\n t = t - 1;\n }\n moves = moves + 1;\n }\n moves + (t - 1)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_moves_spec(target: int, max_doubles: int) -> int\n decreases target,\n {\n if target <= 1 {\n 0\n } else if max_doubles <= 0 {\n target - 1\n } else if target % 2 == 0 {\n 1 + Self::min_moves_spec(target / 2, max_doubles - 1)\n } else {\n 1 + Self::min_moves_spec(target - 1, max_doubles)\n }\n }\n\n pub fn min_moves(target: i32, max_doubles: i32) -> (result: i32)\n requires\n 1 <= target <= 1_000_000_000,\n 0 <= max_doubles <= 100,\n ensures\n result as int == Self::min_moves_spec(target as int, max_doubles as int),\n {\n let mut t = target;\n let mut k = max_doubles;\n let mut moves = 0;\n while t > 1 && k > 0 {\n if t % 2 == 0 {\n t = t / 2;\n k = k - 1;\n } else {\n t = t - 1;\n }\n moves = moves + 1;\n }\n moves + (t - 1)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_moves_spec(target: int, max_doubles: int) -> int\n decreases target,\n {\n if target <= 1 {\n 0\n } else if max_doubles <= 0 {\n target - 1\n } else if target % 2 == 0 {\n 1 + Self::min_moves_spec(target / 2, max_doubles - 1)\n } else {\n 1 + Self::min_moves_spec(target - 1, max_doubles)\n }\n }\n\n pub fn min_moves(target: i32, max_doubles: i32) -> (result: i32)\n requires\n 1 <= target <= 1_000_000_000,\n 0 <= max_doubles <= 100,\n ensures\n result as int == Self::min_moves_spec(target as int, max_doubles as int),\n {\n let mut t = target;\n let mut k = max_doubles;\n let mut moves = 0;\n while t > 1 && k > 0\n invariant\n 1 <= t <= target,\n 0 <= k <= max_doubles,\n 0 <= moves,\n moves as int + t as int <= target as int + 1,\n moves as int + Self::min_moves_spec(t as int, k as int)\n == Self::min_moves_spec(target as int, max_doubles as int),\n decreases t,\n {\n if t % 2 == 0 {\n assert(Self::min_moves_spec(t as int, k as int)\n == 1 + Self::min_moves_spec((t / 2) as int, (k - 1) as int));\n t = t / 2;\n k = k - 1;\n } else {\n assert(Self::min_moves_spec(t as int, k as int)\n == 1 + Self::min_moves_spec((t - 1) as int, k as int));\n t = t - 1;\n }\n moves = moves + 1;\n }\n\n assert(t <= 1 || k <= 0);\n if t <= 1 {\n assert(Self::min_moves_spec(t as int, k as int) == 0);\n } else {\n assert(k <= 0);\n assert(Self::min_moves_spec(t as int, k as int) == t as int - 1);\n }\n assert(1 <= t);\n assert(moves as int + t as int - 1 <= target as int) by (nonlinear_arith)\n requires\n moves as int + t as int <= target as int + 1,\n 1 <= t,\n ;\n moves + (t - 1)\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2139/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2139/tests/mutated_testcases.jsonl"} {"problem": "lc2147", "family": "leetcode", "description": "# Number of Ways to Divide a Long Corridor\n\nAlong a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.\n\nOne room divider has already been installed to the left of index `0`, and another to the right of index `n - 1`. Additional room dividers can be installed. For each position between indices `i - 1` and `i` (`1 <= i <= n - 1`), at most one divider can be installed.\n\nDivide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.\n\nReturn the number of ways to divide the corridor. Since the answer may be very large, return it modulo $10^9 + 7$. If there is no way, return `0`.\n\n## Example 1:\n\n> **Input:** corridor = \"SSPPSPS\"\n> **Output:** 3\n> **Explanation:** There are 3 different ways to divide the corridor. Note that in each way, every section has exactly two seats.\n\n## Example 2:\n\n> **Input:** corridor = \"PPSPSP\"\n> **Output:** 1\n> **Explanation:** The only valid division uses no additional dividers.\n\n## Example 3:\n\n> **Input:** corridor = \"S\"\n> **Output:** 0\n\n## Constraints:\n\n- `n == corridor.length`\n- `1 <= n <= 10^5`\n- `corridor[i]` is either `'S'` or `'P'`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn number_of_ways(corridor: String) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nSenior Staff, Math, String, Dynamic Programming, Biweekly Contest 70\nAcceptance rate: 54.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seat_count_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0int\n } else {\n Self::seat_count_prefix(s, n - 1) + if s[n - 1] == 'S' { 1int } else { 0int }\n }\n }\n\n pub open spec fn plants_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0int\n } else {\n let prev_seats = Self::seat_count_prefix(s, n - 1);\n if s[n - 1] == 'P' && prev_seats >= 2 && prev_seats % 2 == 0 {\n Self::plants_prefix(s, n - 1) + 1\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn ways_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 1\n } else {\n let prev = Self::ways_prefix(s, n - 1);\n let prev_seats = Self::seat_count_prefix(s, n - 1);\n if s[n - 1] == 'S' && prev_seats >= 2 && prev_seats % 2 == 0 {\n (prev * (Self::plants_prefix(s, n - 1) + 1)) % 1_000_000_007\n } else {\n prev\n }\n }\n }\n\n pub open spec fn number_of_ways_spec(s: Seq) -> int {\n let seats = Self::seat_count_prefix(s, s.len() as int);\n if seats == 0 || seats % 2 == 1 {\n 0\n } else {\n Self::ways_prefix(s, s.len() as int)\n }\n }\n\n pub fn number_of_ways(corridor: String) -> (result: i32)\n requires\n 1 <= corridor@.len() <= 100_000,\n forall |i: int| 0 <= i < corridor@.len() ==> corridor@[i] == 'S' || corridor@[i] == 'P',\n ensures\n 0 <= result < 1_000_000_007,\n result as int == Self::number_of_ways_spec(corridor@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn number_of_ways(corridor: String) -> i32 {\n let mod_num: u128 = 1_000_000_007;\n let len = corridor.as_str().unicode_len();\n let mut seat_count: usize = 0;\n let mut plants: usize = 0;\n let mut ways: u128 = 1;\n let mut i: usize = 0;\n\n while i < len {\n let c = corridor.as_str().get_char(i);\n if c == 'S' {\n if seat_count >= 2 && seat_count % 2 == 0 {\n\t\t\t\t\tlet sep = plants + 1;\n\t\t\t\t\tlet prod = ways * sep as u128;\n\t\t\t\t\tlet new_ways = prod % mod_num;\n\t\t\t\t\tways = new_ways;\n }\n seat_count += 1;\n plants = 0;\n } else {\n if seat_count >= 2 && seat_count % 2 == 0 {\n plants += 1;\n }\n }\n i += 1;\n }\n\n if seat_count == 0 || seat_count % 2 == 1 {\n 0\n } else {\n ways as i32\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seat_count_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0int\n } else {\n Self::seat_count_prefix(s, n - 1) + if s[n - 1] == 'S' { 1int } else { 0int }\n }\n }\n\n pub open spec fn plants_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0int\n } else {\n let prev_seats = Self::seat_count_prefix(s, n - 1);\n if s[n - 1] == 'P' && prev_seats >= 2 && prev_seats % 2 == 0 {\n Self::plants_prefix(s, n - 1) + 1\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn ways_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 1\n } else {\n let prev = Self::ways_prefix(s, n - 1);\n let prev_seats = Self::seat_count_prefix(s, n - 1);\n if s[n - 1] == 'S' && prev_seats >= 2 && prev_seats % 2 == 0 {\n (prev * (Self::plants_prefix(s, n - 1) + 1)) % 1_000_000_007\n } else {\n prev\n }\n }\n }\n\n pub open spec fn number_of_ways_spec(s: Seq) -> int {\n let seats = Self::seat_count_prefix(s, s.len() as int);\n if seats == 0 || seats % 2 == 1 {\n 0\n } else {\n Self::ways_prefix(s, s.len() as int)\n }\n }\n\n pub fn number_of_ways(corridor: String) -> (result: i32)\n requires\n 1 <= corridor@.len() <= 100_000,\n forall |i: int| 0 <= i < corridor@.len() ==> corridor@[i] == 'S' || corridor@[i] == 'P',\n ensures\n 0 <= result < 1_000_000_007,\n result as int == Self::number_of_ways_spec(corridor@),\n {\n let mod_num: u128 = 1_000_000_007;\n let len = corridor.as_str().unicode_len();\n let mut seat_count: usize = 0;\n let mut plants: usize = 0;\n let mut ways: u128 = 1;\n let mut i: usize = 0;\n\n while i < len {\n let c = corridor.as_str().get_char(i);\n if c == 'S' {\n if seat_count >= 2 && seat_count % 2 == 0 {\n\t\t\t\t\tlet sep = plants + 1;\n\t\t\t\t\tlet prod = ways * sep as u128;\n\t\t\t\t\tlet new_ways = prod % mod_num;\n\t\t\t\t\tways = new_ways;\n }\n seat_count += 1;\n plants = 0;\n } else {\n if seat_count >= 2 && seat_count % 2 == 0 {\n plants += 1;\n }\n }\n i += 1;\n }\n\n if seat_count == 0 || seat_count % 2 == 1 {\n 0\n } else {\n ways as i32\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seat_count_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0int\n } else {\n Self::seat_count_prefix(s, n - 1) + if s[n - 1] == 'S' { 1int } else { 0int }\n }\n }\n\n pub open spec fn plants_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 0int\n } else {\n let prev_seats = Self::seat_count_prefix(s, n - 1);\n if s[n - 1] == 'P' && prev_seats >= 2 && prev_seats % 2 == 0 {\n Self::plants_prefix(s, n - 1) + 1\n } else {\n 0int\n }\n }\n }\n\n pub open spec fn ways_prefix(s: Seq, n: int) -> int\n decreases n\n {\n if n <= 0 {\n 1\n } else {\n let prev = Self::ways_prefix(s, n - 1);\n let prev_seats = Self::seat_count_prefix(s, n - 1);\n if s[n - 1] == 'S' && prev_seats >= 2 && prev_seats % 2 == 0 {\n (prev * (Self::plants_prefix(s, n - 1) + 1)) % 1_000_000_007\n } else {\n prev\n }\n }\n }\n\n pub open spec fn number_of_ways_spec(s: Seq) -> int {\n let seats = Self::seat_count_prefix(s, s.len() as int);\n if seats == 0 || seats % 2 == 1 {\n 0\n } else {\n Self::ways_prefix(s, s.len() as int)\n }\n }\n\n pub fn number_of_ways(corridor: String) -> (result: i32)\n requires\n 1 <= corridor@.len() <= 100_000,\n forall |i: int| 0 <= i < corridor@.len() ==> corridor@[i] == 'S' || corridor@[i] == 'P',\n ensures\n 0 <= result < 1_000_000_007,\n result as int == Self::number_of_ways_spec(corridor@),\n {\n let mod_num: u128 = 1_000_000_007;\n let len = corridor.as_str().unicode_len();\n let mut seat_count: usize = 0;\n let mut plants: usize = 0;\n let mut ways: u128 = 1;\n let mut i: usize = 0;\n\n while i < len\n invariant\n mod_num == 1_000_000_007u128,\n len == corridor@.len(),\n 0 <= i <= len,\n 1 <= len <= 100_000,\n forall |k: int| 0 <= k < corridor@.len() ==> corridor@[k] == 'S' || corridor@[k] == 'P',\n seat_count as int == Self::seat_count_prefix(corridor@, i as int),\n 0 <= seat_count as int <= i as int,\n plants as int == Self::plants_prefix(corridor@, i as int),\n 0 <= plants as int <= i as int,\n 0 <= ways < mod_num,\n ways as int == Self::ways_prefix(corridor@, i as int),\n decreases len - i,\n {\n let c = corridor.as_str().get_char(i);\n proof {\n assert(corridor@[i as int] == c);\n }\n if c == 'S' {\n if seat_count >= 2 && seat_count % 2 == 0 {\n let sep = plants + 1;\n proof {\n assert(sep == plants + 1);\n assert(1 <= sep) by (nonlinear_arith)\n requires\n sep == plants + 1,\n {};\n assert(sep <= len) by (nonlinear_arith)\n requires\n sep == plants + 1,\n plants as int <= i as int,\n i < len,\n {};\n assert(ways * (sep as u128) < 340_282_366_920_938_463_463_374_607_431_768_211_455u128) by (nonlinear_arith)\n requires\n 0 <= ways < mod_num,\n 1 <= sep,\n sep <= len,\n len <= 100_000,\n mod_num == 1_000_000_007u128,\n {};\n }\n let prod = ways * sep as u128;\n let new_ways = prod % mod_num;\n proof {\n assert(corridor@[i as int] == 'S');\n assert(sep == plants + 1);\n assert(sep as int == plants as int + 1) by (nonlinear_arith)\n requires\n sep == plants + 1,\n {};\n assert(sep as int == Self::plants_prefix(corridor@, i as int) + 1) by (nonlinear_arith)\n requires\n sep as int == plants as int + 1,\n plants as int == Self::plants_prefix(corridor@, i as int),\n {};\n assert(new_ways as int\n == ((ways as int) * (sep as int)) % 1_000_000_007int) by (nonlinear_arith)\n requires\n new_ways == prod % mod_num,\n prod == ways * (sep as u128),\n 0 <= ways < mod_num,\n 1 <= sep,\n sep <= len,\n len <= 100_000,\n mod_num == 1_000_000_007u128,\n {};\n assert(Self::ways_prefix(corridor@, i as int + 1)\n == (Self::ways_prefix(corridor@, i as int)\n * (Self::plants_prefix(corridor@, i as int) + 1)) % 1_000_000_007);\n assert(new_ways as int\n == (Self::ways_prefix(corridor@, i as int)\n * (Self::plants_prefix(corridor@, i as int) + 1)) % 1_000_000_007int) by (nonlinear_arith)\n requires\n 0 <= ways < mod_num,\n new_ways as int == (prod as int) % 1_000_000_007int,\n prod as int == (ways as int) * (sep as int),\n ways as int == Self::ways_prefix(corridor@, i as int),\n sep as int == Self::plants_prefix(corridor@, i as int) + 1,\n mod_num == 1_000_000_007u128,\n {};\n }\n ways = new_ways;\n }\n proof {\n assert(seat_count < 100_000usize) by (nonlinear_arith)\n requires\n seat_count as int <= i as int,\n i < len,\n len <= 100_000,\n {};\n assert(Self::seat_count_prefix(corridor@, i as int + 1) == Self::seat_count_prefix(corridor@, i as int) + 1);\n assert(Self::plants_prefix(corridor@, i as int + 1) == 0);\n if !(seat_count >= 2 && seat_count % 2 == 0) {\n assert(Self::ways_prefix(corridor@, i as int + 1) == Self::ways_prefix(corridor@, i as int));\n }\n }\n seat_count += 1;\n plants = 0;\n } else {\n if seat_count >= 2 && seat_count % 2 == 0 {\n proof {\n assert(corridor@[i as int] == 'P');\n assert(Self::seat_count_prefix(corridor@, i as int + 1) == Self::seat_count_prefix(corridor@, i as int));\n assert(Self::plants_prefix(corridor@, i as int + 1) == Self::plants_prefix(corridor@, i as int) + 1);\n assert(Self::ways_prefix(corridor@, i as int + 1) == Self::ways_prefix(corridor@, i as int));\n }\n plants += 1;\n } else {\n proof {\n assert(corridor@[i as int] == 'P');\n assert(Self::seat_count_prefix(corridor@, i as int + 1) == Self::seat_count_prefix(corridor@, i as int));\n assert(Self::plants_prefix(corridor@, i as int + 1) == 0);\n assert(Self::ways_prefix(corridor@, i as int + 1) == Self::ways_prefix(corridor@, i as int));\n }\n }\n }\n i += 1;\n }\n\n proof {\n assert(seat_count == Self::seat_count_prefix(corridor@, len as int));\n assert(ways as int == Self::ways_prefix(corridor@, len as int));\n }\n if seat_count == 0 || seat_count % 2 == 1 {\n proof {\n assert(Self::number_of_ways_spec(corridor@) == 0);\n }\n 0\n } else {\n proof {\n assert(ways <= 2_147_483_647u128) by (nonlinear_arith)\n requires\n 0 <= ways < mod_num,\n mod_num == 1_000_000_007u128,\n {};\n assert(Self::seat_count_prefix(corridor@, len as int) == seat_count);\n assert(Self::seat_count_prefix(corridor@, len as int) != 0);\n assert(Self::seat_count_prefix(corridor@, len as int) % 2 != 1);\n assert(Self::number_of_ways_spec(corridor@) == Self::ways_prefix(corridor@, len as int));\n }\n ways as i32\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2147/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2147/tests/mutated_testcases.jsonl"} {"problem": "lc2148", "family": "leetcode", "description": "# Count Elements With Strictly Smaller and Greater Elements \n\nGiven an integer array `nums`, return *the number of elements that have **both** a strictly smaller and a strictly greater element appear in *`nums`.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [11,7,2,15]\n**Output:** 2\n**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.\nElement 11 has element 7 strictly smaller than it and element 15 strictly greater than it.\nIn total there are 2 elements having both a strictly smaller and a strictly greater element appear in `nums`.\n```\n\nExample 2:\n\n```\n**Input:** nums = [-3,3,3,90]\n**Output:** 2\n**Explanation:** The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.\nSince there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in `nums`.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 100`\n\n\t- `-10^5 <= nums[i] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_elements(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sorting, Counting\nAcceptance rate: 59.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_val(s: Seq, end: int) -> int\n decreases end\n {\n if end <= 1 { s[0] as int }\n else {\n let rest = Self::min_val(s, end - 1);\n if (s[end - 1] as int) < rest { s[end - 1] as int } else { rest }\n }\n }\n\n pub open spec fn max_val(s: Seq, end: int) -> int\n decreases end\n {\n if end <= 1 { s[0] as int }\n else {\n let rest = Self::max_val(s, end - 1);\n if (s[end - 1] as int) > rest { s[end - 1] as int } else { rest }\n }\n }\n\n pub open spec fn count_between(s: Seq, lo: int, hi: int, end: int) -> int\n decreases end\n {\n if end <= 0 { 0 }\n else {\n Self::count_between(s, lo, hi, end - 1) + if lo < (s[end - 1] as int) && (s[end - 1] as int) < hi { 1int } else { 0int }\n }\n }\n\n pub fn count_elements(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() && nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100_000 <= #[trigger] nums[i] && nums[i] <= 100_000,\n ensures\n 0 <= result <= nums.len() as i32,\n nums.len() <= 1 ==> result == 0,\n nums.len() > 1 ==> result as int == Self::count_between(nums@, Self::min_val(nums@, nums.len() as int), Self::max_val(nums@, nums.len() as int), nums.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_elements(nums: Vec) -> i32 {\n let n = nums.len();\n if n <= 1 {\n return 0;\n }\n\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n while i < n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i = i + 1;\n }\n\n let mut ans: i32 = 0;\n i = 0;\n while i < n {\n if nums[i] > min_v && nums[i] < max_v {\n ans = ans + 1;\n }\n i = i + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_val(s: Seq, end: int) -> int\n decreases end\n {\n if end <= 1 { s[0] as int }\n else {\n let rest = Self::min_val(s, end - 1);\n if (s[end - 1] as int) < rest { s[end - 1] as int } else { rest }\n }\n }\n\n pub open spec fn max_val(s: Seq, end: int) -> int\n decreases end\n {\n if end <= 1 { s[0] as int }\n else {\n let rest = Self::max_val(s, end - 1);\n if (s[end - 1] as int) > rest { s[end - 1] as int } else { rest }\n }\n }\n\n pub open spec fn count_between(s: Seq, lo: int, hi: int, end: int) -> int\n decreases end\n {\n if end <= 0 { 0 }\n else {\n Self::count_between(s, lo, hi, end - 1) + if lo < (s[end - 1] as int) && (s[end - 1] as int) < hi { 1int } else { 0int }\n }\n }\n\n pub fn count_elements(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() && nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100_000 <= #[trigger] nums[i] && nums[i] <= 100_000,\n ensures\n 0 <= result <= nums.len() as i32,\n nums.len() <= 1 ==> result == 0,\n nums.len() > 1 ==> result as int == Self::count_between(nums@, Self::min_val(nums@, nums.len() as int), Self::max_val(nums@, nums.len() as int), nums.len() as int),\n {\n let n = nums.len();\n if n <= 1 {\n return 0;\n }\n\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n while i < n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i = i + 1;\n }\n\n let mut ans: i32 = 0;\n i = 0;\n while i < n {\n if nums[i] > min_v && nums[i] < max_v {\n ans = ans + 1;\n }\n i = i + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_val(s: Seq, end: int) -> int\n decreases end\n {\n if end <= 1 { s[0] as int }\n else {\n let rest = Self::min_val(s, end - 1);\n if (s[end - 1] as int) < rest { s[end - 1] as int } else { rest }\n }\n }\n\n pub open spec fn max_val(s: Seq, end: int) -> int\n decreases end\n {\n if end <= 1 { s[0] as int }\n else {\n let rest = Self::max_val(s, end - 1);\n if (s[end - 1] as int) > rest { s[end - 1] as int } else { rest }\n }\n }\n\n pub open spec fn count_between(s: Seq, lo: int, hi: int, end: int) -> int\n decreases end\n {\n if end <= 0 { 0 }\n else {\n Self::count_between(s, lo, hi, end - 1) + if lo < (s[end - 1] as int) && (s[end - 1] as int) < hi { 1int } else { 0int }\n }\n }\n\n pub fn count_elements(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() && nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100_000 <= #[trigger] nums[i] && nums[i] <= 100_000,\n ensures\n 0 <= result <= nums.len() as i32,\n nums.len() <= 1 ==> result == 0,\n nums.len() > 1 ==> result as int == Self::count_between(nums@, Self::min_val(nums@, nums.len() as int), Self::max_val(nums@, nums.len() as int), nums.len() as int),\n {\n let n = nums.len();\n if n <= 1 {\n return 0;\n }\n\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n while i < n\n invariant\n 1 <= i <= n,\n n == nums.len(),\n n >= 2,\n min_v as int == Self::min_val(nums@, i as int),\n max_v as int == Self::max_val(nums@, i as int),\n decreases n - i,\n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i = i + 1;\n }\n\n let mut ans: i32 = 0;\n i = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n n <= 2147483647,\n ans as int == Self::count_between(nums@, min_v as int, max_v as int, i as int),\n 0 <= ans as int <= i as int,\n decreases n - i,\n {\n if nums[i] > min_v && nums[i] < max_v {\n ans = ans + 1;\n }\n i = i + 1;\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2148/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2148/tests/mutated_testcases.jsonl"} {"problem": "lc2154", "family": "leetcode", "description": "# Keep Multiplying Found Values by Two\n\nYou are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.\n\nYou then do the following steps:\n\n- If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).\n\n- Otherwise, **stop** the process.\n\n- **Repeat** this process with the new number as long as you keep finding the number.\n\nReturn *the **final** value of *`original`.\n\n## Example 1:\n\n> **Input:** nums = [5,3,6,1,12], original = 3\n> **Output:** 24\n> **Explanation:** \n> - 3 is found in nums. 3 is multiplied by 2 to obtain 6.\n> - 6 is found in nums. 6 is multiplied by 2 to obtain 12.\n> - 12 is found in nums. 12 is multiplied by 2 to obtain 24.\n> - 24 is not found in nums. Thus, 24 is returned.\n\n## Example 2:\n\n> **Input:** nums = [2,7,9], original = 4\n> **Output:** 4\n> **Explanation:**\n> - 4 is not found in nums. Thus, 4 is returned.\n\n## Constraints:\n\n- `1 <= nums.length <= 1000`\n- `1 <= nums[i], original <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_final_value(nums: Vec, original: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Sorting, Simulation\nAcceptance rate: 75.1%\n", "spec": "use vstd::arithmetic::power::pow;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\nimpl Solution {\n pub open spec fn chain_value(original: i32, k: nat) -> int {\n original as int * pow(2, k)\n }\n\n pub open spec fn appears(nums: Seq, v: int) -> bool {\n exists |i: int| 0 <= i < nums.len() && #[trigger] nums[i] == v\n }\n\n pub fn find_final_value(nums: Vec, original: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n 1 <= original <= 1000,\n ensures\n exists |k: nat|\n result as int == Self::chain_value(original, k)\n && (forall |t: nat| t < k ==> #[trigger] Self::appears(nums@, Self::chain_value(original, t)))\n && forall |i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] != result,\n {\n }\n}\n}\n", "code": "impl Solution {\n pub fn find_final_value(nums: Vec, original: i32) -> i32 {\n let mut current = original;\n let mut found = true;\n while found {\n found = false;\n let mut i = 0;\n while i < nums.len() {\n if nums[i] == current {\n found = true;\n }\n i += 1;\n }\n if found {\n current *= 2;\n }\n }\n current\n }\n}\n", "code_spec": "use vstd::arithmetic::power::pow;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\nimpl Solution {\n pub open spec fn chain_value(original: i32, k: nat) -> int {\n original as int * pow(2, k)\n }\n\n pub open spec fn appears(nums: Seq, v: int) -> bool {\n exists |i: int| 0 <= i < nums.len() && #[trigger] nums[i] == v\n }\n\n pub fn find_final_value(nums: Vec, original: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n 1 <= original <= 1000,\n ensures\n exists |k: nat|\n result as int == Self::chain_value(original, k)\n && (forall |t: nat| t < k ==> #[trigger] Self::appears(nums@, Self::chain_value(original, t)))\n && forall |i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] != result,\n {\n let mut current = original;\n let mut found = true;\n while found {\n found = false;\n let mut i = 0;\n while i < nums.len() {\n if nums[i] == current {\n found = true;\n }\n i += 1;\n }\n if found {\n current *= 2;\n }\n }\n current\n }\n}\n}\n", "verified": "use vstd::arithmetic::power::{lemma_pow0, lemma_pow_adds, lemma_pow1, pow};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\nimpl Solution {\n pub open spec fn chain_value(original: i32, k: nat) -> int {\n original as int * pow(2, k)\n }\n\n pub open spec fn appears(nums: Seq, v: int) -> bool {\n exists |i: int| 0 <= i < nums.len() && #[trigger] nums[i] == v\n }\n\n pub fn find_final_value(nums: Vec, original: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n 1 <= original <= 1000,\n ensures\n exists |k: nat|\n result as int == Self::chain_value(original, k)\n && (forall |t: nat| t < k ==> #[trigger] Self::appears(nums@, Self::chain_value(original, t)))\n && forall |i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] != result,\n {\n let mut current = original;\n let mut found = true;\n proof {\n lemma_pow0(2);\n assert(pow(2, 0) == 1);\n assert(current as int == Self::chain_value(original, 0));\n assert(exists |kk: nat| current as int == Self::chain_value(original, kk)) by {\n assert(current as int == Self::chain_value(original, 0));\n }\n assert(exists |kk: nat|\n current as int == Self::chain_value(original, kk)\n && forall |t: nat| t < kk ==> Self::appears(nums@, Self::chain_value(original, t))\n ) by {\n assert(current as int == Self::chain_value(original, 0));\n }\n }\n while found\n invariant\n 1 <= nums.len() <= 1000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 1000,\n 1 <= original <= 1000,\n original <= current <= 2000,\n !found ==> forall |k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] != current,\n exists |kk: nat|\n current as int == Self::chain_value(original, kk)\n && forall |t: nat| t < kk ==> #[trigger] Self::appears(nums@, Self::chain_value(original, t))\n decreases 2000 - current, if found { 1int } else { 0int }\n {\n found = false;\n let mut i = 0;\n while i < nums.len()\n invariant\n 0 <= i <= nums.len(),\n 1 <= nums.len() <= 1000,\n 1 <= original <= 1000,\n original <= current <= 2000,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 1000,\n found <==> exists |k: int| 0 <= k < i && #[trigger] nums[k] == current\n decreases nums.len() - i\n {\n if nums[i] == current {\n found = true;\n }\n i += 1;\n }\n if found {\n let ghost old_current = current as int;\n let ghost old_k: nat = choose |kk: nat| old_current == Self::chain_value(original, kk)\n && forall |t: nat| t < kk ==> Self::appears(nums@, Self::chain_value(original, t));\n proof {\n let idx = choose |k: int| 0 <= k < nums.len() && nums[k] == current;\n assert(current == nums[idx]);\n lemma_pow_adds(2, old_k, 1);\n lemma_pow1(2);\n assert(old_current == Self::chain_value(original, old_k));\n assert(pow(2, old_k + 1) == pow(2, old_k) * pow(2, 1));\n assert(pow(2, old_k + 1) == pow(2, old_k) * 2);\n assert(Self::appears(nums@, Self::chain_value(original, old_k))) by {\n assert(nums[idx] == current);\n assert(current as int == old_current);\n assert(old_current == Self::chain_value(original, old_k));\n }\n }\n current *= 2;\n proof {\n assert(current as int == old_current * 2);\n assert(current as int == Self::chain_value(original, old_k + 1)) by(nonlinear_arith)\n requires\n old_current == Self::chain_value(original, old_k),\n pow(2, old_k + 1) == pow(2, old_k) * 2,\n current as int == old_current * 2,\n {}\n assert(exists |kk: nat|\n current as int == Self::chain_value(original, kk)\n && forall |t: nat| t < kk ==> Self::appears(nums@, Self::chain_value(original, t))\n ) by {\n assert(current as int == Self::chain_value(original, old_k + 1));\n assert forall |t: nat| t < old_k + 1 implies Self::appears(nums@, Self::chain_value(original, t)) by {\n if t < old_k {\n assert(Self::appears(nums@, Self::chain_value(original, t)));\n } else {\n assert(t == old_k);\n assert(Self::appears(nums@, Self::chain_value(original, old_k)));\n }\n }\n }\n }\n }\n }\n proof {\n let ghost kk: nat = choose |k0: nat|\n current as int == Self::chain_value(original, k0)\n && forall |t: nat| t < k0 ==> Self::appears(nums@, Self::chain_value(original, t));\n assert(forall |i: int| 0 <= i < nums.len() ==> nums[i] != current);\n assert(exists |k: nat|\n current as int == Self::chain_value(original, k)\n && forall |t: nat| t < k ==> Self::appears(nums@, Self::chain_value(original, t))\n && forall |i: int| 0 <= i < nums.len() ==> nums[i] != current\n ) by {\n assert(current as int == Self::chain_value(original, kk));\n }\n }\n current\n }\n}\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2154/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2154/tests/mutated_testcases.jsonl"} {"problem": "lc2160", "family": "leetcode", "description": "# Minimum Sum of Four Digit Number After Splitting Digits\n\nYou are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.\n\n\t- For example, given `num = 2932`, you have the following digits: two `2`'s, one `9` and one `3`. Some of the possible pairs `[new1, new2]` are `[22, 93]`, `[23, 92]`, `[223, 9]` and `[2, 329]`.\n\nReturn *the **minimum** possible sum of *`new1`* and *`new2`.\n\n \n\nExample 1:\n\n```\n**Input:** num = 2932\n**Output:** 52\n**Explanation:** Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.\nThe minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.\n```\n\nExample 2:\n\n```\n**Input:** num = 4009\n**Output:** 13\n**Explanation:** Some possible pairs [new1, new2] are [0, 49], [490, 0], etc. \nThe minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13.\n```\n\n \n\n**Constraints:**\n\n\t- `1000 <= num <= 9999`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_sum(num: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Greedy, Sorting\nAcceptance rate: 86.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn digit0(num: int) -> int {\n num / 1000\n}\n\npub open spec fn digit1(num: int) -> int {\n (num / 100) % 10\n}\n\npub open spec fn digit2(num: int) -> int {\n (num / 10) % 10\n}\n\npub open spec fn digit3(num: int) -> int {\n num % 10\n}\n\npub open spec fn sorted_d1(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let cd_lo = if c < d { c } else { d };\n if ab_lo < cd_lo { ab_lo } else { cd_lo }\n}\n\npub open spec fn sorted_d4(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_hi = if a < b { b } else { a };\n let cd_hi = if c < d { d } else { c };\n if ab_hi < cd_hi { cd_hi } else { ab_hi }\n}\n\npub open spec fn sorted_d2(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n if m1 < m2 { m1 } else { m2 }\n}\n\npub open spec fn sorted_d3(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n if m1 < m2 { m2 } else { m1 }\n}\n\nimpl Solution {\n pub fn minimum_sum(num: i32) -> (result: i32)\n requires\n 1000 <= num <= 9999,\n ensures\n result as int == (sorted_d1(num as int) + sorted_d2(num as int)) * 10\n + sorted_d3(num as int) + sorted_d4(num as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_sum(num: i32) -> i32 {\n let a = num / 1000;\n let b = (num / 100) % 10;\n let c = (num / 10) % 10;\n let d = num % 10;\n\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n\n let d1 = if ab_lo < cd_lo { ab_lo } else { cd_lo };\n let d4 = if ab_hi < cd_hi { cd_hi } else { ab_hi };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n let d2 = if m1 < m2 { m1 } else { m2 };\n let d3 = if m1 < m2 { m2 } else { m1 };\n\n (d1 + d2) * 10 + d3 + d4\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn digit0(num: int) -> int {\n num / 1000\n}\n\npub open spec fn digit1(num: int) -> int {\n (num / 100) % 10\n}\n\npub open spec fn digit2(num: int) -> int {\n (num / 10) % 10\n}\n\npub open spec fn digit3(num: int) -> int {\n num % 10\n}\n\npub open spec fn sorted_d1(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let cd_lo = if c < d { c } else { d };\n if ab_lo < cd_lo { ab_lo } else { cd_lo }\n}\n\npub open spec fn sorted_d4(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_hi = if a < b { b } else { a };\n let cd_hi = if c < d { d } else { c };\n if ab_hi < cd_hi { cd_hi } else { ab_hi }\n}\n\npub open spec fn sorted_d2(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n if m1 < m2 { m1 } else { m2 }\n}\n\npub open spec fn sorted_d3(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n if m1 < m2 { m2 } else { m1 }\n}\n\nimpl Solution {\n pub fn minimum_sum(num: i32) -> (result: i32)\n requires\n 1000 <= num <= 9999,\n ensures\n result as int == (sorted_d1(num as int) + sorted_d2(num as int)) * 10\n + sorted_d3(num as int) + sorted_d4(num as int),\n {\n let a = num / 1000;\n let b = (num / 100) % 10;\n let c = (num / 10) % 10;\n let d = num % 10;\n\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n\n let d1 = if ab_lo < cd_lo { ab_lo } else { cd_lo };\n let d4 = if ab_hi < cd_hi { cd_hi } else { ab_hi };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n let d2 = if m1 < m2 { m1 } else { m2 };\n let d3 = if m1 < m2 { m2 } else { m1 };\n\n (d1 + d2) * 10 + d3 + d4\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn digit0(num: int) -> int {\n num / 1000\n}\n\npub open spec fn digit1(num: int) -> int {\n (num / 100) % 10\n}\n\npub open spec fn digit2(num: int) -> int {\n (num / 10) % 10\n}\n\npub open spec fn digit3(num: int) -> int {\n num % 10\n}\n\npub open spec fn sorted_d1(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let cd_lo = if c < d { c } else { d };\n if ab_lo < cd_lo { ab_lo } else { cd_lo }\n}\n\npub open spec fn sorted_d4(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_hi = if a < b { b } else { a };\n let cd_hi = if c < d { d } else { c };\n if ab_hi < cd_hi { cd_hi } else { ab_hi }\n}\n\npub open spec fn sorted_d2(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n if m1 < m2 { m1 } else { m2 }\n}\n\npub open spec fn sorted_d3(num: int) -> int {\n let a = digit0(num);\n let b = digit1(num);\n let c = digit2(num);\n let d = digit3(num);\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n if m1 < m2 { m2 } else { m1 }\n}\n\nimpl Solution {\n pub fn minimum_sum(num: i32) -> (result: i32)\n requires\n 1000 <= num <= 9999,\n ensures\n result as int == (sorted_d1(num as int) + sorted_d2(num as int)) * 10\n + sorted_d3(num as int) + sorted_d4(num as int),\n {\n let a = num / 1000;\n let b = (num / 100) % 10;\n let c = (num / 10) % 10;\n let d = num % 10;\n\n let ab_lo = if a < b { a } else { b };\n let ab_hi = if a < b { b } else { a };\n let cd_lo = if c < d { c } else { d };\n let cd_hi = if c < d { d } else { c };\n\n let d1 = if ab_lo < cd_lo { ab_lo } else { cd_lo };\n let d4 = if ab_hi < cd_hi { cd_hi } else { ab_hi };\n let m1 = if ab_lo < cd_lo { cd_lo } else { ab_lo };\n let m2 = if ab_hi < cd_hi { ab_hi } else { cd_hi };\n let d2 = if m1 < m2 { m1 } else { m2 };\n let d3 = if m1 < m2 { m2 } else { m1 };\n\n proof {\n assert(digit0(num as int) == a as int);\n assert(digit1(num as int) == b as int);\n assert(digit2(num as int) == c as int);\n assert(digit3(num as int) == d as int);\n\n assert(sorted_d1(num as int) == d1 as int);\n assert(sorted_d2(num as int) == d2 as int);\n assert(sorted_d3(num as int) == d3 as int);\n assert(sorted_d4(num as int) == d4 as int);\n }\n\n (d1 + d2) * 10 + d3 + d4\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2160/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2160/tests/mutated_testcases.jsonl"} {"problem": "lc2169", "family": "leetcode", "description": "# Count Operations to Obtain Zero\n\nYou are given two **non-negative** integers `num1` and `num2`.\n\nIn one **operation**, if `num1 >= num2`, you must subtract `num2` from `num1`, otherwise subtract `num1` from `num2`.\n\n- For example, if `num1 = 5` and `num2 = 4`, subtract `num2` from `num1`, thus obtaining `num1 = 1` and `num2 = 4`. However, if `num1 = 4` and `num2 = 5`, after one operation, `num1 = 4` and `num2 = 1`.\n\nReturn *the **number of operations** required to make either* `num1 = 0` *or* `num2 = 0`.\n\n## Example 1:\n\n> **Input:** num1 = 2, num2 = 3\n> **Output:** 3\n> **Explanation:** \n> - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.\n> - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.\n> - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.\n> Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.\n> So the total number of operations required is 3.\n\n## Example 2:\n\n> **Input:** num1 = 10, num2 = 10\n> **Output:** 1\n> **Explanation:** \n> - Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.\n> Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.\n> So the total number of operations required is 1.\n\n## Constraints:\n\n- `0 <= num1, num2 <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_operations(num1: i32, num2: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Simulation\nAcceptance rate: 79.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn count_ops(a: int, b: int) -> int\n decreases a + b\n{\n if a <= 0 || b <= 0 {\n 0\n } else if a >= b {\n 1 + count_ops(a - b, b)\n } else {\n 1 + count_ops(a, b - a)\n }\n}\n\nimpl Solution {\n pub fn count_operations(num1: i32, num2: i32) -> (result: i32)\n requires\n 0 <= num1 && num1 <= 100000,\n 0 <= num2 && num2 <= 100000,\n ensures\n result == count_ops(num1 as int, num2 as int),\n {\n }\n}\n}\n", "code": "impl Solution {\n pub fn count_operations(num1: i32, num2: i32) -> i32\n {\n let mut a = num1;\n let mut b = num2;\n let mut ops: i32 = 0;\n while a != 0 && b != 0\n {\n if a >= b {\n a -= b;\n } else {\n b -= a;\n }\n ops += 1;\n }\n ops\n }\n}", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn count_ops(a: int, b: int) -> int\n decreases a + b\n{\n if a <= 0 || b <= 0 {\n 0\n } else if a >= b {\n 1 + count_ops(a - b, b)\n } else {\n 1 + count_ops(a, b - a)\n }\n}\n\nimpl Solution {\n pub fn count_operations(num1: i32, num2: i32) -> (result: i32)\n requires\n 0 <= num1 && num1 <= 100000,\n 0 <= num2 && num2 <= 100000,\n ensures\n result == count_ops(num1 as int, num2 as int),\n {\n let mut a = num1;\n let mut b = num2;\n let mut ops: i32 = 0;\n while a != 0 && b != 0\n {\n if a >= b {\n a -= b;\n } else {\n b -= a;\n }\n ops += 1;\n }\n ops\n }\n}\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn count_ops(a: int, b: int) -> int\n decreases a + b\n{\n if a <= 0 || b <= 0 {\n 0\n } else if a >= b {\n 1 + count_ops(a - b, b)\n } else {\n 1 + count_ops(a, b - a)\n }\n}\n\nproof fn count_ops_nonneg(a: int, b: int)\n ensures count_ops(a, b) >= 0\n decreases a + b\n{\n if a <= 0 || b <= 0 {\n } else if a >= b {\n count_ops_nonneg(a - b, b);\n } else {\n count_ops_nonneg(a, b - a);\n }\n}\n\nproof fn count_ops_ge1(a: int, b: int)\n requires a > 0, b > 0\n ensures count_ops(a, b) >= 1\n decreases a + b\n{\n if a >= b {\n assert(count_ops(a, b) == 1 + count_ops(a - b, b));\n count_ops_nonneg(a - b, b);\n } else {\n assert(count_ops(a, b) == 1 + count_ops(a, b - a));\n count_ops_nonneg(a, b - a);\n }\n}\n\nproof fn count_ops_le(a: int, b: int)\n requires a >= 0, b >= 0\n ensures count_ops(a, b) <= a + b\n decreases a + b\n{\n if a <= 0 || b <= 0 {\n } else if a >= b {\n count_ops_le(a - b, b);\n } else {\n count_ops_le(a, b - a);\n }\n}\n\nimpl Solution {\n pub fn count_operations(num1: i32, num2: i32) -> (result: i32)\n requires\n 0 <= num1 && num1 <= 100000,\n 0 <= num2 && num2 <= 100000,\n ensures\n result == count_ops(num1 as int, num2 as int),\n {\n let mut a = num1;\n let mut b = num2;\n let mut ops: i32 = 0;\n proof {\n count_ops_le(num1 as int, num2 as int);\n }\n while a != 0 && b != 0\n invariant\n 0 <= a,\n 0 <= b,\n 0 <= ops as int <= 200000,\n ops as int + count_ops(a as int, b as int) == count_ops(num1 as int, num2 as int),\n num1 as int + num2 as int <= 200000\n decreases a + b\n {\n proof {\n count_ops_ge1(a as int, b as int);\n count_ops_le(num1 as int, num2 as int);\n if a >= b {\n assert(count_ops(a as int, b as int) == 1 + count_ops(a as int - b as int, b as int));\n count_ops_le(a as int - b as int, b as int);\n } else {\n assert(count_ops(a as int, b as int) == 1 + count_ops(a as int, b as int - a as int));\n count_ops_le(a as int, b as int - a as int);\n }\n assert((ops as int) < 200000) by(nonlinear_arith)\n requires\n ops as int + count_ops(a as int, b as int) == count_ops(num1 as int, num2 as int),\n count_ops(a as int, b as int) >= 1,\n count_ops(num1 as int, num2 as int) <= num1 as int + num2 as int,\n num1 as int + num2 as int <= 200000,\n {}\n }\n if a >= b {\n a -= b;\n } else {\n b -= a;\n }\n ops += 1;\n }\n ops\n }\n}\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2169/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2169/tests/mutated_testcases.jsonl"} {"problem": "lc2176", "family": "leetcode", "description": "# 2176. Count Equal and Divisible Pairs in an Array\n\nGiven a **0-indexed** integer array `nums` of length `n` and an integer `k`, return the number of pairs `(i, j)` where `0 <= i < j < n`, such that `nums[i] == nums[j]` and `(i * j)` is divisible by `k`.\n\n## Example 1\n\n- **Input:** `nums = [3,1,2,2,2,1,3], k = 2`\n- **Output:** `4`\n- **Explanation:** There are 4 pairs that meet all the requirements:\n - `nums[0] == nums[6]`, and `0 * 6 == 0`, which is divisible by `2`.\n - `nums[2] == nums[3]`, and `2 * 3 == 6`, which is divisible by `2`.\n - `nums[2] == nums[4]`, and `2 * 4 == 8`, which is divisible by `2`.\n - `nums[3] == nums[4]`, and `3 * 4 == 12`, which is divisible by `2`.\n\n## Example 2\n\n- **Input:** `nums = [1,2,3,4], k = 1`\n- **Output:** `0`\n- **Explanation:** Since no value in `nums` is repeated, there are no pairs `(i,j)` that meet all the requirements.\n\n## Constraints\n\n- $1 \\leq nums.length \\leq 100$\n- $1 \\leq nums[i], k \\leq 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_pairs(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\n84.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_valid_pair(nums: Seq, k: int, i: int, j: int) -> bool {\n 0 <= i < j < nums.len()\n && nums[i] == nums[j]\n && (i * j) % k == 0\n}\n\npub open spec fn count_inner(nums: Seq, k: int, i: int, j: int) -> int\n decreases j - i - 1,\n{\n if j <= i + 1 {\n 0\n } else {\n count_inner(nums, k, i, j - 1)\n + if is_valid_pair(nums, k, i, j - 1) { 1int } else { 0int }\n }\n}\n\npub open spec fn count_all(nums: Seq, k: int, i: int) -> int\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n 0\n } else {\n count_all(nums, k, i + 1) + count_inner(nums, k, i, nums.len() as int)\n }\n}\n\nimpl Solution {\n pub fn count_pairs(nums: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 100,\n ensures\n res as int == count_all(nums@, k as int, 0),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn count_pairs(nums: Vec, k: i32) -> i32 {\n let n = nums.len();\n let mut count: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = i + 1;\n while j < n {\n if nums[i] == nums[j] && ((i * j) as i32) % k == 0 {\n count = count + 1;\n }\n j += 1;\n }\n i += 1;\n }\n count as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_valid_pair(nums: Seq, k: int, i: int, j: int) -> bool {\n 0 <= i < j < nums.len()\n && nums[i] == nums[j]\n && (i * j) % k == 0\n}\n\npub open spec fn count_inner(nums: Seq, k: int, i: int, j: int) -> int\n decreases j - i - 1,\n{\n if j <= i + 1 {\n 0\n } else {\n count_inner(nums, k, i, j - 1)\n + if is_valid_pair(nums, k, i, j - 1) { 1int } else { 0int }\n }\n}\n\npub open spec fn count_all(nums: Seq, k: int, i: int) -> int\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n 0\n } else {\n count_all(nums, k, i + 1) + count_inner(nums, k, i, nums.len() as int)\n }\n}\n\nimpl Solution {\n pub fn count_pairs(nums: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 100,\n ensures\n res as int == count_all(nums@, k as int, 0),\n {\n let n = nums.len();\n let mut count: i64 = 0;\n let mut i: usize = 0;\n\n while i < n\n {\n let mut j: usize = i + 1;\n\n while j < n\n {\n if nums[i] == nums[j] && ((i * j) as i32) % k == 0 {\n count = count + 1;\n }\n j += 1;\n }\n i += 1;\n }\n\n count as i32\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_valid_pair(nums: Seq, k: int, i: int, j: int) -> bool {\n 0 <= i < j < nums.len()\n && nums[i] == nums[j]\n && (i * j) % k == 0\n}\n\npub open spec fn count_inner(nums: Seq, k: int, i: int, j: int) -> int\n decreases j - i - 1,\n{\n if j <= i + 1 {\n 0\n } else {\n count_inner(nums, k, i, j - 1)\n + if is_valid_pair(nums, k, i, j - 1) { 1int } else { 0int }\n }\n}\n\npub open spec fn count_all(nums: Seq, k: int, i: int) -> int\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n 0\n } else {\n count_all(nums, k, i + 1) + count_inner(nums, k, i, nums.len() as int)\n }\n}\n\nproof fn lemma_count_inner_bound(nums: Seq, k: int, i: int, j: int)\n requires\n 0 <= i,\n j <= nums.len(),\n ensures\n 0 <= count_inner(nums, k, i, j) <= if j > i + 1 { j - i - 1 } else { 0 },\n decreases j - i - 1,\n{\n if j <= i + 1 {\n } else {\n lemma_count_inner_bound(nums, k, i, j - 1);\n }\n}\n\nproof fn lemma_count_inner_mono(nums: Seq, k: int, i: int, j1: int, j2: int)\n requires\n 0 <= i,\n j1 <= j2,\n j2 <= nums.len(),\n ensures\n count_inner(nums, k, i, j1) <= count_inner(nums, k, i, j2),\n decreases j2 - j1,\n{\n if j1 >= j2 {\n } else {\n lemma_count_inner_mono(nums, k, i, j1, j2 - 1);\n \n }\n}\n\nproof fn lemma_count_all_bound(nums: Seq, k: int, i: int)\n requires\n 0 <= i,\n nums.len() <= 100,\n ensures\n 0 <= count_all(nums, k, i),\n count_all(nums, k, i) <= if i < nums.len() {\n (nums.len() - i) * nums.len()\n } else {\n 0int\n },\n decreases nums.len() - i,\n{\n if i >= nums.len() {\n } else {\n lemma_count_all_bound(nums, k, i + 1);\n lemma_count_inner_bound(nums, k, i, nums.len() as int);\n assert(count_inner(nums, k, i, nums.len() as int) <= nums.len() - i - 1);\n assert(count_all(nums, k, i + 1) <= if (i + 1) < nums.len() {\n (nums.len() - (i + 1)) * nums.len()\n } else {\n 0int\n });\n assert(count_all(nums, k, i) <= (nums.len() - i - 1) + (nums.len() - (i + 1)) * nums.len()) by {\n \n }\n assert((nums.len() - i - 1) + (nums.len() - (i + 1)) * nums.len() <= (nums.len() - i) * nums.len()) by(nonlinear_arith)\n requires\n 0 <= i < nums.len() <= 100,\n {}\n }\n}\n\nimpl Solution {\n pub fn count_pairs(nums: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 100,\n ensures\n res as int == count_all(nums@, k as int, 0),\n {\n let n = nums.len();\n let mut count: i64 = 0;\n let mut i: usize = 0;\n\n proof {\n lemma_count_all_bound(nums@, k as int, 0);\n assert(count_all(nums@, k as int, 0) <= nums.len() * nums.len());\n assert(nums.len() * nums.len() <= 10000) by(nonlinear_arith)\n requires nums.len() <= 100;\n }\n\n while i < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 100,\n forall |idx: int| 0 <= idx < nums.len() ==> 1 <= #[trigger] nums[idx] <= 100,\n 1 <= k <= 100,\n 0 <= i <= n,\n 0 <= count <= 10000,\n count == count_all(nums@, k as int, 0) - count_all(nums@, k as int, i as int),\n decreases n - i,\n {\n let mut j: usize = i + 1;\n\n proof {\n lemma_count_inner_bound(nums@, k as int, i as int, (i + 1) as int);\n }\n\n while j < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 100,\n forall |idx: int| 0 <= idx < nums.len() ==> 1 <= #[trigger] nums[idx] <= 100,\n 1 <= k <= 100,\n 0 <= i < n,\n i + 1 <= j <= n,\n 0 <= count <= 10000,\n count == count_all(nums@, k as int, 0) - count_all(nums@, k as int, i as int)\n + count_inner(nums@, k as int, i as int, j as int),\n decreases n - j,\n {\n proof {\n \n \n lemma_count_inner_bound(nums@, k as int, i as int, (j + 1) as int);\n lemma_count_all_bound(nums@, k as int, i as int);\n lemma_count_all_bound(nums@, k as int, (i + 1) as int);\n \n lemma_count_inner_mono(nums@, k as int, i as int, (j + 1) as int, nums.len() as int);\n \n \n \n \n \n \n assert(count_all(nums@, k as int, i as int) == count_all(nums@, k as int, (i + 1) as int) + count_inner(nums@, k as int, i as int, nums.len() as int));\n }\n\n proof {\n assert(i * j <= 99 * 99) by(nonlinear_arith)\n requires 0 <= i < 100, 0 <= j < 100;\n assert(i * j < 0x8000_0000) by(nonlinear_arith)\n requires 0 <= i * j <= 9801;\n }\n if nums[i] == nums[j] && ((i * j) as i32) % k == 0 {\n proof {\n assert(((i * j) as i32) as int == (i * j) as int);\n assert(is_valid_pair(nums@, k as int, i as int, j as int));\n }\n count = count + 1;\n } else {\n proof {\n if nums@[i as int] == nums@[j as int] {\n assert(((i * j) as i32) as int == (i * j) as int);\n assert(((i * j) as i32) % k != 0);\n assert((i * j) % (k as int) != 0);\n }\n }\n assert(!is_valid_pair(nums@, k as int, i as int, j as int));\n }\n\n \n assert(count == count_all(nums@, k as int, 0) - count_all(nums@, k as int, i as int)\n + count_inner(nums@, k as int, i as int, (j + 1) as int));\n\n proof {\n \n lemma_count_all_bound(nums@, k as int, 0);\n lemma_count_all_bound(nums@, k as int, (i + 1) as int);\n assert(count_inner(nums@, k as int, i as int, (j + 1) as int) <= count_inner(nums@, k as int, i as int, nums.len() as int));\n assert(count_all(nums@, k as int, (i + 1) as int) >= 0);\n assert(count_all(nums@, k as int, 0) <= nums.len() * nums.len());\n assert(nums.len() * nums.len() <= 10000) by(nonlinear_arith)\n requires nums.len() <= 100;\n }\n\n j += 1;\n }\n\n \n assert(count == count_all(nums@, k as int, 0) - count_all(nums@, k as int, i as int)\n + count_inner(nums@, k as int, i as int, n as int));\n \n \n assert(count == count_all(nums@, k as int, 0) - count_all(nums@, k as int, (i + 1) as int));\n\n proof {\n lemma_count_all_bound(nums@, k as int, (i + 1) as int);\n }\n\n i += 1;\n }\n\n \n assert(count_all(nums@, k as int, n as int) == 0int);\n assert(count == count_all(nums@, k as int, 0));\n\n count as i32\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc2176/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2176/tests/mutated_testcases.jsonl"} {"problem": "lc2187", "family": "leetcode", "description": "# Minimum Time to Complete Trips\n\nYou are given an array `time` where `time[i]` denotes the time taken by the `i^th` bus to complete **one trip**.\n\nEach bus can make multiple trips **successively**; that is, the next trip can start **immediately after** completing the current trip. Also, each bus operates **independently**; that is, the trips of one bus do not influence the trips of any other bus.\n\nYou are also given an integer `totalTrips`, which denotes the number of trips all buses should make **in total**. Return *the **minimum time** required for all buses to complete **at least** *`totalTrips`* trips*.\n\n \n\nExample 1:\n\n```\n**Input:** time = [1,2,3], totalTrips = 5\n**Output:** 3\n**Explanation:**\n- At time t = 1, the number of trips completed by each bus are [1,0,0]. \n The total number of trips completed is 1 + 0 + 0 = 1.\n- At time t = 2, the number of trips completed by each bus are [2,1,0]. \n The total number of trips completed is 2 + 1 + 0 = 3.\n- At time t = 3, the number of trips completed by each bus are [3,1,1]. \n The total number of trips completed is 3 + 1 + 1 = 5.\nSo the minimum time needed for all buses to complete at least 5 trips is 3.\n```\n\nExample 2:\n\n```\n**Input:** time = [2], totalTrips = 1\n**Output:** 2\n**Explanation:**\nThere is only one bus, and it will complete its first trip at t = 2.\nSo the minimum time needed to complete 1 trip is 2.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= time.length <= 10^5`\n\n\t- `1 <= time[i], totalTrips <= 10^7`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_time(time: Vec, total_trips: i32) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Binary Search\nAcceptance rate: 39.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn capped_trips_prefix(time: Seq, t: int, cap: int, n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n let prev = Self::capped_trips_prefix(time, t, cap, n - 1);\n let s = prev + t / (time[n - 1] as int);\n if s >= cap { cap } else { s }\n }\n }\n\n pub open spec fn feasible(time: Seq, t: int, total: int) -> bool {\n Self::capped_trips_prefix(time, t, total, time.len() as int) == total\n }\n\n pub fn minimum_time(time: Vec, total_trips: i32) -> (ans: i64)\n requires\n 1 <= time.len() <= 100000,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n 1 <= total_trips <= 10000000,\n ensures\n 1 <= ans <= 100000000000000,\n Self::feasible(time@, ans as int, total_trips as int),\n forall |t: int| 1 <= t < ans ==> !#[trigger] Self::feasible(time@, t, total_trips as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn can_finish(time: &Vec, t: i64, total: i32) -> bool {\n let mut trips: i64 = 0;\n let target = total as i64;\n let mut i: usize = 0;\n while i < time.len() {\n let add = t / (time[i] as i64);\n if trips >= target - add {\n trips = target;\n } else {\n trips = trips + add;\n }\n i = i + 1;\n }\n trips == target\n }\n\n pub fn minimum_time(time: Vec, total_trips: i32) -> i64 {\n let mut left: i64 = 1;\n let mut right: i64 = 100000000000000;\n while left < right {\n let mid = left + (right - left) / 2;\n if Self::can_finish(&time, mid, total_trips) {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n left\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn capped_trips_prefix(time: Seq, t: int, cap: int, n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n let prev = Self::capped_trips_prefix(time, t, cap, n - 1);\n let s = prev + t / (time[n - 1] as int);\n if s >= cap { cap } else { s }\n }\n }\n\n pub open spec fn feasible(time: Seq, t: int, total: int) -> bool {\n Self::capped_trips_prefix(time, t, total, time.len() as int) == total\n }\n\n fn can_finish(time: &Vec, t: i64, total: i32) -> (ok: bool)\n requires\n 1 <= time.len() <= 100000,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n 1 <= t <= 100000000000000,\n 1 <= total <= 10000000,\n ensures\n ok == Self::feasible(time@, t as int, total as int),\n {\n let mut trips: i64 = 0;\n let target = total as i64;\n let mut i: usize = 0;\n while i < time.len() {\n let add = t / (time[i] as i64);\n if trips >= target - add {\n trips = target;\n } else {\n trips = trips + add;\n }\n i = i + 1;\n }\n trips == target\n }\n\n pub fn minimum_time(time: Vec, total_trips: i32) -> (ans: i64)\n requires\n 1 <= time.len() <= 100000,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n 1 <= total_trips <= 10000000,\n ensures\n 1 <= ans <= 100000000000000,\n Self::feasible(time@, ans as int, total_trips as int),\n forall |t: int| 1 <= t < ans ==> !#[trigger] Self::feasible(time@, t, total_trips as int),\n {\n let mut left: i64 = 1;\n let mut right: i64 = 100000000000000;\n while left < right {\n let mid = left + (right - left) / 2;\n if Self::can_finish(&time, mid, total_trips) {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n left\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn capped_trips_prefix(time: Seq, t: int, cap: int, n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n let prev = Self::capped_trips_prefix(time, t, cap, n - 1);\n let s = prev + t / (time[n - 1] as int);\n if s >= cap { cap } else { s }\n }\n }\n\n pub open spec fn feasible(time: Seq, t: int, total: int) -> bool {\n Self::capped_trips_prefix(time, t, total, time.len() as int) == total\n }\n\n proof fn lemma_div_time_monotonic(d: int, t1: int, t2: int)\n requires\n 1 <= d,\n 0 <= t1 <= t2,\n ensures\n t1 / d <= t2 / d,\n {\n assert((t1 / d) <= (t2 / d)) by (nonlinear_arith)\n requires 1 <= d, 0 <= t1 <= t2\n {\n }\n }\n\n proof fn lemma_capped_trips_time_monotonic(time: Seq, t1: int, t2: int, cap: int, n: int)\n requires\n 0 <= n <= time.len(),\n 0 <= t1 <= t2,\n 1 <= cap,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n ensures\n Self::capped_trips_prefix(time, t1, cap, n) <= Self::capped_trips_prefix(time, t2, cap, n),\n decreases n,\n {\n if n > 0 {\n Self::lemma_capped_trips_time_monotonic(time, t1, t2, cap, n - 1);\n assert(1 <= time[n - 1] as int);\n Self::lemma_div_time_monotonic(time[n - 1] as int, t1, t2);\n let p1 = Self::capped_trips_prefix(time, t1, cap, n - 1);\n let p2 = Self::capped_trips_prefix(time, t2, cap, n - 1);\n let s1 = p1 + t1 / (time[n - 1] as int);\n let s2 = p2 + t2 / (time[n - 1] as int);\n assert(p1 + t1 / (time[n - 1] as int) <= p2 + t1 / (time[n - 1] as int)) by (nonlinear_arith)\n requires p1 <= p2\n {\n }\n assert(p2 + t1 / (time[n - 1] as int) <= p2 + t2 / (time[n - 1] as int)) by (nonlinear_arith)\n requires t1 / (time[n - 1] as int) <= t2 / (time[n - 1] as int)\n {\n }\n assert(s1 == p1 + t1 / (time[n - 1] as int));\n assert(s2 == p2 + t2 / (time[n - 1] as int));\n let m = p2 + t1 / (time[n - 1] as int);\n assert(s1 <= m);\n assert(m <= s2);\n assert(s1 <= s2);\n assert(Self::capped_trips_prefix(time, t1, cap, n) == if s1 >= cap { cap } else { s1 });\n assert(Self::capped_trips_prefix(time, t2, cap, n) == if s2 >= cap { cap } else { s2 });\n assert((if s1 >= cap { cap } else { s1 }) <= (if s2 >= cap { cap } else { s2 })) by (nonlinear_arith)\n requires s1 <= s2\n {\n }\n }\n }\n\n proof fn lemma_feasible_time_monotonic(time: Seq, t1: int, t2: int, total: int)\n requires\n 0 <= t1 <= t2,\n 1 <= total,\n Self::feasible(time, t1, total),\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n ensures\n Self::feasible(time, t2, total),\n {\n Self::lemma_capped_trips_time_monotonic(time, t1, t2, total, time.len() as int);\n }\n\n proof fn lemma_capped_sticky(time: Seq, t: int, cap: int, n: int)\n requires\n 1 <= cap,\n 0 <= t,\n 1 <= n < time.len(),\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n Self::capped_trips_prefix(time, t, cap, n) == cap,\n ensures\n Self::capped_trips_prefix(time, t, cap, n + 1) == cap,\n {\n reveal_with_fuel(Solution::capped_trips_prefix, 2);\n let prev = Self::capped_trips_prefix(time, t, cap, n);\n let s = prev + t / (time[n] as int);\n assert(prev == cap);\n assert(1 <= time[n] as int);\n assert(0 <= t / (time[n] as int)) by (nonlinear_arith)\n requires 0 <= t, 1 <= time[n] as int\n {\n }\n assert(s == cap + t / (time[n] as int));\n assert(s >= cap) by (nonlinear_arith)\n requires s == cap + t / (time[n] as int), 0 <= t / (time[n] as int)\n {\n }\n assert(Self::capped_trips_prefix(time, t, cap, n + 1) == cap);\n }\n\n proof fn lemma_capped_from_one_full(time: Seq, t: int, cap: int, n: int)\n requires\n 1 <= n <= time.len(),\n 0 <= t,\n 1 <= cap,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n Self::capped_trips_prefix(time, t, cap, 1) == cap,\n ensures\n Self::capped_trips_prefix(time, t, cap, n) == cap,\n decreases n,\n {\n if n > 1 {\n Self::lemma_capped_from_one_full(time, t, cap, n - 1);\n Self::lemma_capped_sticky(time, t, cap, n - 1);\n }\n }\n\n proof fn lemma_feasible_at_upper_bound(time: Seq, total: int)\n requires\n 1 <= time.len() <= 100000,\n 1 <= total <= 10000000,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n ensures\n Self::feasible(time, 100000000000000, total),\n {\n reveal_with_fuel(Solution::capped_trips_prefix, 2);\n assert(1 <= time[0] as int <= 10000000);\n assert(100000000000000int / (time[0] as int) >= 10000000) by (nonlinear_arith)\n requires 1 <= time[0] as int <= 10000000\n {\n }\n assert(10000000 >= total) by (nonlinear_arith)\n requires total <= 10000000\n {\n }\n assert(100000000000000int / (time[0] as int) >= total) by (nonlinear_arith)\n requires 100000000000000int / (time[0] as int) >= 10000000, 10000000 >= total\n {\n }\n assert(Self::capped_trips_prefix(time, 100000000000000int, total, 0) == 0);\n assert(Self::capped_trips_prefix(time, 100000000000000int, total, 1) == total);\n Self::lemma_capped_from_one_full(time, 100000000000000int, total, time.len() as int);\n assert(Self::capped_trips_prefix(time, 100000000000000int, total, time.len() as int) == total);\n }\n\n fn can_finish(time: &Vec, t: i64, total: i32) -> (ok: bool)\n requires\n 1 <= time.len() <= 100000,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n 1 <= t <= 100000000000000,\n 1 <= total <= 10000000,\n ensures\n ok == Self::feasible(time@, t as int, total as int),\n {\n let mut trips: i64 = 0;\n let target = total as i64;\n let mut i: usize = 0;\n while i < time.len()\n invariant\n 0 <= i <= time.len(),\n 1 <= t <= 100000000000000,\n 1 <= target <= 10000000,\n 1 <= time.len() <= 100000,\n forall |j: int| 0 <= j < time.len() ==> 1 <= #[trigger] time[j] <= 10000000,\n trips as int == Self::capped_trips_prefix(time@, t as int, target as int, i as int),\n 0 <= trips <= target,\n decreases time.len() - i,\n {\n let add = t / (time[i] as i64);\n if trips >= target - add {\n trips = target;\n } else {\n trips = trips + add;\n }\n i = i + 1;\n }\n trips == target\n }\n\n pub fn minimum_time(time: Vec, total_trips: i32) -> (ans: i64)\n requires\n 1 <= time.len() <= 100000,\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n 1 <= total_trips <= 10000000,\n ensures\n 1 <= ans <= 100000000000000,\n Self::feasible(time@, ans as int, total_trips as int),\n forall |t: int| 1 <= t < ans ==> !#[trigger] Self::feasible(time@, t, total_trips as int),\n {\n proof {\n Self::lemma_feasible_at_upper_bound(time@, total_trips as int);\n }\n\n let mut left: i64 = 1;\n let mut right: i64 = 100000000000000;\n while left < right\n invariant\n 1 <= left <= right <= 100000000000000,\n 1 <= total_trips <= 10000000,\n 1 <= time.len() <= 100000,\n Self::feasible(time@, right as int, total_trips as int),\n forall |d: int| 1 <= d < left ==> !#[trigger] Self::feasible(time@, d, total_trips as int),\n forall |i: int| 0 <= i < time.len() ==> 1 <= #[trigger] time[i] <= 10000000,\n decreases right - left,\n {\n let mid = left + (right - left) / 2;\n if Self::can_finish(&time, mid, total_trips) {\n right = mid;\n } else {\n proof {\n assert(!Self::feasible(time@, mid as int, total_trips as int));\n assert forall |d: int| 1 <= d < mid + 1 implies !Self::feasible(time@, d, total_trips as int) by {\n if d < left {\n } else {\n assert(left <= d <= mid);\n if Self::feasible(time@, d, total_trips as int) {\n Self::lemma_feasible_time_monotonic(time@, d, mid as int, total_trips as int);\n assert(Self::feasible(time@, mid as int, total_trips as int));\n }\n }\n }\n }\n left = mid + 1;\n }\n }\n left\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2187/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2187/tests/mutated_testcases.jsonl"} {"problem": "lc2202", "family": "leetcode", "description": "# Maximize the Topmost Element After K Moves\n\nYou are given a 0-indexed integer array `nums` representing a pile, where `nums[0]` is the topmost element.\n\nIn one move, you can do either of the following:\n\n- If the pile is not empty, remove the topmost element.\n\n- If there is at least one removed element, add any one removed element back onto the pile. That element becomes the new top.\n\nYou are also given an integer `k`, the total number of moves.\n\nReturn the maximum possible topmost element after exactly `k` moves. If it is impossible to end with a non-empty pile, return `-1`.\n\n## Example 1:\n\n> **Input:** nums = [5,2,2,4,0,6], k = 4\n> **Output:** 5\n> **Explanation:**\n> One valid sequence is:\n> - Remove 5 \u2192 [2,2,4,0,6]\n> - Remove 2 \u2192 [2,4,0,6]\n> - Remove 2 \u2192 [4,0,6]\n> - Add back 5 \u2192 [5,4,0,6]\n> This achieves the largest possible top value.\n\n## Example 2:\n\n> **Input:** nums = [2], k = 1\n> **Output:** -1\n> **Explanation:**\n> After one move the only action is removing 2, leaving the pile empty.\n\n## Constraints:\n\n- $1 \\leq nums.length \\leq 10^5$\n\n- $0 \\leq nums[i], k \\leq 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn maximum_top(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nStaff, Array, Greedy, Weekly Contest 284\nAcceptance rate: 24.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_max(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n -1\n } else {\n let prev = Self::prefix_max(nums, end - 1);\n let cur = nums[end - 1] as int;\n if prev >= cur { prev } else { cur }\n }\n }\n\n pub open spec fn removable_count(nums: Seq, k: int) -> int {\n if k <= 1 {\n 0\n } else if k - 1 <= nums.len() {\n k - 1\n } else {\n nums.len() as int\n }\n }\n\n pub open spec fn maximum_top_spec(nums: Seq, k: int) -> int {\n if k == 0 {\n nums[0] as int\n } else if nums.len() == 1 {\n if k % 2 == 1 { -1 } else { nums[0] as int }\n } else {\n let r = Self::removable_count(nums, k);\n let removed_best = Self::prefix_max(nums, r);\n let keep = if k < nums.len() { nums[k] as int } else { -1 };\n if removed_best >= keep { removed_best } else { keep }\n }\n }\n\n pub fn maximum_top(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n 0 <= k <= 1_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n result as int == Self::maximum_top_spec(nums@, k as int),\n {\n \n }\n}\n\n}\n", "code": "impl Solution {\n pub fn maximum_top(nums: Vec, k: i32) -> i32\n {\n let n = nums.len();\n\n if k == 0 {\n return nums[0];\n }\n\n if n == 1 {\n if k % 2 == 1 {\n return -1;\n } else {\n return nums[0];\n }\n }\n\n let mut best: i32 = -1;\n let upper = if k <= 1 {\n 0usize\n } else {\n let t = (k as i64) - 1;\n if t <= n as i64 {\n t as usize\n } else {\n n\n }\n };\n\n let mut i: usize = 0;\n while i < upper\n {\n if nums[i] > best {\n best = nums[i];\n }\n i = i + 1;\n }\n\n if (k as i64) < n as i64 {\n let j = k as usize;\n if nums[j] > best {\n best = nums[j];\n }\n } \n\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_max(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n -1\n } else {\n let prev = Self::prefix_max(nums, end - 1);\n let cur = nums[end - 1] as int;\n if prev >= cur { prev } else { cur }\n }\n }\n\n pub open spec fn removable_count(nums: Seq, k: int) -> int {\n if k <= 1 {\n 0\n } else if k - 1 <= nums.len() {\n k - 1\n } else {\n nums.len() as int\n }\n }\n\n pub open spec fn maximum_top_spec(nums: Seq, k: int) -> int {\n if k == 0 {\n nums[0] as int\n } else if nums.len() == 1 {\n if k % 2 == 1 { -1 } else { nums[0] as int }\n } else {\n let r = Self::removable_count(nums, k);\n let removed_best = Self::prefix_max(nums, r);\n let keep = if k < nums.len() { nums[k] as int } else { -1 };\n if removed_best >= keep { removed_best } else { keep }\n }\n }\n\n pub fn maximum_top(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n 0 <= k <= 1_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n result as int == Self::maximum_top_spec(nums@, k as int),\n {\n let n = nums.len();\n\n if k == 0 {\n return nums[0];\n }\n\n if n == 1 {\n if k % 2 == 1 {\n return -1;\n } else {\n return nums[0];\n }\n }\n\n let mut best: i32 = -1;\n let upper = if k <= 1 {\n 0usize\n } else {\n let t = (k as i64) - 1;\n if t <= n as i64 {\n t as usize\n } else {\n n\n }\n };\n\n let mut i: usize = 0;\n while i < upper\n {\n if nums[i] > best {\n best = nums[i];\n }\n i = i + 1;\n }\n\n if (k as i64) < n as i64 {\n let j = k as usize;\n if nums[j] > best {\n best = nums[j];\n }\n } \n\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn prefix_max(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n -1\n } else {\n let prev = Self::prefix_max(nums, end - 1);\n let cur = nums[end - 1] as int;\n if prev >= cur { prev } else { cur }\n }\n }\n\n pub open spec fn removable_count(nums: Seq, k: int) -> int {\n if k <= 1 {\n 0\n } else if k - 1 <= nums.len() {\n k - 1\n } else {\n nums.len() as int\n }\n }\n\n pub open spec fn maximum_top_spec(nums: Seq, k: int) -> int {\n if k == 0 {\n nums[0] as int\n } else if nums.len() == 1 {\n if k % 2 == 1 { -1 } else { nums[0] as int }\n } else {\n let r = Self::removable_count(nums, k);\n let removed_best = Self::prefix_max(nums, r);\n let keep = if k < nums.len() { nums[k] as int } else { -1 };\n if removed_best >= keep { removed_best } else { keep }\n }\n }\n\n pub fn maximum_top(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n 0 <= k <= 1_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n result as int == Self::maximum_top_spec(nums@, k as int),\n {\n let n = nums.len();\n\n if k == 0 {\n return nums[0];\n }\n\n if n == 1 {\n if k % 2 == 1 {\n return -1;\n } else {\n return nums[0];\n }\n }\n\n let mut best: i32 = -1;\n let upper = if k <= 1 {\n 0usize\n } else {\n let t = (k as i64) - 1;\n if t <= n as i64 {\n t as usize\n } else {\n n\n }\n };\n\n let mut i: usize = 0;\n while i < upper\n invariant\n i <= upper,\n upper <= n,\n n == nums.len(),\n n > 1,\n 0 <= k,\n best as int == Self::prefix_max(nums@, i as int),\n upper as int == Self::removable_count(nums@, k as int),\n decreases upper - i,\n {\n if nums[i] > best {\n best = nums[i];\n }\n i = i + 1;\n }\n\n if (k as i64) < n as i64 {\n let j = k as usize;\n if nums[j] > best {\n best = nums[j];\n }\n } \n\n best\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2202/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2202/tests/mutated_testcases.jsonl"} {"problem": "lc2210", "family": "leetcode", "description": "# Count Hills and Valleys in an Array\n\nYou are given a **0-indexed** integer array `nums`. An index `i` is part of a **hill** in `nums` if the closest non-equal neighbors of `i` are smaller than `nums[i]`. Similarly, an index `i` is part of a **valley** in `nums` if the closest non-equal neighbors of `i` are larger than `nums[i]`. Adjacent indices `i` and `j` are part of the **same** hill or valley if `nums[i] == nums[j]`.\n\nNote that for an index to be part of a hill or valley, it must have a non-equal neighbor on **both** the left and right of the index.\n\nReturn the number of hills and valleys in `nums`.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [2,4,1,1,6,5]\n**Output:** 3\n**Explanation:**\nAt index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.\nAt index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill. \nAt index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.\nAt index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.\nAt index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.\nAt index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. \nThere are 3 hills and valleys so we return 3.\n```\n\nExample 2:\n\n```\n**Input:** nums = [6,6,5,5,4,1]\n**Output:** 0\n**Explanation:**\nAt index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.\nAt index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.\nAt index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.\nAt index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.\nAt index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.\nAt index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.\nThere are 0 hills and valleys so we return 0.\n```\n\n \n\n**Constraints:**\n\n\t- `3 <= nums.length <= 100`\n\n\t- `1 <= nums[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_hill_valley(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 69.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn next_diff_or_len(nums: Seq, i: int) -> int\n recommends\n 0 <= i < nums.len(),\n decreases nums.len() - i,\n {\n if i + 1 >= nums.len() {\n nums.len() as int\n } else if nums[i + 1] != nums[i] {\n i + 1\n } else {\n Self::next_diff_or_len(nums, i + 1)\n }\n }\n\n pub open spec fn is_hv_start(nums: Seq, i: int) -> bool {\n if 1 <= i < nums.len() - 1 && nums[i] != nums[i - 1] {\n let r = Self::next_diff_or_len(nums, i);\n r < nums.len()\n && ((nums[i] > nums[i - 1] && nums[i] > nums[r])\n || (nums[i] < nums[i - 1] && nums[i] < nums[r]))\n } else {\n false\n }\n }\n\n pub open spec fn count_hv_upto(nums: Seq, k: int) -> int\n recommends\n 0 <= k <= nums.len(),\n decreases k,\n {\n if k <= 1 {\n 0\n } else {\n Self::count_hv_upto(nums, k - 1) + if Self::is_hv_start(nums, k - 1) { 1int } else { 0int }\n }\n }\n\n pub fn count_hill_valley(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::count_hv_upto(nums@, nums.len() as int - 1),\n 0 <= result <= nums.len(),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_hill_valley(nums: Vec) -> i32 {\n let n = nums.len();\n let mut i: usize = 1;\n let mut count: i32 = 0;\n\n while i + 1 < n {\n if nums[i] == nums[i - 1] {\n i = i + 1;\n continue;\n }\n\n let mut r = i + 1;\n while r < n && nums[r] == nums[i] {\n r = r + 1;\n }\n\n if r < n {\n if (nums[i] > nums[i - 1] && nums[i] > nums[r])\n || (nums[i] < nums[i - 1] && nums[i] < nums[r])\n {\n count = count + 1;\n }\n }\n\n i = i + 1;\n }\n\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn next_diff_or_len(nums: Seq, i: int) -> int\n recommends\n 0 <= i < nums.len(),\n decreases nums.len() - i,\n {\n if i + 1 >= nums.len() {\n nums.len() as int\n } else if nums[i + 1] != nums[i] {\n i + 1\n } else {\n Self::next_diff_or_len(nums, i + 1)\n }\n }\n\n pub open spec fn is_hv_start(nums: Seq, i: int) -> bool {\n if 1 <= i < nums.len() - 1 && nums[i] != nums[i - 1] {\n let r = Self::next_diff_or_len(nums, i);\n r < nums.len()\n && ((nums[i] > nums[i - 1] && nums[i] > nums[r])\n || (nums[i] < nums[i - 1] && nums[i] < nums[r]))\n } else {\n false\n }\n }\n\n pub open spec fn count_hv_upto(nums: Seq, k: int) -> int\n recommends\n 0 <= k <= nums.len(),\n decreases k,\n {\n if k <= 1 {\n 0\n } else {\n Self::count_hv_upto(nums, k - 1) + if Self::is_hv_start(nums, k - 1) { 1int } else { 0int }\n }\n }\n\n pub fn count_hill_valley(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::count_hv_upto(nums@, nums.len() as int - 1),\n 0 <= result <= nums.len(),\n {\n let n = nums.len();\n let mut i: usize = 1;\n let mut count: i32 = 0;\n\n while i + 1 < n {\n if nums[i] == nums[i - 1] {\n i = i + 1;\n continue;\n }\n\n let mut r = i + 1;\n while r < n && nums[r] == nums[i] {\n r = r + 1;\n }\n\n if r < n {\n if (nums[i] > nums[i - 1] && nums[i] > nums[r])\n || (nums[i] < nums[i - 1] && nums[i] < nums[r])\n {\n count = count + 1;\n }\n }\n\n i = i + 1;\n }\n\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn next_diff_or_len(nums: Seq, i: int) -> int\n recommends\n 0 <= i < nums.len(),\n decreases nums.len() - i,\n {\n if i + 1 >= nums.len() {\n nums.len() as int\n } else if nums[i + 1] != nums[i] {\n i + 1\n } else {\n Self::next_diff_or_len(nums, i + 1)\n }\n }\n\n pub open spec fn is_hv_start(nums: Seq, i: int) -> bool {\n if 1 <= i < nums.len() - 1 && nums[i] != nums[i - 1] {\n let r = Self::next_diff_or_len(nums, i);\n r < nums.len()\n && ((nums[i] > nums[i - 1] && nums[i] > nums[r])\n || (nums[i] < nums[i - 1] && nums[i] < nums[r]))\n } else {\n false\n }\n }\n\n pub open spec fn count_hv_upto(nums: Seq, k: int) -> int\n recommends\n 0 <= k <= nums.len(),\n decreases k,\n {\n if k <= 1 {\n 0\n } else {\n Self::count_hv_upto(nums, k - 1) + if Self::is_hv_start(nums, k - 1) { 1int } else { 0int }\n }\n }\n\n proof fn lemma_next_diff_or_len(nums: Seq, i: int, r: int)\n requires\n 0 <= i < nums.len(),\n i + 1 <= r <= nums.len(),\n forall |k: int| i + 1 <= k < r ==> nums[k] == nums[i],\n r == nums.len() || nums[r] != nums[i],\n ensures\n Self::next_diff_or_len(nums, i) == r,\n decreases r - i,\n {\n if i + 1 == r {\n if r < nums.len() {\n assert(nums[i + 1] != nums[i]);\n }\n } else {\n assert(i + 1 < r);\n assert(nums[i + 1] == nums[i]);\n assert forall |k: int| i + 2 <= k < r implies #[trigger] nums[k] == nums[i + 1] by {\n assert(nums[k] == nums[i]);\n }\n Self::lemma_next_diff_or_len(nums, i + 1, r);\n }\n }\n\n pub fn count_hill_valley(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::count_hv_upto(nums@, nums.len() as int - 1),\n 0 <= result <= nums.len(),\n {\n let n = nums.len();\n let mut i: usize = 1;\n let mut count: i32 = 0;\n\n while i + 1 < n\n invariant\n 3 <= n <= 100,\n n == nums.len(),\n 1 <= i < n,\n count as int == Self::count_hv_upto(nums@, i as int),\n 0 <= count <= i,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n decreases n - i,\n {\n let ghost old_i = i;\n let ghost old_count = count;\n\n if nums[i] == nums[i - 1] {\n i = i + 1;\n proof {\n assert(!Self::is_hv_start(nums@, old_i as int));\n assert(Self::count_hv_upto(nums@, i as int)\n == Self::count_hv_upto(nums@, old_i as int)\n + if Self::is_hv_start(nums@, old_i as int) { 1int } else { 0int });\n assert(count == old_count);\n }\n continue;\n }\n\n let mut r = i + 1;\n while r < n && nums[r] == nums[i]\n invariant\n i + 1 <= r <= n,\n n == nums.len(),\n 1 <= i < n,\n nums@[i as int] != nums@[i as int - 1],\n forall |k: int| i + 1 <= k < r ==> nums@[k] == nums@[i as int],\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n decreases n - r,\n {\n r = r + 1;\n }\n\n if r < n {\n if (nums[i] > nums[i - 1] && nums[i] > nums[r])\n || (nums[i] < nums[i - 1] && nums[i] < nums[r])\n {\n count = count + 1;\n }\n }\n\n i = i + 1;\n proof {\n assert(r == n || nums@[r as int] != nums@[old_i as int]);\n Self::lemma_next_diff_or_len(nums@, old_i as int, r as int);\n assert(Self::count_hv_upto(nums@, i as int)\n == Self::count_hv_upto(nums@, old_i as int)\n + if Self::is_hv_start(nums@, old_i as int) { 1int } else { 0int });\n if count == old_count + 1 {\n assert(Self::is_hv_start(nums@, old_i as int));\n } else {\n assert(count == old_count);\n assert(!Self::is_hv_start(nums@, old_i as int));\n }\n }\n }\n\n count\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2210/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2210/tests/mutated_testcases.jsonl"} {"problem": "lc2216", "family": "leetcode", "description": "# Minimum Deletions to Make Array Beautiful\n\nYou are given a **0-indexed** integer array `nums`. The array `nums` is **beautiful** if:\n\n\t- `nums.length` is even.\n\n\t- `nums[i] != nums[i + 1]` for all `i % 2 == 0`.\n\nNote that an empty array is considered beautiful.\n\nYou can delete any number of elements from `nums`. When you delete an element, all the elements to the right of the deleted element will be **shifted one unit to the left** to fill the gap created and all the elements to the left of the deleted element will remain **unchanged**.\n\nReturn *the **minimum** number of elements to delete from *`nums`* to make it **beautiful.*\n\n \n\nExample 1:\n\n```\n**Input:** nums = [1,1,2,3,5]\n**Output:** 1\n**Explanation:** You can delete either `nums[0]` or `nums[1]` to make `nums` = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make `nums` beautiful.\n```\n\nExample 2:\n\n```\n**Input:** nums = [1,1,2,2,3,3]\n**Output:** 2\n**Explanation:** You can delete `nums[0]` and `nums[5]` to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 10^5`\n\n\t- `0 <= nums[i] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_deletion(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Stack, Greedy\nAcceptance rate: 49.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn del_prefix(nums: Seq, n: nat) -> int\n recommends\n n <= nums.len(),\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n let prev = del_prefix(nums, (n - 1) as nat);\n let idx = (n - 2) as int;\n if ((idx - prev) % 2 == 0) && nums[idx] == nums[idx + 1] {\n prev + 1\n } else {\n prev\n }\n }\n}\n\npub open spec fn min_deletion_spec(nums: Seq) -> int {\n let d = del_prefix(nums, nums.len() as nat);\n d + if ((nums.len() - d) % 2 == 1) { 1int } else { 0int }\n}\n\nimpl Solution {\n pub fn min_deletion(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 100000,\n ensures\n result as int == min_deletion_spec(nums@),\n 0 <= result as int <= nums.len(),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_deletion(nums: Vec) -> i32 {\n let mut d: i64 = 0;\n let mut i: usize = 0;\n\n while i + 1 < nums.len() {\n if ((i as i64 - d) % 2 == 0) && nums[i] == nums[i + 1] {\n d = d + 1;\n }\n i = i + 1;\n }\n\n if ((nums.len() as i64 - d) % 2) == 1 {\n d = d + 1;\n }\n\n d as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn del_prefix(nums: Seq, n: nat) -> int\n recommends\n n <= nums.len(),\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n let prev = del_prefix(nums, (n - 1) as nat);\n let idx = (n - 2) as int;\n if ((idx - prev) % 2 == 0) && nums[idx] == nums[idx + 1] {\n prev + 1\n } else {\n prev\n }\n }\n}\n\npub open spec fn min_deletion_spec(nums: Seq) -> int {\n let d = del_prefix(nums, nums.len() as nat);\n d + if ((nums.len() - d) % 2 == 1) { 1int } else { 0int }\n}\n\nimpl Solution {\n pub fn min_deletion(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 100000,\n ensures\n result as int == min_deletion_spec(nums@),\n 0 <= result as int <= nums.len(),\n {\n let mut d: i64 = 0;\n let mut i: usize = 0;\n\n while i + 1 < nums.len() {\n if ((i as i64 - d) % 2 == 0) && nums[i] == nums[i + 1] {\n d = d + 1;\n }\n i = i + 1;\n }\n\n if ((nums.len() as i64 - d) % 2) == 1 {\n d = d + 1;\n }\n\n d as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn del_prefix(nums: Seq, n: nat) -> int\n recommends\n n <= nums.len(),\n decreases n,\n{\n if n <= 1 {\n 0\n } else {\n let prev = del_prefix(nums, (n - 1) as nat);\n let idx = (n - 2) as int;\n if ((idx - prev) % 2 == 0) && nums[idx] == nums[idx + 1] {\n prev + 1\n } else {\n prev\n }\n }\n}\n\npub open spec fn min_deletion_spec(nums: Seq) -> int {\n let d = del_prefix(nums, nums.len() as nat);\n d + if ((nums.len() - d) % 2 == 1) { 1int } else { 0int }\n}\n\nimpl Solution {\n pub fn min_deletion(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 100000,\n ensures\n result as int == min_deletion_spec(nums@),\n 0 <= result as int <= nums.len(),\n {\n let mut d: i64 = 0;\n let mut i: usize = 0;\n\n while i + 1 < nums.len()\n invariant\n nums.len() <= 100000,\n 0 <= i < nums.len(),\n i <= 100000,\n 0 <= d as int <= i as int,\n d as int == del_prefix(nums@, (i + 1) as nat),\n decreases nums.len() - i,\n {\n if ((i as i64 - d) % 2 == 0) && nums[i] == nums[i + 1] {\n d = d + 1;\n }\n proof {\n let next_i = i + 1;\n assert(next_i + 1 <= nums.len());\n assert(del_prefix(nums@, (next_i + 1) as nat)\n == if (((next_i - 1) as int - del_prefix(nums@, next_i as nat)) % 2 == 0)\n && nums@[(next_i - 1) as int] == nums@[next_i as int] {\n del_prefix(nums@, next_i as nat) + 1\n } else {\n del_prefix(nums@, next_i as nat)\n });\n }\n i = i + 1;\n }\n\n proof {\n assert(i < nums.len());\n assert(i + 1 >= nums.len());\n assert(i + 1 == nums.len());\n assert(del_prefix(nums@, nums.len() as nat) == d as int);\n }\n\n if ((nums.len() as i64 - d) % 2) == 1 {\n d = d + 1;\n }\n\n proof {\n let base = del_prefix(nums@, nums.len() as nat);\n assert(base == if nums.len() <= 1 { 0 } else { base });\n assert(d as int == min_deletion_spec(nums@));\n assert(0 <= d as int <= nums.len());\n }\n\n d as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2216/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2216/tests/mutated_testcases.jsonl"} {"problem": "lc223", "family": "leetcode", "description": "# Rectangle Area\n\nGiven the coordinates of two **rectilinear** rectangles in a 2D plane, return *the total area covered by the two rectangles*.\n\nThe first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.\n\nThe second rectangle is defined by its **bottom-left** corner `(bx1, by1)` and its **top-right** corner `(bx2, by2)`.\n\n## Example 1:\n> **Input:** ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2\n> **Output:** 45\n\n## Example 2:\n> **Input:** ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2\n> **Output:** 16\n\n## Constraints:\n\n- $-10^4 <= ax1 <= ax2 <= 10^4$\n\n- $-10^4 <= ay1 <= ay2 <= 10^4$\n\n- $-10^4 <= bx1 <= bx2 <= 10^4$\n\n- $-10^4 <= by1 <= by2 <= 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn compute_area(ax1: i32, ay1: i32, ax2: i32, ay2: i32, bx1: i32, by1: i32, bx2: i32, by2: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Geometry\nAcceptance rate: 48.4%", "spec": "use vstd::prelude::*;\nuse vstd::math::{max as spec_max, min as spec_min};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn compute_area(ax1: i32, ay1: i32, ax2: i32, ay2: i32, bx1: i32, by1: i32, bx2: i32, by2: i32) -> (res: i32) \n requires \n -10_000 <= ax1 <= ax2 <= 10_000, \n -10_000 <= ay1 <= ay2 <= 10_000, \n -10_000 <= bx1 <= bx2 <= 10_000, \n -10_000 <= by1 <= by2 <= 10_000, \n ensures \n res == (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1)\n - spec_max(0, spec_min(ax2 as int, bx2 as int) - spec_max(ax1 as int, bx1 as int)) \n * spec_max(0, spec_min(ay2 as int, by2 as int) - spec_max(ay1 as int, by1 as int))\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn max(x: i32, y: i32) -> i32\n {\n if x >= y { x as i32 } else { y as i32 }\n }\n\n pub fn min(x: i32, y: i32) -> i32\n {\n if x <= y { x as i32} else { y as i32 }\n }\n\n pub fn compute_area(ax1: i32, ay1: i32, ax2: i32, ay2: i32, bx1: i32, by1: i32, bx2: i32, by2: i32) -> i32\n {\n let area1 = (ax2 - ax1) * (ay2 - ay1);\n let area2 = (bx2 - bx1) * (by2 - by1);\n let overlap_x = Self::max(0, Self::min(ax2, bx2) - Self::max(ax1, bx1));\n let overlap_y = Self::max(0, Self::min(ay2, by2) - Self::max(ay1, by1));\n let intersection = overlap_x * overlap_y;\n return area1 + area2 - intersection;\n }\n}\n", "code_spec": "use vstd::prelude::*;\nuse vstd::math::{max as spec_max, min as spec_min};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn max(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_max(x as int, y as int)\n {\n if x >= y { x as i32 } else { y as i32 }\n }\n\n pub fn min(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_min(x as int, y as int)\n {\n if x <= y { x as i32} else { y as i32 }\n }\n\n pub fn compute_area(ax1: i32, ay1: i32, ax2: i32, ay2: i32, bx1: i32, by1: i32, bx2: i32, by2: i32) -> (res: i32) \n requires \n -10_000 <= ax1 <= ax2 <= 10_000, \n -10_000 <= ay1 <= ay2 <= 10_000, \n -10_000 <= bx1 <= bx2 <= 10_000, \n -10_000 <= by1 <= by2 <= 10_000, \n ensures \n res == (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1)\n - spec_max(0, spec_min(ax2 as int, bx2 as int) - spec_max(ax1 as int, bx1 as int)) \n * spec_max(0, spec_min(ay2 as int, by2 as int) - spec_max(ay1 as int, by1 as int))\n {\n let area1 = (ax2 - ax1) * (ay2 - ay1);\n let area2 = (bx2 - bx1) * (by2 - by1);\n let overlap_x = Self::max(0, Self::min(ax2, bx2) - Self::max(ax1, bx1));\n let overlap_y = Self::max(0, Self::min(ay2, by2) - Self::max(ay1, by1));\n let intersection = overlap_x * overlap_y;\n return area1 + area2 - intersection;\n }\n}\n\n}", "verified": "use vstd::prelude::*;\nuse vstd::math::{max as spec_max, min as spec_min};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn max(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_max(x as int, y as int)\n {\n if x >= y { x as i32 } else { y as i32 }\n }\n\n pub fn min(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_min(x as int, y as int)\n {\n if x <= y { x as i32} else { y as i32 }\n }\n\n pub fn compute_area(ax1: i32, ay1: i32, ax2: i32, ay2: i32, bx1: i32, by1: i32, bx2: i32, by2: i32) -> (res: i32) \n requires \n -10_000 <= ax1 <= ax2 <= 10_000, \n -10_000 <= ay1 <= ay2 <= 10_000, \n -10_000 <= bx1 <= bx2 <= 10_000, \n -10_000 <= by1 <= by2 <= 10_000, \n ensures \n res == (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1)\n - spec_max(0, spec_min(ax2 as int, bx2 as int) - spec_max(ax1 as int, bx1 as int)) \n * spec_max(0, spec_min(ay2 as int, by2 as int) - spec_max(ay1 as int, by1 as int))\n {\n assert((ax2 - ax1) * (ay2 - ay1) <= 400_000_000) by(nonlinear_arith)\n requires \n -10_000 <= ax1 <= ax2 <= 10_000, \n -10_000 <= ay1 <= ay2 <= 10_000, \n {}\n\n assert((bx2 - bx1) * (by2 - by1) <= 400_000_000) by(nonlinear_arith)\n requires \n -10_000 <= bx1 <= bx2 <= 10_000, \n -10_000 <= by1 <= by2 <= 10_000, \n {}\n\n let area1 = (ax2 - ax1) * (ay2 - ay1);\n let area2 = (bx2 - bx1) * (by2 - by1);\n let overlap_x = Self::max(0, Self::min(ax2, bx2) - Self::max(ax1, bx1));\n let overlap_y = Self::max(0, Self::min(ay2, by2) - Self::max(ay1, by1));\n \n assert(0 <= overlap_x <= 20_000);\n assert(0 <= overlap_y <= 20_000);\n \n assert(overlap_x * overlap_y <= 400_000_000) by(nonlinear_arith)\n requires \n 0 <= overlap_x <= 20_000,\n 0 <= overlap_y <= 20_000,\n {}\n \n let intersection = overlap_x * overlap_y;\n \n assert(area1 + area2 - intersection <= 800_000_000) by(nonlinear_arith)\n requires\n area1 <= 400_000_000,\n area2 <= 400_000_000,\n intersection >= 0,\n {}\n \n return area1 + area2 - intersection;\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc223/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc223/tests/mutated_testcases.jsonl"} {"problem": "lc2260", "family": "leetcode", "description": "# Minimum Consecutive Cards to Pick Up\n\nYou are given an integer array `cards` where `cards[i]` represents the **value** of the `i^th` card. A pair of cards are **matching** if the cards have the **same** value.\n\nReturn* the **minimum** number of **consecutive** cards you have to pick up to have a pair of **matching** cards among the picked cards.* If it is impossible to have matching cards, return `-1`.\n\n \n\nExample 1:\n\n```\n**Input:** cards = [3,4,2,3,4,7]\n**Output:** 4\n**Explanation:** We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal.\n```\n\nExample 2:\n\n```\n**Input:** cards = [1,0,5,3]\n**Output:** -1\n**Explanation:** There is no way to pick up a set of consecutive cards that contain a pair of matching cards.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= cards.length <= 10^5`\n\n\t- `0 <= cards[i] <= 10^6`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_card_pickup(cards: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Hash Table, Sliding Window\nAcceptance rate: 53.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n pub struct Solution;\n\n impl Solution {\n pub fn minimum_card_pickup(cards: Vec) -> (res: i32)\n requires\n 1 <= cards.len() <= 100000,\n forall|i: int| 0 <= i < cards.len() ==> 0 <= #[trigger] cards[i] <= 1000000,\n ensures\n res == -1 ==> (forall|i: int, j: int| 0 <= i < j < cards.len() as int ==> #[trigger] cards[i] != #[trigger] cards[j]),\n res != -1 ==> (exists|i: int, j: int| 0 <= i < j < cards.len() as int && #[trigger] cards[i] == #[trigger] cards[j] && res as int == j - i + 1),\n res != -1 ==> (forall|i: int, j: int| 0 <= i < j < cards.len() as int && #[trigger] cards[i] == #[trigger] cards[j] ==> res as int <= j - i + 1),\n {\n }\n }\n}\n", "code": "impl Solution {\n pub fn minimum_card_pickup(cards: Vec) -> i32 {\n let n = cards.len();\n let mut last_seen: Vec = Vec::new();\n let mut vi: usize = 0;\n while vi <= 1_000_000 {\n last_seen.push(-1);\n vi += 1;\n }\n\n let mut min_pickup: i32 = -1;\n let mut i: usize = 0;\n while i < n {\n let v = cards[i] as usize;\n let prev = last_seen[v];\n if prev != -1 {\n let cand = (i as i32) - prev + 1;\n if min_pickup == -1 || cand < min_pickup {\n min_pickup = cand;\n }\n }\n last_seen[v] = i as i32;\n i += 1;\n }\n\n min_pickup\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn closest_match(cards: Seq, v: i32, end: int) -> int\n decreases end\n{\n if end <= 0 {\n -1\n } else if cards[end - 1] == v {\n end - 1\n } else {\n closest_match(cards, v, end - 1)\n }\n}\n\npub open spec fn best_pickup_upto(cards: Seq, end: int) -> int\n decreases end\n{\n if end <= 0 {\n -1\n } else {\n let prev_best = best_pickup_upto(cards, end - 1);\n let cm = closest_match(cards, cards[end - 1], end - 1);\n let cand = if cm == -1 { -1 } else { (end - 1) - cm + 1 };\n if cand == -1 {\n prev_best\n } else if prev_best == -1 || cand < prev_best {\n cand\n } else {\n prev_best\n }\n }\n}\n\nimpl Solution {\n pub fn minimum_card_pickup(cards: Vec) -> (res: i32)\n requires\n 1 <= cards.len() <= 100000,\n forall|i: int| 0 <= i < cards.len() ==> 0 <= #[trigger] cards[i] <= 1000000,\n ensures\n res == -1 ==> (forall|i: int, j: int| 0 <= i < j < cards.len() as int ==> #[trigger] cards[i] != #[trigger] cards[j]),\n res != -1 ==> (exists|i: int, j: int| 0 <= i < j < cards.len() as int && #[trigger] cards[i] == #[trigger] cards[j] && res as int == j - i + 1),\n res != -1 ==> (forall|i: int, j: int| 0 <= i < j < cards.len() as int && #[trigger] cards[i] == #[trigger] cards[j] ==> res as int <= j - i + 1),\n {\n let n = cards.len();\n let mut last_seen: Vec = Vec::new();\n let mut vi: usize = 0;\n while vi <= 1_000_000 {\n last_seen.push(-1);\n vi += 1;\n }\n\n let mut min_pickup: i32 = -1;\n let mut i: usize = 0;\n while i < n {\n let v = cards[i] as usize;\n let prev = last_seen[v];\n if prev != -1 {\n let cand = (i as i32) - prev + 1;\n if min_pickup == -1 || cand < min_pickup {\n min_pickup = cand;\n }\n }\n last_seen.set(v, i as i32);\n i += 1;\n }\n\n min_pickup\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn closest_match(cards: Seq, v: i32, end: int) -> int\n decreases end\n{\n if end <= 0 {\n -1\n } else if cards[end - 1] == v {\n end - 1\n } else {\n closest_match(cards, v, end - 1)\n }\n}\n\npub open spec fn best_pickup_upto(cards: Seq, end: int) -> int\n decreases end\n{\n if end <= 0 {\n -1\n } else {\n let prev_best = best_pickup_upto(cards, end - 1);\n let cm = closest_match(cards, cards[end - 1], end - 1);\n let cand = if cm == -1 { -1 } else { (end - 1) - cm + 1 };\n if cand == -1 {\n prev_best\n } else if prev_best == -1 || cand < prev_best {\n cand\n } else {\n prev_best\n }\n }\n}\n\nproof fn lemma_closest_match_char(cards: Seq, v: i32, end: int)\n requires 0 <= end <= cards.len(),\n ensures\n closest_match(cards, v, end) == -1 ==> (forall |k: int| 0 <= k < end ==> #[trigger] cards[k] != v),\n (forall |k: int| 0 <= k < end ==> #[trigger] cards[k] != v) ==> closest_match(cards, v, end) == -1,\n closest_match(cards, v, end) != -1 ==> (\n 0 <= closest_match(cards, v, end) < end\n && cards[closest_match(cards, v, end)] == v\n && (forall |k: int| closest_match(cards, v, end) < k < end ==> #[trigger] cards[k] != v)\n ),\n decreases end\n{\n if end > 0 {\n lemma_closest_match_char(cards, v, end - 1);\n }\n}\n\nproof fn lemma_best_pickup_char(cards: Seq, end: int)\n requires 0 <= end <= cards.len(),\n ensures\n best_pickup_upto(cards, end) == -1 ==>\n (forall |i: int, j: int| 0 <= i < j < end ==> #[trigger] cards[i] != #[trigger] cards[j]),\n (forall |i: int, j: int| 0 <= i < j < end ==> #[trigger] cards[i] != #[trigger] cards[j]) ==>\n best_pickup_upto(cards, end) == -1,\n best_pickup_upto(cards, end) != -1 ==> (\n exists |i: int, j: int| 0 <= i < j < end && #[trigger] cards[i] == #[trigger] cards[j]\n && best_pickup_upto(cards, end) == j - i + 1\n ),\n best_pickup_upto(cards, end) != -1 ==> (\n forall |i: int, j: int| 0 <= i < j < end && #[trigger] cards[i] == #[trigger] cards[j]\n ==> best_pickup_upto(cards, end) <= j - i + 1\n ),\n decreases end\n{\n if end > 0 {\n lemma_best_pickup_char(cards, end - 1);\n lemma_closest_match_char(cards, cards[end - 1], end - 1);\n let prev_best = best_pickup_upto(cards, end - 1);\n let cm = closest_match(cards, cards[end - 1], end - 1);\n let cand = if cm == -1 { -1int } else { (end - 1) - cm + 1 };\n if cm == -1 {\n assert(forall |k: int| 0 <= k < end - 1 ==> #[trigger] cards[k] != cards[end - 1]);\n } else {\n assert(cards[cm] == cards[end - 1]);\n assert(forall |k: int| cm < k < end - 1 ==> #[trigger] cards[k] != cards[end - 1]);\n assert forall |k: int| 0 <= k < end - 1 && #[trigger] cards[k] == cards[end - 1] implies k <= cm by {\n if k > cm {\n assert(cards[k] != cards[end - 1]);\n }\n }\n }\n }\n}\n\nimpl Solution {\n pub fn minimum_card_pickup(cards: Vec) -> (res: i32)\n requires\n 1 <= cards.len() <= 100000,\n forall|i: int| 0 <= i < cards.len() ==> 0 <= #[trigger] cards[i] <= 1000000,\n ensures\n res == -1 ==> (forall|i: int, j: int| 0 <= i < j < cards.len() as int ==> #[trigger] cards[i] != #[trigger] cards[j]),\n res != -1 ==> (exists|i: int, j: int| 0 <= i < j < cards.len() as int && #[trigger] cards[i] == #[trigger] cards[j] && res as int == j - i + 1),\n res != -1 ==> (forall|i: int, j: int| 0 <= i < j < cards.len() as int && #[trigger] cards[i] == #[trigger] cards[j] ==> res as int <= j - i + 1),\n {\n let n = cards.len();\n let mut last_seen: Vec = Vec::new();\n let mut vi: usize = 0;\n while vi <= 1_000_000\n invariant\n last_seen@.len() == vi as int,\n 0 <= vi <= 1_000_001,\n forall |v: int| 0 <= v < vi as int ==> #[trigger] last_seen@[v] == closest_match(cards@, v as i32, 0),\n decreases 1_000_001 - vi,\n {\n last_seen.push(-1);\n vi += 1;\n }\n\n let mut min_pickup: i32 = -1;\n let mut i: usize = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == cards.len(),\n 1 <= cards.len() <= 100000,\n last_seen@.len() == 1_000_001,\n forall |k: int| 0 <= k < cards.len() ==> 0 <= #[trigger] cards@[k] <= 1_000_000,\n forall |v: int| 0 <= v <= 1_000_000 ==> #[trigger] last_seen@[v] == closest_match(cards@, v as i32, i as int),\n min_pickup as int == best_pickup_upto(cards@, i as int),\n -1 <= min_pickup <= 100_000,\n decreases n - i,\n {\n let v = cards[i] as usize;\n proof {\n assert(cards@[i as int] as int == v as int);\n }\n let prev = last_seen[v];\n proof {\n assert(prev as int == closest_match(cards@, cards@[i as int], i as int));\n lemma_closest_match_char(cards@, cards@[i as int], i as int);\n if prev != -1 {\n assert(0 <= prev < n as i32);\n }\n }\n if prev != -1 {\n let cand = (i as i32) - prev + 1;\n if min_pickup == -1 || cand < min_pickup {\n min_pickup = cand;\n }\n }\n proof {\n assert forall |vv: int| 0 <= vv <= 1_000_000 && vv != v as int implies\n #[trigger] last_seen@[vv] == closest_match(cards@, vv as i32, i as int + 1) by {\n assert(cards@[i as int] as int != vv);\n }\n }\n last_seen.set(v, i as i32);\n i += 1;\n }\n\n proof {\n lemma_best_pickup_char(cards@, n as int);\n }\n min_pickup\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2260/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2260/tests/mutated_testcases.jsonl"} {"problem": "lc2293", "family": "leetcode", "description": "# Min Max Game\n\nYou are given a **0-indexed** integer array `nums` whose length is a power of `2`.\n\nApply the following algorithm on `nums`:\n\n\t- Let `n` be the length of `nums`. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n / 2`.\n\n\t- For every **even** index `i` where `0 <= i < n / 2`, **assign** the value of `newNums[i]` as `min(nums[2 * i], nums[2 * i + 1])`.\n\n\t- For every **odd** index `i` where `0 <= i < n / 2`, **assign** the value of `newNums[i]` as `max(nums[2 * i], nums[2 * i + 1])`.\n\n\t- **Replace** the array `nums` with `newNums`.\n\n\t- **Repeat** the entire process starting from step 1.\n\nReturn *the last number that remains in *`nums`* after applying the algorithm.*\n\n \n\nExample 1:\n\n```\n**Input:** nums = [1,3,5,2,4,8,2,2]\n**Output:** 1\n**Explanation:** The following arrays are the results of applying the algorithm repeatedly.\nFirst: nums = [1,5,4,2]\nSecond: nums = [1,4]\nThird: nums = [1]\n1 is the last remaining number, so we return 1.\n```\n\nExample 2:\n\n```\n**Input:** nums = [3]\n**Output:** 3\n**Explanation:** 3 is already the last remaining number, so we return 3.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 1024`\n\n\t- `1 <= nums[i] <= 10^9`\n\n\t- `nums.length` is a power of `2`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_max_game(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Simulation\nAcceptance rate: 64.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn pow2(k: int) -> int\n decreases k,\n {\n if k <= 0 { 1 } else { 2 * Self::pow2(k - 1) }\n }\n\n pub open spec fn round_once(s: Seq) -> Seq {\n Seq::new(\n s.len() / 2,\n |i: int| {\n if i % 2 == 0 {\n if s[2 * i] < s[2 * i + 1] { s[2 * i] } else { s[2 * i + 1] }\n } else {\n if s[2 * i] > s[2 * i + 1] { s[2 * i] } else { s[2 * i + 1] }\n }\n },\n )\n }\n\n pub open spec fn game_after_steps(s: Seq, steps: int) -> Seq\n decreases steps,\n {\n if steps <= 0 || s.len() <= 1 {\n s\n } else {\n Self::game_after_steps(Self::round_once(s), steps - 1)\n }\n }\n\n pub open spec fn game_result(s: Seq) -> i32 {\n Self::game_after_steps(s, 10)[0]\n }\n\n pub fn min_max_game(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 1024,\n exists |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n exists |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k)\n && Self::game_after_steps(nums@, k) == seq![result],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_max_game(nums: Vec) -> i32 {\n let mut nums = nums;\n let mut n: usize = nums.len();\n\n while n > 1 {\n let mut i: usize = 0;\n while i < n / 2 {\n let left = nums[2 * i];\n let right = nums[2 * i + 1];\n let val = if i % 2 == 0 {\n if left < right { left } else { right }\n } else {\n if left > right { left } else { right }\n };\n nums[i] = val;\n i = i + 1;\n }\n n = n / 2;\n }\n\n nums[0]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn pow2(k: int) -> int\n decreases k,\n {\n if k <= 0 { 1 } else { 2 * Self::pow2(k - 1) }\n }\n\n pub open spec fn round_once(s: Seq) -> Seq {\n Seq::new(\n s.len() / 2,\n |i: int| {\n if i % 2 == 0 {\n if s[2 * i] < s[2 * i + 1] { s[2 * i] } else { s[2 * i + 1] }\n } else {\n if s[2 * i] > s[2 * i + 1] { s[2 * i] } else { s[2 * i + 1] }\n }\n },\n )\n }\n\n pub open spec fn game_after_steps(s: Seq, steps: int) -> Seq\n decreases steps,\n {\n if steps <= 0 || s.len() <= 1 {\n s\n } else {\n Self::game_after_steps(Self::round_once(s), steps - 1)\n }\n }\n\n pub open spec fn game_result(s: Seq) -> i32 {\n Self::game_after_steps(s, 10)[0]\n }\n\n pub fn min_max_game(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 1024,\n exists |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n exists |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k)\n && Self::game_after_steps(nums@, k) == seq![result],\n {\n let mut nums = nums;\n let mut n: usize = nums.len();\n\n while n > 1\n decreases n,\n {\n let mut i: usize = 0;\n while i < n / 2\n decreases n / 2 - i,\n {\n let left = nums[2 * i];\n let right = nums[2 * i + 1];\n let val = if i % 2 == 0 {\n if left < right { left } else { right }\n } else {\n if left > right { left } else { right }\n };\n nums.set(i, val);\n i = i + 1;\n }\n n = n / 2;\n }\n\n\n nums[0]\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn pow2(k: int) -> int\n decreases k,\n {\n if k <= 0 { 1 } else { 2 * Self::pow2(k - 1) }\n }\n\n pub open spec fn round_once(s: Seq) -> Seq {\n Seq::new(\n s.len() / 2,\n |i: int| {\n if i % 2 == 0 {\n if s[2 * i] < s[2 * i + 1] { s[2 * i] } else { s[2 * i + 1] }\n } else {\n if s[2 * i] > s[2 * i + 1] { s[2 * i] } else { s[2 * i + 1] }\n }\n },\n )\n }\n\n pub open spec fn game_after_steps(s: Seq, steps: int) -> Seq\n decreases steps,\n {\n if steps <= 0 || s.len() <= 1 {\n s\n } else {\n Self::game_after_steps(Self::round_once(s), steps - 1)\n }\n }\n\n pub open spec fn game_result(s: Seq) -> i32 {\n Self::game_after_steps(s, 10)[0]\n }\n\n pub fn min_max_game(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 1024,\n exists |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n exists |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k)\n && Self::game_after_steps(nums@, k) == seq![result],\n {\n let mut nums = nums;\n let ghost orig = nums@;\n let ghost k0 = choose |k: int| 0 <= k <= 10 && nums.len() == Self::pow2(k);\n let ghost mut cur = nums@;\n let ghost mut rem: int = k0;\n let mut n: usize = nums.len();\n\n while n > 1\n invariant\n 1 <= n <= nums.len(),\n 1 <= nums.len() <= 1024,\n n == cur.len(),\n 0 <= rem <= k0 <= 10,\n n == Self::pow2(rem),\n Self::game_after_steps(cur, rem) == Self::game_after_steps(orig, k0),\n forall |i: int| 0 <= i < n ==> #[trigger] nums[i] == cur[i],\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n decreases n,\n {\n let ghost prev = cur;\n let mut i: usize = 0;\n while i < n / 2\n invariant\n 1 < n <= nums.len(),\n n == prev.len(),\n 0 <= i <= n / 2,\n 1 <= nums.len() <= 1024,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < i ==> nums[k] == Self::round_once(prev)[k],\n forall |k: int| 2 * i <= k < n ==> nums[k] == prev[k],\n decreases n / 2 - i,\n {\n let ghost old_i = i;\n let left = nums[2 * i];\n let right = nums[2 * i + 1];\n let val = if i % 2 == 0 {\n if left < right { left } else { right }\n } else {\n if left > right { left } else { right }\n };\n nums.set(i, val);\n i = i + 1;\n proof {\n assert(left == prev[(2 * old_i) as int]);\n assert(right == prev[(2 * old_i + 1) as int]);\n assert(val == Self::round_once(prev)[old_i as int]);\n assert(nums[old_i as int] == val);\n\n assert forall |k: int| 0 <= k < i implies nums[k] == Self::round_once(prev)[k] by {\n if k < old_i as int {\n } else {\n assert(k == old_i as int);\n }\n };\n\n assert forall |k: int| 2 * i <= k < n implies nums[k] == prev[k] by {\n };\n }\n }\n proof {\n assert(i == n / 2);\n assert(cur == prev);\n assert forall |k: int| 0 <= k < n / 2 implies nums[k] == Self::round_once(prev)[k] by {\n };\n }\n proof {\n cur = Self::round_once(prev);\n assert(rem > 0);\n rem = rem - 1;\n assert(Self::game_after_steps(prev, rem + 1) == Self::game_after_steps(orig, k0));\n assert(Self::game_after_steps(cur, rem) == Self::game_after_steps(orig, k0));\n }\n n = n / 2;\n }\n\n proof {\n assert(n == Self::pow2(rem));\n assert(rem == 0);\n assert(Self::game_after_steps(cur, 0) == Self::game_after_steps(orig, k0));\n assert(cur == Self::game_after_steps(orig, k0));\n assert(cur == Self::game_after_steps(orig, k0));\n assert(nums[0] == cur[0]);\n assert(cur.len() == 1);\n assert(cur == seq![nums[0]]);\n }\n\n nums[0]\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2293/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2293/tests/mutated_testcases.jsonl"} {"problem": "lc2302", "family": "leetcode", "description": "# Count Subarrays With Score Less Than K\n\nYou are given a positive integer array `nums` and an integer `k`.\n\nThe **score** of a subarray is defined as:\n\n- `(sum of the subarray) * (length of the subarray)`\n\nReturn the number of non-empty subarrays of `nums` whose score is strictly less than `k`.\n\n## Example 1\n\n- **Input:** `nums = [2,1,4,3,5]`, `k = 10`\n- **Output:** `6`\n- **Explanation:** The valid subarrays are `[2]`, `[1]`, `[4]`, `[3]`, `[5]`, and `[2,1]`.\n\n## Example 2\n\n- **Input:** `nums = [1,1,1]`, `k = 5`\n- **Output:** `5`\n- **Explanation:** All subarrays except `[1,1,1]` have score less than `5`.\n\n## Constraints\n\n- `1 <= nums.length <= 10^5`\n- `1 <= nums[i] <= 10^5`\n- `1 <= k <= 10^15`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_subarrays(nums: Vec, k: i64) -> i64 {\n \n }\n}\n```\n", "tags": "Hard\nSenior Staff, Array, Binary Search, Sliding Window, Prefix Sum, Biweekly Contest 80\nAcceptance rate: 56.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn subarray_sum(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n decreases end - start,\n {\n if start >= end {\n 0\n } else {\n nums[start] as int + Self::subarray_sum(nums, start + 1, end)\n }\n }\n\n pub open spec fn score(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n {\n Self::subarray_sum(nums, start, end) * (end - start)\n }\n\n pub open spec fn first_valid_start(nums: Seq, k: int, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n 1 <= k,\n decreases end - start,\n {\n if start >= end || Self::score(nums, start, end) < k {\n start\n } else {\n Self::first_valid_start(nums, k, start + 1, end)\n }\n }\n\n pub open spec fn end_count(nums: Seq, k: int, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n 1 <= k,\n {\n end - Self::first_valid_start(nums, k, 0, end)\n }\n\n pub open spec fn count_subarrays_prefix(nums: Seq, k: int, n: int) -> int\n recommends\n 0 <= n <= nums.len(),\n 1 <= k,\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::count_subarrays_prefix(nums, k, n - 1) + Self::end_count(nums, k, n)\n }\n }\n\n pub fn count_subarrays(nums: Vec, k: i64) -> (result: i64)\n requires\n 1 <= nums.len() <= 100_000,\n 1 <= k <= 1_000_000_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result >= 0,\n result as int == Self::count_subarrays_prefix(nums@, k as int, nums.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_subarrays(nums: Vec, k: i64) -> i64 {\n let n = nums.len();\n let mut left = 0usize;\n let mut sum = 0i64;\n let mut answer = 0i64;\n let mut right = 0usize;\n\n while right < n {\n sum += nums[right] as i64;\n\n while left <= right && sum * (right - left + 1) as i64 >= k {\n sum -= nums[left] as i64;\n left += 1;\n }\n\n answer += (right + 1 - left) as i64;\n right += 1;\n }\n\n answer\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn subarray_sum(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n decreases end - start,\n {\n if start >= end {\n 0\n } else {\n nums[start] as int + Self::subarray_sum(nums, start + 1, end)\n }\n }\n\n pub open spec fn score(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n {\n Self::subarray_sum(nums, start, end) * (end - start)\n }\n\n pub open spec fn first_valid_start(nums: Seq, k: int, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n 1 <= k,\n decreases end - start,\n {\n if start >= end || Self::score(nums, start, end) < k {\n start\n } else {\n Self::first_valid_start(nums, k, start + 1, end)\n }\n }\n\n pub open spec fn end_count(nums: Seq, k: int, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n 1 <= k,\n {\n end - Self::first_valid_start(nums, k, 0, end)\n }\n\n pub open spec fn count_subarrays_prefix(nums: Seq, k: int, n: int) -> int\n recommends\n 0 <= n <= nums.len(),\n 1 <= k,\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::count_subarrays_prefix(nums, k, n - 1) + Self::end_count(nums, k, n)\n }\n }\n\n pub fn count_subarrays(nums: Vec, k: i64) -> (result: i64)\n requires\n 1 <= nums.len() <= 100_000,\n 1 <= k <= 1_000_000_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result >= 0,\n result as int == Self::count_subarrays_prefix(nums@, k as int, nums.len() as int),\n {\n let n = nums.len();\n let mut left = 0usize;\n let mut sum = 0i64;\n let mut answer = 0i64;\n let mut right = 0usize;\n\n while right < n {\n sum += nums[right] as i64;\n\n while left <= right && sum * (right - left + 1) as i64 >= k {\n sum -= nums[left] as i64;\n left += 1;\n }\n\n answer += (right + 1 - left) as i64;\n right += 1;\n }\n\n answer\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn subarray_sum(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n decreases end - start,\n {\n if start >= end {\n 0\n } else {\n nums[start] as int + Self::subarray_sum(nums, start + 1, end)\n }\n }\n\n pub open spec fn score(nums: Seq, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n {\n Self::subarray_sum(nums, start, end) * (end - start)\n }\n\n pub open spec fn first_valid_start(nums: Seq, k: int, start: int, end: int) -> int\n recommends\n 0 <= start <= end <= nums.len(),\n 1 <= k,\n decreases end - start,\n {\n if start >= end || Self::score(nums, start, end) < k {\n start\n } else {\n Self::first_valid_start(nums, k, start + 1, end)\n }\n }\n\n pub open spec fn end_count(nums: Seq, k: int, end: int) -> int\n recommends\n 0 <= end <= nums.len(),\n 1 <= k,\n {\n end - Self::first_valid_start(nums, k, 0, end)\n }\n\n pub open spec fn count_subarrays_prefix(nums: Seq, k: int, n: int) -> int\n recommends\n 0 <= n <= nums.len(),\n 1 <= k,\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::count_subarrays_prefix(nums, k, n - 1) + Self::end_count(nums, k, n)\n }\n }\n\n proof fn lemma_subarray_sum_bounds(nums: Seq, start: int, end: int)\n requires\n 0 <= start <= end <= nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n 0 <= Self::subarray_sum(nums, start, end),\n Self::subarray_sum(nums, start, end) <= (end - start) * 100_000,\n decreases end - start,\n {\n reveal_with_fuel(Solution::subarray_sum, 2);\n if start < end {\n Self::lemma_subarray_sum_bounds(nums, start + 1, end);\n assert(0 <= nums[start] as int <= 100_000);\n assert(Self::subarray_sum(nums, start, end)\n == nums[start] as int + Self::subarray_sum(nums, start + 1, end));\n assert(Self::subarray_sum(nums, start, end) <= (end - start) * 100_000) by (nonlinear_arith)\n requires\n Self::subarray_sum(nums, start + 1, end) <= (end - (start + 1)) * 100_000,\n nums[start] as int <= 100_000,\n {};\n }\n }\n\n proof fn lemma_subarray_sum_extend_right(nums: Seq, start: int, end: int)\n requires\n 0 <= start <= end < nums.len(),\n ensures\n Self::subarray_sum(nums, start, end + 1)\n == Self::subarray_sum(nums, start, end) + nums[end] as int,\n decreases end - start,\n {\n reveal_with_fuel(Solution::subarray_sum, 2);\n if start < end {\n Self::lemma_subarray_sum_extend_right(nums, start + 1, end);\n }\n }\n\n proof fn lemma_score_extend_right(nums: Seq, start: int, end: int)\n requires\n 0 <= start < end < nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n Self::score(nums, start, end) < Self::score(nums, start, end + 1),\n decreases end - start,\n {\n Self::lemma_subarray_sum_bounds(nums, start, end);\n Self::lemma_subarray_sum_extend_right(nums, start, end);\n assert(Self::subarray_sum(nums, start, end + 1)\n == Self::subarray_sum(nums, start, end) + nums[end] as int);\n assert(1 <= nums[end] as int);\n assert(Self::subarray_sum(nums, start, end + 1) > Self::subarray_sum(nums, start, end));\n assert(0 <= Self::subarray_sum(nums, start, end));\n assert(0 < end - start);\n assert(Self::score(nums, start, end)\n == Self::subarray_sum(nums, start, end) * (end - start));\n assert(Self::score(nums, start, end + 1)\n == Self::subarray_sum(nums, start, end + 1) * (end + 1 - start));\n assert(Self::score(nums, start, end) < Self::score(nums, start, end + 1)) by (nonlinear_arith)\n requires\n 0 <= Self::subarray_sum(nums, start, end),\n Self::subarray_sum(nums, start, end) < Self::subarray_sum(nums, start, end + 1),\n 0 < end - start,\n end - start < end + 1 - start,\n {};\n }\n\n proof fn lemma_score_shrink_left(nums: Seq, start: int, end: int)\n requires\n 0 <= start < end <= nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n Self::score(nums, start + 1, end) < Self::score(nums, start, end),\n decreases end - start,\n {\n reveal_with_fuel(Solution::subarray_sum, 2);\n Self::lemma_subarray_sum_bounds(nums, start + 1, end);\n assert(Self::subarray_sum(nums, start, end)\n == nums[start] as int + Self::subarray_sum(nums, start + 1, end));\n assert(1 <= nums[start] as int);\n assert(Self::subarray_sum(nums, start + 1, end) < Self::subarray_sum(nums, start, end));\n assert(0 <= Self::subarray_sum(nums, start + 1, end));\n assert(0 <= end - (start + 1) < end - start);\n assert(Self::score(nums, start + 1, end)\n == Self::subarray_sum(nums, start + 1, end) * (end - (start + 1)));\n assert(Self::score(nums, start, end)\n == Self::subarray_sum(nums, start, end) * (end - start));\n if start + 1 == end {\n assert(Self::score(nums, start + 1, end) == 0);\n assert(Self::subarray_sum(nums, start, end) >= 1);\n assert(Self::score(nums, start, end) >= 1) by (nonlinear_arith)\n requires\n Self::subarray_sum(nums, start, end) >= 1,\n end - start >= 1,\n {};\n } else {\n assert(Self::score(nums, start + 1, end) < Self::score(nums, start, end)) by (nonlinear_arith)\n requires\n 0 <= Self::subarray_sum(nums, start + 1, end),\n Self::subarray_sum(nums, start + 1, end) < Self::subarray_sum(nums, start, end),\n 0 <= end - (start + 1),\n end - (start + 1) < end - start,\n {};\n }\n }\n\n proof fn lemma_invalid_mono_left(nums: Seq, k: int, start: int, pivot: int, end: int)\n requires\n 0 <= start <= pivot < end <= nums.len(),\n 1 <= k,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n Self::score(nums, pivot, end) >= k,\n ensures\n Self::score(nums, start, end) >= k,\n decreases pivot - start,\n {\n if start == pivot {\n } else {\n Self::lemma_invalid_mono_left(nums, k, start + 1, pivot, end);\n Self::lemma_score_shrink_left(nums, start, end);\n assert(Self::score(nums, start + 1, end) < Self::score(nums, start, end));\n assert(Self::score(nums, start + 1, end) >= k);\n assert(Self::score(nums, start, end) > Self::score(nums, start + 1, end));\n assert(Self::score(nums, start, end) >= k);\n }\n }\n\n proof fn lemma_first_valid_from_invalid_prefix(nums: Seq, k: int, start: int, left: int, end: int)\n requires\n 0 <= start <= left <= end <= nums.len(),\n 1 <= k,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n forall|s: int| start <= s < left ==> Self::score(nums, s, end) >= k,\n Self::score(nums, left, end) < k,\n ensures\n Self::first_valid_start(nums, k, start, end) == left,\n decreases left - start,\n {\n reveal_with_fuel(Solution::first_valid_start, 2);\n if start < left {\n assert(Self::score(nums, start, end) >= k);\n Self::lemma_first_valid_from_invalid_prefix(nums, k, start + 1, left, end);\n }\n }\n\n pub fn count_subarrays(nums: Vec, k: i64) -> (result: i64)\n requires\n 1 <= nums.len() <= 100_000,\n 1 <= k <= 1_000_000_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result >= 0,\n result as int == Self::count_subarrays_prefix(nums@, k as int, nums.len() as int),\n {\n let n = nums.len();\n let mut left = 0usize;\n let mut sum = 0i64;\n let mut answer = 0i64;\n let mut right = 0usize;\n\n while right < n\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 1 <= k <= 1_000_000_000_000_000,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] nums@[i] <= 100_000,\n 0 <= left <= right <= n,\n sum as int == Self::subarray_sum(nums@, left as int, right as int),\n 0 <= sum as int,\n sum as int <= (right as int - left as int) * 100_000,\n Self::score(nums@, left as int, right as int) < k as int,\n left == 0 || Self::score(nums@, left as int - 1, right as int) >= k as int,\n answer >= 0,\n answer as int == Self::count_subarrays_prefix(nums@, k as int, right as int),\n answer as int <= right as int * (right as int + 1) / 2,\n decreases n - right,\n {\n proof {\n assert((right as int - left as int) * 100_000 <= 10_000_000_000) by (nonlinear_arith)\n requires\n 0 <= right as int - left as int,\n right as int <= 100_000,\n {};\n assert(sum as int + nums@[right as int] as int <= 10_000_100_000) by (nonlinear_arith)\n requires\n sum as int <= (right as int - left as int) * 100_000,\n (right as int - left as int) * 100_000 <= 10_000_000_000,\n nums@[right as int] as int <= 100_000,\n {};\n assert(10_000_100_000 < 9_223_372_036_854_775_807);\n }\n sum += nums[right] as i64;\n\n proof {\n Self::lemma_subarray_sum_extend_right(nums@, left as int, right as int);\n assert(sum as int == Self::subarray_sum(nums@, left as int, right as int + 1));\n Self::lemma_subarray_sum_bounds(nums@, left as int, right as int + 1);\n if left > 0 {\n if left as int - 1 < right as int {\n Self::lemma_score_extend_right(nums@, left as int - 1, right as int);\n assert(Self::score(nums@, left as int - 1, right as int)\n < Self::score(nums@, left as int - 1, right as int + 1));\n assert(Self::score(nums@, left as int - 1, right as int) >= k as int);\n assert(Self::score(nums@, left as int - 1, right as int + 1) >= k as int);\n } else {\n assert(left == right);\n assert(Self::score(nums@, left as int - 1, right as int)\n == nums@[left as int - 1] as int);\n assert(Self::score(nums@, left as int - 1, right as int + 1)\n == (nums@[left as int - 1] as int + nums@[right as int] as int) * 2);\n assert(Self::score(nums@, left as int - 1, right as int + 1)\n >= Self::score(nums@, left as int - 1, right as int));\n assert(Self::score(nums@, left as int - 1, right as int + 1) >= k as int);\n }\n }\n }\n\n proof {\n assert(sum as int * ((right as int + 1) - left as int)\n <= 1_000_000_000_000_000) by (nonlinear_arith)\n requires\n 0 <= sum as int,\n sum as int <= ((right as int + 1) - left as int) * 100_000,\n 0 <= (right as int + 1) - left as int,\n (right as int + 1) - left as int <= 100_000,\n {};\n }\n\n while left <= right && sum * (right - left + 1) as i64 >= k\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 1 <= k <= 1_000_000_000_000_000,\n forall|i: int| 0 <= i < n ==> 1 <= #[trigger] nums@[i] <= 100_000,\n right < n,\n 0 <= left <= right + 1,\n sum as int == Self::subarray_sum(nums@, left as int, right as int + 1),\n 0 <= sum as int,\n sum as int <= ((right as int + 1) - left as int) * 100_000,\n sum as int * ((right as int + 1) - left as int) <= 1_000_000_000_000_000,\n left == 0 || Self::score(nums@, left as int - 1, right as int + 1) >= k as int,\n answer >= 0,\n answer as int == Self::count_subarrays_prefix(nums@, k as int, right as int),\n answer as int <= right as int * (right as int + 1) / 2,\n decreases right + 1 - left,\n {\n proof {\n reveal_with_fuel(Solution::subarray_sum, 2);\n Self::lemma_subarray_sum_bounds(nums@, left as int + 1, right as int + 1);\n assert(sum as int\n == nums@[left as int] as int + Self::subarray_sum(nums@, left as int + 1, right as int + 1));\n assert(sum as int >= nums@[left as int] as int);\n \n assert(sum as int * ((right as int + 1) - left as int) >= k as int);\n }\n let ghost old_left = left;\n let ghost old_sum = sum as int;\n let ghost old_product = old_sum * ((right as int + 1) - old_left as int);\n sum -= nums[left] as i64;\n left += 1;\n proof {\n assert(old_sum == Self::subarray_sum(nums@, old_left as int, right as int + 1));\n assert(sum as int == Self::subarray_sum(nums@, left as int, right as int + 1));\n Self::lemma_subarray_sum_bounds(nums@, left as int, right as int + 1);\n assert(old_product >= k as int);\n assert(Self::score(nums@, old_left as int, right as int + 1) >= k as int);\n assert(left as int - 1 == old_left as int);\n assert(Self::score(nums@, left as int - 1, right as int + 1) >= k as int);\n \n assert(sum as int * ((right as int + 1) - left as int) <= old_product) by (nonlinear_arith)\n requires\n 0 <= sum as int,\n sum as int <= old_sum,\n 0 <= (right as int + 1) - left as int,\n (right as int + 1) - left as int <= (right as int + 1) - old_left as int,\n old_product == old_sum * ((right as int + 1) - old_left as int),\n {};\n }\n }\n\n proof {\n if left <= right {\n assert(sum as int * ((right as int + 1) - left as int) < k as int);\n assert(Self::score(nums@, left as int, right as int + 1)\n == sum as int * ((right as int + 1) - left as int));\n assert(Self::score(nums@, left as int, right as int + 1) < k as int);\n } else {\n assert(left == right + 1);\n assert(sum as int == Self::subarray_sum(nums@, left as int, right as int + 1));\n assert(Self::score(nums@, left as int, right as int + 1) == 0);\n assert(0 < k as int);\n assert(Self::score(nums@, left as int, right as int + 1) < k as int);\n }\n\n assert forall|s: int| 0 <= s < left as int implies #[trigger] Self::score(nums@, s, right as int + 1) >= k as int by {\n if left > 0 {\n Self::lemma_invalid_mono_left(nums@, k as int, s, left as int - 1, right as int + 1);\n }\n };\n\n Self::lemma_first_valid_from_invalid_prefix(\n nums@,\n k as int,\n 0,\n left as int,\n right as int + 1,\n );\n assert(Self::first_valid_start(nums@, k as int, 0, right as int + 1) == left as int);\n reveal(Solution::end_count);\n assert(Self::end_count(nums@, k as int, right as int + 1)\n == (right as int + 1) - left as int);\n reveal_with_fuel(Solution::count_subarrays_prefix, 2);\n assert(Self::count_subarrays_prefix(nums@, k as int, right as int + 1)\n == Self::count_subarrays_prefix(nums@, k as int, right as int)\n + Self::end_count(nums@, k as int, right as int + 1));\n assert(answer as int + ((right as int + 1) - left as int)\n == Self::count_subarrays_prefix(nums@, k as int, right as int + 1));\n\n assert((right as int + 1) - left as int <= right as int + 1) by (nonlinear_arith) {};\n assert(answer as int + ((right as int + 1) - left as int)\n <= (right as int + 1) * (right as int + 2) / 2) by (nonlinear_arith)\n requires\n answer as int <= right as int * (right as int + 1) / 2,\n (right as int + 1) - left as int <= right as int + 1,\n {};\n assert((right as int + 1) * (right as int + 2) / 2 <= 5_000_050_000) by (nonlinear_arith)\n requires\n 0 <= right as int,\n right as int + 1 <= 100_000,\n {};\n assert(answer as int + ((right as int + 1) - left as int) <= 9_223_372_036_854_775_807int) by (nonlinear_arith)\n requires\n answer as int + ((right as int + 1) - left as int) <= 5_000_050_000,\n {};\n }\n\n answer += (right + 1 - left) as i64;\n right += 1;\n }\n\n answer\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2302/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2302/tests/mutated_testcases.jsonl"} {"problem": "lc2319", "family": "leetcode", "description": "# Check if Matrix Is X-Matrix\n\nA square matrix is said to be an **X-Matrix** if **both** of the following conditions hold:\n\n\t- All the elements in the diagonals of the matrix are **non-zero**.\n\n\t- All other elements are 0.\n\nGiven a 2D integer array `grid` of size `n x n` representing a square matrix, return `true`* if *`grid`* is an X-Matrix*. Otherwise, return `false`.\n\n \n\nExample 1:\n\n```\n**Input:** grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]\n**Output:** true\n**Explanation:** Refer to the diagram above. \nAn X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.\nThus, grid is an X-Matrix.\n```\n\nExample 2:\n\n```\n**Input:** grid = [[5,7,0],[0,3,1],[0,5,0]]\n**Output:** false\n**Explanation:** Refer to the diagram above.\nAn X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.\nThus, grid is not an X-Matrix.\n```\n\n \n\n**Constraints:**\n\n\t- `n == grid.length == grid[i].length`\n\n\t- `3 <= n <= 100`\n\n\t- `0 <= grid[i][j] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn check_x_matrix(grid: Vec>) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray, Matrix\nAcceptance rate: 66.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_x_cell(i: int, j: int, n: int) -> bool {\n i == j || i + j == n - 1\n }\n\n pub open spec fn is_x_matrix(g: Seq>) -> bool {\n let n = g.len() as int;\n forall |i: int, j: int|\n 0 <= i < n && 0 <= j < n ==>\n if Self::is_x_cell(i, j, n) {\n #[trigger] g[i][j] != 0\n } else {\n #[trigger] g[i][j] == 0\n }\n }\n\n pub fn check_x_matrix(grid: Vec>) -> (result: bool)\n requires\n 3 <= grid.len() <= 100,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid.len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid.len() ==>\n 0 <= #[trigger] grid[i][j] <= 100000,\n ensures\n result == Self::is_x_matrix(grid@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn check_x_matrix(grid: Vec>) -> bool {\n let n = grid.len();\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = 0;\n while j < n {\n if i == j || i + j == n - 1 {\n if grid[i][j] == 0 {\n return false;\n }\n } else {\n if grid[i][j] != 0 {\n return false;\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_x_cell(i: int, j: int, n: int) -> bool {\n i == j || i + j == n - 1\n }\n\n pub open spec fn is_x_matrix(g: Seq>) -> bool {\n let n = g.len() as int;\n forall |i: int, j: int|\n 0 <= i < n && 0 <= j < n ==>\n if Self::is_x_cell(i, j, n) {\n #[trigger] g[i][j] != 0\n } else {\n #[trigger] g[i][j] == 0\n }\n }\n\n pub fn check_x_matrix(grid: Vec>) -> (result: bool)\n requires\n 3 <= grid.len() <= 100,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid.len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid.len() ==>\n 0 <= #[trigger] grid[i][j] <= 100000,\n ensures\n result == Self::is_x_matrix(grid@),\n {\n let n = grid.len();\n let mut i: usize = 0;\n while i < n {\n let mut j: usize = 0;\n while j < n {\n if i == j || i + j == n - 1 {\n if grid[i][j] == 0 {\n return false;\n }\n } else {\n if grid[i][j] != 0 {\n return false;\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_x_cell(i: int, j: int, n: int) -> bool {\n i == j || i + j == n - 1\n }\n\n pub open spec fn is_x_matrix(g: Seq>) -> bool {\n let n = g.len() as int;\n forall |i: int, j: int|\n 0 <= i < n && 0 <= j < n ==>\n if Self::is_x_cell(i, j, n) {\n #[trigger] g[i][j] != 0\n } else {\n #[trigger] g[i][j] == 0\n }\n }\n\n pub fn check_x_matrix(grid: Vec>) -> (result: bool)\n requires\n 3 <= grid.len() <= 100,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid.len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid.len() ==>\n 0 <= #[trigger] grid[i][j] <= 100000,\n ensures\n result == Self::is_x_matrix(grid@),\n {\n let n = grid.len();\n let mut i: usize = 0;\n while i < n\n invariant\n n == grid.len(),\n 1 <= n <= 100,\n 0 <= i <= n,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid.len() ==>\n 0 <= #[trigger] grid[r][c] <= 100000,\n forall |r: int, c: int| 0 <= r < i as int && 0 <= c < n as int ==>\n if Self::is_x_cell(r, c, n as int) {\n grid@[r][c] != 0\n } else {\n grid@[r][c] == 0\n },\n decreases n - i,\n {\n let mut j: usize = 0;\n while j < n\n invariant\n n == grid.len(),\n 1 <= n <= 100,\n 0 <= i < n,\n 0 <= j <= n,\n grid[i as int].len() == n,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid.len() ==>\n 0 <= #[trigger] grid[r][c] <= 100000,\n forall |r: int, c: int| 0 <= r < i as int && 0 <= c < n as int ==>\n if Self::is_x_cell(r, c, n as int) {\n grid@[r][c] != 0\n } else {\n grid@[r][c] == 0\n },\n forall |c: int| 0 <= c < j as int ==>\n if Self::is_x_cell(i as int, c, n as int) {\n grid@[i as int][c] != 0\n } else {\n grid@[i as int][c] == 0\n },\n decreases n - j,\n {\n assert(grid[i as int].len() == n);\n if i == j || i + j == n - 1 {\n if grid[i][j] == 0 {\n proof {\n assert(Self::is_x_cell(i as int, j as int, n as int));\n assert(grid@[i as int][j as int] == 0);\n assert(!Self::is_x_matrix(grid@));\n }\n return false;\n }\n } else {\n if grid[i][j] != 0 {\n proof {\n assert(!Self::is_x_cell(i as int, j as int, n as int));\n assert(grid@[i as int][j as int] != 0);\n assert(!Self::is_x_matrix(grid@));\n }\n return false;\n }\n }\n j = j + 1;\n }\n i = i + 1;\n }\n proof {\n assert forall |r: int, c: int| 0 <= r < n as int && 0 <= c < n as int implies\n if Self::is_x_cell(r, c, n as int) {\n #[trigger] grid@[r][c] != 0\n } else {\n #[trigger] grid@[r][c] == 0\n } by {\n assert(0 <= r < i as int);\n }\n assert(Self::is_x_matrix(grid@));\n }\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2319/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2319/tests/mutated_testcases.jsonl"} {"problem": "lc238", "family": "leetcode", "description": "# Product of Array Except Self\n\nGiven an integer array `nums`, return *an array `answer` such that `answer[i]` is equal to the product of all the elements of `nums` except `nums[i]`*.\n\nThe product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.\n\nYou must write an algorithm that runs in `O(n)` time and without using the division operation.\n\n## Example 1:\n> **Input:** nums = [1,2,3,4]\n> **Output:** [24,12,8,6]\n\n\n## Example 2:\n> **Input:** nums = [-1,1,0,-3,3]\n> **Output:** [0,0,9,0,0]\n\n## Constraints:\n\n- $2 <= nums.length <= 10^5$\n\n- $-30 <= nums.length <= 30$\n\n- The input is generated such that `answer[i]` is **guaranteed** to fit in a **32-bit** integer.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn product_except_self(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Prefix Sum\nAcceptance rate: 68.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn product_of_range(nums: Seq, start: int, end: int) -> int\n decreases end - start\n {\n if start >= end {\n 1\n } else {\n nums[start] as int * Self::product_of_range(nums, start + 1, end)\n }\n }\n\n pub fn product_except_self(nums: Vec) -> (res: Vec)\n requires \n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n ensures\n res.len() == nums.len(),\n forall |i: int| 0 <= i < res.len() ==> \n res[i] as int == Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int)\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn product_except_self(nums: Vec) -> Vec\n {\n let n = nums.len();\n \n let mut pre = Vec::with_capacity(n);\n for i in 0..n \n {\n pre.push(nums[i]);\n }\n \n let mut suf = Vec::with_capacity(n);\n for i in 0..n \n {\n suf.push(nums[i]);\n }\n\n for i in 1..n \n {\n pre[i] = pre[i] * pre[i - 1];\n suf[n - 1 - i] = suf[n - 1 - i] * suf[n - i];\n }\n\n pre.insert(0, 1);\n suf.push(1);\n\n let mut res = Vec::with_capacity(n);\n for i in 0..n \n {\n res.push(pre[i] * suf[i + 1]);\n }\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn product_of_range(nums: Seq, start: int, end: int) -> int\n decreases end - start\n {\n if start >= end {\n 1\n } else {\n nums[start] as int * Self::product_of_range(nums, start + 1, end)\n }\n }\n\n pub fn product_except_self(nums: Vec) -> (res: Vec)\n requires \n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n ensures\n res.len() == nums.len(),\n forall |i: int| 0 <= i < res.len() ==> \n res[i] as int == Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int)\n {\n let n = nums.len();\n \n let mut pre = Vec::with_capacity(n);\n for i in 0..n \n {\n pre.push(nums[i]);\n }\n \n let mut suf = Vec::with_capacity(n);\n for i in 0..n \n {\n suf.push(nums[i]);\n }\n\n for i in 1..n \n {\n pre[i] = pre[i] * pre[i - 1];\n suf[n - 1 - i] = suf[n - 1 - i] * suf[n - i];\n }\n\n pre.insert(0, 1);\n suf.push(1);\n\n let mut res = Vec::with_capacity(n);\n for i in 0..n \n {\n res.push(pre[i] * suf[i + 1]);\n }\n res\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn product_of_range(nums: Seq, start: int, end: int) -> int\n decreases end - start\n {\n if start >= end {\n 1\n } else {\n nums[start] as int * Self::product_of_range(nums, start + 1, end)\n }\n }\n\n proof fn lemma_product_split(nums: Seq, start: int, mid: int, end: int)\n requires\n 0 <= start <= mid <= end <= nums.len(),\n ensures\n Self::product_of_range(nums, start, end) == \n Self::product_of_range(nums, start, mid) * Self::product_of_range(nums, mid, end),\n decreases end - start\n {\n if start >= end {\n } else if start == mid {\n assert(Self::product_of_range(nums, start, mid) == 1);\n } else if mid == end {\n assert(Self::product_of_range(nums, mid, end) == 1);\n } else {\n Self::lemma_product_split(nums, start + 1, mid, end);\n \n let a = nums[start] as int;\n let b = Self::product_of_range(nums, start + 1, mid);\n let c = Self::product_of_range(nums, mid, end);\n \n assert(a * (b * c) == (a * b) * c) by(nonlinear_arith);\n }\n }\n\n proof fn lemma_product_single(nums: Seq, i: int)\n requires\n 0 <= i < nums.len(),\n ensures\n Self::product_of_range(nums, i, i + 1) == nums[i] as int,\n {\n assert(Self::product_of_range(nums, i, i + 1) == nums[i] as int * Self::product_of_range(nums, i + 1, i + 1));\n assert(Self::product_of_range(nums, i, i + 1) == nums[i] as int * 1);\n }\n\n pub fn product_except_self(nums: Vec) -> (res: Vec)\n requires \n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n ensures\n res.len() == nums.len(),\n forall |i: int| 0 <= i < res.len() ==> \n res[i] as int == Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int)\n {\n let n = nums.len();\n \n let mut pre = Vec::with_capacity(n);\n for i in 0..n \n invariant\n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n n == nums.len(), \n pre.len() == i,\n forall |j: int| 0 <= j < i ==> pre[j] == nums[j],\n {\n pre.push(nums[i]);\n }\n \n let mut suf = Vec::with_capacity(n);\n for i in 0..n \n invariant\n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n n == nums.len(), \n suf.len() == i,\n forall |j: int| 0 <= j < i ==> suf[j] == nums[j],\n {\n suf.push(nums[i]);\n }\n\n proof {\n Self::lemma_product_single(nums@, 0);\n Self::lemma_product_single(nums@, n as int - 1);\n }\n\n for i in 1..n \n invariant\n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n n == nums.len(), \n pre.len() == n,\n suf.len() == n,\n forall |j: int| 0 <= j < i ==> \n pre[j] as int == Self::product_of_range(nums@, 0, j + 1),\n forall |j: int| i <= j < n ==> pre[j] == nums[j],\n forall |j: int| 0 <= j < i ==> \n #[trigger] suf[n - 1 - j] as int == Self::product_of_range(nums@, n - 1 - j, n as int),\n forall |j: int| 0 <= j < n - i ==> suf[j] == nums[j],\n {\n proof {\n assert(pre@[i - 1] as int == Self::product_of_range(nums@, 0, i as int));\n \n Self::lemma_product_split(nums@, 0, i as int, i + 1);\n Self::lemma_product_single(nums@, i as int);\n\n assert(suf@[n - 1 - (i - 1)] as int == Self::product_of_range(nums@, n - 1 - (i - 1), n as int));\n \n Self::lemma_product_split(nums@, n - 1 - i, n - i, n as int);\n Self::lemma_product_single(nums@, n - 1 - i);\n }\n \n pre[i] = pre[i] * pre[i - 1];\n suf[n - 1 - i] = suf[n - 1 - i] * suf[n - i];\n }\n\n pre.insert(0, 1);\n suf.push(1);\n\n proof {\n assert forall |k: int| 0 <= k < n implies \n suf@[k] as int == Self::product_of_range(nums@, k, n as int) by {\n let j = n - 1 - k;\n assert(suf@[n - 1 - j] as int == Self::product_of_range(nums@, n - 1 - j, n as int));\n }\n assert forall |k: int| 0 <= k < n implies \n suf@[k] as int == Self::product_of_range(nums@, k, n as int) by {\n assert(suf@[k] as int == Self::product_of_range(nums@, k, n as int));\n }\n }\n\n let mut res = Vec::with_capacity(n);\n for i in 0..n \n invariant\n 2 <= nums.len() <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> -30 <= #[trigger] nums[i] <= 30, \n forall |i: int| 0 <= i < nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) * \n Self::product_of_range(nums@, i + 1, nums@.len() as int) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, 0, i) <= i32::MAX,\n forall |i: int| 0 <= i <= nums.len() ==> \n i32::MIN <= #[trigger] Self::product_of_range(nums@, i, nums@.len() as int) <= i32::MAX,\n n == nums.len(), \n pre.len() == n + 1,\n suf.len() == n + 1,\n res.len() == i,\n pre[0] == 1,\n suf[n as int] == 1,\n forall |j: int| 0 < j <= n ==> \n pre[j] as int == Self::product_of_range(nums@, 0, j),\n forall |j: int| 0 <= j < n ==> \n suf[j] as int == Self::product_of_range(nums@, j, n as int),\n forall |j: int| 0 <= j < i ==> \n res[j] as int == Self::product_of_range(nums@, 0, j) * \n Self::product_of_range(nums@, j + 1, n as int),\n {\n res.push(pre[i] * suf[i + 1]);\n }\n res\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc238/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc238/tests/mutated_testcases.jsonl"} {"problem": "lc2413", "family": "leetcode", "description": "# Smallest Even Multiple\n\nGiven a positive integer `n`, return the smallest positive integer that is a multiple of both `2` and `n`.\n\n## Example 1:\n\n> **Input:** n = 5\n> **Output:** 10\n> **Explanation:** The smallest multiple of both 5 and 2 is 10.\n\n## Example 2:\n\n> **Input:** n = 6\n> **Output:** 6\n> **Explanation:** The smallest multiple of both 6 and 2 is 6.\n\n## Constraints:\n\n- $1 <= n <= 150$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn smallest_even_multiple(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Math, Number Theory, Weekly Contest 311\nAcceptance rate: 88.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn smallest_even_multiple(n: i32) -> (result: i32)\n requires\n 1 <= n <= 150,\n ensures\n result > 0,\n result as int % 2 == 0,\n result as int % (n as int) == 0,\n forall|m: int| m > 0 && m % 2 == 0 && #[trigger] (m % (n as int)) == 0 ==> result as int <= m,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn smallest_even_multiple(n: i32) -> i32 {\n if n % 2 == 0 {\n n\n } else {\n n * 2\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn smallest_even_multiple(n: i32) -> (result: i32)\n requires\n 1 <= n <= 150,\n ensures\n result > 0,\n result as int % 2 == 0,\n result as int % (n as int) == 0,\n forall|m: int| m > 0 && m % 2 == 0 && #[trigger] (m % (n as int)) == 0 ==> result as int <= m,\n {\n if n % 2 == 0 {\n n\n } else {\n n * 2\n }\n }\n}\n\n}\n", "verified": "use vstd::arithmetic::div_mod::lemma_fundamental_div_mod;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn smallest_even_multiple(n: i32) -> (result: i32)\n requires\n 1 <= n <= 150,\n ensures\n result > 0,\n result as int % 2 == 0,\n result as int % (n as int) == 0,\n forall|m: int| m > 0 && m % 2 == 0 && #[trigger] (m % (n as int)) == 0 ==> result as int <= m,\n {\n if n % 2 == 0 {\n proof {\n assert((n as int) % 2 == 0);\n assert((n as int) % (n as int) == 0);\n assert forall|m: int| m > 0 && m % 2 == 0 && #[trigger] (m % (n as int)) == 0 implies n as int <= m by {\n let q = m / (n as int);\n lemma_fundamental_div_mod(m, n as int);\n assert(m == q * (n as int) + m % (n as int));\n assert(m == q * (n as int));\n assert(q > 0) by (nonlinear_arith)\n requires\n m == q * (n as int),\n m > 0,\n n > 0,\n {\n }\n assert((n as int) <= m) by (nonlinear_arith)\n requires\n q > 0,\n m == q * (n as int),\n n > 0,\n {\n }\n }\n }\n n\n } else {\n proof {\n lemma_fundamental_div_mod(n as int, 2);\n assert(0 <= (n as int) % 2 < 2);\n assert((n as int) % 2 == 1);\n assert((2 * (n as int)) % 2 == 0);\n assert((2 * (n as int)) % (n as int) == 0) by (nonlinear_arith)\n requires\n n > 0,\n {\n }\n assert forall|m: int| m > 0 && m % 2 == 0 && #[trigger] (m % (n as int)) == 0 implies 2 * (n as int) <= m by {\n let q = m / (n as int);\n lemma_fundamental_div_mod(m, n as int);\n assert(m == q * (n as int) + m % (n as int));\n assert(m == q * (n as int)) by {\n assert(m % (n as int) == 0);\n }\n assert(q > 0) by (nonlinear_arith)\n requires\n m == q * (n as int),\n m > 0,\n n > 0,\n {\n }\n if q == 1 {\n assert(m == n as int) by (nonlinear_arith)\n requires\n m == q * (n as int),\n q == 1,\n {\n }\n assert(m % 2 == 0);\n assert((n as int) % 2 == m % 2);\n assert(false);\n }\n assert(q >= 2) by (nonlinear_arith)\n requires\n q > 0,\n q != 1,\n {\n }\n assert(2 * (n as int) <= m) by (nonlinear_arith)\n requires\n q >= 2,\n m == q * (n as int),\n n > 0,\n {\n }\n }\n }\n n * 2\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2413/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2413/tests/mutated_testcases.jsonl"} {"problem": "lc242", "family": "leetcode", "description": "# Valid Anagram\n\nGiven two strings `s` and `t`, return `true` if `t` is an anagram of `s`, and `false` otherwise.\n\n## Example 1:\n\n> **Input:** s = \"anagram\", t = \"nagaram\"\n> **Output:** true\n\n## Example 2:\n\n> **Input:** s = \"rat\", t = \"car\"\n> **Output:** false\n\n## Constraints:\n\n- $1 \\leq \\texttt{s.length}, \\texttt{t.length} \\leq 5 \\times 10^4$\n- `s` and `t` consist of lowercase English letters.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_anagram(s: String, t: String) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nHash Table, String, Sorting\nAcceptance rate: 67.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_char(s: Seq, c: char) -> int\n decreases s.len()\n{\n if s.len() == 0 {\n 0\n } else if s.last() == c {\n count_char(s.drop_last(), c) + 1\n } else {\n count_char(s.drop_last(), c)\n }\n}\n\npub open spec fn is_lowercase_word(s: Seq) -> bool {\n forall |i: int| 0 <= i < s.len() ==> 97 <= (#[trigger] s[i] as u32) && (s[i] as u32) <= 122\n}\n\npub open spec fn letter(j: int) -> char {\n (j + 97) as u8 as char\n}\n\npub open spec fn is_anagram_spec(s: Seq, t: Seq) -> bool {\n s.len() == t.len() && forall |j: int| 0 <= j < 26 ==>\n #[trigger] count_char(s, letter(j)) == count_char(t, letter(j))\n}\n\nimpl Solution {\n pub fn is_anagram(s: String, t: String) -> (res: bool)\n requires\n 1 <= s@.len() <= 50_000,\n 1 <= t@.len() <= 50_000,\n is_lowercase_word(s@),\n is_lowercase_word(t@),\n ensures\n res == is_anagram_spec(s@, t@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_anagram(s: String, t: String) -> bool {\n let s_len = s.as_str().unicode_len();\n let t_len = t.as_str().unicode_len();\n if s_len != t_len {\n return false;\n }\n let mut cnt: Vec = Vec::new();\n let mut i: usize = 0;\n while i < 26 {\n cnt.push(0);\n i += 1;\n }\n i = 0;\n while i < s_len {\n let c = s.as_str().get_char(i);\n let idx = (c as u32 - 97) as usize;\n cnt[idx] = cnt[idx] + 1;\n i += 1;\n }\n i = 0;\n while i < t_len {\n let c = t.as_str().get_char(i);\n let idx = (c as u32 - 97) as usize;\n cnt[idx] = cnt[idx] - 1;\n i += 1;\n }\n let mut k: usize = 0;\n let mut all_zero = true;\n while k < 26 {\n if cnt[k] != 0 {\n all_zero = false;\n }\n k += 1;\n }\n all_zero\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_char(s: Seq, c: char) -> int\n decreases s.len()\n{\n if s.len() == 0 {\n 0\n } else if s.last() == c {\n count_char(s.drop_last(), c) + 1\n } else {\n count_char(s.drop_last(), c)\n }\n}\n\npub open spec fn is_lowercase_word(s: Seq) -> bool {\n forall |i: int| 0 <= i < s.len() ==> 97 <= (#[trigger] s[i] as u32) && (s[i] as u32) <= 122\n}\n\npub open spec fn letter(j: int) -> char {\n (j + 97) as u8 as char\n}\n\npub open spec fn is_anagram_spec(s: Seq, t: Seq) -> bool {\n s.len() == t.len() && forall |j: int| 0 <= j < 26 ==>\n #[trigger] count_char(s, letter(j)) == count_char(t, letter(j))\n}\n\nimpl Solution {\n pub fn is_anagram(s: String, t: String) -> (res: bool)\n requires\n 1 <= s@.len() <= 50_000,\n 1 <= t@.len() <= 50_000,\n is_lowercase_word(s@),\n is_lowercase_word(t@),\n ensures\n res == is_anagram_spec(s@, t@),\n {\n let s_len = s.as_str().unicode_len();\n let t_len = t.as_str().unicode_len();\n if s_len != t_len {\n return false;\n }\n let mut cnt: Vec = Vec::new();\n let mut i: usize = 0;\n while i < 26 {\n cnt.push(0);\n i += 1;\n }\n i = 0;\n while i < s_len {\n let c = s.as_str().get_char(i);\n let idx = (c as u32 - 97) as usize;\n cnt.set(idx, cnt[idx] + 1);\n i += 1;\n }\n i = 0;\n while i < t_len {\n let c = t.as_str().get_char(i);\n let idx = (c as u32 - 97) as usize;\n cnt.set(idx, cnt[idx] - 1);\n i += 1;\n }\n let mut k: usize = 0;\n let mut all_zero = true;\n while k < 26 {\n if cnt[k] != 0 {\n all_zero = false;\n }\n k += 1;\n }\n all_zero\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_char(s: Seq, c: char) -> int\n decreases s.len()\n{\n if s.len() == 0 {\n 0\n } else if s.last() == c {\n count_char(s.drop_last(), c) + 1\n } else {\n count_char(s.drop_last(), c)\n }\n}\n\npub open spec fn is_lowercase_word(s: Seq) -> bool {\n forall |i: int| 0 <= i < s.len() ==> 97 <= (#[trigger] s[i] as u32) && (s[i] as u32) <= 122\n}\n\npub open spec fn letter(j: int) -> char {\n (j + 97) as u8 as char\n}\n\npub open spec fn is_anagram_spec(s: Seq, t: Seq) -> bool {\n s.len() == t.len() && forall |j: int| 0 <= j < 26 ==>\n #[trigger] count_char(s, letter(j)) == count_char(t, letter(j))\n}\n\nproof fn lemma_count_char_bounds(s: Seq, c: char)\n ensures count_char(s, c) <= s.len(),\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_count_char_bounds(s.drop_last(), c);\n }\n}\n\nproof fn lemma_count_char_nonneg(s: Seq, c: char)\n ensures count_char(s, c) >= 0,\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_count_char_nonneg(s.drop_last(), c);\n }\n}\n\nproof fn lemma_anagram_one(s: Seq, t: Seq, j: int, cnt_j: i32)\n requires\n 0 <= j < 26,\n cnt_j == 0,\n cnt_j as int == count_char(s, letter(j)) - count_char(t, letter(j)),\n ensures\n count_char(s, letter(j)) == count_char(t, letter(j)),\n{\n}\n\nimpl Solution {\n pub fn is_anagram(s: String, t: String) -> (res: bool)\n requires\n 1 <= s@.len() <= 50_000,\n 1 <= t@.len() <= 50_000,\n is_lowercase_word(s@),\n is_lowercase_word(t@),\n ensures\n res == is_anagram_spec(s@, t@),\n {\n let s_len = s.as_str().unicode_len();\n let t_len = t.as_str().unicode_len();\n if s_len != t_len {\n proof {\n assert(!is_anagram_spec(s@, t@));\n }\n return false;\n }\n let mut cnt: Vec = Vec::new();\n let mut i: usize = 0;\n while i < 26\n invariant\n 0 <= i <= 26,\n cnt.len() == i,\n forall |k: int| 0 <= k < i ==> #[trigger] cnt[k] == 0,\n decreases 26 - i\n {\n cnt.push(0);\n i += 1;\n }\n i = 0;\n while i < s_len\n invariant\n is_lowercase_word(s@),\n 0 <= i <= s_len,\n s_len <= 50_000,\n s_len as int == s@.len(),\n cnt.len() == 26,\n forall |k: int| 0 <= k < 26 ==> #[trigger] cnt[k] == count_char(s@.take(i as int), letter(k)),\n forall |k: int| 0 <= k < 26 ==> 0 <= #[trigger] cnt[k] <= i as int,\n forall |k: int| 0 <= k < 26 ==> cnt[k] <= 50_000,\n decreases s_len - i\n {\n let c = s.as_str().get_char(i);\n proof {\n assert(s@.index(i as int) == c);\n assert(97 <= c as u32 && c as u32 <= 122);\n }\n let idx = (c as u32 - 97) as usize;\n proof {\n assert(idx < 26);\n let s_take_i = s@.take(i as int);\n let s_take_i1 = s@.take((i + 1) as int);\n assert(s_take_i1.drop_last() =~= s_take_i);\n assert(s_take_i1.last() == c);\n assert(count_char(s_take_i1, letter(idx as int)) == count_char(s_take_i, letter(idx as int)) + 1);\n assert(forall |k: int| 0 <= k < 26 && k != idx ==>\n #[trigger] count_char(s_take_i1, letter(k)) == count_char(s_take_i, letter(k)));\n }\n cnt.set(idx, cnt[idx] + 1);\n i += 1;\n }\n proof {\n assert(s@.take(s_len as int) =~= s@);\n }\n i = 0;\n while i < t_len\n invariant\n is_lowercase_word(s@),\n is_lowercase_word(t@),\n s_len == t_len,\n 0 <= i <= t_len,\n t_len <= 50_000,\n t_len as int == t@.len(),\n cnt.len() == 26,\n forall |k: int| 0 <= k < 26 ==> #[trigger] cnt[k] == count_char(s@, letter(k)) - count_char(t@.take(i as int), letter(k)),\n decreases t_len - i\n {\n let c = t.as_str().get_char(i);\n proof {\n assert(t@.index(i as int) == c);\n assert(97 <= c as u32 && c as u32 <= 122);\n }\n let idx = (c as u32 - 97) as usize;\n proof {\n assert(idx < 26);\n lemma_count_char_nonneg(s@, letter(idx as int));\n lemma_count_char_bounds(t@.take(i as int), letter(idx as int));\n assert(count_char(t@.take(i as int), letter(idx as int)) <= i as int);\n assert(cnt[idx as int] == count_char(s@, letter(idx as int)) - count_char(t@.take(i as int), letter(idx as int)));\n assert(cnt[idx as int] >= -(i as int));\n assert(i <= 50_000);\n assert(cnt[idx as int] >= -50_000);\n }\n proof {\n let t_take_i = t@.take(i as int);\n let t_take_i1 = t@.take((i + 1) as int);\n assert(t_take_i1.drop_last() =~= t_take_i);\n assert(t_take_i1.last() == c);\n assert(count_char(t_take_i1, letter(idx as int)) == count_char(t_take_i, letter(idx as int)) + 1);\n assert(forall |k: int| 0 <= k < 26 && k != idx ==>\n #[trigger] count_char(t_take_i1, letter(k)) == count_char(t_take_i, letter(k)));\n }\n cnt.set(idx, cnt[idx] - 1);\n i += 1;\n }\n proof {\n assert(t@.take(t_len as int) =~= t@);\n }\n let mut k: usize = 0;\n let mut all_zero = true;\n while k < 26\n invariant\n s_len == t_len,\n s_len as int == s@.len(),\n t_len as int == t@.len(),\n 0 <= k <= 26,\n cnt.len() == 26,\n forall |j: int| 0 <= j < 26 ==> cnt[j] == count_char(s@, letter(j)) - count_char(t@, letter(j)),\n all_zero == (forall |j: int| 0 <= j < k as int ==> cnt[j] == 0),\n decreases 26 - k\n {\n if cnt[k] != 0 {\n all_zero = false;\n }\n k += 1;\n }\n proof {\n assert(s@.len() == t_len as int);\n assert(t@.len() == t_len as int);\n assert(s_len == t_len);\n if all_zero {\n lemma_anagram_one(s@, t@, 0, cnt[0]);\n lemma_anagram_one(s@, t@, 1, cnt[1]);\n lemma_anagram_one(s@, t@, 2, cnt[2]);\n lemma_anagram_one(s@, t@, 3, cnt[3]);\n lemma_anagram_one(s@, t@, 4, cnt[4]);\n lemma_anagram_one(s@, t@, 5, cnt[5]);\n lemma_anagram_one(s@, t@, 6, cnt[6]);\n lemma_anagram_one(s@, t@, 7, cnt[7]);\n lemma_anagram_one(s@, t@, 8, cnt[8]);\n lemma_anagram_one(s@, t@, 9, cnt[9]);\n lemma_anagram_one(s@, t@, 10, cnt[10]);\n lemma_anagram_one(s@, t@, 11, cnt[11]);\n lemma_anagram_one(s@, t@, 12, cnt[12]);\n lemma_anagram_one(s@, t@, 13, cnt[13]);\n lemma_anagram_one(s@, t@, 14, cnt[14]);\n lemma_anagram_one(s@, t@, 15, cnt[15]);\n lemma_anagram_one(s@, t@, 16, cnt[16]);\n lemma_anagram_one(s@, t@, 17, cnt[17]);\n lemma_anagram_one(s@, t@, 18, cnt[18]);\n lemma_anagram_one(s@, t@, 19, cnt[19]);\n lemma_anagram_one(s@, t@, 20, cnt[20]);\n lemma_anagram_one(s@, t@, 21, cnt[21]);\n lemma_anagram_one(s@, t@, 22, cnt[22]);\n lemma_anagram_one(s@, t@, 23, cnt[23]);\n lemma_anagram_one(s@, t@, 24, cnt[24]);\n lemma_anagram_one(s@, t@, 25, cnt[25]);\n assert(forall |j: int| 0 <= j < 26 ==> count_char(s@, letter(j)) == count_char(t@, letter(j)));\n assert(is_anagram_spec(s@, t@));\n } else {\n assert(!is_anagram_spec(s@, t@));\n }\n }\n all_zero\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc242/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc242/tests/mutated_testcases.jsonl"} {"problem": "lc2425", "family": "leetcode", "description": "# Bitwise XOR of All Pairings\n\nYou are given two **0-indexed** arrays, `nums1` and `nums2`, consisting of non-negative integers. Let there be another array, `nums3`, which contains the bitwise XOR of **all pairings** of integers between `nums1` and `nums2` (every integer in `nums1` is paired with every integer in `nums2` **exactly once**).\n\nReturn* the **bitwise XOR** of all integers in *`nums3`.\n\n \n\nExample 1:\n\n```\n**Input:** nums1 = [2,1,3], nums2 = [10,2,5,0]\n**Output:** 13\n**Explanation:**\nA possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].\nThe bitwise XOR of all these numbers is 13, so we return 13.\n```\n\nExample 2:\n\n```\n**Input:** nums1 = [1,2], nums2 = [3,4]\n**Output:** 0\n**Explanation:**\nAll possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],\nand nums1[1] ^ nums2[1].\nThus, one possible nums3 array is [2,5,1,6].\n2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums1.length, nums2.length <= 10^5`\n\n\t- `0 <= nums1[i], nums2[j] <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn xor_all_nums(nums1: Vec, nums2: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Bit Manipulation, Brainteaser\nAcceptance rate: 66.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn xor_all_spec(nums: Seq, idx: int) -> i32\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n nums[idx] ^ Self::xor_all_spec(nums, idx + 1)\n }\n }\n\n pub open spec fn xor_all_nums_spec(nums1: Seq, nums2: Seq) -> i32 {\n let x1 = if nums2.len() % 2 == 1 { Self::xor_all_spec(nums1, 0) } else { 0 };\n let x2 = if nums1.len() % 2 == 1 { Self::xor_all_spec(nums2, 0) } else { 0 };\n x1 ^ x2\n }\n\n pub fn xor_all_nums(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1_000_000_000,\n forall |j: int| 0 <= j < nums2.len() ==> 0 <= #[trigger] nums2[j] <= 1_000_000_000,\n ensures\n result == Self::xor_all_nums_spec(nums1@, nums2@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn xor_all_exec(nums: &Vec, idx: usize) -> i32 {\n if idx >= nums.len() {\n 0\n } else {\n nums[idx] ^ Self::xor_all_exec(nums, idx + 1)\n }\n }\n\n pub fn xor_all_nums(nums1: Vec, nums2: Vec) -> i32 {\n let x1: i32 = if nums2.len() % 2 == 1 { Self::xor_all_exec(&nums1, 0) } else { 0 };\n let x2: i32 = if nums1.len() % 2 == 1 { Self::xor_all_exec(&nums2, 0) } else { 0 };\n x1 ^ x2\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn xor_all_spec(nums: Seq, idx: int) -> i32\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n nums[idx] ^ Self::xor_all_spec(nums, idx + 1)\n }\n }\n\n pub open spec fn xor_all_nums_spec(nums1: Seq, nums2: Seq) -> i32 {\n let x1 = if nums2.len() % 2 == 1 { Self::xor_all_spec(nums1, 0) } else { 0 };\n let x2 = if nums1.len() % 2 == 1 { Self::xor_all_spec(nums2, 0) } else { 0 };\n x1 ^ x2\n }\n\n fn xor_all_exec(nums: &Vec, idx: usize) -> (res: i32)\n requires\n idx <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n res == Self::xor_all_spec(nums@, idx as int),\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n nums[idx] ^ Self::xor_all_exec(nums, idx + 1)\n }\n }\n\n pub fn xor_all_nums(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1_000_000_000,\n forall |j: int| 0 <= j < nums2.len() ==> 0 <= #[trigger] nums2[j] <= 1_000_000_000,\n ensures\n result == Self::xor_all_nums_spec(nums1@, nums2@),\n {\n let x1: i32 = if nums2.len() % 2 == 1 { Self::xor_all_exec(&nums1, 0) } else { 0 };\n let x2: i32 = if nums1.len() % 2 == 1 { Self::xor_all_exec(&nums2, 0) } else { 0 };\n x1 ^ x2\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn xor_all_spec(nums: Seq, idx: int) -> i32\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n nums[idx] ^ Self::xor_all_spec(nums, idx + 1)\n }\n }\n\n pub open spec fn xor_all_nums_spec(nums1: Seq, nums2: Seq) -> i32 {\n let x1 = if nums2.len() % 2 == 1 { Self::xor_all_spec(nums1, 0) } else { 0 };\n let x2 = if nums1.len() % 2 == 1 { Self::xor_all_spec(nums2, 0) } else { 0 };\n x1 ^ x2\n }\n\n fn xor_all_exec(nums: &Vec, idx: usize) -> (res: i32)\n requires\n idx <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n res == Self::xor_all_spec(nums@, idx as int),\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n nums[idx] ^ Self::xor_all_exec(nums, idx + 1)\n }\n }\n\n pub fn xor_all_nums(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1_000_000_000,\n forall |j: int| 0 <= j < nums2.len() ==> 0 <= #[trigger] nums2[j] <= 1_000_000_000,\n ensures\n result == Self::xor_all_nums_spec(nums1@, nums2@),\n {\n let x1: i32 = if nums2.len() % 2 == 1 { Self::xor_all_exec(&nums1, 0) } else { 0 };\n let x2: i32 = if nums1.len() % 2 == 1 { Self::xor_all_exec(&nums2, 0) } else { 0 };\n x1 ^ x2\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2425/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2425/tests/mutated_testcases.jsonl"} {"problem": "lc2428", "family": "leetcode", "description": "# Maximum Sum of an Hourglass\n\nYou are given an `m x n` integer matrix `grid`.\n\nWe define an **hourglass** as a part of the matrix with the following form:\n\n```\na b c\n d\ne f g\n```\n\nReturn the **maximum** sum of the elements of an hourglass.\n\nAn hourglass cannot be rotated and must be entirely contained within the matrix.\n\n## Example 1:\n\n> **Input:** `grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]`\n> \n> **Output:** `30`\n> \n> **Explanation:** The hourglass with maximum sum is `6 + 2 + 1 + 2 + 9 + 2 + 8 = 30`.\n\n## Example 2:\n\n> **Input:** `grid = [[1,2,3],[4,5,6],[7,8,9]]`\n> \n> **Output:** `35`\n> \n> **Explanation:** There is only one hourglass, and its sum is `1 + 2 + 3 + 5 + 7 + 8 + 9 = 35`.\n\n## Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `3 <= m, n <= 150`\n- `0 <= grid[i][j] <= 10^6`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_sum(grid: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nSenior, Array, Matrix, Prefix Sum, Weekly Contest 313\nAcceptance rate: 76.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn hourglass_sum(grid: Seq>, i: int, j: int) -> int\n recommends\n 0 <= i,\n 0 <= j,\n i + 2 < grid.len(),\n grid.len() > 0,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n j + 2 < grid[0].len(),\n{\n grid[i][j] as int + grid[i][j + 1] as int + grid[i][j + 2] as int + grid[i + 1][j + 1] as int\n + grid[i + 2][j] as int + grid[i + 2][j + 1] as int + grid[i + 2][j + 2] as int\n}\n\nimpl Solution {\n pub fn max_sum(grid: Vec>) -> (result: i32)\n requires\n 3 <= grid.len() <= 150,\n forall |i: int| 0 <= i < grid.len() ==> 3 <= #[trigger] grid[i].len() <= 150,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[0].len() ==> 0 <= #[trigger] grid[i][j] <= 1_000_000,\n ensures\n exists |i: int, j: int|\n 0 <= i && i + 2 < grid.len() && 0 <= j && j + 2 < grid[0].len() && result as int == hourglass_sum(grid@, i, j),\n forall |i: int, j: int|\n 0 <= i && i + 2 < grid.len() && 0 <= j && j + 2 < grid[0].len() ==> hourglass_sum(grid@, i, j) <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_sum(grid: Vec>) -> i32 {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut i: usize = 0;\n let mut best_i: usize = 0;\n let mut best_j: usize = 0;\n let mut best = grid[0][0] + grid[0][1] + grid[0][2] + grid[1][1] + grid[2][0] + grid[2][1] + grid[2][2];\n\n while i + 2 < rows {\n let mut j: usize = 0;\n while j + 2 < cols {\n let sum = grid[i][j]\n + grid[i][j + 1]\n + grid[i][j + 2]\n + grid[i + 1][j + 1]\n + grid[i + 2][j]\n + grid[i + 2][j + 1]\n + grid[i + 2][j + 2];\n if sum > best {\n let old_best = best;\n best = sum;\n best_i = i;\n best_j = j;\n }\n j += 1;\n }\n i += 1;\n }\n\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn hourglass_sum(grid: Seq>, i: int, j: int) -> int\n recommends\n 0 <= i,\n 0 <= j,\n i + 2 < grid.len(),\n grid.len() > 0,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n j + 2 < grid[0].len(),\n{\n grid[i][j] as int + grid[i][j + 1] as int + grid[i][j + 2] as int + grid[i + 1][j + 1] as int\n + grid[i + 2][j] as int + grid[i + 2][j + 1] as int + grid[i + 2][j + 2] as int\n}\n\nimpl Solution {\n pub fn max_sum(grid: Vec>) -> (result: i32)\n requires\n 3 <= grid.len() <= 150,\n forall |i: int| 0 <= i < grid.len() ==> 3 <= #[trigger] grid[i].len() <= 150,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[0].len() ==> 0 <= #[trigger] grid[i][j] <= 1_000_000,\n ensures\n exists |i: int, j: int|\n 0 <= i && i + 2 < grid.len() && 0 <= j && j + 2 < grid[0].len() && result as int == hourglass_sum(grid@, i, j),\n forall |i: int, j: int|\n 0 <= i && i + 2 < grid.len() && 0 <= j && j + 2 < grid[0].len() ==> hourglass_sum(grid@, i, j) <= result as int,\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut i: usize = 0;\n let mut best_i: usize = 0;\n let mut best_j: usize = 0;\n let mut best = grid[0][0] + grid[0][1] + grid[0][2] + grid[1][1] + grid[2][0] + grid[2][1] + grid[2][2];\n\n while i + 2 < rows {\n let mut j: usize = 0;\n while j + 2 < cols {\n let sum = grid[i][j]\n + grid[i][j + 1]\n + grid[i][j + 2]\n + grid[i + 1][j + 1]\n + grid[i + 2][j]\n + grid[i + 2][j + 1]\n + grid[i + 2][j + 2];\n if sum > best {\n let old_best = best;\n best = sum;\n best_i = i;\n best_j = j;\n }\n j += 1;\n }\n i += 1;\n }\n\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn hourglass_sum(grid: Seq>, i: int, j: int) -> int\n recommends\n 0 <= i,\n 0 <= j,\n i + 2 < grid.len(),\n grid.len() > 0,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n j + 2 < grid[0].len(),\n{\n grid[i][j] as int + grid[i][j + 1] as int + grid[i][j + 2] as int + grid[i + 1][j + 1] as int\n + grid[i + 2][j] as int + grid[i + 2][j + 1] as int + grid[i + 2][j + 2] as int\n}\n\nimpl Solution {\n pub fn max_sum(grid: Vec>) -> (result: i32)\n requires\n 3 <= grid.len() <= 150,\n forall |i: int| 0 <= i < grid.len() ==> 3 <= #[trigger] grid[i].len() <= 150,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[0].len() ==> 0 <= #[trigger] grid[i][j] <= 1_000_000,\n ensures\n exists |i: int, j: int|\n 0 <= i && i + 2 < grid.len() && 0 <= j && j + 2 < grid[0].len() && result as int == hourglass_sum(grid@, i, j),\n forall |i: int, j: int|\n 0 <= i && i + 2 < grid.len() && 0 <= j && j + 2 < grid[0].len() ==> hourglass_sum(grid@, i, j) <= result as int,\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut i: usize = 0;\n let mut best_i: usize = 0;\n let mut best_j: usize = 0;\n proof {\n assert(0 < rows);\n assert(1 < rows);\n assert(2 < rows);\n assert(grid[0].len() == cols);\n assert(grid[1].len() == cols);\n assert(grid[2].len() == cols);\n assert(0 < cols);\n assert(1 < cols);\n assert(2 < cols);\n }\n let mut best = grid[0][0] + grid[0][1] + grid[0][2] + grid[1][1] + grid[2][0] + grid[2][1] + grid[2][2];\n\n proof {\n assert(best as int == hourglass_sum(grid@, 0, 0));\n }\n\n while i + 2 < rows\n invariant\n rows == grid.len(),\n cols == grid[0].len(),\n 3 <= rows <= 150,\n 3 <= cols <= 150,\n 0 <= i <= rows,\n 0 <= best_i && best_i + 2 < rows,\n 0 <= best_j && best_j + 2 < cols,\n best as int == hourglass_sum(grid@, best_i as int, best_j as int),\n forall |r: int| 0 <= r < rows ==> 3 <= #[trigger] grid[r].len() <= 150,\n forall |r: int| 0 <= r < rows ==> #[trigger] grid[r].len() == cols,\n forall |r: int, c: int| 0 <= r < rows && 0 <= c < cols ==> 0 <= #[trigger] grid[r][c] <= 1_000_000,\n forall |r: int, c: int| 0 <= r < i && 0 <= c && c + 2 < cols ==> #[trigger] hourglass_sum(grid@, r, c) <= best as int,\n decreases rows - i,\n {\n let mut j: usize = 0;\n while j + 2 < cols\n invariant\n rows == grid.len(),\n cols == grid[0].len(),\n 3 <= rows <= 150,\n 3 <= cols <= 150,\n 0 <= i && i + 2 < rows,\n 0 <= j <= cols,\n 0 <= best_i && best_i + 2 < rows,\n 0 <= best_j && best_j + 2 < cols,\n best as int == hourglass_sum(grid@, best_i as int, best_j as int),\n forall |r: int| 0 <= r < rows ==> 3 <= #[trigger] grid[r].len() <= 150,\n forall |r: int| 0 <= r < rows ==> #[trigger] grid[r].len() == cols,\n forall |r: int, c: int| 0 <= r < rows && 0 <= c < cols ==> 0 <= #[trigger] grid[r][c] <= 1_000_000,\n forall |r: int, c: int| 0 <= r < i && 0 <= c && c + 2 < cols ==> #[trigger] hourglass_sum(grid@, r, c) <= best as int,\n forall |c: int| 0 <= c < j && c + 2 < cols ==> #[trigger] hourglass_sum(grid@, i as int, c) <= best as int,\n decreases cols - j,\n {\n proof {\n assert(i + 2 < rows);\n assert(j + 2 < cols);\n assert(j < cols);\n assert(j + 1 < cols);\n assert(j + 2 < cols);\n assert(i < rows);\n assert(i + 1 < rows);\n assert(i + 2 < rows);\n assert(grid[i as int].len() == cols);\n assert(grid[(i + 1) as int].len() == cols);\n assert(grid[(i + 2) as int].len() == cols);\n assert(j < grid[i as int].len());\n assert(j + 1 < grid[i as int].len());\n assert(j + 2 < grid[i as int].len());\n assert(j + 1 < grid[(i + 1) as int].len());\n assert(j < grid[(i + 2) as int].len());\n assert(j + 1 < grid[(i + 2) as int].len());\n assert(j + 2 < grid[(i + 2) as int].len());\n }\n let sum = grid[i][j]\n + grid[i][j + 1]\n + grid[i][j + 2]\n + grid[i + 1][j + 1]\n + grid[i + 2][j]\n + grid[i + 2][j + 1]\n + grid[i + 2][j + 2];\n proof {\n assert(sum as int == hourglass_sum(grid@, i as int, j as int));\n }\n if sum > best {\n let old_best = best;\n best = sum;\n best_i = i;\n best_j = j;\n proof {\n assert((old_best as int) < (best as int));\n assert(best as int == hourglass_sum(grid@, best_i as int, best_j as int));\n assert forall |r: int, c: int| 0 <= r < i && 0 <= c && c + 2 < cols implies #[trigger] hourglass_sum(grid@, r, c) <= best as int by {\n assert(hourglass_sum(grid@, r, c) <= old_best as int);\n };\n assert forall |c: int| 0 <= c < j && c + 2 < cols implies #[trigger] hourglass_sum(grid@, i as int, c) <= best as int by {\n assert(hourglass_sum(grid@, i as int, c) <= old_best as int);\n };\n }\n } else {\n proof {\n assert(hourglass_sum(grid@, i as int, j as int) <= best as int);\n }\n }\n j += 1;\n }\n proof {\n assert forall |r: int, c: int| 0 <= r < i + 1 && 0 <= c && c + 2 < cols implies #[trigger] hourglass_sum(grid@, r, c) <= best as int by {\n if r < i {\n } else {\n assert(r == i);\n assert(c + 2 < cols);\n assert(!(j + 2 < cols));\n assert(cols <= j + 2);\n assert(c < j);\n }\n };\n }\n i += 1;\n }\n\n proof {\n assert forall |r: int, c: int| 0 <= r && r + 2 < rows && 0 <= c && c + 2 < cols implies #[trigger] hourglass_sum(grid@, r, c) <= best as int by {\n assert(r + 2 < rows);\n assert(!(i + 2 < rows));\n assert(rows <= i + 2);\n assert(r < i);\n };\n }\n\n best\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2428/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2428/tests/mutated_testcases.jsonl"} {"problem": "lc2448", "family": "leetcode", "description": "# Minimum Cost to Make Array Equal\n\nYou are given two **0-indexed** arrays `nums` and `cost` consisting each of `n` **positive** integers.\n\nYou can do the following operation **any** number of times:\n\n- Increase or decrease **any** element of the array `nums` by `1`.\n\nThe cost of doing one operation on the `i^th` element is `cost[i]`.\n\nReturn the **minimum** total cost such that all the elements of the array `nums` become **equal**.\n\n## Example 1:\n\n> **Input:** nums = [1,3,5,2], cost = [2,3,1,14]\n> **Output:** 8\n> **Explanation:** We can make all the elements equal to 2 in the following way:\n> - Increase the 0^th element one time. The cost is 2.\n> - Decrease the 1^st element one time. The cost is 3.\n> - Decrease the 2^nd element three times. The cost is 1 + 1 + 1 = 3.\n> The total cost is 2 + 3 + 3 = 8.\n> It can be shown that we cannot make the array equal with a smaller cost.\n\n## Example 2:\n\n> **Input:** nums = [2,2,2,2,2], cost = [4,2,8,1,3]\n> **Output:** 0\n> **Explanation:** All the elements are already equal, so no operations are needed.\n\n## Constraints:\n\n- $n == nums.length == cost.length$\n- $1 \\le n \\le 10^5$\n- $1 \\le nums[i], cost[i] \\le 10^6$\n- Test cases are generated such that the answer fits in a 53-bit integer.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_cost(nums: Vec, cost: Vec) -> i64 {\n \n }\n}\n```\n", "tags": "Hard\nSenior Staff, Array, Binary Search, Greedy, Sorting, Prefix Sum, Weekly Contest 316\nAcceptance rate: 46.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n }\n\n pub open spec fn move_cost(nums: Seq, cost: Seq, target: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::move_cost(nums, cost, target, n - 1)\n + Self::abs_diff(nums[n - 1] as int, target) * cost[n - 1] as int\n }\n }\n\n pub fn min_cost(nums: Vec, cost: Vec) -> (result: i64)\n requires\n 1 <= nums.len() && nums.len() <= 100_000,\n nums.len() == cost.len(),\n forall|i: int| 0 <= i && i < nums.len() ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n forall|i: int| 0 <= i && i < cost.len() ==> 1 <= cost[i] && cost[i] <= 1_000_000,\n exists|target: int| 1 <= target && target <= 1_000_000\n && #[trigger] Self::move_cost(nums@, cost@, target, nums.len() as int) <= 9007199254740991,\n ensures\n result >= 0,\n forall|target: int| 1 <= target && target <= 1_000_000 ==> result as int <= #[trigger] Self::move_cost(nums@, cost@, target, nums.len() as int),\n exists|target: int| 1 <= target && target <= 1_000_000 && result as int == Self::move_cost(nums@, cost@, target, nums.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_cost(nums: Vec, cost: Vec) -> i64 {\n let max_value: usize = 1_000_000;\n let n = nums.len();\n let mut weights: Vec = Vec::new();\n let mut zeroes: usize = 0;\n\n while zeroes < max_value + 1 {\n weights.push(0);\n zeroes = zeroes + 1;\n }\n\n let mut total_weight: i64 = 0;\n let mut current: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let value = nums[i] as usize;\n let c = cost[i] as i64;\n weights[value] = weights[value] + c;\n total_weight = total_weight + c;\n current = current + (nums[i] as i64 - 1) * c;\n i = i + 1;\n }\n\n let mut target: usize = 1;\n let mut prefix: i64 = weights[1];\n let mut best: i64 = current;\n while target < max_value {\n let delta = prefix - (total_weight - prefix);\n current = current + delta;\n target = target + 1;\n prefix = prefix + weights[target];\n if current < best {\n best = current;\n }\n }\n\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n }\n\n pub open spec fn move_cost(nums: Seq, cost: Seq, target: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::move_cost(nums, cost, target, n - 1)\n + Self::abs_diff(nums[n - 1] as int, target) * cost[n - 1] as int\n }\n }\n\n pub open spec fn total_weight(cost: Seq, n: int) -> int\n recommends\n 0 <= n && n <= cost.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::total_weight(cost, n - 1) + cost[n - 1] as int\n }\n }\n\n pub open spec fn bucket_weight(nums: Seq, cost: Seq, value: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::bucket_weight(nums, cost, value, n - 1)\n + if nums[n - 1] as int == value { cost[n - 1] as int } else { 0 }\n }\n }\n\n pub open spec fn prefix_weight(nums: Seq, cost: Seq, target: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::prefix_weight(nums, cost, target, n - 1)\n + if nums[n - 1] as int <= target { cost[n - 1] as int } else { 0 }\n }\n }\n\n pub fn min_cost(nums: Vec, cost: Vec) -> (result: i64)\n requires\n 1 <= nums.len() && nums.len() <= 100_000,\n nums.len() == cost.len(),\n forall|i: int| 0 <= i && i < nums.len() ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n forall|i: int| 0 <= i && i < cost.len() ==> 1 <= cost[i] && cost[i] <= 1_000_000,\n exists|target: int| 1 <= target && target <= 1_000_000\n && #[trigger] Self::move_cost(nums@, cost@, target, nums.len() as int) <= 9007199254740991,\n ensures\n result >= 0,\n forall|target: int| 1 <= target && target <= 1_000_000 ==> result as int <= #[trigger] Self::move_cost(nums@, cost@, target, nums.len() as int),\n exists|target: int| 1 <= target && target <= 1_000_000 && result as int == Self::move_cost(nums@, cost@, target, nums.len() as int),\n {\n let max_value: usize = 1_000_000;\n let n = nums.len();\n let mut weights: Vec = Vec::new();\n let mut zeroes: usize = 0;\n\n while zeroes < max_value + 1 {\n weights.push(0);\n zeroes = zeroes + 1;\n }\n\n let mut total_weight: i64 = 0;\n let mut current: i64 = 0;\n let mut i: usize = 0;\n while i < n {\n let value = nums[i] as usize;\n let c = cost[i] as i64;\n weights.set(value, weights[value] + c);\n total_weight = total_weight + c;\n current = current + (nums[i] as i64 - 1) * c;\n i = i + 1;\n }\n\n let mut target: usize = 1;\n let mut prefix: i64 = weights[1];\n let mut best: i64 = current;\n while target < max_value {\n let delta = prefix - (total_weight - prefix);\n current = current + delta;\n target = target + 1;\n prefix = prefix + weights[target];\n if current < best {\n best = current;\n }\n }\n\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn abs_diff(a: int, b: int) -> int {\n if a >= b { a - b } else { b - a }\n }\n\n pub open spec fn move_cost(nums: Seq, cost: Seq, target: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::move_cost(nums, cost, target, n - 1)\n + Self::abs_diff(nums[n - 1] as int, target) * cost[n - 1] as int\n }\n }\n\n pub open spec fn total_weight(cost: Seq, n: int) -> int\n recommends\n 0 <= n && n <= cost.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::total_weight(cost, n - 1) + cost[n - 1] as int\n }\n }\n\n pub open spec fn bucket_weight(nums: Seq, cost: Seq, value: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::bucket_weight(nums, cost, value, n - 1)\n + if nums[n - 1] as int == value { cost[n - 1] as int } else { 0 }\n }\n }\n\n pub open spec fn prefix_weight(nums: Seq, cost: Seq, target: int, n: int) -> int\n recommends\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::prefix_weight(nums, cost, target, n - 1)\n + if nums[n - 1] as int <= target { cost[n - 1] as int } else { 0 }\n }\n }\n\n proof fn lemma_total_weight_step(cost: Seq, n: int)\n requires\n 0 <= n && n < cost.len(),\n ensures\n Self::total_weight(cost, n + 1) == Self::total_weight(cost, n) + cost[n] as int,\n {\n reveal_with_fuel(Solution::total_weight, 2);\n }\n\n proof fn lemma_bucket_step(nums: Seq, cost: Seq, value: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n < nums.len(),\n ensures\n Self::bucket_weight(nums, cost, value, n + 1)\n == Self::bucket_weight(nums, cost, value, n)\n + if nums[n] as int == value { cost[n] as int } else { 0 },\n {\n reveal_with_fuel(Solution::bucket_weight, 2);\n }\n\n proof fn lemma_move_cost_extend(nums: Seq, cost: Seq, target: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n < nums.len(),\n ensures\n Self::move_cost(nums, cost, target, n + 1)\n == Self::move_cost(nums, cost, target, n)\n + Self::abs_diff(nums[n] as int, target) * cost[n] as int,\n {\n reveal_with_fuel(Solution::move_cost, 2);\n }\n\n proof fn lemma_move_cost_nonnegative(nums: Seq, cost: Seq, target: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n forall|i: int| 0 <= i && i < n ==> 1 <= cost[i],\n ensures\n 0 <= Self::move_cost(nums, cost, target, n),\n decreases n,\n {\n if n > 0 {\n Self::lemma_move_cost_nonnegative(nums, cost, target, n - 1);\n reveal_with_fuel(Solution::move_cost, 2);\n reveal(Solution::abs_diff);\n }\n }\n\n proof fn lemma_move_cost_bounded(nums: Seq, cost: Seq, target: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n 1 <= target && target <= 1_000_000,\n forall|i: int| 0 <= i && i < n ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n forall|i: int| 0 <= i && i < n ==> 1 <= cost[i] && cost[i] <= 1_000_000,\n ensures\n Self::move_cost(nums, cost, target, n) <= n * 1_000_000_000_000int,\n decreases n,\n {\n if n > 0 {\n let k = n - 1;\n Self::lemma_move_cost_bounded(nums, cost, target, k);\n Self::lemma_move_cost_extend(nums, cost, target, k);\n reveal(Solution::abs_diff);\n assert(Self::abs_diff(nums[k] as int, target) <= 1_000_000);\n assert(Self::abs_diff(nums[k] as int, target) * cost[k] as int <= 1_000_000_000_000int) by (nonlinear_arith)\n requires\n Self::abs_diff(nums[k] as int, target) <= 1_000_000,\n cost[k] as int <= 1_000_000,\n {};\n assert(Self::move_cost(nums, cost, target, n) <= n * 1_000_000_000_000int) by (nonlinear_arith)\n requires\n n == k + 1,\n Self::move_cost(nums, cost, target, k) <= k * 1_000_000_000_000int,\n Self::move_cost(nums, cost, target, n) == Self::move_cost(nums, cost, target, k) + Self::abs_diff(nums[k] as int, target) * cost[k] as int,\n Self::abs_diff(nums[k] as int, target) * cost[k] as int <= 1_000_000_000_000int,\n {};\n }\n }\n\n proof fn lemma_prefix_bounded(nums: Seq, cost: Seq, target: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n forall|i: int| 0 <= i && i < n ==> 1 <= cost[i],\n ensures\n 0 <= Self::prefix_weight(nums, cost, target, n) <= Self::total_weight(cost, n),\n decreases n,\n {\n if n > 0 {\n Self::lemma_prefix_bounded(nums, cost, target, n - 1);\n Self::lemma_total_weight_step(cost, n - 1);\n reveal_with_fuel(Solution::prefix_weight, 2);\n }\n }\n\n proof fn lemma_prefix_zero(nums: Seq, cost: Seq, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n forall|i: int| 0 <= i && i < n ==> 1 <= nums[i],\n ensures\n Self::prefix_weight(nums, cost, 0, n) == 0,\n decreases n,\n {\n if n > 0 {\n Self::lemma_prefix_zero(nums, cost, n - 1);\n reveal_with_fuel(Solution::prefix_weight, 2);\n }\n }\n\n proof fn lemma_prefix_bucket_step(nums: Seq, cost: Seq, target: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n forall|i: int| 0 <= i && i < n ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n 0 <= target && target < 1_000_000,\n ensures\n Self::prefix_weight(nums, cost, target + 1, n)\n == Self::prefix_weight(nums, cost, target, n)\n + Self::bucket_weight(nums, cost, target + 1, n),\n decreases n,\n {\n if n > 0 {\n Self::lemma_prefix_bucket_step(nums, cost, target, n - 1);\n reveal_with_fuel(Solution::prefix_weight, 2);\n reveal_with_fuel(Solution::bucket_weight, 2);\n let a = nums[n - 1] as int;\n let w = cost[n - 1] as int;\n if a <= target {\n assert(a != target + 1);\n } else if a == target + 1 {\n } else {\n assert(target + 1 < a);\n }\n assert((if a <= target + 1 { w } else { 0 })\n == (if a <= target { w } else { 0 }) + (if a == target + 1 { w } else { 0 }));\n }\n }\n\n proof fn lemma_abs_step(a: int, target: int)\n requires\n 1 <= a && a <= 1_000_000,\n 1 <= target && target < 1_000_000,\n ensures\n Self::abs_diff(a, target + 1)\n == Self::abs_diff(a, target) + if a <= target { 1 } else { -1 },\n {\n reveal(Solution::abs_diff);\n if a <= target {\n assert(Self::abs_diff(a, target + 1) == target + 1 - a);\n assert(Self::abs_diff(a, target) == target - a);\n } else {\n assert(target + 1 <= a);\n assert(Self::abs_diff(a, target + 1) == a - (target + 1));\n assert(Self::abs_diff(a, target) == a - target);\n }\n }\n\n proof fn lemma_cost_target_step(nums: Seq, cost: Seq, target: int, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n forall|i: int| 0 <= i && i < n ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n forall|i: int| 0 <= i && i < n ==> 1 <= cost[i] && cost[i] <= 1_000_000,\n 1 <= target && target < 1_000_000,\n ensures\n Self::move_cost(nums, cost, target + 1, n)\n == Self::move_cost(nums, cost, target, n)\n + Self::prefix_weight(nums, cost, target, n)\n - (Self::total_weight(cost, n) - Self::prefix_weight(nums, cost, target, n)),\n decreases n,\n {\n if n > 0 {\n let k = n - 1;\n Self::lemma_cost_target_step(nums, cost, target, k);\n Self::lemma_move_cost_extend(nums, cost, target + 1, k);\n Self::lemma_move_cost_extend(nums, cost, target, k);\n Self::lemma_total_weight_step(cost, k);\n reveal_with_fuel(Solution::prefix_weight, 2);\n let a = nums[k] as int;\n let w = cost[k] as int;\n Self::lemma_abs_step(a, target);\n if a <= target {\n assert((if a <= target { w } else { 0 }) == w);\n } else {\n assert((if a <= target { w } else { 0 }) == 0);\n }\n assert(Self::abs_diff(a, target + 1) * w\n == Self::abs_diff(a, target) * w + (if a <= target { w } else { -w })) by (nonlinear_arith)\n requires\n Self::abs_diff(a, target + 1)\n == Self::abs_diff(a, target) + if a <= target { 1 } else { -1 },\n 1 <= w,\n {};\n assert((if a <= target { w } else { -w })\n == (if a <= target { w } else { 0 }) - (w - (if a <= target { w } else { 0 })));\n }\n }\n\n proof fn lemma_prefix_one(nums: Seq, cost: Seq, n: int)\n requires\n nums.len() == cost.len(),\n 0 <= n && n <= nums.len(),\n forall|i: int| 0 <= i && i < n ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n ensures\n Self::prefix_weight(nums, cost, 1, n) == Self::bucket_weight(nums, cost, 1, n),\n {\n Self::lemma_prefix_zero(nums, cost, n);\n Self::lemma_prefix_bucket_step(nums, cost, 0, n);\n assert(Self::prefix_weight(nums, cost, 1, n)\n == Self::prefix_weight(nums, cost, 0, n) + Self::bucket_weight(nums, cost, 1, n));\n }\n\n pub fn min_cost(nums: Vec, cost: Vec) -> (result: i64)\n requires\n 1 <= nums.len() && nums.len() <= 100_000,\n nums.len() == cost.len(),\n forall|i: int| 0 <= i && i < nums.len() ==> 1 <= nums[i] && nums[i] <= 1_000_000,\n forall|i: int| 0 <= i && i < cost.len() ==> 1 <= cost[i] && cost[i] <= 1_000_000,\n exists|target: int| 1 <= target && target <= 1_000_000\n && #[trigger] Self::move_cost(nums@, cost@, target, nums.len() as int) <= 9007199254740991,\n ensures\n result >= 0,\n forall|target: int| 1 <= target && target <= 1_000_000 ==> result as int <= #[trigger] Self::move_cost(nums@, cost@, target, nums.len() as int),\n exists|target: int| 1 <= target && target <= 1_000_000 && result as int == Self::move_cost(nums@, cost@, target, nums.len() as int),\n {\n let max_value: usize = 1_000_000;\n let n = nums.len();\n let mut weights: Vec = Vec::new();\n let mut zeroes: usize = 0;\n\n while zeroes < max_value + 1\n invariant\n max_value == 1_000_000,\n weights@.len() == zeroes as int,\n 0 <= zeroes && zeroes <= max_value + 1,\n forall|j: int| 0 <= j && j < zeroes as int ==> #[trigger] weights@[j] == 0,\n decreases max_value + 1 - zeroes,\n {\n weights.push(0);\n zeroes = zeroes + 1;\n }\n\n let mut total_weight: i64 = 0;\n let mut current: i64 = 0;\n let mut i: usize = 0;\n\n proof {\n reveal_with_fuel(Solution::total_weight, 1);\n reveal_with_fuel(Solution::move_cost, 1);\n assert(forall|v: int| 1 <= v && v <= max_value as int ==> (#[trigger] weights@[v]) as int == Self::bucket_weight(nums@, cost@, v, 0)) by {\n let v: int = arbitrary();\n reveal_with_fuel(Solution::bucket_weight, 1);\n }\n }\n\n while i < n\n invariant\n max_value == 1_000_000,\n n == nums.len(),\n n == cost.len(),\n 1 <= n && n <= 100_000,\n weights.len() == max_value + 1,\n weights@[0] == 0,\n forall|j: int| 0 <= j && j < n as int ==> 1 <= nums@[j] && nums@[j] <= 1_000_000,\n forall|j: int| 0 <= j && j < n as int ==> 1 <= cost@[j] && cost@[j] <= 1_000_000,\n 0 <= i && i <= n,\n 0 <= total_weight as int <= i as int * 1_000_000,\n 0 <= current as int <= i as int * 1_000_000_000_000,\n total_weight as int == Self::total_weight(cost@, i as int),\n current as int == Self::move_cost(nums@, cost@, 1, i as int),\n forall|v: int| 1 <= v && v <= max_value as int ==> (#[trigger] weights@[v]) as int == Self::bucket_weight(nums@, cost@, v, i as int),\n forall|v: int| 0 <= v && v <= max_value as int ==> 0 <= (#[trigger] weights@[v]) as int <= total_weight as int,\n decreases n - i,\n {\n let value = nums[i] as usize;\n let c = cost[i] as i64;\n let ghost prev_bucket = weights[value as int];\n let ghost prev_weights = weights@;\n let ghost k = i as int;\n\n proof {\n assert(1 <= nums@[k] && nums@[k] <= 1_000_000);\n assert(1 <= cost@[k] && cost@[k] <= 1_000_000);\n assert(1 <= value as int && value as int <= max_value as int);\n assert(0 <= prev_bucket as int <= total_weight as int);\n assert(prev_bucket as int + c as int <= (k + 1) * 1_000_000) by (nonlinear_arith)\n requires\n prev_bucket as int <= total_weight as int,\n total_weight as int <= k * 1_000_000,\n c as int <= 1_000_000,\n {};\n assert((nums@[k] as int - 1) * c as int <= 1_000_000_000_000) by (nonlinear_arith)\n requires\n nums@[k] as int <= 1_000_000,\n c as int <= 1_000_000,\n 1 <= nums@[k] as int,\n {};\n assert(current as int + (nums@[k] as int - 1) * c as int <= (k + 1) * 1_000_000_000_000) by (nonlinear_arith)\n requires\n current as int <= k * 1_000_000_000_000,\n (nums@[k] as int - 1) * c as int <= 1_000_000_000_000,\n {};\n }\n\n weights.set(value, weights[value] + c);\n total_weight = total_weight + c;\n current = current + (nums[i] as i64 - 1) * c;\n\n proof {\n Self::lemma_bucket_step(nums@, cost@, value as int, k);\n Self::lemma_total_weight_step(cost@, k);\n Self::lemma_move_cost_extend(nums@, cost@, 1, k);\n assert(total_weight as int == Self::total_weight(cost@, k + 1));\n assert(current as int == Self::move_cost(nums@, cost@, 1, k + 1));\n assert(weights@[0] == 0);\n assert(forall|v: int| 1 <= v && v <= max_value as int ==> (#[trigger] weights@[v]) as int == Self::bucket_weight(nums@, cost@, v, k + 1)) by {\n let v: int = arbitrary();\n if 1 <= v && v <= max_value as int {\n assert(0 <= v && v < weights@.len());\n assert(0 <= v && v < prev_weights.len());\n if v == value as int {\n assert(weights@[v] as int == prev_weights[v] as int + c as int);\n assert(prev_weights[v] as int == Self::bucket_weight(nums@, cost@, v, k));\n } else {\n assert(weights@[v] == prev_weights[v]);\n }\n }\n }\n assert(forall|v: int| 0 <= v && v <= max_value as int ==> 0 <= (#[trigger] weights@[v]) as int <= total_weight as int) by {\n let v: int = arbitrary();\n if 0 <= v && v <= max_value as int {\n assert(v < weights@.len());\n assert(v < prev_weights.len());\n if v == value as int {\n assert(weights@[v] as int == prev_weights[v] as int + c as int);\n } else {\n assert(weights@[v] == prev_weights[v]);\n }\n }\n }\n }\n\n i = i + 1;\n }\n\n let mut target: usize = 1;\n let mut prefix: i64 = weights[1];\n let mut best: i64 = current;\n let ghost mut best_target: int = 1;\n\n proof {\n Self::lemma_prefix_one(nums@, cost@, n as int);\n assert(prefix as int == Self::prefix_weight(nums@, cost@, 1, n as int));\n assert(best as int == Self::move_cost(nums@, cost@, best_target, n as int));\n assert(forall|u: int| 1 <= u && u <= target as int ==> best as int <= #[trigger] Self::move_cost(nums@, cost@, u, n as int)) by {\n let u: int = arbitrary();\n if 1 <= u && u <= target as int {\n assert(u == 1);\n }\n }\n }\n\n while target < max_value\n invariant\n max_value == 1_000_000,\n n == nums.len(),\n n == cost.len(),\n 1 <= n && n <= 100_000,\n weights.len() == max_value + 1,\n weights@[0] == 0,\n forall|j: int| 0 <= j && j < n as int ==> 1 <= nums@[j] && nums@[j] <= 1_000_000,\n forall|j: int| 0 <= j && j < n as int ==> 1 <= cost@[j] && cost@[j] <= 1_000_000,\n total_weight as int == Self::total_weight(cost@, n as int),\n 0 <= total_weight as int <= n as int * 1_000_000,\n forall|v: int| 1 <= v && v <= max_value as int ==> (#[trigger] weights@[v]) as int == Self::bucket_weight(nums@, cost@, v, n as int),\n forall|v: int| 0 <= v && v <= max_value as int ==> 0 <= (#[trigger] weights@[v]) as int <= total_weight as int,\n 1 <= target && target <= max_value,\n prefix as int == Self::prefix_weight(nums@, cost@, target as int, n as int),\n 0 <= prefix as int <= total_weight as int,\n current as int == Self::move_cost(nums@, cost@, target as int, n as int),\n 0 <= current as int <= 100_000_000_000_000_000,\n 1 <= best_target && best_target <= target as int,\n best as int == Self::move_cost(nums@, cost@, best_target, n as int),\n 0 <= best as int <= 100_000_000_000_000_000,\n forall|u: int| 1 <= u && u <= target as int ==> best as int <= #[trigger] Self::move_cost(nums@, cost@, u, n as int),\n decreases max_value - target,\n {\n let ghost old_best = best;\n let ghost old_best_target = best_target;\n let ghost old_target = target as int;\n let delta = prefix - (total_weight - prefix);\n\n proof {\n Self::lemma_cost_target_step(nums@, cost@, old_target, n as int);\n assert(delta as int == 2 * prefix as int - total_weight as int) by (nonlinear_arith)\n requires\n delta as int == prefix as int - (total_weight as int - prefix as int),\n {};\n assert(-(total_weight as int) <= delta as int) by (nonlinear_arith)\n requires\n delta as int == 2 * prefix as int - total_weight as int,\n 0 <= prefix as int,\n prefix as int <= total_weight as int,\n {};\n assert(delta as int <= total_weight as int) by (nonlinear_arith)\n requires\n delta as int == 2 * prefix as int - total_weight as int,\n 0 <= prefix as int,\n prefix as int <= total_weight as int,\n {};\n assert(current as int + delta as int == Self::move_cost(nums@, cost@, old_target + 1, n as int));\n Self::lemma_move_cost_nonnegative(nums@, cost@, old_target + 1, n as int);\n Self::lemma_move_cost_bounded(nums@, cost@, old_target + 1, n as int);\n assert(0 <= current as int + delta as int);\n assert(current as int + delta as int <= 100_000_000_000_000_000);\n }\n\n current = current + delta;\n target = target + 1;\n\n proof {\n Self::lemma_prefix_bucket_step(nums@, cost@, old_target, n as int);\n assert(prefix as int + weights@[target as int] as int\n == Self::prefix_weight(nums@, cost@, old_target, n as int)\n + Self::bucket_weight(nums@, cost@, target as int, n as int));\n assert(prefix as int + weights@[target as int] as int\n == Self::prefix_weight(nums@, cost@, target as int, n as int));\n Self::lemma_prefix_bounded(nums@, cost@, target as int, n as int);\n }\n\n prefix = prefix + weights[target];\n if current < best {\n best = current;\n proof {\n best_target = target as int;\n }\n }\n\n proof {\n if current < old_best {\n assert(best as int == Self::move_cost(nums@, cost@, best_target, n as int));\n } else {\n assert(best == old_best);\n assert(best_target == old_best_target);\n }\n assert(forall|u: int| 1 <= u && u <= target as int ==> best as int <= #[trigger] Self::move_cost(nums@, cost@, u, n as int)) by {\n let u: int = arbitrary();\n if 1 <= u && u <= target as int {\n if u < target as int {\n assert(u <= old_target);\n if current < old_best {\n assert(best == current);\n assert(current < old_best);\n assert(old_best as int <= Self::move_cost(nums@, cost@, u, n as int));\n } else {\n assert(best == old_best);\n }\n } else {\n assert(u == target as int);\n if current < old_best {\n assert(best == current);\n } else {\n assert(best == old_best);\n assert(old_best <= current);\n }\n }\n }\n }\n }\n }\n\n proof {\n assert(forall|target0: int| 1 <= target0 && target0 <= 1_000_000 ==> best as int <= #[trigger] Self::move_cost(nums@, cost@, target0, n as int)) by {\n let target0: int = arbitrary();\n }\n assert(exists|target0: int| 1 <= target0 && target0 <= 1_000_000 && best as int == Self::move_cost(nums@, cost@, target0, n as int)) by {\n let witness = best_target;\n assert(1 <= witness && witness <= 1_000_000);\n assert(best as int == Self::move_cost(nums@, cost@, witness, n as int));\n }\n }\n\n best\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2448/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2448/tests/mutated_testcases.jsonl"} {"problem": "lc2485", "family": "leetcode", "description": "# Find the Pivot Integer\n\nGiven a positive integer `n`, find the **pivot integer** `x` such that:\n\n\t- The sum of all elements between `1` and `x` inclusively equals the sum of all elements between `x` and `n` inclusively.\n\nReturn *the pivot integer *`x`. If no such integer exists, return `-1`. It is guaranteed that there will be at most one pivot index for the given input.\n\n \n\nExample 1:\n\n```\n**Input:** n = 8\n**Output:** 6\n**Explanation:** 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.\n```\n\nExample 2:\n\n```\n**Input:** n = 1\n**Output:** 1\n**Explanation:** 1 is the pivot integer since: 1 = 1.\n```\n\nExample 3:\n\n```\n**Input:** n = 4\n**Output:** -1\n**Explanation:** It can be proved that no such integer exist.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn pivot_integer(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Prefix Sum\nAcceptance rate: 83.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_to(end: int) -> int\n decreases end\n {\n if end <= 0 { 0 }\n else { Self::sum_to(end - 1) + end }\n }\n\n pub fn pivot_integer(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n ensures\n result == -1 ==> forall|x: int| 1 <= x <= n as int ==> #[trigger] Self::sum_to(x) != Self::sum_to(n as int) - Self::sum_to(x - 1),\n result != -1 ==> 1 <= result <= n && Self::sum_to(result as int) == Self::sum_to(n as int) - Self::sum_to(result as int - 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn pivot_integer(n: i32) -> i32 {\n let n_u = n as usize;\n let mut x: usize = 1;\n while x <= n_u {\n let mut left: i32 = 0;\n let mut i: usize = 1;\n while i <= x {\n left = left + i as i32;\n i = i + 1;\n }\n let mut right: i32 = 0;\n i = x;\n while i <= n_u {\n right = right + i as i32;\n i = i + 1;\n }\n if left == right {\n return x as i32;\n }\n x = x + 1;\n }\n -1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_to(end: int) -> int\n decreases end\n {\n if end <= 0 { 0 }\n else { Self::sum_to(end - 1) + end }\n }\n\n pub fn pivot_integer(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n ensures\n result == -1 ==> forall|x: int| 1 <= x <= n as int ==> #[trigger] Self::sum_to(x) != Self::sum_to(n as int) - Self::sum_to(x - 1),\n result != -1 ==> 1 <= result <= n && Self::sum_to(result as int) == Self::sum_to(n as int) - Self::sum_to(result as int - 1),\n {\n let n_u = n as usize;\n let mut x: usize = 1;\n while x <= n_u {\n let mut left: i32 = 0;\n let mut i: usize = 1;\n while i <= x {\n left = left + i as i32;\n i = i + 1;\n }\n let mut right: i32 = 0;\n i = x;\n while i <= n_u {\n right = right + i as i32;\n i = i + 1;\n }\n if left == right {\n return x as i32;\n }\n x = x + 1;\n }\n -1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_to(end: int) -> int\n decreases end\n {\n if end <= 0 { 0 }\n else { Self::sum_to(end - 1) + end }\n }\n\n proof fn sum_to_bound(x: int)\n requires 0 <= x <= 1000,\n ensures 0 <= Self::sum_to(x) <= x * 1001,\n decreases x,\n {\n if x > 0 {\n Self::sum_to_bound(x - 1);\n }\n }\n\n pub fn pivot_integer(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n ensures\n result == -1 ==> forall|x: int| 1 <= x <= n as int ==> #[trigger] Self::sum_to(x) != Self::sum_to(n as int) - Self::sum_to(x - 1),\n result != -1 ==> 1 <= result <= n && Self::sum_to(result as int) == Self::sum_to(n as int) - Self::sum_to(result as int - 1),\n {\n let n_u = n as usize;\n let mut x: usize = 1;\n while x <= n_u\n invariant\n 1 <= x <= n_u + 1,\n 1 <= n <= 1000,\n n_u == n as usize,\n n_u <= 1000,\n forall|y: int| 1 <= y < x as int ==> #[trigger] Self::sum_to(y) != Self::sum_to(n as int) - Self::sum_to(y - 1),\n decreases n_u + 1 - x\n {\n let mut left: i32 = 0;\n let mut i: usize = 1;\n while i <= x\n invariant\n 1 <= i <= x + 1,\n x <= n_u,\n n_u <= 1000,\n 1 <= n <= 1000,\n n_u == n as usize,\n left as int == Self::sum_to(i as int - 1),\n 0 <= left <= 1_001_000,\n decreases x + 1 - i\n {\n proof { Self::sum_to_bound(i as int); }\n left = left + i as i32;\n i = i + 1;\n }\n\n let mut right: i32 = 0;\n i = x;\n while i <= n_u\n invariant\n 1 <= x <= n_u,\n x <= i <= n_u + 1,\n n_u <= 1000,\n 1 <= n <= 1000,\n n_u == n as usize,\n right as int == Self::sum_to(i as int - 1) - Self::sum_to(x as int - 1),\n 0 <= right <= 1_001_000,\n decreases n_u + 1 - i\n {\n proof {\n Self::sum_to_bound(i as int);\n Self::sum_to_bound(x as int - 1);\n }\n right = right + i as i32;\n i = i + 1;\n }\n\n proof {\n assert(left as int == Self::sum_to(x as int));\n assert(right as int == Self::sum_to(n as int) - Self::sum_to(x as int - 1));\n }\n\n if left == right {\n return x as i32;\n }\n x = x + 1;\n }\n -1\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2485/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2485/tests/mutated_testcases.jsonl"} {"problem": "lc2511", "family": "leetcode", "description": "# 2511. Maximum Enemy Forts That Can Be Captured\n\nYou are given a 0-indexed integer array `forts` of length `n` representing the positions of several forts. `forts[i]` can be `-1`, `0`, or `1` where:\n\n- `-1` represents there is no fort at the `i`th position.\n- `0` indicates there is an enemy fort at the `i`th position.\n- `1` indicates the fort at the `i`th position is under your command.\n\nNow you have decided to move your army from one of your forts at position `i` to an empty position `j` such that:\n\n- `0 <= i, j <= n - 1`\n- The army travels over enemy forts only. Formally, for all `k` where `min(i,j) < k < max(i,j)`, `forts[k] == 0`.\n\nWhile moving the army, all the enemy forts that come in the way are captured.\n\nReturn the maximum number of enemy forts that can be captured. In case it is impossible to move your army, or you do not have any fort under your command, return `0`.\n\n## Example 1\n\n- **Input:** `forts = [1,0,0,-1,0,0,0,0,1]`\n- **Output:** `4`\n- **Explanation:** Moving the army from position 8 to position 3 captures 4 enemy forts (at positions 4, 5, 6, and 7). Moving from position 0 to position 3 captures only 2. The maximum is 4.\n\n## Example 2\n\n- **Input:** `forts = [0,0,1,-1]`\n- **Output:** `0`\n- **Explanation:** Since no enemy fort can be captured, 0 is returned.\n\n## Constraints\n\n- $1 \\leq \\text{forts.length} \\leq 1000$\n- `forts[i]` is `-1`, `0`, or `1`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn capture_forts(forts: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nTwo Pointers\nAcceptance Rate: 43.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn find_next_nonzero(forts: Seq, start: nat) -> nat\n recommends start <= forts.len(),\n decreases forts.len() - start,\n {\n if start >= forts.len() {\n start\n } else if forts[start as int] != 0 {\n start\n } else {\n Self::find_next_nonzero(forts, start + 1)\n }\n }\n\n pub open spec fn scan_spec(forts: Seq, i: nat, best: nat) -> nat\n recommends i <= forts.len(),\n decreases forts.len() - i,\n {\n if i >= forts.len() {\n best\n } else if forts[i as int] == 1 || forts[i as int] == -1 {\n let j = Self::find_next_nonzero(forts, i + 1);\n let new_best = if j < forts.len() && forts[i as int] + forts[j as int] == 0 {\n let count = (j - i - 1) as nat;\n if best < count { count } else { best }\n } else {\n best\n };\n Self::scan_spec(forts, i + 1, new_best)\n } else {\n Self::scan_spec(forts, i + 1, best)\n }\n }\n\n pub open spec fn capture_forts_spec(forts: Seq) -> nat {\n Self::scan_spec(forts, 0, 0)\n }\n\n pub fn capture_forts(forts: Vec) -> (res: i32)\n requires\n 1 <= forts.len() <= 1000,\n forall|i: int| 0 <= i < forts.len() ==>\n (forts[i] == -1 || forts[i] == 0 || forts[i] == 1),\n ensures\n 0 <= res,\n res as nat == Self::capture_forts_spec(forts@),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn capture_forts(forts: Vec) -> i32 {\n let mut best: i32 = 0;\n let mut i: usize = 0;\n\n while i < forts.len() {\n if forts[i] == 1 || forts[i] == -1 {\n let mut j: usize = i + 1;\n while j < forts.len() && forts[j] == 0 {\n j = j + 1;\n }\n if j < forts.len() && forts[i] + forts[j] == 0 {\n let count_usize = j - i - 1;\n let count = count_usize as i32;\n if count > best {\n best = count;\n }\n }\n if j > i + 1 {\n i = j - 1;\n }\n }\n i = i + 1;\n }\n\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn find_next_nonzero(forts: Seq, start: nat) -> nat\n recommends start <= forts.len(),\n decreases forts.len() - start,\n {\n if start >= forts.len() {\n start\n } else if forts[start as int] != 0 {\n start\n } else {\n Self::find_next_nonzero(forts, start + 1)\n }\n }\n\n\n pub open spec fn scan_spec(forts: Seq, i: nat, best: nat) -> nat\n recommends i <= forts.len(),\n decreases forts.len() - i,\n {\n if i >= forts.len() {\n best\n } else if forts[i as int] == 1 || forts[i as int] == -1 {\n let j = Self::find_next_nonzero(forts, i + 1);\n let new_best = if j < forts.len() && forts[i as int] + forts[j as int] == 0 {\n let count = (j - i - 1) as nat;\n if best < count { count } else { best }\n } else {\n best\n };\n Self::scan_spec(forts, i + 1, new_best)\n } else {\n Self::scan_spec(forts, i + 1, best)\n }\n }\n\n pub open spec fn capture_forts_spec(forts: Seq) -> nat {\n Self::scan_spec(forts, 0, 0)\n }\n\n pub fn capture_forts(forts: Vec) -> (res: i32)\n requires\n 1 <= forts.len() <= 1000,\n forall|i: int| 0 <= i < forts.len() ==>\n (forts[i] == -1 || forts[i] == 0 || forts[i] == 1),\n ensures\n 0 <= res,\n res as nat == Self::capture_forts_spec(forts@),\n {\n let mut best: i32 = 0;\n let mut i: usize = 0;\n\n while i < forts.len()\n decreases forts.len() - i,\n {\n\n if forts[i] == 1 || forts[i] == -1 {\n let mut j: usize = i + 1;\n\n while j < forts.len() && forts[j] == 0\n decreases forts.len() - j,\n {\n j = j + 1;\n\n \n }\n\n \n\n if j < forts.len() && forts[i] + forts[j] == 0 {\n let count_usize = j - i - 1;\n\n \n\n let count = count_usize as i32;\n if count > best {\n best = count;\n }\n\n \n } else {\n \n }\n\n \n if j > i + 1 {\n i = j - 1;\n }\n } else {\n \n }\n\n i = i + 1;\n\n \n }\n\n \n\n best\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn find_next_nonzero(forts: Seq, start: nat) -> nat\n recommends start <= forts.len(),\n decreases forts.len() - start,\n {\n if start >= forts.len() {\n start\n } else if forts[start as int] != 0 {\n start\n } else {\n Self::find_next_nonzero(forts, start + 1)\n }\n }\n\n proof fn lemma_scan_spec_skip(forts: Seq, i: nat, best: nat, j: nat)\n requires\n i <= j <= forts.len(),\n forall |k: nat| i <= k < j ==> forts[k as int] == 0,\n ensures\n Self::scan_spec(forts, i, best) == Self::scan_spec(forts, j, best),\n decreases j - i,\n {\n if i == j {\n } else {\n assert(forts[i as int] == 0);\n Self::lemma_scan_spec_skip(forts, i + 1, best, j);\n }\n }\n\n pub open spec fn scan_spec(forts: Seq, i: nat, best: nat) -> nat\n recommends i <= forts.len(),\n decreases forts.len() - i,\n {\n if i >= forts.len() {\n best\n } else if forts[i as int] == 1 || forts[i as int] == -1 {\n let j = Self::find_next_nonzero(forts, i + 1);\n let new_best = if j < forts.len() && forts[i as int] + forts[j as int] == 0 {\n let count = (j - i - 1) as nat;\n if best < count { count } else { best }\n } else {\n best\n };\n Self::scan_spec(forts, i + 1, new_best)\n } else {\n Self::scan_spec(forts, i + 1, best)\n }\n }\n\n pub open spec fn capture_forts_spec(forts: Seq) -> nat {\n Self::scan_spec(forts, 0, 0)\n }\n\n pub fn capture_forts(forts: Vec) -> (res: i32)\n requires\n 1 <= forts.len() <= 1000,\n forall|i: int| 0 <= i < forts.len() ==>\n (forts[i] == -1 || forts[i] == 0 || forts[i] == 1),\n ensures\n 0 <= res,\n res as nat == Self::capture_forts_spec(forts@),\n {\n let mut best: i32 = 0;\n let mut i: usize = 0;\n\n while i < forts.len()\n invariant\n 1 <= forts.len() <= 1000,\n forall|k: int| 0 <= k < forts.len() ==>\n (forts[k] == -1 || forts[k] == 0 || forts[k] == 1),\n 0 <= i <= forts.len(),\n 0 <= best as int <= forts@.len() - 1,\n Self::scan_spec(forts@, i as nat, best as nat) == Self::capture_forts_spec(forts@),\n decreases forts.len() - i,\n {\n let ghost old_i_nat: nat = i as nat;\n let ghost old_best_nat: nat = best as nat;\n\n if forts[i] == 1 || forts[i] == -1 {\n let mut j: usize = i + 1;\n\n while j < forts.len() && forts[j] == 0\n invariant\n 1 <= forts.len() <= 1000,\n forall|k: int| 0 <= k < forts.len() ==>\n (forts[k] == -1 || forts[k] == 0 || forts[k] == 1),\n 0 <= i < forts.len(),\n i + 1 <= j <= forts.len(),\n forall|k: nat| (i + 1) as nat <= k < j as nat ==> forts@[k as int] == 0,\n Self::find_next_nonzero(forts@, j as nat)\n == Self::find_next_nonzero(forts@, (i + 1) as nat),\n decreases forts.len() - j,\n {\n let ghost old_j_nat: nat = j as nat;\n j = j + 1;\n\n proof {\n assert(old_j_nat < forts@.len());\n assert(forts@[old_j_nat as int] == 0);\n assert(Self::find_next_nonzero(forts@, old_j_nat)\n == Self::find_next_nonzero(forts@, old_j_nat + 1));\n }\n }\n\n proof {\n if j == forts.len() {\n assert(Self::find_next_nonzero(forts@, j as nat) == j as nat);\n } else {\n assert(!(j < forts.len() && forts@[j as int] == 0));\n assert(forts@[j as int] != 0);\n assert(Self::find_next_nonzero(forts@, j as nat) == j as nat);\n }\n assert(j as nat == Self::find_next_nonzero(forts@, (i + 1) as nat));\n }\n\n if j < forts.len() && forts[i] + forts[j] == 0 {\n let count_usize = j - i - 1;\n\n proof {\n assert(i + 1 <= j);\n assert(count_usize < forts.len());\n assert(forts.len() <= 1000);\n assert(count_usize <= i32::MAX as usize);\n }\n\n let count = count_usize as i32;\n if count > best {\n best = count;\n }\n\n proof {\n let count_nat = (j as nat - old_i_nat - 1) as nat;\n assert(count_nat == count_usize as nat);\n assert(count as nat == count_nat);\n assert(best as nat == if old_best_nat < count_nat { count_nat } else { old_best_nat });\n assert(count_nat <= forts@.len() - 1);\n assert(best as int <= forts@.len() - 1);\n }\n } else {\n proof {\n assert(best as nat == old_best_nat);\n assert(best as int <= forts@.len() - 1);\n }\n }\n\n proof {\n assert(Self::find_next_nonzero(forts@, old_i_nat + 1) == j as nat);\n assert(forts@[old_i_nat as int] == 1 || forts@[old_i_nat as int] == -1);\n assert(Self::scan_spec(forts@, old_i_nat, old_best_nat)\n == Self::scan_spec(forts@, old_i_nat + 1, best as nat));\n \n Self::lemma_scan_spec_skip(forts@, old_i_nat + 1, best as nat, j as nat);\n assert(Self::scan_spec(forts@, old_i_nat + 1, best as nat) == Self::scan_spec(forts@, j as nat, best as nat));\n }\n if j > i + 1 {\n i = j - 1;\n }\n } else {\n proof {\n assert(forts@[old_i_nat as int] != 1 && forts@[old_i_nat as int] != -1);\n assert(best as nat == old_best_nat);\n assert(Self::scan_spec(forts@, old_i_nat, old_best_nat)\n == Self::scan_spec(forts@, old_i_nat + 1, best as nat));\n }\n }\n\n i = i + 1;\n\n proof {\n assert(Self::scan_spec(forts@, i as nat, best as nat) == Self::capture_forts_spec(forts@)) by {\n assert(Self::scan_spec(forts@, old_i_nat, old_best_nat) == Self::capture_forts_spec(forts@));\n }\n }\n }\n\n proof {\n assert(i == forts.len());\n assert(Self::scan_spec(forts@, forts.len() as nat, best as nat) == best as nat);\n assert(best as nat == Self::capture_forts_spec(forts@));\n }\n\n best\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc2511/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2511/tests/mutated_testcases.jsonl"} {"problem": "lc2520", "family": "leetcode", "description": "# Count the Digits That Divide a Number\n\nGiven an integer `num`, return the number of digits in `num` that divide `num`.\n\nAn integer `val` divides `num` if `num % val == 0`.\n\n## Example 1:\n\n> **Input:** num = 7\n> **Output:** 1\n> **Explanation:** 7 divides itself, hence the answer is 1.\n\n## Example 2:\n\n> **Input:** num = 121\n> **Output:** 2\n> **Explanation:** 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.\n\n## Example 3:\n\n> **Input:** num = 1248\n> **Output:** 4\n> **Explanation:** 1248 is divisible by all of its digits, hence the answer is 4.\n\n## Constraints:\n\n- $1 <= num <= 10^9$\n- `num` does not contain `0` as one of its digits.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_digits(num: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Math, Weekly Contest 326\nAcceptance rate: 85.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn has_no_zero_digits(n: nat) -> bool\n decreases n,\n {\n if n == 0 {\n true\n } else {\n n % 10 != 0 && Self::has_no_zero_digits(n / 10)\n }\n }\n\n pub open spec fn digit_contrib_spec(orig: nat, digit: nat) -> nat {\n if digit == 0 {\n 0\n } else if orig % digit == 0 {\n 1\n } else {\n 0\n }\n }\n\n pub open spec fn count_digits_bounded(orig: nat, rem: nat, digits: nat) -> nat\n decreases digits,\n {\n if digits == 0 {\n 0\n } else {\n Self::digit_contrib_spec(orig, rem % 10)\n + Self::count_digits_bounded(orig, rem / 10, (digits - 1) as nat)\n }\n }\n\n pub open spec fn count_digits_spec(num: nat) -> nat {\n Self::count_digits_bounded(num, num, 10)\n }\n\n fn digit_contrib(orig: u128, digit: u128) -> (result: i32)\n ensures\n result as nat == Self::digit_contrib_spec(orig as nat, digit as nat),\n 0 <= result <= 1,\n {\n \n }\n\n pub fn count_digits(num: i32) -> (result: i32)\n requires\n 1 <= num <= 1_000_000_000,\n Self::has_no_zero_digits(num as nat),\n ensures\n result as nat == Self::count_digits_spec(num as nat),\n 0 <= result <= 10,\n {\n \n }\n}\n\n} \n", "code": "impl Solution {\n fn digit_contrib(orig: u128, digit: u128) -> i32\n {\n if digit == 0 {\n 0\n } else if orig % digit == 0 {\n 1\n } else {\n 0\n }\n }\n\n pub fn count_digits(num: i32) -> i32\n {\n let orig = num as u128;\n let mut rem = orig;\n let mut digits: u8 = 10;\n let mut count: i32 = 0;\n while digits > 0\n {\n let old_count = count;\n let old_digits = digits;\n let old_rem = rem;\n let digit = rem % 10;\n let add = Self::digit_contrib(orig, digit);\n count = count + add;\n rem = rem / 10;\n digits = digits - 1;\n }\n count\n }\n}\n\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn has_no_zero_digits(n: nat) -> bool\n decreases n,\n {\n if n == 0 {\n true\n } else {\n n % 10 != 0 && Self::has_no_zero_digits(n / 10)\n }\n }\n\n pub open spec fn digit_contrib_spec(orig: nat, digit: nat) -> nat {\n if digit == 0 {\n 0\n } else if orig % digit == 0 {\n 1\n } else {\n 0\n }\n }\n\n pub open spec fn count_digits_bounded(orig: nat, rem: nat, digits: nat) -> nat\n decreases digits,\n {\n if digits == 0 {\n 0\n } else {\n Self::digit_contrib_spec(orig, rem % 10)\n + Self::count_digits_bounded(orig, rem / 10, (digits - 1) as nat)\n }\n }\n\n pub open spec fn count_digits_spec(num: nat) -> nat {\n Self::count_digits_bounded(num, num, 10)\n }\n\n fn digit_contrib(orig: u128, digit: u128) -> (result: i32)\n ensures\n result as nat == Self::digit_contrib_spec(orig as nat, digit as nat),\n 0 <= result <= 1,\n {\n if digit == 0 {\n 0\n } else if orig % digit == 0 {\n 1\n } else {\n 0\n }\n }\n\n pub fn count_digits(num: i32) -> (result: i32)\n requires\n 1 <= num <= 1_000_000_000,\n Self::has_no_zero_digits(num as nat),\n ensures\n result as nat == Self::count_digits_spec(num as nat),\n 0 <= result <= 10,\n {\n let orig = num as u128;\n let mut rem = orig;\n let mut digits: u8 = 10;\n let mut count: i32 = 0;\n while digits > 0\n {\n let old_count = count;\n let old_digits = digits;\n let old_rem = rem;\n let digit = rem % 10;\n let add = Self::digit_contrib(orig, digit);\n count = count + add;\n rem = rem / 10;\n digits = digits - 1;\n }\n count\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn has_no_zero_digits(n: nat) -> bool\n decreases n,\n {\n if n == 0 {\n true\n } else {\n n % 10 != 0 && Self::has_no_zero_digits(n / 10)\n }\n }\n\n pub open spec fn digit_contrib_spec(orig: nat, digit: nat) -> nat {\n if digit == 0 {\n 0\n } else if orig % digit == 0 {\n 1\n } else {\n 0\n }\n }\n\n pub open spec fn count_digits_bounded(orig: nat, rem: nat, digits: nat) -> nat\n decreases digits,\n {\n if digits == 0 {\n 0\n } else {\n Self::digit_contrib_spec(orig, rem % 10)\n + Self::count_digits_bounded(orig, rem / 10, (digits - 1) as nat)\n }\n }\n\n pub open spec fn count_digits_spec(num: nat) -> nat {\n Self::count_digits_bounded(num, num, 10)\n }\n\n fn digit_contrib(orig: u128, digit: u128) -> (result: i32)\n ensures\n result as nat == Self::digit_contrib_spec(orig as nat, digit as nat),\n 0 <= result <= 1,\n {\n if digit == 0 {\n 0\n } else if orig % digit == 0 {\n 1\n } else {\n 0\n }\n }\n\n pub fn count_digits(num: i32) -> (result: i32)\n requires\n 1 <= num <= 1_000_000_000,\n Self::has_no_zero_digits(num as nat),\n ensures\n result as nat == Self::count_digits_spec(num as nat),\n 0 <= result <= 10,\n {\n let orig = num as u128;\n let mut rem = orig;\n let mut digits: u8 = 10;\n let mut count: i32 = 0;\n while digits > 0\n invariant\n digits <= 10,\n 0 <= count,\n count as nat + Self::count_digits_bounded(orig as nat, rem as nat, digits as nat)\n == Self::count_digits_spec(orig as nat),\n count as nat + digits as nat <= 10,\n decreases digits,\n {\n let old_count = count;\n let old_digits = digits;\n let old_rem = rem;\n let digit = rem % 10;\n let add = Self::digit_contrib(orig, digit);\n count = count + add;\n rem = rem / 10;\n digits = digits - 1;\n }\n count\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc2520/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2520/tests/mutated_testcases.jsonl"} {"problem": "lc2529", "family": "leetcode", "description": "# Maximum Count of Positive Integer and Negative Integer\n\nGiven an array `nums` sorted in **non-decreasing** order, return the maximum between the number of positive integers and the number of negative integers. In other words, if the number of positive integers in `nums` is `pos` and the number of negative integers is `neg`, then return the maximum of `pos` and `neg`.\n\n**Note** that `0` is neither positive nor negative.\n\n## Example 1:\n\n> **Input:** nums = [-2,-1,-1,1,2,3]\n> **Output:** 3\n> **Explanation:** There are 3 positive integers and 3 negative integers. The maximum count among them is 3.\n\n## Example 2:\n\n> **Input:** nums = [-3,-2,-1,0,0,1,2]\n> **Output:** 3\n> **Explanation:** There are 2 positive integers and 3 negative integers. The maximum count among them is 3.\n\n## Example 3:\n\n> **Input:** nums = [5,20,66,1314]\n> **Output:** 4\n> **Explanation:** There are 4 positive integers and 0 negative integers. The maximum count among them is 4.\n\n## Constraints:\n\n- `1 <= nums.length <= 2000`\n- `-2000 <= nums[i] <= 2000`\n- `nums` is sorted in **non-decreasing** order.\n\n**Follow up:** Can you solve the problem in `O(log(n))` time complexity?\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn maximum_count(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Binary Search, Counting\nAcceptance rate: 74.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn count_neg(nums: Seq) -> int\n decreases nums.len()\n{\n if nums.len() == 0 {\n 0\n } else if nums.last() < 0 {\n count_neg(nums.drop_last()) + 1\n } else {\n count_neg(nums.drop_last())\n }\n}\n\npub open spec fn count_pos(nums: Seq) -> int\n decreases nums.len()\n{\n if nums.len() == 0 {\n 0\n } else if nums.last() > 0 {\n count_pos(nums.drop_last()) + 1\n } else {\n count_pos(nums.drop_last())\n }\n}\n\nimpl Solution {\n pub fn maximum_count(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> -2000 <= #[trigger] nums[i] <= 2000,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] <= nums[j],\n ensures\n result == if count_pos(nums@) >= count_neg(nums@) { count_pos(nums@) as i32 } else { count_neg(nums@) as i32 },\n {\n }\n}\n}\n", "code": "impl Solution {\n pub fn maximum_count(nums: Vec) -> i32 {\n let mut neg: i32 = 0;\n let mut pos: i32 = 0;\n let mut i = 0;\n while i < nums.len() {\n if nums[i] < 0 {\n neg += 1;\n } else if nums[i] > 0 {\n pos += 1;\n }\n i += 1;\n }\n if pos >= neg { pos } else { neg }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn count_neg(nums: Seq) -> int\n decreases nums.len()\n{\n if nums.len() == 0 {\n 0\n } else if nums.last() < 0 {\n count_neg(nums.drop_last()) + 1\n } else {\n count_neg(nums.drop_last())\n }\n}\n\npub open spec fn count_pos(nums: Seq) -> int\n decreases nums.len()\n{\n if nums.len() == 0 {\n 0\n } else if nums.last() > 0 {\n count_pos(nums.drop_last()) + 1\n } else {\n count_pos(nums.drop_last())\n }\n}\n\nimpl Solution {\n pub fn maximum_count(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> -2000 <= #[trigger] nums[i] <= 2000,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] <= nums[j],\n ensures\n result == if count_pos(nums@) >= count_neg(nums@) { count_pos(nums@) as i32 } else { count_neg(nums@) as i32 },\n {\n let mut neg: i32 = 0;\n let mut pos: i32 = 0;\n let mut i = 0;\n while i < nums.len() {\n if nums[i] < 0 {\n neg += 1;\n } else if nums[i] > 0 {\n pos += 1;\n }\n i += 1;\n }\n if pos >= neg { pos } else { neg }\n }\n}\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\npub struct Solution;\n\npub open spec fn count_neg(nums: Seq) -> int\n decreases nums.len()\n{\n if nums.len() == 0 {\n 0\n } else if nums.last() < 0 {\n count_neg(nums.drop_last()) + 1\n } else {\n count_neg(nums.drop_last())\n }\n}\n\npub open spec fn count_pos(nums: Seq) -> int\n decreases nums.len()\n{\n if nums.len() == 0 {\n 0\n } else if nums.last() > 0 {\n count_pos(nums.drop_last()) + 1\n } else {\n count_pos(nums.drop_last())\n }\n}\n\nimpl Solution {\n pub fn maximum_count(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> -2000 <= #[trigger] nums[i] <= 2000,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] <= nums[j],\n ensures\n result == if count_pos(nums@) >= count_neg(nums@) { count_pos(nums@) as i32 } else { count_neg(nums@) as i32 },\n {\n let mut neg: i32 = 0;\n let mut pos: i32 = 0;\n let mut i = 0;\n while i < nums.len()\n invariant\n 0 <= i <= nums.len(),\n 1 <= nums.len() <= 2000,\n neg == count_neg(nums@.take(i as int)),\n pos == count_pos(nums@.take(i as int)),\n 0 <= neg <= i,\n 0 <= pos <= i\n decreases nums.len() - i\n {\n proof {\n let old_take = nums@.take(i as int);\n let new_take = nums@.take((i + 1) as int);\n assert(new_take.drop_last() =~= old_take);\n assert(new_take.last() == nums[i as int]);\n }\n if nums[i] < 0 {\n neg += 1;\n } else if nums[i] > 0 {\n pos += 1;\n }\n i += 1;\n }\n proof {\n assert(nums@.take(nums.len() as int) =~= nums@);\n }\n if pos >= neg { pos } else { neg }\n }\n}\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2529/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2529/tests/mutated_testcases.jsonl"} {"problem": "lc2535", "family": "leetcode", "description": "# Difference Between Element Sum and Digit Sum of an Array\n\nYou are given a positive integer array `nums`.\n\n- The **element sum** is the sum of all the elements in `nums`.\n- The **digit sum** is the sum of all the digits (not necessarily distinct) that appear in `nums`.\n\nReturn *the **absolute** difference between the **element sum** and **digit sum** of *`nums`.\n\n**Note** that the absolute difference between two integers `x` and `y` is defined as `|x - y|`.\n\n## Example 1:\n\n> **Input:** `nums = [1,15,6,3]`\n> **Output:** `9`\n> **Explanation:** \n> The element sum of nums is `1 + 15 + 6 + 3 = 25`.\n> The digit sum of nums is `1 + 1 + 5 + 6 + 3 = 16`.\n> The absolute difference between the element sum and digit sum is `|25 - 16| = 9`.\n\n## Example 2:\n\n> **Input:** `nums = [1,2,3,4]`\n> **Output:** `0`\n> **Explanation:**\n> The element sum of nums is `1 + 2 + 3 + 4 = 10`.\n> The digit sum of nums is `1 + 2 + 3 + 4 = 10`.\n> The absolute difference between the element sum and digit sum is `|10 - 10| = 0`.\n\n## Constraints:\n\n- `1 <= nums.length <= 2000`\n- `1 <= nums[i] <= 2000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn difference_of_sum(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Math\nAcceptance rate: 85.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n (n % 10) + Self::digit_sum(n / 10)\n }\n }\n\n pub open spec fn array_element_sum(nums: Seq, idx: int) -> int\n decreases idx,\n {\n if idx <= 0 {\n 0\n } else {\n Self::array_element_sum(nums, idx - 1) + nums[idx - 1] as int\n }\n }\n\n pub open spec fn array_digit_sum(nums: Seq, idx: int) -> int\n decreases idx,\n {\n if idx <= 0 {\n 0\n } else {\n Self::array_digit_sum(nums, idx - 1) + Self::digit_sum(nums[idx - 1] as int)\n }\n }\n\n pub open spec fn abs_diff(a: int, b: int) -> int {\n if a > b { a - b } else { b - a }\n }\n\n pub fn difference_of_sum(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 2000,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 2000,\n ensures\n result == Self::abs_diff(\n Self::array_element_sum(nums@, nums.len() as int), \n Self::array_digit_sum(nums@, nums.len() as int)\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn difference_of_sum(nums: Vec) -> i32 {\n let mut elem_sum: i32 = 0;\n let mut digit_sum: i32 = 0;\n let mut i: usize = 0;\n let n: usize = nums.len();\n\n while i < n {\n let mut val = nums[i];\n elem_sum += val;\n\n let mut internal_digit_sum: i32 = 0;\n \n while val > 0 {\n let d = (val as u32 % 10) as i32;\n internal_digit_sum += d;\n val = (val as u32 / 10) as i32;\n }\n \n digit_sum += internal_digit_sum;\n i += 1;\n }\n\n if elem_sum > digit_sum {\n elem_sum - digit_sum\n } else {\n digit_sum - elem_sum\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n (n % 10) + Self::digit_sum(n / 10)\n }\n }\n\n pub open spec fn array_element_sum(nums: Seq, idx: int) -> int\n decreases idx,\n {\n if idx <= 0 {\n 0\n } else {\n Self::array_element_sum(nums, idx - 1) + nums[idx - 1] as int\n }\n }\n\n pub open spec fn array_digit_sum(nums: Seq, idx: int) -> int\n decreases idx,\n {\n if idx <= 0 {\n 0\n } else {\n Self::array_digit_sum(nums, idx - 1) + Self::digit_sum(nums[idx - 1] as int)\n }\n }\n\n pub open spec fn abs_diff(a: int, b: int) -> int {\n if a > b { a - b } else { b - a }\n }\n\n pub fn difference_of_sum(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 2000,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 2000,\n ensures\n result == Self::abs_diff(\n Self::array_element_sum(nums@, nums.len() as int), \n Self::array_digit_sum(nums@, nums.len() as int)\n ),\n {\n let mut elem_sum: i32 = 0;\n let mut digit_sum: i32 = 0;\n let mut i: usize = 0;\n let n: usize = nums.len();\n\n while i < n {\n let mut val = nums[i];\n elem_sum += val;\n\n let mut internal_digit_sum: i32 = 0;\n \n while val > 0 {\n let d = (val as u32 % 10) as i32;\n internal_digit_sum += d;\n val = (val as u32 / 10) as i32;\n }\n \n digit_sum += internal_digit_sum;\n i += 1;\n }\n\n if elem_sum > digit_sum {\n elem_sum - digit_sum\n } else {\n digit_sum - elem_sum\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n (n % 10) + Self::digit_sum(n / 10)\n }\n }\n\n pub open spec fn array_element_sum(nums: Seq, idx: int) -> int\n decreases idx,\n {\n if idx <= 0 {\n 0\n } else {\n Self::array_element_sum(nums, idx - 1) + nums[idx - 1] as int\n }\n }\n\n pub open spec fn array_digit_sum(nums: Seq, idx: int) -> int\n decreases idx,\n {\n if idx <= 0 {\n 0\n } else {\n Self::array_digit_sum(nums, idx - 1) + Self::digit_sum(nums[idx - 1] as int)\n }\n }\n\n pub open spec fn abs_diff(a: int, b: int) -> int {\n if a > b { a - b } else { b - a }\n }\n\n pub fn difference_of_sum(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 2000,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 2000,\n ensures\n result == Self::abs_diff(\n Self::array_element_sum(nums@, nums.len() as int), \n Self::array_digit_sum(nums@, nums.len() as int)\n ),\n {\n let mut elem_sum: i32 = 0;\n let mut digit_sum: i32 = 0;\n let mut i: usize = 0;\n let n: usize = nums.len();\n\n while i < n \n invariant\n 1 <= n <= 2000,\n n == nums.len(),\n 0 <= i <= n,\n 0 <= elem_sum <= i * 2000,\n 0 <= digit_sum <= i * 2000, \n forall|j: int| 0 <= j < n ==> 1 <= #[trigger] nums[j] <= 2000,\n elem_sum as int == Self::array_element_sum(nums@, i as int),\n digit_sum as int == Self::array_digit_sum(nums@, i as int),\n decreases n - i,\n {\n let mut val = nums[i];\n elem_sum += val;\n\n let mut internal_digit_sum: i32 = 0;\n \n while val > 0\n invariant\n 0 <= i < nums.len(),\n 1 <= nums[i as int] <= 2000,\n 0 <= val <= nums[i as int],\n 0 <= internal_digit_sum,\n internal_digit_sum + val <= nums[i as int],\n internal_digit_sum as int + Self::digit_sum(val as int) == Self::digit_sum(nums[i as int] as int),\n decreases val,\n {\n let d = (val as u32 % 10) as i32;\n internal_digit_sum += d;\n val = (val as u32 / 10) as i32;\n \n proof {\n \n }\n }\n \n digit_sum += internal_digit_sum;\n i += 1;\n }\n\n if elem_sum > digit_sum {\n elem_sum - digit_sum\n } else {\n digit_sum - elem_sum\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2535/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2535/tests/mutated_testcases.jsonl"} {"problem": "lc2540", "family": "leetcode", "description": "# Minimum Common Value\n\nGiven two integer arrays `nums1` and `nums2`, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer among `nums1` and `nums2`, return `-1`.\n\nNote that an integer is common to `nums1` and `nums2` if both arrays have at least one occurrence of that integer.\n\n## Example 1:\n\n> **Input:** nums1 = [1,2,3], nums2 = [2,4]\n> **Output:** 2\n> **Explanation:** The smallest element common to both arrays is 2, so we return 2.\n\n## Example 2:\n\n> **Input:** nums1 = [1,2,3,6], nums2 = [2,3,4,5]\n> **Output:** 2\n> **Explanation:** There are two common elements, 2 and 3, and 2 is the smallest.\n\n## Constraints:\n\n- `1 <= nums1.length, nums2.length <= 10^5`\n- `1 <= nums1[i], nums2[j] <= 10^9`\n- Both `nums1` and `nums2` are sorted in non-decreasing order.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn get_common(nums1: Vec, nums2: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Array, Hash Table, Two Pointers, Binary Search, Biweekly Contest 96\nAcceptance rate: 58.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn get_common(nums1: Vec, nums2: Vec) -> (res: i32)\n requires\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n forall |i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 1_000_000_000,\n forall |i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 1_000_000_000,\n forall |i: int, j: int| 0 <= i < j < nums1.len() ==> nums1[i] <= nums1[j],\n forall |i: int, j: int| 0 <= i < j < nums2.len() ==> nums2[i] <= nums2[j],\n ensures\n res == -1 <==> forall |x: i32| #[trigger] nums1@.contains(x) ==> !nums2@.contains(x),\n res != -1 ==> nums1@.contains(res) && nums2@.contains(res),\n res != -1 ==> forall |x: i32| nums1@.contains(x) && nums2@.contains(x) ==> res <= x,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn get_common(nums1: Vec, nums2: Vec) -> i32 {\n let n1 = nums1.len();\n let n2 = nums2.len();\n let mut i: usize = 0;\n let mut j: usize = 0;\n\n while i < n1 && j < n2 {\n if nums1[i] == nums2[j] {\n return nums1[i];\n } else if nums1[i] < nums2[j] {\n i = i + 1;\n } else {\n j = j + 1;\n }\n }\n\n -1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn get_common(nums1: Vec, nums2: Vec) -> (res: i32)\n requires\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n forall |i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 1_000_000_000,\n forall |i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 1_000_000_000,\n forall |i: int, j: int| 0 <= i < j < nums1.len() ==> nums1[i] <= nums1[j],\n forall |i: int, j: int| 0 <= i < j < nums2.len() ==> nums2[i] <= nums2[j],\n ensures\n res == -1 <==> forall |x: i32| #[trigger] nums1@.contains(x) ==> !nums2@.contains(x),\n res != -1 ==> nums1@.contains(res) && nums2@.contains(res),\n res != -1 ==> forall |x: i32| nums1@.contains(x) && nums2@.contains(x) ==> res <= x,\n {\n let n1 = nums1.len();\n let n2 = nums2.len();\n let mut i: usize = 0;\n let mut j: usize = 0;\n\n while i < n1 && j < n2 {\n if nums1[i] == nums2[j] {\n return nums1[i];\n } else if nums1[i] < nums2[j] {\n i = i + 1;\n } else {\n j = j + 1;\n }\n }\n\n -1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn get_common(nums1: Vec, nums2: Vec) -> (res: i32)\n requires\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n forall |i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 1_000_000_000,\n forall |i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 1_000_000_000,\n forall |i: int, j: int| 0 <= i < j < nums1.len() ==> nums1[i] <= nums1[j],\n forall |i: int, j: int| 0 <= i < j < nums2.len() ==> nums2[i] <= nums2[j],\n ensures\n res == -1 <==> forall |x: i32| #[trigger] nums1@.contains(x) ==> !nums2@.contains(x),\n res != -1 ==> nums1@.contains(res) && nums2@.contains(res),\n res != -1 ==> forall |x: i32| nums1@.contains(x) && nums2@.contains(x) ==> res <= x,\n {\n let n1 = nums1.len();\n let n2 = nums2.len();\n let mut i: usize = 0;\n let mut j: usize = 0;\n\n while i < n1 && j < n2\n invariant\n 1 <= nums1.len() <= 100_000,\n 1 <= nums2.len() <= 100_000,\n n1 == nums1.len(),\n n2 == nums2.len(),\n 0 <= i <= n1,\n 0 <= j <= n2,\n forall |k: int| 0 <= k < nums1.len() ==> 1 <= #[trigger] nums1[k] <= 1_000_000_000,\n forall |k: int| 0 <= k < nums2.len() ==> 1 <= #[trigger] nums2[k] <= 1_000_000_000,\n forall |k: int, l: int| 0 <= k < l < nums1.len() ==> nums1[k] <= nums1[l],\n forall |k: int, l: int| 0 <= k < l < nums2.len() ==> nums2[k] <= nums2[l],\n forall |p: int, q: int| 0 <= p < i && 0 <= q < n2 ==> nums1@[p] != nums2@[q],\n forall |p: int, q: int| 0 <= p < n1 && 0 <= q < j ==> nums1@[p] != nums2@[q],\n decreases n1 - i + n2 - j\n {\n if nums1[i] == nums2[j] {\n assert(nums1@.contains(nums1@[i as int]));\n assert(nums2@.contains(nums2@[j as int]));\n assert forall |x: i32| nums1@.contains(x) && nums2@.contains(x) implies nums1@[i as int] <= x by {\n if nums1@.contains(x) && nums2@.contains(x) {\n let p = choose |p: int| 0 <= p < n1 && nums1@[p] == x;\n let q = choose |q: int| 0 <= q < n2 && nums2@[q] == x;\n if p < (i as int) {\n assert(nums1@[p] != nums2@[q]);\n assert(nums1@[p] == nums2@[q]);\n }\n if q < (j as int) {\n assert(nums1@[p] != nums2@[q]);\n assert(nums1@[p] == nums2@[q]);\n }\n assert((i as int) <= p);\n if (i as int) == p {\n assert(nums1@[i as int] <= nums1@[p]);\n } else {\n assert((i as int) < p);\n assert(nums1@[i as int] <= nums1@[p]);\n }\n assert(nums1@[p] == x);\n }\n }\n return nums1[i];\n } else if nums1[i] < nums2[j] {\n assert forall |q: int| 0 <= q < n2 implies nums1@[i as int] != nums2@[q] by {\n if 0 <= q < n2 {\n if q < (j as int) {\n assert(nums1@[i as int] != nums2@[q]);\n } else {\n assert(nums2@[j as int] <= nums2@[q]);\n assert(nums1@[i as int] < nums2@[j as int]);\n }\n }\n }\n i = i + 1;\n } else {\n assert forall |p: int| 0 <= p < n1 implies nums1@[p] != nums2@[j as int] by {\n if 0 <= p < n1 {\n if p < (i as int) {\n assert(nums1@[p] != nums2@[j as int]);\n } else {\n assert(nums1@[i as int] <= nums1@[p]);\n assert(nums2@[j as int] < nums1@[i as int]);\n }\n }\n }\n j = j + 1;\n }\n }\n\n assert(i == n1 || j == n2);\n assert forall |x: i32| nums1@.contains(x) implies !nums2@.contains(x) by {\n if nums1@.contains(x) {\n if nums2@.contains(x) {\n let p = choose |p: int| 0 <= p < n1 && nums1@[p] == x;\n let q = choose |q: int| 0 <= q < n2 && nums2@[q] == x;\n if i == n1 {\n assert(p < (i as int));\n assert(nums1@[p] != nums2@[q]);\n } else {\n assert(j == n2);\n assert(q < (j as int));\n assert(nums1@[p] != nums2@[q]);\n }\n assert(nums1@[p] == nums2@[q]);\n }\n }\n }\n\n -1\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2540/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2540/tests/mutated_testcases.jsonl"} {"problem": "lc2544", "family": "leetcode", "description": "# Alternating Digit Sum\n\nYou are given a positive integer `n`. Each digit of `n` has a sign according to the following rules:\n\n- The most significant digit is assigned a positive sign.\n- Each other digit has an opposite sign to its adjacent digits.\n\nReturn the sum of all digits with their corresponding sign.\n\n## Example 1:\n\n> **Input:** n = 521\n> **Output:** 4\n> **Explanation:** (+5) + (-2) + (+1) = 4.\n\n## Example 2:\n\n> **Input:** n = 111\n> **Output:** 1\n> **Explanation:** (+1) + (-1) + (+1) = 1.\n\n## Example 3:\n\n> **Input:** n = 886996\n> **Output:** 0\n> **Explanation:** (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.\n\n## Constraints:\n\n- `1 <= n <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn alternate_digit_sum(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 69.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_count_10(n: int) -> nat\n recommends\n 1 <= n <= 1_000_000_000,\n {\n if n >= 1_000_000_000 {\n 10\n } else if n >= 100_000_000 {\n 9\n } else if n >= 10_000_000 {\n 8\n } else if n >= 1_000_000 {\n 7\n } else if n >= 100_000 {\n 6\n } else if n >= 10_000 {\n 5\n } else if n >= 1_000 {\n 4\n } else if n >= 100 {\n 3\n } else if n >= 10 {\n 2\n } else {\n 1\n }\n }\n\n pub open spec fn alternating_suffix_sum(rem: nat, digits: nat, sign: int) -> int\n decreases digits,\n {\n if digits == 0 {\n 0\n } else {\n sign * (rem % 10) as int\n + Self::alternating_suffix_sum(rem / 10, (digits - 1) as nat, -sign)\n }\n }\n\n pub open spec fn alternating_digit_sum_spec(n: nat) -> int\n recommends\n 1 <= n <= 1_000_000_000,\n {\n let d = Self::digit_count_10(n as int);\n let s = if d % 2 == 0 { -1 } else { 1 };\n Self::alternating_suffix_sum(n, d, s)\n }\n\n pub fn alternate_digit_sum(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1_000_000_000,\n ensures\n result as int == Self::alternating_digit_sum_spec(n as nat),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn alternate_digit_sum(n: i32) -> i32 {\n let mut digits: i32;\n if n >= 1_000_000_000 {\n digits = 10;\n } else if n >= 100_000_000 {\n digits = 9;\n } else if n >= 10_000_000 {\n digits = 8;\n } else if n >= 1_000_000 {\n digits = 7;\n } else if n >= 100_000 {\n digits = 6;\n } else if n >= 10_000 {\n digits = 5;\n } else if n >= 1_000 {\n digits = 4;\n } else if n >= 100 {\n digits = 3;\n } else if n >= 10 {\n digits = 2;\n } else {\n digits = 1;\n }\n\n let mut rem: i32 = n;\n let mut sign: i32;\n if digits % 2 == 0 {\n sign = -1;\n } else {\n sign = 1;\n }\n let mut ans: i32 = 0;\n\n while digits > 0 {\n let old_ans = ans;\n let old_digits = digits;\n let old_rem = rem;\n let old_sign = sign;\n let digit = rem % 10;\n ans = ans + sign * digit;\n rem = rem / 10;\n sign = -sign;\n digits = digits - 1;\n }\n\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_count_10(n: int) -> nat\n recommends\n 1 <= n <= 1_000_000_000,\n {\n if n >= 1_000_000_000 {\n 10\n } else if n >= 100_000_000 {\n 9\n } else if n >= 10_000_000 {\n 8\n } else if n >= 1_000_000 {\n 7\n } else if n >= 100_000 {\n 6\n } else if n >= 10_000 {\n 5\n } else if n >= 1_000 {\n 4\n } else if n >= 100 {\n 3\n } else if n >= 10 {\n 2\n } else {\n 1\n }\n }\n\n pub open spec fn alternating_suffix_sum(rem: nat, digits: nat, sign: int) -> int\n decreases digits,\n {\n if digits == 0 {\n 0\n } else {\n sign * (rem % 10) as int\n + Self::alternating_suffix_sum(rem / 10, (digits - 1) as nat, -sign)\n }\n }\n\n pub open spec fn alternating_digit_sum_spec(n: nat) -> int\n recommends\n 1 <= n <= 1_000_000_000,\n {\n let d = Self::digit_count_10(n as int);\n let s = if d % 2 == 0 { -1 } else { 1 };\n Self::alternating_suffix_sum(n, d, s)\n }\n\n pub fn alternate_digit_sum(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1_000_000_000,\n ensures\n result as int == Self::alternating_digit_sum_spec(n as nat),\n {\n let mut digits: i32;\n if n >= 1_000_000_000 {\n digits = 10;\n } else if n >= 100_000_000 {\n digits = 9;\n } else if n >= 10_000_000 {\n digits = 8;\n } else if n >= 1_000_000 {\n digits = 7;\n } else if n >= 100_000 {\n digits = 6;\n } else if n >= 10_000 {\n digits = 5;\n } else if n >= 1_000 {\n digits = 4;\n } else if n >= 100 {\n digits = 3;\n } else if n >= 10 {\n digits = 2;\n } else {\n digits = 1;\n }\n\n let mut rem: i32 = n;\n let mut sign: i32;\n if digits % 2 == 0 {\n sign = -1;\n } else {\n sign = 1;\n }\n let mut ans: i32 = 0;\n\n while digits > 0 {\n let old_ans = ans;\n let old_digits = digits;\n let old_rem = rem;\n let old_sign = sign;\n let digit = rem % 10;\n ans = ans + sign * digit;\n rem = rem / 10;\n sign = -sign;\n digits = digits - 1;\n }\n\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_count_10(n: int) -> nat\n recommends\n 1 <= n <= 1_000_000_000,\n {\n if n >= 1_000_000_000 {\n 10\n } else if n >= 100_000_000 {\n 9\n } else if n >= 10_000_000 {\n 8\n } else if n >= 1_000_000 {\n 7\n } else if n >= 100_000 {\n 6\n } else if n >= 10_000 {\n 5\n } else if n >= 1_000 {\n 4\n } else if n >= 100 {\n 3\n } else if n >= 10 {\n 2\n } else {\n 1\n }\n }\n\n pub open spec fn alternating_suffix_sum(rem: nat, digits: nat, sign: int) -> int\n decreases digits,\n {\n if digits == 0 {\n 0\n } else {\n sign * (rem % 10) as int\n + Self::alternating_suffix_sum(rem / 10, (digits - 1) as nat, -sign)\n }\n }\n\n pub open spec fn alternating_digit_sum_spec(n: nat) -> int\n recommends\n 1 <= n <= 1_000_000_000,\n {\n let d = Self::digit_count_10(n as int);\n let s = if d % 2 == 0 { -1 } else { 1 };\n Self::alternating_suffix_sum(n, d, s)\n }\n\n pub fn alternate_digit_sum(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1_000_000_000,\n ensures\n result as int == Self::alternating_digit_sum_spec(n as nat),\n {\n let mut digits: i32;\n if n >= 1_000_000_000 {\n digits = 10;\n } else if n >= 100_000_000 {\n digits = 9;\n } else if n >= 10_000_000 {\n digits = 8;\n } else if n >= 1_000_000 {\n digits = 7;\n } else if n >= 100_000 {\n digits = 6;\n } else if n >= 10_000 {\n digits = 5;\n } else if n >= 1_000 {\n digits = 4;\n } else if n >= 100 {\n digits = 3;\n } else if n >= 10 {\n digits = 2;\n } else {\n digits = 1;\n }\n\n proof {\n assert(digits as int == Self::digit_count_10(n as int));\n }\n\n let mut rem: i32 = n;\n let mut sign: i32;\n if digits % 2 == 0 {\n sign = -1;\n } else {\n sign = 1;\n }\n let mut ans: i32 = 0;\n\n while digits > 0\n invariant\n 1 <= n as int <= 1_000_000_000,\n 0 <= rem,\n 0 <= digits <= 10,\n sign == 1 || sign == -1,\n -9 * (10 - digits as int) <= ans as int <= 9 * (10 - digits as int),\n ans as int + Self::alternating_suffix_sum(rem as nat, digits as nat, sign as int)\n == Self::alternating_digit_sum_spec(n as nat),\n decreases digits,\n {\n let old_ans = ans;\n let old_digits = digits;\n let old_rem = rem;\n let old_sign = sign;\n let digit = rem % 10;\n proof {\n assert(0 <= digit <= 9);\n assert(Self::alternating_suffix_sum(old_rem as nat, old_digits as nat, old_sign as int)\n == old_sign as int * (old_rem as int % 10)\n + Self::alternating_suffix_sum((old_rem / 10) as nat, (old_digits - 1) as nat, -(old_sign as int)));\n assert(-9 <= old_sign * digit <= 9);\n assert(-9 * (10 - (old_digits as int - 1))\n <= old_ans + old_sign * digit\n <= 9 * (10 - (old_digits as int - 1))) by (nonlinear_arith)\n requires\n -9 * (10 - old_digits as int) <= old_ans <= 9 * (10 - old_digits as int),\n -9 <= old_sign * digit <= 9,\n ;\n }\n ans = ans + sign * digit;\n rem = rem / 10;\n sign = -sign;\n digits = digits - 1;\n proof {\n assert(ans as int + Self::alternating_suffix_sum(rem as nat, digits as nat, sign as int)\n == Self::alternating_digit_sum_spec(n as nat));\n }\n }\n\n proof {\n assert(digits == 0);\n assert(ans as int + Self::alternating_suffix_sum(rem as nat, digits as nat, sign as int)\n == Self::alternating_digit_sum_spec(n as nat));\n assert(Self::alternating_suffix_sum(rem as nat, 0, sign as int) == 0);\n assert(ans as int == Self::alternating_digit_sum_spec(n as nat));\n }\n\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2544/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2544/tests/mutated_testcases.jsonl"} {"problem": "lc2553", "family": "leetcode", "description": "# Separate the Digits in an Array\n\nGiven an array of positive integers `nums`, return *an array *`answer`* that consists of the digits of each integer in *`nums`* after separating them in **the same order** they appear in *`nums`.\n\nTo separate the digits of an integer is to get all the digits it has in the same order.\n\n\t- For example, for the integer `10921`, the separation of its digits is `[1,0,9,2,1]`.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [13,25,83,77]\n**Output:** [1,3,2,5,8,3,7,7]\n**Explanation:** \n- The separation of 13 is [1,3].\n- The separation of 25 is [2,5].\n- The separation of 83 is [8,3].\n- The separation of 77 is [7,7].\nanswer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.\n```\n\nExample 2:\n\n```\n**Input:** nums = [7,1,3,9]\n**Output:** [7,1,3,9]\n**Explanation:** The separation of each integer in nums is itself.\nanswer = [7,1,3,9].\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 1000`\n\n\t- `1 <= nums[i] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn separate_digits(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Simulation\nAcceptance rate: 80.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digits_spec(num: int) -> Seq\n decreases num,\n {\n if num < 10 {\n seq![num as i32]\n } else {\n Self::digits_spec(num / 10).push((num % 10) as i32)\n }\n }\n\n pub open spec fn separate_prefix_spec(nums: Seq, end: int) -> Seq\n decreases end,\n {\n if end <= 0 {\n Seq::::empty()\n } else {\n Self::separate_prefix_spec(nums, end - 1) + Self::digits_spec(nums[end - 1] as int)\n }\n }\n\n pub open spec fn separate_spec(nums: Seq) -> Seq {\n Self::separate_prefix_spec(nums, nums.len() as int)\n }\n\n pub fn separate_digits(nums: Vec) -> (result: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000,\n ensures\n result@ == Self::separate_spec(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn digits_exec(num: i32) -> Vec {\n if num < 10 {\n vec![num]\n } else {\n let mut rest = Self::digits_exec(num / 10);\n rest.push(num % 10);\n rest\n }\n }\n\n pub fn separate_digits(nums: Vec) -> Vec {\n let n = nums.len();\n let mut ans: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let d = Self::digits_exec(nums[i]);\n let mut j: usize = 0;\n while j < d.len() {\n ans.push(d[j]);\n j += 1;\n }\n i += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digits_spec(num: int) -> Seq\n decreases num,\n {\n if num < 10 {\n seq![num as i32]\n } else {\n Self::digits_spec(num / 10).push((num % 10) as i32)\n }\n }\n\n pub open spec fn separate_prefix_spec(nums: Seq, end: int) -> Seq\n decreases end,\n {\n if end <= 0 {\n Seq::::empty()\n } else {\n Self::separate_prefix_spec(nums, end - 1) + Self::digits_spec(nums[end - 1] as int)\n }\n }\n\n pub open spec fn separate_spec(nums: Seq) -> Seq {\n Self::separate_prefix_spec(nums, nums.len() as int)\n }\n\n fn digits_exec(num: i32) -> (result: Vec)\n requires\n 1 <= num <= 100000,\n ensures\n result@ == Self::digits_spec(num as int),\n {\n if num < 10 {\n vec![num]\n } else {\n let mut rest = Self::digits_exec(num / 10);\n rest.push(num % 10);\n rest\n }\n }\n\n pub fn separate_digits(nums: Vec) -> (result: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000,\n ensures\n result@ == Self::separate_spec(nums@),\n {\n let n = nums.len();\n let mut ans: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n let d = Self::digits_exec(nums[i]);\n let mut j: usize = 0;\n while j < d.len() {\n ans.push(d[j]);\n j += 1;\n }\n i += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digits_spec(num: int) -> Seq\n decreases num,\n {\n if num < 10 {\n seq![num as i32]\n } else {\n Self::digits_spec(num / 10).push((num % 10) as i32)\n }\n }\n\n pub open spec fn separate_prefix_spec(nums: Seq, end: int) -> Seq\n decreases end,\n {\n if end <= 0 {\n Seq::::empty()\n } else {\n Self::separate_prefix_spec(nums, end - 1) + Self::digits_spec(nums[end - 1] as int)\n }\n }\n\n pub open spec fn separate_spec(nums: Seq) -> Seq {\n Self::separate_prefix_spec(nums, nums.len() as int)\n }\n\n fn digits_exec(num: i32) -> (result: Vec)\n requires\n 1 <= num <= 100000,\n ensures\n result@ == Self::digits_spec(num as int),\n decreases num,\n {\n if num < 10 {\n vec![num]\n } else {\n let mut rest = Self::digits_exec(num / 10);\n rest.push(num % 10);\n rest\n }\n }\n\n pub fn separate_digits(nums: Vec) -> (result: Vec)\n requires\n 1 <= nums.len() <= 1000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000,\n ensures\n result@ == Self::separate_spec(nums@),\n {\n let n = nums.len();\n let mut ans: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n\n invariant\n n == nums.len(),\n 0 <= i <= n,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100000,\n ans@ == Self::separate_prefix_spec(nums@, i as int),\n decreases n - i,\n {\n let d = Self::digits_exec(nums[i]);\n let mut j: usize = 0;\n while j < d.len()\n invariant\n 0 <= j <= d.len(),\n d@ == Self::digits_spec(nums[i as int] as int),\n ans@\n == Self::separate_prefix_spec(nums@, i as int)\n + d@.subrange(0, j as int),\n decreases d.len() - j,\n {\n ans.push(d[j]);\n j += 1;\n }\n assert(ans@\n == Self::separate_prefix_spec(nums@, i as int)\n + Self::digits_spec(nums[i as int] as int));\n i += 1;\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2553/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2553/tests/mutated_testcases.jsonl"} {"problem": "lc2579", "family": "leetcode", "description": "# Count Total Number of Colored Cells\n\nThere exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer `n`, indicating that you must do the following routine for `n` minutes:\n\n\t- At the first minute, color **any** arbitrary unit cell blue.\n\n\t- Every minute thereafter, color blue **every** uncolored cell that touches a blue cell.\n\nBelow is a pictorial representation of the state of the grid after minutes 1, 2, and 3.\n\nReturn *the number of **colored cells** at the end of *`n` *minutes*.\n\n \n\nExample 1:\n\n```\n**Input:** n = 1\n**Output:** 1\n**Explanation:** After 1 minute, there is only 1 blue cell, so we return 1.\n```\n\nExample 2:\n\n```\n**Input:** n = 2\n**Output:** 5\n**Explanation:** After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn colored_cells(n: i32) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nMath\nAcceptance rate: 66.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn colored_until(i: nat) -> int\n recommends\n i >= 1,\n{\n 1 + 2 * i as int * (i as int - 1)\n}\n\nimpl Solution {\n pub fn colored_cells(n: i32) -> (result: i64)\n requires\n 1 <= n <= 100000,\n ensures\n result as int == colored_until(n as nat),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn colored_cells(n: i32) -> i64 {\n let mut ans: i128 = 1;\n let mut i: i128 = 1;\n\n while i < n as i128 {\n ans = ans + 4 * i;\n i = i + 1;\n }\n\n ans as i64\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn colored_until(i: nat) -> int\n recommends\n i >= 1,\n{\n 1 + 2 * i as int * (i as int - 1)\n}\n\nimpl Solution {\n pub fn colored_cells(n: i32) -> (result: i64)\n requires\n 1 <= n <= 100000,\n ensures\n result as int == colored_until(n as nat),\n {\n let mut ans: i128 = 1;\n let mut i: i128 = 1;\n\n while i < n as i128 {\n ans = ans + 4 * i;\n i = i + 1;\n }\n\n ans as i64\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn colored_until(i: nat) -> int\n recommends\n i >= 1,\n{\n 1 + 2 * i as int * (i as int - 1)\n}\n\nimpl Solution {\n pub fn colored_cells(n: i32) -> (result: i64)\n requires\n 1 <= n <= 100000,\n ensures\n result as int == colored_until(n as nat),\n {\n let mut ans: i128 = 1;\n let mut i: i128 = 1;\n\n while i < n as i128\n invariant\n 1 <= n as int <= 100000,\n 1 <= i as int <= n as int,\n ans as int == colored_until(i as nat),\n decreases n as i128 - i,\n {\n proof {\n assert(1 <= ans as int <= 20000000001) by (nonlinear_arith)\n requires\n ans as int == colored_until(i as nat),\n 1 <= i as int <= 100000,\n ;\n assert(4 <= 4 * i as int <= 400000);\n assert(-170141183460469231731687303715884105728\n <= ans + 4 * i\n < 170141183460469231731687303715884105728);\n }\n ans = ans + 4 * i;\n proof {\n assert(colored_until((i + 1) as nat) == colored_until(i as nat) + 4 * i as int) by (nonlinear_arith)\n requires\n 1 <= i,\n ;\n }\n i = i + 1;\n }\n\n proof {\n assert(i as int == n as int);\n assert(ans as int == colored_until(n as nat));\n assert(1 <= n as int <= 100000);\n assert(1 <= ans as int <= 20000000001) by (nonlinear_arith)\n requires\n ans as int == colored_until(n as nat),\n 1 <= n as int <= 100000,\n ;\n assert(-9223372036854775808 <= ans as int <= 9223372036854775807);\n assert((ans as i64) as int == ans as int);\n }\n\n ans as i64\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2579/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2579/tests/mutated_testcases.jsonl"} {"problem": "lc2582", "family": "leetcode", "description": "# 2582. Pass the Pillow\n\nThere are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.\n\n- For example, once the pillow reaches the `n`th person they pass it to the `n - 1`th person, then to the `n - 2`th person and so on.\n\nGiven the two positive integers `n` and `time`, return the index of the person holding the pillow after `time` seconds.\n\n## Example 1:\n```\nInput: n = 4, time = 5\nOutput: 2\nExplanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.\nAfter five seconds, the 2nd person is holding the pillow.\n```\n\n## Example 2:\n```\nInput: n = 3, time = 2\nOutput: 3\nExplanation: People pass the pillow in the following way: 1 -> 2 -> 3.\nAfter two seconds, the 3rd person is holding the pillow.\n```\n\n## Constraints:\n\n- `2 <= n <= 1000`\n- `1 <= time <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn pass_the_pillow(n: i32, time: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nSimulation\nAcceptance rate: 56.6%", "spec": "use vstd::arithmetic::div_mod::{\n lemma_mod_bound, lemma_mod_decreases, lemma_mod_equivalence, lemma_mod_is_zero,\n lemma_mod_multiples_basic, lemma_mod_multiples_vanish, lemma_mod_twice,\n};\nuse vstd::arithmetic::logarithm::{lemma_log0, lemma_log_is_ordered, lemma_log_pow, log};\nuse vstd::arithmetic::mul::{\n lemma_mul_by_zero_is_zero, lemma_mul_increases, lemma_mul_is_distributive_add_other_way,\n};\nuse vstd::arithmetic::power::pow;\nuse vstd::arithmetic::power2::{lemma2_to64, lemma_pow2, pow2};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn pass_the_pillow_spec_inner(n: nat, time: nat, pos: nat, dir: bool) -> nat\n decreases time,\n {\n if time == 0 {\n pos\n } else {\n let dir = if pos == 1 {\n true\n } else if pos == n {\n false\n } else {\n dir\n };\n Self::pass_the_pillow_spec_inner(\n n,\n (time - 1) as nat,\n (pos + if dir {\n 1\n } else {\n -1\n }) as nat,\n dir,\n )\n }\n }\n\n pub open spec fn pass_the_pillow_spec(n: nat, time: nat) -> nat {\n Self::pass_the_pillow_spec_inner(n, time, 1, true)\n }\n\n pub fn pass_the_pillow(n: i32, time: i32) -> (res: i32)\n requires\n 2 <= n <= 1000,\n 1 <= time <= 1000,\n ensures\n res == Self::pass_the_pillow_spec(n as nat, time as nat) as i32,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn pass_the_pillow(n: i32, time: i32) -> i32 {\n let m = 2 * n - 2;\n let t = time % m;\n if t < n {\n t + 1\n } else {\n 2 * n - t - 1\n }\n }\n}\n", "code_spec": "use vstd::arithmetic::div_mod::{\n lemma_mod_bound, lemma_mod_decreases, lemma_mod_equivalence, lemma_mod_is_zero,\n lemma_mod_multiples_basic, lemma_mod_multiples_vanish, lemma_mod_twice,\n};\nuse vstd::arithmetic::logarithm::{lemma_log0, lemma_log_is_ordered, lemma_log_pow, log};\nuse vstd::arithmetic::mul::{\n lemma_mul_by_zero_is_zero, lemma_mul_increases, lemma_mul_is_distributive_add_other_way,\n};\nuse vstd::arithmetic::power::pow;\nuse vstd::arithmetic::power2::{lemma2_to64, lemma_pow2, pow2};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn pass_the_pillow_spec_inner(n: nat, time: nat, pos: nat, dir: bool) -> nat\n decreases time,\n {\n if time == 0 {\n pos\n } else {\n let dir = if pos == 1 {\n true\n } else if pos == n {\n false\n } else {\n dir\n };\n Self::pass_the_pillow_spec_inner(\n n,\n (time - 1) as nat,\n (pos + if dir {\n 1\n } else {\n -1\n }) as nat,\n dir,\n )\n }\n }\n\n pub open spec fn pass_the_pillow_spec(n: nat, time: nat) -> nat {\n Self::pass_the_pillow_spec_inner(n, time, 1, true)\n }\n\n pub fn pass_the_pillow(n: i32, time: i32) -> (res: i32)\n requires\n 2 <= n <= 1000,\n 1 <= time <= 1000,\n ensures\n res == Self::pass_the_pillow_spec(n as nat, time as nat) as i32,\n {\n let m = 2 * n - 2;\n let t = time % m;\n if t < n {\n t + 1\n } else {\n 2 * n - t - 1\n }\n }\n}\n\n} \n", "verified": "use vstd::arithmetic::div_mod::{\n lemma_mod_bound, lemma_mod_decreases, lemma_mod_equivalence, lemma_mod_is_zero,\n lemma_mod_multiples_basic, lemma_mod_multiples_vanish, lemma_mod_twice,\n};\nuse vstd::arithmetic::logarithm::{lemma_log0, lemma_log_is_ordered, lemma_log_pow, log};\nuse vstd::arithmetic::mul::{\n lemma_mul_by_zero_is_zero, lemma_mul_increases, lemma_mul_is_distributive_add_other_way,\n};\nuse vstd::arithmetic::power::pow;\nuse vstd::arithmetic::power2::{lemma2_to64, lemma_pow2, pow2};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub proof fn lemma_mod_congruent(x: int, y: int, m: int)\n requires\n x % m == y % m,\n 0 <= x <= y,\n m > 0,\n ensures\n exists|n: nat| y == #[trigger] (n * m) + x,\n decreases y,\n{\n lemma_mod_equivalence(y, x, m);\n let diff = y - x;\n if diff == 0 {\n lemma_mul_by_zero_is_zero(m);\n } else {\n lemma_mod_is_zero(diff as nat, m as nat);\n if diff == m {\n assert(y == 1 * m + x);\n } else {\n lemma_mod_multiples_vanish(-1, y, m);\n lemma_mod_congruent(x, y - m, m);\n let last_n = choose|n: nat| (y - m) == #[trigger] (n * m) + x;\n lemma_mul_is_distributive_add_other_way(m, last_n as int, 1);\n }\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn pass_the_pillow_spec_inner(n: nat, time: nat, pos: nat, dir: bool) -> nat\n decreases time,\n {\n if time == 0 {\n pos\n } else {\n let dir = if pos == 1 {\n true\n } else if pos == n {\n false\n } else {\n dir\n };\n Self::pass_the_pillow_spec_inner(\n n,\n (time - 1) as nat,\n (pos + if dir {\n 1\n } else {\n -1\n }) as nat,\n dir,\n )\n }\n }\n\n pub open spec fn pass_the_pillow_spec(n: nat, time: nat) -> nat {\n Self::pass_the_pillow_spec_inner(n, time, 1, true)\n }\n\n pub proof fn examples() {\n assert(Self::pass_the_pillow_spec(4, 5) == 2) by (compute);\n assert(Self::pass_the_pillow_spec(3, 2) == 3) by (compute);\n }\n\n pub proof fn lemma_pillow_travels_straight(n: nat, time: nat, pos: nat, dir: bool, steps: nat)\n requires\n 2 <= n,\n 1 <= pos <= n,\n 1 <= pos + if dir {\n steps as int\n } else {\n -steps\n } <= n,\n steps <= time,\n ensures\n Self::pass_the_pillow_spec_inner(n, time, pos, dir) == Self::pass_the_pillow_spec_inner(\n n,\n (time - steps) as nat,\n (pos + if dir {\n steps as int\n } else {\n -steps\n }) as nat,\n dir,\n ),\n decreases steps,\n {\n if steps != 0 {\n Self::lemma_pillow_travels_straight(\n n,\n (time - 1) as nat,\n (pos + if dir {\n 1\n } else {\n -1\n }) as nat,\n dir,\n (steps - 1) as nat,\n );\n }\n }\n\n pub open spec fn mod_congruent(x: int, y: int, m: int) -> bool {\n x % m == y % m\n }\n\n pub proof fn lemma_pillow_periodic_base(n: nat, t1: int, t2: int)\n requires\n 2 <= n,\n t2 == t1 + 2 * n - 2,\n 0 <= t1 < t2,\n ensures\n Self::pass_the_pillow_spec(n, t1 as nat) == Self::pass_the_pillow_spec(n, t2 as nat),\n {\n let halfway = (n - 1) as nat;\n Self::lemma_pillow_travels_straight(n, t2 as nat, 1, true, halfway);\n Self::lemma_pillow_travels_straight(n, (t2 - halfway) as nat, n, false, halfway);\n }\n\n pub proof fn lemma_pillow_periodic(n: nat, t1: int, t2: int)\n requires\n 2 <= n,\n Self::mod_congruent(t1 as int, t2 as int, 2 * n - 2),\n 0 <= t1 <= t2,\n ensures\n Self::pass_the_pillow_spec(n, t1 as nat) == Self::pass_the_pillow_spec(n, t2 as nat),\n decreases t2,\n {\n let m = 2 * n - 2;\n lemma_mod_congruent(t1, t2, m);\n let a = choose|a: int| a >= 0 && t2 == #[trigger] (a * m) + t1;\n if a == 0 {\n \n } else if a == 1 {\n Self::lemma_pillow_periodic_base(n, t1, t2);\n } else {\n lemma_mod_multiples_vanish(-1, t2, m);\n lemma_mul_is_distributive_add_other_way(m, a, -1);\n lemma_mul_increases(a - 1, m);\n Self::lemma_pillow_periodic(n, t1, t2 - m);\n Self::lemma_pillow_periodic_base(n, t2 - m, t2);\n }\n }\n\n pub fn pass_the_pillow(n: i32, time: i32) -> (res: i32)\n requires\n 2 <= n <= 1000,\n 1 <= time <= 1000,\n ensures\n res == Self::pass_the_pillow_spec(n as nat, time as nat) as i32,\n {\n let m = 2 * n - 2;\n let t = time % m;\n proof {\n lemma_mod_twice(time as int, m as int);\n lemma_mod_bound(time as int, m as int);\n lemma_mod_decreases(time as nat, m as nat);\n Self::lemma_pillow_periodic(n as nat, t as int, time as int);\n }\n if t < n {\n proof {\n Self::lemma_pillow_travels_straight(n as nat, t as nat, 1, true, t as nat);\n }\n t + 1\n } else {\n proof {\n Self::lemma_pillow_travels_straight(n as nat, t as nat, 1, true, (n - 1) as nat);\n Self::lemma_pillow_travels_straight(\n n as nat,\n (t - (n - 1)) as nat,\n n as nat,\n false,\n (t - (n - 1)) as nat,\n );\n }\n 2 * n - t - 1\n }\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc2582/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2582/tests/mutated_testcases.jsonl"} {"problem": "lc2595", "family": "leetcode", "description": "# Number of Even and Odd Bits\n\nYou are given a **positive** integer `n`.\n\nLet `even` denote the number of even indices in the binary representation of `n` with value 1.\n\nLet `odd` denote the number of odd indices in the binary representation of `n` with value 1.\n\nNote that bits are indexed from **right to left** in the binary representation of a number.\n\nReturn the array `[even, odd]`.\n\n## Example 1:\n\n> **Input:** n = 50\n> **Output:** [1,2]\n> **Explanation:**\n\nThe binary representation of 50 is `110010`.\n\nIt contains 1 on indices 1, 4, and 5.\n\n## Example 2:\n\n> **Input:** n = 2\n> **Output:** [0,1]\n> **Explanation:**\n\nThe binary representation of 2 is `10`.\n\nIt contains 1 only on index 1.\n\n## Constraints:\n\n- `1 <= n <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn even_odd_bit(n: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nBit Manipulation\nAcceptance rate: 73.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn even_bits_spec(n: int) -> int\n recommends\n 1 <= n <= 1000,\n {\n (n / 1) % 2 + (n / 4) % 2 + (n / 16) % 2 + (n / 64) % 2 + (n / 256) % 2 + (n / 1024) % 2\n }\n\n pub open spec fn odd_bits_spec(n: int) -> int\n recommends\n 1 <= n <= 1000,\n {\n (n / 2) % 2 + (n / 8) % 2 + (n / 32) % 2 + (n / 128) % 2 + (n / 512) % 2\n }\n\n pub fn even_odd_bit(n: i32) -> (result: Vec)\n requires\n 1 <= n <= 1000,\n ensures\n result@.len() == 2,\n result@[0] as int == Self::even_bits_spec(n as int),\n result@[1] as int == Self::odd_bits_spec(n as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn bit_value(n: i32, div: i32) -> i32 {\n (n / div) % 2\n }\n\n pub fn even_odd_bit(n: i32) -> Vec {\n let even = Self::bit_value(n, 1) + Self::bit_value(n, 4) + Self::bit_value(n, 16)\n + Self::bit_value(n, 64) + Self::bit_value(n, 256) + Self::bit_value(n, 1024);\n let odd = Self::bit_value(n, 2) + Self::bit_value(n, 8) + Self::bit_value(n, 32)\n + Self::bit_value(n, 128) + Self::bit_value(n, 512);\n\n let mut result = Vec::new();\n result.push(even);\n result.push(odd);\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn even_bits_spec(n: int) -> int\n recommends\n 1 <= n <= 1000,\n {\n (n / 1) % 2 + (n / 4) % 2 + (n / 16) % 2 + (n / 64) % 2 + (n / 256) % 2 + (n / 1024) % 2\n }\n\n pub open spec fn odd_bits_spec(n: int) -> int\n recommends\n 1 <= n <= 1000,\n {\n (n / 2) % 2 + (n / 8) % 2 + (n / 32) % 2 + (n / 128) % 2 + (n / 512) % 2\n }\n\n fn bit_value(n: i32, div: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n div > 0,\n ensures\n result as int == ((n as int / div as int) % 2),\n 0 <= result <= 1,\n {\n (n / div) % 2\n }\n\n pub fn even_odd_bit(n: i32) -> (result: Vec)\n requires\n 1 <= n <= 1000,\n ensures\n result@.len() == 2,\n result@[0] as int == Self::even_bits_spec(n as int),\n result@[1] as int == Self::odd_bits_spec(n as int),\n {\n let even = Self::bit_value(n, 1) + Self::bit_value(n, 4) + Self::bit_value(n, 16)\n + Self::bit_value(n, 64) + Self::bit_value(n, 256) + Self::bit_value(n, 1024);\n let odd = Self::bit_value(n, 2) + Self::bit_value(n, 8) + Self::bit_value(n, 32)\n + Self::bit_value(n, 128) + Self::bit_value(n, 512);\n\n let mut result = Vec::new();\n result.push(even);\n result.push(odd);\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn even_bits_spec(n: int) -> int\n recommends\n 1 <= n <= 1000,\n {\n (n / 1) % 2 + (n / 4) % 2 + (n / 16) % 2 + (n / 64) % 2 + (n / 256) % 2 + (n / 1024) % 2\n }\n\n pub open spec fn odd_bits_spec(n: int) -> int\n recommends\n 1 <= n <= 1000,\n {\n (n / 2) % 2 + (n / 8) % 2 + (n / 32) % 2 + (n / 128) % 2 + (n / 512) % 2\n }\n\n fn bit_value(n: i32, div: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n div > 0,\n ensures\n result as int == ((n as int / div as int) % 2),\n 0 <= result <= 1,\n {\n (n / div) % 2\n }\n\n pub fn even_odd_bit(n: i32) -> (result: Vec)\n requires\n 1 <= n <= 1000,\n ensures\n result@.len() == 2,\n result@[0] as int == Self::even_bits_spec(n as int),\n result@[1] as int == Self::odd_bits_spec(n as int),\n {\n let even = Self::bit_value(n, 1) + Self::bit_value(n, 4) + Self::bit_value(n, 16)\n + Self::bit_value(n, 64) + Self::bit_value(n, 256) + Self::bit_value(n, 1024);\n let odd = Self::bit_value(n, 2) + Self::bit_value(n, 8) + Self::bit_value(n, 32)\n + Self::bit_value(n, 128) + Self::bit_value(n, 512);\n\n let mut result = Vec::new();\n result.push(even);\n result.push(odd);\n\n proof {\n assert(even as int == Self::even_bits_spec(n as int));\n assert(odd as int == Self::odd_bits_spec(n as int));\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2595/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2595/tests/mutated_testcases.jsonl"} {"problem": "lc2600", "family": "leetcode", "description": "# K Items With the Maximum Sum\n\nThere is a bag that consists of items, each item has a number `1`, `0`, or `-1` written on it.\n\nYou are given four non-negative integers `numOnes`, `numZeros`, `numNegOnes`, and `k`.\n\nThe bag initially contains:\n\n- `numOnes` items with `1`s written on them.\n- `numZeroes` items with `0`s written on them.\n- `numNegOnes` items with `-1`s written on them.\n\nWe want to pick exactly `k` items among the available items. Return the maximum possible sum of numbers written on the items.\n\n \n\n## Example 1:\n\n```\n**Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2\n**Output:** 2\n**Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.\nIt can be proven that 2 is the maximum possible sum.\n```\n\n## Example 2:\n\n```\n**Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4\n**Output:** 3\n**Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.\nIt can be proven that 3 is the maximum possible sum.\n```\n\n \n\n## Constraints:\n\n- `0 <= numOnes, numZeros, numNegOnes <= 50`\n- `0 <= k <= numOnes + numZeros + numNegOnes`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Greedy\nAcceptance rate: 60.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_sum_spec(num_ones: int, num_zeros: int, k: int) -> int\n recommends\n 0 <= num_ones <= 50,\n 0 <= num_zeros <= 50,\n 0 <= k,\n {\n if k <= num_ones {\n k\n } else if k <= num_ones + num_zeros {\n num_ones\n } else {\n num_ones - (k - num_ones - num_zeros)\n }\n }\n\n pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> (result: i32)\n requires\n 0 <= num_ones <= 50,\n 0 <= num_zeros <= 50,\n 0 <= num_neg_ones <= 50,\n 0 <= k <= num_ones + num_zeros + num_neg_ones,\n ensures\n result as int == Self::max_sum_spec(num_ones as int, num_zeros as int, k as int),\n -50 <= result as int <= 50,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {\n if k <= num_ones {\n k\n } else if k <= num_ones + num_zeros {\n num_ones\n } else {\n num_ones - (k - num_ones - num_zeros)\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_sum_spec(num_ones: int, num_zeros: int, k: int) -> int\n recommends\n 0 <= num_ones <= 50,\n 0 <= num_zeros <= 50,\n 0 <= k,\n {\n if k <= num_ones {\n k\n } else if k <= num_ones + num_zeros {\n num_ones\n } else {\n num_ones - (k - num_ones - num_zeros)\n }\n }\n\n pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> (result: i32)\n requires\n 0 <= num_ones <= 50,\n 0 <= num_zeros <= 50,\n 0 <= num_neg_ones <= 50,\n 0 <= k <= num_ones + num_zeros + num_neg_ones,\n ensures\n result as int == Self::max_sum_spec(num_ones as int, num_zeros as int, k as int),\n -50 <= result as int <= 50,\n {\n if k <= num_ones {\n k\n } else if k <= num_ones + num_zeros {\n num_ones\n } else {\n num_ones - (k - num_ones - num_zeros)\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_sum_spec(num_ones: int, num_zeros: int, k: int) -> int\n recommends\n 0 <= num_ones <= 50,\n 0 <= num_zeros <= 50,\n 0 <= k,\n {\n if k <= num_ones {\n k\n } else if k <= num_ones + num_zeros {\n num_ones\n } else {\n num_ones - (k - num_ones - num_zeros)\n }\n }\n\n pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> (result: i32)\n requires\n 0 <= num_ones <= 50,\n 0 <= num_zeros <= 50,\n 0 <= num_neg_ones <= 50,\n 0 <= k <= num_ones + num_zeros + num_neg_ones,\n ensures\n result as int == Self::max_sum_spec(num_ones as int, num_zeros as int, k as int),\n -50 <= result as int <= 50,\n {\n if k <= num_ones {\n proof {\n assert(Self::max_sum_spec(num_ones as int, num_zeros as int, k as int) == k as int);\n assert(-50 <= k as int <= 50);\n }\n k\n } else if k <= num_ones + num_zeros {\n proof {\n assert(Self::max_sum_spec(num_ones as int, num_zeros as int, k as int) == num_ones as int);\n assert(-50 <= num_ones as int <= 50);\n }\n num_ones\n } else {\n proof {\n assert(Self::max_sum_spec(num_ones as int, num_zeros as int, k as int)\n == num_ones as int - (k as int - num_ones as int - num_zeros as int));\n assert(-50 <= (num_ones - (k - num_ones - num_zeros)) as int <= 50);\n }\n num_ones - (k - num_ones - num_zeros)\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2600/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2600/tests/mutated_testcases.jsonl"} {"problem": "lc2651", "family": "leetcode", "description": "# Calculate Delayed Arrival Time\n\nYou are given a positive integer `arrivalTime` denoting the arrival time of a train in hours, and another positive integer `delayedTime` denoting the amount of delay in hours.\n\nReturn *the time when the train will arrive at the station.*\n\nNote that the time in this problem is in 24-hours format.\n\n## Example 1:\n\n> **Input:** `arrivalTime = 15`, `delayedTime = 5 `\n> **Output:** `20 `\n> **Explanation:** Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours).\n\n## Example 2:\n\n> **Input:** `arrivalTime = 13`, `delayedTime = 11`\n> **Output:** `0`\n> **Explanation:** Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0).\n\n## Constraints:\n\n- `1 <= arrivalTime < 24`\n- `1 <= delayedTime <= 24`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 75.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn arrival_spec(arrival: int, delayed: int) -> int {\n (arrival + delayed) % 24\n }\n\n pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> (result: i32)\n requires\n 1 <= arrival_time < 24,\n 1 <= delayed_time <= 24,\n ensures\n result == Self::arrival_spec(arrival_time as int, delayed_time as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> i32 {\n (arrival_time + delayed_time) % 24\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn arrival_spec(arrival: int, delayed: int) -> int {\n (arrival + delayed) % 24\n }\n\n pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> (result: i32)\n requires\n 1 <= arrival_time < 24,\n 1 <= delayed_time <= 24,\n ensures\n result == Self::arrival_spec(arrival_time as int, delayed_time as int),\n {\n (arrival_time + delayed_time) % 24\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn arrival_spec(arrival: int, delayed: int) -> int {\n (arrival + delayed) % 24\n }\n\n pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> (result: i32)\n requires\n 1 <= arrival_time < 24,\n 1 <= delayed_time <= 24,\n ensures\n result == Self::arrival_spec(arrival_time as int, delayed_time as int),\n {\n (arrival_time + delayed_time) % 24\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2651/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2651/tests/mutated_testcases.jsonl"} {"problem": "lc274", "family": "leetcode", "description": "# H-Index\n\nGiven an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `$i^th$` paper, return *the researcher's h-index*.\n\nAccording to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of `h` such that the given researcher has published at least `h` papers that have each been cited at least `h` times.\n\n## Example 1:\n\n> **Input:** citations = [3,0,6,1,5]\n> **Output:** 3\n> **Explanation:** [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.\n> Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.\n\n## Example 2:\n\n> **Input:** citations = [1,3,1]\n> **Output:** 1\n\n## Constraints:\n\n- $n == citations.length$\n\n- $1 <= n <= 5000$\n\n- $0 <= citations[i] <= 1000$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn h_index(citations: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Sorting, Counting Sort\nAcceptance rate: 40.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_at_least(citations: Seq, threshold: i32) -> int \n decreases citations.len()\n {\n if citations.len() == 0 {\n 0\n } else {\n (if citations[0] >= threshold { 1 as int } else { 0 }) + \n Self::count_at_least(citations.subrange(1, citations.len() as int), threshold)\n }\n }\n\n pub fn h_index(citations: Vec) -> (res: i32) \n requires\n 1 <= citations.len() <= 5_000, \n forall |i: int| 0 <= i < citations.len() ==> 0 <= #[trigger] citations[i] <= 1_000,\n ensures \n 0 <= res <= citations.len(),\n Self::count_at_least(citations@, res) >= res,\n forall |h: int| res < h <= citations.len() + 1 ==> \n #[trigger] Self::count_at_least(citations@, h as i32) < h,\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn h_index(citations: Vec) -> i32\n {\n let n = citations.len();\n\n let mut h: usize = n;\n while h > 0 \n {\n let mut count: usize = 0;\n let mut j: usize = 0;\n \n while j < n \n {\n if citations[j] >= h as i32 {\n count = count + 1;\n } \n \n j = j + 1;\n }\n \n if count >= h \n {\n return h as i32;\n }\n \n h = h - 1;\n }\n\n 0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_at_least(citations: Seq, threshold: i32) -> int \n decreases citations.len()\n {\n if citations.len() == 0 {\n 0\n } else {\n (if citations[0] >= threshold { 1 as int } else { 0 }) + \n Self::count_at_least(citations.subrange(1, citations.len() as int), threshold)\n }\n }\n\n pub fn h_index(citations: Vec) -> (res: i32) \n requires\n 1 <= citations.len() <= 5_000, \n forall |i: int| 0 <= i < citations.len() ==> 0 <= #[trigger] citations[i] <= 1_000,\n ensures \n 0 <= res <= citations.len(),\n Self::count_at_least(citations@, res) >= res,\n forall |h: int| res < h <= citations.len() + 1 ==> \n #[trigger] Self::count_at_least(citations@, h as i32) < h,\n {\n let n = citations.len();\n\n let mut h: usize = n;\n while h > 0 \n {\n let mut count: usize = 0;\n let mut j: usize = 0;\n \n while j < n \n {\n if citations[j] >= h as i32 {\n count = count + 1;\n } \n \n j = j + 1;\n }\n\n if count >= h \n {\n return h as i32;\n }\n \n h = h - 1;\n }\n\n 0\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_at_least(citations: Seq, threshold: i32) -> int \n decreases citations.len()\n {\n if citations.len() == 0 {\n 0\n } else {\n (if citations[0] >= threshold { 1 as int } else { 0 }) + \n Self::count_at_least(citations.subrange(1, citations.len() as int), threshold)\n }\n }\n\n proof fn lemma_subrange_full(citations: Seq)\n ensures citations.subrange(0, citations.len() as int) == citations\n {\n assert(citations =~= citations.subrange(0, citations.len() as int));\n }\n\n proof fn lemma_count_bounded(citations: Seq, threshold: i32)\n ensures Self::count_at_least(citations, threshold) <= citations.len()\n decreases citations.len()\n {\n if citations.len() == 0 {\n } else {\n Self::lemma_count_bounded(citations.subrange(1, citations.len() as int), threshold);\n }\n }\n\n proof fn lemma_count_nonnegative(citations: Seq, threshold: i32)\n ensures \n Self::count_at_least(citations, threshold) >= 0\n decreases citations.len(), \n {\n if citations.len() == 0 {\n } \n else {\n Self::lemma_count_nonnegative(citations.subrange(1, citations.len() as int), threshold);\n }\n }\n\n proof fn lemma_count_extend(citations: Seq, j: int, threshold: i32)\n requires \n 0 <= j < citations.len()\n ensures\n Self::count_at_least(citations.subrange(0, j + 1), threshold) ==\n Self::count_at_least(citations.subrange(0, j), threshold) +\n (if citations[j] >= threshold { 1 as int } else { 0 })\n decreases j\n {\n if j == 0 {\n assert(citations.subrange(0, 0) =~= Seq::::empty());\n assert(citations.subrange(0, 1).subrange(1, 1) =~= Seq::::empty());\n } else {\n let tail = citations.subrange(1, citations.len() as int);\n \n Self::lemma_count_extend(tail, j - 1, threshold);\n \n assert(tail.subrange(0, j) =~= citations.subrange(1, j + 1));\n assert(tail.subrange(0, j - 1) =~= citations.subrange(1, j));\n assert(citations.subrange(0, j + 1).subrange(1, j + 1) =~= citations.subrange(1, j + 1));\n assert(citations.subrange(0, j).subrange(1, j) =~= citations.subrange(1, j));\n }\n }\n\n pub fn h_index(citations: Vec) -> (res: i32) \n requires\n 1 <= citations.len() <= 5_000, \n forall |i: int| 0 <= i < citations.len() ==> 0 <= #[trigger] citations[i] <= 1_000,\n ensures \n 0 <= res <= citations.len(),\n Self::count_at_least(citations@, res) >= res,\n forall |h: int| res < h <= citations.len() + 1 ==> \n #[trigger] Self::count_at_least(citations@, h as i32) < h,\n {\n let n = citations.len();\n\n proof {\n Self::lemma_count_bounded(citations@, (n + 1) as i32);\n }\n\n let mut h: usize = n;\n while h > 0 \n invariant\n n == citations.len(),\n 0 <= h <= n,\n 1 <= citations.len() <= 5_000, \n forall |i: int| 0 <= i < citations.len() ==> 0 <= #[trigger] citations[i] <= 1_000,\n forall |h_cand: int| h < h_cand <= n + 1 ==> \n #[trigger] Self::count_at_least(citations@, h_cand as i32) < h_cand,\n decreases h, \n {\n let mut count: usize = 0;\n let mut j: usize = 0;\n \n while j < n \n invariant\n n == citations.len(),\n 0 <= j <= n,\n 0 <= count <= j,\n 0 < h <= n,\n 1 <= citations.len() <= 5_000, \n forall |i: int| 0 <= i < citations.len() ==> 0 <= #[trigger] citations[i] <= 1_000,\n count == Self::count_at_least(citations@.subrange(0, j as int), h as i32),\n decreases n - j, \n {\n proof {\n Self::lemma_count_extend(citations@, j as int, h as i32);\n }\n \n if citations[j] >= h as i32 {\n count = count + 1;\n } \n \n j = j + 1;\n }\n \n proof {\n Self::lemma_subrange_full(citations@);\n }\n \n if count >= h \n {\n return h as i32;\n }\n \n h = h - 1;\n }\n\n proof {\n Self::lemma_count_nonnegative(citations@, 0);\n }\n \n 0\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc274/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc274/tests/mutated_testcases.jsonl"} {"problem": "lc2745", "family": "leetcode", "description": "# Construct the Longest New String\n\nYou are given three integers `x`, `y`, and `z`.\n\nYou have `x` strings equal to `\"AA\"`, `y` strings equal to `\"BB\"`, and `z` strings equal to `\"AB\"`. You want to choose some (possibly all or none) of these strings and concatenate them in some order to form a new string. This new string must not contain `\"AAA\"` or `\"BBB\"` as a substring.\n\nReturn *the maximum possible length of the new string*.\n\nA **substring** is a contiguous **non-empty** sequence of characters within a string.\n\n \n\nExample 1:\n\n```\n**Input:** x = 2, y = 5, z = 1\n**Output:** 12\n**Explanation: **We can concatenate the strings \"BB\", \"AA\", \"BB\", \"AA\", \"BB\", and \"AB\" in that order. Then, our new string is \"BBAABBAABBAB\". \nThat string has length 12, and we can show that it is impossible to construct a string of longer length.\n```\n\nExample 2:\n\n```\n**Input:** x = 3, y = 2, z = 2\n**Output:** 14\n**Explanation:** We can concatenate the strings \"AB\", \"AB\", \"AA\", \"BB\", \"AA\", \"BB\", and \"AA\" in that order. Then, our new string is \"ABABAABBAABBAA\". \nThat string has length 14, and we can show that it is impossible to construct a string of longer length.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= x, y, z <= 50`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_string(x: i32, y: i32, z: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Dynamic Programming, Greedy, Brainteaser\nAcceptance rate: 54.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn longest_string_spec(x: int, y: int, z: int) -> int {\n (Self::min2(x, y + 1) + Self::min2(x + 1, y) + z) * 2\n }\n\n pub fn longest_string(x: i32, y: i32, z: i32) -> (result: i32)\n requires\n 1 <= x <= 50,\n 1 <= y <= 50,\n 1 <= z <= 50,\n ensures\n result as int == Self::longest_string_spec(x as int, y as int, z as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn min2_exec(a: i32, b: i32) -> i32 {\n if a <= b { a } else { b }\n }\n\n pub fn longest_string(x: i32, y: i32, z: i32) -> i32 {\n (Self::min2_exec(x, y + 1) + Self::min2_exec(x + 1, y) + z) * 2\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn longest_string_spec(x: int, y: int, z: int) -> int {\n (Self::min2(x, y + 1) + Self::min2(x + 1, y) + z) * 2\n }\n\n fn min2_exec(a: i32, b: i32) -> (res: i32)\n ensures\n res as int == Self::min2(a as int, b as int),\n {\n if a <= b { a } else { b }\n }\n\n pub fn longest_string(x: i32, y: i32, z: i32) -> (result: i32)\n requires\n 1 <= x <= 50,\n 1 <= y <= 50,\n 1 <= z <= 50,\n ensures\n result as int == Self::longest_string_spec(x as int, y as int, z as int),\n {\n (Self::min2_exec(x, y + 1) + Self::min2_exec(x + 1, y) + z) * 2\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min2(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn longest_string_spec(x: int, y: int, z: int) -> int {\n (Self::min2(x, y + 1) + Self::min2(x + 1, y) + z) * 2\n }\n\n fn min2_exec(a: i32, b: i32) -> (res: i32)\n ensures\n res as int == Self::min2(a as int, b as int),\n {\n if a <= b { a } else { b }\n }\n\n pub fn longest_string(x: i32, y: i32, z: i32) -> (result: i32)\n requires\n 1 <= x <= 50,\n 1 <= y <= 50,\n 1 <= z <= 50,\n ensures\n result as int == Self::longest_string_spec(x as int, y as int, z as int),\n {\n proof {\n assert((x as int) + 1 <= 51);\n assert((y as int) + 1 <= 51);\n assert(0 <= Self::min2(x as int, (y as int) + 1) <= 50);\n assert(0 <= Self::min2((x as int) + 1, y as int) <= 50);\n assert(\n (Self::min2(x as int, (y as int) + 1)\n + Self::min2((x as int) + 1, y as int)\n + (z as int)) * 2 <= 304\n );\n }\n (Self::min2_exec(x, y + 1) + Self::min2_exec(x + 1, y) + z) * 2\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2745/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2745/tests/mutated_testcases.jsonl"} {"problem": "lc276", "family": "leetcode", "description": "# Paint Fence\n\nThere is a fence with `n` posts, and each post can be painted with one of the `k` colors.\n\nYou have to paint all the posts such that no more than two adjacent fence posts have the same color.\n\nReturn the number of ways you can paint the fence.\n\n## Example 1:\n\n> **Input:** n = 3, k = 2\n> **Output:** 6\n\n## Example 2:\n\n> **Input:** n = 1, k = 1\n> **Output:** 1\n\n## Constraints:\n\n- $0 <= n <= 50$\n- $1 <= k <= 10^5$\n- The answer is guaranteed to fit in a 32-bit signed integer.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_ways(n: i32, k: i32) -> i32 {\n\n }\n}\n```\n", "tags": "Medium\nDynamic Programming, Math\nAcceptance rate: N/A (premium)\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn paint_ways(n: int, k: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else if n == 1 {\n k\n } else if n == 2 {\n k * k\n } else {\n (k - 1) * (Self::paint_ways(n - 1, k) + Self::paint_ways(n - 2, k))\n }\n }\n\n pub fn num_ways(n: i32, k: i32) -> (res: i32)\n requires\n 0 <= n <= 50,\n 1 <= k <= 100000,\n Self::paint_ways(n as int, k as int) <= i32::MAX as int,\n ensures\n res as int == Self::paint_ways(n as int, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn num_ways(n: i32, k: i32) -> i32 {\n if n == 0 {\n return 0;\n }\n if n == 1 {\n return k;\n }\n\n let mut prev2: i32 = k;\n let mut prev1: i32 = k * k;\n let mut i: i32 = 3;\n while i <= n {\n let sum = prev1 + prev2;\n let next = (k - 1) * sum;\n prev2 = prev1;\n prev1 = next;\n i = i + 1;\n }\n prev1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn paint_ways(n: int, k: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else if n == 1 {\n k\n } else if n == 2 {\n k * k\n } else {\n (k - 1) * (Self::paint_ways(n - 1, k) + Self::paint_ways(n - 2, k))\n }\n }\n\n pub fn num_ways(n: i32, k: i32) -> (res: i32)\n requires\n 0 <= n <= 50,\n 1 <= k <= 100000,\n Self::paint_ways(n as int, k as int) <= i32::MAX as int,\n ensures\n res as int == Self::paint_ways(n as int, k as int),\n {\n if n == 0 {\n return 0;\n }\n if n == 1 {\n return k;\n }\n\n let mut prev2: i32 = k;\n let mut prev1: i32 = k * k;\n let mut i: i32 = 3;\n while i <= n {\n let sum = prev1 + prev2;\n let next = (k - 1) * sum;\n prev2 = prev1;\n prev1 = next;\n i = i + 1;\n }\n prev1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn paint_ways(n: int, k: int) -> int\n decreases n\n {\n if n <= 0 {\n 0\n } else if n == 1 {\n k\n } else if n == 2 {\n k * k\n } else {\n (k - 1) * (Self::paint_ways(n - 1, k) + Self::paint_ways(n - 2, k))\n }\n }\n\n proof fn paint_ways_nonnegative(n: int, k: int)\n requires\n 0 <= n,\n 1 <= k,\n ensures\n 0 <= Self::paint_ways(n, k),\n decreases n,\n {\n if n <= 2 {\n } else {\n Self::paint_ways_nonnegative(n - 1, k);\n Self::paint_ways_nonnegative(n - 2, k);\n }\n }\n\n proof fn paint_ways_step_monotonic(n: int, k: int)\n requires\n 0 <= n,\n 2 <= k,\n ensures\n Self::paint_ways(n, k) <= Self::paint_ways(n + 1, k),\n {\n if n == 0 {\n } else if n == 1 {\n assert(k <= k * k) by (nonlinear_arith)\n requires\n 2 <= k,\n {}\n } else {\n Self::paint_ways_nonnegative(n, k);\n Self::paint_ways_nonnegative(n - 1, k);\n assert((k - 1) * (Self::paint_ways(n, k) + Self::paint_ways(n - 1, k)) >= Self::paint_ways(n, k)) by (nonlinear_arith)\n requires\n 2 <= k,\n 0 <= Self::paint_ways(n, k),\n 0 <= Self::paint_ways(n - 1, k),\n {}\n }\n }\n\n proof fn paint_ways_monotonic(i: int, j: int, k: int)\n requires\n 0 <= i <= j,\n 2 <= k,\n ensures\n Self::paint_ways(i, k) <= Self::paint_ways(j, k),\n decreases j - i,\n {\n if i < j {\n Self::paint_ways_monotonic(i, j - 1, k);\n Self::paint_ways_step_monotonic(j - 1, k);\n }\n }\n\n proof fn paint_ways_k_one_le_one(n: int)\n requires\n 1 <= n,\n ensures\n Self::paint_ways(n, 1) <= 1,\n decreases n,\n {\n if n <= 2 {\n } else {\n assert(Self::paint_ways(n, 1) == 0);\n }\n }\n\n pub fn num_ways(n: i32, k: i32) -> (res: i32)\n requires\n 0 <= n <= 50,\n 1 <= k <= 100000,\n Self::paint_ways(n as int, k as int) <= i32::MAX as int,\n ensures\n res as int == Self::paint_ways(n as int, k as int),\n {\n if n == 0 {\n return 0;\n }\n if n == 1 {\n return k;\n }\n\n proof {\n if k == 1 {\n assert((k as int) * (k as int) <= i32::MAX as int) by (nonlinear_arith)\n requires\n k == 1,\n {}\n } else {\n assert(2 <= k);\n if n > 2 {\n Self::paint_ways_monotonic(2, n as int, k as int);\n }\n assert(Self::paint_ways(2, k as int) <= Self::paint_ways(n as int, k as int));\n assert(Self::paint_ways(2, k as int) == (k as int) * (k as int));\n assert((k as int) * (k as int) <= i32::MAX as int);\n }\n }\n let mut prev2: i32 = k;\n let mut prev1: i32 = k * k;\n let mut i: i32 = 3;\n while i <= n\n invariant\n 2 <= n <= 50,\n 1 <= k <= 100000,\n 3 <= i <= n + 1,\n prev2 as int == Self::paint_ways((i - 2) as int, k as int),\n prev1 as int == Self::paint_ways((i - 1) as int, k as int),\n 0 <= prev2 as int <= i32::MAX as int,\n 0 <= prev1 as int <= i32::MAX as int,\n Self::paint_ways(n as int, k as int) <= i32::MAX as int,\n decreases n - i + 1,\n {\n proof {\n if k == 1 {\n assert(i >= 3);\n Self::paint_ways_nonnegative((i - 1) as int, 1);\n Self::paint_ways_nonnegative((i - 2) as int, 1);\n Self::paint_ways_k_one_le_one((i - 1) as int);\n Self::paint_ways_k_one_le_one((i - 2) as int);\n assert(prev1 as int + prev2 as int <= 2) by (nonlinear_arith)\n requires\n 0 <= prev1 as int <= 1,\n 0 <= prev2 as int <= 1,\n {}\n assert(prev1 as int + prev2 as int <= i32::MAX as int);\n } else {\n assert(2 <= k);\n Self::paint_ways_nonnegative((i - 1) as int, k as int);\n Self::paint_ways_nonnegative((i - 2) as int, k as int);\n assert(Self::paint_ways(i as int, k as int) == (k as int - 1) * (Self::paint_ways((i - 1) as int, k as int) + Self::paint_ways((i - 2) as int, k as int)));\n assert(Self::paint_ways(i as int, k as int) >= Self::paint_ways((i - 1) as int, k as int) + Self::paint_ways((i - 2) as int, k as int)) by (nonlinear_arith)\n requires\n 2 <= k,\n 0 <= Self::paint_ways((i - 1) as int, k as int),\n 0 <= Self::paint_ways((i - 2) as int, k as int),\n {}\n assert(prev1 as int + prev2 as int <= Self::paint_ways(i as int, k as int));\n Self::paint_ways_monotonic(i as int, n as int, k as int);\n assert(Self::paint_ways(i as int, k as int) <= Self::paint_ways(n as int, k as int));\n assert(prev1 as int + prev2 as int <= i32::MAX as int);\n }\n }\n let sum = prev1 + prev2;\n proof {\n assert(sum as int == prev1 as int + prev2 as int);\n if k == 1 {\n assert(k - 1 == 0);\n assert(0 <= (k as int - 1) * (sum as int) <= i32::MAX as int) by (nonlinear_arith)\n requires\n k == 1,\n {}\n } else {\n assert(2 <= k);\n assert(Self::paint_ways(i as int, k as int) == (k as int - 1) * (sum as int));\n Self::paint_ways_monotonic(i as int, n as int, k as int);\n assert(Self::paint_ways(i as int, k as int) <= Self::paint_ways(n as int, k as int));\n assert((k as int - 1) * (sum as int) <= i32::MAX as int);\n assert(0 <= (k as int - 1) * (sum as int));\n }\n }\n let next = (k - 1) * sum;\n proof {\n assert(next as int == (k as int - 1) * (sum as int));\n assert(next as int == Self::paint_ways(i as int, k as int));\n }\n prev2 = prev1;\n prev1 = next;\n i = i + 1;\n }\n prev1\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc276/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc276/tests/mutated_testcases.jsonl"} {"problem": "lc2760", "family": "leetcode", "description": "# Longest Even Odd Subarray With Threshold\n\nYou are given a **0-indexed** integer array `nums` and an integer `threshold`.\n\nFind the length of the **longest subarray** of `nums` starting at index `l` and ending at index `r` `(0 <= l <= r < nums.length)` that satisfies the following conditions:\n\n\t- `nums[l] % 2 == 0`\n\n\t- For all indices `i` in the range `[l, r - 1]`, `nums[i] % 2 != nums[i + 1] % 2`\n\n\t- For all indices `i` in the range `[l, r]`, `nums[i] <= threshold`\n\nReturn *an integer denoting the length of the longest such subarray.*\n\n**Note:** A **subarray** is a contiguous non-empty sequence of elements within an array.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [3,2,5,4], threshold = 5\n**Output:** 3\n**Explanation:** In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.\nHence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.\n```\n\nExample 2:\n\n```\n**Input:** nums = [1,2], threshold = 2\n**Output:** 1\n**Explanation:** In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. \nIt satisfies all the conditions and we can show that 1 is the maximum possible achievable length.\n```\n\nExample 3:\n\n```\n**Input:** nums = [2,3,4,5], threshold = 4\n**Output:** 3\n**Explanation:** In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. \nIt satisfies all the conditions.\nHence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 100 `\n\n\t- `1 <= nums[i] <= 100 `\n\n\t- `1 <= threshold <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_alternating_subarray(nums: Vec, threshold: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sliding Window\nAcceptance rate: 31.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn can_extend(nums: Seq, threshold: int, k: int) -> bool\n recommends\n 1 <= k < nums.len(),\n {\n nums[k] as int <= threshold && (nums[k] as int % 2) != (nums[k - 1] as int % 2)\n }\n\n pub open spec fn greedy_len_k(nums: Seq, threshold: int, i: int, k: int) -> int\n recommends\n 0 <= i < nums.len(),\n 0 <= k <= nums.len() - i - 1,\n decreases k,\n {\n if k <= 0 {\n if nums[i] as int % 2 == 0 && nums[i] as int <= threshold { 1int } else { 0int }\n } else {\n let prev = Self::greedy_len_k(nums, threshold, i, k - 1);\n let idx = i + k;\n if prev == k && Self::can_extend(nums, threshold, idx) {\n prev + 1int\n } else {\n prev\n }\n }\n }\n\n pub open spec fn start_len(nums: Seq, threshold: int, i: int) -> int\n recommends\n 0 <= i < nums.len(),\n {\n Self::greedy_len_k(nums, threshold, i, nums.len() - i - 1)\n }\n\n pub open spec fn best_prefix(nums: Seq, threshold: int, upto: int) -> int\n recommends\n 0 <= upto <= nums.len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n let s = upto - 1;\n let prev = Self::best_prefix(nums, threshold, upto - 1);\n let cur = Self::start_len(nums, threshold, s);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub fn longest_alternating_subarray(nums: Vec, threshold: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n 1 <= threshold <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::best_prefix(nums@, threshold as int, nums.len() as int),\n 0 <= result <= nums.len(),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn longest_alternating_subarray(nums: Vec, threshold: i32) -> i32 {\n let n = nums.len();\n let mut ans: i32 = 0;\n let mut i: usize = 0;\n\n while i < n {\n let mut len: i32 = 0;\n let mut active: bool = false;\n if nums[i] % 2 == 0 && nums[i] <= threshold {\n len = 1;\n active = true;\n }\n\n let mut j: usize = i + 1;\n while j < n {\n if active && nums[j] <= threshold && nums[j] % 2 != nums[j - 1] % 2 {\n len = len + 1;\n } else {\n active = false;\n }\n j = j + 1;\n }\n\n if len > ans {\n ans = len;\n }\n\n i = i + 1;\n }\n\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn can_extend(nums: Seq, threshold: int, k: int) -> bool\n recommends\n 1 <= k < nums.len(),\n {\n nums[k] as int <= threshold && (nums[k] as int % 2) != (nums[k - 1] as int % 2)\n }\n\n pub open spec fn greedy_len_k(nums: Seq, threshold: int, i: int, k: int) -> int\n recommends\n 0 <= i < nums.len(),\n 0 <= k <= nums.len() - i - 1,\n decreases k,\n {\n if k <= 0 {\n if nums[i] as int % 2 == 0 && nums[i] as int <= threshold { 1int } else { 0int }\n } else {\n let prev = Self::greedy_len_k(nums, threshold, i, k - 1);\n let idx = i + k;\n if prev == k && Self::can_extend(nums, threshold, idx) {\n prev + 1int\n } else {\n prev\n }\n }\n }\n\n pub open spec fn start_len(nums: Seq, threshold: int, i: int) -> int\n recommends\n 0 <= i < nums.len(),\n {\n Self::greedy_len_k(nums, threshold, i, nums.len() - i - 1)\n }\n\n pub open spec fn best_prefix(nums: Seq, threshold: int, upto: int) -> int\n recommends\n 0 <= upto <= nums.len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n let s = upto - 1;\n let prev = Self::best_prefix(nums, threshold, upto - 1);\n let cur = Self::start_len(nums, threshold, s);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub fn longest_alternating_subarray(nums: Vec, threshold: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n 1 <= threshold <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::best_prefix(nums@, threshold as int, nums.len() as int),\n 0 <= result <= nums.len(),\n {\n let n = nums.len();\n let mut ans: i32 = 0;\n let mut i: usize = 0;\n\n while i < n {\n let mut len: i32 = 0;\n let mut active: bool = false;\n if nums[i] % 2 == 0 && nums[i] <= threshold {\n len = 1;\n active = true;\n }\n\n let mut j: usize = i + 1;\n while j < n {\n if active && nums[j] <= threshold && nums[j] % 2 != nums[j - 1] % 2 {\n len = len + 1;\n } else {\n active = false;\n }\n j = j + 1;\n }\n\n if len > ans {\n ans = len;\n }\n\n i = i + 1;\n }\n\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn can_extend(nums: Seq, threshold: int, k: int) -> bool\n recommends\n 1 <= k < nums.len(),\n {\n nums[k] as int <= threshold && (nums[k] as int % 2) != (nums[k - 1] as int % 2)\n }\n\n pub open spec fn greedy_len_k(nums: Seq, threshold: int, i: int, k: int) -> int\n recommends\n 0 <= i < nums.len(),\n 0 <= k <= nums.len() - i - 1,\n decreases k,\n {\n if k <= 0 {\n if nums[i] as int % 2 == 0 && nums[i] as int <= threshold { 1int } else { 0int }\n } else {\n let prev = Self::greedy_len_k(nums, threshold, i, k - 1);\n let idx = i + k;\n if prev == k && Self::can_extend(nums, threshold, idx) {\n prev + 1int\n } else {\n prev\n }\n }\n }\n\n pub open spec fn start_len(nums: Seq, threshold: int, i: int) -> int\n recommends\n 0 <= i < nums.len(),\n {\n Self::greedy_len_k(nums, threshold, i, nums.len() - i - 1)\n }\n\n pub open spec fn best_prefix(nums: Seq, threshold: int, upto: int) -> int\n recommends\n 0 <= upto <= nums.len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n let s = upto - 1;\n let prev = Self::best_prefix(nums, threshold, upto - 1);\n let cur = Self::start_len(nums, threshold, s);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub fn longest_alternating_subarray(nums: Vec, threshold: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n 1 <= threshold <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::best_prefix(nums@, threshold as int, nums.len() as int),\n 0 <= result <= nums.len(),\n {\n let n = nums.len();\n let mut ans: i32 = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= threshold <= 100,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n 0 <= i <= n,\n 0 <= ans <= n,\n ans as int == Self::best_prefix(nums@, threshold as int, i as int),\n decreases n - i,\n {\n let mut len: i32 = 0;\n let mut active: bool = false;\n if nums[i] % 2 == 0 && nums[i] <= threshold {\n len = 1;\n active = true;\n }\n\n let mut j: usize = i + 1;\n while j < n\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= threshold <= 100,\n 0 <= i < n,\n i + 1 <= j <= n,\n 0 <= len <= n,\n len as int == Self::greedy_len_k(nums@, threshold as int, i as int, j as int - (i as int + 1)),\n active ==> len as int == j as int - i as int,\n !active ==> (len as int) < (j as int - i as int),\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n decreases n - j,\n {\n let ghost old_j = j;\n let ghost old_len = len;\n let ghost old_active = active;\n let ghost took_extend = old_active && nums[old_j as int] <= threshold && nums[old_j as int] % 2 != nums[(old_j - 1) as int] % 2;\n let ghost old_k = old_j as int - (i as int + 1);\n\n if active && nums[j] <= threshold && nums[j] % 2 != nums[j - 1] % 2 {\n len = len + 1;\n } else {\n active = false;\n }\n\n proof {\n assert(old_len as int == Self::greedy_len_k(nums@, threshold as int, i as int, old_k));\n assert(Self::greedy_len_k(nums@, threshold as int, i as int, old_k + 1)\n == if Self::greedy_len_k(nums@, threshold as int, i as int, old_k)\n == old_k + 1\n && Self::can_extend(nums@, threshold as int, old_j as int)\n {\n Self::greedy_len_k(nums@, threshold as int, i as int, old_k) + 1\n } else {\n Self::greedy_len_k(nums@, threshold as int, i as int, old_k)\n });\n\n if took_extend {\n assert((old_len as int) == (old_j as int - i as int));\n assert(Self::can_extend(nums@, threshold as int, old_j as int));\n assert((len as int) == (old_len as int + 1int));\n } else {\n if old_active {\n assert((old_len as int) == (old_j as int - i as int));\n assert(!Self::can_extend(nums@, threshold as int, old_j as int));\n assert(len == old_len);\n assert((len as int) < (old_j as int + 1 - i as int));\n } else {\n assert((old_len as int) < (old_j as int - i as int));\n assert(len == old_len);\n assert((len as int) < (old_j as int + 1 - i as int));\n }\n }\n }\n\n j = j + 1;\n }\n\n proof {\n assert(j == n);\n assert(len as int == Self::greedy_len_k(nums@, threshold as int, i as int, n as int - (i as int + 1)));\n assert(len as int == Self::start_len(nums@, threshold as int, i as int));\n }\n\n let ghost old_ans = ans;\n if len > ans {\n ans = len;\n }\n proof {\n assert(old_ans as int == Self::best_prefix(nums@, threshold as int, i as int));\n assert(Self::best_prefix(nums@, threshold as int, i as int + 1)\n == if Self::start_len(nums@, threshold as int, i as int)\n > Self::best_prefix(nums@, threshold as int, i as int)\n {\n Self::start_len(nums@, threshold as int, i as int)\n } else {\n Self::best_prefix(nums@, threshold as int, i as int)\n });\n }\n\n i = i + 1;\n }\n\n proof {\n assert(i == n);\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2760/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2760/tests/mutated_testcases.jsonl"} {"problem": "lc2784", "family": "leetcode", "description": "# Check if Array is Good\n\nYou are given an integer array `nums`. We consider an array **good **if it is a permutation of an array `base[n]`.\n\n`base[n] = [1, 2, ..., n - 1, n, n] `(in other words, it is an array of length `n + 1` which contains `1` to `n - 1 `exactly once, plus two occurrences of `n`). For example, `base[1] = [1, 1]` and` base[3] = [1, 2, 3, 3]`.\n\nReturn `true` *if the given array is good, otherwise return** *`false`.\n\n**Note: **A permutation of integers represents an arrangement of these numbers.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [2, 1, 3]\n**Output:** false\n**Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.\n```\n\nExample 2:\n\n```\n**Input:** nums = [1, 3, 3, 2]\n**Output:** true\n**Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.\n```\n\nExample 3:\n\n```\n**Input:** nums = [1, 1]\n**Output:** true\n**Explanation:** Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.\n```\n\nExample 4:\n\n```\n**Input:** nums = [3, 4, 4, 1, 2, 1]\n**Output:** false\n**Explanation:** Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 100`\n\n\t- `1 <= num[i] <= 200`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_good(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Sorting\nAcceptance rate: 48.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_value(nums: Seq, target: int, idx: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n (if nums[idx] as int == target { 1int } else { 0int })\n + Self::count_value(nums, target, idx + 1)\n }\n }\n\n pub open spec fn all_single(nums: Seq, v: int, n: int) -> bool\n decreases if v <= n { n - v + 1 } else { 0int },\n {\n if v >= n {\n true\n } else {\n Self::count_value(nums, v, 0) == 1 && Self::all_single(nums, v + 1, n)\n }\n }\n\n pub open spec fn is_good_spec(nums: Seq) -> bool {\n if nums.len() < 2 {\n false\n } else {\n let n = nums.len() - 1;\n Self::count_value(nums, n, 0) == 2 && Self::all_single(nums, 1, n)\n }\n }\n\n pub fn is_good(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 200,\n ensures\n result == Self::is_good_spec(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn count_value_exec(nums: &Vec, target: i32, idx: usize) -> usize {\n if idx >= nums.len() {\n 0\n } else {\n let tail: usize = Self::count_value_exec(nums, target, idx + 1);\n let add: usize = if nums[idx] == target { 1 } else { 0 };\n tail + add\n }\n }\n\n fn check_single_exec(nums: &Vec, v: usize, n: usize) -> bool {\n if v >= n {\n true\n } else {\n let cnt: usize = Self::count_value_exec(nums, v as i32, 0);\n cnt == 1 && Self::check_single_exec(nums, v + 1, n)\n }\n }\n\n pub fn is_good(nums: Vec) -> bool {\n if nums.len() < 2 {\n return false;\n }\n\n let n: usize = nums.len() - 1;\n let cnt_n: usize = Self::count_value_exec(&nums, n as i32, 0);\n let ok_single: bool = Self::check_single_exec(&nums, 1, n);\n cnt_n == 2 && ok_single\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_value(nums: Seq, target: int, idx: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n (if nums[idx] as int == target { 1int } else { 0int })\n + Self::count_value(nums, target, idx + 1)\n }\n }\n\n pub open spec fn all_single(nums: Seq, v: int, n: int) -> bool\n decreases if v <= n { n - v + 1 } else { 0int },\n {\n if v >= n {\n true\n } else {\n Self::count_value(nums, v, 0) == 1 && Self::all_single(nums, v + 1, n)\n }\n }\n\n pub open spec fn is_good_spec(nums: Seq) -> bool {\n if nums.len() < 2 {\n false\n } else {\n let n = nums.len() - 1;\n Self::count_value(nums, n, 0) == 2 && Self::all_single(nums, 1, n)\n }\n }\n\n fn count_value_exec(nums: &Vec, target: i32, idx: usize) -> (res: usize)\n requires\n idx <= nums.len(),\n nums.len() <= 100,\n ensures\n res as int == Self::count_value(nums@, target as int, idx as int),\n res <= nums.len() - idx,\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let tail: usize = Self::count_value_exec(nums, target, idx + 1);\n let add: usize = if nums[idx] == target { 1 } else { 0 };\n tail + add\n }\n }\n\n fn check_single_exec(nums: &Vec, v: usize, n: usize) -> (res: bool)\n requires\n nums.len() <= 100,\n n + 1 == nums.len(),\n 1 <= v,\n v <= n + 1,\n ensures\n res == Self::all_single(nums@, v as int, n as int),\n decreases n + 1 - v,\n {\n if v >= n {\n true\n } else {\n let cnt: usize = Self::count_value_exec(nums, v as i32, 0);\n cnt == 1 && Self::check_single_exec(nums, v + 1, n)\n }\n }\n\n pub fn is_good(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 200,\n ensures\n result == Self::is_good_spec(nums@),\n {\n if nums.len() < 2 {\n return false;\n }\n\n let n: usize = nums.len() - 1;\n let cnt_n: usize = Self::count_value_exec(&nums, n as i32, 0);\n let ok_single: bool = Self::check_single_exec(&nums, 1, n);\n cnt_n == 2 && ok_single\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_value(nums: Seq, target: int, idx: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n (if nums[idx] as int == target { 1int } else { 0int })\n + Self::count_value(nums, target, idx + 1)\n }\n }\n\n pub open spec fn all_single(nums: Seq, v: int, n: int) -> bool\n decreases if v <= n { n - v + 1 } else { 0int },\n {\n if v >= n {\n true\n } else {\n Self::count_value(nums, v, 0) == 1 && Self::all_single(nums, v + 1, n)\n }\n }\n\n pub open spec fn is_good_spec(nums: Seq) -> bool {\n if nums.len() < 2 {\n false\n } else {\n let n = nums.len() - 1;\n Self::count_value(nums, n, 0) == 2 && Self::all_single(nums, 1, n)\n }\n }\n\n fn count_value_exec(nums: &Vec, target: i32, idx: usize) -> (res: usize)\n requires\n idx <= nums.len(),\n nums.len() <= 100,\n ensures\n res as int == Self::count_value(nums@, target as int, idx as int),\n res <= nums.len() - idx,\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let tail: usize = Self::count_value_exec(nums, target, idx + 1);\n let add: usize = if nums[idx] == target { 1 } else { 0 };\n proof {\n assert(tail <= nums.len() - (idx + 1));\n assert(nums.len() - (idx + 1) < usize::MAX);\n }\n let ghost res: usize = (tail + add) as usize;\n proof {\n assert(add == 0 || add == 1);\n if add == 0 {\n assert(res <= nums.len() - idx);\n } else {\n assert(tail + 1 <= nums.len() - idx);\n assert(res <= nums.len() - idx);\n }\n assert(res as int\n == (if nums[idx as int] as int == target as int { 1int } else { 0int })\n + Self::count_value(nums@, target as int, idx as int + 1));\n }\n tail + add\n }\n }\n\n fn check_single_exec(nums: &Vec, v: usize, n: usize) -> (res: bool)\n requires\n nums.len() <= 100,\n n + 1 == nums.len(),\n 1 <= v,\n v <= n + 1,\n ensures\n res == Self::all_single(nums@, v as int, n as int),\n decreases n + 1 - v,\n {\n if v >= n {\n true\n } else {\n let cnt: usize = Self::count_value_exec(nums, v as i32, 0);\n cnt == 1 && Self::check_single_exec(nums, v + 1, n)\n }\n }\n\n pub fn is_good(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 200,\n ensures\n result == Self::is_good_spec(nums@),\n {\n if nums.len() < 2 {\n proof {\n assert(!Self::is_good_spec(nums@));\n }\n return false;\n }\n\n let n: usize = nums.len() - 1;\n let cnt_n: usize = Self::count_value_exec(&nums, n as i32, 0);\n let ok_single: bool = Self::check_single_exec(&nums, 1, n);\n proof {\n assert((cnt_n == 2) == (Self::count_value(nums@, n as int, 0) == 2));\n assert(ok_single == Self::all_single(nums@, 1, n as int));\n assert((cnt_n == 2 && ok_single) == Self::is_good_spec(nums@));\n }\n cnt_n == 2 && ok_single\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2784/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2784/tests/mutated_testcases.jsonl"} {"problem": "lc2829", "family": "leetcode", "description": "# Determine the Minimum Sum of a k-avoiding Array\n\nYou are given two integers, `n` and `k`.\n\nAn array of **distinct** positive integers is called a **k-avoiding** array if there does not exist any pair of distinct elements that sum to `k`.\n\nReturn *the **minimum** possible sum of a k-avoiding array of length *`n`.\n\n \n\nExample 1:\n\n```\n**Input:** n = 5, k = 4\n**Output:** 18\n**Explanation:** Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18.\nIt can be proven that there is no k-avoiding array with a sum less than 18.\n```\n\nExample 2:\n\n```\n**Input:** n = 2, k = 6\n**Output:** 3\n**Explanation:** We can construct the array [1,2], which has a sum of 3.\nIt can be proven that there is no k-avoiding array with a sum less than 3.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n, k <= 50`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_sum(n: i32, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Greedy\nAcceptance rate: 60.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn tri(x: int) -> int {\n x * (x + 1) / 2\n }\n\n pub open spec fn sum_range(start: int, count: int) -> int {\n count * (2 * start + count - 1) / 2\n }\n\n pub open spec fn minimum_sum_spec(n: int, k: int) -> int {\n let a = if n <= k / 2 { n } else { k / 2 };\n let b = n - a;\n Self::tri(a) + Self::sum_range(k, b)\n }\n\n pub fn minimum_sum(n: i32, k: i32) -> (ans: i32)\n requires\n 1 <= n <= 50,\n 1 <= k <= 50,\n ensures\n ans as int == Self::minimum_sum_spec(n as int, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_sum(n: i32, k: i32) -> i32 {\n let a: i64 = if (n as i64) <= (k as i64) / 2 { n as i64 } else { (k as i64) / 2 };\n let b: i64 = n as i64 - a;\n let left: i64 = a * (a + 1) / 2;\n let right: i64 = b * (2 * k as i64 + b - 1) / 2;\n (left + right) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn tri(x: int) -> int {\n x * (x + 1) / 2\n }\n\n pub open spec fn sum_range(start: int, count: int) -> int {\n count * (2 * start + count - 1) / 2\n }\n\n pub open spec fn minimum_sum_spec(n: int, k: int) -> int {\n let a = if n <= k / 2 { n } else { k / 2 };\n let b = n - a;\n Self::tri(a) + Self::sum_range(k, b)\n }\n\n pub fn minimum_sum(n: i32, k: i32) -> (ans: i32)\n requires\n 1 <= n <= 50,\n 1 <= k <= 50,\n ensures\n ans as int == Self::minimum_sum_spec(n as int, k as int),\n {\n let a: i64 = if (n as i64) <= (k as i64) / 2 { n as i64 } else { (k as i64) / 2 };\n let b: i64 = n as i64 - a;\n let left: i64 = a * (a + 1) / 2;\n let right: i64 = b * (2 * k as i64 + b - 1) / 2;\n (left + right) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn tri(x: int) -> int {\n x * (x + 1) / 2\n }\n\n pub open spec fn sum_range(start: int, count: int) -> int {\n count * (2 * start + count - 1) / 2\n }\n\n pub open spec fn minimum_sum_spec(n: int, k: int) -> int {\n let a = if n <= k / 2 { n } else { k / 2 };\n let b = n - a;\n Self::tri(a) + Self::sum_range(k, b)\n }\n\n pub fn minimum_sum(n: i32, k: i32) -> (ans: i32)\n requires\n 1 <= n <= 50,\n 1 <= k <= 50,\n ensures\n ans as int == Self::minimum_sum_spec(n as int, k as int),\n {\n let a: i64 = if (n as i64) <= (k as i64) / 2 { n as i64 } else { (k as i64) / 2 };\n let b: i64 = n as i64 - a;\n proof {\n assert(0 <= a <= 50);\n assert(0 <= b <= 50);\n assert(0 <= a * (a + 1) <= 2550) by (nonlinear_arith)\n requires 0 <= a <= 50\n {\n }\n assert(0 <= b * (2 * k as i64 + b - 1) <= 7450) by (nonlinear_arith)\n requires\n 0 <= b <= 50,\n 1 <= k as int <= 50,\n {\n }\n }\n let left: i64 = a * (a + 1) / 2;\n let right: i64 = b * (2 * k as i64 + b - 1) / 2;\n (left + right) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2829/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2829/tests/mutated_testcases.jsonl"} {"problem": "lc2834", "family": "leetcode", "description": "# Find the Minimum Possible Sum of a Beautiful Array\n\nYou are given positive integers `n` and `target`.\n\nAn array `nums` is **beautiful** if it meets the following conditions:\n\n\t- `nums.length == n`.\n\n\t- `nums` consists of pairwise **distinct** **positive** integers.\n\n\t- There doesn't exist two **distinct** indices, `i` and `j`, in the range `[0, n - 1]`, such that `nums[i] + nums[j] == target`.\n\nReturn *the **minimum** possible sum that a beautiful array could have modulo *`10^9 + 7`.\n\n \n\nExample 1:\n\n```\n**Input:** n = 2, target = 3\n**Output:** 4\n**Explanation:** We can see that nums = [1,3] is beautiful.\n- The array nums has length n = 2.\n- The array nums consists of pairwise distinct positive integers.\n- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.\nIt can be proven that 4 is the minimum possible sum that a beautiful array could have.\n```\n\nExample 2:\n\n```\n**Input:** n = 3, target = 3\n**Output:** 8\n**Explanation:** We can see that nums = [1,3,4] is beautiful.\n- The array nums has length n = 3.\n- The array nums consists of pairwise distinct positive integers.\n- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.\nIt can be proven that 8 is the minimum possible sum that a beautiful array could have.\n```\n\nExample 3:\n\n```\n**Input:** n = 1, target = 1\n**Output:** 1\n**Explanation:** We can see, that nums = [1] is beautiful.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n <= 10^9`\n\n\t- `1 <= target <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_possible_sum(n: i32, target: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Greedy\nAcceptance rate: 35.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn tri(x: int) -> int {\n x * (x + 1) / 2\n }\n\n pub open spec fn sum_range(start: int, count: int) -> int {\n count * (2 * start + count - 1) / 2\n }\n\n pub open spec fn minimum_raw(n: int, target: int) -> int {\n let a = if n <= target / 2 { n } else { target / 2 };\n let b = n - a;\n Self::tri(a) + Self::sum_range(target, b)\n }\n\n pub fn minimum_possible_sum(n: i32, target: i32) -> (ans: i32)\n requires\n 1 <= n <= 1000000000,\n 1 <= target <= 1000000000,\n ensures\n ans as int == Self::minimum_raw(n as int, target as int) % 1000000007,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_possible_sum(n: i32, target: i32) -> i32 {\n let m: i64 = 1_000_000_007;\n let a: i64 = if (n as i64) <= (target as i64) / 2 { n as i64 } else { (target as i64) / 2 };\n let b: i64 = n as i64 - a;\n let left: i64 = a * (a + 1) / 2;\n let right: i64 = b * (2 * target as i64 + b - 1) / 2;\n ((left + right) % m) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn tri(x: int) -> int {\n x * (x + 1) / 2\n }\n\n pub open spec fn sum_range(start: int, count: int) -> int {\n count * (2 * start + count - 1) / 2\n }\n\n pub open spec fn minimum_raw(n: int, target: int) -> int {\n let a = if n <= target / 2 { n } else { target / 2 };\n let b = n - a;\n Self::tri(a) + Self::sum_range(target, b)\n }\n\n pub fn minimum_possible_sum(n: i32, target: i32) -> (ans: i32)\n requires\n 1 <= n <= 1000000000,\n 1 <= target <= 1000000000,\n ensures\n ans as int == Self::minimum_raw(n as int, target as int) % 1000000007,\n {\n let m: i64 = 1_000_000_007;\n let a: i64 = if (n as i64) <= (target as i64) / 2 { n as i64 } else { (target as i64) / 2 };\n let b: i64 = n as i64 - a;\n let left: i64 = a * (a + 1) / 2;\n let right: i64 = b * (2 * target as i64 + b - 1) / 2;\n ((left + right) % m) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn tri(x: int) -> int {\n x * (x + 1) / 2\n }\n\n pub open spec fn sum_range(start: int, count: int) -> int {\n count * (2 * start + count - 1) / 2\n }\n\n pub open spec fn minimum_raw(n: int, target: int) -> int {\n let a = if n <= target / 2 { n } else { target / 2 };\n let b = n - a;\n Self::tri(a) + Self::sum_range(target, b)\n }\n\n pub fn minimum_possible_sum(n: i32, target: i32) -> (ans: i32)\n requires\n 1 <= n <= 1000000000,\n 1 <= target <= 1000000000,\n ensures\n ans as int == Self::minimum_raw(n as int, target as int) % 1000000007,\n {\n let m: i64 = 1_000_000_007;\n let a: i64 = if (n as i64) <= (target as i64) / 2 { n as i64 } else { (target as i64) / 2 };\n let b: i64 = n as i64 - a;\n proof {\n assert(0 <= a <= 1000000000);\n assert(0 <= b <= 1000000000);\n assert(0 <= a * (a + 1) <= 1000000001000000000) by (nonlinear_arith)\n requires 0 <= a <= 1000000000\n {\n }\n assert(0 <= b * (2 * target as i64 + b - 1) <= 2999999999000000000) by (nonlinear_arith)\n requires\n 0 <= b <= 1000000000,\n 1 <= target as int <= 1000000000,\n {\n }\n }\n let left: i64 = a * (a + 1) / 2;\n let right: i64 = b * (2 * target as i64 + b - 1) / 2;\n ((left + right) % m) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2834/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2834/tests/mutated_testcases.jsonl"} {"problem": "lc2869", "family": "leetcode", "description": "# Minimum Operations to Collect Elements\n\nYou are given an array `nums` of positive integers and an integer `k`.\n\nIn one operation, you can remove the last element of the array and add it to your collection.\n\nReturn *the **minimum number of operations** needed to collect elements* `1, 2, ..., k`.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [3,1,5,4,2], k = 2\n**Output:** 4\n**Explanation:** After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4.\n```\n\nExample 2:\n\n```\n**Input:** nums = [3,1,5,4,2], k = 5\n**Output:** 5\n**Explanation:** After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5.\n```\n\nExample 3:\n\n```\n**Input:** nums = [3,2,5,3,1], k = 3\n**Output:** 4\n**Explanation:** After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 50`\n\n\t- `1 <= nums[i] <= nums.length`\n\n\t- `1 <= k <= nums.length`\n\n\t- The input is generated such that you can collect elements `1, 2, ..., k`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Bit Manipulation\nAcceptance rate: 62.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seen_in_suffix(nums: Seq, v: int, ops: int) -> bool {\n exists |q: int|\n 0 <= q < ops\n && 0 <= nums.len() - ops + q < nums.len()\n && #[trigger] nums[nums.len() - ops + q] == v\n }\n\n pub open spec fn all_seen(nums: Seq, k: int, ops: int) -> bool {\n forall |v: int| 1 <= v <= k ==> Self::seen_in_suffix(nums, v, ops)\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n Self::all_seen(nums@, k as int, nums.len() as int),\n ensures\n 1 <= result <= nums.len(),\n Self::all_seen(nums@, k as int, result as int),\n forall |t: int| 1 <= t < result ==> !Self::all_seen(nums@, k as int, t),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn contains_in_suffix(nums: &Vec, ops: usize, v: i32) -> bool {\n let n = nums.len();\n let mut t: usize = 0;\n while t < ops {\n let idx = n - ops + t;\n if nums[idx] == v {\n return true;\n }\n t = t + 1;\n }\n false\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n let n = nums.len();\n let mut ops: usize = 1;\n while ops <= n {\n let mut ok = true;\n let mut v: i32 = 1;\n while v <= k {\n let found = Self::contains_in_suffix(&nums, ops, v);\n if !found {\n ok = false;\n }\n v = v + 1;\n }\n if ok {\n return ops as i32;\n }\n ops = ops + 1;\n }\n n as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seen_in_suffix(nums: Seq, v: int, ops: int) -> bool {\n exists |q: int|\n 0 <= q < ops\n && 0 <= nums.len() - ops + q < nums.len()\n && #[trigger] nums[nums.len() - ops + q] == v\n }\n\n pub open spec fn all_seen(nums: Seq, k: int, ops: int) -> bool {\n forall |v: int| 1 <= v <= k ==> Self::seen_in_suffix(nums, v, ops)\n }\n\n fn contains_in_suffix(nums: &Vec, ops: usize, v: i32) -> (found: bool)\n requires\n ops <= nums.len(),\n ensures\n found == Self::seen_in_suffix(nums@, v as int, ops as int),\n {\n let n = nums.len();\n let mut t: usize = 0;\n while t < ops {\n let idx = n - ops + t;\n if nums[idx] == v {\n return true;\n }\n t = t + 1;\n }\n false\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n Self::all_seen(nums@, k as int, nums.len() as int),\n ensures\n 1 <= result <= nums.len(),\n Self::all_seen(nums@, k as int, result as int),\n forall |t: int| 1 <= t < result ==> !Self::all_seen(nums@, k as int, t),\n {\n let n = nums.len();\n let mut ops: usize = 1;\n while ops <= n {\n let mut ok = true;\n let mut v: i32 = 1;\n while v <= k {\n let found = Self::contains_in_suffix(&nums, ops, v);\n if !found {\n ok = false;\n }\n v = v + 1;\n }\n if ok {\n return ops as i32;\n }\n ops = ops + 1;\n }\n n as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seen_in_suffix(nums: Seq, v: int, ops: int) -> bool {\n exists |q: int|\n 0 <= q < ops\n && 0 <= nums.len() - ops + q < nums.len()\n && #[trigger] nums[nums.len() - ops + q] == v\n }\n\n pub open spec fn all_seen(nums: Seq, k: int, ops: int) -> bool {\n forall |v: int| 1 <= v <= k ==> Self::seen_in_suffix(nums, v, ops)\n }\n\n fn contains_in_suffix(nums: &Vec, ops: usize, v: i32) -> (found: bool)\n requires\n ops <= nums.len(),\n ensures\n found == Self::seen_in_suffix(nums@, v as int, ops as int),\n {\n let n = nums.len();\n let mut t: usize = 0;\n while t < ops\n invariant\n ops <= nums.len(),\n n == nums.len(),\n 0 <= t <= ops,\n forall |q: int| 0 <= q < t as int ==> #[trigger] nums[n as int - ops as int + q] != v,\n decreases ops - t,\n {\n let idx = n - ops + t;\n if nums[idx] == v {\n proof {\n assert(0 <= (t as int));\n assert((t as int) < (ops as int));\n assert(0 <= (n as int) - (ops as int) + (t as int));\n assert((n as int) - (ops as int) + (t as int) < (n as int));\n assert(nums[(n as int) - (ops as int) + (t as int)] == v);\n assert(Self::seen_in_suffix(nums@, v as int, ops as int));\n }\n return true;\n }\n t = t + 1;\n }\n proof {\n assert forall |q: int| 0 <= q < ops as int implies #[trigger] nums[n as int - ops as int + q] != v by {\n assert(q < t as int);\n }\n assert(!Self::seen_in_suffix(nums@, v as int, ops as int));\n }\n false\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n Self::all_seen(nums@, k as int, nums.len() as int),\n ensures\n 1 <= result <= nums.len(),\n Self::all_seen(nums@, k as int, result as int),\n forall |t: int| 1 <= t < result ==> !Self::all_seen(nums@, k as int, t),\n {\n let n = nums.len();\n let mut ops: usize = 1;\n while ops <= n\n invariant\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n Self::all_seen(nums@, k as int, nums.len() as int),\n n == nums.len(),\n 1 <= ops <= n + 1,\n forall |t: int| 1 <= t < ops ==> !Self::all_seen(nums@, k as int, t),\n decreases n + 1 - ops,\n {\n let mut ok = true;\n assert(ops <= n);\n let mut v: i32 = 1;\n while v <= k\n invariant\n 1 <= v <= k + 1,\n ops <= n,\n n == nums.len(),\n k <= 50,\n ok == (forall |x: int| 1 <= x < v as int ==> Self::seen_in_suffix(nums@, x, ops as int)),\n decreases k + 1 - v,\n {\n let found = Self::contains_in_suffix(&nums, ops, v);\n if !found {\n ok = false;\n }\n v = v + 1;\n }\n if ok {\n proof {\n assert(Self::all_seen(nums@, k as int, ops as int));\n }\n return ops as i32;\n }\n ops = ops + 1;\n }\n n as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2869/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2869/tests/mutated_testcases.jsonl"} {"problem": "lc287", "family": "leetcode", "description": "# Find the Duplicate Number\n\nGiven an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.\n\nThere is only **one repeated number** in `nums`, return *this repeated number*.\n\nYou must solve the problem **without** modifying the array `nums` and using only constant extra space.\n\n## Example 1:\n\n> **Input:** nums = [1,3,4,2,2]\n> **Output:** 2\n\n## Example 2:\n\n> **Input:** nums = [3,1,3,4,2]\n> **Output:** 3\n\n## Example 3:\n\n> **Input:** nums = [3,3,3,3,3]\n> **Output:** 3\n\n## Constraints:\n\n- $1 <= n <= 10^5$\n\n- $nums.length == n + 1$\n\n- $1 <= nums[i] <= n$\n\n- All the integers in `nums` appear only **once** except for **precisely one integer** which appears **two or more** times.\n\n**Follow up:**\n\n- How can we prove that at least one duplicate number must exist in `nums`?\n\n- Can you solve the problem in linear runtime complexity?\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_duplicate(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Two Pointers, Binary Search, Bit Manipulation\nAcceptance rate: 63.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn find_duplicate(nums: Vec) -> (res: i32) \n requires\n 1 <= nums.len() - 1 <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len() - 1, \n exists |i: int, j: int| 0 <= i < j < nums.len() && nums[i] == nums[j],\n forall |i1: int, j1: int, i2: int, j2: int|\n 0 <= i1 < j1 < nums.len() &&\n 0 <= i2 < j2 < nums.len() &&\n nums[i1] == nums[j1] &&\n nums[i2] == nums[j2] ==> nums[i1] == nums[i2],\n ensures \n 1 <= res <= nums.len() - 1,\n exists |i: int, j: int| 0 <= i < j < nums.len() && \n nums[i] == nums[j] && nums[i] == res,\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn find_duplicate(nums: Vec) -> i32\n {\n let mut numTrack: [bool;100001] = [false;100001];\n\n let mut i = 0;\n while i < nums.len()\n {\n if numTrack[nums[i] as usize] {\n return nums[i];\n }\n else {\n numTrack[nums[i] as usize] = true;\n }\n i += 1;\n }\n return 0 \n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn find_duplicate(nums: Vec) -> (res: i32) \n requires\n 1 <= nums.len() - 1 <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len() - 1, \n exists |i: int, j: int| 0 <= i < j < nums.len() && nums[i] == nums[j],\n forall |i1: int, j1: int, i2: int, j2: int|\n 0 <= i1 < j1 < nums.len() &&\n 0 <= i2 < j2 < nums.len() &&\n nums[i1] == nums[j1] &&\n nums[i2] == nums[j2] ==> nums[i1] == nums[i2],\n ensures \n 1 <= res <= nums.len() - 1,\n exists |i: int, j: int| 0 <= i < j < nums.len() && \n nums[i] == nums[j] && nums[i] == res,\n {\n let mut numTrack: [bool;100001] = [false;100001];\n\n let mut i = 0;\n while i < nums.len()\n {\n if numTrack[nums[i] as usize] {\n return nums[i];\n }\n else {\n numTrack[nums[i] as usize] = true;\n }\n i += 1;\n }\n return 0 \n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn find_duplicate(nums: Vec) -> (res: i32) \n requires\n 1 <= nums.len() - 1 <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len() - 1, \n exists |i: int, j: int| 0 <= i < j < nums.len() && nums[i] == nums[j],\n forall |i1: int, j1: int, i2: int, j2: int|\n 0 <= i1 < j1 < nums.len() &&\n 0 <= i2 < j2 < nums.len() &&\n nums[i1] == nums[j1] &&\n nums[i2] == nums[j2] ==> nums[i1] == nums[i2],\n ensures \n 1 <= res <= nums.len() - 1,\n exists |i: int, j: int| 0 <= i < j < nums.len() && \n nums[i] == nums[j] && nums[i] == res,\n {\n let mut numTrack: [bool;100001] = [false;100001];\n\n let mut i = 0;\n while i < nums.len()\n invariant \n 1 <= nums.len() - 1 <= 100_000, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len() - 1, \n exists |i: int, j: int| 0 <= i < j < nums.len() && nums[i] == nums[j],\n forall |k: int| 1 <= k <= 100_000 ==> \n numTrack[k] == exists |j: int| 0 <= j < i && nums[j] == k,\n forall |ii: int, jj: int| 0 <= ii < jj < i ==> nums[ii] != nums[jj],\n decreases nums.len() - i, \n {\n if numTrack[nums[i] as usize] {\n return nums[i];\n }\n else {\n numTrack[nums[i] as usize] = true;\n }\n i += 1;\n }\n return 0 \n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc287/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc287/tests/mutated_testcases.jsonl"} {"problem": "lc2873", "family": "leetcode", "description": "# Maximum Value of an Ordered Triplet I\n\nYou are given a **0-indexed** integer array `nums`.\n\nReturn ***the maximum value over all triplets of indices*** `(i, j, k)` *such that* `i < j < k`. If all such triplets have a negative value, return `0`.\n\nThe **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [12,6,1,2,7]\n**Output:** 77\n**Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.\nIt can be shown that there are no ordered triplets of indices with a value greater than 77.\n```\n\nExample 2:\n\n```\n**Input:** nums = [1,10,3,4,19]\n**Output:** 133\n**Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.\nIt can be shown that there are no ordered triplets of indices with a value greater than 133.\n```\n\nExample 3:\n\n```\n**Input:** nums = [1,2,3]\n**Output:** 0\n**Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.\n```\n\n \n\n**Constraints:**\n\n\t- `3 <= nums.length <= 100`\n\n\t- `1 <= nums[i] <= 10^6`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn maximum_triplet_value(nums: Vec) -> i64 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 58.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn triplet_value(nums: Seq, i: int, j: int, k: int) -> int\n recommends\n 0 <= i < j < k < nums.len(),\n {\n (nums[i] as int - nums[j] as int) * nums[k] as int\n }\n\n pub open spec fn max2(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn max_k(nums: Seq, i: int, j: int, k: int, acc: int) -> int\n decreases nums.len() - k,\n {\n if k >= nums.len() {\n acc\n } else {\n let v = Self::triplet_value(nums, i, j, k);\n Self::max_k(nums, i, j, k + 1, Self::max2(acc, v))\n }\n }\n\n pub open spec fn max_j(nums: Seq, i: int, j: int, acc: int) -> int\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n acc\n } else {\n let acc2 = Self::max_k(nums, i, j, j + 1, acc);\n Self::max_j(nums, i, j + 1, acc2)\n }\n }\n\n pub open spec fn max_i(nums: Seq, i: int, acc: int) -> int\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n acc\n } else {\n let acc2 = Self::max_j(nums, i, i + 1, acc);\n Self::max_i(nums, i + 1, acc2)\n }\n }\n\n pub open spec fn maximum_triplet_value_spec(nums: Seq) -> int {\n Self::max_i(nums, 0, 0)\n }\n\n pub fn maximum_triplet_value(nums: Vec) -> (result: i64)\n requires\n 3 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n result as int == Self::maximum_triplet_value_spec(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn solve_k(nums: &Vec, i: usize, j: usize, k: usize, acc: i64) -> i64 {\n if k >= nums.len() {\n acc\n } else {\n let v: i64 = (nums[i] as i64 - nums[j] as i64) * nums[k] as i64;\n let acc2: i64 = if acc >= v { acc } else { v };\n Self::solve_k(nums, i, j, k + 1, acc2)\n }\n }\n\n fn solve_j(nums: &Vec, i: usize, j: usize, acc: i64) -> i64 {\n if j >= nums.len() {\n acc\n } else {\n let acc2: i64 = Self::solve_k(nums, i, j, j + 1, acc);\n Self::solve_j(nums, i, j + 1, acc2)\n }\n }\n\n fn solve_i(nums: &Vec, i: usize, acc: i64) -> i64 {\n if i >= nums.len() {\n acc\n } else {\n let acc2: i64 = Self::solve_j(nums, i, i + 1, acc);\n Self::solve_i(nums, i + 1, acc2)\n }\n }\n\n pub fn maximum_triplet_value(nums: Vec) -> i64 {\n Self::solve_i(&nums, 0, 0)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn triplet_value(nums: Seq, i: int, j: int, k: int) -> int\n recommends\n 0 <= i < j < k < nums.len(),\n {\n (nums[i] as int - nums[j] as int) * nums[k] as int\n }\n\n pub open spec fn max2(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn max_k(nums: Seq, i: int, j: int, k: int, acc: int) -> int\n decreases nums.len() - k,\n {\n if k >= nums.len() {\n acc\n } else {\n let v = Self::triplet_value(nums, i, j, k);\n Self::max_k(nums, i, j, k + 1, Self::max2(acc, v))\n }\n }\n\n pub open spec fn max_j(nums: Seq, i: int, j: int, acc: int) -> int\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n acc\n } else {\n let acc2 = Self::max_k(nums, i, j, j + 1, acc);\n Self::max_j(nums, i, j + 1, acc2)\n }\n }\n\n pub open spec fn max_i(nums: Seq, i: int, acc: int) -> int\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n acc\n } else {\n let acc2 = Self::max_j(nums, i, i + 1, acc);\n Self::max_i(nums, i + 1, acc2)\n }\n }\n\n pub open spec fn maximum_triplet_value_spec(nums: Seq) -> int {\n Self::max_i(nums, 0, 0)\n }\n\n fn solve_k(nums: &Vec, i: usize, j: usize, k: usize, acc: i64) -> (res: i64)\n requires\n i < j,\n j < nums.len(),\n j <= k,\n k <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 1_000_000,\n -9_223_372_036_854_775_808 <= acc <= 9_223_372_036_854_775_807,\n ensures\n res as int == Self::max_k(nums@, i as int, j as int, k as int, acc as int),\n decreases nums.len() - k,\n {\n if k >= nums.len() {\n acc\n } else {\n let v: i64 = (nums[i] as i64 - nums[j] as i64) * nums[k] as i64;\n let acc2: i64 = if acc >= v { acc } else { v };\n Self::solve_k(nums, i, j, k + 1, acc2)\n }\n }\n\n fn solve_j(nums: &Vec, i: usize, j: usize, acc: i64) -> (res: i64)\n requires\n i < j,\n i < nums.len(),\n j <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 1_000_000,\n -9_223_372_036_854_775_808 <= acc <= 9_223_372_036_854_775_807,\n ensures\n res as int == Self::max_j(nums@, i as int, j as int, acc as int),\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n acc\n } else {\n let acc2: i64 = Self::solve_k(nums, i, j, j + 1, acc);\n Self::solve_j(nums, i, j + 1, acc2)\n }\n }\n\n fn solve_i(nums: &Vec, i: usize, acc: i64) -> (res: i64)\n requires\n i <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 1_000_000,\n -9_223_372_036_854_775_808 <= acc <= 9_223_372_036_854_775_807,\n ensures\n res as int == Self::max_i(nums@, i as int, acc as int),\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n acc\n } else {\n let acc2: i64 = Self::solve_j(nums, i, i + 1, acc);\n Self::solve_i(nums, i + 1, acc2)\n }\n }\n\n pub fn maximum_triplet_value(nums: Vec) -> (result: i64)\n requires\n 3 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n result as int == Self::maximum_triplet_value_spec(nums@),\n {\n Self::solve_i(&nums, 0, 0)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn triplet_value(nums: Seq, i: int, j: int, k: int) -> int\n recommends\n 0 <= i < j < k < nums.len(),\n {\n (nums[i] as int - nums[j] as int) * nums[k] as int\n }\n\n pub open spec fn max2(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn max_k(nums: Seq, i: int, j: int, k: int, acc: int) -> int\n decreases nums.len() - k,\n {\n if k >= nums.len() {\n acc\n } else {\n let v = Self::triplet_value(nums, i, j, k);\n Self::max_k(nums, i, j, k + 1, Self::max2(acc, v))\n }\n }\n\n pub open spec fn max_j(nums: Seq, i: int, j: int, acc: int) -> int\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n acc\n } else {\n let acc2 = Self::max_k(nums, i, j, j + 1, acc);\n Self::max_j(nums, i, j + 1, acc2)\n }\n }\n\n pub open spec fn max_i(nums: Seq, i: int, acc: int) -> int\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n acc\n } else {\n let acc2 = Self::max_j(nums, i, i + 1, acc);\n Self::max_i(nums, i + 1, acc2)\n }\n }\n\n pub open spec fn maximum_triplet_value_spec(nums: Seq) -> int {\n Self::max_i(nums, 0, 0)\n }\n\n fn solve_k(nums: &Vec, i: usize, j: usize, k: usize, acc: i64) -> (res: i64)\n requires\n i < j,\n j < nums.len(),\n j <= k,\n k <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 1_000_000,\n -9_223_372_036_854_775_808 <= acc <= 9_223_372_036_854_775_807,\n ensures\n res as int == Self::max_k(nums@, i as int, j as int, k as int, acc as int),\n decreases nums.len() - k,\n {\n if k >= nums.len() {\n acc\n } else {\n proof {\n assert(0 <= nums[i as int] <= 1_000_000);\n assert(0 <= nums[j as int] <= 1_000_000);\n assert(0 <= nums[k as int] <= 1_000_000);\n assert(-1_000_000 <= nums[i as int] - nums[j as int] <= 1_000_000);\n assert(-1_000_000_000_000 <= (nums[i as int] - nums[j as int]) * nums[k as int] <= 1_000_000_000_000) by (nonlinear_arith)\n requires\n -1_000_000 <= nums[i as int] - nums[j as int] <= 1_000_000,\n 0 <= nums[k as int] <= 1_000_000;\n }\n let v: i64 = (nums[i] as i64 - nums[j] as i64) * nums[k] as i64;\n let acc2: i64 = if acc >= v { acc } else { v };\n proof {\n assert(v as int == Self::triplet_value(nums@, i as int, j as int, k as int));\n assert(acc2 as int == Self::max2(acc as int, v as int));\n }\n Self::solve_k(nums, i, j, k + 1, acc2)\n }\n }\n\n fn solve_j(nums: &Vec, i: usize, j: usize, acc: i64) -> (res: i64)\n requires\n i < j,\n i < nums.len(),\n j <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 1_000_000,\n -9_223_372_036_854_775_808 <= acc <= 9_223_372_036_854_775_807,\n ensures\n res as int == Self::max_j(nums@, i as int, j as int, acc as int),\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n acc\n } else {\n let acc2: i64 = Self::solve_k(nums, i, j, j + 1, acc);\n Self::solve_j(nums, i, j + 1, acc2)\n }\n }\n\n fn solve_i(nums: &Vec, i: usize, acc: i64) -> (res: i64)\n requires\n i <= nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 1_000_000,\n -9_223_372_036_854_775_808 <= acc <= 9_223_372_036_854_775_807,\n ensures\n res as int == Self::max_i(nums@, i as int, acc as int),\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n acc\n } else {\n let acc2: i64 = Self::solve_j(nums, i, i + 1, acc);\n Self::solve_i(nums, i + 1, acc2)\n }\n }\n\n pub fn maximum_triplet_value(nums: Vec) -> (result: i64)\n requires\n 3 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000,\n ensures\n result as int == Self::maximum_triplet_value_spec(nums@),\n {\n Self::solve_i(&nums, 0, 0)\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2873/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2873/tests/mutated_testcases.jsonl"} {"problem": "lc2909", "family": "leetcode", "description": "# Minimum Sum of Mountain Triplets II\n\nYou are given a **0-indexed** array `nums` of integers.\n\nA triplet of indices `(i, j, k)` is a **mountain** if:\n\n- `i < j < k`\n- `nums[i] < nums[j]` and `nums[k] < nums[j]`\n\nReturn the **minimum possible sum** of a mountain triplet of `nums`. If no such triplet exists, return `-1`.\n\n## Example 1:\n\n> **Input:** nums = [8,6,1,5,3]\n> **Output:** 9\n> **Explanation:** Triplet `(2, 3, 4)` is a mountain triplet of sum 9 since:\n> - `2 < 3 < 4`\n> - `nums[2] < nums[3]` and `nums[4] < nums[3]`\n>\n> The sum is `nums[2] + nums[3] + nums[4] = 9`. There is no mountain triplet with a smaller sum.\n\n## Example 2:\n\n> **Input:** nums = [5,4,8,7,10,2]\n> **Output:** 13\n> **Explanation:** Triplet `(1, 3, 5)` is a mountain triplet of sum 13 since:\n> - `1 < 3 < 5`\n> - `nums[1] < nums[3]` and `nums[5] < nums[3]`\n>\n> The sum is `nums[1] + nums[3] + nums[5] = 13`. There is no mountain triplet with a smaller sum.\n\n## Example 3:\n\n> **Input:** nums = [6,5,4,3,4,5]\n> **Output:** -1\n> **Explanation:** There are no mountain triplets in `nums`.\n\n## Constraints:\n\n- `3 <= nums.length <= 10^5`\n- `1 <= nums[i] <= 10^8`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_sum(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nSenior, Array, Weekly Contest 368\nAcceptance rate: 54.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_triplet(nums: Seq, i: int, j: int, k: int) -> bool {\n &&& 0 <= i < j < k < nums.len()\n &&& nums[i] < nums[j]\n &&& nums[k] < nums[j]\n }\n\n pub open spec fn triplet_sum(nums: Seq, i: int, j: int, k: int) -> int {\n nums[i] as int + nums[j] as int + nums[k] as int\n }\n\n pub fn minimum_sum(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n ensures\n result == -1 ==> forall |i: int, j: int, k: int|\n !Self::is_mountain_triplet(nums@, i, j, k),\n result != -1 ==> exists |i: int, j: int, k: int|\n Self::is_mountain_triplet(nums@, i, j, k)\n && result as int == Self::triplet_sum(nums@, i, j, k),\n result != -1 ==> forall |i: int, j: int, k: int|\n Self::is_mountain_triplet(nums@, i, j, k)\n ==> result as int <= Self::triplet_sum(nums@, i, j, k),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_sum(nums: Vec) -> i32 {\n let n = nums.len();\n\n let mut left_idx: Vec = Vec::new();\n left_idx.push(0);\n\n let mut p: usize = 1;\n while p < n {\n let mut best = left_idx[p - 1];\n if nums[p] < nums[best] {\n best = p;\n }\n left_idx.push(best);\n p += 1;\n }\n\n let mut right_idx: Vec = Vec::new();\n let mut q: usize = 0;\n while q < n {\n right_idx.push(q);\n q += 1;\n }\n\n let mut q: usize = n - 1;\n while q > 0 {\n let prev = q - 1;\n let mut best = right_idx[q];\n if nums[prev] <= nums[best] {\n best = prev;\n }\n right_idx[prev] = best;\n q -= 1;\n }\n\n let mut best_sum: i32 = 300000001;\n let mut j: usize = 1;\n while j + 1 < n {\n let left = left_idx[j - 1];\n let right = right_idx[j + 1];\n if nums[left] < nums[j] && nums[right] < nums[j] {\n let candidate = nums[left] + nums[j] + nums[right];\n if candidate < best_sum {\n best_sum = candidate;\n }\n }\n j += 1;\n }\n\n if best_sum == 300000001 {\n -1\n } else {\n best_sum\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_triplet(nums: Seq, i: int, j: int, k: int) -> bool {\n &&& 0 <= i < j < k < nums.len()\n &&& nums[i] < nums[j]\n &&& nums[k] < nums[j]\n }\n\n pub open spec fn triplet_sum(nums: Seq, i: int, j: int, k: int) -> int {\n nums[i] as int + nums[j] as int + nums[k] as int\n }\n\n pub fn minimum_sum(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n ensures\n result == -1 ==> forall |i: int, j: int, k: int|\n !Self::is_mountain_triplet(nums@, i, j, k),\n result != -1 ==> exists |i: int, j: int, k: int|\n Self::is_mountain_triplet(nums@, i, j, k)\n && result as int == Self::triplet_sum(nums@, i, j, k),\n result != -1 ==> forall |i: int, j: int, k: int|\n Self::is_mountain_triplet(nums@, i, j, k)\n ==> result as int <= Self::triplet_sum(nums@, i, j, k),\n {\n let n = nums.len();\n\n let mut left_idx: Vec = Vec::new();\n left_idx.push(0);\n let mut p: usize = 1;\n while p < n {\n let mut best = left_idx[p - 1];\n if nums[p] < nums[best] {\n best = p;\n }\n left_idx.push(best);\n p += 1;\n }\n\n let mut right_idx: Vec = Vec::new();\n let mut q: usize = 0;\n while q < n {\n right_idx.push(q);\n q += 1;\n }\n\n let mut q: usize = n - 1;\n while q > 0 {\n let prev = q - 1;\n let mut best = right_idx[q];\n if nums[prev] <= nums[best] {\n best = prev;\n }\n right_idx.set(prev, best);\n q -= 1;\n }\n\n let mut best_sum: i32 = 300000001;\n let mut j: usize = 1;\n while j + 1 < n {\n let left = left_idx[j - 1];\n let right = right_idx[j + 1];\n if nums[left] < nums[j] && nums[right] < nums[j] {\n let candidate = nums[left] + nums[j] + nums[right];\n if candidate < best_sum {\n best_sum = candidate;\n }\n }\n j += 1;\n }\n\n if best_sum == 300000001 {\n -1\n } else {\n best_sum\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_triplet(nums: Seq, i: int, j: int, k: int) -> bool {\n &&& 0 <= i < j < k < nums.len()\n &&& nums[i] < nums[j]\n &&& nums[k] < nums[j]\n }\n\n pub open spec fn triplet_sum(nums: Seq, i: int, j: int, k: int) -> int {\n nums[i] as int + nums[j] as int + nums[k] as int\n }\n\n pub fn minimum_sum(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n ensures\n result == -1 ==> forall |i: int, j: int, k: int|\n !Self::is_mountain_triplet(nums@, i, j, k),\n result != -1 ==> exists |i: int, j: int, k: int|\n Self::is_mountain_triplet(nums@, i, j, k)\n && result as int == Self::triplet_sum(nums@, i, j, k),\n result != -1 ==> forall |i: int, j: int, k: int|\n Self::is_mountain_triplet(nums@, i, j, k)\n ==> result as int <= Self::triplet_sum(nums@, i, j, k),\n {\n let n = nums.len();\n\n let mut left_idx: Vec = Vec::new();\n left_idx.push(0);\n let mut p: usize = 1;\n while p < n\n invariant\n n == nums.len(),\n 3 <= n <= 100000,\n 1 <= p <= n,\n left_idx.len() == p,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n forall |j: int| 0 <= j < p ==> 0 <= #[trigger] left_idx[j] <= j,\n forall |j: int, t: int|\n 0 <= j < p && 0 <= t <= j ==> nums[#[trigger] left_idx[j] as int] <= #[trigger] nums[t],\n decreases n - p,\n {\n let mut best = left_idx[p - 1];\n if nums[p] < nums[best] {\n best = p;\n proof {\n assert forall |t: int| 0 <= t <= p as int implies nums[best as int] <= #[trigger] nums[t] by {\n if t == p as int {\n } else {\n assert(0 <= t <= p as int - 1);\n assert(nums[left_idx[p as int - 1] as int] <= nums[t]);\n assert(nums[best as int] == nums[p as int]);\n assert(nums[p as int] < nums[left_idx[p as int - 1] as int]);\n }\n };\n }\n } else {\n proof {\n assert forall |t: int| 0 <= t <= p as int implies nums[best as int] <= #[trigger] nums[t] by {\n if t == p as int {\n assert(nums[p as int] >= nums[best as int]);\n } else {\n assert(0 <= t <= p as int - 1);\n assert(nums[left_idx[p as int - 1] as int] <= nums[t]);\n assert(best == left_idx[p as int - 1]);\n }\n };\n }\n }\n left_idx.push(best);\n p += 1;\n }\n\n let mut right_idx: Vec = Vec::new();\n let mut q: usize = 0;\n while q < n\n invariant\n n == nums.len(),\n 3 <= n <= 100000,\n 0 <= q <= n,\n right_idx.len() == q,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n forall |j: int| 0 <= j < q ==> #[trigger] right_idx[j] == j,\n decreases n - q,\n {\n right_idx.push(q);\n q += 1;\n }\n\n let mut q: usize = n - 1;\n while q > 0\n invariant\n n == nums.len(),\n 3 <= n <= 100000,\n 0 <= q < n,\n right_idx.len() == n,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n forall |j: int| 0 <= j < q ==> #[trigger] right_idx[j] == j,\n forall |j: int| q <= j < n ==> j <= #[trigger] right_idx[j] < n,\n forall |j: int, t: int|\n q <= j < n && j <= t < n ==> nums[#[trigger] right_idx[j] as int] <= #[trigger] nums[t],\n decreases q,\n {\n let prev = q - 1;\n let mut best = right_idx[q];\n if nums[prev] <= nums[best] {\n best = prev;\n proof {\n assert forall |t: int| prev as int <= t < n as int implies nums[best as int] <= #[trigger] nums[t] by {\n if t == prev as int {\n } else {\n assert(q as int <= t < n as int);\n assert(nums[right_idx[q as int] as int] <= nums[t]);\n assert(nums[best as int] == nums[prev as int]);\n assert(nums[prev as int] <= nums[right_idx[q as int] as int]);\n }\n };\n }\n } else {\n proof {\n assert forall |t: int| prev as int <= t < n as int implies nums[best as int] <= #[trigger] nums[t] by {\n if t == prev as int {\n assert(nums[prev as int] > nums[best as int]);\n } else {\n assert(q as int <= t < n as int);\n assert(nums[right_idx[q as int] as int] <= nums[t]);\n assert(best == right_idx[q as int]);\n }\n };\n }\n }\n right_idx.set(prev, best);\n q -= 1;\n }\n\n let mut best_sum: i32 = 300000001;\n let ghost mut best_i: int = 0;\n let ghost mut best_j: int = 0;\n let ghost mut best_k: int = 0;\n let mut j: usize = 1;\n while j + 1 < n\n invariant\n n == nums.len(),\n 3 <= n <= 100000,\n 1 <= j <= n - 1,\n left_idx.len() == n,\n right_idx.len() == n,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100000000,\n forall |c: int| 0 <= c < n ==> 0 <= #[trigger] left_idx[c] <= c,\n forall |c: int, t: int|\n 0 <= c < n && 0 <= t <= c ==> nums[#[trigger] left_idx[c] as int] <= #[trigger] nums[t],\n forall |c: int| 0 <= c < n ==> c <= #[trigger] right_idx[c] < n,\n forall |c: int, t: int|\n 0 <= c < n && c <= t < n ==> nums[#[trigger] right_idx[c] as int] <= #[trigger] nums[t],\n 0 < best_sum <= 300000001,\n best_sum == 300000001 ==> forall |i: int, c: int, k: int|\n 1 <= c < j ==> !Self::is_mountain_triplet(nums@, i, c, k),\n best_sum < 300000001 ==> Self::is_mountain_triplet(nums@, best_i, best_j, best_k),\n best_sum < 300000001 ==> 1 <= best_j < j,\n best_sum < 300000001 ==> best_sum as int == Self::triplet_sum(nums@, best_i, best_j, best_k),\n best_sum < 300000001 ==> forall |i: int, c: int, k: int|\n 1 <= c < j && Self::is_mountain_triplet(nums@, i, c, k)\n ==> best_sum as int <= Self::triplet_sum(nums@, i, c, k),\n decreases n - 1 - j,\n {\n let left = left_idx[j - 1];\n let right = right_idx[j + 1];\n let ghost old_best = best_sum;\n\n if nums[left] < nums[j] && nums[right] < nums[j] {\n let candidate = nums[left] + nums[j] + nums[right];\n proof {\n assert(Self::is_mountain_triplet(nums@, left as int, j as int, right as int));\n assert(candidate as int == Self::triplet_sum(nums@, left as int, j as int, right as int));\n assert forall |i: int, k: int|\n Self::is_mountain_triplet(nums@, i, j as int, k)\n implies candidate as int <= Self::triplet_sum(nums@, i, j as int, k) by {\n assert(0 <= i < j as int);\n assert((j as int) < k);\n assert(k < n as int);\n assert(nums[left as int] <= nums[i]);\n assert(nums[right as int] <= nums[k]);\n };\n }\n if candidate < best_sum {\n best_sum = candidate;\n proof {\n best_i = left as int;\n best_j = j as int;\n best_k = right as int;\n }\n }\n proof {\n assert(best_sum < 300000001);\n assert forall |i: int, c: int, k: int|\n 1 <= c < j as int + 1 && Self::is_mountain_triplet(nums@, i, c, k)\n implies best_sum as int <= Self::triplet_sum(nums@, i, c, k) by {\n if c < j as int {\n assert(old_best < 300000001);\n assert(old_best as int <= Self::triplet_sum(nums@, i, c, k));\n assert(best_sum <= old_best);\n } else {\n assert(c == j as int);\n assert(candidate as int <= Self::triplet_sum(nums@, i, c, k));\n if best_sum == old_best {\n assert(old_best as int <= candidate as int);\n } else {\n assert(best_sum == candidate);\n }\n }\n };\n }\n } else {\n proof {\n assert forall |i: int, k: int| !Self::is_mountain_triplet(nums@, i, j as int, k) by {\n if Self::is_mountain_triplet(nums@, i, j as int, k) {\n if nums[left as int] >= nums[j as int] {\n assert(0 <= i < j as int);\n assert(nums[left as int] <= nums[i]);\n assert(nums[i] < nums[j as int]);\n assert(false);\n } else {\n assert(nums[right as int] >= nums[j as int]);\n assert((j as int) < k);\n assert(k < n as int);\n assert(nums[right as int] <= nums[k]);\n assert(nums[k] < nums[j as int]);\n assert(false);\n }\n }\n };\n }\n }\n\n proof {\n if best_sum == 300000001 {\n assert forall |i: int, c: int, k: int|\n 1 <= c < j as int + 1 implies !Self::is_mountain_triplet(nums@, i, c, k) by {\n if c < j as int {\n assert(!Self::is_mountain_triplet(nums@, i, c, k));\n } else {\n assert(j as int <= c);\n assert(c < j as int + 1);\n assert(c == j as int);\n assert(!Self::is_mountain_triplet(nums@, i, c, k));\n }\n };\n } else {\n assert(Self::is_mountain_triplet(nums@, best_i, best_j, best_k));\n assert(1 <= best_j <= j as int);\n assert(best_sum as int == Self::triplet_sum(nums@, best_i, best_j, best_k));\n assert forall |i: int, c: int, k: int|\n 1 <= c < j as int + 1 && Self::is_mountain_triplet(nums@, i, c, k)\n implies best_sum as int <= Self::triplet_sum(nums@, i, c, k) by {\n if c < j as int {\n if old_best < 300000001 {\n assert(old_best as int <= Self::triplet_sum(nums@, i, c, k));\n assert(best_sum <= old_best);\n }\n }\n };\n }\n }\n\n j += 1;\n }\n\n if best_sum == 300000001 {\n proof {\n assert forall |i: int, c: int, k: int|\n !Self::is_mountain_triplet(nums@, i, c, k) by {\n if Self::is_mountain_triplet(nums@, i, c, k) {\n assert(1 <= c < n as int - 1);\n assert(c < j as int);\n }\n };\n }\n -1\n } else {\n proof {\n assert(exists |i: int, c: int, k: int|\n Self::is_mountain_triplet(nums@, i, c, k)\n && best_sum as int == Self::triplet_sum(nums@, i, c, k));\n assert forall |i: int, c: int, k: int|\n Self::is_mountain_triplet(nums@, i, c, k)\n implies best_sum as int <= Self::triplet_sum(nums@, i, c, k) by {\n assert(1 <= c < n as int - 1);\n assert(c < j as int);\n };\n }\n best_sum\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2909/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2909/tests/mutated_testcases.jsonl"} {"problem": "lc2913", "family": "leetcode", "description": "# Subarrays Distinct Element Sum of Squares I\n\nYou are given a **0-indexed **integer array `nums`.\n\nThe **distinct count** of a subarray of `nums` is defined as:\n\n\t- Let `nums[i..j]` be a subarray of `nums` consisting of all the indices from `i` to `j` such that `0 <= i <= j < nums.length`. Then the number of distinct values in `nums[i..j]` is called the distinct count of `nums[i..j]`.\n\nReturn *the sum of the **squares** of **distinct counts** of all subarrays of *`nums`.\n\nA subarray is a contiguous **non-empty** sequence of elements within an array.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [1,2,1]\n**Output:** 15\n**Explanation:** Six possible subarrays are:\n[1]: 1 distinct value\n[2]: 1 distinct value\n[1]: 1 distinct value\n[1,2]: 2 distinct values\n[2,1]: 2 distinct values\n[1,2,1]: 2 distinct values\nThe sum of the squares of the distinct counts in all subarrays is equal to 1^2 + 1^2 + 1^2 + 2^2 + 2^2 + 2^2 = 15.\n```\n\nExample 2:\n\n```\n**Input:** nums = [1,1]\n**Output:** 3\n**Explanation:** Three possible subarrays are:\n[1]: 1 distinct value\n[1]: 1 distinct value\n[1,1]: 1 distinct value\nThe sum of the squares of the distinct counts in all subarrays is equal to 1^2 + 1^2 + 1^2 = 3.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 100`\n\n\t- `1 <= nums[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn sum_counts(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table\nAcceptance rate: 80.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains_in_range(nums: Seq, start: int, end: int, value: int) -> bool {\n exists |k: int| start <= k < end && #[trigger] nums[k] as int == value\n }\n\n pub open spec fn distinct_count(nums: Seq, start: int, end: int, value: int) -> int\n decreases value,\n {\n if value <= 0 {\n 0\n } else {\n Self::distinct_count(nums, start, end, value - 1)\n + if Self::contains_in_range(nums, start, end, value) { 1int } else { 0int }\n }\n }\n\n pub open spec fn subarray_score(nums: Seq, start: int, end: int) -> int {\n let d = Self::distinct_count(nums, start, end, 100);\n d * d\n }\n\n pub open spec fn sum_end_until(nums: Seq, start: int, end: int) -> int\n decreases end - start,\n {\n if end <= start {\n 0\n } else {\n Self::sum_end_until(nums, start, end - 1) + Self::subarray_score(nums, start, end)\n }\n }\n\n pub open spec fn sum_starts_prefix(nums: Seq, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::sum_starts_prefix(nums, upto - 1) + Self::sum_end_until(nums, upto - 1, nums.len() as int)\n }\n }\n\n pub fn sum_counts(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::sum_starts_prefix(nums@, nums.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn contains_range(nums: &Vec, start: usize, end: usize, value: i32) -> bool {\n let mut k: usize = start;\n let mut found = false;\n while k < end {\n if nums[k] == value {\n found = true;\n }\n k = k + 1;\n }\n found\n }\n\n fn distinct_count_range(nums: &Vec, start: usize, end: usize) -> i32 {\n let mut value: i32 = 1;\n let mut count: i32 = 0;\n while value <= 100 {\n if Self::contains_range(nums, start, end, value) {\n count = count + 1;\n }\n value = value + 1;\n }\n count\n }\n\n pub fn sum_counts(nums: Vec) -> i32 {\n let n: usize = nums.len();\n let mut i: usize = 0;\n let mut total: i32 = 0;\n while i < n {\n let mut end: usize = i + 1;\n let mut row_sum: i32 = 0;\n while end <= n {\n let d = Self::distinct_count_range(&nums, i, end);\n row_sum = row_sum + d * d;\n end = end + 1;\n }\n total = total + row_sum;\n i = i + 1;\n }\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains_in_range(nums: Seq, start: int, end: int, value: int) -> bool {\n exists |k: int| start <= k < end && #[trigger] nums[k] as int == value\n }\n\n pub open spec fn distinct_count(nums: Seq, start: int, end: int, value: int) -> int\n decreases value,\n {\n if value <= 0 {\n 0\n } else {\n Self::distinct_count(nums, start, end, value - 1)\n + if Self::contains_in_range(nums, start, end, value) { 1int } else { 0int }\n }\n }\n\n pub open spec fn subarray_score(nums: Seq, start: int, end: int) -> int {\n let d = Self::distinct_count(nums, start, end, 100);\n d * d\n }\n\n pub open spec fn sum_end_until(nums: Seq, start: int, end: int) -> int\n decreases end - start,\n {\n if end <= start {\n 0\n } else {\n Self::sum_end_until(nums, start, end - 1) + Self::subarray_score(nums, start, end)\n }\n }\n\n pub open spec fn sum_starts_prefix(nums: Seq, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::sum_starts_prefix(nums, upto - 1) + Self::sum_end_until(nums, upto - 1, nums.len() as int)\n }\n }\n\n fn contains_range(nums: &Vec, start: usize, end: usize, value: i32) -> (found: bool)\n requires\n start <= end <= nums.len(),\n ensures\n found == Self::contains_in_range(nums@, start as int, end as int, value as int),\n {\n let mut k: usize = start;\n let mut found = false;\n while k < end {\n if nums[k] == value {\n found = true;\n }\n k = k + 1;\n }\n found\n }\n\n fn distinct_count_range(nums: &Vec, start: usize, end: usize) -> (count: i32)\n requires\n start < end <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n count as int == Self::distinct_count(nums@, start as int, end as int, 100),\n 0 <= count <= 100,\n {\n let mut value: i32 = 1;\n let mut count: i32 = 0;\n while value <= 100 {\n if Self::contains_range(nums, start, end, value) {\n count = count + 1;\n }\n value = value + 1;\n }\n count\n }\n\n pub fn sum_counts(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::sum_starts_prefix(nums@, nums.len() as int),\n {\n let n: usize = nums.len();\n let mut i: usize = 0;\n let mut total: i32 = 0;\n while i < n {\n let mut end: usize = i + 1;\n let mut row_sum: i32 = 0;\n while end <= n {\n let d = Self::distinct_count_range(&nums, i, end);\n row_sum = row_sum + d * d;\n end = end + 1;\n }\n total = total + row_sum;\n i = i + 1;\n }\n total\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains_in_range(nums: Seq, start: int, end: int, value: int) -> bool {\n exists |k: int| start <= k < end && #[trigger] nums[k] as int == value\n }\n\n pub open spec fn distinct_count(nums: Seq, start: int, end: int, value: int) -> int\n decreases value,\n {\n if value <= 0 {\n 0\n } else {\n Self::distinct_count(nums, start, end, value - 1)\n + if Self::contains_in_range(nums, start, end, value) { 1int } else { 0int }\n }\n }\n\n pub open spec fn subarray_score(nums: Seq, start: int, end: int) -> int {\n let d = Self::distinct_count(nums, start, end, 100);\n d * d\n }\n\n pub open spec fn sum_end_until(nums: Seq, start: int, end: int) -> int\n decreases end - start,\n {\n if end <= start {\n 0\n } else {\n Self::sum_end_until(nums, start, end - 1) + Self::subarray_score(nums, start, end)\n }\n }\n\n pub open spec fn sum_starts_prefix(nums: Seq, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::sum_starts_prefix(nums, upto - 1) + Self::sum_end_until(nums, upto - 1, nums.len() as int)\n }\n }\n\n fn contains_range(nums: &Vec, start: usize, end: usize, value: i32) -> (found: bool)\n requires\n start <= end <= nums.len(),\n ensures\n found == Self::contains_in_range(nums@, start as int, end as int, value as int),\n {\n let mut k: usize = start;\n let mut found = false;\n while k < end\n invariant\n start <= k <= end,\n end <= nums.len(),\n found == (exists |j: int| start as int <= j < k as int && #[trigger] nums[j] as int == value as int),\n decreases end - k,\n {\n if nums[k] == value {\n found = true;\n }\n k = k + 1;\n }\n found\n }\n\n fn distinct_count_range(nums: &Vec, start: usize, end: usize) -> (count: i32)\n requires\n start < end <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n count as int == Self::distinct_count(nums@, start as int, end as int, 100),\n 0 <= count <= 100,\n {\n let mut value: i32 = 1;\n let mut count: i32 = 0;\n while value <= 100\n invariant\n 1 <= value <= 101,\n count as int == Self::distinct_count(nums@, start as int, end as int, value as int - 1),\n 0 <= count <= value - 1,\n start < end <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n decreases 101 - value,\n {\n if Self::contains_range(nums, start, end, value) {\n count = count + 1;\n }\n value = value + 1;\n }\n count\n }\n\n pub fn sum_counts(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result as int == Self::sum_starts_prefix(nums@, nums.len() as int),\n {\n let n: usize = nums.len();\n let mut i: usize = 0;\n let mut total: i32 = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 1 <= n <= 100,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n total as int == Self::sum_starts_prefix(nums@, i as int),\n 0 <= total as int <= 1000000 * i as int,\n decreases n - i,\n {\n let mut end: usize = i + 1;\n let mut row_sum: i32 = 0;\n while end <= n\n invariant\n i + 1 <= end <= n + 1,\n n == nums.len(),\n 1 <= n <= 100,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n row_sum as int == Self::sum_end_until(nums@, i as int, end as int - 1),\n 0 <= row_sum as int,\n row_sum as int <= 10000 * (end as int - i as int - 1),\n decreases n + 1 - end,\n {\n let d = Self::distinct_count_range(&nums, i, end);\n proof {\n assert(d as int == Self::distinct_count(nums@, i as int, end as int, 100));\n assert(0 <= d <= 100);\n assert((d * d) as int == Self::subarray_score(nums@, i as int, end as int));\n assert(Self::sum_end_until(nums@, i as int, end as int)\n == Self::sum_end_until(nums@, i as int, end as int - 1)\n + Self::subarray_score(nums@, i as int, end as int));\n assert(d * d <= 10000) by (nonlinear_arith)\n requires\n 0 <= d <= 100;\n assert(end as int - i as int - 1 >= 0);\n }\n row_sum = row_sum + d * d;\n end = end + 1;\n }\n proof {\n assert(n as int - i as int >= 1);\n assert(row_sum as int <= 10000 * (n as int - i as int));\n assert(10000 * (n as int - i as int) <= 1000000) by (nonlinear_arith)\n requires\n 1 <= n <= 100,\n i < n;\n assert(total as int + row_sum as int <= 1000000 * ((i as int) + 1));\n assert(1000000 * ((i as int) + 1) <= 100000000) by (nonlinear_arith)\n requires\n 0 <= i < n,\n 1 <= n <= 100;\n }\n total = total + row_sum;\n i = i + 1;\n }\n total\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2913/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2913/tests/mutated_testcases.jsonl"} {"problem": "lc2917", "family": "leetcode", "description": "# Find the K-or of an Array\n\nYou are given an integer array `nums`, and an integer `k`. Let's introduce **K-or** operation by extending the standard bitwise OR. In K-or, a bit position in the result is set to `1` if at least `k` numbers in `nums` have a `1` in that position.\n\nReturn *the K-or of* `nums`.\n\n \n\nExample 1: \n\n**Input:** nums = [7,12,9,8,9,15], k = 4 \n\n**Output:** 9 \n\n**Explanation: **\n\nRepresent numbers in binary:\n\n\t\n\t\t\n\t\t\t**Number**\n\t\t\tBit 3\n\t\t\tBit 2\n\t\t\tBit 1\n\t\t\tBit 0\n\t\t\n\t\t\n\t\t\t**7**\n\t\t\t0\n\t\t\t1\n\t\t\t1\n\t\t\t1\n\t\t\n\t\t\n\t\t\t**12**\n\t\t\t1\n\t\t\t1\n\t\t\t0\n\t\t\t0\n\t\t\n\t\t\n\t\t\t**9**\n\t\t\t1\n\t\t\t0\n\t\t\t0\n\t\t\t1\n\t\t\n\t\t\n\t\t\t**8**\n\t\t\t1\n\t\t\t0\n\t\t\t0\n\t\t\t0\n\t\t\n\t\t\n\t\t\t**9**\n\t\t\t1\n\t\t\t0\n\t\t\t0\n\t\t\t1\n\t\t\n\t\t\n\t\t\t**15**\n\t\t\t1\n\t\t\t1\n\t\t\t1\n\t\t\t1\n\t\t\n\t\t\n\t\t\t**Result = 9**\n\t\t\t1\n\t\t\t0\n\t\t\t0\n\t\t\t1\n\t\t\n\t\n\nBit 0 is set in 7, 9, 9, and 15. Bit 3 is set in 12, 9, 8, 9, and 15.\n\nOnly bits 0 and 3 qualify. The result is `(1001)_2 = 9`.\n\nExample 2: \n\n**Input:** nums = [2,12,1,11,4,5], k = 6 \n\n**Output:** 0 \n\n**Explanation: **No bit appears as 1 in all six array numbers, as required for K-or with `k = 6`. Thus, the result is 0.\n\nExample 3: \n\n**Input:** nums = [10,8,5,9,11,6,8], k = 1 \n\n**Output:** 15 \n\n**Explanation: ** Since `k == 1`, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is `10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15`.\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 50`\n\n\t- `0 <= nums[i] < 2^31`\n\n\t- `1 <= k <= nums.length`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_k_or(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Bit Manipulation\nAcceptance rate: 72.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn bit_set_spec(x: i32, bit: int) -> bool\n recommends\n 0 <= bit < 31,\n {\n ((x >> (bit as u32)) & 1) == 1\n }\n\n pub open spec fn count_bit_spec(nums: Seq, bit: int, idx: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let add: int = if Self::bit_set_spec(nums[idx], bit) { 1int } else { 0int };\n add + Self::count_bit_spec(nums, bit, idx + 1)\n }\n }\n\n pub open spec fn find_k_or_from_spec(nums: Seq, k: int, bit: int) -> i32\n decreases 31 - bit,\n {\n if bit >= 31 {\n 0\n } else {\n let add: i32 = if Self::count_bit_spec(nums, bit, 0) >= k {\n 1i32 << (bit as u32)\n } else {\n 0\n };\n add | Self::find_k_or_from_spec(nums, k, bit + 1)\n }\n }\n\n pub open spec fn find_k_or_spec(nums: Seq, k: int) -> i32 {\n Self::find_k_or_from_spec(nums, k, 0)\n }\n\n pub fn find_k_or(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n result == Self::find_k_or_spec(nums@, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn bit_set_exec(x: i32, bit: usize) -> bool {\n ((x >> (bit as u32)) & 1) == 1\n }\n\n fn count_bit_exec(nums: &Vec, bit: usize, idx: usize) -> i32 {\n if idx >= nums.len() {\n 0\n } else {\n let add: i32 = if Self::bit_set_exec(nums[idx], bit) { 1 } else { 0 };\n let tail: i32 = Self::count_bit_exec(nums, bit, idx + 1);\n add + tail\n }\n }\n\n fn find_k_or_from_exec(nums: &Vec, k: i32, bit: usize) -> i32 {\n if bit >= 31 {\n 0\n } else {\n let add: i32 = if Self::count_bit_exec(nums, bit, 0) >= k {\n 1i32 << (bit as u32)\n } else {\n 0\n };\n add | Self::find_k_or_from_exec(nums, k, bit + 1)\n }\n }\n\n pub fn find_k_or(nums: Vec, k: i32) -> i32 {\n Self::find_k_or_from_exec(&nums, k, 0)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn bit_set_spec(x: i32, bit: int) -> bool\n recommends\n 0 <= bit < 31,\n {\n ((x >> (bit as u32)) & 1) == 1\n }\n\n pub open spec fn count_bit_spec(nums: Seq, bit: int, idx: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let add: int = if Self::bit_set_spec(nums[idx], bit) { 1int } else { 0int };\n add + Self::count_bit_spec(nums, bit, idx + 1)\n }\n }\n\n pub open spec fn find_k_or_from_spec(nums: Seq, k: int, bit: int) -> i32\n decreases 31 - bit,\n {\n if bit >= 31 {\n 0\n } else {\n let add: i32 = if Self::count_bit_spec(nums, bit, 0) >= k {\n 1i32 << (bit as u32)\n } else {\n 0\n };\n add | Self::find_k_or_from_spec(nums, k, bit + 1)\n }\n }\n\n pub open spec fn find_k_or_spec(nums: Seq, k: int) -> i32 {\n Self::find_k_or_from_spec(nums, k, 0)\n }\n\n fn bit_set_exec(x: i32, bit: usize) -> (res: bool)\n requires\n 0 <= x < 2_147_483_648,\n bit <= 30,\n ensures\n res == Self::bit_set_spec(x, bit as int),\n {\n ((x >> (bit as u32)) & 1) == 1\n }\n\n fn count_bit_exec(nums: &Vec, bit: usize, idx: usize) -> (res: i32)\n requires\n nums.len() <= 50,\n bit <= 30,\n idx <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n res as int == Self::count_bit_spec(nums@, bit as int, idx as int),\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let add: i32 = if Self::bit_set_exec(nums[idx], bit) { 1 } else { 0 };\n let tail: i32 = Self::count_bit_exec(nums, bit, idx + 1);\n add + tail\n }\n }\n\n fn find_k_or_from_exec(nums: &Vec, k: i32, bit: usize) -> (res: i32)\n requires\n nums.len() <= 50,\n bit <= 31,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n res == Self::find_k_or_from_spec(nums@, k as int, bit as int),\n decreases 31 - bit,\n {\n if bit >= 31 {\n 0\n } else {\n let add: i32 = if Self::count_bit_exec(nums, bit, 0) >= k {\n 1i32 << (bit as u32)\n } else {\n 0\n };\n add | Self::find_k_or_from_exec(nums, k, bit + 1)\n }\n }\n\n pub fn find_k_or(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n result == Self::find_k_or_spec(nums@, k as int),\n {\n Self::find_k_or_from_exec(&nums, k, 0)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn bit_set_spec(x: i32, bit: int) -> bool\n recommends\n 0 <= bit < 31,\n {\n ((x >> (bit as u32)) & 1) == 1\n }\n\n pub open spec fn count_bit_spec(nums: Seq, bit: int, idx: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let add: int = if Self::bit_set_spec(nums[idx], bit) { 1int } else { 0int };\n add + Self::count_bit_spec(nums, bit, idx + 1)\n }\n }\n\n proof fn lemma_count_bit_bound(nums: Seq, bit: int, idx: int)\n requires\n idx <= nums.len(),\n ensures\n 0 <= Self::count_bit_spec(nums, bit, idx) <= nums.len() - idx,\n decreases nums.len() - idx,\n {\n if idx < nums.len() {\n Self::lemma_count_bit_bound(nums, bit, idx + 1);\n }\n }\n\n pub open spec fn find_k_or_from_spec(nums: Seq, k: int, bit: int) -> i32\n decreases 31 - bit,\n {\n if bit >= 31 {\n 0\n } else {\n let add: i32 = if Self::count_bit_spec(nums, bit, 0) >= k {\n 1i32 << (bit as u32)\n } else {\n 0\n };\n add | Self::find_k_or_from_spec(nums, k, bit + 1)\n }\n }\n\n pub open spec fn find_k_or_spec(nums: Seq, k: int) -> i32 {\n Self::find_k_or_from_spec(nums, k, 0)\n }\n\n fn bit_set_exec(x: i32, bit: usize) -> (res: bool)\n requires\n 0 <= x < 2_147_483_648,\n bit <= 30,\n ensures\n res == Self::bit_set_spec(x, bit as int),\n {\n ((x >> (bit as u32)) & 1) == 1\n }\n\n fn count_bit_exec(nums: &Vec, bit: usize, idx: usize) -> (res: i32)\n requires\n nums.len() <= 50,\n bit <= 30,\n idx <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n res as int == Self::count_bit_spec(nums@, bit as int, idx as int),\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n 0\n } else {\n let add: i32 = if Self::bit_set_exec(nums[idx], bit) { 1 } else { 0 };\n let tail: i32 = Self::count_bit_exec(nums, bit, idx + 1);\n proof {\n Self::lemma_count_bit_bound(nums@, bit as int, idx as int + 1);\n assert(add == 0 || add == 1);\n assert(0 <= tail as int <= nums.len() as int);\n assert(add as int + tail as int <= nums.len() as int + 1);\n assert(nums.len() <= 50);\n assert(add as int + tail as int <= 51);\n }\n add + tail\n }\n }\n\n fn find_k_or_from_exec(nums: &Vec, k: i32, bit: usize) -> (res: i32)\n requires\n nums.len() <= 50,\n bit <= 31,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n res == Self::find_k_or_from_spec(nums@, k as int, bit as int),\n decreases 31 - bit,\n {\n if bit >= 31 {\n 0\n } else {\n let add: i32 = if Self::count_bit_exec(nums, bit, 0) >= k {\n 1i32 << (bit as u32)\n } else {\n 0\n };\n add | Self::find_k_or_from_exec(nums, k, bit + 1)\n }\n }\n\n pub fn find_k_or(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 2_147_483_648,\n ensures\n result == Self::find_k_or_spec(nums@, k as int),\n {\n Self::find_k_or_from_exec(&nums, k, 0)\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2917/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2917/tests/mutated_testcases.jsonl"} {"problem": "lc2928", "family": "leetcode", "description": "# Distribute Candies Among Children I\n\nYou are given two positive integers `n` and `limit`.\n\nReturn *the **total number** of ways to distribute *`n` *candies among *`3`* children such that no child gets more than *`limit`* candies.*\n\n \n\nExample 1:\n\n```\n**Input:** n = 5, limit = 2\n**Output:** 3\n**Explanation:** There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).\n```\n\nExample 2:\n\n```\n**Input:** n = 3, limit = 3\n**Output:** 10\n**Explanation:** There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n <= 50`\n\n\t- `1 <= limit <= 50`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn distribute_candies(n: i32, limit: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Combinatorics, Enumeration\nAcceptance rate: 76.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_b(n: nat, limit: nat, a: nat, b: nat) -> bool {\n a <= limit && b <= limit && a + b <= n && n - a - b <= limit\n }\n\n pub open spec fn count_b_prefix(n: nat, limit: nat, a: nat, kb: nat) -> nat\n decreases kb,\n {\n if kb == 0 {\n 0\n } else {\n Self::count_b_prefix(n, limit, a, (kb - 1) as nat)\n + if Self::valid_b(n, limit, a, (kb - 1) as nat) { 1nat } else { 0nat }\n }\n }\n\n pub open spec fn count_a_prefix(n: nat, limit: nat, ka: nat) -> nat\n decreases ka,\n {\n if ka == 0 {\n 0\n } else {\n Self::count_a_prefix(n, limit, (ka - 1) as nat)\n + Self::count_b_prefix(n, limit, (ka - 1) as nat, (limit + 1) as nat)\n }\n }\n\n pub open spec fn total_ways(n: nat, limit: nat) -> nat {\n Self::count_a_prefix(n, limit, (limit + 1) as nat)\n }\n\n pub fn distribute_candies(n: i32, limit: i32) -> (result: i32)\n requires\n 1 <= n <= 50,\n 1 <= limit <= 50,\n ensures\n result as nat == Self::total_ways(n as nat, limit as nat),\n 0 <= result <= 2601,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn distribute_candies(n: i32, limit: i32) -> i32 {\n let mut a: i32 = 0;\n let mut total: i32 = 0;\n while a <= limit {\n let mut b: i32 = 0;\n while b <= limit {\n let c = n - a - b;\n if 0 <= c && c <= limit {\n total = total + 1;\n }\n b = b + 1;\n }\n a = a + 1;\n }\n total\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_b(n: nat, limit: nat, a: nat, b: nat) -> bool {\n a <= limit && b <= limit && a + b <= n && n - a - b <= limit\n }\n\n pub open spec fn count_b_prefix(n: nat, limit: nat, a: nat, kb: nat) -> nat\n decreases kb,\n {\n if kb == 0 {\n 0\n } else {\n Self::count_b_prefix(n, limit, a, (kb - 1) as nat)\n + if Self::valid_b(n, limit, a, (kb - 1) as nat) { 1nat } else { 0nat }\n }\n }\n\n pub open spec fn count_a_prefix(n: nat, limit: nat, ka: nat) -> nat\n decreases ka,\n {\n if ka == 0 {\n 0\n } else {\n Self::count_a_prefix(n, limit, (ka - 1) as nat)\n + Self::count_b_prefix(n, limit, (ka - 1) as nat, (limit + 1) as nat)\n }\n }\n\n pub open spec fn total_ways(n: nat, limit: nat) -> nat {\n Self::count_a_prefix(n, limit, (limit + 1) as nat)\n }\n\n pub fn distribute_candies(n: i32, limit: i32) -> (result: i32)\n requires\n 1 <= n <= 50,\n 1 <= limit <= 50,\n ensures\n result as nat == Self::total_ways(n as nat, limit as nat),\n 0 <= result <= 2601,\n {\n let mut a: i32 = 0;\n let mut total: i32 = 0;\n while a <= limit {\n let mut b: i32 = 0;\n while b <= limit {\n let c = n - a - b;\n if 0 <= c && c <= limit {\n total = total + 1;\n }\n b = b + 1;\n }\n a = a + 1;\n }\n total\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_b(n: nat, limit: nat, a: nat, b: nat) -> bool {\n a <= limit && b <= limit && a + b <= n && n - a - b <= limit\n }\n\n pub open spec fn count_b_prefix(n: nat, limit: nat, a: nat, kb: nat) -> nat\n decreases kb,\n {\n if kb == 0 {\n 0\n } else {\n Self::count_b_prefix(n, limit, a, (kb - 1) as nat)\n + if Self::valid_b(n, limit, a, (kb - 1) as nat) { 1nat } else { 0nat }\n }\n }\n\n pub open spec fn count_a_prefix(n: nat, limit: nat, ka: nat) -> nat\n decreases ka,\n {\n if ka == 0 {\n 0\n } else {\n Self::count_a_prefix(n, limit, (ka - 1) as nat)\n + Self::count_b_prefix(n, limit, (ka - 1) as nat, (limit + 1) as nat)\n }\n }\n\n pub open spec fn total_ways(n: nat, limit: nat) -> nat {\n Self::count_a_prefix(n, limit, (limit + 1) as nat)\n }\n\n pub fn distribute_candies(n: i32, limit: i32) -> (result: i32)\n requires\n 1 <= n <= 50,\n 1 <= limit <= 50,\n ensures\n result as nat == Self::total_ways(n as nat, limit as nat),\n 0 <= result <= 2601,\n {\n let mut a: i32 = 0;\n let mut total: i32 = 0;\n while a <= limit\n invariant\n 0 <= a <= limit + 1,\n 0 <= total <= 2601,\n total <= a * (limit + 1),\n total as nat == Self::count_a_prefix(n as nat, limit as nat, a as nat),\n 1 <= n <= 50,\n 1 <= limit <= 50,\n decreases limit - a + 1,\n {\n let mut b: i32 = 0;\n while b <= limit\n invariant\n 0 <= b <= limit + 1,\n 0 <= total <= 2601,\n total <= a * (limit + 1) + b,\n total as nat\n == Self::count_a_prefix(n as nat, limit as nat, a as nat)\n + Self::count_b_prefix(n as nat, limit as nat, a as nat, b as nat),\n 0 <= a <= limit,\n 1 <= n <= 50,\n 1 <= limit <= 50,\n decreases limit - b + 1,\n {\n let c = n - a - b;\n if 0 <= c && c <= limit {\n assert(a <= limit);\n assert(b <= limit);\n proof {\n assert(a * (limit + 1) + b <= 2600) by (nonlinear_arith)\n requires\n 0 <= a,\n a <= limit,\n 0 <= b,\n b <= limit,\n 1 <= limit,\n limit <= 50;\n }\n assert(total <= a * (limit + 1) + b);\n assert(total <= 2600);\n total = total + 1;\n }\n assert(b + 1 <= limit + 1);\n b = b + 1;\n proof {\n assert(Self::count_b_prefix(n as nat, limit as nat, a as nat, b as nat)\n == Self::count_b_prefix(n as nat, limit as nat, a as nat, ((b - 1) as nat))\n + if Self::valid_b(n as nat, limit as nat, a as nat, ((b - 1) as nat)) { 1nat } else { 0nat });\n }\n }\n\n assert(b == limit + 1);\n assert(total <= a * (limit + 1) + (limit + 1));\n proof {\n assert(total <= (a + 1) * (limit + 1)) by (nonlinear_arith)\n requires\n total <= a * (limit + 1) + (limit + 1),\n 0 <= a,\n 1 <= limit;\n }\n a = a + 1;\n proof {\n assert(Self::count_a_prefix(n as nat, limit as nat, a as nat)\n == Self::count_a_prefix(n as nat, limit as nat, ((a - 1) as nat))\n + Self::count_b_prefix(n as nat, limit as nat, ((a - 1) as nat), (limit + 1) as nat));\n }\n }\n\n total\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2928/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2928/tests/mutated_testcases.jsonl"} {"problem": "lc2932", "family": "leetcode", "description": "# Maximum Strong Pair XOR I\n\nYou are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition:\n\n- `|x - y| <= min(x, y)`\n\nYou need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array.\n\n*Return the **maximum** `XOR` value out of all possible strong pairs in the array `nums`.*\n\n**Note** that you can pick the same integer twice to form a pair.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,4,5]\n> **Output:** 7\n> **Explanation:** There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).\n> The maximum XOR possible from these pairs is 3 XOR 4 = 7.\n\n## Example 2:\n\n> **Input:** nums = [10,100]\n> **Output:** 0\n> **Explanation:** There are 2 strong pairs in the array nums: (10, 10) and (100, 100).\n> The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.\n\n## Example 3:\n\n> **Input:** nums = [5,6,25,30]\n> **Output:** 7\n> **Explanation:** There are 6 strong pairs in the array nums: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).\n> The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.\n\n## Constraints:\n\n- $1 <= nums.length <= 50$\n\n- $1 <= nums[i] <= 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn maximum_strong_pair_xor(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Bit Manipulation, Trie, Sliding Window, Weekly Contest 371\nAcceptance rate: 75.8%\n", "spec": "use vstd::prelude::*;\nuse vstd::math::{max as spec_max, min as spec_min, abs as spec_abs};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn maximum_strong_pair_xor(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100, \n ensures \n forall |i: int, j: int| \n 0 <= i < nums.len() && 0 <= j < nums.len() &&\n spec_abs((nums[i] - nums[j]) as int) <= spec_min(nums[i] as int, nums[j] as int)\n ==> (nums[i] ^ nums[j]) <= res,\n exists |i: int, j: int| \n 0 <= i < nums.len() && 0 <= j < nums.len() &&\n spec_abs((nums[i] - nums[j]) as int) <= spec_min(nums[i] as int, nums[j] as int) &&\n (nums[i] ^ nums[j]) == res,\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn abs(x: i32) -> i32\n {\n if x < 0 { -x } else { x }\n }\n\n pub fn max(x: i32, y: i32) -> i32\n {\n if x >= y { x } else { y }\n }\n\n pub fn min(x: i32, y: i32) -> i32\n {\n if x <= y { x } else { y }\n }\n\n pub fn maximum_strong_pair_xor(nums: Vec) -> i32\n {\n let mut max_xor = 0;\n for i in 0..nums.len() \n {\n for j in i..nums.len() \n {\n if Self::abs(nums[i] - nums[j]) <= Self::min(nums[i], nums[j]) {\n let current_xor = nums[i] ^ nums[j];\n max_xor = Self::max(max_xor, current_xor);\n }\n }\n }\n \n max_xor\n }\n}\n", "code_spec": "use vstd::prelude::*;\nuse vstd::math::{max as spec_max, min as spec_min, abs as spec_abs};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn abs(x: i32) -> (res: i32)\n requires \n x > i32::MIN, \n ensures \n (res as int) == spec_abs(x as int), \n {\n if x < 0 { -x } else { x }\n }\n\n pub fn max(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_max(x as int, y as int)\n {\n if x >= y { x } else { y }\n }\n\n pub fn min(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_min(x as int, y as int)\n {\n if x <= y { x } else { y }\n }\n\n pub fn maximum_strong_pair_xor(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100, \n ensures \n forall |i: int, j: int| \n 0 <= i < nums.len() && 0 <= j < nums.len() &&\n spec_abs((nums[i] - nums[j]) as int) <= spec_min(nums[i] as int, nums[j] as int)\n ==> (nums[i] ^ nums[j]) <= res,\n exists |i: int, j: int| \n 0 <= i < nums.len() && 0 <= j < nums.len() &&\n spec_abs((nums[i] - nums[j]) as int) <= spec_min(nums[i] as int, nums[j] as int) &&\n (nums[i] ^ nums[j]) == res,\n {\n let mut max_xor = 0;\n \n for i in 0..nums.len() \n {\n for j in i..nums.len() \n {\n if Self::abs(nums[i] - nums[j]) <= Self::min(nums[i], nums[j]) {\n let current_xor = nums[i] ^ nums[j];\n max_xor = Self::max(max_xor, current_xor);\n }\n }\n }\n \n max_xor\n }\n}\n\n}", "verified": "use vstd::prelude::*;\nuse vstd::math::{max as spec_max, min as spec_min, abs as spec_abs};\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nproof fn xor_comm(a: i32, b: i32)\n ensures \n a ^ b == b ^ a, \n{\n assert(a ^ b == b ^ a) by(bit_vector);\n}\n\nproof fn xor_self_zero(a: i32)\n ensures \n a ^ a == 0, \n{\n assert(a ^ a == 0) by(bit_vector);\n}\n\nimpl Solution {\n pub fn abs(x: i32) -> (res: i32)\n requires \n x > i32::MIN, \n ensures \n (res as int) == spec_abs(x as int), \n {\n if x < 0 { -x } else { x }\n }\n\n pub fn max(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_max(x as int, y as int)\n {\n if x >= y { x } else { y }\n }\n\n pub fn min(x: i32, y: i32) -> (res: i32)\n ensures (res as int) == spec_min(x as int, y as int)\n {\n if x <= y { x } else { y }\n }\n\n pub fn maximum_strong_pair_xor(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100, \n ensures \n forall |i: int, j: int| \n 0 <= i < nums.len() && 0 <= j < nums.len() &&\n spec_abs((nums[i] - nums[j]) as int) <= spec_min(nums[i] as int, nums[j] as int)\n ==> (nums[i] ^ nums[j]) <= res,\n exists |i: int, j: int| \n 0 <= i < nums.len() && 0 <= j < nums.len() &&\n spec_abs((nums[i] - nums[j]) as int) <= spec_min(nums[i] as int, nums[j] as int) &&\n (nums[i] ^ nums[j]) == res,\n {\n let mut max_xor = 0;\n \n proof {\n assert(0 <= spec_min(nums[0int] as int, nums[0int] as int));\n xor_self_zero(nums[0]);\n }\n \n for i in 0..nums.len() \n invariant\n 1 <= nums.len() <= 50, \n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n max_xor >= 0,\n forall |ii: int, jj: int| \n 0 <= ii < i && 0 <= jj < nums.len() &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int)\n ==> (nums[ii] ^ nums[jj]) <= max_xor,\n exists |ii: int, jj: int| \n 0 <= ii < nums.len() && 0 <= jj < nums.len() &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int) &&\n (nums[ii] ^ nums[jj]) == max_xor,\n {\n for j in i..nums.len() \n invariant\n 1 <= nums.len() <= 50,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 100,\n 0 <= i < nums.len(),\n i <= j <= nums.len(),\n max_xor >= 0,\n forall |ii: int, jj: int| \n ((0 <= ii < i && 0 <= jj < nums.len()) ||\n (ii == i && i <= jj < j)) &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int)\n ==> (nums[ii] ^ nums[jj]) <= max_xor,\n exists |ii: int, jj: int| \n 0 <= ii < nums.len() && 0 <= jj < nums.len() &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int) &&\n (nums[ii] ^ nums[jj]) == max_xor,\n {\n if Self::abs(nums[i] - nums[j]) <= Self::min(nums[i], nums[j]) {\n let current_xor = nums[i] ^ nums[j];\n max_xor = Self::max(max_xor, current_xor);\n }\n }\n \n assert forall |ii: int, jj: int| \n 0 <= ii < i + 1 && 0 <= jj < nums.len() &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int)\n implies (nums[ii] ^ nums[jj]) <= max_xor\n by {\n if ii < i {\n assert((0 <= ii < i && 0 <= jj < nums.len()) &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int));\n } else {\n if jj >= i {\n assert((ii == i && i <= jj < nums.len()) &&\n spec_abs((nums[ii] - nums[jj]) as int) <= spec_min(nums[ii] as int, nums[jj] as int));\n } else {\n xor_comm(nums[ii], nums[jj]);\n assert((0 <= jj < i && 0 <= ii < nums.len()) &&\n spec_abs((nums[jj] - nums[ii]) as int) <= spec_min(nums[jj] as int, nums[ii] as int));\n }\n }\n };\n }\n \n max_xor\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc2932/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2932/tests/mutated_testcases.jsonl"} {"problem": "lc2938", "family": "leetcode", "description": "# Separate Black and White Balls\n\nThere are `n` balls on a table, each ball has a color black or white.\n\nYou are given a **0-indexed** binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively.\n\nIn each step, you can choose two adjacent balls and swap them.\n\nReturn *the **minimum** number of steps to group all the black balls to the right and all the white balls to the left*.\n\n \n\nExample 1:\n\n```\n**Input:** s = \"101\"\n**Output:** 1\n**Explanation:** We can group all the black balls to the right in the following way:\n- Swap s[0] and s[1], s = \"011\".\nInitially, 1s are not grouped together, requiring at least 1 step to group them to the right.\n```\n\nExample 2:\n\n```\n**Input:** s = \"100\"\n**Output:** 2\n**Explanation:** We can group all the black balls to the right in the following way:\n- Swap s[0] and s[1], s = \"010\".\n- Swap s[1] and s[2], s = \"001\".\nIt can be proven that the minimum number of steps needed is 2.\n```\n\nExample 3:\n\n```\n**Input:** s = \"0111\"\n**Output:** 0\n**Explanation:** All the black balls are already grouped to the right.\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= n == s.length <= 10^5`\n\n\t- `s[i]` is either `'0'` or `'1'`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_steps(s: String) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nTwo Pointers, String, Greedy\nAcceptance rate: 63.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ones_prefix(s: Seq, n: int) -> int\n recommends\n 0 <= n <= s.len(),\n forall |i: int| 0 <= i < s.len() ==> s[i] == '0' || s[i] == '1',\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::ones_prefix(s, n - 1) + if s[n - 1] == '1' { 1int } else { 0int }\n }\n }\n\n pub open spec fn inv_prefix(s: Seq, n: int) -> int\n recommends\n 0 <= n <= s.len(),\n forall |i: int| 0 <= i < s.len() ==> s[i] == '0' || s[i] == '1',\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::inv_prefix(s, n - 1)\n + if s[n - 1] == '0' { Self::ones_prefix(s, n - 1) } else { 0int }\n }\n }\n\n pub open spec fn answer_spec(s: Seq) -> int {\n Self::inv_prefix(s, s.len() as int)\n }\n\n pub fn minimum_steps(s: String) -> (result: i64)\n requires\n 1 <= s@.len() <= 100000,\n forall |i: int| 0 <= i < s@.len() ==> s@[i] == '0' || s@[i] == '1',\n ensures\n result as int == Self::answer_spec(s@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_steps(s: String) -> i64 {\n let len = s.as_str().unicode_len();\n let mut i: usize = 0;\n let mut ones: i64 = 0;\n let mut steps: i64 = 0;\n\n while i < len {\n let c = s.as_str().get_char(i);\n if c == '1' {\n ones = ones + 1;\n } else {\n steps = steps + ones;\n }\n i = i + 1;\n }\n\n steps\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ones_prefix(s: Seq, n: int) -> int\n recommends\n 0 <= n <= s.len(),\n forall |i: int| 0 <= i < s.len() ==> s[i] == '0' || s[i] == '1',\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::ones_prefix(s, n - 1) + if s[n - 1] == '1' { 1int } else { 0int }\n }\n }\n\n pub open spec fn inv_prefix(s: Seq, n: int) -> int\n recommends\n 0 <= n <= s.len(),\n forall |i: int| 0 <= i < s.len() ==> s[i] == '0' || s[i] == '1',\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::inv_prefix(s, n - 1)\n + if s[n - 1] == '0' { Self::ones_prefix(s, n - 1) } else { 0int }\n }\n }\n\n pub open spec fn answer_spec(s: Seq) -> int {\n Self::inv_prefix(s, s.len() as int)\n }\n\n pub fn minimum_steps(s: String) -> (result: i64)\n requires\n 1 <= s@.len() <= 100000,\n forall |i: int| 0 <= i < s@.len() ==> s@[i] == '0' || s@[i] == '1',\n ensures\n result as int == Self::answer_spec(s@),\n {\n let len = s.as_str().unicode_len();\n let mut i: usize = 0;\n let mut ones: i64 = 0;\n let mut steps: i64 = 0;\n\n while i < len {\n let c = s.as_str().get_char(i);\n if c == '1' {\n ones = ones + 1;\n } else {\n steps = steps + ones;\n }\n i = i + 1;\n }\n\n steps\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ones_prefix(s: Seq, n: int) -> int\n recommends\n 0 <= n <= s.len(),\n forall |i: int| 0 <= i < s.len() ==> s[i] == '0' || s[i] == '1',\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::ones_prefix(s, n - 1) + if s[n - 1] == '1' { 1int } else { 0int }\n }\n }\n\n pub open spec fn inv_prefix(s: Seq, n: int) -> int\n recommends\n 0 <= n <= s.len(),\n forall |i: int| 0 <= i < s.len() ==> s[i] == '0' || s[i] == '1',\n decreases n,\n {\n if n <= 0 {\n 0\n } else {\n Self::inv_prefix(s, n - 1)\n + if s[n - 1] == '0' { Self::ones_prefix(s, n - 1) } else { 0int }\n }\n }\n\n pub open spec fn answer_spec(s: Seq) -> int {\n Self::inv_prefix(s, s.len() as int)\n }\n\n proof fn lemma_ones_step(s: Seq, i: int)\n requires\n 0 <= i < s.len(),\n forall |k: int| 0 <= k < s.len() ==> s[k] == '0' || s[k] == '1',\n ensures\n Self::ones_prefix(s, i + 1) == Self::ones_prefix(s, i) + if s[i] == '1' { 1int } else { 0int },\n {\n }\n\n proof fn lemma_inv_step(s: Seq, i: int)\n requires\n 0 <= i < s.len(),\n forall |k: int| 0 <= k < s.len() ==> s[k] == '0' || s[k] == '1',\n ensures\n Self::inv_prefix(s, i + 1)\n == Self::inv_prefix(s, i)\n + if s[i] == '0' { Self::ones_prefix(s, i) } else { 0int },\n {\n }\n\n pub fn minimum_steps(s: String) -> (result: i64)\n requires\n 1 <= s@.len() <= 100000,\n forall |i: int| 0 <= i < s@.len() ==> s@[i] == '0' || s@[i] == '1',\n ensures\n result as int == Self::answer_spec(s@),\n {\n let len = s.as_str().unicode_len();\n let mut i: usize = 0;\n let mut ones: i64 = 0;\n let mut steps: i64 = 0;\n\n while i < len\n invariant\n len == s@.len(),\n 1 <= len <= 100000,\n forall |k: int| 0 <= k < s@.len() ==> s@[k] == '0' || s@[k] == '1',\n 0 <= i <= len,\n 0 <= ones as int == Self::ones_prefix(s@, i as int),\n 0 <= steps as int == Self::inv_prefix(s@, i as int),\n ones as int <= i as int,\n ones as int <= 100000,\n steps as int <= (i as int) * 100000,\n decreases len - i,\n {\n let ghost old_i: int = i as int;\n let ghost old_ones: int = ones as int;\n let ghost old_steps: int = steps as int;\n let c = s.as_str().get_char(i);\n\n proof {\n Self::lemma_ones_step(s@, old_i);\n Self::lemma_inv_step(s@, old_i);\n }\n\n if c == '1' {\n ones = ones + 1;\n } else {\n proof {\n assert((steps as int) <= (i as int) * 100000);\n assert((steps as int) + (ones as int) <= 10000100000);\n assert((steps as int) + (ones as int) < 9223372036854775807);\n }\n steps = steps + ones;\n }\n\n proof {\n if c == '1' {\n assert(ones as int == old_ones + 1);\n assert(steps as int == old_steps);\n assert(old_steps <= old_i * 100000);\n assert((old_i as int) * 100000 <= (old_i + 1) * 100000);\n assert(steps as int <= (old_i + 1) * 100000);\n } else {\n assert(ones as int == old_ones);\n assert(steps as int == old_steps + old_ones);\n assert(old_steps <= old_i * 100000);\n assert(old_ones <= 100000);\n assert(steps as int <= old_i * 100000 + 100000);\n assert(old_i * 100000 + 100000 == (old_i + 1) * 100000);\n assert(steps as int <= (old_i + 1) * 100000);\n }\n assert(ones as int == Self::ones_prefix(s@, old_i + 1));\n assert(steps as int == Self::inv_prefix(s@, old_i + 1));\n }\n\n i = i + 1;\n }\n\n proof {\n assert(i == len);\n assert(steps as int == Self::inv_prefix(s@, len as int));\n assert(Self::answer_spec(s@) == Self::inv_prefix(s@, s@.len() as int));\n assert(len as int == s@.len() as int);\n }\n\n steps\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2938/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2938/tests/mutated_testcases.jsonl"} {"problem": "lc2956", "family": "leetcode", "description": "# Find Common Elements Between Two Arrays\n\nYou are given two integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively. Calculate the following values:\n\n\t- `answer1` : the number of indices `i` such that `nums1[i]` exists in `nums2`.\n\n\t- `answer2` : the number of indices `i` such that `nums2[i]` exists in `nums1`.\n\nReturn `[answer1,answer2]`.\n\n \n\nExample 1:\n\n**Input:** nums1 = [2,3,2], nums2 = [1,2]\n\n**Output:** [2,1]\n\n**Explanation:**\n\nExample 2:\n\n**Input:** nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]\n\n**Output:** [3,4]\n\n**Explanation:**\n\nThe elements at indices 1, 2, and 3 in `nums1` exist in `nums2` as well. So `answer1` is 3.\n\nThe elements at indices 0, 1, 3, and 4 in `nums2` exist in `nums1`. So `answer2` is 4.\n\nExample 3:\n\n**Input:** nums1 = [3,4,2,3], nums2 = [1,5]\n\n**Output:** [0,0]\n\n**Explanation:**\n\nNo numbers are common between `nums1` and `nums2`, so answer is [0,0].\n\n \n\n**Constraints:**\n\n\t- `n == nums1.length`\n\n\t- `m == nums2.length`\n\n\t- `1 <= n, m <= 100`\n\n\t- `1 <= nums1[i], nums2[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_intersection_values(nums1: Vec, nums2: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table\nAcceptance rate: 84.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn in_seq(s: Seq, x: i32) -> bool {\n exists |j: int| 0 <= j < s.len() && s[j] == x\n }\n\n pub open spec fn count_in_other_prefix(a: Seq, b: Seq, n: nat) -> nat\n recommends\n n <= a.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::count_in_other_prefix(a, b, (n - 1) as nat)\n + if Self::in_seq(b, a[(n - 1) as int]) { 1nat } else { 0nat }\n }\n }\n\n pub fn find_intersection_values(nums1: Vec, nums2: Vec) -> (result: Vec)\n requires\n 1 <= nums1.len() <= 100,\n 1 <= nums2.len() <= 100,\n forall|i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 100,\n forall|i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 100,\n ensures\n result.len() == 2,\n result[0] as nat == Self::count_in_other_prefix(nums1@, nums2@, nums1.len() as nat),\n result[1] as nat == Self::count_in_other_prefix(nums2@, nums1@, nums2.len() as nat),\n 0 <= result[0] <= nums1.len() as i32,\n 0 <= result[1] <= nums2.len() as i32,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_intersection_values(nums1: Vec, nums2: Vec) -> Vec {\n let mut c1: usize = 0;\n let mut i: usize = 0;\n while i < nums1.len() {\n let cur = nums1[i];\n let mut ok = false;\n let mut j: usize = 0;\n while j < nums2.len() {\n if cur == nums2[j] {\n ok = true;\n break;\n }\n j = j + 1;\n }\n if ok {\n c1 = c1 + 1;\n }\n i = i + 1;\n }\n\n let mut c2: usize = 0;\n i = 0;\n while i < nums2.len() {\n let cur = nums2[i];\n let mut ok = false;\n let mut j: usize = 0;\n while j < nums1.len() {\n if cur == nums1[j] {\n ok = true;\n break;\n }\n j = j + 1;\n }\n if ok {\n c2 = c2 + 1;\n }\n i = i + 1;\n }\n\n let mut out: Vec = Vec::new();\n out.push(c1 as i32);\n out.push(c2 as i32);\n out\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn in_seq(s: Seq, x: i32) -> bool {\n exists |j: int| 0 <= j < s.len() && s[j] == x\n }\n\n pub open spec fn count_in_other_prefix(a: Seq, b: Seq, n: nat) -> nat\n recommends\n n <= a.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::count_in_other_prefix(a, b, (n - 1) as nat)\n + if Self::in_seq(b, a[(n - 1) as int]) { 1nat } else { 0nat }\n }\n }\n\n pub fn find_intersection_values(nums1: Vec, nums2: Vec) -> (result: Vec)\n requires\n 1 <= nums1.len() <= 100,\n 1 <= nums2.len() <= 100,\n forall|i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 100,\n forall|i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 100,\n ensures\n result.len() == 2,\n result[0] as nat == Self::count_in_other_prefix(nums1@, nums2@, nums1.len() as nat),\n result[1] as nat == Self::count_in_other_prefix(nums2@, nums1@, nums2.len() as nat),\n 0 <= result[0] <= nums1.len() as i32,\n 0 <= result[1] <= nums2.len() as i32,\n {\n let mut c1: usize = 0;\n let mut i: usize = 0;\n while i < nums1.len()\n decreases nums1.len() - i,\n {\n let cur = nums1[i];\n let mut ok = false;\n let mut j: usize = 0;\n while j < nums2.len()\n decreases nums2.len() - j,\n {\n if cur == nums2[j] {\n ok = true;\n break;\n }\n j = j + 1;\n }\n if ok {\n c1 = c1 + 1;\n }\n i = i + 1;\n }\n\n let mut c2: usize = 0;\n i = 0;\n while i < nums2.len()\n decreases nums2.len() - i,\n {\n let cur = nums2[i];\n let mut ok = false;\n let mut j: usize = 0;\n while j < nums1.len()\n decreases nums1.len() - j,\n {\n if cur == nums1[j] {\n ok = true;\n break;\n }\n j = j + 1;\n }\n if ok {\n c2 = c2 + 1;\n }\n i = i + 1;\n }\n\n let mut out: Vec = Vec::new();\n out.push(c1 as i32);\n out.push(c2 as i32);\n out\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn in_seq(s: Seq, x: i32) -> bool {\n exists |j: int| 0 <= j < s.len() && s[j] == x\n }\n\n pub open spec fn count_in_other_prefix(a: Seq, b: Seq, n: nat) -> nat\n recommends\n n <= a.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::count_in_other_prefix(a, b, (n - 1) as nat)\n + if Self::in_seq(b, a[(n - 1) as int]) { 1nat } else { 0nat }\n }\n }\n\n pub fn find_intersection_values(nums1: Vec, nums2: Vec) -> (result: Vec)\n requires\n 1 <= nums1.len() <= 100,\n 1 <= nums2.len() <= 100,\n forall|i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 100,\n forall|i: int| 0 <= i < nums2.len() ==> 1 <= #[trigger] nums2[i] <= 100,\n ensures\n result.len() == 2,\n result[0] as nat == Self::count_in_other_prefix(nums1@, nums2@, nums1.len() as nat),\n result[1] as nat == Self::count_in_other_prefix(nums2@, nums1@, nums2.len() as nat),\n 0 <= result[0] <= nums1.len() as i32,\n 0 <= result[1] <= nums2.len() as i32,\n {\n let mut c1: usize = 0;\n let mut i: usize = 0;\n while i < nums1.len()\n invariant\n 0 <= i <= nums1.len(),\n c1 <= i,\n c1 as nat == Self::count_in_other_prefix(nums1@, nums2@, i as nat),\n decreases nums1.len() - i,\n {\n let cur = nums1[i];\n let mut ok = false;\n let mut j: usize = 0;\n while j < nums2.len()\n invariant_except_break\n !ok,\n forall|t: int| 0 <= t < j ==> nums2[t] != cur,\n invariant\n 0 <= j <= nums2.len(),\n ensures\n ok <==> Self::in_seq(nums2@, cur),\n decreases nums2.len() - j,\n {\n if cur == nums2[j] {\n assert(Self::in_seq(nums2@, cur));\n ok = true;\n break;\n }\n j = j + 1;\n }\n if ok {\n c1 = c1 + 1;\n }\n i = i + 1;\n }\n\n let mut c2: usize = 0;\n i = 0;\n while i < nums2.len()\n invariant\n 0 <= i <= nums2.len(),\n c2 <= i,\n c2 as nat == Self::count_in_other_prefix(nums2@, nums1@, i as nat),\n decreases nums2.len() - i,\n {\n let cur = nums2[i];\n let mut ok = false;\n let mut j: usize = 0;\n while j < nums1.len()\n invariant_except_break\n !ok,\n forall|t: int| 0 <= t < j ==> nums1[t] != cur,\n invariant\n 0 <= j <= nums1.len(),\n ensures\n ok <==> Self::in_seq(nums1@, cur),\n decreases nums1.len() - j,\n {\n if cur == nums1[j] {\n assert(Self::in_seq(nums1@, cur));\n ok = true;\n break;\n }\n j = j + 1;\n }\n if ok {\n c2 = c2 + 1;\n }\n i = i + 1;\n }\n\n let mut out: Vec = Vec::new();\n out.push(c1 as i32);\n out.push(c2 as i32);\n out\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2956/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2956/tests/mutated_testcases.jsonl"} {"problem": "lc2963", "family": "leetcode", "description": "# Count the Number of Good Partitions\n\nYou are given an integer array `nums`.\n\nA partition of `nums` into one or more contiguous subarrays is called **good** if every distinct value of `nums` appears in exactly one subarray of the partition. Equivalently, no value may appear in two different parts.\n\nReturn the number of good partitions of `nums`. Since the answer can be large, return it modulo `1_000_000_007`.\n\n## Example 1\n\n- **Input:** `nums = [1,2,3,4]`\n- **Output:** `8`\n- **Explanation:** Every cut position is valid, so there are `2^(4-1) = 8` good partitions.\n\n## Example 2\n\n- **Input:** `nums = [1,1,1,1]`\n- **Output:** `1`\n- **Explanation:** All occurrences of `1` must stay in the same part.\n\n## Example 3\n\n- **Input:** `nums = [1,2,1,3]`\n- **Output:** `2`\n- **Explanation:** The value `1` forces the prefix `[1,2,1]` to stay together, and the last value `3` forms the second block.\n\n## Constraints\n\n- `1 <= nums.length <= 10^5`\n- `1 <= nums[i] <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn number_of_good_partitions(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nSenior Staff, Array, Hash Table, Greedy, Math, Weekly Contest 375\nAcceptance rate: 63.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub const MOD: i64 = 1_000_000_007;\n\n pub open spec fn last_occurrence(nums: Seq, target: i32, upto: int) -> int\n decreases upto\n {\n if upto <= 0 {\n -1\n } else if nums[upto - 1] == target {\n upto - 1\n } else {\n Self::last_occurrence(nums, target, upto - 1)\n }\n }\n\n pub open spec fn close_block(nums: Seq, processed: int, frontier: int) -> int\n recommends\n 0 <= processed <= nums.len(),\n -1 <= frontier < nums.len(),\n decreases nums.len() - processed\n {\n if processed >= nums.len() || processed > frontier {\n frontier\n } else {\n let last = Self::last_occurrence(nums, nums[processed], nums.len() as int);\n let new_frontier = if last > frontier { last } else { frontier };\n Self::close_block(nums, processed + 1, new_frontier)\n }\n }\n\n pub open spec fn block_end(nums: Seq, start: int) -> int\n recommends\n 0 <= start < nums.len(),\n {\n let first = Self::last_occurrence(nums, nums[start], nums.len() as int);\n Self::close_block(nums, start + 1, first)\n }\n\n pub open spec fn number_of_good_partitions_from(nums: Seq, start: int) -> int\n recommends\n 0 <= start <= nums.len(),\n decreases nums.len() - start\n {\n if start >= nums.len() {\n 1\n } else {\n let next = Self::block_end(nums, start) + 1;\n if next <= start || next >= nums.len() {\n 1\n } else {\n (2 * Self::number_of_good_partitions_from(nums, next)) % (Self::MOD as int)\n }\n }\n }\n\n pub open spec fn number_of_good_partitions_spec(nums: Seq) -> int {\n Self::number_of_good_partitions_from(nums, 0)\n }\n\n pub fn number_of_good_partitions(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result < Self::MOD,\n result as int == Self::number_of_good_partitions_spec(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub const MOD: i64 = 1_000_000_007;\n\n pub fn number_of_good_partitions(nums: Vec) -> i32 {\n let n = nums.len();\n let mut last_map: std::collections::HashMap = std::collections::HashMap::new();\n let mut idx: usize = 0;\n while idx < n {\n let v = nums[idx];\n last_map.insert(v, idx);\n idx += 1;\n }\n\n let mut answer: i64 = 1;\n let mut start = 0usize;\n\n while start < n {\n let old_start = start;\n let mut end = *last_map.get(&nums[start]).unwrap();\n let mut i = start + 1;\n while i < n && i <= end {\n let old_end = end;\n let candidate = *last_map.get(&nums[i]).unwrap();\n if candidate > end {\n end = candidate;\n }\n i += 1;\n }\n start = end + 1;\n if start < n {\n answer = (answer * 2) % Self::MOD;\n }\n }\n\n answer as i32\n }\n}\n", "code_spec": "use vstd::arithmetic::div_mod::lemma_mul_mod_noop;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\nbroadcast use vstd::std_specs::hash::group_hash_axioms;\n\npub struct Solution;\n\nimpl Solution {\n pub const MOD: i64 = 1_000_000_007;\n\n pub open spec fn last_occurrence(nums: Seq, target: i32, upto: int) -> int\n decreases upto\n {\n if upto <= 0 {\n -1\n } else if nums[upto - 1] == target {\n upto - 1\n } else {\n Self::last_occurrence(nums, target, upto - 1)\n }\n }\n\n pub open spec fn close_block(nums: Seq, processed: int, frontier: int) -> int\n recommends\n 0 <= processed <= nums.len(),\n -1 <= frontier < nums.len(),\n decreases nums.len() - processed\n {\n if processed >= nums.len() || processed > frontier {\n frontier\n } else {\n let last = Self::last_occurrence(nums, nums[processed], nums.len() as int);\n let new_frontier = if last > frontier { last } else { frontier };\n Self::close_block(nums, processed + 1, new_frontier)\n }\n }\n\n pub open spec fn block_end(nums: Seq, start: int) -> int\n recommends\n 0 <= start < nums.len(),\n {\n let first = Self::last_occurrence(nums, nums[start], nums.len() as int);\n Self::close_block(nums, start + 1, first)\n }\n\n pub open spec fn number_of_good_partitions_from(nums: Seq, start: int) -> int\n recommends\n 0 <= start <= nums.len(),\n decreases nums.len() - start\n {\n if start >= nums.len() {\n 1\n } else {\n let next = Self::block_end(nums, start) + 1;\n if next <= start || next >= nums.len() {\n 1\n } else {\n (2 * Self::number_of_good_partitions_from(nums, next)) % (Self::MOD as int)\n }\n }\n }\n\n pub open spec fn number_of_good_partitions_spec(nums: Seq) -> int {\n Self::number_of_good_partitions_from(nums, 0)\n }\n\n\n\n\n\n\n pub fn number_of_good_partitions(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result < Self::MOD,\n result as int == Self::number_of_good_partitions_spec(nums@),\n {\n let n = nums.len();\n\n let mut last_map: std::collections::HashMap = std::collections::HashMap::new();\n let mut idx: usize = 0;\n while idx < n\n decreases n - idx,\n {\n let v = nums[idx];\n last_map.insert(v, idx);\n \n idx += 1;\n } \n\n let mut answer: i64 = 1;\n let mut start = 0usize;\n\n while start < n\n decreases n - start,\n {\n let old_start = start;\n\n let mut end = *last_map.get(&nums[start]).unwrap();\n let mut i = start + 1;\n \n while i < n && i <= end\n decreases n - i,\n {\n let old_end = end;\n\n let candidate = *last_map.get(&nums[i]).unwrap();\n if candidate > end {\n end = candidate;\n }\n \n i += 1;\n }\n \n start = end + 1;\n if start < n {\n \n answer = (answer * 2) % Self::MOD;\n } else {\n \n }\n }\n\n \n answer as i32\n }\n}\n\n}\n", "verified": "use vstd::arithmetic::div_mod::lemma_mul_mod_noop;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\nbroadcast use vstd::std_specs::hash::group_hash_axioms;\n\npub struct Solution;\n\nimpl Solution {\n pub const MOD: i64 = 1_000_000_007;\n\n pub open spec fn last_occurrence(nums: Seq, target: i32, upto: int) -> int\n decreases upto\n {\n if upto <= 0 {\n -1\n } else if nums[upto - 1] == target {\n upto - 1\n } else {\n Self::last_occurrence(nums, target, upto - 1)\n }\n }\n\n pub open spec fn close_block(nums: Seq, processed: int, frontier: int) -> int\n recommends\n 0 <= processed <= nums.len(),\n -1 <= frontier < nums.len(),\n decreases nums.len() - processed\n {\n if processed >= nums.len() || processed > frontier {\n frontier\n } else {\n let last = Self::last_occurrence(nums, nums[processed], nums.len() as int);\n let new_frontier = if last > frontier { last } else { frontier };\n Self::close_block(nums, processed + 1, new_frontier)\n }\n }\n\n pub open spec fn block_end(nums: Seq, start: int) -> int\n recommends\n 0 <= start < nums.len(),\n {\n let first = Self::last_occurrence(nums, nums[start], nums.len() as int);\n Self::close_block(nums, start + 1, first)\n }\n\n pub open spec fn number_of_good_partitions_from(nums: Seq, start: int) -> int\n recommends\n 0 <= start <= nums.len(),\n decreases nums.len() - start\n {\n if start >= nums.len() {\n 1\n } else {\n let next = Self::block_end(nums, start) + 1;\n if next <= start || next >= nums.len() {\n 1\n } else {\n (2 * Self::number_of_good_partitions_from(nums, next)) % (Self::MOD as int)\n }\n }\n }\n\n pub open spec fn number_of_good_partitions_spec(nums: Seq) -> int {\n Self::number_of_good_partitions_from(nums, 0)\n }\n\n proof fn lemma_last_occurrence_at_least(nums: Seq, target: i32, idx: int, upto: int)\n requires\n 0 <= idx < upto <= nums.len(),\n nums[idx] == target,\n ensures\n idx <= Self::last_occurrence(nums, target, upto),\n decreases upto - idx,\n {\n if upto != idx + 1 {\n if nums[upto - 1] == target {\n } else {\n Self::lemma_last_occurrence_at_least(nums, target, idx, upto - 1);\n }\n }\n }\n\n proof fn lemma_last_occurrence_bound(nums: Seq, target: i32, upto: int)\n requires\n 0 <= upto <= nums.len(),\n ensures\n Self::last_occurrence(nums, target, upto) < upto,\n decreases upto,\n {\n if upto <= 0 {\n } else if nums[upto - 1] == target {\n } else {\n Self::lemma_last_occurrence_bound(nums, target, upto - 1);\n }\n }\n\n proof fn lemma_last_occurrence_is_target(nums: Seq, target: i32, upto: int)\n requires\n 0 <= upto <= nums.len(),\n Self::last_occurrence(nums, target, upto) >= 0,\n ensures\n nums[Self::last_occurrence(nums, target, upto)] == target,\n decreases upto,\n {\n if upto <= 0 {\n } else if nums[upto - 1] == target {\n } else {\n Self::lemma_last_occurrence_is_target(nums, target, upto - 1);\n }\n }\n\n proof fn lemma_last_occurrence_agrees(nums: Seq, target: i32, idx: int, upto: int)\n requires\n 0 <= idx < upto <= nums.len(),\n nums[idx] == target,\n forall|k: int| idx < k < upto ==> nums[k] != target,\n ensures\n Self::last_occurrence(nums, target, upto) == idx,\n decreases upto - idx,\n {\n if upto == idx + 1 {\n } else {\n assert(nums[upto - 1] != target);\n Self::lemma_last_occurrence_agrees(nums, target, idx, upto - 1);\n }\n }\n\n proof fn lemma_hashmap_matches_last_occurrence(nums: Seq, map: Map, n: int)\n requires\n n == nums.len(),\n forall |v: i32| #[trigger] map.contains_key(v) ==> {\n let idx = map[v] as int;\n 0 <= idx < n &&\n nums[idx] == v &&\n forall |k: int| idx < k < n ==> nums[k] != v\n },\n forall |i: int| 0 <= i < n ==> map.contains_key(#[trigger] nums[i]),\n ensures\n forall |v: i32| #[trigger] map.contains_key(v) ==>\n map[v] as int == Self::last_occurrence(nums, v, n),\n {\n assert forall |v: i32| #[trigger] map.contains_key(v) implies\n map[v] as int == Self::last_occurrence(nums, v, n) by {\n let idx = map[v] as int;\n Self::lemma_last_occurrence_agrees(nums, v, idx, n);\n }\n }\n\n pub fn number_of_good_partitions(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n 1 <= result < Self::MOD,\n result as int == Self::number_of_good_partitions_spec(nums@),\n {\n let n = nums.len();\n\n let mut last_map: std::collections::HashMap = std::collections::HashMap::new();\n let mut idx: usize = 0;\n while idx < n\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= idx <= n,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n forall |v: i32| #[trigger] last_map@.contains_key(v) ==> {\n let last_idx = last_map@[v] as int;\n 0 <= last_idx < idx &&\n nums@[last_idx] == v &&\n forall |k: int| last_idx < k < idx ==> nums@[k] != v\n },\n forall |i: int| 0 <= i < idx ==> last_map@.contains_key(#[trigger] nums@[i]),\n decreases n - idx,\n {\n let v = nums[idx];\n last_map.insert(v, idx);\n proof {\n assert forall |w: i32| #[trigger] last_map@.contains_key(w) implies ({\n let last_idx = last_map@[w] as int;\n 0 <= last_idx < idx + 1 &&\n nums@[last_idx] == w &&\n forall |k: int| last_idx < k < idx + 1 ==> nums@[k] != w\n }) by {\n if w == v {\n assert(last_map@[w] == idx);\n } else {\n }\n }\n assert forall |i: int| 0 <= i < idx + 1 implies last_map@.contains_key(#[trigger] nums@[i]) by {\n if i == idx as int {\n assert(nums@[i] == v);\n assert(last_map@.contains_key(v));\n }\n }\n }\n idx += 1;\n }\n\n \n proof {\n Self::lemma_hashmap_matches_last_occurrence(nums@, last_map@, n as int);\n }\n\n let mut answer: i64 = 1;\n let mut start = 0usize;\n\n while start < n\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= start <= n,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] nums[k] <= 1_000_000_000,\n 1 <= answer < Self::MOD,\n ((answer as int) * Self::number_of_good_partitions_from(nums@, start as int))\n % (Self::MOD as int) == Self::number_of_good_partitions_spec(nums@),\n \n forall |v: i32| #[trigger] last_map@.contains_key(v) ==> {\n let last_idx = last_map@[v] as int;\n 0 <= last_idx < n &&\n nums@[last_idx] == v &&\n last_idx == Self::last_occurrence(nums@, v, n as int)\n },\n forall |i: int| 0 <= i < n ==> last_map@.contains_key(#[trigger] nums@[i]),\n decreases n - start,\n {\n let old_start = start;\n\n let mut end = *last_map.get(&nums[start]).unwrap();\n let mut i = start + 1;\n proof {\n assert(last_map@.contains_key(nums[start as int]));\n assert(end as int == Self::last_occurrence(nums@, nums[start as int], n as int));\n Self::lemma_last_occurrence_at_least(nums@, nums[start as int], start as int, n as int);\n assert(start as int <= end as int);\n assert(Self::close_block(nums@, i as int, end as int)\n == Self::block_end(nums@, start as int));\n }\n while i < n && i <= end\n invariant\n n == nums.len(),\n 1 <= n <= 100_000,\n 0 <= old_start < n,\n old_start < i <= n,\n old_start <= end < n,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] nums[k] <= 1_000_000_000,\n Self::close_block(nums@, i as int, end as int)\n == Self::block_end(nums@, old_start as int),\n forall |v: i32| #[trigger] last_map@.contains_key(v) ==> {\n let last_idx = last_map@[v] as int;\n 0 <= last_idx < n &&\n nums@[last_idx] == v &&\n last_idx == Self::last_occurrence(nums@, v, n as int)\n },\n forall |ii: int| 0 <= ii < n ==> last_map@.contains_key(#[trigger] nums@[ii]),\n decreases n - i,\n {\n let old_end = end;\n\n let candidate = *last_map.get(&nums[i]).unwrap();\n if candidate > end {\n end = candidate;\n }\n proof {\n assert(last_map@.contains_key(nums[i as int]));\n assert(candidate as int == Self::last_occurrence(nums@, nums[i as int], n as int));\n let new_end = end as int;\n let candidate_int = candidate as int;\n if candidate > old_end {\n assert(new_end == candidate_int);\n } else {\n assert(new_end == old_end as int);\n }\n assert(Self::close_block(nums@, i as int, old_end as int)\n == Self::close_block(nums@, i as int + 1, new_end));\n }\n i += 1;\n }\n proof {\n assert(i as int > end as int);\n assert(Self::close_block(nums@, i as int, end as int) == end as int);\n assert(end as int == Self::block_end(nums@, old_start as int));\n }\n start = end + 1;\n if start < n {\n proof {\n let suffix = Self::number_of_good_partitions_from(nums@, start as int);\n assert(Self::number_of_good_partitions_from(nums@, old_start as int)\n == (2 * suffix) % (Self::MOD as int));\n lemma_mul_mod_noop(answer as int, 2 * suffix, Self::MOD as int);\n lemma_mul_mod_noop(answer as int * 2, suffix, Self::MOD as int);\n assert((((answer as int) * 2) * suffix)\n == ((answer as int) * (2 * suffix))) by (nonlinear_arith);\n assert(\n ((((answer as int) * 2) % (Self::MOD as int)) * suffix)\n % (Self::MOD as int)\n == ((answer as int)\n * Self::number_of_good_partitions_from(nums@, old_start as int))\n % (Self::MOD as int)\n );\n assert(answer * 2 <= 2_000_000_012) by (nonlinear_arith)\n requires\n 1 <= answer < Self::MOD,\n Self::MOD == 1_000_000_007,\n {};\n }\n answer = (answer * 2) % Self::MOD;\n } else {\n proof {\n assert(Self::number_of_good_partitions_from(nums@, old_start as int) == 1);\n }\n }\n }\n\n proof {\n assert(Self::number_of_good_partitions_from(nums@, n as int) == 1);\n assert(answer as int == Self::number_of_good_partitions_spec(nums@));\n assert(answer < 2_147_483_647) by (nonlinear_arith)\n requires\n 1 <= answer < Self::MOD,\n Self::MOD == 1_000_000_007,\n {};\n }\n answer as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2963/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2963/tests/mutated_testcases.jsonl"} {"problem": "lc2996", "family": "leetcode", "description": "# Smallest Missing Integer Greater Than Sequential Prefix Sum\n\nYou are given a **0-indexed** array of integers `nums`.\n\nA prefix `nums[0..i]` is **sequential** if, for all `1 <= j <= i`, `nums[j] = nums[j - 1] + 1`. In particular, the prefix consisting only of `nums[0]` is **sequential**.\n\nReturn *the **smallest** integer* `x` *missing from* `nums` *such that* `x` *is greater than or equal to the sum of the **longest** sequential prefix.*\n\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,2,5]\n>**Output:** 6\n>**Explanation:** The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.\n\n\n## Example 2:\n\n> **Input:** nums = [3,4,5,1,12,14,13]\n> **Output:** 15\n> **Explanation:** The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.\n\n## Constraints:\n\n- $1 <= nums.length <= 50$\n\n- $1 <= nums[i] <= 50$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn missing_integer(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Array, Hash Table, Sorting, Biweekly Contest 121\nAcceptance rate: 35.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains(nums: Seq, value: i32) -> bool {\n exists |j: int| 0 <= j < nums.len() && #[trigger] nums[j] == value\n }\n\n pub open spec fn sequential_prefix_len(nums: Seq) -> nat\n decreases nums.len(),\n {\n if nums.len() == 0 {\n 0nat\n } else if nums.len() == 1 {\n 1nat\n } else if nums[1] == nums[0] + 1 {\n 1nat + Self::sequential_prefix_len(nums.subrange(1, nums.len() as int))\n } else {\n 1nat\n }\n }\n\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn sequential_prefix_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, Self::sequential_prefix_len(nums) as int)\n }\n\n pub fn missing_integer(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n res >= Self::sequential_prefix_sum(nums@),\n !Self::contains(nums@, res),\n forall |x: int| Self::sequential_prefix_sum(nums@) <= x < res ==> #[trigger] Self::contains(nums@, x as i32),\n 1 <= res,\n {\n \n }\n}\n\n}\n", "code": "impl Solution {\n pub fn missing_integer(nums: Vec) -> i32\n {\n let n = nums.len();\n\n let mut prefix_sum = nums[0];\n let mut i: usize = 1;\n \n while i < n && nums[i] == nums[i - 1] + 1\n {\n prefix_sum += nums[i];\n i += 1;\n }\n\n let mut candidate = prefix_sum;\n let mut found = false;\n while !found\n {\n let mut exists = false;\n let mut j: usize = 0;\n while j < n && !exists\n {\n if nums[j] == candidate {\n exists = true;\n }\n j += 1;\n }\n\n if !exists {\n found = true;\n } else {\n candidate += 1;\n }\n }\n\n candidate\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains(nums: Seq, value: i32) -> bool {\n exists |j: int| 0 <= j < nums.len() && #[trigger] nums[j] == value\n }\n\n pub open spec fn sequential_prefix_len(nums: Seq) -> nat\n decreases nums.len(),\n {\n if nums.len() == 0 {\n 0nat\n } else if nums.len() == 1 {\n 1nat\n } else if nums[1] == nums[0] + 1 {\n 1nat + Self::sequential_prefix_len(nums.subrange(1, nums.len() as int))\n } else {\n 1nat\n }\n }\n\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn sequential_prefix_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, Self::sequential_prefix_len(nums) as int)\n }\n\n pub fn missing_integer(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n res >= Self::sequential_prefix_sum(nums@),\n !Self::contains(nums@, res),\n forall |x: int| Self::sequential_prefix_sum(nums@) <= x < res ==> #[trigger] Self::contains(nums@, x as i32),\n 1 <= res,\n {\n let n = nums.len();\n\n let mut prefix_sum = nums[0];\n let mut i: usize = 1;\n \n while i < n && nums[i] == nums[i - 1] + 1\n {\n prefix_sum += nums[i];\n i += 1;\n }\n\n let mut candidate = prefix_sum;\n let mut found = false;\n while !found\n {\n let mut exists = false;\n let mut j: usize = 0;\n while j < n && !exists\n {\n if nums[j] == candidate {\n exists = true;\n }\n j += 1;\n }\n\n if !exists {\n found = true;\n } else {\n candidate += 1;\n }\n }\n\n candidate\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains(nums: Seq, value: i32) -> bool {\n exists |j: int| 0 <= j < nums.len() && #[trigger] nums[j] == value\n }\n\n pub open spec fn sequential_prefix_len(nums: Seq) -> nat\n decreases nums.len(),\n {\n if nums.len() == 0 {\n 0nat\n } else if nums.len() == 1 {\n 1nat\n } else if nums[1] == nums[0] + 1 {\n 1nat + Self::sequential_prefix_len(nums.subrange(1, nums.len() as int))\n } else {\n 1nat\n }\n }\n\n pub open spec fn prefix_sum(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::prefix_sum(nums, end - 1) + nums[end - 1] as int\n }\n }\n\n pub open spec fn sequential_prefix_sum(nums: Seq) -> int {\n Self::prefix_sum(nums, Self::sequential_prefix_len(nums) as int)\n }\n\n proof fn lemma_sequential_prefix_len_at_break(nums: Seq, i: int)\n requires\n 1 <= i <= nums.len(),\n forall |j: int| 1 <= j < i ==> #[trigger] nums[j] == nums[j - 1] + 1,\n i < nums.len() ==> nums[i] != nums[i - 1] + 1,\n ensures\n Self::sequential_prefix_len(nums) == i,\n decreases i,\n {\n if i == 1 {\n if nums.len() == 1 {\n } else {\n assert(nums[1] != nums[0] + 1);\n }\n } else {\n Self::lemma_sequential_prefix_len_at_break(nums.subrange(1, nums.len() as int), i - 1);\n }\n }\n\n pub fn missing_integer(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n res >= Self::sequential_prefix_sum(nums@),\n !Self::contains(nums@, res),\n forall |x: int| Self::sequential_prefix_sum(nums@) <= x < res ==> #[trigger] Self::contains(nums@, x as i32),\n 1 <= res,\n {\n let n = nums.len();\n\n let mut prefix_sum = nums[0];\n let mut i: usize = 1;\n proof {\n reveal_with_fuel(Solution::prefix_sum, 2);\n assert(Self::prefix_sum(nums@, 0) == 0);\n assert(Self::prefix_sum(nums@, 1) == nums@[0] as int);\n assert(prefix_sum as int == Self::prefix_sum(nums@, i as int));\n }\n while i < n && nums[i] == nums[i - 1] + 1\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 50,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 50,\n 1 <= i <= n,\n prefix_sum as int == Self::prefix_sum(nums@, i as int),\n 1 <= prefix_sum as int <= i as int * 50,\n forall |j: int| 1 <= j < i as int ==> #[trigger] nums[j] == nums[j - 1] + 1,\n decreases n - i\n {\n prefix_sum += nums[i];\n i += 1;\n }\n\n proof {\n Self::lemma_sequential_prefix_len_at_break(nums@, i as int);\n assert(Self::sequential_prefix_len(nums@) == i as int);\n assert(Self::sequential_prefix_sum(nums@) == Self::prefix_sum(nums@, i as int));\n assert(prefix_sum as int == Self::sequential_prefix_sum(nums@));\n }\n\n let mut candidate = prefix_sum;\n let mut found = false;\n while !found\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 50,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 50,\n 1 <= candidate <= 2500,\n candidate as int >= Self::sequential_prefix_sum(nums@),\n forall |x: int| Self::sequential_prefix_sum(nums@) <= x < candidate ==> #[trigger] Self::contains(nums@, x as i32),\n found ==> !Self::contains(nums@, candidate),\n decreases 2500 - candidate, if found { 0int } else { 1int }, \n {\n let mut exists = false;\n let mut j: usize = 0;\n while j < n && !exists\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 50,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 50,\n 0 <= j <= n,\n exists ==> exists |t: int| 0 <= t < j && nums[t] == candidate,\n !exists ==> forall |t: int| 0 <= t < j ==> nums[t] != candidate,\n decreases n - j, \n {\n if nums[j] == candidate {\n exists = true;\n }\n j += 1;\n }\n\n if !exists {\n assert(j == n);\n assert forall |t: int| 0 <= t < nums.len() implies nums[t] != candidate by {\n assert(t < j);\n };\n assert(!Self::contains(nums@, candidate));\n found = true;\n } else {\n assert(exists |t: int| 0 <= t < j && (nums@).index(t) == candidate);\n let ghost t = choose |t: int| 0 <= t < j && (nums@).index(t) == candidate;\n assert(0 <= t < nums.len());\n assert((nums@).index(t) == candidate);\n assert(Self::contains(nums@, candidate));\n candidate += 1;\n assert(forall |x: int| Self::sequential_prefix_sum(nums@) <= x < candidate ==> #[trigger] Self::contains(nums@, x as i32)) by {\n assert forall |x: int| Self::sequential_prefix_sum(nums@) <= x < candidate implies #[trigger] Self::contains(nums@, x as i32) by {\n if x < candidate - 1 {\n assert(Self::contains(nums@, x as i32));\n } else {\n assert(x == candidate - 1);\n assert(Self::contains(nums@, (candidate - 1) as i32));\n }\n };\n };\n }\n }\n\n candidate\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc2996/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc2996/tests/mutated_testcases.jsonl"} {"problem": "lc3001", "family": "leetcode", "description": "# Minimum Moves to Capture The Queen\n\nThere is a **1-indexed** `8 x 8` chessboard containing `3` pieces.\n\nYou are given `6` integers `a`, `b`, `c`, `d`, `e`, and `f` where:\n\n\t- `(a, b)` denotes the position of the white rook.\n\n\t- `(c, d)` denotes the position of the white bishop.\n\n\t- `(e, f)` denotes the position of the black queen.\n\nGiven that you can only move the white pieces, return *the **minimum** number of moves required to capture the black queen*.\n\n**Note** that:\n\n\t- Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.\n\n\t- Bishops can move any number of squares diagonally, but cannot jump over other pieces.\n\n\t- A rook or a bishop can capture the queen if it is located in a square that they can move to.\n\n\t- The queen does not move.\n\n \n\nExample 1:\n\n```\n**Input:** a = 1, b = 1, c = 8, d = 8, e = 2, f = 3\n**Output:** 2\n**Explanation:** We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).\nIt is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.\n```\n\nExample 2:\n\n```\n**Input:** a = 5, b = 3, c = 3, d = 4, e = 5, f = 2\n**Output:** 1\n**Explanation:** We can capture the black queen in a single move by doing one of the following: \n- Move the white rook to (5, 2).\n- Move the white bishop to (5, 2).\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= a, b, c, d, e, f <= 8`\n\n\t- No two pieces are on the same square.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Enumeration\nAcceptance rate: 22.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn between(x: int, lo: int, hi: int) -> bool {\n (lo < x && x < hi) || (hi < x && x < lo)\n }\n\n pub open spec fn rook_row_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n a == e && !(c == a && Self::between(d, b, f))\n }\n\n pub open spec fn rook_col_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n b == f && !(d == b && Self::between(c, a, e))\n }\n\n pub open spec fn rook_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n Self::rook_row_attack(a, b, c, d, e, f) || Self::rook_col_attack(a, b, c, d, e, f)\n }\n\n pub open spec fn bishop_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n if c + d == e + f {\n !(a + b == c + d && Self::between(a, c, e))\n } else if c - d == e - f {\n !(a - b == c - d && Self::between(a, c, e))\n } else {\n false\n }\n }\n\n pub open spec fn can_capture_one(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n Self::rook_attack(a, b, c, d, e, f) || Self::bishop_attack(a, b, c, d, e, f)\n }\n\n pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> (result: i32)\n requires\n 1 <= a <= 8,\n 1 <= b <= 8,\n 1 <= c <= 8,\n 1 <= d <= 8,\n 1 <= e <= 8,\n 1 <= f <= 8,\n a != c || b != d,\n a != e || b != f,\n c != e || d != f,\n ensures\n result as int == if Self::can_capture_one(a as int, b as int, c as int, d as int, e as int, f as int) { 1int } else { 2int },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 {\n let rook_row_attack = a == e && !(c == a && ((b < d && d < f) || (f < d && d < b)));\n let rook_col_attack = b == f && !(d == b && ((a < c && c < e) || (e < c && c < a)));\n let rook_attack = rook_row_attack || rook_col_attack;\n\n let bishop_diag_sum = c + d == e + f;\n let bishop_diag_diff = c - d == e - f;\n let bishop_attack = if bishop_diag_sum {\n !(a + b == c + d && ((c < a && a < e) || (e < a && a < c)))\n } else if bishop_diag_diff {\n !(a - b == c - d && ((c < a && a < e) || (e < a && a < c)))\n } else {\n false\n };\n\n if rook_attack || bishop_attack {\n 1\n } else {\n 2\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn between(x: int, lo: int, hi: int) -> bool {\n (lo < x && x < hi) || (hi < x && x < lo)\n }\n\n pub open spec fn rook_row_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n a == e && !(c == a && Self::between(d, b, f))\n }\n\n pub open spec fn rook_col_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n b == f && !(d == b && Self::between(c, a, e))\n }\n\n pub open spec fn rook_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n Self::rook_row_attack(a, b, c, d, e, f) || Self::rook_col_attack(a, b, c, d, e, f)\n }\n\n pub open spec fn bishop_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n if c + d == e + f {\n !(a + b == c + d && Self::between(a, c, e))\n } else if c - d == e - f {\n !(a - b == c - d && Self::between(a, c, e))\n } else {\n false\n }\n }\n\n pub open spec fn can_capture_one(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n Self::rook_attack(a, b, c, d, e, f) || Self::bishop_attack(a, b, c, d, e, f)\n }\n\n pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> (result: i32)\n requires\n 1 <= a <= 8,\n 1 <= b <= 8,\n 1 <= c <= 8,\n 1 <= d <= 8,\n 1 <= e <= 8,\n 1 <= f <= 8,\n a != c || b != d,\n a != e || b != f,\n c != e || d != f,\n ensures\n result as int == if Self::can_capture_one(a as int, b as int, c as int, d as int, e as int, f as int) { 1int } else { 2int },\n {\n let rook_row_attack = a == e && !(c == a && ((b < d && d < f) || (f < d && d < b)));\n let rook_col_attack = b == f && !(d == b && ((a < c && c < e) || (e < c && c < a)));\n let rook_attack = rook_row_attack || rook_col_attack;\n\n let bishop_diag_sum = c + d == e + f;\n let bishop_diag_diff = c - d == e - f;\n let bishop_attack = if bishop_diag_sum {\n !(a + b == c + d && ((c < a && a < e) || (e < a && a < c)))\n } else if bishop_diag_diff {\n !(a - b == c - d && ((c < a && a < e) || (e < a && a < c)))\n } else {\n false\n };\n\n if rook_attack || bishop_attack {\n 1\n } else {\n 2\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn between(x: int, lo: int, hi: int) -> bool {\n (lo < x && x < hi) || (hi < x && x < lo)\n }\n\n pub open spec fn rook_row_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n a == e && !(c == a && Self::between(d, b, f))\n }\n\n pub open spec fn rook_col_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n b == f && !(d == b && Self::between(c, a, e))\n }\n\n pub open spec fn rook_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n Self::rook_row_attack(a, b, c, d, e, f) || Self::rook_col_attack(a, b, c, d, e, f)\n }\n\n pub open spec fn bishop_attack(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n if c + d == e + f {\n !(a + b == c + d && Self::between(a, c, e))\n } else if c - d == e - f {\n !(a - b == c - d && Self::between(a, c, e))\n } else {\n false\n }\n }\n\n pub open spec fn can_capture_one(a: int, b: int, c: int, d: int, e: int, f: int) -> bool {\n Self::rook_attack(a, b, c, d, e, f) || Self::bishop_attack(a, b, c, d, e, f)\n }\n\n pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> (result: i32)\n requires\n 1 <= a <= 8,\n 1 <= b <= 8,\n 1 <= c <= 8,\n 1 <= d <= 8,\n 1 <= e <= 8,\n 1 <= f <= 8,\n a != c || b != d,\n a != e || b != f,\n c != e || d != f,\n ensures\n result as int == if Self::can_capture_one(a as int, b as int, c as int, d as int, e as int, f as int) { 1int } else { 2int },\n {\n let rook_row_attack = a == e && !(c == a && ((b < d && d < f) || (f < d && d < b)));\n let rook_col_attack = b == f && !(d == b && ((a < c && c < e) || (e < c && c < a)));\n let rook_attack = rook_row_attack || rook_col_attack;\n\n let bishop_diag_sum = c + d == e + f;\n let bishop_diag_diff = c - d == e - f;\n let bishop_attack = if bishop_diag_sum {\n !(a + b == c + d && ((c < a && a < e) || (e < a && a < c)))\n } else if bishop_diag_diff {\n !(a - b == c - d && ((c < a && a < e) || (e < a && a < c)))\n } else {\n false\n };\n\n proof {\n assert(rook_row_attack == Self::rook_row_attack(a as int, b as int, c as int, d as int, e as int, f as int));\n assert(rook_col_attack == Self::rook_col_attack(a as int, b as int, c as int, d as int, e as int, f as int));\n assert(rook_attack == Self::rook_attack(a as int, b as int, c as int, d as int, e as int, f as int));\n if bishop_diag_sum {\n assert(c as int + d as int == e as int + f as int);\n assert(bishop_attack == Self::bishop_attack(a as int, b as int, c as int, d as int, e as int, f as int));\n } else if bishop_diag_diff {\n assert(c as int - d as int == e as int - f as int);\n assert(bishop_attack == Self::bishop_attack(a as int, b as int, c as int, d as int, e as int, f as int));\n } else {\n assert(Self::bishop_attack(a as int, b as int, c as int, d as int, e as int, f as int) == false);\n assert(bishop_attack == Self::bishop_attack(a as int, b as int, c as int, d as int, e as int, f as int));\n }\n assert((rook_attack || bishop_attack) == Self::can_capture_one(a as int, b as int, c as int, d as int, e as int, f as int));\n }\n\n if rook_attack || bishop_attack {\n 1\n } else {\n 2\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3001/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3001/tests/mutated_testcases.jsonl"} {"problem": "lc3010", "family": "leetcode", "description": "# Divide an Array Into Subarrays With Minimum Cost I\n\nYou are given an array of integers `nums` of length `n`.\n\nThe **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`.\n\nYou need to divide `nums` into `3` **disjoint contiguous **subarrays.\n\nReturn *the **minimum** possible **sum** of the cost of these subarrays*.\n\n \n\nExample 1:\n\n```\n**Input:** nums = [1,2,3,12]\n**Output:** 6\n**Explanation:** The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.\nThe other possible ways to form 3 subarrays are:\n- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.\n- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.\n```\n\nExample 2:\n\n```\n**Input:** nums = [5,4,3]\n**Output:** 12\n**Explanation:** The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.\nIt can be shown that 12 is the minimum cost achievable.\n```\n\nExample 3:\n\n```\n**Input:** nums = [10,3,1,1]\n**Output:** 12\n**Explanation:** The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.\nIt can be shown that 12 is the minimum cost achievable.\n```\n\n \n\n**Constraints:**\n\n\t- `3 <= n <= 50`\n\n\t- `1 <= nums[i] <= 50`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_cost(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sorting, Enumeration\nAcceptance rate: 80.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\npub open spec fn split_cost(nums: Seq, i: int, j: int) -> int {\n nums[0] as int + nums[i] as int + nums[j] as int\n}\n\n\npub open spec fn spec_minimum_cost(nums: Seq) -> int {\n \n \n \n \n nums[0] as int + spec_two_smallest_sum(nums, nums.len() as int)\n}\n\n\n\npub open spec fn spec_min1(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 1 {\n 51 \n } else if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n nums[k - 1] as int\n } else {\n spec_min1(nums, k - 1)\n }\n}\n\npub open spec fn spec_min2(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 1 {\n 51\n } else if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n \n spec_min1(nums, k - 1)\n } else if (nums[k - 1] as int) < spec_min2(nums, k - 1) {\n nums[k - 1] as int\n } else {\n spec_min2(nums, k - 1)\n }\n}\n\npub open spec fn spec_two_smallest_sum(nums: Seq, k: int) -> int {\n spec_min1(nums, k) + spec_min2(nums, k)\n}\n\nimpl Solution {\n pub fn minimum_cost(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n \n result as int == spec_minimum_cost(nums@),\n \n exists |i: int, j: int|\n 1 <= i < j < nums.len()\n && result as int == split_cost(nums@, i, j),\n \n forall |i: int, j: int|\n 1 <= i < j < nums.len()\n ==> result as int <= split_cost(nums@, i, j),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn minimum_cost(nums: Vec) -> i32 {\n let n = nums.len();\n let mut min1: i32 = 51;\n let mut min2: i32 = 51;\n let mut i: usize = 1;\n while i < n {\n if nums[i] < min1 {\n min2 = min1;\n min1 = nums[i];\n } else if nums[i] < min2 {\n min2 = nums[i];\n }\n i = i + 1;\n }\n nums[0] + min1 + min2\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\npub open spec fn split_cost(nums: Seq, i: int, j: int) -> int {\n nums[0] as int + nums[i] as int + nums[j] as int\n}\n\n\npub open spec fn spec_minimum_cost(nums: Seq) -> int {\n \n \n \n \n nums[0] as int + spec_two_smallest_sum(nums, nums.len() as int)\n}\n\n\n\npub open spec fn spec_min1(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 1 {\n 51 \n } else if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n nums[k - 1] as int\n } else {\n spec_min1(nums, k - 1)\n }\n}\n\npub open spec fn spec_min2(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 1 {\n 51\n } else if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n \n spec_min1(nums, k - 1)\n } else if (nums[k - 1] as int) < spec_min2(nums, k - 1) {\n nums[k - 1] as int\n } else {\n spec_min2(nums, k - 1)\n }\n}\n\npub open spec fn spec_two_smallest_sum(nums: Seq, k: int) -> int {\n spec_min1(nums, k) + spec_min2(nums, k)\n}\n\nimpl Solution {\n pub fn minimum_cost(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n \n result as int == spec_minimum_cost(nums@),\n \n exists |i: int, j: int|\n 1 <= i < j < nums.len()\n && result as int == split_cost(nums@, i, j),\n \n forall |i: int, j: int|\n 1 <= i < j < nums.len()\n ==> result as int <= split_cost(nums@, i, j),\n {\n let n = nums.len();\n let mut min1: i32 = 51;\n let mut min2: i32 = 51;\n let mut i: usize = 1;\n while i < n {\n if nums[i] < min1 {\n min2 = min1;\n min1 = nums[i];\n } else if nums[i] < min2 {\n min2 = nums[i];\n }\n i = i + 1;\n }\n nums[0] + min1 + min2\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\n\n\npub open spec fn split_cost(nums: Seq, i: int, j: int) -> int {\n nums[0] as int + nums[i] as int + nums[j] as int\n}\n\n\npub open spec fn spec_minimum_cost(nums: Seq) -> int {\n \n \n \n \n nums[0] as int + spec_two_smallest_sum(nums, nums.len() as int)\n}\n\n\n\npub open spec fn spec_min1(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 1 {\n 51 \n } else if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n nums[k - 1] as int\n } else {\n spec_min1(nums, k - 1)\n }\n}\n\npub open spec fn spec_min2(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 1 {\n 51\n } else if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n \n spec_min1(nums, k - 1)\n } else if (nums[k - 1] as int) < spec_min2(nums, k - 1) {\n nums[k - 1] as int\n } else {\n spec_min2(nums, k - 1)\n }\n}\n\npub open spec fn spec_two_smallest_sum(nums: Seq, k: int) -> int {\n spec_min1(nums, k) + spec_min2(nums, k)\n}\n\n\nproof fn lemma_min1_le_min2(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n spec_min1(nums, k) <= spec_min2(nums, k),\n decreases k,\n{\n if k <= 1 {\n } else {\n lemma_min1_le_min2(nums, k - 1);\n }\n}\n\n\nproof fn lemma_min1_bounds(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n 1 <= spec_min1(nums, k) <= 51,\n k >= 2 ==> 1 <= spec_min1(nums, k) <= 50,\n decreases k,\n{\n if k <= 1 {\n } else {\n lemma_min1_bounds(nums, k - 1);\n }\n}\n\n\nproof fn lemma_min2_bounds(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n 1 <= spec_min2(nums, k) <= 51,\n k >= 3 ==> 1 <= spec_min2(nums, k) <= 50,\n decreases k,\n{\n if k <= 1 {\n } else {\n lemma_min2_bounds(nums, k - 1);\n lemma_min1_bounds(nums, k - 1);\n }\n}\n\n\nproof fn lemma_min1_is_min(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n forall |j: int| 1 <= j < k ==> spec_min1(nums, k) <= #[trigger] (nums[j] as int),\n decreases k,\n{\n if k <= 1 {\n } else {\n lemma_min1_is_min(nums, k - 1);\n }\n}\n\n\nproof fn lemma_min1_exists(nums: Seq, k: int)\n requires\n 2 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n exists |j: int| 1 <= j < k && spec_min1(nums, k) == #[trigger] (nums[j] as int),\n decreases k,\n{\n if k <= 2 {\n \n \n \n assert(spec_min1(nums, 1) == 51);\n assert(nums[1] as int <= 50);\n assert((nums[1] as int) < spec_min1(nums, 1));\n assert(spec_min1(nums, 2) == nums[1] as int);\n } else {\n lemma_min1_exists(nums, k - 1);\n }\n}\n\n\nproof fn lemma_two_smallest_optimal(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n forall |i: int, j: int|\n 1 <= i < j < k ==>\n spec_min1(nums, k) + spec_min2(nums, k) <= (#[trigger] nums[i] as int) + (#[trigger] nums[j] as int),\n decreases k,\n{\n if k <= 2 {\n } else {\n lemma_two_smallest_optimal(nums, k - 1);\n lemma_min1_is_min(nums, k - 1);\n lemma_min1_le_min2(nums, k - 1);\n lemma_min1_is_min(nums, k);\n }\n}\n\n\n\nproof fn lemma_two_smallest_achievable(nums: Seq, k: int)\n requires\n 3 <= k <= nums.len() as int,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n exists |i: int, j: int|\n 1 <= i < j < k\n && spec_min1(nums, k) + spec_min2(nums, k) == (#[trigger] nums[i] as int) + (#[trigger] nums[j] as int),\n decreases k,\n{\n if k == 3 {\n \n \n assert(spec_min1(nums, 1) == 51);\n assert(spec_min2(nums, 1) == 51);\n assert((nums[1] as int) < 51);\n assert(spec_min1(nums, 2) == nums[1] as int);\n assert(spec_min2(nums, 2) == 51);\n \n if (nums[2] as int) < (nums[1] as int) {\n assert(spec_min1(nums, 3) == nums[2] as int);\n assert(spec_min2(nums, 3) == nums[1] as int);\n } else {\n assert(spec_min1(nums, 3) == nums[1] as int);\n assert((nums[2] as int) < spec_min2(nums, 2));\n assert(spec_min2(nums, 3) == nums[2] as int);\n }\n assert(spec_min1(nums, 3) + spec_min2(nums, 3) == (nums[1] as int) + (nums[2] as int));\n } else {\n lemma_two_smallest_achievable(nums, k - 1);\n lemma_min1_exists(nums, k - 1);\n \n \n \n \n \n \n \n \n \n \n if (nums[k - 1] as int) < spec_min1(nums, k - 1) {\n \n let j0 = choose |j: int| 1 <= j < k - 1 && spec_min1(nums, k - 1) == #[trigger] (nums[j] as int);\n assert(1 <= j0 < k - 1);\n \n \n assert(spec_min1(nums, k) + spec_min2(nums, k) == (nums[k - 1] as int) + (nums[j0] as int));\n if j0 < k - 1 {\n assert(1 <= j0 && j0 < k - 1 && k - 1 < k);\n }\n } else if (nums[k - 1] as int) < spec_min2(nums, k - 1) {\n let j0 = choose |j: int| 1 <= j < k - 1 && spec_min1(nums, k - 1) == #[trigger] (nums[j] as int);\n assert(1 <= j0 < k - 1);\n assert(spec_min1(nums, k) + spec_min2(nums, k) == (nums[j0] as int) + (nums[k - 1] as int));\n } else {\n \n }\n }\n}\n\nimpl Solution {\n pub fn minimum_cost(nums: Vec) -> (result: i32)\n requires\n 3 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n \n result as int == spec_minimum_cost(nums@),\n \n exists |i: int, j: int|\n 1 <= i < j < nums.len()\n && result as int == split_cost(nums@, i, j),\n \n forall |i: int, j: int|\n 1 <= i < j < nums.len()\n ==> result as int <= split_cost(nums@, i, j),\n {\n let n = nums.len();\n let mut min1: i32 = 51;\n let mut min2: i32 = 51;\n let mut i: usize = 1;\n while i < n\n invariant\n n == nums.len(),\n 3 <= n <= 50,\n 1 <= i <= n,\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 50,\n min1 as int == spec_min1(nums@, i as int),\n min2 as int == spec_min2(nums@, i as int),\n 1 <= min1 <= 51,\n 1 <= min2 <= 51,\n min1 <= min2,\n decreases n - i,\n {\n proof {\n lemma_min1_bounds(nums@, (i + 1) as int);\n lemma_min2_bounds(nums@, (i + 1) as int);\n lemma_min1_le_min2(nums@, (i + 1) as int);\n }\n if nums[i] < min1 {\n min2 = min1;\n min1 = nums[i];\n } else if nums[i] < min2 {\n min2 = nums[i];\n }\n i = i + 1;\n }\n proof {\n lemma_two_smallest_optimal(nums@, n as int);\n lemma_two_smallest_achievable(nums@, n as int);\n \n assert forall |i: int, j: int|\n 1 <= i < j < nums.len()\n implies\n (nums[0] as int + min1 as int + min2 as int) <= split_cost(nums@, i, j)\n by {\n assert(spec_min1(nums@, n as int) + spec_min2(nums@, n as int) <= (nums[i] as int) + (nums[j] as int));\n }\n \n let (wi, wj) = choose |i: int, j: int|\n 1 <= i < j < n\n && spec_min1(nums@, n as int) + spec_min2(nums@, n as int) == (#[trigger] nums[i] as int) + (#[trigger] nums[j] as int);\n assert(1 <= wi < wj < nums.len());\n assert((nums[0] as int + min1 as int + min2 as int) == split_cost(nums@, wi, wj));\n }\n nums[0] + min1 + min2\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc3010/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3010/tests/mutated_testcases.jsonl"} {"problem": "lc3011", "family": "leetcode", "description": "# Find if Array Can Be Sorted\n\nYou are given a **0-indexed** array of **positive** integers `nums`.\n\nIn one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero).\n\nReturn `true` if you can sort the array in ascending order; otherwise, return `false`.\n\n## Example 1:\n\n> **Input:** nums = [8,4,2,30,15] \n> **Output:** true \n> **Explanation:** \n> The numbers 2, 4, and 8 each have one set bit. \n> The numbers 15 and 30 each have four set bits. \n> We can sort using valid adjacent swaps and obtain `[2,4,8,15,30]`.\n\n## Example 2:\n\n> **Input:** nums = [1,2,3,4,5] \n> **Output:** true\n\n## Example 3:\n\n> **Input:** nums = [3,16,8,4,2] \n> **Output:** false\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `1 <= nums[i] <= 2^8`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn can_sort_array(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nArray, Bit Manipulation, Sorting\nAcceptance rate: 66.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn popcount_helper(x: int, acc: int) -> int\n decreases x,\n {\n if x <= 0 {\n acc\n } else {\n Self::popcount_helper(x / 2, acc + (x % 2))\n }\n }\n\n pub open spec fn popcount(x: int) -> int {\n Self::popcount_helper(x, 0)\n }\n\n pub open spec fn scan_spec(\n nums: Seq,\n i: int,\n has_prev: bool,\n prev_max: int,\n curr_bits: int,\n curr_min: int,\n curr_max: int,\n ) -> bool\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n !has_prev || prev_max <= curr_min\n } else {\n let x = nums[i] as int;\n let b = Self::popcount(x);\n if b == curr_bits {\n Self::scan_spec(\n nums,\n i + 1,\n has_prev,\n prev_max,\n curr_bits,\n Self::min_int(curr_min, x),\n Self::max_int(curr_max, x),\n )\n } else {\n (!has_prev || prev_max <= curr_min)\n && Self::scan_spec(nums, i + 1, true, curr_max, b, x, x)\n }\n }\n }\n\n pub open spec fn can_sort_array_spec(nums: Seq) -> bool {\n if nums.len() == 0 {\n true\n } else {\n let x0 = nums[0] as int;\n Self::scan_spec(nums, 1, false, 0, Self::popcount(x0), x0, x0)\n }\n }\n\n pub fn can_sort_array(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 256,\n ensures\n result <==> Self::can_sort_array_spec(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn popcount_exec(x: i32) -> i32 {\n let mut y = x as u32;\n let mut bits = 0i32;\n while y > 0 {\n bits += (y % 2) as i32;\n y /= 2;\n }\n bits\n }\n\n pub fn can_sort_array(nums: Vec) -> bool {\n let n = nums.len();\n let mut i: usize = 1;\n let mut prev_max: i32 = 0;\n let mut has_prev: bool = false;\n\n let mut curr_bits: i32 = Self::popcount_exec(nums[0]);\n let mut curr_min: i32 = nums[0];\n let mut curr_max: i32 = nums[0];\n\n let result = Self::can_sort_array_impl(&nums, n, i, prev_max, has_prev, curr_bits, curr_min, curr_max);\n result\n }\n\n fn can_sort_array_impl(\n nums: &Vec,\n n: usize,\n i: usize,\n prev_max: i32,\n has_prev: bool,\n curr_bits: i32,\n curr_min: i32,\n curr_max: i32,\n ) -> bool {\n if i >= n {\n !has_prev || prev_max <= curr_min\n } else {\n let x = nums[i];\n let b = Self::popcount_exec(x);\n\n if b == curr_bits {\n let mut next_curr_min = curr_min;\n let mut next_curr_max = curr_max;\n if x < next_curr_min {\n next_curr_min = x;\n }\n if x > next_curr_max {\n next_curr_max = x;\n }\n Self::can_sort_array_impl(nums, n, i + 1, prev_max, has_prev, curr_bits, next_curr_min, next_curr_max)\n } else {\n if has_prev && prev_max > curr_min {\n false\n } else {\n Self::can_sort_array_impl(nums, n, i + 1, curr_max, true, b, x, x)\n }\n }\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn popcount_helper(x: int, acc: int) -> int\n decreases x,\n {\n if x <= 0 {\n acc\n } else {\n Self::popcount_helper(x / 2, acc + (x % 2))\n }\n }\n\n pub open spec fn popcount(x: int) -> int {\n Self::popcount_helper(x, 0)\n }\n\n pub open spec fn scan_spec(\n nums: Seq,\n i: int,\n has_prev: bool,\n prev_max: int,\n curr_bits: int,\n curr_min: int,\n curr_max: int,\n ) -> bool\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n !has_prev || prev_max <= curr_min\n } else {\n let x = nums[i] as int;\n let b = Self::popcount(x);\n if b == curr_bits {\n Self::scan_spec(\n nums,\n i + 1,\n has_prev,\n prev_max,\n curr_bits,\n Self::min_int(curr_min, x),\n Self::max_int(curr_max, x),\n )\n } else {\n (!has_prev || prev_max <= curr_min)\n && Self::scan_spec(nums, i + 1, true, curr_max, b, x, x)\n }\n }\n }\n\n pub open spec fn can_sort_array_spec(nums: Seq) -> bool {\n if nums.len() == 0 {\n true\n } else {\n let x0 = nums[0] as int;\n Self::scan_spec(nums, 1, false, 0, Self::popcount(x0), x0, x0)\n }\n }\n\n fn popcount_exec(x: i32) -> (bits: i32)\n requires\n 0 <= x <= 256,\n ensures\n bits as int == Self::popcount(x as int),\n {\n let mut y = x as u32;\n let mut bits = 0i32;\n while y > 0 {\n bits += (y % 2) as i32;\n y /= 2;\n }\n bits\n }\n\n pub fn can_sort_array(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 256,\n ensures\n result <==> Self::can_sort_array_spec(nums@),\n {\n let n = nums.len();\n let mut i: usize = 1;\n let mut prev_max: i32 = 0;\n let mut has_prev: bool = false;\n\n let mut curr_bits: i32 = Self::popcount_exec(nums[0]);\n let mut curr_min: i32 = nums[0];\n let mut curr_max: i32 = nums[0];\n let result = Self::can_sort_array_impl(&nums, n, i, prev_max, has_prev, curr_bits, curr_min, curr_max);\n result\n }\n\n fn can_sort_array_impl(\n nums: &Vec,\n n: usize,\n i: usize,\n prev_max: i32,\n has_prev: bool,\n curr_bits: i32,\n curr_min: i32,\n curr_max: i32,\n ) -> (result: bool)\n requires\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= i <= n,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 256,\n 0 <= prev_max <= 256,\n 1 <= curr_min <= 256,\n 1 <= curr_max <= 256,\n Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int)\n == Self::can_sort_array_spec(nums@),\n ensures\n result <==> Self::can_sort_array_spec(nums@),\n {\n if i >= n {\n !has_prev || prev_max <= curr_min\n } else {\n let x = nums[i];\n let b = Self::popcount_exec(x);\n\n if b == curr_bits {\n let mut next_curr_min = curr_min;\n let mut next_curr_max = curr_max;\n if x < next_curr_min {\n next_curr_min = x;\n }\n if x > next_curr_max {\n next_curr_max = x;\n }\n Self::can_sort_array_impl(nums, n, i + 1, prev_max, has_prev, curr_bits, next_curr_min, next_curr_max)\n } else {\n if has_prev && prev_max > curr_min {\n false\n } else {\n Self::can_sort_array_impl(nums, n, i + 1, curr_max, true, b, x, x)\n }\n }\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn popcount_helper(x: int, acc: int) -> int\n decreases x,\n {\n if x <= 0 {\n acc\n } else {\n Self::popcount_helper(x / 2, acc + (x % 2))\n }\n }\n\n pub open spec fn popcount(x: int) -> int {\n Self::popcount_helper(x, 0)\n }\n\n proof fn lemma_popcount_helper_additive(x: int, acc: int)\n requires\n x >= 0,\n acc >= 0,\n ensures\n Self::popcount_helper(x, acc) == Self::popcount(x) + acc,\n Self::popcount_helper(x, acc) >= acc,\n decreases x,\n {\n if x > 0 {\n Self::lemma_popcount_helper_additive(x / 2, acc + (x % 2));\n Self::lemma_popcount_helper_additive(x / 2, x % 2);\n assert(x % 2 >= 0);\n }\n }\n\n proof fn lemma_popcount_step(x: int)\n requires\n x >= 0,\n ensures\n Self::popcount(x) == Self::popcount(x / 2) + (x % 2),\n {\n Self::lemma_popcount_helper_additive(x / 2, x % 2);\n }\n\n proof fn lemma_popcount_le(x: int)\n requires\n x >= 0,\n ensures\n Self::popcount(x) <= x,\n decreases x,\n {\n if x > 0 {\n Self::lemma_popcount_le(x / 2);\n Self::lemma_popcount_step(x);\n assert(0 <= x % 2 <= 1);\n assert(x / 2 + (x % 2) <= x) by (nonlinear_arith)\n requires\n x >= 0,\n {\n }\n }\n }\n\n pub open spec fn scan_spec(\n nums: Seq,\n i: int,\n has_prev: bool,\n prev_max: int,\n curr_bits: int,\n curr_min: int,\n curr_max: int,\n ) -> bool\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n !has_prev || prev_max <= curr_min\n } else {\n let x = nums[i] as int;\n let b = Self::popcount(x);\n if b == curr_bits {\n Self::scan_spec(\n nums,\n i + 1,\n has_prev,\n prev_max,\n curr_bits,\n Self::min_int(curr_min, x),\n Self::max_int(curr_max, x),\n )\n } else {\n (!has_prev || prev_max <= curr_min)\n && Self::scan_spec(nums, i + 1, true, curr_max, b, x, x)\n }\n }\n }\n\n pub open spec fn can_sort_array_spec(nums: Seq) -> bool {\n if nums.len() == 0 {\n true\n } else {\n let x0 = nums[0] as int;\n Self::scan_spec(nums, 1, false, 0, Self::popcount(x0), x0, x0)\n }\n }\n\n fn popcount_exec(x: i32) -> (bits: i32)\n requires\n 0 <= x <= 256,\n ensures\n bits as int == Self::popcount(x as int),\n {\n let ghost x0 = x as int;\n let mut y = x as u32;\n let mut bits = 0i32;\n while y > 0\n invariant\n 0 <= y as int <= x0 <= 256,\n 0 <= bits,\n Self::popcount_helper(y as int, bits as int) == Self::popcount(x0),\n decreases y as int,\n {\n let ghost old_y = y as int;\n let ghost old_bits = bits as int;\n let ghost bit = y % 2;\n proof {\n assert(0 < old_y);\n assert(0 <= old_y % 2 <= 1);\n Self::lemma_popcount_helper_additive(old_y, old_bits);\n Self::lemma_popcount_le(x0);\n assert(old_bits <= Self::popcount_helper(old_y, old_bits));\n assert(old_bits <= Self::popcount(x0));\n assert(old_bits <= x0);\n assert(old_bits + (old_y % 2) <= 257);\n assert(Self::popcount_helper(old_y, old_bits) == Self::popcount_helper(old_y / 2, old_bits + (old_y % 2)));\n assert(old_bits + (old_y % 2) >= 0);\n assert(old_bits + (bit as int) == old_bits + (old_y % 2));\n assert(0 <= (y % 2) as i32 <= 1);\n assert(bits <= 256);\n assert(bits + (y % 2) as i32 <= 257);\n }\n bits += (y % 2) as i32;\n y /= 2;\n proof {\n assert(y as int == old_y / 2);\n assert(bits as int == old_bits + (old_y % 2));\n assert(Self::popcount_helper(y as int, bits as int) == Self::popcount(x0));\n }\n }\n proof {\n assert(y == 0);\n assert(Self::popcount_helper(0, bits as int) == bits as int);\n assert(bits as int == Self::popcount(x0));\n }\n bits\n }\n\n pub fn can_sort_array(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 256,\n ensures\n result <==> Self::can_sort_array_spec(nums@),\n {\n let n = nums.len();\n let mut i: usize = 1;\n let mut prev_max: i32 = 0;\n let mut has_prev: bool = false;\n\n let mut curr_bits: i32 = Self::popcount_exec(nums[0]);\n let mut curr_min: i32 = nums[0];\n let mut curr_max: i32 = nums[0];\n\n let result = Self::can_sort_array_impl(&nums, n, i, prev_max, has_prev, curr_bits, curr_min, curr_max);\n result\n }\n\n fn can_sort_array_impl(\n nums: &Vec,\n n: usize,\n i: usize,\n prev_max: i32,\n has_prev: bool,\n curr_bits: i32,\n curr_min: i32,\n curr_max: i32,\n ) -> (result: bool)\n requires\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= i <= n,\n forall |k: int| 0 <= k < nums.len() ==> 1 <= #[trigger] nums[k] <= 256,\n 0 <= prev_max <= 256,\n 1 <= curr_min <= 256,\n 1 <= curr_max <= 256,\n Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int)\n == Self::can_sort_array_spec(nums@),\n ensures\n result <==> Self::can_sort_array_spec(nums@),\n decreases n - i,\n {\n if i >= n {\n proof {\n assert(i as int >= nums@.len());\n assert(Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int)\n == (!has_prev || prev_max as int <= curr_min as int));\n assert((!has_prev || prev_max <= curr_min) == (!has_prev || prev_max as int <= curr_min as int));\n assert((!has_prev || prev_max <= curr_min) == Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int));\n assert((!has_prev || prev_max <= curr_min) == Self::can_sort_array_spec(nums@));\n }\n !has_prev || prev_max <= curr_min\n } else {\n let x = nums[i];\n let b = Self::popcount_exec(x);\n if b == curr_bits {\n let mut next_curr_min = curr_min;\n let mut next_curr_max = curr_max;\n if x < next_curr_min {\n next_curr_min = x;\n }\n if x > next_curr_max {\n next_curr_max = x;\n }\n proof {\n assert(next_curr_min as int == Self::min_int(curr_min as int, x as int));\n assert(next_curr_max as int == Self::max_int(curr_max as int, x as int));\n assert(Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int)\n == Self::scan_spec(\n nums@,\n i as int + 1,\n has_prev,\n prev_max as int,\n curr_bits as int,\n next_curr_min as int,\n next_curr_max as int,\n ));\n }\n Self::can_sort_array_impl(nums, n, i + 1, prev_max, has_prev, curr_bits, next_curr_min, next_curr_max)\n } else {\n if has_prev && prev_max > curr_min {\n proof {\n assert((!has_prev || prev_max as int <= curr_min as int) == false);\n assert(Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int) == false);\n assert(Self::can_sort_array_spec(nums@) == false);\n }\n false\n } else {\n proof {\n assert(!has_prev || prev_max <= curr_min);\n assert(Self::scan_spec(nums@, i as int, has_prev, prev_max as int, curr_bits as int, curr_min as int, curr_max as int)\n == Self::scan_spec(nums@, i as int + 1, true, curr_max as int, b as int, x as int, x as int));\n }\n Self::can_sort_array_impl(nums, n, i + 1, curr_max, true, b, x, x)\n }\n }\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3011/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3011/tests/mutated_testcases.jsonl"} {"problem": "lc3028", "family": "leetcode", "description": "# Ant on the Boundary\n\nAn ant is on a boundary. It sometimes goes left and sometimes right.\n\nYou are given an array of **non-zero** integers `nums`. The ant starts reading `nums` from the first element of it to its end. At each step, it moves according to the value of the current element:\n\n- If `nums[i] < 0`, it moves **left** by `-nums[i]` units.\n- If `nums[i] > 0`, it moves **right** by `nums[i]` units.\n\nReturn *the number of times the ant **returns** to the boundary*.\n\n**Notes:**\n\n- There is an infinite space on both sides of the boundary.\n- We check whether the ant is on the boundary only **after** it has moved `|nums[i]|` units. In other words, if the ant crosses the boundary during its movement, it does not count.\n\n## Examples\n\n**Example 1:**\n\n**Input:** nums = [2,3,-5]\n\n**Output:** 1\n\n**Explanation:** After the first step, the ant is 2 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is on the boundary. So the answer is 1.\n\n**Example 2:**\n\n**Input:** nums = [3,2,-3,-4]\n\n**Output:** 0\n\n**Explanation:** After the first step, the ant is 3 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is 2 steps to the right of the boundary. After the fourth step, the ant is 2 steps to the left of the boundary. The ant never returned to the boundary, so the answer is 0.\n\n## Constraints\n\n- `1 <= nums.length <= 100`\n- `-10 <= nums[i] <= 10`\n- `nums[i] != 0`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn return_to_boundary_count(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nSimulation\nPrefix Sum\n74.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_sum(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n spec_prefix_sum(nums, k - 1) + nums[k - 1] as int\n }\n}\n\npub open spec fn spec_boundary_count(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else if spec_prefix_sum(nums, k) == 0 {\n spec_boundary_count(nums, k - 1) + 1\n } else {\n spec_boundary_count(nums, k - 1)\n }\n}\n\nimpl Solution {\n pub fn return_to_boundary_count(nums: Vec) -> (count: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -10 <= #[trigger] nums[i] <= 10,\n forall |i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] != 0,\n ensures\n count == spec_boundary_count(nums@, nums.len() as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn return_to_boundary_count(nums: Vec) -> i32 {\n let n = nums.len();\n let mut position: i32 = 0;\n let mut count: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n position += nums[i];\n if position == 0 {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_sum(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n spec_prefix_sum(nums, k - 1) + nums[k - 1] as int\n }\n}\n\npub open spec fn spec_boundary_count(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else if spec_prefix_sum(nums, k) == 0 {\n spec_boundary_count(nums, k - 1) + 1\n } else {\n spec_boundary_count(nums, k - 1)\n }\n}\n\nimpl Solution {\n pub fn return_to_boundary_count(nums: Vec) -> (count: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -10 <= #[trigger] nums[i] <= 10,\n forall |i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] != 0,\n ensures\n count == spec_boundary_count(nums@, nums.len() as int),\n {\n let n = nums.len();\n let mut position: i32 = 0;\n let mut count: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n position += nums[i];\n if position == 0 {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_prefix_sum(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n spec_prefix_sum(nums, k - 1) + nums[k - 1] as int\n }\n}\n\npub open spec fn spec_boundary_count(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else if spec_prefix_sum(nums, k) == 0 {\n spec_boundary_count(nums, k - 1) + 1\n } else {\n spec_boundary_count(nums, k - 1)\n }\n}\n\nproof fn lemma_prefix_sum_bounds(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -10 <= #[trigger] nums[i] <= 10,\n ensures\n -10 * k <= spec_prefix_sum(nums, k) <= 10 * k,\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_prefix_sum_bounds(nums, k - 1);\n assert(-10 * (k - 1) - 10 == -10 * k) by(nonlinear_arith);\n assert(10 * (k - 1) + 10 == 10 * k) by(nonlinear_arith);\n }\n}\n\nproof fn lemma_boundary_count_bounds(nums: Seq, k: int)\n requires\n 0 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -10 <= #[trigger] nums[i] <= 10,\n ensures\n 0 <= spec_boundary_count(nums, k) <= k,\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_boundary_count_bounds(nums, k - 1);\n }\n}\n\nimpl Solution {\n pub fn return_to_boundary_count(nums: Vec) -> (count: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -10 <= #[trigger] nums[i] <= 10,\n forall |i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] != 0,\n ensures\n count == spec_boundary_count(nums@, nums.len() as int),\n {\n let n = nums.len();\n let mut position: i32 = 0;\n let mut count: i32 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n forall |j: int| 0 <= j < nums.len() ==> -10 <= #[trigger] nums[j] <= 10,\n 0 <= i <= n,\n position as int == spec_prefix_sum(nums@, i as int),\n -1000 <= position <= 1000,\n count as int == spec_boundary_count(nums@, i as int),\n 0 <= count <= i as i32,\n decreases n - i,\n {\n proof {\n lemma_prefix_sum_bounds(nums@, (i + 1) as int);\n lemma_boundary_count_bounds(nums@, (i + 1) as int);\n }\n position += nums[i];\n if position == 0 {\n count += 1;\n }\n i += 1;\n }\n count\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc3028/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3028/tests/mutated_testcases.jsonl"} {"problem": "lc3040", "family": "leetcode", "description": "# Maximum Number of Operations With the Same Score II\n\nGiven an array of integers `nums`, you can perform any of the following operations while `nums` contains at least 2 elements:\n\n- Choose the first two elements of `nums` and delete them.\n- Choose the last two elements of `nums` and delete them.\n- Choose the first and the last elements of `nums` and delete them.\n\nThe score of the operation is the sum of the deleted elements.\n\nYour task is to find the maximum number of operations that can be performed such that all operations have the same score.\n\nReturn the maximum number of operations possible that satisfy the condition above.\n\n## Example 1\n\n**Input:** nums = [3,2,1,2,3,4] \n**Output:** 3 \n**Explanation:** We perform the following operations:\n- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].\n- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].\n- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].\nWe are unable to perform any more operations as nums is empty.\n\n## Example 2\n\n**Input:** nums = [3,2,6,1,4] \n**Output:** 2 \n**Explanation:** We perform the following operations:\n- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].\n- Delete the last two elements, with score 1 + 4 = 5, nums = [6].\nIt can be proven that we can perform at most 2 operations.\n\n## Constraints\n\n- `2 <= nums.length <= 2000`\n- `1 <= nums[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_operations(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming, Memoization\nAcceptance rate: 34.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn best(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn best3(a: int, b: int, c: int) -> int {\n best(a, best(b, c))\n}\n\npub open spec fn interval_ops(nums: Seq, l: int, r: int, target: int) -> int\n decreases if l <= r { r - l + 1 } else { 0 },\n{\n if l >= r {\n 0\n } else {\n let a = if l + 1 <= r && (nums[l] as int + nums[l + 1] as int == target) {\n 1 + interval_ops(nums, l + 2, r, target)\n } else {\n 0\n };\n let b = if nums[l] as int + nums[r] as int == target {\n 1 + interval_ops(nums, l + 1, r - 1, target)\n } else {\n 0\n };\n let c = if l <= r - 1 && (nums[r - 1] as int + nums[r] as int == target) {\n 1 + interval_ops(nums, l, r - 2, target)\n } else {\n 0\n };\n best3(a, b, c)\n }\n}\n\npub open spec fn max_operations_spec(nums: Seq) -> int {\n let n = nums.len() as int;\n let s1 = nums[0] as int + nums[1] as int;\n let s2 = nums[0] as int + nums[n - 1] as int;\n let s3 = nums[n - 2] as int + nums[n - 1] as int;\n best3(\n 1 + interval_ops(nums, 2, n - 1, s1),\n 1 + interval_ops(nums, 1, n - 2, s2),\n 1 + interval_ops(nums, 0, n - 3, s3),\n )\n}\n\nimpl Solution {\n pub fn max_operations(nums: Vec) -> (result: i32)\n requires\n 2 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == max_operations_spec(nums@),\n {\n }\n}\n\n}\n", "code": "fn best_exec(a: i32, b: i32) -> i32 {\n if a >= b { a } else { b }\n}\n\nfn best3_exec(a: i32, b: i32, c: i32) -> i32 {\n best_exec(a, best_exec(b, c))\n}\n\nfn solve_fixed(nums: &Vec, target: i32) -> Vec {\n let n = nums.len();\n let nn: usize = n * n;\n let mut dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < nn {\n dp.push(0);\n idx += 1;\n }\n let mut len: usize = 2;\n while len <= n {\n let bound: usize = n - len;\n let mut l: usize = 0;\n while l <= bound {\n let r = l + len - 1;\n let mut a: i32 = 0;\n if nums[l] + nums[l + 1] == target {\n let child: i32;\n if len > 3 {\n child = dp[(l + 2) * n + r];\n } else {\n child = 0;\n }\n a = 1 + child;\n }\n let mut b: i32 = 0;\n if nums[l] + nums[r] == target {\n let child: i32;\n if len > 3 {\n child = dp[(l + 1) * n + (r - 1)];\n } else {\n child = 0;\n }\n b = 1 + child;\n }\n let mut c: i32 = 0;\n if nums[r - 1] + nums[r] == target {\n let child: i32;\n if len > 3 {\n child = dp[l * n + (r - 2)];\n } else {\n child = 0;\n }\n c = 1 + child;\n }\n let val = best3_exec(a, b, c);\n dp[l * n + r] = val;\n l += 1;\n }\n len += 1;\n }\n dp\n}\n\nimpl Solution {\n pub fn max_operations(nums: Vec) -> i32 {\n let n = nums.len();\n if n <= 3 {\n return 1;\n }\n let s1 = nums[0] + nums[1];\n let s2 = nums[0] + nums[n - 1];\n let s3 = nums[n - 2] + nums[n - 1];\n let dp1 = solve_fixed(&nums, s1);\n let dp2 = solve_fixed(&nums, s2);\n let dp3 = solve_fixed(&nums, s3);\n let a = 1 + dp1[2 * n + (n - 1)];\n let b = 1 + dp2[1 * n + (n - 2)];\n let c = 1 + dp3[0 * n + (n - 3)];\n let ans = best3_exec(a, b, c);\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn best(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn best3(a: int, b: int, c: int) -> int {\n best(a, best(b, c))\n}\n\npub open spec fn interval_ops(nums: Seq, l: int, r: int, target: int) -> int\n decreases if l <= r { r - l + 1 } else { 0 },\n{\n if l >= r {\n 0\n } else {\n let a = if l + 1 <= r && (nums[l] as int + nums[l + 1] as int == target) {\n 1 + interval_ops(nums, l + 2, r, target)\n } else {\n 0\n };\n let b = if nums[l] as int + nums[r] as int == target {\n 1 + interval_ops(nums, l + 1, r - 1, target)\n } else {\n 0\n };\n let c = if l <= r - 1 && (nums[r - 1] as int + nums[r] as int == target) {\n 1 + interval_ops(nums, l, r - 2, target)\n } else {\n 0\n };\n best3(a, b, c)\n }\n}\n\npub open spec fn max_operations_spec(nums: Seq) -> int {\n let n = nums.len() as int;\n let s1 = nums[0] as int + nums[1] as int;\n let s2 = nums[0] as int + nums[n - 1] as int;\n let s3 = nums[n - 2] as int + nums[n - 1] as int;\n best3(\n 1 + interval_ops(nums, 2, n - 1, s1),\n 1 + interval_ops(nums, 1, n - 2, s2),\n 1 + interval_ops(nums, 0, n - 3, s3),\n )\n}\n\nfn best_exec(a: i32, b: i32) -> (result: i32)\n ensures result as int == best(a as int, b as int),\n{\n if a >= b { a } else { b }\n}\n\nfn best3_exec(a: i32, b: i32, c: i32) -> (result: i32)\n ensures result as int == best3(a as int, b as int, c as int),\n{\n best_exec(a, best_exec(b, c))\n}\n\nfn solve_fixed(nums: &Vec, target: i32) -> (dp: Vec)\n requires\n 2 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n dp.len() == nums.len() * nums.len(),\n forall |l: int, r: int| 0 <= l <= r < nums.len() ==>\n #[trigger] dp[l * nums.len() as int + r] == interval_ops(nums@, l, r, target as int)\n && 0 <= dp[l * nums.len() as int + r] <= r - l + 1,\n{\n let n = nums.len();\n let nn: usize = n * n;\n let mut dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < nn {\n dp.push(0);\n idx += 1;\n }\n let mut len: usize = 2;\n while len <= n {\n let bound: usize = n - len;\n let mut l: usize = 0;\n while l <= bound {\n let r = l + len - 1;\n let mut a: i32 = 0;\n if nums[l] + nums[l + 1] == target {\n let child: i32;\n if len > 3 {\n child = dp[(l + 2) * n + r];\n } else {\n child = 0;\n }\n a = 1 + child;\n }\n let mut b: i32 = 0;\n if nums[l] + nums[r] == target {\n let child: i32;\n if len > 3 {\n child = dp[(l + 1) * n + (r - 1)];\n } else {\n child = 0;\n }\n b = 1 + child;\n }\n let mut c: i32 = 0;\n if nums[r - 1] + nums[r] == target {\n let child: i32;\n if len > 3 {\n child = dp[l * n + (r - 2)];\n } else {\n child = 0;\n }\n c = 1 + child;\n }\n let val = best3_exec(a, b, c);\n dp.set(l * n + r, val);\n l += 1;\n }\n len += 1;\n }\n dp\n}\n\nimpl Solution {\n pub fn max_operations(nums: Vec) -> (result: i32)\n requires\n 2 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == max_operations_spec(nums@),\n {\n let n = nums.len();\n if n <= 3 {\n return 1;\n }\n let s1 = nums[0] + nums[1];\n let s2 = nums[0] + nums[n - 1];\n let s3 = nums[n - 2] + nums[n - 1];\n let dp1 = solve_fixed(&nums, s1);\n let dp2 = solve_fixed(&nums, s2);\n let dp3 = solve_fixed(&nums, s3);\n let a = 1 + dp1[2 * n + (n - 1)];\n let b = 1 + dp2[1 * n + (n - 2)];\n let c = 1 + dp3[0 * n + (n - 3)];\n let ans = best3_exec(a, b, c);\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn best(a: int, b: int) -> int {\n if a >= b { a } else { b }\n}\n\npub open spec fn best3(a: int, b: int, c: int) -> int {\n best(a, best(b, c))\n}\n\npub open spec fn interval_ops(nums: Seq, l: int, r: int, target: int) -> int\n decreases if l <= r { r - l + 1 } else { 0 },\n{\n if l >= r {\n 0\n } else {\n let a = if l + 1 <= r && (nums[l] as int + nums[l + 1] as int == target) {\n 1 + interval_ops(nums, l + 2, r, target)\n } else {\n 0\n };\n let b = if nums[l] as int + nums[r] as int == target {\n 1 + interval_ops(nums, l + 1, r - 1, target)\n } else {\n 0\n };\n let c = if l <= r - 1 && (nums[r - 1] as int + nums[r] as int == target) {\n 1 + interval_ops(nums, l, r - 2, target)\n } else {\n 0\n };\n best3(a, b, c)\n }\n}\n\npub open spec fn max_operations_spec(nums: Seq) -> int {\n let n = nums.len() as int;\n let s1 = nums[0] as int + nums[1] as int;\n let s2 = nums[0] as int + nums[n - 1] as int;\n let s3 = nums[n - 2] as int + nums[n - 1] as int;\n best3(\n 1 + interval_ops(nums, 2, n - 1, s1),\n 1 + interval_ops(nums, 1, n - 2, s2),\n 1 + interval_ops(nums, 0, n - 3, s3),\n )\n}\n\nproof fn lemma_interval_ops_bound(nums: Seq, l: int, r: int, target: int)\n requires 0 <= l, r < nums.len(),\n ensures 0 <= interval_ops(nums, l, r, target) <= (if l <= r { r - l + 1 } else { 0 }),\n decreases if l <= r { r - l + 1 } else { 0 },\n{\n if l < r {\n if l + 1 <= r {\n lemma_interval_ops_bound(nums, l + 2, r, target);\n }\n lemma_interval_ops_bound(nums, l + 1, r - 1, target);\n if l <= r - 1 {\n lemma_interval_ops_bound(nums, l, r - 2, target);\n }\n }\n}\n\nproof fn lemma_max_operations_spec_small(nums: Seq)\n requires 2 <= nums.len() <= 3,\n ensures max_operations_spec(nums) == 1,\n{\n let n = nums.len() as int;\n assert(interval_ops(nums, 2, n - 1, nums[0] as int + nums[1] as int) == 0);\n assert(interval_ops(nums, 1, n - 2, nums[0] as int + nums[n - 1] as int) == 0);\n assert(interval_ops(nums, 0, n - 3, nums[n - 2] as int + nums[n - 1] as int) == 0);\n}\n\nproof fn lemma_flat_index_injective(n: int, l: int, r: int, l2: int, r2: int)\n requires\n 0 <= l < n, 0 <= r < n, 0 <= l2 < n, 0 <= r2 < n,\n l * n + r == l2 * n + r2,\n ensures l == l2 && r == r2,\n{\n if l != l2 {\n if l < l2 {\n assert((l2 - l) * n == r - r2) by (nonlinear_arith)\n requires l * n + r == l2 * n + r2;\n assert(l2 - l >= 1);\n assert((l2 - l) * n >= n) by (nonlinear_arith)\n requires l2 - l >= 1, n >= 1;\n assert(false);\n } else {\n assert((l - l2) * n == r2 - r) by (nonlinear_arith)\n requires l * n + r == l2 * n + r2;\n assert(l - l2 >= 1);\n assert((l - l2) * n >= n) by (nonlinear_arith)\n requires l - l2 >= 1, n >= 1;\n assert(false);\n }\n }\n}\n\nfn best_exec(a: i32, b: i32) -> (result: i32)\n ensures result as int == best(a as int, b as int),\n{\n if a >= b { a } else { b }\n}\n\nfn best3_exec(a: i32, b: i32, c: i32) -> (result: i32)\n ensures result as int == best3(a as int, b as int, c as int),\n{\n best_exec(a, best_exec(b, c))\n}\n\nfn solve_fixed(nums: &Vec, target: i32) -> (dp: Vec)\n requires\n 2 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n dp.len() == nums.len() * nums.len(),\n forall |l: int, r: int| 0 <= l <= r < nums.len() ==>\n #[trigger] dp[l * nums.len() as int + r] == interval_ops(nums@, l, r, target as int)\n && 0 <= dp[l * nums.len() as int + r] <= r - l + 1,\n{\n let n = nums.len();\n proof {\n assert(n * n <= 2000 * 2000) by (nonlinear_arith)\n requires n <= 2000;\n }\n let nn: usize = n * n;\n let mut dp: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < nn\n invariant\n idx <= nn,\n nn == n * n,\n dp.len() == idx,\n forall |k: int| 0 <= k < idx as int ==> dp[k] == 0,\n decreases nn - idx,\n {\n dp.push(0);\n idx += 1;\n }\n proof {\n assert forall |l: int, r: int| 0 <= l <= r < n as int && r - l + 1 < 2 implies\n #[trigger] dp[l * n as int + r] == interval_ops(nums@, l, r, target as int) by {\n assert(l == r);\n assert(l * n as int + l < n as int * n as int) by (nonlinear_arith)\n requires 0 <= l < n as int;\n assert(dp[l * n as int + r] == 0);\n assert(interval_ops(nums@, l, r, target as int) == 0);\n }\n }\n let mut len: usize = 2;\n while len <= n\n invariant\n 2 <= len <= n + 1,\n n <= 2000,\n n == nums.len(),\n forall |i: int| 0 <= i < n as int ==> 1 <= #[trigger] nums[i] <= 1000,\n nn == n * n,\n dp.len() == nn,\n forall |l: int, r: int| 0 <= l <= r < n as int && r - l + 1 < len as int ==>\n #[trigger] dp[l * n as int + r] == interval_ops(nums@, l, r, target as int),\n decreases n + 1 - len,\n {\n let bound: usize = n - len;\n let mut l: usize = 0;\n while l <= bound\n invariant\n l <= bound + 1,\n bound == n - len,\n bound <= 2000,\n n <= 2000,\n n == nums.len(),\n forall |i: int| 0 <= i < n as int ==> 1 <= #[trigger] nums[i] <= 1000,\n 2 <= len <= n,\n nn == n * n,\n dp.len() == nn,\n forall |l2: int, r2: int| 0 <= l2 <= r2 < n as int\n && (r2 - l2 + 1 < len as int\n || (r2 - l2 + 1 == len as int && l2 < l as int)) ==>\n #[trigger] dp[l2 * n as int + r2] == interval_ops(nums@, l2, r2, target as int),\n decreases (bound + 1) - l,\n {\n let r = l + len - 1;\n proof {\n assert(l < n);\n assert(l + 1 <= r);\n assert(r < n);\n assert(r >= 1);\n }\n proof {\n if len > 3 {\n assert(l + 2 < n);\n assert(r >= 2);\n }\n }\n let mut a: i32 = 0;\n if nums[l] + nums[l + 1] == target {\n let child: i32;\n if len > 3 {\n proof {\n assert((l + 2) * n + r < nn) by (nonlinear_arith)\n requires l + 2 < n, r < n, nn == n * n;\n }\n child = dp[(l + 2) * n + r];\n proof {\n assert((l + 2) * n + r == (l as int + 2) * n as int + r as int) by (nonlinear_arith);\n assert(child as int == interval_ops(nums@, l as int + 2, r as int, target as int));\n lemma_interval_ops_bound(nums@, l as int + 2, r as int, target as int);\n assert(child as int <= r as int - (l as int + 2) + 1);\n }\n } else {\n child = 0;\n proof {\n assert(l as int + 2 >= r as int);\n assert(interval_ops(nums@, l as int + 2, r as int, target as int) == 0);\n }\n }\n proof {\n assert(child as int <= n as int);\n }\n a = 1 + child;\n }\n let mut b: i32 = 0;\n if nums[l] + nums[r] == target {\n let child: i32;\n if len > 3 {\n proof {\n assert((l + 1) * n + (r - 1) < nn) by (nonlinear_arith)\n requires l + 1 < n, r - 1 < n, nn == n * n;\n }\n child = dp[(l + 1) * n + (r - 1)];\n proof {\n assert((l + 1) * n + (r - 1) == (l as int + 1) * n as int + (r as int - 1))\n by (nonlinear_arith);\n assert(child as int == interval_ops(nums@, l as int + 1, r as int - 1, target as int));\n lemma_interval_ops_bound(nums@, l as int + 1, r as int - 1, target as int);\n assert(child as int <= (r as int - 1) - (l as int + 1) + 1);\n }\n } else {\n child = 0;\n proof {\n assert(interval_ops(nums@, l as int + 1, r as int - 1, target as int) == 0);\n }\n }\n proof {\n assert(child as int <= n as int);\n }\n b = 1 + child;\n }\n let mut c: i32 = 0;\n if nums[r - 1] + nums[r] == target {\n let child: i32;\n if len > 3 {\n proof {\n assert(l * n + (r - 2) < nn) by (nonlinear_arith)\n requires l < n, r - 2 < n, nn == n * n;\n }\n child = dp[l * n + (r - 2)];\n proof {\n assert(l * n + (r - 2) == l as int * n as int + (r as int - 2)) by (nonlinear_arith);\n assert(child as int == interval_ops(nums@, l as int, r as int - 2, target as int));\n lemma_interval_ops_bound(nums@, l as int, r as int - 2, target as int);\n assert(child as int <= (r as int - 2) - l as int + 1);\n }\n } else {\n child = 0;\n proof {\n assert(interval_ops(nums@, l as int, r as int - 2, target as int) == 0);\n }\n }\n proof {\n assert(child as int <= n as int);\n }\n c = 1 + child;\n }\n let val = best3_exec(a, b, c);\n proof {\n assert(val as int == best3(a as int, b as int, c as int));\n assert(interval_ops(nums@, l as int, r as int, target as int)\n == best3(a as int, b as int, c as int));\n }\n let ghost dp_before = dp@;\n proof {\n assert(l * n + r < nn) by (nonlinear_arith)\n requires l < n, r < n, nn == n * n;\n }\n dp.set(l * n + r, val);\n proof {\n assert(l * n + r == l as int * n as int + r as int) by (nonlinear_arith);\n assert(dp@ == dp_before.update(l as int * n as int + r as int, val));\n assert forall |l2: int, r2: int| 0 <= l2 <= r2 < n as int\n && (r2 - l2 + 1 < len as int\n || (r2 - l2 + 1 == len as int && l2 < (l + 1) as int)) implies\n #[trigger] dp[l2 * n as int + r2] == interval_ops(nums@, l2, r2, target as int) by {\n if l2 == l as int && r2 == r as int {\n } else {\n if l2 * n as int + r2 == l as int * n as int + r as int {\n lemma_flat_index_injective(n as int, l as int, r as int, l2, r2);\n assert(false);\n }\n assert(l2 * n as int + r2 < nn as int) by (nonlinear_arith)\n requires l2 < n as int, r2 < n as int, nn == n * n;\n assert(dp[l2 * n as int + r2] == dp_before[l2 * n as int + r2]);\n }\n }\n }\n l += 1;\n }\n len += 1;\n }\n proof {\n assert forall |l: int, r: int| 0 <= l <= r < n as int implies\n 0 <= #[trigger] dp[l * n as int + r] <= r - l + 1 by {\n lemma_interval_ops_bound(nums@, l, r, target as int);\n }\n }\n dp\n}\n\nimpl Solution {\n pub fn max_operations(nums: Vec) -> (result: i32)\n requires\n 2 <= nums.len() <= 2000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == max_operations_spec(nums@),\n {\n let n = nums.len();\n if n <= 3 {\n proof {\n lemma_max_operations_spec_small(nums@);\n }\n return 1;\n }\n let s1 = nums[0] + nums[1];\n let s2 = nums[0] + nums[n - 1];\n let s3 = nums[n - 2] + nums[n - 1];\n let dp1 = solve_fixed(&nums, s1);\n let dp2 = solve_fixed(&nums, s2);\n let dp3 = solve_fixed(&nums, s3);\n proof {\n assert(dp1[2 * n as int + (n as int - 1)] == interval_ops(nums@, 2, n as int - 1, s1 as int)\n && dp1[2 * n as int + (n as int - 1)] <= n as int - 1 - 2 + 1);\n assert(dp2[1 * n as int + (n as int - 2)] == interval_ops(nums@, 1, n as int - 2, s2 as int)\n && dp2[1 * n as int + (n as int - 2)] <= n as int - 2 - 1 + 1);\n assert(dp3[0 * n as int + (n as int - 3)] == interval_ops(nums@, 0, n as int - 3, s3 as int)\n && dp3[0 * n as int + (n as int - 3)] <= n as int - 3 - 0 + 1);\n assert(2 * n + (n - 1) < n * n) by (nonlinear_arith)\n requires n >= 4;\n assert(1 * n + (n - 2) < n * n) by (nonlinear_arith)\n requires n >= 4;\n assert(0 * n + (n - 3) < n * n) by (nonlinear_arith)\n requires n >= 4;\n assert(2 * n as int + (n as int - 1) == 2 * n + (n - 1));\n assert(1 * n as int + (n as int - 2) == 1 * n + (n - 2));\n assert(0 * n as int + (n as int - 3) == 0 * n + (n - 3));\n }\n let a = 1 + dp1[2 * n + (n - 1)];\n let b = 1 + dp2[1 * n + (n - 2)];\n let c = 1 + dp3[0 * n + (n - 3)];\n let ans = best3_exec(a, b, c);\n proof {\n assert(ans as int == best3(a as int, b as int, c as int));\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3040/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3040/tests/mutated_testcases.jsonl"} {"problem": "lc3044", "family": "leetcode", "description": "# Most Frequent Prime\n\nYou are given an `m x n` 0-indexed 2D matrix `mat`. From every cell, you can create numbers in the following way:\n\n- There are at most 8 paths from the cell: east, south-east, south, south-west, west, north-west, north, and north-east.\n- Select one path and append digits along that path to the number being formed.\n- Numbers are generated at every step. For example, for digits `1, 9, 1`, the generated numbers are `1`, `19`, `191`.\n\nReturn the most frequent prime number greater than `10` among all numbers created by traversing the matrix, or `-1` if no such prime exists. If multiple primes have the same highest frequency, return the largest one.\n\n**Note:** You cannot change direction while moving.\n\n## Example 1:\n\n> **Input:** mat = [[1,1],[9,9],[1,1]]\n> **Output:** 19\n\n## Example 2:\n\n> **Input:** mat = [[7]]\n> **Output:** -1\n> **Explanation:** The only number formed is 7, which is prime but not greater than 10.\n\n## Example 3:\n\n> **Input:** mat = [[9,7,8],[4,6,5],[2,8,6]]\n> **Output:** 97\n\n## Constraints:\n\n- `m == mat.length`\n- `n == mat[i].length`\n- `1 <= m, n <= 6`\n- `1 <= mat[i][j] <= 9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn most_frequent_prime(mat: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Hash Table, Math, Matrix, Counting, Enumeration, Number Theory\nAcceptance rate: 45.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn check_no_divisor(n: int, d: int) -> bool {\n n % d != 0\n }\n\n pub open spec fn prime_int(n: int) -> bool {\n n > 1 && forall|d: int| 2 <= d && d * d <= n ==> #[trigger] Self::check_no_divisor(n, d)\n }\n\n pub open spec fn is_candidate_prime(n: int) -> bool {\n n > 10 && Self::prime_int(n)\n }\n\n pub open spec fn row_delta(d: int) -> int {\n if d == 0 {\n 0\n } else if d == 1 {\n 1\n } else if d == 2 {\n 1\n } else if d == 3 {\n 1\n } else if d == 4 {\n 0\n } else if d == 5 {\n -1\n } else if d == 6 {\n -1\n } else {\n -1\n }\n }\n\n pub open spec fn col_delta(d: int) -> int {\n if d == 0 {\n 1\n } else if d == 1 {\n 1\n } else if d == 2 {\n 0\n } else if d == 3 {\n -1\n } else if d == 4 {\n -1\n } else if d == 5 {\n -1\n } else if d == 6 {\n 0\n } else {\n 1\n }\n }\n\n pub open spec fn row_at(r: int, d: int, step: int) -> int {\n r + step * Self::row_delta(d)\n }\n\n pub open spec fn col_at(c: int, d: int, step: int) -> int {\n c + step * Self::col_delta(d)\n }\n\n pub open spec fn in_bounds(mat: Seq>, rr: int, cc: int) -> bool {\n mat.len() > 0 && 0 <= rr < mat.len() && 0 <= cc < mat[0].len() as int\n }\n\n pub open spec fn path_valid(mat: Seq>, r: int, c: int, d: int, step: int) -> bool {\n 0 <= step <= 5 && mat.len() > 0 && forall|t: int| 0 <= t <= step ==> Self::in_bounds(mat, Self::row_at(r, d, t), Self::col_at(c, d, t))\n }\n\n pub open spec fn number_on_path(mat: Seq>, r: int, c: int, d: int, step: int) -> int\n decreases step\n {\n if step <= 0 {\n mat[r][c] as int\n } else {\n Self::number_on_path(mat, r, c, d, step - 1) * 10 + mat[Self::row_at(r, d, step)][Self::col_at(c, d, step)] as int\n }\n }\n\n pub open spec fn direction_numbers_upto(mat: Seq>, r: int, c: int, d: int, step_bound: int) -> Seq\n decreases step_bound\n {\n if step_bound <= 0 {\n seq![]\n } else {\n let prev = Self::direction_numbers_upto(mat, r, c, d, step_bound - 1);\n if Self::path_valid(mat, r, c, d, step_bound) {\n prev.push(Self::number_on_path(mat, r, c, d, step_bound) as i32)\n } else {\n prev\n }\n }\n }\n\n pub open spec fn cell_numbers_upto(mat: Seq>, r: int, c: int, d_bound: int) -> Seq\n decreases d_bound\n {\n if d_bound <= 0 {\n seq![]\n } else {\n Self::cell_numbers_upto(mat, r, c, d_bound - 1) + Self::direction_numbers_upto(mat, r, c, d_bound - 1, 5)\n }\n }\n\n pub open spec fn all_numbers_upto(mat: Seq>, idx_bound: int) -> Seq\n decreases idx_bound\n {\n if idx_bound <= 0 || mat.len() == 0 || mat[0].len() == 0 {\n seq![]\n } else {\n let prev = Self::all_numbers_upto(mat, idx_bound - 1);\n let idx = idx_bound - 1;\n if 0 <= idx < (mat.len() * mat[0].len()) as int {\n prev + Self::cell_numbers_upto(mat, idx / (mat[0].len() as int), idx % (mat[0].len() as int), 8)\n } else {\n prev\n }\n }\n }\n\n pub open spec fn all_numbers(mat: Seq>) -> Seq {\n if mat.len() == 0 || mat[0].len() == 0 {\n seq![]\n } else {\n Self::all_numbers_upto(mat, (mat.len() * mat[0].len()) as int)\n }\n }\n\n pub open spec fn count_occurrences(s: Seq, value: i32) -> nat\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n Self::count_occurrences(s.drop_last(), value) + if s.last() == value { 1 as nat } else { 0 as nat }\n }\n }\n\n pub fn most_frequent_prime(mat: Vec>) -> (res: i32)\n requires\n 1 <= mat.len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> 1 <= #[trigger] mat[i].len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==> 1 <= #[trigger] mat[i][j] <= 9,\n ensures\n (res == -1) <==> (forall|k: int| 0 <= k < Self::all_numbers(mat@).len() ==> !Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[k] as int)),\n res != -1 ==> (Self::is_candidate_prime(res as int) &&\n forall|k: int| 0 <= k < Self::all_numbers(mat@).len() && Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[k] as int) ==>\n (Self::count_occurrences(Self::all_numbers(mat@), Self::all_numbers(mat@)[k]) < Self::count_occurrences(Self::all_numbers(mat@), res) ||\n (Self::count_occurrences(Self::all_numbers(mat@), Self::all_numbers(mat@)[k]) == Self::count_occurrences(Self::all_numbers(mat@), res) && Self::all_numbers(mat@)[k] <= res))),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn get_dx(d: usize) -> i32 {\n if d == 0 { 0 } else if d == 1 { 1 } else if d == 2 { 1 } else if d == 3 { 1 }\n else if d == 4 { 0 } else if d == 5 { -1 } else if d == 6 { -1 } else { -1 }\n }\n\n fn get_dy(d: usize) -> i32 {\n if d == 0 { 1 } else if d == 1 { 1 } else if d == 2 { 0 } else if d == 3 { -1 }\n else if d == 4 { -1 } else if d == 5 { -1 } else if d == 6 { 0 } else { 1 }\n }\n\n fn is_prime(num: i32) -> bool {\n if num <= 1 {\n return false;\n }\n let mut d = 2;\n while d * d <= num {\n if num % d == 0 {\n return false;\n }\n d += 1;\n }\n true\n }\n\n pub fn most_frequent_prime(mat: Vec>) -> i32 {\n let m = mat.len();\n let n = mat[0].len();\n let total = m * n;\n\n let mut nums: Vec = Vec::new();\n let mut idx = 0;\n while idx < total {\n let r = idx / n;\n let c = idx % n;\n let mut d = 0;\n while d < 8 {\n let mut rr = r as i32;\n let mut cc = c as i32;\n let mut cur = mat[r][c];\n let mut active = true;\n let mut step = 1;\n while step <= 5 {\n let dx_d = Self::get_dx(d);\n let dy_d = Self::get_dy(d);\n rr += dx_d;\n cc += dy_d;\n if active && 0 <= rr && rr < m as i32 && 0 <= cc && cc < n as i32 {\n cur = cur * 10 + mat[rr as usize][cc as usize];\n nums.push(cur);\n } else {\n active = false;\n }\n step += 1;\n }\n d += 1;\n }\n idx += 1;\n }\n\n let mut best = -1;\n let mut best_count: i32 = 0;\n let mut i = 0;\n while i < nums.len() {\n let val = nums[i];\n if val > 10 && Self::is_prime(val) {\n let mut count: i32 = 0;\n let mut j = 0;\n while j < nums.len() {\n if nums[j] == val {\n count += 1;\n }\n j += 1;\n }\n if count > best_count || (count == best_count && val > best) {\n best_count = count;\n best = val;\n }\n }\n i += 1;\n }\n\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn check_no_divisor(n: int, d: int) -> bool {\n n % d != 0\n }\n\n pub open spec fn prime_int(n: int) -> bool {\n n > 1 && forall|d: int| 2 <= d && d * d <= n ==> #[trigger] Self::check_no_divisor(n, d)\n }\n\n pub open spec fn is_candidate_prime(n: int) -> bool {\n n > 10 && Self::prime_int(n)\n }\n\n pub open spec fn row_delta(d: int) -> int {\n if d == 0 {\n 0\n } else if d == 1 {\n 1\n } else if d == 2 {\n 1\n } else if d == 3 {\n 1\n } else if d == 4 {\n 0\n } else if d == 5 {\n -1\n } else if d == 6 {\n -1\n } else {\n -1\n }\n }\n\n pub open spec fn col_delta(d: int) -> int {\n if d == 0 {\n 1\n } else if d == 1 {\n 1\n } else if d == 2 {\n 0\n } else if d == 3 {\n -1\n } else if d == 4 {\n -1\n } else if d == 5 {\n -1\n } else if d == 6 {\n 0\n } else {\n 1\n }\n }\n\n pub open spec fn row_at(r: int, d: int, step: int) -> int {\n r + step * Self::row_delta(d)\n }\n\n pub open spec fn col_at(c: int, d: int, step: int) -> int {\n c + step * Self::col_delta(d)\n }\n\n pub open spec fn in_bounds(mat: Seq>, rr: int, cc: int) -> bool {\n mat.len() > 0 && 0 <= rr < mat.len() && 0 <= cc < mat[0].len() as int\n }\n\n pub open spec fn path_valid(mat: Seq>, r: int, c: int, d: int, step: int) -> bool {\n 0 <= step <= 5 && mat.len() > 0 && forall|t: int| 0 <= t <= step ==> Self::in_bounds(mat, Self::row_at(r, d, t), Self::col_at(c, d, t))\n }\n\n pub open spec fn number_on_path(mat: Seq>, r: int, c: int, d: int, step: int) -> int\n decreases step\n {\n if step <= 0 {\n mat[r][c] as int\n } else {\n Self::number_on_path(mat, r, c, d, step - 1) * 10 + mat[Self::row_at(r, d, step)][Self::col_at(c, d, step)] as int\n }\n }\n\n pub open spec fn direction_numbers_upto(mat: Seq>, r: int, c: int, d: int, step_bound: int) -> Seq\n decreases step_bound\n {\n if step_bound <= 0 {\n seq![]\n } else {\n let prev = Self::direction_numbers_upto(mat, r, c, d, step_bound - 1);\n if Self::path_valid(mat, r, c, d, step_bound) {\n prev.push(Self::number_on_path(mat, r, c, d, step_bound) as i32)\n } else {\n prev\n }\n }\n }\n\n pub open spec fn cell_numbers_upto(mat: Seq>, r: int, c: int, d_bound: int) -> Seq\n decreases d_bound\n {\n if d_bound <= 0 {\n seq![]\n } else {\n Self::cell_numbers_upto(mat, r, c, d_bound - 1) + Self::direction_numbers_upto(mat, r, c, d_bound - 1, 5)\n }\n }\n\n pub open spec fn all_numbers_upto(mat: Seq>, idx_bound: int) -> Seq\n decreases idx_bound\n {\n if idx_bound <= 0 || mat.len() == 0 || mat[0].len() == 0 {\n seq![]\n } else {\n let prev = Self::all_numbers_upto(mat, idx_bound - 1);\n let idx = idx_bound - 1;\n if 0 <= idx < (mat.len() * mat[0].len()) as int {\n prev + Self::cell_numbers_upto(mat, idx / (mat[0].len() as int), idx % (mat[0].len() as int), 8)\n } else {\n prev\n }\n }\n }\n\n pub open spec fn all_numbers(mat: Seq>) -> Seq {\n if mat.len() == 0 || mat[0].len() == 0 {\n seq![]\n } else {\n Self::all_numbers_upto(mat, (mat.len() * mat[0].len()) as int)\n }\n }\n\n pub open spec fn count_occurrences(s: Seq, value: i32) -> nat\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n Self::count_occurrences(s.drop_last(), value) + if s.last() == value { 1 as nat } else { 0 as nat }\n }\n }\n\n fn get_dx(d: usize) -> (res: i32)\n requires 0 <= d < 8,\n ensures -1 <= res <= 1, res as int == Self::row_delta(d as int),\n {\n if d == 0 { 0 } else if d == 1 { 1 } else if d == 2 { 1 } else if d == 3 { 1 }\n else if d == 4 { 0 } else if d == 5 { -1 } else if d == 6 { -1 } else { -1 }\n }\n\n fn get_dy(d: usize) -> (res: i32)\n requires 0 <= d < 8,\n ensures -1 <= res <= 1, res as int == Self::col_delta(d as int),\n {\n if d == 0 { 1 } else if d == 1 { 1 } else if d == 2 { 0 } else if d == 3 { -1 }\n else if d == 4 { -1 } else if d == 5 { -1 } else if d == 6 { 0 } else { 1 }\n }\n\n fn is_prime(num: i32) -> (res: bool)\n requires\n num >= 0,\n num <= 999_999,\n ensures\n res == Self::prime_int(num as int),\n {\n if num <= 1 {\n return false;\n }\n let mut d = 2;\n while d * d <= num {\n if num % d == 0 {\n return false;\n }\n d += 1;\n }\n true\n }\n\n pub fn most_frequent_prime(mat: Vec>) -> (res: i32)\n requires\n 1 <= mat.len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> 1 <= #[trigger] mat[i].len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==> 1 <= #[trigger] mat[i][j] <= 9,\n ensures\n (res == -1) <==> (forall|k: int| 0 <= k < Self::all_numbers(mat@).len() ==> !Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[k] as int)),\n res != -1 ==> (Self::is_candidate_prime(res as int) &&\n forall|k: int| 0 <= k < Self::all_numbers(mat@).len() && Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[k] as int) ==>\n (Self::count_occurrences(Self::all_numbers(mat@), Self::all_numbers(mat@)[k]) < Self::count_occurrences(Self::all_numbers(mat@), res) ||\n (Self::count_occurrences(Self::all_numbers(mat@), Self::all_numbers(mat@)[k]) == Self::count_occurrences(Self::all_numbers(mat@), res) && Self::all_numbers(mat@)[k] <= res))),\n {\n let m = mat.len();\n let n = mat[0].len();\n let total = m * n;\n\n let mut nums: Vec = Vec::new();\n let mut idx = 0;\n while idx < total {\n let r = idx / n;\n let c = idx % n;\n let mut d = 0;\n while d < 8 {\n let mut rr = r as i32;\n let mut cc = c as i32;\n let mut cur = mat[r][c];\n let mut active = true;\n let mut step = 1;\n while step <= 5 {\n let dx_d = Self::get_dx(d);\n let dy_d = Self::get_dy(d);\n rr += dx_d;\n cc += dy_d;\n if active && 0 <= rr && rr < m as i32 && 0 <= cc && cc < n as i32 {\n cur = cur * 10 + mat[rr as usize][cc as usize];\n nums.push(cur);\n } else {\n active = false;\n }\n step += 1;\n }\n d += 1;\n }\n idx += 1;\n }\n\n let mut best = -1;\n let mut best_count: i32 = 0;\n let mut i = 0;\n while i < nums.len() {\n let val = nums[i];\n if val > 10 && Self::is_prime(val) {\n let mut count: i32 = 0;\n let mut j = 0;\n while j < nums.len() {\n if nums[j] == val {\n count += 1;\n }\n j += 1;\n }\n if count > best_count || (count == best_count && val > best) {\n best_count = count;\n best = val;\n }\n }\n i += 1;\n }\n\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn check_no_divisor(n: int, d: int) -> bool {\n n % d != 0\n }\n\n pub open spec fn prime_int(n: int) -> bool {\n n > 1 && forall|d: int| 2 <= d && d * d <= n ==> #[trigger] Self::check_no_divisor(n, d)\n }\n\n pub open spec fn is_candidate_prime(n: int) -> bool {\n n > 10 && Self::prime_int(n)\n }\n\n pub open spec fn row_delta(d: int) -> int {\n if d == 0 {\n 0\n } else if d == 1 {\n 1\n } else if d == 2 {\n 1\n } else if d == 3 {\n 1\n } else if d == 4 {\n 0\n } else if d == 5 {\n -1\n } else if d == 6 {\n -1\n } else {\n -1\n }\n }\n\n pub open spec fn col_delta(d: int) -> int {\n if d == 0 {\n 1\n } else if d == 1 {\n 1\n } else if d == 2 {\n 0\n } else if d == 3 {\n -1\n } else if d == 4 {\n -1\n } else if d == 5 {\n -1\n } else if d == 6 {\n 0\n } else {\n 1\n }\n }\n\n pub open spec fn row_at(r: int, d: int, step: int) -> int {\n r + step * Self::row_delta(d)\n }\n\n pub open spec fn col_at(c: int, d: int, step: int) -> int {\n c + step * Self::col_delta(d)\n }\n\n pub open spec fn in_bounds(mat: Seq>, rr: int, cc: int) -> bool {\n mat.len() > 0 && 0 <= rr < mat.len() && 0 <= cc < mat[0].len() as int\n }\n\n pub open spec fn path_valid(mat: Seq>, r: int, c: int, d: int, step: int) -> bool {\n 0 <= step <= 5 && mat.len() > 0 && forall|t: int| 0 <= t <= step ==> Self::in_bounds(mat, Self::row_at(r, d, t), Self::col_at(c, d, t))\n }\n\n pub open spec fn number_on_path(mat: Seq>, r: int, c: int, d: int, step: int) -> int\n decreases step\n {\n if step <= 0 {\n mat[r][c] as int\n } else {\n Self::number_on_path(mat, r, c, d, step - 1) * 10 + mat[Self::row_at(r, d, step)][Self::col_at(c, d, step)] as int\n }\n }\n\n pub open spec fn direction_numbers_upto(mat: Seq>, r: int, c: int, d: int, step_bound: int) -> Seq\n decreases step_bound\n {\n if step_bound <= 0 {\n seq![]\n } else {\n let prev = Self::direction_numbers_upto(mat, r, c, d, step_bound - 1);\n if Self::path_valid(mat, r, c, d, step_bound) {\n prev.push(Self::number_on_path(mat, r, c, d, step_bound) as i32)\n } else {\n prev\n }\n }\n }\n\n pub open spec fn cell_numbers_upto(mat: Seq>, r: int, c: int, d_bound: int) -> Seq\n decreases d_bound\n {\n if d_bound <= 0 {\n seq![]\n } else {\n Self::cell_numbers_upto(mat, r, c, d_bound - 1) + Self::direction_numbers_upto(mat, r, c, d_bound - 1, 5)\n }\n }\n\n pub open spec fn all_numbers_upto(mat: Seq>, idx_bound: int) -> Seq\n decreases idx_bound\n {\n if idx_bound <= 0 || mat.len() == 0 || mat[0].len() == 0 {\n seq![]\n } else {\n let prev = Self::all_numbers_upto(mat, idx_bound - 1);\n let idx = idx_bound - 1;\n if 0 <= idx < (mat.len() * mat[0].len()) as int {\n prev + Self::cell_numbers_upto(mat, idx / (mat[0].len() as int), idx % (mat[0].len() as int), 8)\n } else {\n prev\n }\n }\n }\n\n pub open spec fn all_numbers(mat: Seq>) -> Seq {\n if mat.len() == 0 || mat[0].len() == 0 {\n seq![]\n } else {\n Self::all_numbers_upto(mat, (mat.len() * mat[0].len()) as int)\n }\n }\n\n pub open spec fn count_occurrences(s: Seq, value: i32) -> nat\n decreases s.len()\n {\n if s.len() == 0 {\n 0\n } else {\n Self::count_occurrences(s.drop_last(), value) + if s.last() == value { 1 as nat } else { 0 as nat }\n }\n }\n\n proof fn lemma_count_extend(s: Seq, value: i32, elem: i32)\n ensures\n Self::count_occurrences(s.push(elem), value) ==\n Self::count_occurrences(s, value) + if elem == value { 1 as nat } else { 0 as nat },\n {\n assert(s.push(elem).drop_last() =~= s);\n }\n\n proof fn lemma_count_pos_implies_exists(s: Seq, value: i32)\n requires\n Self::count_occurrences(s, value) > 0,\n ensures\n exists|k: int| 0 <= k < s.len() && s[k] == value,\n decreases s.len(),\n {\n if s.len() == 0 {\n } else if s.last() == value {\n assert(s[s.len() - 1] == value);\n } else {\n Self::lemma_count_pos_implies_exists(s.drop_last(), value);\n let k = choose|k: int| 0 <= k < s.drop_last().len() && s.drop_last()[k] == value;\n assert(s[k] == value);\n }\n }\n\n pub open spec fn max_path_num(step: int) -> int\n decreases step,\n {\n if step <= 0 { 9 }\n else { Self::max_path_num(step - 1) * 10 + 9 }\n }\n\n proof fn lemma_max_path_num_bound(step: int)\n requires 0 <= step <= 5,\n ensures Self::max_path_num(step) <= 999_999,\n {\n assert(Self::max_path_num(0) == 9);\n assert(Self::max_path_num(1) == 99);\n assert(Self::max_path_num(2) == 999);\n assert(Self::max_path_num(3) == 9999);\n assert(Self::max_path_num(4) == 99999);\n assert(Self::max_path_num(5) == 999999);\n }\n\n fn get_dx(d: usize) -> (res: i32)\n requires 0 <= d < 8,\n ensures -1 <= res <= 1, res as int == Self::row_delta(d as int),\n {\n if d == 0 { 0 } else if d == 1 { 1 } else if d == 2 { 1 } else if d == 3 { 1 }\n else if d == 4 { 0 } else if d == 5 { -1 } else if d == 6 { -1 } else { -1 }\n }\n\n fn get_dy(d: usize) -> (res: i32)\n requires 0 <= d < 8,\n ensures -1 <= res <= 1, res as int == Self::col_delta(d as int),\n {\n if d == 0 { 1 } else if d == 1 { 1 } else if d == 2 { 0 } else if d == 3 { -1 }\n else if d == 4 { -1 } else if d == 5 { -1 } else if d == 6 { 0 } else { 1 }\n }\n\n proof fn lemma_max_path_num_positive(step: int)\n requires 0 <= step,\n ensures Self::max_path_num(step) >= 9,\n decreases step,\n {\n if step > 0 {\n Self::lemma_max_path_num_positive(step - 1);\n }\n }\n\n fn is_prime(num: i32) -> (res: bool)\n requires\n num >= 0,\n num <= 999_999,\n ensures\n res == Self::prime_int(num as int),\n {\n if num <= 1 {\n return false;\n }\n\n let mut d = 2;\n while d * d <= num\n invariant\n num >= 2,\n num <= 999_999,\n 2 <= d,\n d <= 1000,\n d as int * d as int <= 1_000_000,\n forall|k: int| 2 <= k < d ==> #[trigger] Self::check_no_divisor(num as int, k),\n decreases num - d + 1,\n {\n assert(d as int <= num as int) by(nonlinear_arith)\n requires d as int >= 2, d as int * d as int <= num as int;\n if num % d == 0 {\n proof {\n assert(2 <= d as int && d as int * d as int <= num as int);\n assert(!Self::check_no_divisor(num as int, d as int));\n assert(!Self::prime_int(num as int));\n }\n return false;\n }\n let ghost old_d = d;\n d += 1;\n proof {\n assert(old_d as int * old_d as int <= num as int);\n assert(old_d <= 999) by(nonlinear_arith)\n requires old_d as int * old_d as int <= 999_999int, old_d >= 2i32;\n assert(d <= 1000);\n assert(d as int * d as int <= 1_000_000) by(nonlinear_arith)\n requires d as int <= 1000, d as int >= 2;\n assert forall|k: int| 2 <= k < d implies #[trigger] Self::check_no_divisor(num as int, k) by {\n if k < old_d as int {\n } else {\n assert(k == old_d as int);\n assert(num as int % k != 0);\n }\n }\n }\n }\n\n proof {\n assert(num > 1);\n assert(d as int * d as int > num as int);\n assert forall|k: int| 2 <= k && k * k <= num as int implies #[trigger] Self::check_no_divisor(num as int, k) by {\n assert(k < d as int) by(nonlinear_arith)\n requires\n k >= 0,\n d as int >= 2,\n k * k <= num as int,\n d as int * d as int > num as int;\n }\n }\n\n true\n }\n\n pub fn most_frequent_prime(mat: Vec>) -> (res: i32)\n requires\n 1 <= mat.len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> 1 <= #[trigger] mat[i].len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==> 1 <= #[trigger] mat[i][j] <= 9,\n ensures\n (res == -1) <==> (forall|k: int| 0 <= k < Self::all_numbers(mat@).len() ==> !Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[k] as int)),\n res != -1 ==> (Self::is_candidate_prime(res as int) &&\n forall|k: int| 0 <= k < Self::all_numbers(mat@).len() && Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[k] as int) ==>\n (Self::count_occurrences(Self::all_numbers(mat@), Self::all_numbers(mat@)[k]) < Self::count_occurrences(Self::all_numbers(mat@), res) ||\n (Self::count_occurrences(Self::all_numbers(mat@), Self::all_numbers(mat@)[k]) == Self::count_occurrences(Self::all_numbers(mat@), res) && Self::all_numbers(mat@)[k] <= res))),\n {\n let m = mat.len();\n let n = mat[0].len();\n assert(m * n <= 36) by(nonlinear_arith)\n requires 1 <= m <= 6usize, 1 <= n <= 6usize;\n let total = m * n;\n let ghost dx = [0, 1, 1, 1, 0, -1, -1, -1];\n let ghost dy = [1, 1, 0, -1, -1, -1, 0, 1];\n\n let mut nums: Vec = Vec::new();\n let mut idx = 0;\n while idx < total\n invariant\n m == mat.len(),\n n == mat[0].len(),\n total == m * n,\n 1 <= m <= 6,\n 1 <= n <= 6,\n total <= 36,\n 0 <= idx <= total,\n forall|i: int| 0 <= i < mat.len() ==> 1 <= #[trigger] mat[i].len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==> 1 <= #[trigger] mat[i][j] <= 9,\n nums@ == Self::all_numbers_upto(mat@, idx as int),\n forall|k: int| 0 <= k < nums.len() ==> 11 <= #[trigger] nums[k] <= 999_999,\n nums.len() as int <= idx as int * 40,\n decreases total - idx,\n {\n assert(idx / n < m) by(nonlinear_arith) requires 0 <= idx, idx < m * n, 1 <= n;\n assert(idx % n < n) by(nonlinear_arith) requires 0 <= idx, 1 <= n;\n let r = idx / n;\n let c = idx % n;\n let mut d = 0;\n while d < 8\n invariant\n m == mat.len(),\n n == mat[0].len(),\n total == m * n,\n 1 <= m <= 6,\n 1 <= n <= 6,\n total <= 36,\n 0 <= idx < total,\n r == idx / n,\n c == idx % n,\n r < m,\n c < n,\n 0 <= d <= 8,\n forall|i: int| 0 <= i < mat.len() ==> 1 <= #[trigger] mat[i].len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==> 1 <= #[trigger] mat[i][j] <= 9,\n nums@ == Self::all_numbers_upto(mat@, idx as int) + Self::cell_numbers_upto(mat@, r as int, c as int, d as int),\n forall|k: int| 0 <= k < nums.len() ==> 11 <= #[trigger] nums[k] <= 999_999,\n nums.len() as int <= idx as int * 40 + d as int * 5,\n decreases 8 - d,\n {\n assert(mat[r as int].len() == n);\n let mut rr = r as i32;\n let mut cc = c as i32;\n let mut cur = mat[r][c];\n let mut active = true;\n let mut step = 1;\n proof {\n assert(Self::row_at(r as int, d as int, 0) == r as int);\n assert(Self::col_at(c as int, d as int, 0) == c as int);\n assert(Self::in_bounds(mat@, r as int, c as int));\n assert(Self::path_valid(mat@, r as int, c as int, d as int, 0));\n Self::lemma_max_path_num_positive(0);\n }\n while step <= 5\n invariant\n m == mat.len(),\n n == mat[0].len(),\n total == m * n,\n 1 <= m <= 6,\n 1 <= n <= 6,\n total <= 36,\n 0 <= idx < total,\n r == idx / n,\n c == idx % n,\n r < m,\n c < n,\n 0 <= d < 8,\n 1 <= step <= 6,\n rr as int >= r as int - (step as int - 1),\n rr as int <= r as int + (step as int - 1),\n cc as int >= c as int - (step as int - 1),\n cc as int <= c as int + (step as int - 1),\n active ==> 1 <= cur,\n active ==> cur as int <= Self::max_path_num(step as int - 1),\n forall|i: int| 0 <= i < mat.len() ==> 1 <= #[trigger] mat[i].len() <= 6,\n forall|i: int| 0 <= i < mat.len() ==> #[trigger] mat[i].len() == mat[0].len(),\n forall|i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==> 1 <= #[trigger] mat[i][j] <= 9,\n nums@ == Self::all_numbers_upto(mat@, idx as int) +\n Self::cell_numbers_upto(mat@, r as int, c as int, d as int) +\n Self::direction_numbers_upto(mat@, r as int, c as int, d as int, step as int - 1),\n active ==> Self::path_valid(mat@, r as int, c as int, d as int, step as int - 1),\n active ==> rr as int == Self::row_at(r as int, d as int, step as int - 1),\n active ==> cc as int == Self::col_at(c as int, d as int, step as int - 1),\n active ==> cur as int == Self::number_on_path(mat@, r as int, c as int, d as int, step as int - 1),\n !active ==> !Self::path_valid(mat@, r as int, c as int, d as int, step as int - 1),\n forall|k: int| 0 <= k < nums.len() ==> 11 <= #[trigger] nums[k] <= 999_999,\n nums.len() as int <= idx as int * 40 + d as int * 5 + step as int - 1,\n decreases 6 - step,\n {\n let dx_d = Self::get_dx(d);\n let dy_d = Self::get_dy(d);\n rr += dx_d;\n cc += dy_d;\n if active && 0 <= rr && rr < m as i32 && 0 <= cc && cc < n as i32 {\n assert(mat[rr as int].len() == n);\n proof {\n Self::lemma_max_path_num_bound(step as int - 1);\n assert(cur as int <= Self::max_path_num(step as int - 1));\n assert(Self::max_path_num(step as int - 1) <= 999_999);\n assert(cur as int * 10 + 9 <= 999_999 * 10 + 9);\n }\n cur = cur * 10 + mat[rr as usize][cc as usize];\n proof {\n assert(active);\n assert(Self::path_valid(mat@, r as int, c as int, d as int, step as int - 1));\n assert(Self::row_at(r as int, d as int, step as int) == Self::row_at(r as int, d as int, step as int - 1) + Self::row_delta(d as int)) by {\n let x = Self::row_delta(d as int);\n assert(step as int * x == (step as int - 1) * x + x) by(nonlinear_arith);\n };\n assert(Self::col_at(c as int, d as int, step as int) == Self::col_at(c as int, d as int, step as int - 1) + Self::col_delta(d as int)) by {\n let x = Self::col_delta(d as int);\n assert(step as int * x == (step as int - 1) * x + x) by(nonlinear_arith);\n };\n assert(dx_d as int == Self::row_delta(d as int));\n assert(dy_d as int == Self::col_delta(d as int));\n assert(rr as int == Self::row_at(r as int, d as int, step as int));\n assert(cc as int == Self::col_at(c as int, d as int, step as int));\n assert(Self::in_bounds(mat@, rr as int, cc as int));\n assert(Self::path_valid(mat@, r as int, c as int, d as int, step as int));\n assert(cur as int == Self::number_on_path(mat@, r as int, c as int, d as int, step as int));\n }\n nums.push(cur);\n proof {\n assert(cur >= 11);\n Self::lemma_max_path_num_bound(step as int);\n }\n } else {\n proof {\n if active {\n assert(Self::row_at(r as int, d as int, step as int) == Self::row_at(r as int, d as int, step as int - 1) + Self::row_delta(d as int)) by {\n let x = Self::row_delta(d as int);\n assert(step as int * x == (step as int - 1) * x + x) by(nonlinear_arith);\n };\n assert(Self::col_at(c as int, d as int, step as int) == Self::col_at(c as int, d as int, step as int - 1) + Self::col_delta(d as int)) by {\n let x = Self::col_delta(d as int);\n assert(step as int * x == (step as int - 1) * x + x) by(nonlinear_arith);\n };\n assert(rr as int == Self::row_at(r as int, d as int, step as int));\n assert(cc as int == Self::col_at(c as int, d as int, step as int));\n assert(!Self::in_bounds(mat@, rr as int, cc as int));\n assert(!Self::path_valid(mat@, r as int, c as int, d as int, step as int));\n } else {\n assert(!Self::path_valid(mat@, r as int, c as int, d as int, step as int - 1));\n assert(!Self::path_valid(mat@, r as int, c as int, d as int, step as int));\n }\n }\n active = false;\n }\n proof {\n if Self::path_valid(mat@, r as int, c as int, d as int, step as int) {\n assert(Self::direction_numbers_upto(mat@, r as int, c as int, d as int, step as int) ==\n Self::direction_numbers_upto(mat@, r as int, c as int, d as int, step as int - 1).push(\n Self::number_on_path(mat@, r as int, c as int, d as int, step as int) as i32));\n } else {\n assert(Self::direction_numbers_upto(mat@, r as int, c as int, d as int, step as int) ==\n Self::direction_numbers_upto(mat@, r as int, c as int, d as int, step as int - 1));\n }\n }\n step += 1;\n }\n proof {\n assert(step == 6);\n assert(Self::direction_numbers_upto(mat@, r as int, c as int, d as int, 5) ==\n Self::direction_numbers_upto(mat@, r as int, c as int, d as int, step as int - 1));\n }\n d += 1;\n }\n proof {\n assert(d == 8);\n assert(Self::cell_numbers_upto(mat@, r as int, c as int, 8) == Self::cell_numbers_upto(mat@, r as int, c as int, d as int));\n assert(nums.len() as int <= idx as int * 40 + 40) by(nonlinear_arith)\n requires nums.len() as int <= idx as int * 40 + d as int * 5, d == 8usize;\n }\n idx += 1;\n proof {\n assert(nums.len() as int <= (idx as int) * 40) by(nonlinear_arith)\n requires nums.len() as int <= (idx as int - 1) * 40 + 40;\n }\n }\n\n proof {\n assert(idx == total);\n assert(total as int == mat@.len() * mat@[0].len());\n assert(nums@ == Self::all_numbers_upto(mat@, total as int));\n assert(Self::all_numbers(mat@) == Self::all_numbers_upto(mat@, total as int));\n assert(nums.len() as int <= total as int * 40) by(nonlinear_arith)\n requires nums.len() as int <= idx as int * 40, idx == total;\n assert(total as int * 40 <= 1440) by(nonlinear_arith)\n requires total as int <= 36;\n }\n\n let mut best = -1;\n let mut best_count: i32 = 0;\n let mut i = 0;\n while i < nums.len()\n invariant\n nums@ == Self::all_numbers(mat@),\n 0 <= i <= nums.len(),\n best_count >= 0,\n (best == -1) <==> (best_count == 0),\n best != -1 ==> Self::is_candidate_prime(best as int),\n best != -1 ==> best_count == Self::count_occurrences(nums@, best),\n best != -1 ==> 11 <= best <= 999_999,\n best == -1 ==> forall|k: int| 0 <= k < i ==> !Self::is_candidate_prime(#[trigger] nums[k] as int),\n forall|k: int| 0 <= k < i && Self::is_candidate_prime(#[trigger] nums[k] as int) ==>\n (Self::count_occurrences(nums@, nums[k]) < best_count ||\n (Self::count_occurrences(nums@, nums[k]) == best_count && nums[k] <= best)),\n forall|k: int| 0 <= k < nums.len() ==> 11 <= #[trigger] nums[k] <= 999_999,\n nums.len() <= 1440,\n decreases nums.len() - i,\n {\n let val = nums[i];\n if val > 10 && Self::is_prime(val) {\n let mut count: i32 = 0;\n let mut j = 0;\n while j < nums.len()\n invariant\n nums@ == Self::all_numbers(mat@),\n 0 <= j <= nums.len(),\n nums.len() <= 1440,\n val == nums[i as int],\n count == Self::count_occurrences(nums@.subrange(0, j as int), val),\n 0 <= count <= j,\n j > i ==> count >= 1,\n forall|k: int| 0 <= k < nums.len() ==> 11 <= #[trigger] nums[k] <= 999_999,\n decreases nums.len() - j,\n {\n if nums[j] == val {\n count += 1;\n }\n proof {\n let old_j = j;\n let prefix = nums@.subrange(0, old_j as int);\n let next_prefix = nums@.subrange(0, (old_j + 1) as int);\n assert(next_prefix =~= prefix.push(nums[old_j as int]));\n Self::lemma_count_extend(prefix, val, nums[old_j as int]);\n }\n j += 1;\n }\n proof {\n assert(nums@.subrange(0, nums.len() as int) =~= nums@);\n assert(count >= 1);\n }\n\n if count > best_count || (count == best_count && val > best) {\n best_count = count;\n best = val;\n }\n }\n i += 1;\n }\n\n proof {\n assert(i == nums.len());\n if best != -1 {\n assert(best_count > 0);\n assert(Self::count_occurrences(nums@, best) > 0);\n Self::lemma_count_pos_implies_exists(nums@, best);\n let k = choose|k: int| 0 <= k < nums@.len() && nums@[k] == best;\n assert(Self::is_candidate_prime(nums@[k] as int));\n assert(!(forall|kk: int| 0 <= kk < Self::all_numbers(mat@).len() ==> !Self::is_candidate_prime(#[trigger] Self::all_numbers(mat@)[kk] as int)));\n }\n }\n\n best\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc3044/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3044/tests/mutated_testcases.jsonl"} {"problem": "lc3046", "family": "leetcode", "description": "# Split the Array\n\nYou are given an integer array `nums` of even length. You have to split the array into two parts `nums1` and `nums2` such that:\n\n- `nums1.length == nums2.length == nums.length / 2`\n- `nums1` contains distinct elements\n- `nums2` contains distinct elements\n\nReturn `true` if it is possible to split the array, and `false` otherwise.\n\n## Example 1:\n\n> **Input:** nums = [1,1,2,2,3,4]\n> **Output:** true\n> **Explanation:** One possible split is nums1 = [1,2,3] and nums2 = [1,2,4].\n\n## Example 2:\n\n> **Input:** nums = [1,1,1,1]\n> **Output:** false\n> **Explanation:** The only possible split is nums1 = [1,1] and nums2 = [1,1]. Neither part contains distinct elements.\n\n## Constraints:\n\n- $1 <= nums.length <= 100$\n- $nums.length \\% 2 == 0$\n- $1 <= nums[i] <= 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_possible_to_split(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Array, Hash Table, Counting, Weekly Contest 386\nAcceptance rate: 61.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_possible_to_split(nums: Vec) -> (res: bool)\n requires\n 1 <= nums.len() <= 100,\n nums.len() % 2 == 0,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n res == (forall|i: int, j: int, k: int|\n 0 <= i < j < k < nums.len() ==>\n !(nums[i] == nums[j] && nums[j] == nums[k])),\n {\n\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_possible_to_split(nums: Vec) -> bool {\n for i in 0..nums.len() {\n for j in i + 1..nums.len() {\n for k in j + 1..nums.len() {\n if nums[i] == nums[j] && nums[j] == nums[k] {\n return false;\n }\n }\n }\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_possible_to_split(nums: Vec) -> (res: bool)\n requires\n 1 <= nums.len() <= 100,\n nums.len() % 2 == 0,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n res == (forall|i: int, j: int, k: int|\n 0 <= i < j < k < nums.len() ==>\n !(nums[i] == nums[j] && nums[j] == nums[k])),\n {\n for i in 0..nums.len() {\n for j in i + 1..nums.len() {\n for k in j + 1..nums.len() {\n if nums[i] == nums[j] && nums[j] == nums[k] {\n return false;\n }\n }\n }\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_possible_to_split(nums: Vec) -> (res: bool)\n requires\n 1 <= nums.len() <= 100,\n nums.len() % 2 == 0,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n res == (forall|i: int, j: int, k: int|\n 0 <= i < j < k < nums.len() ==>\n !(nums[i] == nums[j] && nums[j] == nums[k])),\n {\n for i in 0..nums.len()\n invariant\n 0 <= i <= nums.len(),\n 1 <= nums.len() <= 100,\n nums.len() % 2 == 0,\n forall|m: int| 0 <= m < nums.len() ==> 1 <= #[trigger] nums[m] <= 100,\n forall|a: int, b: int, c: int|\n 0 <= a < b < c < nums.len() && a < i ==>\n !(nums[a] == nums[b] && nums[b] == nums[c]),\n {\n for j in i + 1..nums.len()\n invariant\n 0 <= i < nums.len(),\n i + 1 <= j <= nums.len(),\n 1 <= nums.len() <= 100,\n nums.len() % 2 == 0,\n forall|m: int| 0 <= m < nums.len() ==> 1 <= #[trigger] nums[m] <= 100,\n forall|a: int, b: int, c: int|\n 0 <= a < b < c < nums.len() && a < i ==>\n !(nums[a] == nums[b] && nums[b] == nums[c]),\n forall|b: int, c: int|\n i < b < c < nums.len() && b < j ==>\n !(nums[i as int] == nums[b] && nums[b] == nums[c]),\n {\n for k in j + 1..nums.len()\n invariant\n 0 <= i < nums.len(),\n i < j < nums.len(),\n j + 1 <= k <= nums.len(),\n 1 <= nums.len() <= 100,\n nums.len() % 2 == 0,\n forall|m: int| 0 <= m < nums.len() ==> 1 <= #[trigger] nums[m] <= 100,\n forall|a: int, b: int, c: int|\n 0 <= a < b < c < nums.len() && a < i ==>\n !(nums[a] == nums[b] && nums[b] == nums[c]),\n forall|b: int, c: int|\n i < b < c < nums.len() && b < j ==>\n !(nums[i as int] == nums[b] && nums[b] == nums[c]),\n forall|c: int|\n j < c < nums.len() && c < k ==>\n !(nums[i as int] == nums[j as int] && nums[j as int] == nums[c]),\n {\n if nums[i] == nums[j] && nums[j] == nums[k] {\n return false;\n }\n }\n }\n }\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3046/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3046/tests/mutated_testcases.jsonl"} {"problem": "lc3065", "family": "leetcode", "description": "# Minimum Operations to Exceed Threshold Value I\n\nYou are given a **0-indexed** integer array `nums`, and an integer `k`.\n\nIn one operation, you can remove one occurrence of the smallest element of `nums`.\n\nReturn the ***minimum** number of operations needed so that all elements of the array are greater than or equal to `k`*.\n\n## Example 1:\n\n> **Input:** nums = [2,11,10,1,3], k = 10\n> **Output:** 3\n> **Explanation:** After one operation, nums becomes equal to [2, 11, 10, 3].\n> After two operations, nums becomes equal to [11, 10, 3].\n> After three operations, nums becomes equal to [11, 10].\n> At this stage, all the elements of nums are greater than or equal to 10 so we can stop.\n> It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.\n\n## Example 2:\n\n> **Input:** nums = [1,1,2,4,9], k = 1\n> **Output:** 0\n> **Explanation:** All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.\n\n## Example 3:\n\n> **Input:** nums = [1,1,2,4,9], k = 9\n> **Output:** 4\n> **Explanation:** only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.\n\n## Constraints:\n\n- $1 <= nums.length <= 50$\n\n- $1 <= nums[i] <= 10^9$\n\n- $1 <= k <= 10^9$\n\n- The input is generated such that there is at least one index `i` such that `nums[i] >= k`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Biweekly Contest 125\nAcceptance rate: 86.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> (res: i32) \n requires \n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000, \n 1 <= k <= 1_000_000_000, \n exists |i: int| 0 <= i < nums.len() && nums[i] >= k, \n ensures \n (res as int) == nums@.filter(|x: i32| x < k).len(),\n 0 <= res <= nums.len(), \n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 \n {\n let mut count: i32 = 0;\n for i in 0..nums.len() \n {\n if nums[i] < k {\n count += 1;\n }\n }\n return count;\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> (res: i32) \n requires \n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000, \n 1 <= k <= 1_000_000_000, \n exists |i: int| 0 <= i < nums.len() && nums[i] >= k, \n ensures \n (res as int) == nums@.filter(|x: i32| x < k).len(),\n 0 <= res <= nums.len(), \n {\n let mut count: i32 = 0;\n for i in 0..nums.len() \n {\n if nums[i] < k {\n count += 1;\n }\n }\n \n return count;\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n proof fn lemma_filter_push(seq: Seq, x: T, f: spec_fn(T) -> bool)\n ensures\n seq.push(x).filter(f).len() == seq.filter(f).len() + if f(x) { 1 as nat } else { 0 as nat },\n seq.push(x).filter(f) =~= seq.filter(f) + (if f(x) { seq![x] } else { Seq::empty() }),\n decreases seq.len(),\n {\n reveal_with_fuel(Seq::filter, 2);\n\n if seq.len() == 0 {\n } else {\n let head = seq[0];\n let tail = seq.subrange(1, seq.len() as int);\n \n Self::lemma_filter_push(tail, x, f);\n\n assert(seq =~= seq![head] + tail);\n assert(seq.push(x) =~= seq![head] + tail.push(x));\n\n if f(head) {\n assert(seq.filter(f) =~= seq![head] + tail.filter(f));\n assert(seq.push(x).filter(f) =~= seq![head] + tail.push(x).filter(f));\n } else {\n assert(seq.filter(f) =~= tail.filter(f));\n assert(seq.push(x).filter(f) =~= tail.push(x).filter(f));\n }\n }\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (res: i32) \n requires \n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000, \n 1 <= k <= 1_000_000_000, \n exists |i: int| 0 <= i < nums.len() && nums[i] >= k, \n ensures \n (res as int) == nums@.filter(|x: i32| x < k).len(),\n 0 <= res <= nums.len(), \n {\n let mut count: i32 = 0;\n for i in 0..nums.len() \n invariant\n 1 <= nums.len() <= 50, \n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000, \n 1 <= k <= 1_000_000_000, \n exists |i: int| 0 <= i < nums.len() && nums[i] >= k, \n count == nums@.subrange(0, i as int).filter(|x: i32| x < k).len(),\n 0 <= count <= i,\n {\n proof {\n Self::lemma_filter_push(nums@.subrange(0, i as int), nums@[i as int], |x: i32| x < k);\n assert(nums@.subrange(0, i as int + 1) == nums@.subrange(0, i as int).push(nums@[i as int]));\n }\n \n if nums[i] < k {\n count += 1;\n }\n }\n \n assert(nums@.subrange(0, nums.len() as int) == nums@);\n \n return count;\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc3065/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3065/tests/mutated_testcases.jsonl"} {"problem": "lc3066", "family": "leetcode", "description": "# Minimum Operations to Exceed Threshold Value II\n\nYou are given a 0-indexed integer array `nums`, and an integer `k`.\n\nYou are allowed to perform operations on `nums`. In one operation, you can:\n\n- Select the two smallest integers `x` and `y` in `nums`.\n- Remove `x` and `y` from `nums`.\n- Insert `min(x, y) * 2 + max(x, y)` into `nums`.\n\nYou can only apply an operation when `nums` has at least two elements.\n\nReturn the minimum number of operations needed so that all elements of `nums` are greater than or equal to `k`.\n\n## Example 1:\n\n> **Input:** nums = [2,11,10,1,3], k = 10\n> **Output:** 2\n> **Explanation:**\n> After the first operation, nums becomes [4,11,10,3].\n> After the second operation, nums becomes [10,11,10].\n> Now all elements are greater than or equal to 10.\n\n## Example 2:\n\n> **Input:** nums = [1,1,2,4,9], k = 20\n> **Output:** 4\n> **Explanation:**\n> After one operation, nums becomes [2,4,9,3].\n> After two operations, nums becomes [7,4,9].\n> After three operations, nums becomes [15,9].\n> After four operations, nums becomes [33].\n> Now all elements are greater than or equal to 20.\n\n## Constraints:\n\n- `2 <= nums.length <= 2 * 10^5`\n- `1 <= nums[i], k <= 10^9`\n- The input is generated such that an answer always exists.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Heap (Priority Queue), Simulation\nAcceptance rate: 45.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n\npub open spec fn is_desc(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i <= j < s.len() ==> s[i] >= s[j]\n}\n\npub open spec fn to_int_seq(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { Seq::empty() }\n else { Self::to_int_seq(s.drop_last()).push(s.last() as int) }\n}\n\npub open spec fn insert_desc(s: Seq, v: int) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { seq![v] }\n else if v >= s[0] { seq![v].add(s) }\n else { seq![s[0]].add(Self::insert_desc(s.subrange(1, s.len() as int), v)) }\n}\n\npub open spec fn ssort(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { Seq::empty() }\n else { Self::insert_desc(Self::ssort(s.drop_last()), s.last()) }\n}\n\npub open spec fn merge_ops_n(s: Seq, k: int, n: nat) -> int\n decreases n\n{\n if n == 0 { 0 }\n else if s.len() < 2 { 0 }\n else if s[s.len() - 1] >= k { 0 }\n else {\n 1 + Self::merge_ops_n(Self::insert_desc(s.subrange(0, s.len() - 2), 2 * s[s.len() - 1] + s[s.len() - 2]), k, (n - 1) as nat)\n }\n}\n\npub open spec fn merge_ops(s: Seq, k: int) -> int {\n Self::merge_ops_n(s, k, s.len())\n}\n\npub fn min_operations(nums: Vec, k: i32) -> (res: i32)\n requires\n 2 <= nums.len() <= 200_000,\n 1 <= k <= 1_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n res as int == Self::merge_ops(Self::ssort(Self::to_int_seq(nums@)), k as int),\n{\n}\n\n}\n\n}\n", "code": "impl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n let mut h: Vec = Vec::new();\n let mut t: usize = 0;\n while t < nums.len() {\n let v = nums[t];\n let vi = v as i64;\n let mut p: usize = 0;\n while p < h.len() && h[p] > vi {\n p += 1;\n }\n h.insert(p, vi);\n t += 1;\n }\n let mut ops: i32 = 0;\n while h.len() >= 2 && h[h.len() - 1] < k as i64 {\n let nh = h.len();\n let x = h[nh - 1];\n let y = h[nh - 2];\n let merged = 2 * x + y;\n let _ = h.pop();\n let _ = h.pop();\n let mut p: usize = 0;\n while p < h.len() && h[p] > merged {\n p += 1;\n }\n h.insert(p, merged);\n ops = ops + 1;\n }\n ops\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n\npub open spec fn is_desc(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i <= j < s.len() ==> s[i] >= s[j]\n}\n\npub open spec fn to_int_seq(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { Seq::empty() }\n else { Self::to_int_seq(s.drop_last()).push(s.last() as int) }\n}\n\npub open spec fn insert_desc(s: Seq, v: int) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { seq![v] }\n else if v >= s[0] { seq![v].add(s) }\n else { seq![s[0]].add(Self::insert_desc(s.subrange(1, s.len() as int), v)) }\n}\n\npub open spec fn ssort(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { Seq::empty() }\n else { Self::insert_desc(Self::ssort(s.drop_last()), s.last()) }\n}\n\npub open spec fn merge_ops_n(s: Seq, k: int, n: nat) -> int\n decreases n\n{\n if n == 0 { 0 }\n else if s.len() < 2 { 0 }\n else if s[s.len() - 1] >= k { 0 }\n else {\n 1 + Self::merge_ops_n(Self::insert_desc(s.subrange(0, s.len() - 2), 2 * s[s.len() - 1] + s[s.len() - 2]), k, (n - 1) as nat)\n }\n}\n\npub open spec fn merge_ops(s: Seq, k: int) -> int {\n Self::merge_ops_n(s, k, s.len())\n}\n\npub fn min_operations(nums: Vec, k: i32) -> (res: i32)\n requires\n 2 <= nums.len() <= 200_000,\n 1 <= k <= 1_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n res as int == Self::merge_ops(Self::ssort(Self::to_int_seq(nums@)), k as int),\n{\n let mut h: Vec = Vec::new();\n let mut t: usize = 0;\n while t < nums.len() {\n let v = nums[t];\n let vi = v as i64;\n let mut p: usize = 0;\n while p < h.len() && h[p] > vi {\n p += 1;\n }\n h.insert(p, vi);\n t += 1;\n }\n let mut ops: i32 = 0;\n while h.len() >= 2 && h[h.len() - 1] < k as i64 {\n let nh = h.len();\n let x = h[nh - 1];\n let y = h[nh - 2];\n let merged = 2 * x + y;\n let _ = h.pop();\n let _ = h.pop();\n let mut p: usize = 0;\n while p < h.len() && h[p] > merged {\n p += 1;\n }\n h.insert(p, merged);\n ops = ops + 1;\n }\n ops\n}\n\n}\n\n}\n", "verified": "use vstd::prelude::*;\nfn main() {}\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n\npub open spec fn is_desc(s: Seq) -> bool {\n forall|i: int, j: int| 0 <= i <= j < s.len() ==> s[i] >= s[j]\n}\n\npub open spec fn to_int_seq(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { Seq::empty() }\n else { Self::to_int_seq(s.drop_last()).push(s.last() as int) }\n}\n\nproof fn lemma_to_int(s: Seq)\n ensures\n Self::to_int_seq(s).len() == s.len(),\n forall|q: int| 0 <= q < s.len() ==> #[trigger] Self::to_int_seq(s)[q] == s[q] as int,\n decreases s.len()\n{\n if s.len() == 0 {} else { Self::lemma_to_int(s.drop_last()); }\n}\n\npub open spec fn insert_desc(s: Seq, v: int) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { seq![v] }\n else if v >= s[0] { seq![v].add(s) }\n else { seq![s[0]].add(Self::insert_desc(s.subrange(1, s.len() as int), v)) }\n}\n\nproof fn lemma_insert_len(s: Seq, v: int)\n ensures Self::insert_desc(s, v).len() == s.len() + 1\n decreases s.len()\n{ if s.len() == 0 {} else if v >= s[0] {} else { Self::lemma_insert_len(s.subrange(1, s.len() as int), v); } }\n\nproof fn lemma_insert_pos(s: Seq, v: int, p: int)\n requires\n 0 <= p <= s.len(),\n forall|t: int| 0 <= t < p ==> s[t] > v,\n p < s.len() ==> s[p] <= v,\n ensures\n s.insert(p, v) == Self::insert_desc(s, v),\n decreases s.len()\n{\n if s.len() == 0 {\n assert(s.insert(0, v) =~= seq![v]);\n } else if v >= s[0] {\n assert(p == 0);\n assert(s.insert(0, v) =~= seq![v].add(s));\n } else {\n assert(p >= 1);\n let sub = s.subrange(1, s.len() as int);\n Self::lemma_insert_pos(sub, v, p - 1);\n assert(s.insert(p, v) =~= seq![s[0]].add(sub.insert(p - 1, v)));\n }\n}\n\nproof fn lemma_insert_desc_sorted(s: Seq, v: int)\n requires Self::is_desc(s)\n ensures\n Self::is_desc(Self::insert_desc(s, v)),\n Self::insert_desc(s, v).len() == s.len() + 1,\n forall|w: int| #[trigger] Self::insert_desc(s, v).contains(w) ==> w == v || s.contains(w),\n decreases s.len()\n{\n Self::lemma_insert_len(s, v);\n let r = Self::insert_desc(s, v);\n if s.len() == 0 {\n } else if v >= s[0] {\n assert(r =~= seq![v].add(s));\n assert forall|i: int, j: int| 0 <= i <= j < r.len() implies r[i] >= r[j] by {\n if i == 0 { if j == 0 {} else { assert(r[j] == s[j - 1]); assert(s[0] >= s[j - 1]); } }\n else { assert(r[i] == s[i - 1]); assert(r[j] == s[j - 1]); assert(s[i-1] >= s[j-1]); }\n }\n } else {\n let sub = s.subrange(1, s.len() as int);\n Self::lemma_insert_desc_sorted(sub, v);\n Self::lemma_insert_len(sub, v);\n let tail = Self::insert_desc(sub, v);\n assert(r =~= seq![s[0]].add(tail));\n assert forall|i: int, j: int| 0 <= i <= j < r.len() implies r[i] >= r[j] by {\n if i == 0 {\n if j == 0 {} else {\n assert(r[j] == tail[j - 1]);\n assert(tail.contains(tail[j - 1]));\n if tail[j - 1] == v {} else {\n assert(sub.contains(tail[j - 1]));\n let c = choose|c: int| 0 <= c < sub.len() && sub[c] == tail[j - 1];\n assert(sub[c] == s[c + 1]);\n assert(s[0] >= s[c + 1]);\n }\n }\n } else { assert(r[i] == tail[i - 1]); assert(r[j] == tail[j - 1]); }\n }\n assert forall|w: int| #[trigger] r.contains(w) implies w == v || s.contains(w) by {\n if w == s[0] { assert(s.contains(s[0])); }\n else {\n assert(tail.contains(w));\n if w == v {} else {\n assert(sub.contains(w));\n let c = choose|c: int| 0 <= c < sub.len() && sub[c] == w;\n assert(s[c + 1] == w); assert(s.contains(w));\n }\n }\n }\n }\n}\n\npub open spec fn ssort(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() == 0 { Seq::empty() }\n else { Self::insert_desc(Self::ssort(s.drop_last()), s.last()) }\n}\n\nproof fn lemma_ssort_props(s: Seq)\n ensures Self::is_desc(Self::ssort(s)), Self::ssort(s).len() == s.len()\n decreases s.len()\n{\n if s.len() == 0 {\n assert(Self::ssort(s) =~= Seq::::empty());\n } else {\n Self::lemma_ssort_props(s.drop_last());\n Self::lemma_insert_desc_sorted(Self::ssort(s.drop_last()), s.last());\n Self::lemma_insert_len(Self::ssort(s.drop_last()), s.last());\n }\n}\n\npub open spec fn merge_ops_n(s: Seq, k: int, n: nat) -> int\n decreases n\n{\n if n == 0 { 0 }\n else if s.len() < 2 { 0 }\n else if s[s.len() - 1] >= k { 0 }\n else {\n 1 + Self::merge_ops_n(Self::insert_desc(s.subrange(0, s.len() - 2), 2 * s[s.len() - 1] + s[s.len() - 2]), k, (n - 1) as nat)\n }\n}\n\npub open spec fn merge_ops(s: Seq, k: int) -> int {\n Self::merge_ops_n(s, k, s.len())\n}\n\npub fn min_operations(nums: Vec, k: i32) -> (res: i32)\n requires\n 2 <= nums.len() <= 200_000,\n 1 <= k <= 1_000_000_000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1_000_000_000,\n ensures\n res as int == Self::merge_ops(Self::ssort(Self::to_int_seq(nums@)), k as int),\n{\n let ghost ni = Self::to_int_seq(nums@);\n proof { Self::lemma_to_int(nums@); }\n\n let mut h: Vec = Vec::new();\n let ghost mut g: Seq = Seq::empty();\n let mut t: usize = 0;\n while t < nums.len()\n invariant\n 0 <= t <= nums.len(),\n 2 <= nums.len() <= 200_000,\n 1 <= k <= 1_000_000_000,\n ni.len() == nums@.len(),\n forall|q: int| 0 <= q < ni.len() ==> #[trigger] ni[q] == nums@[q] as int,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums@[i] <= 1_000_000_000,\n h@.len() == g.len(),\n forall|q: int| 0 <= q < h@.len() ==> #[trigger] h@[q] as int == g[q],\n g == Self::ssort(ni.subrange(0, t as int)),\n Self::is_desc(g),\n forall|q: int| 0 <= q < g.len() ==> 1 <= #[trigger] g[q] <= 1_000_000_000,\n decreases nums.len() - t,\n {\n let v = nums[t];\n let vi = v as i64;\n assert(ni[t as int] == vi as int);\n let mut p: usize = 0;\n while p < h.len() && h[p] > vi\n invariant\n 0 <= p <= h@.len(),\n forall|q: int| 0 <= q < p ==> #[trigger] h@[q] > vi,\n h@.len() == g.len(),\n forall|q: int| 0 <= q < h@.len() ==> #[trigger] h@[q] as int == g[q],\n decreases h@.len() - p,\n {\n p += 1;\n }\n let ghost gold = g;\n proof {\n assert forall|q: int| 0 <= q < p implies g[q] > vi as int by {\n assert(h@[q] > vi);\n }\n if p < g.len() { assert(h@[p as int] <= vi); }\n }\n h.insert(p, vi);\n proof {\n g = gold.insert(p as int, vi as int);\n Self::lemma_insert_pos(gold, vi as int, p as int);\n Self::lemma_insert_desc_sorted(gold, vi as int);\n assert(g == Self::insert_desc(gold, vi as int));\n let pref1 = ni.subrange(0, (t + 1) as int);\n assert(pref1.drop_last() =~= ni.subrange(0, t as int));\n assert(pref1.last() == vi as int);\n assert(Self::ssort(pref1) == Self::insert_desc(Self::ssort(pref1.drop_last()), pref1.last()));\n assert forall|q: int| 0 <= q < h@.len() implies h@[q] as int == g[q] by {}\n assert(1 <= vi <= 1_000_000_000);\n }\n t += 1;\n }\n proof {\n assert(ni.subrange(0, nums.len() as int) =~= ni);\n Self::lemma_ssort_props(ni);\n }\n\n let ghost gfull = g;\n let mut ops: i32 = 0;\n while h.len() >= 2 && h[h.len() - 1] < k as i64\n invariant\n h@.len() == g.len(),\n forall|q: int| 0 <= q < h@.len() ==> #[trigger] h@[q] as int == g[q],\n Self::is_desc(g),\n 0 <= ops,\n nums@.len() <= 200_000,\n h@.len() + ops as int == nums@.len(),\n ops as int <= nums@.len(),\n 1 <= k <= 1_000_000_000,\n forall|q: int| 0 <= q < g.len() ==> 1 <= #[trigger] g[q] <= 1_000_000_000 + 2 * (k as int) * (ops as int),\n ops as int + Self::merge_ops(g, k as int) == Self::merge_ops(gfull, k as int),\n decreases h@.len(),\n {\n let ghost gold = g;\n let nh = h.len();\n let x = h[nh - 1];\n let y = h[nh - 2];\n proof {\n assert(g[nh as int - 1] == x as int);\n assert(g[nh as int - 2] == y as int);\n assert(x < k as i64);\n assert(1 <= y <= 1_000_000_000 + 2 * (k as int) * (ops as int));\n }\n assert(ops as int <= 200_000);\n assert(2 * (x as int) + (y as int) <= i64::MAX) by (nonlinear_arith)\n requires x < k as i64, 1 <= k <= 1_000_000_000,\n y as int <= 1_000_000_000 + 2 * (k as int) * (ops as int),\n ops as int <= 200_000;\n let merged = 2 * x + y;\n let _ = h.pop();\n let _ = h.pop();\n proof {\n g = gold.subrange(0, gold.len() - 2);\n assert(h@.len() == g.len());\n assert forall|q: int| 0 <= q < h@.len() implies h@[q] as int == g[q] by {}\n assert(Self::is_desc(g));\n }\n let mut p: usize = 0;\n while p < h.len() && h[p] > merged\n invariant\n 0 <= p <= h@.len(),\n forall|q: int| 0 <= q < p ==> #[trigger] h@[q] > merged,\n h@.len() == g.len(),\n forall|q: int| 0 <= q < h@.len() ==> #[trigger] h@[q] as int == g[q],\n decreases h@.len() - p,\n {\n p += 1;\n }\n let ghost gmid = g;\n proof {\n assert(forall|q: int| 0 <= q < p ==> g[q] > merged as int);\n assert(p < g.len() ==> g[p as int] <= merged as int);\n }\n h.insert(p, merged);\n proof {\n g = gmid.insert(p as int, merged as int);\n Self::lemma_insert_pos(gmid, merged as int, p as int);\n Self::lemma_insert_desc_sorted(gmid, merged as int);\n assert(merged as int == 2 * gold[gold.len() - 1] + gold[gold.len() - 2]);\n assert(gmid =~= gold.subrange(0, gold.len() - 2));\n assert(g == Self::insert_desc(gold.subrange(0, gold.len() - 2), 2 * gold[gold.len() - 1] + gold[gold.len() - 2]));\n assert(gold.len() >= 2);\n assert(gold[gold.len() - 1] < k as int);\n assert(Self::merge_ops(gold, k as int) == 1 + Self::merge_ops(g, k as int));\n assert forall|q: int| 0 <= q < h@.len() implies h@[q] as int == g[q] by {}\n assert(2 * (k as int) * (ops as int) <= 2 * (k as int) * ((ops + 1) as int)) by (nonlinear_arith)\n requires 1 <= k, 0 <= ops;\n assert(1 <= merged as int);\n assert(merged as int <= 1_000_000_000 + 2 * (k as int) * ((ops + 1) as int)) by (nonlinear_arith)\n requires x < k as i64, 1 <= x, 1 <= k <= 1_000_000_000,\n y as int <= 1_000_000_000 + 2 * (k as int) * (ops as int),\n merged as int == 2 * (x as int) + (y as int);\n assert forall|q: int| 0 <= q < g.len() implies 1 <= #[trigger] g[q] <= 1_000_000_000 + 2 * (k as int) * ((ops + 1) as int) by {\n if q == p as int {\n assert(g[q] == merged as int);\n } else if q < p as int {\n assert(g[q] == gmid[q]);\n assert(gmid[q] == gold[q]);\n } else {\n assert(g[q] == gmid[q - 1]);\n assert(gmid[q - 1] == gold[q - 1]);\n }\n }\n }\n ops = ops + 1;\n }\n proof {\n if h@.len() < 2 {\n assert(g.len() < 2);\n assert(Self::merge_ops(g, k as int) == 0);\n } else {\n assert(h@[h@.len() - 1] >= k as i64);\n assert(g[g.len() - 1] >= k as int);\n assert(Self::merge_ops(g, k as int) == 0);\n }\n }\n ops\n}\n\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3066/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3066/tests/mutated_testcases.jsonl"} {"problem": "lc3070", "family": "leetcode", "description": "# Count Submatrices with Top-Left Element and Sum Less Than k\n\nYou are given a **0-indexed** integer matrix `grid` and an integer `k`.\n\nReturn the number of submatrices that contain the top-left element of `grid`, and have a sum less than or equal to `k`.\n\n## Example 1\n\n> **Input:** `grid = [[7,6,3],[6,6,1]]`, `k = 18` \n> **Output:** `4` \n> **Explanation:** There are only 4 submatrices that contain the top-left element of `grid` and have a sum less than or equal to 18.\n\n## Example 2\n\n> **Input:** `grid = [[7,2,9],[1,5,0],[2,6,6]]`, `k = 20` \n> **Output:** `6` \n> **Explanation:** There are only 6 submatrices that contain the top-left element of `grid` and have a sum less than or equal to 20.\n\n## Constraints\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 1000`\n- `0 <= grid[i][j] <= 1000`\n- `1 <= k <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_submatrices(grid: Vec>, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Matrix, Prefix Sum\nAcceptance rate: 74.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn col_sum(grid: Seq>, rows: int, c: int) -> int\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::col_sum(grid, rows - 1, c) + grid[rows - 1][c] as int\n }\n }\n\n pub open spec fn rect_sum(grid: Seq>, rows: int, cols: int) -> int\n decreases cols,\n {\n if rows <= 0 || cols <= 0 {\n 0\n } else {\n Self::rect_sum(grid, rows, cols - 1) + Self::col_sum(grid, rows, cols - 1)\n }\n }\n\n pub open spec fn row_count(grid: Seq>, k: int, r: int, cols: int) -> int\n decreases cols,\n {\n if cols <= 0 {\n 0\n } else {\n Self::row_count(grid, k, r, cols - 1)\n + if Self::rect_sum(grid, r + 1, cols) <= k { 1int } else { 0int }\n }\n }\n\n pub open spec fn total_count(grid: Seq>, k: int, rows: int) -> int\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::total_count(grid, k, rows - 1)\n + Self::row_count(grid, k, rows - 1, grid[0].len() as int)\n }\n }\n\n pub open spec fn count_submatrices_spec(grid: Seq>, k: int) -> int {\n Self::total_count(grid, k, grid.len() as int)\n }\n\n pub fn count_submatrices(grid: Vec>, k: i32) -> (result: i32)\n requires\n 1 <= grid.len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n 1 <= grid[0].len() <= 1000,\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 1000,\n 1 <= k <= 1_000_000_000,\n ensures\n result as int == Self::count_submatrices_spec(grid@, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_submatrices(grid: Vec>, k: i32) -> i32 {\n let m = grid.len();\n let n = grid[0].len();\n let mut cols: Vec = Vec::new();\n let mut t: usize = 0;\n while t < n {\n cols.push(0);\n t = t + 1;\n }\n\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n let k64 = k as i64;\n while i < m {\n let mut row_prefix: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n let old_col = cols[j];\n let new_col = old_col + grid[i][j] as i64;\n cols[j] = new_col;\n row_prefix = row_prefix + new_col;\n if row_prefix <= k64 {\n ans = ans + 1;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n\n ans as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn col_sum(grid: Seq>, rows: int, c: int) -> int\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::col_sum(grid, rows - 1, c) + grid[rows - 1][c] as int\n }\n }\n\n pub open spec fn rect_sum(grid: Seq>, rows: int, cols: int) -> int\n decreases cols,\n {\n if rows <= 0 || cols <= 0 {\n 0\n } else {\n Self::rect_sum(grid, rows, cols - 1) + Self::col_sum(grid, rows, cols - 1)\n }\n }\n\n pub open spec fn row_count(grid: Seq>, k: int, r: int, cols: int) -> int\n decreases cols,\n {\n if cols <= 0 {\n 0\n } else {\n Self::row_count(grid, k, r, cols - 1)\n + if Self::rect_sum(grid, r + 1, cols) <= k { 1int } else { 0int }\n }\n }\n\n pub open spec fn total_count(grid: Seq>, k: int, rows: int) -> int\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::total_count(grid, k, rows - 1)\n + Self::row_count(grid, k, rows - 1, grid[0].len() as int)\n }\n }\n\n pub open spec fn count_submatrices_spec(grid: Seq>, k: int) -> int {\n Self::total_count(grid, k, grid.len() as int)\n }\n\n pub fn count_submatrices(grid: Vec>, k: i32) -> (result: i32)\n requires\n 1 <= grid.len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n 1 <= grid[0].len() <= 1000,\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 1000,\n 1 <= k <= 1_000_000_000,\n ensures\n result as int == Self::count_submatrices_spec(grid@, k as int),\n {\n let m = grid.len();\n let n = grid[0].len();\n let mut cols: Vec = Vec::new();\n let mut t: usize = 0;\n while t < n {\n cols.push(0);\n t = t + 1;\n }\n\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n let k64 = k as i64;\n while i < m {\n let mut row_prefix: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n let old_col = cols[j];\n let new_col = old_col + grid[i][j] as i64;\n cols.set(j, new_col);\n row_prefix = row_prefix + new_col;\n if row_prefix <= k64 {\n ans = ans + 1;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n\n ans as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn col_sum(grid: Seq>, rows: int, c: int) -> int\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::col_sum(grid, rows - 1, c) + grid[rows - 1][c] as int\n }\n }\n\n pub open spec fn rect_sum(grid: Seq>, rows: int, cols: int) -> int\n decreases cols,\n {\n if rows <= 0 || cols <= 0 {\n 0\n } else {\n Self::rect_sum(grid, rows, cols - 1) + Self::col_sum(grid, rows, cols - 1)\n }\n }\n\n pub open spec fn row_count(grid: Seq>, k: int, r: int, cols: int) -> int\n decreases cols,\n {\n if cols <= 0 {\n 0\n } else {\n Self::row_count(grid, k, r, cols - 1)\n + if Self::rect_sum(grid, r + 1, cols) <= k { 1int } else { 0int }\n }\n }\n\n pub open spec fn total_count(grid: Seq>, k: int, rows: int) -> int\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::total_count(grid, k, rows - 1)\n + Self::row_count(grid, k, rows - 1, grid[0].len() as int)\n }\n }\n\n pub open spec fn count_submatrices_spec(grid: Seq>, k: int) -> int {\n Self::total_count(grid, k, grid.len() as int)\n }\n\n pub open spec fn area_cols(rows: int, cols: int) -> int\n decreases cols,\n {\n if rows <= 0 || cols <= 0 {\n 0\n } else {\n Self::area_cols(rows, cols - 1) + rows\n }\n }\n\n pub open spec fn area_rows(rows: int, cols: int) -> int\n decreases rows,\n {\n if rows <= 0 || cols <= 0 {\n 0\n } else {\n Self::area_rows(rows - 1, cols) + cols\n }\n }\n\n proof fn lemma_cmp_i64_i32(x: i64, y: i32)\n requires\n 0 <= x <= 1_000_000_000,\n 1 <= y <= 1_000_000_000,\n ensures\n (x <= y as i64) <==> (x as int <= y as int),\n {\n }\n\n proof fn lemma_area_cols_bound(rows: int, cols: int)\n requires\n 0 <= rows <= 1000,\n 0 <= cols <= 1000,\n ensures\n 0 <= Self::area_cols(rows, cols),\n Self::area_cols(rows, cols) <= 1000 * cols,\n Self::area_cols(rows, cols) <= 1_000_000,\n decreases cols,\n {\n if rows <= 0 || cols <= 0 {\n } else {\n Self::lemma_area_cols_bound(rows, cols - 1);\n assert(Self::area_cols(rows, cols) == Self::area_cols(rows, cols - 1) + rows);\n assert(Self::area_cols(rows, cols - 1) <= 1000 * (cols - 1));\n assert(rows <= 1000);\n assert(1000 * (cols - 1) + rows <= 1000 * cols);\n }\n }\n\n proof fn lemma_area_rows_bound(rows: int, cols: int)\n requires\n 0 <= rows <= 1000,\n 0 <= cols <= 1000,\n ensures\n 0 <= Self::area_rows(rows, cols),\n Self::area_rows(rows, cols) <= 1000 * rows,\n Self::area_rows(rows, cols) <= 1_000_000,\n decreases rows,\n {\n if rows <= 0 || cols <= 0 {\n } else {\n Self::lemma_area_rows_bound(rows - 1, cols);\n assert(Self::area_rows(rows, cols) == Self::area_rows(rows - 1, cols) + cols);\n assert(Self::area_rows(rows - 1, cols) <= 1000 * (rows - 1));\n assert(cols <= 1000);\n assert(1000 * (rows - 1) + cols <= 1000 * rows);\n }\n }\n\n proof fn lemma_col_sum_bound(grid: Seq>, rows: int, c: int)\n requires\n 0 < grid.len() <= 1000,\n 0 < grid[0].len() <= 1000,\n 0 <= rows <= grid.len(),\n 0 <= c < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, j: int| 0 <= r < grid.len() && 0 <= j < grid[r].len() ==> 0 <= #[trigger] grid[r][j] <= 1000,\n ensures\n 0 <= Self::col_sum(grid, rows, c),\n Self::col_sum(grid, rows, c) <= 1000 * rows,\n Self::col_sum(grid, rows, c) <= 1_000_000,\n decreases rows,\n {\n if rows <= 0 {\n } else {\n Self::lemma_col_sum_bound(grid, rows - 1, c);\n assert(Self::col_sum(grid, rows, c) == Self::col_sum(grid, rows - 1, c) + grid[rows - 1][c] as int);\n assert(Self::col_sum(grid, rows - 1, c) <= 1000 * (rows - 1));\n assert(0 <= grid[rows - 1][c] <= 1000);\n assert(1000 * (rows - 1) + grid[rows - 1][c] as int <= 1000 * rows);\n }\n }\n\n proof fn lemma_rect_sum_bound(grid: Seq>, rows: int, cols: int)\n requires\n 0 < grid.len() <= 1000,\n 0 < grid[0].len() <= 1000,\n 0 <= rows <= grid.len(),\n 0 <= cols <= grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, j: int| 0 <= r < grid.len() && 0 <= j < grid[r].len() ==> 0 <= #[trigger] grid[r][j] <= 1000,\n ensures\n 0 <= Self::rect_sum(grid, rows, cols),\n Self::rect_sum(grid, rows, cols) <= 1000 * Self::area_cols(rows, cols),\n Self::rect_sum(grid, rows, cols) <= 1_000_000_000,\n decreases cols,\n {\n if rows <= 0 || cols <= 0 {\n } else {\n Self::lemma_rect_sum_bound(grid, rows, cols - 1);\n Self::lemma_col_sum_bound(grid, rows, cols - 1);\n Self::lemma_area_cols_bound(rows, cols);\n assert(Self::rect_sum(grid, rows, cols)\n == Self::rect_sum(grid, rows, cols - 1) + Self::col_sum(grid, rows, cols - 1));\n assert(Self::rect_sum(grid, rows, cols - 1) <= 1000 * Self::area_cols(rows, cols - 1));\n assert(Self::col_sum(grid, rows, cols - 1) <= 1000 * rows);\n assert(Self::area_cols(rows, cols) == Self::area_cols(rows, cols - 1) + rows);\n assert(1000 * Self::area_cols(rows, cols - 1) + 1000 * rows\n == 1000 * Self::area_cols(rows, cols));\n assert(Self::rect_sum(grid, rows, cols) <= 1000 * Self::area_cols(rows, cols));\n assert(Self::area_cols(rows, cols) <= 1_000_000);\n }\n }\n\n proof fn lemma_row_count_bound(grid: Seq>, k: int, r: int, cols: int)\n requires\n 0 < grid.len(),\n 0 < grid[0].len(),\n 0 <= r < grid.len(),\n 0 <= cols <= grid[0].len(),\n ensures\n 0 <= Self::row_count(grid, k, r, cols),\n Self::row_count(grid, k, r, cols) <= cols,\n decreases cols,\n {\n if cols <= 0 {\n } else {\n Self::lemma_row_count_bound(grid, k, r, cols - 1);\n assert(Self::row_count(grid, k, r, cols)\n == Self::row_count(grid, k, r, cols - 1)\n + if Self::rect_sum(grid, r + 1, cols) <= k { 1int } else { 0int });\n }\n }\n\n proof fn lemma_total_count_bound(grid: Seq>, k: int, rows: int)\n requires\n 0 < grid.len() <= 1000,\n 0 < grid[0].len() <= 1000,\n 0 <= rows <= grid.len(),\n ensures\n 0 <= Self::total_count(grid, k, rows),\n Self::total_count(grid, k, rows) <= Self::area_rows(rows, grid[0].len() as int),\n Self::total_count(grid, k, rows) <= 1_000_000,\n decreases rows,\n {\n if rows <= 0 {\n } else {\n Self::lemma_total_count_bound(grid, k, rows - 1);\n Self::lemma_row_count_bound(grid, k, rows - 1, grid[0].len() as int);\n Self::lemma_area_rows_bound(rows, grid[0].len() as int);\n assert(Self::total_count(grid, k, rows)\n == Self::total_count(grid, k, rows - 1)\n + Self::row_count(grid, k, rows - 1, grid[0].len() as int));\n assert(Self::total_count(grid, k, rows - 1)\n <= Self::area_rows(rows - 1, grid[0].len() as int));\n assert(Self::row_count(grid, k, rows - 1, grid[0].len() as int)\n <= grid[0].len() as int);\n assert(Self::area_rows(rows, grid[0].len() as int)\n == Self::area_rows(rows - 1, grid[0].len() as int) + grid[0].len() as int);\n assert(Self::total_count(grid, k, rows)\n <= Self::area_rows(rows, grid[0].len() as int));\n assert(Self::area_rows(rows, grid[0].len() as int) <= 1_000_000);\n }\n }\n\n pub fn count_submatrices(grid: Vec>, k: i32) -> (result: i32)\n requires\n 1 <= grid.len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n 1 <= grid[0].len() <= 1000,\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 1000,\n 1 <= k <= 1_000_000_000,\n ensures\n result as int == Self::count_submatrices_spec(grid@, k as int),\n {\n let m = grid.len();\n let n = grid[0].len();\n\n let mut cols: Vec = Vec::new();\n let mut t: usize = 0;\n while t < n\n invariant\n n == grid[0].len(),\n 1 <= n <= 1000,\n 0 <= t <= n,\n cols.len() == t,\n forall |c: int| 0 <= c < t ==> #[trigger] cols[c] == 0,\n decreases n - t,\n {\n cols.push(0);\n t = t + 1;\n }\n\n let mut ans: i64 = 0;\n let mut i: usize = 0;\n let k64 = k as i64;\n while i < m\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < n ==> 0 <= #[trigger] grid[r][c] <= 1000,\n 1 <= k <= 1_000_000_000,\n 0 <= i <= m,\n k64 == k as i64,\n cols.len() == n,\n forall |c: int| 0 <= c < n ==> #[trigger] cols[c] as int == Self::col_sum(grid@, i as int, c),\n forall |c: int| 0 <= c < n ==> 0 <= #[trigger] cols[c] <= 1_000_000,\n ans as int == Self::total_count(grid@, k as int, i as int),\n 0 <= ans <= 1_000_000,\n decreases m - i,\n {\n let mut row_prefix: i64 = 0;\n let mut j: usize = 0;\n while j < n\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < n ==> 0 <= #[trigger] grid[r][c] <= 1000,\n 1 <= k <= 1_000_000_000,\n 0 <= i < m,\n 0 <= j <= n,\n k64 == k as i64,\n cols.len() == n,\n forall |c: int| 0 <= c < j ==> #[trigger] cols[c] as int == Self::col_sum(grid@, i as int + 1, c),\n forall |c: int| j <= c < n ==> #[trigger] cols[c] as int == Self::col_sum(grid@, i as int, c),\n forall |c: int| 0 <= c < j ==> 0 <= #[trigger] cols[c] <= 1_000_000,\n forall |c: int| j <= c < n ==> 0 <= #[trigger] cols[c] <= 1_000_000,\n row_prefix as int == Self::rect_sum(grid@, i as int + 1, j as int),\n 0 <= row_prefix <= 1_000_000_000,\n ans as int\n == Self::total_count(grid@, k as int, i as int)\n + Self::row_count(grid@, k as int, i as int, j as int),\n 0 <= ans <= 1_000_000,\n decreases n - j,\n {\n let ghost j_old = j as int;\n let ghost ans_old = ans as int;\n let ghost cols_before = cols@;\n\n proof {\n assert(i < m);\n assert(j < n);\n assert(grid[i as int].len() == n);\n assert((j as int) < grid[i as int].len());\n }\n\n let old_col = cols[j];\n let new_col = old_col + grid[i][j] as i64;\n cols.set(j, new_col);\n row_prefix = row_prefix + new_col;\n\n if row_prefix <= k64 {\n ans = ans + 1;\n }\n\n proof {\n assert(old_col as int == Self::col_sum(grid@, i as int, j_old));\n assert(Self::col_sum(grid@, i as int + 1, j_old)\n == Self::col_sum(grid@, i as int, j_old) + grid@[i as int][j_old] as int);\n assert(new_col as int == Self::col_sum(grid@, i as int + 1, j_old));\n\n assert(Self::rect_sum(grid@, i as int + 1, j_old + 1)\n == Self::rect_sum(grid@, i as int + 1, j_old)\n + Self::col_sum(grid@, i as int + 1, j_old));\n assert(row_prefix as int == Self::rect_sum(grid@, i as int + 1, j_old + 1));\n\n assert(Self::row_count(grid@, k as int, i as int, j_old + 1)\n == Self::row_count(grid@, k as int, i as int, j_old)\n + if Self::rect_sum(grid@, i as int + 1, j_old + 1) <= k as int { 1int } else { 0int });\n\n Self::lemma_rect_sum_bound(grid@, i as int + 1, j_old + 1);\n assert(0 <= row_prefix <= 1_000_000_000);\n Self::lemma_cmp_i64_i32(row_prefix, k);\n assert(k64 == k as i64);\n assert((row_prefix <= k64) <==> (row_prefix as int <= k as int));\n if row_prefix <= k64 {\n assert(row_prefix as int <= k as int);\n assert(ans as int == ans_old + 1);\n } else {\n assert(!(row_prefix as int <= k as int));\n assert(ans as int == ans_old);\n }\n\n assert(ans as int\n == Self::total_count(grid@, k as int, i as int)\n + Self::row_count(grid@, k as int, i as int, j_old + 1));\n\n assert forall |c: int| 0 <= c < j_old + 1 implies cols[c] as int == Self::col_sum(grid@, i as int + 1, c) by {\n if c < j_old {\n assert(cols[c] == cols_before[c]);\n } else {\n assert(c == j_old);\n }\n }\n\n assert forall |c: int| j_old + 1 <= c < n implies cols[c] as int == Self::col_sum(grid@, i as int, c) by {\n assert(cols[c] == cols_before[c]);\n }\n\n assert forall |c: int| 0 <= c < j_old + 1 implies 0 <= #[trigger] cols[c] <= 1_000_000 by {\n if c < j_old {\n assert(cols[c] == cols_before[c]);\n } else {\n assert(c == j_old);\n Self::lemma_col_sum_bound(grid@, i as int + 1, j_old);\n assert(cols[j_old] as int == Self::col_sum(grid@, i as int + 1, j_old));\n assert(0 <= cols[j_old] <= 1_000_000);\n assert(cols[c] == cols[j_old]);\n }\n }\n\n assert forall |c: int| j_old + 1 <= c < n implies 0 <= #[trigger] cols[c] <= 1_000_000 by {\n assert(cols[c] == cols_before[c]);\n }\n\n Self::lemma_total_count_bound(grid@, k as int, i as int);\n Self::lemma_row_count_bound(grid@, k as int, i as int, j_old + 1);\n Self::lemma_area_rows_bound(i as int, n as int);\n assert(Self::total_count(grid@, k as int, i as int) <= Self::area_rows(i as int, n as int));\n assert(Self::row_count(grid@, k as int, i as int, j_old + 1) <= j_old + 1);\n assert(j_old + 1 <= n as int);\n assert(ans as int <= Self::area_rows(i as int, n as int) + n as int);\n assert(Self::area_rows(i as int, n as int) <= 1000 * i as int);\n assert(i < m);\n assert(m <= 1000);\n assert(i as int <= 999);\n assert(1000 * i as int + n as int <= 1_000_000);\n assert(0 <= ans <= 1_000_000);\n }\n\n j = j + 1;\n }\n\n proof {\n assert(j == n);\n assert(Self::total_count(grid@, k as int, i as int + 1)\n == Self::total_count(grid@, k as int, i as int)\n + Self::row_count(grid@, k as int, i as int, n as int));\n assert(ans as int == Self::total_count(grid@, k as int, i as int + 1));\n assert forall |c: int| 0 <= c < n implies cols[c] as int == Self::col_sum(grid@, i as int + 1, c) by {\n }\n Self::lemma_total_count_bound(grid@, k as int, i as int + 1);\n assert(0 <= ans <= 1_000_000);\n }\n\n i = i + 1;\n }\n\n proof {\n assert(i == m);\n assert(ans as int == Self::count_submatrices_spec(grid@, k as int));\n Self::lemma_total_count_bound(grid@, k as int, m as int);\n assert(0 <= ans <= 1_000_000);\n }\n\n ans as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3070/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3070/tests/mutated_testcases.jsonl"} {"problem": "lc3095", "family": "leetcode", "description": "# Shortest Subarray With OR at Least K I\n\nYou are given an array `nums` of non-negative integers and an integer `k`.\n\nA subarray is called special if the bitwise OR of all its elements is at least `k`.\n\nReturn the length of the shortest non-empty special subarray of `nums`, or `-1` if no such subarray exists.\n\n## Example 1\n\n> **Input:** `nums = [1,2,3]`, `k = 2` \n> **Output:** `1` \n> **Explanation:** `[3]` has OR `3`, so the answer is `1`. (`[2]` also works.)\n\n## Example 2\n\n> **Input:** `nums = [2,1,8]`, `k = 10` \n> **Output:** `3` \n> **Explanation:** `[2,1,8]` has OR `11`, so the answer is `3`.\n\n## Example 3\n\n> **Input:** `nums = [1,2]`, `k = 0` \n> **Output:** `1` \n> **Explanation:** `[1]` has OR `1`, so the answer is `1`.\n\n## Constraints\n\n- `1 <= nums.length <= 50`\n- `0 <= nums[i] <= 50`\n- `0 <= k < 64`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Bit Manipulation, Sliding Window\nAcceptance rate: 44.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn imin(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn sub_or(nums: Seq, start: int, end: int) -> i32\n decreases end - start,\n {\n if end <= start {\n 0i32\n } else {\n Self::sub_or(nums, start, end - 1) | nums[end - 1]\n }\n }\n\n pub open spec fn min_len_start_upto(nums: Seq, k: i32, start: int, upto: int) -> int\n decreases upto - start,\n {\n if upto <= start {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_start_upto(nums, k, start, upto - 1);\n let cand = if Self::sub_or(nums, start, upto) >= k {\n upto - start\n } else {\n nums.len() as int + 1\n };\n Self::imin(prev, cand)\n }\n }\n\n pub open spec fn min_len_prefix(nums: Seq, k: i32, processed: int) -> int\n decreases processed,\n {\n if processed <= 0 {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_prefix(nums, k, processed - 1);\n let cur = Self::min_len_start_upto(nums, k, processed - 1, nums.len() as int);\n Self::imin(prev, cur)\n }\n }\n\n pub open spec fn minimum_subarray_length_spec(nums: Seq, k: i32) -> int {\n let best = Self::min_len_prefix(nums, k, nums.len() as int);\n if best <= nums.len() as int { best } else { -1 }\n }\n\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 50,\n 0 <= k < 64,\n ensures\n result as int == Self::minimum_subarray_length_spec(nums@, k),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> i32 {\n let n = nums.len();\n let mut ans: i32 = n as i32 + 1;\n let mut i: usize = 0;\n while i < n {\n let mut cur_or: i32 = 0;\n let mut j: usize = i;\n let mut best_i: i32 = n as i32 + 1;\n while j < n {\n cur_or = cur_or | nums[j];\n let cand: i32;\n if cur_or >= k {\n cand = (j - i + 1) as i32;\n } else {\n cand = n as i32 + 1;\n }\n if cand < best_i {\n best_i = cand;\n }\n j = j + 1;\n }\n if best_i < ans {\n ans = best_i;\n }\n i = i + 1;\n }\n if ans <= n as i32 {\n ans\n } else {\n -1\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn imin(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn sub_or(nums: Seq, start: int, end: int) -> i32\n decreases end - start,\n {\n if end <= start {\n 0i32\n } else {\n Self::sub_or(nums, start, end - 1) | nums[end - 1]\n }\n }\n\n pub open spec fn min_len_start_upto(nums: Seq, k: i32, start: int, upto: int) -> int\n decreases upto - start,\n {\n if upto <= start {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_start_upto(nums, k, start, upto - 1);\n let cand = if Self::sub_or(nums, start, upto) >= k {\n upto - start\n } else {\n nums.len() as int + 1\n };\n Self::imin(prev, cand)\n }\n }\n\n pub open spec fn min_len_prefix(nums: Seq, k: i32, processed: int) -> int\n decreases processed,\n {\n if processed <= 0 {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_prefix(nums, k, processed - 1);\n let cur = Self::min_len_start_upto(nums, k, processed - 1, nums.len() as int);\n Self::imin(prev, cur)\n }\n }\n\n pub open spec fn minimum_subarray_length_spec(nums: Seq, k: i32) -> int {\n let best = Self::min_len_prefix(nums, k, nums.len() as int);\n if best <= nums.len() as int { best } else { -1 }\n }\n\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 50,\n 0 <= k < 64,\n ensures\n result as int == Self::minimum_subarray_length_spec(nums@, k),\n {\n let n = nums.len();\n let mut ans: i32 = n as i32 + 1;\n let mut i: usize = 0;\n while i < n {\n let mut cur_or: i32 = 0;\n let mut j: usize = i;\n let mut best_i: i32 = n as i32 + 1;\n while j < n {\n cur_or = cur_or | nums[j];\n let cand: i32;\n if cur_or >= k {\n cand = (j - i + 1) as i32;\n } else {\n cand = n as i32 + 1;\n }\n if cand < best_i {\n best_i = cand;\n }\n j = j + 1;\n }\n if best_i < ans {\n ans = best_i;\n }\n i = i + 1;\n }\n if ans <= n as i32 {\n ans\n } else {\n -1\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn imin(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn sub_or(nums: Seq, start: int, end: int) -> i32\n decreases end - start,\n {\n if end <= start {\n 0i32\n } else {\n Self::sub_or(nums, start, end - 1) | nums[end - 1]\n }\n }\n\n pub open spec fn min_len_start_upto(nums: Seq, k: i32, start: int, upto: int) -> int\n decreases upto - start,\n {\n if upto <= start {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_start_upto(nums, k, start, upto - 1);\n let cand = if Self::sub_or(nums, start, upto) >= k {\n upto - start\n } else {\n nums.len() as int + 1\n };\n Self::imin(prev, cand)\n }\n }\n\n pub open spec fn min_len_prefix(nums: Seq, k: i32, processed: int) -> int\n decreases processed,\n {\n if processed <= 0 {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_prefix(nums, k, processed - 1);\n let cur = Self::min_len_start_upto(nums, k, processed - 1, nums.len() as int);\n Self::imin(prev, cur)\n }\n }\n\n pub open spec fn minimum_subarray_length_spec(nums: Seq, k: i32) -> int {\n let best = Self::min_len_prefix(nums, k, nums.len() as int);\n if best <= nums.len() as int { best } else { -1 }\n }\n\n proof fn lemma_sub_or_extend(nums: Seq, start: int, end: int)\n requires\n 0 <= start <= end < nums.len(),\n ensures\n Self::sub_or(nums, start, end + 1) == (Self::sub_or(nums, start, end) | nums[end]),\n {\n reveal_with_fuel(Solution::sub_or, 2);\n }\n\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 50,\n 0 <= k < 64,\n ensures\n result as int == Self::minimum_subarray_length_spec(nums@, k),\n {\n let n = nums.len();\n let mut ans: i32 = n as i32 + 1;\n let mut i: usize = 0;\n\n while i < n\n invariant\n 1 <= n <= 50,\n n == nums.len(),\n forall |j: int| 0 <= j < nums.len() ==> 0 <= #[trigger] nums[j] <= 50,\n 0 <= k < 64,\n 0 <= i <= n,\n 1 <= ans <= n as i32 + 1,\n ans as int == Self::min_len_prefix(nums@, k, i as int),\n decreases n - i,\n {\n let mut cur_or: i32 = 0;\n let mut j: usize = i;\n let mut best_i: i32 = n as i32 + 1;\n\n while j < n\n invariant\n 1 <= n <= 50,\n n == nums.len(),\n forall |t: int| 0 <= t < nums.len() ==> 0 <= #[trigger] nums[t] <= 50,\n 0 <= k < 64,\n 0 <= i < n,\n i <= j <= n,\n 1 <= best_i <= n as i32 + 1,\n cur_or == Self::sub_or(nums@, i as int, j as int),\n best_i as int == Self::min_len_start_upto(nums@, k, i as int, j as int),\n decreases n - j,\n {\n cur_or = cur_or | nums[j];\n\n let cand: i32;\n if cur_or >= k {\n cand = (j - i + 1) as i32;\n } else {\n cand = n as i32 + 1;\n }\n\n let ghost old_best = best_i as int;\n if cand < best_i {\n best_i = cand;\n }\n\n proof {\n Self::lemma_sub_or_extend(nums@, i as int, j as int);\n assert(cur_or == Self::sub_or(nums@, i as int, (j + 1) as int));\n\n assert(Self::min_len_start_upto(nums@, k, i as int, (j + 1) as int)\n == Self::imin(\n Self::min_len_start_upto(nums@, k, i as int, j as int),\n if Self::sub_or(nums@, i as int, (j + 1) as int) >= k {\n (j + 1) as int - i as int\n } else {\n n as int + 1\n }\n ));\n assert(old_best == Self::min_len_start_upto(nums@, k, i as int, j as int));\n assert(cand as int\n == if Self::sub_or(nums@, i as int, (j + 1) as int) >= k {\n (j + 1) as int - i as int\n } else {\n n as int + 1\n });\n if cand < old_best as i32 {\n assert(best_i as int == cand as int);\n } else {\n assert(best_i as int == old_best);\n }\n assert(best_i as int == Self::imin(old_best, cand as int));\n }\n\n j = j + 1;\n }\n\n if best_i < ans {\n ans = best_i;\n }\n\n proof {\n assert(j == n);\n assert(ans as int\n == Self::imin(Self::min_len_prefix(nums@, k, i as int), Self::min_len_start_upto(nums@, k, i as int, n as int)));\n assert(Self::min_len_prefix(nums@, k, (i + 1) as int)\n == Self::imin(Self::min_len_prefix(nums@, k, i as int), Self::min_len_start_upto(nums@, k, i as int, n as int)));\n assert(ans as int == Self::min_len_prefix(nums@, k, (i + 1) as int));\n }\n\n i = i + 1;\n }\n\n if ans <= n as i32 {\n ans\n } else {\n -1\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3095/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3095/tests/mutated_testcases.jsonl"} {"problem": "lc3096", "family": "leetcode", "description": "# Minimum Levels to Gain More Points\n\nYou are given a binary array `possible` of length `n`.\n\nAlice and Bob are playing a game with `n` levels. If `possible[i] == 1`, then level `i` can be cleared. If `possible[i] == 0`, then level `i` cannot be cleared. Clearing a level gives `+1` point, and failing a level gives `-1` point.\n\nAlice plays a prefix of levels in order, starting from level `0`. Bob plays all remaining levels.\n\nReturn the minimum number of levels Alice should play so that Alice gets strictly more points than Bob. If no such split exists, return `-1`.\n\nEach player must play at least one level.\n\n## Example 1:\n\n> **Input:** possible = [1,0,1,0]\n> **Output:** 1\n> **Explanation:**\n> - If Alice plays 1 level: Alice = 1, Bob = -1.\n> - If Alice plays 2 levels: Alice = 0, Bob = 0.\n> - If Alice plays 3 levels: Alice = 1, Bob = -1.\n> The minimum is 1.\n\n## Example 2:\n\n> **Input:** possible = [1,1,1,1,1]\n> **Output:** 3\n> **Explanation:**\n> - If Alice plays 1 level: Alice = 1, Bob = 4.\n> - If Alice plays 2 levels: Alice = 2, Bob = 3.\n> - If Alice plays 3 levels: Alice = 3, Bob = 2.\n> The minimum is 3.\n\n## Example 3:\n\n> **Input:** possible = [0,0]\n> **Output:** -1\n> **Explanation:**\n> The only split gives Alice = -1 and Bob = -1, so Alice is not strictly ahead.\n\n## Constraints:\n\n- `2 <= n == possible.length <= 10^5`\n- `possible[i]` is either `0` or `1`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_levels(possible: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Prefix Sum\nAcceptance rate: 40.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn level_score(x: i32) -> int {\n if x == 1 { 1 } else { -1 }\n }\n\n pub open spec fn prefix_score(possible: Seq, end: int) -> int\n recommends\n 0 <= end <= possible.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_score(possible, end - 1) + Self::level_score(possible[end - 1])\n }\n }\n\n pub open spec fn alice_wins_after_k(possible: Seq, k: int) -> bool {\n &&& 1 <= k < possible.len()\n &&& 2 * Self::prefix_score(possible, k) > Self::prefix_score(possible, possible.len() as int)\n }\n\n pub fn minimum_levels(possible: Vec) -> (result: i32)\n requires\n 2 <= possible.len() <= 100000,\n forall |i: int| 0 <= i < possible.len() ==> (#[trigger] possible[i] == 0 || #[trigger] possible[i] == 1),\n ensures\n result == -1 ==> forall |k: int| 1 <= k < possible.len() ==> !Self::alice_wins_after_k(possible@, k),\n result != -1 ==> (\n 1 <= result as int && (result as int) < possible.len()\n && Self::alice_wins_after_k(possible@, result as int)\n && forall |k: int| 1 <= k < result as int ==> !Self::alice_wins_after_k(possible@, k)\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_levels(possible: Vec) -> i32 {\n let n = possible.len();\n let mut total: i64 = 0;\n let mut i: usize = 0;\n\n while i < n {\n let delta: i64 = if possible[i] == 1 { 1 } else { -1 };\n total = total + delta;\n i += 1;\n }\n\n let mut prefix: i64 = 0;\n i = 0;\n\n while i < n - 1 {\n let delta: i64 = if possible[i] == 1 { 1 } else { -1 };\n prefix = prefix + delta;\n if 2 * prefix > total {\n return (i + 1) as i32;\n }\n i += 1;\n }\n\n -1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn level_score(x: i32) -> int {\n if x == 1 { 1 } else { -1 }\n }\n\n pub open spec fn prefix_score(possible: Seq, end: int) -> int\n recommends\n 0 <= end <= possible.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_score(possible, end - 1) + Self::level_score(possible[end - 1])\n }\n }\n\n pub open spec fn alice_wins_after_k(possible: Seq, k: int) -> bool {\n &&& 1 <= k < possible.len()\n &&& 2 * Self::prefix_score(possible, k) > Self::prefix_score(possible, possible.len() as int)\n }\n\n pub fn minimum_levels(possible: Vec) -> (result: i32)\n requires\n 2 <= possible.len() <= 100000,\n forall |i: int| 0 <= i < possible.len() ==> (#[trigger] possible[i] == 0 || #[trigger] possible[i] == 1),\n ensures\n result == -1 ==> forall |k: int| 1 <= k < possible.len() ==> !Self::alice_wins_after_k(possible@, k),\n result != -1 ==> (\n 1 <= result as int && (result as int) < possible.len()\n && Self::alice_wins_after_k(possible@, result as int)\n && forall |k: int| 1 <= k < result as int ==> !Self::alice_wins_after_k(possible@, k)\n ),\n {\n let n = possible.len();\n let mut total: i64 = 0;\n let mut i: usize = 0;\n\n while i < n {\n let delta: i64 = if possible[i] == 1 { 1 } else { -1 };\n total = total + delta;\n i += 1;\n }\n\n let mut prefix: i64 = 0;\n i = 0;\n\n while i < n - 1 {\n let delta: i64 = if possible[i] == 1 { 1 } else { -1 };\n prefix = prefix + delta;\n if 2 * prefix > total {\n return (i + 1) as i32;\n }\n i += 1;\n }\n\n -1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn level_score(x: i32) -> int {\n if x == 1 { 1 } else { -1 }\n }\n\n pub open spec fn prefix_score(possible: Seq, end: int) -> int\n recommends\n 0 <= end <= possible.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::prefix_score(possible, end - 1) + Self::level_score(possible[end - 1])\n }\n }\n\n pub open spec fn alice_wins_after_k(possible: Seq, k: int) -> bool {\n &&& 1 <= k < possible.len()\n &&& 2 * Self::prefix_score(possible, k) > Self::prefix_score(possible, possible.len() as int)\n }\n\n proof fn lemma_prefix_step(possible: Seq, i: int)\n requires\n 0 <= i < possible.len(),\n ensures\n Self::prefix_score(possible, i + 1) == Self::prefix_score(possible, i) + Self::level_score(possible[i]),\n {\n }\n\n pub fn minimum_levels(possible: Vec) -> (result: i32)\n requires\n 2 <= possible.len() <= 100000,\n forall |i: int| 0 <= i < possible.len() ==> (#[trigger] possible[i] == 0 || #[trigger] possible[i] == 1),\n ensures\n result == -1 ==> forall |k: int| 1 <= k < possible.len() ==> !Self::alice_wins_after_k(possible@, k),\n result != -1 ==> (\n 1 <= result as int && (result as int) < possible.len()\n && Self::alice_wins_after_k(possible@, result as int)\n && forall |k: int| 1 <= k < result as int ==> !Self::alice_wins_after_k(possible@, k)\n ),\n {\n let n = possible.len();\n let mut total: i64 = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n n == possible.len(),\n 2 <= n <= 100000,\n 0 <= i <= n,\n forall |k: int| 0 <= k < n ==> (#[trigger] possible[k] == 0 || #[trigger] possible[k] == 1),\n total as int == Self::prefix_score(possible@, i as int),\n -(i as int) <= total as int <= i as int,\n decreases n - i,\n {\n let delta: i64 = if possible[i] == 1 { 1 } else { -1 };\n proof {\n Self::lemma_prefix_step(possible@, i as int);\n assert(delta as int == Self::level_score(possible@[i as int]));\n }\n total = total + delta;\n proof {\n assert(total as int == Self::prefix_score(possible@, i as int + 1));\n assert(-(i as int + 1) <= total as int <= i as int + 1);\n }\n i += 1;\n }\n\n let mut prefix: i64 = 0;\n i = 0;\n\n while i < n - 1\n invariant\n n == possible.len(),\n 2 <= n <= 100000,\n 0 <= i <= n - 1,\n forall |k: int| 0 <= k < n ==> (#[trigger] possible[k] == 0 || #[trigger] possible[k] == 1),\n total as int == Self::prefix_score(possible@, n as int),\n prefix as int == Self::prefix_score(possible@, i as int),\n -(i as int) <= prefix as int <= i as int,\n forall |k: int| 1 <= k <= i as int ==> !Self::alice_wins_after_k(possible@, k),\n decreases n - 1 - i,\n {\n let delta: i64 = if possible[i] == 1 { 1 } else { -1 };\n proof {\n Self::lemma_prefix_step(possible@, i as int);\n assert(delta as int == Self::level_score(possible@[i as int]));\n }\n prefix = prefix + delta;\n proof {\n assert(prefix as int == Self::prefix_score(possible@, i as int + 1));\n assert(-(i as int + 1) <= prefix as int <= i as int + 1);\n }\n if 2 * prefix > total {\n proof {\n assert(1 <= i as int + 1 < n as int);\n assert(Self::alice_wins_after_k(possible@, i as int + 1));\n assert(forall |k: int| 1 <= k < i as int + 1 ==> !Self::alice_wins_after_k(possible@, k)) by {\n assert forall |k: int| 1 <= k < i as int + 1 implies !Self::alice_wins_after_k(possible@, k) by {\n assert(1 <= k <= i as int);\n }\n };\n assert(i + 1 <= i32::MAX as usize);\n }\n return (i + 1) as i32;\n } else {\n proof {\n assert(!(Self::alice_wins_after_k(possible@, i as int + 1)));\n }\n }\n i += 1;\n }\n\n proof {\n assert(i == n - 1);\n assert(forall |k: int| 1 <= k < possible.len() ==> !Self::alice_wins_after_k(possible@, k)) by {\n assert forall |k: int| 1 <= k < possible.len() implies !Self::alice_wins_after_k(possible@, k) by {\n assert(1 <= k <= i as int);\n }\n };\n }\n\n -1\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3096/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3096/tests/mutated_testcases.jsonl"} {"problem": "lc3097", "family": "leetcode", "description": "# Shortest Subarray With OR at Least K II\n\nYou are given an array `nums` of non-negative integers and an integer `k`.\n\nA subarray is called special if the bitwise OR of all its elements is at least `k`.\n\nReturn the length of the shortest non-empty special subarray of `nums`, or `-1` if no such subarray exists.\n\n## Example 1\n\n> **Input:** `nums = [1,2,3]`, `k = 2` \n> **Output:** `1` \n> **Explanation:** `[3]` has OR `3`, so the answer is `1`. (`[2]` also works.)\n\n## Example 2\n\n> **Input:** `nums = [2,1,8]`, `k = 10` \n> **Output:** `3` \n> **Explanation:** `[2,1,8]` has OR `11`, so the answer is `3`.\n\n## Example 3\n\n> **Input:** `nums = [1,2]`, `k = 0` \n> **Output:** `1` \n> **Explanation:** `[1]` has OR `1`, so the answer is `1`.\n\n## Constraints\n\n- `1 <= nums.length <= 2 * 10^5`\n- `0 <= nums[i] <= 10^9`\n- `0 <= k <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Bit Manipulation, Sliding Window\nAcceptance rate: 50.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn imin(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn sub_or(nums: Seq, start: int, end: int) -> i32\n decreases end - start,\n {\n if end <= start {\n 0i32\n } else {\n Self::sub_or(nums, start, end - 1) | nums[end - 1]\n }\n }\n\n pub open spec fn min_len_start_upto(nums: Seq, k: i32, start: int, upto: int) -> int\n decreases upto - start,\n {\n if upto <= start {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_start_upto(nums, k, start, upto - 1);\n let cand = if Self::sub_or(nums, start, upto) >= k {\n upto - start\n } else {\n nums.len() as int + 1\n };\n Self::imin(prev, cand)\n }\n }\n\n pub open spec fn min_len_prefix(nums: Seq, k: i32, processed: int) -> int\n decreases processed,\n {\n if processed <= 0 {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_prefix(nums, k, processed - 1);\n let cur = Self::min_len_start_upto(nums, k, processed - 1, nums.len() as int);\n Self::imin(prev, cur)\n }\n }\n\n pub open spec fn minimum_subarray_length_spec(nums: Seq, k: i32) -> int {\n let best = Self::min_len_prefix(nums, k, nums.len() as int);\n if best <= nums.len() as int { best } else { -1 }\n }\n\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 200000,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n 0 <= k <= 1_000_000_000,\n ensures\n result as int == Self::minimum_subarray_length_spec(nums@, k),\n {\n }\n}\n\n}\n", "code": "fn bit_set_exec(x: i32, b: u32) -> bool {\n (x >> b) & 1 == 1\n}\n\nimpl Solution {\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> i32 {\n let n = nums.len();\n\n let mut cnt: Vec = Vec::new();\n let mut bi: usize = 0;\n while bi < 30 {\n cnt.push(0);\n bi += 1;\n }\n\n let mut l: usize = 0;\n let mut r: usize = 0;\n let mut window_or: i32 = 0;\n let mut best: i32 = (n as i32) + 1;\n\n while l < n {\n while r < n && !(r > l && window_or >= k) {\n let old_r = r;\n let x = nums[r];\n let mut b: usize = 0;\n while b < 30 {\n let bit_here = bit_set_exec(x, b as u32);\n if bit_here {\n cnt[b] = cnt[b] + 1;\n }\n b += 1;\n }\n window_or = window_or | x;\n r += 1;\n }\n\n let old_best = best;\n if window_or >= k {\n let candidate: i32 = (r - l) as i32;\n if candidate < best {\n best = candidate;\n } else {\n best = old_best;\n }\n } else {\n best = old_best;\n }\n\n let removed = nums[l];\n let old_l = l;\n let mut b2: usize = 0;\n while b2 < 30 {\n let bit_here = bit_set_exec(removed, b2 as u32);\n if bit_here {\n cnt[b2] = cnt[b2] - 1;\n }\n b2 += 1;\n }\n\n let mut new_or: i32 = 0;\n let mut b3: usize = 0;\n while b3 < 30 {\n if cnt[b3] > 0 {\n new_or = new_or | (1i32 << (b3 as u32));\n }\n b3 += 1;\n }\n window_or = new_or;\n l += 1;\n }\n\n if best <= n as i32 {\n best\n } else {\n -1\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn imin(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn sub_or(nums: Seq, start: int, end: int) -> i32\n decreases end - start,\n {\n if end <= start {\n 0i32\n } else {\n Self::sub_or(nums, start, end - 1) | nums[end - 1]\n }\n }\n\n pub open spec fn min_len_start_upto(nums: Seq, k: i32, start: int, upto: int) -> int\n decreases upto - start,\n {\n if upto <= start {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_start_upto(nums, k, start, upto - 1);\n let cand = if Self::sub_or(nums, start, upto) >= k {\n upto - start\n } else {\n nums.len() as int + 1\n };\n Self::imin(prev, cand)\n }\n }\n\n pub open spec fn min_len_prefix(nums: Seq, k: i32, processed: int) -> int\n decreases processed,\n {\n if processed <= 0 {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_prefix(nums, k, processed - 1);\n let cur = Self::min_len_start_upto(nums, k, processed - 1, nums.len() as int);\n Self::imin(prev, cur)\n }\n }\n\n pub open spec fn minimum_subarray_length_spec(nums: Seq, k: i32) -> int {\n let best = Self::min_len_prefix(nums, k, nums.len() as int);\n if best <= nums.len() as int { best } else { -1 }\n }\n}\n\npub open spec fn bit_set(x: i32, b: u32) -> bool {\n (x >> b) & 1 == 1\n}\n\nfn bit_set_exec(x: i32, b: u32) -> (result: bool)\n requires 0 <= x, b < 30,\n ensures result == bit_set(x, b),\n{\n (x >> b) & 1 == 1\n}\n\nimpl Solution {\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 200000,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n 0 <= k <= 1_000_000_000,\n ensures\n result as int == Self::minimum_subarray_length_spec(nums@, k),\n {\n let n = nums.len();\n\n let mut cnt: Vec = Vec::new();\n let mut bi: usize = 0;\n while bi < 30 {\n cnt.push(0);\n bi += 1;\n }\n\n let mut l: usize = 0;\n let mut r: usize = 0;\n let mut window_or: i32 = 0;\n let mut best: i32 = (n as i32) + 1;\n\n while l < n {\n while r < n && !(r > l && window_or >= k) {\n let old_r = r;\n let x = nums[r];\n let mut b: usize = 0;\n while b < 30 {\n let bit_here = bit_set_exec(x, b as u32);\n if bit_here {\n cnt.set(b, cnt[b] + 1);\n }\n b += 1;\n }\n window_or = window_or | x;\n r += 1;\n }\n\n let old_best = best;\n if window_or >= k {\n let candidate: i32 = (r - l) as i32;\n if candidate < best {\n best = candidate;\n } else {\n best = old_best;\n }\n } else {\n best = old_best;\n }\n\n let removed = nums[l];\n let old_l = l;\n let mut b2: usize = 0;\n while b2 < 30 {\n let bit_here = bit_set_exec(removed, b2 as u32);\n if bit_here {\n cnt.set(b2, cnt[b2] - 1);\n }\n b2 += 1;\n }\n\n let mut new_or: i32 = 0;\n let mut b3: usize = 0;\n while b3 < 30 {\n if cnt[b3] > 0 {\n new_or = new_or | (1i32 << (b3 as u32));\n }\n b3 += 1;\n }\n window_or = new_or;\n l += 1;\n }\n\n if best <= n as i32 {\n best\n } else {\n -1\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn imin(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn sub_or(nums: Seq, start: int, end: int) -> i32\n decreases end - start,\n {\n if end <= start {\n 0i32\n } else {\n Self::sub_or(nums, start, end - 1) | nums[end - 1]\n }\n }\n\n pub open spec fn min_len_start_upto(nums: Seq, k: i32, start: int, upto: int) -> int\n decreases upto - start,\n {\n if upto <= start {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_start_upto(nums, k, start, upto - 1);\n let cand = if Self::sub_or(nums, start, upto) >= k {\n upto - start\n } else {\n nums.len() as int + 1\n };\n Self::imin(prev, cand)\n }\n }\n\n pub open spec fn min_len_prefix(nums: Seq, k: i32, processed: int) -> int\n decreases processed,\n {\n if processed <= 0 {\n nums.len() as int + 1\n } else {\n let prev = Self::min_len_prefix(nums, k, processed - 1);\n let cur = Self::min_len_start_upto(nums, k, processed - 1, nums.len() as int);\n Self::imin(prev, cur)\n }\n }\n\n pub open spec fn minimum_subarray_length_spec(nums: Seq, k: i32) -> int {\n let best = Self::min_len_prefix(nums, k, nums.len() as int);\n if best <= nums.len() as int { best } else { -1 }\n }\n}\n\npub open spec fn bit_set(x: i32, b: u32) -> bool {\n (x >> b) & 1 == 1\n}\n\npub open spec fn low_mask(x: i32, upto: u32) -> i32 {\n x & (((1i32 << upto) - 1) as i32)\n}\n\nproof fn lemma_mask_step(x: i32, upto: u32)\n requires 0 <= x, upto < 30,\n ensures low_mask(x, (upto + 1) as u32) == (low_mask(x, upto) | (if bit_set(x, upto) { 1i32 << upto } else { 0i32 })),\n{\n assert(low_mask(x, (upto + 1) as u32) == (low_mask(x, upto) | (if bit_set(x, upto) { 1i32 << upto } else { 0i32 }))) by (bit_vector)\n requires upto < 30;\n}\n\nproof fn lemma_same_bits_upto_eq(x: i32, y: i32, upto: u32)\n requires\n 0 <= x, 0 <= y,\n forall |b: u32| b < upto ==> bit_set(x, b) == bit_set(y, b),\n upto <= 30,\n ensures low_mask(x, upto) == low_mask(y, upto),\n decreases upto,\n{\n if upto > 0 {\n let prev: u32 = (upto - 1) as u32;\n lemma_same_bits_upto_eq(x, y, prev);\n lemma_mask_step(x, prev);\n lemma_mask_step(y, prev);\n assert(bit_set(x, prev) == bit_set(y, prev));\n assert((prev + 1) as u32 == upto);\n } else {\n assert(low_mask(x, 0) == 0) by (bit_vector);\n assert(low_mask(y, 0) == 0) by (bit_vector);\n }\n}\n\nproof fn lemma_low_mask_full(x: i32)\n requires 0 <= x < 1073741824i32,\n ensures low_mask(x, 30) == x,\n{\n assert(low_mask(x, 30) == x) by (bit_vector)\n requires 0 <= x < 1073741824i32;\n}\n\nproof fn lemma_eq_from_same_bits(x: i32, y: i32)\n requires\n 0 <= x < 1073741824i32, 0 <= y < 1073741824i32,\n forall |b: u32| b < 30 ==> bit_set(x, b) == bit_set(y, b),\n ensures x == y,\n{\n lemma_same_bits_upto_eq(x, y, 30);\n lemma_low_mask_full(x);\n lemma_low_mask_full(y);\n}\n\nproof fn lemma_or_ge(x: i32, y: i32)\n requires 0 <= x, 0 <= y,\n ensures (x | y) >= x, (x | y) >= y, (x | y) >= 0,\n{\n assert((x | y) >= x && (x | y) >= y && (x | y) >= 0) by (bit_vector)\n requires x >= 0, y >= 0;\n}\n\nproof fn lemma_or_bit(a: i32, c: i32, b: u32)\n ensures bit_set(a | c, b) == (bit_set(a, b) || bit_set(c, b)),\n{\n assert(bit_set(a | c, b) == (bit_set(a, b) || bit_set(c, b))) by (bit_vector);\n}\n\nproof fn lemma_or_bound(x: i32, y: i32)\n requires 0 <= x < 1073741824i32, 0 <= y < 1073741824i32,\n ensures 0 <= (x | y) < 1073741824i32,\n{\n assert(0 <= (x | y) && (x | y) < 1073741824i32) by (bit_vector)\n requires 0 <= x < 1073741824i32, 0 <= y < 1073741824i32;\n}\n\nproof fn lemma_or_zero(x: i32)\n requires 0 <= x,\n ensures (x | 0i32) == x,\n{\n assert((x | 0i32) == x) by (bit_vector)\n requires 0 <= x;\n}\n\nproof fn lemma_or_assoc(a: i32, b: i32, c: i32)\n ensures (a | b | c) == (a | (b | c)),\n{\n assert((a | b | c) == (a | (b | c))) by (bit_vector);\n}\n\nproof fn lemma_sub_or_bound(nums: Seq, start: int, end: int)\n requires\n 0 <= start <= end <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 1073741824i32,\n ensures 0 <= Solution::sub_or(nums, start, end) < 1073741824i32,\n decreases end - start,\n{\n if end > start {\n lemma_sub_or_bound(nums, start, end - 1);\n lemma_or_bound(Solution::sub_or(nums, start, end - 1), nums[end - 1]);\n }\n}\n\npub open spec fn count_bit(nums: Seq, start: int, end: int, b: u32) -> int\n decreases end - start,\n{\n if end <= start {\n 0\n } else {\n count_bit(nums, start, end - 1, b) + (if bit_set(nums[end - 1], b) { 1int } else { 0int })\n }\n}\n\nproof fn lemma_count_bit_nonneg(nums: Seq, start: int, end: int, b: u32)\n requires 0 <= start <= end,\n ensures count_bit(nums, start, end, b) >= 0,\n decreases end - start,\n{\n if end > start {\n lemma_count_bit_nonneg(nums, start, end - 1, b);\n }\n}\n\nproof fn lemma_sub_or_bit_char(nums: Seq, start: int, end: int, b: u32)\n requires 0 <= start <= end <= nums.len(),\n ensures bit_set(Solution::sub_or(nums, start, end), b) == (count_bit(nums, start, end, b) > 0),\n decreases end - start,\n{\n if end > start {\n lemma_sub_or_bit_char(nums, start, end - 1, b);\n lemma_or_bit(Solution::sub_or(nums, start, end - 1), nums[end - 1], b);\n lemma_count_bit_nonneg(nums, start, end - 1, b);\n } else {\n assert(bit_set(0i32, b) == false) by (bit_vector);\n }\n}\n\nproof fn lemma_sub_or_split(nums: Seq, lo: int, mid: int, hi: int)\n requires 0 <= lo <= mid <= hi <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 1073741824i32,\n ensures Solution::sub_or(nums, lo, hi) == (Solution::sub_or(nums, lo, mid) | Solution::sub_or(nums, mid, hi)),\n decreases hi - mid,\n{\n if hi > mid {\n lemma_sub_or_split(nums, lo, mid, hi - 1);\n lemma_or_assoc(Solution::sub_or(nums, lo, mid), Solution::sub_or(nums, mid, hi - 1), nums[hi - 1]);\n } else {\n assert(Solution::sub_or(nums, mid, hi) == 0i32);\n lemma_sub_or_bound(nums, lo, mid);\n lemma_or_zero(Solution::sub_or(nums, lo, mid));\n }\n}\n\nproof fn lemma_sub_or_mono_left(nums: Seq, lo: int, mid: int, hi: int)\n requires 0 <= lo <= mid <= hi <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 1073741824i32,\n ensures Solution::sub_or(nums, mid, hi) <= Solution::sub_or(nums, lo, hi),\n{\n lemma_sub_or_split(nums, lo, mid, hi);\n lemma_sub_or_bound(nums, lo, mid);\n lemma_sub_or_bound(nums, mid, hi);\n lemma_or_ge(Solution::sub_or(nums, lo, mid), Solution::sub_or(nums, mid, hi));\n}\n\nproof fn lemma_sub_or_mono_right(nums: Seq, lo: int, mid: int, hi: int)\n requires 0 <= lo <= mid <= hi <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 1073741824i32,\n ensures Solution::sub_or(nums, lo, mid) <= Solution::sub_or(nums, lo, hi),\n{\n lemma_sub_or_split(nums, lo, mid, hi);\n lemma_sub_or_bound(nums, lo, mid);\n lemma_sub_or_bound(nums, mid, hi);\n lemma_or_ge(Solution::sub_or(nums, lo, mid), Solution::sub_or(nums, mid, hi));\n}\n\nproof fn lemma_start_upto_all_invalid(nums: Seq, k: i32, start: int, upto: int)\n requires\n 0 <= start <= upto <= nums.len(),\n forall |e: int| start < e <= upto ==> Solution::sub_or(nums, start, e) < k,\n ensures Solution::min_len_start_upto(nums, k, start, upto) == nums.len() as int + 1,\n decreases upto - start,\n{\n if upto > start {\n lemma_start_upto_all_invalid(nums, k, start, upto - 1);\n }\n}\n\nproof fn lemma_start_upto_first_hit(nums: Seq, k: i32, start: int, r: int, upto: int)\n requires\n 0 <= start < r <= upto <= nums.len(),\n forall |e: int| start < e < r ==> Solution::sub_or(nums, start, e) < k,\n Solution::sub_or(nums, start, r) >= k,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 1073741824i32,\n ensures Solution::min_len_start_upto(nums, k, start, upto) == r - start,\n decreases upto - r,\n{\n lemma_start_upto_all_invalid(nums, k, start, r - 1);\n if upto == r {\n } else {\n lemma_start_upto_first_hit(nums, k, start, r, upto - 1);\n lemma_sub_or_mono_right(nums, start, r, upto);\n }\n}\n\npub open spec fn cnt_bits_value(cnt: Seq, upto: int) -> i32\n decreases upto,\n{\n if upto <= 0 {\n 0i32\n } else {\n let prev = cnt_bits_value(cnt, upto - 1);\n if cnt[upto - 1] > 0 { prev | (1i32 << ((upto - 1) as u32)) } else { prev }\n }\n}\n\nproof fn lemma_bit_of_pow2(c: u32, b: u32)\n requires c < 30, b < 30,\n ensures bit_set(1i32 << c, b) == (b == c),\n{\n assert(bit_set(1i32 << c, b) == (b == c)) by (bit_vector)\n requires c < 30, b < 30;\n}\n\nproof fn lemma_cnt_bits_value_char(cnt: Seq, upto: int, b: u32)\n requires 0 <= upto <= 30, b < 30,\n ensures bit_set(cnt_bits_value(cnt, upto), b) == (b < upto && cnt[b as int] > 0),\n decreases upto,\n{\n if upto > 0 {\n lemma_cnt_bits_value_char(cnt, upto - 1, b);\n let prev = cnt_bits_value(cnt, upto - 1);\n let bit_c: u32 = (upto - 1) as u32;\n if cnt[upto - 1] > 0 {\n lemma_or_bit(prev, 1i32 << bit_c, b);\n lemma_bit_of_pow2(bit_c, b);\n }\n } else {\n assert(bit_set(0i32, b) == false) by (bit_vector);\n }\n}\n\nproof fn lemma_cnt_bits_value_bound(cnt: Seq, upto: int)\n requires 0 <= upto <= 30,\n ensures 0 <= cnt_bits_value(cnt, upto) < 1073741824i32,\n decreases upto,\n{\n if upto > 0 {\n lemma_cnt_bits_value_bound(cnt, upto - 1);\n let prev = cnt_bits_value(cnt, upto - 1);\n let bit_c: u32 = (upto - 1) as u32;\n assert(0 <= prev && 0 <= (1i32 << bit_c) < 1073741824i32) by (bit_vector)\n requires 0 <= prev < 1073741824i32, bit_c < 30;\n lemma_or_bound(prev, 1i32 << bit_c);\n lemma_or_zero(prev);\n }\n}\n\nproof fn lemma_cnt_bits_value_matches_sub_or(nums: Seq, start: int, end: int, cnt: Seq)\n requires\n 0 <= start <= end <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < 1073741824i32,\n cnt.len() == 30,\n forall |b: int| 0 <= b < 30 ==> cnt[b] == count_bit(nums, start, end, b as u32),\n ensures cnt_bits_value(cnt, 30) == Solution::sub_or(nums, start, end),\n{\n lemma_sub_or_bound(nums, start, end);\n lemma_cnt_bits_value_bound(cnt, 30);\n assert forall |b: u32| b < 30 implies\n bit_set(cnt_bits_value(cnt, 30), b) == bit_set(Solution::sub_or(nums, start, end), b) by {\n lemma_cnt_bits_value_char(cnt, 30, b);\n lemma_sub_or_bit_char(nums, start, end, b);\n };\n lemma_eq_from_same_bits(cnt_bits_value(cnt, 30), Solution::sub_or(nums, start, end));\n}\n\nproof fn lemma_count_bit_start_step(nums: Seq, start: int, end: int, b: u32)\n requires 0 <= start < end <= nums.len(),\n ensures\n count_bit(nums, start, end, b)\n == count_bit(nums, start + 1, end, b) + (if bit_set(nums[start], b) { 1int } else { 0int }),\n decreases end - start,\n{\n if end == start + 1 {\n assert(count_bit(nums, start, start, b) == 0);\n assert(count_bit(nums, start + 1, start + 1, b) == 0);\n } else {\n lemma_count_bit_start_step(nums, start, end - 1, b);\n }\n}\n\nfn bit_set_exec(x: i32, b: u32) -> (result: bool)\n requires 0 <= x, b < 30,\n ensures result == bit_set(x, b),\n{\n (x >> b) & 1 == 1\n}\n\nproof fn lemma_count_bit_bound(nums: Seq, start: int, end: int, b: u32)\n requires 0 <= start <= end,\n ensures 0 <= count_bit(nums, start, end, b) <= end - start,\n decreases end - start,\n{\n if end > start {\n lemma_count_bit_bound(nums, start, end - 1, b);\n }\n}\n\nproof fn lemma_min_len_prefix_bound(nums: Seq, k: i32, processed: int)\n requires 0 <= processed <= nums.len(),\n ensures Solution::min_len_prefix(nums, k, processed) <= nums.len() as int + 1,\n decreases processed,\n{\n if processed > 0 {\n lemma_min_len_prefix_bound(nums, k, processed - 1);\n }\n}\n\nimpl Solution {\n pub fn minimum_subarray_length(nums: Vec, k: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 200000,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n 0 <= k <= 1_000_000_000,\n ensures\n result as int == Self::minimum_subarray_length_spec(nums@, k),\n {\n let n = nums.len();\n\n assert(forall |i: int| 0 <= i < n ==> 0 <= #[trigger] nums@[i] < 1073741824i32) by {\n assert forall |i: int| 0 <= i < n implies 0 <= #[trigger] nums@[i] < 1073741824i32 by {\n }\n };\n\n let mut cnt: Vec = Vec::new();\n let mut bi: usize = 0;\n while bi < 30\n invariant\n cnt.len() == bi,\n bi <= 30,\n forall |bb: int| 0 <= bb < bi ==> cnt[bb] == 0,\n decreases 30 - bi,\n {\n cnt.push(0);\n bi += 1;\n }\n\n let mut l: usize = 0;\n let mut r: usize = 0;\n let mut window_or: i32 = 0;\n let mut best: i32 = (n as i32) + 1;\n\n assert(Solution::min_len_prefix(nums@, k, 0) == n as int + 1);\n assert(Solution::sub_or(nums@, 0, 0) == 0i32);\n assert forall |b: int| 0 <= b < 30 implies cnt[b] as int == count_bit(nums@, 0, 0, b as u32) by {\n }\n\n while l < n\n invariant\n n == nums.len(),\n n <= 200000,\n 0 <= l <= r <= n,\n cnt.len() == 30,\n forall |b: int| 0 <= b < 30 ==> cnt[b] as int == count_bit(nums@, l as int, r as int, b as u32),\n forall |b: int| 0 <= b < 30 ==> 0 <= #[trigger] cnt[b] as int <= n as int,\n window_or == Solution::sub_or(nums@, l as int, r as int),\n best as int == Solution::min_len_prefix(nums@, k, l as int),\n forall |i: int| 0 <= i < n ==> 0 <= #[trigger] nums@[i] < 1073741824i32,\n forall |e: int| l < e < r as int ==> Solution::sub_or(nums@, l as int, e) < k,\n decreases n - l,\n {\n while r < n && !(r > l && window_or >= k)\n invariant\n n == nums.len(),\n n <= 200000,\n l <= r <= n,\n cnt.len() == 30,\n forall |b: int| 0 <= b < 30 ==> cnt[b] as int == count_bit(nums@, l as int, r as int, b as u32),\n forall |b: int| 0 <= b < 30 ==> 0 <= #[trigger] cnt[b] as int <= n as int,\n window_or == Solution::sub_or(nums@, l as int, r as int),\n forall |i: int| 0 <= i < n ==> 0 <= #[trigger] nums@[i] < 1073741824i32,\n forall |e: int| l < e < r as int ==> Solution::sub_or(nums@, l as int, e) < k,\n decreases n - r,\n {\n if r > l {\n assert(Solution::sub_or(nums@, l as int, r as int) < k);\n }\n let old_r = r;\n let x = nums[r];\n assert(x == nums@[old_r as int]);\n assert(0 <= nums@[old_r as int] < 1073741824i32);\n let mut b: usize = 0;\n while b < 30\n invariant\n b <= 30,\n cnt.len() == 30,\n n <= 200000,\n l <= old_r < n,\n x == nums@[old_r as int],\n 0 <= x < 1073741824i32,\n forall |bb: int| 0 <= bb < b ==> cnt[bb] as int == count_bit(nums@, l as int, old_r as int + 1, bb as u32),\n forall |bb: int| b <= bb < 30 ==> cnt[bb] as int == count_bit(nums@, l as int, old_r as int, bb as u32),\n forall |bb: int| 0 <= bb < 30 ==> 0 <= #[trigger] cnt[bb] as int <= n as int,\n decreases 30 - b,\n {\n let bit_here = bit_set_exec(x, b as u32);\n assert(count_bit(nums@, l as int, old_r as int + 1, b as u32)\n == count_bit(nums@, l as int, old_r as int, b as u32)\n + (if bit_set(nums@[old_r as int], b as u32) { 1int } else { 0int }));\n proof {\n lemma_count_bit_bound(nums@, l as int, old_r as int + 1, b as u32);\n }\n assert(0 <= cnt[b as int] as int <= n as int);\n if bit_here {\n assert(cnt[b as int] as int + 1 <= n as int);\n cnt.set(b, cnt[b] + 1);\n }\n b += 1;\n }\n window_or = window_or | x;\n assert(Solution::sub_or(nums@, l as int, old_r as int + 1)\n == (Solution::sub_or(nums@, l as int, old_r as int) | nums@[old_r as int]));\n r += 1;\n assert forall |e: int| l < e < r as int implies Solution::sub_or(nums@, l as int, e) < k by {\n }\n }\n assert(r > l);\n let old_best = best;\n assert(old_best as int == Solution::min_len_prefix(nums@, k, l as int));\n\n if window_or >= k {\n proof { lemma_start_upto_first_hit(nums@, k, l as int, r as int, n as int); }\n assert(Solution::min_len_start_upto(nums@, k, l as int, n as int) == r as int - l as int);\n assert(n <= 200000);\n assert(r - l <= n);\n assert(0 <= (r - l) as int <= 200000int);\n let candidate: i32 = (r - l) as i32;\n assert(candidate as int == r as int - l as int);\n if candidate < best {\n best = candidate;\n } else {\n best = old_best;\n }\n } else {\n proof {\n lemma_start_upto_all_invalid(nums@, k, l as int, n as int);\n lemma_min_len_prefix_bound(nums@, k, l as int);\n }\n assert(Solution::min_len_start_upto(nums@, k, l as int, n as int) == n as int + 1);\n assert(old_best as int <= n as int + 1);\n best = old_best;\n }\n assert(best as int == Solution::imin(\n Solution::min_len_prefix(nums@, k, l as int),\n Solution::min_len_start_upto(nums@, k, l as int, n as int)));\n assert(best as int == Solution::min_len_prefix(nums@, k, l as int + 1));\n\n let removed = nums[l];\n let old_l = l;\n assert(removed == nums@[old_l as int]);\n assert(0 <= nums@[old_l as int] < 1073741824i32);\n let mut b2: usize = 0;\n while b2 < 30\n invariant\n b2 <= 30,\n cnt.len() == 30,\n n == nums.len(),\n n <= 200000,\n old_l < r <= n,\n removed == nums@[old_l as int],\n 0 <= removed < 1073741824i32,\n forall |bb: int| 0 <= bb < b2 ==> cnt[bb] as int == count_bit(nums@, old_l as int + 1, r as int, bb as u32),\n forall |bb: int| b2 <= bb < 30 ==> cnt[bb] as int == count_bit(nums@, old_l as int, r as int, bb as u32),\n forall |bb: int| 0 <= bb < 30 ==> 0 <= #[trigger] cnt[bb] as int <= n as int,\n decreases 30 - b2,\n {\n let bit_here = bit_set_exec(removed, b2 as u32);\n proof {\n lemma_count_bit_start_step(nums@, old_l as int, r as int, b2 as u32);\n lemma_count_bit_bound(nums@, old_l as int + 1, r as int, b2 as u32);\n }\n if bit_here {\n cnt.set(b2, cnt[b2] - 1);\n }\n b2 += 1;\n }\n\n let mut new_or: i32 = 0;\n let mut b3: usize = 0;\n while b3 < 30\n invariant\n b3 <= 30,\n cnt.len() == 30,\n old_l + 1 <= r <= n,\n forall |bb: int| 0 <= bb < 30 ==> cnt[bb] as int == count_bit(nums@, old_l as int + 1, r as int, bb as u32),\n new_or == cnt_bits_value(cnt@.map_values(|x: i32| x as int), b3 as int),\n decreases 30 - b3,\n {\n if cnt[b3] > 0 {\n new_or = new_or | (1i32 << (b3 as u32));\n }\n b3 += 1;\n }\n proof {\n lemma_cnt_bits_value_matches_sub_or(nums@, old_l as int + 1, r as int, cnt@.map_values(|x: i32| x as int));\n }\n window_or = new_or;\n l += 1;\n\n assert forall |e: int| l < e < r as int implies Solution::sub_or(nums@, l as int, e) < k by {\n lemma_sub_or_mono_left(nums@, l as int - 1, l as int, e);\n }\n }\n\n if best <= n as i32 {\n best\n } else {\n -1\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3097/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3097/tests/mutated_testcases.jsonl"} {"problem": "lc3100", "family": "leetcode", "description": "# Water Bottles II\n\nYou are given two integers `numBottles` and `numExchange`.\n\n`numBottles` is the number of full water bottles you initially have. In one operation, you can:\n\n- Drink any number of full bottles, turning them into empty bottles.\n- Exchange `numExchange` empty bottles for one full bottle, then increase `numExchange` by `1`.\n\nYou cannot exchange multiple batches using the same `numExchange` value in a single step.\n\nReturn the maximum number of water bottles you can drink.\n\n## Example 1:\n\n> **Input:** numBottles = 13, numExchange = 6 \n> **Output:** 15\n\n## Example 2:\n\n> **Input:** numBottles = 10, numExchange = 3 \n> **Output:** 13\n\n## Constraints:\n\n- `1 <= numBottles <= 100`\n- `1 <= numExchange <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_bottles_drunk(num_bottles: i32, num_exchange: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Simulation\nAcceptance rate: 78.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn extra_drinks(empty: int, exchange: int) -> int\n decreases if exchange <= 1 { 1int } else { 0int }, empty,\n {\n if empty < exchange || exchange <= 0 {\n 0\n } else {\n 1 + Self::extra_drinks(empty - exchange + 1, exchange + 1)\n }\n }\n\n pub open spec fn max_bottles_drunk_spec(num_bottles: int, num_exchange: int) -> int {\n num_bottles + Self::extra_drinks(num_bottles, num_exchange)\n }\n\n pub fn max_bottles_drunk(num_bottles: i32, num_exchange: i32) -> (result: i32)\n requires\n 1 <= num_bottles <= 100,\n 1 <= num_exchange <= 100,\n ensures\n result as int == Self::max_bottles_drunk_spec(num_bottles as int, num_exchange as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_bottles_drunk(num_bottles: i32, num_exchange: i32) -> i32 {\n let mut ans = num_bottles;\n let mut empty = num_bottles;\n let mut exchange = num_exchange;\n while empty >= exchange {\n empty = empty - exchange + 1;\n exchange = exchange + 1;\n ans = ans + 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn extra_drinks(empty: int, exchange: int) -> int\n decreases if exchange <= 1 { 1int } else { 0int }, empty,\n {\n if empty < exchange || exchange <= 0 {\n 0\n } else {\n 1 + Self::extra_drinks(empty - exchange + 1, exchange + 1)\n }\n }\n\n pub open spec fn max_bottles_drunk_spec(num_bottles: int, num_exchange: int) -> int {\n num_bottles + Self::extra_drinks(num_bottles, num_exchange)\n }\n\n pub fn max_bottles_drunk(num_bottles: i32, num_exchange: i32) -> (result: i32)\n requires\n 1 <= num_bottles <= 100,\n 1 <= num_exchange <= 100,\n ensures\n result as int == Self::max_bottles_drunk_spec(num_bottles as int, num_exchange as int),\n {\n let mut ans = num_bottles;\n let mut empty = num_bottles;\n let mut exchange = num_exchange;\n while empty >= exchange {\n empty = empty - exchange + 1;\n exchange = exchange + 1;\n ans = ans + 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn extra_drinks(empty: int, exchange: int) -> int\n decreases if exchange <= 1 { 1int } else { 0int }, empty,\n {\n if empty < exchange || exchange <= 0 {\n 0\n } else {\n 1 + Self::extra_drinks(empty - exchange + 1, exchange + 1)\n }\n }\n\n pub open spec fn max_bottles_drunk_spec(num_bottles: int, num_exchange: int) -> int {\n num_bottles + Self::extra_drinks(num_bottles, num_exchange)\n }\n\n pub fn max_bottles_drunk(num_bottles: i32, num_exchange: i32) -> (result: i32)\n requires\n 1 <= num_bottles <= 100,\n 1 <= num_exchange <= 100,\n ensures\n result as int == Self::max_bottles_drunk_spec(num_bottles as int, num_exchange as int),\n {\n let mut ans = num_bottles;\n let mut empty = num_bottles;\n let mut exchange = num_exchange;\n\n while empty >= exchange\n invariant\n 1 <= num_bottles <= 100,\n 1 <= num_exchange <= 100,\n 0 <= empty <= num_bottles,\n 1 <= exchange,\n ans as int == num_bottles as int + exchange as int - num_exchange as int,\n ans as int + Self::extra_drinks(empty as int, exchange as int)\n == Self::max_bottles_drunk_spec(num_bottles as int, num_exchange as int),\n decreases if exchange <= 1 { 1int } else { 0int }, empty as int,\n {\n let ghost prev_ans = ans as int;\n let ghost prev_empty = empty as int;\n let ghost prev_exchange = exchange as int;\n\n proof {\n reveal_with_fuel(Solution::extra_drinks, 2);\n assert(prev_empty >= prev_exchange);\n assert(prev_exchange > 0);\n assert(Self::extra_drinks(prev_empty, prev_exchange)\n == 1 + Self::extra_drinks(prev_empty - prev_exchange + 1, prev_exchange + 1));\n assert(exchange <= empty);\n assert(empty <= num_bottles);\n assert(exchange <= 100);\n assert(ans as int <= 199);\n }\n\n empty = empty - exchange + 1;\n exchange = exchange + 1;\n ans = ans + 1;\n\n proof {\n assert(empty as int == prev_empty - prev_exchange + 1);\n assert(exchange as int == prev_exchange + 1);\n assert(ans as int == prev_ans + 1);\n assert(0 <= empty as int);\n assert(empty as int <= num_bottles as int);\n assert(ans as int == num_bottles as int + exchange as int - num_exchange as int);\n assert(ans as int + Self::extra_drinks(empty as int, exchange as int)\n == prev_ans + Self::extra_drinks(prev_empty, prev_exchange));\n }\n }\n\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3100/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3100/tests/mutated_testcases.jsonl"} {"problem": "lc3105", "family": "leetcode", "description": "# Longest Strictly Increasing or Strictly Decreasing Subarray\n\nYou are given an array of integers `nums`. Return the length of the longest subarray of `nums` which is either strictly increasing or strictly decreasing.\n\n## Examples\n\n**Example 1:**\nInput: nums = [1,4,3,3,2]\nOutput: 2\nExplanation:\nThe strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].\nThe strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].\nHence, we return 2.\n\n**Example 2:**\nInput: nums = [3,3,3,3]\nOutput: 1\nExplanation:\nThe strictly increasing subarrays of nums are [3], [3], [3], and [3].\nThe strictly decreasing subarrays of nums are [3], [3], [3], and [3].\nHence, we return 1.\n\n**Example 3:**\nInput: nums = [3,2,1]\nOutput: 3\nExplanation:\nThe strictly increasing subarrays of nums are [3], [2], and [1].\nThe strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].\nHence, we return 3.\n\n## Constraints\n\n- 1 <= nums.length <= 50\n- 1 <= nums[i] <= 50\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_monotonic_subarray(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\n64.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_inc(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if nums[i] > nums[i - 1] {\n spec_inc(nums, i - 1) + 1\n } else {\n 1\n }\n}\n\n\npub open spec fn spec_dec(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if nums[i] < nums[i - 1] {\n spec_dec(nums, i - 1) + 1\n } else {\n 1\n }\n}\n\n\npub open spec fn spec_max(a: int, b: int) -> int\n{\n if a >= b { a } else { b }\n}\n\n\npub open spec fn spec_best(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 1\n } else {\n spec_max(\n spec_best(nums, k - 1),\n spec_max(spec_inc(nums, k), spec_dec(nums, k)),\n )\n }\n}\n\npub open spec fn spec_longest_monotonic_subarray(nums: Seq) -> int\n{\n if nums.len() == 0 {\n 0\n } else {\n spec_best(nums, (nums.len() - 1) as int)\n }\n}\n\nimpl Solution {\n pub fn longest_monotonic_subarray(nums: Vec) -> (result: i32)\n requires\n nums.len() > 0,\n nums.len() <= 50,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n result as int == spec_longest_monotonic_subarray(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn longest_monotonic_subarray(nums: Vec) -> i32 {\n let n = nums.len();\n if n == 0 {\n return 0;\n }\n let mut best = 1i32;\n let mut inc = 1i32;\n let mut dec = 1i32;\n let mut i = 1;\n while i < n {\n if nums[i] > nums[i - 1] {\n inc = inc + 1;\n } else {\n inc = 1;\n }\n if nums[i] < nums[i - 1] {\n dec = dec + 1;\n } else {\n dec = 1;\n }\n let cur = if inc > dec { inc } else { dec };\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_inc(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if nums[i] > nums[i - 1] {\n spec_inc(nums, i - 1) + 1\n } else {\n 1\n }\n}\n\n\npub open spec fn spec_dec(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if nums[i] < nums[i - 1] {\n spec_dec(nums, i - 1) + 1\n } else {\n 1\n }\n}\n\n\npub open spec fn spec_max(a: int, b: int) -> int\n{\n if a >= b { a } else { b }\n}\n\n\npub open spec fn spec_best(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 1\n } else {\n spec_max(\n spec_best(nums, k - 1),\n spec_max(spec_inc(nums, k), spec_dec(nums, k)),\n )\n }\n}\n\npub open spec fn spec_longest_monotonic_subarray(nums: Seq) -> int\n{\n if nums.len() == 0 {\n 0\n } else {\n spec_best(nums, (nums.len() - 1) as int)\n }\n}\n\nimpl Solution {\n pub fn longest_monotonic_subarray(nums: Vec) -> (result: i32)\n requires\n nums.len() > 0,\n nums.len() <= 50,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n result as int == spec_longest_monotonic_subarray(nums@),\n {\n let n = nums.len();\n if n == 0 {\n return 0;\n }\n let mut best = 1i32;\n let mut inc = 1i32;\n let mut dec = 1i32;\n let mut i = 1;\n while i < n\n decreases n - i,\n {\n if nums[i] > nums[i - 1] {\n inc = inc + 1;\n } else {\n inc = 1;\n }\n if nums[i] < nums[i - 1] {\n dec = dec + 1;\n } else {\n dec = 1;\n }\n let cur = if inc > dec { inc } else { dec };\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_inc(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if nums[i] > nums[i - 1] {\n spec_inc(nums, i - 1) + 1\n } else {\n 1\n }\n}\n\n\npub open spec fn spec_dec(nums: Seq, i: int) -> int\n decreases i,\n{\n if i <= 0 {\n 1\n } else if nums[i] < nums[i - 1] {\n spec_dec(nums, i - 1) + 1\n } else {\n 1\n }\n}\n\n\npub open spec fn spec_max(a: int, b: int) -> int\n{\n if a >= b { a } else { b }\n}\n\n\npub open spec fn spec_best(nums: Seq, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 1\n } else {\n spec_max(\n spec_best(nums, k - 1),\n spec_max(spec_inc(nums, k), spec_dec(nums, k)),\n )\n }\n}\n\npub open spec fn spec_longest_monotonic_subarray(nums: Seq) -> int\n{\n if nums.len() == 0 {\n 0\n } else {\n spec_best(nums, (nums.len() - 1) as int)\n }\n}\n\nproof fn lemma_inc_bounds(nums: Seq, i: int)\n requires\n nums.len() > 0,\n nums.len() <= 50,\n 0 <= i < nums.len() as int,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 50,\n ensures\n 1 <= spec_inc(nums, i) <= i + 1,\n decreases i,\n{\n if i <= 0 {\n } else if nums[i] > nums[i - 1] {\n lemma_inc_bounds(nums, i - 1);\n } else {\n }\n}\n\nproof fn lemma_dec_bounds(nums: Seq, i: int)\n requires\n nums.len() > 0,\n nums.len() <= 50,\n 0 <= i < nums.len() as int,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 50,\n ensures\n 1 <= spec_dec(nums, i) <= i + 1,\n decreases i,\n{\n if i <= 0 {\n } else if nums[i] < nums[i - 1] {\n lemma_dec_bounds(nums, i - 1);\n } else {\n }\n}\n\nproof fn lemma_best_bounds(nums: Seq, k: int)\n requires\n nums.len() > 0,\n nums.len() <= 50,\n 0 <= k < nums.len() as int,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 50,\n ensures\n 1 <= spec_best(nums, k) <= 50,\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_best_bounds(nums, k - 1);\n lemma_inc_bounds(nums, k);\n lemma_dec_bounds(nums, k);\n }\n}\n\nimpl Solution {\n pub fn longest_monotonic_subarray(nums: Vec) -> (result: i32)\n requires\n nums.len() > 0,\n nums.len() <= 50,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 50,\n ensures\n result as int == spec_longest_monotonic_subarray(nums@),\n {\n let n = nums.len();\n if n == 0 {\n return 0;\n }\n let mut best = 1i32;\n let mut inc = 1i32;\n let mut dec = 1i32;\n let mut i = 1;\n\n proof {\n lemma_inc_bounds(nums@, 0);\n lemma_dec_bounds(nums@, 0);\n lemma_best_bounds(nums@, 0);\n }\n\n while i < n\n invariant\n 1 <= i <= n,\n n == nums.len(),\n n > 0,\n n <= 50,\n forall|j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums@[j] <= 50,\n inc as int == spec_inc(nums@, (i - 1) as int),\n dec as int == spec_dec(nums@, (i - 1) as int),\n best as int == spec_best(nums@, (i - 1) as int),\n 1 <= inc <= 50,\n 1 <= dec <= 50,\n 1 <= best <= 50,\n decreases n - i,\n {\n proof {\n lemma_inc_bounds(nums@, i as int);\n lemma_dec_bounds(nums@, i as int);\n lemma_best_bounds(nums@, i as int);\n }\n\n if nums[i] > nums[i - 1] {\n inc = inc + 1;\n } else {\n inc = 1;\n }\n if nums[i] < nums[i - 1] {\n dec = dec + 1;\n } else {\n dec = 1;\n }\n let cur = if inc > dec { inc } else { dec };\n if cur > best {\n best = cur;\n }\n i = i + 1;\n }\n best\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3105/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3105/tests/mutated_testcases.jsonl"} {"problem": "lc3107", "family": "leetcode", "description": "# Minimum Operations to Make Median of Array Equal to K\n\nYou are given an integer array `nums` and a non-negative integer `k`.\nIn one operation, you can increase or decrease any element by 1.\n\nReturn the minimum number of operations needed to make the median of `nums` equal to `k`.\n\nThe median of an array is defined as the middle element of the array when it is sorted in non-decreasing order.\nIf there are two choices for a median, the larger of the two values is taken.\n\n## Examples\n\n**Example 1:**\nInput: nums = [2,5,6,8,5], k = 4\nOutput: 2\nExplanation:\nWe can subtract one from `nums[1]` and `nums[4]` to obtain `[2,4,6,8,4]`.\nThe median of the resulting array is equal to `k`.\n\n**Example 2:**\nInput: nums = [2,5,6,8,5], k = 7\nOutput: 3\nExplanation:\nWe can add one to `nums[1]` twice and add one to `nums[2]` once to obtain `[2,7,7,8,5]`.\n\n**Example 3:**\nInput: nums = [1,2,3,4,5,6], k = 4\nOutput: 0\nExplanation:\nThe median of the array is already equal to `k`.\n\n## Constraints\n\n- 1 <= nums.length <= 2 * 10^5\n- 1 <= nums[i] <= 10^9\n- 1 <= k <= 10^9\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations_to_make_median_k(nums: Vec, k: i32) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Greedy, Sorting\nAcceptance rate: 47.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sorted_between(a: Seq, from: int, to: int) -> bool {\n forall |i: int, j: int| from <= i < j < to ==> a[i] <= a[j]\n }\n\n pub open spec fn is_reorder_of(r: Seq, p: Seq, s: Seq) -> bool {\n &&& r.len() == s.len()\n &&& p.len() == s.len()\n &&& forall|i: int| 0 <= i < r.len() ==> 0 <= #[trigger] r[i] < r.len()\n &&& forall|i: int, j: int| 0 <= i < j < r.len() ==> r[i] != r[j]\n &&& p =~= r.map_values(|i: int| s[i])\n }\n\n pub open spec fn op_cost_at(v: int, idx: int, mid: int, k: int) -> int {\n if idx < mid {\n if v > k { v - k } else { 0 }\n } else if idx == mid {\n if v >= k { v - k } else { k - v }\n } else {\n if v < k { k - v } else { 0 }\n }\n }\n\n pub open spec fn cost_prefix(s: Seq, k: int, mid: int, n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else if n > s.len() {\n Self::cost_prefix(s, k, mid, s.len() as int)\n } else {\n Self::cost_prefix(s, k, mid, n - 1)\n + Self::op_cost_at(s[n - 1] as int, n - 1, mid, k)\n }\n }\n\n pub open spec fn cost_all(s: Seq, k: int) -> int {\n Self::cost_prefix(s, k, (s.len() / 2) as int, s.len() as int)\n }\n\n pub fn min_operations_to_make_median_k(nums: Vec, k: i32) -> (result: i64)\n requires\n 1 <= nums.len() <= 200000,\n 1 <= k <= 1000000000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000000000,\n ensures\n exists|s: Seq, r: Seq|\n Self::sorted_between(s, 0, s.len() as int)\n && Self::is_reorder_of(r, s, nums@)\n && result as int == Self::cost_all(s, k as int),\n {\n }\n}\n\n}\n", "code": "fn merge_exec(a: &Vec, b: &Vec) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len() {\n if j >= b.len() || (i < a.len() && a[i] <= b[j]) {\n result.push(a[i]);\n i += 1;\n } else {\n result.push(b[j]);\n j += 1;\n }\n }\n result\n}\n\nfn merge_sort_exec(v: &Vec) -> Vec {\n if v.len() <= 1 {\n let mut result: Vec = Vec::new();\n let mut k: usize = 0;\n while k < v.len() {\n result.push(v[k]);\n k += 1;\n }\n result\n } else {\n let mid = v.len() / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid {\n left.push(v[i]);\n i += 1;\n }\n let mut right: Vec = Vec::new();\n let mut i2: usize = mid;\n while i2 < v.len() {\n right.push(v[i2]);\n i2 += 1;\n }\n let sorted_left = merge_sort_exec(&left);\n let sorted_right = merge_sort_exec(&right);\n let result = merge_exec(&sorted_left, &sorted_right);\n result\n }\n}\n\nfn encode_exec(v: i32, i: usize) -> i64 {\n (v as i64) * 200000 + (i as i64)\n}\n\nfn decode_val_exec(e: i64) -> i64 {\n e / 200000\n}\n\nimpl Solution {\n pub fn min_operations_to_make_median_k(nums: Vec, k: i32) -> i64 {\n let n = nums.len();\n\n let mut enc: Vec = Vec::new();\n let mut ii: usize = 0;\n while ii < n {\n let e = encode_exec(nums[ii], ii);\n enc.push(e);\n ii += 1;\n }\n\n let sorted = merge_sort_exec(&enc);\n\n let mut a: Vec = Vec::new();\n let mut pp: usize = 0;\n while pp < n {\n let dv = decode_val_exec(sorted[pp]);\n let v32 = dv as i32;\n a.push(v32);\n pp += 1;\n }\n\n let mid = n / 2;\n let mut ans: i128 = 0;\n let mut idx: usize = 0;\n while idx < n {\n let v = a[idx];\n if idx < mid {\n if v > k {\n let d = v - k;\n ans = ans + d as i128;\n }\n } else if idx == mid {\n if v >= k {\n let d = v - k;\n ans = ans + d as i128;\n } else {\n let d = k - v;\n ans = ans + d as i128;\n }\n } else {\n if v < k {\n let d = k - v;\n ans = ans + d as i128;\n }\n }\n idx = idx + 1;\n }\n\n ans as i64\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sorted_between(a: Seq, from: int, to: int) -> bool {\n forall |i: int, j: int| from <= i < j < to ==> a[i] <= a[j]\n }\n\n pub open spec fn is_reorder_of(r: Seq, p: Seq, s: Seq) -> bool {\n &&& r.len() == s.len()\n &&& p.len() == s.len()\n &&& forall|i: int| 0 <= i < r.len() ==> 0 <= #[trigger] r[i] < r.len()\n &&& forall|i: int, j: int| 0 <= i < j < r.len() ==> r[i] != r[j]\n &&& p =~= r.map_values(|i: int| s[i])\n }\n\n pub open spec fn op_cost_at(v: int, idx: int, mid: int, k: int) -> int {\n if idx < mid {\n if v > k { v - k } else { 0 }\n } else if idx == mid {\n if v >= k { v - k } else { k - v }\n } else {\n if v < k { k - v } else { 0 }\n }\n }\n\n pub open spec fn cost_prefix(s: Seq, k: int, mid: int, n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else if n > s.len() {\n Self::cost_prefix(s, k, mid, s.len() as int)\n } else {\n Self::cost_prefix(s, k, mid, n - 1)\n + Self::op_cost_at(s[n - 1] as int, n - 1, mid, k)\n }\n }\n\n pub open spec fn cost_all(s: Seq, k: int) -> int {\n Self::cost_prefix(s, k, (s.len() / 2) as int, s.len() as int)\n }\n}\n\npub open spec fn encode(v: int, i: int) -> int {\n v * 200000 + i\n}\n\npub open spec fn decode_val(e: int) -> int {\n e / 200000\n}\n\npub open spec fn sorted_asc(s: Seq) -> bool {\n forall|a: int, b: int| 0 <= a <= b < s.len() ==> s[a] <= s[b]\n}\n\npub open spec fn merge_seq(a: Seq, b: Seq) -> Seq\n decreases a.len() + b.len()\n{\n if a.len() == 0 {\n b\n } else if b.len() == 0 {\n a\n } else if a[0] <= b[0] {\n seq![a[0]] + merge_seq(a.drop_first(), b)\n } else {\n seq![b[0]] + merge_seq(a, b.drop_first())\n }\n}\n\npub open spec fn merge_sort_seq(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() <= 1 {\n s\n } else {\n let mid = s.len() as int / 2;\n merge_seq(merge_sort_seq(s.subrange(0, mid)), merge_sort_seq(s.subrange(mid, s.len() as int)))\n }\n}\n\npub open spec fn to_int_seq64(s: Seq) -> Seq {\n s.map_values(|x: i64| x as int)\n}\n\nfn merge_exec(a: &Vec, b: &Vec) -> (result: Vec)\n requires\n sorted_asc(to_int_seq64(a@)),\n sorted_asc(to_int_seq64(b@)),\n ensures\n to_int_seq64(result@) == merge_seq(to_int_seq64(a@), to_int_seq64(b@)),\n{\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n while i < a.len() || j < b.len() {\n if j >= b.len() || (i < a.len() && a[i] <= b[j]) {\n result.push(a[i]);\n i += 1;\n } else {\n result.push(b[j]);\n j += 1;\n }\n }\n result\n}\n\nfn merge_sort_exec(v: &Vec) -> (result: Vec)\n requires v.len() <= 200_000,\n ensures to_int_seq64(result@) == merge_sort_seq(to_int_seq64(v@)),\n decreases v.len()\n{\n if v.len() <= 1 {\n let mut result: Vec = Vec::new();\n let mut k: usize = 0;\n while k < v.len() {\n result.push(v[k]);\n k += 1;\n }\n result\n } else {\n let mid = v.len() / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid {\n left.push(v[i]);\n i += 1;\n }\n let mut right: Vec = Vec::new();\n let mut i2: usize = mid;\n while i2 < v.len() {\n right.push(v[i2]);\n i2 += 1;\n }\n let sorted_left = merge_sort_exec(&left);\n let sorted_right = merge_sort_exec(&right);\n let result = merge_exec(&sorted_left, &sorted_right);\n result\n }\n}\n\nfn encode_exec(v: i32, i: usize) -> (result: i64)\n requires 1 <= v <= 1_000_000_000, i < 200000,\n ensures result as int == encode(v as int, i as int),\n{\n (v as i64) * 200000 + (i as i64)\n}\n\nfn decode_val_exec(e: i64) -> (result: i64)\n requires 0 <= e <= 1_000_000_000i64 * 200000i64 + 199999i64,\n ensures result as int == decode_val(e as int),\n{\n e / 200000\n}\n\nimpl Solution {\n pub fn min_operations_to_make_median_k(nums: Vec, k: i32) -> (result: i64)\n requires\n 1 <= nums.len() <= 200000,\n 1 <= k <= 1000000000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000000000,\n ensures\n exists|s: Seq, r: Seq|\n Self::sorted_between(s, 0, s.len() as int)\n && Self::is_reorder_of(r, s, nums@)\n && result as int == Self::cost_all(s, k as int),\n {\n let n = nums.len();\n\n let mut enc: Vec = Vec::new();\n let mut ii: usize = 0;\n while ii < n {\n let e = encode_exec(nums[ii], ii);\n enc.push(e);\n ii += 1;\n }\n\n let sorted = merge_sort_exec(&enc);\n\n let mut a: Vec = Vec::new();\n let mut pp: usize = 0;\n while pp < n {\n let dv = decode_val_exec(sorted[pp]);\n let v32 = dv as i32;\n a.push(v32);\n pp += 1;\n }\n\n let mid = n / 2;\n let mut ans: i128 = 0;\n let mut idx: usize = 0;\n while idx < n {\n let v = a[idx];\n if idx < mid {\n if v > k {\n let d = v - k;\n ans = ans + d as i128;\n }\n } else if idx == mid {\n if v >= k {\n let d = v - k;\n ans = ans + d as i128;\n } else {\n let d = k - v;\n ans = ans + d as i128;\n }\n } else {\n if v < k {\n let d = k - v;\n ans = ans + d as i128;\n }\n }\n idx = idx + 1;\n }\n\n ans as i64\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sorted_between(a: Seq, from: int, to: int) -> bool {\n forall |i: int, j: int| from <= i < j < to ==> a[i] <= a[j]\n }\n\n pub open spec fn is_reorder_of(r: Seq, p: Seq, s: Seq) -> bool {\n &&& r.len() == s.len()\n &&& p.len() == s.len()\n &&& forall|i: int| 0 <= i < r.len() ==> 0 <= #[trigger] r[i] < r.len()\n &&& forall|i: int, j: int| 0 <= i < j < r.len() ==> r[i] != r[j]\n &&& p =~= r.map_values(|i: int| s[i])\n }\n\n pub open spec fn op_cost_at(v: int, idx: int, mid: int, k: int) -> int {\n if idx < mid {\n if v > k { v - k } else { 0 }\n } else if idx == mid {\n if v >= k { v - k } else { k - v }\n } else {\n if v < k { k - v } else { 0 }\n }\n }\n\n pub open spec fn cost_prefix(s: Seq, k: int, mid: int, n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 0\n } else if n > s.len() {\n Self::cost_prefix(s, k, mid, s.len() as int)\n } else {\n Self::cost_prefix(s, k, mid, n - 1)\n + Self::op_cost_at(s[n - 1] as int, n - 1, mid, k)\n }\n }\n\n pub open spec fn cost_all(s: Seq, k: int) -> int {\n Self::cost_prefix(s, k, (s.len() / 2) as int, s.len() as int)\n }\n}\n\npub open spec fn encode(v: int, i: int) -> int {\n v * 200000 + i\n}\n\npub open spec fn decode_idx(e: int) -> int {\n e % 200000\n}\n\npub open spec fn decode_val(e: int) -> int {\n e / 200000\n}\n\nproof fn lemma_encode_decode_idx(v: int, i: int)\n requires 0 <= i < 200000,\n ensures decode_idx(encode(v, i)) == i,\n{\n assert(encode(v, i) == v * 200000 + i);\n}\n\nproof fn lemma_encode_decode_val(v: int, i: int)\n requires 0 <= i < 200000,\n ensures decode_val(encode(v, i)) == v,\n{\n assert(encode(v, i) == v * 200000 + i);\n}\n\nproof fn lemma_encode_order(v1: int, i1: int, v2: int, i2: int)\n requires 0 <= i1 < 200000, 0 <= i2 < 200000,\n ensures\n encode(v1, i1) < encode(v2, i2) <==> (v1 < v2 || (v1 == v2 && i1 < i2)),\n encode(v1, i1) == encode(v2, i2) <==> (v1 == v2 && i1 == i2),\n{\n if v1 < v2 {\n assert(encode(v1, i1) < encode(v2, i2)) by (nonlinear_arith)\n requires v1 < v2, 0 <= i1 < 200000, 0 <= i2 < 200000;\n } else if v1 > v2 {\n assert(encode(v1, i1) > encode(v2, i2)) by (nonlinear_arith)\n requires v1 > v2, 0 <= i1 < 200000, 0 <= i2 < 200000;\n }\n}\n\npub open spec fn sorted_asc(s: Seq) -> bool {\n forall|a: int, b: int| 0 <= a <= b < s.len() ==> s[a] <= s[b]\n}\n\npub open spec fn merge_seq(a: Seq, b: Seq) -> Seq\n decreases a.len() + b.len()\n{\n if a.len() == 0 {\n b\n } else if b.len() == 0 {\n a\n } else if a[0] <= b[0] {\n seq![a[0]] + merge_seq(a.drop_first(), b)\n } else {\n seq![b[0]] + merge_seq(a, b.drop_first())\n }\n}\n\nproof fn lemma_sorted_drop_first(s: Seq)\n requires sorted_asc(s), s.len() >= 1,\n ensures sorted_asc(s.drop_first()),\n{\n assert forall |a: int, b: int| 0 <= a <= b < s.drop_first().len() implies\n s.drop_first()[a] <= s.drop_first()[b] by {\n assert(s.drop_first()[a] == s[a + 1]);\n assert(s.drop_first()[b] == s[b + 1]);\n }\n}\n\nproof fn lemma_sorted_cons(x: int, rest: Seq)\n requires sorted_asc(rest), rest.len() == 0 || x <= rest[0],\n ensures sorted_asc(seq![x] + rest),\n{\n assert forall |a: int, b: int| 0 <= a <= b < (seq![x] + rest).len() implies\n (seq![x] + rest)[a] <= (seq![x] + rest)[b] by {\n if a == 0 {\n if b > 0 {\n assert((seq![x] + rest)[b] == rest[b - 1]);\n if b > 1 {\n assert(rest[0] <= rest[b - 1]);\n }\n }\n } else {\n assert((seq![x] + rest)[a] == rest[a - 1]);\n assert((seq![x] + rest)[b] == rest[b - 1]);\n }\n }\n}\n\nproof fn lemma_merge_seq_all_ge(a: Seq, b: Seq, lo: int)\n requires\n forall |i: int| 0 <= i < a.len() ==> lo <= a[i],\n forall |i: int| 0 <= i < b.len() ==> lo <= b[i],\n ensures forall |i: int| 0 <= i < merge_seq(a, b).len() ==> lo <= #[trigger] merge_seq(a, b)[i],\n decreases a.len() + b.len(),\n{\n if a.len() == 0 || b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_merge_seq_all_ge(a.drop_first(), b, lo);\n assert(merge_seq(a, b) =~= seq![a[0]] + merge_seq(a.drop_first(), b));\n } else {\n lemma_merge_seq_all_ge(a, b.drop_first(), lo);\n assert(merge_seq(a, b) =~= seq![b[0]] + merge_seq(a, b.drop_first()));\n }\n}\n\nproof fn lemma_merge_seq_sorted(a: Seq, b: Seq)\n requires sorted_asc(a), sorted_asc(b),\n ensures sorted_asc(merge_seq(a, b)),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 || b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_sorted_drop_first(a);\n lemma_merge_seq_sorted(a.drop_first(), b);\n if merge_seq(a.drop_first(), b).len() > 0 {\n if a.drop_first().len() > 0 {\n assert(a[0] <= a.drop_first()[0]);\n }\n lemma_merge_seq_all_ge(a.drop_first(), b, a[0]);\n }\n lemma_sorted_cons(a[0], merge_seq(a.drop_first(), b));\n assert(merge_seq(a, b) =~= seq![a[0]] + merge_seq(a.drop_first(), b));\n } else {\n lemma_sorted_drop_first(b);\n lemma_merge_seq_sorted(a, b.drop_first());\n if merge_seq(a, b.drop_first()).len() > 0 {\n if b.drop_first().len() > 0 {\n assert(b[0] <= b.drop_first()[0]);\n }\n lemma_merge_seq_all_ge(a, b.drop_first(), b[0]);\n }\n lemma_sorted_cons(b[0], merge_seq(a, b.drop_first()));\n assert(merge_seq(a, b) =~= seq![b[0]] + merge_seq(a, b.drop_first()));\n }\n}\n\nproof fn lemma_merge_seq_len(a: Seq, b: Seq)\n ensures merge_seq(a, b).len() == a.len() + b.len(),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 || b.len() == 0 {\n } else if a[0] <= b[0] {\n lemma_merge_seq_len(a.drop_first(), b);\n } else {\n lemma_merge_seq_len(a, b.drop_first());\n }\n}\n\npub open spec fn merge_sort_seq(s: Seq) -> Seq\n decreases s.len()\n{\n if s.len() <= 1 {\n s\n } else {\n let mid = s.len() as int / 2;\n merge_seq(merge_sort_seq(s.subrange(0, mid)), merge_sort_seq(s.subrange(mid, s.len() as int)))\n }\n}\n\nproof fn lemma_merge_sort_seq_sorted(s: Seq)\n ensures sorted_asc(merge_sort_seq(s)),\n decreases s.len()\n{\n if s.len() > 1 {\n let mid = s.len() as int / 2;\n lemma_merge_sort_seq_sorted(s.subrange(0, mid));\n lemma_merge_sort_seq_sorted(s.subrange(mid, s.len() as int));\n lemma_merge_seq_sorted(merge_sort_seq(s.subrange(0, mid)), merge_sort_seq(s.subrange(mid, s.len() as int)));\n }\n}\n\nproof fn lemma_merge_sort_seq_len(s: Seq)\n ensures merge_sort_seq(s).len() == s.len(),\n decreases s.len()\n{\n if s.len() > 1 {\n let mid = s.len() as int / 2;\n lemma_merge_sort_seq_len(s.subrange(0, mid));\n lemma_merge_sort_seq_len(s.subrange(mid, s.len() as int));\n lemma_merge_seq_len(merge_sort_seq(s.subrange(0, mid)), merge_sort_seq(s.subrange(mid, s.len() as int)));\n }\n}\n\npub open spec fn seq_count(s: Seq, v: int) -> int\n decreases s.len(),\n{\n if s.len() == 0 {\n 0\n } else {\n (if s.last() == v { 1int } else { 0int }) + seq_count(s.drop_last(), v)\n }\n}\n\nproof fn lemma_seq_count_nonneg(s: Seq, v: int)\n ensures seq_count(s, v) >= 0,\n decreases s.len(),\n{\n if s.len() > 0 {\n lemma_seq_count_nonneg(s.drop_last(), v);\n }\n}\n\nproof fn lemma_seq_count_cons(x: int, rest: Seq, v: int)\n ensures seq_count(seq![x] + rest, v) == seq_count(rest, v) + (if x == v { 1int } else { 0int }),\n decreases rest.len(),\n{\n if rest.len() == 0 {\n assert(seq![x] + rest =~= seq![x]);\n assert(seq_count(seq![x] + rest, v) == seq_count(seq![x], v));\n assert(seq![x].drop_last() =~= Seq::::empty());\n assert(seq_count(seq![x], v) == (if x == v { 1int } else { 0int }) + seq_count(seq![x].drop_last(), v));\n } else {\n assert((seq![x] + rest).drop_last() =~= seq![x] + rest.drop_last());\n assert((seq![x] + rest).last() == rest.last());\n lemma_seq_count_cons(x, rest.drop_last(), v);\n }\n}\n\nproof fn lemma_seq_count_concat(a: Seq, b: Seq, v: int)\n ensures seq_count(a + b, v) == seq_count(a, v) + seq_count(b, v),\n decreases b.len(),\n{\n if b.len() == 0 {\n assert(a + b =~= a);\n } else {\n assert((a + b).drop_last() =~= a + b.drop_last());\n assert((a + b).last() == b.last());\n lemma_seq_count_concat(a, b.drop_last(), v);\n }\n}\n\nproof fn lemma_seq_count_zero_no_contains(s: Seq, v: int)\n requires !s.contains(v),\n ensures seq_count(s, v) == 0,\n decreases s.len(),\n{\n if s.len() > 0 {\n assert(s.contains(s.last()));\n assert forall |i: int| 0 <= i < s.drop_last().len() implies s.drop_last()[i] == s[i] by {}\n if s.drop_last().contains(v) {\n let idx = choose |idx: int| 0 <= idx < s.drop_last().len() && s.drop_last()[idx] == v;\n assert(s[idx] == v);\n assert(false);\n }\n lemma_seq_count_zero_no_contains(s.drop_last(), v);\n }\n}\n\nproof fn lemma_merge_seq_count(a: Seq, b: Seq, v: int)\n ensures seq_count(merge_seq(a, b), v) == seq_count(a, v) + seq_count(b, v),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n assert(merge_seq(a, b) =~= b);\n assert(seq_count(a, v) == 0);\n } else if b.len() == 0 {\n assert(merge_seq(a, b) =~= a);\n assert(seq_count(b, v) == 0);\n } else if a[0] <= b[0] {\n lemma_merge_seq_count(a.drop_first(), b, v);\n assert(merge_seq(a, b) =~= seq![a[0]] + merge_seq(a.drop_first(), b));\n lemma_seq_count_cons(a[0], merge_seq(a.drop_first(), b), v);\n assert(a =~= seq![a[0]] + a.drop_first());\n lemma_seq_count_cons(a[0], a.drop_first(), v);\n } else {\n lemma_merge_seq_count(a, b.drop_first(), v);\n assert(merge_seq(a, b) =~= seq![b[0]] + merge_seq(a, b.drop_first()));\n lemma_seq_count_cons(b[0], merge_seq(a, b.drop_first()), v);\n assert(b =~= seq![b[0]] + b.drop_first());\n lemma_seq_count_cons(b[0], b.drop_first(), v);\n }\n}\n\nproof fn lemma_merge_sort_seq_count(s: Seq, v: int)\n ensures seq_count(merge_sort_seq(s), v) == seq_count(s, v),\n decreases s.len(),\n{\n if s.len() > 1 {\n let mid = s.len() as int / 2;\n lemma_merge_sort_seq_count(s.subrange(0, mid), v);\n lemma_merge_sort_seq_count(s.subrange(mid, s.len() as int), v);\n lemma_merge_seq_count(merge_sort_seq(s.subrange(0, mid)), merge_sort_seq(s.subrange(mid, s.len() as int)), v);\n assert(s =~= s.subrange(0, mid) + s.subrange(mid, s.len() as int));\n lemma_seq_count_concat(s.subrange(0, mid), s.subrange(mid, s.len() as int), v);\n }\n}\n\nproof fn lemma_merge_seq_contains(a: Seq, b: Seq, v: int)\n ensures merge_seq(a, b).contains(v) <==> (a.contains(v) || b.contains(v)),\n decreases a.len() + b.len(),\n{\n if a.len() == 0 {\n assert(merge_seq(a, b) =~= b);\n } else if b.len() == 0 {\n assert(merge_seq(a, b) =~= a);\n } else if a[0] <= b[0] {\n lemma_merge_seq_contains(a.drop_first(), b, v);\n assert(merge_seq(a, b) =~= seq![a[0]] + merge_seq(a.drop_first(), b));\n assert(a =~= seq![a[0]] + a.drop_first());\n vstd::seq_lib::lemma_seq_concat_contains_all_elements(seq![a[0]], merge_seq(a.drop_first(), b), v);\n vstd::seq_lib::lemma_seq_concat_contains_all_elements(seq![a[0]], a.drop_first(), v);\n assert(seq![a[0]][0] == a[0]);\n if seq![a[0]].contains(v) {\n let idx = choose |idx: int| 0 <= idx < seq![a[0]].len() && seq![a[0]][idx] == v;\n assert(idx == 0);\n }\n } else {\n lemma_merge_seq_contains(a, b.drop_first(), v);\n assert(merge_seq(a, b) =~= seq![b[0]] + merge_seq(a, b.drop_first()));\n vstd::seq_lib::lemma_seq_concat_contains_all_elements(seq![b[0]], merge_seq(a, b.drop_first()), v);\n vstd::seq_lib::lemma_seq_concat_contains_all_elements(seq![b[0]], b.drop_first(), v);\n assert(seq![b[0]][0] == b[0]);\n if seq![b[0]].contains(v) {\n let idx = choose |idx: int| 0 <= idx < seq![b[0]].len() && seq![b[0]][idx] == v;\n assert(idx == 0);\n }\n assert(b =~= seq![b[0]] + b.drop_first());\n }\n}\n\npub open spec fn pts_encoded(nums: Seq) -> Seq\n decreases nums.len()\n{\n if nums.len() == 0 {\n Seq::empty()\n } else {\n pts_encoded(nums.subrange(0, nums.len() - 1)).push(encode(nums[nums.len() - 1] as int, nums.len() - 1))\n }\n}\n\nproof fn lemma_pts_encoded_len(nums: Seq)\n ensures pts_encoded(nums).len() == nums.len(),\n decreases nums.len(),\n{\n if nums.len() > 0 {\n lemma_pts_encoded_len(nums.subrange(0, nums.len() - 1));\n }\n}\n\nproof fn lemma_pts_encoded_index(nums: Seq, i: int)\n requires 0 <= i < nums.len(),\n ensures pts_encoded(nums)[i] == encode(nums[i] as int, i),\n decreases nums.len(),\n{\n lemma_pts_encoded_len(nums);\n if i < nums.len() - 1 {\n lemma_pts_encoded_index(nums.subrange(0, nums.len() - 1), i);\n assert(nums.subrange(0, nums.len() - 1)[i] == nums[i]);\n }\n}\n\nproof fn lemma_pts_encoded_count_le1(nums: Seq, v: int)\n requires nums.len() <= 200000,\n ensures seq_count(pts_encoded(nums), v) <= 1,\n decreases nums.len(),\n{\n lemma_pts_encoded_len(nums);\n if nums.len() > 0 {\n let prev = nums.subrange(0, nums.len() - 1);\n lemma_pts_encoded_count_le1(prev, v);\n let lastidx = nums.len() - 1;\n let lastenc = encode(nums[lastidx] as int, lastidx);\n assert(pts_encoded(nums) =~= pts_encoded(prev).push(lastenc));\n assert(pts_encoded(nums).drop_last() =~= pts_encoded(prev));\n assert(pts_encoded(nums).last() == lastenc);\n if lastenc == v {\n if pts_encoded(prev).contains(v) {\n lemma_pts_encoded_len(prev);\n let i = choose |i: int| 0 <= i < prev.len() && pts_encoded(prev)[i] == v;\n lemma_pts_encoded_index(prev, i);\n assert(prev[i] == nums[i]);\n assert(encode(nums[i] as int, i) == v);\n assert(v == lastenc);\n assert(encode(nums[i] as int, i) == encode(nums[lastidx] as int, lastidx));\n lemma_encode_order(nums[i] as int, i, nums[lastidx] as int, lastidx);\n assert(i == lastidx);\n assert(false);\n }\n lemma_seq_count_zero_no_contains(pts_encoded(prev), v);\n }\n }\n}\n\npub open spec fn sorted_enc(nums: Seq) -> Seq {\n merge_sort_seq(pts_encoded(nums))\n}\n\nproof fn lemma_sorted_enc_props(nums: Seq)\n requires nums.len() <= 200000,\n ensures\n sorted_asc(sorted_enc(nums)),\n sorted_enc(nums).len() == nums.len(),\n forall |v: int| sorted_enc(nums).contains(v) <==> pts_encoded(nums).contains(v),\n forall |v: int| seq_count(sorted_enc(nums), v) <= 1,\n{\n lemma_merge_sort_seq_sorted(pts_encoded(nums));\n lemma_merge_sort_seq_len(pts_encoded(nums));\n lemma_pts_encoded_len(nums);\n assert forall |v: int| sorted_enc(nums).contains(v) <==> pts_encoded(nums).contains(v) by {\n lemma_merge_seq_contains_wrap(nums, v);\n }\n assert forall |v: int| seq_count(sorted_enc(nums), v) <= 1 by {\n lemma_merge_sort_seq_count(pts_encoded(nums), v);\n lemma_pts_encoded_count_le1(nums, v);\n }\n}\n\nproof fn lemma_merge_seq_contains_wrap(nums: Seq, v: int)\n ensures sorted_enc(nums).contains(v) <==> pts_encoded(nums).contains(v),\n decreases pts_encoded(nums).len(),\n{\n lemma_merge_sort_seq_contains(pts_encoded(nums), v);\n}\n\nproof fn lemma_merge_sort_seq_contains(s: Seq, v: int)\n ensures merge_sort_seq(s).contains(v) <==> s.contains(v),\n decreases s.len(),\n{\n if s.len() <= 1 {\n } else {\n let mid = s.len() as int / 2;\n lemma_merge_sort_seq_contains(s.subrange(0, mid), v);\n lemma_merge_sort_seq_contains(s.subrange(mid, s.len() as int), v);\n lemma_merge_seq_contains(merge_sort_seq(s.subrange(0, mid)), merge_sort_seq(s.subrange(mid, s.len() as int)), v);\n assert(s =~= s.subrange(0, mid) + s.subrange(mid, s.len() as int));\n assert forall |x: int| s.contains(x) implies (s.subrange(0, mid).contains(x) || s.subrange(mid, s.len() as int).contains(x)) by {\n if s.contains(x) {\n let k = choose |k: int| 0 <= k < s.len() && s[k] == x;\n if k < mid {\n assert(s.subrange(0, mid)[k] == x);\n } else {\n assert(s.subrange(mid, s.len() as int)[k - mid] == x);\n }\n }\n }\n assert forall |x: int| (s.subrange(0, mid).contains(x) || s.subrange(mid, s.len() as int).contains(x)) implies s.contains(x) by {\n if s.subrange(0, mid).contains(x) {\n let k = choose |k: int| 0 <= k < mid && s.subrange(0, mid)[k] == x;\n assert(s[k] == x);\n }\n if s.subrange(mid, s.len() as int).contains(x) {\n let k = choose |k: int| 0 <= k < s.len() - mid && s.subrange(mid, s.len() as int)[k] == x;\n assert(s[k + mid] == x);\n }\n }\n }\n}\n\nproof fn lemma_sorted_enc_pos_to_index(nums: Seq, p: int)\n requires\n 0 <= p < nums.len(),\n nums.len() <= 200000,\n ensures\n 0 <= decode_idx(sorted_enc(nums)[p]) < nums.len(),\n sorted_enc(nums)[p] == encode(nums[decode_idx(sorted_enc(nums)[p])] as int, decode_idx(sorted_enc(nums)[p])),\n{\n lemma_sorted_enc_props(nums);\n assert(sorted_enc(nums).contains(sorted_enc(nums)[p]));\n assert(pts_encoded(nums).contains(sorted_enc(nums)[p]));\n lemma_pts_encoded_len(nums);\n let i = choose |i: int| 0 <= i < nums.len() && pts_encoded(nums)[i] == sorted_enc(nums)[p];\n lemma_pts_encoded_index(nums, i);\n assert(sorted_enc(nums)[p] == encode(nums[i] as int, i));\n lemma_encode_decode_idx(nums[i] as int, i);\n assert(decode_idx(sorted_enc(nums)[p]) == i);\n}\n\nproof fn lemma_sorted_enc_injective(nums: Seq, p1: int, p2: int)\n requires\n 0 <= p1 < nums.len(), 0 <= p2 < nums.len(), p1 != p2,\n nums.len() <= 200000,\n ensures decode_idx(sorted_enc(nums)[p1]) != decode_idx(sorted_enc(nums)[p2]),\n{\n lemma_sorted_enc_props(nums);\n lemma_sorted_enc_pos_to_index(nums, p1);\n lemma_sorted_enc_pos_to_index(nums, p2);\n if decode_idx(sorted_enc(nums)[p1]) == decode_idx(sorted_enc(nums)[p2]) {\n assert(sorted_enc(nums)[p1] == sorted_enc(nums)[p2]);\n let v = sorted_enc(nums)[p1];\n assert(sorted_enc(nums).drop_last().len() >= 0);\n lemma_two_distinct_positions_count_ge2(sorted_enc(nums), p1, p2, v);\n assert(seq_count(sorted_enc(nums), v) >= 2);\n assert(false);\n }\n}\n\nproof fn lemma_two_distinct_positions_count_ge2(s: Seq, p1: int, p2: int, v: int)\n requires\n 0 <= p1 < s.len(), 0 <= p2 < s.len(), p1 != p2,\n s[p1] == v, s[p2] == v,\n ensures seq_count(s, v) >= 2,\n decreases s.len(),\n{\n lemma_seq_count_nonneg(s, v);\n let lo = if p1 < p2 { p1 } else { p2 };\n let hi = if p1 < p2 { p2 } else { p1 };\n assert(0 <= lo < hi < s.len());\n assert(s[lo] == v);\n assert(s[hi] == v);\n if hi == s.len() - 1 {\n assert(s.last() == v);\n assert(s.drop_last()[lo] == v);\n assert(0 <= lo < s.drop_last().len());\n lemma_seq_count_contains_ge1(s.drop_last(), v, lo);\n assert(seq_count(s, v) == 1 + seq_count(s.drop_last(), v));\n } else {\n lemma_two_distinct_positions_count_ge2(s.drop_last(), p1, p2, v);\n }\n}\n\nproof fn lemma_seq_count_contains_ge1(s: Seq, v: int, idx: int)\n requires 0 <= idx < s.len(), s[idx] == v,\n ensures seq_count(s, v) >= 1,\n decreases s.len(),\n{\n lemma_seq_count_nonneg(s, v);\n if idx == s.len() - 1 {\n assert(s.last() == v);\n lemma_seq_count_nonneg(s.drop_last(), v);\n } else {\n assert(s.drop_last()[idx] == v);\n lemma_seq_count_contains_ge1(s.drop_last(), v, idx);\n }\n}\n\npub open spec fn to_int_seq64(s: Seq) -> Seq {\n s.map_values(|x: i64| x as int)\n}\n\nproof fn lemma_merge_seq_skip_step_a(a: Seq, b: Seq, i: int, j: int)\n requires 0 <= i < a.len(), 0 <= j <= b.len(), (j >= b.len() || a[i] <= b[j]),\n ensures merge_seq(a.skip(i), b.skip(j)) =~= seq![a[i]] + merge_seq(a.skip(i + 1), b.skip(j)),\n{\n assert(a.skip(i)[0] == a[i]);\n assert(a.skip(i).drop_first() =~= a.skip(i + 1));\n if j < b.len() {\n assert(b.skip(j)[0] == b[j]);\n assert(a.skip(i)[0] <= b.skip(j)[0]);\n } else {\n assert(b.skip(j).len() == 0);\n }\n}\n\nproof fn lemma_merge_seq_skip_step_b(a: Seq, b: Seq, i: int, j: int)\n requires 0 <= i <= a.len(), 0 <= j < b.len(), (i >= a.len() || b[j] < a[i]),\n ensures merge_seq(a.skip(i), b.skip(j)) =~= seq![b[j]] + merge_seq(a.skip(i), b.skip(j + 1)),\n{\n assert(b.skip(j)[0] == b[j]);\n assert(b.skip(j).drop_first() =~= b.skip(j + 1));\n if i < a.len() {\n assert(a.skip(i)[0] == a[i]);\n assert(b.skip(j)[0] < a.skip(i)[0]);\n } else {\n assert(a.skip(i).len() == 0);\n }\n}\n\nfn merge_exec(a: &Vec, b: &Vec) -> (result: Vec)\n requires\n sorted_asc(to_int_seq64(a@)),\n sorted_asc(to_int_seq64(b@)),\n ensures\n to_int_seq64(result@) == merge_seq(to_int_seq64(a@), to_int_seq64(b@)),\n{\n let ghost av = to_int_seq64(a@);\n let ghost bv = to_int_seq64(b@);\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n let mut j: usize = 0;\n proof {\n assert(av.skip(0) =~= av);\n assert(bv.skip(0) =~= bv);\n assert(to_int_seq64(result@) =~= Seq::::empty());\n }\n while i < a.len() || j < b.len()\n invariant\n i <= a.len(),\n j <= b.len(),\n result.len() == i + j,\n to_int_seq64(a@) == av,\n to_int_seq64(b@) == bv,\n to_int_seq64(result@) + merge_seq(av.skip(i as int), bv.skip(j as int)) == merge_seq(av, bv),\n decreases (a.len() - i) + (b.len() - j),\n {\n if j >= b.len() || (i < a.len() && a[i] <= b[j]) {\n proof {\n lemma_merge_seq_skip_step_a(av, bv, i as int, j as int);\n assert(to_int_seq64(result@).push(a@[i as int] as int) =~= to_int_seq64(result@) + seq![a@[i as int] as int]);\n }\n result.push(a[i]);\n i += 1;\n } else {\n proof {\n lemma_merge_seq_skip_step_b(av, bv, i as int, j as int);\n assert(to_int_seq64(result@).push(b@[j as int] as int) =~= to_int_seq64(result@) + seq![b@[j as int] as int]);\n }\n result.push(b[j]);\n j += 1;\n }\n }\n proof {\n assert(av.skip(i as int).len() == 0);\n assert(bv.skip(j as int).len() == 0);\n assert(merge_seq(av.skip(i as int), bv.skip(j as int)) =~= Seq::::empty());\n assert(to_int_seq64(result@) == merge_seq(av, bv));\n }\n result\n}\n\nfn merge_sort_exec(v: &Vec) -> (result: Vec)\n requires v.len() <= 200_000,\n ensures to_int_seq64(result@) == merge_sort_seq(to_int_seq64(v@)),\n decreases v.len()\n{\n if v.len() <= 1 {\n let mut result: Vec = Vec::new();\n let mut k: usize = 0;\n while k < v.len()\n invariant k <= v.len(), result@.len() == k as int,\n forall |t: int| 0 <= t < k ==> result@[t] == v@[t],\n decreases v.len() - k,\n {\n result.push(v[k]);\n k += 1;\n }\n proof {\n assert(result@ =~= v@);\n }\n result\n } else {\n let mid = v.len() / 2;\n let mut left: Vec = Vec::new();\n let mut i: usize = 0;\n while i < mid\n invariant i <= mid, mid <= v.len(), left@.len() == i as int,\n forall |t: int| 0 <= t < i ==> left@[t] == v@[t],\n decreases mid - i,\n {\n left.push(v[i]);\n i += 1;\n }\n let mut right: Vec = Vec::new();\n let mut i2: usize = mid;\n while i2 < v.len()\n invariant mid <= i2 <= v.len(), right@.len() == i2 - mid,\n forall |t: int| 0 <= t < i2 - mid ==> right@[t as int] == v@[t + mid as int],\n decreases v.len() - i2,\n {\n right.push(v[i2]);\n i2 += 1;\n }\n proof {\n assert(left@ =~= v@.subrange(0, mid as int));\n assert(right@ =~= v@.subrange(mid as int, v@.len() as int));\n }\n let sorted_left = merge_sort_exec(&left);\n let sorted_right = merge_sort_exec(&right);\n proof {\n lemma_merge_sort_seq_sorted(to_int_seq64(v@).subrange(0, mid as int));\n lemma_merge_sort_seq_sorted(to_int_seq64(v@).subrange(mid as int, v@.len() as int));\n assert(to_int_seq64(left@) =~= to_int_seq64(v@).subrange(0, mid as int));\n assert(to_int_seq64(right@) =~= to_int_seq64(v@).subrange(mid as int, v@.len() as int));\n }\n let result = merge_exec(&sorted_left, &sorted_right);\n proof {\n assert(to_int_seq64(result@) == merge_seq(to_int_seq64(sorted_left@), to_int_seq64(sorted_right@)));\n assert(merge_sort_seq(to_int_seq64(v@)) ==\n merge_seq(merge_sort_seq(to_int_seq64(v@).subrange(0, mid as int)),\n merge_sort_seq(to_int_seq64(v@).subrange(mid as int, v@.len() as int))));\n }\n result\n }\n}\n\nfn encode_exec(v: i32, i: usize) -> (result: i64)\n requires 1 <= v <= 1_000_000_000, i < 200000,\n ensures result as int == encode(v as int, i as int),\n{\n (v as i64) * 200000 + (i as i64)\n}\n\nfn decode_val_exec(e: i64) -> (result: i64)\n requires 0 <= e <= 1_000_000_000i64 * 200000i64 + 199999i64,\n ensures result as int == decode_val(e as int),\n{\n e / 200000\n}\n\nimpl Solution {\n pub fn min_operations_to_make_median_k(nums: Vec, k: i32) -> (result: i64)\n requires\n 1 <= nums.len() <= 200000,\n 1 <= k <= 1000000000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 1000000000,\n ensures\n exists|s: Seq, r: Seq|\n Self::sorted_between(s, 0, s.len() as int)\n && Self::is_reorder_of(r, s, nums@)\n && result as int == Self::cost_all(s, k as int),\n {\n let n = nums.len();\n let ghost old_nums = nums@;\n\n let mut enc: Vec = Vec::new();\n let mut ii: usize = 0;\n while ii < n\n invariant\n ii <= n,\n n == nums.len(),\n n <= 200000,\n enc.len() == ii,\n forall |t: int| 0 <= t < n as int ==> 1 <= #[trigger] nums@[t] <= 1_000_000_000,\n forall |t: int| 0 <= t < ii as int ==> enc@[t] as int == encode(nums@[t] as int, t),\n decreases n - ii,\n {\n let e = encode_exec(nums[ii], ii);\n enc.push(e);\n ii += 1;\n }\n proof {\n lemma_pts_encoded_len(nums@);\n assert forall |t: int| 0 <= t < n as int implies enc@[t] as int == pts_encoded(nums@)[t] by {\n lemma_pts_encoded_index(nums@, t);\n }\n assert(to_int_seq64(enc@).len() == pts_encoded(nums@).len());\n assert(to_int_seq64(enc@) =~= pts_encoded(nums@));\n }\n\n let sorted = merge_sort_exec(&enc);\n proof {\n assert(to_int_seq64(sorted@) == merge_sort_seq(pts_encoded(nums@)));\n assert(to_int_seq64(sorted@) == sorted_enc(nums@));\n lemma_sorted_enc_props(nums@);\n }\n\n let mut a: Vec = Vec::new();\n let mut pp: usize = 0;\n while pp < n\n invariant\n pp <= n,\n n == nums.len(),\n n <= 200000,\n sorted.len() == n,\n to_int_seq64(sorted@) == sorted_enc(nums@),\n forall |t: int| 0 <= t < n as int ==> 1 <= #[trigger] nums@[t] <= 1_000_000_000,\n a.len() == pp,\n forall |t: int| 0 <= t < pp as int ==> a@[t] as int == decode_val(sorted_enc(nums@)[t]),\n forall |t: int| 0 <= t < pp as int ==> a@[t] as int == nums@[decode_idx(sorted_enc(nums@)[t])] as int,\n decreases n - pp,\n {\n proof {\n assert(sorted@[pp as int] as int == sorted_enc(nums@)[pp as int]);\n assert(0 <= sorted_enc(nums@)[pp as int] <= 1_000_000_000i64 as int * 200000i64 as int + 199999i64 as int) by {\n lemma_sorted_enc_pos_to_index(nums@, pp as int);\n let idx = decode_idx(sorted_enc(nums@)[pp as int]);\n assert(sorted_enc(nums@)[pp as int] == encode(nums@[idx] as int, idx));\n assert(1 <= nums@[idx] <= 1000000000);\n assert(0 <= idx < n as int);\n };\n }\n let dv = decode_val_exec(sorted[pp]);\n proof {\n lemma_sorted_enc_pos_to_index(nums@, pp as int);\n let idx = decode_idx(sorted_enc(nums@)[pp as int]);\n assert(sorted_enc(nums@)[pp as int] == encode(nums@[idx] as int, idx));\n lemma_encode_decode_val(nums@[idx] as int, idx);\n assert(dv as int == nums@[idx] as int);\n assert(1 <= dv as int <= 1000000000);\n }\n let v32 = dv as i32;\n proof {\n assert(v32 as int == dv as int);\n }\n a.push(v32);\n proof {\n assert(a@[pp as int] as int == v32 as int);\n assert(a@[pp as int] as int == decode_val(sorted_enc(nums@)[pp as int]));\n let idx = decode_idx(sorted_enc(nums@)[pp as int]);\n assert(a@[pp as int] as int == nums@[idx] as int);\n }\n pp += 1;\n }\n\n proof {\n assert(a@.len() == n as int);\n assert forall |i1: int, j1: int| 0 <= i1 < j1 < n as int implies a@[i1] <= a@[j1] by {\n assert(sorted_enc(nums@)[i1] <= sorted_enc(nums@)[j1]);\n let idx_i = decode_idx(sorted_enc(nums@)[i1]);\n let idx_j = decode_idx(sorted_enc(nums@)[j1]);\n lemma_sorted_enc_pos_to_index(nums@, i1);\n lemma_sorted_enc_pos_to_index(nums@, j1);\n assert(sorted_enc(nums@)[i1] == encode(nums@[idx_i] as int, idx_i));\n assert(sorted_enc(nums@)[j1] == encode(nums@[idx_j] as int, idx_j));\n if nums@[idx_i] as int > nums@[idx_j] as int {\n lemma_encode_order(nums@[idx_i] as int, idx_i, nums@[idx_j] as int, idx_j);\n assert(encode(nums@[idx_i] as int, idx_i) > encode(nums@[idx_j] as int, idx_j));\n assert(false);\n }\n assert(a@[i1] as int == nums@[idx_i] as int);\n assert(a@[j1] as int == nums@[idx_j] as int);\n }\n assert(Self::sorted_between(a@, 0, a@.len() as int));\n\n let r = Seq::new(n as nat, |p: int| decode_idx(sorted_enc(nums@)[p]));\n assert forall |p: int| 0 <= p < r.len() implies 0 <= #[trigger] r[p] < r.len() by {\n lemma_sorted_enc_pos_to_index(nums@, p);\n }\n assert forall |i1: int, j1: int| 0 <= i1 < j1 < r.len() implies r[i1] != r[j1] by {\n lemma_sorted_enc_injective(nums@, i1, j1);\n }\n assert forall |p: int| 0 <= p < n as int implies #[trigger] a@[p] == r.map_values(|i: int| nums@[i])[p] by {\n assert(r.map_values(|i: int| nums@[i])[p] == nums@[r[p]]);\n assert(r[p] == decode_idx(sorted_enc(nums@)[p]));\n }\n assert(a@ =~= r.map_values(|i: int| nums@[i]));\n assert(Self::is_reorder_of(r, a@, nums@));\n }\n\n let mid = n / 2;\n let mut ans: i128 = 0;\n let mut idx: usize = 0;\n while idx < n\n invariant\n n == a.len(),\n n <= 200000,\n 1 <= k <= 1000000000,\n mid == n / 2,\n 0 <= idx <= n,\n forall |t: int| 0 <= t < n as int ==> 1 <= #[trigger] a@[t] <= 1000000000,\n Self::sorted_between(a@, 0, n as int),\n ans as int == Self::cost_prefix(a@, k as int, mid as int, idx as int),\n 0 <= ans as int <= idx as int * 1000000000,\n decreases n - idx,\n {\n let ghost old_ans = ans as int;\n let v = a[idx];\n\n if idx < mid {\n if v > k {\n let d = v - k;\n ans = ans + d as i128;\n }\n proof {\n if v > k {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == v as int - k as int);\n } else {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == 0);\n }\n }\n } else if idx == mid {\n if v >= k {\n let d = v - k;\n ans = ans + d as i128;\n } else {\n let d = k - v;\n ans = ans + d as i128;\n }\n proof {\n if v >= k {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == v as int - k as int);\n } else {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == k as int - v as int);\n }\n }\n } else {\n if v < k {\n let d = k - v;\n ans = ans + d as i128;\n }\n proof {\n if v < k {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == k as int - v as int);\n } else {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == 0);\n }\n }\n }\n\n proof {\n assert(1 <= v <= 1000000000);\n if idx < mid {\n if v > k {\n assert(0 < v as int - k as int <= 1000000000);\n }\n } else if idx == mid {\n if v >= k {\n assert(0 <= v as int - k as int <= 1000000000);\n } else {\n assert(0 < k as int - v as int <= 1000000000);\n }\n } else {\n if v < k {\n assert(0 < k as int - v as int <= 1000000000);\n }\n }\n\n assert(Self::cost_prefix(a@, k as int, mid as int, idx as int + 1)\n == Self::cost_prefix(a@, k as int, mid as int, idx as int)\n + Self::op_cost_at(a@[idx as int] as int, idx as int, mid as int, k as int));\n assert(a@[idx as int] == v);\n assert(ans as int == old_ans + Self::op_cost_at(v as int, idx as int, mid as int, k as int));\n assert(ans as int == Self::cost_prefix(a@, k as int, mid as int, idx as int + 1));\n\n assert(0 <= Self::op_cost_at(v as int, idx as int, mid as int, k as int) <= 1000000000) by {\n if idx < mid {\n if v > k {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == v as int - k as int);\n } else {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == 0);\n }\n } else if idx == mid {\n if v >= k {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == v as int - k as int);\n } else {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == k as int - v as int);\n }\n } else {\n if v < k {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == k as int - v as int);\n } else {\n assert(Self::op_cost_at(v as int, idx as int, mid as int, k as int) == 0);\n }\n }\n };\n\n assert(0 <= ans as int);\n assert(ans as int <= (idx as int + 1) * 1000000000);\n }\n\n idx = idx + 1;\n }\n\n proof {\n assert(idx == n);\n assert(ans as int == Self::cost_prefix(a@, k as int, mid as int, n as int));\n assert(mid as int == (a@.len() / 2) as int);\n assert(ans as int == Self::cost_all(a@, k as int));\n\n let r_final = Seq::new(n as nat, |p: int| decode_idx(sorted_enc(nums@)[p]));\n assert(Self::is_reorder_of(r_final, a@, nums@));\n assert(exists|s: Seq, r: Seq|\n Self::sorted_between(s, 0, s.len() as int)\n && Self::is_reorder_of(r, s, nums@)\n && (ans as i64) as int == Self::cost_all(s, k as int)) by {\n let s = a@;\n let r = r_final;\n assert(Self::sorted_between(s, 0, s.len() as int));\n assert(Self::is_reorder_of(r, s, nums@));\n assert((ans as i64) as int == ans as int);\n assert(ans as int == Self::cost_all(s, k as int));\n };\n }\n\n ans as i64\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3107/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3107/tests/mutated_testcases.jsonl"} {"problem": "lc3115", "family": "leetcode", "description": "# Maximum Prime Difference\n\nYou are given an integer array `nums`.\n\nReturn an integer that is the **maximum** distance between the **indices** of two (not necessarily different) prime numbers in `nums`*.*\n\n \n\nExample 1:\n\n**Input:** nums = [4,2,9,5,3]\n\n**Output:** 3\n\n**Explanation:** `nums[1]`, `nums[3]`, and `nums[4]` are prime. So the answer is `|4 - 1| = 3`.\n\nExample 2:\n\n**Input:** nums = [4,8,2,8]\n\n**Output:** 0\n\n**Explanation:** `nums[2]` is prime. Because there is just one prime number, the answer is `|2 - 2| = 0`.\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 3 * 10^5`\n\n\t- `1 <= nums[i] <= 100`\n\n\t- The input is generated such that the number of prime numbers in the `nums` is at least one.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn maximum_prime_difference(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math, Number Theory\nAcceptance rate: 58.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_prime(n: int) -> bool {\n n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19\n || n == 23 || n == 29 || n == 31 || n == 37 || n == 41 || n == 43 || n == 47\n || n == 53 || n == 59 || n == 61 || n == 67 || n == 71 || n == 73 || n == 79\n || n == 83 || n == 89 || n == 97\n }\n\n pub fn maximum_prime_difference(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 300000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n exists|i: int| 0 <= i < nums.len() && Self::is_prime(nums[i] as int),\n ensures\n result >= 0,\n exists|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n && #[trigger] (j - i) == result,\n forall|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n ==> #[trigger] (j - i) <= result,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_prime_exec(n: i32) -> bool {\n n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19\n || n == 23 || n == 29 || n == 31 || n == 37 || n == 41 || n == 43 || n == 47\n || n == 53 || n == 59 || n == 61 || n == 67 || n == 71 || n == 73 || n == 79\n || n == 83 || n == 89 || n == 97\n }\n\n pub fn maximum_prime_difference(nums: Vec) -> i32 {\n let n = nums.len();\n let mut first: usize = 0;\n let mut last: usize = 0;\n let mut found = false;\n let mut idx: usize = 0;\n\n while idx < n {\n if Self::is_prime_exec(nums[idx]) {\n if !found {\n first = idx;\n last = idx;\n found = true;\n } else {\n last = idx;\n }\n }\n idx = idx + 1;\n }\n\n let mut result: i32 = 0;\n let mut t: usize = first;\n while t < last {\n t = t + 1;\n result = result + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_prime(n: int) -> bool {\n n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19\n || n == 23 || n == 29 || n == 31 || n == 37 || n == 41 || n == 43 || n == 47\n || n == 53 || n == 59 || n == 61 || n == 67 || n == 71 || n == 73 || n == 79\n || n == 83 || n == 89 || n == 97\n }\n\n pub fn is_prime_exec(n: i32) -> (result: bool)\n ensures\n result == Self::is_prime(n as int),\n {\n n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19\n || n == 23 || n == 29 || n == 31 || n == 37 || n == 41 || n == 43 || n == 47\n || n == 53 || n == 59 || n == 61 || n == 67 || n == 71 || n == 73 || n == 79\n || n == 83 || n == 89 || n == 97\n }\n\n pub fn maximum_prime_difference(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 300000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n exists|i: int| 0 <= i < nums.len() && Self::is_prime(nums[i] as int),\n ensures\n result >= 0,\n exists|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n && #[trigger] (j - i) == result,\n forall|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n ==> #[trigger] (j - i) <= result,\n {\n let n = nums.len();\n let mut first: usize = 0;\n let mut last: usize = 0;\n let mut found = false;\n let mut idx: usize = 0;\n\n while idx < n {\n if Self::is_prime_exec(nums[idx]) {\n if !found {\n first = idx;\n last = idx;\n found = true;\n } else {\n last = idx;\n }\n }\n idx = idx + 1;\n }\n\n let mut result: i32 = 0;\n let mut t: usize = first;\n while t < last {\n t = t + 1;\n result = result + 1;\n }\n\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_prime(n: int) -> bool {\n n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19\n || n == 23 || n == 29 || n == 31 || n == 37 || n == 41 || n == 43 || n == 47\n || n == 53 || n == 59 || n == 61 || n == 67 || n == 71 || n == 73 || n == 79\n || n == 83 || n == 89 || n == 97\n }\n\n pub fn is_prime_exec(n: i32) -> (result: bool)\n ensures\n result == Self::is_prime(n as int),\n {\n n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19\n || n == 23 || n == 29 || n == 31 || n == 37 || n == 41 || n == 43 || n == 47\n || n == 53 || n == 59 || n == 61 || n == 67 || n == 71 || n == 73 || n == 79\n || n == 83 || n == 89 || n == 97\n }\n\n pub fn maximum_prime_difference(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 300000,\n forall|i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n exists|i: int| 0 <= i < nums.len() && Self::is_prime(nums[i] as int),\n ensures\n result >= 0,\n exists|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n && #[trigger] (j - i) == result,\n forall|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n ==> #[trigger] (j - i) <= result,\n {\n let n = nums.len();\n let mut first: usize = 0;\n let mut last: usize = 0;\n let mut found = false;\n let mut idx: usize = 0;\n\n while idx < n\n invariant\n 1 <= n <= 300000,\n n == nums.len(),\n 0 <= idx <= n,\n forall|k: int| 0 <= k < n as int ==> 1 <= #[trigger] nums[k] <= 100,\n !found ==> forall|k: int| 0 <= k < idx as int ==> !Self::is_prime(#[trigger] nums[k] as int),\n found ==> 0 <= first <= last < idx <= n,\n found ==> Self::is_prime(nums[first as int] as int),\n found ==> Self::is_prime(nums[last as int] as int),\n found ==> forall|k: int| 0 <= k < first as int ==> !Self::is_prime(#[trigger] nums[k] as int),\n found ==> forall|k: int| 0 <= k < idx as int ==> (k <= last as int || !Self::is_prime(#[trigger] nums[k] as int)),\n decreases n - idx,\n {\n if Self::is_prime_exec(nums[idx]) {\n if !found {\n first = idx;\n last = idx;\n found = true;\n } else {\n last = idx;\n }\n }\n idx = idx + 1;\n }\n\n assert(found) by {\n if !found {\n let witness = choose|i: int| 0 <= i < nums.len() && Self::is_prime(nums[i] as int);\n assert(0 <= witness < nums.len());\n assert(idx == n);\n assert(!Self::is_prime(nums[witness] as int));\n assert(Self::is_prime(nums[witness] as int));\n }\n };\n\n let mut result: i32 = 0;\n let mut t: usize = first;\n while t < last\n invariant\n 1 <= n <= 300000,\n n == nums.len(),\n found,\n 0 <= first <= last < n,\n Self::is_prime(nums[first as int] as int),\n Self::is_prime(nums[last as int] as int),\n forall|k: int| 0 <= k < first as int ==> !Self::is_prime(#[trigger] nums[k] as int),\n forall|k: int| 0 <= k < n as int ==> (k <= last as int || !Self::is_prime(#[trigger] nums[k] as int)),\n first <= t <= last,\n result >= 0,\n result as int == t as int - first as int,\n result as int <= 300000,\n decreases last - t,\n {\n t = t + 1;\n result = result + 1;\n }\n\n proof {\n assert(t == last);\n assert(result as int == last as int - first as int);\n\n assert(exists|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n && (j - i) == result) by {\n let i = first as int;\n let j = last as int;\n assert(0 <= i <= j < nums.len());\n assert(Self::is_prime(nums[i] as int));\n assert(Self::is_prime(nums[j] as int));\n assert(j - i == result as int);\n };\n\n assert forall|i: int, j: int|\n 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int)\n implies (j - i) <= result by {\n if 0 <= i <= j < nums.len()\n && Self::is_prime(nums[i] as int)\n && Self::is_prime(nums[j] as int) {\n assert(first as int <= i) by {\n if i < first as int {\n assert(!Self::is_prime(nums[i] as int));\n assert(Self::is_prime(nums[i] as int));\n }\n };\n assert(j <= last as int) by {\n if j > last as int {\n assert(!(j <= last as int));\n assert(j < n as int);\n assert(j <= last as int || !Self::is_prime(nums[j] as int));\n assert(!Self::is_prime(nums[j] as int));\n assert(Self::is_prime(nums[j] as int));\n }\n };\n assert(j - i <= last as int - first as int);\n assert(last as int - first as int == result as int);\n }\n };\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3115/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3115/tests/mutated_testcases.jsonl"} {"problem": "lc3122", "family": "leetcode", "description": "# Minimum Number of Operations to Satisfy Conditions\n\nYou are given a 2D matrix `grid` of size `m x n`. In one **operation**, you can change the value of **any** cell to **any** non-negative number. You need to perform some **operations** such that each cell `grid[i][j]` is:\n\n- Equal to the cell below it, i.e. `grid[i][j] == grid[i + 1][j]` (if it exists).\n- Different from the cell to its right, i.e. `grid[i][j] != grid[i][j + 1]` (if it exists).\n\nReturn the **minimum** number of operations needed.\n\n## Example 1:\n\n> **Input:** grid = [[1,0,2],[1,0,2]]\n> **Output:** 0\n\n**Explanation:**\n\nAll the cells in the matrix already satisfy the properties.\n\n## Example 2:\n\n> **Input:** grid = [[1,1,1],[0,0,0]]\n> **Output:** 3\n\n**Explanation:**\n\nThe matrix becomes `[[1,0,1],[1,0,1]]` which satisfies the properties, by doing these 3 operations:\n\n- Change `grid[1][0]` to 1.\n- Change `grid[0][1]` to 0.\n- Change `grid[1][2]` to 1.\n\n## Example 3:\n\n> **Input:** grid = [[1],[2],[3]]\n> **Output:** 2\n\n**Explanation:**\n\nThere is a single column. We can change the value to 1 in each cell using 2 operations.\n\n## Constraints:\n\n- `1 <= n, m <= 1000`\n- `0 <= grid[i][j] <= 9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_operations(grid: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming, Matrix\nAcceptance rate: 41.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn big_cost() -> int {\n 1_000_000_000int\n }\n\n pub open spec fn col_count_prefix(grid: Seq>, col: int, val: int, rows: int) -> int\n recommends\n grid.len() > 0,\n 0 <= rows <= grid.len(),\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::col_count_prefix(grid, col, val, rows - 1)\n + if grid[rows - 1][col] == val as i32 { 1int } else { 0int }\n }\n }\n\n pub open spec fn col_cost(grid: Seq>, col: int, val: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n {\n grid.len() as int - Self::col_count_prefix(grid, col, val, grid.len() as int)\n }\n\n pub open spec fn min_cost_prefix_last(grid: Seq>, cols: int, last: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= last < 10,\n decreases cols, 0int,\n {\n if cols <= 1 {\n Self::col_cost(grid, 0, last)\n } else {\n Self::col_cost(grid, cols - 1, last)\n + Self::min_prev_prefix(grid, cols - 1, last, 10)\n }\n }\n\n pub open spec fn min_prev_prefix(grid: Seq>, cols: int, last: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= last < 10,\n 0 <= upto <= 10,\n decreases cols, 1int, upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_prev_prefix(grid, cols, last, upto - 1);\n let cand = upto - 1;\n if cand == last {\n rest\n } else {\n let cur = Self::min_cost_prefix_last(grid, cols, cand);\n if cur <= rest { cur } else { rest }\n }\n }\n }\n\n pub open spec fn min_total_prefix(grid: Seq>, cols: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= upto <= 10,\n decreases upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_total_prefix(grid, cols, upto - 1);\n let cand = upto - 1;\n let cur = Self::min_cost_prefix_last(grid, cols, cand);\n if cur <= rest { cur } else { rest }\n }\n }\n\n pub open spec fn min_operations_spec(grid: Seq>) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n {\n Self::min_total_prefix(grid, grid[0].len() as int, 10)\n }\n\n pub fn minimum_operations(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 1000,\n 1 <= grid[0].len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n ensures\n result as int == Self::min_operations_spec(grid@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_operations(grid: Vec>) -> i32 {\n let m = grid.len();\n let n = grid[0].len();\n let m_i64 = m as i64;\n\n let mut dp_prev: Vec = Vec::new();\n let mut v: usize = 0;\n while v < 10 {\n let mut matches: i64 = 0;\n let mut i: usize = 0;\n while i < m {\n if grid[i][0] == v as i32 {\n matches = matches + 1;\n }\n i = i + 1;\n }\n let cost = m_i64 - matches;\n dp_prev.push(cost);\n v = v + 1;\n }\n\n let mut col: usize = 1;\n while col < n {\n let mut dp_cur: Vec = Vec::new();\n v = 0;\n while v < 10 {\n let mut matches: i64 = 0;\n let mut i: usize = 0;\n while i < m {\n if grid[i][col] == v as i32 {\n matches = matches + 1;\n }\n i = i + 1;\n }\n let cost = m_i64 - matches;\n\n let mut best: i64 = 1_000_000_000;\n let mut u: usize = 0;\n while u < 10 {\n if u != v && dp_prev[u] < best {\n best = dp_prev[u];\n }\n u = u + 1;\n }\n\n let value = cost + best;\n dp_cur.push(value);\n v = v + 1;\n }\n dp_prev = dp_cur;\n col = col + 1;\n }\n\n let mut answer: i64 = 1_000_000_000;\n v = 0;\n while v < 10 {\n if dp_prev[v] < answer {\n answer = dp_prev[v];\n }\n v = v + 1;\n }\n\n answer as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn big_cost() -> int {\n 1_000_000_000int\n }\n\n pub open spec fn col_count_prefix(grid: Seq>, col: int, val: int, rows: int) -> int\n recommends\n grid.len() > 0,\n 0 <= rows <= grid.len(),\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::col_count_prefix(grid, col, val, rows - 1)\n + if grid[rows - 1][col] == val as i32 { 1int } else { 0int }\n }\n }\n\n pub open spec fn col_cost(grid: Seq>, col: int, val: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n {\n grid.len() as int - Self::col_count_prefix(grid, col, val, grid.len() as int)\n }\n\n pub open spec fn min_cost_prefix_last(grid: Seq>, cols: int, last: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= last < 10,\n decreases cols, 0int,\n {\n if cols <= 1 {\n Self::col_cost(grid, 0, last)\n } else {\n Self::col_cost(grid, cols - 1, last)\n + Self::min_prev_prefix(grid, cols - 1, last, 10)\n }\n }\n\n pub open spec fn min_prev_prefix(grid: Seq>, cols: int, last: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= last < 10,\n 0 <= upto <= 10,\n decreases cols, 1int, upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_prev_prefix(grid, cols, last, upto - 1);\n let cand = upto - 1;\n if cand == last {\n rest\n } else {\n let cur = Self::min_cost_prefix_last(grid, cols, cand);\n if cur <= rest { cur } else { rest }\n }\n }\n }\n\n pub open spec fn min_total_prefix(grid: Seq>, cols: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= upto <= 10,\n decreases upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_total_prefix(grid, cols, upto - 1);\n let cand = upto - 1;\n let cur = Self::min_cost_prefix_last(grid, cols, cand);\n if cur <= rest { cur } else { rest }\n }\n }\n\n pub open spec fn min_operations_spec(grid: Seq>) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n {\n Self::min_total_prefix(grid, grid[0].len() as int, 10)\n }\n\n pub fn minimum_operations(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 1000,\n 1 <= grid[0].len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n ensures\n result as int == Self::min_operations_spec(grid@),\n {\n let m = grid.len();\n let n = grid[0].len();\n let m_i64 = m as i64;\n\n let mut dp_prev: Vec = Vec::new();\n let mut v: usize = 0;\n while v < 10 {\n let mut matches: i64 = 0;\n let mut i: usize = 0;\n while i < m {\n if grid[i][0] == v as i32 {\n matches = matches + 1;\n }\n i = i + 1;\n }\n let cost = m_i64 - matches;\n dp_prev.push(cost);\n v = v + 1;\n }\n\n let mut col: usize = 1;\n while col < n {\n let mut dp_cur: Vec = Vec::new();\n v = 0;\n while v < 10 {\n let mut matches: i64 = 0;\n let mut i: usize = 0;\n while i < m {\n if grid[i][col] == v as i32 {\n matches = matches + 1;\n }\n i = i + 1;\n }\n let cost = m_i64 - matches;\n\n let mut best: i64 = 1_000_000_000;\n let mut u: usize = 0;\n while u < 10 {\n if u != v && dp_prev[u] < best {\n best = dp_prev[u];\n }\n u = u + 1;\n }\n\n let value = cost + best;\n dp_cur.push(value);\n v = v + 1;\n }\n dp_prev = dp_cur;\n col = col + 1;\n }\n\n let mut answer: i64 = 1_000_000_000;\n v = 0;\n while v < 10 {\n if dp_prev[v] < answer {\n answer = dp_prev[v];\n }\n v = v + 1;\n }\n\n answer as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn big_cost() -> int {\n 1_000_000_000int\n }\n\n pub open spec fn col_count_prefix(grid: Seq>, col: int, val: int, rows: int) -> int\n recommends\n grid.len() > 0,\n 0 <= rows <= grid.len(),\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n decreases rows,\n {\n if rows <= 0 {\n 0\n } else {\n Self::col_count_prefix(grid, col, val, rows - 1)\n + if grid[rows - 1][col] == val as i32 { 1int } else { 0int }\n }\n }\n\n pub open spec fn col_cost(grid: Seq>, col: int, val: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n {\n grid.len() as int - Self::col_count_prefix(grid, col, val, grid.len() as int)\n }\n\n pub open spec fn min_cost_prefix_last(grid: Seq>, cols: int, last: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= last < 10,\n decreases cols, 0int,\n {\n if cols <= 1 {\n Self::col_cost(grid, 0, last)\n } else {\n Self::col_cost(grid, cols - 1, last)\n + Self::min_prev_prefix(grid, cols - 1, last, 10)\n }\n }\n\n pub open spec fn min_prev_prefix(grid: Seq>, cols: int, last: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= last < 10,\n 0 <= upto <= 10,\n decreases cols, 1int, upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_prev_prefix(grid, cols, last, upto - 1);\n let cand = upto - 1;\n if cand == last {\n rest\n } else {\n let cur = Self::min_cost_prefix_last(grid, cols, cand);\n if cur <= rest { cur } else { rest }\n }\n }\n }\n\n pub open spec fn min_total_prefix(grid: Seq>, cols: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 1 <= cols <= grid[0].len(),\n 0 <= upto <= 10,\n decreases upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_total_prefix(grid, cols, upto - 1);\n let cand = upto - 1;\n let cur = Self::min_cost_prefix_last(grid, cols, cand);\n if cur <= rest { cur } else { rest }\n }\n }\n\n pub open spec fn min_operations_spec(grid: Seq>) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n {\n Self::min_total_prefix(grid, grid[0].len() as int, 10)\n }\n\n pub open spec fn min_vec_prefix(dp: Seq, forbid: int, upto: int) -> int\n recommends\n dp.len() == 10,\n 0 <= forbid < 10,\n 0 <= upto <= 10,\n decreases upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_vec_prefix(dp, forbid, upto - 1);\n let cand = upto - 1;\n if cand == forbid {\n rest\n } else {\n let cur = dp[cand] as int;\n if cur <= rest { cur } else { rest }\n }\n }\n }\n\n pub open spec fn min_vec_all_prefix(dp: Seq, upto: int) -> int\n recommends\n dp.len() == 10,\n 0 <= upto <= 10,\n decreases upto,\n {\n if upto <= 0 {\n Self::big_cost()\n } else {\n let rest = Self::min_vec_all_prefix(dp, upto - 1);\n let cand = upto - 1;\n let cur = dp[cand] as int;\n if cur <= rest { cur } else { rest }\n }\n }\n\n proof fn lemma_col_count_prefix_bounds(grid: Seq>, col: int, val: int, rows: int)\n requires\n grid.len() > 0,\n 0 <= rows <= grid.len(),\n 0 <= col < grid[0].len(),\n 0 <= val < 10,\n ensures\n 0 <= Self::col_count_prefix(grid, col, val, rows) <= rows,\n decreases rows,\n {\n if rows > 0 {\n Self::lemma_col_count_prefix_bounds(grid, col, val, rows - 1);\n }\n }\n\n proof fn lemma_min_vec_prefix_eq_spec(dp: Seq, grid: Seq>, cols: int, forbid: int, upto: int)\n requires\n grid.len() > 0,\n dp.len() == 10,\n 1 <= cols <= grid[0].len(),\n 0 <= forbid < 10,\n 0 <= upto <= 10,\n forall |u: int| 0 <= u < 10 ==> dp[u] as int == #[trigger] Self::min_cost_prefix_last(grid, cols, u),\n ensures\n Self::min_vec_prefix(dp, forbid, upto) == Self::min_prev_prefix(grid, cols, forbid, upto),\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_min_vec_prefix_eq_spec(dp, grid, cols, forbid, upto - 1);\n let rest_vec = Self::min_vec_prefix(dp, forbid, upto - 1);\n let rest_spec = Self::min_prev_prefix(grid, cols, forbid, upto - 1);\n assert(rest_vec == rest_spec);\n let cand = upto - 1;\n if cand == forbid {\n assert(Self::min_vec_prefix(dp, forbid, upto) == rest_vec);\n assert(Self::min_prev_prefix(grid, cols, forbid, upto) == rest_spec);\n } else {\n assert(0 <= cand < 10);\n assert(dp[cand] as int == Self::min_cost_prefix_last(grid, cols, cand));\n assert(Self::min_vec_prefix(dp, forbid, upto) == if dp[cand] as int <= rest_vec { dp[cand] as int } else { rest_vec });\n assert(Self::min_prev_prefix(grid, cols, forbid, upto) ==\n if Self::min_cost_prefix_last(grid, cols, cand) <= rest_spec {\n Self::min_cost_prefix_last(grid, cols, cand)\n } else {\n rest_spec\n });\n }\n }\n }\n\n proof fn lemma_min_vec_prefix_nonneg(dp: Seq, forbid: int, upto: int)\n requires\n dp.len() == 10,\n 0 <= forbid < 10,\n 0 <= upto <= 10,\n forall |u: int| 0 <= u < 10 ==> 0 <= #[trigger] dp[u] as int,\n ensures\n 0 <= Self::min_vec_prefix(dp, forbid, upto),\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_min_vec_prefix_nonneg(dp, forbid, upto - 1);\n }\n }\n\n proof fn lemma_min_vec_prefix_le_entry(dp: Seq, forbid: int, upto: int, cand: int)\n requires\n dp.len() == 10,\n 0 <= forbid < 10,\n 0 < upto <= 10,\n 0 <= cand < upto,\n cand != forbid,\n ensures\n Self::min_vec_prefix(dp, forbid, upto) <= dp[cand] as int,\n decreases upto,\n {\n if upto > 1 {\n let last = upto - 1;\n if cand < last {\n Self::lemma_min_vec_prefix_le_entry(dp, forbid, upto - 1, cand);\n }\n }\n }\n\n proof fn lemma_min_vec_all_prefix_eq_spec(dp: Seq, grid: Seq>, cols: int, upto: int)\n requires\n grid.len() > 0,\n dp.len() == 10,\n 1 <= cols <= grid[0].len(),\n 0 <= upto <= 10,\n forall |u: int| 0 <= u < 10 ==> dp[u] as int == #[trigger] Self::min_cost_prefix_last(grid, cols, u),\n ensures\n Self::min_vec_all_prefix(dp, upto) == Self::min_total_prefix(grid, cols, upto),\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_min_vec_all_prefix_eq_spec(dp, grid, cols, upto - 1);\n }\n }\n\n proof fn lemma_min_vec_all_prefix_nonneg(dp: Seq, upto: int)\n requires\n dp.len() == 10,\n 0 <= upto <= 10,\n forall |u: int| 0 <= u < 10 ==> 0 <= #[trigger] dp[u] as int,\n ensures\n 0 <= Self::min_vec_all_prefix(dp, upto),\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_min_vec_all_prefix_nonneg(dp, upto - 1);\n }\n }\n\n proof fn lemma_min_vec_all_prefix_le_entry(dp: Seq, upto: int, cand: int)\n requires\n dp.len() == 10,\n 0 < upto <= 10,\n 0 <= cand < upto,\n ensures\n Self::min_vec_all_prefix(dp, upto) <= dp[cand] as int,\n decreases upto,\n {\n if upto > 1 {\n let last = upto - 1;\n if cand < last {\n Self::lemma_min_vec_all_prefix_le_entry(dp, upto - 1, cand);\n }\n }\n }\n\n pub fn minimum_operations(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 1000,\n 1 <= grid[0].len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n ensures\n result as int == Self::min_operations_spec(grid@),\n {\n let m = grid.len();\n let n = grid[0].len();\n let m_i64 = m as i64;\n\n let mut dp_prev: Vec = Vec::new();\n let mut v: usize = 0;\n while v < 10\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m_i64 == m as i64,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n 0 <= v <= 10,\n dp_prev.len() == v,\n forall |t: int| 0 <= t < dp_prev.len() ==>\n #[trigger] dp_prev[t] as int == Self::min_cost_prefix_last(grid@, 1, t),\n forall |t: int| 0 <= t < dp_prev.len() ==> 0 <= #[trigger] dp_prev[t] as int <= m as int,\n decreases 10 - v,\n {\n let mut matches: i64 = 0;\n let mut i: usize = 0;\n while i < m\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n 0 <= v < 10,\n 0 <= i <= m,\n 0 <= matches as int <= i as int,\n matches as int == Self::col_count_prefix(grid@, 0, v as int, i as int),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n decreases m - i,\n {\n proof {\n assert(i < grid.len());\n assert(grid[i as int].len() == grid[0].len());\n assert(0 < grid[i as int].len());\n }\n if grid[i][0] == v as i32 {\n matches = matches + 1;\n }\n i = i + 1;\n }\n let cost = m_i64 - matches;\n let ghost before = dp_prev@;\n dp_prev.push(cost);\n proof {\n assert(dp_prev@ == before.push(cost));\n assert(matches as int == Self::col_count_prefix(grid@, 0, v as int, m as int));\n Self::lemma_col_count_prefix_bounds(grid@, 0, v as int, m as int);\n assert(cost as int == Self::col_cost(grid@, 0, v as int));\n assert(Self::min_cost_prefix_last(grid@, 1, v as int) == Self::col_cost(grid@, 0, v as int));\n assert(0 <= cost as int <= m as int);\n }\n v = v + 1;\n }\n\n let mut col: usize = 1;\n while col < n\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m_i64 == m as i64,\n 1 <= col <= n,\n dp_prev.len() == 10,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n forall |t: int| 0 <= t < 10 ==> #[trigger] dp_prev[t] as int == Self::min_cost_prefix_last(grid@, col as int, t),\n forall |t: int| 0 <= t < 10 ==> 0 <= #[trigger] dp_prev[t] as int <= (col as int) * (m as int),\n decreases n - col,\n {\n let mut dp_cur: Vec = Vec::new();\n v = 0;\n while v < 10\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m_i64 == m as i64,\n 1 <= col < n,\n 0 <= v <= 10,\n dp_prev.len() == 10,\n dp_cur.len() == v,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n forall |t: int| 0 <= t < 10 ==> #[trigger] dp_prev[t] as int == Self::min_cost_prefix_last(grid@, col as int, t),\n forall |t: int| 0 <= t < 10 ==> 0 <= #[trigger] dp_prev[t] as int <= (col as int) * (m as int),\n forall |t: int| 0 <= t < dp_cur.len() ==> #[trigger] dp_cur[t] as int == Self::min_cost_prefix_last(grid@, col as int + 1, t),\n forall |t: int| 0 <= t < dp_cur.len() ==> 0 <= #[trigger] dp_cur[t] as int <= (col as int + 1) * (m as int),\n decreases 10 - v,\n {\n let mut matches: i64 = 0;\n let mut i: usize = 0;\n while i < m\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n 1 <= col < n,\n 0 <= v < 10,\n 0 <= i <= m,\n 0 <= matches as int <= i as int,\n matches as int == Self::col_count_prefix(grid@, col as int, v as int, i as int),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> 0 <= #[trigger] grid[r][c] <= 9,\n decreases m - i,\n {\n proof {\n assert(i < grid.len());\n assert(grid[i as int].len() == grid[0].len());\n assert(col < grid[i as int].len());\n }\n if grid[i][col] == v as i32 {\n matches = matches + 1;\n }\n i = i + 1;\n }\n let cost = m_i64 - matches;\n\n let mut best: i64 = 1_000_000_000;\n let mut u: usize = 0;\n while u < 10\n invariant\n 1 <= col < n,\n 1 <= m <= 1000,\n 0 <= v < 10,\n 0 <= u <= 10,\n dp_prev.len() == 10,\n best as int == Self::min_vec_prefix(dp_prev@, v as int, u as int),\n 0 <= best as int <= 1_000_000_000,\n forall |t: int| 0 <= t < 10 ==> 0 <= #[trigger] dp_prev[t] as int <= (col as int) * (m as int),\n decreases 10 - u,\n {\n if u != v && dp_prev[u] < best {\n best = dp_prev[u];\n }\n proof {\n if u as int != v as int {\n let old = Self::min_vec_prefix(dp_prev@, v as int, u as int);\n let cur = dp_prev@[u as int] as int;\n let newv = Self::min_vec_prefix(dp_prev@, v as int, u as int + 1);\n assert(newv == if cur <= old { cur } else { old });\n } else {\n assert(Self::min_vec_prefix(dp_prev@, v as int, u as int + 1)\n == Self::min_vec_prefix(dp_prev@, v as int, u as int));\n }\n }\n u = u + 1;\n }\n\n proof {\n Self::lemma_min_vec_prefix_nonneg(dp_prev@, v as int, 10);\n let alt: int = if v == 0 { 1 } else { 0 };\n assert(0 <= alt < 10);\n assert(alt != v as int);\n Self::lemma_min_vec_prefix_le_entry(dp_prev@, v as int, 10, alt);\n assert(best as int <= dp_prev[alt as int] as int);\n assert(dp_prev[alt as int] as int <= (col as int) * (m as int));\n assert(0 <= best as int <= (col as int) * (m as int));\n assert(0 <= cost as int <= m as int);\n assert(0 <= cost as int + best as int <= (col as int + 1) * (m as int)) by (nonlinear_arith)\n requires\n 0 <= cost as int,\n cost as int <= m as int,\n 0 <= best as int,\n best as int <= (col as int) * (m as int),\n 1 <= m as int,\n {};\n assert(0 <= best as int <= 1_000_000_000);\n assert(0 <= cost as int <= 1000);\n assert(cost as int + best as int <= 1_000_001_000) by (nonlinear_arith)\n requires\n 0 <= cost as int <= 1000,\n 0 <= best as int <= 1_000_000_000,\n {};\n assert(cost + best >= 0);\n assert(cost + best <= 1_000_001_000);\n }\n let value = cost + best;\n let ghost before = dp_cur@;\n dp_cur.push(value);\n proof {\n assert(matches as int == Self::col_count_prefix(grid@, col as int, v as int, m as int));\n Self::lemma_col_count_prefix_bounds(grid@, col as int, v as int, m as int);\n assert(cost as int == Self::col_cost(grid@, col as int, v as int));\n Self::lemma_min_vec_prefix_eq_spec(dp_prev@, grid@, col as int, v as int, 10);\n assert(best as int == Self::min_prev_prefix(grid@, col as int, v as int, 10));\n assert(value as int == Self::min_cost_prefix_last(grid@, col as int + 1, v as int));\n assert(dp_cur@ == before.push(value));\n assert(0 <= value as int <= (col as int + 1) * (m as int));\n }\n v = v + 1;\n }\n dp_prev = dp_cur;\n col = col + 1;\n }\n\n let mut answer: i64 = 1_000_000_000;\n v = 0;\n while v < 10\n invariant\n m == grid.len(),\n n == grid[0].len(),\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n col == n,\n dp_prev.len() == 10,\n 0 <= v <= 10,\n answer as int == Self::min_vec_all_prefix(dp_prev@, v as int),\n forall |t: int| 0 <= t < 10 ==> #[trigger] dp_prev[t] as int == Self::min_cost_prefix_last(grid@, n as int, t),\n forall |t: int| 0 <= t < 10 ==> 0 <= #[trigger] dp_prev[t] as int <= (n as int) * (m as int),\n decreases 10 - v,\n {\n if dp_prev[v] < answer {\n answer = dp_prev[v];\n }\n proof {\n let old = Self::min_vec_all_prefix(dp_prev@, v as int);\n let cur = dp_prev@[v as int] as int;\n assert(Self::min_vec_all_prefix(dp_prev@, v as int + 1)\n == if cur <= old { cur } else { old });\n }\n v = v + 1;\n }\n\n proof {\n Self::lemma_min_vec_all_prefix_eq_spec(dp_prev@, grid@, n as int, 10);\n Self::lemma_min_vec_all_prefix_nonneg(dp_prev@, 10);\n Self::lemma_min_vec_all_prefix_le_entry(dp_prev@, 10, 0);\n assert(answer as int == Self::min_total_prefix(grid@, n as int, 10));\n assert(answer as int == Self::min_operations_spec(grid@));\n assert(0 <= answer as int);\n assert(answer as int <= dp_prev[0] as int);\n assert(dp_prev[0] as int <= (n as int) * (m as int));\n assert((n as int) * (m as int) <= 1_000_000) by (nonlinear_arith)\n requires\n 1 <= n as int <= 1000,\n 1 <= m as int <= 1000,\n {};\n assert(answer as int <= 1_000_000);\n assert(answer as int <= i32::MAX);\n assert((answer as i32) as int == answer as int);\n }\n\n answer as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3122/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3122/tests/mutated_testcases.jsonl"} {"problem": "lc3128", "family": "leetcode", "description": "# Right Triangles\n\nYou are given a 2D boolean matrix `grid`.\n\nA collection of 3 elements of `grid` is a **right triangle** if one element is in the **same row** as another element and in the **same column** as the third element. The 3 elements do **not** need to be adjacent.\n\nReturn the number of right triangles formed by 3 elements of `grid` such that all 3 elements have value `1`.\n\n## Example 1\n\n> **Input:** `grid = [[0,1,0],[0,1,1],[0,1,0]]`\n> **Output:** `2`\n\n## Example 2\n\n> **Input:** `grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]`\n> **Output:** `0`\n\n## Example 3\n\n> **Input:** `grid = [[1,0,1],[1,0,0],[1,0,0]]`\n> **Output:** `2`\n\n## Constraints\n\n- `1 <= grid.length <= 1000`\n- `1 <= grid[i].length <= 1000`\n- `0 <= grid[i][j] <= 1`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn number_of_right_triangles(grid: Vec>) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Hash Table, Math, Combinatorics, Counting\nAcceptance rate: 48.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn row_ones_prefix(grid: Seq>, row: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[row].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::row_ones_prefix(grid, row, upto - 1)\n + if grid[row][upto - 1] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn row_ones(grid: Seq>, row: int) -> int\n recommends\n grid.len() > 0,\n 0 <= row < grid.len(),\n {\n Self::row_ones_prefix(grid, row, grid[row].len() as int)\n }\n\n pub open spec fn col_ones_prefix(grid: Seq>, col: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= upto <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::col_ones_prefix(grid, col, upto - 1)\n + if grid[upto - 1][col] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn col_ones(grid: Seq>, col: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n Self::col_ones_prefix(grid, col, grid.len() as int)\n }\n\n pub open spec fn triangle_at(grid: Seq>, row: int, col: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= row < grid.len(),\n 0 <= col < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n if grid[row][col] == 1 {\n (Self::row_ones(grid, row) - 1) * (Self::col_ones(grid, col) - 1)\n } else {\n 0\n }\n }\n\n pub open spec fn row_triangle_sum_prefix(grid: Seq>, row: int, upto: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::row_triangle_sum_prefix(grid, row, upto - 1)\n + Self::triangle_at(grid, row, upto - 1)\n }\n }\n\n pub open spec fn total_triangle_sum_prefix(grid: Seq>, upto_rows: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= upto_rows <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto_rows,\n {\n if upto_rows <= 0 {\n 0\n } else {\n Self::total_triangle_sum_prefix(grid, upto_rows - 1)\n + Self::row_triangle_sum_prefix(grid, upto_rows - 1, grid[0].len() as int)\n }\n }\n\n pub open spec fn right_triangles_spec(grid: Seq>) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n Self::total_triangle_sum_prefix(grid, grid.len() as int)\n }\n\n pub fn number_of_right_triangles(grid: Vec>) -> (result: i64)\n requires\n 1 <= grid.len() <= 1000,\n 1 <= grid[0].len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n ensures\n result as int == Self::right_triangles_spec(grid@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn number_of_right_triangles(grid: Vec>) -> i64 {\n let m = grid.len();\n let n = grid[0].len();\n\n let mut row_counts: Vec = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row_sum: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n if grid[i][j] == 1 {\n row_sum = row_sum + 1;\n }\n j = j + 1;\n }\n row_counts.push(row_sum);\n i = i + 1;\n }\n\n let mut col_counts: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let mut col_sum: i64 = 0;\n i = 0;\n while i < m {\n if grid[i][j] == 1 {\n col_sum = col_sum + 1;\n }\n i = i + 1;\n }\n col_counts.push(col_sum);\n j = j + 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < m {\n j = 0;\n while j < n {\n if grid[i][j] == 1 {\n let add = (row_counts[i] - 1) * (col_counts[j] - 1);\n ans = ans + add;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn row_ones_prefix(grid: Seq>, row: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[row].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::row_ones_prefix(grid, row, upto - 1)\n + if grid[row][upto - 1] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn row_ones(grid: Seq>, row: int) -> int\n recommends\n grid.len() > 0,\n 0 <= row < grid.len(),\n {\n Self::row_ones_prefix(grid, row, grid[row].len() as int)\n }\n\n pub open spec fn col_ones_prefix(grid: Seq>, col: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= upto <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::col_ones_prefix(grid, col, upto - 1)\n + if grid[upto - 1][col] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn col_ones(grid: Seq>, col: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n Self::col_ones_prefix(grid, col, grid.len() as int)\n }\n\n pub open spec fn triangle_at(grid: Seq>, row: int, col: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= row < grid.len(),\n 0 <= col < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n if grid[row][col] == 1 {\n (Self::row_ones(grid, row) - 1) * (Self::col_ones(grid, col) - 1)\n } else {\n 0\n }\n }\n\n pub open spec fn row_triangle_sum_prefix(grid: Seq>, row: int, upto: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::row_triangle_sum_prefix(grid, row, upto - 1)\n + Self::triangle_at(grid, row, upto - 1)\n }\n }\n\n pub open spec fn total_triangle_sum_prefix(grid: Seq>, upto_rows: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= upto_rows <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto_rows,\n {\n if upto_rows <= 0 {\n 0\n } else {\n Self::total_triangle_sum_prefix(grid, upto_rows - 1)\n + Self::row_triangle_sum_prefix(grid, upto_rows - 1, grid[0].len() as int)\n }\n }\n\n pub open spec fn right_triangles_spec(grid: Seq>) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n Self::total_triangle_sum_prefix(grid, grid.len() as int)\n }\n\n pub fn number_of_right_triangles(grid: Vec>) -> (result: i64)\n requires\n 1 <= grid.len() <= 1000,\n 1 <= grid[0].len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n ensures\n result as int == Self::right_triangles_spec(grid@),\n {\n let m = grid.len();\n let n = grid[0].len();\n\n let mut row_counts: Vec = Vec::new();\n let mut i: usize = 0;\n while i < m {\n let mut row_sum: i64 = 0;\n let mut j: usize = 0;\n while j < n {\n if grid[i][j] == 1 {\n row_sum = row_sum + 1;\n }\n j = j + 1;\n }\n row_counts.push(row_sum);\n i = i + 1;\n }\n\n let mut col_counts: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n {\n let mut col_sum: i64 = 0;\n i = 0;\n while i < m {\n if grid[i][j] == 1 {\n col_sum = col_sum + 1;\n }\n i = i + 1;\n }\n col_counts.push(col_sum);\n j = j + 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < m {\n j = 0;\n while j < n {\n if grid[i][j] == 1 {\n let add = (row_counts[i] - 1) * (col_counts[j] - 1);\n ans = ans + add;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn row_ones_prefix(grid: Seq>, row: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[row].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::row_ones_prefix(grid, row, upto - 1)\n + if grid[row][upto - 1] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn row_ones(grid: Seq>, row: int) -> int\n recommends\n grid.len() > 0,\n 0 <= row < grid.len(),\n {\n Self::row_ones_prefix(grid, row, grid[row].len() as int)\n }\n\n pub open spec fn col_ones_prefix(grid: Seq>, col: int, upto: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= upto <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::col_ones_prefix(grid, col, upto - 1)\n + if grid[upto - 1][col] == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn col_ones(grid: Seq>, col: int) -> int\n recommends\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n Self::col_ones_prefix(grid, col, grid.len() as int)\n }\n\n pub open spec fn triangle_at(grid: Seq>, row: int, col: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= row < grid.len(),\n 0 <= col < grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n if grid[row][col] == 1 {\n (Self::row_ones(grid, row) - 1) * (Self::col_ones(grid, col) - 1)\n } else {\n 0\n }\n }\n\n pub open spec fn row_triangle_sum_prefix(grid: Seq>, row: int, upto: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[0].len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else {\n Self::row_triangle_sum_prefix(grid, row, upto - 1)\n + Self::triangle_at(grid, row, upto - 1)\n }\n }\n\n pub open spec fn total_triangle_sum_prefix(grid: Seq>, upto_rows: int) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n 0 <= upto_rows <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n decreases upto_rows,\n {\n if upto_rows <= 0 {\n 0\n } else {\n Self::total_triangle_sum_prefix(grid, upto_rows - 1)\n + Self::row_triangle_sum_prefix(grid, upto_rows - 1, grid[0].len() as int)\n }\n }\n\n pub open spec fn right_triangles_spec(grid: Seq>) -> int\n recommends\n grid.len() > 0,\n grid[0].len() > 0,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n {\n Self::total_triangle_sum_prefix(grid, grid.len() as int)\n }\n\n proof fn lemma_row_ones_prefix_bounds(grid: Seq>, row: int, upto: int)\n requires\n grid.len() > 0,\n 0 <= row < grid.len(),\n 0 <= upto <= grid[row].len(),\n forall |c: int| 0 <= c < grid[row].len() ==> {\n let v = #[trigger] grid[row][c];\n v == 0 || v == 1\n },\n ensures\n 0 <= Self::row_ones_prefix(grid, row, upto) <= upto,\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_row_ones_prefix_bounds(grid, row, upto - 1);\n assert(grid[row][upto - 1] == 0 || grid[row][upto - 1] == 1);\n }\n }\n\n proof fn lemma_col_ones_prefix_bounds(grid: Seq>, col: int, upto: int)\n requires\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= upto <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n ensures\n 0 <= Self::col_ones_prefix(grid, col, upto) <= upto,\n decreases upto,\n {\n if upto > 0 {\n Self::lemma_col_ones_prefix_bounds(grid, col, upto - 1);\n assert(grid[upto - 1][col] == 0 || grid[upto - 1][col] == 1);\n }\n }\n\n proof fn lemma_row_ones_ge_one_at(grid: Seq>, row: int, col: int, upto: int)\n requires\n grid.len() > 0,\n 0 <= row < grid.len(),\n 0 <= col < upto <= grid[row].len(),\n grid[row][col] == 1,\n forall |c: int| 0 <= c < grid[row].len() ==> {\n let v = #[trigger] grid[row][c];\n v == 0 || v == 1\n },\n ensures\n Self::row_ones_prefix(grid, row, upto) >= 1,\n decreases upto,\n {\n if upto - 1 == col {\n Self::lemma_row_ones_prefix_bounds(grid, row, upto - 1);\n assert(grid[row][upto - 1] == 1);\n assert(Self::row_ones_prefix(grid, row, upto)\n == Self::row_ones_prefix(grid, row, upto - 1) + 1);\n } else {\n assert(col < upto - 1);\n Self::lemma_row_ones_ge_one_at(grid, row, col, upto - 1);\n assert(Self::row_ones_prefix(grid, row, upto)\n == Self::row_ones_prefix(grid, row, upto - 1)\n + if grid[row][upto - 1] == 1 { 1int } else { 0int });\n assert(0 <= if grid[row][upto - 1] == 1 { 1int } else { 0int });\n }\n }\n\n proof fn lemma_col_ones_ge_one_at(grid: Seq>, col: int, row: int, upto: int)\n requires\n grid.len() > 0,\n 0 <= col < grid[0].len(),\n 0 <= row < upto <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n grid[row][col] == 1,\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n ensures\n Self::col_ones_prefix(grid, col, upto) >= 1,\n decreases upto,\n {\n if upto - 1 == row {\n Self::lemma_col_ones_prefix_bounds(grid, col, upto - 1);\n assert(grid[upto - 1][col] == 1);\n assert(Self::col_ones_prefix(grid, col, upto)\n == Self::col_ones_prefix(grid, col, upto - 1) + 1);\n } else {\n assert(row < upto - 1);\n Self::lemma_col_ones_ge_one_at(grid, col, row, upto - 1);\n assert(Self::col_ones_prefix(grid, col, upto)\n == Self::col_ones_prefix(grid, col, upto - 1)\n + if grid[upto - 1][col] == 1 { 1int } else { 0int });\n assert(0 <= if grid[upto - 1][col] == 1 { 1int } else { 0int });\n }\n }\n\n pub fn number_of_right_triangles(grid: Vec>) -> (result: i64)\n requires\n 1 <= grid.len() <= 1000,\n 1 <= grid[0].len() <= 1000,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall |r: int, c: int| 0 <= r < grid.len() && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n ensures\n result as int == Self::right_triangles_spec(grid@),\n {\n let m = grid.len();\n let n = grid[0].len();\n\n let mut row_counts: Vec = Vec::new();\n let mut i: usize = 0;\n while i < m\n invariant\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m == grid.len(),\n n == grid[0].len(),\n forall |r: int| 0 <= r < m as int ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < m as int && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n 0 <= i <= m,\n row_counts.len() == i,\n forall |r: int| 0 <= r < i as int ==> #[trigger] row_counts[r] as int == Self::row_ones(grid@, r),\n forall |r: int| 0 <= r < i as int ==> 0 <= #[trigger] row_counts[r] <= n as i64,\n decreases m - i,\n {\n let mut row_sum: i64 = 0;\n let mut j: usize = 0;\n while j < n\n invariant\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m == grid.len(),\n n == grid[0].len(),\n 0 <= i < m,\n forall |r: int| 0 <= r < m as int ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < m as int && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n 0 <= j <= n,\n grid[i as int].len() == n,\n row_sum as int == Self::row_ones_prefix(grid@, i as int, j as int),\n 0 <= row_sum <= j as i64,\n decreases n - j,\n {\n if grid[i][j] == 1 {\n row_sum = row_sum + 1;\n }\n proof {\n assert(Self::row_ones_prefix(grid@, i as int, j as int + 1)\n == Self::row_ones_prefix(grid@, i as int, j as int)\n + if grid[i as int][j as int] == 1 { 1int } else { 0int });\n }\n j = j + 1;\n }\n proof {\n assert(j == n);\n assert(row_sum as int == Self::row_ones(grid@, i as int));\n Self::lemma_row_ones_prefix_bounds(grid@, i as int, n as int);\n assert(0 <= row_sum <= n as i64);\n }\n let ghost old_row_counts = row_counts@;\n let ghost old_i = i as int;\n row_counts.push(row_sum);\n proof {\n assert(row_counts@ == old_row_counts.push(row_sum));\n assert(forall |r: int| 0 <= r < old_i ==> #[trigger] row_counts[r] as int == Self::row_ones(grid@, r));\n assert(row_counts[old_i] as int == Self::row_ones(grid@, old_i));\n assert forall |r: int| 0 <= r < old_i + 1 implies #[trigger] row_counts[r] as int == Self::row_ones(grid@, r) by {\n if r < old_i {\n assert(row_counts[r] == old_row_counts[r]);\n } else {\n assert(r == old_i);\n }\n };\n assert forall |r: int| 0 <= r < old_i + 1 implies 0 <= #[trigger] row_counts[r] <= n as i64 by {\n if r < old_i {\n assert(row_counts[r] == old_row_counts[r]);\n } else {\n assert(r == old_i);\n assert(0 <= row_counts[r] <= n as i64);\n }\n };\n }\n i = i + 1;\n }\n\n let mut col_counts: Vec = Vec::new();\n let mut j: usize = 0;\n while j < n\n invariant\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m == grid.len(),\n n == grid[0].len(),\n forall |r: int| 0 <= r < m as int ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < m as int && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n row_counts.len() == m,\n forall |r: int| 0 <= r < m as int ==> #[trigger] row_counts[r] as int == Self::row_ones(grid@, r),\n forall |r: int| 0 <= r < m as int ==> 0 <= #[trigger] row_counts[r] <= n as i64,\n 0 <= j <= n,\n col_counts.len() == j,\n forall |c: int| 0 <= c < j as int ==> #[trigger] col_counts[c] as int == Self::col_ones(grid@, c),\n forall |c: int| 0 <= c < j as int ==> 0 <= #[trigger] col_counts[c] <= m as i64,\n decreases n - j,\n {\n let mut col_sum: i64 = 0;\n i = 0;\n while i < m\n invariant\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m == grid.len(),\n n == grid[0].len(),\n 0 <= j < n,\n forall |r: int| 0 <= r < m as int ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < m as int && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n 0 <= i <= m,\n i < m ==> grid[i as int].len() == n,\n col_sum as int == Self::col_ones_prefix(grid@, j as int, i as int),\n 0 <= col_sum <= i as i64,\n decreases m - i,\n {\n if grid[i][j] == 1 {\n col_sum = col_sum + 1;\n }\n proof {\n assert(Self::col_ones_prefix(grid@, j as int, i as int + 1)\n == Self::col_ones_prefix(grid@, j as int, i as int)\n + if grid[i as int][j as int] == 1 { 1int } else { 0int });\n }\n i = i + 1;\n }\n proof {\n assert(i == m);\n assert(col_sum as int == Self::col_ones(grid@, j as int));\n Self::lemma_col_ones_prefix_bounds(grid@, j as int, m as int);\n assert(0 <= col_sum <= m as i64);\n }\n let ghost old_col_counts = col_counts@;\n let ghost old_j = j as int;\n col_counts.push(col_sum);\n proof {\n assert(col_counts@ == old_col_counts.push(col_sum));\n assert(forall |c: int| 0 <= c < old_j ==> #[trigger] col_counts[c] as int == Self::col_ones(grid@, c));\n assert(col_counts[old_j] as int == Self::col_ones(grid@, old_j));\n assert forall |c: int| 0 <= c < old_j + 1 implies #[trigger] col_counts[c] as int == Self::col_ones(grid@, c) by {\n if c < old_j {\n assert(col_counts[c] == old_col_counts[c]);\n } else {\n assert(c == old_j);\n }\n };\n assert forall |c: int| 0 <= c < old_j + 1 implies 0 <= #[trigger] col_counts[c] <= m as i64 by {\n if c < old_j {\n assert(col_counts[c] == old_col_counts[c]);\n } else {\n assert(c == old_j);\n assert(0 <= col_counts[c] <= m as i64);\n }\n };\n }\n j = j + 1;\n }\n\n let mut ans: i64 = 0;\n i = 0;\n while i < m\n invariant\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m == grid.len(),\n n == grid[0].len(),\n forall |r: int| 0 <= r < m as int ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < m as int && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n row_counts.len() == m,\n forall |r: int| 0 <= r < m as int ==> #[trigger] row_counts[r] as int == Self::row_ones(grid@, r),\n forall |r: int| 0 <= r < m as int ==> 0 <= #[trigger] row_counts[r] <= n as i64,\n col_counts.len() == n,\n forall |c: int| 0 <= c < n as int ==> #[trigger] col_counts[c] as int == Self::col_ones(grid@, c),\n forall |c: int| 0 <= c < n as int ==> 0 <= #[trigger] col_counts[c] <= m as i64,\n 0 <= i <= m,\n ans as int == Self::total_triangle_sum_prefix(grid@, i as int),\n 0 <= ans as int <= i as int * n as int * 1_000_000,\n decreases m - i,\n {\n j = 0;\n while j < n\n invariant\n 1 <= m <= 1000,\n 1 <= n <= 1000,\n m == grid.len(),\n n == grid[0].len(),\n forall |r: int| 0 <= r < m as int ==> #[trigger] grid[r].len() == n,\n forall |r: int, c: int| 0 <= r < m as int && 0 <= c < grid[r].len() ==> {\n let v = #[trigger] grid[r][c];\n v == 0 || v == 1\n },\n row_counts.len() == m,\n forall |r: int| 0 <= r < m as int ==> #[trigger] row_counts[r] as int == Self::row_ones(grid@, r),\n forall |r: int| 0 <= r < m as int ==> 0 <= #[trigger] row_counts[r] <= n as i64,\n col_counts.len() == n,\n forall |c: int| 0 <= c < n as int ==> #[trigger] col_counts[c] as int == Self::col_ones(grid@, c),\n forall |c: int| 0 <= c < n as int ==> 0 <= #[trigger] col_counts[c] <= m as i64,\n 0 <= i < m,\n 0 <= j <= n,\n grid[i as int].len() == n,\n ans as int == Self::total_triangle_sum_prefix(grid@, i as int)\n + Self::row_triangle_sum_prefix(grid@, i as int, j as int),\n 0 <= ans as int <= (i as int * n as int + j as int) * 1_000_000,\n decreases n - j,\n {\n let ghost old_ans = ans as int;\n if grid[i][j] == 1 {\n proof {\n assert(grid[i as int][j as int] == 1);\n assert(forall |c: int| 0 <= c < grid[i as int].len() ==> {\n let v = #[trigger] grid[i as int][c];\n v == 0 || v == 1\n });\n Self::lemma_row_ones_ge_one_at(grid@, i as int, j as int, n as int);\n Self::lemma_col_ones_ge_one_at(grid@, j as int, i as int, m as int);\n assert(Self::row_ones(grid@, i as int) == Self::row_ones_prefix(grid@, i as int, n as int));\n assert(Self::col_ones(grid@, j as int) == Self::col_ones_prefix(grid@, j as int, m as int));\n assert(1 <= Self::row_ones(grid@, i as int));\n assert(1 <= Self::col_ones(grid@, j as int));\n assert(0 <= row_counts[i as int] <= n as i64);\n assert(0 <= col_counts[j as int] <= m as i64);\n assert(1 <= row_counts[i as int] as int);\n assert(1 <= col_counts[j as int] as int);\n assert(0 <= row_counts[i as int] - 1 <= 999);\n assert(0 <= col_counts[j as int] - 1 <= 999);\n assert((row_counts[i as int] - 1) * (col_counts[j as int] - 1) <= 998001)\n by (nonlinear_arith)\n requires\n 0 <= row_counts[i as int] - 1 <= 999,\n 0 <= col_counts[j as int] - 1 <= 999;\n assert(0 <= (row_counts[i as int] - 1) * (col_counts[j as int] - 1))\n by (nonlinear_arith)\n requires\n 0 <= row_counts[i as int] - 1 <= 999,\n 0 <= col_counts[j as int] - 1 <= 999;\n }\n let add = (row_counts[i] - 1) * (col_counts[j] - 1);\n proof {\n assert(row_counts[i as int] as int == Self::row_ones(grid@, i as int));\n assert(col_counts[j as int] as int == Self::col_ones(grid@, j as int));\n assert(add as int == (row_counts[i as int] as int - 1) * (col_counts[j as int] as int - 1));\n assert(add as int == Self::triangle_at(grid@, i as int, j as int));\n assert(0 <= add <= 1_000_000);\n assert(0 <= old_ans <= (i as int * n as int + j as int) * 1_000_000);\n assert(old_ans + add as int <= (i as int * n as int + j as int) * 1_000_000 + 1_000_000);\n assert((i as int * n as int + j as int) * 1_000_000 + 1_000_000\n == (i as int * n as int + j as int + 1) * 1_000_000);\n assert(old_ans + add as int <= (i as int * n as int + j as int + 1) * 1_000_000);\n let ii: int = i as int;\n let jj: int = j as int;\n let nn: int = n as int;\n assert(0 <= ii < m as int);\n assert(0 <= jj < nn);\n assert(nn <= 1000);\n assert(m as int <= 1000);\n assert(ii < 1000);\n assert(jj < 1000);\n assert(ii * nn + jj + 1 <= 1_000_000)\n by (nonlinear_arith)\n requires\n 0 <= ii,\n ii < 1000,\n 0 <= jj,\n jj < 1000,\n 1 <= nn,\n nn <= 1000;\n assert((i as int * n as int + j as int + 1) * 1_000_000 <= 1_000_000_000_000);\n assert(1_000 * 1_000 * 1_000_000 < 9_223_372_036_854_775_807);\n assert((old_ans + add as int) < 9_223_372_036_854_775_807);\n }\n ans = ans + add;\n proof {\n assert(ans as int == old_ans + add as int);\n assert(0 <= ans as int <= (i as int * n as int + j as int + 1) * 1_000_000);\n }\n } else {\n proof {\n assert(0 <= old_ans <= (i as int * n as int + j as int) * 1_000_000);\n assert((i as int * n as int + j as int) * 1_000_000\n <= (i as int * n as int + j as int + 1) * 1_000_000);\n assert(ans as int == old_ans);\n assert(0 <= ans as int <= (i as int * n as int + j as int + 1) * 1_000_000);\n }\n }\n proof {\n if grid[i as int][j as int] == 1 {\n assert(Self::triangle_at(grid@, i as int, j as int)\n == (Self::row_ones(grid@, i as int) - 1) * (Self::col_ones(grid@, j as int) - 1));\n } else {\n assert(Self::triangle_at(grid@, i as int, j as int) == 0);\n }\n assert(Self::row_triangle_sum_prefix(grid@, i as int, j as int + 1)\n == Self::row_triangle_sum_prefix(grid@, i as int, j as int)\n + Self::triangle_at(grid@, i as int, j as int));\n assert(Self::total_triangle_sum_prefix(grid@, i as int)\n + Self::row_triangle_sum_prefix(grid@, i as int, j as int + 1)\n == Self::total_triangle_sum_prefix(grid@, i as int)\n + Self::row_triangle_sum_prefix(grid@, i as int, j as int)\n + Self::triangle_at(grid@, i as int, j as int));\n }\n j = j + 1;\n }\n proof {\n assert(j == n);\n assert(ans as int == Self::total_triangle_sum_prefix(grid@, i as int)\n + Self::row_triangle_sum_prefix(grid@, i as int, n as int));\n assert(Self::row_triangle_sum_prefix(grid@, i as int, n as int)\n == Self::row_triangle_sum_prefix(grid@, i as int, grid[0].len() as int));\n assert(Self::total_triangle_sum_prefix(grid@, i as int + 1)\n == Self::total_triangle_sum_prefix(grid@, i as int)\n + Self::row_triangle_sum_prefix(grid@, i as int, grid[0].len() as int));\n assert(0 <= ans as int <= (i as int * n as int + n as int) * 1_000_000);\n let ii: int = i as int;\n let nn: int = n as int;\n assert(ii * nn + nn <= (ii + 1) * nn)\n by (nonlinear_arith)\n requires\n 0 <= ii,\n 1 <= nn;\n assert((i as int * n as int + n as int) * 1_000_000 <= (i as int + 1) * n as int * 1_000_000);\n }\n i = i + 1;\n }\n\n proof {\n assert(i == m);\n assert(ans as int == Self::total_triangle_sum_prefix(grid@, m as int));\n assert(Self::right_triangles_spec(grid@) == Self::total_triangle_sum_prefix(grid@, grid.len() as int));\n assert(grid.len() == m);\n }\n\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3128/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3128/tests/mutated_testcases.jsonl"} {"problem": "lc3132", "family": "leetcode", "description": "# Find the Integer Added to Array II\n\nYou are given two integer arrays `nums1` and `nums2`.\n\nFrom `nums1` two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable `x`.\n\nAs a result, `nums1` becomes **equal** to `nums2`. Two arrays are considered **equal** when they contain the same integers with the same frequencies.\n\nReturn the **minimum** possible integer* *`x`* *that achieves this equivalence.\n\n \n\nExample 1:\n\n**Input:** nums1 = [4,20,16,12,8], nums2 = [14,18,10]\n\n**Output:** -2\n\n**Explanation:**\n\nAfter removing elements at indices `[0,4]` and adding -2, `nums1` becomes `[18,14,10]`.\n\nExample 2:\n\n**Input:** nums1 = [3,5,5,3], nums2 = [7,7]\n\n**Output:** 2\n\n**Explanation:**\n\nAfter removing elements at indices `[0,3]` and adding 2, `nums1` becomes `[7,7]`.\n\n \n\n**Constraints:**\n\n\t- `3 <= nums1.length <= 200`\n\n\t- `nums2.length == nums1.length - 2`\n\n\t- `0 <= nums1[i], nums2[i] <= 1000`\n\n\t- The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by removing two elements and adding `x` to each element of `nums1`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_added_integer(nums1: Vec, nums2: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Two Pointers, Sorting, Enumeration\nAcceptance rate: 32.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_plain_prefix(nums: Seq, end: int, v: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::count_plain_prefix(nums, end - 1, v) + if nums[end - 1] as int == v { 1int } else { 0int }\n }\n }\n\n pub open spec fn count_shift_prefix(nums: Seq, end: int, x: int, v: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::count_shift_prefix(nums, end - 1, x, v) + if nums[end - 1] as int + x == v { 1int } else { 0int }\n }\n }\n\n pub open spec fn valid_x_spec(nums1: Seq, nums2: Seq, x: int) -> bool {\n forall |v: int| 0 <= v <= 1000 ==> Self::count_shift_prefix(nums1, nums1.len() as int, x, v) >= Self::count_plain_prefix(nums2, nums2.len() as int, v)\n }\n\n pub fn minimum_added_integer(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 3 <= nums1.len() <= 200,\n nums2.len() + 2 == nums1.len(),\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1000,\n forall |i: int| 0 <= i < nums2.len() ==> 0 <= #[trigger] nums2[i] <= 1000,\n exists |x: int| -1000 <= x <= 1000 && Self::valid_x_spec(nums1@, nums2@, x),\n ensures\n -1000 <= result <= 1000,\n Self::valid_x_spec(nums1@, nums2@, result as int),\n forall |x: int| -1000 <= x < result ==> !Self::valid_x_spec(nums1@, nums2@, x),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_added_integer(nums1: Vec, nums2: Vec) -> i32 {\n let n1 = nums1.len();\n let n2 = nums2.len();\n let mut x = -1000;\n while x <= 1000 {\n let mut ok = true;\n let mut v = 0;\n let mut fail_v = -1;\n while v <= 1000 && ok {\n let mut c1 = 0;\n let mut i: usize = 0;\n while i < n1 {\n if nums1[i] as i64 + x as i64 == v as i64 {\n c1 += 1;\n }\n i += 1;\n }\n let mut c2 = 0;\n let mut j: usize = 0;\n while j < n2 {\n if nums2[j] as i64 == v as i64 {\n c2 += 1;\n }\n j += 1;\n }\n if c1 < c2 {\n ok = false;\n fail_v = v;\n }\n v += 1;\n }\n if ok {\n return x;\n }\n let _ = fail_v;\n x += 1;\n }\n 0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_plain_prefix(nums: Seq, end: int, v: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::count_plain_prefix(nums, end - 1, v) + if nums[end - 1] as int == v { 1int } else { 0int }\n }\n }\n\n pub open spec fn count_shift_prefix(nums: Seq, end: int, x: int, v: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::count_shift_prefix(nums, end - 1, x, v) + if nums[end - 1] as int + x == v { 1int } else { 0int }\n }\n }\n\n pub open spec fn valid_x_spec(nums1: Seq, nums2: Seq, x: int) -> bool {\n forall |v: int| 0 <= v <= 1000 ==> Self::count_shift_prefix(nums1, nums1.len() as int, x, v) >= Self::count_plain_prefix(nums2, nums2.len() as int, v)\n }\n\n pub fn minimum_added_integer(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 3 <= nums1.len() <= 200,\n nums2.len() + 2 == nums1.len(),\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1000,\n forall |i: int| 0 <= i < nums2.len() ==> 0 <= #[trigger] nums2[i] <= 1000,\n exists |x: int| -1000 <= x <= 1000 && Self::valid_x_spec(nums1@, nums2@, x),\n ensures\n -1000 <= result <= 1000,\n Self::valid_x_spec(nums1@, nums2@, result as int),\n forall |x: int| -1000 <= x < result ==> !Self::valid_x_spec(nums1@, nums2@, x),\n {\n let n1 = nums1.len();\n let n2 = nums2.len();\n let mut x = -1000;\n while x <= 1000 {\n let mut ok = true;\n let mut v = 0;\n let mut fail_v = -1;\n while v <= 1000 && ok {\n let mut c1 = 0;\n let mut i: usize = 0;\n while i < n1 {\n if nums1[i] as i64 + x as i64 == v as i64 {\n c1 += 1;\n }\n i += 1;\n }\n let mut c2 = 0;\n let mut j: usize = 0;\n while j < n2 {\n if nums2[j] as i64 == v as i64 {\n c2 += 1;\n }\n j += 1;\n }\n if c1 < c2 {\n ok = false;\n fail_v = v;\n }\n v += 1;\n }\n if ok {\n return x;\n }\n let _ = fail_v;\n x += 1;\n }\n 0\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_plain_prefix(nums: Seq, end: int, v: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::count_plain_prefix(nums, end - 1, v) + if nums[end - 1] as int == v { 1int } else { 0int }\n }\n }\n\n pub open spec fn count_shift_prefix(nums: Seq, end: int, x: int, v: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0int\n } else {\n Self::count_shift_prefix(nums, end - 1, x, v) + if nums[end - 1] as int + x == v { 1int } else { 0int }\n }\n }\n\n pub open spec fn valid_x_spec(nums1: Seq, nums2: Seq, x: int) -> bool {\n forall |v: int| 0 <= v <= 1000 ==> Self::count_shift_prefix(nums1, nums1.len() as int, x, v) >= Self::count_plain_prefix(nums2, nums2.len() as int, v)\n }\n\n pub fn minimum_added_integer(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 3 <= nums1.len() <= 200,\n nums2.len() + 2 == nums1.len(),\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1000,\n forall |i: int| 0 <= i < nums2.len() ==> 0 <= #[trigger] nums2[i] <= 1000,\n exists |x: int| -1000 <= x <= 1000 && Self::valid_x_spec(nums1@, nums2@, x),\n ensures\n -1000 <= result <= 1000,\n Self::valid_x_spec(nums1@, nums2@, result as int),\n forall |x: int| -1000 <= x < result ==> !Self::valid_x_spec(nums1@, nums2@, x),\n {\n let n1 = nums1.len();\n let n2 = nums2.len();\n let mut x = -1000;\n while x <= 1000\n invariant\n n1 == nums1.len(),\n n2 == nums2.len(),\n 3 <= n1 <= 200,\n n2 + 2 == n1,\n -1000 <= x <= 1001,\n forall |i: int| 0 <= i < nums1.len() ==> 0 <= #[trigger] nums1[i] <= 1000,\n forall |i: int| 0 <= i < nums2.len() ==> 0 <= #[trigger] nums2[i] <= 1000,\n forall |k: int| -1000 <= k < x ==> !Self::valid_x_spec(nums1@, nums2@, k),\n exists |k: int| x <= k <= 1000 && Self::valid_x_spec(nums1@, nums2@, k),\n decreases 1001 - x as int,\n {\n let mut ok = true;\n let mut v = 0;\n let mut fail_v = -1;\n while v <= 1000 && ok\n invariant\n n1 == nums1.len(),\n n2 == nums2.len(),\n 3 <= nums1.len() <= 200,\n nums2.len() + 2 == nums1.len(),\n -1000 <= x <= 1000,\n 0 <= v <= 1001,\n ok ==> forall |u: int| 0 <= u < v ==> Self::count_shift_prefix(nums1@, n1 as int, x as int, u) >= Self::count_plain_prefix(nums2@, n2 as int, u),\n !ok ==> 0 <= fail_v < v <= 1001,\n !ok ==> Self::count_shift_prefix(nums1@, n1 as int, x as int, fail_v as int) < Self::count_plain_prefix(nums2@, n2 as int, fail_v as int),\n decreases 1001 - v as int,\n {\n let mut c1 = 0;\n let mut i: usize = 0;\n while i < n1\n invariant\n n1 == nums1.len(),\n 3 <= nums1.len() <= 200,\n 0 <= i <= n1,\n 0 <= c1 <= i as int,\n c1 as int == Self::count_shift_prefix(nums1@, i as int, x as int, v as int),\n decreases n1 - i,\n {\n if nums1[i] as i64 + x as i64 == v as i64 {\n proof {\n assert(i < n1);\n assert(i < 200usize);\n assert(c1 < 200);\n }\n c1 += 1;\n }\n i += 1;\n }\n let mut c2 = 0;\n let mut j: usize = 0;\n while j < n2\n invariant\n n2 == nums2.len(),\n 3 <= nums1.len() <= 200,\n nums2.len() + 2 == nums1.len(),\n 0 <= j <= n2,\n 0 <= c2 <= j as int,\n c2 as int == Self::count_plain_prefix(nums2@, j as int, v as int),\n decreases n2 - j,\n {\n if nums2[j] as i64 == v as i64 {\n proof {\n assert(j < n2);\n assert(j < 200usize);\n assert(c2 < 200);\n }\n c2 += 1;\n }\n j += 1;\n }\n proof {\n assert(c1 as int == Self::count_shift_prefix(nums1@, n1 as int, x as int, v as int));\n assert(c2 as int == Self::count_plain_prefix(nums2@, n2 as int, v as int));\n }\n if c1 < c2 {\n ok = false;\n fail_v = v;\n }\n v += 1;\n }\n if ok {\n proof {\n assert(v == 1001);\n assert forall |u: int| 0 <= u <= 1000 implies Self::count_shift_prefix(nums1@, n1 as int, x as int, u) >= Self::count_plain_prefix(nums2@, n2 as int, u) by {\n assert(0 <= u < v);\n }\n assert(Self::valid_x_spec(nums1@, nums2@, x as int));\n assert forall |k: int| -1000 <= k < x as int implies !Self::valid_x_spec(nums1@, nums2@, k) by {\n }\n }\n return x;\n }\n proof {\n assert(!ok);\n assert(0 <= fail_v < v <= 1001);\n assert(Self::count_shift_prefix(nums1@, n1 as int, x as int, fail_v as int) < Self::count_plain_prefix(nums2@, n2 as int, fail_v as int));\n assert(!Self::valid_x_spec(nums1@, nums2@, x as int));\n }\n let _ = fail_v;\n x += 1;\n }\n proof {\n assert(x == 1001);\n assert(false);\n }\n 0\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3132/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3132/tests/mutated_testcases.jsonl"} {"problem": "lc3133", "family": "leetcode", "description": "# Minimum Array End\n\nYou are given two integers `n` and `x`. You have to construct an array of **positive** integers `nums` of size `n` where for every `0 <= i < n - 1`, `nums[i + 1]` is **greater than** `nums[i]`, and the result of the bitwise `AND` operation between all elements of `nums` is `x`.\n\nReturn the **minimum** possible value of `nums[n - 1]`.\n\n \n\nExample 1:\n\n**Input:** n = 3, x = 4\n\n**Output:** 6\n\n**Explanation:**\n\n`nums` can be `[4,5,6]` and its last element is 6.\n\nExample 2:\n\n**Input:** n = 2, x = 7\n\n**Output:** 15\n\n**Explanation:**\n\n`nums` can be `[7,15]` and its last element is 15.\n\n \n\n**Constraints:**\n\n\t- `1 <= n, x <= 10^8`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_end(n: i32, x: i32) -> i64 {\n \n }\n}\n```\n", "tags": "Medium\nBit Manipulation\nAcceptance rate: 55.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_end_from(x: int, cur: int, rem: int, pos: int) -> int\n decreases 63 - pos, rem,\n {\n if pos >= 63 || rem <= 0 {\n cur\n } else {\n let bit64 = 1i64 << (pos as i32);\n if ((x as i64) & bit64) != 0 {\n Self::min_end_from(x, cur, rem, pos + 1)\n } else {\n let cur2 = if ((rem as i64) & 1i64) == 1i64 { ((cur as i64) | bit64) as int } else { cur };\n Self::min_end_from(x, cur2, rem / 2, pos + 1)\n }\n }\n }\n\n pub open spec fn min_end_spec(n: int, x: int, result: int) -> bool {\n &&& 1 <= n <= 100_000_000\n &&& 1 <= x <= 100_000_000\n &&& result <= 9_223_372_036_854_775_807\n &&& result == Self::min_end_from(x, x, n - 1, 0)\n }\n\n pub fn min_end(n: i32, x: i32) -> (result: i64)\n requires\n 1 <= n <= 100_000_000,\n 1 <= x <= 100_000_000,\n ensures\n Self::min_end_spec(n as int, x as int, result as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_end(n: i32, x: i32) -> i64 {\n let mut result = x as i64;\n let mut remaining = (n - 1) as i64;\n let mut pos = 0i32;\n let xx = x as i64;\n\n while pos < 63 && remaining > 0 {\n let position = 1i64 << pos;\n if (xx & position) == 0 {\n if (remaining & 1) == 1 {\n result = result | position;\n }\n remaining = remaining / 2;\n }\n pos = pos + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_end_from(x: int, cur: int, rem: int, pos: int) -> int\n decreases 63 - pos, rem,\n {\n if pos >= 63 || rem <= 0 {\n cur\n } else {\n let bit64 = 1i64 << (pos as i32);\n if ((x as i64) & bit64) != 0 {\n Self::min_end_from(x, cur, rem, pos + 1)\n } else {\n let cur2 = if ((rem as i64) & 1i64) == 1i64 { ((cur as i64) | bit64) as int } else { cur };\n Self::min_end_from(x, cur2, rem / 2, pos + 1)\n }\n }\n }\n\n pub open spec fn min_end_spec(n: int, x: int, result: int) -> bool {\n &&& 1 <= n <= 100_000_000\n &&& 1 <= x <= 100_000_000\n &&& result <= 9_223_372_036_854_775_807\n &&& result == Self::min_end_from(x, x, n - 1, 0)\n }\n\n pub fn min_end(n: i32, x: i32) -> (result: i64)\n requires\n 1 <= n <= 100_000_000,\n 1 <= x <= 100_000_000,\n ensures\n Self::min_end_spec(n as int, x as int, result as int),\n {\n let mut result = x as i64;\n let mut remaining = (n - 1) as i64;\n let mut pos = 0i32;\n let xx = x as i64;\n\n while pos < 63 && remaining > 0 {\n let position = 1i64 << pos;\n if (xx & position) == 0 {\n if (remaining & 1) == 1 {\n result = result | position;\n }\n remaining = remaining / 2;\n }\n pos = pos + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_end_from(x: int, cur: int, rem: int, pos: int) -> int\n decreases 63 - pos, rem,\n {\n if pos >= 63 || rem <= 0 {\n cur\n } else {\n let bit64 = 1i64 << (pos as i32);\n if ((x as i64) & bit64) != 0 {\n Self::min_end_from(x, cur, rem, pos + 1)\n } else {\n let cur2 = if ((rem as i64) & 1i64) == 1i64 { ((cur as i64) | bit64) as int } else { cur };\n Self::min_end_from(x, cur2, rem / 2, pos + 1)\n }\n }\n }\n\n pub open spec fn min_end_spec(n: int, x: int, result: int) -> bool {\n &&& 1 <= n <= 100_000_000\n &&& 1 <= x <= 100_000_000\n &&& result <= 9_223_372_036_854_775_807\n &&& result == Self::min_end_from(x, x, n - 1, 0)\n }\n\n pub fn min_end(n: i32, x: i32) -> (result: i64)\n requires\n 1 <= n <= 100_000_000,\n 1 <= x <= 100_000_000,\n ensures\n Self::min_end_spec(n as int, x as int, result as int),\n {\n let mut result = x as i64;\n let mut remaining = (n - 1) as i64;\n let mut pos = 0i32;\n let xx = x as i64;\n\n while pos < 63 && remaining > 0\n invariant\n 1 <= n <= 100_000_000,\n 1 <= x <= 100_000_000,\n 0 <= pos <= 63,\n 0 <= remaining,\n xx == x as i64,\n result <= 9_223_372_036_854_775_807i64,\n Self::min_end_from(x as int, result as int, remaining as int, pos as int)\n == Self::min_end_from(x as int, x as int, n as int - 1, 0),\n decreases 63 - pos, remaining,\n {\n assert(0 <= pos < 63);\n let position = 1i64 << pos;\n if (xx & position) == 0 {\n let ghost old_result = result;\n let ghost old_remaining = remaining;\n let ghost old_pos = pos;\n assert(((x as i64) & position) == 0);\n if (remaining & 1) == 1 {\n result = result | position;\n }\n assert(Self::min_end_from(x as int, old_result as int, old_remaining as int, old_pos as int)\n == Self::min_end_from(x as int, result as int, remaining / 2, pos as int + 1));\n remaining = remaining / 2;\n } else {\n assert(((x as i64) & position) != 0);\n assert(Self::min_end_from(x as int, result as int, remaining as int, pos as int)\n == Self::min_end_from(x as int, result as int, remaining as int, pos as int + 1));\n }\n pos = pos + 1;\n }\n assert(!(pos < 63 && remaining > 0));\n assert(pos >= 63 || remaining <= 0);\n assert(Self::min_end_from(x as int, result as int, remaining as int, pos as int) == result as int);\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3133/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3133/tests/mutated_testcases.jsonl"} {"problem": "lc3142", "family": "leetcode", "description": "# Check if Grid Satisfies Conditions\n\nYou are given a 2D matrix `grid` of size `m x n`. You need to check whether each cell `grid[i][j]` satisfies the following conditions:\n\n- If the cell below it exists, then `grid[i][j] == grid[i + 1][j]`.\n- If the cell to its right exists, then `grid[i][j] != grid[i][j + 1]`.\n\nReturn `true` if all the cells satisfy these conditions. Otherwise, return `false`.\n\n## Example 1:\n\n> **Input:** grid = [[1,0,2],[1,0,2]]\n> **Output:** true\n\n## Example 2:\n\n> **Input:** grid = [[1,1,1],[0,0,0]]\n> **Output:** false\n\n## Example 3:\n\n> **Input:** grid = [[1],[2],[3]]\n> **Output:** false\n\n## Constraints:\n\n- `1 <= n, m <= 10`\n- `0 <= grid[i][j] <= 9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn satisfies_conditions(grid: Vec>) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Array, Matrix, Biweekly Contest 130\nAcceptance rate: 45.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn satisfies_conditions(grid: Vec>) -> (result: bool)\n requires\n 1 <= grid.len() <= 10,\n forall |i: int| 0 <= i < grid.len() ==> 1 <= #[trigger] grid[i].len() <= 10,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[0].len() ==> 0 <= #[trigger] grid[i][j] <= 9,\n ensures\n result == (\n (forall |i: int, j: int|\n 0 <= i && i + 1 < grid.len() && 0 <= j < grid[0].len() ==>\n #[trigger] grid[i][j] == grid[i + 1][j])\n &&\n (forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j && j + 1 < grid[0].len() ==>\n #[trigger] grid[i][j] != grid[i][j + 1])\n ),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn satisfies_conditions(grid: Vec>) -> bool {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut i: usize = 0;\n while i < rows {\n let mut j: usize = 0;\n while j < cols {\n if i + 1 < rows && grid[i][j] != grid[i + 1][j] {\n return false;\n }\n if j + 1 < cols && grid[i][j] == grid[i][j + 1] {\n return false;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn satisfies_conditions(grid: Vec>) -> (result: bool)\n requires\n 1 <= grid.len() <= 10,\n forall |i: int| 0 <= i < grid.len() ==> 1 <= #[trigger] grid[i].len() <= 10,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[0].len() ==> 0 <= #[trigger] grid[i][j] <= 9,\n ensures\n result == (\n (forall |i: int, j: int|\n 0 <= i && i + 1 < grid.len() && 0 <= j < grid[0].len() ==>\n #[trigger] grid[i][j] == grid[i + 1][j])\n &&\n (forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j && j + 1 < grid[0].len() ==>\n #[trigger] grid[i][j] != grid[i][j + 1])\n ),\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut i: usize = 0;\n while i < rows {\n let mut j: usize = 0;\n while j < cols {\n if i + 1 < rows && grid[i][j] != grid[i + 1][j] {\n return false;\n }\n if j + 1 < cols && grid[i][j] == grid[i][j + 1] {\n return false;\n }\n j = j + 1;\n }\n i = i + 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn satisfies_conditions(grid: Vec>) -> (result: bool)\n requires\n 1 <= grid.len() <= 10,\n forall |i: int| 0 <= i < grid.len() ==> 1 <= #[trigger] grid[i].len() <= 10,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid[0].len(),\n forall |i: int, j: int| 0 <= i < grid.len() && 0 <= j < grid[0].len() ==> 0 <= #[trigger] grid[i][j] <= 9,\n ensures\n result == (\n (forall |i: int, j: int|\n 0 <= i && i + 1 < grid.len() && 0 <= j < grid[0].len() ==>\n #[trigger] grid[i][j] == grid[i + 1][j])\n &&\n (forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j && j + 1 < grid[0].len() ==>\n #[trigger] grid[i][j] != grid[i][j + 1])\n ),\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut i: usize = 0;\n while i < rows\n invariant\n rows == grid.len(),\n cols == grid[0].len(),\n 1 <= rows <= 10,\n 1 <= cols <= 10,\n 0 <= i <= rows,\n forall |r: int| 0 <= r < rows ==> 1 <= #[trigger] grid[r].len() <= 10,\n forall |r: int| 0 <= r < rows ==> #[trigger] grid[r].len() == cols,\n forall |r: int, c: int| 0 <= r < rows && 0 <= c < cols ==> 0 <= #[trigger] grid[r][c] <= 9,\n forall |r: int, c: int|\n 0 <= r < i && 0 <= c < cols ==>\n ((r + 1 < rows ==> #[trigger] grid[r][c] == grid[r + 1][c])\n && (c + 1 < cols ==> #[trigger] grid[r][c] != grid[r][c + 1])),\n decreases rows - i,\n {\n let mut j: usize = 0;\n while j < cols\n invariant\n rows == grid.len(),\n cols == grid[0].len(),\n 1 <= rows <= 10,\n 1 <= cols <= 10,\n 0 <= i < rows,\n 0 <= j <= cols,\n forall |r: int| 0 <= r < rows ==> 1 <= #[trigger] grid[r].len() <= 10,\n forall |r: int| 0 <= r < rows ==> #[trigger] grid[r].len() == cols,\n forall |r: int, c: int| 0 <= r < rows && 0 <= c < cols ==> 0 <= #[trigger] grid[r][c] <= 9,\n forall |r: int, c: int|\n 0 <= r < i && 0 <= c < cols ==>\n ((r + 1 < rows ==> #[trigger] grid[r][c] == grid[r + 1][c])\n && (c + 1 < cols ==> #[trigger] grid[r][c] != grid[r][c + 1])),\n forall |c: int|\n 0 <= c < j ==>\n ((i as int + 1 < rows ==> #[trigger] grid[i as int][c] == grid[i as int + 1][c])\n && (c + 1 < cols ==> #[trigger] grid[i as int][c] != grid[i as int][c + 1])),\n decreases cols - j,\n {\n proof {\n assert(i < rows);\n assert(j < cols);\n assert(grid[i as int].len() == cols);\n if i + 1 < rows {\n assert(grid[i as int + 1].len() == cols);\n }\n }\n if i + 1 < rows && grid[i][j] != grid[i + 1][j] {\n proof {\n assert(!(forall |r: int, c: int|\n 0 <= r && r + 1 < grid.len() && 0 <= c < grid[0].len() ==> #[trigger] grid[r][c] == grid[r + 1][c])) by {\n if forall |r: int, c: int|\n 0 <= r && r + 1 < grid.len() && 0 <= c < grid[0].len() ==> #[trigger] grid[r][c] == grid[r + 1][c]\n {\n assert(0 <= i as int);\n assert((i as int) + 1 < grid.len());\n assert(0 <= j as int);\n assert((j as int) < grid[0].len());\n assert(grid[i as int][j as int] == grid[i as int + 1][j as int]);\n assert(false);\n }\n };\n }\n return false;\n }\n if j + 1 < cols && grid[i][j] == grid[i][j + 1] {\n proof {\n assert(!(forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c && c + 1 < grid[0].len() ==> #[trigger] grid[r][c] != grid[r][c + 1])) by {\n if forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c && c + 1 < grid[0].len() ==> #[trigger] grid[r][c] != grid[r][c + 1]\n {\n assert(0 <= i as int);\n assert((i as int) < grid.len());\n assert(0 <= j as int);\n assert((j as int) + 1 < grid[0].len());\n assert(grid[i as int][j as int] != grid[i as int][j as int + 1]);\n assert(false);\n }\n };\n }\n return false;\n }\n j = j + 1;\n }\n proof {\n assert(j == cols);\n assert forall |r: int, c: int|\n 0 <= r < i as int + 1 && 0 <= c < cols implies\n ((r + 1 < rows ==> #[trigger] grid[r][c] == grid[r + 1][c])\n && (c + 1 < cols ==> #[trigger] grid[r][c] != grid[r][c + 1])) by {\n if r < i as int {\n } else {\n assert(r == i as int);\n }\n };\n }\n i = i + 1;\n }\n proof {\n assert(i == rows);\n assert forall |r: int, c: int|\n 0 <= r && r + 1 < grid.len() && 0 <= c < grid[0].len() implies #[trigger] grid[r][c] == grid[r + 1][c] by {\n assert(r < i as int);\n };\n assert forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c && c + 1 < grid[0].len() implies #[trigger] grid[r][c] != grid[r][c + 1] by {\n assert(r < i as int);\n };\n }\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3142/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3142/tests/mutated_testcases.jsonl"} {"problem": "lc3151", "family": "leetcode", "description": "# Special Array I\n\nAn array is considered **special** if the parity of every pair of adjacent elements is different. In other words, one element in each pair must be even, and the other must be odd.\n\nYou are given an array of integers `nums`. Return `true` if `nums` is a special array. Otherwise, return `false`.\n\n## Example 1:\n\n> **Input:** nums = [1]\n> **Output:** true\n> **Explanation:** There is only one element, so the array is special.\n\n## Example 2:\n\n> **Input:** nums = [2,1,4]\n> **Output:** true\n> **Explanation:** The pairs `(2,1)` and `(1,4)` have different parity.\n\n## Example 3:\n\n> **Input:** nums = [4,3,1,6]\n> **Output:** false\n> **Explanation:** `nums[1]` and `nums[2]` are both odd.\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `1 <= nums[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_array_special(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Array, Weekly Contest 398\nAcceptance rate: 81.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_array_special(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result == (forall |i: int| 0 <= i && i + 1 < nums.len() ==> (#[trigger] nums[i]) % 2 != nums[i + 1] % 2),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_array_special(nums: Vec) -> bool {\n let n = nums.len();\n let mut i: usize = 1;\n while i < n {\n if nums[i - 1] % 2 == nums[i] % 2 {\n return false;\n }\n i = i + 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_array_special(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result == (forall |i: int| 0 <= i && i + 1 < nums.len() ==> (#[trigger] nums[i]) % 2 != nums[i + 1] % 2),\n {\n let n = nums.len();\n let mut i: usize = 1;\n while i < n {\n if nums[i - 1] % 2 == nums[i] % 2 {\n return false;\n }\n i = i + 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn is_array_special(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result == (forall |i: int| 0 <= i && i + 1 < nums.len() ==> (#[trigger] nums[i]) % 2 != nums[i + 1] % 2),\n {\n let n = nums.len();\n let mut i: usize = 1;\n while i < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 100,\n 1 <= i <= n,\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 100,\n forall |j: int| 0 <= j && j + 1 < i as int ==> (#[trigger] nums[j]) % 2 != nums[j + 1] % 2,\n decreases n - i,\n {\n if nums[i - 1] % 2 == nums[i] % 2 {\n proof {\n let j = i as int - 1;\n assert(0 <= j);\n assert(j + 1 == i as int);\n assert(j + 1 < nums.len());\n assert(nums[j] % 2 == nums[j + 1] % 2);\n assert(!(forall |k: int| 0 <= k && k + 1 < nums.len() ==> (#[trigger] nums[k]) % 2 != nums[k + 1] % 2)) by {\n if forall |k: int| 0 <= k && k + 1 < nums.len() ==> (#[trigger] nums[k]) % 2 != nums[k + 1] % 2 {\n assert(nums[j] % 2 != nums[j + 1] % 2);\n assert(false);\n }\n }\n }\n return false;\n }\n i = i + 1;\n }\n proof {\n assert(i == n);\n assert forall |j: int| 0 <= j && j + 1 < nums.len() implies (#[trigger] nums[j]) % 2 != nums[j + 1] % 2 by {\n assert(j + 1 < i as int);\n };\n }\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3151/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3151/tests/mutated_testcases.jsonl"} {"problem": "lc3159", "family": "leetcode", "description": "# Find Occurrences of an Element in an Array\n\nYou are given an integer array `nums`, an integer array `queries`, and an integer `x`.\n\nFor each `queries[i]`, you need to find the index of the `queries[i]^th` occurrence of `x` in the `nums` array. If there are fewer than `queries[i]` occurrences of `x`, the answer should be -1 for that query.\n\nReturn an integer array `answer` containing the answers to all queries.\n\n \n\nExample 1:\n\n**Input:** nums = [1,3,1,7], queries = [1,3,2,4], x = 1\n\n**Output:** [0,-1,2,-1]\n\n**Explanation:**\n\n\t- For the 1^st query, the first occurrence of 1 is at index 0.\n\n\t- For the 2^nd query, there are only two occurrences of 1 in `nums`, so the answer is -1.\n\n\t- For the 3^rd query, the second occurrence of 1 is at index 2.\n\n\t- For the 4^th query, there are only two occurrences of 1 in `nums`, so the answer is -1.\n\nExample 2:\n\n**Input:** nums = [1,2,3], queries = [10], x = 5\n\n**Output:** [-1]\n\n**Explanation:**\n\n\t- For the 1^st query, 5 doesn't exist in `nums`, so the answer is -1.\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length, queries.length <= 10^5`\n\n\t- `1 <= queries[i] <= 10^5`\n\n\t- `1 <= nums[i], x <= 10^4`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn occurrences_of_element(nums: Vec, queries: Vec, x: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Hash Table\nAcceptance rate: 73.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_x_prefix(nums: Seq, x: int, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_x_prefix(nums, x, end - 1)\n + if nums[end - 1] as int == x { 1int } else { 0int }\n }\n }\n\n pub open spec fn query_answer_ok(nums: Seq, x: int, k: int, value: int) -> bool {\n if value == -1 {\n Self::count_x_prefix(nums, x, nums.len() as int) < k\n } else {\n &&& 0 <= value < nums.len()\n &&& nums[value] as int == x\n &&& Self::count_x_prefix(nums, x, value) == k - 1\n &&& Self::count_x_prefix(nums, x, value + 1) == k\n }\n }\n\n pub open spec fn kth_occurrence_from(nums: Seq, x: int, k: int, idx: int, seen: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n -1\n } else {\n let hit = if nums[idx] as int == x { 1int } else { 0int };\n let seen2 = seen + hit;\n if nums[idx] as int == x && seen2 == k {\n idx\n } else {\n Self::kth_occurrence_from(nums, x, k, idx + 1, seen2)\n }\n }\n }\n\n pub open spec fn kth_occurrence_index(nums: Seq, x: int, k: int) -> int {\n if k <= 0 { -1 } else { Self::kth_occurrence_from(nums, x, k, 0, 0) }\n }\n\n pub fn occurrences_of_element(nums: Vec, queries: Vec, x: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100000,\n 1 <= queries.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10000,\n forall |i: int| 0 <= i < queries.len() ==> 1 <= #[trigger] queries[i] <= 100000,\n 1 <= x <= 10000,\n ensures\n result.len() == queries.len(),\n forall |qi: int| 0 <= qi < queries.len()\n ==> Self::query_answer_ok(nums@, x as int, queries[qi] as int, #[trigger] result[qi] as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn occurrences_of_element(nums: Vec, queries: Vec, x: i32) -> Vec {\n let mut positions: Vec = Vec::new();\n let mut i = 0usize;\n while i < nums.len() {\n if nums[i] == x {\n positions.push(i as i32);\n }\n i += 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut j = 0usize;\n while j < queries.len() {\n let q = queries[j] as usize;\n if q == 0 || q > positions.len() {\n ans.push(-1);\n } else {\n ans.push(positions[q - 1]);\n }\n j += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_x_prefix(nums: Seq, x: int, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_x_prefix(nums, x, end - 1)\n + if nums[end - 1] as int == x { 1int } else { 0int }\n }\n }\n\n pub open spec fn query_answer_ok(nums: Seq, x: int, k: int, value: int) -> bool {\n if value == -1 {\n Self::count_x_prefix(nums, x, nums.len() as int) < k\n } else {\n &&& 0 <= value < nums.len()\n &&& nums[value] as int == x\n &&& Self::count_x_prefix(nums, x, value) == k - 1\n &&& Self::count_x_prefix(nums, x, value + 1) == k\n }\n }\n\n pub open spec fn kth_occurrence_from(nums: Seq, x: int, k: int, idx: int, seen: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n -1\n } else {\n let hit = if nums[idx] as int == x { 1int } else { 0int };\n let seen2 = seen + hit;\n if nums[idx] as int == x && seen2 == k {\n idx\n } else {\n Self::kth_occurrence_from(nums, x, k, idx + 1, seen2)\n }\n }\n }\n\n pub open spec fn kth_occurrence_index(nums: Seq, x: int, k: int) -> int {\n if k <= 0 { -1 } else { Self::kth_occurrence_from(nums, x, k, 0, 0) }\n }\n\n pub fn occurrences_of_element(nums: Vec, queries: Vec, x: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100000,\n 1 <= queries.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10000,\n forall |i: int| 0 <= i < queries.len() ==> 1 <= #[trigger] queries[i] <= 100000,\n 1 <= x <= 10000,\n ensures\n result.len() == queries.len(),\n forall |qi: int| 0 <= qi < queries.len()\n ==> Self::query_answer_ok(nums@, x as int, queries[qi] as int, #[trigger] result[qi] as int),\n {\n let mut positions: Vec = Vec::new();\n let mut i = 0usize;\n while i < nums.len() {\n if nums[i] == x {\n positions.push(i as i32);\n }\n i += 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut j = 0usize;\n while j < queries.len() {\n let q = queries[j] as usize;\n if q == 0 || q > positions.len() {\n ans.push(-1);\n } else {\n ans.push(positions[q - 1]);\n }\n j += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_x_prefix(nums: Seq, x: int, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_x_prefix(nums, x, end - 1)\n + if nums[end - 1] as int == x { 1int } else { 0int }\n }\n }\n\n pub open spec fn query_answer_ok(nums: Seq, x: int, k: int, value: int) -> bool {\n if value == -1 {\n Self::count_x_prefix(nums, x, nums.len() as int) < k\n } else {\n &&& 0 <= value < nums.len()\n &&& nums[value] as int == x\n &&& Self::count_x_prefix(nums, x, value) == k - 1\n &&& Self::count_x_prefix(nums, x, value + 1) == k\n }\n }\n\n pub open spec fn kth_occurrence_from(nums: Seq, x: int, k: int, idx: int, seen: int) -> int\n decreases nums.len() - idx,\n {\n if idx >= nums.len() {\n -1\n } else {\n let hit = if nums[idx] as int == x { 1int } else { 0int };\n let seen2 = seen + hit;\n if nums[idx] as int == x && seen2 == k {\n idx\n } else {\n Self::kth_occurrence_from(nums, x, k, idx + 1, seen2)\n }\n }\n }\n\n pub open spec fn kth_occurrence_index(nums: Seq, x: int, k: int) -> int {\n if k <= 0 { -1 } else { Self::kth_occurrence_from(nums, x, k, 0, 0) }\n }\n\n pub fn occurrences_of_element(nums: Vec, queries: Vec, x: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100000,\n 1 <= queries.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 10000,\n forall |i: int| 0 <= i < queries.len() ==> 1 <= #[trigger] queries[i] <= 100000,\n 1 <= x <= 10000,\n ensures\n result.len() == queries.len(),\n forall |qi: int| 0 <= qi < queries.len()\n ==> Self::query_answer_ok(nums@, x as int, queries[qi] as int, #[trigger] result[qi] as int),\n {\n let mut positions: Vec = Vec::new();\n let mut i = 0usize;\n while i < nums.len()\n invariant\n 0 <= i <= nums.len(),\n nums.len() <= i32::MAX as usize,\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 10000,\n forall |r: int| 0 <= r < positions.len() ==> 0 <= #[trigger] positions[r] < nums.len(),\n forall |r: int| 0 <= r < positions.len() ==> nums[positions[r] as int] == x,\n positions.len() as int == Self::count_x_prefix(nums@, x as int, i as int),\n forall |r: int| 0 <= r < positions.len()\n ==> Self::count_x_prefix(nums@, x as int, #[trigger] positions[r] as int) == r,\n forall |r: int| 0 <= r < positions.len()\n ==> Self::count_x_prefix(nums@, x as int, #[trigger] positions[r] as int + 1) == r + 1,\n positions.len() <= i,\n decreases nums.len() - i,\n {\n if nums[i] == x {\n assert(i <= i32::MAX as usize);\n positions.push(i as i32);\n assert(0 <= positions[positions.len() as int - 1]);\n assert(positions[positions.len() as int - 1] < nums.len());\n assert(nums[positions[positions.len() as int - 1] as int] == x);\n }\n i += 1;\n }\n\n let mut ans: Vec = Vec::new();\n let mut j = 0usize;\n while j < queries.len()\n invariant\n 0 <= j <= queries.len(),\n ans.len() == j,\n nums.len() <= i32::MAX as usize,\n forall |t: int| 0 <= t < queries.len() ==> 1 <= #[trigger] queries[t] <= 100000,\n forall |r: int| 0 <= r < positions.len() ==> 0 <= #[trigger] positions[r] < nums.len(),\n forall |r: int| 0 <= r < positions.len() ==> nums[positions[r] as int] == x,\n positions.len() as int == Self::count_x_prefix(nums@, x as int, nums.len() as int),\n forall |r: int| 0 <= r < positions.len()\n ==> Self::count_x_prefix(nums@, x as int, #[trigger] positions[r] as int) == r,\n forall |r: int| 0 <= r < positions.len()\n ==> Self::count_x_prefix(nums@, x as int, #[trigger] positions[r] as int + 1) == r + 1,\n forall |t: int| 0 <= t < j\n ==> Self::query_answer_ok(nums@, x as int, queries[t] as int, #[trigger] ans[t] as int),\n decreases queries.len() - j,\n {\n let q = queries[j] as usize;\n if q == 0 || q > positions.len() {\n ans.push(-1);\n assert(Self::count_x_prefix(nums@, x as int, nums.len() as int) < queries[j as int] as int);\n assert(Self::query_answer_ok(nums@, x as int, queries[j as int] as int, -1));\n } else {\n ans.push(positions[q - 1]);\n assert(0 <= q as int - 1 < positions.len());\n assert(Self::count_x_prefix(nums@, x as int, positions[q as int - 1] as int) == queries[j as int] as int - 1);\n assert(Self::count_x_prefix(nums@, x as int, positions[q as int - 1] as int + 1) == queries[j as int] as int);\n assert(Self::query_answer_ok(nums@, x as int, queries[j as int] as int, positions[q as int - 1] as int));\n }\n j += 1;\n }\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3159/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3159/tests/mutated_testcases.jsonl"} {"problem": "lc3162", "family": "leetcode", "description": "# Find the Number of Good Pairs I\n\nYou are given 2 integer arrays `nums1` and `nums2` of lengths `n` and `m` respectively. You are also given a **positive** integer `k`.\n\nA pair `(i, j)` is called **good** if `nums1[i]` is divisible by `nums2[j] * k` (`0 <= i <= n - 1`, `0 <= j <= m - 1`).\n\nReturn the total number of **good** pairs.\n\n## Example 1:\n\n> **Input:** nums1 = [1,3,4], nums2 = [1,3,4], k = 1\n> **Output:** 5\n> **Explanation:** The 5 good pairs are `(0, 0)`, `(1, 0)`, `(1, 1)`, `(2, 0)`, and `(2, 2)`.\n\n## Example 2:\n\n> **Input:** nums1 = [1,2,4,12], nums2 = [2,4], k = 3\n> **Output:** 2\n> **Explanation:** The 2 good pairs are `(3, 0)` and `(3, 1)`.\n\n## Constraints:\n\n- $1 <= n, m <= 50$\n\n- $1 <= nums1[i], nums2[j] <= 50$\n\n- $1 <= k <= 50$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn number_of_pairs(nums1: Vec, nums2: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table\nAcceptance rate: 86.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn inner_count(nums1: Seq, nums2: Seq, k: int, i: int, j: int) -> int\n decreases nums2.len() - j,\n {\n if j >= nums2.len() {\n 0\n } else {\n (if nums1[i] as int % (nums2[j] as int * k) == 0 { 1int } else { 0int })\n + Self::inner_count(nums1, nums2, k, i, j + 1)\n }\n }\n\n pub open spec fn good_pair_count(nums1: Seq, nums2: Seq, k: int, i: int) -> int\n decreases nums1.len() - i,\n {\n if i >= nums1.len() {\n 0\n } else {\n Self::inner_count(nums1, nums2, k, i, 0) + Self::good_pair_count(nums1, nums2, k, i + 1)\n }\n }\n\n pub fn number_of_pairs(nums1: Vec, nums2: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums1.len() <= 50,\n 1 <= nums2.len() <= 50,\n forall|i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 50,\n forall|j: int| 0 <= j < nums2.len() ==> 1 <= #[trigger] nums2[j] <= 50,\n 1 <= k <= 50,\n ensures\n res as int == Self::good_pair_count(nums1@, nums2@, k as int, 0),\n 0 <= res <= nums1.len() * nums2.len(),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn number_of_pairs(nums1: Vec, nums2: Vec, k: i32) -> i32 {\n let mut count: i32 = 0;\n let n = nums1.len();\n let m = nums2.len();\n for i in 0..n {\n let mut inner: i32 = 0;\n for j in 0..m {\n if nums1[i] % (nums2[j] * k) == 0 {\n inner += 1;\n }\n }\n count += inner;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn inner_count(nums1: Seq, nums2: Seq, k: int, i: int, j: int) -> int\n decreases nums2.len() - j,\n {\n if j >= nums2.len() {\n 0\n } else {\n (if nums1[i] as int % (nums2[j] as int * k) == 0 { 1int } else { 0int })\n + Self::inner_count(nums1, nums2, k, i, j + 1)\n }\n }\n\n pub open spec fn good_pair_count(nums1: Seq, nums2: Seq, k: int, i: int) -> int\n decreases nums1.len() - i,\n {\n if i >= nums1.len() {\n 0\n } else {\n Self::inner_count(nums1, nums2, k, i, 0) + Self::good_pair_count(nums1, nums2, k, i + 1)\n }\n }\n\n pub fn number_of_pairs(nums1: Vec, nums2: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums1.len() <= 50,\n 1 <= nums2.len() <= 50,\n forall|i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 50,\n forall|j: int| 0 <= j < nums2.len() ==> 1 <= #[trigger] nums2[j] <= 50,\n 1 <= k <= 50,\n ensures\n res as int == Self::good_pair_count(nums1@, nums2@, k as int, 0),\n 0 <= res <= nums1.len() * nums2.len(),\n {\n let mut count: i32 = 0;\n let n = nums1.len();\n let m = nums2.len();\n for i in 0..n\n {\n let mut inner: i32 = 0;\n for j in 0..m\n {\n if nums1[i] % (nums2[j] * k) == 0 {\n inner += 1;\n }\n }\n count += inner;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn inner_count(nums1: Seq, nums2: Seq, k: int, i: int, j: int) -> int\n decreases nums2.len() - j,\n {\n if j >= nums2.len() {\n 0\n } else {\n (if nums1[i] as int % (nums2[j] as int * k) == 0 { 1int } else { 0int })\n + Self::inner_count(nums1, nums2, k, i, j + 1)\n }\n }\n\n pub open spec fn good_pair_count(nums1: Seq, nums2: Seq, k: int, i: int) -> int\n decreases nums1.len() - i,\n {\n if i >= nums1.len() {\n 0\n } else {\n Self::inner_count(nums1, nums2, k, i, 0) + Self::good_pair_count(nums1, nums2, k, i + 1)\n }\n }\n\n proof fn lemma_inner_count_bound(nums1: Seq, nums2: Seq, k: int, i: int, j: int)\n requires\n 0 <= i < nums1.len(),\n 0 <= j <= nums2.len(),\n ensures\n 0 <= Self::inner_count(nums1, nums2, k, i, j) <= nums2.len() - j,\n decreases nums2.len() - j,\n {\n if j < nums2.len() {\n Self::lemma_inner_count_bound(nums1, nums2, k, i, j + 1);\n }\n }\n\n proof fn lemma_good_pair_count_bound(nums1: Seq, nums2: Seq, k: int, i: int)\n requires\n 0 <= i <= nums1.len(),\n ensures\n 0 <= Self::good_pair_count(nums1, nums2, k, i) <= (nums1.len() - i) * nums2.len(),\n decreases nums1.len() - i,\n {\n if i < nums1.len() {\n Self::lemma_inner_count_bound(nums1, nums2, k, i, 0);\n Self::lemma_good_pair_count_bound(nums1, nums2, k, i + 1);\n assert((nums1.len() - i) * nums2.len() == nums2.len() + (nums1.len() - i - 1) * nums2.len()) by (nonlinear_arith)\n requires i < nums1.len() {}\n }\n }\n\n pub fn number_of_pairs(nums1: Vec, nums2: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums1.len() <= 50,\n 1 <= nums2.len() <= 50,\n forall|i: int| 0 <= i < nums1.len() ==> 1 <= #[trigger] nums1[i] <= 50,\n forall|j: int| 0 <= j < nums2.len() ==> 1 <= #[trigger] nums2[j] <= 50,\n 1 <= k <= 50,\n ensures\n res as int == Self::good_pair_count(nums1@, nums2@, k as int, 0),\n 0 <= res <= nums1.len() * nums2.len(),\n {\n let mut count: i32 = 0;\n let n = nums1.len();\n let m = nums2.len();\n\n proof {\n Self::lemma_good_pair_count_bound(nums1@, nums2@, k as int, 0);\n assert(nums1.len() as int * nums2.len() as int <= 2500) by (nonlinear_arith)\n requires 1 <= nums1.len() <= 50, 1 <= nums2.len() <= 50 {}\n }\n\n for i in 0..n\n invariant\n n == nums1.len(),\n m == nums2.len(),\n 1 <= n <= 50,\n 1 <= m <= 50,\n forall|ii: int| 0 <= ii < nums1.len() ==> 1 <= #[trigger] nums1[ii] <= 50,\n forall|jj: int| 0 <= jj < nums2.len() ==> 1 <= #[trigger] nums2[jj] <= 50,\n 1 <= k <= 50,\n count as int == Self::good_pair_count(nums1@, nums2@, k as int, 0)\n - Self::good_pair_count(nums1@, nums2@, k as int, i as int),\n 0 <= count <= 2500,\n Self::good_pair_count(nums1@, nums2@, k as int, 0) <= 2500,\n {\n let mut inner: i32 = 0;\n for j in 0..m\n invariant\n n == nums1.len(),\n m == nums2.len(),\n 1 <= n <= 50,\n 1 <= m <= 50,\n 0 <= i < n,\n forall|ii: int| 0 <= ii < nums1.len() ==> 1 <= #[trigger] nums1[ii] <= 50,\n forall|jj: int| 0 <= jj < nums2.len() ==> 1 <= #[trigger] nums2[jj] <= 50,\n 1 <= k <= 50,\n inner as int == Self::inner_count(nums1@, nums2@, k as int, i as int, 0)\n - Self::inner_count(nums1@, nums2@, k as int, i as int, j as int),\n 0 <= inner <= j,\n {\n proof {\n Self::lemma_inner_count_bound(nums1@, nums2@, k as int, i as int, j as int + 1);\n assert(nums2@[j as int] as int * (k as int) >= 1) by (nonlinear_arith)\n requires 1 <= nums2@[j as int] <= 50, 1 <= k <= 50 {}\n assert(nums2@[j as int] as int * (k as int) <= 2500) by (nonlinear_arith)\n requires 1 <= nums2@[j as int] <= 50, 1 <= k <= 50 {}\n }\n if nums1[i] % (nums2[j] * k) == 0 {\n inner += 1;\n }\n }\n\n proof {\n Self::lemma_inner_count_bound(nums1@, nums2@, k as int, i as int, 0);\n Self::lemma_good_pair_count_bound(nums1@, nums2@, k as int, i as int + 1);\n }\n\n count += inner;\n }\n count\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3162/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3162/tests/mutated_testcases.jsonl"} {"problem": "lc3175", "family": "leetcode", "description": "# Find The First Player to win K Games in a Row\n\nA competition consists of `n` players numbered from `0` to `n - 1`.\n\nYou are given an integer array `skills` of size `n` and a **positive** integer `k`, where `skills[i]` is the skill level of player `i`. All integers in `skills` are **unique**.\n\nAll players are standing in a queue in order from player `0` to player `n - 1`.\n\nThe competition process is as follows:\n\n\t- The first two players in the queue play a game, and the player with the **higher** skill level wins.\n\n\t- After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.\n\nThe winner of the competition is the **first** player who wins `k` games **in a row**.\n\nReturn the initial index of the *winning* player.\n\n \n\nExample 1:\n\n**Input:** skills = [4,2,6,3,9], k = 2\n\n**Output:** 2\n\n**Explanation:**\n\nInitially, the queue of players is `[0,1,2,3,4]`. The following process happens:\n\n\t- Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is `[0,2,3,4,1]`.\n\n\t- Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is `[2,3,4,1,0]`.\n\n\t- Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is `[2,4,1,0,3]`.\n\nPlayer 2 won `k = 2` games in a row, so the winner is player 2.\n\nExample 2:\n\n**Input:** skills = [2,5,4], k = 3\n\n**Output:** 1\n\n**Explanation:**\n\nInitially, the queue of players is `[0,1,2]`. The following process happens:\n\n\t- Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`.\n\n\t- Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is `[1,0,2]`.\n\n\t- Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`.\n\nPlayer 1 won `k = 3` games in a row, so the winner is player 1.\n\n \n\n**Constraints:**\n\n\t- `n == skills.length`\n\n\t- `2 <= n <= 10^5`\n\n\t- `1 <= k <= 10^9`\n\n\t- `1 <= skills[i] <= 10^6`\n\n\t- All integers in `skills` are unique.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_winning_player(skills: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Simulation\nAcceptance rate: 40.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_winner_spec(skills: Seq, k: int, winner: int) -> bool {\n &&& 2 <= skills.len() <= 100000\n &&& 1 <= k <= 1_000_000_000\n &&& 0 <= winner < skills.len()\n &&& forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000\n &&& forall |i: int, j: int| 0 <= i < j < skills.len() ==> skills[i] != skills[j]\n }\n\n pub open spec fn winner_from(skills: Seq, k: int, i: int, cur: int, win: int) -> int\n decreases skills.len() - i\n {\n if win >= k {\n cur\n } else if i >= skills.len() {\n cur\n } else if skills[i] > skills[cur] {\n Self::winner_from(skills, k, i + 1, i, 1)\n } else {\n Self::winner_from(skills, k, i + 1, cur, win + 1)\n }\n }\n\n pub open spec fn winner(skills: Seq, k: int) -> int {\n Self::winner_from(skills, k, 1, 0, 0)\n }\n\n pub fn find_winning_player(skills: Vec, k: i32) -> (result: i32)\n requires\n 2 <= skills.len() <= 100000,\n 1 <= k <= 1_000_000_000,\n forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000,\n forall |i: int, j: int| 0 <= i < j < skills.len() ==> skills[i] != skills[j],\n ensures\n Self::valid_winner_spec(skills@, k as int, result as int),\n result as int == Self::winner(skills@, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_winning_player(skills: Vec, k: i32) -> i32 {\n let n = skills.len();\n let mut champ = 0usize;\n let mut win = 0i64;\n let mut j = 1usize;\n while j < n && win < k as i64 {\n if skills[j] > skills[champ] {\n champ = j;\n win = 0;\n }\n win = win.checked_add(1).unwrap_or(win);\n j += 1;\n }\n champ as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_winner_spec(skills: Seq, k: int, winner: int) -> bool {\n &&& 2 <= skills.len() <= 100000\n &&& 1 <= k <= 1_000_000_000\n &&& 0 <= winner < skills.len()\n &&& forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000\n &&& forall |i: int, j: int| 0 <= i < j < skills.len() ==> skills[i] != skills[j]\n }\n\n pub open spec fn winner_from(skills: Seq, k: int, i: int, cur: int, win: int) -> int\n decreases skills.len() - i\n {\n if win >= k {\n cur\n } else if i >= skills.len() {\n cur\n } else if skills[i] > skills[cur] {\n Self::winner_from(skills, k, i + 1, i, 1)\n } else {\n Self::winner_from(skills, k, i + 1, cur, win + 1)\n }\n }\n\n pub open spec fn winner(skills: Seq, k: int) -> int {\n Self::winner_from(skills, k, 1, 0, 0)\n }\n\n pub fn find_winning_player(skills: Vec, k: i32) -> (result: i32)\n requires\n 2 <= skills.len() <= 100000,\n 1 <= k <= 1_000_000_000,\n forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000,\n forall |i: int, j: int| 0 <= i < j < skills.len() ==> skills[i] != skills[j],\n ensures\n Self::valid_winner_spec(skills@, k as int, result as int),\n result as int == Self::winner(skills@, k as int),\n {\n let n = skills.len();\n let mut champ = 0usize;\n let mut win = 0i64;\n let mut j = 1usize;\n while j < n && win < k as i64 {\n if skills[j] > skills[champ] {\n champ = j;\n win = 0;\n }\n win = win.checked_add(1).unwrap_or(win);\n j += 1;\n }\n champ as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_winner_spec(skills: Seq, k: int, winner: int) -> bool {\n &&& 2 <= skills.len() <= 100000\n &&& 1 <= k <= 1_000_000_000\n &&& 0 <= winner < skills.len()\n &&& forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000\n &&& forall |i: int, j: int| 0 <= i < j < skills.len() ==> skills[i] != skills[j]\n }\n\n pub open spec fn winner_from(skills: Seq, k: int, i: int, cur: int, win: int) -> int\n decreases skills.len() - i\n {\n if win >= k {\n cur\n } else if i >= skills.len() {\n cur\n } else if skills[i] > skills[cur] {\n Self::winner_from(skills, k, i + 1, i, 1)\n } else {\n Self::winner_from(skills, k, i + 1, cur, win + 1)\n }\n }\n\n pub open spec fn winner(skills: Seq, k: int) -> int {\n Self::winner_from(skills, k, 1, 0, 0)\n }\n\n pub fn find_winning_player(skills: Vec, k: i32) -> (result: i32)\n requires\n 2 <= skills.len() <= 100000,\n 1 <= k <= 1_000_000_000,\n forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000,\n forall |i: int, j: int| 0 <= i < j < skills.len() ==> skills[i] != skills[j],\n ensures\n Self::valid_winner_spec(skills@, k as int, result as int),\n result as int == Self::winner(skills@, k as int),\n {\n let n = skills.len();\n let mut champ = 0usize;\n let mut win = 0i64;\n let mut j = 1usize;\n while j < n && win < k as i64\n invariant\n n == skills.len(),\n 2 <= n <= 100000,\n 1 <= k <= 1_000_000_000,\n 0 <= champ < n,\n 1 <= j <= n,\n 0 <= win <= k as i64,\n forall |i: int| 0 <= i < skills.len() ==> 1 <= #[trigger] skills[i] <= 1_000_000,\n forall |i: int, j2: int| 0 <= i < j2 < skills.len() ==> skills[i] != skills[j2],\n Self::winner(skills@, k as int)\n == Self::winner_from(skills@, k as int, j as int, champ as int, win as int),\n decreases n - j,\n {\n let ghost jo = j as int;\n let ghost champo = champ as int;\n let ghost wino = win as int;\n proof {\n reveal_with_fuel(Solution::winner_from, 2);\n assert(wino < k as int);\n assert(jo < skills.len());\n }\n if skills[j] > skills[champ] {\n champ = j;\n win = 0;\n }\n win = win.checked_add(1).unwrap_or(win);\n j += 1;\n }\n proof {\n reveal_with_fuel(Solution::winner_from, 2);\n assert(Self::winner_from(skills@, k as int, j as int, champ as int, win as int)\n == champ as int);\n }\n champ as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3175/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3175/tests/mutated_testcases.jsonl"} {"problem": "lc3178", "family": "leetcode", "description": "# Find the Child Who Has the Ball After K Seconds\n\nYou are given two positive integers `n` and `k`.\n\nThere are `n` children labeled from `0` to `n - 1`, standing in a line from left to right.\n\nInitially, child `0` has a ball, and the ball is passed to the right. Every second, the current child passes the ball to an adjacent child. When the ball reaches either end (`0` or `n - 1`), the passing direction reverses.\n\nReturn the label of the child holding the ball after `k` seconds.\n\n## Example 1\n\n> **Input:** `n = 3`, `k = 5` \n> **Output:** `1`\n\n## Example 2\n\n> **Input:** `n = 5`, `k = 6` \n> **Output:** `2`\n\n## Example 3\n\n> **Input:** `n = 4`, `k = 2` \n> **Output:** `2`\n\n## Constraints\n\n- `2 <= n <= 50`\n- `1 <= k <= 50`\n\n**Note:** This is the same problem as LeetCode 2582 (Pass the Pillow).\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn number_of_child(n: i32, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Simulation\nAcceptance rate: 62.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn number_of_child_spec(n: int, k: int) -> int {\n let m = 2 * n - 2;\n let t = k % m;\n if t < n { t } else { 2 * n - t - 2 }\n }\n\n pub fn number_of_child(n: i32, k: i32) -> (result: i32)\n requires\n 2 <= n <= 50,\n 1 <= k <= 50,\n ensures\n result as int == Self::number_of_child_spec(n as int, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn number_of_child(n: i32, k: i32) -> i32 {\n let m = 2 * n - 2;\n let t = k % m;\n if t < n {\n t\n } else {\n 2 * n - t - 2\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn number_of_child_spec(n: int, k: int) -> int {\n let m = 2 * n - 2;\n let t = k % m;\n if t < n { t } else { 2 * n - t - 2 }\n }\n\n pub fn number_of_child(n: i32, k: i32) -> (result: i32)\n requires\n 2 <= n <= 50,\n 1 <= k <= 50,\n ensures\n result as int == Self::number_of_child_spec(n as int, k as int),\n {\n let m = 2 * n - 2;\n let t = k % m;\n if t < n {\n t\n } else {\n 2 * n - t - 2\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn number_of_child_spec(n: int, k: int) -> int {\n let m = 2 * n - 2;\n let t = k % m;\n if t < n { t } else { 2 * n - t - 2 }\n }\n\n pub fn number_of_child(n: i32, k: i32) -> (result: i32)\n requires\n 2 <= n <= 50,\n 1 <= k <= 50,\n ensures\n result as int == Self::number_of_child_spec(n as int, k as int),\n {\n let m = 2 * n - 2;\n let t = k % m;\n if t < n {\n t\n } else {\n 2 * n - t - 2\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3178/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3178/tests/mutated_testcases.jsonl"} {"problem": "lc319", "family": "leetcode", "description": "# Bulb Switcher\n\nThere are `n` bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.\n\nOn the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the $i$-th round, you toggle every $i$-th bulb. For the $n$-th round, you only toggle the last bulb.\n\nReturn the number of bulbs that are on after $n$ rounds.\n\n## Example 1:\n\n> **Input:** n = 3\n> **Output:** 1\n> **Explanation:** At first, the three bulbs are [off, off, off]. After the first round, the three bulbs are [on, on, on]. After the second round, the three bulbs are [on, off, on]. After the third round, the three bulbs are [on, off, off]. So you should return 1 because only one bulb is on.\n\n## Example 2:\n\n> **Input:** n = 0\n> **Output:** 0\n\n## Example 3:\n\n> **Input:** n = 1\n> **Output:** 1\n\n## Constraints:\n\n- $0 \\le n \\le 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn bulb_switch(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nMath, Brainteaser\nAcceptance rate: 55.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn bulb_switch(n: i32) -> (res: i32)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n res >= 0,\n res * res <= n,\n (res + 1) * (res + 1) > n,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn bulb_switch(n: i32) -> i32 {\n let mut i: i32 = 0;\n loop\n {\n if (i as i64 + 1) * (i as i64 + 1) > n as i64 {\n break;\n }\n i += 1;\n }\n i\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn bulb_switch(n: i32) -> (res: i32)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n res >= 0,\n res * res <= n,\n (res + 1) * (res + 1) > n,\n {\n let mut i: i32 = 0;\n loop\n {\n if (i as i64 + 1) * (i as i64 + 1) > n as i64 {\n break;\n }\n i += 1;\n }\n i\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nproof fn lemma_sqrt_bound(i: int, n: int)\n requires\n 0 <= i,\n i * i <= n,\n n <= 1_000_000_000,\n ensures\n i <= 31623,\n{\n if i > 31623 {\n assert(i * i >= 31624 * 31624) by (nonlinear_arith)\n requires i > 31623;\n }\n}\n\nproof fn lemma_sq_i64_bound(i: i64)\n requires\n 0 <= i <= 31624,\n ensures\n i * i <= 1_000_100_000i64,\n i * i >= 0,\n{\n assert(i * i <= 31624 * 31624) by (nonlinear_arith)\n requires 0 <= i <= 31624;\n assert(31624i64 * 31624i64 == 1_000_077_376i64) by (nonlinear_arith);\n}\n\nimpl Solution {\n pub fn bulb_switch(n: i32) -> (res: i32)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n res >= 0,\n res * res <= n,\n (res + 1) * (res + 1) > n,\n {\n let mut i: i32 = 0;\n let ghost mut found = false;\n loop\n invariant\n 0 <= n <= 1_000_000_000,\n 0 <= i <= 31623,\n i as int * i as int <= n as int,\n found ==> (i as int + 1) * (i as int + 1) > n as int,\n ensures\n 0 <= i <= 31623,\n i as int * i as int <= n as int,\n (i as int + 1) * (i as int + 1) > n as int,\n decreases 31624 - i as int,\n {\n proof {\n lemma_sq_i64_bound((i + 1) as i64);\n }\n if (i as i64 + 1) * (i as i64 + 1) > n as i64 {\n proof { found = true; }\n break;\n }\n proof {\n lemma_sqrt_bound(i as int + 1, n as int);\n }\n i += 1;\n }\n i\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc319/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc319/tests/mutated_testcases.jsonl"} {"problem": "lc3192", "family": "leetcode", "description": "# Minimum Operations to Make Binary Array Elements Equal to One II\n\nYou are given a binary array `nums`.\n\nYou can do the following operation on the array **any** number of times (possibly zero):\n\n\t- Choose **any** index `i` from the array and **flip** **all** the elements from index `i` to the end of the array.\n\n**Flipping** an element means changing its value from 0 to 1, and from 1 to 0.\n\nReturn the **minimum** number of operations required to make all elements in `nums` equal to 1.\n\n \n\nExample 1:\n\n**Input:** nums = [0,1,1,0,1]\n\n**Output:** 4\n\n**Explanation:**\n\nWe can do the following operations:\n\n\t- Choose the index `i = 1`. The resulting array will be `nums = [0,**0**,**0**,**1**,**0**]`.\n\n\t- Choose the index `i = 0`. The resulting array will be `nums = [**1**,**1**,**1**,**0**,**1**]`.\n\n\t- Choose the index `i = 4`. The resulting array will be `nums = [1,1,1,0,**0**]`.\n\n\t- Choose the index `i = 3`. The resulting array will be `nums = [1,1,1,**1**,**1**]`.\n\nExample 2:\n\n**Input:** nums = [1,0,0,0]\n\n**Output:** 1\n\n**Explanation:**\n\nWe can do the following operation:\n\n\t- Choose the index `i = 1`. The resulting array will be `nums = [1,**1**,**1**,**1**]`.\n\n \n\n**Constraints:**\n\n\t- `1 <= nums.length <= 10^5`\n\n\t- `0 <= nums[i] <= 1`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming, Greedy\nAcceptance rate: 65.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_ops_from(nums: Seq, i: int, flip: int) -> int\n decreases if i < nums.len() { nums.len() - i } else { 0 },\n {\n if i < nums.len() {\n if nums[i] as int == flip {\n 1 + Self::min_ops_from(nums, i + 1, 1 - flip)\n } else {\n Self::min_ops_from(nums, i + 1, flip)\n }\n } else {\n 0\n }\n }\n\n pub open spec fn min_operations_spec(nums: Seq, result: int) -> bool {\n &&& 1 <= nums.len() <= 100000\n &&& forall |i: int| 0 <= i < nums.len() ==> (#[trigger] nums[i] == 0 || nums[i] == 1)\n &&& result == Self::min_ops_from(nums, 0, 0)\n }\n\n pub fn min_operations(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> (#[trigger] nums[i] == 0 || nums[i] == 1),\n ensures\n Self::min_operations_spec(nums@, result as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_operations(nums: Vec) -> i32 {\n let n = nums.len();\n let mut ans = 0i32;\n let mut flip = 0i32;\n let mut i = 0usize;\n while i < n {\n if nums[i] == flip {\n ans = ans + 1;\n flip = if flip == 0 { 1 } else { 0 };\n }\n i += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_ops_from(nums: Seq, i: int, flip: int) -> int\n decreases if i < nums.len() { nums.len() - i } else { 0 },\n {\n if i < nums.len() {\n if nums[i] as int == flip {\n 1 + Self::min_ops_from(nums, i + 1, 1 - flip)\n } else {\n Self::min_ops_from(nums, i + 1, flip)\n }\n } else {\n 0\n }\n }\n\n pub open spec fn min_operations_spec(nums: Seq, result: int) -> bool {\n &&& 1 <= nums.len() <= 100000\n &&& forall |i: int| 0 <= i < nums.len() ==> (#[trigger] nums[i] == 0 || nums[i] == 1)\n &&& result == Self::min_ops_from(nums, 0, 0)\n }\n\n pub fn min_operations(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> (#[trigger] nums[i] == 0 || nums[i] == 1),\n ensures\n Self::min_operations_spec(nums@, result as int),\n {\n let n = nums.len();\n let mut ans = 0i32;\n let mut flip = 0i32;\n let mut i = 0usize;\n while i < n {\n if nums[i] == flip {\n ans = ans + 1;\n flip = if flip == 0 { 1 } else { 0 };\n }\n i += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_ops_from(nums: Seq, i: int, flip: int) -> int\n decreases if i < nums.len() { nums.len() - i } else { 0 },\n {\n if i < nums.len() {\n if nums[i] as int == flip {\n 1 + Self::min_ops_from(nums, i + 1, 1 - flip)\n } else {\n Self::min_ops_from(nums, i + 1, flip)\n }\n } else {\n 0\n }\n }\n\n pub open spec fn min_operations_spec(nums: Seq, result: int) -> bool {\n &&& 1 <= nums.len() <= 100000\n &&& forall |i: int| 0 <= i < nums.len() ==> (#[trigger] nums[i] == 0 || nums[i] == 1)\n &&& result == Self::min_ops_from(nums, 0, 0)\n }\n\n pub fn min_operations(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> (#[trigger] nums[i] == 0 || nums[i] == 1),\n ensures\n Self::min_operations_spec(nums@, result as int),\n {\n let n = nums.len();\n let mut ans = 0i32;\n let mut flip = 0i32;\n let mut i = 0usize;\n while i < n\n invariant\n 1 <= n <= 100000,\n n == nums.len(),\n 0 <= i <= n,\n 0 <= ans <= i as int,\n flip == 0 || flip == 1,\n ans as int + Self::min_ops_from(nums@, i as int, flip as int) == Self::min_ops_from(nums@, 0, 0),\n decreases n - i,\n {\n assert(i < n);\n if nums[i] == flip {\n assert(Self::min_ops_from(nums@, i as int, flip as int) == 1 + Self::min_ops_from(nums@, i as int + 1, 1 - (flip as int)));\n ans = ans + 1;\n flip = if flip == 0 { 1 } else { 0 };\n } else {\n assert(Self::min_ops_from(nums@, i as int, flip as int) == Self::min_ops_from(nums@, i as int + 1, flip as int));\n }\n i += 1;\n }\n assert(Self::min_ops_from(nums@, n as int, flip as int) == 0);\n assert(ans as int == Self::min_ops_from(nums@, 0, 0));\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3192/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3192/tests/mutated_testcases.jsonl"} {"problem": "lc3218", "family": "leetcode", "description": "# Minimum Cost for Cutting Cake I\n\nThere is an `m x n` cake that needs to be cut into `1 x 1` pieces.\n\nYou are given integers `m`, `n`, and two arrays:\n\n\t- `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.\n\n\t- `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.\n\nIn one operation, you can choose any piece of cake that is not yet a `1 x 1` square and perform one of the following cuts:\n\n\t- Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.\n\n\t- Cut along a vertical line `j` at a cost of `verticalCut[j]`.\n\nAfter the cut, the piece of cake is divided into two distinct pieces.\n\nThe cost of a cut depends only on the initial cost of the line and does not change.\n\nReturn the **minimum** total cost to cut the entire cake into `1 x 1` pieces.\n\n \n\nExample 1:\n\n**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]\n\n**Output:** 13\n\n**Explanation:**\n\n\t- Perform a cut on the vertical line 0 with cost 5, current total cost is 5.\n\n\t- Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.\n\n\t- Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.\n\n\t- Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.\n\n\t- Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.\n\nThe total cost is `5 + 1 + 1 + 3 + 3 = 13`.\n\nExample 2:\n\n**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]\n\n**Output:** 15\n\n**Explanation:**\n\n\t- Perform a cut on the horizontal line 0 with cost 7.\n\n\t- Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.\n\n\t- Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.\n\nThe total cost is `7 + 4 + 4 = 15`.\n\n \n\n**Constraints:**\n\n\t- `1 <= m, n <= 20`\n\n\t- `horizontalCut.length == m - 1`\n\n\t- `verticalCut.length == n - 1`\n\n\t- `1 <= horizontalCut[i], verticalCut[i] <= 10^3`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_cost(m: i32, n: i32, horizontal_cut: Vec, vertical_cut: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Two Pointers, Dynamic Programming, Greedy, Sorting\nAcceptance rate: 58.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_cost_piece(horizontal: int, vertical: Seq, end: int) -> int\n recommends\n 0 <= end <= vertical.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::min_cost_piece(horizontal, vertical, end - 1)\n + if horizontal <= vertical[end - 1] as int {\n horizontal\n } else {\n vertical[end - 1] as int\n }\n }\n }\n\n pub open spec fn cross_cost(horizontal: Seq, h_end: int, vertical: Seq) -> int\n recommends\n 0 <= h_end <= horizontal.len(),\n decreases h_end,\n {\n if h_end <= 0 {\n 0\n } else {\n Self::cross_cost(horizontal, h_end - 1, vertical)\n + Self::min_cost_piece(horizontal[h_end - 1] as int, vertical, vertical.len() as int)\n }\n }\n\n pub open spec fn sum_prefix(values: Seq, end: int) -> int\n recommends\n 0 <= end <= values.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::sum_prefix(values, end - 1) + values[end - 1] as int\n }\n }\n\n pub open spec fn exact_cost(horizontal: Seq, vertical: Seq) -> int {\n Self::sum_prefix(horizontal, horizontal.len() as int)\n + Self::sum_prefix(vertical, vertical.len() as int)\n + Self::cross_cost(horizontal, horizontal.len() as int, vertical)\n }\n\n pub open spec fn minimum_cost_spec(m: i32, n: i32, horizontal_cut: Seq, vertical_cut: Seq, result: int) -> bool {\n &&& 1 <= m <= 20\n &&& 1 <= n <= 20\n &&& horizontal_cut.len() == m - 1\n &&& vertical_cut.len() == n - 1\n &&& forall |i: int| 0 <= i < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[i] <= 1000\n &&& forall |j: int| 0 <= j < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[j] <= 1000\n &&& result == Self::exact_cost(horizontal_cut, vertical_cut)\n }\n\n pub fn minimum_cost(m: i32, n: i32, horizontal_cut: Vec, vertical_cut: Vec) -> (result: i32)\n requires\n 1 <= m <= 20,\n 1 <= n <= 20,\n horizontal_cut.len() == m - 1,\n vertical_cut.len() == n - 1,\n forall |i: int| 0 <= i < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[i] <= 1000,\n forall |j: int| 0 <= j < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[j] <= 1000,\n ensures\n result as int == Self::exact_cost(horizontal_cut@, vertical_cut@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_cost(m: i32, n: i32, horizontal_cut: Vec, vertical_cut: Vec) -> i32 {\n let hm = horizontal_cut.len();\n let vn = vertical_cut.len();\n let mut res = 0i32;\n\n let mut i = 0usize;\n while i < hm {\n res = res.checked_add(horizontal_cut[i]).unwrap_or(res);\n i += 1;\n }\n\n let mut j = 0usize;\n while j < vn {\n res = res.checked_add(vertical_cut[j]).unwrap_or(res);\n j += 1;\n }\n\n let mut a = 0usize;\n while a < hm {\n let mut b = 0usize;\n while b < vn {\n let add = if horizontal_cut[a] <= vertical_cut[b] { horizontal_cut[a] } else { vertical_cut[b] };\n res = res.checked_add(add).unwrap_or(res);\n b += 1;\n }\n a += 1;\n }\n\n if res < 0 { 0 } else { res }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_cost_piece(horizontal: int, vertical: Seq, end: int) -> int\n recommends\n 0 <= end <= vertical.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::min_cost_piece(horizontal, vertical, end - 1)\n + if horizontal <= vertical[end - 1] as int {\n horizontal\n } else {\n vertical[end - 1] as int\n }\n }\n }\n\n pub open spec fn cross_cost(horizontal: Seq, h_end: int, vertical: Seq) -> int\n recommends\n 0 <= h_end <= horizontal.len(),\n decreases h_end,\n {\n if h_end <= 0 {\n 0\n } else {\n Self::cross_cost(horizontal, h_end - 1, vertical)\n + Self::min_cost_piece(horizontal[h_end - 1] as int, vertical, vertical.len() as int)\n }\n }\n\n pub open spec fn sum_prefix(values: Seq, end: int) -> int\n recommends\n 0 <= end <= values.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::sum_prefix(values, end - 1) + values[end - 1] as int\n }\n }\n\n pub open spec fn exact_cost(horizontal: Seq, vertical: Seq) -> int {\n Self::sum_prefix(horizontal, horizontal.len() as int)\n + Self::sum_prefix(vertical, vertical.len() as int)\n + Self::cross_cost(horizontal, horizontal.len() as int, vertical)\n }\n\n\n\n\n pub open spec fn minimum_cost_spec(m: i32, n: i32, horizontal_cut: Seq, vertical_cut: Seq, result: int) -> bool {\n &&& 1 <= m <= 20\n &&& 1 <= n <= 20\n &&& horizontal_cut.len() == m - 1\n &&& vertical_cut.len() == n - 1\n &&& forall |i: int| 0 <= i < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[i] <= 1000\n &&& forall |j: int| 0 <= j < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[j] <= 1000\n &&& result == Self::exact_cost(horizontal_cut, vertical_cut)\n }\n\n pub fn minimum_cost(m: i32, n: i32, horizontal_cut: Vec, vertical_cut: Vec) -> (result: i32)\n requires\n 1 <= m <= 20,\n 1 <= n <= 20,\n horizontal_cut.len() == m - 1,\n vertical_cut.len() == n - 1,\n forall |i: int| 0 <= i < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[i] <= 1000,\n forall |j: int| 0 <= j < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[j] <= 1000,\n ensures\n result as int == Self::exact_cost(horizontal_cut@, vertical_cut@),\n {\n let hm = horizontal_cut.len();\n let vn = vertical_cut.len();\n let mut res = 0i32;\n\n let mut i = 0usize;\n while i < hm\n decreases hm - i,\n {\n if false {\n res = res.checked_add(horizontal_cut[i]).unwrap_or(res);\n }\n res = res + horizontal_cut[i];\n i += 1;\n }\n\n let mut j = 0usize;\n while j < vn\n decreases vn - j,\n {\n if false {\n res = res.checked_add(vertical_cut[j]).unwrap_or(res);\n }\n res = res + vertical_cut[j];\n j += 1;\n }\n\n let mut a = 0usize;\n while a < hm\n decreases hm - a,\n {\n let mut b = 0usize;\n while b < vn\n decreases vn - b,\n {\n let add = if horizontal_cut[a] <= vertical_cut[b] { horizontal_cut[a] } else { vertical_cut[b] };\n if false {\n res = res.checked_add(add).unwrap_or(res);\n }\n res = res + add;\n b += 1;\n }\n a += 1;\n }\n\n if false {\n if res < 0 { 0 } else { res }\n } else {\n res\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_cost_piece(horizontal: int, vertical: Seq, end: int) -> int\n recommends\n 0 <= end <= vertical.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::min_cost_piece(horizontal, vertical, end - 1)\n + if horizontal <= vertical[end - 1] as int {\n horizontal\n } else {\n vertical[end - 1] as int\n }\n }\n }\n\n pub open spec fn cross_cost(horizontal: Seq, h_end: int, vertical: Seq) -> int\n recommends\n 0 <= h_end <= horizontal.len(),\n decreases h_end,\n {\n if h_end <= 0 {\n 0\n } else {\n Self::cross_cost(horizontal, h_end - 1, vertical)\n + Self::min_cost_piece(horizontal[h_end - 1] as int, vertical, vertical.len() as int)\n }\n }\n\n pub open spec fn sum_prefix(values: Seq, end: int) -> int\n recommends\n 0 <= end <= values.len(),\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::sum_prefix(values, end - 1) + values[end - 1] as int\n }\n }\n\n pub open spec fn exact_cost(horizontal: Seq, vertical: Seq) -> int {\n Self::sum_prefix(horizontal, horizontal.len() as int)\n + Self::sum_prefix(vertical, vertical.len() as int)\n + Self::cross_cost(horizontal, horizontal.len() as int, vertical)\n }\n\n proof fn lemma_sum_prefix_bounds(values: Seq, end: int)\n requires\n 0 <= end <= values.len(),\n forall |k: int| 0 <= k < values.len() ==> 1 <= #[trigger] values[k] <= 1000,\n ensures\n 0 <= Self::sum_prefix(values, end) <= end * 1000,\n decreases end,\n {\n if end > 0 {\n Self::lemma_sum_prefix_bounds(values, end - 1);\n assert(1 <= values[end - 1] <= 1000);\n }\n }\n\n proof fn lemma_min_cost_piece_bounds(horizontal: int, vertical: Seq, end: int)\n requires\n 1 <= horizontal <= 1000,\n 0 <= end <= vertical.len(),\n forall |k: int| 0 <= k < vertical.len() ==> 1 <= #[trigger] vertical[k] <= 1000,\n ensures\n 0 <= Self::min_cost_piece(horizontal, vertical, end) <= end * 1000,\n decreases end,\n {\n if end > 0 {\n Self::lemma_min_cost_piece_bounds(horizontal, vertical, end - 1);\n assert(1 <= vertical[end - 1] <= 1000);\n }\n }\n\n proof fn lemma_cross_cost_bounds(horizontal: Seq, h_end: int, vertical: Seq)\n requires\n 0 <= h_end <= horizontal.len(),\n forall |k: int| 0 <= k < horizontal.len() ==> 1 <= #[trigger] horizontal[k] <= 1000,\n forall |k: int| 0 <= k < vertical.len() ==> 1 <= #[trigger] vertical[k] <= 1000,\n ensures\n 0 <= Self::cross_cost(horizontal, h_end, vertical) <= h_end * vertical.len() * 1000,\n decreases h_end,\n {\n if h_end > 0 {\n Self::lemma_cross_cost_bounds(horizontal, h_end - 1, vertical);\n assert(1 <= horizontal[h_end - 1] <= 1000);\n Self::lemma_min_cost_piece_bounds(horizontal[h_end - 1] as int, vertical, vertical.len() as int);\n assert(Self::cross_cost(horizontal, h_end, vertical)\n == Self::cross_cost(horizontal, h_end - 1, vertical)\n + Self::min_cost_piece(horizontal[h_end - 1] as int, vertical, vertical.len() as int));\n assert(0 <= Self::cross_cost(horizontal, h_end - 1, vertical));\n assert(Self::cross_cost(horizontal, h_end - 1, vertical) <= (h_end - 1) * (vertical.len() as int) * 1000);\n assert(0 <= Self::min_cost_piece(horizontal[h_end - 1] as int, vertical, vertical.len() as int));\n assert(Self::min_cost_piece(horizontal[h_end - 1] as int, vertical, vertical.len() as int)\n <= (vertical.len() as int) * 1000);\n assert(Self::cross_cost(horizontal, h_end, vertical)\n <= (h_end - 1) * (vertical.len() as int) * 1000 + (vertical.len() as int) * 1000);\n assert((h_end - 1) * (vertical.len() as int) * 1000 + (vertical.len() as int) * 1000\n == h_end * (vertical.len() as int) * 1000) by (nonlinear_arith) {}\n }\n }\n\n pub open spec fn minimum_cost_spec(m: i32, n: i32, horizontal_cut: Seq, vertical_cut: Seq, result: int) -> bool {\n &&& 1 <= m <= 20\n &&& 1 <= n <= 20\n &&& horizontal_cut.len() == m - 1\n &&& vertical_cut.len() == n - 1\n &&& forall |i: int| 0 <= i < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[i] <= 1000\n &&& forall |j: int| 0 <= j < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[j] <= 1000\n &&& result == Self::exact_cost(horizontal_cut, vertical_cut)\n }\n\n pub fn minimum_cost(m: i32, n: i32, horizontal_cut: Vec, vertical_cut: Vec) -> (result: i32)\n requires\n 1 <= m <= 20,\n 1 <= n <= 20,\n horizontal_cut.len() == m - 1,\n vertical_cut.len() == n - 1,\n forall |i: int| 0 <= i < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[i] <= 1000,\n forall |j: int| 0 <= j < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[j] <= 1000,\n ensures\n result as int == Self::exact_cost(horizontal_cut@, vertical_cut@),\n {\n let hm = horizontal_cut.len();\n let vn = vertical_cut.len();\n let mut res = 0i32;\n\n let mut i = 0usize;\n while i < hm\n invariant\n hm == horizontal_cut.len(),\n vn == vertical_cut.len(),\n 0 <= i <= hm,\n hm <= 19,\n vn <= 19,\n hm as int == m - 1,\n vn as int == n - 1,\n forall |k: int| 0 <= k < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[k] <= 1000,\n forall |k: int| 0 <= k < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[k] <= 1000,\n res as int == Self::sum_prefix(horizontal_cut@, i as int),\n res >= 0,\n decreases hm - i,\n {\n proof {\n Self::lemma_sum_prefix_bounds(horizontal_cut@, i as int);\n }\n assert(Self::sum_prefix(horizontal_cut@, i as int + 1)\n == Self::sum_prefix(horizontal_cut@, i as int) + horizontal_cut@[i as int] as int);\n assert(horizontal_cut@[i as int] <= 1000);\n assert(i < hm);\n assert(res as int + horizontal_cut@[i as int] as int <= 20000);\n if false {\n res = res.checked_add(horizontal_cut[i]).unwrap_or(res);\n }\n res = res + horizontal_cut[i];\n i += 1;\n }\n\n let mut j = 0usize;\n while j < vn\n invariant\n hm == horizontal_cut.len(),\n vn == vertical_cut.len(),\n 0 <= j <= vn,\n hm <= 19,\n vn <= 19,\n hm as int == m - 1,\n vn as int == n - 1,\n forall |k: int| 0 <= k < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[k] <= 1000,\n forall |k: int| 0 <= k < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[k] <= 1000,\n res as int\n == Self::sum_prefix(horizontal_cut@, hm as int)\n + Self::sum_prefix(vertical_cut@, j as int),\n res >= 0,\n decreases vn - j,\n {\n proof {\n Self::lemma_sum_prefix_bounds(horizontal_cut@, hm as int);\n Self::lemma_sum_prefix_bounds(vertical_cut@, j as int);\n }\n assert(Self::sum_prefix(vertical_cut@, j as int + 1)\n == Self::sum_prefix(vertical_cut@, j as int) + vertical_cut@[j as int] as int);\n assert(vertical_cut@[j as int] <= 1000);\n assert(j < vn);\n assert(res as int + vertical_cut@[j as int] as int <= 39000);\n if false {\n res = res.checked_add(vertical_cut[j]).unwrap_or(res);\n }\n res = res + vertical_cut[j];\n j += 1;\n }\n\n let mut a = 0usize;\n while a < hm\n invariant\n hm == horizontal_cut.len(),\n vn == vertical_cut.len(),\n 0 <= a <= hm,\n hm <= 19,\n vn <= 19,\n hm as int == m - 1,\n vn as int == n - 1,\n forall |k: int| 0 <= k < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[k] <= 1000,\n forall |k: int| 0 <= k < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[k] <= 1000,\n res as int\n == Self::sum_prefix(horizontal_cut@, hm as int)\n + Self::sum_prefix(vertical_cut@, vn as int)\n + Self::cross_cost(horizontal_cut@, a as int, vertical_cut@),\n res >= 0,\n decreases hm - a,\n {\n let mut b = 0usize;\n while b < vn\n invariant\n hm == horizontal_cut.len(),\n vn == vertical_cut.len(),\n 0 <= a < hm,\n 0 <= b <= vn,\n hm <= 19,\n vn <= 19,\n hm as int == m - 1,\n vn as int == n - 1,\n forall |k: int| 0 <= k < horizontal_cut.len() ==> 1 <= #[trigger] horizontal_cut[k] <= 1000,\n forall |k: int| 0 <= k < vertical_cut.len() ==> 1 <= #[trigger] vertical_cut[k] <= 1000,\n res as int\n == Self::sum_prefix(horizontal_cut@, hm as int)\n + Self::sum_prefix(vertical_cut@, vn as int)\n + Self::cross_cost(horizontal_cut@, a as int, vertical_cut@)\n + Self::min_cost_piece(horizontal_cut@[a as int] as int, vertical_cut@, b as int),\n res >= 0,\n decreases vn - b,\n {\n proof {\n Self::lemma_sum_prefix_bounds(horizontal_cut@, hm as int);\n Self::lemma_sum_prefix_bounds(vertical_cut@, vn as int);\n Self::lemma_cross_cost_bounds(horizontal_cut@, a as int, vertical_cut@);\n Self::lemma_min_cost_piece_bounds(horizontal_cut@[a as int] as int, vertical_cut@, b as int);\n }\n let add = if horizontal_cut[a] <= vertical_cut[b] { horizontal_cut[a] } else { vertical_cut[b] };\n assert(Self::min_cost_piece(horizontal_cut@[a as int] as int, vertical_cut@, b as int + 1)\n == Self::min_cost_piece(horizontal_cut@[a as int] as int, vertical_cut@, b as int)\n + if horizontal_cut@[a as int] as int <= vertical_cut@[b as int] as int {\n horizontal_cut@[a as int] as int\n } else {\n vertical_cut@[b as int] as int\n });\n assert(add as int <= 1000);\n assert(Self::sum_prefix(horizontal_cut@, hm as int) <= hm as int * 1000);\n assert(Self::sum_prefix(vertical_cut@, vn as int) <= vn as int * 1000);\n assert(Self::cross_cost(horizontal_cut@, a as int, vertical_cut@) <= a as int * vn as int * 1000);\n assert(Self::min_cost_piece(horizontal_cut@[a as int] as int, vertical_cut@, b as int) <= b as int * 1000);\n assert(a < hm);\n assert(b < vn);\n assert(a + 1 <= hm);\n assert(b + 1 <= vn);\n assert(a <= 18usize);\n assert(b <= 18usize);\n assert(hm as int <= 19);\n assert(vn as int <= 19);\n assert(a as int <= 18);\n assert(b as int <= 18);\n assert(res as int\n <= hm as int * 1000 + vn as int * 1000 + a as int * vn as int * 1000 + b as int * 1000);\n assert(hm as int * 1000 + vn as int * 1000 + a as int * vn as int * 1000 + b as int * 1000 <= 399000)\n by (nonlinear_arith)\n requires\n 0 <= a as int,\n (a as int) < (hm as int),\n (hm as int) <= 19,\n 0 <= b as int,\n (b as int) < (vn as int),\n (vn as int) <= 19,\n {}\n assert(res as int + add as int <= 400000);\n if false {\n res = res.checked_add(add).unwrap_or(res);\n }\n res = res + add;\n b += 1;\n }\n assert(Self::cross_cost(horizontal_cut@, a as int + 1, vertical_cut@)\n == Self::cross_cost(horizontal_cut@, a as int, vertical_cut@)\n + Self::min_cost_piece(horizontal_cut@[a as int] as int, vertical_cut@, vn as int));\n a += 1;\n }\n\n assert(res as int\n == Self::sum_prefix(horizontal_cut@, hm as int)\n + Self::sum_prefix(vertical_cut@, vn as int)\n + Self::cross_cost(horizontal_cut@, hm as int, vertical_cut@));\n proof {\n Self::lemma_sum_prefix_bounds(horizontal_cut@, hm as int);\n Self::lemma_sum_prefix_bounds(vertical_cut@, vn as int);\n Self::lemma_cross_cost_bounds(horizontal_cut@, hm as int, vertical_cut@);\n }\n assert(res >= 0);\n if false {\n if res < 0 { 0 } else { res }\n } else {\n res\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3218/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3218/tests/mutated_testcases.jsonl"} {"problem": "lc3222", "family": "leetcode", "description": "# Find the Winning Player in Coin Game\n\nYou are given two **positive** integers `x` and `y`, denoting the number of coins with values 75 and 10 *respectively*.\n\nAlice and Bob are playing a game. Each turn, starting with **Alice**, the player must pick up coins with a **total** value 115. If the player is unable to do so, they **lose** the game.\n\nReturn the *name* of the player who wins the game if both players play **optimally**.\n\n \n\nExample 1:\n\n**Input:** x = 2, y = 7\n\n**Output:** \"Alice\"\n\n**Explanation:**\n\nThe game ends in a single turn:\n\n\t- Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.\n\nExample 2:\n\n**Input:** x = 4, y = 11\n\n**Output:** \"Bob\"\n\n**Explanation:**\n\nThe game ends in 2 turns:\n\n\t- Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.\n\n\t- Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.\n\n \n\n**Constraints:**\n\n\t- `1 <= x, y <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn winning_player(x: i32, y: i32) -> String {\n \n }\n}\n```\n", "tags": "Easy\nMath, Simulation, Game Theory\nAcceptance rate: 53.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn rounds(x: int, y: int) -> int {\n if x <= y / 4 { x } else { y / 4 }\n }\n\n pub fn winning_player(x: i32, y: i32) -> (result: String)\n requires\n 1 <= x <= 100,\n 1 <= y <= 100,\n ensures\n result@ == if Self::rounds(x as int, y as int) % 2 == 1 {\n \"Alice\"@\n } else {\n \"Bob\"@\n },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn winning_player(x: i32, y: i32) -> String {\n let turns = if x <= y / 4 { x } else { y / 4 };\n if turns % 2 == 1 {\n \"Alice\".to_string()\n } else {\n \"Bob\".to_string()\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn rounds(x: int, y: int) -> int {\n if x <= y / 4 { x } else { y / 4 }\n }\n\n pub fn winning_player(x: i32, y: i32) -> (result: String)\n requires\n 1 <= x <= 100,\n 1 <= y <= 100,\n ensures\n result@ == if Self::rounds(x as int, y as int) % 2 == 1 {\n \"Alice\"@\n } else {\n \"Bob\"@\n },\n {\n let turns = if x <= y / 4 { x } else { y / 4 };\n if turns % 2 == 1 {\n \"Alice\".to_string()\n } else {\n \"Bob\".to_string()\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn rounds(x: int, y: int) -> int {\n if x <= y / 4 { x } else { y / 4 }\n }\n\n pub fn winning_player(x: i32, y: i32) -> (result: String)\n requires\n 1 <= x <= 100,\n 1 <= y <= 100,\n ensures\n result@ == if Self::rounds(x as int, y as int) % 2 == 1 {\n \"Alice\"@\n } else {\n \"Bob\"@\n },\n {\n let turns = if x <= y / 4 { x } else { y / 4 };\n if turns % 2 == 1 {\n \"Alice\".to_string()\n } else {\n \"Bob\".to_string()\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3222/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3222/tests/mutated_testcases.jsonl"} {"problem": "lc3254", "family": "leetcode", "description": "# Find the Power of K-Size Subarrays I\n\nYou are given an array of integers `nums` of length `n` and a positive integer `k`.\n\nThe **power** of an array is defined as:\n\n- Its maximum element if all of its elements are consecutive and sorted in ascending order.\n- `-1` otherwise.\n\nYou need to find the power of all subarrays of `nums` of size `k`.\n\nReturn an integer array `results` of size `n - k + 1`, where `results[i]` is the power of `nums[i..(i + k - 1)]`.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,4,3,2,5], k = 3\n> **Output:** [3,4,-1,-1,-1]\n> **Explanation:**\n> There are 5 subarrays of `nums` of size 3:\n> - `[1,2,3]` has power 3.\n> - `[2,3,4]` has power 4.\n> - `[3,4,3]` is not consecutive.\n> - `[4,3,2]` is not sorted in ascending order.\n> - `[3,2,5]` is not consecutive.\n\n## Example 2:\n\n> **Input:** nums = [2,2,2,2,2], k = 4\n> **Output:** [-1,-1]\n\n## Example 3:\n\n> **Input:** nums = [3,2,3,2,3,2], k = 2\n> **Output:** [-1,3,-1,3,-1]\n\n## Constraints:\n\n- `1 <= n == nums.length <= 500`\n- `1 <= nums[i] <= 10^5`\n- `1 <= k <= n`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn results_array(nums: Vec, k: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nSenior, Array, Sliding Window, Biweekly Contest 137\nAcceptance rate: 62.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn consecutive_step(nums: Seq, i: int) -> bool\n recommends\n 0 <= i + 1 < nums.len(),\n {\n nums[i + 1] == nums[i] + 1\n }\n\n pub open spec fn window_has_power(nums: Seq, start: int, k: int) -> bool\n recommends\n 0 <= start,\n 1 <= k,\n start + k <= nums.len(),\n {\n forall |j: int| start <= j < start + k - 1 ==> #[trigger] Self::consecutive_step(nums, j)\n }\n\n pub open spec fn window_power(nums: Seq, start: int, k: int) -> int\n recommends\n 0 <= start,\n 1 <= k,\n start + k <= nums.len(),\n {\n if Self::window_has_power(nums, start, k) {\n nums[start + k - 1] as int\n } else {\n -1\n }\n }\n\n pub fn results_array(nums: Vec, k: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 500,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result@.len() == nums.len() as int - k as int + 1,\n forall |i: int| 0 <= i < result@.len() ==> #[trigger] result@[i] as int == Self::window_power(nums@, i, k as int),\n {\n }\n}\n\n}", "code": "impl Solution {\n pub fn results_array(nums: Vec, k: i32) -> Vec {\n let n = nums.len();\n let k_usize = k as usize;\n let mut result: Vec = Vec::new();\n let mut run_len: usize = 1;\n if k_usize == 1 {\n result.push(nums[0]);\n }\n let mut i: usize = 1;\n while i < n {\n let prev_val = nums[i - 1];\n let curr_val = nums[i];\n let prev_run_len = run_len;\n if prev_val + 1 == curr_val {\n run_len = prev_run_len + 1;\n } else {\n run_len = 1;\n }\n if i + 1 >= k_usize {\n let out = if run_len >= k_usize { curr_val } else { -1 };\n result.push(out);\n }\n i += 1;\n }\n result\n }\n}", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn consecutive_step(nums: Seq, i: int) -> bool\n recommends\n 0 <= i + 1 < nums.len(),\n {\n nums[i + 1] == nums[i] + 1\n }\n\n pub open spec fn window_has_power(nums: Seq, start: int, k: int) -> bool\n recommends\n 0 <= start,\n 1 <= k,\n start + k <= nums.len(),\n {\n forall |j: int| start <= j < start + k - 1 ==> #[trigger] Self::consecutive_step(nums, j)\n }\n\n pub open spec fn window_power(nums: Seq, start: int, k: int) -> int\n recommends\n 0 <= start,\n 1 <= k,\n start + k <= nums.len(),\n {\n if Self::window_has_power(nums, start, k) {\n nums[start + k - 1] as int\n } else {\n -1\n }\n }\n\n pub fn results_array(nums: Vec, k: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 500,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result@.len() == nums.len() as int - k as int + 1,\n forall |i: int| 0 <= i < result@.len() ==> #[trigger] result@[i] as int == Self::window_power(nums@, i, k as int),\n {\n let n = nums.len();\n let k_usize = k as usize;\n let mut result: Vec = Vec::new();\n let mut run_len: usize = 1;\n if k_usize == 1 {\n result.push(nums[0]);\n }\n let mut i: usize = 1;\n while i < n {\n let prev_val = nums[i - 1];\n let curr_val = nums[i];\n let prev_run_len = run_len;\n if prev_val + 1 == curr_val {\n run_len = prev_run_len + 1;\n } else {\n run_len = 1;\n }\n if i + 1 >= k_usize {\n let out = if run_len >= k_usize { curr_val } else { -1 };\n result.push(out);\n }\n i += 1;\n }\n result\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn consecutive_step(nums: Seq, i: int) -> bool\n recommends\n 0 <= i + 1 < nums.len(),\n {\n nums[i + 1] == nums[i] + 1\n }\n\n pub open spec fn window_has_power(nums: Seq, start: int, k: int) -> bool\n recommends\n 0 <= start,\n 1 <= k,\n start + k <= nums.len(),\n {\n forall |j: int| start <= j < start + k - 1 ==> #[trigger] Self::consecutive_step(nums, j)\n }\n\n pub open spec fn window_power(nums: Seq, start: int, k: int) -> int\n recommends\n 0 <= start,\n 1 <= k,\n start + k <= nums.len(),\n {\n if Self::window_has_power(nums, start, k) {\n nums[start + k - 1] as int\n } else {\n -1\n }\n }\n\n pub fn results_array(nums: Vec, k: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 500,\n 1 <= k <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100_000,\n ensures\n result@.len() == nums.len() as int - k as int + 1,\n forall |i: int| 0 <= i < result@.len() ==> #[trigger] result@[i] as int == Self::window_power(nums@, i, k as int),\n {\n let n = nums.len();\n let k_usize = k as usize;\n let mut result: Vec = Vec::new();\n let mut run_len: usize = 1;\n if k_usize == 1 {\n result.push(nums[0]);\n proof {\n assert(Self::window_has_power(nums@, 0, 1)) by {\n assert forall |j: int| 0 <= j < 0 implies #[trigger] Self::consecutive_step(nums@, j) by {\n }\n };\n assert(Self::window_power(nums@, 0, 1) == nums[0] as int);\n assert forall |s: int| 0 <= s < result@.len() implies #[trigger] result@[s] as int == Self::window_power(nums@, s, k as int) by {\n assert(s == 0);\n }\n }\n }\n let mut i: usize = 1;\n while i < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 500,\n 1 <= k <= nums.len(),\n k_usize == k as usize,\n 1 <= k_usize <= n,\n 1 <= i <= n,\n 1 <= run_len <= i,\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 100_000,\n forall |j: int| i as int - run_len as int <= j < i as int - 1 ==> #[trigger] Self::consecutive_step(nums@, j),\n run_len as int >= i as int || !(#[trigger] Self::consecutive_step(nums@, i as int - run_len as int - 1)),\n k_usize != 1 || result@.len() == i as int,\n !(k_usize > 1 && i < k_usize) || result@.len() == 0,\n !(k_usize > 1 && k_usize <= i) || result@.len() == i as int - k as int + 1,\n forall |s: int| 0 <= s < result@.len() ==> #[trigger] result@[s] as int == Self::window_power(nums@, s, k as int),\n decreases n - i,\n {\n let prev_val = nums[i - 1];\n let curr_val = nums[i];\n let prev_run_len = run_len;\n if prev_val + 1 == curr_val {\n proof {\n assert(prev_run_len <= i);\n }\n run_len = prev_run_len + 1;\n } else {\n run_len = 1;\n }\n proof {\n let i_int = i as int;\n let run_len_int = run_len as int;\n let prev_run_len_int = prev_run_len as int;\n if prev_val + 1 == curr_val {\n assert forall |j: int| i_int + 1 - run_len_int <= j < i_int implies #[trigger] Self::consecutive_step(nums@, j) by {\n if j < i_int - 1 {\n assert(i_int - prev_run_len_int <= j < i_int - 1);\n } else {\n assert(j == i_int - 1);\n assert(Self::consecutive_step(nums@, j));\n }\n };\n if run_len_int < i_int + 1 {\n assert(prev_run_len_int < i_int);\n assert(i_int - run_len_int == i_int - prev_run_len_int - 1);\n assert(!Self::consecutive_step(nums@, i_int - run_len_int));\n }\n } else {\n assert forall |j: int| i_int + 1 - run_len_int <= j < i_int implies #[trigger] Self::consecutive_step(nums@, j) by {\n };\n assert(run_len_int < i_int + 1);\n assert(!Self::consecutive_step(nums@, i_int - run_len_int));\n }\n }\n if i + 1 >= k_usize {\n let out = if run_len >= k_usize { curr_val } else { -1 };\n proof {\n let i_int = i as int;\n let run_len_int = run_len as int;\n let start = i_int + 1 - k as int;\n if run_len >= k_usize {\n assert(Self::window_has_power(nums@, start, k as int)) by {\n assert forall |j: int| start <= j < start + k as int - 1 implies #[trigger] Self::consecutive_step(nums@, j) by {\n assert(i_int + 1 - run_len_int <= start);\n assert(start + k as int - 1 == i_int);\n assert(i_int + 1 - run_len_int <= j < i_int);\n }\n };\n assert(out == curr_val);\n assert(Self::window_power(nums@, start, k as int) == curr_val as int);\n } else {\n assert(k_usize > 1);\n let bad = i_int - run_len_int;\n assert(start <= bad < i_int);\n assert(!Self::window_has_power(nums@, start, k as int)) by {\n if Self::window_has_power(nums@, start, k as int) {\n assert(Self::consecutive_step(nums@, bad));\n assert(!Self::consecutive_step(nums@, bad));\n assert(false);\n }\n };\n assert(out == -1);\n assert(Self::window_power(nums@, start, k as int) == -1);\n }\n }\n let ghost old_result = result@;\n result.push(out);\n proof {\n let i_int = i as int;\n let start = i_int + 1 - k as int;\n assert(result@ == old_result.push(out));\n assert forall |s: int| 0 <= s < result@.len() implies #[trigger] result@[s] as int == Self::window_power(nums@, s, k as int) by {\n if s < old_result.len() {\n assert(result@[s] == old_result[s]);\n } else {\n assert(s == old_result.len());\n assert(old_result.len() == start);\n assert(result@[s] == out);\n }\n };\n if k_usize == 1 {\n assert(old_result.len() == i_int);\n assert(result@.len() == i_int + 1);\n } else if i == k_usize - 1 {\n assert(old_result.len() == 0);\n assert(result@.len() == 1);\n } else {\n assert(old_result.len() == i_int - k as int + 1);\n assert(result@.len() == i_int - k as int + 2);\n }\n }\n }\n i += 1;\n }\n result\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc3254/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3254/tests/mutated_testcases.jsonl"} {"problem": "lc326", "family": "leetcode", "description": "# 326. Power of Three\nGiven an integer n, return `true` if it is a power of three. Otherwise, return `false`.\n\nAn integer `n` is a power of three, if there exists an integer `x` such that `n == 3**x`.\n\n## Example 1:\n> **Input:** n = 27 \n> **Output:** true \n> **Explanation:** 27 = 3**3\n\n\n## Example 2:\n> **Input:** n = 0 \n> **Output:** false \n> **Explanation:** There is no x where 3**x = 0. \n\n## Example 3:\n> **Input:** n = -1 \n> **Output:** false \n> **Explanation:** There is no x where 3x = (-1).\n\n## Constraints:\n\n- $-2^{31} <= n <= 2^{31} - 1$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_power_of_three(n: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMath\nBit Manipulation\nRecursion\nAcceptance rate: 51.4%\n", "spec": "use vstd::arithmetic::{logarithm::log, power::pow};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_is_power_of_three(n: int) -> bool {\n if n <= 0 {\n false\n } else {\n pow(3, log(3, n) as nat) == n\n }\n }\n\n pub fn is_power_of_three(n: i32) -> (res: bool)\n requires\n -2_147_483_648 <= n <= 2_147_483_647,\n ensures\n res == Self::spec_is_power_of_three(n as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn is_power_of_three(n: i32) -> bool {\n if n <= 0 {\n false\n } else {\n let mut x = n;\n while x > 1 && x % 3 == 0 {\n x = x / 3;\n }\n x == 1\n }\n }\n}\n", "code_spec": "use vstd::arithmetic::{logarithm::log, power::pow};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_is_power_of_three(n: int) -> bool {\n if n <= 0 {\n false\n } else {\n pow(3, log(3, n) as nat) == n\n }\n }\n\n pub fn is_power_of_three(n: i32) -> (res: bool)\n requires\n -2_147_483_648 <= n <= 2_147_483_647,\n ensures\n res == Self::spec_is_power_of_three(n as int),\n {\n if n <= 0 {\n false\n } else {\n let mut x = n;\n while x > 1 && x % 3 == 0 {\n x = x / 3;\n }\n x == 1\n }\n }\n}\n\n} \n", "verified": "use vstd::arithmetic::{\n div_mod::lemma_fundamental_div_mod,\n logarithm::{lemma_log0, lemma_log_nonnegative, lemma_log_s, log},\n power::{lemma_pow0, pow},\n};\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_is_power_of_three(n: int) -> bool {\n if n <= 0 {\n false\n } else {\n pow(3, log(3, n) as nat) == n\n }\n }\n\n pub fn is_power_of_three(n: i32) -> (res: bool)\n requires\n -2_147_483_648 <= n <= 2_147_483_647,\n ensures\n res == Self::spec_is_power_of_three(n as int),\n {\n if n <= 0 {\n false\n } else {\n let mut x = n;\n while x > 1 && x % 3 == 0\n invariant\n x > 0,\n Self::spec_is_power_of_three(n as int) == Self::spec_is_power_of_three(\n x as int,\n ),\n decreases x,\n {\n proof {\n let old_x = x as int;\n let new_x = old_x / 3;\n lemma_fundamental_div_mod(old_x, 3);\n lemma_log_s(3, old_x);\n lemma_log_nonnegative(3, new_x);\n reveal(pow);\n }\n x = x / 3;\n }\n proof {\n if x == 1 {\n lemma_log0(3, 1 as int);\n lemma_pow0(3);\n } else {\n if Self::spec_is_power_of_three(x as int) {\n let k = log(3, x as int) as nat;\n lemma_pow0(3);\n reveal(pow);\n }\n }\n }\n x == 1\n }\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc326/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc326/tests/mutated_testcases.jsonl"} {"problem": "lc3264", "family": "leetcode", "description": "# Final Array State After K Multiplication Operations I\n\nYou are given an integer array `nums`, an integer `k`, and an integer `multiplier`.\n\nYou need to perform `k` operations on `nums`. In each operation:\n\n- Find the minimum value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears first.\n- Replace the selected minimum value `x` with `x * multiplier`.\n\nReturn an integer array denoting the final state of `nums` after performing all `k` operations.\n\n## Example 1:\n\n> **Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2 \n> **Output:** [8,4,6,5,6]\n\n## Example 2:\n\n> **Input:** nums = [1,2], k = 3, multiplier = 4 \n> **Output:** [16,8]\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `1 <= nums[i] <= 100`\n- `1 <= k <= 10`\n- `1 <= multiplier <= 5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn get_final_state(nums: Vec, k: i32, multiplier: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Math, Heap (Priority Queue), Simulation\nAcceptance rate: 86.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn min_index_prefix(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 1 {\n 0\n } else {\n let j = Self::min_index_prefix(s, end - 1);\n if s[end - 1] < s[j] {\n end - 1\n } else {\n j\n }\n }\n }\n\n pub open spec fn apply_once(s: Seq, multiplier: int) -> Seq {\n if s.len() == 0 {\n s\n } else {\n let idx = Self::min_index_prefix(s, s.len() as int);\n s.update(idx, s[idx] * multiplier)\n }\n }\n\n pub open spec fn after_k(s: Seq, k: int, multiplier: int) -> Seq\n decreases k,\n {\n if k <= 0 {\n s\n } else {\n Self::apply_once(Self::after_k(s, k - 1, multiplier), multiplier)\n }\n }\n\n pub fn get_final_state(nums: Vec, k: i32, multiplier: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 10,\n 1 <= multiplier <= 5,\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() ==> {\n &&& 1 <= #[trigger] result[i]\n &&& result[i] as int == Self::after_k(Self::to_int_seq(nums@), k as int, multiplier as int)[i]\n },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn get_final_state(nums: Vec, k: i32, multiplier: i32) -> Vec {\n let mut nums = nums;\n let n = nums.len();\n let mut step: i32 = 0;\n while step < k {\n let mut min_idx: usize = 0;\n let mut j: usize = 1;\n while j < n {\n if nums[j] < nums[min_idx] {\n min_idx = j;\n }\n j = j + 1;\n }\n let old_val: i32 = nums[min_idx];\n let new_val: i32 = old_val * multiplier;\n nums[min_idx] = new_val;\n step = step + 1;\n }\n nums\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn min_index_prefix(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 1 {\n 0\n } else {\n let j = Self::min_index_prefix(s, end - 1);\n if s[end - 1] < s[j] {\n end - 1\n } else {\n j\n }\n }\n }\n\n pub open spec fn apply_once(s: Seq, multiplier: int) -> Seq {\n if s.len() == 0 {\n s\n } else {\n let idx = Self::min_index_prefix(s, s.len() as int);\n s.update(idx, s[idx] * multiplier)\n }\n }\n\n pub open spec fn after_k(s: Seq, k: int, multiplier: int) -> Seq\n decreases k,\n {\n if k <= 0 {\n s\n } else {\n Self::apply_once(Self::after_k(s, k - 1, multiplier), multiplier)\n }\n }\n\n pub fn get_final_state(nums: Vec, k: i32, multiplier: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 10,\n 1 <= multiplier <= 5,\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() ==> {\n &&& 1 <= #[trigger] result[i]\n &&& result[i] as int == Self::after_k(Self::to_int_seq(nums@), k as int, multiplier as int)[i]\n },\n {\n let mut nums = nums;\n let n = nums.len();\n let mut step: i32 = 0;\n while step < k {\n let mut min_idx: usize = 0;\n let mut j: usize = 1;\n while j < n {\n if nums[j] < nums[min_idx] {\n min_idx = j;\n }\n j = j + 1;\n }\n let old_val: i32 = nums[min_idx];\n let new_val: i32 = old_val * multiplier;\n nums.set(min_idx, new_val);\n step = step + 1;\n }\n nums\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn to_int_seq(s: Seq) -> Seq {\n Seq::new(s.len(), |i: int| s[i] as int)\n }\n\n pub open spec fn min_index_prefix(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 1 {\n 0\n } else {\n let j = Self::min_index_prefix(s, end - 1);\n if s[end - 1] < s[j] {\n end - 1\n } else {\n j\n }\n }\n }\n\n pub open spec fn apply_once(s: Seq, multiplier: int) -> Seq {\n if s.len() == 0 {\n s\n } else {\n let idx = Self::min_index_prefix(s, s.len() as int);\n s.update(idx, s[idx] * multiplier)\n }\n }\n\n pub open spec fn after_k(s: Seq, k: int, multiplier: int) -> Seq\n decreases k,\n {\n if k <= 0 {\n s\n } else {\n Self::apply_once(Self::after_k(s, k - 1, multiplier), multiplier)\n }\n }\n\n pub open spec fn pow5(n: int) -> int\n decreases n,\n {\n if n <= 0 {\n 1\n } else {\n 5 * Self::pow5(n - 1)\n }\n }\n\n proof fn lemma_to_int_update(s: Seq, idx: int, val: i32)\n requires\n 0 <= idx < s.len(),\n ensures\n Self::to_int_seq(s.update(idx, val)) == Self::to_int_seq(s).update(idx, val as int),\n {\n assert(Self::to_int_seq(s.update(idx, val)).len() == s.len());\n assert(Self::to_int_seq(s).update(idx, val as int).len() == s.len());\n assert forall |j: int| 0 <= j < s.len() implies #[trigger] Self::to_int_seq(s.update(idx, val))[j] == Self::to_int_seq(s).update(idx, val as int)[j] by {\n if j == idx {\n assert(Self::to_int_seq(s.update(idx, val))[j] == val as int);\n } else {\n assert(s.update(idx, val)[j] == s[j]);\n assert(Self::to_int_seq(s.update(idx, val))[j] == s[j] as int);\n }\n };\n }\n\n proof fn lemma_min_index_prefix_bounds(s: Seq, end: int)\n requires\n 1 <= end <= s.len(),\n ensures\n 0 <= Self::min_index_prefix(s, end) < end,\n decreases end,\n {\n if end <= 1 {\n } else {\n Self::lemma_min_index_prefix_bounds(s, end - 1);\n let j = Self::min_index_prefix(s, end - 1);\n assert(0 <= j < end - 1);\n if s[end - 1] < s[j] {\n } else {\n assert(j < end);\n }\n }\n }\n\n proof fn lemma_pow5_pos(n: int)\n requires\n 0 <= n,\n ensures\n 1 <= Self::pow5(n),\n decreases n,\n {\n if n <= 0 {\n } else {\n Self::lemma_pow5_pos(n - 1);\n }\n }\n\n proof fn lemma_pow5_le_9765625(n: int)\n requires\n 0 <= n <= 10,\n ensures\n Self::pow5(n) <= 9_765_625,\n {\n if n == 0 {\n reveal_with_fuel(Solution::pow5, 1);\n } else if n == 1 {\n reveal_with_fuel(Solution::pow5, 2);\n } else if n == 2 {\n reveal_with_fuel(Solution::pow5, 3);\n } else if n == 3 {\n reveal_with_fuel(Solution::pow5, 4);\n } else if n == 4 {\n reveal_with_fuel(Solution::pow5, 5);\n } else if n == 5 {\n reveal_with_fuel(Solution::pow5, 6);\n } else if n == 6 {\n reveal_with_fuel(Solution::pow5, 7);\n } else if n == 7 {\n reveal_with_fuel(Solution::pow5, 8);\n } else if n == 8 {\n reveal_with_fuel(Solution::pow5, 9);\n } else if n == 9 {\n reveal_with_fuel(Solution::pow5, 10);\n } else {\n assert(n == 10);\n reveal_with_fuel(Solution::pow5, 11);\n }\n }\n\n proof fn lemma_mul_le_five(x: int, m: int)\n requires\n 0 <= x,\n 1 <= m <= 5,\n ensures\n x * m <= x * 5,\n {\n if m == 1 {\n } else if m == 2 {\n } else if m == 3 {\n } else if m == 4 {\n } else {\n assert(m == 5);\n }\n }\n\n proof fn lemma_mul_ge_one(x: int, m: int)\n requires\n 1 <= x,\n 1 <= m <= 5,\n ensures\n 1 <= x * m,\n {\n if m == 1 {\n } else if m == 2 {\n } else if m == 3 {\n } else if m == 4 {\n } else {\n assert(m == 5);\n }\n }\n\n pub fn get_final_state(nums: Vec, k: i32, multiplier: i32) -> (result: Vec)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 10,\n 1 <= multiplier <= 5,\n ensures\n result.len() == nums.len(),\n forall |i: int| 0 <= i < result.len() ==> {\n &&& 1 <= #[trigger] result[i]\n &&& result[i] as int == Self::after_k(Self::to_int_seq(nums@), k as int, multiplier as int)[i]\n },\n {\n let mut nums = nums;\n let n = nums.len();\n let ghost init = Self::to_int_seq(nums@);\n let mut step: i32 = 0;\n while step < k\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= k <= 10,\n 1 <= multiplier <= 5,\n 0 <= step <= k,\n Self::to_int_seq(nums@) == Self::after_k(init, step as int, multiplier as int),\n forall |i: int| 0 <= i < nums.len() ==> {\n &&& 1 <= #[trigger] nums[i] as int\n &&& nums[i] as int <= 100 * Self::pow5(step as int)\n },\n decreases (k - step) as int,\n {\n let ghost before = nums@;\n let ghost before_int = Self::to_int_seq(before);\n\n let mut min_idx: usize = 0;\n let mut j: usize = 1;\n while j < n\n invariant\n n == nums.len(),\n nums@ == before,\n before_int == Self::to_int_seq(before),\n 1 <= n <= 100,\n 1 <= k <= 10,\n 1 <= multiplier <= 5,\n 0 <= step < k,\n 1 <= j <= n,\n 0 <= min_idx < j,\n min_idx as int == Self::min_index_prefix(before_int, j as int),\n decreases n - j,\n {\n let ghost old_idx = min_idx as int;\n if nums[j] < nums[min_idx] {\n min_idx = j;\n proof {\n assert(before_int[j as int] == nums[j as int] as int);\n assert(before_int[old_idx] == nums[old_idx] as int);\n assert(before_int[j as int] < before_int[old_idx]);\n assert(Self::min_index_prefix(before_int, (j + 1) as int) == j as int);\n }\n } else {\n proof {\n assert(before_int[j as int] == nums[j as int] as int);\n assert(before_int[old_idx] == nums[old_idx] as int);\n assert(before_int[j as int] >= before_int[old_idx]);\n assert(Self::min_index_prefix(before_int, (j + 1) as int) == old_idx);\n }\n }\n j = j + 1;\n proof {\n assert(min_idx as int == Self::min_index_prefix(before_int, j as int));\n }\n }\n\n proof {\n assert(j == n);\n assert(min_idx as int == Self::min_index_prefix(before_int, n as int));\n Self::lemma_min_index_prefix_bounds(before_int, n as int);\n assert(0 <= min_idx < n);\n }\n\n let old_val: i32 = nums[min_idx];\n proof {\n assert(1 <= old_val as int <= 100 * Self::pow5(step as int));\n assert(step + 1 <= k);\n assert((step + 1) as int <= 10);\n Self::lemma_mul_le_five(old_val as int, multiplier as int);\n assert(old_val as int * multiplier as int <= old_val as int * 5);\n assert(old_val as int * 5 <= 100 * Self::pow5(step as int) * 5);\n assert(100 * Self::pow5((step + 1) as int) == 100 * (5 * Self::pow5(step as int)));\n assert(old_val as int * multiplier as int <= 100 * Self::pow5((step + 1) as int));\n Self::lemma_pow5_le_9765625((step + 1) as int);\n assert(100 * Self::pow5((step + 1) as int) <= 976_562_500);\n assert(old_val as int * multiplier as int <= 2_147_483_647);\n Self::lemma_mul_ge_one(old_val as int, multiplier as int);\n }\n let new_val: i32 = old_val * multiplier;\n nums.set(min_idx, new_val);\n\n proof {\n assert(before_int[min_idx as int] == old_val as int);\n assert(new_val as int == old_val as int * multiplier as int);\n Self::lemma_to_int_update(before, min_idx as int, new_val);\n assert(Self::to_int_seq(nums@) == before_int.update(min_idx as int, new_val as int));\n assert(before_int.update(min_idx as int, new_val as int)\n == before_int.update(min_idx as int, before_int[min_idx as int] * multiplier as int));\n assert(Self::apply_once(before_int, multiplier as int)\n == before_int.update(Self::min_index_prefix(before_int, before_int.len() as int),\n before_int[Self::min_index_prefix(before_int, before_int.len() as int)] * multiplier as int));\n assert(min_idx as int == Self::min_index_prefix(before_int, before_int.len() as int));\n assert(Self::to_int_seq(nums@) == Self::apply_once(before_int, multiplier as int));\n assert(Self::after_k(init, (step + 1) as int, multiplier as int)\n == Self::apply_once(Self::after_k(init, step as int, multiplier as int), multiplier as int));\n assert(Self::after_k(init, step as int, multiplier as int) == before_int);\n assert(Self::to_int_seq(nums@) == Self::after_k(init, (step + 1) as int, multiplier as int));\n\n assert forall |i: int| 0 <= i < nums.len() implies {\n &&& 1 <= #[trigger] nums[i] as int\n &&& nums[i] as int <= 100 * Self::pow5((step + 1) as int)\n } by {\n if i == min_idx as int {\n assert(nums[i] == new_val);\n assert(1 <= new_val as int);\n assert(new_val as int <= 100 * Self::pow5((step + 1) as int));\n } else {\n assert(nums[i] == before[i]);\n assert(1 <= before[i] as int <= 100 * Self::pow5(step as int));\n Self::lemma_pow5_pos(step as int);\n assert(100 * Self::pow5(step as int) <= 100 * Self::pow5((step + 1) as int));\n }\n };\n }\n\n step = step + 1;\n }\n\n nums\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3264/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3264/tests/mutated_testcases.jsonl"} {"problem": "lc33", "family": "leetcode", "description": "# 33. Search in Rotated Sorted Array\n\nThere is an integer array `nums` sorted in ascending order (with **distinct** values).\n\nPrior to being passed to your function, `nums` is **possibly left rotated** at an unknown index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be left rotated by `3` indices and become `[4,5,6,7,0,1,2]`.\n\nGiven the array `nums` **after** the possible rotation and an integer `target`, return the index of `target` if it is in `nums`, or `-1` if it is not in `nums`.\n\nYou must write an algorithm with `O(log n)` runtime complexity.\n\n## Example 1:\n\n```\nInput: nums = [4,5,6,7,0,1,2], target = 0\nOutput: 4\n```\n\n## Example 2:\n\n```\nInput: nums = [4,5,6,7,0,1,2], target = 3\nOutput: -1\n```\n\n## Example 3:\n\n```\nInput: nums = [1], target = 0\nOutput: -1\n```\n\n## Constraints:\n\n- 1 <= nums.length <= $5000$\n\n- $-10^4$ <= nums[i] <= $10^4$\n\n- All values of `nums` are **unique**.\n\n- `nums` is an ascending array that is possibly rotated.\n\n- $-10^4$ <= target <= $10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn search(nums: Vec, target: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray\nBinary Search\nAcceptance rate: 43.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ord_tuple(small: &(bool, i32), big: &(bool, i32)) -> bool {\n if small.0 != big.0 {\n !small.0 && big.0\n } else {\n small.1 < big.1\n }\n }\n\n pub open spec fn rot_tuple(nums: &Vec, i: int) -> (bool, i32) {\n if 0 <= i < nums.len() {\n (nums[i] < nums[0], nums[i])\n } else {\n (false, 0)\n }\n }\n\n pub fn search(nums: Vec, target: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 5_000,\n forall|i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n forall|i: int, j: int|\n 0 <= i < j < nums.len() ==> #[trigger] Self::ord_tuple(\n &Self::rot_tuple(&nums, i),\n &Self::rot_tuple(&nums, j),\n ),\n -10_000 <= target <= 10_000,\n ensures\n res == -1 || 0 <= res < nums.len(),\n 0 <= res < nums.len() ==> target == nums[res as int],\n res == -1 ==> forall|i: int| 0 <= i < nums.len() ==> nums[i] != target,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn ord_tuple_exec(small: &(bool, i32), big: &(bool, i32)) -> bool {\n if small.0 != big.0 {\n !small.0 && big.0\n } else {\n small.1 < big.1\n }\n }\n\n pub fn search(nums: Vec, target: i32) -> i32 {\n let mut i1: usize = 0;\n let mut i2: usize = nums.len() - 1;\n\n let target_tuple = (target < nums[0], target);\n\n while i1 != i2 {\n let ix = i1 + (i2 - i1) / 2;\n if Self::ord_tuple_exec(&(nums[ix] < nums[0], nums[ix]), &target_tuple) {\n i1 = ix + 1;\n } else {\n i2 = ix;\n }\n }\n\n if nums[i1] != target {\n -1\n } else {\n i1 as i32\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ord_tuple(small: &(bool, i32), big: &(bool, i32)) -> bool {\n if small.0 != big.0 {\n !small.0 && big.0\n } else {\n small.1 < big.1\n }\n }\n\n pub fn ord_tuple_exec(small: &(bool, i32), big: &(bool, i32)) -> bool {\n if small.0 != big.0 {\n !small.0 && big.0\n } else {\n small.1 < big.1\n }\n }\n\n pub open spec fn rot_tuple(nums: &Vec, i: int) -> (bool, i32) {\n if 0 <= i < nums.len() {\n (nums[i] < nums[0], nums[i])\n } else {\n (false, 0)\n }\n }\n\n pub fn search(nums: Vec, target: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 5_000,\n forall|i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n forall|i: int, j: int|\n 0 <= i < j < nums.len() ==> #[trigger] Self::ord_tuple(\n &Self::rot_tuple(&nums, i),\n &Self::rot_tuple(&nums, j),\n ),\n -10_000 <= target <= 10_000,\n ensures\n res == -1 || 0 <= res < nums.len(),\n 0 <= res < nums.len() ==> target == nums[res as int],\n res == -1 ==> forall|i: int| 0 <= i < nums.len() ==> nums[i] != target,\n {\n let mut i1: usize = 0;\n let mut i2: usize = nums.len() - 1;\n\n let target_tuple = (target < nums[0], target);\n\n while i1 != i2 {\n let ix = i1 + (i2 - i1) / 2;\n if Self::ord_tuple_exec(&(nums[ix] < nums[0], nums[ix]), &target_tuple) {\n i1 = ix + 1;\n } else {\n i2 = ix;\n }\n }\n\n if nums[i1] != target {\n let mut k: usize = 0;\n while k < nums.len() {\n if nums[k] == target {\n return k as i32;\n }\n k += 1;\n }\n -1\n } else {\n i1 as i32\n }\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn ord_tuple(small: &(bool, i32), big: &(bool, i32)) -> bool {\n if small.0 != big.0 {\n !small.0 && big.0\n } else {\n small.1 < big.1\n }\n }\n\n pub fn ord_tuple_exec(small: &(bool, i32), big: &(bool, i32)) -> bool\n returns\n Self::ord_tuple(small, big),\n {\n if small.0 != big.0 {\n !small.0 && big.0\n } else {\n small.1 < big.1\n }\n }\n\n pub open spec fn rot_tuple(nums: &Vec, i: int) -> (bool, i32) {\n if 0 <= i < nums.len() {\n (nums[i] < nums[0], nums[i])\n } else {\n (false, 0)\n }\n }\n\n pub fn search(nums: Vec, target: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 5_000,\n forall|i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n forall|i: int, j: int|\n 0 <= i < j < nums.len() ==> #[trigger] Self::ord_tuple(\n &Self::rot_tuple(&nums, i),\n &Self::rot_tuple(&nums, j),\n ),\n -10_000 <= target <= 10_000,\n ensures\n res == -1 || 0 <= res < nums.len(),\n 0 <= res < nums.len() ==> target == nums[res as int],\n res == -1 ==> forall|i: int| 0 <= i < nums.len() ==> nums[i] != target,\n {\n let mut i1: usize = 0;\n let mut i2: usize = nums.len() - 1;\n\n let target_tuple = (target < nums[0], target);\n\n while i1 != i2\n invariant\n 1 <= nums.len() <= 10_000,\n forall|i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n i2 < nums.len(),\n forall|i: int, j: int|\n 0 <= i < j < nums.len() ==> #[trigger] Self::ord_tuple(\n &Self::rot_tuple(&nums, i),\n &Self::rot_tuple(&nums, j),\n ),\n i1 <= i2,\n decreases i2 - i1,\n {\n let ix = i1 + (i2 - i1) / 2;\n if Self::ord_tuple_exec(&(nums[ix] < nums[0], nums[ix]), &target_tuple) {\n i1 = ix + 1;\n } else {\n i2 = ix;\n }\n }\n\n if nums[i1] != target {\n let mut k: usize = 0;\n while k < nums.len()\n invariant\n 1 <= nums.len() <= 10_000,\n 0 <= k <= nums.len(),\n forall|i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n forall|i: int| 0 <= i < k ==> nums[i] != target,\n decreases nums.len() - k,\n {\n if nums[k] == target {\n return k as i32;\n }\n k += 1;\n }\n -1\n } else {\n i1 as i32\n }\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc33/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc33/tests/mutated_testcases.jsonl"} {"problem": "lc3349", "family": "leetcode", "description": "# Adjacent Increasing Subarrays Detection I\n\nGiven an array `nums` of `n` integers and an integer `k`, determine whether there exist two adjacent subarrays of length `k` such that both subarrays are strictly increasing.\n\nMore formally, check if there is an index `a` such that:\n\n- `nums[a..a + k - 1]` is strictly increasing.\n- `nums[a + k..a + 2 * k - 1]` is strictly increasing.\n\nReturn `true` if such subarrays exist, and `false` otherwise.\n\n## Example 1:\n\n> **Input:** nums = [2,5,7,8,9,2,3,4,3,1], k = 3\n> **Output:** true\n> **Explanation:** `[7, 8, 9]` and `[2, 3, 4]` are adjacent strictly increasing subarrays of length 3.\n\n## Example 2:\n\n> **Input:** nums = [1,2,3,4,4,4,4,5,6,7], k = 5\n> **Output:** false\n\n## Constraints:\n\n- `2 <= nums.length <= 100`\n- `1 < 2 * k <= nums.length`\n- `-1000 <= nums[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn has_increasing_subarrays(nums: Vec, k: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 48.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_strictly_increasing(nums: Seq, start: int, len: int) -> bool {\n 1 <= len\n && 0 <= start\n && start + len <= nums.len()\n && forall |j: int| start <= j < start + len - 1 ==> #[trigger] nums[j] < nums[j + 1]\n }\n\n pub open spec fn adjacent_pair_at(nums: Seq, a: int, k: int) -> bool {\n 1 <= k\n && 0 <= a\n && a + 2 * k <= nums.len()\n && Self::is_strictly_increasing(nums, a, k)\n && Self::is_strictly_increasing(nums, a + k, k)\n }\n\n pub open spec fn has_adjacent_increasing_subarrays(nums: Seq, k: int) -> bool {\n 1 <= k\n && exists |a: int| #[trigger] Self::adjacent_pair_at(nums, a, k)\n }\n\n pub fn has_increasing_subarrays(nums: Vec, k: i32) -> (result: bool)\n requires\n 2 <= nums.len() <= 100,\n 1 < 2 * k as int,\n 2 * k as int <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result <==> Self::has_adjacent_increasing_subarrays(nums@, k as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn max_increasing_subarrays(nums: &Vec) -> i32 {\n let n = nums.len();\n let mut prev = 0usize;\n let mut curr = 1usize;\n let mut ans = 0usize;\n let mut i = 1usize;\n while i < n {\n if nums[i] > nums[i - 1] {\n curr = curr + 1;\n } else {\n prev = curr;\n curr = 1;\n }\n let split = curr / 2;\n let cross = if prev < curr { prev } else { curr };\n let mut next_ans = ans;\n if split > next_ans {\n next_ans = split;\n }\n if cross > next_ans {\n next_ans = cross;\n }\n ans = next_ans;\n i = i + 1;\n }\n ans as i32\n }\n\n pub fn has_increasing_subarrays(nums: Vec, k: i32) -> bool {\n let best = Self::max_increasing_subarrays(&nums);\n best >= k\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_strictly_increasing(nums: Seq, start: int, len: int) -> bool {\n 1 <= len\n && 0 <= start\n && start + len <= nums.len()\n && forall |j: int| start <= j < start + len - 1 ==> #[trigger] nums[j] < nums[j + 1]\n }\n\n pub open spec fn adjacent_pair_at(nums: Seq, a: int, k: int) -> bool {\n 1 <= k\n && 0 <= a\n && a + 2 * k <= nums.len()\n && Self::is_strictly_increasing(nums, a, k)\n && Self::is_strictly_increasing(nums, a + k, k)\n }\n\n pub open spec fn has_adjacent_increasing_subarrays(nums: Seq, k: int) -> bool {\n 1 <= k\n && exists |a: int| #[trigger] Self::adjacent_pair_at(nums, a, k)\n }\n\n fn max_increasing_subarrays(nums: &Vec) -> (result: i32)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n 1 <= result as int,\n 2 * result as int <= nums.len(),\n Self::has_adjacent_increasing_subarrays(nums@, result as int),\n forall |k: int| 1 <= k <= nums.len() / 2 && #[trigger] Self::has_adjacent_increasing_subarrays(nums@, k)\n ==> k <= result as int,\n {\n let n = nums.len();\n let mut prev = 0usize;\n let mut curr = 1usize;\n let mut ans = 0usize;\n let mut i = 1usize;\n while i < n {\n if nums[i] > nums[i - 1] {\n curr = curr + 1;\n } else {\n prev = curr;\n curr = 1;\n }\n let split = curr / 2;\n let cross = if prev < curr { prev } else { curr };\n let mut next_ans = ans;\n if split > next_ans {\n next_ans = split;\n }\n if cross > next_ans {\n next_ans = cross;\n }\n ans = next_ans;\n i = i + 1;\n }\n ans as i32\n }\n\n pub fn has_increasing_subarrays(nums: Vec, k: i32) -> (result: bool)\n requires\n 2 <= nums.len() <= 100,\n 1 < 2 * k as int,\n 2 * k as int <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result <==> Self::has_adjacent_increasing_subarrays(nums@, k as int),\n {\n let best = Self::max_increasing_subarrays(&nums);\n best >= k\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_strictly_increasing(nums: Seq, start: int, len: int) -> bool {\n 1 <= len\n && 0 <= start\n && start + len <= nums.len()\n && forall |j: int| start <= j < start + len - 1 ==> #[trigger] nums[j] < nums[j + 1]\n }\n\n pub open spec fn adjacent_pair_at(nums: Seq, a: int, k: int) -> bool {\n 1 <= k\n && 0 <= a\n && a + 2 * k <= nums.len()\n && Self::is_strictly_increasing(nums, a, k)\n && Self::is_strictly_increasing(nums, a + k, k)\n }\n\n pub open spec fn has_adjacent_increasing_subarrays(nums: Seq, k: int) -> bool {\n 1 <= k\n && exists |a: int| #[trigger] Self::adjacent_pair_at(nums, a, k)\n }\n\n proof fn lemma_subsegment_of_increasing(nums: Seq, start: int, len: int, sub_start: int, sub_len: int)\n requires\n Self::is_strictly_increasing(nums, start, len),\n start <= sub_start,\n 1 <= sub_len,\n sub_start + sub_len <= start + len,\n ensures\n Self::is_strictly_increasing(nums, sub_start, sub_len),\n {\n assert(0 <= sub_start);\n assert(sub_start + sub_len <= nums.len());\n assert forall |j: int| sub_start <= j < sub_start + sub_len - 1 implies #[trigger] nums[j] < nums[j + 1] by {\n assert(start <= j);\n assert(j < start + len - 1);\n };\n }\n\n proof fn lemma_has_for_smaller(nums: Seq, k_big: int, k_small: int)\n requires\n Self::has_adjacent_increasing_subarrays(nums, k_big),\n 1 <= k_small <= k_big,\n ensures\n Self::has_adjacent_increasing_subarrays(nums, k_small),\n {\n let a = choose |a: int| #[trigger] Self::adjacent_pair_at(nums, a, k_big);\n let start_small = a + (k_big - k_small);\n assert(0 <= a);\n assert(a + 2 * k_big <= nums.len());\n assert(0 <= start_small);\n assert(start_small + k_small <= a + k_big);\n assert(a + k_big + k_small <= a + 2 * k_big);\n Self::lemma_subsegment_of_increasing(nums, a, k_big, start_small, k_small);\n Self::lemma_subsegment_of_increasing(nums, a + k_big, k_big, a + k_big, k_small);\n assert(Self::has_adjacent_increasing_subarrays(nums, k_small)) by {\n assert(Self::adjacent_pair_at(nums, start_small, k_small));\n };\n }\n\n fn max_increasing_subarrays(nums: &Vec) -> (result: i32)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n 1 <= result as int,\n 2 * result as int <= nums.len(),\n Self::has_adjacent_increasing_subarrays(nums@, result as int),\n forall |k: int| 1 <= k <= nums.len() / 2 && #[trigger] Self::has_adjacent_increasing_subarrays(nums@, k)\n ==> k <= result as int,\n {\n let n = nums.len();\n let mut prev = 0usize;\n let mut curr = 1usize;\n let mut ans = 0usize;\n let ghost mut start: int = 0;\n let ghost mut best_a: int = 0;\n let mut i = 1usize;\n\n while i < n\n invariant\n 2 <= n <= 100,\n n == nums.len(),\n 1 <= i <= n,\n 0 <= start < i,\n 0 <= prev <= start,\n curr == i - start,\n 1 <= curr,\n start == 0 ==> prev == 0,\n start > 0 ==> 1 <= prev <= start,\n forall |j: int| start as int <= j < i as int - 1 ==> #[trigger] nums@[j] < nums@[j + 1],\n start > 0 ==> nums@[start as int - 1] >= nums@[start as int],\n start > 0 ==> forall |j: int| (start - prev) as int <= j < start as int - 1 ==> #[trigger] nums@[j] < nums@[j + 1],\n start > prev ==> nums@[(start - prev) as int - 1] >= nums@[(start - prev) as int],\n 2 * ans as int <= i as int,\n ans > 0 ==> 0 <= best_a && best_a + 2 * ans as int <= i as int,\n ans > 0 ==> Self::is_strictly_increasing(nums@, best_a, ans as int),\n ans > 0 ==> Self::is_strictly_increasing(nums@, best_a + ans as int, ans as int),\n forall |a: int, k: int|\n #[trigger] Self::adjacent_pair_at(nums@, a, k)\n && a + 2 * k <= i as int\n ==> k <= ans as int,\n decreases n - i,\n {\n let ghost old_start = start as int;\n let ghost old_prev = prev as int;\n let ghost old_curr = curr as int;\n let ghost old_ans = ans as int;\n let ghost old_best_a = best_a;\n\n proof {\n assert(old_curr == i as int - old_start);\n assert forall |j: int| old_start <= j < i as int - 1 implies #[trigger] nums@[j] < nums@[j + 1] by {\n assert(nums@[j] < nums@[j + 1]);\n };\n assert forall |a: int, k: int|\n #[trigger] Self::adjacent_pair_at(nums@, a, k)\n && a + 2 * k <= i as int\n implies k <= old_ans by {\n assert(k <= ans as int);\n };\n if old_ans > 0 {\n assert(0 <= old_best_a && old_best_a + 2 * old_ans <= i as int);\n assert(Self::is_strictly_increasing(nums@, old_best_a, old_ans));\n assert(Self::is_strictly_increasing(nums@, old_best_a + old_ans, old_ans));\n }\n if old_start > 0 {\n assert(nums@[old_start - 1] >= nums@[old_start]);\n assert(1 <= old_prev <= old_start);\n assert forall |j: int| old_start - old_prev <= j < old_start - 1 implies #[trigger] nums@[j] < nums@[j + 1] by {\n assert(nums@[j] < nums@[j + 1]);\n };\n if old_start > old_prev {\n assert(nums@[old_start - old_prev - 1] >= nums@[old_start - old_prev]);\n }\n }\n }\n\n if nums[i] > nums[i - 1] {\n curr = curr + 1;\n proof {\n assert(start as int == old_start);\n assert(curr as int == i as int + 1 - start as int);\n assert forall |j: int| start as int <= j < i as int implies #[trigger] nums@[j] < nums@[j + 1] by {\n if j < i as int - 1 {\n assert(old_start <= j < i as int - 1);\n } else {\n assert(j == i as int - 1);\n assert(nums@[i as int - 1] < nums@[i as int]);\n }\n };\n }\n } else {\n prev = curr;\n proof { start = i as int; }\n curr = 1;\n proof {\n assert(prev as int == old_curr);\n assert(start as int - prev as int == old_start);\n assert(curr as int == i as int + 1 - start as int);\n assert(nums@[start as int - 1] >= nums@[start as int]);\n assert forall |j: int| (start - prev) as int <= j < start as int - 1 implies #[trigger] nums@[j] < nums@[j + 1] by {\n assert(old_start <= j < i as int - 1);\n };\n if start > prev {\n assert(start as int - prev as int > 0);\n assert(nums@[(start - prev) as int - 1] >= nums@[(start - prev) as int]);\n }\n }\n }\n\n let split = curr / 2;\n let cross = if prev < curr { prev } else { curr };\n let mut next_ans = ans;\n let ghost mut next_best_a = best_a;\n\n if split > next_ans {\n proof {\n assert(1 <= split as int);\n assert(2 * split as int <= curr as int) by (nonlinear_arith)\n requires\n split == curr / 2,\n {};\n Self::lemma_subsegment_of_increasing(nums@, start as int, curr as int, start as int, split as int);\n Self::lemma_subsegment_of_increasing(nums@, start as int, curr as int, start as int + split as int, split as int);\n next_best_a = start as int;\n }\n next_ans = split;\n }\n\n if cross > next_ans {\n proof {\n assert(1 <= cross as int);\n assert(start > 0);\n assert(cross as int <= prev as int);\n assert(cross as int <= curr as int);\n assert(0 <= start as int - cross as int);\n Self::lemma_subsegment_of_increasing(\n nums@,\n start as int - prev as int,\n prev as int,\n start as int - cross as int,\n cross as int,\n );\n Self::lemma_subsegment_of_increasing(nums@, start as int, curr as int, start as int, cross as int);\n next_best_a = start as int - cross as int;\n }\n next_ans = cross;\n }\n\n proof {\n assert(next_ans as int >= old_ans);\n assert(next_ans as int >= split as int);\n assert(next_ans as int >= cross as int);\n if next_ans > 0 {\n if next_ans as int == old_ans {\n assert(0 <= next_best_a && next_best_a + 2 * next_ans as int <= i as int + 1);\n assert(Self::is_strictly_increasing(nums@, next_best_a, next_ans as int));\n assert(Self::is_strictly_increasing(nums@, next_best_a + next_ans as int, next_ans as int));\n } else if next_ans == split {\n assert(next_best_a == start as int);\n assert(0 <= next_best_a);\n assert(start as int + 2 * split as int <= start as int + curr as int);\n assert(start as int + curr as int == i as int + 1);\n assert(next_best_a + 2 * next_ans as int <= i as int + 1);\n } else {\n assert(next_ans == cross);\n assert(next_best_a == start as int - cross as int);\n assert(0 <= next_best_a);\n assert(start as int + cross as int <= start as int + curr as int);\n assert(start as int + curr as int == i as int + 1);\n assert(next_best_a + 2 * next_ans as int == start as int + cross as int);\n assert(next_best_a + 2 * next_ans as int <= i as int + 1);\n }\n }\n\n assert forall |a: int, k: int|\n #[trigger] Self::adjacent_pair_at(nums@, a, k)\n && a + 2 * k <= i as int + 1\n implies k <= next_ans as int by {\n if a + 2 * k <= i as int {\n assert(k <= old_ans);\n assert(old_ans <= next_ans as int);\n } else {\n assert(a + 2 * k == i as int + 1);\n let second_start = a + k;\n if second_start < start as int {\n assert(second_start <= start as int - 1);\n assert(start as int <= second_start + k - 1);\n assert(nums@[start as int - 1] < nums@[start as int]);\n assert(start > 0);\n assert(nums@[start as int - 1] >= nums@[start as int]);\n assert(false);\n }\n if a >= start as int {\n assert(2 * k <= curr as int) by (nonlinear_arith)\n requires\n a + 2 * k == i as int + 1,\n a >= start as int,\n curr as int == i as int + 1 - start as int,\n {};\n assert(k <= split as int) by (nonlinear_arith)\n requires\n split == curr / 2,\n 2 * k <= curr as int,\n {};\n assert(split as int <= next_ans as int);\n } else {\n assert(second_start >= start as int);\n if second_start > start as int {\n assert(a <= start as int - 1);\n assert(start as int <= a + k - 1);\n assert(nums@[start as int - 1] < nums@[start as int]);\n assert(start > 0);\n assert(nums@[start as int - 1] >= nums@[start as int]);\n assert(false);\n }\n assert(second_start == start as int);\n if a < start as int - prev as int {\n if start as int - prev as int > 0 {\n assert(a <= start as int - prev as int - 1);\n assert(start as int - prev as int <= a + k - 1) by (nonlinear_arith)\n requires\n a + k == start as int,\n a < start as int - prev as int,\n 1 <= prev as int,\n {};\n assert(nums@[start as int - prev as int - 1] < nums@[start as int - prev as int]);\n assert(nums@[start as int - prev as int - 1] >= nums@[start as int - prev as int]);\n assert(false);\n } else {\n assert(a < 0);\n assert(false);\n }\n }\n assert(k == start as int - a);\n assert(k <= prev as int);\n assert(k <= curr as int) by (nonlinear_arith)\n requires\n a + 2 * k == i as int + 1,\n a + k == start as int,\n curr as int == i as int + 1 - start as int,\n {};\n assert(k <= cross as int);\n assert(cross as int <= next_ans as int);\n }\n }\n };\n }\n\n ans = next_ans;\n proof {\n best_a = next_best_a;\n }\n i = i + 1;\n }\n\n proof {\n assert(Self::is_strictly_increasing(nums@, 0, 1));\n assert(Self::is_strictly_increasing(nums@, 1, 1));\n assert(Self::has_adjacent_increasing_subarrays(nums@, 1)) by {\n let a = 0int;\n assert(Self::adjacent_pair_at(nums@, a, 1));\n };\n assert(1 <= ans as int) by {\n assert(0 + 2 * 1 <= n as int);\n assert(Self::is_strictly_increasing(nums@, 0, 1));\n assert(Self::is_strictly_increasing(nums@, 1, 1));\n }\n assert(Self::has_adjacent_increasing_subarrays(nums@, ans as int)) by {\n assert(ans > 0);\n assert(Self::adjacent_pair_at(nums@, best_a, ans as int));\n };\n assert forall |k: int| 1 <= k <= nums.len() / 2 && #[trigger] Self::has_adjacent_increasing_subarrays(nums@, k)\n implies k <= ans as int by {\n let a = choose |a: int|\n Self::adjacent_pair_at(nums@, a, k);\n assert(a + 2 * k <= n as int);\n assert(k <= ans as int);\n };\n }\n\n ans as i32\n }\n\n\n pub fn has_increasing_subarrays(nums: Vec, k: i32) -> (result: bool)\n requires\n 2 <= nums.len() <= 100,\n 1 < 2 * k as int,\n 2 * k as int <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result <==> Self::has_adjacent_increasing_subarrays(nums@, k as int),\n {\n let best = Self::max_increasing_subarrays(&nums);\n proof {\n assert(1 <= k as int);\n if best >= k {\n Self::lemma_has_for_smaller(nums@, best as int, k as int);\n } else {\n assert(!(Self::has_adjacent_increasing_subarrays(nums@, k as int))) by {\n if Self::has_adjacent_increasing_subarrays(nums@, k as int) {\n assert(k as int <= best as int);\n assert(false);\n }\n };\n }\n }\n best >= k\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc3349/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3349/tests/mutated_testcases.jsonl"} {"problem": "lc3364", "family": "leetcode", "description": "# Minimum Positive Sum Subarray\n\nYou are given an integer array `nums` and two integers `l` and `r`. Your task is to find the minimum sum of a subarray whose length is between `l` and `r` (inclusive) and whose sum is greater than `0`.\n\nReturn the minimum positive subarray sum. If no such subarray exists, return `-1`.\n\nA subarray is a contiguous non-empty sequence of elements within an array.\n\n## Example 1:\n\n> **Input:** nums = [3, -2, 1, 4], l = 2, r = 3\n> **Output:** 1\n>\n> **Explanation:** Valid positive subarrays with length in `[2, 3]` are `[3, -2]` (sum `1`), `[1, 4]` (sum `5`), `[3, -2, 1]` (sum `2`), and `[-2, 1, 4]` (sum `3`). The minimum positive sum is `1`.\n\n## Example 2:\n\n> **Input:** nums = [-2, 2, -3, 1], l = 2, r = 3\n> **Output:** -1\n>\n> **Explanation:** No subarray with length in `[2, 3]` has a sum greater than `0`.\n\n## Example 3:\n\n> **Input:** nums = [1, 2, 3, 4], l = 2, r = 4\n> **Output:** 3\n>\n> **Explanation:** The subarray `[1, 2]` has length `2` and sum `3`, which is the minimum positive sum.\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `1 <= l <= r <= nums.length`\n- `-1000 <= nums[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_sum_subarray(nums: Vec, l: i32, r: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sliding Window, Prefix Sum\nAcceptance rate: 45.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn window_sum(nums: Seq, start: int, len: int) -> int\n recommends\n 0 <= start,\n 0 <= len,\n start + len <= nums.len(),\n decreases len\n {\n if len <= 0 {\n 0\n } else {\n nums[start] as int + Self::window_sum(nums, start + 1, len - 1)\n }\n }\n\n pub open spec fn min_two(a: int, b: int) -> int {\n if a == -1 {\n b\n } else if b == -1 {\n a\n } else if a <= b {\n a\n } else {\n b\n }\n }\n\n pub open spec fn window_candidate(nums: Seq, start: int, len: int) -> int\n recommends\n 0 <= start,\n 0 <= len,\n start + len <= nums.len(),\n {\n let s = Self::window_sum(nums, start, len);\n if s > 0 { s } else { -1 }\n }\n\n pub open spec fn min_for_len_prefix(nums: Seq, len: int, count: int) -> int\n recommends\n 1 <= len <= nums.len(),\n 0 <= count <= nums.len() - len + 1,\n decreases count\n {\n if count <= 0 {\n -1\n } else {\n let prev = Self::min_for_len_prefix(nums, len, count - 1);\n let cand = Self::window_candidate(nums, count - 1, len);\n Self::min_two(prev, cand)\n }\n }\n\n pub open spec fn min_for_lengths_prefix(nums: Seq, l: int, upto: int) -> int\n recommends\n 1 <= l <= nums.len(),\n l <= upto <= nums.len() + 1,\n decreases upto - l\n {\n if upto <= l {\n -1\n } else {\n let prev = Self::min_for_lengths_prefix(nums, l, upto - 1);\n let len = upto - 1;\n let all_starts = nums.len() - len + 1;\n let cur = Self::min_for_len_prefix(nums, len, all_starts);\n Self::min_two(prev, cur)\n }\n }\n\n pub fn minimum_sum_subarray(nums: Vec, l: i32, r: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n 1 <= l <= r <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == Self::min_for_lengths_prefix(nums@, l as int, r as int + 1),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_sum_subarray(nums: Vec, l: i32, r: i32) -> i32 {\n let n = nums.len();\n let mut best: i64 = -1;\n let mut len: usize = l as usize;\n while len <= r as usize {\n let mut start: usize = 0;\n while start + len <= n {\n let mut sum: i64 = 0;\n let mut t: usize = 0;\n while t < len {\n sum = sum + nums[start + t] as i64;\n t += 1;\n }\n if sum > 0 && (best == -1 || sum < best) {\n best = sum;\n }\n start += 1;\n }\n len += 1;\n }\n best as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn window_sum(nums: Seq, start: int, len: int) -> int\n recommends\n 0 <= start,\n 0 <= len,\n start + len <= nums.len(),\n decreases len\n {\n if len <= 0 {\n 0\n } else {\n nums[start] as int + Self::window_sum(nums, start + 1, len - 1)\n }\n }\n\n pub open spec fn min_two(a: int, b: int) -> int {\n if a == -1 {\n b\n } else if b == -1 {\n a\n } else if a <= b {\n a\n } else {\n b\n }\n }\n\n pub open spec fn window_candidate(nums: Seq, start: int, len: int) -> int\n recommends\n 0 <= start,\n 0 <= len,\n start + len <= nums.len(),\n {\n let s = Self::window_sum(nums, start, len);\n if s > 0 { s } else { -1 }\n }\n\n pub open spec fn min_for_len_prefix(nums: Seq, len: int, count: int) -> int\n recommends\n 1 <= len <= nums.len(),\n 0 <= count <= nums.len() - len + 1,\n decreases count\n {\n if count <= 0 {\n -1\n } else {\n let prev = Self::min_for_len_prefix(nums, len, count - 1);\n let cand = Self::window_candidate(nums, count - 1, len);\n Self::min_two(prev, cand)\n }\n }\n\n pub open spec fn min_for_lengths_prefix(nums: Seq, l: int, upto: int) -> int\n recommends\n 1 <= l <= nums.len(),\n l <= upto <= nums.len() + 1,\n decreases upto - l\n {\n if upto <= l {\n -1\n } else {\n let prev = Self::min_for_lengths_prefix(nums, l, upto - 1);\n let len = upto - 1;\n let all_starts = nums.len() - len + 1;\n let cur = Self::min_for_len_prefix(nums, len, all_starts);\n Self::min_two(prev, cur)\n }\n }\n\n pub fn minimum_sum_subarray(nums: Vec, l: i32, r: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n 1 <= l <= r <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == Self::min_for_lengths_prefix(nums@, l as int, r as int + 1),\n {\n let n = nums.len();\n let mut best: i64 = -1;\n let mut len: usize = l as usize;\n while len <= r as usize {\n let mut start: usize = 0;\n while start + len <= n {\n let mut sum: i64 = 0;\n let mut t: usize = 0;\n while t < len {\n sum = sum + nums[start + t] as i64;\n t += 1;\n }\n if sum > 0 && (best == -1 || sum < best) {\n best = sum;\n }\n start += 1;\n }\n len += 1;\n }\n best as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn window_sum(nums: Seq, start: int, len: int) -> int\n recommends\n 0 <= start,\n 0 <= len,\n start + len <= nums.len(),\n decreases len\n {\n if len <= 0 {\n 0\n } else {\n nums[start] as int + Self::window_sum(nums, start + 1, len - 1)\n }\n }\n\n pub open spec fn min_two(a: int, b: int) -> int {\n if a == -1 {\n b\n } else if b == -1 {\n a\n } else if a <= b {\n a\n } else {\n b\n }\n }\n\n pub open spec fn window_candidate(nums: Seq, start: int, len: int) -> int\n recommends\n 0 <= start,\n 0 <= len,\n start + len <= nums.len(),\n {\n let s = Self::window_sum(nums, start, len);\n if s > 0 { s } else { -1 }\n }\n\n pub open spec fn min_for_len_prefix(nums: Seq, len: int, count: int) -> int\n recommends\n 1 <= len <= nums.len(),\n 0 <= count <= nums.len() - len + 1,\n decreases count\n {\n if count <= 0 {\n -1\n } else {\n let prev = Self::min_for_len_prefix(nums, len, count - 1);\n let cand = Self::window_candidate(nums, count - 1, len);\n Self::min_two(prev, cand)\n }\n }\n\n pub open spec fn min_for_lengths_prefix(nums: Seq, l: int, upto: int) -> int\n recommends\n 1 <= l <= nums.len(),\n l <= upto <= nums.len() + 1,\n decreases upto - l\n {\n if upto <= l {\n -1\n } else {\n let prev = Self::min_for_lengths_prefix(nums, l, upto - 1);\n let len = upto - 1;\n let all_starts = nums.len() - len + 1;\n let cur = Self::min_for_len_prefix(nums, len, all_starts);\n Self::min_two(prev, cur)\n }\n }\n\n proof fn lemma_min_two_assoc(a: int, b: int, c: int)\n ensures\n Self::min_two(Self::min_two(a, b), c) == Self::min_two(a, Self::min_two(b, c)),\n {\n if a == -1 {\n } else if b == -1 {\n } else if c == -1 {\n } else if a <= b {\n if a <= c {\n } else {\n }\n } else {\n if b <= c {\n } else {\n }\n }\n }\n\n proof fn lemma_window_sum_step(nums: Seq, start: int, len: int)\n requires\n 0 <= start,\n 1 <= len,\n start + len <= nums.len(),\n ensures\n Self::window_sum(nums, start, len) == Self::window_sum(nums, start, len - 1) + nums[start + len - 1],\n decreases len,\n {\n if len == 1 {\n assert(Self::window_sum(nums, start, 0) == 0);\n assert(Self::window_sum(nums, start, 1) == nums[start] as int + Self::window_sum(nums, start + 1, 0));\n } else {\n Self::lemma_window_sum_step(nums, start + 1, len - 1);\n }\n }\n\n pub fn minimum_sum_subarray(nums: Vec, l: i32, r: i32) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n 1 <= l <= r <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == Self::min_for_lengths_prefix(nums@, l as int, r as int + 1),\n {\n let n = nums.len();\n let mut best: i64 = -1;\n let mut len: usize = l as usize;\n while len <= r as usize\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= l <= r <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n l as usize <= len <= r as usize + 1,\n best == -1 || 1 <= best <= 100000,\n best as int == Self::min_for_lengths_prefix(nums@, l as int, len as int),\n decreases r as usize + 1 - len,\n {\n let mut start: usize = 0;\n proof {\n reveal_with_fuel(Solution::min_for_len_prefix, 2);\n assert(Self::min_for_len_prefix(nums@, len as int, 0) == -1);\n assert(Self::min_two(Self::min_for_lengths_prefix(nums@, l as int, len as int), -1)\n == Self::min_for_lengths_prefix(nums@, l as int, len as int));\n }\n while start + len <= n\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= l <= r <= nums.len(),\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n l as usize <= len <= r as usize,\n 0 <= start <= n - len + 1,\n best == -1 || 1 <= best <= 100000,\n best as int == Self::min_two(\n Self::min_for_lengths_prefix(nums@, l as int, len as int),\n Self::min_for_len_prefix(nums@, len as int, start as int),\n ),\n decreases n - len + 1 - start,\n {\n let mut sum: i64 = 0;\n let mut t: usize = 0;\n while t < len\n invariant\n n == nums.len(),\n 1 <= n <= 100,\n 1 <= len <= n,\n start + len <= n,\n 0 <= t <= len,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n sum as int == Self::window_sum(nums@, start as int, t as int),\n -1000 * (t as int) <= sum as int <= 1000 * (t as int),\n decreases len - t,\n {\n proof {\n Self::lemma_window_sum_step(nums@, start as int, t as int + 1);\n assert(nums@[start as int + t as int] >= -1000);\n assert(nums@[start as int + t as int] <= 1000);\n }\n sum = sum + nums[start + t] as i64;\n t += 1;\n }\n let ghost old_best = best;\n if sum > 0 && (best == -1 || sum < best) {\n best = sum;\n }\n proof {\n assert(sum as int == Self::window_sum(nums@, start as int, len as int));\n assert(-1000 * (len as int) <= sum as int <= 1000 * (len as int));\n assert(1 <= len <= 100);\n if sum > 0 {\n assert(sum as int <= 1000 * (len as int));\n assert(sum as int <= 100000) by (nonlinear_arith)\n requires sum as int <= 1000 * (len as int), len as int <= 100;\n }\n let cand = Self::window_candidate(nums@, start as int, len as int);\n if sum > 0 {\n assert(cand == sum as int);\n } else {\n assert(cand == -1);\n }\n\n if sum > 0 && (old_best == -1 || sum < old_best) {\n assert(best == sum);\n assert(best as int == cand);\n if old_best == -1 {\n assert(Self::min_two(old_best as int, cand) == cand);\n } else {\n assert(cand < old_best as int);\n assert(Self::min_two(old_best as int, cand) == cand);\n }\n } else {\n assert(best == old_best);\n if cand == -1 {\n assert(Self::min_two(old_best as int, cand) == old_best as int);\n } else {\n assert(cand == sum as int);\n assert(!(old_best == -1 || sum < old_best));\n assert(old_best != -1);\n assert(old_best as int <= cand);\n assert(Self::min_two(old_best as int, cand) == old_best as int);\n }\n }\n assert(best as int == Self::min_two(old_best as int, cand));\n assert(old_best as int == Self::min_two(\n Self::min_for_lengths_prefix(nums@, l as int, len as int),\n Self::min_for_len_prefix(nums@, len as int, start as int),\n ));\n reveal_with_fuel(Solution::min_for_len_prefix, 2);\n assert(Self::min_for_len_prefix(nums@, len as int, start as int + 1) == Self::min_two(\n Self::min_for_len_prefix(nums@, len as int, start as int),\n cand,\n ));\n Self::lemma_min_two_assoc(\n Self::min_for_lengths_prefix(nums@, l as int, len as int),\n Self::min_for_len_prefix(nums@, len as int, start as int),\n cand,\n );\n assert(best as int == Self::min_two(\n Self::min_for_lengths_prefix(nums@, l as int, len as int),\n Self::min_for_len_prefix(nums@, len as int, start as int + 1),\n ));\n if best != -1 {\n assert(best <= 100000);\n }\n }\n start += 1;\n }\n proof {\n assert(start + len > n);\n assert(start <= n - len + 1);\n if start <= n - len {\n assert(start + len <= n);\n assert(false);\n }\n assert(start == n - len + 1);\n reveal_with_fuel(Solution::min_for_lengths_prefix, 2);\n assert(Self::min_for_lengths_prefix(nums@, l as int, len as int + 1) == Self::min_two(\n Self::min_for_lengths_prefix(nums@, l as int, len as int),\n Self::min_for_len_prefix(nums@, len as int, (n - len + 1) as int),\n ));\n assert(best as int == Self::min_for_lengths_prefix(nums@, l as int, len as int + 1));\n }\n len += 1;\n }\n proof {\n assert(len == r as usize + 1);\n assert(best == -1 || 1 <= best <= 100000);\n }\n best as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3364/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3364/tests/mutated_testcases.jsonl"} {"problem": "lc3375", "family": "leetcode", "description": "# Minimum Operations to Make Array Values Equal to K\n\nYou are given an integer array `nums` and an integer `k`.\n\nAn integer `h` is called **valid** if all values in the array that are **strictly greater** than `h` are identical.\n\nFor example, if `nums = [10, 8, 10, 8]`, a valid integer is `h = 9` because all `nums[i] > 9` are equal to 10, but 5 is not a valid integer.\n\nYou are allowed to perform the following operation on `nums`:\n\n- Select an integer `h` that is valid for the current values in `nums`.\n- For each index `i` where `nums[i] > h`, set `nums[i]` to `h`.\n\nReturn the minimum number of operations required to make every element in `nums` equal to `k`. If it is impossible to make all elements equal to `k`, return -1.\n\n## Example 1:\n\n> **Input:** nums = [5,2,5,4,5], k = 2\n> **Output:** 2\n> **Explanation:** The operations can be performed in order using valid integers 4 and then 2.\n\n## Example 2:\n\n> **Input:** nums = [2,1,2], k = 2\n> **Output:** -1\n> **Explanation:** It is impossible to make all the values equal to 2.\n\n## Example 3:\n\n> **Input:** nums = [9,7,5,3], k = 1\n> **Output:** 4\n> **Explanation:** The operations can be performed using valid integers in the order 7, 5, 3, and 1.\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `1 <= nums[i] <= 100`\n- `1 <= k <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table\nAcceptance rate: 73.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn all_ge_k(nums: Seq, k: int) -> bool {\n forall |i: int| 0 <= i < nums.len() ==> nums[i] as int >= k\n }\n\n pub open spec fn value_present(nums: Seq, v: int) -> bool {\n exists |i: int| 0 <= i < nums.len() && nums[i] as int == v\n }\n\n pub open spec fn count_distinct_in_range(nums: Seq, start: int, end: int) -> int\n decreases end - start\n {\n if start >= end {\n 0\n } else {\n (if Self::value_present(nums, start) { 1int } else { 0int })\n + Self::count_distinct_in_range(nums, start + 1, end)\n }\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 100,\n ensures\n -1 <= res <= 100,\n Self::all_ge_k(nums@, k as int) ==> res as int == Self::count_distinct_in_range(nums@, k as int + 1, 101),\n !Self::all_ge_k(nums@, k as int) ==> res == -1,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_operations(nums: Vec, k: i32) -> i32 {\n let mut i: usize = 0;\n while i < nums.len() {\n let x = nums[i];\n if x < k {\n return -1;\n }\n i = i + 1;\n }\n\n let mut ans: i32 = 0;\n let mut value: i32 = k + 1;\n while value <= 100 {\n let mut found: bool = false;\n let mut j: usize = 0;\n while j < nums.len() {\n if nums[j] == value {\n found = true;\n }\n j = j + 1;\n }\n if found {\n ans = ans + 1;\n }\n value = value + 1;\n }\n\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn all_ge_k(nums: Seq, k: int) -> bool {\n forall |i: int| 0 <= i < nums.len() ==> nums[i] as int >= k\n }\n\n pub open spec fn value_present(nums: Seq, v: int) -> bool {\n exists |i: int| 0 <= i < nums.len() && nums[i] as int == v\n }\n\n pub open spec fn count_distinct_in_range(nums: Seq, start: int, end: int) -> int\n decreases end - start\n {\n if start >= end {\n 0\n } else {\n (if Self::value_present(nums, start) { 1int } else { 0int })\n + Self::count_distinct_in_range(nums, start + 1, end)\n }\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 100,\n ensures\n -1 <= res <= 100,\n Self::all_ge_k(nums@, k as int) ==> res as int == Self::count_distinct_in_range(nums@, k as int + 1, 101),\n !Self::all_ge_k(nums@, k as int) ==> res == -1,\n {\n let mut i: usize = 0;\n while i < nums.len() {\n let x = nums[i];\n if x < k {\n return -1;\n }\n i = i + 1;\n }\n\n let mut ans: i32 = 0;\n let mut value: i32 = k + 1;\n while value <= 100 {\n let mut found: bool = false;\n let mut j: usize = 0;\n while j < nums.len() {\n if nums[j] == value {\n found = true;\n }\n j = j + 1;\n }\n if found {\n ans = ans + 1;\n }\n value = value + 1;\n }\n\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn all_ge_k(nums: Seq, k: int) -> bool {\n forall |i: int| 0 <= i < nums.len() ==> nums[i] as int >= k\n }\n\n pub open spec fn value_present(nums: Seq, v: int) -> bool {\n exists |i: int| 0 <= i < nums.len() && nums[i] as int == v\n }\n\n pub open spec fn count_distinct_in_range(nums: Seq, start: int, end: int) -> int\n decreases end - start\n {\n if start >= end {\n 0\n } else {\n (if Self::value_present(nums, start) { 1int } else { 0int })\n + Self::count_distinct_in_range(nums, start + 1, end)\n }\n }\n\n proof fn lemma_count_extend(nums: Seq, start: int, end: int)\n requires\n start <= end <= 101,\n ensures\n Self::count_distinct_in_range(nums, start, end + 1)\n == Self::count_distinct_in_range(nums, start, end)\n + (if Self::value_present(nums, end) { 1int } else { 0int }),\n decreases end - start,\n {\n if start == end {\n assert(Self::count_distinct_in_range(nums, start + 1, end + 1) == 0);\n } else {\n Self::lemma_count_extend(nums, start + 1, end);\n }\n }\n\n proof fn lemma_count_bounded(nums: Seq, start: int, end: int)\n requires\n start <= end <= 101,\n ensures\n 0 <= Self::count_distinct_in_range(nums, start, end) <= end - start,\n decreases end - start,\n {\n if start >= end {\n } else {\n Self::lemma_count_bounded(nums, start + 1, end);\n }\n }\n\n pub fn min_operations(nums: Vec, k: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n 1 <= k <= 100,\n ensures\n -1 <= res <= 100,\n Self::all_ge_k(nums@, k as int) ==> res as int == Self::count_distinct_in_range(nums@, k as int + 1, 101),\n !Self::all_ge_k(nums@, k as int) ==> res == -1,\n {\n let mut i: usize = 0;\n while i < nums.len()\n invariant\n 1 <= nums.len() <= 100,\n forall |t: int| 0 <= t < nums.len() ==> 1 <= #[trigger] nums[t] <= 100,\n 1 <= k <= 100,\n 0 <= i <= nums.len(),\n forall |t: int| 0 <= t < i as int ==> nums@[t] as int >= k as int,\n decreases nums.len() - i,\n {\n let x = nums[i];\n if x < k {\n proof {\n assert(!Self::all_ge_k(nums@, k as int)) by {\n if Self::all_ge_k(nums@, k as int) {\n assert(nums@[i as int] as int >= k as int);\n assert(nums@[i as int] == x);\n assert(x < k);\n assert(false);\n }\n }\n }\n return -1;\n }\n i = i + 1;\n }\n\n proof {\n assert(Self::all_ge_k(nums@, k as int)) by {\n assert(forall |t: int| 0 <= t < nums.len() ==> nums@[t] as int >= k as int) by {\n assert(i == nums.len());\n assert(forall |t: int| 0 <= t < i as int ==> nums@[t] as int >= k as int);\n }\n }\n }\n\n let mut ans: i32 = 0;\n let mut value: i32 = k + 1;\n while value <= 100\n invariant\n 1 <= nums.len() <= 100,\n forall |t: int| 0 <= t < nums.len() ==> 1 <= #[trigger] nums[t] <= 100,\n 1 <= k <= 100,\n Self::all_ge_k(nums@, k as int),\n k + 1 <= value <= 101,\n 0 <= ans <= 100,\n ans as int == Self::count_distinct_in_range(nums@, k as int + 1, value as int),\n decreases 101 - value,\n {\n let mut found: bool = false;\n let mut j: usize = 0;\n while j < nums.len()\n invariant\n 1 <= nums.len() <= 100,\n forall |t: int| 0 <= t < nums.len() ==> 1 <= #[trigger] nums[t] <= 100,\n k + 1 <= value <= 100,\n 0 <= j <= nums.len(),\n found ==> (exists |t: int| 0 <= t < j as int && nums@[t] as int == value as int),\n !found ==> (forall |t: int| 0 <= t < j as int ==> nums@[t] as int != value as int),\n decreases nums.len() - j,\n {\n if nums[j] == value {\n found = true;\n }\n j = j + 1;\n }\n\n proof {\n assert(j == nums.len());\n if found {\n let t = choose |t: int| 0 <= t < j as int && nums@[t] as int == value as int;\n assert(0 <= t < nums.len());\n assert(Self::value_present(nums@, value as int));\n } else {\n assert(!Self::value_present(nums@, value as int)) by {\n if Self::value_present(nums@, value as int) {\n let t = choose |t: int| 0 <= t < nums.len() && nums@[t] as int == value as int;\n assert(0 <= t < j as int);\n assert(nums@[t] as int != value as int);\n assert(false);\n }\n }\n }\n Self::lemma_count_extend(nums@, k as int + 1, value as int);\n }\n\n let ghost old_ans = ans;\n if found {\n ans = ans + 1;\n }\n\n proof {\n if found {\n assert(Self::value_present(nums@, value as int));\n assert(old_ans as int == Self::count_distinct_in_range(nums@, k as int + 1, value as int));\n assert(ans == old_ans + 1);\n assert(Self::count_distinct_in_range(nums@, k as int + 1, value as int + 1)\n == Self::count_distinct_in_range(nums@, k as int + 1, value as int) + 1);\n assert(ans as int == Self::count_distinct_in_range(nums@, k as int + 1, value as int + 1));\n } else {\n assert(!Self::value_present(nums@, value as int));\n assert(ans == old_ans);\n assert(Self::count_distinct_in_range(nums@, k as int + 1, value as int + 1)\n == Self::count_distinct_in_range(nums@, k as int + 1, value as int));\n assert(ans as int == Self::count_distinct_in_range(nums@, k as int + 1, value as int + 1));\n }\n }\n\n proof {\n Self::lemma_count_bounded(nums@, k as int + 1, value as int + 1);\n assert(ans as int == Self::count_distinct_in_range(nums@, k as int + 1, value as int + 1));\n assert(0 <= ans as int <= (value as int + 1) - (k as int + 1));\n assert((value as int + 1) - (k as int + 1) <= 100);\n assert(0 <= ans <= 100);\n }\n\n value = value + 1;\n }\n\n proof {\n Self::lemma_count_bounded(nums@, k as int + 1, 101);\n assert(ans as int == Self::count_distinct_in_range(nums@, k as int + 1, 101));\n assert(ans as int <= 101 - (k as int + 1));\n assert(ans <= 100);\n }\n\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3375/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3375/tests/mutated_testcases.jsonl"} {"problem": "lc3386", "family": "leetcode", "description": "# Button with Longest Push Time\n\nYou are given a 2D array `events` which represents a sequence of events where a child pushes a series of buttons on a keyboard.\n\nEach `events[i] = [index_i, time_i]` indicates that the button at index `index_i` was pressed at time `time_i`.\n\nThe array is sorted in increasing order of `time_i`.\n\nThe time taken to press a button is the difference in time between consecutive button presses. The time for the first button is simply the time at which it was pressed.\n\nReturn the index of the button that took the longest time to push. If multiple buttons have the same longest time, return the smallest index.\n\n## Example 1:\n\n> **Input:** `events = [[1,2],[2,5],[3,9],[1,15]]`\n> **Output:** `1`\n> **Explanation:**\n> - Button with index 1 is pressed at time 2.\n> - Button with index 2 is pressed at time 5, so it took `5 - 2 = 3` units of time.\n> - Button with index 3 is pressed at time 9, so it took `9 - 5 = 4` units of time.\n> - Button with index 1 is pressed at time 15, so it took `15 - 9 = 6` units of time.\n\n## Example 2:\n\n> **Input:** `events = [[10,5],[1,7]]`\n> **Output:** `10`\n> **Explanation:**\n> - Button with index 10 is pressed at time 5.\n> - Button with index 1 is pressed at time 7, so it took `7 - 5 = 2` units of time.\n\n## Constraints:\n\n- `1 <= events.length <= 1000`\n- `events[i] == [index_i, time_i]`\n- `1 <= index_i, time_i <= 10^5`\n- `events` is sorted in increasing order of `time_i`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn button_with_longest_time(events: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 41.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_event(e: Seq) -> bool {\n e.len() == 2 && 1 <= e[0] <= 100000 && 1 <= e[1] <= 100000\n }\n\n pub open spec fn press_time(events: Seq>, i: int) -> int\n recommends\n 0 <= i < events.len(),\n forall |k: int| 0 <= k < events.len() ==> Self::valid_event(events[k]@),\n {\n if i == 0 {\n events[0][1] as int\n } else {\n events[i][1] as int - events[i - 1][1] as int\n }\n }\n\n pub fn button_with_longest_time(events: Vec>) -> (result: i32)\n requires\n 1 <= events.len() <= 1000,\n forall |i: int| 0 <= i < events.len() ==> Self::valid_event(events[i]@),\n forall |i: int, j: int|\n 0 <= i < j < events.len()\n && Self::valid_event(events[i]@)\n && Self::valid_event(events[j]@)\n ==> events[i][1] < events[j][1],\n ensures\n exists |best_pos: int|\n 0 <= best_pos < events.len()\n && Self::valid_event(events[best_pos]@)\n && result == events[best_pos][0]\n && forall |j: int|\n 0 <= j < events.len() && Self::valid_event(events[j]@)\n ==> Self::press_time(events@, j) < Self::press_time(events@, best_pos)\n || (Self::press_time(events@, j) == Self::press_time(events@, best_pos)\n && result <= events[j][0]),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn button_with_longest_time(events: Vec>) -> i32 {\n let n = events.len();\n let mut best_idx: i32 = events[0][0];\n let mut best_time: i32 = events[0][1];\n let mut prev_time: i32 = events[0][1];\n let mut i: usize = 1;\n while i < n {\n let current_idx: i32 = events[i][0];\n let current_time: i32 = events[i][1] - prev_time;\n if current_time > best_time {\n best_time = current_time;\n best_idx = current_idx;\n } else if current_time == best_time && current_idx < best_idx {\n best_idx = current_idx;\n }\n prev_time = events[i][1];\n i = i + 1;\n }\n best_idx\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_event(e: Seq) -> bool {\n e.len() == 2 && 1 <= e[0] <= 100000 && 1 <= e[1] <= 100000\n }\n\n pub open spec fn press_time(events: Seq>, i: int) -> int\n recommends\n 0 <= i < events.len(),\n forall |k: int| 0 <= k < events.len() ==> Self::valid_event(events[k]@),\n {\n if i == 0 {\n events[0][1] as int\n } else {\n events[i][1] as int - events[i - 1][1] as int\n }\n }\n\n pub fn button_with_longest_time(events: Vec>) -> (result: i32)\n requires\n 1 <= events.len() <= 1000,\n forall |i: int| 0 <= i < events.len() ==> Self::valid_event(events[i]@),\n forall |i: int, j: int|\n 0 <= i < j < events.len()\n && Self::valid_event(events[i]@)\n && Self::valid_event(events[j]@)\n ==> events[i][1] < events[j][1],\n ensures\n exists |best_pos: int|\n 0 <= best_pos < events.len()\n && Self::valid_event(events[best_pos]@)\n && result == events[best_pos][0]\n && forall |j: int|\n 0 <= j < events.len() && Self::valid_event(events[j]@)\n ==> Self::press_time(events@, j) < Self::press_time(events@, best_pos)\n || (Self::press_time(events@, j) == Self::press_time(events@, best_pos)\n && result <= events[j][0]),\n {\n let n = events.len();\n let mut best_idx: i32 = events[0][0];\n let mut best_time: i32 = events[0][1];\n let mut prev_time: i32 = events[0][1];\n let mut i: usize = 1;\n while i < n {\n let current_idx: i32 = events[i][0];\n let current_time: i32 = events[i][1] - prev_time;\n if current_time > best_time {\n best_time = current_time;\n best_idx = current_idx;\n } else if current_time == best_time && current_idx < best_idx {\n best_idx = current_idx;\n }\n prev_time = events[i][1];\n i = i + 1;\n }\n best_idx\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn valid_event(e: Seq) -> bool {\n e.len() == 2 && 1 <= e[0] <= 100000 && 1 <= e[1] <= 100000\n }\n\n pub open spec fn press_time(events: Seq>, i: int) -> int\n recommends\n 0 <= i < events.len(),\n forall |k: int| 0 <= k < events.len() ==> Self::valid_event(events[k]@),\n {\n if i == 0 {\n events[0][1] as int\n } else {\n events[i][1] as int - events[i - 1][1] as int\n }\n }\n\n pub fn button_with_longest_time(events: Vec>) -> (result: i32)\n requires\n 1 <= events.len() <= 1000,\n forall |i: int| 0 <= i < events.len() ==> Self::valid_event(events[i]@),\n forall |i: int, j: int|\n 0 <= i < j < events.len()\n && Self::valid_event(events[i]@)\n && Self::valid_event(events[j]@)\n ==> events[i][1] < events[j][1],\n ensures\n exists |best_pos: int|\n 0 <= best_pos < events.len()\n && Self::valid_event(events[best_pos]@)\n && result == events[best_pos][0]\n && forall |j: int|\n 0 <= j < events.len() && Self::valid_event(events[j]@)\n ==> Self::press_time(events@, j) < Self::press_time(events@, best_pos)\n || (Self::press_time(events@, j) == Self::press_time(events@, best_pos)\n && result <= events[j][0]),\n {\n proof {\n assert(events.len() >= 1);\n assert(Self::valid_event(events[0]@));\n }\n\n let n = events.len();\n let mut best_idx: i32 = events[0][0];\n let mut best_time: i32 = events[0][1];\n let mut prev_time: i32 = events[0][1];\n let ghost mut best_pos: int = 0;\n\n proof {\n assert(best_time as int == Self::press_time(events@, 0));\n assert(best_idx == events[0][0]);\n assert forall |k: int| 0 <= k < 1 && Self::valid_event(events[k]@) implies\n Self::press_time(events@, k) < best_time as int\n || (Self::press_time(events@, k) == best_time as int && best_idx <= events[k][0]) by {\n assert(k == 0);\n };\n }\n\n let mut i: usize = 1;\n while i < n\n invariant\n n == events.len(),\n 1 <= n <= 1000,\n forall |k: int| 0 <= k < events.len() ==> Self::valid_event(events[k]@),\n 1 <= i <= n,\n prev_time == events[i - 1][1],\n 0 <= best_pos < i as int,\n best_idx == events[best_pos][0],\n best_time as int == Self::press_time(events@, best_pos),\n forall |k: int|\n 0 <= k < i as int && Self::valid_event(events[k]@)\n ==> Self::press_time(events@, k) < best_time as int\n || (Self::press_time(events@, k) == best_time as int\n && best_idx <= events[k][0]),\n decreases n - i,\n {\n proof {\n assert(Self::valid_event(events[i as int]@));\n assert(Self::valid_event(events[i as int - 1]@));\n }\n\n let current_idx: i32 = events[i][0];\n let current_time: i32 = events[i][1] - prev_time;\n\n proof {\n assert(prev_time == events[i as int - 1][1]);\n assert(current_time as int == events[i as int][1] as int - events[i as int - 1][1] as int);\n assert(current_time as int == Self::press_time(events@, i as int));\n }\n\n let ghost old_best_pos = best_pos;\n let ghost old_best_idx = best_idx;\n let ghost old_best_time = best_time as int;\n\n if current_time > best_time {\n best_time = current_time;\n best_idx = current_idx;\n proof {\n best_pos = i as int;\n }\n proof {\n assert(best_time as int == Self::press_time(events@, best_pos));\n assert(best_idx == events[best_pos][0]);\n assert forall |k: int| 0 <= k < i as int + 1 && Self::valid_event(events[k]@) implies\n Self::press_time(events@, k) < best_time as int\n || (Self::press_time(events@, k) == best_time as int\n && best_idx <= events[k][0]) by {\n if k < i as int {\n assert(Self::press_time(events@, k) < old_best_time\n || (Self::press_time(events@, k) == old_best_time\n && old_best_idx <= events[k][0]));\n if Self::press_time(events@, k) == old_best_time {\n assert(Self::press_time(events@, k) < best_time as int);\n }\n if Self::press_time(events@, k) < old_best_time {\n assert(Self::press_time(events@, k) < best_time as int);\n }\n } else {\n assert(k == i as int);\n assert(Self::press_time(events@, k) == best_time as int);\n assert(best_idx == events[k][0]);\n }\n };\n }\n } else if current_time == best_time && current_idx < best_idx {\n best_idx = current_idx;\n proof {\n best_pos = i as int;\n }\n proof {\n assert(best_time as int == old_best_time);\n assert(best_time as int == Self::press_time(events@, best_pos));\n assert(best_idx == events[best_pos][0]);\n assert forall |k: int| 0 <= k < i as int + 1 && Self::valid_event(events[k]@) implies\n Self::press_time(events@, k) < best_time as int\n || (Self::press_time(events@, k) == best_time as int\n && best_idx <= events[k][0]) by {\n if k < i as int {\n assert(Self::press_time(events@, k) < old_best_time\n || (Self::press_time(events@, k) == old_best_time\n && old_best_idx <= events[k][0]));\n if Self::press_time(events@, k) == old_best_time {\n assert(best_idx < old_best_idx);\n assert(best_idx <= events[k][0]);\n }\n } else {\n assert(k == i as int);\n assert(Self::press_time(events@, k) == best_time as int);\n assert(best_idx == events[k][0]);\n }\n };\n }\n } else {\n proof {\n assert(best_pos == old_best_pos);\n assert(best_time as int == old_best_time);\n assert(best_idx == old_best_idx);\n assert forall |k: int| 0 <= k < i as int + 1 && Self::valid_event(events[k]@) implies\n Self::press_time(events@, k) < best_time as int\n || (Self::press_time(events@, k) == best_time as int\n && best_idx <= events[k][0]) by {\n if k < i as int {\n assert(Self::press_time(events@, k) < old_best_time\n || (Self::press_time(events@, k) == old_best_time\n && old_best_idx <= events[k][0]));\n } else {\n assert(k == i as int);\n assert(Self::press_time(events@, k) == current_time as int);\n assert(current_time as int <= best_time as int);\n if Self::press_time(events@, k) == best_time as int {\n assert(current_time == best_time);\n assert(!(current_idx < best_idx));\n assert(best_idx <= events[k][0]);\n }\n }\n };\n }\n }\n\n prev_time = events[i][1];\n i = i + 1;\n }\n\n proof {\n assert(i == n);\n assert(exists |p: int|\n 0 <= p < events.len()\n && Self::valid_event(events[p]@)\n && best_idx == events[p][0]\n && forall |j: int|\n 0 <= j < events.len() && Self::valid_event(events[j]@)\n ==> Self::press_time(events@, j) < Self::press_time(events@, p)\n || (Self::press_time(events@, j) == Self::press_time(events@, p)\n && best_idx <= events[j][0])) by {\n let p = best_pos;\n assert(0 <= p < events.len());\n assert(Self::valid_event(events[p]@));\n assert(best_idx == events[p][0]);\n assert forall |j: int|\n 0 <= j < events.len() && Self::valid_event(events[j]@)\n implies Self::press_time(events@, j) < Self::press_time(events@, p)\n || (Self::press_time(events@, j) == Self::press_time(events@, p)\n && best_idx <= events[j][0]) by {\n assert(j < i as int);\n assert(Self::press_time(events@, j) < best_time as int\n || (Self::press_time(events@, j) == best_time as int\n && best_idx <= events[j][0]));\n assert(Self::press_time(events@, p) == best_time as int);\n };\n };\n }\n\n best_idx\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3386/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3386/tests/mutated_testcases.jsonl"} {"problem": "lc3423", "family": "leetcode", "description": "# Maximum Difference Between Adjacent Elements in a Circular Array\n\nGiven a **circular** array `nums`, find the **maximum absolute difference** between adjacent elements.\n\n**Note:** In a circular array, the first and last elements are adjacent.\n\n## Examples\n\n**Example 1:**\n\n**Input:** nums = [1,2,4]\n\n**Output:** 3\n\n**Explanation:**\n\nBecause `nums` is circular, `nums[0]` and `nums[2]` are adjacent. They have the maximum absolute difference of `|4 - 1| = 3`.\n\n**Example 2:**\n\n**Input:** nums = [-5,-10,-5]\n\n**Output:** 5\n\n**Explanation:**\n\nThe adjacent elements `nums[0]` and `nums[1]` have the maximum absolute difference of `|-5 - (-10)| = 5`.\n\n## Constraints\n\n- `2 <= nums.length <= 100`\n- `-100 <= nums[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_adjacent_distance(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\n75.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_abs(x: int) -> int {\n if x < 0 { -x } else { x }\n}\n\npub open spec fn spec_adj_diff(nums: Seq, n: int, j: int) -> int {\n if j == 0 {\n spec_abs(nums[0] as int - nums[n - 1] as int)\n } else {\n spec_abs(nums[j] as int - nums[j - 1] as int)\n }\n}\n\npub open spec fn spec_max_diff(nums: Seq, n: int, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else if spec_adj_diff(nums, n, k - 1) > spec_max_diff(nums, n, k - 1) {\n spec_adj_diff(nums, n, k - 1)\n } else {\n spec_max_diff(nums, n, k - 1)\n }\n}\n\nimpl Solution {\n pub fn max_adjacent_distance(nums: Vec) -> (diff: i32)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n diff == spec_max_diff(nums@, nums.len() as int, nums.len() as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn max_adjacent_distance(nums: Vec) -> i32 {\n let n = nums.len();\n let mut d = nums[0] - nums[n - 1];\n if d < 0 {\n d = -d;\n }\n let mut diff = d;\n let mut i: usize = 1;\n while i < n {\n let mut d = nums[i] - nums[i - 1];\n if d < 0 {\n d = -d;\n }\n if d > diff {\n diff = d;\n }\n i += 1;\n }\n diff\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_abs(x: int) -> int {\n if x < 0 { -x } else { x }\n}\n\npub open spec fn spec_adj_diff(nums: Seq, n: int, j: int) -> int {\n if j == 0 {\n spec_abs(nums[0] as int - nums[n - 1] as int)\n } else {\n spec_abs(nums[j] as int - nums[j - 1] as int)\n }\n}\n\npub open spec fn spec_max_diff(nums: Seq, n: int, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else if spec_adj_diff(nums, n, k - 1) > spec_max_diff(nums, n, k - 1) {\n spec_adj_diff(nums, n, k - 1)\n } else {\n spec_max_diff(nums, n, k - 1)\n }\n}\n\nimpl Solution {\n pub fn max_adjacent_distance(nums: Vec) -> (diff: i32)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n diff == spec_max_diff(nums@, nums.len() as int, nums.len() as int),\n {\n let n = nums.len();\n let mut d = nums[0] - nums[n - 1];\n if d < 0 {\n d = -d;\n }\n let mut diff = d;\n let mut i: usize = 1;\n while i < n {\n let mut d = nums[i] - nums[i - 1];\n if d < 0 {\n d = -d;\n }\n if d > diff {\n diff = d;\n }\n i += 1;\n }\n diff\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn spec_abs(x: int) -> int {\n if x < 0 { -x } else { x }\n}\n\npub open spec fn spec_adj_diff(nums: Seq, n: int, j: int) -> int {\n if j == 0 {\n spec_abs(nums[0] as int - nums[n - 1] as int)\n } else {\n spec_abs(nums[j] as int - nums[j - 1] as int)\n }\n}\n\npub open spec fn spec_max_diff(nums: Seq, n: int, k: int) -> int\n decreases k,\n{\n if k <= 0 {\n 0\n } else if spec_adj_diff(nums, n, k - 1) > spec_max_diff(nums, n, k - 1) {\n spec_adj_diff(nums, n, k - 1)\n } else {\n spec_max_diff(nums, n, k - 1)\n }\n}\n\nproof fn lemma_max_diff_bounds(nums: Seq, n: int, k: int)\n requires\n 0 <= k <= n,\n n == nums.len(),\n 2 <= n <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n 0 <= spec_max_diff(nums, n, k) <= 200,\n decreases k,\n{\n if k <= 0 {\n } else {\n lemma_max_diff_bounds(nums, n, k - 1);\n }\n}\n\nimpl Solution {\n pub fn max_adjacent_distance(nums: Vec) -> (diff: i32)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> -100 <= #[trigger] nums[i] <= 100,\n ensures\n diff == spec_max_diff(nums@, nums.len() as int, nums.len() as int),\n {\n let n = nums.len();\n let mut d = nums[0] - nums[n - 1];\n if d < 0 {\n d = -d;\n }\n assert(d as int == spec_adj_diff(nums@, n as int, 0int));\n let mut diff = d;\n assert(spec_max_diff(nums@, n as int, 0int) == 0);\n assert(d >= 0);\n assert(diff as int == spec_max_diff(nums@, n as int, 1int));\n let mut i: usize = 1;\n while i < n\n invariant\n n == nums.len(),\n 2 <= n <= 100,\n forall |j: int| 0 <= j < nums.len() ==> -100 <= #[trigger] nums[j] <= 100,\n 1 <= i <= n,\n diff as int == spec_max_diff(nums@, n as int, i as int),\n 0 <= diff <= 200,\n decreases n - i,\n {\n proof {\n lemma_max_diff_bounds(nums@, n as int, i as int);\n }\n let mut d = nums[i] - nums[i - 1];\n if d < 0 {\n d = -d;\n }\n assert(d as int == spec_adj_diff(nums@, n as int, i as int));\n if d > diff {\n diff = d;\n }\n i += 1;\n }\n diff\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc3423/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3423/tests/mutated_testcases.jsonl"} {"problem": "lc3477", "family": "leetcode", "description": "# Fruits Into Baskets II\n\nYou are given two integer arrays, `fruits` and `baskets`, each of length `n`, where:\n\n- `fruits[i]` is the quantity of the `i`th type of fruit.\n- `baskets[j]` is the capacity of the `j`th basket.\n\nFrom left to right, place the fruits according to these rules:\n\n- Each fruit type must be placed in the leftmost available basket whose capacity is at least that fruit quantity.\n- Each basket can hold only one type of fruit.\n- If a fruit type cannot be placed in any basket, it remains unplaced.\n\nReturn the number of fruit types that remain unplaced after all possible placements.\n\n## Example 1:\n\n> **Input:** fruits = [4,2,5], baskets = [3,5,4]\n> **Output:** 1\n>\n> **Explanation:**\n>\n> - `fruits[0] = 4` is placed in `baskets[1] = 5`.\n> - `fruits[1] = 2` is placed in `baskets[0] = 3`.\n> - `fruits[2] = 5` cannot be placed in `baskets[2] = 4`.\n\n## Example 2:\n\n> **Input:** fruits = [3,6,1], baskets = [6,4,7]\n> **Output:** 0\n>\n> **Explanation:**\n>\n> - `fruits[0] = 3` is placed in `baskets[0] = 6`.\n> - `fruits[1] = 6` is placed in `baskets[2] = 7`.\n> - `fruits[2] = 1` is placed in `baskets[1] = 4`.\n\n## Constraints:\n\n- `n == fruits.length == baskets.length`\n- `1 <= n <= 100`\n- `1 <= fruits[i], baskets[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec, baskets: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Binary Search, Segment Tree, Simulation, Ordered Set\nAcceptance rate: 70.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_eligible(used: Seq, baskets: Seq, fruit: i32, idx: int) -> bool {\n 0 <= idx < baskets.len() && idx < used.len() && used[idx] == 0 && baskets[idx] >= fruit\n}\n\npub open spec fn choose_basket(used: Seq, baskets: Seq, fruit: i32, j: int) -> int\n recommends\n used.len() == baskets.len(),\n 0 <= j <= baskets.len(),\n decreases baskets.len() - j,\n{\n if j >= baskets.len() {\n -1\n } else if is_eligible(used, baskets, fruit, j) {\n j\n } else {\n choose_basket(used, baskets, fruit, j + 1)\n }\n}\n\npub open spec fn spec_used_prefix(fruits: Seq, baskets: Seq, k: int) -> Seq\n recommends\n fruits.len() == baskets.len(),\n 0 <= k <= fruits.len(),\n decreases k,\n{\n if k <= 0 {\n Seq::new(baskets.len(), |i: int| 0i32)\n } else {\n let prev = spec_used_prefix(fruits, baskets, k - 1);\n let c = choose_basket(prev, baskets, fruits[k - 1], 0);\n if 0 <= c < prev.len() {\n prev.update(c, 1i32)\n } else {\n prev\n }\n }\n}\n\npub open spec fn spec_unplaced_prefix(fruits: Seq, baskets: Seq, k: int) -> int\n recommends\n fruits.len() == baskets.len(),\n 0 <= k <= fruits.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n let prev_used = spec_used_prefix(fruits, baskets, k - 1);\n let c = choose_basket(prev_used, baskets, fruits[k - 1], 0);\n spec_unplaced_prefix(fruits, baskets, k - 1) + if c < 0 { 1int } else { 0int }\n }\n}\n\nimpl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec, baskets: Vec) -> (result: i32)\n requires\n 1 <= fruits.len() <= 100,\n fruits.len() == baskets.len(),\n forall |i: int| 0 <= i < fruits.len() ==> 1 <= #[trigger] fruits[i] <= 1000,\n forall |i: int| 0 <= i < baskets.len() ==> 1 <= #[trigger] baskets[i] <= 1000,\n ensures\n result as int == spec_unplaced_prefix(fruits@, baskets@, fruits.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec, baskets: Vec) -> i32 {\n let n = fruits.len();\n let mut used: Vec = vec![0; n];\n let mut unplaced: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let fruit = fruits[i];\n let mut placed_idx: i32 = -1;\n let mut j: usize = 0;\n while j < n {\n if placed_idx == -1 && used[j] == 0 && baskets[j] >= fruit {\n used[j] = 1;\n placed_idx = j as i32;\n }\n j += 1;\n }\n if placed_idx == -1 {\n unplaced += 1;\n }\n i += 1;\n }\n unplaced\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_eligible(used: Seq, baskets: Seq, fruit: i32, idx: int) -> bool {\n 0 <= idx < baskets.len() && idx < used.len() && used[idx] == 0 && baskets[idx] >= fruit\n}\n\npub open spec fn choose_basket(used: Seq, baskets: Seq, fruit: i32, j: int) -> int\n recommends\n used.len() == baskets.len(),\n 0 <= j <= baskets.len(),\n decreases baskets.len() - j,\n{\n if j >= baskets.len() {\n -1\n } else if is_eligible(used, baskets, fruit, j) {\n j\n } else {\n choose_basket(used, baskets, fruit, j + 1)\n }\n}\n\npub open spec fn spec_used_prefix(fruits: Seq, baskets: Seq, k: int) -> Seq\n recommends\n fruits.len() == baskets.len(),\n 0 <= k <= fruits.len(),\n decreases k,\n{\n if k <= 0 {\n Seq::new(baskets.len(), |i: int| 0i32)\n } else {\n let prev = spec_used_prefix(fruits, baskets, k - 1);\n let c = choose_basket(prev, baskets, fruits[k - 1], 0);\n if 0 <= c < prev.len() {\n prev.update(c, 1i32)\n } else {\n prev\n }\n }\n}\n\npub open spec fn spec_unplaced_prefix(fruits: Seq, baskets: Seq, k: int) -> int\n recommends\n fruits.len() == baskets.len(),\n 0 <= k <= fruits.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n let prev_used = spec_used_prefix(fruits, baskets, k - 1);\n let c = choose_basket(prev_used, baskets, fruits[k - 1], 0);\n spec_unplaced_prefix(fruits, baskets, k - 1) + if c < 0 { 1int } else { 0int }\n }\n}\n\nimpl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec, baskets: Vec) -> (result: i32)\n requires\n 1 <= fruits.len() <= 100,\n fruits.len() == baskets.len(),\n forall |i: int| 0 <= i < fruits.len() ==> 1 <= #[trigger] fruits[i] <= 1000,\n forall |i: int| 0 <= i < baskets.len() ==> 1 <= #[trigger] baskets[i] <= 1000,\n ensures\n result as int == spec_unplaced_prefix(fruits@, baskets@, fruits.len() as int),\n {\n let n = fruits.len();\n let mut used: Vec = vec![0; n];\n let mut unplaced: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let fruit = fruits[i];\n let mut placed_idx: i32 = -1;\n let mut j: usize = 0;\n while j < n {\n if placed_idx == -1 && used[j] == 0 && baskets[j] >= fruit {\n used.set(j, 1);\n placed_idx = j as i32;\n }\n j += 1;\n }\n if placed_idx == -1 {\n unplaced += 1;\n }\n i += 1;\n }\n unplaced\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_eligible(used: Seq, baskets: Seq, fruit: i32, idx: int) -> bool {\n 0 <= idx < baskets.len() && idx < used.len() && used[idx] == 0 && baskets[idx] >= fruit\n}\n\npub open spec fn choose_basket(used: Seq, baskets: Seq, fruit: i32, j: int) -> int\n recommends\n used.len() == baskets.len(),\n 0 <= j <= baskets.len(),\n decreases baskets.len() - j,\n{\n if j >= baskets.len() {\n -1\n } else if is_eligible(used, baskets, fruit, j) {\n j\n } else {\n choose_basket(used, baskets, fruit, j + 1)\n }\n}\n\npub open spec fn spec_used_prefix(fruits: Seq, baskets: Seq, k: int) -> Seq\n recommends\n fruits.len() == baskets.len(),\n 0 <= k <= fruits.len(),\n decreases k,\n{\n if k <= 0 {\n Seq::new(baskets.len(), |i: int| 0i32)\n } else {\n let prev = spec_used_prefix(fruits, baskets, k - 1);\n let c = choose_basket(prev, baskets, fruits[k - 1], 0);\n if 0 <= c < prev.len() {\n prev.update(c, 1i32)\n } else {\n prev\n }\n }\n}\n\npub open spec fn spec_unplaced_prefix(fruits: Seq, baskets: Seq, k: int) -> int\n recommends\n fruits.len() == baskets.len(),\n 0 <= k <= fruits.len(),\n decreases k,\n{\n if k <= 0 {\n 0\n } else {\n let prev_used = spec_used_prefix(fruits, baskets, k - 1);\n let c = choose_basket(prev_used, baskets, fruits[k - 1], 0);\n spec_unplaced_prefix(fruits, baskets, k - 1) + if c < 0 { 1int } else { 0int }\n }\n}\n\nproof fn lemma_choose_characterization(used: Seq, baskets: Seq, fruit: i32, j: int)\n requires\n used.len() == baskets.len(),\n 0 <= j <= baskets.len(),\n ensures\n choose_basket(used, baskets, fruit, j) == -1 ==> forall |t: int| j <= t < baskets.len() ==> !#[trigger] is_eligible(used, baskets, fruit, t),\n choose_basket(used, baskets, fruit, j) != -1 ==> (\n j <= choose_basket(used, baskets, fruit, j) < baskets.len()\n && is_eligible(used, baskets, fruit, choose_basket(used, baskets, fruit, j))\n && forall |t: int| j <= t < choose_basket(used, baskets, fruit, j) ==> !#[trigger] is_eligible(used, baskets, fruit, t)\n ),\n decreases baskets.len() - j,\n{\n if j < baskets.len() {\n if is_eligible(used, baskets, fruit, j) {\n } else {\n lemma_choose_characterization(used, baskets, fruit, j + 1);\n let c = choose_basket(used, baskets, fruit, j + 1);\n assert(choose_basket(used, baskets, fruit, j) == c);\n if c == -1 {\n assert forall |t: int| j <= t < baskets.len() implies !is_eligible(used, baskets, fruit, t) by {\n if t == j {\n assert(!is_eligible(used, baskets, fruit, j));\n } else {\n assert(j + 1 <= t < baskets.len());\n }\n }\n } else {\n assert(j < c < baskets.len());\n assert(is_eligible(used, baskets, fruit, c));\n assert forall |t: int| j <= t < c implies !is_eligible(used, baskets, fruit, t) by {\n if t == j {\n assert(!is_eligible(used, baskets, fruit, j));\n } else {\n assert(j + 1 <= t < c);\n }\n }\n }\n }\n }\n}\n\nproof fn lemma_choose_unique(used: Seq, baskets: Seq, fruit: i32, j: int, idx: int)\n requires\n used.len() == baskets.len(),\n 0 <= j <= idx < baskets.len(),\n is_eligible(used, baskets, fruit, idx),\n forall |t: int| j <= t < idx ==> !#[trigger] is_eligible(used, baskets, fruit, t),\n ensures\n choose_basket(used, baskets, fruit, j) == idx,\n{\n lemma_choose_characterization(used, baskets, fruit, j);\n let c = choose_basket(used, baskets, fruit, j);\n if c == -1 {\n assert(!is_eligible(used, baskets, fruit, idx));\n assert(false);\n } else {\n if c < idx {\n assert(is_eligible(used, baskets, fruit, c));\n assert(!is_eligible(used, baskets, fruit, c));\n assert(false);\n }\n if idx < c {\n assert(!is_eligible(used, baskets, fruit, idx));\n assert(false);\n }\n assert(c == idx);\n }\n}\n\nproof fn lemma_prefix_step(fruits: Seq, baskets: Seq, k: int)\n requires\n fruits.len() == baskets.len(),\n 0 <= k < fruits.len(),\n ensures\n spec_used_prefix(fruits, baskets, k + 1) == {\n let prev = spec_used_prefix(fruits, baskets, k);\n let c = choose_basket(prev, baskets, fruits[k], 0);\n if 0 <= c < prev.len() {\n prev.update(c, 1i32)\n } else {\n prev\n }\n },\n spec_unplaced_prefix(fruits, baskets, k + 1) == {\n let prev_used = spec_used_prefix(fruits, baskets, k);\n let c = choose_basket(prev_used, baskets, fruits[k], 0);\n spec_unplaced_prefix(fruits, baskets, k) + if c < 0 { 1int } else { 0int }\n },\n{\n reveal_with_fuel(spec_used_prefix, 2);\n reveal_with_fuel(spec_unplaced_prefix, 2);\n}\n\nimpl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec, baskets: Vec) -> (result: i32)\n requires\n 1 <= fruits.len() <= 100,\n fruits.len() == baskets.len(),\n forall |i: int| 0 <= i < fruits.len() ==> 1 <= #[trigger] fruits[i] <= 1000,\n forall |i: int| 0 <= i < baskets.len() ==> 1 <= #[trigger] baskets[i] <= 1000,\n ensures\n result as int == spec_unplaced_prefix(fruits@, baskets@, fruits.len() as int),\n {\n let n = fruits.len();\n let mut used: Vec = vec![0; n];\n let mut unplaced: i32 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == fruits.len(),\n n == baskets.len(),\n 1 <= n <= 100,\n forall |k: int| 0 <= k < fruits.len() ==> 1 <= #[trigger] fruits[k] <= 1000,\n forall |k: int| 0 <= k < baskets.len() ==> 1 <= #[trigger] baskets[k] <= 1000,\n 0 <= i <= n,\n used.len() == n,\n used@ == spec_used_prefix(fruits@, baskets@, i as int),\n unplaced as int == spec_unplaced_prefix(fruits@, baskets@, i as int),\n 0 <= unplaced as int <= i as int,\n decreases n - i,\n {\n let fruit = fruits[i];\n let ghost prev_used = used@;\n let mut placed_idx: i32 = -1;\n let mut j: usize = 0;\n while j < n\n invariant\n n == fruits.len(),\n n == baskets.len(),\n 1 <= n <= 100,\n 0 <= i < n,\n 1 <= fruit <= 1000,\n used.len() == n,\n prev_used.len() == n,\n forall |k: int| 0 <= k < baskets.len() ==> 1 <= #[trigger] baskets[k] <= 1000,\n 0 <= j <= n,\n -1 <= placed_idx,\n placed_idx < j as int,\n placed_idx == -1 ==> (\n used@ == prev_used\n && forall |t: int| 0 <= t < j ==> !#[trigger] is_eligible(prev_used, baskets@, fruit, t)\n ),\n placed_idx != -1 ==> (\n 0 <= placed_idx < j as int\n && is_eligible(prev_used, baskets@, fruit, placed_idx as int)\n && used@ == prev_used.update(placed_idx as int, 1i32)\n && forall |t: int| 0 <= t < placed_idx as int ==> !#[trigger] is_eligible(prev_used, baskets@, fruit, t)\n ),\n decreases n - j,\n {\n if placed_idx == -1 && used[j] == 0 && baskets[j] >= fruit {\n used.set(j, 1);\n placed_idx = j as i32;\n proof {\n assert(used@ == prev_used.update(j as int, 1i32));\n assert(is_eligible(prev_used, baskets@, fruit, j as int));\n }\n }\n j += 1;\n }\n proof {\n let chosen = choose_basket(prev_used, baskets@, fruit, 0);\n lemma_choose_characterization(prev_used, baskets@, fruit, 0);\n if placed_idx == -1 {\n assert(used@ == prev_used);\n assert(forall |t: int| 0 <= t < n ==> !#[trigger] is_eligible(prev_used, baskets@, fruit, t));\n if chosen != -1 {\n assert(0 <= chosen < n);\n assert(is_eligible(prev_used, baskets@, fruit, chosen));\n assert(!is_eligible(prev_used, baskets@, fruit, chosen));\n assert(false);\n }\n assert(chosen == -1);\n } else {\n assert(0 <= placed_idx < n as int);\n assert(is_eligible(prev_used, baskets@, fruit, placed_idx as int));\n assert(forall |t: int| 0 <= t < placed_idx as int ==> !#[trigger] is_eligible(prev_used, baskets@, fruit, t));\n lemma_choose_unique(prev_used, baskets@, fruit, 0, placed_idx as int);\n assert(chosen == placed_idx as int);\n assert(used@ == prev_used.update(chosen, 1i32));\n }\n lemma_prefix_step(fruits@, baskets@, i as int);\n }\n if placed_idx == -1 {\n unplaced += 1;\n }\n proof {\n let chosen = choose_basket(prev_used, baskets@, fruit, 0);\n if placed_idx == -1 {\n assert(chosen == -1);\n assert(used@ == prev_used);\n } else {\n assert(chosen >= 0);\n assert(used@ == prev_used.update(chosen, 1i32));\n }\n assert(used@ == spec_used_prefix(fruits@, baskets@, (i + 1) as int));\n if placed_idx == -1 {\n assert(unplaced as int == spec_unplaced_prefix(fruits@, baskets@, i as int) + 1);\n } else {\n assert(unplaced as int == spec_unplaced_prefix(fruits@, baskets@, i as int));\n }\n assert(unplaced as int == spec_unplaced_prefix(fruits@, baskets@, (i + 1) as int));\n assert(0 <= unplaced as int <= (i + 1) as int);\n }\n i += 1;\n }\n unplaced\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3477/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3477/tests/mutated_testcases.jsonl"} {"problem": "lc3492", "family": "leetcode", "description": "# Maximum Containers on a Ship\n\nYou are given positive integers `n`, `w`, and `maxWeight`.\n\nThere is an `n x n` cargo deck on a ship. Each cell can hold one container, and each container weighs exactly `w`.\n\nThe total weight of loaded containers must be at most `maxWeight`.\n\nReturn the maximum number of containers that can be loaded.\n\n## Example 1:\n\n> **Input:** n = 2, w = 3, maxWeight = 15 \n> **Output:** 4 \n> **Explanation:** The deck has 4 cells. Loading all 4 containers weighs 12, which is within `maxWeight`.\n\n## Example 2:\n\n> **Input:** n = 3, w = 5, maxWeight = 20 \n> **Output:** 4 \n> **Explanation:** The deck has 9 cells, but 5 containers would weigh 25. So the maximum feasible number is 4.\n\n## Constraints:\n\n- `1 <= n <= 1000`\n- `1 <= w <= 1000`\n- `1 <= maxWeight <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_containers(n: i32, w: i32, max_weight: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 75.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_containers_spec(n: int, w: int, max_weight: int) -> int {\n let area = n * n;\n let by_weight = max_weight / w;\n if area <= by_weight {\n area\n } else {\n by_weight\n }\n }\n\n pub fn max_containers(n: i32, w: i32, max_weight: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n 1 <= w <= 1000,\n 1 <= max_weight <= 1_000_000_000,\n ensures\n result as int == Self::max_containers_spec(n as int, w as int, max_weight as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_containers(n: i32, w: i32, max_weight: i32) -> i32 {\n let area = n * n;\n let by_weight = max_weight / w;\n if area <= by_weight {\n area\n } else {\n by_weight\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_containers_spec(n: int, w: int, max_weight: int) -> int {\n let area = n * n;\n let by_weight = max_weight / w;\n if area <= by_weight {\n area\n } else {\n by_weight\n }\n }\n\n pub fn max_containers(n: i32, w: i32, max_weight: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n 1 <= w <= 1000,\n 1 <= max_weight <= 1_000_000_000,\n ensures\n result as int == Self::max_containers_spec(n as int, w as int, max_weight as int),\n {\n let area = n * n;\n let by_weight = max_weight / w;\n if area <= by_weight {\n area\n } else {\n by_weight\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_containers_spec(n: int, w: int, max_weight: int) -> int {\n let area = n * n;\n let by_weight = max_weight / w;\n if area <= by_weight {\n area\n } else {\n by_weight\n }\n }\n\n pub fn max_containers(n: i32, w: i32, max_weight: i32) -> (result: i32)\n requires\n 1 <= n <= 1000,\n 1 <= w <= 1000,\n 1 <= max_weight <= 1_000_000_000,\n ensures\n result as int == Self::max_containers_spec(n as int, w as int, max_weight as int),\n {\n assert(0 < n * n <= 1_000_000) by (nonlinear_arith)\n requires\n 1 <= n <= 1000,\n {\n }\n let area = n * n;\n let by_weight = max_weight / w;\n\n assert(area as int == n as int * n as int);\n assert(by_weight as int == max_weight as int / w as int);\n\n if area <= by_weight {\n assert(Self::max_containers_spec(n as int, w as int, max_weight as int) == area as int);\n area\n } else {\n assert(Self::max_containers_spec(n as int, w as int, max_weight as int) == by_weight as int);\n by_weight\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3492/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3492/tests/mutated_testcases.jsonl"} {"problem": "lc3507", "family": "leetcode", "description": "# Minimum Pair Removal to Sort Array I\n\nGiven an array `nums`, you can perform the following operation any number of times:\n\n- Select the adjacent pair with the minimum sum in `nums`. If multiple such pairs exist, choose the leftmost one.\n- Replace the pair with their sum.\n\nReturn the minimum number of operations needed to make the array non-decreasing.\n\nAn array is non-decreasing if each element is greater than or equal to its previous element.\n\n## Example 1:\n\n> **Input:** nums = [5,2,3,1]\n> **Output:** 2\n>\n> **Explanation:**\n> - The pair (3,1) has the minimum sum of 4. After replacement, nums = [5,2,4].\n> - The pair (2,4) has the minimum sum of 6. After replacement, nums = [5,6].\n> The array becomes non-decreasing in two operations.\n\n## Example 2:\n\n> **Input:** nums = [1,2,2]\n> **Output:** 0\n>\n> **Explanation:**\n> The array is already non-decreasing.\n\n## Constraints:\n\n- `1 <= nums.length <= 50`\n- `-1000 <= nums[i] <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_pair_removal(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Linked List, Heap (Priority Queue), Simulation, Doubly-Linked List, Ordered Set\nAcceptance rate: 65.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_non_decreasing(s: Seq) -> bool {\n forall |i: int| 0 < i && i < s.len() ==> s[i - 1] <= #[trigger] s[i]\n }\n\n pub open spec fn pair_sum(s: Seq, i: int) -> int {\n if 0 <= i && i + 1 < s.len() {\n s[i] as int + s[i + 1] as int\n } else {\n 0\n }\n }\n\n pub open spec fn pair_sum_i32(s: Seq, i: int) -> i32 {\n if 0 <= i && i + 1 < s.len() {\n ((s[i] as i64 + s[i + 1] as i64) as i32)\n } else {\n 0\n }\n }\n\n pub open spec fn min_index_prefix(s: Seq, upto: int) -> int\n decreases upto,\n {\n if s.len() < 2 || upto <= 1 {\n 0\n } else {\n let prev = Self::min_index_prefix(s, upto - 1);\n let cur = upto - 1;\n if Self::pair_sum(s, cur) < Self::pair_sum(s, prev) {\n cur\n } else {\n prev\n }\n }\n }\n\n pub open spec fn min_pair_index(s: Seq) -> int {\n if s.len() < 2 {\n 0\n } else {\n Self::min_index_prefix(s, s.len() - 1)\n }\n }\n\n pub open spec fn merge_at(s: Seq, idx: int) -> Seq {\n if s.len() < 2 {\n s\n } else if 0 <= idx && idx + 1 < s.len() {\n s.subrange(0, idx) + seq![Self::pair_sum_i32(s, idx)] + s.subrange(idx + 2, s.len() as int)\n } else {\n s.subrange(0, 0) + seq![Self::pair_sum_i32(s, 0)] + s.subrange(2, s.len() as int)\n }\n }\n\n pub open spec fn next_seq(s: Seq) -> Seq {\n if s.len() < 2 {\n s\n } else {\n Self::merge_at(s, Self::min_pair_index(s))\n }\n }\n\n pub open spec fn steps_to_sort_fuel(s: Seq, fuel: nat) -> nat\n decreases fuel,\n {\n if fuel == 0 || s.len() <= 1 || Self::is_non_decreasing(s) {\n 0\n } else {\n 1nat + Self::steps_to_sort_fuel(Self::next_seq(s), (fuel - 1) as nat)\n }\n }\n\n pub open spec fn steps_to_sort(s: Seq) -> nat {\n Self::steps_to_sort_fuel(s, s.len() as nat)\n }\n\n pub fn minimum_pair_removal(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == Self::steps_to_sort(nums@) as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn is_non_decreasing_vec(arr: &Vec) -> bool {\n if arr.len() <= 1 {\n return true;\n }\n let mut i: usize = 1;\n while i < arr.len() {\n if arr[i - 1] > arr[i] {\n return false;\n }\n i += 1;\n }\n true\n }\n\n pub fn minimum_pair_removal(nums: Vec) -> i32 {\n let mut arr = nums;\n let mut ops: usize = 0;\n let mut sorted = Self::is_non_decreasing_vec(&arr);\n\n while !sorted {\n let n = arr.len();\n let mut best_idx: usize = 0;\n let mut best_sum: i64 = arr[0] as i64 + arr[1] as i64;\n let mut i: usize = 1;\n while i < n - 1 {\n let cur_sum: i64 = arr[i] as i64 + arr[i + 1] as i64;\n if cur_sum < best_sum {\n best_idx = i;\n best_sum = cur_sum;\n }\n i += 1;\n }\n\n let mut next_arr: Vec = Vec::new();\n let mut j: usize = 0;\n while j < best_idx {\n next_arr.push(arr[j]);\n j += 1;\n }\n let merged = (arr[best_idx] as i64 + arr[best_idx + 1] as i64) as i32;\n next_arr.push(merged);\n j = best_idx + 2;\n while j < n {\n next_arr.push(arr[j]);\n j += 1;\n }\n\n arr = next_arr;\n ops += 1;\n sorted = Self::is_non_decreasing_vec(&arr);\n }\n\n ops as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_non_decreasing(s: Seq) -> bool {\n forall |i: int| 0 < i && i < s.len() ==> s[i - 1] <= #[trigger] s[i]\n }\n\n pub open spec fn pair_sum(s: Seq, i: int) -> int {\n if 0 <= i && i + 1 < s.len() {\n s[i] as int + s[i + 1] as int\n } else {\n 0\n }\n }\n\n pub open spec fn pair_sum_i32(s: Seq, i: int) -> i32 {\n if 0 <= i && i + 1 < s.len() {\n ((s[i] as i64 + s[i + 1] as i64) as i32)\n } else {\n 0\n }\n }\n\n pub open spec fn min_index_prefix(s: Seq, upto: int) -> int\n decreases upto,\n {\n if s.len() < 2 || upto <= 1 {\n 0\n } else {\n let prev = Self::min_index_prefix(s, upto - 1);\n let cur = upto - 1;\n if Self::pair_sum(s, cur) < Self::pair_sum(s, prev) {\n cur\n } else {\n prev\n }\n }\n }\n\n pub open spec fn min_pair_index(s: Seq) -> int {\n if s.len() < 2 {\n 0\n } else {\n Self::min_index_prefix(s, s.len() - 1)\n }\n }\n\n pub open spec fn merge_at(s: Seq, idx: int) -> Seq {\n if s.len() < 2 {\n s\n } else if 0 <= idx && idx + 1 < s.len() {\n s.subrange(0, idx) + seq![Self::pair_sum_i32(s, idx)] + s.subrange(idx + 2, s.len() as int)\n } else {\n s.subrange(0, 0) + seq![Self::pair_sum_i32(s, 0)] + s.subrange(2, s.len() as int)\n }\n }\n\n pub open spec fn next_seq(s: Seq) -> Seq {\n if s.len() < 2 {\n s\n } else {\n Self::merge_at(s, Self::min_pair_index(s))\n }\n }\n\n pub open spec fn steps_to_sort_fuel(s: Seq, fuel: nat) -> nat\n decreases fuel,\n {\n if fuel == 0 || s.len() <= 1 || Self::is_non_decreasing(s) {\n 0\n } else {\n 1nat + Self::steps_to_sort_fuel(Self::next_seq(s), (fuel - 1) as nat)\n }\n }\n\n pub open spec fn steps_to_sort(s: Seq) -> nat {\n Self::steps_to_sort_fuel(s, s.len() as nat)\n }\n\n fn is_non_decreasing_vec(arr: &Vec) -> (res: bool)\n ensures\n res == Self::is_non_decreasing(arr@),\n {\n if arr.len() <= 1 {\n return true;\n }\n let mut i: usize = 1;\n while i < arr.len() {\n if arr[i - 1] > arr[i] {\n return false;\n }\n i += 1;\n }\n true\n }\n\n pub fn minimum_pair_removal(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == Self::steps_to_sort(nums@) as int,\n {\n let mut arr = nums;\n let mut ops: usize = 0;\n let mut sorted = Self::is_non_decreasing_vec(&arr);\n\n while !sorted {\n let n = arr.len();\n let mut best_idx: usize = 0;\n let mut best_sum: i64 = arr[0] as i64 + arr[1] as i64;\n let mut i: usize = 1;\n while i < n - 1 {\n let cur_sum: i64 = arr[i] as i64 + arr[i + 1] as i64;\n if cur_sum < best_sum {\n best_idx = i;\n best_sum = cur_sum;\n }\n i += 1;\n }\n\n let mut next_arr: Vec = Vec::new();\n let mut j: usize = 0;\n while j < best_idx {\n next_arr.push(arr[j]);\n j += 1;\n }\n #[verifier::truncate]\n let merged = (arr[best_idx] as i64 + arr[best_idx + 1] as i64) as i32;\n next_arr.push(merged);\n j = best_idx + 2;\n while j < n {\n next_arr.push(arr[j]);\n j += 1;\n }\n\n arr = next_arr;\n ops += 1;\n sorted = Self::is_non_decreasing_vec(&arr);\n }\n\n ops as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_non_decreasing(s: Seq) -> bool {\n forall |i: int| 0 < i && i < s.len() ==> s[i - 1] <= #[trigger] s[i]\n }\n\n pub open spec fn pair_sum(s: Seq, i: int) -> int {\n if 0 <= i && i + 1 < s.len() {\n s[i] as int + s[i + 1] as int\n } else {\n 0\n }\n }\n\n pub open spec fn pair_sum_i32(s: Seq, i: int) -> i32 {\n if 0 <= i && i + 1 < s.len() {\n ((s[i] as i64 + s[i + 1] as i64) as i32)\n } else {\n 0\n }\n }\n\n pub open spec fn min_index_prefix(s: Seq, upto: int) -> int\n decreases upto,\n {\n if s.len() < 2 || upto <= 1 {\n 0\n } else {\n let prev = Self::min_index_prefix(s, upto - 1);\n let cur = upto - 1;\n if Self::pair_sum(s, cur) < Self::pair_sum(s, prev) {\n cur\n } else {\n prev\n }\n }\n }\n\n pub open spec fn min_pair_index(s: Seq) -> int {\n if s.len() < 2 {\n 0\n } else {\n Self::min_index_prefix(s, s.len() - 1)\n }\n }\n\n pub open spec fn merge_at(s: Seq, idx: int) -> Seq {\n if s.len() < 2 {\n s\n } else if 0 <= idx && idx + 1 < s.len() {\n s.subrange(0, idx) + seq![Self::pair_sum_i32(s, idx)] + s.subrange(idx + 2, s.len() as int)\n } else {\n s.subrange(0, 0) + seq![Self::pair_sum_i32(s, 0)] + s.subrange(2, s.len() as int)\n }\n }\n\n pub open spec fn next_seq(s: Seq) -> Seq {\n if s.len() < 2 {\n s\n } else {\n Self::merge_at(s, Self::min_pair_index(s))\n }\n }\n\n pub open spec fn steps_to_sort_fuel(s: Seq, fuel: nat) -> nat\n decreases fuel,\n {\n if fuel == 0 || s.len() <= 1 || Self::is_non_decreasing(s) {\n 0\n } else {\n 1nat + Self::steps_to_sort_fuel(Self::next_seq(s), (fuel - 1) as nat)\n }\n }\n\n pub open spec fn steps_to_sort(s: Seq) -> nat {\n Self::steps_to_sort_fuel(s, s.len() as nat)\n }\n\n proof fn lemma_not_non_decreasing_len_ge_2(s: Seq)\n requires\n !Self::is_non_decreasing(s),\n ensures\n s.len() >= 2,\n {\n if s.len() <= 1 {\n assert(Self::is_non_decreasing(s));\n assert(false);\n }\n }\n\n proof fn lemma_subrange_extend(s: Seq, start: int, end: int)\n requires\n 0 <= start <= end < s.len(),\n ensures\n s.subrange(start, end + 1) =~= s.subrange(start, end).push(s[end]),\n {\n assert(s.subrange(start, end + 1).len() == s.subrange(start, end).push(s[end]).len());\n assert forall |k: int| 0 <= k && k < s.subrange(start, end + 1).len() implies\n s.subrange(start, end + 1)[k] == s.subrange(start, end).push(s[end])[k] by {\n if k < s.subrange(start, end).len() {\n assert(s.subrange(start, end)[k] == s[start + k]);\n assert(s.subrange(start, end + 1)[k] == s[start + k]);\n assert(s.subrange(start, end).push(s[end])[k] == s.subrange(start, end)[k]);\n } else {\n assert(k == s.subrange(start, end).len());\n assert(s.subrange(start, end + 1)[k] == s[end]);\n assert(s.subrange(start, end).push(s[end])[k] == s[end]);\n }\n };\n }\n\n proof fn lemma_merge_len_valid(s: Seq, idx: int)\n requires\n s.len() >= 2,\n 0 <= idx,\n idx + 1 < s.len(),\n ensures\n Self::merge_at(s, idx).len() == s.len() - 1,\n {\n }\n\n fn is_non_decreasing_vec(arr: &Vec) -> (res: bool)\n ensures\n res == Self::is_non_decreasing(arr@),\n {\n if arr.len() <= 1 {\n proof {\n assert(Self::is_non_decreasing(arr@));\n }\n return true;\n }\n let mut i: usize = 1;\n while i < arr.len()\n invariant\n 1 <= i <= arr.len(),\n forall |k: int| 1 <= k && k < i as int ==> arr@[k - 1] <= #[trigger] arr@[k],\n decreases arr.len() - i,\n {\n if arr[i - 1] > arr[i] {\n proof {\n assert(arr@[i as int - 1] > arr@[i as int]);\n assert(!Self::is_non_decreasing(arr@));\n }\n return false;\n }\n i += 1;\n }\n proof {\n assert forall |k: int| 0 < k && k < arr.len() implies arr@[k - 1] <= #[trigger] arr@[k] by {\n assert(1 <= k && k < i as int);\n };\n assert(Self::is_non_decreasing(arr@));\n }\n true\n }\n\n pub fn minimum_pair_removal(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 50,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result as int == Self::steps_to_sort(nums@) as int,\n {\n let mut arr = nums;\n let mut ops: usize = 0;\n let mut sorted = Self::is_non_decreasing_vec(&arr);\n\n while !sorted\n invariant\n 1 <= arr.len() <= nums.len(),\n ops <= nums.len(),\n sorted == Self::is_non_decreasing(arr@),\n ops as int + arr.len() as int == nums.len() as int,\n ops as int + Self::steps_to_sort(arr@) as int == Self::steps_to_sort(nums@) as int,\n decreases arr.len(),\n {\n proof {\n assert(!Self::is_non_decreasing(arr@));\n Self::lemma_not_non_decreasing_len_ge_2(arr@);\n }\n\n let n = arr.len();\n let mut best_idx: usize = 0;\n let mut best_sum: i64 = arr[0] as i64 + arr[1] as i64;\n let mut i: usize = 1;\n while i < n - 1\n invariant\n n == arr.len(),\n 2 <= n,\n 1 <= i <= n - 1,\n 0 <= best_idx < i,\n best_idx as int == Self::min_index_prefix(arr@, i as int),\n best_sum as int == Self::pair_sum(arr@, best_idx as int),\n decreases n - 1 - i,\n {\n let cur_sum: i64 = arr[i] as i64 + arr[i + 1] as i64;\n proof {\n assert(cur_sum as int == Self::pair_sum(arr@, i as int));\n assert(Self::min_index_prefix(arr@, i as int + 1)\n == if Self::pair_sum(arr@, i as int) < Self::pair_sum(arr@, Self::min_index_prefix(arr@, i as int)) {\n i as int\n } else {\n Self::min_index_prefix(arr@, i as int)\n });\n }\n if cur_sum < best_sum {\n proof {\n assert(Self::pair_sum(arr@, i as int) < Self::pair_sum(arr@, Self::min_index_prefix(arr@, i as int)));\n assert(Self::min_index_prefix(arr@, i as int + 1) == i as int);\n }\n best_idx = i;\n best_sum = cur_sum;\n } else {\n proof {\n assert(Self::pair_sum(arr@, i as int) >= Self::pair_sum(arr@, Self::min_index_prefix(arr@, i as int)));\n assert(Self::min_index_prefix(arr@, i as int + 1) == Self::min_index_prefix(arr@, i as int));\n }\n }\n i += 1;\n }\n\n proof {\n assert(i == n - 1);\n assert(best_idx as int == Self::min_pair_index(arr@));\n }\n\n let ghost old_arr = arr@;\n let ghost old_ops = ops as int;\n let mut next_arr: Vec = Vec::new();\n let mut j: usize = 0;\n while j < best_idx\n invariant\n n == arr.len(),\n old_arr == arr@,\n 0 <= j <= best_idx,\n best_idx <= n - 2,\n next_arr@ =~= old_arr.subrange(0, j as int),\n decreases best_idx - j,\n {\n let ghost prev = next_arr@;\n next_arr.push(arr[j]);\n proof {\n Self::lemma_subrange_extend(old_arr, 0, j as int);\n assert(old_arr.subrange(0, j as int + 1) =~= old_arr.subrange(0, j as int).push(old_arr[j as int]));\n assert(next_arr@ =~= prev.push(arr[j as int]));\n assert(arr[j as int] == old_arr[j as int]);\n assert(next_arr@ =~= old_arr.subrange(0, j as int + 1));\n }\n j += 1;\n }\n\n #[verifier::truncate]\n let merged = (arr[best_idx] as i64 + arr[best_idx + 1] as i64) as i32;\n let ghost prev_prefix = next_arr@;\n next_arr.push(merged);\n proof {\n assert(best_idx + 1 < n);\n assert(prev_prefix =~= old_arr.subrange(0, best_idx as int));\n assert(arr[best_idx as int] == old_arr[best_idx as int]);\n assert(arr[best_idx as int + 1] == old_arr[best_idx as int + 1]);\n assert(merged == Self::pair_sum_i32(old_arr, best_idx as int));\n assert(next_arr@ =~= prev_prefix.push(merged));\n assert(next_arr@ =~= old_arr.subrange(0, best_idx as int) + seq![merged]);\n }\n\n j = best_idx + 2;\n while j < n\n invariant\n n == arr.len(),\n old_arr == arr@,\n best_idx <= n - 2,\n best_idx + 2 <= j <= n,\n merged == Self::pair_sum_i32(old_arr, best_idx as int),\n next_arr@ =~= old_arr.subrange(0, best_idx as int)\n + seq![merged]\n + old_arr.subrange((best_idx + 2) as int, j as int),\n decreases n - j,\n {\n let ghost prev = next_arr@;\n next_arr.push(arr[j]);\n proof {\n Self::lemma_subrange_extend(old_arr, (best_idx + 2) as int, j as int);\n assert(old_arr.subrange((best_idx + 2) as int, j as int + 1)\n =~= old_arr.subrange((best_idx + 2) as int, j as int).push(old_arr[j as int]));\n assert(next_arr@ =~= prev.push(arr[j as int]));\n assert(arr[j as int] == old_arr[j as int]);\n assert(next_arr@ =~= old_arr.subrange(0, best_idx as int)\n + seq![merged]\n + old_arr.subrange((best_idx + 2) as int, j as int + 1));\n }\n j += 1;\n }\n\n proof {\n assert(next_arr@ =~= old_arr.subrange(0, best_idx as int)\n + seq![merged]\n + old_arr.subrange((best_idx + 2) as int, n as int));\n assert(next_arr@ =~= old_arr.subrange(0, best_idx as int)\n + seq![Self::pair_sum_i32(old_arr, best_idx as int)]\n + old_arr.subrange((best_idx + 2) as int, n as int));\n assert(next_arr@ =~= Self::merge_at(old_arr, best_idx as int));\n assert(next_arr@ =~= Self::next_seq(old_arr));\n Self::lemma_merge_len_valid(old_arr, best_idx as int);\n assert(next_arr.len() as int == old_arr.len() - 1);\n assert(Self::steps_to_sort(old_arr) == Self::steps_to_sort_fuel(old_arr, old_arr.len() as nat));\n assert(Self::steps_to_sort(old_arr)\n == 1nat + Self::steps_to_sort_fuel(Self::next_seq(old_arr), (old_arr.len() as nat - 1nat) as nat));\n assert(Self::steps_to_sort_fuel(Self::next_seq(old_arr), (old_arr.len() as nat - 1nat) as nat)\n == Self::steps_to_sort_fuel(next_arr@, next_arr.len() as nat));\n assert(Self::steps_to_sort_fuel(next_arr@, next_arr.len() as nat) == Self::steps_to_sort(next_arr@));\n assert(Self::steps_to_sort(old_arr) as int == 1 + Self::steps_to_sort(next_arr@) as int);\n assert(old_ops + 1 + Self::steps_to_sort(next_arr@) as int == Self::steps_to_sort(nums@) as int);\n assert(old_ops + 1 <= nums.len() as int);\n }\n\n arr = next_arr;\n ops += 1;\n sorted = Self::is_non_decreasing_vec(&arr);\n }\n\n proof {\n assert(sorted == Self::is_non_decreasing(arr@));\n assert(Self::steps_to_sort(arr@) == 0);\n assert(ops as int == Self::steps_to_sort(nums@) as int);\n assert(ops <= nums.len());\n assert(nums.len() <= 50);\n assert(ops as int <= 50);\n }\n\n ops as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3507/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3507/tests/mutated_testcases.jsonl"} {"problem": "lc3536", "family": "leetcode", "description": "# Maximum Product of Two Digits\n\nYou are given a positive integer `n`.\n\nReturn the maximum product of any two digits in `n`.\n\nYou may use the same digit value twice only if that digit appears at least twice in `n`.\n\n## Example 1:\n\n> **Input:** n = 31\n> **Output:** 3\n\n## Example 2:\n\n> **Input:** n = 22\n> **Output:** 4\n\n## Example 3:\n\n> **Input:** n = 124\n> **Output:** 8\n\n## Constraints:\n\n- `10 <= n <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_product(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Sorting\nAcceptance rate: 69.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn scan_max_product(x: nat, max1: nat, max2: nat) -> nat\n recommends\n max2 <= max1 <= 9,\n decreases x,\n {\n if x == 0 {\n max1 * max2\n } else {\n let d = x % 10;\n let rest = x / 10;\n if d > max1 {\n Solution::scan_max_product(rest, d, max1)\n } else if d > max2 {\n Solution::scan_max_product(rest, max1, d)\n } else {\n Solution::scan_max_product(rest, max1, max2)\n }\n }\n }\n\n pub open spec fn max_product_spec(x: nat) -> nat {\n Solution::scan_max_product(x, 0, 0)\n }\n\n pub fn max_product(n: i32) -> (res: i32)\n requires\n 10 <= n <= 1_000_000_000,\n ensures\n res as int == Solution::max_product_spec(n as nat),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_product(n: i32) -> i32 {\n let mut cur: u32 = n as u32;\n let mut max1: u32 = 0;\n let mut max2: u32 = 0;\n\n while cur != 0 {\n let d: u32 = cur % 10;\n if d > max1 {\n max2 = max1;\n max1 = d;\n } else if d > max2 {\n max2 = d;\n }\n cur = cur / 10;\n }\n\n (max1 * max2) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn scan_max_product(x: nat, max1: nat, max2: nat) -> nat\n recommends\n max2 <= max1 <= 9,\n decreases x,\n {\n if x == 0 {\n max1 * max2\n } else {\n let d = x % 10;\n let rest = x / 10;\n if d > max1 {\n Solution::scan_max_product(rest, d, max1)\n } else if d > max2 {\n Solution::scan_max_product(rest, max1, d)\n } else {\n Solution::scan_max_product(rest, max1, max2)\n }\n }\n }\n\n pub open spec fn max_product_spec(x: nat) -> nat {\n Solution::scan_max_product(x, 0, 0)\n }\n\n pub fn max_product(n: i32) -> (res: i32)\n requires\n 10 <= n <= 1_000_000_000,\n ensures\n res as int == Solution::max_product_spec(n as nat),\n {\n let mut cur: u32 = n as u32;\n let mut max1: u32 = 0;\n let mut max2: u32 = 0;\n\n while cur != 0 {\n let d: u32 = cur % 10;\n if d > max1 {\n max2 = max1;\n max1 = d;\n } else if d > max2 {\n max2 = d;\n }\n cur = cur / 10;\n }\n\n (max1 * max2) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn scan_max_product(x: nat, max1: nat, max2: nat) -> nat\n recommends\n max2 <= max1 <= 9,\n decreases x,\n {\n if x == 0 {\n max1 * max2\n } else {\n let d = x % 10;\n let rest = x / 10;\n if d > max1 {\n Solution::scan_max_product(rest, d, max1)\n } else if d > max2 {\n Solution::scan_max_product(rest, max1, d)\n } else {\n Solution::scan_max_product(rest, max1, max2)\n }\n }\n }\n\n pub open spec fn max_product_spec(x: nat) -> nat {\n Solution::scan_max_product(x, 0, 0)\n }\n\n pub fn max_product(n: i32) -> (res: i32)\n requires\n 10 <= n <= 1_000_000_000,\n ensures\n res as int == Solution::max_product_spec(n as nat),\n {\n let mut cur: u32 = n as u32;\n let mut max1: u32 = 0;\n let mut max2: u32 = 0;\n\n while cur != 0\n invariant\n 0 <= cur as int <= n as int,\n 0 <= max2 as int <= max1 as int <= 9,\n Solution::scan_max_product(cur as nat, max1 as nat, max2 as nat)\n == Solution::max_product_spec(n as nat),\n decreases cur,\n {\n let d: u32 = cur % 10;\n if d > max1 {\n max2 = max1;\n max1 = d;\n } else if d > max2 {\n max2 = d;\n }\n cur = cur / 10;\n }\n\n proof {\n assert(max1 <= 9);\n assert(max2 <= 9);\n assert(max1 * max2 <= 81) by (nonlinear_arith)\n requires\n max1 <= 9,\n max2 <= 9,\n {\n }\n }\n (max1 * max2) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3536/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3536/tests/mutated_testcases.jsonl"} {"problem": "lc3622", "family": "leetcode", "description": "# Check Divisibility by Digit Sum and Product\n\nYou are given a positive integer `n`. Determine whether `n` is divisible by the sum of the following two values:\n\n- The **digit sum** of `n` (the sum of its digits).\n- The **digit product** of `n` (the product of its digits).\n\nReturn `true` if `n` is divisible by this sum; otherwise, return `false`.\n\n## Example 1:\n\n> **Input:** n = 99\n> **Output:** true\n> **Explanation:** The digit sum of `99` is `9 + 9 = 18`, and the digit product is `9 * 9 = 81`. Their sum is `99`, and `99` is divisible by `99`.\n\n## Example 2:\n\n> **Input:** n = 23\n> **Output:** false\n> **Explanation:** The digit sum of `23` is `2 + 3 = 5`, and the digit product is `2 * 3 = 6`. Their sum is `11`, and `23` is not divisible by `11`.\n\n## Constraints:\n\n- $1 <= n <= 10^6$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn check_divisibility(n: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 69.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n (n % 10) + Self::digit_sum(n / 10)\n }\n }\n\n pub open spec fn digit_product(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 1\n } else {\n (n % 10) * Self::digit_product(n / 10)\n }\n }\n\n pub open spec fn check_divisibility_spec(n: nat) -> bool {\n let d = Self::digit_sum(n) + Self::digit_product(n);\n d > 0 && n % d == 0\n }\n\n pub fn check_divisibility(n: i32) -> (result: bool)\n requires\n 1 <= n <= 1_000_000,\n ensures\n result == Self::check_divisibility_spec(n as nat),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn digit_sum_exec(x: u32) -> u32 {\n if x == 0 {\n 0\n } else {\n let d = x % 10;\n let q = x / 10;\n let s = Self::digit_sum_exec(q);\n if q == 0 {\n d\n } else {\n d + s\n }\n }\n }\n\n fn digit_product_exec(x: u32) -> u32 {\n if x == 0 {\n 1\n } else {\n let d = x % 10;\n let q = x / 10;\n let p = Self::digit_product_exec(q);\n if q == 0 {\n d\n } else {\n d * p\n }\n }\n }\n\n pub fn check_divisibility(n: i32) -> bool {\n let x = n as u32;\n let s = Self::digit_sum_exec(x);\n let p = Self::digit_product_exec(x);\n let denom = s + p;\n x % denom == 0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n (n % 10) + Self::digit_sum(n / 10)\n }\n }\n\n pub open spec fn digit_product(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 1\n } else {\n (n % 10) * Self::digit_product(n / 10)\n }\n }\n\n pub open spec fn check_divisibility_spec(n: nat) -> bool {\n let d = Self::digit_sum(n) + Self::digit_product(n);\n d > 0 && n % d == 0\n }\n\n fn digit_sum_exec(x: u32) -> (sum: u32)\n requires\n x <= 1_000_000,\n ensures\n sum as nat == Self::digit_sum(x as nat),\n sum <= x,\n x > 0 ==> sum > 0,\n {\n if x == 0 {\n 0\n } else {\n let d = x % 10;\n let q = x / 10;\n let s = Self::digit_sum_exec(q);\n if q == 0 {\n d\n } else {\n d + s\n }\n }\n }\n\n fn digit_product_exec(x: u32) -> (prod: u32)\n requires\n x <= 1_000_000,\n ensures\n prod as nat == Self::digit_product(x as nat),\n x == 0 ==> prod == 1,\n x > 0 ==> prod <= x,\n {\n if x == 0 {\n 1\n } else {\n let d = x % 10;\n let q = x / 10;\n let p = Self::digit_product_exec(q);\n if q == 0 {\n d\n } else {\n d * p\n }\n }\n }\n\n pub fn check_divisibility(n: i32) -> (result: bool)\n requires\n 1 <= n <= 1_000_000,\n ensures\n result == Self::check_divisibility_spec(n as nat),\n {\n let x = n as u32;\n let s = Self::digit_sum_exec(x);\n let p = Self::digit_product_exec(x);\n let denom = s + p;\n x % denom == 0\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn digit_sum(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n (n % 10) + Self::digit_sum(n / 10)\n }\n }\n\n pub open spec fn digit_product(n: nat) -> nat\n decreases n,\n {\n if n == 0 {\n 1\n } else {\n (n % 10) * Self::digit_product(n / 10)\n }\n }\n\n pub open spec fn check_divisibility_spec(n: nat) -> bool {\n let d = Self::digit_sum(n) + Self::digit_product(n);\n d > 0 && n % d == 0\n }\n\n fn digit_sum_exec(x: u32) -> (sum: u32)\n requires\n x <= 1_000_000,\n ensures\n sum as nat == Self::digit_sum(x as nat),\n sum <= x,\n x > 0 ==> sum > 0,\n decreases x,\n {\n if x == 0 {\n 0\n } else {\n let d = x % 10;\n let q = x / 10;\n let s = Self::digit_sum_exec(q);\n if q == 0 {\n assert(x < 10);\n assert(d == x);\n d\n } else {\n proof {\n assert(x as nat == 10 * q as nat + d as nat);\n assert(d <= 9);\n assert(s <= q);\n assert(d as nat + s as nat <= x as nat) by(nonlinear_arith)\n requires\n s as nat <= q as nat,\n x as nat == 10 * q as nat + d as nat,\n d <= 9,\n {}\n }\n d + s\n }\n }\n }\n\n fn digit_product_exec(x: u32) -> (prod: u32)\n requires\n x <= 1_000_000,\n ensures\n prod as nat == Self::digit_product(x as nat),\n x == 0 ==> prod == 1,\n x > 0 ==> prod <= x,\n decreases x,\n {\n if x == 0 {\n assert(Self::digit_product(x as nat) == 1);\n 1\n } else {\n let d = x % 10;\n let q = x / 10;\n let p = Self::digit_product_exec(q);\n if q == 0 {\n assert(x < 10);\n assert(d == x);\n proof {\n assert(Self::digit_product(x as nat) == (x as nat % 10) * Self::digit_product((x as nat) / 10));\n assert((x as nat) / 10 == 0);\n assert(Self::digit_product(0nat) == 1);\n assert(x as nat % 10 == x as nat);\n }\n d\n } else {\n proof {\n assert(q > 0);\n assert(p <= q);\n assert(d <= 9);\n assert(x as nat == 10 * q as nat + d as nat);\n assert(Self::digit_product(x as nat) == d as nat * Self::digit_product(q as nat));\n assert(p as nat == Self::digit_product(q as nat));\n assert(d as nat * p as nat <= x as nat) by(nonlinear_arith)\n requires\n p as nat <= q as nat,\n x as nat == 10 * q as nat + d as nat,\n d <= 9,\n {}\n }\n d * p\n }\n }\n }\n\n pub fn check_divisibility(n: i32) -> (result: bool)\n requires\n 1 <= n <= 1_000_000,\n ensures\n result == Self::check_divisibility_spec(n as nat),\n {\n let x = n as u32;\n let s = Self::digit_sum_exec(x);\n let p = Self::digit_product_exec(x);\n proof {\n assert(x > 0);\n assert(p <= x);\n assert(s <= x);\n assert(s as nat + p as nat <= 2_000_000) by(nonlinear_arith)\n requires\n s as nat <= x as nat,\n p as nat <= x as nat,\n x as nat <= 1_000_000,\n {}\n }\n let denom = s + p;\n assert(denom > 0);\n x % denom == 0\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3622/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3622/tests/mutated_testcases.jsonl"} {"problem": "lc3668", "family": "leetcode", "description": "# Restore Finishing Order\n\nYou are given an integer array `order` of length `n` and an integer array `friends`.\n\n- `order` contains every integer from `1` to `n` exactly once, representing the IDs of the participants of a race in their finishing order.\n- `friends` contains the IDs of your friends in the race sorted in strictly increasing order. Each ID in `friends` is guaranteed to appear in `order`.\n\nReturn an array containing your friends' IDs in their finishing order.\n\n## Example 1:\n\n> **Input:** order = [3,1,2,5,4], friends = [1,3,4]\n> **Output:** [3,1,4]\n> **Explanation:** The finishing order is [**3**, **1**, 2, 5, **4**]. Therefore, the finishing order of your friends is [3, 1, 4].\n\n## Example 2:\n\n> **Input:** order = [1,4,5,3,2], friends = [2,5]\n> **Output:** [5,2]\n> **Explanation:** The finishing order is [1, 4, **5**, 3, **2**]. Therefore, the finishing order of your friends is [5, 2].\n\n## Constraints:\n\n- `1 <= n == order.length <= 100`\n- `order` contains every integer from `1` to `n` exactly once\n- `1 <= friends.length <= min(8, n)`\n- `1 <= friends[i] <= n`\n- `friends` is strictly increasing\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn recover_order(order: Vec, friends: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table\nAcceptance rate: 91.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn filtered_prefix(order: Seq, friends: Seq, k: nat) -> Seq\n decreases k,\n {\n if k == 0 {\n seq![]\n } else {\n let prev = Solution::filtered_prefix(order, friends, (k - 1) as nat);\n if friends.contains(order[(k - 1) as int]) {\n prev.push(order[(k - 1) as int])\n } else {\n prev\n }\n }\n }\n\n pub open spec fn finishing_order(order: Seq, friends: Seq) -> Seq\n {\n Solution::filtered_prefix(order, friends, order.len())\n }\n\n pub fn recover_order(order: Vec, friends: Vec) -> (result: Vec)\n requires\n 1 <= order.len() <= 100,\n forall |i: int| 0 <= i < order.len() ==> 1 <= #[trigger] order[i] <= order.len() as i32,\n forall |i: int, j: int| 0 <= i < j < order.len() ==> order[i] != order[j],\n forall |id: int| 1 <= id <= order.len() ==> #[trigger] order@.contains(id as i32),\n 1 <= friends.len() <= 8,\n friends.len() <= order.len(),\n forall |i: int| 0 <= i < friends.len() ==> 1 <= #[trigger] friends[i] <= order.len() as i32,\n forall |i: int, j: int| 0 <= i < j < friends.len() ==> friends[i] < friends[j],\n forall |i: int| 0 <= i < friends.len() ==> order@.contains(#[trigger] friends[i]),\n ensures\n result@ == Solution::finishing_order(order@, friends@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn recover_order(order: Vec, friends: Vec) -> Vec {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n while i < order.len() {\n let candidate = order[i];\n let mut j: usize = 0;\n let mut found = false;\n while j < friends.len() {\n if friends[j] == candidate {\n found = true;\n }\n j = j + 1;\n }\n if found {\n result.push(candidate);\n }\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn filtered_prefix(order: Seq, friends: Seq, k: nat) -> Seq\n decreases k,\n {\n if k == 0 {\n seq![]\n } else {\n let prev = Solution::filtered_prefix(order, friends, (k - 1) as nat);\n if friends.contains(order[(k - 1) as int]) {\n prev.push(order[(k - 1) as int])\n } else {\n prev\n }\n }\n }\n\n pub open spec fn finishing_order(order: Seq, friends: Seq) -> Seq\n {\n Solution::filtered_prefix(order, friends, order.len())\n }\n\n pub fn recover_order(order: Vec, friends: Vec) -> (result: Vec)\n requires\n 1 <= order.len() <= 100,\n forall |i: int| 0 <= i < order.len() ==> 1 <= #[trigger] order[i] <= order.len() as i32,\n forall |i: int, j: int| 0 <= i < j < order.len() ==> order[i] != order[j],\n forall |id: int| 1 <= id <= order.len() ==> #[trigger] order@.contains(id as i32),\n 1 <= friends.len() <= 8,\n friends.len() <= order.len(),\n forall |i: int| 0 <= i < friends.len() ==> 1 <= #[trigger] friends[i] <= order.len() as i32,\n forall |i: int, j: int| 0 <= i < j < friends.len() ==> friends[i] < friends[j],\n forall |i: int| 0 <= i < friends.len() ==> order@.contains(#[trigger] friends[i]),\n ensures\n result@ == Solution::finishing_order(order@, friends@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n while i < order.len() {\n let candidate = order[i];\n let mut j: usize = 0;\n let mut found = false;\n while j < friends.len() {\n if friends[j] == candidate {\n found = true;\n }\n j = j + 1;\n }\n if found {\n result.push(candidate);\n }\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn filtered_prefix(order: Seq, friends: Seq, k: nat) -> Seq\n decreases k,\n {\n if k == 0 {\n seq![]\n } else {\n let prev = Solution::filtered_prefix(order, friends, (k - 1) as nat);\n if friends.contains(order[(k - 1) as int]) {\n prev.push(order[(k - 1) as int])\n } else {\n prev\n }\n }\n }\n\n pub open spec fn finishing_order(order: Seq, friends: Seq) -> Seq\n {\n Solution::filtered_prefix(order, friends, order.len())\n }\n\n pub fn recover_order(order: Vec, friends: Vec) -> (result: Vec)\n requires\n 1 <= order.len() <= 100,\n forall |i: int| 0 <= i < order.len() ==> 1 <= #[trigger] order[i] <= order.len() as i32,\n forall |i: int, j: int| 0 <= i < j < order.len() ==> order[i] != order[j],\n forall |id: int| 1 <= id <= order.len() ==> #[trigger] order@.contains(id as i32),\n 1 <= friends.len() <= 8,\n friends.len() <= order.len(),\n forall |i: int| 0 <= i < friends.len() ==> 1 <= #[trigger] friends[i] <= order.len() as i32,\n forall |i: int, j: int| 0 <= i < j < friends.len() ==> friends[i] < friends[j],\n forall |i: int| 0 <= i < friends.len() ==> order@.contains(#[trigger] friends[i]),\n ensures\n result@ == Solution::finishing_order(order@, friends@),\n {\n let mut result: Vec = Vec::new();\n let mut i: usize = 0;\n while i < order.len()\n invariant\n i <= order.len(),\n result@ == Solution::filtered_prefix(order@, friends@, i as nat),\n decreases order.len() - i,\n {\n let candidate = order[i];\n let mut j: usize = 0;\n let mut found = false;\n while j < friends.len()\n invariant\n j <= friends.len(),\n candidate == order@[i as int],\n found ==> exists |q: int| 0 <= q < j && friends@[q] == candidate,\n !found ==> forall |q: int| 0 <= q < j ==> friends@[q] != candidate,\n decreases friends.len() - j,\n {\n if friends[j] == candidate {\n found = true;\n }\n j = j + 1;\n }\n\n assert(j == friends.len());\n\n assert(found ==> friends@.contains(candidate)) by {\n if found {\n let q = choose |q: int| 0 <= q < j && friends@[q] == candidate;\n assert(0 <= q < friends.len());\n }\n }\n\n assert(!found ==> !friends@.contains(candidate)) by {\n if !found {\n if friends@.contains(candidate) {\n let q = choose |q: int| 0 <= q < friends.len() && friends@[q] == candidate;\n assert(0 <= q < j);\n assert(forall |t: int| 0 <= t < j ==> friends@[t] != candidate);\n assert(friends@[q] != candidate);\n assert(false);\n }\n }\n }\n\n assert(found <==> friends@.contains(candidate));\n\n let ghost prev = result@;\n\n if found {\n result.push(candidate);\n assert(result@ == prev.push(candidate));\n } else {\n assert(result@ == prev);\n }\n\n assert(prev == Solution::filtered_prefix(order@, friends@, i as nat));\n assert(candidate == order@[i as int]);\n\n assert(Solution::filtered_prefix(order@, friends@, i as nat + 1)\n == if friends@.contains(order@[i as int]) {\n Solution::filtered_prefix(order@, friends@, i as nat).push(order@[i as int])\n } else {\n Solution::filtered_prefix(order@, friends@, i as nat)\n });\n\n if found {\n assert(friends@.contains(order@[i as int]));\n assert(result@ == Solution::filtered_prefix(order@, friends@, i as nat + 1));\n } else {\n assert(!friends@.contains(order@[i as int]));\n assert(result@ == Solution::filtered_prefix(order@, friends@, i as nat + 1));\n }\n\n i = i + 1;\n }\n\n assert(i == order.len());\n assert(result@ == Solution::filtered_prefix(order@, friends@, order.len() as nat));\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3668/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3668/tests/mutated_testcases.jsonl"} {"problem": "lc3688", "family": "leetcode", "description": "# Bitwise OR of Even Numbers in an Array\n\nYou are given an integer array `nums`.\n\nReturn the bitwise **OR** of all **even** numbers in the array.\n\nIf there are no even numbers in `nums`, return 0.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,4,5,6]\n> **Output:** 6\n> **Explanation:** The even numbers are 2, 4, and 6. Their bitwise OR equals 6.\n\n## Example 2:\n\n> **Input:** nums = [7,9,11]\n> **Output:** 0\n> **Explanation:** There are no even numbers, so the result is 0.\n\n## Example 3:\n\n> **Input:** nums = [1,8,16]\n> **Output:** 24\n> **Explanation:** The even numbers are 8 and 16. Their bitwise OR equals 24.\n\n## Constraints:\n\n- `1 <= nums.length <= 100`\n- `1 <= nums[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn even_number_bitwise_o_rs(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Bit Manipulation, Simulation\nAcceptance rate: 84.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn even_or_prefix(nums: Seq, k: int) -> i32\n recommends\n 0 <= k <= nums.len(),\n decreases k,\n {\n if k <= 0 {\n 0\n } else {\n let prev = Self::even_or_prefix(nums, k - 1);\n if nums[k - 1] % 2 == 0 {\n prev | nums[k - 1]\n } else {\n prev\n }\n }\n }\n\n pub fn even_number_bitwise_o_rs(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result == Self::even_or_prefix(nums@, nums.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn even_number_bitwise_o_rs(nums: Vec) -> i32 {\n let n = nums.len();\n let mut result: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n if nums[i] % 2 == 0 {\n result = result | nums[i];\n }\n i = i + 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn even_or_prefix(nums: Seq, k: int) -> i32\n recommends\n 0 <= k <= nums.len(),\n decreases k,\n {\n if k <= 0 {\n 0\n } else {\n let prev = Self::even_or_prefix(nums, k - 1);\n if nums[k - 1] % 2 == 0 {\n prev | nums[k - 1]\n } else {\n prev\n }\n }\n }\n\n pub fn even_number_bitwise_o_rs(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result == Self::even_or_prefix(nums@, nums.len() as int),\n {\n let n = nums.len();\n let mut result: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n if nums[i] % 2 == 0 {\n result = result | nums[i];\n }\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn even_or_prefix(nums: Seq, k: int) -> i32\n recommends\n 0 <= k <= nums.len(),\n decreases k,\n {\n if k <= 0 {\n 0\n } else {\n let prev = Self::even_or_prefix(nums, k - 1);\n if nums[k - 1] % 2 == 0 {\n prev | nums[k - 1]\n } else {\n prev\n }\n }\n }\n\n pub fn even_number_bitwise_o_rs(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n ensures\n result == Self::even_or_prefix(nums@, nums.len() as int),\n {\n let n = nums.len();\n let mut result: i32 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n n == nums.len(),\n 1 <= nums.len() <= 100,\n forall |j: int| 0 <= j < nums.len() ==> 1 <= #[trigger] nums[j] <= 100,\n 0 <= i <= n,\n result == Self::even_or_prefix(nums@, i as int),\n decreases n - i,\n {\n let ghost old_i = i as int;\n let ghost old_result = result;\n\n if nums[i] % 2 == 0 {\n result = result | nums[i];\n }\n\n proof {\n assert(0 <= old_i < nums.len());\n if nums[old_i] % 2 == 0 {\n assert(Self::even_or_prefix(nums@, old_i + 1) == Self::even_or_prefix(nums@, old_i) | nums[old_i]);\n assert(result == old_result | nums[old_i]);\n } else {\n assert(Self::even_or_prefix(nums@, old_i + 1) == Self::even_or_prefix(nums@, old_i));\n assert(result == old_result);\n }\n assert(result == Self::even_or_prefix(nums@, old_i + 1));\n }\n\n i = i + 1;\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3688/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3688/tests/mutated_testcases.jsonl"} {"problem": "lc3731", "family": "leetcode", "description": "# Find Missing Elements\n\nYou are given an integer array `nums` consisting of **unique** integers.\n\nOriginally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array.\n\nThe **smallest** and **largest** integers of the original range are still present in `nums`.\n\nReturn a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list.\n\n## Example 1:\n\n> **Input:** nums = [1,4,2,5]\n> **Output:** [3]\n> **Explanation:**\n> The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. Among these, only 3 is missing.\n\n## Example 2:\n\n> **Input:** nums = [7,8,6,9]\n> **Output:** []\n> **Explanation:**\n> The smallest integer is 6 and the largest is 9, so the full range is `[6,7,8,9]`. All integers are already present, so no integer is missing.\n\n## Example 3:\n\n> **Input:** nums = [5,1]\n> **Output:** [2,3,4]\n> **Explanation:**\n> The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. The missing integers are 2, 3, and 4.\n\n## Constraints:\n\n- `2 <= nums.length <= 100`\n- `1 <= nums[i] <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_missing_elements(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Sorting\nAcceptance rate: 82.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seq_contains(nums: Seq, x: i32) -> bool {\n exists |i: int| 0 <= i < nums.len() && nums[i] == x\n }\n\n pub open spec fn strictly_increasing(nums: Seq) -> bool {\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j]\n }\n\n pub open spec fn is_lower_bound(nums: Seq, lo: i32) -> bool {\n Self::seq_contains(nums, lo)\n && forall |i: int| 0 <= i < nums.len() ==> lo <= nums[i]\n }\n\n pub open spec fn is_upper_bound(nums: Seq, hi: i32) -> bool {\n Self::seq_contains(nums, hi)\n && forall |i: int| 0 <= i < nums.len() ==> nums[i] <= hi\n }\n\n pub fn find_missing_elements(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n exists |lo: i32, hi: i32|\n Self::is_lower_bound(nums@, lo)\n && Self::is_upper_bound(nums@, hi)\n && (forall |i: int|\n 0 <= i < result.len() ==> lo < #[trigger] result@[i] < hi && !Self::seq_contains(nums@, result@[i]))\n && (forall |x: i32|\n lo < x < hi && !Self::seq_contains(nums@, x) ==> #[trigger] Self::seq_contains(result@, x))\n && Self::strictly_increasing(result@),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn find_missing_elements(nums: Vec) -> Vec {\n let n = nums.len();\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n\n while i < n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i = i + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut k = min_v + 1;\n\n while k < max_v {\n let mut found = false;\n let mut j: usize = 0;\n\n while j < n {\n if nums[j] == k {\n found = true;\n }\n j = j + 1;\n }\n\n if !found {\n result.push(k);\n }\n\n k = k + 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seq_contains(nums: Seq, x: i32) -> bool {\n exists |i: int| 0 <= i < nums.len() && nums[i] == x\n }\n\n pub open spec fn strictly_increasing(nums: Seq) -> bool {\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j]\n }\n\n pub open spec fn is_lower_bound(nums: Seq, lo: i32) -> bool {\n Self::seq_contains(nums, lo)\n && forall |i: int| 0 <= i < nums.len() ==> lo <= nums[i]\n }\n\n pub open spec fn is_upper_bound(nums: Seq, hi: i32) -> bool {\n Self::seq_contains(nums, hi)\n && forall |i: int| 0 <= i < nums.len() ==> nums[i] <= hi\n }\n\n pub fn find_missing_elements(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n exists |lo: i32, hi: i32|\n Self::is_lower_bound(nums@, lo)\n && Self::is_upper_bound(nums@, hi)\n && (forall |i: int|\n 0 <= i < result.len() ==> lo < #[trigger] result@[i] < hi && !Self::seq_contains(nums@, result@[i]))\n && (forall |x: i32|\n lo < x < hi && !Self::seq_contains(nums@, x) ==> #[trigger] Self::seq_contains(result@, x))\n && Self::strictly_increasing(result@),\n {\n let n = nums.len();\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n\n while i < n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i = i + 1;\n }\n\n let mut result: Vec = Vec::new();\n let mut k = min_v + 1;\n\n while k < max_v {\n let mut found = false;\n let mut j: usize = 0;\n\n while j < n {\n if nums[j] == k {\n found = true;\n }\n j = j + 1;\n }\n\n if !found {\n result.push(k);\n }\n\n k = k + 1;\n }\n\n result\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seq_contains(nums: Seq, x: i32) -> bool {\n exists |i: int| 0 <= i < nums.len() && nums[i] == x\n }\n\n pub open spec fn strictly_increasing(nums: Seq) -> bool {\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j]\n }\n\n pub open spec fn is_lower_bound(nums: Seq, lo: i32) -> bool {\n Self::seq_contains(nums, lo)\n && forall |i: int| 0 <= i < nums.len() ==> lo <= nums[i]\n }\n\n pub open spec fn is_upper_bound(nums: Seq, hi: i32) -> bool {\n Self::seq_contains(nums, hi)\n && forall |i: int| 0 <= i < nums.len() ==> nums[i] <= hi\n }\n\n pub fn find_missing_elements(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums.len() <= 100,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= 100,\n forall |i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n exists |lo: i32, hi: i32|\n Self::is_lower_bound(nums@, lo)\n && Self::is_upper_bound(nums@, hi)\n && (forall |i: int|\n 0 <= i < result.len() ==> lo < #[trigger] result@[i] < hi && !Self::seq_contains(nums@, result@[i]))\n && (forall |x: i32|\n lo < x < hi && !Self::seq_contains(nums@, x) ==> #[trigger] Self::seq_contains(result@, x))\n && Self::strictly_increasing(result@),\n {\n let n = nums.len();\n let mut min_v = nums[0];\n let mut max_v = nums[0];\n let mut i: usize = 1;\n\n while i < n\n invariant\n n == nums.len(),\n 2 <= n <= 100,\n 1 <= i <= n,\n forall |idx: int| 0 <= idx < n ==> 1 <= #[trigger] nums@[idx] <= 100,\n forall |a: int, b: int| 0 <= a < b < n ==> nums@[a] != nums@[b],\n forall |j: int| 0 <= j < i ==> min_v <= #[trigger] nums@[j],\n forall |j: int| 0 <= j < i ==> #[trigger] nums@[j] <= max_v,\n exists |j: int| 0 <= j < i && nums@[j] == min_v,\n exists |j: int| 0 <= j < i && nums@[j] == max_v,\n decreases n - i,\n {\n if nums[i] < min_v {\n min_v = nums[i];\n }\n if nums[i] > max_v {\n max_v = nums[i];\n }\n i = i + 1;\n }\n\n proof {\n assert(Self::is_lower_bound(nums@, min_v));\n assert(Self::is_upper_bound(nums@, max_v));\n assert(min_v < max_v) by {\n if min_v >= max_v {\n assert(min_v == max_v);\n assert(nums@[0] == min_v);\n assert(nums@[1] == max_v);\n assert(nums@[0] == nums@[1]);\n assert(nums@[0] != nums@[1]);\n }\n }\n }\n\n let mut result: Vec = Vec::new();\n let mut k = min_v + 1;\n\n while k < max_v\n invariant\n n == nums.len(),\n 2 <= n <= 100,\n min_v < max_v,\n min_v + 1 <= k <= max_v,\n Self::is_lower_bound(nums@, min_v),\n Self::is_upper_bound(nums@, max_v),\n forall |ri: int|\n 0 <= ri < result.len() ==> min_v < #[trigger] result@[ri] < k && !Self::seq_contains(nums@, result@[ri]),\n forall |x: i32|\n min_v < x < k && !Self::seq_contains(nums@, x) ==> #[trigger] Self::seq_contains(result@, x),\n Self::strictly_increasing(result@),\n decreases (max_v - k) as int,\n {\n let mut found = false;\n let mut j: usize = 0;\n\n while j < n\n invariant\n n == nums.len(),\n 0 <= j <= n,\n found ==> (exists |t: int| 0 <= t < j && nums@[t] == k),\n !found ==> (forall |t: int| 0 <= t < j ==> nums@[t] != k),\n decreases n - j,\n {\n if nums[j] == k {\n found = true;\n }\n j = j + 1;\n }\n\n proof {\n assert(j == n);\n assert(found <==> Self::seq_contains(nums@, k)) by {\n if found {\n let t = choose |t: int| 0 <= t < j && nums@[t] == k;\n assert(0 <= t < n);\n } else {\n if Self::seq_contains(nums@, k) {\n let t = choose |t: int| 0 <= t < n && nums@[t] == k;\n assert(nums@[t] != k);\n }\n }\n }\n }\n\n if !found {\n let ghost old_result = result@;\n let ghost old_len = result.len();\n result.push(k);\n\n proof {\n assert(result@[old_len as int] == k);\n\n assert forall |ri: int| 0 <= ri < result.len()\n implies min_v < #[trigger] result@[ri] < k + 1 && !Self::seq_contains(nums@, result@[ri]) by {\n if ri < old_len as int {\n assert(result@[ri] == old_result[ri]);\n }\n }\n\n assert forall |x: i32|\n min_v < x < k + 1 && !Self::seq_contains(nums@, x)\n implies #[trigger] Self::seq_contains(result@, x) by {\n if x < k {\n assert(Self::seq_contains(old_result, x));\n let w = choose |p: int| 0 <= p < old_result.len() && old_result[p] == x;\n assert(result@[w] == x);\n } else {\n assert(x == k);\n assert(result@[old_len as int] == x);\n }\n }\n\n assert forall |a: int, b: int| 0 <= a < b < result.len()\n implies #[trigger] result@[a] < #[trigger] result@[b] by {\n if b < old_len as int {\n } else {\n assert(b == old_len as int);\n assert(result@[b] == k);\n assert(result@[a] < k);\n }\n }\n }\n } else {\n proof {\n assert forall |ri: int| 0 <= ri < result.len()\n implies min_v < #[trigger] result@[ri] < k + 1 && !Self::seq_contains(nums@, result@[ri]) by {\n }\n\n assert forall |x: i32|\n min_v < x < k + 1 && !Self::seq_contains(nums@, x)\n implies #[trigger] Self::seq_contains(result@, x) by {\n if x < k {\n } else {\n assert(x == k);\n assert(Self::seq_contains(nums@, x));\n }\n }\n }\n }\n\n k = k + 1;\n }\n\n proof {\n assert(k == max_v);\n assert forall |ri: int| 0 <= ri < result.len()\n implies min_v < #[trigger] result@[ri] < max_v && !Self::seq_contains(nums@, result@[ri]) by {\n assert(result@[ri] < k);\n }\n assert forall |x: i32|\n min_v < x < max_v && !Self::seq_contains(nums@, x)\n implies #[trigger] Self::seq_contains(result@, x) by {\n }\n }\n\n result\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc3731/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3731/tests/mutated_testcases.jsonl"} {"problem": "lc3754", "family": "leetcode", "description": "# Concatenate Non-Zero Digits and Multiply by Sum I\n\nYou are given an integer `n`.\n\nForm a new integer `x` by concatenating all the **non-zero digits** of `n` in their original order. If there are no non-zero digits, set `x = 0`.\n\nLet `sum` be the sum of digits in `x`.\n\nReturn `x * sum`.\n\n## Example 1:\n\n> **Input:** n = 10203004\n> **Output:** 12340\n> **Explanation:**\n> The non-zero digits are 1, 2, 3, and 4, so `x = 1234`.\n> The sum of digits is `1 + 2 + 3 + 4 = 10`.\n> Therefore, the answer is `1234 * 10 = 12340`.\n\n## Example 2:\n\n> **Input:** n = 1000\n> **Output:** 1\n> **Explanation:**\n> The non-zero digit is 1, so `x = 1` and `sum = 1`.\n> Therefore, the answer is `1 * 1 = 1`.\n\n## Constraints:\n\n- `0 <= n <= 10^9`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn sum_and_multiply(n: i32) -> i64 {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 55.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn concat_non_zero_acc(m: int, place: int, acc: int) -> int\n decreases m,\n {\n if m <= 0 {\n acc\n } else {\n let d = m % 10;\n if d == 0 {\n Self::concat_non_zero_acc(m / 10, place, acc)\n } else {\n Self::concat_non_zero_acc(m / 10, place * 10, acc + d * place)\n }\n }\n }\n\n pub open spec fn sum_non_zero_acc(m: int, acc: int) -> int\n decreases m,\n {\n if m <= 0 {\n acc\n } else {\n let d = m % 10;\n if d == 0 {\n Self::sum_non_zero_acc(m / 10, acc)\n } else {\n Self::sum_non_zero_acc(m / 10, acc + d)\n }\n }\n }\n\n pub open spec fn concat_non_zero(n: int) -> int {\n Self::concat_non_zero_acc(n, 1, 0)\n }\n\n pub open spec fn sum_non_zero(n: int) -> int {\n Self::sum_non_zero_acc(n, 0)\n }\n\n pub fn sum_and_multiply(n: i32) -> (res: i64)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n res as int == Self::concat_non_zero(n as int) * Self::sum_non_zero(n as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn sum_and_multiply(n: i32) -> i64 {\n let mut m: i64 = n as i64;\n let mut place: i64 = 1;\n let mut x: i64 = 0;\n let mut sum: i64 = 0;\n\n while m > 0 {\n let digit: i64 = m % 10;\n if digit != 0 {\n x = x + digit * place;\n place = place * 10;\n sum = sum + digit;\n }\n m = m / 10;\n }\n\n x * sum\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn concat_non_zero_acc(m: int, place: int, acc: int) -> int\n decreases m,\n {\n if m <= 0 {\n acc\n } else {\n let d = m % 10;\n if d == 0 {\n Self::concat_non_zero_acc(m / 10, place, acc)\n } else {\n Self::concat_non_zero_acc(m / 10, place * 10, acc + d * place)\n }\n }\n }\n\n pub open spec fn sum_non_zero_acc(m: int, acc: int) -> int\n decreases m,\n {\n if m <= 0 {\n acc\n } else {\n let d = m % 10;\n if d == 0 {\n Self::sum_non_zero_acc(m / 10, acc)\n } else {\n Self::sum_non_zero_acc(m / 10, acc + d)\n }\n }\n }\n\n pub open spec fn concat_non_zero(n: int) -> int {\n Self::concat_non_zero_acc(n, 1, 0)\n }\n\n pub open spec fn sum_non_zero(n: int) -> int {\n Self::sum_non_zero_acc(n, 0)\n }\n\n pub fn sum_and_multiply(n: i32) -> (res: i64)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n res as int == Self::concat_non_zero(n as int) * Self::sum_non_zero(n as int),\n {\n let mut m: i64 = n as i64;\n let mut place: i64 = 1;\n let mut x: i64 = 0;\n let mut sum: i64 = 0;\n\n while m > 0 {\n let digit: i64 = m % 10;\n if digit != 0 {\n x = x + digit * place;\n place = place * 10;\n sum = sum + digit;\n }\n m = m / 10;\n }\n\n x * sum\n }\n}\n\n} \n", "verified": "use vstd::arithmetic::div_mod::lemma_fundamental_div_mod;\nuse vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn concat_non_zero_acc(m: int, place: int, acc: int) -> int\n decreases m,\n {\n if m <= 0 {\n acc\n } else {\n let d = m % 10;\n if d == 0 {\n Self::concat_non_zero_acc(m / 10, place, acc)\n } else {\n Self::concat_non_zero_acc(m / 10, place * 10, acc + d * place)\n }\n }\n }\n\n pub open spec fn sum_non_zero_acc(m: int, acc: int) -> int\n decreases m,\n {\n if m <= 0 {\n acc\n } else {\n let d = m % 10;\n if d == 0 {\n Self::sum_non_zero_acc(m / 10, acc)\n } else {\n Self::sum_non_zero_acc(m / 10, acc + d)\n }\n }\n }\n\n pub open spec fn concat_non_zero(n: int) -> int {\n Self::concat_non_zero_acc(n, 1, 0)\n }\n\n pub open spec fn sum_non_zero(n: int) -> int {\n Self::sum_non_zero_acc(n, 0)\n }\n\n pub fn sum_and_multiply(n: i32) -> (res: i64)\n requires\n 0 <= n <= 1_000_000_000,\n ensures\n res as int == Self::concat_non_zero(n as int) * Self::sum_non_zero(n as int),\n {\n let mut m: i64 = n as i64;\n let mut place: i64 = 1;\n let mut x: i64 = 0;\n let mut sum: i64 = 0;\n\n while m > 0\n invariant\n 0 <= n <= 1_000_000_000,\n 0 <= m,\n 1 <= place,\n 0 <= x,\n 0 <= sum,\n sum as int <= x as int,\n x as int + m as int * place as int <= n as int,\n Self::concat_non_zero_acc(m as int, place as int, x as int) == Self::concat_non_zero(\n n as int,\n ),\n Self::sum_non_zero_acc(m as int, sum as int) == Self::sum_non_zero(n as int),\n decreases m,\n {\n let ghost old_m: int = m as int;\n let ghost old_place: int = place as int;\n let ghost old_x: int = x as int;\n let ghost old_sum: int = sum as int;\n\n let digit: i64 = m % 10;\n\n proof {\n lemma_fundamental_div_mod(old_m, 10);\n assert(old_m == 10 * (old_m / 10) + old_m % 10);\n assert(0 <= old_m % 10 < 10);\n assert(digit as int == old_m % 10);\n assert(0 <= digit <= 9);\n assert(old_m >= 1);\n }\n\n if digit != 0 {\n proof {\n assert(old_place <= old_m * old_place) by (nonlinear_arith)\n requires\n old_m >= 1,\n 1 <= old_place,\n {\n }\n assert(old_place <= n as int) by (nonlinear_arith)\n requires\n old_place <= old_m * old_place,\n old_x + old_m * old_place <= n as int,\n 0 <= old_x,\n {\n }\n assert(0 <= old_x <= n as int) by (nonlinear_arith)\n requires\n 0 <= old_x,\n old_x + old_m * old_place <= n as int,\n 0 <= old_m * old_place,\n {\n }\n assert(0 <= old_sum <= old_x);\n assert(0 <= old_sum + digit as int <= n as int + 9) by (nonlinear_arith)\n requires\n 0 <= old_sum <= old_x,\n 0 <= old_x <= n as int,\n 0 <= digit as int <= 9,\n {\n }\n assert(0 <= old_x + (digit as int) * old_place <= 10_000_000_000) by (nonlinear_arith)\n requires\n 0 <= old_x <= n as int,\n 0 <= digit as int <= 9,\n 0 <= old_place <= n as int,\n n as int <= 1_000_000_000,\n {\n }\n assert(0 <= old_place * 10 <= 10_000_000_000) by (nonlinear_arith)\n requires\n 0 <= old_place <= n as int,\n n as int <= 1_000_000_000,\n {\n }\n assert(0 <= old_sum + digit as int <= i64::MAX as int) by (nonlinear_arith)\n requires\n 0 <= old_sum + digit as int <= n as int + 9,\n n as int <= 1_000_000_000,\n {\n }\n assert(0 <= old_x + (digit as int) * old_place <= i64::MAX as int) by (nonlinear_arith)\n requires\n 0 <= old_x + (digit as int) * old_place <= 10_000_000_000,\n {\n }\n assert(0 <= old_place * 10 <= i64::MAX as int) by (nonlinear_arith)\n requires\n 0 <= old_place * 10 <= 10_000_000_000,\n {\n }\n }\n\n x = x + digit * place;\n place = place * 10;\n sum = sum + digit;\n }\n\n m = m / 10;\n\n proof {\n assert(m as int == old_m / 10);\n assert(digit as int == old_m % 10);\n\n if digit == 0 {\n assert(x as int == old_x);\n assert(place as int == old_place);\n assert(sum as int == old_sum);\n\n assert(\n Self::concat_non_zero_acc(old_m, old_place, old_x)\n == Self::concat_non_zero_acc(old_m / 10, old_place, old_x)\n );\n assert(\n Self::sum_non_zero_acc(old_m, old_sum)\n == Self::sum_non_zero_acc(old_m / 10, old_sum)\n );\n\n assert(m as int * place as int <= old_m * old_place) by (nonlinear_arith)\n requires\n m as int == old_m / 10,\n place as int == old_place,\n 0 <= old_m,\n 0 <= old_place,\n {\n }\n assert(x as int + m as int * place as int <= n as int) by (nonlinear_arith)\n requires\n x as int == old_x,\n m as int * place as int <= old_m * old_place,\n old_x + old_m * old_place <= n as int,\n {\n }\n assert(sum as int <= x as int);\n assert(1 <= place as int);\n } else {\n assert(x as int == old_x + (digit as int) * old_place);\n assert(place as int == old_place * 10);\n assert(sum as int == old_sum + digit as int);\n\n assert(\n Self::concat_non_zero_acc(old_m, old_place, old_x)\n == Self::concat_non_zero_acc(\n old_m / 10,\n old_place * 10,\n old_x + (digit as int) * old_place,\n )\n );\n assert(\n Self::sum_non_zero_acc(old_m, old_sum)\n == Self::sum_non_zero_acc(old_m / 10, old_sum + digit as int)\n );\n\n assert(\n x as int + m as int * place as int == old_x + old_m * old_place\n ) by (nonlinear_arith)\n requires\n x as int == old_x + (digit as int) * old_place,\n m as int == old_m / 10,\n place as int == old_place * 10,\n old_m == 10 * (old_m / 10) + digit as int,\n {\n }\n assert(x as int + m as int * place as int <= n as int) by (nonlinear_arith)\n requires\n x as int + m as int * place as int == old_x + old_m * old_place,\n old_x + old_m * old_place <= n as int,\n {\n }\n assert(sum as int <= x as int) by (nonlinear_arith)\n requires\n sum as int == old_sum + digit as int,\n x as int == old_x + (digit as int) * old_place,\n old_sum <= old_x,\n 1 <= old_place,\n 0 <= digit as int,\n {\n }\n assert(1 <= place as int) by (nonlinear_arith)\n requires\n place as int == old_place * 10,\n 1 <= old_place,\n {\n }\n }\n\n assert(\n Self::concat_non_zero_acc(m as int, place as int, x as int) == Self::concat_non_zero(\n n as int,\n )\n ) by {\n assert(\n Self::concat_non_zero_acc(m as int, place as int, x as int)\n == Self::concat_non_zero_acc(old_m, old_place, old_x)\n );\n }\n\n assert(\n Self::sum_non_zero_acc(m as int, sum as int) == Self::sum_non_zero(n as int)\n ) by {\n assert(\n Self::sum_non_zero_acc(m as int, sum as int)\n == Self::sum_non_zero_acc(old_m, old_sum)\n );\n }\n }\n }\n\n proof {\n assert(m == 0);\n assert(Self::concat_non_zero_acc(0, place as int, x as int) == x as int);\n assert(Self::sum_non_zero_acc(0, sum as int) == sum as int);\n assert(Self::concat_non_zero_acc(m as int, place as int, x as int) == Self::concat_non_zero(\n n as int,\n ));\n assert(Self::sum_non_zero_acc(m as int, sum as int) == Self::sum_non_zero(n as int));\n assert(x as int == Self::concat_non_zero(n as int));\n assert(sum as int == Self::sum_non_zero(n as int));\n assert(0 <= x as int <= n as int) by (nonlinear_arith)\n requires\n x as int + m as int * place as int <= n as int,\n 0 <= x as int,\n 0 <= m as int * place as int,\n {\n }\n assert(0 <= sum as int <= x as int);\n assert(0 <= x as int * sum as int <= 1_000_000_000_000_000_000) by (nonlinear_arith)\n requires\n 0 <= x as int <= n as int,\n 0 <= sum as int <= x as int,\n n as int <= 1_000_000_000,\n {\n }\n assert(0 <= x as int * sum as int <= i64::MAX as int) by (nonlinear_arith)\n requires\n 0 <= x as int * sum as int <= 1_000_000_000_000_000_000,\n {\n }\n }\n\n x * sum\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc3754/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3754/tests/mutated_testcases.jsonl"} {"problem": "lc3803", "family": "leetcode", "description": "# Count Residue Prefixes\n\nYou are given a string `s` consisting only of lowercase English letters.\n\nA **prefix** of `s` is called a **residue** if the number of **distinct characters** in the prefix is equal to `len(prefix) % 3`.\n\nReturn the count of **residue** prefixes in `s`.\n\nA **prefix** of a string is a **non-empty substring** that starts from the beginning of the string and extends to any point within it.\n\n## Example 1:\n\n**Input:** s = \"abc\"\n\n**Output:** 2\n\n**Explanation:**\n\n- Prefix `\"a\"` has 1 distinct character and length modulo 3 is 1, so it is a residue.\n- Prefix `\"ab\"` has 2 distinct characters and length modulo 3 is 2, so it is a residue.\n- Prefix `\"abc\"` does not satisfy the condition.\n\n## Example 2:\n\n**Input:** s = \"dd\"\n\n**Output:** 1\n\n**Explanation:**\n\n- Prefix `\"d\"` has 1 distinct character and length modulo 3 is 1, so it is a residue.\n- Prefix `\"dd\"` has 1 distinct character but length modulo 3 is 2, so it is not a residue.\n\n## Example 3:\n\n**Input:** s = \"bob\"\n\n**Output:** 2\n\n**Explanation:**\n\n- Prefix `\"b\"` has 1 distinct character and length modulo 3 is 1, so it is a residue.\n- Prefix `\"bo\"` has 2 distinct characters and length modulo 3 is 2, so it is a residue.\n\n## Constraints:\n\n- `1 <= s.length <= 100`\n- `s` contains only lowercase English letters.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn residue_prefixes(s: String) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nHash Table, String\nAcceptance rate: 65.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_lowercase_string(s: Seq) -> bool {\n forall |i: int| 0 <= i < s.len() ==> 97 <= (#[trigger] s[i] as u32) && (s[i] as u32) <= 122\n }\n\n pub open spec fn is_first_occurrence(s: Seq, idx: nat) -> bool\n recommends\n idx < s.len(),\n {\n forall |q: int| 0 <= q < idx ==> s[q] != s[idx as int]\n }\n\n pub open spec fn distinct_count_prefix(s: Seq, n: nat) -> int\n recommends\n n <= s.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::distinct_count_prefix(s, (n - 1) as nat)\n + (if Self::is_first_occurrence(s, (n - 1) as nat) { 1int } else { 0int })\n }\n }\n\n pub open spec fn residue_prefix_count(s: Seq, n: nat) -> int\n recommends\n n <= s.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::residue_prefix_count(s, (n - 1) as nat)\n + (if Self::distinct_count_prefix(s, n) == (n as int) % 3 { 1int } else { 0int })\n }\n }\n\n pub fn residue_prefixes(s: String) -> (res: i32)\n requires\n 1 <= s@.len() <= 100,\n Self::is_lowercase_string(s@),\n ensures\n res as int == Self::residue_prefix_count(s@, s@.len()),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn is_first_at(s: &String, p: usize) -> bool {\n let mut q: usize = 0;\n while q < p {\n if s.as_str().get_char(q) == s.as_str().get_char(p) {\n return false;\n }\n q += 1;\n }\n true\n }\n\n pub fn residue_prefixes(s: String) -> i32 {\n let n = s.as_str().unicode_len();\n let mut ans: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut distinct: i32 = 0;\n let mut p: usize = 0;\n while p <= i {\n let is_new = Self::is_first_at(&s, p);\n if is_new {\n distinct += 1;\n }\n p += 1;\n }\n\n let residue: i32 = ((i + 1) % 3) as i32;\n if distinct == residue {\n ans += 1;\n }\n i += 1;\n }\n ans\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_lowercase_string(s: Seq) -> bool {\n forall |i: int| 0 <= i < s.len() ==> 97 <= (#[trigger] s[i] as u32) && (s[i] as u32) <= 122\n }\n\n pub open spec fn is_first_occurrence(s: Seq, idx: nat) -> bool\n recommends\n idx < s.len(),\n {\n forall |q: int| 0 <= q < idx ==> s[q] != s[idx as int]\n }\n\n pub open spec fn distinct_count_prefix(s: Seq, n: nat) -> int\n recommends\n n <= s.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::distinct_count_prefix(s, (n - 1) as nat)\n + (if Self::is_first_occurrence(s, (n - 1) as nat) { 1int } else { 0int })\n }\n }\n\n pub open spec fn residue_prefix_count(s: Seq, n: nat) -> int\n recommends\n n <= s.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::residue_prefix_count(s, (n - 1) as nat)\n + (if Self::distinct_count_prefix(s, n) == (n as int) % 3 { 1int } else { 0int })\n }\n }\n\n fn is_first_at(s: &String, p: usize) -> (res: bool)\n requires\n p < s@.len(),\n ensures\n res <==> Self::is_first_occurrence(s@, p as nat),\n {\n let mut q: usize = 0;\n while q < p {\n if s.as_str().get_char(q) == s.as_str().get_char(p) {\n return false;\n }\n q += 1;\n }\n true\n }\n\n pub fn residue_prefixes(s: String) -> (res: i32)\n requires\n 1 <= s@.len() <= 100,\n Self::is_lowercase_string(s@),\n ensures\n res as int == Self::residue_prefix_count(s@, s@.len()),\n {\n let n = s.as_str().unicode_len();\n let mut ans: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n let mut distinct: i32 = 0;\n let mut p: usize = 0;\n while p <= i {\n let is_new = Self::is_first_at(&s, p);\n if is_new {\n distinct += 1;\n }\n p += 1;\n }\n\n let residue: i32 = ((i + 1) % 3) as i32;\n if distinct == residue {\n ans += 1;\n }\n i += 1;\n }\n ans\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_lowercase_string(s: Seq) -> bool {\n forall |i: int| 0 <= i < s.len() ==> 97 <= (#[trigger] s[i] as u32) && (s[i] as u32) <= 122\n }\n\n pub open spec fn is_first_occurrence(s: Seq, idx: nat) -> bool\n recommends\n idx < s.len(),\n {\n forall |q: int| 0 <= q < idx ==> s[q] != s[idx as int]\n }\n\n pub open spec fn distinct_count_prefix(s: Seq, n: nat) -> int\n recommends\n n <= s.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::distinct_count_prefix(s, (n - 1) as nat)\n + (if Self::is_first_occurrence(s, (n - 1) as nat) { 1int } else { 0int })\n }\n }\n\n pub open spec fn residue_prefix_count(s: Seq, n: nat) -> int\n recommends\n n <= s.len(),\n decreases n,\n {\n if n == 0 {\n 0\n } else {\n Self::residue_prefix_count(s, (n - 1) as nat)\n + (if Self::distinct_count_prefix(s, n) == (n as int) % 3 { 1int } else { 0int })\n }\n }\n\n proof fn lemma_distinct_step(s: Seq, n: nat)\n requires\n n < s.len(),\n ensures\n Self::distinct_count_prefix(s, (n + 1) as nat)\n == Self::distinct_count_prefix(s, n)\n + (if Self::is_first_occurrence(s, n) { 1int } else { 0int }),\n {\n }\n\n proof fn lemma_residue_step(s: Seq, n: nat)\n requires\n n < s.len(),\n ensures\n Self::residue_prefix_count(s, (n + 1) as nat)\n == Self::residue_prefix_count(s, n)\n + (if Self::distinct_count_prefix(s, (n + 1) as nat) == ((n + 1) as int) % 3 { 1int } else { 0int }),\n {\n }\n\n proof fn lemma_distinct_bound(s: Seq, n: nat)\n requires\n n <= s.len(),\n ensures\n 0 <= Self::distinct_count_prefix(s, n) <= n as int,\n decreases n,\n {\n if n > 0 {\n let m = (n - 1) as nat;\n Self::lemma_distinct_bound(s, m);\n Self::lemma_distinct_step(s, m);\n }\n }\n\n proof fn lemma_residue_bound(s: Seq, n: nat)\n requires\n n <= s.len(),\n ensures\n 0 <= Self::residue_prefix_count(s, n) <= n as int,\n decreases n,\n {\n if n > 0 {\n let m = (n - 1) as nat;\n Self::lemma_residue_bound(s, m);\n Self::lemma_distinct_bound(s, n);\n Self::lemma_residue_step(s, m);\n }\n }\n\n fn is_first_at(s: &String, p: usize) -> (res: bool)\n requires\n p < s@.len(),\n ensures\n res <==> Self::is_first_occurrence(s@, p as nat),\n {\n let mut q: usize = 0;\n while q < p\n invariant\n p < s@.len(),\n 0 <= q <= p,\n forall |t: int| 0 <= t < q as int ==> s@[t] != s@[p as int],\n decreases p - q,\n {\n let ghost cq = s@[q as int];\n let ghost cp = s@[p as int];\n if s.as_str().get_char(q) == s.as_str().get_char(p) {\n assert(cq == cp);\n proof {\n assert(s@[q as int] == cq);\n assert(s@[p as int] == cp);\n assert(!(forall |u: int| 0 <= u < p as int ==> s@[u] != s@[p as int])) by {\n let u = q as int;\n assert(0 <= u < p as int);\n assert(s@[u] == s@[p as int]);\n }\n assert(!Self::is_first_occurrence(s@, p as nat));\n }\n return false;\n }\n q += 1;\n }\n proof {\n assert(q == p);\n assert(forall |t: int| 0 <= t < p as int ==> s@[t] != s@[p as int]);\n assert(Self::is_first_occurrence(s@, p as nat));\n }\n true\n }\n\n pub fn residue_prefixes(s: String) -> (res: i32)\n requires\n 1 <= s@.len() <= 100,\n Self::is_lowercase_string(s@),\n ensures\n res as int == Self::residue_prefix_count(s@, s@.len()),\n {\n proof {\n Self::lemma_residue_bound(s@, s@.len());\n }\n let n = s.as_str().unicode_len();\n let mut ans: i32 = 0;\n let mut i: usize = 0;\n while i < n\n invariant\n 1 <= s@.len() <= 100,\n Self::is_lowercase_string(s@),\n n == s@.len(),\n 0 <= i <= n,\n ans as int == Self::residue_prefix_count(s@, i as nat),\n 0 <= ans <= i as int,\n decreases n - i,\n {\n let mut distinct: i32 = 0;\n let mut p: usize = 0;\n while p <= i\n invariant\n 1 <= s@.len() <= 100,\n n == s@.len(),\n i < n,\n 0 <= p <= i + 1,\n distinct as int == Self::distinct_count_prefix(s@, p as nat),\n 0 <= distinct <= p as int,\n decreases i + 1 - p,\n {\n let is_new = Self::is_first_at(&s, p);\n let ghost d_before = distinct;\n proof {\n Self::lemma_distinct_bound(s@, p as nat);\n assert(d_before as int == Self::distinct_count_prefix(s@, p as nat));\n assert(0 <= d_before <= p as int);\n }\n if is_new {\n distinct += 1;\n }\n proof {\n Self::lemma_distinct_step(s@, p as nat);\n if is_new {\n assert(Self::is_first_occurrence(s@, p as nat));\n assert(distinct as int == d_before as int + 1);\n } else {\n assert(!Self::is_first_occurrence(s@, p as nat));\n assert(distinct as int == d_before as int);\n }\n assert(distinct as int == Self::distinct_count_prefix(s@, (p + 1) as nat));\n Self::lemma_distinct_bound(s@, (p + 1) as nat);\n assert(0 <= distinct <= (p + 1) as int);\n }\n p += 1;\n }\n\n let residue: i32 = ((i + 1) % 3) as i32;\n let ghost ans_before = ans;\n if distinct == residue {\n ans += 1;\n }\n proof {\n let ii = i as nat;\n Self::lemma_residue_step(s@, ii);\n assert(ans_before as int == Self::residue_prefix_count(s@, ii));\n assert(distinct as int == Self::distinct_count_prefix(s@, (ii + 1) as nat));\n assert(residue as int == ((ii + 1) as int) % 3);\n if distinct == residue {\n assert(Self::distinct_count_prefix(s@, (ii + 1) as nat) == ((ii + 1) as int) % 3);\n assert(ans as int == ans_before as int + 1);\n } else {\n assert(Self::distinct_count_prefix(s@, (ii + 1) as nat) != ((ii + 1) as int) % 3);\n assert(ans as int == ans_before as int);\n }\n assert(ans as int == Self::residue_prefix_count(s@, (ii + 1) as nat));\n Self::lemma_residue_bound(s@, (ii + 1) as nat);\n assert(0 <= ans <= (ii + 1) as int);\n }\n i += 1;\n }\n\n ans\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3803/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3803/tests/mutated_testcases.jsonl"} {"problem": "lc3827", "family": "leetcode", "description": "# Count Monobit Integers\n\nYou are given an integer `n`.\n\nAn integer is called **Monobit** if all bits in its binary representation are the same.\n\nReturn the count of **Monobit** integers in the range `[0, n]` (inclusive).\n\n## Example 1:\n\n> **Input:** n = 1\n> **Output:** 2\n> **Explanation:** The integers in `[0, 1]` are `0` and `1`, and both have identical bits in binary.\n\n## Example 2:\n\n> **Input:** n = 4\n> **Output:** 3\n> **Explanation:** The integers in `[0, 4]` are `0`, `1`, `2`, `3`, `4`. Only `0`, `1`, and `3` are Monobit.\n\n## Constraints:\n\n- `0 <= n <= 1000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn count_monobit(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nBit Manipulation, Enumeration\nAcceptance rate: 66.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn monobit_value(k: int) -> int\n decreases k,\n {\n if k <= 0 {\n 0\n } else {\n 2 * Self::monobit_value(k - 1) + 1\n }\n }\n\n pub fn count_monobit(n: i32) -> (res: i32)\n requires\n 0 <= n <= 1000,\n ensures\n 1 <= res <= n + 1,\n forall|k: int| 0 <= k < res as int ==> #[trigger] Self::monobit_value(k) <= n as int,\n Self::monobit_value(res as int) > n as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn count_monobit(n: i32) -> i32 {\n let mut count: i32 = 0;\n let mut value: i32 = 0;\n while value <= n {\n count = count + 1;\n value = value * 2 + 1;\n }\n count\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn monobit_value(k: int) -> int\n decreases k,\n {\n if k <= 0 {\n 0\n } else {\n 2 * Self::monobit_value(k - 1) + 1\n }\n }\n\n pub fn count_monobit(n: i32) -> (res: i32)\n requires\n 0 <= n <= 1000,\n ensures\n 1 <= res <= n + 1,\n forall|k: int| 0 <= k < res as int ==> #[trigger] Self::monobit_value(k) <= n as int,\n Self::monobit_value(res as int) > n as int,\n {\n let mut count: i32 = 0;\n let mut value: i32 = 0;\n while value <= n {\n count = count + 1;\n value = value * 2 + 1;\n }\n count\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn monobit_value(k: int) -> int\n decreases k,\n {\n if k <= 0 {\n 0\n } else {\n 2 * Self::monobit_value(k - 1) + 1\n }\n }\n\n proof fn lemma_monobit_step(k: int)\n requires\n k >= 0,\n ensures\n Self::monobit_value(k + 1) == 2 * Self::monobit_value(k) + 1,\n {\n reveal_with_fuel(Solution::monobit_value, 2);\n }\n\n proof fn lemma_monobit_nonneg(k: int)\n requires\n k >= 0,\n ensures\n Self::monobit_value(k) >= 0,\n decreases k,\n {\n if k == 0 {\n reveal_with_fuel(Solution::monobit_value, 2);\n } else {\n Self::lemma_monobit_nonneg(k - 1);\n Self::lemma_monobit_step(k - 1);\n }\n }\n\n proof fn lemma_monobit_ge_index(k: int)\n requires\n k >= 0,\n ensures\n Self::monobit_value(k) >= k,\n decreases k,\n {\n if k == 0 {\n reveal_with_fuel(Solution::monobit_value, 2);\n } else {\n Self::lemma_monobit_ge_index(k - 1);\n Self::lemma_monobit_step(k - 1);\n Self::lemma_monobit_nonneg(k - 1);\n assert(2 * Self::monobit_value(k - 1) + 1 >= Self::monobit_value(k - 1) + 1);\n assert(Self::monobit_value(k) >= k - 1 + 1);\n }\n }\n\n pub fn count_monobit(n: i32) -> (res: i32)\n requires\n 0 <= n <= 1000,\n ensures\n 1 <= res <= n + 1,\n forall|k: int| 0 <= k < res as int ==> #[trigger] Self::monobit_value(k) <= n as int,\n Self::monobit_value(res as int) > n as int,\n {\n let mut count: i32 = 0;\n let mut value: i32 = 0;\n while value <= n\n invariant\n 0 <= n <= 1000,\n 0 <= count <= n + 1,\n value as int == Self::monobit_value(count as int),\n forall|k: int| 0 <= k < count as int ==> #[trigger] Self::monobit_value(k) <= n as int,\n decreases (n - count + 1) as int,\n {\n proof {\n Self::lemma_monobit_ge_index(count as int);\n assert(count as int <= value as int);\n }\n count = count + 1;\n value = value * 2 + 1;\n proof {\n Self::lemma_monobit_step(count as int - 1);\n assert(value as int == 2 * Self::monobit_value(count as int - 1) + 1);\n assert(value as int == Self::monobit_value(count as int));\n assert forall|k: int| 0 <= k < count as int implies #[trigger] Self::monobit_value(k) <= n as int by {\n if k < count as int - 1 {\n } else {\n assert(k == count as int - 1);\n assert(Self::monobit_value(k) == Self::monobit_value(count as int - 1));\n }\n }\n Self::lemma_monobit_ge_index(count as int - 1);\n assert(Self::monobit_value(count as int - 1) >= count as int - 1);\n assert(Self::monobit_value(count as int - 1) <= n as int);\n assert(count <= n + 1);\n }\n }\n proof {\n assert(count > 0);\n assert(value as int > n as int);\n assert(value as int == Self::monobit_value(count as int));\n }\n count\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3827/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3827/tests/mutated_testcases.jsonl"} {"problem": "lc3861", "family": "leetcode", "description": "# Minimum Capacity Box\n\nYou are given an integer array `capacity`, where `capacity[i]` represents the capacity of the `i`th box, and an integer `itemSize` representing the size of an item.\n\nThe `i`th box can store the item if `capacity[i] >= itemSize`.\n\nReturn an integer denoting the index of the box with the **minimum** capacity that can store the item. If multiple such boxes exist, return the **smallest index**.\n\nIf no box can store the item, return `-1`.\n\n## Example 1:\n\n> **Input:** capacity = [1,5,3,7], itemSize = 3 \n> **Output:** 2\n\n## Example 2:\n\n> **Input:** capacity = [3,5,4,3], itemSize = 2 \n> **Output:** 0\n\n## Example 3:\n\n> **Input:** capacity = [4], itemSize = 5 \n> **Output:** -1\n\n## Constraints:\n\n- $1 <= capacity.length <= 100$\n- $1 <= capacity[i] <= 100$\n- $1 <= itemSize <= 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn minimum_index(capacity: Vec, item_size: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 72.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_minimum_capacity_index(capacity: Seq, item_size: i32, idx: int) -> bool {\n 0 <= idx < capacity.len()\n && capacity[idx] >= item_size\n && forall |j: int| 0 <= j < capacity.len() && capacity[j] >= item_size ==> capacity[idx] <= #[trigger] capacity[j]\n && forall |j: int| 0 <= j < capacity.len() && capacity[j] >= item_size && capacity[j] == capacity[idx] ==> idx <= j\n }\n\n pub open spec fn valid_result(capacity: Seq, item_size: i32, res: int) -> bool {\n (res == -1 && forall |j: int| 0 <= j < capacity.len() ==> #[trigger] capacity[j] < item_size)\n || (0 <= res && Self::is_minimum_capacity_index(capacity, item_size, res))\n }\n\n pub fn minimum_index(capacity: Vec, item_size: i32) -> (res: i32)\n requires\n 1 <= capacity.len() <= 100,\n forall |k: int| 0 <= k < capacity.len() ==> 1 <= #[trigger] capacity[k] <= 100,\n 1 <= item_size <= 100,\n ensures\n Self::valid_result(capacity@, item_size, res as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn minimum_index(capacity: Vec, item_size: i32) -> i32 {\n let n = capacity.len();\n let mut i: usize = 0;\n let mut found: bool = false;\n let mut best_idx: usize = 0;\n let mut best_cap: i32 = 0;\n\n while i < n {\n let cap_i = capacity[i];\n if cap_i >= item_size {\n if !found {\n found = true;\n best_idx = i;\n best_cap = cap_i;\n } else if cap_i < best_cap {\n found = true;\n best_idx = i;\n best_cap = cap_i;\n }\n }\n i = i + 1;\n }\n\n if found {\n best_idx as i32\n } else {\n -1\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_minimum_capacity_index(capacity: Seq, item_size: i32, idx: int) -> bool {\n 0 <= idx < capacity.len()\n && capacity[idx] >= item_size\n && forall |j: int| 0 <= j < capacity.len() && capacity[j] >= item_size ==> capacity[idx] <= #[trigger] capacity[j]\n && forall |j: int| 0 <= j < capacity.len() && capacity[j] >= item_size && capacity[j] == capacity[idx] ==> idx <= j\n }\n\n pub open spec fn valid_result(capacity: Seq, item_size: i32, res: int) -> bool {\n (res == -1 && forall |j: int| 0 <= j < capacity.len() ==> #[trigger] capacity[j] < item_size)\n || (0 <= res && Self::is_minimum_capacity_index(capacity, item_size, res))\n }\n\n pub fn minimum_index(capacity: Vec, item_size: i32) -> (res: i32)\n requires\n 1 <= capacity.len() <= 100,\n forall |k: int| 0 <= k < capacity.len() ==> 1 <= #[trigger] capacity[k] <= 100,\n 1 <= item_size <= 100,\n ensures\n Self::valid_result(capacity@, item_size, res as int),\n {\n let n = capacity.len();\n let mut i: usize = 0;\n let mut found: bool = false;\n let mut best_idx: usize = 0;\n let mut best_cap: i32 = 0;\n\n while i < n {\n let cap_i = capacity[i];\n if cap_i >= item_size {\n if !found {\n found = true;\n best_idx = i;\n best_cap = cap_i;\n } else if cap_i < best_cap {\n found = true;\n best_idx = i;\n best_cap = cap_i;\n }\n }\n i = i + 1;\n }\n\n if found {\n best_idx as i32\n } else {\n -1\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_minimum_capacity_index(capacity: Seq, item_size: i32, idx: int) -> bool {\n 0 <= idx < capacity.len()\n && capacity[idx] >= item_size\n && forall |j: int| 0 <= j < capacity.len() && capacity[j] >= item_size ==> capacity[idx] <= #[trigger] capacity[j]\n && forall |j: int| 0 <= j < capacity.len() && capacity[j] >= item_size && capacity[j] == capacity[idx] ==> idx <= j\n }\n\n pub open spec fn valid_result(capacity: Seq, item_size: i32, res: int) -> bool {\n (res == -1 && forall |j: int| 0 <= j < capacity.len() ==> #[trigger] capacity[j] < item_size)\n || (0 <= res && Self::is_minimum_capacity_index(capacity, item_size, res))\n }\n\n pub fn minimum_index(capacity: Vec, item_size: i32) -> (res: i32)\n requires\n 1 <= capacity.len() <= 100,\n forall |k: int| 0 <= k < capacity.len() ==> 1 <= #[trigger] capacity[k] <= 100,\n 1 <= item_size <= 100,\n ensures\n Self::valid_result(capacity@, item_size, res as int),\n {\n let n = capacity.len();\n let mut i: usize = 0;\n let mut found: bool = false;\n let mut best_idx: usize = 0;\n let mut best_cap: i32 = 0;\n\n while i < n\n invariant\n n == capacity.len(),\n 1 <= n <= 100,\n 1 <= item_size <= 100,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] capacity[k] <= 100,\n 0 <= i <= n,\n !found ==> forall |j: int| 0 <= j < i ==> #[trigger] capacity[j] < item_size,\n found ==> 0 <= best_idx < i,\n found ==> best_cap == capacity[best_idx as int],\n found ==> capacity[best_idx as int] >= item_size,\n found ==> forall |j: int| 0 <= j < i && capacity[j] >= item_size ==> best_cap <= #[trigger] capacity[j],\n found ==> forall |j: int| 0 <= j < i && capacity[j] >= item_size && capacity[j] == best_cap ==> best_idx as int <= j,\n decreases n - i,\n {\n let cap_i = capacity[i];\n if cap_i >= item_size {\n if !found {\n found = true;\n best_idx = i;\n best_cap = cap_i;\n\n proof {\n assert(best_idx as int == i as int);\n assert(best_cap == capacity[i as int]);\n assert(best_cap >= item_size);\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size implies best_cap <= #[trigger] capacity[j] by {\n if j == i as int {\n } else {\n assert(0 <= j < i);\n assert(capacity[j] < item_size);\n assert(false);\n }\n }\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size && capacity[j] == best_cap implies best_idx as int <= j by {\n if j == i as int {\n } else {\n assert(0 <= j < i);\n assert(capacity[j] < item_size);\n assert(capacity[j] >= item_size);\n assert(false);\n }\n }\n }\n } else if cap_i < best_cap {\n let ghost old_best_cap = best_cap;\n found = true;\n best_idx = i;\n best_cap = cap_i;\n\n proof {\n assert(best_idx as int == i as int);\n assert(best_cap == capacity[i as int]);\n assert(best_cap >= item_size);\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size implies best_cap <= #[trigger] capacity[j] by {\n if j == i as int {\n } else {\n assert(0 <= j < i);\n assert(old_best_cap <= capacity[j]);\n assert(cap_i < old_best_cap);\n }\n }\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size && capacity[j] == best_cap implies best_idx as int <= j by {\n if j == i as int {\n } else {\n assert(0 <= j < i);\n assert(old_best_cap <= capacity[j]);\n assert(capacity[j] == best_cap);\n assert(best_cap == cap_i);\n assert(cap_i < old_best_cap);\n assert(capacity[j] < old_best_cap);\n assert(false);\n }\n }\n }\n } else {\n proof {\n assert(found);\n assert(cap_i >= best_cap);\n assert(best_idx < i);\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size implies best_cap <= #[trigger] capacity[j] by {\n if j == i as int {\n assert(capacity[j] == cap_i);\n } else {\n assert(0 <= j < i);\n }\n }\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size && capacity[j] == best_cap implies best_idx as int <= j by {\n if j == i as int {\n assert(cap_i == best_cap);\n } else {\n assert(0 <= j < i);\n }\n }\n }\n }\n } else {\n proof {\n if !found {\n assert forall |j: int| 0 <= j < i as int + 1 implies #[trigger] capacity[j] < item_size by {\n if j == i as int {\n assert(capacity[j] == cap_i);\n } else {\n assert(0 <= j < i);\n }\n }\n } else {\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size implies best_cap <= #[trigger] capacity[j] by {\n if j == i as int {\n assert(capacity[j] == cap_i);\n assert(cap_i < item_size);\n assert(capacity[j] >= item_size);\n assert(false);\n } else {\n assert(0 <= j < i);\n }\n }\n\n assert forall |j: int| 0 <= j < i as int + 1 && capacity[j] >= item_size && capacity[j] == best_cap implies best_idx as int <= j by {\n if j == i as int {\n assert(capacity[j] == cap_i);\n assert(cap_i < item_size);\n assert(capacity[j] >= item_size);\n assert(false);\n } else {\n assert(0 <= j < i);\n }\n }\n }\n }\n }\n i = i + 1;\n }\n\n if found {\n proof {\n assert(i == n);\n assert(0 <= best_idx < n);\n assert(capacity[best_idx as int] >= item_size);\n assert(forall |j: int| 0 <= j < n && capacity[j] >= item_size ==> best_cap <= #[trigger] capacity[j]);\n assert(best_cap == capacity[best_idx as int]);\n assert(forall |j: int| 0 <= j < n && capacity[j] >= item_size && capacity[j] == capacity[best_idx as int] ==> best_idx as int <= j);\n assert(Self::is_minimum_capacity_index(capacity@, item_size, best_idx as int));\n assert(Self::valid_result(capacity@, item_size, best_idx as int));\n }\n best_idx as i32\n } else {\n proof {\n assert(i == n);\n assert(forall |j: int| 0 <= j < n ==> #[trigger] capacity[j] < item_size);\n assert(Self::valid_result(capacity@, item_size, -1));\n }\n -1\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc3861/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc3861/tests/mutated_testcases.jsonl"} {"problem": "lc41", "family": "leetcode", "description": "# First Missing Positive\n\nGiven an unsorted integer array `nums`. Return the smallest positive integer that is not present in `nums`.\n\nYou must implement an algorithm that runs in `O(n)` time and uses `O(1)` auxiliary space.\n\n## Example 1:\n\n> **Input:** nums = [1,2,0]\n> **Output:** 3\n> **Explanation:** The numbers in the range [1,2] are all in the array.\n\n## Example 2:\n\n> **Input:** nums = [3,4,-1,1]\n> **Output:** 2\n> **Explanation:** 1 is in the array but 2 is missing.\n\n## Example 3:\n\n> **Input:** nums = [7,8,9,11,12]\n> **Output:** 1\n> **Explanation:** The smallest positive integer 1 is missing.\n\n## Constraints:\n\n- $1 <= nums.length <= 10^5$\n\n- $-2^{31} <= nums[i] <= 2^{31} - 1$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn first_missing_positive(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Hash Table\nAcceptance rate: 42.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains_value(nums: Seq, v: int) -> bool {\n exists|i: int| 0 <= i < nums.len() && nums[i] as int == v\n }\n\n pub fn first_missing_positive(nums: Vec) -> (result: i32)\n requires\n nums.len() >= 1,\n nums.len() <= 100_000,\n ensures\n result >= 1,\n !Self::contains_value(nums@, result as int),\n forall|v: int| 1 <= v < result as int ==> Self::contains_value(nums@, v),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn first_missing_positive(nums: Vec) -> i32\n {\n let n = nums.len();\n let mut arr = nums;\n let n_i32 = n as i32;\n let mut i: usize = 0;\n while i < n {\n while arr[i] >= 1 && arr[i] <= n_i32\n && arr[(arr[i] as usize) - 1] != arr[i]\n {\n let j = (arr[i] as usize) - 1;\n let vi = arr[i];\n let vj = arr[j];\n arr[j] = vi;\n arr[i] = vj;\n }\n i += 1;\n }\n let mut k: usize = 0;\n while k < n {\n if arr[k] != (k as i32) + 1 {\n return (k as i32) + 1;\n }\n k += 1;\n }\n (n as i32) + 1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn contains_value(nums: Seq, v: int) -> bool {\n exists|i: int| 0 <= i < nums.len() && nums[i] as int == v\n }\n\n pub fn first_missing_positive(nums: Vec) -> (result: i32)\n requires\n nums.len() >= 1,\n nums.len() <= 100_000,\n ensures\n result >= 1,\n !Self::contains_value(nums@, result as int),\n forall|v: int| 1 <= v < result as int ==> Self::contains_value(nums@, v),\n {\n let n = nums.len();\n let mut arr = nums;\n let n_i32 = n as i32;\n let mut i: usize = 0;\n while i < n\n {\n while arr[i] >= 1 && arr[i] <= n_i32\n && arr[(arr[i] as usize) - 1] != arr[i]\n {\n let j = (arr[i] as usize) - 1;\n let vi = arr[i];\n let vj = arr[j];\n arr.set(j, vi);\n arr.set(i, vj);\n }\n i += 1;\n }\n let mut k: usize = 0;\n while k < n\n {\n if arr[k] != (k as i32) + 1 {\n return (k as i32) + 1;\n }\n k += 1;\n }\n (n as i32) + 1\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n \n\n pub open spec fn contains_value(nums: Seq, v: int) -> bool {\n exists|i: int| 0 <= i < nums.len() && nums[i] as int == v\n }\n\n \n pub open spec fn count_correct(nums: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::count_correct(nums, end - 1)\n + if nums[end - 1] as int == end { 1int } else { 0int }\n }\n }\n\n \n\n proof fn lemma_count_correct_nonneg(nums: Seq, end: int)\n requires\n end >= 0,\n end <= nums.len(),\n ensures\n Self::count_correct(nums, end) >= 0,\n decreases end,\n {\n if end > 0 {\n Self::lemma_count_correct_nonneg(nums, end - 1);\n }\n }\n\n proof fn lemma_count_correct_bounded(nums: Seq, end: int)\n requires\n end >= 0,\n end <= nums.len(),\n ensures\n Self::count_correct(nums, end) <= end,\n decreases end,\n {\n if end > 0 {\n Self::lemma_count_correct_bounded(nums, end - 1);\n }\n }\n\n \n proof fn lemma_update_count_correct(nums: Seq, pos: int, val: i32, end: int)\n requires\n 0 <= pos < nums.len(),\n end >= 0,\n end <= nums.len(),\n ensures\n Self::count_correct(nums.update(pos, val), end)\n == Self::count_correct(nums, end)\n - (if pos < end && nums[pos] as int == pos + 1 { 1int } else { 0int })\n + (if pos < end && val as int == pos + 1 { 1int } else { 0int }),\n decreases end,\n {\n if end > 0 {\n Self::lemma_update_count_correct(nums, pos, val, end - 1);\n }\n }\n\n \n proof fn lemma_swap_preserves_contains(\n before: Seq, after: Seq, i: int, j: int, v: int,\n )\n requires\n before.len() == after.len(),\n 0 <= i < before.len(),\n 0 <= j < before.len(),\n after == before.update(j, before[i]).update(i, before[j]),\n ensures\n Self::contains_value(after, v) == Self::contains_value(before, v),\n {\n \n if Self::contains_value(before, v) {\n let wit = choose|k: int| 0 <= k < before.len() && before[k] as int == v;\n if wit == i {\n \n \n \n if i == j {\n assert(after[i] as int == v);\n } else {\n assert(after[j] == before[i]);\n assert(after[j] as int == v);\n }\n } else if wit == j {\n \n assert(after[i] == before[j]);\n assert(after[i] as int == v);\n } else {\n \n assert(after[wit] == before[wit]);\n assert(after[wit] as int == v);\n }\n }\n \n if Self::contains_value(after, v) {\n let wit = choose|k: int| 0 <= k < after.len() && after[k] as int == v;\n if wit == i {\n \n assert(before[j] as int == v);\n } else if wit == j {\n if i == j {\n assert(before[i] as int == v);\n } else {\n \n assert(after[j] == before[i]);\n assert(before[i] as int == v);\n }\n } else {\n assert(after[wit] == before[wit]);\n assert(before[wit] as int == v);\n }\n }\n }\n\n \n\n pub fn first_missing_positive(nums: Vec) -> (result: i32)\n requires\n nums.len() >= 1,\n nums.len() <= 100_000,\n ensures\n result >= 1,\n !Self::contains_value(nums@, result as int),\n forall|v: int| 1 <= v < result as int ==> Self::contains_value(nums@, v),\n {\n let n = nums.len();\n let mut arr = nums;\n let n_i32 = n as i32;\n\n \n let mut i: usize = 0;\n while i < n\n invariant\n n == arr@.len(),\n n <= 100_000,\n n_i32 == n as i32,\n 0 <= i <= n,\n \n forall|v: int|\n Self::contains_value(arr@, v) == Self::contains_value(nums@, v),\n \n forall|k: int|\n #![trigger arr@[k]]\n 0 <= k < i as int && 1 <= arr@[k] as int <= n as int\n ==> arr@[(arr@[k] as int - 1)] == arr@[k],\n decreases n - i,\n {\n \n while arr[i] >= 1 && arr[i] <= n_i32\n && arr[(arr[i] as usize) - 1] != arr[i]\n invariant\n n == arr@.len(),\n n <= 100_000,\n n_i32 == n as i32,\n 0 <= i < n,\n \n forall|v: int|\n Self::contains_value(arr@, v) == Self::contains_value(nums@, v),\n \n forall|k: int|\n #![trigger arr@[k]]\n 0 <= k < i as int && 1 <= arr@[k] as int <= n as int\n ==> arr@[(arr@[k] as int - 1)] == arr@[k],\n decreases n as int - Self::count_correct(arr@, n as int),\n {\n let j = (arr[i] as usize) - 1;\n let vi = arr[i];\n let vj = arr[j];\n\n \n let ghost before = arr@;\n\n proof {\n assert(before[j as int] as int != (j + 1) as int) by {\n };\n\n assert(i != j) by {\n };\n assert(before[i as int] as int != (i + 1) as int);\n }\n\n arr.set(j, vi);\n arr.set(i, vj);\n\n proof {\n let after = arr@;\n\n \n assert forall|v: int| #![auto]\n Self::contains_value(after, v) == Self::contains_value(nums@, v)\n by {\n Self::lemma_swap_preserves_contains(before, after, i as int, j as int, v);\n };\n\n \n let mid = before.update(j as int, vi);\n Self::lemma_update_count_correct(before, j as int, vi, n as int);\n assert(Self::count_correct(mid, n as int)\n == Self::count_correct(before, n as int) + 1);\n\n Self::lemma_update_count_correct(mid, i as int, vj, n as int);\n assert(mid[i as int] == before[i as int]);\n assert(Self::count_correct(after, n as int)\n >= Self::count_correct(before, n as int) + 1);\n\n Self::lemma_count_correct_bounded(after, n as int);\n Self::lemma_count_correct_nonneg(before, n as int);\n\n \n assert forall|k: int|\n #![trigger arr@[k]]\n 0 <= k < i as int && 1 <= arr@[k] as int <= n as int\n implies arr@[(arr@[k] as int - 1)] == arr@[k]\n by {\n if k == j as int {\n assert(after[k] as int == (k + 1));\n assert(after[(after[k] as int - 1)] == after[k]);\n } else {\n assert(after[k] == before[k]);\n\n let t = before[k] as int - 1;\n assert(before[t] == before[k]);\n\n assert(t != j as int) by {\n if t == j as int {\n assert(before[j as int] == before[k]);\n assert(before[k] as int == j as int + 1);\n assert(before[j as int] as int == j as int + 1);\n assert(before[i as int] as int == j as int + 1);\n assert(before[j as int] == before[i as int]);\n assert(false);\n }\n };\n\n assert(t != i as int) by {\n if t == i as int {\n assert(before[k] as int == i as int + 1);\n assert(before[i as int] == before[k]);\n assert(before[i as int] as int == i as int + 1);\n assert(false);\n }\n };\n\n assert(after[t] == before[t]);\n assert(after[t] == after[k]);\n }\n };\n }\n }\n i += 1;\n }\n\n \n let ghost final_nums = arr@;\n\n \n proof {\n assert forall|v: int|\n 1 <= v <= n as int && #[trigger] Self::contains_value(nums@, v)\n implies final_nums[(v - 1)] as int == v\n by {\n assert(Self::contains_value(final_nums, v));\n let p = choose|p: int| 0 <= p < final_nums.len() && final_nums[p] as int == v;\n assert(final_nums[(final_nums[p] as int - 1)] == final_nums[p]);\n };\n }\n\n let mut k: usize = 0;\n while k < n\n invariant\n n == final_nums.len(),\n n <= 100_000,\n n == nums@.len(),\n 0 <= k <= n,\n arr@ == final_nums,\n forall|v: int| #![auto]\n Self::contains_value(final_nums, v) == Self::contains_value(nums@, v),\n forall|v: int|\n 1 <= v <= n as int && #[trigger] Self::contains_value(nums@, v)\n ==> final_nums[(v - 1)] as int == v,\n \n forall|m: int| 0 <= m < k as int ==> final_nums[m] as int == m + 1,\n \n forall|v: int| 1 <= v <= k as int ==> Self::contains_value(nums@, v),\n decreases n - k,\n {\n if arr[k] != (k as i32) + 1 {\n proof {\n if Self::contains_value(nums@, (k as int + 1)) {\n \n \n assert(final_nums[k as int] as int == (k as int + 1));\n assert(false);\n }\n }\n return (k as i32) + 1;\n }\n proof {\n assert(final_nums[k as int] as int == (k as int + 1));\n assert(Self::contains_value(final_nums, (k as int + 1)));\n assert(Self::contains_value(nums@, (k as int + 1)));\n }\n k += 1;\n }\n\n \n proof {\n if Self::contains_value(nums@, (n as int + 1)) {\n assert(Self::contains_value(final_nums, (n as int + 1)));\n let wit = choose|p: int|\n 0 <= p < final_nums.len() && final_nums[p] as int == (n as int + 1);\n assert(final_nums[wit] as int == wit + 1);\n assert(false);\n }\n }\n (n as i32) + 1\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc41/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc41/tests/mutated_testcases.jsonl"} {"problem": "lc414", "family": "leetcode", "description": "# Third Maximum Number\n\nGiven an integer array `nums`, return **the third distinct maximum** number in this array. If the third maximum does not exist, return the **maximum** number.\n\n## Example 1:\n\n> **Input:** nums = [3,2,1]\n> **Output:** 1\n> **Explanation:** \nThe first distinct maximum is 3.\nThe second distinct maximum is 2.\nThe third distinct maximum is 1.\n\n## Example 2:\n\n> **Input:** nums = [1,2]\n> **Output:** 2\n> **Explanation:** \nThe first distinct maximum is 2.\nThe second distinct maximum is 1.\nThe third distinct maximum does not exist, so the maximum (2) is returned instead.\n\n## Example 3:\n\n> **Input:** nums = [2,2,3,1]\n> **Output:** 1\n> **Explanation:** \nThe first distinct maximum is 3.\nThe second distinct maximum is 2 (both 2's are counted together since they have the same value).\nThe third distinct maximum is 1.\n\n## Constraints:\n\n- $1 <= nums.length <= 10^4$\n\n- $-2^{31} <= nums[i] <= 2^{31} - 1$\n\n**Follow up:** Can you find an O(n) solution?\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn third_max(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Sorting\nAcceptance rate: 38.7%", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn third_max(nums: Vec) -> (res: i32) \n requires\n 1 <= nums.len() <= 10_000, \n forall|i: int| 0 <= i < nums.len() ==> i32::MIN <= #[trigger] nums[i] <= i32::MAX,\n ensures \n exists|i: int| 0 <= i < nums.len() && res == #[trigger] nums[i],\n (exists|a: int, b: int, c: int| \n 0 <= a < nums.len() && 0 <= b < nums.len() && 0 <= c < nums.len() &&\n #[trigger] nums[a] != #[trigger] nums[b] && #[trigger] nums[c] != nums[a] && nums[b] != nums[c]) ==>\n (\n exists|k: int| 0 <= k < nums.len() && res == #[trigger] nums[k] &&\n (exists|a: int, b: int| 0 <= a < nums.len() && 0 <= b < nums.len() &&\n #[trigger] nums[a] > res && #[trigger] nums[b] > res && nums[a] != nums[b]) &&\n (forall|i: int| 0 <= i < nums.len() && #[trigger] nums[i] > res ==> \n (forall|j: int| 0 <= j < nums.len() && #[trigger] nums[j] > res && nums[i] != nums[j] ==>\n (forall|k: int| 0 <= k < nums.len() && #[trigger] nums[k] > res ==>\n nums[k] == nums[i] || nums[k] == nums[j])))\n ),\n (forall|a: int, b: int, c: int| \n 0 <= a < nums.len() && 0 <= b < nums.len() && 0 <= c < nums.len() ==>\n !(#[trigger] nums[a] != #[trigger] nums[b] && #[trigger] nums[c] != nums[a] && nums[b] != nums[c])) ==>\n (\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] <= res\n ),\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn third_max(nums: Vec) -> i32\n {\n let mut first: i32 = nums[0];\n let mut second: i32 = i32::MIN;\n let mut third: i32 = i32::MIN;\n let mut has_second = false;\n let mut has_third = false;\n \n let mut idx = 1;\n while idx < nums.len()\n {\n let num = nums[idx];\n let old_first = first;\n let old_second = second;\n let old_third = third;\n let old_has_second = has_second;\n let old_has_third = has_third;\n \n if num > first {\n third = second;\n has_third = has_second;\n second = first;\n has_second = true;\n first = num;\n } else if num < first && (!has_second || num > second) {\n third = second;\n has_third = has_second;\n second = num;\n has_second = true;\n } else if has_second && num < second && (!has_third || num > third) {\n third = num;\n has_third = true;\n } \n \n idx += 1;\n }\n \n if has_third {\n third\n } else {\n first\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn third_max(nums: Vec) -> (res: i32) \n requires\n 1 <= nums.len() <= 10_000, \n forall|i: int| 0 <= i < nums.len() ==> i32::MIN <= #[trigger] nums[i] <= i32::MAX,\n ensures \n exists|i: int| 0 <= i < nums.len() && res == #[trigger] nums[i],\n (exists|a: int, b: int, c: int| \n 0 <= a < nums.len() && 0 <= b < nums.len() && 0 <= c < nums.len() &&\n #[trigger] nums[a] != #[trigger] nums[b] && #[trigger] nums[c] != nums[a] && nums[b] != nums[c]) ==>\n (\n exists|k: int| 0 <= k < nums.len() && res == #[trigger] nums[k] &&\n (exists|a: int, b: int| 0 <= a < nums.len() && 0 <= b < nums.len() &&\n #[trigger] nums[a] > res && #[trigger] nums[b] > res && nums[a] != nums[b]) &&\n (forall|i: int| 0 <= i < nums.len() && #[trigger] nums[i] > res ==> \n (forall|j: int| 0 <= j < nums.len() && #[trigger] nums[j] > res && nums[i] != nums[j] ==>\n (forall|k: int| 0 <= k < nums.len() && #[trigger] nums[k] > res ==>\n nums[k] == nums[i] || nums[k] == nums[j])))\n ),\n (forall|a: int, b: int, c: int| \n 0 <= a < nums.len() && 0 <= b < nums.len() && 0 <= c < nums.len() ==>\n !(#[trigger] nums[a] != #[trigger] nums[b] && #[trigger] nums[c] != nums[a] && nums[b] != nums[c])) ==>\n (\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] <= res\n ),\n {\n let mut first: i32 = nums[0];\n let mut second: i32 = i32::MIN;\n let mut third: i32 = i32::MIN;\n let mut has_second = false;\n let mut has_third = false;\n \n let mut idx = 1;\n while idx < nums.len()\n {\n let num = nums[idx];\n let old_first = first;\n let old_second = second;\n let old_third = third;\n let old_has_second = has_second;\n let old_has_third = has_third;\n \n if num > first {\n third = second;\n has_third = has_second;\n second = first;\n has_second = true;\n first = num;\n } else if num < first && (!has_second || num > second) {\n third = second;\n has_third = has_second;\n second = num;\n has_second = true;\n } else if has_second && num < second && (!has_third || num > third) {\n third = num;\n has_third = true;\n } \n \n idx += 1;\n }\n \n if has_third {\n third\n } else {\n first\n }\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn third_max(nums: Vec) -> (res: i32) \n requires\n 1 <= nums.len() <= 10_000, \n forall|i: int| 0 <= i < nums.len() ==> i32::MIN <= #[trigger] nums[i] <= i32::MAX,\n ensures \n exists|i: int| 0 <= i < nums.len() && res == #[trigger] nums[i],\n (exists|a: int, b: int, c: int| \n 0 <= a < nums.len() && 0 <= b < nums.len() && 0 <= c < nums.len() &&\n #[trigger] nums[a] != #[trigger] nums[b] && #[trigger] nums[c] != nums[a] && nums[b] != nums[c]) ==>\n (\n exists|k: int| 0 <= k < nums.len() && res == #[trigger] nums[k] &&\n (exists|a: int, b: int| 0 <= a < nums.len() && 0 <= b < nums.len() &&\n #[trigger] nums[a] > res && #[trigger] nums[b] > res && nums[a] != nums[b]) &&\n (forall|i: int| 0 <= i < nums.len() && #[trigger] nums[i] > res ==> \n (forall|j: int| 0 <= j < nums.len() && #[trigger] nums[j] > res && nums[i] != nums[j] ==>\n (forall|k: int| 0 <= k < nums.len() && #[trigger] nums[k] > res ==>\n nums[k] == nums[i] || nums[k] == nums[j])))\n ),\n (forall|a: int, b: int, c: int| \n 0 <= a < nums.len() && 0 <= b < nums.len() && 0 <= c < nums.len() ==>\n !(#[trigger] nums[a] != #[trigger] nums[b] && #[trigger] nums[c] != nums[a] && nums[b] != nums[c])) ==>\n (\n forall|i: int| 0 <= i < nums.len() ==> #[trigger] nums[i] <= res\n ),\n {\n let mut first: i32 = nums[0];\n let mut second: i32 = i32::MIN;\n let mut third: i32 = i32::MIN;\n let mut has_second = false;\n let mut has_third = false;\n \n let mut idx = 1;\n while idx < nums.len()\n invariant\n 1 <= nums.len() <= 10_000, \n forall|i: int| 0 <= i < nums.len() ==> i32::MIN <= #[trigger] nums[i] <= i32::MAX,\n 1 <= idx <= nums.len(),\n exists|i: int| 0 <= i < idx && first == #[trigger] nums[i],\n has_second ==> exists|i: int| 0 <= i < idx && second == #[trigger] nums[i],\n has_third ==> exists|i: int| 0 <= i < idx && third == #[trigger] nums[i],\n has_third ==> has_second,\n has_third ==> first > second && second > third,\n has_second && !has_third ==> first > second,\n forall|j: int| 0 <= j < idx ==> #[trigger] nums[j] <= first,\n has_second ==> forall|j: int| 0 <= j < idx && #[trigger] nums[j] > second ==> nums[j] == first,\n !has_second ==> forall|j: int| 0 <= j < idx ==> #[trigger] nums[j] == first,\n (has_second && !has_third) ==> forall|j: int| 0 <= j < idx ==> \n #[trigger] nums[j] == first || nums[j] == second,\n has_third ==> forall|j: int| 0 <= j < idx && #[trigger] nums[j] > third ==> \n nums[j] == first || nums[j] == second,\n decreases nums.len() - idx,\n {\n let num = nums[idx];\n let old_first = first;\n let old_second = second;\n let old_third = third;\n let old_has_second = has_second;\n let old_has_third = has_third;\n \n if num > first {\n third = second;\n has_third = has_second;\n second = first;\n has_second = true;\n first = num;\n } else if num < first && (!has_second || num > second) {\n third = second;\n has_third = has_second;\n second = num;\n has_second = true;\n } else if has_second && num < second && (!has_third || num > third) {\n third = num;\n has_third = true;\n } \n \n idx += 1;\n }\n \n if has_third {\n proof {\n let fi = choose|i: int| 0 <= i < nums.len() as int && first == #[trigger] nums[i];\n let si = choose|i: int| 0 <= i < nums.len() as int && second == #[trigger] nums[i];\n assert(nums[fi] > third);\n assert(nums[si] > third);\n assert(nums[fi] != nums[si]);\n }\n third\n } else {\n first\n }\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc414/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc414/tests/mutated_testcases.jsonl"} {"problem": "lc453", "family": "leetcode", "description": "# Minimum Moves to Equal Array Elements\n\nGiven an integer array `nums` of size `n`, return *the minimum number of moves required to make all array elements equal*.\n\nIn one move, you can increment `n - 1` elements of the array by `1`.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3]\n> **Output:** 3\n> **Explanation:** Only three moves are needed (remember each move increments two elements):\n> [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]\n\n## Example 2:\n\n> **Input:** nums = [1,1,1]\n> **Output:** 0\n\n## Constraints:\n\n- $1 \\le \\texttt{nums.length} \\le 10^5$\n- $-10^9 \\le \\texttt{nums[i]} \\le 10^9$\n- The answer is guaranteed to fit in a **32-bit** integer.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_moves(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math\nAcceptance rate: 58.6%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seq_sum(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::seq_sum(s, end - 1) + s[end - 1] as int\n }\n }\n\n pub open spec fn seq_min(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 1 {\n if end <= 0 { 0 } else { s[0] as int }\n } else {\n let prev = Self::seq_min(s, end - 1);\n let cur = s[end - 1] as int;\n if prev <= cur { prev } else { cur }\n }\n }\n\n pub open spec fn min_moves_spec(nums: Seq) -> int {\n let n = nums.len() as int;\n Self::seq_sum(nums, n) - n * Self::seq_min(nums, n)\n }\n\n pub fn min_moves(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==>\n -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000,\n Self::min_moves_spec(nums@) >= -2_147_483_648,\n Self::min_moves_spec(nums@) <= 2_147_483_647,\n ensures\n result as int == Self::min_moves_spec(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn min_moves(nums: Vec) -> i32\n {\n let n = nums.len();\n let mut min_val = nums[0];\n let mut sum = nums[0] as i64;\n let mut i = 1usize;\n while i < n {\n if nums[i] < min_val {\n min_val = nums[i];\n }\n sum = sum + nums[i] as i64;\n i = i + 1;\n }\n (sum - (n as i64) * (min_val as i64)) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seq_sum(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::seq_sum(s, end - 1) + s[end - 1] as int\n }\n }\n\n pub open spec fn seq_min(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 1 {\n if end <= 0 { 0 } else { s[0] as int }\n } else {\n let prev = Self::seq_min(s, end - 1);\n let cur = s[end - 1] as int;\n if prev <= cur { prev } else { cur }\n }\n }\n\n pub open spec fn min_moves_spec(nums: Seq) -> int {\n let n = nums.len() as int;\n Self::seq_sum(nums, n) - n * Self::seq_min(nums, n)\n }\n\n pub fn min_moves(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==>\n -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000,\n Self::min_moves_spec(nums@) >= -2_147_483_648,\n Self::min_moves_spec(nums@) <= 2_147_483_647,\n ensures\n result as int == Self::min_moves_spec(nums@),\n {\n let n = nums.len();\n let mut min_val = nums[0];\n let mut sum = nums[0] as i64;\n let mut i = 1usize;\n while i < n\n {\n if nums[i] < min_val {\n min_val = nums[i];\n }\n sum = sum + nums[i] as i64;\n i = i + 1;\n }\n (sum - (n as i64) * (min_val as i64)) as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn seq_sum(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 0 {\n 0\n } else {\n Self::seq_sum(s, end - 1) + s[end - 1] as int\n }\n }\n\n pub open spec fn seq_min(s: Seq, end: int) -> int\n decreases end,\n {\n if end <= 1 {\n if end <= 0 { 0 } else { s[0] as int }\n } else {\n let prev = Self::seq_min(s, end - 1);\n let cur = s[end - 1] as int;\n if prev <= cur { prev } else { cur }\n }\n }\n\n pub open spec fn min_moves_spec(nums: Seq) -> int {\n let n = nums.len() as int;\n Self::seq_sum(nums, n) - n * Self::seq_min(nums, n)\n }\n\n proof fn lemma_seq_sum_unfold(s: Seq, end: int)\n requires 0 <= end <= s.len(),\n ensures\n Self::seq_sum(s, end + 1) == Self::seq_sum(s, end) + s[end] as int,\n decreases end,\n {\n reveal_with_fuel(Solution::seq_sum, 3);\n }\n\n proof fn lemma_seq_min_unfold(s: Seq, end: int)\n requires 1 <= end < s.len(),\n ensures\n Self::seq_min(s, end + 1) == (if Self::seq_min(s, end) <= s[end] as int\n { Self::seq_min(s, end) } else { s[end] as int }),\n decreases end,\n {\n reveal_with_fuel(Solution::seq_min, 3);\n }\n\n proof fn lemma_seq_min_single(s: Seq)\n requires 1 <= s.len(),\n ensures Self::seq_min(s, 1) == s[0] as int,\n {\n reveal_with_fuel(Solution::seq_min, 2);\n }\n\n pub fn min_moves(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==>\n -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000,\n Self::min_moves_spec(nums@) >= -2_147_483_648,\n Self::min_moves_spec(nums@) <= 2_147_483_647,\n ensures\n result as int == Self::min_moves_spec(nums@),\n {\n let n = nums.len();\n let mut min_val = nums[0];\n let mut sum = nums[0] as i64;\n let mut i = 1usize;\n\n proof {\n reveal_with_fuel(Solution::seq_sum, 2);\n reveal_with_fuel(Solution::seq_min, 2);\n assert(Self::seq_sum(nums@, 1) == nums[0] as int);\n assert(Self::seq_min(nums@, 1) == nums[0] as int);\n }\n\n while i < n\n invariant\n 1 <= n <= 100_000,\n n == nums.len(),\n 1 <= i <= n,\n forall |j: int| 0 <= j < nums.len() ==>\n -1_000_000_000 <= #[trigger] nums[j] <= 1_000_000_000,\n min_val as int == Self::seq_min(nums@, i as int),\n sum as int == Self::seq_sum(nums@, i as int),\n -(i as int) * 1_000_000_000 <= sum as int <= (i as int) * 1_000_000_000,\n -102_000_000_000_000i64 <= sum <= 102_000_000_000_000i64,\n decreases n - i,\n {\n if nums[i] < min_val {\n min_val = nums[i];\n }\n proof {\n Self::lemma_seq_sum_unfold(nums@, i as int);\n Self::lemma_seq_min_unfold(nums@, i as int);\n assert(Self::seq_sum(nums@, i as int + 1) == Self::seq_sum(nums@, i as int) + nums@[i as int] as int);\n assert(-102_000_000_000_000 <= sum as int + nums@[i as int] as int <= 102_000_000_000_000);\n }\n sum = sum + nums[i] as i64;\n i = i + 1;\n }\n\n proof {\n assert(sum as int == Self::seq_sum(nums@, n as int));\n assert(min_val as int == Self::seq_min(nums@, n as int));\n assert(Self::min_moves_spec(nums@) == Self::seq_sum(nums@, n as int)\n - (n as int) * Self::seq_min(nums@, n as int));\n assert((sum - (n as i64) * (min_val as i64)) as int == Self::min_moves_spec(nums@));\n assert(-2_147_483_648 <= (sum - (n as i64) * (min_val as i64)) <= 2_147_483_647);\n }\n (sum - (n as i64) * (min_val as i64)) as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc453/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc453/tests/mutated_testcases.jsonl"} {"problem": "lc456", "family": "leetcode", "description": "# 132 Pattern\n\nGiven an array of `n` integers `nums`, a **132 pattern** is a subsequence of three integers `nums[i]`, `nums[j]` and `nums[k]` such that `i < j < k` and `nums[i] < nums[k] < nums[j]`.\n\nReturn `true` *if there is a **132 pattern** in `nums`, otherwise, return* `false`.\n\n## Example 1:\n\n> **Input:** nums = [1,2,3,4]\n> **Output:** false\n> **Explanation:** There is no 132 pattern in the sequence.\n\n## Example 2:\n\n> **Input:** nums = [3,1,4,2]\n> **Output:** true\n> **Explanation:** There is a 132 pattern in the sequence: [1, 4, 2].\n\n## Example 3:\n\n> **Input:** nums = [-1,3,2,0]\n> **Output:** true\n> **Explanation:** There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].\n\n## Constraints:\n\n- $n == nums.length$\n\n- $1 <= n <= 2 * 10^5$\n\n- $-10^9 <= nums[i] <= 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find132pattern(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nPrincipal, Array, Binary Search, Stack, Monotonic Stack, Ordered Set\nAcceptance rate: 34.5%", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn find132pattern(nums: Vec) -> (res: bool) \n requires \n 1 <= nums.len() <= 200_000, \n forall |i: int| 0 <= i < nums.len() ==> -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000, \n ensures\n res == (exists |i: int, j: int, k: int| \n 0 <= i < j < k < nums.len() &&\n #[trigger] nums[i] < #[trigger] nums[k] < #[trigger] nums[j]), \n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn find132pattern(nums: Vec) -> bool {\n if nums.len() < 3 {\n return false;\n }\n\n let mut min_left: Vec = Vec::new();\n min_left.push(nums[0]);\n \n let mut m: usize = 1;\n while m < nums.len() {\n let prev = min_left[m - 1];\n let curr = nums[m];\n if curr < prev {\n min_left.push(curr);\n } else {\n min_left.push(prev);\n }\n m += 1;\n }\n\n let mut stack: Vec = Vec::new();\n\n let mut j: usize = nums.len() - 1;\n\n while j > 0 {\n let current = nums[j];\n let l_min = min_left[j - 1];\n\n while stack.len() > 0 && *stack.last().unwrap() <= l_min {\n stack.pop();\n }\n\n if current > l_min {\n if stack.len() > 0 && *stack.last().unwrap() < current {\n return true;\n }\n \n stack.push(current);\n }\n\n j -= 1;\n }\n \n false\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn spec_min_prefix(nums: Seq, j: int) -> i32\n recommends 0 <= j < nums.len()\n decreases j\n{\n if j <= 0 {\n nums[0]\n } else if nums[j] < spec_min_prefix(nums, j - 1) {\n nums[j]\n } else {\n spec_min_prefix(nums, j - 1)\n }\n}\n\n\n\n\npub struct Solution;\n\nimpl Solution {\n pub fn find132pattern(nums: Vec) -> (res: bool) \n requires \n 1 <= nums.len() <= 200_000, \n forall |i: int| 0 <= i < nums.len() ==> -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000, \n ensures\n res == (exists |i: int, j: int, k: int| \n 0 <= i < j < k < nums.len() &&\n #[trigger] nums[i] < #[trigger] nums[k] < #[trigger] nums[j]), \n {\n if nums.len() < 3 {\n \n return false;\n }\n\n let mut min_left: Vec = Vec::new();\n min_left.push(nums[0]);\n \n let mut m: usize = 1;\n while m < nums.len()\n decreases nums.len() - m\n {\n let prev = min_left[m - 1];\n let curr = nums[m];\n if curr < prev {\n min_left.push(curr);\n } else {\n min_left.push(prev);\n }\n m += 1;\n }\n\n let mut stack: Vec = Vec::new();\n\n let mut j: usize = nums.len() - 1;\n\n while j > 0\n decreases j\n {\n let current = nums[j];\n let l_min = min_left[j - 1];\n\n while stack.len() > 0 && *stack.last().unwrap() <= l_min\n decreases stack.len()\n {\n stack.pop();\n \n }\n\n if current > l_min {\n if stack.len() > 0 && *stack.last().unwrap() < current {\n return true;\n }\n stack.push(current);\n }\n j -= 1;\n }\n \n false\n }\n}\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub open spec fn spec_min_prefix(nums: Seq, j: int) -> i32\n recommends 0 <= j < nums.len()\n decreases j\n{\n if j <= 0 {\n nums[0]\n } else if nums[j] < spec_min_prefix(nums, j - 1) {\n nums[j]\n } else {\n spec_min_prefix(nums, j - 1)\n }\n}\n\nproof fn lemma_min_prefix_le(nums: Seq, j: int, i: int)\n requires 0 <= i <= j, j < nums.len()\n ensures spec_min_prefix(nums, j) <= nums[i]\n decreases j\n{\n if j == 0 {\n } else if i == j {\n if nums[j] < spec_min_prefix(nums, j - 1) {\n } else {\n lemma_min_prefix_le(nums, j - 1, i - 1);\n }\n } else {\n lemma_min_prefix_le(nums, j - 1, i);\n }\n}\n\nproof fn lemma_min_prefix_witness(nums: Seq, j: int) -> (w: int)\n requires 0 <= j, j < nums.len()\n ensures 0 <= w <= j, spec_min_prefix(nums, j) == nums[w]\n decreases j\n{\n if j == 0 {\n 0\n } else if nums[j] < spec_min_prefix(nums, j - 1) {\n j\n } else {\n lemma_min_prefix_witness(nums, j - 1)\n }\n}\n\nproof fn lemma_min_prefix_mono(nums: Seq, a: int, b: int)\n requires 0 <= a <= b, b < nums.len()\n ensures spec_min_prefix(nums, a) >= spec_min_prefix(nums, b)\n decreases b\n{\n if a == b {\n } else {\n lemma_min_prefix_mono(nums, a, b - 1);\n }\n}\n\npub struct Solution;\n\nimpl Solution {\n pub fn find132pattern(nums: Vec) -> (res: bool) \n requires \n 1 <= nums.len() <= 200_000, \n forall |i: int| 0 <= i < nums.len() ==> -1_000_000_000 <= #[trigger] nums[i] <= 1_000_000_000, \n ensures\n res == (exists |i: int, j: int, k: int| \n 0 <= i < j < k < nums.len() &&\n #[trigger] nums[i] < #[trigger] nums[k] < #[trigger] nums[j]), \n {\n if nums.len() < 3 {\n proof {\n assert forall |i: int, j: int, k: int| \n 0 <= i < j < k < nums.len() implies \n !(#[trigger] nums[i] < #[trigger] nums[k] < #[trigger] nums[j]) by {};\n }\n return false;\n }\n\n let mut min_left: Vec = Vec::new();\n min_left.push(nums[0]);\n \n let mut m: usize = 1;\n while m < nums.len()\n invariant\n 1 <= nums.len() <= 200_000, \n forall |idx: int| 0 <= idx < nums.len() ==> -1_000_000_000 <= #[trigger] nums[idx] <= 1_000_000_000,\n 1 <= m <= nums.len(),\n min_left.len() == m,\n forall |j: int| 0 <= j < m ==> #[trigger] min_left[j] == spec_min_prefix(nums@, j),\n decreases nums.len() - m\n {\n let prev = min_left[m - 1];\n let curr = nums[m];\n if curr < prev {\n min_left.push(curr);\n } else {\n min_left.push(prev);\n }\n m += 1;\n }\n\n let mut stack: Vec = Vec::new();\n let ghost mut ghost_idxs: Seq = Seq::empty();\n\n let mut j: usize = nums.len() - 1;\n\n while j > 0\n invariant\n 3 <= nums.len() <= 200_000, \n forall |idx: int| 0 <= idx < nums.len() ==> -1_000_000_000 <= #[trigger] nums[idx] <= 1_000_000_000,\n min_left.len() == nums.len(),\n forall |jj: int| 0 <= jj < nums.len() ==> #[trigger] min_left[jj] == spec_min_prefix(nums@, jj),\n 0 <= j < nums.len(),\n stack.len() == ghost_idxs.len(),\n forall |id: int| 0 <= id < stack.len() ==> j < #[trigger] ghost_idxs[id] < nums.len(),\n forall |id: int| 0 <= id < stack.len() ==> #[trigger] stack[id] == nums[ghost_idxs[id]],\n forall |id: int| 0 <= id < stack.len() ==> #[trigger] stack[id] > min_left[j as int],\n forall |a: int, b: int| 0 <= a < b < stack.len() ==> #[trigger] stack[a] >= #[trigger] stack[b],\n forall |ii: int, jj: int, kk: int| \n 0 <= ii < jj < kk < nums.len() && jj > j ==>\n !(#[trigger] nums[ii] < #[trigger] nums[kk] < #[trigger] nums[jj]),\n forall |k: int| j < k < nums.len() && #[trigger] nums[k] > min_left[j as int] ==> \n (exists |id: int| 0 <= id < stack.len() && #[trigger] ghost_idxs[id] == k),\n decreases j\n {\n let current = nums[j];\n let l_min = min_left[j - 1];\n \n proof {\n lemma_min_prefix_mono(nums@, (j - 1) as int, j as int);\n }\n\n let ghost old_stack_view = stack@;\n let ghost old_ghost_idxs = ghost_idxs;\n \n \n while stack.len() > 0 && *stack.last().unwrap() <= l_min\n invariant\n 3 <= nums.len() <= 200_000,\n forall |idx: int| 0 <= idx < nums.len() ==> -1_000_000_000 <= #[trigger] nums[idx] <= 1_000_000_000,\n 0 < j < nums.len(),\n min_left.len() == nums.len(),\n l_min == min_left[(j - 1) as int],\n current == nums[j as int],\n stack.len() == ghost_idxs.len(),\n forall |id: int| 0 <= id < stack.len() ==> j < #[trigger] ghost_idxs[id] < nums.len(),\n forall |id: int| 0 <= id < stack.len() ==> #[trigger] stack[id] == nums[ghost_idxs[id]],\n forall |a: int, b: int| 0 <= a < b < stack.len() ==> #[trigger] stack[a] >= #[trigger] stack[b],\n stack.len() <= old_stack_view.len(),\n forall |id: int| 0 <= id < stack.len() ==> #[trigger] stack[id] == old_stack_view[id],\n forall |id: int| 0 <= id < stack.len() ==> #[trigger] ghost_idxs[id] == old_ghost_idxs[id],\n forall |old_id: int| 0 <= old_id < old_stack_view.len() && #[trigger] old_stack_view[old_id] > l_min ==> old_id < stack.len(),\n decreases stack.len()\n {\n stack.pop();\n proof { ghost_idxs = ghost_idxs.drop_last(); }\n }\n\n proof {\n assert forall |id: int| 0 <= id < stack.len() implies #[trigger] stack[id] > l_min by {\n if stack.len() > 0 {\n assert(stack[id] >= stack[stack.len() - 1]);\n }\n };\n }\n\n if current > l_min {\n if stack.len() > 0 && *stack.last().unwrap() < current {\n \n proof {\n let top = (stack.len() - 1) as int;\n let k_idx = ghost_idxs[top];\n let min_i = lemma_min_prefix_witness(nums@, (j - 1) as int);\n assert(nums[min_i] == l_min);\n assert(l_min < stack[top]);\n assert(stack[top] == nums[k_idx]);\n assert(nums[k_idx] < current);\n assert(current == nums[j as int]);\n }\n return true;\n }\n \n stack.push(current);\n proof {\n ghost_idxs = ghost_idxs.push(j as int);\n }\n }\n\n proof {\n assert forall |id: int| 0 <= id < stack.len() implies #[trigger] stack[id] > l_min by {\n if current > l_min && id == stack.len() - 1 {\n } \n };\n\n assert forall |id: int| 0 <= id < stack.len() implies j - 1 < #[trigger] ghost_idxs[id] < nums.len() by {\n if current > l_min && id == stack.len() - 1 {\n assert(ghost_idxs[id] == j as int);\n }\n };\n \n assert forall |id: int| 0 <= id < stack.len() implies #[trigger] stack[id] == nums[ghost_idxs[id]] by {\n if current > l_min && id == stack.len() - 1 {\n assert(ghost_idxs[id] == j as int);\n }\n };\n \n assert forall |a: int, b: int| 0 <= a < b < stack.len() implies #[trigger] stack[a] >= #[trigger] stack[b] by {\n if current > l_min && b == stack.len() - 1 && a < stack.len() - 1 {\n let last_before = (stack.len() - 2) as int;\n assert(stack[a] >= stack[last_before]);\n assert(stack[last_before] >= current);\n }\n };\n\n assert forall |ii: int, kk: int| \n 0 <= ii < j && j < kk < nums.len() implies \n !(#[trigger] nums[ii] < #[trigger] nums[kk] && nums[kk] < nums[j as int]) by {\n if nums[ii] < nums[kk] && nums[kk] < nums[j as int] {\n lemma_min_prefix_le(nums@, (j-1) as int, ii);\n assert(nums[ii] >= l_min);\n assert(nums[kk] > l_min);\n \n if current <= l_min {\n assert(nums[j as int] <= l_min);\n } else {\n lemma_min_prefix_mono(nums@, (j-1) as int, j as int);\n assert(nums[kk] > min_left[j as int]);\n let old_id = choose |id: int| 0 <= id < old_stack_view.len() && old_ghost_idxs[id] == kk;\n assert(old_stack_view[old_id] == nums[kk]);\n assert(old_stack_view[old_id] > l_min);\n assert(old_id < stack.len() - 1);\n assert(stack[old_id] == nums[kk]);\n assert(stack[old_id] < current);\n if stack.len() >= 2 {\n let lb = (stack.len() - 2) as int;\n assert(stack[lb] >= current);\n assert(stack[lb] <= stack[old_id]);\n }\n }\n }\n };\n \n assert forall |k: int| j - 1 < k < nums.len() && #[trigger] nums[k] > l_min implies \n (exists |id: int| 0 <= id < stack.len() && #[trigger] ghost_idxs[id] == k) by {\n if k == j as int {\n if current > l_min {\n let new_id = (stack.len() - 1) as int;\n assert(ghost_idxs[new_id] == k);\n }\n } else {\n lemma_min_prefix_mono(nums@, (j-1) as int, j as int);\n assert(nums[k] > min_left[j as int]);\n let old_id = choose |id: int| 0 <= id < old_stack_view.len() && old_ghost_idxs[id] == k;\n assert(old_stack_view[old_id] == nums[k]);\n assert(old_stack_view[old_id] > l_min);\n if current > l_min {\n assert(old_id < stack.len() - 1);\n } else {\n assert(old_id < stack.len());\n }\n assert(ghost_idxs[old_id] == k);\n }\n };\n }\n\n j -= 1;\n }\n \n proof {\n assert forall |i: int, j: int, k: int| \n 0 <= i < j < k < nums.len() implies \n !(#[trigger] nums[i] < #[trigger] nums[k] < #[trigger] nums[j]) by {\n };\n }\n false\n }\n}\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc456/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc456/tests/mutated_testcases.jsonl"} {"problem": "lc458", "family": "leetcode", "description": "# Poor Pigs\n\nThere are `buckets` buckets of liquid, where **exactly one** of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have `minutesToTest` minutes to determine which bucket is poisonous.\n\nYou can feed the pigs according to these steps:\n\n- Choose some live pigs to feed.\n- For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.\n- Wait for `minutesToDie` minutes. You may **not** feed any other pigs during this time.\n- After `minutesToDie` minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.\n- Repeat this process until you run out of time.\n\nGiven `buckets`, `minutesToDie`, and `minutesToTest`, return *the **minimum** number of pigs needed to figure out which bucket is poisonous within the allotted time*.\n\n## Example 1:\n\n> **Input:** buckets = 4, minutesToDie = 15, minutesToTest = 15\n> **Output:** 2\n> **Explanation:** We can determine the poisonous bucket as follows:\n> At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3.\n> At time 15, there are 4 possible outcomes:\n> - If only the first pig dies, then bucket 1 must be poisonous.\n> - If only the second pig dies, then bucket 3 must be poisonous.\n> - If both pigs die, then bucket 2 must be poisonous.\n> - If neither pig dies, then bucket 4 must be poisonous.\n\n## Example 2:\n\n> **Input:** buckets = 4, minutesToDie = 15, minutesToTest = 30\n> **Output:** 2\n> **Explanation:** We can determine the poisonous bucket as follows:\n> At time 0, feed the first pig bucket 1, and feed the second pig bucket 2.\n> At time 15, there are 2 possible outcomes:\n> - If either pig dies, then the poisonous bucket is the one it was fed.\n> - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4.\n> At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed.\n\n## Constraints:\n\n- `1 <= buckets <= 1000`\n- `1 <= minutesToDie <= minutesToTest <= 100`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nMath, Dynamic Programming, Combinatorics\nAcceptance rate: 59.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_pow(base: int, exp: nat) -> int\n decreases exp,\n {\n if exp == 0 {\n 1\n } else {\n base * Self::spec_pow(base, (exp - 1) as nat)\n }\n }\n\n pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> (result: i32)\n requires\n 1 <= buckets <= 1000,\n 1 <= minutes_to_die <= minutes_to_test <= 100,\n 1 <= minutes_to_test as int / minutes_to_die as int,\n ensures\n 0 <= result,\n Self::spec_pow((minutes_to_test as int / minutes_to_die as int) + 1, result as nat) >= buckets as int,\n forall |r: int| 0 <= r < result as int ==> (#[trigger] Self::spec_pow((minutes_to_test as int / minutes_to_die as int) + 1, r as nat)) < buckets as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> i32 {\n let states = minutes_to_test / minutes_to_die + 1;\n let mut pigs: i32 = 0;\n let mut capacity: i32 = 1;\n while capacity < buckets {\n let old_capacity = capacity;\n let prod: i64 = old_capacity as i64 * states as i64;\n capacity = prod as i32;\n pigs += 1;\n }\n pigs\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_pow(base: int, exp: nat) -> int\n decreases exp,\n {\n if exp == 0 {\n 1\n } else {\n base * Self::spec_pow(base, (exp - 1) as nat)\n }\n }\n\n pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> (result: i32)\n requires\n 1 <= buckets <= 1000,\n 1 <= minutes_to_die <= minutes_to_test <= 100,\n 1 <= minutes_to_test as int / minutes_to_die as int,\n ensures\n 0 <= result,\n Self::spec_pow((minutes_to_test as int / minutes_to_die as int) + 1, result as nat) >= buckets as int,\n forall |r: int| 0 <= r < result as int ==> (#[trigger] Self::spec_pow((minutes_to_test as int / minutes_to_die as int) + 1, r as nat)) < buckets as int,\n {\n let states = minutes_to_test / minutes_to_die + 1;\n let mut pigs: i32 = 0;\n let mut capacity: i32 = 1;\n while capacity < buckets {\n let old_capacity = capacity;\n let prod: i64 = old_capacity as i64 * states as i64;\n capacity = prod as i32;\n pigs += 1;\n }\n pigs\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_pow(base: int, exp: nat) -> int\n decreases exp,\n {\n if exp == 0 {\n 1\n } else {\n base * Self::spec_pow(base, (exp - 1) as nat)\n }\n }\n\n pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> (result: i32)\n requires\n 1 <= buckets <= 1000,\n 1 <= minutes_to_die <= minutes_to_test <= 100,\n 1 <= minutes_to_test as int / minutes_to_die as int,\n ensures\n 0 <= result,\n Self::spec_pow((minutes_to_test as int / minutes_to_die as int) + 1, result as nat) >= buckets as int,\n forall |r: int| 0 <= r < result as int ==> (#[trigger] Self::spec_pow((minutes_to_test as int / minutes_to_die as int) + 1, r as nat)) < buckets as int,\n {\n let states = minutes_to_test / minutes_to_die + 1;\n let mut pigs: i32 = 0;\n let mut capacity: i32 = 1;\n\n while capacity < buckets\n invariant\n 1 <= buckets <= 1000,\n 1 <= minutes_to_die <= minutes_to_test <= 100,\n 1 <= minutes_to_test as int / minutes_to_die as int,\n states == minutes_to_test / minutes_to_die + 1,\n 2 <= states <= 101,\n 0 <= pigs,\n capacity >= 1,\n pigs as int <= capacity as int,\n capacity as int == Self::spec_pow(states as int, pigs as nat),\n forall |r: int| 0 <= r < pigs as int ==> (#[trigger] Self::spec_pow(states as int, r as nat)) < buckets as int,\n decreases buckets - capacity + i32::MAX,\n {\n let old_capacity = capacity;\n let ghost old_pigs = pigs;\n proof {\n assert(old_capacity as int == Self::spec_pow(states as int, old_pigs as nat));\n assert(Self::spec_pow(states as int, pigs as nat) < buckets as int);\n assert(old_capacity <= 999) by (nonlinear_arith)\n requires\n old_capacity < buckets,\n buckets <= 1000,\n {\n }\n assert(0 <= old_capacity as int * states as int <= 100899) by (nonlinear_arith)\n requires\n 0 <= old_capacity <= 999,\n 0 <= states <= 101,\n {\n }\n }\n let prod: i64 = old_capacity as i64 * states as i64;\n assert(prod as int == old_capacity as int * states as int);\n assert(0 <= prod <= 100899);\n capacity = prod as i32;\n assert(capacity as int == prod as int);\n pigs += 1;\n proof {\n assert(pigs == old_pigs + 1);\n assert(Self::spec_pow(states as int, pigs as nat)\n == states as int * Self::spec_pow(states as int, old_pigs as nat));\n assert(capacity as int == old_capacity as int * states as int);\n assert(capacity as int == Self::spec_pow(states as int, pigs as nat));\n assert(Self::spec_pow(states as int, old_pigs as nat) < buckets as int);\n assert forall |r: int| 0 <= r < pigs as int implies (#[trigger] Self::spec_pow(states as int, r as nat)) < buckets as int by {\n if r < old_pigs as int {\n } else {\n assert(r == old_pigs as int);\n }\n }\n assert(old_pigs as int + 1 <= old_capacity as int + 1);\n assert(old_capacity as int + 1 <= old_capacity as int * states as int) by (nonlinear_arith)\n requires\n old_capacity >= 1,\n states >= 2,\n {\n }\n assert(pigs as int <= capacity as int);\n }\n }\n\n proof {\n assert(!(capacity < buckets));\n assert(capacity as int >= buckets as int);\n assert(Self::spec_pow(states as int, pigs as nat) >= buckets as int);\n }\n\n pigs\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc458/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc458/tests/mutated_testcases.jsonl"} {"problem": "lc477", "family": "leetcode", "description": "# Total Hamming Distance\n\nThe Hamming distance between two integers is the number of positions at which the corresponding bits are different.\n\nGiven an integer array `nums`, return the sum of Hamming distances between all the pairs of the integers in `nums`.\n\n\n## Example 1:\n\n> **Input:** nums = [4,14,2]\n> **Output:** 6\\\n> **Explanation**:\n> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case).\n> The answer will be:\n> HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.\n\n## Example 2:\n\n> **Input:** nums = [4,14,4]\n> **Output:** 4\n\n## Constraints:\n\n- $1 <= nums.length <= 10^{4}$\n- $0 <= nums[i] <= 10^{9}$\n- The answer for the given input will fit in a 32-bit integer.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn total_hamming_distance(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nBit Manipulation\nAcceptance Rate: 54.6%", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn hamming_distance_spec_helper(x: nat, acc: nat) -> nat\n decreases x,\n {\n if x == 0 {\n acc\n } else {\n let ones = x % 2;\n let new_acc = acc + ones;\n Solution::hamming_distance_spec_helper(x / 2, new_acc)\n }\n }\n\n pub open spec fn hamming_distance_spec(xor_result: nat) -> nat {\n Solution::hamming_distance_spec_helper(xor_result, 0)\n }\n\n pub open spec fn total_hamming_distance_spec(nums: Seq, i: nat, j: nat, acc: nat) -> nat\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n acc\n } else if j >= nums.len() {\n Solution::total_hamming_distance_spec(nums, i + 1, i + 2, acc)\n } else {\n let xor_val = (nums[i as int] ^ nums[j as int]) as nat;\n let dist = Solution::hamming_distance_spec(xor_val);\n Solution::total_hamming_distance_spec(nums, i, j + 1, acc + dist)\n }\n }\n\n pub fn total_hamming_distance(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 10000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n i32::MIN <= Solution::total_hamming_distance_spec(nums@, 0, 1, 0) <= i32::MAX,\n ensures\n res == Solution::total_hamming_distance_spec(nums@, 0, 1, 0),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn total_hamming_distance(nums: Vec) -> i32 {\n let n = nums.len();\n let mut total: i64 = 0;\n let mut pw: i64 = 1;\n let mut b: usize = 0;\n while b < 30 {\n let mut ones: i64 = 0;\n let mut idx: usize = 0;\n while idx < n {\n if (nums[idx] as i64 / pw) % 2 == 1 {\n ones += 1;\n }\n idx += 1;\n }\n total += ones * ((n as i64) - ones);\n pw *= 2;\n b += 1;\n }\n total as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn hamming_distance_spec_helper(x: nat, acc: nat) -> nat\n decreases x,\n {\n if x == 0 {\n acc\n } else {\n let ones = x % 2;\n let new_acc = acc + ones;\n Solution::hamming_distance_spec_helper(x / 2, new_acc)\n }\n }\n\n pub open spec fn hamming_distance_spec(xor_result: nat) -> nat {\n Solution::hamming_distance_spec_helper(xor_result, 0)\n }\n\n pub open spec fn total_hamming_distance_spec(nums: Seq, i: nat, j: nat, acc: nat) -> nat\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n acc\n } else if j >= nums.len() {\n Solution::total_hamming_distance_spec(nums, i + 1, i + 2, acc)\n } else {\n let xor_val = (nums[i as int] ^ nums[j as int]) as nat;\n let dist = Solution::hamming_distance_spec(xor_val);\n Solution::total_hamming_distance_spec(nums, i, j + 1, acc + dist)\n }\n }\n\n pub fn total_hamming_distance(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 10000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n i32::MIN <= Solution::total_hamming_distance_spec(nums@, 0, 1, 0) <= i32::MAX,\n ensures\n res == Solution::total_hamming_distance_spec(nums@, 0, 1, 0),\n {\n let n = nums.len();\n let mut total: i64 = 0;\n let mut pw: i64 = 1;\n let mut b: usize = 0;\n while b < 30 {\n let mut ones: i64 = 0;\n let mut idx: usize = 0;\n while idx < n {\n if (nums[idx] as i64 / pw) % 2 == 1 {\n ones += 1;\n }\n idx += 1;\n }\n total += ones * ((n as i64) - ones);\n pw *= 2;\n b += 1;\n }\n total as i32\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn hamming_distance_spec_helper(x: nat, acc: nat) -> nat\n decreases x,\n {\n if x == 0 {\n acc\n } else {\n let ones = x % 2;\n let new_acc = acc + ones;\n Solution::hamming_distance_spec_helper(x / 2, new_acc)\n }\n }\n\n pub open spec fn hamming_distance_spec(xor_result: nat) -> nat {\n Solution::hamming_distance_spec_helper(xor_result, 0)\n }\n\n proof fn hamming_distance_properties(x: nat, a: nat)\n ensures\n Solution::hamming_distance_spec_helper(x, a) == Solution::hamming_distance_spec_helper(\n x / 2, a + (x % 2)),\n Solution::hamming_distance_spec_helper(x, a) == a + Solution::hamming_distance_spec_helper(x, 0),\n decreases x,\n {\n if x == 0 {\n } else {\n Solution::hamming_distance_properties(x / 2, a + (x % 2));\n Solution::hamming_distance_properties(x / 2, x % 2);\n }\n }\n\n pub open spec fn pow2(e: nat) -> nat\n decreases e\n {\n if e == 0 { 1 } else { 2 * Solution::pow2((e - 1) as nat) }\n }\n\n proof fn lemma_pow2_pos(e: nat)\n ensures\n Solution::pow2(e) >= 1,\n decreases e,\n {\n if e == 0 {\n } else {\n Solution::lemma_pow2_pos((e - 1) as nat);\n }\n }\n\n pub open spec fn diff_bit_sum(a: nat, b: nat, num_bits: nat) -> nat\n decreases num_bits\n {\n if num_bits == 0 { 0 }\n else {\n let d: nat = if (a % 2) != (b % 2) { 1nat } else { 0nat };\n d + Solution::diff_bit_sum(a / 2, b / 2, (num_bits - 1) as nat)\n }\n }\n\n proof fn lemma_xor_nonneg_bound(ai: i32, bi: i32)\n requires\n 0 <= ai <= i32::MAX,\n 0 <= bi <= i32::MAX,\n ensures\n 0 <= (ai ^ bi) <= i32::MAX,\n {\n assert(0 <= (ai ^ bi) <= i32::MAX) by (bit_vector)\n requires\n 0 <= ai <= i32::MAX,\n 0 <= bi <= i32::MAX;\n }\n\n proof fn lemma_hamming_eq_diff_bit_sum(a: nat, b: nat, xor_val: nat, num_bits: nat)\n requires\n xor_val == ((a as i32) ^ (b as i32)) as nat,\n a < Solution::pow2(num_bits),\n b < Solution::pow2(num_bits),\n a <= i32::MAX as nat,\n b <= i32::MAX as nat,\n ensures\n Solution::hamming_distance_spec_helper(xor_val, 0) == Solution::diff_bit_sum(a, b, num_bits),\n decreases num_bits\n {\n let ai: i32 = a as i32;\n let bi: i32 = b as i32;\n Solution::lemma_xor_nonneg_bound(ai, bi);\n let xi: i32 = ai ^ bi;\n assert(xor_val == xi as nat);\n assert(xi == xor_val as i32);\n\n if num_bits == 0 {\n assert(Solution::pow2(0) == 1);\n assert(a == 0 && b == 0);\n assert(ai == 0i32 && bi == 0i32);\n assert(xi == 0i32) by (bit_vector)\n requires\n xi == ai ^ bi,\n ai == 0i32,\n bi == 0i32;\n assert(xor_val == 0);\n } else {\n let a2 = a / 2;\n let b2 = b / 2;\n let xor2 = xor_val / 2;\n assert(Solution::pow2(num_bits) == 2 * Solution::pow2((num_bits - 1) as nat));\n Solution::lemma_pow2_pos((num_bits - 1) as nat);\n assert(a2 < Solution::pow2((num_bits - 1) as nat)) by (nonlinear_arith)\n requires\n a < 2 * Solution::pow2((num_bits - 1) as nat),\n a2 == a / 2;\n assert(b2 < Solution::pow2((num_bits - 1) as nat)) by (nonlinear_arith)\n requires\n b < 2 * Solution::pow2((num_bits - 1) as nat),\n b2 == b / 2;\n\n let a2i: i32 = a2 as i32;\n let b2i: i32 = b2 as i32;\n assert(a2i == ai / 2) by (nonlinear_arith)\n requires\n a2i == a2 as i32,\n ai == a as i32,\n a2 == a / 2,\n 0 <= a as int <= i32::MAX as int;\n assert(b2i == bi / 2) by (nonlinear_arith)\n requires\n b2i == b2 as i32,\n bi == b as i32,\n b2 == b / 2,\n 0 <= b as int <= i32::MAX as int;\n let x2i: i32 = (xi / 2) as i32;\n assert(x2i == (a2i ^ b2i)) by (bit_vector)\n requires\n xi == ai ^ bi,\n 0 <= ai,\n 0 <= bi,\n a2i == ai / 2,\n b2i == bi / 2,\n x2i == xi / 2;\n Solution::lemma_xor_nonneg_bound(a2i, b2i);\n assert(xor2 == x2i as nat) by (nonlinear_arith)\n requires\n xor2 == xor_val / 2,\n xor_val == xi as nat,\n x2i == xi / 2,\n 0 <= xi as int <= i32::MAX as int;\n assert(x2i == a2i ^ b2i);\n assert(xor2 == ((a2 as i32) ^ (b2 as i32)) as nat);\n\n assert((xi % 2 == 1i32) == ((ai % 2 == 1i32) != (bi % 2 == 1i32))) by (bit_vector)\n requires\n xi == ai ^ bi,\n 0 <= ai,\n 0 <= bi;\n assert(xor_val % 2 == (xi % 2) as nat) by (nonlinear_arith)\n requires\n xor_val == xi as nat,\n 0 <= xi;\n assert(a % 2 == (ai % 2) as nat) by (nonlinear_arith)\n requires\n a == ai as nat,\n 0 <= ai;\n assert(b % 2 == (bi % 2) as nat) by (nonlinear_arith)\n requires\n b == bi as nat,\n 0 <= bi;\n assert((xor_val % 2) == (if (a % 2) != (b % 2) { 1nat } else { 0nat }));\n Solution::hamming_distance_properties(xor_val, 0);\n Solution::hamming_distance_properties(xor2, xor_val % 2);\n Solution::lemma_hamming_eq_diff_bit_sum(a2, b2, xor2, (num_bits - 1) as nat);\n }\n }\n\n pub open spec fn total_hamming_distance_spec(nums: Seq, i: nat, j: nat, acc: nat) -> nat\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n acc\n } else if j >= nums.len() {\n Solution::total_hamming_distance_spec(nums, i + 1, i + 2, acc)\n } else {\n let xor_val = (nums[i as int] ^ nums[j as int]) as nat;\n let dist = Solution::hamming_distance_spec(xor_val);\n Solution::total_hamming_distance_spec(nums, i, j + 1, acc + dist)\n }\n }\n\n pub open spec fn total_diff_bit_sum(nums: Seq, i: nat, j: nat, num_bits: nat) -> nat\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n 0\n } else if j >= nums.len() {\n Solution::total_diff_bit_sum(nums, i + 1, i + 2, num_bits)\n } else {\n Solution::diff_bit_sum(nums[i as int] as nat, nums[j as int] as nat, num_bits)\n + Solution::total_diff_bit_sum(nums, i, j + 1, num_bits)\n }\n }\n\n proof fn total_hamming_distance_spec_add_acc(nums: Seq, i: nat, j: nat, acc: nat)\n ensures\n Solution::total_hamming_distance_spec(nums, i, j, acc)\n == Solution::total_hamming_distance_spec(nums, i, j, 0) + acc,\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n } else if j >= nums.len() {\n Solution::total_hamming_distance_spec_add_acc(nums, i + 1, i + 2, acc);\n } else {\n let dist = Solution::hamming_distance_spec((nums[i as int] ^ nums[j as int]) as nat);\n Solution::total_hamming_distance_spec_add_acc(nums, i, j + 1, acc + dist);\n Solution::total_hamming_distance_spec_add_acc(nums, i, j + 1, dist);\n }\n }\n\n proof fn lemma_total_eq_total_diff(nums: Seq, i: nat, j: nat, num_bits: nat)\n requires\n forall|k: int| 0 <= k < nums.len() ==> 0 <= #[trigger] nums[k] < Solution::pow2(num_bits) as int,\n forall|k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] <= i32::MAX,\n ensures\n Solution::total_hamming_distance_spec(nums, i, j, 0) == Solution::total_diff_bit_sum(nums, i, j, num_bits),\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n } else if j >= nums.len() {\n Solution::lemma_total_eq_total_diff(nums, i + 1, i + 2, num_bits);\n } else {\n Solution::lemma_total_eq_total_diff(nums, i, j + 1, num_bits);\n let a = nums[i as int] as nat;\n let b = nums[j as int] as nat;\n let xor_val = (nums[i as int] ^ nums[j as int]) as nat;\n Solution::lemma_hamming_eq_diff_bit_sum(a, b, xor_val, num_bits);\n let dist = Solution::hamming_distance_spec(xor_val);\n Solution::total_hamming_distance_spec_add_acc(nums, i, j + 1, dist);\n }\n }\n\n pub open spec fn bit_of(x: nat, b: nat) -> nat {\n (x / Solution::pow2(b)) % 2\n }\n\n proof fn lemma_div_compose(a: nat, p: nat)\n requires\n p >= 1,\n ensures\n a / (2 * p) == (a / 2) / p,\n {\n assert(a / (2 * p) == (a / 2) / p) by (nonlinear_arith)\n requires\n p >= 1;\n }\n\n proof fn lemma_bit_of_step(x: nat, b: nat)\n ensures\n Solution::bit_of(x, b + 1) == Solution::bit_of(x / 2, b),\n {\n Solution::lemma_pow2_pos(b);\n assert(Solution::pow2(b + 1) == 2 * Solution::pow2(b));\n Solution::lemma_div_compose(x, Solution::pow2(b));\n assert(x / Solution::pow2(b + 1) == (x / 2) / Solution::pow2(b));\n }\n\n proof fn lemma_diff_bit_sum_step(a: nat, b: nat, k: nat)\n requires\n k >= 1,\n ensures\n Solution::diff_bit_sum(a, b, k) == Solution::diff_bit_sum(a, b, (k - 1) as nat)\n + (if Solution::bit_of(a, (k - 1) as nat) != Solution::bit_of(b, (k - 1) as nat) { 1nat } else { 0nat }),\n decreases k,\n {\n let m: nat = (k - 1) as nat;\n if m == 0 {\n assert(Solution::pow2(0) == 1);\n assert(Solution::bit_of(a, 0) == a % 2);\n assert(Solution::bit_of(b, 0) == b % 2);\n } else {\n Solution::lemma_diff_bit_sum_step(a / 2, b / 2, m);\n Solution::lemma_bit_of_step(a, (m - 1) as nat);\n Solution::lemma_bit_of_step(b, (m - 1) as nat);\n let d0: nat = if (a % 2) != (b % 2) { 1nat } else { 0nat };\n assert(Solution::diff_bit_sum(a, b, k) == d0 + Solution::diff_bit_sum(a / 2, b / 2, m));\n assert(Solution::diff_bit_sum(a, b, m) == d0 + Solution::diff_bit_sum(a / 2, b / 2, (m - 1) as nat));\n assert(Solution::diff_bit_sum(a / 2, b / 2, m) == Solution::diff_bit_sum(a / 2, b / 2, (m - 1) as nat)\n + (if Solution::bit_of(a / 2, (m - 1) as nat) != Solution::bit_of(b / 2, (m - 1) as nat) { 1nat } else { 0nat }));\n assert(Solution::bit_of(a, m) == Solution::bit_of(a / 2, (m - 1) as nat));\n assert(Solution::bit_of(b, m) == Solution::bit_of(b / 2, (m - 1) as nat));\n }\n }\n\n pub open spec fn count_bit_set_upto(nums: Seq, b: nat, upto: nat) -> nat\n decreases upto\n {\n if upto == 0 { 0 }\n else {\n let prev = Solution::count_bit_set_upto(nums, b, (upto - 1) as nat);\n if Solution::bit_of(nums[upto as int - 1] as nat, b) == 1 { prev + 1 } else { prev }\n }\n }\n\n pub open spec fn pairs_differ_at_bit(nums: Seq, i: nat, j: nat, b: nat) -> nat\n decreases nums.len() - i, nums.len() - j\n {\n if i >= nums.len() {\n 0\n } else if j >= nums.len() {\n Solution::pairs_differ_at_bit(nums, i + 1, i + 2, b)\n } else {\n let d = if Solution::bit_of(nums[i as int] as nat, b) != Solution::bit_of(nums[j as int] as nat, b) { 1nat } else { 0nat };\n d + Solution::pairs_differ_at_bit(nums, i, j + 1, b)\n }\n }\n\n proof fn lemma_total_diff_bit_sum_step(nums: Seq, i: nat, j: nat, k: nat)\n requires\n k >= 1,\n ensures\n Solution::total_diff_bit_sum(nums, i, j, k)\n == Solution::total_diff_bit_sum(nums, i, j, (k - 1) as nat)\n + Solution::pairs_differ_at_bit(nums, i, j, (k - 1) as nat),\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n } else if j >= nums.len() {\n Solution::lemma_total_diff_bit_sum_step(nums, i + 1, i + 2, k);\n } else {\n Solution::lemma_total_diff_bit_sum_step(nums, i, j + 1, k);\n Solution::lemma_diff_bit_sum_step(nums[i as int] as nat, nums[j as int] as nat, k);\n let m: nat = (k - 1) as nat;\n let d = if Solution::bit_of(nums[i as int] as nat, m) != Solution::bit_of(nums[j as int] as nat, m) { 1nat } else { 0nat };\n assert(Solution::diff_bit_sum(nums[i as int] as nat, nums[j as int] as nat, k)\n == Solution::diff_bit_sum(nums[i as int] as nat, nums[j as int] as nat, m) + d);\n assert(Solution::total_diff_bit_sum(nums, i, j, k)\n == Solution::diff_bit_sum(nums[i as int] as nat, nums[j as int] as nat, k)\n + Solution::total_diff_bit_sum(nums, i, j + 1, k));\n assert(Solution::total_diff_bit_sum(nums, i, j, m)\n == Solution::diff_bit_sum(nums[i as int] as nat, nums[j as int] as nat, m)\n + Solution::total_diff_bit_sum(nums, i, j + 1, m));\n assert(Solution::pairs_differ_at_bit(nums, i, j, m)\n == d + Solution::pairs_differ_at_bit(nums, i, j + 1, m));\n }\n }\n\n pub open spec fn count_diff_from(nums: Seq, i: nat, j: nat, b: nat) -> nat\n decreases nums.len() - j\n {\n if j >= nums.len() {\n 0\n } else {\n let d = if Solution::bit_of(nums[j as int] as nat, b) != Solution::bit_of(nums[i as int] as nat, b) { 1nat } else { 0nat };\n d + Solution::count_diff_from(nums, i, j + 1, b)\n }\n }\n\n proof fn lemma_pairs_differ_row(nums: Seq, i: nat, j: nat, b: nat)\n requires\n i < nums.len(),\n i < j,\n j <= nums.len(),\n ensures\n Solution::pairs_differ_at_bit(nums, i, j, b)\n == Solution::count_diff_from(nums, i, j, b) + Solution::pairs_differ_at_bit(nums, i + 1, i + 2, b),\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n } else {\n Solution::lemma_pairs_differ_row(nums, i, j + 1, b);\n }\n }\n\n pub open spec fn count_diff_from_countable(nums: Seq, b: nat, a: nat, c: nat) -> nat\n decreases c - a when a <= c\n {\n if a >= c {\n 0\n } else {\n (if Solution::bit_of(nums[a as int] as nat, b) == 1 { 1nat } else { 0nat })\n + Solution::count_diff_from_countable(nums, b, (a + 1) as nat, c)\n }\n }\n\n proof fn lemma_count_bit_set_upto_step(nums: Seq, b: nat, upto: nat)\n requires\n upto < nums.len(),\n ensures\n Solution::count_bit_set_upto(nums, b, upto + 1) == Solution::count_bit_set_upto(nums, b, upto)\n + (if Solution::bit_of(nums[upto as int] as nat, b) == 1 { 1nat } else { 0nat }),\n {\n }\n\n proof fn lemma_count_bit_set_additive(nums: Seq, b: nat, a: nat, c: nat)\n requires\n a <= c <= nums.len(),\n ensures\n Solution::count_bit_set_upto(nums, b, c)\n == Solution::count_bit_set_upto(nums, b, a) + Solution::count_diff_from_countable(nums, b, a, c),\n decreases c - a,\n {\n if a >= c {\n } else {\n Solution::lemma_count_bit_set_additive(nums, b, (a + 1) as nat, c);\n Solution::lemma_count_bit_set_upto_step(nums, b, a);\n }\n }\n\n proof fn lemma_count_diff_from_formula(nums: Seq, i: nat, j: nat, b: nat)\n requires\n i < nums.len(),\n j <= nums.len(),\n ensures\n Solution::bit_of(nums[i as int] as nat, b) == 1 ==>\n Solution::count_diff_from(nums, i, j, b)\n == (nums.len() - j) - Solution::count_diff_from_countable(nums, b, j, nums.len()),\n Solution::bit_of(nums[i as int] as nat, b) == 0 ==>\n Solution::count_diff_from(nums, i, j, b)\n == Solution::count_diff_from_countable(nums, b, j, nums.len()),\n decreases nums.len() - j,\n {\n if j >= nums.len() {\n } else {\n Solution::lemma_count_diff_from_formula(nums, i, j + 1, b);\n let nj = nums[j as int] as nat;\n let d: nat = if Solution::bit_of(nj, b) != Solution::bit_of(nums[i as int] as nat, b) { 1nat } else { 0nat };\n assert(Solution::count_diff_from(nums, i, j, b) == d + Solution::count_diff_from(nums, i, j + 1, b));\n assert(Solution::count_diff_from_countable(nums, b, j, nums.len())\n == (if Solution::bit_of(nj, b) == 1 { 1nat } else { 0nat })\n + Solution::count_diff_from_countable(nums, b, (j + 1) as nat, nums.len()));\n }\n }\n\n proof fn lemma_count_diff_from_countable_bound(nums: Seq, b: nat, a: nat, c: nat)\n requires\n a <= c,\n ensures\n Solution::count_diff_from_countable(nums, b, a, c) <= c - a,\n decreases c - a,\n {\n if a >= c {\n } else {\n Solution::lemma_count_diff_from_countable_bound(nums, b, (a + 1) as nat, c);\n }\n }\n\n proof fn lemma_pairs_differ_combinatorial(nums: Seq, i: nat, b: nat)\n requires\n i <= nums.len(),\n forall|k: int| 0 <= k < nums.len() ==> 0 <= #[trigger] nums[k],\n ensures\n Solution::pairs_differ_at_bit(nums, i, (i + 1) as nat, b)\n == Solution::count_diff_from_countable(nums, b, i, nums.len())\n * ((nums.len() - i) - Solution::count_diff_from_countable(nums, b, i, nums.len())),\n decreases nums.len() - i,\n {\n if i >= nums.len() {\n assert(Solution::pairs_differ_at_bit(nums, i, (i + 1) as nat, b) == 0);\n assert(Solution::count_diff_from_countable(nums, b, i, nums.len()) == 0);\n } else {\n let i1: nat = (i + 1) as nat;\n let i2: nat = (i + 2) as nat;\n Solution::lemma_pairs_differ_combinatorial(nums, i1, b);\n Solution::lemma_pairs_differ_row(nums, i, i1, b);\n Solution::lemma_count_diff_from_formula(nums, i, i1, b);\n let ones_i1: nat = Solution::count_diff_from_countable(nums, b, i1, nums.len());\n let ones_i: nat = Solution::count_diff_from_countable(nums, b, i, nums.len());\n Solution::lemma_count_diff_from_countable_bound(nums, b, i1, nums.len());\n Solution::lemma_count_diff_from_countable_bound(nums, b, i, nums.len());\n assert(nums[i as int] >= 0);\n let bit_i: nat = Solution::bit_of(nums[i as int] as nat, b);\n let n: int = nums.len() as int;\n let zeros_i1: int = (n - i - 1) - ones_i1 as int;\n let zeros_i: int = (n - i) - ones_i as int;\n let cd: nat = Solution::count_diff_from(nums, i, i1, b);\n let pdi: nat = Solution::pairs_differ_at_bit(nums, i, i1, b);\n let pdi1: nat = Solution::pairs_differ_at_bit(nums, i1, i2, b);\n assert(pdi == cd + pdi1);\n assert(Solution::pairs_differ_at_bit(nums, i1, (i1 + 1) as nat, b)\n == Solution::count_diff_from_countable(nums, b, i1, nums.len())\n * ((nums.len() - i1) - Solution::count_diff_from_countable(nums, b, i1, nums.len())));\n assert(i1 + 1 == i2);\n assert(zeros_i1 as nat == (nums.len() - i1) - ones_i1) by (nonlinear_arith)\n requires\n zeros_i1 == (n - i - 1) - ones_i1 as int,\n n == nums.len() as int,\n i1 == i + 1,\n i < nums.len(),\n ones_i1 <= nums.len() - i1;\n assert(pdi1 == ones_i1 * (zeros_i1 as nat));\n assert(bit_i == 1 ==> cd == (zeros_i1 as nat));\n assert(bit_i == 0 ==> cd == ones_i1);\n assert(bit_i == 1 ==> ones_i == 1 + ones_i1);\n assert(bit_i == 0 ==> ones_i == ones_i1);\n assert(bit_i == 0 || bit_i == 1);\n assert(pdi as int == ones_i as int * zeros_i) by (nonlinear_arith)\n requires\n pdi as int == cd as int + pdi1 as int,\n pdi1 as int == ones_i1 as int * zeros_i1,\n bit_i == 1 ==> cd as int == zeros_i1,\n bit_i == 0 ==> cd as int == ones_i1 as int,\n bit_i == 1 ==> ones_i as int == 1 + ones_i1 as int,\n bit_i == 0 ==> ones_i as int == ones_i1 as int,\n zeros_i1 == (n - i - 1) - ones_i1 as int,\n zeros_i == (n - i) - ones_i as int,\n bit_i == 0 || bit_i == 1;\n assert(zeros_i >= 0) by (nonlinear_arith)\n requires\n zeros_i == (n - i) - ones_i as int,\n n == nums.len() as int,\n ones_i <= nums.len() - i;\n assert(zeros_i as nat == (nums.len() - i) - ones_i) by (nonlinear_arith)\n requires\n zeros_i == (n - i) - ones_i as int,\n n == nums.len() as int,\n ones_i <= nums.len() - i,\n zeros_i >= 0;\n assert(pdi == ones_i * (zeros_i as nat)) by (nonlinear_arith)\n requires\n pdi as int == ones_i as int * zeros_i,\n zeros_i >= 0;\n assert(pdi == ones_i * ((nums.len() - i) - ones_i));\n }\n }\n\n proof fn lemma_total_diff_bit_sum_zero(nums: Seq, i: nat, j: nat)\n ensures\n Solution::total_diff_bit_sum(nums, i, j, 0) == 0,\n decreases nums.len() - i, nums.len() - j,\n {\n if i >= nums.len() {\n } else if j >= nums.len() {\n Solution::lemma_total_diff_bit_sum_zero(nums, i + 1, i + 2);\n } else {\n Solution::lemma_total_diff_bit_sum_zero(nums, i, j + 1);\n assert(Solution::diff_bit_sum(nums[i as int] as nat, nums[j as int] as nat, 0) == 0);\n }\n }\n\n pub open spec fn bit_formula_sum(nums: Seq, num_bits: nat) -> nat\n decreases num_bits\n {\n if num_bits == 0 {\n 0\n } else {\n let ones = Solution::count_bit_set_upto(nums, (num_bits - 1) as nat, nums.len());\n (Solution::bit_formula_sum(nums, (num_bits - 1) as nat) + ones * (nums.len() - ones)) as nat\n }\n }\n\n proof fn lemma_total_diff_bit_sum_eq_formula(nums: Seq, num_bits: nat)\n requires\n forall|k: int| 0 <= k < nums.len() ==> 0 <= #[trigger] nums[k],\n ensures\n Solution::total_diff_bit_sum(nums, 0, 1, num_bits) == Solution::bit_formula_sum(nums, num_bits),\n decreases num_bits,\n {\n if num_bits == 0 {\n Solution::lemma_total_diff_bit_sum_zero(nums, 0, 1);\n assert(Solution::bit_formula_sum(nums, 0) == 0);\n } else {\n let m: nat = (num_bits - 1) as nat;\n Solution::lemma_total_diff_bit_sum_eq_formula(nums, m);\n Solution::lemma_total_diff_bit_sum_step(nums, 0, 1, num_bits);\n Solution::lemma_pairs_differ_combinatorial(nums, 0, m);\n Solution::lemma_count_bit_set_additive(nums, m, 0, nums.len());\n let ones = Solution::count_bit_set_upto(nums, m, nums.len());\n assert(Solution::count_diff_from_countable(nums, m, 0, nums.len()) == ones);\n assert(Solution::pairs_differ_at_bit(nums, 0, 1, m) == ones * (nums.len() - ones));\n assert(Solution::total_diff_bit_sum(nums, 0, 1, num_bits)\n == Solution::total_diff_bit_sum(nums, 0, 1, m) + Solution::pairs_differ_at_bit(nums, 0, 1, m));\n assert(Solution::bit_formula_sum(nums, num_bits)\n == Solution::bit_formula_sum(nums, m) + ones * (nums.len() - ones));\n assert(Solution::total_diff_bit_sum(nums, 0, 1, num_bits) == Solution::bit_formula_sum(nums, num_bits));\n }\n }\n\n proof fn lemma_total_hamming_eq_bit_formula(nums: Seq, num_bits: nat)\n requires\n forall|k: int| 0 <= k < nums.len() ==> 0 <= #[trigger] nums[k] < Solution::pow2(num_bits) as int,\n forall|k: int| 0 <= k < nums.len() ==> #[trigger] nums[k] <= i32::MAX,\n ensures\n Solution::total_hamming_distance_spec(nums, 0, 1, 0) == Solution::bit_formula_sum(nums, num_bits),\n {\n Solution::lemma_total_eq_total_diff(nums, 0, 1, num_bits);\n Solution::lemma_total_diff_bit_sum_eq_formula(nums, num_bits);\n }\n\n proof fn lemma_count_bit_set_upto_bound(nums: Seq, b: nat, upto: nat)\n requires\n upto <= nums.len(),\n ensures\n Solution::count_bit_set_upto(nums, b, upto) <= upto,\n decreases upto,\n {\n if upto > 0 {\n Solution::lemma_count_bit_set_upto_bound(nums, b, (upto - 1) as nat);\n }\n }\n\n proof fn lemma_pow2_mono(e1: nat, e2: nat)\n requires\n e1 <= e2,\n ensures\n Solution::pow2(e1) <= Solution::pow2(e2),\n decreases e2 - e1,\n {\n if e1 < e2 {\n Solution::lemma_pow2_mono(e1, (e2 - 1) as nat);\n Solution::lemma_pow2_pos((e2 - 1) as nat);\n assert(Solution::pow2(e2) == 2 * Solution::pow2((e2 - 1) as nat));\n }\n }\n\n proof fn lemma_pow2_30()\n ensures\n Solution::pow2(30) == 1073741824,\n {\n assert(Solution::pow2(0) == 1);\n assert(Solution::pow2(1) == 2 * Solution::pow2(0));\n assert(Solution::pow2(2) == 2 * Solution::pow2(1));\n assert(Solution::pow2(3) == 2 * Solution::pow2(2));\n assert(Solution::pow2(4) == 2 * Solution::pow2(3));\n assert(Solution::pow2(5) == 2 * Solution::pow2(4));\n assert(Solution::pow2(6) == 2 * Solution::pow2(5));\n assert(Solution::pow2(7) == 2 * Solution::pow2(6));\n assert(Solution::pow2(8) == 2 * Solution::pow2(7));\n assert(Solution::pow2(9) == 2 * Solution::pow2(8));\n assert(Solution::pow2(10) == 2 * Solution::pow2(9));\n assert(Solution::pow2(11) == 2 * Solution::pow2(10));\n assert(Solution::pow2(12) == 2 * Solution::pow2(11));\n assert(Solution::pow2(13) == 2 * Solution::pow2(12));\n assert(Solution::pow2(14) == 2 * Solution::pow2(13));\n assert(Solution::pow2(15) == 2 * Solution::pow2(14));\n assert(Solution::pow2(16) == 2 * Solution::pow2(15));\n assert(Solution::pow2(17) == 2 * Solution::pow2(16));\n assert(Solution::pow2(18) == 2 * Solution::pow2(17));\n assert(Solution::pow2(19) == 2 * Solution::pow2(18));\n assert(Solution::pow2(20) == 2 * Solution::pow2(19));\n assert(Solution::pow2(21) == 2 * Solution::pow2(20));\n assert(Solution::pow2(22) == 2 * Solution::pow2(21));\n assert(Solution::pow2(23) == 2 * Solution::pow2(22));\n assert(Solution::pow2(24) == 2 * Solution::pow2(23));\n assert(Solution::pow2(25) == 2 * Solution::pow2(24));\n assert(Solution::pow2(26) == 2 * Solution::pow2(25));\n assert(Solution::pow2(27) == 2 * Solution::pow2(26));\n assert(Solution::pow2(28) == 2 * Solution::pow2(27));\n assert(Solution::pow2(29) == 2 * Solution::pow2(28));\n assert(Solution::pow2(30) == 2 * Solution::pow2(29));\n }\n\n proof fn lemma_bit_extract(x: i32, pw: i64, b: nat)\n requires\n 0 <= x,\n pw as int == Solution::pow2(b) as int,\n ensures\n ((x as i64 / pw) % 2 == 1) == (Solution::bit_of(x as nat, b) == 1),\n {\n Solution::lemma_pow2_pos(b);\n assert(Solution::bit_of(x as nat, b) == (x as nat / Solution::pow2(b)) % 2);\n assert(pw >= 1);\n let qi: i64 = (x as i64 / pw) as i64;\n let qn: nat = x as nat / Solution::pow2(b);\n assert(qi as int == (x as nat as int) / (Solution::pow2(b) as int)) by (nonlinear_arith)\n requires\n 0 <= x,\n pw as int == Solution::pow2(b) as int,\n pw >= 1,\n qi as int == (x as int) / (pw as int);\n assert(qn as int == (x as nat as int) / (Solution::pow2(b) as int)) by (nonlinear_arith)\n requires\n 0 <= x,\n Solution::pow2(b) >= 1,\n qn == x as nat / Solution::pow2(b);\n assert(qi as int == qn as int);\n assert(qi >= 0);\n assert((qi % 2 == 1) == (qn % 2 == 1)) by (nonlinear_arith)\n requires\n qi as int == qn as int,\n qi >= 0;\n }\n\n pub fn total_hamming_distance(nums: Vec) -> (res: i32)\n requires\n 1 <= nums.len() <= 10000,\n forall|i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] <= 1_000_000_000,\n i32::MIN <= Solution::total_hamming_distance_spec(nums@, 0, 1, 0) <= i32::MAX,\n ensures\n res == Solution::total_hamming_distance_spec(nums@, 0, 1, 0),\n {\n let n = nums.len();\n let mut total: i64 = 0;\n let mut pw: i64 = 1;\n let mut b: usize = 0;\n proof {\n assert(Solution::pow2(0) == 1);\n }\n while b < 30\n invariant\n 0 <= b <= 30,\n n == nums.len(),\n forall|k: int| 0 <= k < n ==> 0 <= #[trigger] nums[k as int] <= 1_000_000_000,\n pw as int == Solution::pow2(b as nat) as int,\n pw >= 1,\n total as int == Solution::bit_formula_sum(nums@, b as nat),\n 0 <= total <= (b as i64) * 100_000_000,\n n <= 10000,\n decreases 30 - b,\n {\n let mut ones: i64 = 0;\n let mut idx: usize = 0;\n while idx < n\n invariant\n 0 <= idx <= n,\n n == nums.len(),\n n <= 10000,\n forall|k: int| 0 <= k < n ==> 0 <= #[trigger] nums[k as int] <= 1_000_000_000,\n pw as int == Solution::pow2(b as nat) as int,\n pw >= 1,\n ones as int == Solution::count_bit_set_upto(nums@, b as nat, idx as nat),\n decreases n - idx,\n {\n proof {\n Solution::lemma_bit_extract(nums@[idx as int], pw, b as nat);\n Solution::lemma_count_bit_set_upto_bound(nums@, b as nat, idx as nat);\n assert(Solution::count_bit_set_upto(nums@, b as nat, idx as nat) <= idx);\n assert(ones as int <= idx as int);\n assert(ones <= idx as i64);\n }\n if (nums[idx] as i64 / pw) % 2 == 1 {\n ones += 1;\n }\n idx += 1;\n proof {\n assert(Solution::count_bit_set_upto(nums@, b as nat, idx as nat)\n == Solution::count_bit_set_upto(nums@, b as nat, (idx - 1) as nat)\n + (if Solution::bit_of(nums[idx as int - 1] as nat, b as nat) == 1 { 1nat } else { 0nat }));\n }\n }\n proof {\n Solution::lemma_count_bit_set_upto_bound(nums@, b as nat, n as nat);\n assert(Solution::count_bit_set_upto(nums@, b as nat, n as nat) <= n);\n assert(ones as int == Solution::count_bit_set_upto(nums@, b as nat, n as nat));\n assert(n as nat == nums@.len());\n assert(Solution::bit_formula_sum(nums@, (b + 1) as nat)\n == Solution::bit_formula_sum(nums@, b as nat)\n + Solution::count_bit_set_upto(nums@, b as nat, nums@.len()) as nat\n * ((nums@.len() - Solution::count_bit_set_upto(nums@, b as nat, nums@.len())) as nat));\n assert(Solution::bit_formula_sum(nums@, (b + 1) as nat)\n == Solution::bit_formula_sum(nums@, b as nat) + (ones as nat) * ((n - ones) as nat));\n assert(0 <= ones as int <= n as int);\n assert(0 <= ones <= n as i64);\n assert(ones * ((n as i64) - ones) <= 100_000_000) by (nonlinear_arith)\n requires\n 0 <= ones <= n as i64,\n n as i64 <= 10000;\n }\n total += ones * ((n as i64) - ones);\n proof {\n Solution::lemma_pow2_mono(b as nat, 29);\n Solution::lemma_pow2_30();\n assert(Solution::pow2(30) == 2 * Solution::pow2(29));\n assert(Solution::pow2(29) == 536870912);\n assert(pw as int <= 536870912);\n }\n pw *= 2;\n b += 1;\n proof {\n assert(Solution::pow2(b as nat) == 2 * Solution::pow2((b - 1) as nat));\n }\n }\n proof {\n Solution::lemma_pow2_30();\n assert(forall|k: int| 0 <= k < nums.len() ==> 0 <= #[trigger] nums@[k] < Solution::pow2(30) as int);\n assert(forall|k: int| 0 <= k < nums.len() ==> #[trigger] nums@[k] <= i32::MAX);\n Solution::lemma_total_hamming_eq_bit_formula(nums@, 30);\n }\n total as i32\n }\n\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc477/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc477/tests/mutated_testcases.jsonl"} {"problem": "lc492", "family": "leetcode", "description": "# 492. Construct the Rectangle\n\nA web developer needs to know how to design a web page's size. So, given a specific rectangular web page's area, your job is to design a rectangular web page whose length `L` and width `W` satisfy the following requirements:\n\n- The area of the rectangular web page you designed must equal to the given target area.\n- The width `W` should not be larger than the length `L`, which means `L >= W`.\n- The difference between length `L` and width `W` should be as small as possible.\n\nReturn an array `[L, W]` where `L` and `W` are the length and width of the web page you designed in sequence.\n\n## Example 1\n\n- **Input:** `area = 4`\n- **Output:** `[2,2]`\n- **Explanation:** The target area is 4, and all the possible ways to construct it are `[1,4]`, `[2,2]`, `[4,1]`. But according to requirement 2, `[1,4]` is illegal; according to requirement 3, `[4,1]` is not optimal compared to `[2,2]`. So the length `L` is 2, and the width `W` is 2.\n\n## Example 2\n\n- **Input:** `area = 37`\n- **Output:** `[37,1]`\n\n## Example 3\n\n- **Input:** `area = 122122`\n- **Output:** `[427,286]`\n\n## Constraints\n\n- $1 \\leq \\text{area} \\leq 10^7$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn construct_rectangle(area: i32) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance Rate: 63.0%", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn scan_spec(area: nat, w: nat, best_w: nat) -> nat\n recommends\n 1 <= w,\n 1 <= best_w < w,\n area % best_w == 0,\n area / best_w >= best_w,\n decreases area + 1 - w,\n {\n if w > area {\n best_w\n } else if area % w == 0 && area / w >= w {\n Self::scan_spec(area, w + 1, w)\n } else {\n Self::scan_spec(area, w + 1, best_w)\n }\n }\n\n pub open spec fn construct_rectangle_spec(area: nat) -> (nat, nat) {\n let w = Self::scan_spec(area, 1, 1);\n (area / w, w)\n }\n\n pub fn construct_rectangle(area: i32) -> (res: Vec)\n requires\n 1 <= area <= 10_000_000,\n ensures\n res.len() == 2,\n res[0] as nat == Self::construct_rectangle_spec(area as nat).0,\n res[1] as nat == Self::construct_rectangle_spec(area as nat).1,\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn construct_rectangle(area: i32) -> Vec {\n let mut best_l: i32 = area;\n let mut best_w: i32 = 1;\n let mut w: i32 = 1;\n\n while w <= area {\n if area % w == 0 && area / w >= w {\n best_l = area / w;\n best_w = w;\n }\n w += 1;\n }\n\n vec![best_l, best_w]\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn scan_spec(area: nat, w: nat, best_w: nat) -> nat\n recommends\n 1 <= w,\n 1 <= best_w < w,\n area % best_w == 0,\n area / best_w >= best_w,\n decreases area + 1 - w,\n {\n if w > area {\n best_w\n } else if area % w == 0 && area / w >= w {\n Self::scan_spec(area, w + 1, w)\n } else {\n Self::scan_spec(area, w + 1, best_w)\n }\n }\n\n pub open spec fn construct_rectangle_spec(area: nat) -> (nat, nat) {\n let w = Self::scan_spec(area, 1, 1);\n (area / w, w)\n }\n\n pub fn construct_rectangle(area: i32) -> (res: Vec)\n requires\n 1 <= area <= 10_000_000,\n ensures\n res.len() == 2,\n res[0] as nat == Self::construct_rectangle_spec(area as nat).0,\n res[1] as nat == Self::construct_rectangle_spec(area as nat).1,\n {\n let mut best_l: i32 = area;\n let mut best_w: i32 = 1;\n let mut w: i32 = 1;\n\n while w <= area {\n if area % w == 0 && area / w >= w {\n best_l = area / w;\n best_w = w;\n }\n w += 1;\n }\n\n vec![best_l, best_w]\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn scan_spec(area: nat, w: nat, best_w: nat) -> nat\n recommends\n 1 <= w,\n 1 <= best_w < w,\n area % best_w == 0,\n area / best_w >= best_w,\n decreases area + 1 - w,\n {\n if w > area {\n best_w\n } else if area % w == 0 && area / w >= w {\n Self::scan_spec(area, w + 1, w)\n } else {\n Self::scan_spec(area, w + 1, best_w)\n }\n }\n\n pub open spec fn construct_rectangle_spec(area: nat) -> (nat, nat) {\n let w = Self::scan_spec(area, 1, 1);\n (area / w, w)\n }\n\n pub fn construct_rectangle(area: i32) -> (res: Vec)\n requires\n 1 <= area <= 10_000_000,\n ensures\n res.len() == 2,\n res[0] as nat == Self::construct_rectangle_spec(area as nat).0,\n res[1] as nat == Self::construct_rectangle_spec(area as nat).1,\n {\n let mut best_l: i32 = area;\n let mut best_w: i32 = 1;\n let mut w: i32 = 1;\n\n while w <= area\n invariant\n 1 <= area <= 10_000_000,\n 1 <= w <= area + 1,\n 1 <= best_w <= w,\n 2 <= w ==> best_w < w,\n area % best_w == 0,\n area / best_w >= best_w,\n best_l == area / best_w,\n w == 1 ==> best_w == 1,\n w == 1 ==> Self::scan_spec(area as nat, w as nat, best_w as nat) == Self::scan_spec(area as nat, 1, 1),\n 2 <= w ==> Self::scan_spec(area as nat, w as nat, best_w as nat) == Self::scan_spec(area as nat, 1, 1),\n decreases area - w + 1,\n {\n if area % w == 0 && area / w >= w {\n proof {\n if 2 <= w {\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat)\n == Self::scan_spec(area as nat, 1, 1));\n } else {\n assert(w == 1);\n assert(best_w == 1);\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat)\n == Self::scan_spec(area as nat, 1, 1));\n }\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat)\n == Self::scan_spec(area as nat, (w + 1) as nat, w as nat));\n }\n\n best_l = area / w;\n best_w = w;\n } else {\n proof {\n if 2 <= w {\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat)\n == Self::scan_spec(area as nat, 1, 1));\n } else {\n assert(w == 1);\n assert(best_w == 1);\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat)\n == Self::scan_spec(area as nat, 1, 1));\n }\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat)\n == Self::scan_spec(area as nat, (w + 1) as nat, best_w as nat));\n }\n }\n\n w += 1;\n }\n\n proof {\n assert(w == area + 1);\n assert(w > area);\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat) == best_w as nat);\n assert(2 <= w);\n assert(Self::scan_spec(area as nat, w as nat, best_w as nat) == Self::scan_spec(area as nat, 1, 1));\n assert(best_w as nat == Self::scan_spec(area as nat, 1, 1));\n assert(best_l as nat == area as nat / best_w as nat);\n }\n\n vec![best_l, best_w]\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc492/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc492/tests/mutated_testcases.jsonl"} {"problem": "lc518", "family": "leetcode", "description": "# Coin Change II\n\nYou are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.\n\nReturn *the number of combinations that make up that amount*. If that amount of money cannot be made up by any combination of the coins, return `0`.\n\nYou may assume that you have an infinite number of each kind of coin.\n\nThe answer is **guaranteed** to fit into a signed **32-bit** integer.\n\n \n\nExample 1:\n\n```\n**Input:** amount = 5, coins = [1,2,5]\n**Output:** 4\n**Explanation:** there are four ways to make up the amount:\n5=5\n5=2+2+1\n5=2+1+1+1\n5=1+1+1+1+1\n```\n\nExample 2:\n\n```\n**Input:** amount = 3, coins = [2]\n**Output:** 0\n**Explanation:** the amount of 3 cannot be made up just with coins of 2.\n```\n\nExample 3:\n\n```\n**Input:** amount = 10, coins = [10]\n**Output:** 1\n```\n\n \n\n**Constraints:**\n\n\t- `1 <= coins.length <= 300`\n\n\t- `1 <= coins[i] <= 5000`\n\n\t- All the values of `coins` are **unique**.\n\n\t- `0 <= amount <= 5000`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn change(amount: i32, coins: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming\nAcceptance rate: 60.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn coin_change_ways(coins: Seq, coins_used: nat, amount: int) -> int\n decreases coins_used, amount,\n {\n if coins_used == 0 {\n if amount == 0 { 1 } else { 0 }\n } else {\n let idx = (coins_used - 1) as int;\n let c = coins[idx] as int;\n Self::coin_change_ways(coins, (coins_used - 1) as nat, amount)\n + (if 1 <= c <= amount {\n Self::coin_change_ways(coins, coins_used, amount - c)\n } else {\n 0\n })\n }\n }\n\n pub fn change(amount: i32, coins: Vec) -> (res: i32)\n requires\n 0 <= amount <= 5000,\n 1 <= coins.len() <= 300,\n forall |i: int| 0 <= i < coins.len() ==> 1 <= #[trigger] coins[i] <= 5000,\n forall |i: int, j: int| 0 <= i < j < coins.len() ==> coins[i] != coins[j],\n Self::coin_change_ways(coins@, coins.len() as nat, amount as int) <= i32::MAX,\n ensures\n res as int == Self::coin_change_ways(coins@, coins.len() as nat, amount as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn coin_rec(coins: &Vec, i: usize, a: usize, memo: &mut Vec, width: usize) -> i32 {\n let idx = i * width + a;\n let cur = memo[idx];\n if cur != -1 {\n return cur;\n }\n let result: i32;\n if i == 0 {\n result = if a == 0 { 1 } else { 0 };\n } else {\n let c = coins[i - 1];\n let part1 = Self::coin_rec(coins, i - 1, a, memo, width);\n let mut total = part1;\n if (c as usize) <= a {\n let part2 = Self::coin_rec(coins, i, a - c as usize, memo, width);\n total = total + part2;\n }\n result = total;\n }\n memo[idx] = result;\n result\n }\n\n pub fn change(amount: i32, coins: Vec) -> i32 {\n let width = amount as usize + 1;\n let total_len = (coins.len() + 1) * width;\n let mut memo: Vec = Vec::new();\n let mut k: usize = 0;\n while k < total_len {\n memo.push(-1);\n k += 1;\n }\n Self::coin_rec(&coins, coins.len(), amount as usize, &mut memo, width)\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn coin_change_ways(coins: Seq, coins_used: nat, amount: int) -> int\n decreases coins_used, amount,\n {\n if coins_used == 0 {\n if amount == 0 { 1 } else { 0 }\n } else {\n let idx = (coins_used - 1) as int;\n let c = coins[idx] as int;\n Self::coin_change_ways(coins, (coins_used - 1) as nat, amount)\n + (if 1 <= c <= amount {\n Self::coin_change_ways(coins, coins_used, amount - c)\n } else {\n 0\n })\n }\n }\n\n pub open spec fn memo_sound(coins: Seq, memo: Seq, width: int) -> bool {\n forall |ii: int, aa: int| 0 <= ii <= coins.len() && 0 <= aa < width\n ==> #[trigger] memo[ii * width + aa] != -1\n ==> memo[ii * width + aa] as int == Self::coin_change_ways(coins, ii as nat, aa)\n }\n\n fn coin_rec(coins: &Vec, i: usize, a: usize, memo: &mut Vec, width: usize) -> (res: i32)\n requires\n forall |k: int| 0 <= k < coins.len() ==> 1 <= #[trigger] coins[k] <= 5000,\n coins.len() <= 300,\n width >= 1,\n old(memo).len() == (coins.len() + 1) * width,\n i <= coins.len(),\n a < width,\n Self::memo_sound(coins@, old(memo)@, width as int),\n Self::coin_change_ways(coins@, i as nat, a as int) <= i32::MAX,\n ensures\n res as int == Self::coin_change_ways(coins@, i as nat, a as int),\n memo.len() == old(memo).len(),\n Self::memo_sound(coins@, memo@, width as int),\n {\n let idx = i * width + a;\n let cur = memo[idx];\n if cur != -1 {\n return cur;\n }\n let result: i32;\n if i == 0 {\n result = if a == 0 { 1 } else { 0 };\n } else {\n let c = coins[i - 1];\n let part1 = Self::coin_rec(coins, i - 1, a, memo, width);\n let mut total = part1;\n if (c as usize) <= a {\n let part2 = Self::coin_rec(coins, i, a - c as usize, memo, width);\n total = total + part2;\n }\n result = total;\n }\n memo.set(idx, result);\n result\n }\n\n pub fn change(amount: i32, coins: Vec) -> (res: i32)\n requires\n 0 <= amount <= 5000,\n 1 <= coins.len() <= 300,\n forall |i: int| 0 <= i < coins.len() ==> 1 <= #[trigger] coins[i] <= 5000,\n forall |i: int, j: int| 0 <= i < j < coins.len() ==> coins[i] != coins[j],\n Self::coin_change_ways(coins@, coins.len() as nat, amount as int) <= i32::MAX,\n ensures\n res as int == Self::coin_change_ways(coins@, coins.len() as nat, amount as int),\n {\n let width = amount as usize + 1;\n let total_len = (coins.len() + 1) * width;\n let mut memo: Vec = Vec::new();\n let mut k: usize = 0;\n while k < total_len {\n memo.push(-1);\n k += 1;\n }\n Self::coin_rec(&coins, coins.len(), amount as usize, &mut memo, width)\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn coin_change_ways(coins: Seq, coins_used: nat, amount: int) -> int\n decreases coins_used, amount,\n {\n if coins_used == 0 {\n if amount == 0 { 1 } else { 0 }\n } else {\n let idx = (coins_used - 1) as int;\n let c = coins[idx] as int;\n Self::coin_change_ways(coins, (coins_used - 1) as nat, amount)\n + (if 1 <= c <= amount {\n Self::coin_change_ways(coins, coins_used, amount - c)\n } else {\n 0\n })\n }\n }\n\n proof fn lemma_ways_nonneg(coins: Seq, coins_used: nat, amount: int)\n requires\n coins_used <= coins.len(),\n 0 <= amount,\n ensures\n 0 <= Self::coin_change_ways(coins, coins_used, amount),\n decreases coins_used, amount,\n {\n if coins_used == 0 {\n } else if amount == 0 {\n Self::lemma_ways_nonneg(coins, (coins_used - 1) as nat, 0);\n } else {\n let c = coins[(coins_used - 1) as int] as int;\n if 1 <= c <= amount {\n Self::lemma_ways_nonneg(coins, coins_used, amount - c);\n Self::lemma_ways_nonneg(coins, (coins_used - 1) as nat, amount);\n } else {\n Self::lemma_ways_nonneg(coins, (coins_used - 1) as nat, amount);\n }\n }\n }\n\n proof fn lemma_bound_drop_index(coins: Seq, coins_used: nat, amount: int)\n requires\n coins_used > 0,\n coins_used <= coins.len(),\n 0 <= amount,\n ensures\n Self::coin_change_ways(coins, (coins_used - 1) as nat, amount)\n <= Self::coin_change_ways(coins, coins_used, amount),\n {\n let c = coins[(coins_used - 1) as int] as int;\n if 1 <= c <= amount {\n Self::lemma_ways_nonneg(coins, coins_used, amount - c);\n }\n }\n\n proof fn lemma_bound_drop_amount(coins: Seq, coins_used: nat, amount: int)\n requires\n coins_used > 0,\n coins_used <= coins.len(),\n 0 <= amount,\n 1 <= coins[(coins_used - 1) as int] as int <= amount,\n ensures\n Self::coin_change_ways(coins, coins_used, amount - (coins[(coins_used - 1) as int] as int))\n <= Self::coin_change_ways(coins, coins_used, amount),\n {\n Self::lemma_ways_nonneg(coins, (coins_used - 1) as nat, amount);\n }\n\n pub open spec fn memo_sound(coins: Seq, memo: Seq, width: int) -> bool {\n forall |ii: int, aa: int| 0 <= ii <= coins.len() && 0 <= aa < width\n ==> #[trigger] memo[ii * width + aa] != -1\n ==> memo[ii * width + aa] as int == Self::coin_change_ways(coins, ii as nat, aa)\n }\n\n proof fn lemma_idx_bound(coins_len: int, width: int, ii: int, aa: int)\n requires\n 0 <= ii <= coins_len,\n 0 <= aa < width,\n width >= 1,\n ensures\n 0 <= ii * width + aa < (coins_len + 1) * width,\n {\n assert(0 <= ii * width + aa < (coins_len + 1) * width) by (nonlinear_arith)\n requires\n 0 <= ii <= coins_len,\n 0 <= aa < width,\n width >= 1,\n {}\n }\n\n proof fn lemma_idx_unique(width: int, i1: int, a1: int, i2: int, a2: int)\n requires\n 0 <= a1 < width,\n 0 <= a2 < width,\n i1 * width + a1 == i2 * width + a2,\n ensures\n i1 == i2,\n a1 == a2,\n {\n assert((i1 - i2) * width == a2 - a1) by (nonlinear_arith)\n requires i1 * width + a1 == i2 * width + a2;\n if i1 != i2 {\n assert(i1 - i2 != 0);\n if i1 > i2 {\n assert((i1 - i2) * width >= width) by (nonlinear_arith)\n requires i1 - i2 >= 1, width > 0;\n assert(false);\n } else {\n assert((i1 - i2) * width <= -width) by (nonlinear_arith)\n requires i1 - i2 <= -1, width > 0;\n assert(false);\n }\n }\n }\n\n fn coin_rec(coins: &Vec, i: usize, a: usize, memo: &mut Vec, width: usize) -> (res: i32)\n requires\n forall |k: int| 0 <= k < coins.len() ==> 1 <= #[trigger] coins[k] <= 5000,\n coins.len() <= 300,\n width >= 1,\n old(memo).len() == (coins.len() + 1) * width,\n i <= coins.len(),\n a < width,\n Self::memo_sound(coins@, old(memo)@, width as int),\n Self::coin_change_ways(coins@, i as nat, a as int) <= i32::MAX,\n ensures\n res as int == Self::coin_change_ways(coins@, i as nat, a as int),\n memo.len() == old(memo).len(),\n Self::memo_sound(coins@, memo@, width as int),\n decreases i, a,\n {\n proof {\n Self::lemma_idx_bound(coins.len() as int, width as int, i as int, a as int);\n }\n let idx = i * width + a;\n let cur = memo[idx];\n if cur != -1 {\n assert(Self::memo_sound(coins@, memo@, width as int));\n assert(memo@[idx as int] != -1);\n assert(memo@[idx as int] as int == Self::coin_change_ways(coins@, i as nat, a as int));\n return cur;\n }\n let result: i32;\n if i == 0 {\n result = if a == 0 { 1 } else { 0 };\n } else {\n let c = coins[i - 1];\n proof {\n Self::lemma_bound_drop_index(coins@, i as nat, a as int);\n }\n let part1 = Self::coin_rec(coins, i - 1, a, memo, width);\n let mut total = part1;\n if (c as usize) <= a {\n proof {\n assert(1 <= c as int <= a as int);\n Self::lemma_bound_drop_amount(coins@, i as nat, a as int);\n }\n let part2 = Self::coin_rec(coins, i, a - c as usize, memo, width);\n proof {\n Self::lemma_ways_nonneg(coins@, (i - 1) as nat, a as int);\n Self::lemma_ways_nonneg(coins@, i as nat, (a - c as usize) as int);\n assert(Self::coin_change_ways(coins@, i as nat, a as int)\n == Self::coin_change_ways(coins@, (i - 1) as nat, a as int)\n + Self::coin_change_ways(coins@, i as nat, (a - c as usize) as int));\n }\n total = total + part2;\n } else {\n proof {\n assert(!(1 <= c as int <= a as int));\n assert(Self::coin_change_ways(coins@, i as nat, a as int)\n == Self::coin_change_ways(coins@, (i - 1) as nat, a as int));\n }\n }\n result = total;\n }\n proof {\n assert(result as int == Self::coin_change_ways(coins@, i as nat, a as int));\n }\n let ghost pre_memo = memo@;\n memo.set(idx, result);\n proof {\n assert(memo@ == pre_memo.update(idx as int, result));\n assert(memo@.len() == (coins.len() as int + 1) * width as int);\n assert forall |ii: int, aa: int| 0 <= ii <= coins.len() as int && 0 <= aa < width as int &&\n #[trigger] memo@[ii * width as int + aa] != -1\n implies memo@[ii * width as int + aa] as int == Self::coin_change_ways(coins@, ii as nat, aa)\n by {\n let jdx = ii * width as int + aa;\n Self::lemma_idx_bound(coins.len() as int, width as int, ii, aa);\n assert(0 <= jdx < memo@.len());\n if jdx == idx as int {\n Self::lemma_idx_unique(width as int, ii, aa, i as int, a as int);\n } else {\n assert(memo@[jdx] == pre_memo[jdx]);\n }\n }\n }\n result\n }\n\n pub fn change(amount: i32, coins: Vec) -> (res: i32)\n requires\n 0 <= amount <= 5000,\n 1 <= coins.len() <= 300,\n forall |i: int| 0 <= i < coins.len() ==> 1 <= #[trigger] coins[i] <= 5000,\n forall |i: int, j: int| 0 <= i < j < coins.len() ==> coins[i] != coins[j],\n Self::coin_change_ways(coins@, coins.len() as nat, amount as int) <= i32::MAX,\n ensures\n res as int == Self::coin_change_ways(coins@, coins.len() as nat, amount as int),\n {\n let width = amount as usize + 1;\n proof {\n assert((coins.len() + 1) * width <= 301 * 5001) by (nonlinear_arith)\n requires\n coins.len() <= 300,\n width <= 5001,\n {}\n }\n let total_len = (coins.len() + 1) * width;\n let mut memo: Vec = Vec::new();\n let mut k: usize = 0;\n while k < total_len\n invariant\n 0 <= k <= total_len,\n memo.len() == k,\n forall |idx: int| 0 <= idx < k ==> memo@[idx] == -1,\n decreases total_len - k,\n {\n memo.push(-1);\n k += 1;\n }\n proof {\n assert forall |ii: int, aa: int| 0 <= ii <= coins.len() as int && 0 <= aa < width as int &&\n #[trigger] memo@[ii * width as int + aa] != -1\n implies memo@[ii * width as int + aa] as int == Self::coin_change_ways(coins@, ii as nat, aa)\n by {\n Self::lemma_idx_bound(coins.len() as int, width as int, ii, aa);\n assert(memo@[ii * width as int + aa] == -1);\n }\n }\n Self::coin_rec(&coins, coins.len(), amount as usize, &mut memo, width)\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc518/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc518/tests/mutated_testcases.jsonl"} {"problem": "lc520", "family": "leetcode", "description": "# Detect Capital\n\nWe define the usage of capitals in a word to be right when one of the following cases holds:\n\n- All letters in this word are capitals, like `\"USA\"`.\n\n- All letters in this word are not capitals, like `\"leetcode\"`.\n\n- Only the first letter in this word is capital, like `\"Google\"`.\n\nGiven a string `word`, return `true` if the usage of capitals in it is right.\n\n## Example 1:\n\n> **Input:** word = \"USA\"\n> **Output:** true\n\n## Example 2:\n\n> **Input:** word = \"FlaG\"\n> **Output:** false\n\n## Constraints:\n\n- $1 <= word.length <= 100$\n\n- `word` consists of lowercase and uppercase English letters.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn detect_capital_use(word: String) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, String\nAcceptance rate: 56.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn detect_capital_use(word: String) -> (res: bool) \n requires\n 1 <= word@.len() <= 100,\n forall |i: int| 0 <= i < word@.len() ==> \n (('A' <= word@[i] && word@[i] <= 'Z') || ('a' <= word@[i] && word@[i] <= 'z')),\n ensures\n res == (\n (forall |i: int| 0 <= i < word@.len() ==> ('A' <= word@[i] && word@[i] <= 'Z')) ||\n (forall |i: int| 0 <= i < word@.len() ==> ('a' <= word@[i] && word@[i] <= 'z')) ||\n (('A' <= word@[0] && word@[0] <= 'Z') && \n forall |i: int| 1 <= i < word@.len() ==> ('a' <= word@[i] && word@[i] <= 'z'))\n ),\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn detect_capital_use(word: String) -> bool\n {\n let len = word.as_str().unicode_len();\n let mut all_upper = true;\n let mut i: usize = 0;\n\n while i < len && all_upper\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'A' && c <= 'Z') {\n all_upper = false;\n }\n i += 1;\n }\n \n if all_upper {\n return true;\n }\n \n let mut all_lower = true;\n i = 0;\n while i < len && all_lower\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'a' && c <= 'z') {\n all_lower = false;\n }\n i += 1;\n }\n \n if all_lower {\n return true;\n }\n \n let first = word.as_str().get_char(0);\n if !(first >= 'A' && first <= 'Z') {\n return false;\n }\n \n i = 1;\n let mut rest_lower = true;\n while i < len && rest_lower\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'a' && c <= 'z') {\n rest_lower = false;\n }\n i += 1;\n }\n \n rest_lower\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn detect_capital_use(word: String) -> (res: bool) \n requires\n 1 <= word@.len() <= 100,\n forall |i: int| 0 <= i < word@.len() ==> \n (('A' <= word@[i] && word@[i] <= 'Z') || ('a' <= word@[i] && word@[i] <= 'z')),\n ensures\n res == (\n (forall |i: int| 0 <= i < word@.len() ==> ('A' <= word@[i] && word@[i] <= 'Z')) ||\n (forall |i: int| 0 <= i < word@.len() ==> ('a' <= word@[i] && word@[i] <= 'z')) ||\n (('A' <= word@[0] && word@[0] <= 'Z') && \n forall |i: int| 1 <= i < word@.len() ==> ('a' <= word@[i] && word@[i] <= 'z'))\n ),\n {\n let len = word.as_str().unicode_len();\n \n let mut all_upper = true;\n let mut i: usize = 0;\n while i < len && all_upper\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'A' && c <= 'Z') {\n all_upper = false;\n }\n i += 1;\n }\n \n if all_upper {\n return true;\n }\n \n let mut all_lower = true;\n i = 0;\n while i < len && all_lower\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'a' && c <= 'z') {\n all_lower = false;\n }\n i += 1;\n }\n \n if all_lower {\n return true;\n }\n \n let first = word.as_str().get_char(0);\n if !(first >= 'A' && first <= 'Z') {\n return false;\n }\n \n i = 1;\n let mut rest_lower = true;\n while i < len && rest_lower\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'a' && c <= 'z') {\n rest_lower = false;\n }\n i += 1;\n }\n \n rest_lower\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn detect_capital_use(word: String) -> (res: bool) \n requires\n 1 <= word@.len() <= 100,\n forall |i: int| 0 <= i < word@.len() ==> \n (('A' <= word@[i] && word@[i] <= 'Z') || ('a' <= word@[i] && word@[i] <= 'z')),\n ensures\n res == (\n (forall |i: int| 0 <= i < word@.len() ==> ('A' <= word@[i] && word@[i] <= 'Z')) ||\n (forall |i: int| 0 <= i < word@.len() ==> ('a' <= word@[i] && word@[i] <= 'z')) ||\n (('A' <= word@[0] && word@[0] <= 'Z') && \n forall |i: int| 1 <= i < word@.len() ==> ('a' <= word@[i] && word@[i] <= 'z'))\n ),\n {\n let len = word.as_str().unicode_len();\n \n let mut all_upper = true;\n let mut i: usize = 0;\n while i < len && all_upper\n invariant\n 1 <= word@.len() <= 100,\n len == word@.len(),\n forall |i: int| 0 <= i < word@.len() ==> \n (('A' <= word@[i] && word@[i] <= 'Z') || ('a' <= word@[i] && word@[i] <= 'z')),\n 0 <= i <= len,\n all_upper ==> forall |j: int| 0 <= j < i ==> ('A' <= word@[j] && word@[j] <= 'Z'),\n !all_upper ==> exists |j: int| 0 <= j < i && !('A' <= word@[j] && word@[j] <= 'Z'),\n decreases len - i,\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'A' && c <= 'Z') {\n all_upper = false;\n }\n i += 1;\n }\n \n if all_upper {\n return true;\n }\n \n let mut all_lower = true;\n i = 0;\n while i < len && all_lower\n invariant\n 1 <= word@.len() <= 100,\n len == word@.len(),\n forall |i: int| 0 <= i < word@.len() ==> \n (('A' <= word@[i] && word@[i] <= 'Z') || ('a' <= word@[i] && word@[i] <= 'z')),\n 0 <= i <= len,\n all_lower ==> forall |j: int| 0 <= j < i ==> ('a' <= word@[j] && word@[j] <= 'z'),\n !all_lower ==> exists |j: int| 0 <= j < i && !('a' <= word@[j] && word@[j] <= 'z'),\n decreases len - i,\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'a' && c <= 'z') {\n all_lower = false;\n }\n i += 1;\n }\n \n if all_lower {\n return true;\n }\n \n let first = word.as_str().get_char(0);\n if !(first >= 'A' && first <= 'Z') {\n return false;\n }\n \n i = 1;\n let mut rest_lower = true;\n while i < len && rest_lower\n invariant\n 1 <= word@.len() <= 100,\n len == word@.len(),\n forall |i: int| 0 <= i < word@.len() ==> \n (('A' <= word@[i] && word@[i] <= 'Z') || ('a' <= word@[i] && word@[i] <= 'z')),\n 'A' <= word@[0] && word@[0] <= 'Z',\n 1 <= i <= len,\n rest_lower ==> forall |j: int| 1 <= j < i ==> ('a' <= word@[j] && word@[j] <= 'z'),\n !rest_lower ==> exists |j: int| 1 <= j < i && !('a' <= word@[j] && word@[j] <= 'z'),\n decreases len - i,\n {\n let c = word.as_str().get_char(i);\n if !(c >= 'a' && c <= 'z') {\n rest_lower = false;\n }\n i += 1;\n }\n \n rest_lower\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc520/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc520/tests/mutated_testcases.jsonl"} {"problem": "lc53", "family": "leetcode", "description": "# Maximum Subarray\n\nGiven an integer array `nums`, find the subarray with the largest sum, and return its sum.\n\n## Example 1:\n\n> **Input:** nums = [-2,1,-3,4,-1,2,1,-5,4]\n> **Output:** 6\n> **Explanation:** The subarray [4,-1,2,1] has the largest sum 6.\n\n## Example 2:\n\n> **Input:** nums = [1]\n> **Output:** 1\n> **Explanation:** The subarray [1] has the largest sum 1.\n\n## Example 3:\n\n> **Input:** nums = [5,4,-1,7,8]\n> **Output:** 23\n> **Explanation:** The subarray [5,4,-1,7,8] has the largest sum 23.\n\n## Constraints:\n\n- $1 <= nums.length <= 10^5$\n\n- $-10^4 <= nums[i] <= 10^4$\n\n**Follow up:** If you have figured out the `O(n)` solution, try coding another solution using the **divide and conquer** approach, which is more subtle.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_sub_array(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Divide and Conquer, Dynamic Programming\nAcceptance rate: 53.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n s[l] as int + Self::sum_range(s, l + 1, r)\n }\n }\n\n pub fn max_sub_array(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n ensures\n exists |l: int, r: int|\n 0 <= l < r <= nums.len() as int &&\n result as int == #[trigger] Self::sum_range(nums@, l, r),\n forall |l: int, r: int|\n 0 <= l < r <= nums.len() as int ==>\n #[trigger] Self::sum_range(nums@, l, r) <= result as int,\n {\n \n }\n}\n\n} \n", "code": "impl Solution {\n pub fn max_sub_array(nums: Vec) -> i32\n {\n let n = nums.len();\n\n let mut max_here: i64 = nums[0] as i64;\n let mut max_so_far: i64 = nums[0] as i64;\n\n let mut i: usize = 1;\n\n while i < n\n {\n let old_max_here: i64 = max_here;\n let candidate: i64 = old_max_here + nums[i] as i64;\n let old_max_so_far: i64 = max_so_far;\n\n if candidate >= nums[i] as i64 {\n max_here = candidate;\n } else {\n max_here = nums[i] as i64;\n }\n if max_here > max_so_far {\n max_so_far = max_here;\n }\n\n i += 1;\n }\n\n max_so_far as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n s[l] as int + Self::sum_range(s, l + 1, r)\n }\n }\n\n pub fn max_sub_array(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n ensures\n exists |l: int, r: int|\n 0 <= l < r <= nums.len() as int &&\n result as int == #[trigger] Self::sum_range(nums@, l, r),\n forall |l: int, r: int|\n 0 <= l < r <= nums.len() as int ==>\n #[trigger] Self::sum_range(nums@, l, r) <= result as int,\n {\n let n = nums.len();\n\n let mut max_here: i64 = nums[0] as i64;\n let mut max_so_far: i64 = nums[0] as i64;\n\n let mut i: usize = 1;\n\n while i < n\n {\n let old_max_here: i64 = max_here;\n let candidate: i64 = old_max_here + nums[i] as i64;\n let old_max_so_far: i64 = max_so_far;\n\n if candidate >= nums[i] as i64 {\n max_here = candidate;\n } else {\n max_here = nums[i] as i64;\n }\n if max_here > max_so_far {\n max_so_far = max_here;\n }\n\n i += 1;\n }\n\n max_so_far as i32\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(s: Seq, l: int, r: int) -> int\n recommends\n 0 <= l <= r <= s.len(),\n decreases r - l,\n {\n if l >= r {\n 0\n } else {\n s[l] as int + Self::sum_range(s, l + 1, r)\n }\n }\n\n proof fn lemma_sum_range_split(s: Seq, l: int, m: int, r: int)\n requires\n 0 <= l <= m <= r <= s.len(),\n ensures\n Self::sum_range(s, l, r) == Self::sum_range(s, l, m) + Self::sum_range(s, m, r),\n decreases m - l,\n {\n if l < m {\n Self::lemma_sum_range_split(s, l + 1, m, r);\n }\n }\n\n proof fn lemma_sum_range_single(s: Seq, i: int)\n requires\n 0 <= i < s.len(),\n ensures\n Self::sum_range(s, i, i + 1) == s[i] as int,\n {\n assert(Self::sum_range(s, i + 1, i + 1) == 0);\n }\n\n proof fn lemma_sum_range_upper(s: Seq, l: int, r: int)\n requires\n 0 <= l <= r <= s.len(),\n forall |k: int| 0 <= k < s.len() ==> #[trigger] s[k] <= 10_000,\n ensures\n Self::sum_range(s, l, r) <= 10_000 * (r - l),\n decreases r - l,\n {\n if l < r {\n Self::lemma_sum_range_upper(s, l + 1, r);\n assert(10_000 * (r - l) == 10_000 + 10_000 * (r - l - 1)) by(nonlinear_arith);\n }\n }\n\n pub fn max_sub_array(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 <= #[trigger] nums[i] <= 10_000,\n ensures\n exists |l: int, r: int|\n 0 <= l < r <= nums.len() as int &&\n result as int == #[trigger] Self::sum_range(nums@, l, r),\n forall |l: int, r: int|\n 0 <= l < r <= nums.len() as int ==>\n #[trigger] Self::sum_range(nums@, l, r) <= result as int,\n {\n let n = nums.len();\n\n let ghost mut ghost_l: int = 0;\n let ghost mut ghost_best_l: int = 0;\n let ghost mut ghost_best_r: int = 1;\n\n let mut max_here: i64 = nums[0] as i64;\n let mut max_so_far: i64 = nums[0] as i64;\n\n proof {\n Self::lemma_sum_range_single(nums@, 0);\n }\n\n let mut i: usize = 1;\n\n while i < n\n invariant\n 1 <= i <= n,\n n == nums.len() >= 1,\n 1 <= n <= 100_000,\n forall |k: int| 0 <= k < n ==> -10_000 <= #[trigger] nums@[k] <= 10_000,\n 0 <= ghost_l < i,\n 0 <= ghost_best_l < ghost_best_r <= i,\n max_here == Self::sum_range(nums@, ghost_l, i as int),\n forall |j: int| 0 <= j < i as int\n ==> #[trigger] Self::sum_range(nums@, j, i as int) <= max_here,\n max_so_far == Self::sum_range(nums@, ghost_best_l, ghost_best_r),\n forall |j: int, k: int| 0 <= j < k <= i as int\n ==> #[trigger] Self::sum_range(nums@, j, k) <= max_so_far,\n -10_000 <= max_here,\n max_here <= 1_000_000_000,\n -10_000 <= max_so_far,\n max_so_far <= 1_000_000_000,\n decreases n - i,\n {\n let old_max_here: i64 = max_here;\n let candidate: i64 = old_max_here + nums[i] as i64;\n let old_max_so_far: i64 = max_so_far;\n\n if candidate >= nums[i] as i64 {\n max_here = candidate;\n proof {\n Self::lemma_sum_range_split(nums@, ghost_l, i as int, i as int + 1);\n Self::lemma_sum_range_single(nums@, i as int);\n Self::lemma_sum_range_upper(nums@, ghost_l, i as int + 1);\n\n assert forall |j: int| 0 <= j < i as int + 1\n implies #[trigger] Self::sum_range(nums@, j, i as int + 1) <= max_here\n by {\n if j == i as int {\n Self::lemma_sum_range_single(nums@, i as int);\n } else {\n Self::lemma_sum_range_split(nums@, j, i as int, i as int + 1);\n Self::lemma_sum_range_single(nums@, i as int);\n assert(Self::sum_range(nums@, j, i as int) <= old_max_here);\n }\n };\n }\n } else {\n max_here = nums[i] as i64;\n proof {\n ghost_l = i as int;\n Self::lemma_sum_range_single(nums@, i as int);\n assert forall |j: int| 0 <= j < i as int + 1\n implies #[trigger] Self::sum_range(nums@, j, i as int + 1) <= max_here\n by {\n if j == i as int {\n Self::lemma_sum_range_single(nums@, i as int);\n } else {\n Self::lemma_sum_range_split(nums@, j, i as int, i as int + 1);\n Self::lemma_sum_range_single(nums@, i as int);\n }\n };\n }\n }\n if max_here > max_so_far {\n max_so_far = max_here;\n proof {\n ghost_best_l = ghost_l;\n ghost_best_r = i as int + 1;\n }\n }\n\n i += 1;\n }\n\n max_so_far as i32\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc53/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc53/tests/mutated_testcases.jsonl"} {"problem": "lc566", "family": "leetcode", "description": "# Reshape the Matrix\n\nIn MATLAB, there is a handy function called `reshape` which can reshape an `m x n` matrix into a new one with a different size `r x c` keeping its original data.\n\nYou are given an `m x n` matrix `mat` and two integers `r` and `c` representing the number of rows and the number of columns of the wanted reshaped matrix.\n\nThe reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.\n\nIf the `reshape` operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.\n\n## Example 1:\n\n> **Input:** mat = [[1,2],[3,4]], r = 1, c = 4\n> **Output:** [[1,2,3,4]]\n\n\n## Example 2:\n\n> **Input:** mat = [[1,2],[3,4]], r = 2, c = 4\n> **Output:** [[1,2],[3,4]]\n\n## Constraints:\n\n- $m == mat.length$\n\n- $n == mat[i].length$\n\n- $1 <= m, n <= 100$\n\n- $-1000 <= mat[i][j] <= 1000$\n\n- $1 <= r, c <= 300$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn matrix_reshape(mat: Vec>, r: i32, c: i32) -> Vec> {\n \n }\n}\n```\n", "tags": "Easy\nMid Level, Array, Matrix, Simulation\nAcceptance rate: 64.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn matrix_reshape(mat: Vec>, r: i32, c: i32) -> (res: Vec>)\n requires\n 1 <= mat.len() <= 100,\n 1 <= mat[0].len() <= 100,\n 1 <= r <= 300,\n 1 <= c <= 300,\n forall |k: int| 0 <= k < mat.len() ==> #[trigger] mat[k].len() == mat[0].len(),\n forall |i:int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==> -1_000 <= #[trigger] mat[i][j] <= 1_000, \n mat.len() * mat[0].len() <= usize::MAX,\n r * c <= usize::MAX,\n ensures\n ({\n let m = mat.len();\n let n = mat[0].len();\n if m * n != r as usize * c as usize {\n res@ =~~= mat@\n } else {\n res.len() == r as int\n && (forall |i: int| 0 <= i < r ==> #[trigger] res[i].len() == c as int)\n && (forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==>\n res[((i * n + j) / c as int) as int][((i * n + j) % c as int) as int] == mat[i][j])\n }\n }),\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn matrix_reshape(mat: Vec>, r: i32, c: i32) -> Vec>\n {\n let m = mat.len();\n let n = mat[0].len();\n\n if m * n != r as usize * c as usize {\n return mat;\n }\n\n let mut result: Vec> = Vec::new();\n let mut ri: usize = 0;\n while ri < r as usize\n {\n let mut row: Vec = Vec::new();\n let mut ci: usize = 0;\n while ci < c as usize\n {\n row.push(0i32);\n ci += 1;\n }\n result.push(row);\n ri += 1;\n }\n\n let mut i: usize = 0;\n while i < m\n {\n let mut j: usize = 0;\n while j < n\n {\n let flat: usize = i * n + j;\n let new_row: usize = flat / c as usize;\n let new_col: usize = flat % c as usize;\n let val = mat[i][j];\n let mut row = result[new_row].clone();\n row.set(new_col, val);\n result.set(new_row, row);\n j += 1;\n }\n i += 1;\n }\n\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn matrix_reshape(mat: Vec>, r: i32, c: i32) -> (res: Vec>)\n requires\n 1 <= mat.len() <= 100,\n 1 <= mat[0].len() <= 100,\n 1 <= r <= 300,\n 1 <= c <= 300,\n forall |k: int| 0 <= k < mat.len() ==> #[trigger] mat[k].len() == mat[0].len(),\n forall |i:int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==> -1_000 <= #[trigger] mat[i][j] <= 1_000,\n mat.len() * mat[0].len() <= usize::MAX,\n r * c <= usize::MAX,\n ensures\n ({\n let m = mat.len();\n let n = mat[0].len();\n if m * n != r as usize * c as usize {\n res@ =~~= mat@\n } else {\n res.len() == r as int\n && (forall |i: int| 0 <= i < r ==> #[trigger] res[i].len() == c as int)\n && (forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==>\n res[((i * n + j) / c as int) as int][((i * n + j) % c as int) as int] == mat[i][j])\n }\n }),\n {\n let m = mat.len();\n let n = mat[0].len();\n\n if m * n != r as usize * c as usize {\n return mat;\n }\n\n let mut result: Vec> = Vec::new();\n let mut ri: usize = 0;\n while ri < r as usize\n {\n let mut row: Vec = Vec::new();\n let mut ci: usize = 0;\n while ci < c as usize\n {\n row.push(0i32);\n ci += 1;\n }\n result.push(row);\n ri += 1;\n }\n\n let mut i: usize = 0;\n while i < m\n {\n let mut j: usize = 0;\n while j < n\n {\n let flat: usize = i * n + j;\n let new_row: usize = flat / c as usize;\n let new_col: usize = flat % c as usize;\n\n let val = mat[i][j];\n let mut row = result[new_row].clone();\n row.set(new_col, val);\n result.set(new_row, row);\n\n j += 1;\n }\n i += 1;\n }\n\n result\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn matrix_reshape(mat: Vec>, r: i32, c: i32) -> (res: Vec>)\n requires\n 1 <= mat.len() <= 100,\n 1 <= mat[0].len() <= 100,\n 1 <= r <= 300,\n 1 <= c <= 300,\n forall |k: int| 0 <= k < mat.len() ==> #[trigger] mat[k].len() == mat[0].len(),\n forall |i:int, j: int| 0 <= i < mat.len() && 0 <= j < mat[i].len() ==> -1_000 <= #[trigger] mat[i][j] <= 1_000, \n mat.len() * mat[0].len() <= usize::MAX,\n r * c <= usize::MAX,\n ensures\n ({\n let m = mat.len();\n let n = mat[0].len();\n if m * n != r as usize * c as usize {\n res@ =~~= mat@\n } else {\n res.len() == r as int\n && (forall |i: int| 0 <= i < r ==> #[trigger] res[i].len() == c as int)\n && (forall |i: int, j: int| 0 <= i < mat.len() && 0 <= j < mat[0].len() ==>\n res[((i * n + j) / c as int) as int][((i * n + j) % c as int) as int] == mat[i][j])\n }\n }),\n {\n let m = mat.len();\n let n = mat[0].len();\n\n if m * n != r as usize * c as usize {\n return mat;\n }\n\n let mut result: Vec> = Vec::new();\n let mut ri: usize = 0;\n while ri < r as usize\n invariant\n r >= 0,\n c >= 0,\n ri <= r as usize,\n result.len() == ri,\n forall |i: int| 0 <= i < ri ==> #[trigger] result[i].len() == c as int,\n decreases r as int - ri as int,\n {\n let mut row: Vec = Vec::new();\n let mut ci: usize = 0;\n while ci < c as usize\n invariant\n c >= 0,\n ci <= c as usize,\n row.len() == ci,\n decreases c as int - ci as int,\n {\n row.push(0i32);\n ci += 1;\n }\n result.push(row);\n ri += 1;\n }\n\n let mut i: usize = 0;\n while i < m\n invariant\n m == mat.len(),\n n == mat[0].len(),\n m > 0,\n n > 0,\n r > 0,\n c > 0,\n m * n == r as usize * c as usize,\n m * n <= usize::MAX,\n forall |k: int| 0 <= k < mat.len() ==> #[trigger] mat[k].len() == n,\n i <= m,\n result.len() == r as int,\n forall |k: int| 0 <= k < r ==> #[trigger] result[k].len() == c as int,\n forall |ii: int, jj: int|\n 0 <= ii < i && 0 <= jj < n ==>\n result[(ii * n + jj) / c as int][(ii * n + jj) % c as int] == mat[ii][jj],\n decreases m - i,\n {\n let mut j: usize = 0;\n while j < n\n invariant\n m == mat.len(),\n n == mat[0].len(),\n m > 0,\n n > 0,\n r > 0,\n c > 0,\n m * n == r as usize * c as usize,\n m * n <= usize::MAX,\n forall |k: int| 0 <= k < mat.len() ==> #[trigger] mat[k].len() == n,\n i < m,\n j <= n,\n result.len() == r as int,\n forall |k: int| 0 <= k < r ==> #[trigger] result[k].len() == c as int,\n forall |ii: int, jj: int|\n 0 <= ii < i && 0 <= jj < n ==>\n result[(ii * n + jj) / c as int][(ii * n + jj) % c as int] == mat[ii][jj],\n forall |jj: int|\n 0 <= jj < j ==>\n result[(i * n + jj) / c as int][(i * n + jj) % c as int] == mat[i as int][jj],\n decreases n - j,\n {\n proof {\n assert(i * n + j < m * n) by (nonlinear_arith)\n requires i < m, j < n, n > 0, m > 0;\n }\n\n let flat: usize = i * n + j;\n let new_row: usize = flat / c as usize;\n let new_col: usize = flat % c as usize;\n\n proof {\n assert(flat < r as usize * c as usize);\n assert(new_row < r as usize) by (nonlinear_arith)\n requires\n flat < r as usize * c as usize,\n c as usize > 0usize,\n new_row == flat / c as usize;\n assert(new_col < c as usize);\n assert(mat[i as int].len() == n);\n }\n\n let ghost result_snap = result@;\n let val = mat[i][j];\n let mut row = result[new_row].clone();\n row.set(new_col, val);\n result.set(new_row, row);\n\n proof {\n assert forall |ii: int, jj: int|\n 0 <= ii < i && 0 <= jj < n\n implies result@[(ii * n + jj) / c as int][(ii * n + jj) % c as int] == mat[ii][jj]\n by {\n let r_idx = (ii * n + jj) / c as int;\n let c_idx = (ii * n + jj) % c as int;\n\n assert(ii * (n as int) + jj < (m as int) * (n as int)) by (nonlinear_arith)\n requires 0 <= ii, ii < (i as int), (i as int) < (m as int),\n 0 <= jj, jj < (n as int), (n as int) > 0;\n assert(r_idx < r as int) by (nonlinear_arith)\n requires\n r_idx == (ii * n as int + jj) / c as int,\n 0 <= ii * n as int + jj,\n ii * n as int + jj < m as int * n as int,\n m as int * n as int == r as int * c as int,\n c as int > 0;\n\n if r_idx != new_row as int {\n assert(result@[r_idx] == result_snap[r_idx]);\n assert(c_idx < result@[r_idx].len() as int);\n assert(c_idx < result_snap[r_idx].len() as int);\n assert(result@[r_idx][c_idx] == result_snap[r_idx][c_idx]);\n } else {\n assert(ii * n + jj < i as int * n + j as int) by (nonlinear_arith)\n requires ii < i as int, 0 <= jj, jj < n as int, 0 <= j as int, n > 0;\n assert(c_idx != new_col as int) by (nonlinear_arith)\n requires\n 0 <= ii * n + jj,\n ii * n + jj < i as int * n as int + j as int,\n flat as int == i as int * n as int + j as int,\n r_idx == (ii * n + jj) / c as int,\n c_idx == (ii * n + jj) % c as int,\n new_row as int == flat as int / c as int,\n new_col as int == flat as int % c as int,\n r_idx == new_row as int,\n c as int > 0;\n assert(result@[new_row as int][c_idx] == result_snap[new_row as int][c_idx]);\n }\n };\n\n assert forall |jj: int|\n 0 <= jj < j\n implies result@[(i as int * n as int + jj) / c as int][(i as int * n as int + jj) % c as int] == mat[i as int][jj]\n by {\n let r_idx = (i as int * n as int + jj) / c as int;\n let c_idx = (i as int * n as int + jj) % c as int;\n\n assert((i as int) * (n as int) + jj < (m as int) * (n as int)) by (nonlinear_arith)\n requires 0 <= i as int, (i as int) < (m as int), 0 <= jj, jj < n as int, n as int > 0;\n assert(r_idx < r as int) by (nonlinear_arith)\n requires\n r_idx == (i as int * n as int + jj) / c as int,\n 0 <= i as int * n as int + jj,\n i as int * n as int + jj < m as int * n as int,\n m as int * n as int == r as int * c as int,\n c as int > 0;\n\n if r_idx != new_row as int {\n assert(result@[r_idx] == result_snap[r_idx]);\n assert(c_idx < result@[r_idx].len() as int);\n assert(c_idx < result_snap[r_idx].len() as int);\n assert(result@[r_idx][c_idx] == result_snap[r_idx][c_idx]);\n } else {\n assert(i as int * n as int + jj < i as int * n as int + j as int) by (nonlinear_arith)\n requires jj < j as int, n as int >= 0;\n assert(c_idx != new_col as int) by (nonlinear_arith)\n requires\n 0 <= i as int * n as int + jj,\n i as int * n as int + jj < i as int * n as int + j as int,\n flat as int == i as int * n as int + j as int,\n r_idx == (i as int * n as int + jj) / c as int,\n c_idx == (i as int * n as int + jj) % c as int,\n new_row as int == flat as int / c as int,\n new_col as int == flat as int % c as int,\n r_idx == new_row as int,\n c as int > 0;\n assert(result@[new_row as int][c_idx] == result_snap[new_row as int][c_idx]);\n }\n };\n }\n\n j += 1;\n }\n i += 1;\n }\n\n result\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc566/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc566/tests/mutated_testcases.jsonl"} {"problem": "lc575", "family": "leetcode", "description": "# Distribute Candies\n\nAlice has `n` candies, where the `i`th candy is of type `candyType[i]`. Alice\nnoticed that she started to gain weight, so she visited a doctor. The doctor\nadvised Alice to only eat `n / 2` of the candies she has (`n` is always even).\nAlice likes her candies very much, and she wants to eat the maximum number of\ndifferent types of candies while still following the doctor's advice.\n\nGiven the integer array `candyType` of length `n`, return the **maximum** number\nof different types of candies she can eat if she only eats `n / 2` of them.\n\n## Example 1:\n\n> **Input:** candyType = [1,1,2,2,3,3]\n> **Output:** 3\n> **Explanation:** Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.\n\n## Example 2:\n\n> **Input:** candyType = [1,1,2,3]\n> **Output:** 2\n> **Explanation:** Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.\n\n## Example 3:\n\n> **Input:** candyType = [6,6,6,6]\n> **Output:** 1\n> **Explanation:** Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.\n\n## Constraints:\n\n- `n == candyType.length`\n- `2 <= n <= 10^4`\n- `n` is even\n- `-10^5 <= candyType[i] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn distribute_candies(candy_type: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nSenior, Array, Hash Table\nAcceptance rate: 70.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn has_val(s: Seq, v: i32) -> bool {\n exists|i: int| 0 <= i < s.len() && s[i] == v\n }\n\n pub open spec fn num_distinct(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() == 0 {\n 0int\n } else {\n let rest = Self::num_distinct(s.drop_last());\n if Self::has_val(s.drop_last(), s.last()) {\n rest\n } else {\n rest + 1\n }\n }\n }\n\n pub fn distribute_candies(candy_type: Vec) -> (res: i32)\n requires\n candy_type.len() % 2 == 0,\n 2 <= candy_type.len() <= 10_000,\n forall|i: int| 0 <= i < candy_type.len() ==>\n -100_000 <= #[trigger] candy_type[i] <= 100_000,\n ensures\n res == if Self::num_distinct(candy_type@) <= candy_type.len() / 2 {\n Self::num_distinct(candy_type@) as i32\n } else {\n (candy_type.len() / 2) as i32\n },\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn distribute_candies(candy_type: Vec) -> i32 {\n let mut seen: Vec = Vec::new();\n let mut fill_idx = 0usize;\n while fill_idx < 200001usize {\n seen.push(false);\n fill_idx += 1;\n }\n let mut distinct = 0i32;\n let n = candy_type.len();\n let mut i = 0usize;\n while i < n {\n let ci = candy_type[i];\n let offset = (ci as i64 + 100_000i64) as usize;\n if !seen[offset] {\n seen[offset] = true;\n distinct += 1;\n }\n i += 1;\n }\n let half = (n / 2) as i32;\n if distinct <= half { distinct } else { half }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn has_val(s: Seq, v: i32) -> bool {\n exists|i: int| 0 <= i < s.len() && s[i] == v\n }\n\n pub open spec fn num_distinct(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() == 0 {\n 0int\n } else {\n let rest = Self::num_distinct(s.drop_last());\n if Self::has_val(s.drop_last(), s.last()) {\n rest\n } else {\n rest + 1\n }\n }\n }\n\n pub fn distribute_candies(candy_type: Vec) -> (res: i32)\n requires\n candy_type.len() % 2 == 0,\n 2 <= candy_type.len() <= 10_000,\n forall|i: int| 0 <= i < candy_type.len() ==>\n -100_000 <= #[trigger] candy_type[i] <= 100_000,\n ensures\n res == if Self::num_distinct(candy_type@) <= candy_type.len() / 2 {\n Self::num_distinct(candy_type@) as i32\n } else {\n (candy_type.len() / 2) as i32\n },\n {\n let mut seen: Vec = Vec::new();\n let mut fill_idx = 0usize;\n while fill_idx < 200001usize {\n seen.push(false);\n fill_idx += 1;\n }\n let mut distinct = 0i32;\n let n = candy_type.len();\n let mut i = 0usize;\n while i < n {\n let ci = candy_type[i];\n let offset = (ci as i64 + 100_000i64) as usize;\n if !seen[offset] {\n seen.set(offset, true);\n distinct += 1;\n }\n i += 1;\n }\n let half = (n / 2) as i32;\n if distinct <= half { distinct } else { half }\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn has_val(s: Seq, v: i32) -> bool {\n exists|i: int| 0 <= i < s.len() && s[i] == v\n }\n\n pub open spec fn num_distinct(s: Seq) -> int\n decreases s.len(),\n {\n if s.len() == 0 {\n 0int\n } else {\n let rest = Self::num_distinct(s.drop_last());\n if Self::has_val(s.drop_last(), s.last()) {\n rest\n } else {\n rest + 1\n }\n }\n }\n\n proof fn lemma_has_val_push(s: Seq, v: i32, w: i32)\n ensures Self::has_val(s.push(v), w) == (Self::has_val(s, w) || w == v),\n {\n if Self::has_val(s, w) {\n let wit = choose|idx: int| 0 <= idx < s.len() && s[idx] == w;\n assert(0 <= wit < s.push(v).len() && s.push(v)[wit] == w);\n }\n if w == v {\n assert(0 <= s.len() < s.push(v).len() && s.push(v)[s.len() as int] == w);\n }\n if Self::has_val(s.push(v), w) {\n let wit = choose|idx: int| 0 <= idx < s.push(v).len() && s.push(v)[idx] == w;\n if wit < s.len() {\n assert(s[wit] == w);\n }\n }\n }\n\n proof fn lemma_distinct_push(s: Seq, v: i32)\n ensures Self::num_distinct(s.push(v)) ==\n Self::num_distinct(s) + (if Self::has_val(s, v) { 0int } else { 1int }),\n {\n assert(s.push(v).drop_last() =~= s);\n assert(s.push(v).last() == v);\n }\n\n proof fn lemma_distinct_nonneg(s: Seq)\n ensures Self::num_distinct(s) >= 0,\n decreases s.len(),\n {\n if s.len() > 0 {\n Self::lemma_distinct_nonneg(s.drop_last());\n }\n }\n\n proof fn lemma_distinct_le_len(s: Seq)\n ensures Self::num_distinct(s) <= s.len(),\n decreases s.len(),\n {\n if s.len() > 0 {\n Self::lemma_distinct_le_len(s.drop_last());\n }\n }\n\n pub fn distribute_candies(candy_type: Vec) -> (res: i32)\n requires\n candy_type.len() % 2 == 0,\n 2 <= candy_type.len() <= 10_000,\n forall|i: int| 0 <= i < candy_type.len() ==>\n -100_000 <= #[trigger] candy_type[i] <= 100_000,\n ensures\n res == if Self::num_distinct(candy_type@) <= candy_type.len() / 2 {\n Self::num_distinct(candy_type@) as i32\n } else {\n (candy_type.len() / 2) as i32\n },\n {\n let mut seen: Vec = Vec::new();\n let mut fill_idx = 0usize;\n while fill_idx < 200001usize\n invariant\n fill_idx <= 200001,\n seen@.len() == fill_idx,\n forall|k: int| 0 <= k < fill_idx ==> seen@[k] == false,\n decreases 200001usize - fill_idx,\n {\n seen.push(false);\n fill_idx += 1;\n }\n\n let mut distinct = 0i32;\n let n = candy_type.len();\n let ghost ct = candy_type@;\n let mut i = 0usize;\n\n assert(Self::num_distinct(ct.take(0int)) == 0) by {\n assert(ct.take(0int) =~= Seq::::empty());\n assert(Seq::::empty().len() == 0);\n reveal_with_fuel(Solution::num_distinct, 1);\n };\n\n while i < n\n invariant\n i <= n,\n n == candy_type.len(),\n ct == candy_type@,\n seen@.len() == 200001,\n n <= 10_000,\n 0 <= distinct,\n distinct <= i as i32,\n distinct == Self::num_distinct(ct.take(i as int)),\n forall|k: int| 0 <= k < candy_type.len() ==>\n -100_000 <= #[trigger] candy_type@[k] <= 100_000,\n forall|j: int| 0 <= j < 200001 ==> (seen@[j] == Self::has_val(ct.take(i as int), (j - 100_000) as i32)),\n decreases n - i,\n {\n let ci = candy_type[i];\n let offset = (ci as i64 + 100_000i64) as usize;\n\n assert(0 <= offset < 200001) by {\n assert(-100_000 <= ci as int <= 100_000);\n assert(offset as int == ci as int + 100_000);\n };\n\n let ghost sub_prev = ct.take(i as int);\n let ghost sub_next = ct.take(i as int + 1);\n assert(sub_next =~= sub_prev.push(ci));\n\n if !seen[offset] {\n let ghost old_seen_seq: Seq = seen@;\n proof {\n assert(!Self::has_val(sub_prev, ci)) by {\n assert(seen@[offset as int] == false);\n assert(offset as int == ci as int + 100_000);\n assert((offset as int - 100_000) as i32 == ci) by {\n assert(offset as int - 100_000 == ci as int);\n };\n };\n Self::lemma_distinct_push(sub_prev, ci);\n assert(Self::num_distinct(sub_next) == Self::num_distinct(sub_prev) + 1);\n }\n seen.set(offset, true);\n distinct += 1;\n proof {\n assert forall|j: int| 0 <= j < 200001 implies\n seen@[j] == Self::has_val(sub_next, (j - 100_000) as i32)\n by {\n if j == offset as int {\n assert(seen@[j] == true);\n assert(Self::has_val(sub_next, ci)) by {\n assert(sub_next.len() > 0);\n assert(sub_next.last() == ci);\n };\n assert((j - 100_000) as i32 == ci) by {\n assert(j == ci as int + 100_000);\n };\n } else {\n assert(seen@[j] == old_seen_seq[j]);\n let w = (j - 100_000) as i32;\n assert(w != ci) by {\n assert(j != offset as int);\n assert(j != ci as int + 100_000);\n };\n Self::lemma_has_val_push(sub_prev, ci, w);\n assert(Self::has_val(sub_next, w) == Self::has_val(sub_prev, w));\n }\n };\n }\n } else {\n proof {\n assert(Self::has_val(sub_prev, ci)) by {\n assert(seen@[offset as int] == true);\n assert((offset as int - 100_000) as i32 == ci) by {\n assert(offset as int - 100_000 == ci as int);\n };\n };\n Self::lemma_distinct_push(sub_prev, ci);\n assert(Self::num_distinct(sub_next) == Self::num_distinct(sub_prev));\n assert forall|j: int| 0 <= j < 200001 implies\n seen@[j] == Self::has_val(sub_next, (j - 100_000) as i32)\n by {\n let w = (j - 100_000) as i32;\n Self::lemma_has_val_push(sub_prev, ci, w);\n assert(Self::has_val(sub_next, w) == (Self::has_val(sub_prev, w) || w == ci));\n assert(Self::has_val(sub_prev, ci));\n if w == ci {\n assert(Self::has_val(sub_next, w));\n assert(seen@[j] == Self::has_val(sub_prev, w));\n } else {\n assert(Self::has_val(sub_next, w) == Self::has_val(sub_prev, w));\n }\n };\n }\n }\n i += 1;\n }\n\n proof {\n assert(ct.take(n as int) =~= ct);\n Self::lemma_distinct_nonneg(ct);\n Self::lemma_distinct_le_len(ct);\n }\n\n let half = (n / 2) as i32;\n if distinct <= half { distinct } else { half }\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc575/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc575/tests/mutated_testcases.jsonl"} {"problem": "lc605", "family": "leetcode", "description": "# Can Place Flowers\n\nYou have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in **adjacent** plots.\n\nGiven an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return `true` *if* `n` *new flowers can be planted in the* `flowerbed` *without violating the no-adjacent-flowers rule and* `false` *otherwise*.\n\n## Example 1:\n\n> **Input:** flowerbed = [1,0,0,0,1], n = 1\n> **Output:** true\n\n## Example 2:\n\n> **Input:** flowerbed = [1,0,0,0,1], n = 2\n> **Output:** false\n\n## Constraints:\n\n- $1 <= flowerbed.length <= 2 * 10^4$\n\n- `flowerbed[i]` is `0` or `1`.\n\n- There are no two adjacent flowers in `flowerbed`.\n\n- $0 <= n <= flowerbed.length$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn can_place_flowers(flowerbed: Vec, n: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nSenior, Array, Greedy\nAcceptance rate: 29.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_additional_from(flowerbed: Seq, i: int, prev_planted: bool) -> int\n decreases flowerbed.len() - i,\n {\n if i >= flowerbed.len() {\n 0\n } else if flowerbed[i] == 1 {\n Self::max_additional_from(flowerbed, i + 1, true)\n } else {\n let next_empty = i + 1 >= flowerbed.len() || flowerbed[i + 1] == 0;\n if !prev_planted && next_empty {\n 1 + Self::max_additional_from(flowerbed, i + 1, true)\n } else {\n Self::max_additional_from(flowerbed, i + 1, false)\n }\n }\n }\n\n pub fn can_place_flowers(flowerbed: Vec, n: i32) -> (res: bool)\n requires\n 1 <= flowerbed.len() <= 20_000,\n forall |i: int| 0 <= i < flowerbed.len() ==> (#[trigger] flowerbed[i] == 0 || flowerbed[i] == 1),\n forall |i: int| 0 <= i < flowerbed.len() - 1 ==> !(#[trigger] flowerbed[i] == 1 && flowerbed[i + 1] == 1),\n 0 <= n <= flowerbed.len(),\n ensures\n res == (n as int <= Self::max_additional_from(flowerbed@, 0, false)),\n {\n \n }\n}\n\n}\n", "code": "impl Solution {\n pub fn can_place_flowers(flowerbed: Vec, n: i32) -> bool\n {\n let len = flowerbed.len();\n let mut i: usize = 0;\n let mut count: usize = 0;\n let mut prev_planted = false;\n\n while i < len\n {\n if flowerbed[i] == 1 {\n prev_planted = true;\n } else {\n let next_empty = if i + 1 < len { flowerbed[i + 1] == 0 } else { true };\n if !prev_planted && next_empty {\n count = count + 1;\n prev_planted = true;\n } else {\n prev_planted = false;\n }\n }\n i = i + 1;\n }\n\n count as i32 >= n\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_additional_from(flowerbed: Seq, i: int, prev_planted: bool) -> int\n decreases flowerbed.len() - i,\n {\n if i >= flowerbed.len() {\n 0\n } else if flowerbed[i] == 1 {\n Self::max_additional_from(flowerbed, i + 1, true)\n } else {\n let next_empty = i + 1 >= flowerbed.len() || flowerbed[i + 1] == 0;\n if !prev_planted && next_empty {\n 1 + Self::max_additional_from(flowerbed, i + 1, true)\n } else {\n Self::max_additional_from(flowerbed, i + 1, false)\n }\n }\n }\n\n pub fn can_place_flowers(flowerbed: Vec, n: i32) -> (res: bool)\n requires\n 1 <= flowerbed.len() <= 20_000,\n forall |i: int| 0 <= i < flowerbed.len() ==> (#[trigger] flowerbed[i] == 0 || flowerbed[i] == 1),\n forall |i: int| 0 <= i < flowerbed.len() - 1 ==> !(#[trigger] flowerbed[i] == 1 && flowerbed[i + 1] == 1),\n 0 <= n <= flowerbed.len(),\n ensures\n res == (n as int <= Self::max_additional_from(flowerbed@, 0, false)),\n {\n let len = flowerbed.len();\n let mut i: usize = 0;\n let mut count: usize = 0;\n let mut prev_planted = false;\n\n while i < len\n {\n if flowerbed[i] == 1 {\n prev_planted = true;\n } else {\n let next_empty = if i + 1 < len { flowerbed[i + 1] == 0 } else { true };\n if !prev_planted && next_empty {\n count = count + 1;\n prev_planted = true;\n } else {\n prev_planted = false;\n }\n }\n i = i + 1;\n }\n\n count as i32 >= n\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn max_additional_from(flowerbed: Seq, i: int, prev_planted: bool) -> int\n decreases flowerbed.len() - i,\n {\n if i >= flowerbed.len() {\n 0\n } else if flowerbed[i] == 1 {\n Self::max_additional_from(flowerbed, i + 1, true)\n } else {\n let next_empty = i + 1 >= flowerbed.len() || flowerbed[i + 1] == 0;\n if !prev_planted && next_empty {\n 1 + Self::max_additional_from(flowerbed, i + 1, true)\n } else {\n Self::max_additional_from(flowerbed, i + 1, false)\n }\n }\n }\n\n pub fn can_place_flowers(flowerbed: Vec, n: i32) -> (res: bool)\n requires\n 1 <= flowerbed.len() <= 20_000,\n forall |i: int| 0 <= i < flowerbed.len() ==> (#[trigger] flowerbed[i] == 0 || flowerbed[i] == 1),\n forall |i: int| 0 <= i < flowerbed.len() - 1 ==> !(#[trigger] flowerbed[i] == 1 && flowerbed[i + 1] == 1),\n 0 <= n <= flowerbed.len(),\n ensures\n res == (n as int <= Self::max_additional_from(flowerbed@, 0, false)),\n {\n let len = flowerbed.len();\n let mut i: usize = 0;\n let mut count: usize = 0;\n let mut prev_planted = false;\n\n while i < len\n invariant\n 1 <= flowerbed.len() <= 20_000,\n forall |k: int| 0 <= k < flowerbed.len() ==> (#[trigger] flowerbed[k] == 0 || flowerbed[k] == 1),\n forall |k: int| 0 <= k < flowerbed.len() - 1 ==> !(#[trigger] flowerbed[k] == 1 && flowerbed[k + 1] == 1),\n 0 <= n <= flowerbed.len(),\n len == flowerbed.len(),\n 0 <= i <= len,\n count <= i,\n count as int + Self::max_additional_from(flowerbed@, i as int, prev_planted) == Self::max_additional_from(flowerbed@, 0, false),\n decreases len - i,\n {\n if flowerbed[i] == 1 {\n prev_planted = true;\n } else {\n let next_empty = if i + 1 < len { flowerbed[i + 1] == 0 } else { true };\n if !prev_planted && next_empty {\n count = count + 1;\n prev_planted = true;\n } else {\n prev_planted = false;\n }\n }\n i = i + 1;\n }\n\n count as i32 >= n\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc605/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc605/tests/mutated_testcases.jsonl"} {"problem": "lc645", "family": "leetcode", "description": "# Set Mismatch\n\nYou have a set of integers `s`, which originally contains all the numbers from `1` to `n`. Unfortunately, due to some error, one of the numbers in `s` got duplicated to another number in the set, which results in **repetition of one** number and **loss of another** number.\n\nYou are given an integer array `nums` representing the data status of this set after the error.\n\nFind the number that occurs twice and the number that is missing and return them in the form of an array.\n\n## Example 1:\n\n> **Input:** nums = [1,2,2,4]\n> **Output:** [2,3]\n\n## Example 2:\n\n> **Input:** nums = [1,1]\n> **Output:** [1,2]\n\n## Constraints:\n\n- $2 <= nums.length <= 10^4$\n\n- $1 <= nums[i] <= nums.length$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn find_error_nums(nums: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Easy\nArray, Hash Table, Bit Manipulation, Sorting\nAcceptance rate: 42.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn appears_in(s: Seq, val: i32) -> bool {\n exists |j: int| 0 <= j < s.len() && #[trigger] s[j] == val\n }\n\n pub open spec fn appears_twice(s: Seq, val: i32) -> bool {\n exists |j1: int, j2: int| 0 <= j1 < j2 < s.len()\n && #[trigger] s[j1] == val && #[trigger] s[j2] == val\n }\n\n pub fn find_error_nums(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n exists |d: int| 1 <= d <= nums.len() && #[trigger] Self::appears_twice(nums@, d as i32),\n exists |m: int| 1 <= m <= nums.len() && !#[trigger] Self::appears_in(nums@, m as i32),\n forall |v1: i32, v2: i32|\n Self::appears_twice(nums@, v1) && Self::appears_twice(nums@, v2) ==> v1 == v2,\n forall |v1: int, v2: int|\n 1 <= v1 <= nums.len() && 1 <= v2 <= nums.len()\n && !#[trigger] Self::appears_in(nums@, v1 as i32) && !#[trigger] Self::appears_in(nums@, v2 as i32)\n ==> v1 == v2,\n ensures\n result.len() == 2,\n 1 <= result[0] <= nums.len() as i32,\n 1 <= result[1] <= nums.len() as i32,\n result[0] != result[1],\n Self::appears_twice(nums@, result[0]),\n !Self::appears_in(nums@, result[1]),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn find_error_nums(nums: Vec) -> Vec {\n let n = nums.len();\n let mut dup: i32 = 1;\n let mut miss: i32 = 1;\n let mut found_dup: bool = false;\n let mut found_miss: bool = false;\n let mut k: usize = 1;\n while k <= n {\n let mut seen_first: bool = false;\n let mut seen_second: bool = false;\n let mut i: usize = 0;\n while i < n {\n if nums[i] == k as i32 {\n if seen_first {\n seen_second = true;\n }\n seen_first = true;\n }\n i = i + 1;\n }\n if seen_second && !found_dup {\n dup = k as i32;\n found_dup = true;\n }\n if !seen_first && !found_miss {\n miss = k as i32;\n found_miss = true;\n }\n k = k + 1;\n }\n let mut result = Vec::new();\n result.push(dup);\n result.push(miss);\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn appears_in(s: Seq, val: i32) -> bool {\n exists |j: int| 0 <= j < s.len() && #[trigger] s[j] == val\n }\n\n pub open spec fn appears_twice(s: Seq, val: i32) -> bool {\n exists |j1: int, j2: int| 0 <= j1 < j2 < s.len()\n && #[trigger] s[j1] == val && #[trigger] s[j2] == val\n }\n\n pub fn find_error_nums(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n exists |d: int| 1 <= d <= nums.len() && #[trigger] Self::appears_twice(nums@, d as i32),\n exists |m: int| 1 <= m <= nums.len() && !#[trigger] Self::appears_in(nums@, m as i32),\n forall |v1: i32, v2: i32|\n Self::appears_twice(nums@, v1) && Self::appears_twice(nums@, v2) ==> v1 == v2,\n forall |v1: int, v2: int|\n 1 <= v1 <= nums.len() && 1 <= v2 <= nums.len()\n && !#[trigger] Self::appears_in(nums@, v1 as i32) && !#[trigger] Self::appears_in(nums@, v2 as i32)\n ==> v1 == v2,\n ensures\n result.len() == 2,\n 1 <= result[0] <= nums.len() as i32,\n 1 <= result[1] <= nums.len() as i32,\n result[0] != result[1],\n Self::appears_twice(nums@, result[0]),\n !Self::appears_in(nums@, result[1]),\n {\n let n = nums.len();\n let mut dup: i32 = 1;\n let mut miss: i32 = 1;\n let mut found_dup: bool = false;\n let mut found_miss: bool = false;\n let mut k: usize = 1;\n\n while k <= n {\n let mut seen_first: bool = false;\n let mut first_idx: usize = 0;\n let mut seen_second: bool = false;\n let mut i: usize = 0;\n\n while i < n {\n if nums[i] == k as i32 {\n if seen_first {\n seen_second = true;\n } else {\n first_idx = i;\n }\n seen_first = true;\n }\n i = i + 1;\n }\n\n if seen_second && !found_dup {\n dup = k as i32;\n found_dup = true;\n }\n if !seen_first && !found_miss {\n miss = k as i32;\n found_miss = true;\n }\n\n k = k + 1;\n }\n\n let mut result = Vec::new();\n result.push(dup);\n result.push(miss);\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn appears_in(s: Seq, val: i32) -> bool {\n exists |j: int| 0 <= j < s.len() && #[trigger] s[j] == val\n }\n\n pub open spec fn appears_twice(s: Seq, val: i32) -> bool {\n exists |j1: int, j2: int| 0 <= j1 < j2 < s.len()\n && #[trigger] s[j1] == val && #[trigger] s[j2] == val\n }\n\n pub fn find_error_nums(nums: Vec) -> (result: Vec)\n requires\n 2 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> 1 <= #[trigger] nums[i] <= nums.len(),\n exists |d: int| 1 <= d <= nums.len() && #[trigger] Self::appears_twice(nums@, d as i32),\n exists |m: int| 1 <= m <= nums.len() && !#[trigger] Self::appears_in(nums@, m as i32),\n forall |v1: i32, v2: i32|\n Self::appears_twice(nums@, v1) && Self::appears_twice(nums@, v2) ==> v1 == v2,\n forall |v1: int, v2: int|\n 1 <= v1 <= nums.len() && 1 <= v2 <= nums.len()\n && !#[trigger] Self::appears_in(nums@, v1 as i32) && !#[trigger] Self::appears_in(nums@, v2 as i32)\n ==> v1 == v2,\n ensures\n result.len() == 2,\n 1 <= result[0] <= nums.len() as i32,\n 1 <= result[1] <= nums.len() as i32,\n result[0] != result[1],\n Self::appears_twice(nums@, result[0]),\n !Self::appears_in(nums@, result[1]),\n {\n let n = nums.len();\n let mut dup: i32 = 1;\n let mut miss: i32 = 1;\n let mut found_dup: bool = false;\n let mut found_miss: bool = false;\n let mut k: usize = 1;\n\n while k <= n\n invariant\n 1 <= k <= n + 1,\n n == nums.len(),\n 2 <= n <= 10_000,\n forall |i: int| 0 <= i < n ==> 1 <= #[trigger] nums[i] <= n as i32,\n exists |d: int| 1 <= d <= n && #[trigger] Self::appears_twice(nums@, d as i32),\n exists |m: int| 1 <= m <= n && !#[trigger] Self::appears_in(nums@, m as i32),\n 1 <= dup <= n as i32,\n 1 <= miss <= n as i32,\n found_dup ==> Self::appears_twice(nums@, dup),\n found_miss ==> !Self::appears_in(nums@, miss),\n !found_dup ==> forall |d: int| 1 <= d < k as int ==> !#[trigger] Self::appears_twice(nums@, d as i32),\n !found_miss ==> forall |m: int| 1 <= m < k as int ==> #[trigger] Self::appears_in(nums@, m as i32),\n decreases n + 1 - k,\n {\n let mut seen_first: bool = false;\n let mut first_idx: usize = 0;\n let mut seen_second: bool = false;\n let mut i: usize = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 1 <= k <= n,\n 0 <= first_idx < n,\n forall |j: int| 0 <= j < n ==> 1 <= #[trigger] nums[j] <= n as i32,\n !seen_first ==> forall |j: int| 0 <= j < i as int ==> nums[j] != k as i32,\n seen_first ==> (first_idx < i && nums[first_idx as int] == k as i32),\n seen_first && !seen_second ==> forall |j: int|\n 0 <= j < i as int && j != first_idx as int ==> nums[j] != k as i32,\n seen_second ==> Self::appears_twice(nums@, k as i32),\n decreases n - i,\n {\n if nums[i] == k as i32 {\n if seen_first {\n proof {\n assert(nums@[first_idx as int] == k as i32);\n assert(nums@[i as int] == k as i32);\n assert((first_idx as int) < (i as int));\n }\n seen_second = true;\n } else {\n first_idx = i;\n }\n seen_first = true;\n }\n i = i + 1;\n }\n\n if seen_second && !found_dup {\n dup = k as i32;\n found_dup = true;\n }\n if !seen_first && !found_miss {\n miss = k as i32;\n found_miss = true;\n }\n\n proof {\n if !seen_second && !found_dup {\n if !seen_first {\n assert forall |j1: int, j2: int|\n 0 <= j1 < j2 < n as int\n implies !(#[trigger] nums@[j1] == k as i32\n && #[trigger] nums@[j2] == k as i32) by {\n assert(nums@[j1] != k as i32);\n };\n } else {\n assert forall |j1: int, j2: int|\n 0 <= j1 < j2 < n as int\n implies !(#[trigger] nums@[j1] == k as i32\n && #[trigger] nums@[j2] == k as i32) by {\n if j1 == first_idx as int {\n assert(nums@[j2] != k as i32);\n } else {\n assert(nums@[j1] != k as i32);\n }\n };\n }\n assert(!Self::appears_twice(nums@, k as i32));\n }\n if seen_first && !found_miss {\n assert(nums@[first_idx as int] == k as i32);\n assert(Self::appears_in(nums@, k as i32));\n }\n }\n\n k = k + 1;\n }\n\n proof {\n if !found_dup {\n let d = choose |d: int| 1 <= d <= n as int\n && #[trigger] Self::appears_twice(nums@, d as i32);\n assert(1 <= d && d <= n as int);\n assert(!Self::appears_twice(nums@, d as i32));\n assert(false);\n }\n if !found_miss {\n let m = choose |m: int| 1 <= m <= n as int\n && !#[trigger] Self::appears_in(nums@, m as i32);\n assert(1 <= m && m <= n as int);\n assert(Self::appears_in(nums@, m as i32));\n assert(false);\n }\n }\n\n let mut result = Vec::new();\n result.push(dup);\n result.push(miss);\n\n proof {\n assert(result@.len() == 2);\n assert(result@[0] == dup);\n assert(result@[1] == miss);\n \n assert(Self::appears_twice(nums@, dup));\n assert(!Self::appears_in(nums@, miss));\n }\n\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc645/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc645/tests/mutated_testcases.jsonl"} {"problem": "lc69", "family": "leetcode", "description": "# Sqrt(x)\n\nGiven a non-negative integer `x`, return *the square root* of `x` *rounded down to the nearest integer*. The returned integer should be **non-negative** as well.\n\nYou **must not use** any built-in exponent function or operator.\n\n- For example, do not use `pow(x, 0.5)` in c++ or `x ** 0.5` in python.\n\n## Example 1:\n\n```\nInput: x = 4\nOutput: 2\nExplanation: The square root of 4 is 2, so we return 2.\n```\n\n## Example 2:\n\n```\nInput: x = 8\nOutput: 2\nExplanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.\n```\n\n## Constraints:\n\n- 0 <= x <= $2^{31} - 1$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn my_sqrt(x: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nMath, Binary Search\nAcceptance rate: 40.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn my_sqrt(x: i32) -> (res: i32) \n requires \n 0 <= x <= i32::MAX, \n ensures \n (res as int) * (res as int) <= x as int\n && ((res as int) + 1) * ((res as int) + 1) > x as int,\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn my_sqrt(x: i32) -> i32\n {\n let num: u64 = x as u64;\n let mut i: u64 = 0;\n let mut j: u64 = num;\n let mut found: bool = false; \n\n while i <= j && !found\n {\n let mid = (i + j) / 2;\n let tmp = mid * mid;\n\n if tmp == num {\n found = true;\n } else if tmp > num {\n j = mid - 1;\n } else {\n i = mid + 1;\n }\n }\n\n ((i + j) / 2) as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn my_sqrt(x: i32) -> (res: i32) \n requires \n 0 <= x <= i32::MAX, \n ensures \n (res as int) * (res as int) <= x as int\n && ((res as int) + 1) * ((res as int) + 1) > x as int,\n {\n let num: u64 = x as u64;\n let mut i: u64 = 0;\n let mut j: u64 = num;\n let mut found: bool = false; \n\n while i <= j && !found\n {\n let mid = (i + j) / 2;\n let tmp = mid * mid;\n\n if tmp == num {\n found = true;\n } else if tmp > num {\n j = mid - 1;\n } else {\n i = mid + 1;\n }\n }\n\n ((i + j) / 2) as i32\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn my_sqrt(x: i32) -> (res: i32) \n requires \n 0 <= x <= i32::MAX, \n ensures \n (res as int) * (res as int) <= x as int\n && ((res as int) + 1) * ((res as int) + 1) > x as int,\n {\n let num: u64 = x as u64;\n let mut i: u64 = 0;\n let mut j: u64 = num;\n let mut found: bool = false; \n\n assert((j + 1) * (j + 1) > num) by(nonlinear_arith)\n requires\n j == num, \n num <= i32::MAX,\n {}\n\n while i <= j && !found\n invariant \n num == x as u64,\n 0 <= num <= i32::MAX, \n 0 <= i <= num + 1,\n 0 <= j <= num,\n i > 0 ==> (i - 1) * (i - 1) <= num,\n (j + 1) * (j + 1) > num,\n i > j ==> i == j + 1,\n found ==> ((i + j) / 2) * ((i + j) / 2) == num, \n found ==> ((i + j) / 2 + 1) * ((i + j) / 2 + 1) > num,\n decreases j - i + 1, if found { 0int } else { 1int },\n {\n let mid = (i + j) / 2;\n \n assert(mid * mid <= u64::MAX) by(nonlinear_arith)\n requires\n mid <= i32::MAX,\n {}\n\n let tmp = mid * mid;\n\n if tmp == num {\n found = true;\n assert((mid + 1) * (mid + 1) > num) by(nonlinear_arith)\n requires\n mid * mid == num,\n mid <= i32::MAX,\n {}\n } else if tmp > num {\n assert(mid >= 1) by(nonlinear_arith)\n requires\n mid * mid > num,\n num >= 0,\n {}\n j = mid - 1;\n } else {\n i = mid + 1;\n }\n }\n\n ((i + j) / 2) as i32\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc69/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc69/tests/mutated_testcases.jsonl"} {"problem": "lc704", "family": "leetcode", "description": "# Binary Search\n\nGiven an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.\n\nYou must write an algorithm with `O(log n)` runtime complexity.\n\n## Example 1:\n\n```\nInput: nums = [-1,0,3,5,9,12], target = 9\nOutput: 4\nExplanation: 9 exists in nums and its index is 4\n```\n\n## Example 2:\n\n```\nInput: nums = [-1,0,3,5,9,12], target = 2\nOutput: -1\nExplanation: 2 does not exist in nums so return -1\n```\n\n## Constraints:\n\n- 1 <= nums.length <= $10^4$\n\n- $-10^4$ < nums[i], target < $10^4$\n\n- All the integers in `nums` are **unique**.\n\n- nums is sorted in ascending order.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn search(nums: Vec, target: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Binary Search\nAcceptance rate: 60.1%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n\n pub fn search(nums: Vec, target: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 10_000,\n -10_000 < target < 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 < #[trigger] nums[i] < 10_000,\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j],\n ensures\n -1 <= res < nums.len(),\n res == -1 ==> (forall |i: int| 0 <= i < nums.len() ==> nums[i] != target),\n res != -1 ==> nums[res as int] == target,\n {\n\n }\n\n}\n\n}\n", "code": "impl Solution {\n\n pub fn search(nums: Vec, target: i32) -> i32\n {\n let mut i1: usize = 0;\n let mut i2: usize = nums.len() - 1;\n\n while i1 != i2\n {\n let ix = i1 + (i2 - i1) / 2;\n if nums[ix] < target {\n i1 = ix + 1;\n } else {\n i2 = ix;\n }\n }\n\n if nums[i1] != target {\n -1\n }\n else {\n i1 as i32\n }\n }\n\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n\n pub fn search(nums: Vec, target: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 10_000,\n -10_000 < target < 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 < #[trigger] nums[i] < 10_000,\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j],\n ensures\n -1 <= res < nums.len(),\n res == -1 ==> (forall |i: int| 0 <= i < nums.len() ==> nums[i] != target),\n res != -1 ==> nums[res as int] == target,\n {\n let mut i1: usize = 0;\n let mut i2: usize = nums.len() - 1;\n\n while i1 != i2\n {\n let ix = i1 + (i2 - i1) / 2;\n if nums[ix] < target {\n i1 = ix + 1;\n } else {\n i2 = ix;\n }\n }\n\n if nums[i1] != target {\n -1\n }\n else {\n i1 as i32\n }\n }\n\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n\n pub fn search(nums: Vec, target: i32) -> (res: i32)\n requires\n 1 <= nums.len() <= 10_000,\n -10_000 < target < 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 < #[trigger] nums[i] < 10_000,\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j],\n ensures\n -1 <= res < nums.len(),\n res == -1 ==> (forall |i: int| 0 <= i < nums.len() ==> nums[i] != target),\n res != -1 ==> nums[res as int] == target,\n {\n let mut i1: usize = 0;\n let mut i2: usize = nums.len() - 1;\n\n while i1 != i2\n invariant\n 1 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -10_000 < #[trigger] nums[i] < 10_000,\n forall|i: int, j: int| 0 <= i < j < nums.len() ==> nums[i] < nums[j],\n i1 <= i2 < nums.len(),\n forall |t: int| 0 <= t < i1 ==> nums[t] < target,\n nums[i2 as int] >= target || i2 == nums.len() - 1,\n decreases i2 - i1,\n {\n let ix = i1 + (i2 - i1) / 2;\n if nums[ix] < target {\n proof {\n assert forall |t: int| 0 <= t < (ix + 1) as int implies nums[t] < target by {\n if t < i1 as int {\n } else if t < ix as int {\n assert(nums[t] < nums[ix as int]);\n }\n }\n }\n i1 = ix + 1;\n } else {\n i2 = ix;\n }\n }\n\n if nums[i1] != target {\n proof {\n assert forall |t: int| 0 <= t < nums.len() implies nums[t] != target by {\n if t < i1 as int {\n } else if t == i1 as int {\n } else {\n assert(nums[i1 as int] >= target);\n assert(nums[i1 as int] < nums[t]);\n }\n }\n }\n -1\n }\n else {\n i1 as i32\n }\n }\n\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc704/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc704/tests/mutated_testcases.jsonl"} {"problem": "lc714", "family": "leetcode", "description": "# Best Time to Buy and Sell Stock with Transaction Fee\n\nYou are given an array `prices` where `prices[i]` is the price of a given stock on the $i^{th}$ day, and an integer `fee` representing a transaction fee.\n\nFind the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.\n\n**Note:**\n- You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).\n- The transaction fee is only charged once for each stock purchase and sale.\n\n## Example 1:\n\n> **Input:** prices = [1,3,2,8,4,9], fee = 2\n> **Output:** 8\n> **Explanation:** The maximum profit can be achieved by:\n> - Buying at prices[0] = 1\n> - Selling at prices[3] = 8\n> - Buying at prices[4] = 4\n> - Selling at prices[5] = 9\n> The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.\n\n## Example 2:\n\n> **Input:** prices = [1,3,7,5,10,3], fee = 3\n> **Output:** 6\n\n## Constraints:\n\n- $1 <= prices.length <= 5 * 10^4$\n\n- $1 <= prices[i] < 5 * 10^4$\n\n- $0 <= fee < 5 * 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_profit(prices: Vec, fee: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Dynamic Programming, Greedy\nAcceptance rate: 71.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn spec_cash(prices: Seq, fee: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n 0\n } else {\n Self::spec_max(\n Self::spec_cash(prices, fee, i - 1),\n Self::spec_hold(prices, fee, i - 1) + prices[i] as int - fee\n )\n }\n }\n\n pub open spec fn spec_hold(prices: Seq, fee: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n -(prices[0] as int)\n } else {\n Self::spec_max(\n Self::spec_hold(prices, fee, i - 1),\n Self::spec_cash(prices, fee, i - 1) - prices[i] as int\n )\n }\n }\n\n pub fn max_profit(prices: Vec, fee: i32) -> (res: i32)\n requires\n 1 <= prices.len() <= 50_000,\n forall |i: int| 0 <= i < prices.len() ==> 1 <= #[trigger] prices[i] < 50_000,\n 0 <= fee < 50_000,\n ensures\n res as int == Self::spec_cash(prices@, fee as int, (prices.len() - 1) as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_profit(prices: Vec, fee: i32) -> i32 {\n let mut cash: i32 = 0;\n let mut hold: i32 = -prices[0];\n let n = prices.len();\n for i in 1..n {\n let prev_cash = cash;\n let prev_hold = hold;\n if prev_hold + prices[i] - fee > prev_cash {\n cash = prev_hold + prices[i] - fee;\n }\n if prev_cash - prices[i] > prev_hold {\n hold = prev_cash - prices[i];\n }\n }\n cash\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn spec_cash(prices: Seq, fee: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n 0\n } else {\n Self::spec_max(\n Self::spec_cash(prices, fee, i - 1),\n Self::spec_hold(prices, fee, i - 1) + prices[i] as int - fee\n )\n }\n }\n\n pub open spec fn spec_hold(prices: Seq, fee: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n -(prices[0] as int)\n } else {\n Self::spec_max(\n Self::spec_hold(prices, fee, i - 1),\n Self::spec_cash(prices, fee, i - 1) - prices[i] as int\n )\n }\n }\n\n pub fn max_profit(prices: Vec, fee: i32) -> (res: i32)\n requires\n 1 <= prices.len() <= 50_000,\n forall |i: int| 0 <= i < prices.len() ==> 1 <= #[trigger] prices[i] < 50_000,\n 0 <= fee < 50_000,\n ensures\n res as int == Self::spec_cash(prices@, fee as int, (prices.len() - 1) as int),\n {\n let mut cash: i32 = 0;\n let mut hold: i32 = -prices[0];\n let n = prices.len();\n for i in 1..n {\n let prev_cash = cash;\n let prev_hold = hold;\n if prev_hold + prices[i] - fee > prev_cash {\n cash = prev_hold + prices[i] - fee;\n }\n if prev_cash - prices[i] > prev_hold {\n hold = prev_cash - prices[i];\n }\n }\n cash\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn spec_cash(prices: Seq, fee: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n 0\n } else {\n Self::spec_max(\n Self::spec_cash(prices, fee, i - 1),\n Self::spec_hold(prices, fee, i - 1) + prices[i] as int - fee\n )\n }\n }\n\n pub open spec fn spec_hold(prices: Seq, fee: int, i: int) -> int\n decreases i\n {\n if i <= 0 {\n -(prices[0] as int)\n } else {\n Self::spec_max(\n Self::spec_hold(prices, fee, i - 1),\n Self::spec_cash(prices, fee, i - 1) - prices[i] as int\n )\n }\n }\n\n proof fn spec_cash_nonneg(prices: Seq, fee: int, i: int)\n requires\n 0 <= i < prices.len(),\n forall |k: int| 0 <= k < prices.len() ==> 1 <= #[trigger] prices[k] < 50_000,\n 0 <= fee < 50_000,\n ensures\n Self::spec_cash(prices, fee, i) >= 0,\n decreases i,\n {\n if i > 0 {\n Self::spec_cash_nonneg(prices, fee, i - 1);\n Self::spec_hold_lower(prices, fee, i - 1);\n }\n }\n\n proof fn spec_hold_lower(prices: Seq, fee: int, i: int)\n requires\n 0 <= i < prices.len(),\n forall |k: int| 0 <= k < prices.len() ==> 1 <= #[trigger] prices[k] < 50_000,\n 0 <= fee < 50_000,\n ensures\n Self::spec_hold(prices, fee, i) >= -50_000,\n decreases i,\n {\n if i > 0 {\n Self::spec_hold_lower(prices, fee, i - 1);\n Self::spec_cash_nonneg(prices, fee, i - 1);\n }\n }\n\n proof fn spec_hold_le_prev_cash(prices: Seq, fee: int, i: int)\n requires\n 0 <= i < prices.len(),\n forall |k: int| 0 <= k < prices.len() ==> 1 <= #[trigger] prices[k] < 50_000,\n 0 <= fee < 50_000,\n ensures\n i == 0 ==> Self::spec_hold(prices, fee, i) <= 0,\n i > 0 ==> Self::spec_hold(prices, fee, i) <= Self::spec_cash(prices, fee, i - 1),\n decreases i,\n {\n if i > 0 {\n Self::spec_hold_le_prev_cash(prices, fee, i - 1);\n Self::spec_cash_nonneg(prices, fee, i - 1);\n }\n }\n\n proof fn spec_cash_upper(prices: Seq, fee: int, i: int)\n requires\n 0 <= i < prices.len(),\n prices.len() <= 50_000,\n forall |k: int| 0 <= k < prices.len() ==> 1 <= #[trigger] prices[k] < 50_000,\n 0 <= fee < 50_000,\n ensures\n Self::spec_cash(prices, fee, i) <= 25_000 * (i + 1),\n decreases i,\n {\n if i > 0 {\n Self::spec_cash_upper(prices, fee, i - 1);\n Self::spec_hold_le_prev_cash(prices, fee, i - 1);\n if i >= 2 {\n Self::spec_cash_upper(prices, fee, i - 2);\n }\n }\n }\n\n proof fn spec_hold_upper(prices: Seq, fee: int, i: int)\n requires\n 0 <= i < prices.len(),\n prices.len() <= 50_000,\n forall |k: int| 0 <= k < prices.len() ==> 1 <= #[trigger] prices[k] < 50_000,\n 0 <= fee < 50_000,\n ensures\n Self::spec_hold(prices, fee, i) <= 25_000 * (i + 1),\n {\n Self::spec_hold_le_prev_cash(prices, fee, i);\n if i > 0 {\n Self::spec_cash_upper(prices, fee, i - 1);\n }\n }\n\n pub fn max_profit(prices: Vec, fee: i32) -> (res: i32)\n requires\n 1 <= prices.len() <= 50_000,\n forall |i: int| 0 <= i < prices.len() ==> 1 <= #[trigger] prices[i] < 50_000,\n 0 <= fee < 50_000,\n ensures\n res as int == Self::spec_cash(prices@, fee as int, (prices.len() - 1) as int),\n {\n let ghost p = prices@;\n let ghost f = fee as int;\n let mut cash: i32 = 0;\n let mut hold: i32 = -prices[0];\n let n = prices.len();\n for i in 1..n\n invariant\n n == prices.len(),\n n <= 50_000,\n p == prices@,\n f == fee as int,\n forall |k: int| 0 <= k < n ==> 1 <= #[trigger] prices[k] < 50_000,\n 0 <= fee < 50_000,\n cash as int == Self::spec_cash(p, f, (i - 1) as int),\n hold as int == Self::spec_hold(p, f, (i - 1) as int),\n 0 <= cash <= 25_000 * i,\n -50_000 <= hold <= 25_000 * i,\n {\n proof {\n Self::spec_cash_nonneg(p, f, i as int);\n Self::spec_cash_upper(p, f, i as int);\n Self::spec_hold_lower(p, f, i as int);\n Self::spec_hold_upper(p, f, i as int);\n }\n let prev_cash = cash;\n let prev_hold = hold;\n if prev_hold + prices[i] - fee > prev_cash {\n cash = prev_hold + prices[i] - fee;\n }\n if prev_cash - prices[i] > prev_hold {\n hold = prev_cash - prices[i];\n }\n }\n cash\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc714/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc714/tests/mutated_testcases.jsonl"} {"problem": "lc724", "family": "leetcode", "description": "# Find Pivot Index\n\nGiven an array of integers `nums`, calculate the **pivot index** of this array.\n\nThe **pivot index** is the index where the sum of all the numbers **strictly** to the left of the index is equal to the sum of all the numbers **strictly** to the right of the index.\n\nIf the index is on the left edge of the array, then the left sum is `0` because there are no elements to the left. This also applies to the right edge of the array.\n\nReturn the **leftmost pivot index**. If no such index exists, return `-1`.\n\n## Example 1:\n> **Input:** nums = [1,7,3,6,5,6]\n> **Output:** 3\n> **Explanation:** The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11. Right sum = nums[4] + nums[5] = 5 + 6 = 11.\n\n## Example 2:\n> **Input:** nums = [1,2,3]\n> **Output:** -1\n> **Explanation:** There is no index that satisfies the conditions in the problem statement.\n\n## Example 3:\n> **Input:** nums = [2,1,-1]\n> **Output:** 0\n> **Explanation:** The pivot index is 0. Left sum = 0 (no elements to the left of index 0). Right sum = nums[1] + nums[2] = 1 + (-1) = 0.\n\n## Constraints:\n- $1 <= nums.length <= 10^4$\n- $-1000 <= nums[i] <= 1000$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn pivot_index(nums: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Prefix Sum\nAcceptance rate: 55.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(nums: Seq, lo: int, hi: int) -> int\n decreases hi - lo\n {\n if lo >= hi { 0 }\n else { nums[lo] as int + Self::sum_range(nums, lo + 1, hi) }\n }\n\n pub fn pivot_index(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result == -1 || 0 <= result < nums.len(),\n result >= 0 ==> Self::sum_range(nums@, 0, result as int) == Self::sum_range(nums@, result as int + 1, nums.len() as int),\n result >= 0 ==> forall |j: int| 0 <= j < result as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, nums.len() as int),\n result == -1 ==> forall |j: int| 0 <= j < nums.len() as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, nums.len() as int),\n {\n\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn pivot_index(nums: Vec) -> i32 {\n let n = nums.len();\n let mut total: i32 = 0;\n let mut i: usize = 0;\n while i < n {\n total = total + nums[i];\n i = i + 1;\n }\n let mut left_sum: i32 = 0;\n i = 0;\n while i < n {\n if left_sum == total - left_sum - nums[i] {\n return i as i32;\n }\n left_sum = left_sum + nums[i];\n i = i + 1;\n }\n -1\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(nums: Seq, lo: int, hi: int) -> int\n decreases hi - lo\n {\n if lo >= hi { 0 }\n else { nums[lo] as int + Self::sum_range(nums, lo + 1, hi) }\n }\n\n pub fn pivot_index(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result == -1 || 0 <= result < nums.len(),\n result >= 0 ==> Self::sum_range(nums@, 0, result as int) == Self::sum_range(nums@, result as int + 1, nums.len() as int),\n result >= 0 ==> forall |j: int| 0 <= j < result as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, nums.len() as int),\n result == -1 ==> forall |j: int| 0 <= j < nums.len() as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, nums.len() as int),\n {\n let n = nums.len();\n let mut total: i32 = 0;\n let mut i: usize = 0;\n\n while i < n\n {\n total = total + nums[i];\n i = i + 1;\n }\n\n let mut left_sum: i32 = 0;\n i = 0;\n while i < n\n {\n if left_sum == total - left_sum - nums[i] {\n return i as i32;\n }\n left_sum = left_sum + nums[i];\n i = i + 1;\n }\n -1\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn sum_range(nums: Seq, lo: int, hi: int) -> int\n decreases hi - lo\n {\n if lo >= hi { 0 }\n else { nums[lo] as int + Self::sum_range(nums, lo + 1, hi) }\n }\n\n proof fn sum_range_split(nums: Seq, lo: int, mid: int, hi: int)\n requires\n 0 <= lo <= mid <= hi <= nums.len(),\n ensures\n Self::sum_range(nums, lo, hi) == Self::sum_range(nums, lo, mid) + Self::sum_range(nums, mid, hi),\n decreases mid - lo,\n {\n if lo < mid {\n Self::sum_range_split(nums, lo + 1, mid, hi);\n }\n }\n\n pub fn pivot_index(nums: Vec) -> (result: i32)\n requires\n 1 <= nums.len() <= 10_000,\n forall |i: int| 0 <= i < nums.len() ==> -1000 <= #[trigger] nums[i] <= 1000,\n ensures\n result == -1 || 0 <= result < nums.len(),\n result >= 0 ==> Self::sum_range(nums@, 0, result as int) == Self::sum_range(nums@, result as int + 1, nums.len() as int),\n result >= 0 ==> forall |j: int| 0 <= j < result as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, nums.len() as int),\n result == -1 ==> forall |j: int| 0 <= j < nums.len() as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, nums.len() as int),\n {\n let n = nums.len();\n let mut total: i32 = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n n <= 10_000,\n forall |k: int| 0 <= k < nums.len() ==> -1000 <= #[trigger] nums[k] <= 1000,\n total as int == Self::sum_range(nums@, 0, i as int),\n -1000 * (i as int) <= total as int <= 1000 * (i as int),\n decreases n - i,\n {\n proof {\n assert(Self::sum_range(nums@, i as int + 1, i as int + 1) == 0int);\n Self::sum_range_split(nums@, 0, i as int, (i + 1) as int);\n }\n total = total + nums[i];\n i = i + 1;\n }\n\n let mut left_sum: i32 = 0;\n i = 0;\n while i < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n n <= 10_000,\n forall |k: int| 0 <= k < nums.len() ==> -1000 <= #[trigger] nums[k] <= 1000,\n left_sum as int == Self::sum_range(nums@, 0, i as int),\n total as int == Self::sum_range(nums@, 0, n as int),\n -1000 * (i as int) <= left_sum as int <= 1000 * (i as int),\n -1000 * (n as int) <= total as int <= 1000 * (n as int),\n forall |j: int| 0 <= j < i as int ==>\n Self::sum_range(nums@, 0, j) != Self::sum_range(nums@, j + 1, n as int),\n decreases n - i,\n {\n proof {\n Self::sum_range_split(nums@, 0, i as int, n as int);\n assert(Self::sum_range(nums@, i as int, n as int) == nums@[i as int] as int + Self::sum_range(nums@, i as int + 1, n as int));\n assert(Self::sum_range(nums@, i as int + 1, n as int) == total as int - left_sum as int - nums@[i as int] as int);\n }\n if left_sum == total - left_sum - nums[i] {\n return i as i32;\n }\n proof {\n assert(Self::sum_range(nums@, i as int + 1, i as int + 1) == 0int);\n Self::sum_range_split(nums@, 0, i as int, (i + 1) as int);\n }\n left_sum = left_sum + nums[i];\n i = i + 1;\n }\n -1\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc724/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc724/tests/mutated_testcases.jsonl"} {"problem": "lc739", "family": "leetcode", "description": "# Daily Temperatures\n\nYou are given an array of integers `temperatures` representing the daily temperatures.\n\nReturn an array `answer` such that `answer[i]` is the number of days you have to wait after the $i^{th}$ day to get a warmer temperature. If there is no future day for which this is possible, keep `answer[i] == 0` instead.\n\n## Example 1:\n\n> **Input:** temperatures = [73,74,75,71,69,72,76,73]\n> **Output:** [1,1,4,2,1,1,0,0]\n\n## Example 2:\n\n> **Input:** temperatures = [30,40,50,60]\n> **Output:** [1,1,1,0]\n\n## Example 3:\n\n> **Input:** temperatures = [30,60,90]\n> **Output:** [1,1,0]\n\n## Constraints:\n\n- $1 <= temperatures.length <= 10^5$\n- $30 <= temperatures[i] <= 100$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn daily_temperatures(temperatures: Vec) -> Vec {\n \n }\n}\n```\n", "tags": "Medium\nArray, Stack, Monotonic Stack\nAcceptance rate: 68.4%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn daily_temperatures(temperatures: Vec) -> (res: Vec)\n requires\n 1 <= temperatures.len() <= 100_000,\n forall |i: int| 0 <= i < temperatures.len() ==> 30 <= #[trigger] temperatures[i] <= 100,\n ensures\n res.len() == temperatures.len(),\n forall |i: int| 0 <= i < temperatures.len() ==>\n 0 <= #[trigger] res[i] && res[i] as int <= temperatures.len() - 1 - i\n && if res[i] == 0 {\n forall |j: int| i < j < temperatures.len() ==> temperatures[j] <= temperatures[i]\n } else {\n let d = res[i] as int;\n 1 <= d && i + d < temperatures.len()\n && temperatures[i + d] > temperatures[i]\n && forall |j: int| i < j < i + d ==> temperatures[j] <= temperatures[i]\n },\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn daily_temperatures(temperatures: Vec) -> Vec\n {\n let n = temperatures.len();\n\n let mut res: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n\n {\n res.push(0);\n idx = idx + 1;\n }\n\n let mut next_pos: Vec = Vec::new();\n let mut t0: usize = 0;\n while t0 <= 100\n {\n next_pos.push(n);\n t0 = t0 + 1;\n }\n\n let mut i: usize = n;\n while i > 0\n {\n let idx = i - 1;\n let cur = temperatures[idx] as usize;\n let mut best: usize = n;\n let mut t: usize = cur + 1;\n while t <= 100\n {\n let candidate = next_pos[t];\n if candidate < best {\n best = candidate;\n }\n t = t + 1;\n }\n\n if best < n {\n let d: i32 = (best - idx) as i32;\n res[idx] = d;\n } else {\n res[idx] = 0;\n }\n\n next_pos[cur] = idx;\n i = idx;\n }\n\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn daily_temperatures(temperatures: Vec) -> (res: Vec)\n requires\n 1 <= temperatures.len() <= 100_000,\n forall |i: int| 0 <= i < temperatures.len() ==> 30 <= #[trigger] temperatures[i] <= 100,\n ensures\n res.len() == temperatures.len(),\n forall |i: int| 0 <= i < temperatures.len() ==>\n 0 <= #[trigger] res[i] && res[i] as int <= temperatures.len() - 1 - i\n && if res[i] == 0 {\n forall |j: int| i < j < temperatures.len() ==> temperatures[j] <= temperatures[i]\n } else {\n let d = res[i] as int;\n 1 <= d && i + d < temperatures.len()\n && temperatures[i + d] > temperatures[i]\n && forall |j: int| i < j < i + d ==> temperatures[j] <= temperatures[i]\n },\n {\n let n = temperatures.len();\n\n let mut res: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n\n {\n res.push(0);\n idx = idx + 1;\n }\n\n let mut next_pos: Vec = Vec::new();\n let mut t0: usize = 0;\n while t0 <= 100\n {\n next_pos.push(n);\n t0 = t0 + 1;\n }\n\n let mut i: usize = n;\n while i > 0\n {\n let idx = i - 1;\n let cur = temperatures[idx] as usize;\n let mut best: usize = n;\n let mut t: usize = cur + 1;\n while t <= 100\n {\n let candidate = next_pos[t];\n if candidate < best {\n best = candidate;\n }\n t = t + 1;\n }\n\n if best < n {\n let d: i32 = (best - idx) as i32;\n res[idx] = d;\n } else {\n res[idx] = 0;\n }\n\n next_pos[cur] = idx;\n i = idx;\n }\n\n res\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn bounded_temps(temps: Seq) -> bool {\n forall |i: int| 0 <= i < temps.len() ==> 30 <= #[trigger] temps[i] <= 100\n}\n\npub open spec fn correct_at(temps: Seq, res: Seq, k: int) -> bool {\n 0 <= k < temps.len()\n && 0 <= res[k]\n && res[k] <= temps.len() - 1 - k\n && if res[k] == 0 {\n forall |m: int| k < m < temps.len() ==> temps[m] <= temps[k]\n } else {\n let d = res[k] as int;\n 1 <= d && k + d < temps.len()\n && temps[k + d] > temps[k]\n && forall |m: int| k < m < k + d ==> temps[m] <= temps[k]\n }\n}\n\npub open spec fn next_pos_ok_for(temps: Seq, start: int, next: Seq, t: int) -> bool {\n 0 <= t < next.len()\n && start <= (next[t] as int)\n && (next[t] as int) <= temps.len()\n && (if (next[t] as int) < temps.len() {\n temps[next[t] as int] == t as i32\n && forall |m: int| start <= m < (next[t] as int) ==> temps[m] != t as i32\n } else {\n forall |m: int| start <= m < temps.len() ==> temps[m] != t as i32\n })\n}\n\npub proof fn i32_cast_nonneg(x: int)\n requires\n 0 <= x <= 100_000,\n ensures\n 0 <= (x as i32),\n ((x as i32) as int) == x,\n{\n assert(x < 0x8000_0000) by (nonlinear_arith)\n requires\n x <= 100_000,\n {}\n assert(0 <= (x as i32));\n assert(((x as i32) as int) == x);\n}\n\npub proof fn no_warmer_after(temps: Seq, next: Seq, idx: int)\n requires\n bounded_temps(temps),\n 0 <= idx < temps.len(),\n next.len() == 101,\n forall |t: int| 0 <= t <= 100 ==> #[trigger] next_pos_ok_for(temps, idx + 1, next, t),\n forall |t: int| temps[idx] < t <= 100 ==> next[t] as int == temps.len(),\n ensures\n forall |m: int| idx < m < temps.len() ==> temps[m] <= temps[idx]\n{\n assert forall |m: int| idx < m < temps.len() implies temps[m] <= temps[idx] by {\n if temps[m] > temps[idx] {\n assert(30 <= temps[m] <= 100);\n let t = temps[m] as int;\n assert(t as i32 == temps[m]);\n assert(temps[idx] < t <= 100);\n assert(next[t] as int == temps.len());\n assert(next_pos_ok_for(temps, idx + 1, next, t));\n assert(temps[m] != t as i32);\n }\n }\n}\n\npub proof fn warmer_after_at_best(temps: Seq, next: Seq, idx: int, best: int)\n requires\n bounded_temps(temps),\n 0 <= idx < best < temps.len(),\n next.len() == 101,\n forall |t: int| 0 <= t <= 100 ==> #[trigger] next_pos_ok_for(temps, idx + 1, next, t),\n exists |t: int| temps[idx] < t <= 100 && next[t] as int == best,\n forall |t: int| temps[idx] < t <= 100 ==> best <= next[t] as int,\n ensures\n temps[best] > temps[idx],\n forall |m: int| idx < m < best ==> temps[m] <= temps[idx]\n{\n let witness = choose |t: int| temps[idx] < t <= 100 && next[t] as int == best;\n assert(next_pos_ok_for(temps, idx + 1, next, witness));\n assert(temps[best] == witness as i32);\n assert((temps[idx] as int) < witness);\n assert(temps[idx] < witness as i32);\n assert(temps[best] > temps[idx]);\n assert forall |m: int| idx < m < best implies temps[m] <= temps[idx] by {\n if temps[m] > temps[idx] {\n assert(30 <= temps[m] <= 100);\n let t = temps[m] as int;\n assert(t as i32 == temps[m]);\n assert(temps[idx] < t <= 100);\n assert(next_pos_ok_for(temps, idx + 1, next, t));\n if next[t] as int == temps.len() {\n assert(temps[m] != t as i32);\n } else {\n if m < next[t] as int {\n assert(temps[m] != t as i32);\n }\n }\n assert((next[t] as int) <= m);\n assert(best <= next[t] as int);\n assert(best <= m);\n }\n }\n}\n\npub proof fn correct_at_preserved_update(temps: Seq, res: Seq, i: int, k: int, v: i32)\n requires\n 0 <= i < temps.len(),\n 0 <= k < temps.len(),\n i != k,\n correct_at(temps, res, k),\n res.len() == temps.len(),\n ensures\n correct_at(temps, res.update(i, v), k),\n{\n assert(res.update(i, v)[k] == res[k]);\n}\n\nimpl Solution {\n pub fn daily_temperatures(temperatures: Vec) -> (res: Vec)\n requires\n 1 <= temperatures.len() <= 100_000,\n forall |i: int| 0 <= i < temperatures.len() ==> 30 <= #[trigger] temperatures[i] <= 100,\n ensures\n res.len() == temperatures.len(),\n forall |i: int| 0 <= i < temperatures.len() ==>\n 0 <= #[trigger] res[i] && res[i] as int <= temperatures.len() - 1 - i\n && if res[i] == 0 {\n forall |j: int| i < j < temperatures.len() ==> temperatures[j] <= temperatures[i]\n } else {\n let d = res[i] as int;\n 1 <= d && i + d < temperatures.len()\n && temperatures[i + d] > temperatures[i]\n && forall |j: int| i < j < i + d ==> temperatures[j] <= temperatures[i]\n },\n {\n let n = temperatures.len();\n let ghost n_int: int = n as int;\n proof {\n assert(n_int == n as int);\n assert(n_int <= 100_000);\n assert(temperatures@.len() as int == n_int);\n assert(bounded_temps(temperatures@));\n }\n\n let mut res: Vec = Vec::new();\n let mut idx: usize = 0;\n while idx < n\n invariant\n 0 <= idx <= n,\n res.len() == idx,\n res@.len() as int == idx as int,\n n == temperatures.len(),\n n_int == n as int,\n n <= 100_000,\n forall |k: int| 0 <= k < idx as int ==> res@[k] == 0,\n decreases n - idx\n {\n res.push(0);\n idx = idx + 1;\n }\n\n proof {\n assert(res@.len() as int == n_int);\n assert(forall |k: int| #![trigger res@[k]] 0 <= k < n_int ==> res@[k] == 0);\n }\n\n let mut next_pos: Vec = Vec::new();\n let mut t0: usize = 0;\n while t0 <= 100\n invariant\n 0 <= t0 <= 101,\n next_pos.len() == t0,\n forall |t: int| 0 <= t < t0 as int ==> next_pos@[t] == n,\n decreases 101 - t0\n {\n next_pos.push(n);\n t0 = t0 + 1;\n }\n\n proof {\n assert(next_pos@.len() == 101);\n assert(forall |t: int| 0 <= t <= 100 ==> next_pos@[t] == n);\n assert forall |t: int| 0 <= t <= 100 implies #[trigger] next_pos_ok_for(temperatures@, n_int, next_pos@, t) by {\n assert(next_pos@[t] as int == n_int);\n }\n }\n\n let mut i: usize = n;\n while i > 0\n invariant\n 0 <= i <= n,\n n == temperatures.len(),\n n_int == n as int,\n n <= 100_000,\n bounded_temps(temperatures@),\n res@.len() as int == n_int,\n next_pos@.len() == 101,\n forall |k: int| 0 <= k < i as int ==> res@[k] == 0,\n forall |k: int| 0 <= k < n_int ==> 0 <= res@[k],\n forall |k: int| #![trigger correct_at(temperatures@, res@, k)] i as int <= k < n_int ==> correct_at(temperatures@, res@, k),\n forall |t: int| 0 <= t <= 100 ==> #[trigger] next_pos_ok_for(temperatures@, i as int, next_pos@, t),\n decreases i\n {\n let idx = i - 1;\n let ghost res_before = res@;\n proof {\n assert(0 <= idx < n);\n assert(30 <= temperatures@[idx as int] <= 100);\n }\n let ghost next_before = next_pos@;\n let cur = temperatures[idx] as usize;\n let mut best: usize = n;\n let mut t: usize = cur + 1;\n while t <= 100\n invariant\n next_pos.len() == 101,\n next_pos@ == next_before,\n next_before.len() == 101,\n 0 <= idx < n,\n (cur as int) == (temperatures@[idx as int] as int),\n cur <= 100,\n cur + 1 <= t <= 101,\n best <= n,\n forall |temp: int| 0 <= temp <= 100 ==> #[trigger] next_pos_ok_for(temperatures@, i as int, next_before, temp),\n forall |q: int| ((cur as int) < q && q < (t as int)) ==> (best as int) <= (next_before[q] as int),\n best < n ==> exists |q: int| ((cur as int) < q && q < (t as int)) && (best as int) == (next_before[q] as int),\n decreases 101 - t\n {\n let ghost t_old: int = t as int;\n let ghost best_old: int = best as int;\n let candidate = next_pos[t];\n if candidate < best {\n best = candidate;\n }\n t = t + 1;\n proof {\n assert((cur as int) < t_old && t_old <= 100);\n assert((candidate as int) == (next_before[t_old] as int));\n assert forall |q: int| ((cur as int) < q && q < (t as int)) implies (best as int) <= (next_before[q] as int) by {\n if q == t_old {\n if candidate < best_old as usize {\n assert((best as int) == (candidate as int));\n } else {\n assert((best as int) == best_old);\n assert(best_old <= (candidate as int));\n }\n } else {\n assert((cur as int) < q && q < t_old);\n if candidate < best_old as usize {\n assert((best as int) == (candidate as int));\n assert((candidate as int) < best_old);\n assert(best_old <= (next_before[q] as int));\n } else {\n assert((best as int) == best_old);\n }\n }\n }\n if best < n {\n if candidate < best_old as usize {\n assert((best as int) == (candidate as int));\n assert((cur as int) < t_old && t_old < (t as int));\n assert((best as int) == (next_before[t_old] as int));\n } else {\n assert((best as int) == best_old);\n }\n }\n }\n }\n\n if best < n {\n proof {\n let witness = choose |q: int| ((cur as int) < q && q < 101) && (best as int) == (next_before[q] as int);\n assert(next_pos_ok_for(temperatures@, i as int, next_before, witness));\n assert(i as int <= best as int);\n assert(idx < best);\n }\n let d: i32 = (best - idx) as i32;\n proof {\n assert(exists |q: int| ((cur as int) < q && q < 101) && (best as int) == (next_before[q] as int));\n warmer_after_at_best(temperatures@, next_before, idx as int, best as int);\n assert(0 <= (best as int) - (idx as int));\n assert((best as int) - (idx as int) < n_int) by (nonlinear_arith)\n requires\n idx < best,\n best < n,\n n_int == n as int,\n {}\n assert((best as int) - (idx as int) <= 100_000) by (nonlinear_arith)\n requires\n (best as int) - (idx as int) < n_int,\n n_int <= 100_000,\n {}\n i32_cast_nonneg((best as int) - (idx as int));\n assert(d == ((best as int) - (idx as int)) as i32);\n assert((d as int) == (best as int) - (idx as int));\n assert(1 <= (d as int) && (idx as int) + (d as int) < temperatures.len());\n assert(temperatures@[(idx as int) + (d as int)] == temperatures@[best as int]);\n assert(correct_at(temperatures@, res_before.update(idx as int, d), idx as int));\n }\n res[idx] = d;\n } else {\n proof {\n assert forall |q: int| ((cur as int) < q && q < 101) implies (next_before[q] as int) == n_int by {\n assert((best as int) <= (next_before[q] as int));\n assert(next_pos_ok_for(temperatures@, i as int, next_before, q));\n assert((next_before[q] as int) <= n_int);\n }\n no_warmer_after(temperatures@, next_before, idx as int);\n assert(res_before[idx as int] == 0);\n assert(correct_at(temperatures@, res_before, idx as int));\n }\n res[idx] = 0;\n }\n\n next_pos[cur] = idx;\n proof {\n assert(next_before.update(cur as int, idx) == next_pos@);\n assert forall |temp: int| 0 <= temp <= 100 implies #[trigger] next_pos_ok_for(temperatures@, idx as int, next_pos@, temp) by {\n if temp == cur as int {\n assert(next_pos@[temp] == idx);\n assert(temperatures@[idx as int] == temp as i32);\n } else {\n assert(next_pos@[temp] == next_before[temp]);\n assert(next_pos_ok_for(temperatures@, idx as int + 1, next_before, temp));\n assert(temperatures@[idx as int] != temp as i32);\n }\n }\n assert forall |k: int| idx as int <= k < n_int implies correct_at(temperatures@, res@, k) by {\n if k == idx as int {\n if best < n {\n assert(correct_at(temperatures@, res_before.update(idx as int, res@[idx as int]), idx as int));\n } else {\n assert(correct_at(temperatures@, res_before, idx as int));\n assert(res_before == res@);\n }\n } else {\n assert(idx as int != k);\n correct_at_preserved_update(\n temperatures@,\n res_before,\n idx as int,\n k,\n res@[idx as int],\n );\n assert(res_before.update(idx as int, res@[idx as int]) == res@);\n }\n }\n }\n i = idx;\n }\n\n proof {\n assert(i == 0);\n assert(forall |k: int| 0 <= k < n_int ==> correct_at(temperatures@, res@, k));\n assert forall |idx: int| 0 <= idx < temperatures.len() implies\n 0 <= #[trigger] res[idx] && res[idx] as int <= temperatures.len() - 1 - idx\n && if res[idx] == 0 {\n forall |j: int| idx < j < temperatures.len() ==> temperatures[j] <= temperatures[idx]\n } else {\n let d = res[idx] as int;\n 1 <= d && idx + d < temperatures.len()\n && temperatures[idx + d] > temperatures[idx]\n && forall |j: int| idx < j < idx + d ==> temperatures[j] <= temperatures[idx]\n } by {\n assert(correct_at(temperatures@, res@, idx));\n }\n }\n\n res\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc739/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc739/tests/mutated_testcases.jsonl"} {"problem": "lc74", "family": "leetcode", "description": "# Search a 2D Matrix\n\nYou are given an `m x n` integer matrix `matrix` with the following two properties:\n\n- Each row is sorted in non-decreasing order.\n\n- The first integer of each row is greater than the last integer of the previous row.\n\nGiven an integer `target`, return `true` *if `target` is in `matrix` or `false` otherwise*.\n\nYou must write a solution in `O(log(m * n))` time complexity.\n\n## Example 1:\n\n> Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3\n> Output: true\n\n## Example 2:\n\n> Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13\n> Output: false\n\n## Constraints:\n\n- $m == matrix.length$\n\n- $n == matrix[i].length$\n\n- $1 <= m, n <= 100$\n\n- $-10^4 <= matrix[i][j], target <= 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn search_matrix(matrix: Vec>, target: i32) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nArray, Binary Search, Matrix\nAcceptance rate: 53.2%\n", "spec": "use vstd::prelude::*;\nuse vstd::arithmetic::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn search_matrix(matrix: Vec>, target: i32) -> (res: bool) \n requires\n 1 <= matrix.len() <= 100, \n 1 <= matrix[0].len() <= 100, \n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len()\n ==> -10_000 <= #[trigger] matrix[i][j] <= 10_000, \n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1], \n forall |i: int| 1 <= i < matrix.len() ==> \n #[trigger] matrix[i][0] > matrix[i - 1][matrix[0].len() - 1],\n -10_000 <= target <= 10_000, \n ensures\n res == (exists |i: int, j: int| \n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() && matrix[i][j] == target),\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn search_matrix(matrix: Vec>, target: i32) -> bool \n {\n let rows = matrix.len() as i32;\n let cols = matrix[0].len() as i32;\n\n let mut start = 0;\n let mut end = rows * cols - 1;\n\n while start <= end \n {\n let mid = start + (end - start) / 2;\n let mid_value = matrix[(mid / cols) as usize][(mid % cols) as usize];\n\n if mid_value == target {\n return true;\n } else if mid_value < target {\n start = mid + 1;\n } else {\n end = mid - 1;\n }\n }\n\n false\n }\n}\n", "code_spec": "use vstd::prelude::*;\nuse vstd::arithmetic::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn search_matrix(matrix: Vec>, target: i32) -> (res: bool) \n requires\n 1 <= matrix.len() <= 100, \n 1 <= matrix[0].len() <= 100, \n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len()\n ==> -10_000 <= #[trigger] matrix[i][j] <= 10_000, \n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1], \n forall |i: int| 1 <= i < matrix.len() ==> \n #[trigger] matrix[i][0] > matrix[i - 1][matrix[0].len() - 1],\n -10_000 <= target <= 10_000, \n ensures\n res == (exists |i: int, j: int| \n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() && matrix[i][j] == target),\n {\n let rows = matrix.len() as i32;\n let cols = matrix[0].len() as i32;\n\n let mut start = 0;\n let mut end = rows * cols - 1;\n\n while start <= end \n {\n let mid = start + (end - start) / 2;\n let mid_value = matrix[(mid / cols) as usize][(mid % cols) as usize];\n\n if mid_value == target {\n return true;\n } else if mid_value < target {\n start = mid + 1;\n } else {\n end = mid - 1;\n }\n }\n\n false\n }\n}\n\n}", "verified": "use vstd::prelude::*;\nuse vstd::arithmetic::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n proof fn lemma_row_transitive(matrix: Vec>, i: int, j1: int, j2: int)\n requires\n 0 <= i < matrix.len(),\n 0 <= j1 <= j2 < matrix[i].len(),\n forall |j: int| 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1],\n ensures\n matrix[i][j1] <= matrix[i][j2],\n decreases j2 - j1,\n {\n if j1 < j2 {\n Self::lemma_row_transitive(matrix, i, j1, j2 - 1);\n }\n }\n\n proof fn lemma_between_rows(matrix: Vec>, i1: int, i2: int, cols: int)\n requires\n 0 <= i1 < i2 < matrix.len(),\n cols == matrix[0].len(),\n 1 <= cols <= 100,\n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1],\n forall |i: int| 1 <= i < matrix.len() ==> \n #[trigger] matrix[i][0] > matrix[i - 1][matrix[0].len() - 1],\n ensures\n matrix[i1][cols - 1] < matrix[i2][0],\n decreases i2 - i1,\n {\n if i1 + 1 == i2 {\n } else {\n Self::lemma_between_rows(matrix, i1 + 1, i2, cols);\n Self::lemma_row_transitive(matrix, i1 + 1, 0, cols - 1);\n }\n }\n\n proof fn lemma_matrix_sorted_implies_flat_sorted(matrix: Vec>, idx1: int, idx2: int, cols: int)\n requires\n 1 <= matrix.len() <= 100,\n 1 <= matrix[0].len() <= 100,\n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1],\n forall |i: int| 1 <= i < matrix.len() ==> \n #[trigger] matrix[i][0] > matrix[i - 1][matrix[0].len() - 1],\n cols == matrix[0].len(),\n 0 <= idx1 < idx2 < matrix.len() * cols,\n ensures\n matrix[idx1 / cols][idx1 % cols] <= matrix[idx2 / cols][idx2 % cols],\n {\n let i1 = idx1 / cols;\n let j1 = idx1 % cols;\n let i2 = idx2 / cols;\n let j2 = idx2 % cols;\n \n vstd::arithmetic::div_mod::lemma_fundamental_div_mod(idx1, cols);\n vstd::arithmetic::div_mod::lemma_fundamental_div_mod(idx2, cols);\n \n assert(i1 < matrix.len()) by(nonlinear_arith)\n requires\n idx1 == i1 * cols + j1,\n idx1 < matrix.len() * cols,\n 0 <= j1 < cols,\n cols >= 1,\n {}\n\n assert(i2 < matrix.len()) by(nonlinear_arith)\n requires\n idx2 == i2 * cols + j2,\n idx2 < matrix.len() * cols,\n 0 <= j2 < cols,\n cols >= 1,\n {}\n \n if i1 == i2 {\n Self::lemma_row_transitive(matrix, i1, j1, j2);\n } else {\n assert(i1 <= i2) by(nonlinear_arith)\n requires\n idx1 == i1 * cols + j1,\n idx2 == i2 * cols + j2,\n idx1 < idx2,\n 0 <= j1 < cols,\n 0 <= j2 < cols,\n cols >= 1,\n {}\n\n assert(i1 < i2);\n \n Self::lemma_row_transitive(matrix, i1, j1, cols - 1);\n assert(matrix[i1][j1] <= matrix[i1][cols - 1]);\n \n Self::lemma_between_rows(matrix, i1, i2, cols);\n assert(matrix[i1][cols - 1] < matrix[i2][0]);\n \n Self::lemma_row_transitive(matrix, i2, 0, j2);\n assert(matrix[i2][0] <= matrix[i2][j2]);\n }\n }\n\n pub fn search_matrix(matrix: Vec>, target: i32) -> (res: bool) \n requires\n 1 <= matrix.len() <= 100, \n 1 <= matrix[0].len() <= 100, \n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len()\n ==> -10_000 <= #[trigger] matrix[i][j] <= 10_000, \n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1], \n forall |i: int| 1 <= i < matrix.len() ==> \n #[trigger] matrix[i][0] > matrix[i - 1][matrix[0].len() - 1],\n -10_000 <= target <= 10_000, \n ensures\n res == (exists |i: int, j: int| \n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() && matrix[i][j] == target),\n {\n let rows = matrix.len() as i32;\n let cols = matrix[0].len() as i32;\n\n assert(rows * cols <= 10_000) by(nonlinear_arith)\n requires\n rows == matrix.len(),\n 1 <= matrix.len() <= 100, \n cols == matrix[0].len(), \n 1 <= matrix[0].len() <= 100, \n {}\n\n let mut start = 0;\n let mut end = rows * cols - 1;\n\n while start <= end \n invariant\n 1 <= matrix.len() <= 100, \n 1 <= matrix[0].len() <= 100, \n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len()\n ==> -10_000 <= #[trigger] matrix[i][j] <= 10_000, \n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[i].len() - 1 ==> \n #[trigger] matrix[i][j] <= matrix[i][j + 1], \n forall |i: int| 1 <= i < matrix.len() ==> \n #[trigger] matrix[i][0] > matrix[i - 1][matrix[0].len() - 1],\n -10_000 <= target <= 10_000, \n rows == matrix.len(),\n cols == matrix[0].len(), \n 0 <= start <= rows * cols,\n -1 <= end < rows * cols,\n start <= end + 1,\n rows * cols <= 10_000,\n forall |idx: int| 0 <= idx < start ==> {\n let i = idx / (cols as int);\n let j = idx % (cols as int);\n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() ==> \n #[trigger] matrix[idx / (cols as int)][idx % (cols as int)] < target\n },\n forall |idx: int| end < idx < rows * cols ==> {\n let i = idx / (cols as int);\n let j = idx % (cols as int);\n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() ==> \n #[trigger] matrix[idx / (cols as int)][idx % (cols as int)] > target\n },\n decreases end - start + 1, \n {\n let mid = start + (end - start) / 2;\n\n assert(0 <= mid / cols < rows) by(nonlinear_arith)\n requires\n 0 <= mid < rows * cols,\n cols == matrix[0].len(),\n 1 <= matrix[0].len() <= 100,\n rows == matrix.len(),\n {}\n\n assert(matrix[mid / cols].len() == matrix[0].len());\n\n let mid_value = matrix[(mid / cols) as usize][(mid % cols) as usize];\n\n if mid_value == target {\n return true;\n } else if mid_value < target {\n assert forall |idx: int| 0 <= idx < mid + 1 implies {\n let i = idx / (cols as int);\n let j = idx % (cols as int);\n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() ==> \n #[trigger] matrix[(idx / (cols as int)) as int][(idx % (cols as int)) as int] < target\n } by {\n if 0 <= idx < mid + 1 {\n let i = idx / (cols as int);\n let j = idx % (cols as int);\n if 0 <= i < matrix.len() && 0 <= j < matrix[i].len() {\n if idx < start {\n } else if idx < mid {\n Self::lemma_matrix_sorted_implies_flat_sorted(matrix, idx, mid as int, cols as int);\n } else {\n assert(matrix[i][j] == mid_value);\n assert(mid_value < target);\n }\n }\n }\n }\n start = mid + 1;\n } else {\n assert forall |idx: int| mid - 1 < idx < rows * cols implies {\n let i = idx / (cols as int);\n let j = idx % (cols as int);\n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() ==> \n #[trigger] matrix[idx / (cols as int)][idx % (cols as int)] > target\n } by {\n if mid - 1 < idx < rows * cols {\n let i = idx / (cols as int);\n let j = idx % (cols as int);\n if 0 <= i < matrix.len() && 0 <= j < matrix[i].len() {\n if end < idx {\n } else if mid < idx {\n Self::lemma_matrix_sorted_implies_flat_sorted(matrix, mid as int, idx, cols as int);\n } else {\n assert(matrix[i][j] == mid_value);\n assert(target < mid_value);\n }\n }\n }\n }\n end = mid - 1;\n }\n }\n\n assert forall |i: int, j: int| \n 0 <= i < matrix.len() && 0 <= j < matrix[i].len() \n implies matrix[i][j] != target \n by {\n if 0 <= i < matrix.len() && 0 <= j < matrix[i].len() {\n let idx = i * (cols as int) + j;\n\n assert(0 <= idx < rows * cols) by(nonlinear_arith)\n requires\n idx == i * (cols as int) + j,\n 0 <= i < matrix.len(),\n 0 <= j < matrix[i].len(),\n matrix[i].len() == cols,\n rows == matrix.len(),\n 1 <= cols <= 100,\n {}\n\n assert(idx / (cols as int) == i) by(nonlinear_arith)\n requires\n idx == i * (cols as int) + j,\n 0 <= j < cols,\n {}\n \n assert(idx % (cols as int) == j) by(nonlinear_arith)\n requires\n idx == i * (cols as int) + j,\n 0 <= j < cols,\n {}\n }\n }\n\n false\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc74/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc74/tests/mutated_testcases.jsonl"} {"problem": "lc746", "family": "leetcode", "description": "# Min Cost Climbing Stairs\n\nYou are given an integer array `cost` where `cost[i]` is the cost of $i^{th}$ step on a staircase. Once you pay the cost, you can either climb one or two steps.\n\nYou can either start from the step with index `0`, or the step with index `1`.\n\nReturn the *minimum cost to reach the top of the floor*.\n\n## Example 1:\n\n\n> **Input:** cost = [10,15,20]\n> **Output:** 15\n> **Explanation:** You will start at index 1.\n> - Pay 15 and climb two steps to reach the top.\n> The total cost is 15.\n\n\n## Example 2:\n\n> **Input:** cost = [1,100,1,1,1,100,1,1,100,1]\n> **Output:** 6\n> **Explanation:** You will start at index 0.\n> - Pay 1 and climb two steps to reach index 2.\n> - Pay 1 and climb two steps to reach index 4.\n> - Pay 1 and climb two steps to reach index 6.\n> - Pay 1 and climb one step to reach index 7.\n> - Pay 1 and climb two steps to reach index 9.\n> - Pay 1 and climb one step to reach the top.\n> The total cost is 6.\n\n## Constraints:\n\n- $2 <= cost.length <= 1000$\n\n- $0 <= cost[i] <= 999$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_cost_climbing_stairs(cost: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Dynamic Programming, Weekly Contest 63\nAcceptance rate: 67.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_cost_to_reach(cost: Seq, i: int) -> int \n decreases i\n {\n if i == 0 {\n cost[0] as int\n } else if i == 1 {\n cost[1] as int\n } else if 2 <= i < cost.len() {\n (cost[i] as int) + if Self::min_cost_to_reach(cost, i-1) < Self::min_cost_to_reach(cost, i-2) {\n Self::min_cost_to_reach(cost, i-1)\n } else {\n Self::min_cost_to_reach(cost, i-2)\n }\n } else {\n 0\n }\n }\n\n pub open spec fn min_cost_climbing_stairs_spec(cost: Seq) -> int\n {\n let n = cost.len();\n if Self::min_cost_to_reach(cost, (n - 1) as int) < Self::min_cost_to_reach(cost, (n - 2) as int) {\n Self::min_cost_to_reach(cost, (n - 1) as int)\n } else {\n Self::min_cost_to_reach(cost, (n - 2) as int)\n }\n }\n\n pub fn min_cost_climbing_stairs(cost: Vec) -> (res: i32) \n requires \n 2 <= cost.len() <= 1000, \n forall |i: int| 0 <= i < cost.len() ==> 0 <= #[trigger] cost[i] <= 999, \n ensures \n res == Self::min_cost_climbing_stairs_spec(cost@)\n {\n \n }\n}\n\n}", "code": "impl Solution {\n pub fn min_cost_climbing_stairs(cost: Vec) -> i32\n {\n let n = cost.len();\n let (mut a, mut b) = (cost[0], cost[1]);\n for i in 2..n \n { \n let c = cost[i] + if a < b { a } else { b };\n a = b;\n b = c;\n }\n if a < b { a } else { b }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_cost_to_reach(cost: Seq, i: int) -> int \n decreases i\n {\n if i == 0 {\n cost[0] as int\n } else if i == 1 {\n cost[1] as int\n } else if 2 <= i < cost.len() {\n (cost[i] as int) + if Self::min_cost_to_reach(cost, i-1) < Self::min_cost_to_reach(cost, i-2) {\n Self::min_cost_to_reach(cost, i-1)\n } else {\n Self::min_cost_to_reach(cost, i-2)\n }\n } else {\n 0\n }\n }\n\n pub open spec fn min_cost_climbing_stairs_spec(cost: Seq) -> int\n {\n let n = cost.len();\n if Self::min_cost_to_reach(cost, (n - 1) as int) < Self::min_cost_to_reach(cost, (n - 2) as int) {\n Self::min_cost_to_reach(cost, (n - 1) as int)\n } else {\n Self::min_cost_to_reach(cost, (n - 2) as int)\n }\n }\n\n pub fn min_cost_climbing_stairs(cost: Vec) -> (res: i32) \n requires \n 2 <= cost.len() <= 1000, \n forall |i: int| 0 <= i < cost.len() ==> 0 <= #[trigger] cost[i] <= 999, \n ensures \n res == Self::min_cost_climbing_stairs_spec(cost@)\n {\n let n = cost.len();\n let (mut a, mut b) = (cost[0], cost[1]);\n for i in 2..n \n { \n let c = cost[i] + if a < b { a } else { b };\n a = b;\n b = c;\n }\n if a < b { a } else { b }\n }\n}\n\n}", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn min_cost_to_reach(cost: Seq, i: int) -> int \n decreases i\n {\n if i == 0 {\n cost[0] as int\n } else if i == 1 {\n cost[1] as int\n } else if 2 <= i < cost.len() {\n (cost[i] as int) + if Self::min_cost_to_reach(cost, i-1) < Self::min_cost_to_reach(cost, i-2) {\n Self::min_cost_to_reach(cost, i-1)\n } else {\n Self::min_cost_to_reach(cost, i-2)\n }\n } else {\n 0\n }\n }\n\n pub open spec fn min_cost_climbing_stairs_spec(cost: Seq) -> int\n {\n let n = cost.len();\n if Self::min_cost_to_reach(cost, (n - 1) as int) < Self::min_cost_to_reach(cost, (n - 2) as int) {\n Self::min_cost_to_reach(cost, (n - 1) as int)\n } else {\n Self::min_cost_to_reach(cost, (n - 2) as int)\n }\n }\n\n proof fn min_cost_bounds(cost: Seq, i: int)\n requires\n 2 <= cost.len() <= 1000,\n forall |j: int| 0 <= j < cost.len() ==> 0 <= #[trigger] cost[j] <= 999,\n 0 <= i < cost.len(),\n ensures\n 0 <= Self::min_cost_to_reach(cost, i) <= 999 * (i + 1),\n decreases i\n {\n if i >= 2 {\n Self::min_cost_bounds(cost, i - 1);\n Self::min_cost_bounds(cost, i - 2);\n }\n }\n\n pub fn min_cost_climbing_stairs(cost: Vec) -> (res: i32) \n requires \n 2 <= cost.len() <= 1000, \n forall |i: int| 0 <= i < cost.len() ==> 0 <= #[trigger] cost[i] <= 999, \n ensures \n res == Self::min_cost_climbing_stairs_spec(cost@)\n {\n let n = cost.len();\n let (mut a, mut b) = (cost[0], cost[1]);\n for i in 2..n \n invariant\n 2 <= cost.len() <= 1000, \n forall |i: int| 0 <= i < cost.len() ==> 0 <= #[trigger] cost[i] <= 999, \n n == cost.len(),\n 2 <= i <= n,\n forall |j: int| 0 <= j < cost@.len() ==> 0 <= #[trigger] cost@[j] <= 999,\n a as int == Self::min_cost_to_reach(cost@, (i - 2) as int),\n b as int == Self::min_cost_to_reach(cost@, (i - 1) as int),\n 0 <= a <= 999000,\n 0 <= b <= 999000,\n { \n proof {\n Self::min_cost_bounds(cost@, i as int);\n Self::min_cost_bounds(cost@, (i - 1) as int);\n Self::min_cost_bounds(cost@, (i - 2) as int);\n }\n\n let c = cost[i] + if a < b { a } else { b };\n a = b;\n b = c;\n }\n if a < b { a } else { b }\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc746/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc746/tests/mutated_testcases.jsonl"} {"problem": "lc766", "family": "leetcode", "description": "# Toeplitz Matrix\n\nGiven an `m x n` matrix `matrix`, return `true` if the matrix is Toeplitz. Otherwise, return `false`.\n\nA matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.\n\n## Example 1:\n\n> **Input:** matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]\n> **Output:** true\n> **Explanation:** The diagonals are `[9]`, `[5, 5]`, `[1, 1, 1]`, `[2, 2, 2]`, `[3, 3]`, and `[4]`. Every diagonal has the same value in each position.\n\n## Example 2:\n\n> **Input:** matrix = [[1,2],[2,2]]\n> **Output:** false\n> **Explanation:** The diagonal `[1, 2]` contains different values.\n\n## Constraints:\n\n- `m == matrix.length`\n- `n == matrix[i].length`\n- `1 <= m, n <= 20`\n- `0 <= matrix[i][j] <= 99`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_toeplitz_matrix(matrix: Vec>) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray, Matrix\nAcceptance rate: 69.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_toeplitz(matrix: Seq>) -> bool {\n forall |i: int, j: int|\n 1 <= i < matrix.len() && 1 <= j < matrix[0].len() ==> #[trigger] matrix[i][j] == matrix[i - 1][j - 1]\n}\n\nimpl Solution {\n pub fn is_toeplitz_matrix(matrix: Vec>) -> (result: bool)\n requires\n 1 <= matrix.len() <= 20,\n forall |i: int| 0 <= i < matrix.len() ==> 1 <= #[trigger] matrix[i].len() <= 20,\n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len() ==> 0 <= #[trigger] matrix[i][j] <= 99,\n ensures\n result == is_toeplitz(matrix@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_toeplitz_matrix(matrix: Vec>) -> bool\n {\n let rows = matrix.len();\n let cols = matrix[0].len();\n let mut i: usize = 1;\n while i < rows\n {\n let mut j: usize = 1;\n while j < cols\n {\n if matrix[i][j] != matrix[i - 1][j - 1] {\n return false;\n }\n j += 1;\n }\n i += 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_toeplitz(matrix: Seq>) -> bool {\n forall |i: int, j: int|\n 1 <= i < matrix.len() && 1 <= j < matrix[0].len() ==> #[trigger] matrix[i][j] == matrix[i - 1][j - 1]\n}\n\nimpl Solution {\n pub fn is_toeplitz_matrix(matrix: Vec>) -> (result: bool)\n requires\n 1 <= matrix.len() <= 20,\n forall |i: int| 0 <= i < matrix.len() ==> 1 <= #[trigger] matrix[i].len() <= 20,\n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len() ==> 0 <= #[trigger] matrix[i][j] <= 99,\n ensures\n result == is_toeplitz(matrix@),\n {\n let rows = matrix.len();\n let cols = matrix[0].len();\n let mut i: usize = 1;\n while i < rows\n {\n let mut j: usize = 1;\n while j < cols\n {\n if matrix[i][j] != matrix[i - 1][j - 1] {\n return false;\n }\n j += 1;\n }\n i += 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn is_toeplitz(matrix: Seq>) -> bool {\n forall |i: int, j: int|\n 1 <= i < matrix.len() && 1 <= j < matrix[0].len() ==> #[trigger] matrix[i][j] == matrix[i - 1][j - 1]\n}\n\nimpl Solution {\n pub fn is_toeplitz_matrix(matrix: Vec>) -> (result: bool)\n requires\n 1 <= matrix.len() <= 20,\n forall |i: int| 0 <= i < matrix.len() ==> 1 <= #[trigger] matrix[i].len() <= 20,\n forall |i: int| 0 <= i < matrix.len() ==> #[trigger] matrix[i].len() == matrix[0].len(),\n forall |i: int, j: int| 0 <= i < matrix.len() && 0 <= j < matrix[0].len() ==> 0 <= #[trigger] matrix[i][j] <= 99,\n ensures\n result == is_toeplitz(matrix@),\n {\n let rows = matrix.len();\n let cols = matrix[0].len();\n let mut i: usize = 1;\n while i < rows\n invariant\n rows == matrix.len(),\n cols == matrix[0].len(),\n 1 <= rows <= 20,\n 1 <= cols <= 20,\n 1 <= i <= rows,\n forall |r: int| 0 <= r < rows ==> 1 <= #[trigger] matrix[r].len() <= 20,\n forall |r: int| 0 <= r < rows ==> #[trigger] matrix[r].len() == cols,\n forall |r: int, c: int| 0 <= r < rows && 0 <= c < cols ==> 0 <= #[trigger] matrix[r][c] <= 99,\n forall |r: int, c: int|\n 1 <= r < i && 1 <= c < cols ==> #[trigger] matrix[r][c] == matrix[r - 1][c - 1],\n decreases rows - i,\n {\n let mut j: usize = 1;\n while j < cols\n invariant\n rows == matrix.len(),\n cols == matrix[0].len(),\n 1 <= rows <= 20,\n 1 <= cols <= 20,\n 1 <= i < rows,\n 1 <= j <= cols,\n forall |r: int| 0 <= r < rows ==> 1 <= #[trigger] matrix[r].len() <= 20,\n forall |r: int| 0 <= r < rows ==> #[trigger] matrix[r].len() == cols,\n forall |r: int, c: int| 0 <= r < rows && 0 <= c < cols ==> 0 <= #[trigger] matrix[r][c] <= 99,\n forall |r: int, c: int|\n 1 <= r < i && 1 <= c < cols ==> #[trigger] matrix[r][c] == matrix[r - 1][c - 1],\n forall |c: int|\n 1 <= c < j ==> #[trigger] matrix[i as int][c] == matrix[i as int - 1][c - 1],\n decreases cols - j,\n {\n proof {\n assert(1 <= i);\n assert(i < rows);\n assert(1 <= j);\n assert(j < cols);\n assert(matrix[i as int].len() == cols);\n assert(matrix[i as int - 1].len() == cols);\n }\n if matrix[i][j] != matrix[i - 1][j - 1] {\n proof {\n assert(!is_toeplitz(matrix@)) by {\n if is_toeplitz(matrix@) {\n assert(1 <= i);\n assert(i < matrix.len());\n assert(1 <= j);\n assert(j < matrix[0].len());\n assert(matrix[i as int][j as int] == matrix[i as int - 1][j as int - 1]);\n assert(false);\n }\n };\n }\n return false;\n }\n j += 1;\n }\n proof {\n assert(j == cols);\n assert forall |r: int, c: int|\n 1 <= r < i as int + 1 && 1 <= c < cols implies #[trigger] matrix[r][c] == matrix[r - 1][c - 1] by {\n if r < i as int {\n } else {\n assert(r == i as int);\n }\n };\n }\n i += 1;\n }\n proof {\n assert(i == rows);\n assert(is_toeplitz(matrix@)) by {\n assert forall |r: int, c: int|\n 1 <= r < matrix.len() && 1 <= c < matrix[0].len() implies #[trigger] matrix[r][c] == matrix[r - 1][c - 1] by {\n assert(r < i as int);\n };\n };\n }\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc766/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc766/tests/mutated_testcases.jsonl"} {"problem": "lc768", "family": "leetcode", "description": "# Max Chunks To Make Sorted II\n\nYou are given an integer array `arr`.\n\nWe split `arr` into some number of **chunks** (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.\n\nReturn the largest number of chunks we can make to sort the array.\n\n## Example 1:\n\n> **Input:** arr = [5,4,3,2,1]\n> **Output:** 1\n> **Explanation:** Splitting into two or more chunks will not return the required result. For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.\n\n## Example 2:\n\n> **Input:** arr = [2,1,3,4,4]\n> **Output:** 4\n> **Explanation:** We can split into two chunks, such as [2, 1], [3, 4, 4]. However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.\n\n## Constraints:\n\n- $1 <= arr.length <= 2000$\n\n- $0 <= arr[i] <= 10^8$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_chunks_to_sorted(arr: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nArray, Stack, Greedy, Sorting, Monotonic Stack\nAcceptance rate: 54.7%\n", "spec": "use vstd::prelude::*;\nfn main() {}\nverus! {\npub struct Solution;\nimpl Solution {\n pub open spec fn prefix_max_spec(s: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 { s[0] as int }\n else {\n let prev = Self::prefix_max_spec(s, i - 1);\n if s[i] as int > prev { s[i] as int } else { prev }\n }\n }\n\n pub open spec fn suffix_min_spec(s: Seq, i: int) -> int\n decreases s.len() - i,\n {\n if i >= s.len() - 1 { s[s.len() - 1] as int }\n else {\n let next = Self::suffix_min_spec(s, i + 1);\n if next > s[i] as int { s[i] as int } else { next }\n }\n }\n\n pub open spec fn is_chunk_boundary(s: Seq, i: int) -> bool {\n Self::prefix_max_spec(s, i) <= Self::suffix_min_spec(s, i + 1)\n }\n\n pub open spec fn count_boundaries(s: Seq, i: int) -> int\n decreases i + 1,\n {\n if i < 0 { 0 }\n else {\n Self::count_boundaries(s, i - 1)\n + if Self::is_chunk_boundary(s, i) { 1int } else { 0int }\n }\n }\n\n pub fn max_chunks_to_sorted(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 2_000,\n forall|i: int| 0 <= i < arr.len() ==> 0 <= #[trigger] arr[i] <= 100_000_000,\n ensures\n result >= 1,\n result as int == 1 + Self::count_boundaries(arr@, arr@.len() as int - 2),\n {\n }\n}\n}\n", "code": "impl Solution {\n pub fn max_chunks_to_sorted(arr: Vec) -> i32 {\n let n = arr.len();\n \n let mut suffix_min: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n suffix_min.push(arr[i]);\n i += 1;\n }\n \n if n > 1 {\n let mut i: usize = n - 1;\n while i > 0 {\n i -= 1;\n if suffix_min[i + 1] < suffix_min[i] {\n suffix_min[i] = suffix_min[i + 1];\n }\n }\n }\n \n let mut chunks: i32 = 1;\n let mut prefix_max = arr[0];\n let mut i: usize = 0;\n while i < n - 1 {\n if arr[i] > prefix_max {\n prefix_max = arr[i];\n }\n if prefix_max <= suffix_min[i + 1] {\n chunks += 1;\n }\n i += 1;\n }\n chunks\n }\n}\n", "code_spec": "use vstd::prelude::*;\nfn main() {}\nverus! {\npub struct Solution;\nimpl Solution {\n pub open spec fn prefix_max_spec(s: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 { s[0] as int }\n else {\n let prev = Self::prefix_max_spec(s, i - 1);\n if s[i] as int > prev { s[i] as int } else { prev }\n }\n }\n\n pub open spec fn suffix_min_spec(s: Seq, i: int) -> int\n decreases s.len() - i,\n {\n if i >= s.len() - 1 { s[s.len() - 1] as int }\n else {\n let next = Self::suffix_min_spec(s, i + 1);\n if next > s[i] as int { s[i] as int } else { next }\n }\n }\n\n pub open spec fn is_chunk_boundary(s: Seq, i: int) -> bool {\n Self::prefix_max_spec(s, i) <= Self::suffix_min_spec(s, i + 1)\n }\n\n pub open spec fn count_boundaries(s: Seq, i: int) -> int\n decreases i + 1,\n {\n if i < 0 { 0 }\n else {\n Self::count_boundaries(s, i - 1)\n + if Self::is_chunk_boundary(s, i) { 1int } else { 0int }\n }\n }\n\n pub fn max_chunks_to_sorted(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 2_000,\n forall|i: int| 0 <= i < arr.len() ==> 0 <= #[trigger] arr[i] <= 100_000_000,\n ensures\n result >= 1,\n result as int == 1 + Self::count_boundaries(arr@, arr@.len() as int - 2),\n {\n let n = arr.len();\n \n let mut suffix_min: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n {\n suffix_min.push(arr[i]);\n i += 1;\n }\n \n if n > 1 {\n let mut i: usize = n - 1;\n while i > 0 {\n i -= 1;\n if suffix_min[i + 1] < suffix_min[i] {\n suffix_min.set(i, suffix_min[i + 1]);\n }\n }\n }\n \n let mut chunks: i32 = 1;\n let mut prefix_max = arr[0];\n let mut i: usize = 0;\n while i < n - 1 {\n if arr[i] > prefix_max {\n prefix_max = arr[i];\n }\n if prefix_max <= suffix_min[i + 1] {\n chunks += 1;\n }\n i += 1;\n }\n chunks\n }\n}\n}\n", "verified": "use vstd::prelude::*;\nfn main() {}\nverus! {\npub struct Solution;\nimpl Solution {\n pub open spec fn prefix_max_spec(s: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 { s[0] as int }\n else {\n let prev = Self::prefix_max_spec(s, i - 1);\n if s[i] as int > prev { s[i] as int } else { prev }\n }\n }\n\n pub open spec fn suffix_min_spec(s: Seq, i: int) -> int\n decreases s.len() - i,\n {\n if i >= s.len() - 1 { s[s.len() - 1] as int }\n else {\n let next = Self::suffix_min_spec(s, i + 1);\n if next > s[i] as int { s[i] as int } else { next }\n }\n }\n\n pub open spec fn is_chunk_boundary(s: Seq, i: int) -> bool {\n Self::prefix_max_spec(s, i) <= Self::suffix_min_spec(s, i + 1)\n }\n\n pub open spec fn count_boundaries(s: Seq, i: int) -> int\n decreases i + 1,\n {\n if i < 0 { 0 }\n else {\n Self::count_boundaries(s, i - 1)\n + if Self::is_chunk_boundary(s, i) { 1int } else { 0int }\n }\n }\n\n proof fn lemma_prefix_max_bounds(s: Seq, i: int)\n requires\n s.len() > 0,\n 0 <= i < s.len(),\n forall|j: int| 0 <= j < s.len() ==> 0 <= #[trigger] s[j] <= 100_000_000,\n ensures\n 0 <= Self::prefix_max_spec(s, i) <= 100_000_000,\n decreases i,\n {\n if i > 0 {\n Self::lemma_prefix_max_bounds(s, i - 1);\n }\n }\n\n proof fn lemma_suffix_min_bounds(s: Seq, i: int)\n requires\n s.len() > 0,\n 0 <= i < s.len(),\n forall|j: int| 0 <= j < s.len() ==> 0 <= #[trigger] s[j] <= 100_000_000,\n ensures\n 0 <= Self::suffix_min_spec(s, i) <= 100_000_000,\n decreases s.len() - i,\n {\n if i < s.len() - 1 {\n Self::lemma_suffix_min_bounds(s, i + 1);\n }\n }\n\n proof fn lemma_count_boundaries_bounds(s: Seq, i: int)\n requires\n s.len() > 0,\n -1 <= i < s.len(),\n ensures\n 0 <= Self::count_boundaries(s, i) <= i + 1,\n decreases i + 1,\n {\n if i >= 0 {\n Self::lemma_count_boundaries_bounds(s, i - 1);\n }\n }\n\n pub fn max_chunks_to_sorted(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 2_000,\n forall|i: int| 0 <= i < arr.len() ==> 0 <= #[trigger] arr[i] <= 100_000_000,\n ensures\n result >= 1,\n result as int == 1 + Self::count_boundaries(arr@, arr@.len() as int - 2),\n {\n let n = arr.len();\n\n \n let mut suffix_min: Vec = Vec::with_capacity(n);\n let mut i: usize = 0;\n while i < n\n invariant\n n == arr.len(),\n 0 <= i <= n,\n suffix_min.len() == i,\n forall|j: int| 0 <= j < i as int ==> suffix_min[j] == arr[j],\n decreases n - i,\n {\n suffix_min.push(arr[i]);\n i += 1;\n }\n\n \n if n > 1 {\n let mut i: usize = n - 1;\n while i > 0\n invariant\n n == arr.len(),\n n > 1,\n 0 <= i < n,\n suffix_min.len() == n,\n forall|j: int| i as int <= j < n as int\n ==> suffix_min[j] as int == Self::suffix_min_spec(arr@, j),\n forall|j: int| 0 <= j < i as int ==> suffix_min[j] == arr[j],\n forall|j: int| 0 <= j < arr.len()\n ==> 0 <= #[trigger] arr[j] <= 100_000_000,\n decreases i,\n {\n i -= 1;\n \n \n \n \n if suffix_min[i + 1] < suffix_min[i] {\n suffix_min.set(i, suffix_min[i + 1]);\n }\n \n }\n }\n \n \n\n \n let mut chunks: i32 = 1;\n let mut prefix_max = arr[0];\n let mut i: usize = 0;\n proof {\n Self::lemma_count_boundaries_bounds(arr@, -1int);\n }\n while i < n - 1\n invariant\n n == arr.len(),\n n >= 1,\n n <= 2_000,\n 0 <= i <= n - 1,\n suffix_min.len() == n,\n forall|j: int| 0 <= j < n as int\n ==> suffix_min[j] as int == Self::suffix_min_spec(arr@, j),\n forall|j: int| 0 <= j < arr.len()\n ==> 0 <= #[trigger] arr[j] <= 100_000_000,\n i == 0 ==> prefix_max as int == arr[0int] as int,\n i > 0 ==> prefix_max as int == Self::prefix_max_spec(arr@, (i - 1) as int),\n (chunks - 1) as int == Self::count_boundaries(arr@, i as int - 1),\n chunks >= 1,\n chunks as int <= i as int + 1,\n 0 <= prefix_max <= 100_000_000,\n decreases n - 1 - i,\n {\n \n if arr[i] > prefix_max {\n prefix_max = arr[i];\n }\n \n \n \n\n \n if prefix_max <= suffix_min[i + 1] {\n chunks += 1;\n }\n \n \n\n proof {\n Self::lemma_count_boundaries_bounds(arr@, i as int);\n }\n\n i += 1;\n }\n chunks\n }\n}\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc768/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc768/tests/mutated_testcases.jsonl"} {"problem": "lc775", "family": "leetcode", "description": "# Global and Local Inversions\n\nYou are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.\n\nThe number of **global inversions** is the number of the different pairs `(i, j)` where:\n\n\t- `0 <= i < j < n`\n\n\t- `nums[i] > nums[j]`\n\nThe number of **local inversions** is the number of indices `i` where:\n\n\t- `0 <= i < n - 1`\n\n\t- `nums[i] > nums[i + 1]`\n\nReturn `true` if the number of global inversions is equal to the number of local inversions.\n\n## Example 1:\n\n> **Input:** nums = [1,0,2]\n> **Output:** true\n> **Explanation:** There is 1 global inversion and 1 local inversion.\n\n## Example 2:\n\n> **Input:** nums = [1,2,0]\n> **Output:** false\n> **Explanation:** There are 2 global inversions and 1 local inversion.\n\n## Constraints:\n\n- $n == nums.length$\n- $1 <= n <= 10^5$\n- $0 <= nums[i] < n$\n- All the integers of `nums` are **unique**.\n- `nums` is a permutation of all the numbers in the range `[0, n - 1]`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_ideal_permutation(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nArray, Math\nAcceptance rate: 42.7%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn no_nonlocal_inversion(nums: Seq) -> bool {\n forall |i: int, j: int|\n 0 <= i < j < nums.len() && i + 1 < j ==> nums[i] < nums[j]\n }\n\n pub fn is_ideal_permutation(nums: Vec) -> (res: bool)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < nums.len(),\n forall |i: int, j: int| #![trigger nums[i], nums[j]]\n 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n res <==> Self::no_nonlocal_inversion(nums@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_ideal_permutation(nums: Vec) -> bool {\n let n = nums.len();\n if n <= 2 {\n return true;\n }\n\n let mut cmax = nums[0];\n let mut j: usize = 2;\n while j < n {\n if cmax > nums[j] {\n return false;\n }\n let prev = nums[j - 1];\n if prev > cmax {\n cmax = prev;\n }\n j += 1;\n }\n\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn no_nonlocal_inversion(nums: Seq) -> bool {\n forall |i: int, j: int|\n 0 <= i < j < nums.len() && i + 1 < j ==> nums[i] < nums[j]\n }\n\n pub fn is_ideal_permutation(nums: Vec) -> (res: bool)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < nums.len(),\n forall |i: int, j: int| #![trigger nums[i], nums[j]]\n 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n res <==> Self::no_nonlocal_inversion(nums@),\n {\n let n = nums.len();\n if n <= 2 {\n return true;\n }\n\n let mut cmax = nums[0];\n let mut j: usize = 2;\n while j < n {\n if cmax > nums[j] {\n return false;\n }\n let prev = nums[j - 1];\n if prev > cmax {\n cmax = prev;\n }\n j += 1;\n }\n\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn no_nonlocal_inversion(nums: Seq) -> bool {\n forall |i: int, j: int|\n 0 <= i < j < nums.len() && i + 1 < j ==> nums[i] < nums[j]\n }\n\n pub fn is_ideal_permutation(nums: Vec) -> (res: bool)\n requires\n 1 <= nums.len() <= 100_000,\n forall |i: int| 0 <= i < nums.len() ==> 0 <= #[trigger] nums[i] < nums.len(),\n forall |i: int, j: int| #![trigger nums[i], nums[j]]\n 0 <= i < j < nums.len() ==> nums[i] != nums[j],\n ensures\n res <==> Self::no_nonlocal_inversion(nums@),\n {\n let n = nums.len();\n if n <= 2 {\n return true;\n }\n\n let mut cmax = nums[0];\n let mut j: usize = 2;\n while j < n\n invariant\n n == nums.len(),\n 2 <= j <= n,\n forall |k: int| 0 <= k < j as int - 1 ==> nums[k] <= cmax,\n exists |k: int| 0 <= k < j as int - 1 && nums[k] == cmax,\n forall |t: int| #![trigger nums[t]] 2 <= t < j as int ==>\n forall |k: int| #![trigger nums[k]] 0 <= k < t - 1 ==> nums[k] <= nums[t],\n decreases n - j\n {\n if cmax > nums[j] {\n return false;\n }\n\n proof {\n assert(forall |k: int| 0 <= k < j as int - 1 ==> nums[k] <= nums[j as int]);\n }\n\n let prev = nums[j - 1];\n if prev > cmax {\n cmax = prev;\n }\n j += 1;\n }\n\n proof {\n assert(forall |t: int| #![trigger nums[t]] 2 <= t < n as int ==>\n forall |k: int| #![trigger nums[k]] 0 <= k < t - 1 ==> nums[k] <= nums[t]);\n assert(forall |i: int, j: int| #![trigger nums[i], nums[j]]\n 0 <= i < j < n as int ==> nums[i] != nums[j]);\n assert(forall |i: int, j: int|\n 0 <= i < j < n as int && i + 1 < j ==> nums[i] < nums[j]);\n assert(Self::no_nonlocal_inversion(nums@));\n }\n\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc775/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc775/tests/mutated_testcases.jsonl"} {"problem": "lc801", "family": "leetcode", "description": "# Minimum Swaps To Make Sequences Increasing\n\nYou are given two integer arrays `nums1` and `nums2` of the same length `n`. In one operation you may swap `nums1[i]` and `nums2[i]` for any index `0 <= i < n`. Return the **minimum number of swaps** to make both arrays strictly increasing. The test cases are generated so that a solution always exists.\n\n## Example 1:\n\n> **Input:** nums1 = [1,3,5,4], nums2 = [1,2,3,7]\n> **Output:** 1\n> **Explanation:** Swap nums1[3] and nums2[3]. Then nums1 = [1,3,5,7] and nums2 = [1,2,3,4], both strictly increasing.\n\n## Example 2:\n\n> **Input:** nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,7]\n> **Output:** 1\n\n## Constraints:\n\n- $2 \\leq \\texttt{nums1.length} \\leq 10^5$\n- $\\texttt{nums1.length} = \\texttt{nums2.length}$\n- $0 \\leq \\texttt{nums1[i]},\\, \\texttt{nums2[i]} \\leq 2 \\times 10^5$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn min_swap(nums1: Vec, nums2: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Hard\nSenior Staff, Array, Dynamic Programming, Weekly Contest 76\nAcceptance rate: 51.8%", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn keep_spec(nums1: Seq, nums2: Seq, i: int) -> int\n recommends\n 0 <= i < nums1.len(),\n nums1.len() == nums2.len(),\n decreases i,\n {\n if i <= 0 {\n 0int\n } else {\n let pa = nums1[i - 1] as int;\n let pb = nums2[i - 1] as int;\n let a = nums1[i] as int;\n let b = nums2[i] as int;\n let prev_keep = Self::keep_spec(nums1, nums2, i - 1);\n let prev_swap = Self::swap_spec(nums1, nums2, i - 1);\n let from_keep = if a > pa && b > pb { prev_keep } else { i + 1 };\n let from_swap = if a > pb && b > pa { prev_swap } else { i + 1 };\n if from_keep <= from_swap { from_keep } else { from_swap }\n }\n }\n\n pub open spec fn swap_spec(nums1: Seq, nums2: Seq, i: int) -> int\n recommends\n 0 <= i < nums1.len(),\n nums1.len() == nums2.len(),\n decreases i,\n {\n if i <= 0 {\n 1int\n } else {\n let pa = nums1[i - 1] as int;\n let pb = nums2[i - 1] as int;\n let a = nums1[i] as int;\n let b = nums2[i] as int;\n let prev_keep = Self::keep_spec(nums1, nums2, i - 1);\n let prev_swap = Self::swap_spec(nums1, nums2, i - 1);\n let from_keep = if a > pb && b > pa { prev_keep + 1 } else { i + 1 };\n let from_swap = if a > pa && b > pb { prev_swap + 1 } else { i + 1 };\n if from_keep <= from_swap { from_keep } else { from_swap }\n }\n }\n\n pub fn min_swap(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 2 <= nums1.len() <= 100_000,\n nums1.len() == nums2.len(),\n forall|i: int| 0 <= i < nums1.len() ==>\n 0 <= #[trigger] nums1[i] <= 200_000,\n forall|i: int| 0 <= i < nums2.len() ==>\n 0 <= #[trigger] nums2[i] <= 200_000,\n forall|i: int| 1 <= i < nums1.len() ==>\n ((#[trigger] nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1])\n || (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1])),\n ensures\n result >= 0,\n result as int == {\n let n = nums1@.len();\n let k = Self::keep_spec(nums1@, nums2@, (n - 1) as int);\n let s = Self::swap_spec(nums1@, nums2@, (n - 1) as int);\n if k <= s { k } else { s }\n },\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn min_swap(nums1: Vec, nums2: Vec) -> i32 {\n let n = nums1.len();\n let mut keep: i32 = 0;\n let mut swap: i32 = 1;\n let mut i: usize = 1;\n while i < n {\n let a = nums1[i];\n let b = nums2[i];\n let pa = nums1[i - 1];\n let pb = nums2[i - 1];\n let mut new_keep: i32 = i as i32 + 1;\n let mut new_swap: i32 = i as i32 + 1;\n if a > pa && b > pb {\n if keep < new_keep { new_keep = keep; }\n if swap + 1 < new_swap { new_swap = swap + 1; }\n }\n if a > pb && b > pa {\n if swap < new_keep { new_keep = swap; }\n if keep + 1 < new_swap { new_swap = keep + 1; }\n }\n keep = new_keep;\n swap = new_swap;\n i = i + 1;\n }\n if keep < swap { keep } else { swap }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn keep_spec(nums1: Seq, nums2: Seq, i: int) -> int\n recommends\n 0 <= i < nums1.len(),\n nums1.len() == nums2.len(),\n decreases i,\n {\n if i <= 0 {\n 0int\n } else {\n let pa = nums1[i - 1] as int;\n let pb = nums2[i - 1] as int;\n let a = nums1[i] as int;\n let b = nums2[i] as int;\n let prev_keep = Self::keep_spec(nums1, nums2, i - 1);\n let prev_swap = Self::swap_spec(nums1, nums2, i - 1);\n let from_keep = if a > pa && b > pb { prev_keep } else { i + 1 };\n let from_swap = if a > pb && b > pa { prev_swap } else { i + 1 };\n if from_keep <= from_swap { from_keep } else { from_swap }\n }\n }\n\n pub open spec fn swap_spec(nums1: Seq, nums2: Seq, i: int) -> int\n recommends\n 0 <= i < nums1.len(),\n nums1.len() == nums2.len(),\n decreases i,\n {\n if i <= 0 {\n 1int\n } else {\n let pa = nums1[i - 1] as int;\n let pb = nums2[i - 1] as int;\n let a = nums1[i] as int;\n let b = nums2[i] as int;\n let prev_keep = Self::keep_spec(nums1, nums2, i - 1);\n let prev_swap = Self::swap_spec(nums1, nums2, i - 1);\n let from_keep = if a > pb && b > pa { prev_keep + 1 } else { i + 1 };\n let from_swap = if a > pa && b > pb { prev_swap + 1 } else { i + 1 };\n if from_keep <= from_swap { from_keep } else { from_swap }\n }\n }\n\n pub fn min_swap(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 2 <= nums1.len() <= 100_000,\n nums1.len() == nums2.len(),\n forall|i: int| 0 <= i < nums1.len() ==>\n 0 <= #[trigger] nums1[i] <= 200_000,\n forall|i: int| 0 <= i < nums2.len() ==>\n 0 <= #[trigger] nums2[i] <= 200_000,\n forall|i: int| 1 <= i < nums1.len() ==>\n ((#[trigger] nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1])\n || (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1])),\n ensures\n result >= 0,\n result as int == {\n let n = nums1@.len();\n let k = Self::keep_spec(nums1@, nums2@, (n - 1) as int);\n let s = Self::swap_spec(nums1@, nums2@, (n - 1) as int);\n if k <= s { k } else { s }\n },\n {\n let n = nums1.len();\n let mut keep: i32 = 0;\n let mut swap: i32 = 1;\n let mut i: usize = 1;\n while i < n {\n let a = nums1[i];\n let b = nums2[i];\n let pa = nums1[i - 1];\n let pb = nums2[i - 1];\n let mut new_keep: i32 = i as i32 + 1;\n let mut new_swap: i32 = i as i32 + 1;\n if a > pa && b > pb {\n if keep < new_keep { new_keep = keep; }\n if swap + 1 < new_swap { new_swap = swap + 1; }\n }\n if a > pb && b > pa {\n if swap < new_keep { new_keep = swap; }\n if keep + 1 < new_swap { new_swap = keep + 1; }\n }\n keep = new_keep;\n swap = new_swap;\n i = i + 1;\n }\n if keep < swap { keep } else { swap }\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn keep_spec(nums1: Seq, nums2: Seq, i: int) -> int\n recommends\n 0 <= i < nums1.len(),\n nums1.len() == nums2.len(),\n decreases i,\n {\n if i <= 0 {\n 0int\n } else {\n let pa = nums1[i - 1] as int;\n let pb = nums2[i - 1] as int;\n let a = nums1[i] as int;\n let b = nums2[i] as int;\n let prev_keep = Self::keep_spec(nums1, nums2, i - 1);\n let prev_swap = Self::swap_spec(nums1, nums2, i - 1);\n let from_keep = if a > pa && b > pb { prev_keep } else { i + 1 };\n let from_swap = if a > pb && b > pa { prev_swap } else { i + 1 };\n if from_keep <= from_swap { from_keep } else { from_swap }\n }\n }\n\n pub open spec fn swap_spec(nums1: Seq, nums2: Seq, i: int) -> int\n recommends\n 0 <= i < nums1.len(),\n nums1.len() == nums2.len(),\n decreases i,\n {\n if i <= 0 {\n 1int\n } else {\n let pa = nums1[i - 1] as int;\n let pb = nums2[i - 1] as int;\n let a = nums1[i] as int;\n let b = nums2[i] as int;\n let prev_keep = Self::keep_spec(nums1, nums2, i - 1);\n let prev_swap = Self::swap_spec(nums1, nums2, i - 1);\n let from_keep = if a > pb && b > pa { prev_keep + 1 } else { i + 1 };\n let from_swap = if a > pa && b > pb { prev_swap + 1 } else { i + 1 };\n if from_keep <= from_swap { from_keep } else { from_swap }\n }\n }\n\n proof fn spec_bounds(nums1: Seq, nums2: Seq, i: int)\n requires\n 2 <= nums1.len(),\n nums1.len() == nums2.len(),\n 0 <= i < nums1.len(),\n ensures\n 0 <= Self::keep_spec(nums1, nums2, i) <= i + 1,\n 0 <= Self::swap_spec(nums1, nums2, i) <= i + 1,\n decreases i,\n {\n if i > 0 {\n Self::spec_bounds(nums1, nums2, i - 1);\n }\n }\n\n pub fn min_swap(nums1: Vec, nums2: Vec) -> (result: i32)\n requires\n 2 <= nums1.len() <= 100_000,\n nums1.len() == nums2.len(),\n forall|i: int| 0 <= i < nums1.len() ==>\n 0 <= #[trigger] nums1[i] <= 200_000,\n forall|i: int| 0 <= i < nums2.len() ==>\n 0 <= #[trigger] nums2[i] <= 200_000,\n forall|i: int| 1 <= i < nums1.len() ==>\n ((#[trigger] nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1])\n || (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1])),\n ensures\n result >= 0,\n result as int == {\n let n = nums1@.len();\n let k = Self::keep_spec(nums1@, nums2@, (n - 1) as int);\n let s = Self::swap_spec(nums1@, nums2@, (n - 1) as int);\n if k <= s { k } else { s }\n },\n {\n let n = nums1.len();\n let ghost p1 = nums1@;\n let ghost p2 = nums2@;\n let mut keep: i32 = 0;\n let mut swap: i32 = 1;\n let mut i: usize = 1;\n proof {\n Self::spec_bounds(p1, p2, 0int);\n }\n while i < n\n invariant\n n == nums1.len(),\n n == nums2.len(),\n 2 <= n <= 100_000,\n p1 == nums1@,\n p2 == nums2@,\n 1 <= i <= n,\n keep as int == Self::keep_spec(p1, p2, (i - 1) as int),\n swap as int == Self::swap_spec(p1, p2, (i - 1) as int),\n 0 <= keep as int <= i,\n 0 <= swap as int <= i,\n decreases n - i,\n {\n let a = nums1[i];\n let b = nums2[i];\n let pa = nums1[i - 1];\n let pb = nums2[i - 1];\n let mut new_keep: i32 = i as i32 + 1;\n let mut new_swap: i32 = i as i32 + 1;\n if a > pa && b > pb {\n if keep < new_keep { new_keep = keep; }\n if swap + 1 < new_swap { new_swap = swap + 1; }\n }\n if a > pb && b > pa {\n if swap < new_keep { new_keep = swap; }\n if keep + 1 < new_swap { new_swap = keep + 1; }\n }\n proof {\n reveal_with_fuel(Solution::keep_spec, 2);\n reveal_with_fuel(Solution::swap_spec, 2);\n Self::spec_bounds(p1, p2, (i - 1) as int);\n }\n assert(new_keep as int == Self::keep_spec(p1, p2, i as int));\n assert(new_swap as int == Self::swap_spec(p1, p2, i as int));\n keep = new_keep;\n swap = new_swap;\n proof {\n Self::spec_bounds(p1, p2, i as int);\n }\n i = i + 1;\n }\n if keep < swap { keep } else { swap }\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc801/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc801/tests/mutated_testcases.jsonl"} {"problem": "lc845", "family": "leetcode", "description": "# Longest Mountain in Array\n\nYou may recall that an array `arr` is a **mountain array** if and only if:\n\n- `arr.length >= 3`\n- There exists some index `i` (0-indexed) with `0 < i < arr.length - 1` such that:\n - `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`\n - `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`\n\nGiven an integer array `arr`, return the length of the longest subarray, which is a mountain. Return `0` if there is no mountain subarray.\n\n## Example 1:\n> **Input:** arr = [2,1,4,7,3,2,5]\n> **Output:** 5\n> **Explanation:** The longest mountain is [1,4,7,3,2] which has length 5.\n\n## Example 2:\n> **Input:** arr = [2,2,2]\n> **Output:** 0\n> **Explanation:** There is no mountain subarray.\n\n## Constraints:\n- $1 <= arr.length <= 10^4$\n- $0 <= arr[i] <= 10^4$\n\n## Follow up:\n- Can you solve it using only one pass?\n- Can you solve it in `O(1)` space?\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn longest_mountain(arr: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Two Pointers, Dynamic Programming, Enumeration\nAcceptance rate: 40.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_subarray(s: Seq, l: int, r: int, peak: int) -> bool {\n 0 <= l && l < peak && peak < r && r < s.len()\n && (forall |a: int, b: int| l <= a < b <= peak ==> s[a] < s[b])\n && (forall |a: int, b: int| peak <= a < b <= r ==> s[a] > s[b])\n }\n\n pub fn longest_mountain(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 10_000,\n ensures\n result >= 0,\n result > 0 ==> (exists |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n && r - l + 1 == result as int),\n result == 0 ==> (forall |l: int, r: int, peak: int|\n !Self::is_mountain_subarray(arr@, l, r, peak)),\n forall |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n ==> r - l + 1 <= result as int,\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn longest_mountain(arr: Vec) -> i32 {\n let n = arr.len();\n if n < 3 {\n return 0;\n }\n let mut best: i32 = 0;\n let mut up: usize = 0;\n let mut down: usize = 0;\n let mut i: usize = 1;\n while i < n {\n if arr[i] > arr[i - 1] {\n if down > 0 {\n up = 0;\n down = 0;\n }\n up = up + 1;\n } else if arr[i] < arr[i - 1] {\n if up > 0 {\n down = down + 1;\n }\n } else {\n up = 0;\n down = 0;\n }\n if up > 0 && down > 0 {\n let len = (up + down + 1) as i32;\n if len > best {\n best = len;\n }\n }\n i = i + 1;\n }\n best\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_subarray(s: Seq, l: int, r: int, peak: int) -> bool {\n 0 <= l && l < peak && peak < r && r < s.len()\n && (forall |a: int, b: int| l <= a < b <= peak ==> s[a] < s[b])\n && (forall |a: int, b: int| peak <= a < b <= r ==> s[a] > s[b])\n }\n\n pub fn longest_mountain(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 10_000,\n ensures\n result >= 0,\n result > 0 ==> (exists |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n && r - l + 1 == result as int),\n result == 0 ==> (forall |l: int, r: int, peak: int|\n !Self::is_mountain_subarray(arr@, l, r, peak)),\n forall |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n ==> r - l + 1 <= result as int,\n {\n let n = arr.len();\n if n < 3 {\n return 0;\n }\n\n let mut best: i32 = 0;\n let mut up: usize = 0;\n let mut down: usize = 0;\n let mut i: usize = 1;\n\n while i < n {\n if arr[i] > arr[i - 1] {\n if down > 0 {\n up = 0;\n down = 0;\n }\n up = up + 1;\n } else if arr[i] < arr[i - 1] {\n if up > 0 {\n down = down + 1;\n }\n } else {\n up = 0;\n down = 0;\n }\n\n if up > 0 && down > 0 {\n let len = (up + down + 1) as i32;\n if len > best {\n best = len;\n }\n }\n\n i = i + 1;\n }\n\n best\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain_subarray(s: Seq, l: int, r: int, peak: int) -> bool {\n 0 <= l && l < peak && peak < r && r < s.len()\n && (forall |a: int, b: int| l <= a < b <= peak ==> s[a] < s[b])\n && (forall |a: int, b: int| peak <= a < b <= r ==> s[a] > s[b])\n }\n\n pub fn longest_mountain(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 10_000,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 10_000,\n ensures\n result >= 0,\n result > 0 ==> (exists |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n && r - l + 1 == result as int),\n result == 0 ==> (forall |l: int, r: int, peak: int|\n !Self::is_mountain_subarray(arr@, l, r, peak)),\n forall |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n ==> r - l + 1 <= result as int,\n {\n let n = arr.len();\n if n < 3 {\n proof {\n assert forall |l: int, r: int, peak: int|\n !Self::is_mountain_subarray(arr@, l, r, peak) by {};\n }\n return 0;\n }\n\n let mut best: i32 = 0;\n let ghost mut best_l: int = 0;\n let ghost mut best_r: int = 0;\n let ghost mut best_peak: int = 0;\n let mut up: usize = 0;\n let mut down: usize = 0;\n let mut i: usize = 1;\n \n let ghost mut last_plateau: usize = 0;\n\n while i < n\n invariant\n 1 <= i <= n,\n n == arr.len(),\n n >= 3,\n 1 <= n <= 10_000,\n best >= 0,\n forall |k: int| 0 <= k < n ==> 0 <= #[trigger] arr[k] <= 10_000,\n up + down < i,\n up <= i - 1,\n \n up > 0 ==> (\n forall |a: int, b: int|\n i as int - 1 - up as int - down as int <= a < b <= i as int - 1 - down as int\n ==> arr@[a] < arr@[b]\n ),\n \n down > 0 ==> up > 0,\n down > 0 ==> (\n forall |a: int, b: int|\n i as int - 1 - down as int <= a < b <= i as int - 1\n ==> arr@[a] > arr@[b]\n ),\n up == 0 ==> down == 0,\n \n up > 0 ==> (\n i as int - 1 - up as int - down as int == 0\n || arr@[i as int - 1 - up as int - down as int - 1] >= arr@[i as int - 1 - up as int - down as int]\n ),\n \n (last_plateau as int) < (i as int),\n up == 0 ==> (\n last_plateau == 0\n || arr@[last_plateau as int - 1] == arr@[last_plateau as int]\n ),\n up == 0 ==> (\n forall |m: int| (last_plateau as int) < m && m < (i as int) ==> #[trigger] arr@[m] < arr@[m - 1]\n ),\n \n best > 0 ==> (\n Self::is_mountain_subarray(arr@, best_l, best_r, best_peak)\n && best_r - best_l + 1 == best as int\n ),\n \n forall |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak) && r < i as int\n ==> r - l + 1 <= best as int,\n decreases n - i,\n {\n if arr[i] > arr[i - 1] {\n if down > 0 {\n proof {\n \n \n assert(arr@[i as int - 1] < arr@[i as int]);\n \n assert(arr@[i as int - 2] > arr@[i as int - 1]);\n }\n up = 0;\n down = 0;\n }\n up = up + 1;\n } else if arr[i] < arr[i - 1] {\n if up > 0 {\n proof {\n \n assert forall |a: int, b: int|\n i as int - 1 - down as int <= a < b <= i as int\n implies arr@[a] > arr@[b] by {\n if b == i as int {\n if a < i as int - 1 {\n assert(arr@[a] > arr@[i as int - 1]);\n }\n assert(arr@[i as int - 1] > arr@[i as int]);\n }\n };\n }\n down = down + 1;\n }\n \n } else {\n \n proof { last_plateau = i; }\n up = 0;\n down = 0;\n }\n\n if up > 0 && down > 0 {\n let len = (up + down + 1) as i32;\n if len > best {\n proof {\n let peak: int = i as int - down as int;\n let l: int = peak - up as int;\n let r: int = i as int;\n assert(Self::is_mountain_subarray(arr@, l, r, peak));\n best_peak = peak;\n best_l = l;\n best_r = r;\n }\n best = len;\n }\n }\n\n proof {\n \n \n let i_val: int = i as int;\n let P: int = i_val - down as int; \n let L: int = P - up as int; \n\n assert forall |l: int, r: int, peak: int|\n Self::is_mountain_subarray(arr@, l, r, peak) && r == i_val\n implies r - l + 1 <= best as int by {\n\n \n \n\n if up == 0 {\n \n \n if peak > last_plateau as int {\n \n \n assert(arr@[peak - 1] > arr@[peak]);\n \n assert(arr@[peak - 1] < arr@[peak]);\n assert(false);\n } else if peak == last_plateau as int {\n if last_plateau > 0 {\n \n assert(arr@[peak - 1] == arr@[peak]);\n \n assert(arr@[peak - 1] < arr@[peak]);\n assert(false);\n } else {\n \n assert(0 <= l);\n assert(l < peak);\n assert(peak == 0 as int);\n assert(false);\n }\n } else {\n \n if last_plateau > 0 {\n \n assert(arr@[last_plateau as int - 1] == arr@[last_plateau as int]);\n \n \n assert(arr@[last_plateau as int - 1] > arr@[last_plateau as int]);\n assert(false);\n } else {\n \n assert(0 <= l);\n assert(l < peak);\n assert((last_plateau as int) == 0);\n assert(peak < 0 as int);\n assert(false);\n }\n }\n } else if down == 0 {\n \n \n \n \n \n assert(arr@[i_val - 1] < arr@[i_val]) by {\n \n \n \n \n assert forall |a: int, b: int|\n (i_val - up as int) <= a < b <= i_val\n implies arr@[a] < arr@[b] by {\n if b == i_val {\n if a == i_val - 1 {\n \n assert(arr@[i_val - 1] < arr@[i_val]);\n } else {\n \n \n \n \n \n \n assert(arr@[a] < arr@[i_val - 1]);\n assert(arr@[i_val - 1] < arr@[i_val]);\n }\n }\n \n \n };\n };\n \n assert(arr@[i_val - 1] > arr@[i_val]);\n assert(false);\n } else {\n \n \n\n \n if peak != P {\n if peak > P {\n \n assert(arr@[P] > arr@[P + 1]);\n if l <= P {\n \n assert(arr@[P] < arr@[P + 1]);\n } else {\n \n \n assert(arr@[l] > arr@[l + 1]);\n \n assert(arr@[l] < arr@[l + 1]);\n }\n assert(false);\n } else {\n \n assert(arr@[P - 1] < arr@[P]);\n \n assert(arr@[P - 1] > arr@[P]);\n assert(false);\n }\n }\n\n \n if l < L {\n \n assert(L > 0 as int);\n \n assert(arr@[L - 1] >= arr@[L]);\n \n assert(arr@[L - 1] < arr@[L]);\n assert(false);\n }\n\n \n assert(r - l + 1 <= up as int + down as int + 1);\n \n assert(best as int >= up as int + down as int + 1);\n }\n };\n }\n\n i = i + 1;\n }\n\n proof {\n \n assert forall |l: int, r: int, peak: int|\n #[trigger] Self::is_mountain_subarray(arr@, l, r, peak)\n implies r - l + 1 <= best as int by {\n \n assert(r < n as int);\n };\n \n assert(best == 0 ==> forall |l: int, r: int, peak: int|\n !Self::is_mountain_subarray(arr@, l, r, peak)) by {\n if best == 0 {\n assert forall |l: int, r: int, peak: int|\n !Self::is_mountain_subarray(arr@, l, r, peak) by {\n if Self::is_mountain_subarray(arr@, l, r, peak) {\n assert(r - l + 1 <= 0 as int);\n assert(l < peak); assert(peak < r);\n assert(r - l + 1 >= 3 as int);\n assert(false);\n }\n };\n }\n };\n }\n\n best\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc845/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc845/tests/mutated_testcases.jsonl"} {"problem": "lc860", "family": "leetcode", "description": "# Lemonade Change\n\nAt a lemonade stand, each lemonade costs `$5`. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a `$5`, `$10`, or `$20` bill. You must provide the correct change to each customer so that the net transaction is that the customer pays `$5`.\n\nNote that you do not have any change in hand at first.\n\nGiven an integer array `bills` where `bills[i]` is the bill the `i^th` customer pays, return `true` *if you can provide every customer with the correct change, or* `false` *otherwise*.\n\n## Example 1:\n\n> **Input:** bills = [5,5,5,10,20]\n> **Output:** true\n> **Explanation:**\n> From the first 3 customers, we collect three $5 bills in order.\n> From the fourth customer, we collect a $10 bill and give back a $5.\n> From the fifth customer, we give a $10 bill and a $5 bill.\n> Since all customers got correct change, we output true.\n\n## Example 2:\n\n> **Input:** bills = [5,5,10,10,20]\n> **Output:** false\n> **Explanation:**\n> From the first two customers in order, we collect two $5 bills.\n> For the next two customers in order, we collect a $10 bill and give back a $5 bill.\n> For the last customer, we can not give the change of $15 back because we only have two $10 bills.\n> Since not every customer received the correct change, the answer is false.\n\n**Constraints:**\n\n- `1 <= bills.length <= 10^5`\n- `bills[i]` is either `5`, `10`, or `20`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn lemonade_change(bills: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray, Greedy\nAcceptance rate: 59.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_bill_prefix(bills: Seq, n: nat, bill: i32) -> int\n decreases n\n{\n if n == 0 {\n 0\n } else {\n count_bill_prefix(bills, (n - 1) as nat, bill) + if bills[(n - 1) as int] == bill { 1int } else { 0int }\n }\n}\n\npub open spec fn remaining_fives(bills: Seq, n: nat, used_ten: int) -> int {\n count_bill_prefix(bills, n, 5) - count_bill_prefix(bills, n, 10) - 3 * count_bill_prefix(bills, n, 20) + 2 * used_ten\n}\n\npub open spec fn remaining_tens(bills: Seq, n: nat, used_ten: int) -> int {\n count_bill_prefix(bills, n, 10) - used_ten\n}\n\npub open spec fn possible_state(bills: Seq, n: nat, used_ten: int) -> bool\n decreases n\n{\n if n == 0 {\n used_ten == 0\n } else if bills[(n - 1) as int] == 5 {\n possible_state(bills, (n - 1) as nat, used_ten)\n } else if bills[(n - 1) as int] == 10 {\n possible_state(bills, (n - 1) as nat, used_ten) && remaining_fives(bills, (n - 1) as nat, used_ten) >= 1\n } else {\n (used_ten >= 1 && possible_state(bills, (n - 1) as nat, used_ten - 1) && remaining_fives(bills, (n - 1) as nat, used_ten - 1) >= 1 && remaining_tens(bills, (n - 1) as nat, used_ten - 1) >= 1)\n || (possible_state(bills, (n - 1) as nat, used_ten) && remaining_fives(bills, (n - 1) as nat, used_ten) >= 3)\n }\n}\n\npub open spec fn prefix_possible(bills: Seq, n: nat) -> bool {\n exists |used_ten: int| possible_state(bills, n, used_ten)\n}\n\npub open spec fn can_make_change(bills: Seq) -> bool {\n prefix_possible(bills, bills.len())\n}\n\nimpl Solution {\n pub fn lemonade_change(bills: Vec) -> (res: bool)\n requires\n 1 <= bills.len() <= 100_000,\n forall |i: int| 0 <= i < bills.len() ==> bills[i] == 5 || bills[i] == 10 || bills[i] == 20,\n ensures\n res == can_make_change(bills@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn lemonade_change(bills: Vec) -> bool {\n let mut five: usize = 0;\n let mut ten: usize = 0;\n let mut i: usize = 0;\n while i < bills.len() {\n let bill = bills[i];\n if bill == 5 {\n five = five + 1;\n } else if bill == 10 {\n if five == 0 {\n return false;\n }\n five = five - 1;\n ten = ten + 1;\n } else {\n if ten > 0 && five > 0 {\n ten = ten - 1;\n five = five - 1;\n } else if five >= 3 {\n five = five - 3;\n } else {\n return false;\n }\n }\n i = i + 1;\n }\n true\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_bill_prefix(bills: Seq, n: nat, bill: i32) -> int\n decreases n\n{\n if n == 0 {\n 0\n } else {\n count_bill_prefix(bills, (n - 1) as nat, bill) + if bills[(n - 1) as int] == bill { 1int } else { 0int }\n }\n}\n\npub open spec fn remaining_fives(bills: Seq, n: nat, used_ten: int) -> int {\n count_bill_prefix(bills, n, 5) - count_bill_prefix(bills, n, 10) - 3 * count_bill_prefix(bills, n, 20) + 2 * used_ten\n}\n\npub open spec fn remaining_tens(bills: Seq, n: nat, used_ten: int) -> int {\n count_bill_prefix(bills, n, 10) - used_ten\n}\n\npub open spec fn possible_state(bills: Seq, n: nat, used_ten: int) -> bool\n decreases n\n{\n if n == 0 {\n used_ten == 0\n } else if bills[(n - 1) as int] == 5 {\n possible_state(bills, (n - 1) as nat, used_ten)\n } else if bills[(n - 1) as int] == 10 {\n possible_state(bills, (n - 1) as nat, used_ten) && remaining_fives(bills, (n - 1) as nat, used_ten) >= 1\n } else {\n (used_ten >= 1 && possible_state(bills, (n - 1) as nat, used_ten - 1) && remaining_fives(bills, (n - 1) as nat, used_ten - 1) >= 1 && remaining_tens(bills, (n - 1) as nat, used_ten - 1) >= 1)\n || (possible_state(bills, (n - 1) as nat, used_ten) && remaining_fives(bills, (n - 1) as nat, used_ten) >= 3)\n }\n}\n\npub open spec fn prefix_possible(bills: Seq, n: nat) -> bool {\n exists |used_ten: int| possible_state(bills, n, used_ten)\n}\n\npub open spec fn can_make_change(bills: Seq) -> bool {\n prefix_possible(bills, bills.len())\n}\n\nimpl Solution {\n pub fn lemonade_change(bills: Vec) -> (res: bool)\n requires\n 1 <= bills.len() <= 100_000,\n forall |i: int| 0 <= i < bills.len() ==> bills[i] == 5 || bills[i] == 10 || bills[i] == 20,\n ensures\n res == can_make_change(bills@),\n {\n let mut five: usize = 0;\n let mut ten: usize = 0;\n let mut used_ten: usize = 0;\n let mut i: usize = 0;\n while i < bills.len() {\n let bill = bills[i];\n if bill == 5 {\n five = five + 1;\n } else if bill == 10 {\n if five == 0 {\n return false;\n }\n five = five - 1;\n ten = ten + 1;\n } else {\n if ten > 0 && five > 0 {\n ten = ten - 1;\n five = five - 1;\n used_ten = used_ten + 1;\n } else if five >= 3 {\n five = five - 3;\n } else {\n return false;\n }\n }\n i = i + 1;\n }\n true\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\npub open spec fn count_bill_prefix(bills: Seq, n: nat, bill: i32) -> int\n decreases n\n{\n if n == 0 {\n 0\n } else {\n count_bill_prefix(bills, (n - 1) as nat, bill) + if bills[(n - 1) as int] == bill { 1int } else { 0int }\n }\n}\n\npub open spec fn remaining_fives(bills: Seq, n: nat, used_ten: int) -> int {\n count_bill_prefix(bills, n, 5) - count_bill_prefix(bills, n, 10) - 3 * count_bill_prefix(bills, n, 20) + 2 * used_ten\n}\n\npub open spec fn remaining_tens(bills: Seq, n: nat, used_ten: int) -> int {\n count_bill_prefix(bills, n, 10) - used_ten\n}\n\npub open spec fn possible_state(bills: Seq, n: nat, used_ten: int) -> bool\n decreases n\n{\n if n == 0 {\n used_ten == 0\n } else if bills[(n - 1) as int] == 5 {\n possible_state(bills, (n - 1) as nat, used_ten)\n } else if bills[(n - 1) as int] == 10 {\n possible_state(bills, (n - 1) as nat, used_ten) && remaining_fives(bills, (n - 1) as nat, used_ten) >= 1\n } else {\n (used_ten >= 1 && possible_state(bills, (n - 1) as nat, used_ten - 1) && remaining_fives(bills, (n - 1) as nat, used_ten - 1) >= 1 && remaining_tens(bills, (n - 1) as nat, used_ten - 1) >= 1)\n || (possible_state(bills, (n - 1) as nat, used_ten) && remaining_fives(bills, (n - 1) as nat, used_ten) >= 3)\n }\n}\n\npub open spec fn prefix_possible(bills: Seq, n: nat) -> bool {\n exists |used_ten: int| possible_state(bills, n, used_ten)\n}\n\npub open spec fn can_make_change(bills: Seq) -> bool {\n prefix_possible(bills, bills.len())\n}\n\nproof fn lemma_count_bill_prefix_step(bills: Seq, n: nat, bill: i32)\n requires\n n < bills.len(),\n ensures\n count_bill_prefix(bills, n + 1, bill) == count_bill_prefix(bills, n, bill) + if bills[n as int] == bill { 1int } else { 0int },\n{\n reveal_with_fuel(count_bill_prefix, 2);\n}\n\nproof fn lemma_step_bill_5(bills: Seq, n: nat, used_ten: int)\n requires\n n < bills.len(),\n bills[n as int] == 5,\n ensures\n remaining_fives(bills, n + 1, used_ten) == remaining_fives(bills, n, used_ten) + 1,\n remaining_tens(bills, n + 1, used_ten) == remaining_tens(bills, n, used_ten),\n{\n lemma_count_bill_prefix_step(bills, n, 5);\n lemma_count_bill_prefix_step(bills, n, 10);\n}\n\nproof fn lemma_step_bill_10(bills: Seq, n: nat, used_ten: int)\n requires\n n < bills.len(),\n bills[n as int] == 10,\n ensures\n remaining_fives(bills, n + 1, used_ten) == remaining_fives(bills, n, used_ten) - 1,\n remaining_tens(bills, n + 1, used_ten) == remaining_tens(bills, n, used_ten) + 1,\n{\n lemma_count_bill_prefix_step(bills, n, 5);\n lemma_count_bill_prefix_step(bills, n, 10);\n}\n\nproof fn lemma_step_bill_20_use_ten(bills: Seq, n: nat, used_ten: int)\n requires\n n < bills.len(),\n bills[n as int] == 20,\n ensures\n remaining_fives(bills, n + 1, used_ten + 1) == remaining_fives(bills, n, used_ten) - 1,\n remaining_tens(bills, n + 1, used_ten + 1) == remaining_tens(bills, n, used_ten) - 1,\n{\n lemma_count_bill_prefix_step(bills, n, 5);\n lemma_count_bill_prefix_step(bills, n, 10);\n lemma_count_bill_prefix_step(bills, n, 20);\n}\n\nproof fn lemma_step_bill_20_use_fives(bills: Seq, n: nat, used_ten: int)\n requires\n n < bills.len(),\n bills[n as int] == 20,\n ensures\n remaining_fives(bills, n + 1, used_ten) == remaining_fives(bills, n, used_ten) - 3,\n remaining_tens(bills, n + 1, used_ten) == remaining_tens(bills, n, used_ten),\n{\n lemma_count_bill_prefix_step(bills, n, 5);\n lemma_count_bill_prefix_step(bills, n, 10);\n lemma_count_bill_prefix_step(bills, n, 20);\n}\n\nproof fn lemma_tens_positive_implies_fives_smaller(bills: Seq, n: nat, u: int, used_ten: int)\n requires\n remaining_tens(bills, n, used_ten) == 0,\n remaining_tens(bills, n, u) >= 1,\n ensures\n remaining_fives(bills, n, u) <= remaining_fives(bills, n, used_ten) - 2,\n{\n}\n\nproof fn lemma_prefix_possible_of_longer_prefix(bills: Seq, m: nat, n: nat)\n requires\n m <= n <= bills.len(),\n prefix_possible(bills, n),\n ensures\n prefix_possible(bills, m),\n decreases n - m,\n{\n if m < n {\n let used_ten = choose |used_ten: int| possible_state(bills, n, used_ten);\n reveal_with_fuel(possible_state, 2);\n if bills[(n - 1) as int] == 5 {\n assert(prefix_possible(bills, (n - 1) as nat)) by {\n assert(possible_state(bills, (n - 1) as nat, used_ten));\n }\n } else if bills[(n - 1) as int] == 10 {\n assert(prefix_possible(bills, (n - 1) as nat)) by {\n assert(possible_state(bills, (n - 1) as nat, used_ten));\n }\n } else {\n if used_ten >= 1 && possible_state(bills, (n - 1) as nat, used_ten - 1) && remaining_fives(bills, (n - 1) as nat, used_ten - 1) >= 1 && remaining_tens(bills, (n - 1) as nat, used_ten - 1) >= 1 {\n assert(prefix_possible(bills, (n - 1) as nat)) by {\n assert(possible_state(bills, (n - 1) as nat, used_ten - 1));\n }\n } else {\n assert(prefix_possible(bills, (n - 1) as nat)) by {\n assert(possible_state(bills, (n - 1) as nat, used_ten));\n }\n }\n }\n lemma_prefix_possible_of_longer_prefix(bills, m, (n - 1) as nat);\n }\n}\n\nimpl Solution {\n pub fn lemonade_change(bills: Vec) -> (res: bool)\n requires\n 1 <= bills.len() <= 100_000,\n forall |i: int| 0 <= i < bills.len() ==> bills[i] == 5 || bills[i] == 10 || bills[i] == 20,\n ensures\n res == can_make_change(bills@),\n {\n let mut five: usize = 0;\n let mut ten: usize = 0;\n let mut used_ten: usize = 0;\n let mut i: usize = 0;\n while i < bills.len()\n invariant\n 1 <= bills.len() <= 100_000,\n forall |j: int| 0 <= j < bills.len() ==> bills[j] == 5 || bills[j] == 10 || bills[j] == 20,\n 0 <= i <= bills.len(),\n five <= i,\n ten <= i,\n used_ten <= i,\n possible_state(bills@, i as nat, used_ten as int),\n five as int == remaining_fives(bills@, i as nat, used_ten as int),\n ten as int == remaining_tens(bills@, i as nat, used_ten as int),\n forall |u: int| (#[trigger] possible_state(bills@, i as nat, u)) ==> u <= used_ten as int,\n forall |u: int| (#[trigger] possible_state(bills@, i as nat, u)) ==> remaining_fives(bills@, i as nat, u) <= five as int,\n decreases bills.len() - i,\n {\n let bill = bills[i];\n let ghost old_i = i as nat;\n let ghost old_used_ten = used_ten as int;\n let ghost old_five = five as int;\n let ghost old_ten = ten as int;\n\n if bill == 5 {\n five = five + 1;\n proof {\n assert(bills@[old_i as int] == 5);\n lemma_step_bill_5(bills@, old_i, old_used_ten);\n assert(five as int == old_five + 1);\n assert(possible_state(bills@, old_i + 1, old_used_ten));\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies u <= used_ten as int by {\n assert(possible_state(bills@, old_i, u));\n };\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies remaining_fives(bills@, old_i + 1, u) <= five as int by {\n lemma_step_bill_5(bills@, old_i, u);\n assert(possible_state(bills@, old_i, u));\n assert(remaining_fives(bills@, old_i, u) <= old_five);\n };\n }\n } else if bill == 10 {\n if five == 0 {\n proof {\n assert(bills@[old_i as int] == 10);\n assert forall |u: int| !(#[trigger] possible_state(bills@, old_i + 1, u)) by {\n reveal_with_fuel(possible_state, 2);\n if possible_state(bills@, old_i + 1, u) {\n assert(possible_state(bills@, old_i, u));\n assert(remaining_fives(bills@, old_i, u) >= 1);\n assert(remaining_fives(bills@, old_i, u) <= old_five);\n }\n };\n assert(!prefix_possible(bills@, old_i + 1));\n assert(!can_make_change(bills@)) by {\n if can_make_change(bills@) {\n lemma_prefix_possible_of_longer_prefix(bills@, old_i + 1, bills.len() as nat);\n }\n };\n }\n return false;\n }\n five = five - 1;\n ten = ten + 1;\n proof {\n assert(bills@[old_i as int] == 10);\n lemma_step_bill_10(bills@, old_i, old_used_ten);\n assert(possible_state(bills@, old_i + 1, old_used_ten));\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies u <= used_ten as int by {\n reveal_with_fuel(possible_state, 2);\n assert(possible_state(bills@, old_i, u));\n };\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies remaining_fives(bills@, old_i + 1, u) <= five as int by {\n reveal_with_fuel(possible_state, 2);\n lemma_step_bill_10(bills@, old_i, u);\n assert(possible_state(bills@, old_i, u));\n assert(remaining_fives(bills@, old_i, u) <= old_five);\n };\n }\n } else {\n if ten > 0 && five > 0 {\n ten = ten - 1;\n five = five - 1;\n used_ten = used_ten + 1;\n proof {\n assert(bills@[old_i as int] == 20);\n lemma_step_bill_20_use_ten(bills@, old_i, old_used_ten);\n assert(possible_state(bills@, old_i + 1, old_used_ten + 1));\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies u <= used_ten as int by {\n reveal_with_fuel(possible_state, 2);\n if u >= 1 && possible_state(bills@, old_i, u - 1) && remaining_fives(bills@, old_i, u - 1) >= 1 && remaining_tens(bills@, old_i, u - 1) >= 1 {\n assert(u - 1 <= old_used_ten);\n } else {\n assert(possible_state(bills@, old_i, u));\n assert(u <= old_used_ten);\n }\n };\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies remaining_fives(bills@, old_i + 1, u) <= five as int by {\n reveal_with_fuel(possible_state, 2);\n if u >= 1 && possible_state(bills@, old_i, u - 1) && remaining_fives(bills@, old_i, u - 1) >= 1 && remaining_tens(bills@, old_i, u - 1) >= 1 {\n lemma_step_bill_20_use_ten(bills@, old_i, u - 1);\n assert(remaining_fives(bills@, old_i, u - 1) <= old_five);\n } else {\n lemma_step_bill_20_use_fives(bills@, old_i, u);\n assert(possible_state(bills@, old_i, u));\n assert(remaining_fives(bills@, old_i, u) <= old_five);\n }\n };\n }\n } else if five >= 3 {\n five = five - 3;\n proof {\n assert(bills@[old_i as int] == 20);\n lemma_step_bill_20_use_fives(bills@, old_i, old_used_ten);\n assert(possible_state(bills@, old_i + 1, old_used_ten));\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies u <= used_ten as int by {\n reveal_with_fuel(possible_state, 2);\n if u >= 1 && possible_state(bills@, old_i, u - 1) && remaining_fives(bills@, old_i, u - 1) >= 1 && remaining_tens(bills@, old_i, u - 1) >= 1 {\n assert(old_ten == remaining_tens(bills@, old_i, old_used_ten));\n assert(remaining_tens(bills@, old_i, old_used_ten) == 0);\n assert(remaining_tens(bills@, old_i, u - 1) >= 1);\n assert(u - 1 < old_used_ten);\n assert(u <= old_used_ten);\n } else {\n assert(possible_state(bills@, old_i, u));\n assert(u <= old_used_ten);\n }\n };\n assert forall |u: int| (#[trigger] possible_state(bills@, old_i + 1, u)) implies remaining_fives(bills@, old_i + 1, u) <= five as int by {\n reveal_with_fuel(possible_state, 2);\n if u >= 1 && possible_state(bills@, old_i, u - 1) && remaining_fives(bills@, old_i, u - 1) >= 1 && remaining_tens(bills@, old_i, u - 1) >= 1 {\n assert(old_ten == remaining_tens(bills@, old_i, old_used_ten));\n lemma_tens_positive_implies_fives_smaller(bills@, old_i, u - 1, old_used_ten);\n lemma_step_bill_20_use_ten(bills@, old_i, u - 1);\n } else {\n lemma_step_bill_20_use_fives(bills@, old_i, u);\n assert(possible_state(bills@, old_i, u));\n assert(remaining_fives(bills@, old_i, u) <= old_five);\n }\n };\n }\n } else {\n proof {\n assert(bills@[old_i as int] == 20);\n assert forall |u: int| !(#[trigger] possible_state(bills@, old_i + 1, u)) by {\n reveal_with_fuel(possible_state, 2);\n if possible_state(bills@, old_i + 1, u) {\n if u >= 1 && possible_state(bills@, old_i, u - 1) && remaining_fives(bills@, old_i, u - 1) >= 1 && remaining_tens(bills@, old_i, u - 1) >= 1 {\n if old_five == 0 {\n assert(remaining_fives(bills@, old_i, u - 1) <= old_five);\n } else {\n assert(old_ten == remaining_tens(bills@, old_i, old_used_ten));\n assert(remaining_tens(bills@, old_i, old_used_ten) == 0);\n lemma_tens_positive_implies_fives_smaller(bills@, old_i, u - 1, old_used_ten);\n }\n } else {\n assert(possible_state(bills@, old_i, u));\n assert(remaining_fives(bills@, old_i, u) >= 3);\n assert(remaining_fives(bills@, old_i, u) <= old_five);\n }\n }\n };\n assert(!prefix_possible(bills@, old_i + 1));\n assert(!can_make_change(bills@)) by {\n if can_make_change(bills@) {\n lemma_prefix_possible_of_longer_prefix(bills@, old_i + 1, bills.len() as nat);\n }\n };\n }\n return false;\n }\n }\n i = i + 1;\n }\n proof {\n assert(can_make_change(bills@)) by {\n assert(possible_state(bills@, bills.len() as nat, used_ten as int));\n }\n }\n true\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc860/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc860/tests/mutated_testcases.jsonl"} {"problem": "lc861", "family": "leetcode", "description": "# Score After Flipping Matrix\n\nYou are given an `m x n` binary matrix `grid`.\n\nA **move** consists of choosing any row or column and toggling each value in that row or column.\n\nEvery row of the matrix is interpreted as a binary number, and the **score** of the matrix is the sum of these numbers.\n\nReturn the highest possible **score** after making any number of **moves** (including zero moves).\n\n## Example 1:\n\n> **Input:** grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]\n> **Output:** 39\n> **Explanation:** 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39\n\n## Example 2:\n\n> **Input:** grid = [[0]]\n> **Output:** 1\n\n## Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 20`\n- `grid[i][j]` is either `0` or `1`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn matrix_score(grid: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nArray, Greedy, Bit Manipulation, Matrix\nAcceptance rate: 80.3%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn cols(grid: Seq>) -> int {\n if grid.len() > 0 { grid[0].len() as int } else { 0 }\n }\n\n pub open spec fn pow2(exp: int) -> int\n decreases exp,\n {\n if exp <= 0 { 1 } else { 2 * Self::pow2(exp - 1) }\n }\n\n pub open spec fn normalized_bit(grid: Seq>, i: int, j: int) -> int\n recommends\n 0 <= i < grid.len(),\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n {\n if grid[i][0] == 0 {\n 1 - grid[i][j] as int\n } else {\n grid[i][j] as int\n }\n }\n\n pub open spec fn normalized_ones_prefix(grid: Seq>, j: int, rows_end: int) -> int\n recommends\n 0 <= j < Self::cols(grid),\n 0 <= rows_end <= grid.len(),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n decreases rows_end,\n {\n if rows_end <= 0 {\n 0\n } else {\n Self::normalized_ones_prefix(grid, j, rows_end - 1)\n + if Self::normalized_bit(grid, rows_end - 1, j) == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn best_ones_at(grid: Seq>, j: int) -> int\n recommends\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n {\n let ones = Self::normalized_ones_prefix(grid, j, grid.len() as int);\n Self::max_int(ones, grid.len() - ones)\n }\n\n pub open spec fn score_rev_prefix(grid: Seq>, processed: int) -> int\n recommends\n 0 <= processed <= Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n decreases processed,\n {\n if processed <= 0 {\n 0\n } else {\n Self::score_rev_prefix(grid, processed - 1)\n + Self::best_ones_at(grid, Self::cols(grid) - processed) * Self::pow2(processed - 1)\n }\n }\n\n pub fn matrix_score(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 20,\n 1 <= grid[0].len() <= 20,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid[0].len() ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n result as int == Self::score_rev_prefix(grid@, Self::cols(grid@)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn matrix_score(grid: Vec>) -> i32 {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut result: i32 = 0;\n let mut place: i32 = 1;\n let mut j: usize = cols;\n while j > 0 {\n let col = j - 1;\n let mut ones: i32 = 0;\n let mut i: usize = 0;\n while i < rows {\n let one = grid[i][col] == grid[i][0];\n if one {\n ones = ones + 1;\n }\n i = i + 1;\n }\n let zeros = rows as i32 - ones;\n let best = if ones >= zeros { ones } else { zeros };\n result = result + best * place;\n place = place * 2;\n j = j - 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn cols(grid: Seq>) -> int {\n if grid.len() > 0 { grid[0].len() as int } else { 0 }\n }\n\n pub open spec fn pow2(exp: int) -> int\n decreases exp,\n {\n if exp <= 0 { 1 } else { 2 * Self::pow2(exp - 1) }\n }\n\n pub open spec fn normalized_bit(grid: Seq>, i: int, j: int) -> int\n recommends\n 0 <= i < grid.len(),\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n {\n if grid[i][0] == 0 {\n 1 - grid[i][j] as int\n } else {\n grid[i][j] as int\n }\n }\n\n pub open spec fn normalized_ones_prefix(grid: Seq>, j: int, rows_end: int) -> int\n recommends\n 0 <= j < Self::cols(grid),\n 0 <= rows_end <= grid.len(),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n decreases rows_end,\n {\n if rows_end <= 0 {\n 0\n } else {\n Self::normalized_ones_prefix(grid, j, rows_end - 1)\n + if Self::normalized_bit(grid, rows_end - 1, j) == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn best_ones_at(grid: Seq>, j: int) -> int\n recommends\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n {\n let ones = Self::normalized_ones_prefix(grid, j, grid.len() as int);\n Self::max_int(ones, grid.len() - ones)\n }\n\n pub open spec fn score_rev_prefix(grid: Seq>, processed: int) -> int\n recommends\n 0 <= processed <= Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n decreases processed,\n {\n if processed <= 0 {\n 0\n } else {\n Self::score_rev_prefix(grid, processed - 1)\n + Self::best_ones_at(grid, Self::cols(grid) - processed) * Self::pow2(processed - 1)\n }\n }\n\n pub fn matrix_score(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 20,\n 1 <= grid[0].len() <= 20,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid[0].len() ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n result as int == Self::score_rev_prefix(grid@, Self::cols(grid@)),\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let mut result: i32 = 0;\n let mut place: i32 = 1;\n let mut j: usize = cols;\n while j > 0 {\n let col = j - 1;\n let mut ones: i32 = 0;\n let mut i: usize = 0;\n while i < rows {\n let one = grid[i][col] == grid[i][0];\n if one {\n ones = ones + 1;\n }\n i = i + 1;\n }\n let zeros = rows as i32 - ones;\n let best = if ones >= zeros { ones } else { zeros };\n result = result + best * place;\n place = place * 2;\n j = j - 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn cols(grid: Seq>) -> int {\n if grid.len() > 0 { grid[0].len() as int } else { 0 }\n }\n\n pub open spec fn pow2(exp: int) -> int\n decreases exp,\n {\n if exp <= 0 { 1 } else { 2 * Self::pow2(exp - 1) }\n }\n\n pub open spec fn normalized_bit(grid: Seq>, i: int, j: int) -> int\n recommends\n 0 <= i < grid.len(),\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n {\n if grid[i][0] == 0 {\n 1 - grid[i][j] as int\n } else {\n grid[i][j] as int\n }\n }\n\n pub open spec fn normalized_ones_prefix(grid: Seq>, j: int, rows_end: int) -> int\n recommends\n 0 <= j < Self::cols(grid),\n 0 <= rows_end <= grid.len(),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n decreases rows_end,\n {\n if rows_end <= 0 {\n 0\n } else {\n Self::normalized_ones_prefix(grid, j, rows_end - 1)\n + if Self::normalized_bit(grid, rows_end - 1, j) == 1 { 1int } else { 0int }\n }\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn best_ones_at(grid: Seq>, j: int) -> int\n recommends\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n {\n let ones = Self::normalized_ones_prefix(grid, j, grid.len() as int);\n Self::max_int(ones, grid.len() - ones)\n }\n\n pub open spec fn score_rev_prefix(grid: Seq>, processed: int) -> int\n recommends\n 0 <= processed <= Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n decreases processed,\n {\n if processed <= 0 {\n 0\n } else {\n Self::score_rev_prefix(grid, processed - 1)\n + Self::best_ones_at(grid, Self::cols(grid) - processed) * Self::pow2(processed - 1)\n }\n }\n\n proof fn lemma_pow2_step(exp: int)\n requires\n 0 <= exp,\n ensures\n Self::pow2(exp + 1) == 2 * Self::pow2(exp),\n {\n reveal_with_fuel(Solution::pow2, 2);\n }\n\n proof fn lemma_pow2_positive(exp: int)\n requires\n 0 <= exp <= 20,\n ensures\n 1 <= Self::pow2(exp),\n decreases exp,\n {\n if exp == 0 {\n reveal_with_fuel(Solution::pow2, 1);\n } else {\n Self::lemma_pow2_positive(exp - 1);\n Self::lemma_pow2_step(exp - 1);\n assert(Self::pow2(exp) == 2 * Self::pow2(exp - 1));\n assert(1 <= Self::pow2(exp)) by (nonlinear_arith)\n requires\n 1 <= Self::pow2(exp - 1),\n Self::pow2(exp) == 2 * Self::pow2(exp - 1),\n {};\n }\n }\n\n proof fn lemma_pow2_monotonic(a: int, b: int)\n requires\n 0 <= a <= b <= 20,\n ensures\n Self::pow2(a) <= Self::pow2(b),\n decreases b - a,\n {\n if a < b {\n Self::lemma_pow2_monotonic(a, b - 1);\n Self::lemma_pow2_positive(b - 1);\n Self::lemma_pow2_step(b - 1);\n assert(Self::pow2(b) == 2 * Self::pow2(b - 1));\n assert(Self::pow2(b - 1) <= Self::pow2(b)) by (nonlinear_arith)\n requires\n 1 <= Self::pow2(b - 1),\n Self::pow2(b) == 2 * Self::pow2(b - 1),\n {};\n }\n }\n\n proof fn lemma_pow2_20_exact()\n ensures\n Self::pow2(20) == 1_048_576,\n {\n reveal_with_fuel(Solution::pow2, 21);\n }\n\n proof fn lemma_pow2_bound(exp: int)\n requires\n 0 <= exp <= 20,\n ensures\n 1 <= Self::pow2(exp) <= 1_048_576,\n {\n Self::lemma_pow2_positive(exp);\n Self::lemma_pow2_monotonic(exp, 20);\n Self::lemma_pow2_20_exact();\n assert(Self::pow2(exp) <= 1_048_576);\n }\n\n proof fn lemma_score_rev_prefix_nonnegative(grid: Seq>, processed: int)\n requires\n 0 <= processed <= Self::cols(grid),\n Self::cols(grid) <= 20,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < Self::cols(grid) ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n 0 <= Self::score_rev_prefix(grid, processed),\n decreases processed,\n {\n if processed == 0 {\n reveal_with_fuel(Solution::score_rev_prefix, 1);\n } else {\n let k = processed - 1;\n Self::lemma_score_rev_prefix_nonnegative(grid, k);\n Self::lemma_score_rev_prefix_step(grid, k);\n Self::lemma_best_ones_at_bound(grid, Self::cols(grid) - processed);\n Self::lemma_pow2_positive(k);\n assert(0 <= Self::score_rev_prefix(grid, processed)) by (nonlinear_arith)\n requires\n Self::score_rev_prefix(grid, processed)\n == Self::score_rev_prefix(grid, k)\n + Self::best_ones_at(grid, Self::cols(grid) - processed) * Self::pow2(k),\n 0 <= Self::score_rev_prefix(grid, k),\n 0 <= Self::best_ones_at(grid, Self::cols(grid) - processed),\n 1 <= Self::pow2(k),\n {};\n }\n }\n\n proof fn lemma_normalized_bit_value(grid: Seq>, i: int, j: int)\n requires\n 0 <= i < grid.len(),\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < Self::cols(grid) ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n Self::normalized_bit(grid, i, j) == if grid[i][j] == grid[i][0] { 1int } else { 0int },\n 0 <= Self::normalized_bit(grid, i, j) <= 1,\n {\n reveal_with_fuel(Solution::normalized_bit, 1);\n if grid[i][0] == 0 {\n assert(grid[i][j] == 0 || grid[i][j] == 1);\n if grid[i][j] == 0 {\n assert(Self::normalized_bit(grid, i, j) == 1);\n } else {\n assert(grid[i][j] == 1);\n assert(Self::normalized_bit(grid, i, j) == 0);\n }\n } else {\n assert(grid[i][0] == 1);\n assert(grid[i][j] == 0 || grid[i][j] == 1);\n if grid[i][j] == 1 {\n assert(Self::normalized_bit(grid, i, j) == 1);\n } else {\n assert(grid[i][j] == 0);\n assert(Self::normalized_bit(grid, i, j) == 0);\n }\n }\n }\n\n proof fn lemma_normalized_ones_prefix_step(grid: Seq>, j: int, rows_end: int)\n requires\n 0 <= j < Self::cols(grid),\n 0 <= rows_end < grid.len(),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n ensures\n Self::normalized_ones_prefix(grid, j, rows_end + 1)\n == Self::normalized_ones_prefix(grid, j, rows_end)\n + if Self::normalized_bit(grid, rows_end, j) == 1 { 1int } else { 0int },\n {\n reveal_with_fuel(Solution::normalized_ones_prefix, 2);\n }\n\n proof fn lemma_normalized_ones_prefix_bound(grid: Seq>, j: int, rows_end: int)\n requires\n 0 <= j < Self::cols(grid),\n 0 <= rows_end <= grid.len(),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < Self::cols(grid) ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n 0 <= Self::normalized_ones_prefix(grid, j, rows_end) <= rows_end,\n decreases rows_end,\n {\n if rows_end > 0 {\n let k = rows_end - 1;\n Self::lemma_normalized_ones_prefix_bound(grid, j, k);\n Self::lemma_normalized_ones_prefix_step(grid, j, k);\n Self::lemma_normalized_bit_value(grid, k, j);\n }\n }\n\n proof fn lemma_best_ones_at_bound(grid: Seq>, j: int)\n requires\n 0 <= j < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < Self::cols(grid) ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n 0 <= Self::best_ones_at(grid, j) <= grid.len(),\n {\n Self::lemma_normalized_ones_prefix_bound(grid, j, grid.len() as int);\n reveal(Solution::best_ones_at);\n let ones = Self::normalized_ones_prefix(grid, j, grid.len() as int);\n assert(0 <= grid.len() - ones <= grid.len());\n if ones >= grid.len() - ones {\n assert(Self::max_int(ones, grid.len() - ones) == ones);\n } else {\n assert(Self::max_int(ones, grid.len() - ones) == grid.len() - ones);\n }\n }\n\n proof fn lemma_score_rev_prefix_step(grid: Seq>, processed: int)\n requires\n 0 <= processed < Self::cols(grid),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n ensures\n Self::score_rev_prefix(grid, processed + 1)\n == Self::score_rev_prefix(grid, processed)\n + Self::best_ones_at(grid, Self::cols(grid) - processed - 1) * Self::pow2(processed),\n {\n reveal_with_fuel(Solution::score_rev_prefix, 2);\n }\n\n proof fn lemma_score_rev_prefix_bound(grid: Seq>, processed: int)\n requires\n 0 <= processed <= Self::cols(grid),\n Self::cols(grid) <= 20,\n grid.len() <= 20,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < Self::cols(grid) ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n 0 <= Self::score_rev_prefix(grid, processed) <= grid.len() * (Self::pow2(processed) - 1),\n decreases processed,\n {\n Self::lemma_score_rev_prefix_nonnegative(grid, processed);\n if processed == 0 {\n reveal_with_fuel(Solution::score_rev_prefix, 1);\n reveal_with_fuel(Solution::pow2, 1);\n } else {\n let k = processed - 1;\n Self::lemma_score_rev_prefix_bound(grid, k);\n Self::lemma_score_rev_prefix_step(grid, k);\n Self::lemma_pow2_positive(k);\n Self::lemma_pow2_step(k);\n Self::lemma_best_ones_at_bound(grid, Self::cols(grid) - processed);\n assert(Self::score_rev_prefix(grid, processed)\n <= grid.len() * (Self::pow2(k) - 1) + grid.len() * Self::pow2(k)) by (nonlinear_arith)\n requires\n Self::score_rev_prefix(grid, processed)\n == Self::score_rev_prefix(grid, k)\n + Self::best_ones_at(grid, Self::cols(grid) - processed) * Self::pow2(k),\n 0 <= Self::score_rev_prefix(grid, k),\n Self::score_rev_prefix(grid, k) <= grid.len() * (Self::pow2(k) - 1),\n Self::best_ones_at(grid, Self::cols(grid) - processed) <= grid.len(),\n 0 <= Self::pow2(k),\n {};\n assert(grid.len() * (Self::pow2(k) - 1) + grid.len() * Self::pow2(k)\n == grid.len() * (Self::pow2(processed) - 1)) by (nonlinear_arith)\n requires\n Self::pow2(processed) == 2 * Self::pow2(k),\n {};\n assert(Self::score_rev_prefix(grid, processed) <= grid.len() * (Self::pow2(processed) - 1));\n }\n }\n\n proof fn lemma_score_rev_prefix_i32_bound(grid: Seq>, processed: int)\n requires\n 0 <= processed <= Self::cols(grid),\n Self::cols(grid) <= 20,\n grid.len() <= 20,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == Self::cols(grid),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < Self::cols(grid) ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n 0 <= Self::score_rev_prefix(grid, processed) <= 20_971_500,\n {\n Self::lemma_score_rev_prefix_bound(grid, processed);\n Self::lemma_pow2_bound(processed);\n assert(grid.len() * (Self::pow2(processed) - 1) <= 20_971_500) by (nonlinear_arith)\n requires\n grid.len() <= 20,\n Self::pow2(processed) <= 1_048_576,\n {};\n }\n\n pub fn matrix_score(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 20,\n 1 <= grid[0].len() <= 20,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid[0].len() ==> 0 <= #[trigger] grid[r][c] <= 1,\n ensures\n result as int == Self::score_rev_prefix(grid@, Self::cols(grid@)),\n {\n let rows = grid.len();\n let cols = grid[0].len();\n let ghost g = grid@;\n proof {\n assert(rows as int == g.len());\n assert(cols as int == Self::cols(g));\n Self::lemma_pow2_bound(cols as int);\n Self::lemma_score_rev_prefix_i32_bound(g, 0);\n }\n let mut result: i32 = 0;\n let mut place: i32 = 1;\n let mut j: usize = cols;\n while j > 0\n invariant\n g == grid@,\n 1 <= grid.len() <= 20,\n 1 <= grid[0].len() <= 20,\n rows == grid.len(),\n cols == grid[0].len(),\n rows as int == g.len(),\n cols as int == Self::cols(g),\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid[0].len() ==> 0 <= #[trigger] grid[r][c] <= 1,\n 0 <= j <= cols,\n place as int == Self::pow2((cols - j) as int),\n 1 <= place as int <= 1_048_576,\n result as int == Self::score_rev_prefix(g, (cols - j) as int),\n 0 <= result as int <= 20_971_500,\n decreases j,\n {\n let col = j - 1;\n let mut ones: i32 = 0;\n let mut i: usize = 0;\n while i < rows\n invariant\n g == grid@,\n 1 <= grid.len() <= 20,\n 1 <= grid[0].len() <= 20,\n rows == grid.len(),\n cols == grid[0].len(),\n rows as int == g.len(),\n cols as int == Self::cols(g),\n 0 < j <= cols,\n col + 1 == j,\n col < cols,\n forall|r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid[0].len(),\n forall|r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid[0].len() ==> 0 <= #[trigger] grid[r][c] <= 1,\n 0 <= i <= rows,\n ones as int == Self::normalized_ones_prefix(g, col as int, i as int),\n 0 <= ones as int <= i as int,\n decreases rows - i,\n {\n proof {\n assert(i < grid.len());\n assert(grid[i as int].len() == grid[0].len());\n assert(0 < grid[i as int].len());\n assert(col < grid[i as int].len());\n }\n let one = grid[i][col] == grid[i][0];\n proof {\n Self::lemma_normalized_ones_prefix_step(g, col as int, i as int);\n Self::lemma_normalized_bit_value(g, i as int, col as int);\n assert(Self::normalized_ones_prefix(g, col as int, i as int + 1)\n == Self::normalized_ones_prefix(g, col as int, i as int)\n + if one { 1int } else { 0int });\n }\n if one {\n ones = ones + 1;\n }\n i = i + 1;\n }\n let zeros = rows as i32 - ones;\n let best = if ones >= zeros { ones } else { zeros };\n proof {\n let processed = (cols - j) as int;\n assert(ones as int == Self::normalized_ones_prefix(g, col as int, rows as int));\n reveal(Solution::best_ones_at);\n assert(zeros as int == g.len() - ones as int);\n if ones >= zeros {\n assert(Self::max_int(ones as int, zeros as int) == ones as int);\n } else {\n assert(Self::max_int(ones as int, zeros as int) == zeros as int);\n }\n assert(best as int == Self::best_ones_at(g, col as int));\n Self::lemma_score_rev_prefix_step(g, processed);\n assert(Self::score_rev_prefix(g, processed + 1)\n == Self::score_rev_prefix(g, processed)\n + Self::best_ones_at(g, col as int) * Self::pow2(processed));\n assert(result as int + best as int * place as int == Self::score_rev_prefix(g, processed + 1));\n Self::lemma_score_rev_prefix_i32_bound(g, processed + 1);\n assert(result as int + best as int * place as int <= 20_971_500);\n Self::lemma_pow2_bound(processed + 1);\n assert(place as int * 2 == Self::pow2(processed + 1)) by (nonlinear_arith)\n requires\n place as int == Self::pow2(processed),\n Self::pow2(processed + 1) == 2 * Self::pow2(processed),\n {};\n }\n result = result + best * place;\n place = place * 2;\n j = j - 1;\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc861/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc861/tests/mutated_testcases.jsonl"} {"problem": "lc867", "family": "leetcode", "description": "# Transpose Matrix\n\nGiven a 2D integer array `matrix`, return the transpose of `matrix`.\n\nThe transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.\n\n## Example 1:\n\n> **Input:** matrix = [[1,2,3],[4,5,6],[7,8,9]]\n> **Output:** [[1,4,7],[2,5,8],[3,6,9]]\n\n## Example 2:\n\n> **Input:** matrix = [[1,2,3],[4,5,6]]\n> **Output:** [[1,4],[2,5],[3,6]]\n\n## Constraints:\n\n- $m == matrix.length$\n- $n == matrix[i].length$\n- $1 <= m, n <= 1000$\n- $1 <= m * n <= 10^5$\n- $-10^9 <= matrix[i][j] <= 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn transpose(matrix: Vec>) -> Vec> {\n \n }\n}\n```\n", "tags": "Easy\nArray, Matrix, Simulation\nAcceptance rate: 76.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn transpose(matrix: Vec>) -> (res: Vec>)\n requires\n 1 <= matrix.len() <= 1_000,\n 1 <= matrix[0].len() <= 1_000,\n forall |r: int| 0 <= r < matrix.len() ==> #[trigger] matrix[r].len() == matrix[0].len(),\n matrix.len() * matrix[0].len() <= 100_000,\n forall |r: int, c: int| 0 <= r < matrix.len() && 0 <= c < matrix[r].len() ==> -1_000_000_000 <= #[trigger] matrix[r][c] <= 1_000_000_000,\n ensures\n res.len() == matrix[0].len(),\n forall |c: int| 0 <= c < res.len() ==> #[trigger] res[c].len() == matrix.len(),\n forall |r: int, c: int| 0 <= r < matrix.len() && 0 <= c < matrix[0].len() ==> #[trigger] res[c][r] == matrix[r][c],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn transpose(matrix: Vec>) -> Vec> {\n let rows = matrix.len();\n let cols = matrix[0].len();\n let mut result: Vec> = Vec::new();\n let mut c: usize = 0;\n while c < cols {\n let mut row: Vec = Vec::new();\n let mut r: usize = 0;\n while r < rows {\n row.push(matrix[r][c]);\n r += 1;\n }\n result.push(row);\n c += 1;\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn transpose(matrix: Vec>) -> (res: Vec>)\n requires\n 1 <= matrix.len() <= 1_000,\n 1 <= matrix[0].len() <= 1_000,\n forall |r: int| 0 <= r < matrix.len() ==> #[trigger] matrix[r].len() == matrix[0].len(),\n matrix.len() * matrix[0].len() <= 100_000,\n forall |r: int, c: int| 0 <= r < matrix.len() && 0 <= c < matrix[r].len() ==> -1_000_000_000 <= #[trigger] matrix[r][c] <= 1_000_000_000,\n ensures\n res.len() == matrix[0].len(),\n forall |c: int| 0 <= c < res.len() ==> #[trigger] res[c].len() == matrix.len(),\n forall |r: int, c: int| 0 <= r < matrix.len() && 0 <= c < matrix[0].len() ==> #[trigger] res[c][r] == matrix[r][c],\n {\n let rows = matrix.len();\n let cols = matrix[0].len();\n let mut result: Vec> = Vec::new();\n let mut c: usize = 0;\n while c < cols {\n let mut row: Vec = Vec::new();\n let mut r: usize = 0;\n while r < rows {\n row.push(matrix[r][c]);\n r += 1;\n }\n result.push(row);\n c += 1;\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub fn transpose(matrix: Vec>) -> (res: Vec>)\n requires\n 1 <= matrix.len() <= 1_000,\n 1 <= matrix[0].len() <= 1_000,\n forall |r: int| 0 <= r < matrix.len() ==> #[trigger] matrix[r].len() == matrix[0].len(),\n matrix.len() * matrix[0].len() <= 100_000,\n forall |r: int, c: int| 0 <= r < matrix.len() && 0 <= c < matrix[r].len() ==> -1_000_000_000 <= #[trigger] matrix[r][c] <= 1_000_000_000,\n ensures\n res.len() == matrix[0].len(),\n forall |c: int| 0 <= c < res.len() ==> #[trigger] res[c].len() == matrix.len(),\n forall |r: int, c: int| 0 <= r < matrix.len() && 0 <= c < matrix[0].len() ==> #[trigger] res[c][r] == matrix[r][c],\n {\n let rows = matrix.len();\n let cols = matrix[0].len();\n let mut result: Vec> = Vec::new();\n let mut c: usize = 0;\n while c < cols\n invariant\n rows == matrix.len(),\n cols == matrix[0].len(),\n 1 <= rows <= 1_000,\n 1 <= cols <= 1_000,\n rows * cols <= 100_000,\n forall |r: int| 0 <= r < rows ==> #[trigger] matrix[r].len() == cols,\n forall |r: int, cc: int| 0 <= r < rows && 0 <= cc < cols ==> -1_000_000_000 <= #[trigger] matrix[r][cc] <= 1_000_000_000,\n c <= cols,\n result.len() == c,\n forall |cc: int| 0 <= cc < c ==> #[trigger] result[cc].len() == rows,\n forall |r: int, cc: int| 0 <= r < rows && 0 <= cc < c ==> #[trigger] result[cc][r] == matrix[r][cc],\n decreases cols - c,\n {\n let mut row: Vec = Vec::new();\n let mut r: usize = 0;\n while r < rows\n invariant\n rows == matrix.len(),\n cols == matrix[0].len(),\n 1 <= rows <= 1_000,\n 1 <= cols <= 1_000,\n rows * cols <= 100_000,\n forall |rr: int| 0 <= rr < rows ==> #[trigger] matrix[rr].len() == cols,\n forall |rr: int, cc: int| 0 <= rr < rows && 0 <= cc < cols ==> -1_000_000_000 <= #[trigger] matrix[rr][cc] <= 1_000_000_000,\n c < cols,\n r <= rows,\n row.len() == r,\n forall |rr: int| 0 <= rr < r ==> #[trigger] row[rr] == matrix[rr][c as int],\n decreases rows - r,\n {\n proof {\n assert(matrix[r as int].len() == cols);\n }\n row.push(matrix[r][c]);\n r += 1;\n }\n result.push(row);\n c += 1;\n }\n result\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc867/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc867/tests/mutated_testcases.jsonl"} {"problem": "lc868", "family": "leetcode", "description": "# Binary Gap\n\nGiven a positive integer `n`, return the longest distance between any two adjacent `1`'s in the binary representation of `n`. If there are no two adjacent `1`'s, return `0`.\n\nTwo `1`'s are adjacent if there are only `0`'s separating them, possibly none. The distance between two `1`'s is the absolute difference between their bit positions. For example, the two `1`'s in `\"1001\"` have a distance of 3.\n\n## Example 1:\n\n> **Input:** n = 22\n> **Output:** 2\n> **Explanation:** 22 in binary is `\"10110\"`.\n> The first adjacent pair of `1`'s in `\"10110\"` has a distance of 2.\n> The second adjacent pair of `1`'s in `\"10110\"` has a distance of 1.\n> The answer is the largest of these two distances, which is 2.\n> Note that `\"10110\"` is not a valid pair since there is a `1` separating the two `1`'s under consideration.\n\n## Example 2:\n\n> **Input:** n = 8\n> **Output:** 0\n> **Explanation:** 8 in binary is `\"1000\"`.\n> There are no adjacent pairs of `1`'s in the binary representation of 8.\n\n## Example 3:\n\n> **Input:** n = 5\n> **Output:** 2\n> **Explanation:** 5 in binary is `\"101\"`.\n\n## Constraints:\n\n- $1 \\le n \\le 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn binary_gap(n: i32) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nBit Manipulation\nAcceptance rate: 74.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn bit_is_one(n: u32, k: int) -> bool {\n 0 <= k < 31 && (((n >> (k as u32)) & 1u32) == 1u32)\n }\n\n pub open spec fn last_one_pos(n: u32, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n -1\n } else if Self::bit_is_one(n, upto - 1) {\n upto - 1\n } else {\n Self::last_one_pos(n, upto - 1)\n }\n }\n\n pub open spec fn new_gap_at(n: u32, pos: int) -> int {\n let last = Self::last_one_pos(n, pos);\n if last >= 0 { pos - last } else { 0 }\n }\n\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a > b { a } else { b }\n }\n\n pub open spec fn binary_gap_prefix(n: u32, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else if Self::bit_is_one(n, upto - 1) {\n Self::spec_max(\n Self::binary_gap_prefix(n, upto - 1),\n Self::new_gap_at(n, upto - 1),\n )\n } else {\n Self::binary_gap_prefix(n, upto - 1)\n }\n }\n\n pub open spec fn binary_gap_spec(n: u32) -> int {\n Self::binary_gap_prefix(n, 31)\n }\n\n pub fn binary_gap(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1_000_000_000,\n ensures\n result as int == Self::binary_gap_spec(n as u32),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn binary_gap(n: i32) -> i32 {\n let nu = n as u32;\n let mut m = nu;\n let mut pos: u32 = 0;\n let mut best: u32 = 0;\n let mut last: u32 = 0;\n let mut has_last = false;\n while pos < 31u32 {\n if (m & 1u32) == 1u32 {\n if has_last {\n let gap = pos - last;\n if gap > best {\n best = gap;\n }\n }\n last = pos;\n has_last = true;\n }\n m = m >> 1u32;\n pos = pos + 1u32;\n }\n best as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn bit_is_one(n: u32, k: int) -> bool {\n 0 <= k < 31 && (((n >> (k as u32)) & 1u32) == 1u32)\n }\n\n pub open spec fn last_one_pos(n: u32, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n -1\n } else if Self::bit_is_one(n, upto - 1) {\n upto - 1\n } else {\n Self::last_one_pos(n, upto - 1)\n }\n }\n\n pub open spec fn new_gap_at(n: u32, pos: int) -> int {\n let last = Self::last_one_pos(n, pos);\n if last >= 0 { pos - last } else { 0 }\n }\n\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a > b { a } else { b }\n }\n\n pub open spec fn binary_gap_prefix(n: u32, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else if Self::bit_is_one(n, upto - 1) {\n Self::spec_max(\n Self::binary_gap_prefix(n, upto - 1),\n Self::new_gap_at(n, upto - 1),\n )\n } else {\n Self::binary_gap_prefix(n, upto - 1)\n }\n }\n\n pub open spec fn binary_gap_spec(n: u32) -> int {\n Self::binary_gap_prefix(n, 31)\n }\n\n pub fn binary_gap(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1_000_000_000,\n ensures\n result as int == Self::binary_gap_spec(n as u32),\n {\n let nu = n as u32;\n let mut m = nu;\n let mut pos: u32 = 0;\n let mut best: u32 = 0;\n let mut last: u32 = 0;\n let mut has_last = false;\n while pos < 31u32 {\n if (m & 1u32) == 1u32 {\n if has_last {\n let gap = pos - last;\n if gap > best {\n best = gap;\n }\n }\n last = pos;\n has_last = true;\n }\n m = m >> 1u32;\n pos = pos + 1u32;\n }\n best as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn bit_is_one(n: u32, k: int) -> bool {\n 0 <= k < 31 && (((n >> (k as u32)) & 1u32) == 1u32)\n }\n\n pub open spec fn last_one_pos(n: u32, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n -1\n } else if Self::bit_is_one(n, upto - 1) {\n upto - 1\n } else {\n Self::last_one_pos(n, upto - 1)\n }\n }\n\n pub open spec fn new_gap_at(n: u32, pos: int) -> int {\n let last = Self::last_one_pos(n, pos);\n if last >= 0 { pos - last } else { 0 }\n }\n\n pub open spec fn spec_max(a: int, b: int) -> int {\n if a > b { a } else { b }\n }\n\n pub open spec fn binary_gap_prefix(n: u32, upto: int) -> int\n decreases upto,\n {\n if upto <= 0 {\n 0\n } else if Self::bit_is_one(n, upto - 1) {\n Self::spec_max(\n Self::binary_gap_prefix(n, upto - 1),\n Self::new_gap_at(n, upto - 1),\n )\n } else {\n Self::binary_gap_prefix(n, upto - 1)\n }\n }\n\n pub open spec fn binary_gap_spec(n: u32) -> int {\n Self::binary_gap_prefix(n, 31)\n }\n\n pub proof fn lemma_last_one_pos_step(n: u32, upto: int)\n requires\n 0 <= upto < 31,\n ensures\n if Self::bit_is_one(n, upto) {\n Self::last_one_pos(n, upto + 1) == upto\n } else {\n Self::last_one_pos(n, upto + 1) == Self::last_one_pos(n, upto)\n },\n {\n reveal_with_fuel(Solution::last_one_pos, 2);\n }\n\n pub proof fn lemma_binary_gap_prefix_step(n: u32, upto: int)\n requires\n 0 <= upto < 31,\n ensures\n Self::binary_gap_prefix(n, upto + 1) == if Self::bit_is_one(n, upto) {\n Self::spec_max(Self::binary_gap_prefix(n, upto), Self::new_gap_at(n, upto))\n } else {\n Self::binary_gap_prefix(n, upto)\n },\n {\n reveal_with_fuel(Solution::binary_gap_prefix, 2);\n }\n\n pub fn binary_gap(n: i32) -> (result: i32)\n requires\n 1 <= n <= 1_000_000_000,\n ensures\n result as int == Self::binary_gap_spec(n as u32),\n {\n let nu = n as u32;\n let mut m = nu;\n let mut pos: u32 = 0;\n let mut best: u32 = 0;\n let mut last: u32 = 0;\n let mut has_last = false;\n proof {\n assert(m == nu >> pos) by (bit_vector)\n requires\n m == nu,\n pos == 0u32,\n {\n }\n assert(Self::binary_gap_prefix(nu, pos as int) == 0);\n assert(Self::last_one_pos(nu, pos as int) == -1);\n }\n while pos < 31u32\n invariant\n 1 <= n <= 1_000_000_000,\n nu == n as u32,\n pos <= 31u32,\n m == nu >> pos,\n best as int == Self::binary_gap_prefix(nu, pos as int),\n best <= pos,\n has_last ==> last < pos,\n has_last ==> last as int == Self::last_one_pos(nu, pos as int),\n !has_last ==> Self::last_one_pos(nu, pos as int) == -1,\n decreases 31u32 - pos,\n {\n let ghost old_pos = pos;\n let ghost old_m = m;\n let ghost bit = (m & 1u32) == 1u32;\n proof {\n assert(Self::bit_is_one(nu, pos as int) == (((nu >> pos) & 1u32) == 1u32));\n assert(((nu >> pos) & 1u32) == (m & 1u32)) by (bit_vector)\n requires\n pos < 31u32,\n m == nu >> pos,\n {\n }\n assert((((nu >> pos) & 1u32) == 1u32) == bit);\n assert(Self::bit_is_one(nu, pos as int) == bit);\n Self::lemma_last_one_pos_step(nu, pos as int);\n Self::lemma_binary_gap_prefix_step(nu, pos as int);\n }\n if (m & 1u32) == 1u32 {\n if has_last {\n let ghost old_best = best;\n let ghost old_last = last;\n let gap = pos - last;\n if gap > best {\n best = gap;\n }\n proof {\n assert(old_best as int == Self::binary_gap_prefix(nu, pos as int));\n assert(old_last as int == Self::last_one_pos(nu, pos as int));\n assert(Self::new_gap_at(nu, pos as int) == pos as int - Self::last_one_pos(nu, pos as int));\n assert(gap as int == Self::new_gap_at(nu, pos as int));\n assert(best as int == if gap > old_best { gap } else { old_best });\n assert(Self::binary_gap_prefix(nu, pos as int + 1) == Self::spec_max(Self::binary_gap_prefix(nu, pos as int), Self::new_gap_at(nu, pos as int)));\n assert(best as int == Self::binary_gap_prefix(nu, pos as int + 1));\n }\n } else {\n proof {\n assert(Self::last_one_pos(nu, pos as int) == -1);\n assert(Self::new_gap_at(nu, pos as int) == 0);\n assert(Self::spec_max(Self::binary_gap_prefix(nu, pos as int), Self::new_gap_at(nu, pos as int)) == Self::binary_gap_prefix(nu, pos as int));\n assert(Self::binary_gap_prefix(nu, pos as int + 1) == Self::binary_gap_prefix(nu, pos as int));\n assert(best as int == Self::binary_gap_prefix(nu, pos as int + 1));\n }\n }\n last = pos;\n has_last = true;\n proof {\n assert(last as int == pos as int);\n assert(last as int == Self::last_one_pos(nu, pos as int + 1));\n }\n } else {\n proof {\n assert(Self::binary_gap_prefix(nu, pos as int + 1) == Self::binary_gap_prefix(nu, pos as int));\n if has_last {\n assert(last as int == Self::last_one_pos(nu, pos as int));\n assert(last as int == Self::last_one_pos(nu, pos as int + 1));\n } else {\n assert(Self::last_one_pos(nu, pos as int + 1) == Self::last_one_pos(nu, pos as int));\n assert(Self::last_one_pos(nu, pos as int + 1) == -1);\n }\n }\n }\n m = m >> 1u32;\n proof {\n assert((nu >> old_pos) >> 1u32 == nu >> (old_pos + 1u32)) by (bit_vector)\n requires\n old_pos < 31u32,\n {\n }\n assert(m == old_m >> 1u32);\n assert(m == nu >> (old_pos + 1u32));\n }\n pos = pos + 1u32;\n }\n proof {\n assert(pos == 31u32);\n assert(best as int == Self::binary_gap_prefix(nu, 31));\n }\n best as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc868/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc868/tests/mutated_testcases.jsonl"} {"problem": "lc892", "family": "leetcode", "description": "# Surface Area of 3D Shapes\n\nYou are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.\n\nAfter placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.\n\nReturn *the total surface area of the resulting shapes*.\n\n**Note:** The bottom face of each shape counts toward its surface area.\n\n## Example 1:\n\n> **Input:** grid = [[1,2],[3,4]]\n> **Output:** 34\n\n## Example 2:\n\n> **Input:** grid = [[1,1,1],[1,0,1],[1,1,1]]\n> **Output:** 32\n\n## Example 3:\n\n> **Input:** grid = [[2,2,2],[2,1,2],[2,2,2]]\n> **Output:** 46\n\n## Constraints:\n\n- `n == grid.length == grid[i].length`\n- `1 <= n <= 50`\n- `0 <= grid[i][j] <= 50`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn surface_area(grid: Vec>) -> i32 {\n \n }\n}\n```\n", "tags": "Easy\nArray, Math, Geometry, Matrix\nAcceptance rate: 70.5%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn cell_contrib(grid: Seq>, i: int, j: int, n: int) -> int\n recommends\n 0 <= i < n,\n 0 <= j < n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n {\n let v = grid[i][j] as int;\n let base = if v > 0 { 4 * v + 2 } else { 0 };\n let up = if i > 0 { 2 * Self::spec_min(v, grid[i - 1][j] as int) } else { 0 };\n let left = if j > 0 { 2 * Self::spec_min(v, grid[i][j - 1] as int) } else { 0 };\n base - up - left\n }\n\n pub open spec fn row_sum(grid: Seq>, n: int, row: int, col_end: int) -> int\n recommends\n 0 <= row < n,\n 0 <= col_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n decreases col_end,\n {\n if col_end <= 0 {\n 0\n } else {\n Self::row_sum(grid, n, row, col_end - 1) + Self::cell_contrib(grid, row, col_end - 1, n)\n }\n }\n\n pub open spec fn surface_area_spec(grid: Seq>, n: int, row_end: int) -> int\n recommends\n 0 <= row_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n decreases row_end,\n {\n if row_end <= 0 {\n 0\n } else {\n Self::surface_area_spec(grid, n, row_end - 1) + Self::row_sum(grid, n, row_end - 1, n)\n }\n }\n\n pub open spec fn surface_area_total(grid: Seq>, n: int) -> int\n recommends\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n {\n Self::surface_area_spec(grid, n, n)\n }\n\n pub fn surface_area(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 50,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid.len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid.len() ==> 0 <= #[trigger] grid[i][j] <= 50,\n ensures\n result as int == Self::surface_area_total(grid@, grid.len() as int),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn surface_area(grid: Vec>) -> i32 {\n let n = grid.len();\n let mut res: i32 = 0;\n let mut i: usize = 0;\n\n while i < n {\n let mut j: usize = 0;\n while j < n {\n if grid[i][j] > 0 {\n res = res + grid[i][j] * 4 + 2;\n }\n if i > 0 {\n let a = grid[i][j];\n let b = grid[i - 1][j];\n let m = if a <= b { a } else { b };\n res = res - m * 2;\n }\n if j > 0 {\n let a = grid[i][j];\n let b = grid[i][j - 1];\n let m = if a <= b { a } else { b };\n res = res - m * 2;\n }\n j += 1;\n }\n i += 1;\n }\n\n res\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn cell_contrib(grid: Seq>, i: int, j: int, n: int) -> int\n recommends\n 0 <= i < n,\n 0 <= j < n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n {\n let v = grid[i][j] as int;\n let base = if v > 0 { 4 * v + 2 } else { 0 };\n let up = if i > 0 { 2 * Self::spec_min(v, grid[i - 1][j] as int) } else { 0 };\n let left = if j > 0 { 2 * Self::spec_min(v, grid[i][j - 1] as int) } else { 0 };\n base - up - left\n }\n\n pub open spec fn row_sum(grid: Seq>, n: int, row: int, col_end: int) -> int\n recommends\n 0 <= row < n,\n 0 <= col_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n decreases col_end,\n {\n if col_end <= 0 {\n 0\n } else {\n Self::row_sum(grid, n, row, col_end - 1) + Self::cell_contrib(grid, row, col_end - 1, n)\n }\n }\n\n pub open spec fn surface_area_spec(grid: Seq>, n: int, row_end: int) -> int\n recommends\n 0 <= row_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n decreases row_end,\n {\n if row_end <= 0 {\n 0\n } else {\n Self::surface_area_spec(grid, n, row_end - 1) + Self::row_sum(grid, n, row_end - 1, n)\n }\n }\n\n pub open spec fn surface_area_total(grid: Seq>, n: int) -> int\n recommends\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n {\n Self::surface_area_spec(grid, n, n)\n }\n\n pub fn surface_area(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 50,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid.len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid.len() ==> 0 <= #[trigger] grid[i][j] <= 50,\n ensures\n result as int == Self::surface_area_total(grid@, grid.len() as int),\n {\n let n = grid.len();\n let mut res: i32 = 0;\n let mut i: usize = 0;\n\n while i < n\n decreases n - i,\n {\n let mut j: usize = 0;\n\n while j < n\n decreases n - j,\n {\n if grid[i][j] > 0 {\n res = res + grid[i][j] * 4 + 2;\n }\n if i > 0 {\n let a = grid[i][j];\n let b = grid[i - 1][j];\n let m = if a <= b { a } else { b };\n res = res - m * 2;\n }\n if j > 0 {\n let a = grid[i][j];\n let b = grid[i][j - 1];\n let m = if a <= b { a } else { b };\n res = res - m * 2;\n }\n j += 1;\n }\n i += 1;\n }\n\n res\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn spec_min(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn cell_contrib(grid: Seq>, i: int, j: int, n: int) -> int\n recommends\n 0 <= i < n,\n 0 <= j < n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n {\n let v = grid[i][j] as int;\n let base = if v > 0 { 4 * v + 2 } else { 0 };\n let up = if i > 0 { 2 * Self::spec_min(v, grid[i - 1][j] as int) } else { 0 };\n let left = if j > 0 { 2 * Self::spec_min(v, grid[i][j - 1] as int) } else { 0 };\n base - up - left\n }\n\n pub open spec fn row_sum(grid: Seq>, n: int, row: int, col_end: int) -> int\n recommends\n 0 <= row < n,\n 0 <= col_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n decreases col_end,\n {\n if col_end <= 0 {\n 0\n } else {\n Self::row_sum(grid, n, row, col_end - 1) + Self::cell_contrib(grid, row, col_end - 1, n)\n }\n }\n\n pub open spec fn surface_area_spec(grid: Seq>, n: int, row_end: int) -> int\n recommends\n 0 <= row_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n decreases row_end,\n {\n if row_end <= 0 {\n 0\n } else {\n Self::surface_area_spec(grid, n, row_end - 1) + Self::row_sum(grid, n, row_end - 1, n)\n }\n }\n\n pub open spec fn surface_area_total(grid: Seq>, n: int) -> int\n recommends\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n {\n Self::surface_area_spec(grid, n, n)\n }\n\n proof fn lemma_cell_contrib_nonneg(grid: Seq>, i: int, j: int, n: int)\n requires\n 0 <= i < n,\n 0 <= j < n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid.len() ==> 0 <= #[trigger] grid[r][c] <= 50,\n ensures\n 0 <= Self::cell_contrib(grid, i, j, n),\n {\n reveal(Solution::cell_contrib);\n assert(Self::spec_min(grid[i][j] as int, grid[i][j] as int) == grid[i][j] as int);\n }\n\n proof fn lemma_row_sum_full(grid: Seq>, n: int, row: int)\n requires\n 0 <= row < n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n ensures\n Self::surface_area_spec(grid, n, row + 1) == Self::surface_area_spec(grid, n, row)\n + Self::row_sum(grid, n, row, n),\n decreases n - row,\n {\n reveal_with_fuel(Solution::surface_area_spec, 2);\n reveal_with_fuel(Solution::row_sum, 1);\n }\n\n proof fn lemma_cell_contrib_bounded(grid: Seq>, i: int, j: int, n: int)\n requires\n 0 <= i < n,\n 0 <= j < n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid.len() ==> 0 <= #[trigger] grid[r][c] <= 50,\n ensures\n Self::cell_contrib(grid, i, j, n) <= 202,\n decreases 0int,\n {\n reveal(Solution::cell_contrib);\n }\n\n proof fn lemma_surface_area_bounded(grid: Seq>, n: int, row_end: int)\n requires\n 0 <= row_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid.len() ==> 0 <= #[trigger] grid[r][c] <= 50,\n ensures\n Self::surface_area_spec(grid, n, row_end) <= 202 * row_end * n,\n decreases row_end,\n {\n if row_end > 0 {\n Self::lemma_surface_area_bounded(grid, n, row_end - 1);\n Self::lemma_row_sum_bounded(grid, n, row_end - 1, n);\n reveal_with_fuel(Solution::surface_area_spec, 2);\n reveal_with_fuel(Solution::row_sum, 1);\n assert(Self::surface_area_spec(grid, n, row_end) <= 202 * row_end * n) by (nonlinear_arith)\n requires\n Self::surface_area_spec(grid, n, row_end - 1) <= 202 * (row_end - 1) * n,\n Self::row_sum(grid, n, row_end - 1, n) <= 202 * n,\n Self::surface_area_spec(grid, n, row_end)\n == Self::surface_area_spec(grid, n, row_end - 1)\n + Self::row_sum(grid, n, row_end - 1, n);\n }\n }\n\n proof fn lemma_row_sum_bounded(grid: Seq>, n: int, row: int, col_end: int)\n requires\n 0 <= row < n,\n 0 <= col_end <= n,\n 0 <= n <= grid.len(),\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid.len() ==> 0 <= #[trigger] grid[r][c] <= 50,\n ensures\n Self::row_sum(grid, n, row, col_end) <= 202 * col_end,\n decreases col_end,\n {\n if col_end > 0 {\n Self::lemma_row_sum_bounded(grid, n, row, col_end - 1);\n Self::lemma_cell_contrib_bounded(grid, row, col_end - 1, n);\n reveal_with_fuel(Solution::row_sum, 2);\n assert(Self::row_sum(grid, n, row, col_end) <= 202 * col_end) by (nonlinear_arith)\n requires\n Self::row_sum(grid, n, row, col_end - 1) <= 202 * (col_end - 1),\n Self::cell_contrib(grid, row, col_end - 1, n) <= 202,\n Self::row_sum(grid, n, row, col_end)\n == Self::row_sum(grid, n, row, col_end - 1)\n + Self::cell_contrib(grid, row, col_end - 1, n);\n }\n }\n\n pub fn surface_area(grid: Vec>) -> (result: i32)\n requires\n 1 <= grid.len() <= 50,\n forall |i: int| 0 <= i < grid.len() ==> #[trigger] grid[i].len() == grid.len(),\n forall |i: int, j: int|\n 0 <= i < grid.len() && 0 <= j < grid.len() ==> 0 <= #[trigger] grid[i][j] <= 50,\n ensures\n result as int == Self::surface_area_total(grid@, grid.len() as int),\n {\n let n = grid.len();\n let mut res: i32 = 0;\n let mut i: usize = 0;\n\n while i < n\n invariant\n n == grid.len(),\n 1 <= n <= 50,\n 0 <= i <= n,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid.len() ==> 0 <= #[trigger] grid[r][c] <= 50,\n res as int == Self::surface_area_spec(grid@, n as int, i as int),\n 0 <= res as int <= 202 * (n as int) * (n as int),\n decreases n - i,\n {\n proof {\n Self::lemma_surface_area_bounded(grid@, n as int, i as int);\n }\n let mut j: usize = 0;\n\n while j < n\n invariant\n n == grid.len(),\n 1 <= n <= 50,\n i < n,\n 0 <= j <= n,\n forall |r: int| 0 <= r < grid.len() ==> #[trigger] grid[r].len() == grid.len(),\n forall |r: int, c: int|\n 0 <= r < grid.len() && 0 <= c < grid.len() ==> 0 <= #[trigger] grid[r][c] <= 50,\n res as int == Self::surface_area_spec(grid@, n as int, i as int)\n + Self::row_sum(grid@, n as int, i as int, j as int),\n 0 <= res as int,\n res as int <= 202 * (n as int) * (n as int),\n decreases n - j,\n {\n proof {\n assert((i as int) < grid.len() as int);\n assert(grid[i as int].len() == grid.len());\n assert((j as int) < n as int);\n assert((j as int) < grid[i as int].len());\n }\n if grid[i][j] > 0 {\n proof {\n assert(202 * 50 * 50 <= 2147483647) by (nonlinear_arith);\n assert((n as int) <= 50);\n assert(202 * (n as int) * (n as int) <= 2147483647) by (nonlinear_arith)\n requires (n as int) <= 50, 202 * 50 * 50 <= 2147483647;\n assert(res as int + (grid@[i as int][j as int] as int) * 4 + 2 <= 202 * (n as int) * (n as int) + 202)\n by (nonlinear_arith)\n requires\n res as int <= 202 * (n as int) * (n as int),\n (grid@[i as int][j as int] as int) <= 50;\n assert(202 * (n as int) * (n as int) + 202 <= 2147483647) by (nonlinear_arith)\n requires 202 * (n as int) * (n as int) <= 2147483647;\n assert(res as int + (grid@[i as int][j as int] as int) * 4 + 2 <= 2147483647) by (nonlinear_arith)\n requires\n res as int + (grid@[i as int][j as int] as int) * 4 + 2 <= 202 * (n as int) * (n as int) + 202,\n 202 * (n as int) * (n as int) + 202 <= 2147483647;\n }\n res = res + grid[i][j] * 4 + 2;\n }\n if i > 0 {\n proof {\n assert((i as int - 1) >= 0);\n assert((i as int - 1) < grid@.len());\n assert(grid@[i as int - 1].len() == grid@.len());\n assert((j as int) < grid@[i as int - 1].len());\n }\n let a = grid[i][j];\n let b = grid[i - 1][j];\n let m = if a <= b { a } else { b };\n proof {\n assert(res as int >= 2 * (m as int));\n }\n res = res - m * 2;\n }\n if j > 0 {\n proof {\n assert((j as int - 1) >= 0);\n assert((j as int - 1) < grid@[i as int].len());\n }\n let a = grid[i][j];\n let b = grid[i][j - 1];\n let m = if a <= b { a } else { b };\n proof {\n assert(res as int >= 2 * (m as int));\n }\n res = res - m * 2;\n }\n proof {\n Self::lemma_surface_area_bounded(grid@, n as int, i as int);\n Self::lemma_row_sum_bounded(grid@, n as int, i as int, (j as int) + 1);\n assert((i as int) * (n as int) + (j as int) + 1 <= (n as int) * (n as int))\n by (nonlinear_arith) requires (i as int) < (n as int), (j as int) < (n as int);\n assert(res as int <= 202 * (n as int) * (n as int)) by (nonlinear_arith)\n requires\n res as int == Self::surface_area_spec(grid@, n as int, i as int)\n + Self::row_sum(grid@, n as int, i as int, (j as int) + 1),\n Self::surface_area_spec(grid@, n as int, i as int) <= 202 * (i as int) * (n as int),\n Self::row_sum(grid@, n as int, i as int, (j as int) + 1) <= 202 * ((j as int) + 1),\n (i as int) * (n as int) + (j as int) + 1 <= (n as int) * (n as int);\n }\n j += 1;\n }\n\n proof {\n Self::lemma_row_sum_full(grid@, n as int, i as int);\n Self::lemma_surface_area_bounded(grid@, n as int, (i as int) + 1);\n assert((i as int) + 1 <= n as int);\n assert(202 * ((i as int) + 1) * (n as int) <= 202 * (n as int) * (n as int))\n by (nonlinear_arith) requires (i as int) + 1 <= n as int;\n assert(res as int <= 202 * (n as int) * (n as int));\n }\n i += 1;\n }\n\n res\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc892/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc892/tests/mutated_testcases.jsonl"} {"problem": "lc896", "family": "leetcode", "description": "# Monotonic Array\n\nAn array is **monotonic** if it is either monotone increasing or monotone decreasing.\n\nAn array `nums` is monotone increasing if for all `i <= j`, `nums[i] <= nums[j]`. An array `nums` is monotone decreasing if for all `i <= j`, `nums[i] >= nums[j]`.\n\nGiven an integer array `nums`, return `true` if the given array is monotonic, or `false` otherwise.\n\n## Example 1:\n\n> **Input:** nums = [1,2,2,3]\n> **Output:** true\n\n## Example 2:\n\n> **Input:** nums = [6,5,4,4]\n> **Output:** true\n\n## Example 3:\n\n> **Input:** nums = [1,3,2]\n> **Output:** false\n\n## Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^5 <= nums[i] <= 10^5`\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_monotonic(nums: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nJunior, Array, Weekly Contest 100\nAcceptance rate: 62.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_inc_violations(s: Seq, start: int) -> int\n decreases s.len() - start,\n {\n if start + 1 >= s.len() {\n 0\n } else {\n (if s[start] > s[start + 1] { 1int } else { 0int })\n + Self::count_inc_violations(s, start + 1)\n }\n }\n\n pub open spec fn count_dec_violations(s: Seq, start: int) -> int\n decreases s.len() - start,\n {\n if start + 1 >= s.len() {\n 0\n } else {\n (if s[start] < s[start + 1] { 1int } else { 0int })\n + Self::count_dec_violations(s, start + 1)\n }\n }\n\n pub fn is_monotonic(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> -100000 <= #[trigger] nums[i] <= 100000,\n ensures\n result == (Self::count_inc_violations(nums@, 0) == 0 || Self::count_dec_violations(nums@, 0) == 0),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn is_monotonic(nums: Vec) -> bool {\n let n = nums.len();\n let mut inc_bad: i32 = 0;\n let mut dec_bad: i32 = 0;\n let mut i: usize = 0;\n\n while i + 1 < n {\n if nums[i] > nums[i + 1] {\n inc_bad = inc_bad + 1;\n }\n if nums[i] < nums[i + 1] {\n dec_bad = dec_bad + 1;\n }\n i = i + 1;\n }\n\n inc_bad == 0 || dec_bad == 0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_inc_violations(s: Seq, start: int) -> int\n decreases s.len() - start,\n {\n if start + 1 >= s.len() {\n 0\n } else {\n (if s[start] > s[start + 1] { 1int } else { 0int })\n + Self::count_inc_violations(s, start + 1)\n }\n }\n\n pub open spec fn count_dec_violations(s: Seq, start: int) -> int\n decreases s.len() - start,\n {\n if start + 1 >= s.len() {\n 0\n } else {\n (if s[start] < s[start + 1] { 1int } else { 0int })\n + Self::count_dec_violations(s, start + 1)\n }\n }\n\n pub fn is_monotonic(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> -100000 <= #[trigger] nums[i] <= 100000,\n ensures\n result == (Self::count_inc_violations(nums@, 0) == 0 || Self::count_dec_violations(nums@, 0) == 0),\n {\n let n = nums.len();\n let mut inc_bad: i32 = 0;\n let mut dec_bad: i32 = 0;\n let mut i: usize = 0;\n\n while i + 1 < n {\n if nums[i] > nums[i + 1] {\n inc_bad = inc_bad + 1;\n }\n if nums[i] < nums[i + 1] {\n dec_bad = dec_bad + 1;\n }\n i = i + 1;\n }\n\n inc_bad == 0 || dec_bad == 0\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn count_inc_violations(s: Seq, start: int) -> int\n decreases s.len() - start,\n {\n if start + 1 >= s.len() {\n 0\n } else {\n (if s[start] > s[start + 1] { 1int } else { 0int })\n + Self::count_inc_violations(s, start + 1)\n }\n }\n\n pub open spec fn count_dec_violations(s: Seq, start: int) -> int\n decreases s.len() - start,\n {\n if start + 1 >= s.len() {\n 0\n } else {\n (if s[start] < s[start + 1] { 1int } else { 0int })\n + Self::count_dec_violations(s, start + 1)\n }\n }\n\n pub fn is_monotonic(nums: Vec) -> (result: bool)\n requires\n 1 <= nums.len() <= 100000,\n forall |i: int| 0 <= i < nums.len() ==> -100000 <= #[trigger] nums[i] <= 100000,\n ensures\n result == (Self::count_inc_violations(nums@, 0) == 0 || Self::count_dec_violations(nums@, 0) == 0),\n {\n let n = nums.len();\n let mut inc_bad: i32 = 0;\n let mut dec_bad: i32 = 0;\n let mut i: usize = 0;\n\n while i + 1 < n\n invariant\n 0 <= i <= n,\n n == nums.len(),\n 1 <= n <= 100000,\n forall |j: int| 0 <= j < nums.len() ==> -100000 <= #[trigger] nums[j] <= 100000,\n inc_bad as int == Self::count_inc_violations(nums@, 0) - Self::count_inc_violations(nums@, i as int),\n dec_bad as int == Self::count_dec_violations(nums@, 0) - Self::count_dec_violations(nums@, i as int),\n 0 <= inc_bad <= i as i32,\n 0 <= dec_bad <= i as i32,\n decreases n - i,\n {\n if nums[i] > nums[i + 1] {\n inc_bad = inc_bad + 1;\n }\n if nums[i] < nums[i + 1] {\n dec_bad = dec_bad + 1;\n }\n i = i + 1;\n }\n\n inc_bad == 0 || dec_bad == 0\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc896/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc896/tests/mutated_testcases.jsonl"} {"problem": "lc9", "family": "leetcode", "description": "# Palindrome Number\nGiven an integer x, return `true` if `x` is a palindrome, and `false` otherwise.\n\nAn integer is a palindrome when it reads the same forward and backward.\n\nFor example, `121` is a palindrome while `123` is not.\n\n## Example 1:\n\n```\nInput: x = 121\nOutput: true\nExplanation: 121 reads as 121 from left to right and from right to left.\n```\n\n## Example 2:\n\n```\nInput: x = -121\nOutput: false\nExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.\n```\n\n## Example 3:\n\n```\nInput: x = 10\nOutput: false\nExplanation: Reads 01 from right to left. Therefore it is not a palindrome.\n```\n\n## Constraints:\n\n- $ -2^{31} <= x <= 2^{31} - 1$\n\n**Follow up:** Could you solve it without converting the integer to a string?\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn is_palindrome(x: i32) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nMath\nAcceptance rate: 59.8%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn reverse_spec_helper(x: nat, acc: nat) -> nat\n decreases x,\n {\n if x == 0 {\n acc\n } else {\n let last = x % 10;\n let rest = x / 10;\n let new_acc = acc * 10 + last;\n if new_acc > u32::MAX as nat {\n 0\n } else {\n Solution::reverse_spec_helper(rest, new_acc)\n }\n }\n }\n\n pub open spec fn reverse_spec(x: nat) -> nat {\n Solution::reverse_spec_helper(x, 0)\n }\n\n pub open spec fn palindrome_spec(x: int) -> bool {\n if x < 0 {\n false\n } else {\n let xnat = x as nat;\n if Solution::reverse_spec(xnat) == xnat {\n true\n } else {\n false\n }\n }\n }\n\n pub fn is_palindrome(x: i32) -> (res: bool)\n requires\n i32::MIN <= x <= i32::MAX,\n ensures\n res == Solution::palindrome_spec(x as int),\n {\n }\n}\n\n} \n", "code": "impl Solution {\n pub fn is_palindrome(x: i32) -> bool {\n if x < 0 {\n return false;\n }\n let x_unsigned = x as u32;\n let mut cur = x_unsigned;\n let mut rev = 0u32;\n while cur != 0 {\n rev =\n match rev.checked_mul(10) {\n Some(r) => match r.checked_add(cur % 10) {\n Some(n) => n,\n None => {\n return false;\n },\n },\n None => {\n return false;\n },\n };\n cur /= 10;\n }\n rev == x_unsigned\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn reverse_spec_helper(x: nat, acc: nat) -> nat\n decreases x,\n {\n if x == 0 {\n acc\n } else {\n let last = x % 10;\n let rest = x / 10;\n let new_acc = acc * 10 + last;\n if new_acc > u32::MAX as nat {\n 0\n } else {\n Solution::reverse_spec_helper(rest, new_acc)\n }\n }\n }\n\n pub open spec fn reverse_spec(x: nat) -> nat {\n Solution::reverse_spec_helper(x, 0)\n }\n\n pub open spec fn palindrome_spec(x: int) -> bool {\n if x < 0 {\n false\n } else {\n let xnat = x as nat;\n if Solution::reverse_spec(xnat) == xnat {\n true\n } else {\n false\n }\n }\n }\n\n pub fn is_palindrome(x: i32) -> (res: bool)\n requires\n i32::MIN <= x <= i32::MAX,\n ensures\n res == Solution::palindrome_spec(x as int),\n {\n if x < 0 {\n return false;\n }\n let x_unsigned = x as u32;\n let mut cur = x_unsigned;\n let mut rev = 0u32;\n while cur != 0 {\n rev =\n match rev.checked_mul(10) {\n Some(r) => match r.checked_add(cur % 10) {\n Some(n) => n,\n None => {\n return false;\n },\n },\n None => {\n return false;\n },\n };\n cur /= 10;\n }\n rev == x_unsigned\n }\n}\n\n} \n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn reverse_spec_helper(x: nat, acc: nat) -> nat\n decreases x,\n {\n if x == 0 {\n acc\n } else {\n let last = x % 10;\n let rest = x / 10;\n let new_acc = acc * 10 + last;\n if new_acc > u32::MAX as nat {\n 0\n } else {\n Solution::reverse_spec_helper(rest, new_acc)\n }\n }\n }\n\n pub open spec fn reverse_spec(x: nat) -> nat {\n Solution::reverse_spec_helper(x, 0)\n }\n\n pub open spec fn palindrome_spec(x: int) -> bool {\n if x < 0 {\n false\n } else {\n let xnat = x as nat;\n if Solution::reverse_spec(xnat) == xnat {\n true\n } else {\n false\n }\n }\n }\n\n pub fn is_palindrome(x: i32) -> (res: bool)\n requires\n i32::MIN <= x <= i32::MAX,\n ensures\n res == Solution::palindrome_spec(x as int),\n {\n if x < 0 {\n return false;\n }\n let x_unsigned = x as u32;\n let mut cur = x_unsigned;\n let mut rev = 0u32;\n while cur != 0\n invariant\n 0 <= cur <= x,\n Solution::reverse_spec_helper(cur as nat, rev as nat) == Solution::reverse_spec(\n x as nat,\n ),\n decreases cur,\n {\n rev =\n match rev.checked_mul(10) {\n Some(r) => match r.checked_add(cur % 10) {\n Some(n) => n,\n None => {\n return false;\n },\n },\n None => {\n return false;\n },\n };\n cur /= 10;\n }\n rev == x_unsigned\n }\n}\n\n} \n", "main": null, "checker": null, "testcases_path": "leetcode/lc9/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc9/tests/mutated_testcases.jsonl"} {"problem": "lc941", "family": "leetcode", "description": "# Valid Mountain Array\n\nGiven an array of integers `arr`, return `true` if and only if it is a valid mountain array.\n\nRecall that `arr` is a mountain array if and only if:\n\n- `arr.length >= 3`\n- There exists some `i` with `0 < i < arr.length - 1` such that:\n - `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`\n - `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`\n\n## Example 1:\n\n> **Input:** arr = [2,1]\n> **Output:** false\n\n## Example 2:\n\n> **Input:** arr = [3,5,5]\n> **Output:** false\n\n## Example 3:\n\n> **Input:** arr = [0,3,2,1]\n> **Output:** true\n\n## Constraints:\n\n- $1 <= arr.length <= 10^4$\n\n- $0 <= arr[i] <= 10^4$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn valid_mountain_array(arr: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Easy\nArray\nAcceptance rate: 35.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain(s: Seq, peak: int) -> bool {\n s.len() >= 3\n && 0 < peak < s.len() - 1\n && (forall |a: int, b: int| 0 <= a < b <= peak ==> s[a] < s[b])\n && (forall |a: int, b: int| peak <= a < b < s.len() ==> s[a] > s[b])\n }\n\n pub fn valid_mountain_array(arr: Vec) -> (result: bool)\n requires\n 1 <= arr.len() <= 10_000,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 10_000,\n ensures\n result == (exists |peak: int| Self::is_mountain(arr@, peak)),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn valid_mountain_array(arr: Vec) -> bool {\n let n = arr.len();\n if n < 3 {\n return false;\n }\n let mut i: usize = 0;\n while i + 1 < n && arr[i] < arr[i + 1] {\n i = i + 1;\n }\n if i == 0 {\n return false;\n }\n if i == n - 1 {\n return false;\n }\n let peak = i;\n while i + 1 < n && arr[i] > arr[i + 1] {\n i = i + 1;\n }\n if i == n - 1 {\n return true;\n } else {\n return false;\n }\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain(s: Seq, peak: int) -> bool {\n s.len() >= 3\n && 0 < peak < s.len() - 1\n && (forall |a: int, b: int| 0 <= a < b <= peak ==> s[a] < s[b])\n && (forall |a: int, b: int| peak <= a < b < s.len() ==> s[a] > s[b])\n }\n\n pub fn valid_mountain_array(arr: Vec) -> (result: bool)\n requires\n 1 <= arr.len() <= 10_000,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 10_000,\n ensures\n result == (exists |peak: int| Self::is_mountain(arr@, peak)),\n {\n let n = arr.len();\n if n < 3 {\n return false;\n }\n\n let mut i: usize = 0;\n\n while i + 1 < n && arr[i] < arr[i + 1] {\n i = i + 1;\n }\n\n if i == 0 {\n return false;\n }\n\n if i == n - 1 {\n return false;\n }\n\n let peak = i;\n\n while i + 1 < n && arr[i] > arr[i + 1] {\n i = i + 1;\n }\n\n if i == n - 1 {\n return true;\n } else {\n return false;\n }\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn is_mountain(s: Seq, peak: int) -> bool {\n s.len() >= 3\n && 0 < peak < s.len() - 1\n && (forall |a: int, b: int| 0 <= a < b <= peak ==> s[a] < s[b])\n && (forall |a: int, b: int| peak <= a < b < s.len() ==> s[a] > s[b])\n }\n\n pub fn valid_mountain_array(arr: Vec) -> (result: bool)\n requires\n 1 <= arr.len() <= 10_000,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 10_000,\n ensures\n result == (exists |peak: int| Self::is_mountain(arr@, peak)),\n {\n let n = arr.len();\n if n < 3 {\n proof {\n assert forall |p: int| !Self::is_mountain(arr@, p) by {};\n }\n return false;\n }\n\n let mut i: usize = 0;\n\n while i + 1 < n && arr[i] < arr[i + 1]\n invariant\n 0 <= i < n,\n n == arr.len(),\n 1 <= n <= 10_000,\n n >= 3,\n forall |k: int| 0 <= k < n ==> 0 <= #[trigger] arr[k] <= 10_000,\n forall |a: int, b: int| 0 <= a < b <= i as int ==> arr[a] < arr[b],\n decreases n - i,\n {\n i = i + 1;\n }\n\n if i == 0 {\n proof {\n assert forall |p: int| !Self::is_mountain(arr@, p) by {\n if 0 < p < arr@.len() - 1 {\n assert(arr@[0] >= arr@[1]);\n }\n };\n }\n return false;\n }\n\n if i == n - 1 {\n proof {\n assert forall |p: int| !Self::is_mountain(arr@, p) by {\n if 0 < p < arr@.len() - 1 {\n assert(arr@[p] < arr@[p + 1]);\n }\n };\n }\n return false;\n }\n\n let peak = i;\n\n while i + 1 < n && arr[i] > arr[i + 1]\n invariant\n peak <= i < n,\n n == arr.len(),\n 1 <= n <= 10_000,\n 0 < peak < n - 1,\n forall |k: int| 0 <= k < n ==> 0 <= #[trigger] arr[k] <= 10_000,\n forall |a: int, b: int| 0 <= a < b <= peak as int ==> arr[a] < arr[b],\n forall |a: int, b: int| peak as int <= a < b <= i as int ==> arr[a] > arr[b],\n arr@[peak as int] >= arr@[(peak + 1) as int],\n decreases n - i,\n {\n i = i + 1;\n }\n\n if i == n - 1 {\n proof {\n assert(Self::is_mountain(arr@, peak as int));\n }\n return true;\n } else {\n proof {\n assert forall |p: int| !Self::is_mountain(arr@, p) by {\n if 0 < p < arr@.len() - 1 {\n if p > peak as int {\n assert(arr@[peak as int] >= arr@[(peak + 1) as int]);\n assert(0int <= peak as int && (peak as int) + 1 <= p);\n } else {\n assert(p <= i as int);\n assert((i as int) + 1 < arr@.len() as int);\n assert(arr@[i as int] <= arr@[(i + 1) as int]);\n }\n }\n };\n }\n return false;\n }\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc941/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc941/tests/mutated_testcases.jsonl"} {"problem": "lc946", "family": "leetcode", "description": "# Validate Stack Sequences\n\nGiven two integer arrays `pushed` and `popped` each with distinct values, return `true` if this could have been the result of a sequence of push and pop operations on an initially empty stack, or `false` otherwise.\n\n## Example 1:\n\n> **Input:** pushed = [1,2,3,4,5], popped = [4,5,3,2,1]\n> **Output:** true\n> **Explanation:** We might do the following sequence: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1.\n\n## Example 2:\n\n> **Input:** pushed = [1,2,3,4,5], popped = [4,3,5,1,2]\n> **Output:** false\n> **Explanation:** 1 cannot be popped before 2.\n\n## Constraints:\n\n- $1 <= pushed.length <= 1000$\n- $0 <= pushed[i] <= 1000$\n- All the elements of `pushed` are **unique**.\n- $popped.length == pushed.length$\n- `popped` is a permutation of `pushed`.\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> bool {\n \n }\n}\n```\n", "tags": "Medium\nArray, Stack, Simulation\nAcceptance rate: 70.2%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn value_in(s: Seq, x: i32) -> bool {\n exists |i: int| 0 <= i < s.len() && s[i] == x\n }\n\n pub open spec fn reduce_stack(stack: Seq, popped: Seq, j: int) -> Seq\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::reduce_stack(stack.subrange(0, stack.len() as int - 1), popped, j + 1)\n } else {\n stack\n }\n }\n\n pub open spec fn reduce_j(stack: Seq, popped: Seq, j: int) -> int\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::reduce_j(stack.subrange(0, stack.len() as int - 1), popped, j + 1)\n } else {\n j\n }\n }\n\n pub open spec fn state_stack(pushed: Seq, popped: Seq, i: int) -> Seq\n decreases i,\n {\n if i <= 0 {\n seq![]\n } else {\n let prev_stack = Self::state_stack(pushed, popped, i - 1);\n let prev_j = Self::state_j(pushed, popped, i - 1);\n Self::reduce_stack(prev_stack.push(pushed[i - 1]), popped, prev_j)\n }\n }\n\n pub open spec fn state_j(pushed: Seq, popped: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n 0\n } else {\n let prev_stack = Self::state_stack(pushed, popped, i - 1);\n let prev_j = Self::state_j(pushed, popped, i - 1);\n Self::reduce_j(prev_stack.push(pushed[i - 1]), popped, prev_j)\n }\n }\n\n pub open spec fn valid_stack_sequences(pushed: Seq, popped: Seq) -> bool {\n let n = pushed.len() as int;\n Self::state_j(pushed, popped, n) == popped.len() && Self::state_stack(pushed, popped, n).len() == 0\n }\n\n pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> (result: bool)\n requires\n 1 <= pushed.len() <= 1000,\n pushed.len() == popped.len(),\n forall |i: int| 0 <= i < pushed.len() ==> 0 <= #[trigger] pushed[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < pushed.len() ==> pushed[i] != pushed[j],\n forall |i: int| 0 <= i < popped.len() ==> 0 <= #[trigger] popped[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < popped.len() ==> popped[i] != popped[j],\n forall |x: i32|\n #[trigger] Self::value_in(pushed@, x) <==> Self::value_in(popped@, x),\n ensures\n result == Self::valid_stack_sequences(pushed@, popped@),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n fn reduce_stack_exec(stack: &mut Vec, popped: &Vec, j: usize) -> usize {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n stack.pop();\n Self::reduce_stack_exec(stack, popped, j + 1)\n } else {\n j\n }\n }\n\n pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> bool {\n let n = pushed.len();\n let mut stack: Vec = Vec::new();\n let mut i = 0usize;\n let mut j = 0usize;\n while i < n {\n stack.push(pushed[i]);\n i = i + 1;\n j = Self::reduce_stack_exec(&mut stack, &popped, j);\n }\n stack.len() == 0\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn value_in(s: Seq, x: i32) -> bool {\n exists |i: int| 0 <= i < s.len() && s[i] == x\n }\n\n pub open spec fn reduce_stack(stack: Seq, popped: Seq, j: int) -> Seq\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::reduce_stack(stack.subrange(0, stack.len() as int - 1), popped, j + 1)\n } else {\n stack\n }\n }\n\n pub open spec fn reduce_j(stack: Seq, popped: Seq, j: int) -> int\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::reduce_j(stack.subrange(0, stack.len() as int - 1), popped, j + 1)\n } else {\n j\n }\n }\n\n pub open spec fn state_stack(pushed: Seq, popped: Seq, i: int) -> Seq\n decreases i,\n {\n if i <= 0 {\n seq![]\n } else {\n let prev_stack = Self::state_stack(pushed, popped, i - 1);\n let prev_j = Self::state_j(pushed, popped, i - 1);\n Self::reduce_stack(prev_stack.push(pushed[i - 1]), popped, prev_j)\n }\n }\n\n pub open spec fn state_j(pushed: Seq, popped: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n 0\n } else {\n let prev_stack = Self::state_stack(pushed, popped, i - 1);\n let prev_j = Self::state_j(pushed, popped, i - 1);\n Self::reduce_j(prev_stack.push(pushed[i - 1]), popped, prev_j)\n }\n }\n\n pub open spec fn valid_stack_sequences(pushed: Seq, popped: Seq) -> bool {\n let n = pushed.len() as int;\n Self::state_j(pushed, popped, n) == popped.len() && Self::state_stack(pushed, popped, n).len() == 0\n }\n\n fn reduce_stack_exec(stack: &mut Vec, popped: &Vec, j: usize) -> (result: usize)\n requires\n j <= popped.len(),\n ensures\n result as int == Self::reduce_j(old(stack)@, popped@, j as int),\n stack@ == Self::reduce_stack(old(stack)@, popped@, j as int),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n stack.pop();\n Self::reduce_stack_exec(stack, popped, j + 1)\n } else {\n j\n }\n }\n\n pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> (result: bool)\n requires\n 1 <= pushed.len() <= 1000,\n pushed.len() == popped.len(),\n forall |i: int| 0 <= i < pushed.len() ==> 0 <= #[trigger] pushed[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < pushed.len() ==> pushed[i] != pushed[j],\n forall |i: int| 0 <= i < popped.len() ==> 0 <= #[trigger] popped[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < popped.len() ==> popped[i] != popped[j],\n forall |x: i32|\n #[trigger] Self::value_in(pushed@, x) <==> Self::value_in(popped@, x),\n ensures\n result == Self::valid_stack_sequences(pushed@, popped@),\n {\n let n = pushed.len();\n let mut stack: Vec = Vec::new();\n let mut i = 0usize;\n let mut j = 0usize;\n while i < n {\n stack.push(pushed[i]);\n i = i + 1;\n j = Self::reduce_stack_exec(&mut stack, &popped, j);\n }\n stack.len() == 0\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn value_in(s: Seq, x: i32) -> bool {\n exists |i: int| 0 <= i < s.len() && s[i] == x\n }\n\n pub open spec fn reduce_stack(stack: Seq, popped: Seq, j: int) -> Seq\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::reduce_stack(stack.subrange(0, stack.len() as int - 1), popped, j + 1)\n } else {\n stack\n }\n }\n\n pub open spec fn reduce_j(stack: Seq, popped: Seq, j: int) -> int\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::reduce_j(stack.subrange(0, stack.len() as int - 1), popped, j + 1)\n } else {\n j\n }\n }\n\n pub open spec fn state_stack(pushed: Seq, popped: Seq, i: int) -> Seq\n decreases i,\n {\n if i <= 0 {\n seq![]\n } else {\n let prev_stack = Self::state_stack(pushed, popped, i - 1);\n let prev_j = Self::state_j(pushed, popped, i - 1);\n Self::reduce_stack(prev_stack.push(pushed[i - 1]), popped, prev_j)\n }\n }\n\n pub open spec fn state_j(pushed: Seq, popped: Seq, i: int) -> int\n decreases i,\n {\n if i <= 0 {\n 0\n } else {\n let prev_stack = Self::state_stack(pushed, popped, i - 1);\n let prev_j = Self::state_j(pushed, popped, i - 1);\n Self::reduce_j(prev_stack.push(pushed[i - 1]), popped, prev_j)\n }\n }\n\n pub open spec fn valid_stack_sequences(pushed: Seq, popped: Seq) -> bool {\n let n = pushed.len() as int;\n Self::state_j(pushed, popped, n) == popped.len() && Self::state_stack(pushed, popped, n).len() == 0\n }\n\n proof fn lemma_reduce_balance(stack: Seq, popped: Seq, j: int)\n requires\n 0 <= j <= popped.len(),\n ensures\n Self::reduce_stack(stack, popped, j).len() + Self::reduce_j(stack, popped, j) == stack.len() + j,\n decreases stack.len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n Self::lemma_reduce_balance(stack.subrange(0, stack.len() as int - 1), popped, j + 1);\n }\n }\n\n fn reduce_stack_exec(stack: &mut Vec, popped: &Vec, j: usize) -> (result: usize)\n requires\n j <= popped.len(),\n ensures\n result as int == Self::reduce_j(old(stack)@, popped@, j as int),\n stack@ == Self::reduce_stack(old(stack)@, popped@, j as int),\n decreases old(stack).len(),\n {\n if stack.len() > 0 && j < popped.len() && stack[stack.len() - 1] == popped[j] {\n stack.pop();\n Self::reduce_stack_exec(stack, popped, j + 1)\n } else {\n j\n }\n }\n\n pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> (result: bool)\n requires\n 1 <= pushed.len() <= 1000,\n pushed.len() == popped.len(),\n forall |i: int| 0 <= i < pushed.len() ==> 0 <= #[trigger] pushed[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < pushed.len() ==> pushed[i] != pushed[j],\n forall |i: int| 0 <= i < popped.len() ==> 0 <= #[trigger] popped[i] <= 1000,\n forall |i: int, j: int| 0 <= i < j < popped.len() ==> popped[i] != popped[j],\n forall |x: i32|\n #[trigger] Self::value_in(pushed@, x) <==> Self::value_in(popped@, x),\n ensures\n result == Self::valid_stack_sequences(pushed@, popped@),\n {\n let n = pushed.len();\n let mut stack: Vec = Vec::new();\n let mut i = 0usize;\n let mut j = 0usize;\n while i < n\n invariant\n n == pushed.len(),\n n == popped.len(),\n i <= n,\n j <= n,\n stack@ == Self::state_stack(pushed@, popped@, i as int),\n j as int == Self::state_j(pushed@, popped@, i as int),\n stack@.len() + j as int == i as int,\n decreases n - i\n {\n let ghost old_i = i;\n let ghost old_j = j;\n stack.push(pushed[i]);\n i = i + 1;\n j = Self::reduce_stack_exec(&mut stack, &popped, j);\n proof {\n reveal_with_fuel(Solution::state_stack, 2);\n reveal_with_fuel(Solution::state_j, 2);\n assert(old_i as int + 1 == i as int);\n Self::lemma_reduce_balance(\n Self::state_stack(pushed@, popped@, old_i as int).push(pushed@[old_i as int]),\n popped@,\n old_j as int,\n );\n assert(stack@.len() + j as int\n == Self::state_stack(pushed@, popped@, old_i as int).len() + 1 + old_j as int);\n assert(Self::state_stack(pushed@, popped@, old_i as int).len() + old_j as int == old_i as int);\n assert(stack@.len() + j as int == i as int);\n assert(j as int <= i as int);\n }\n }\n proof {\n assert(i == n);\n assert(stack@ == Self::state_stack(pushed@, popped@, n as int));\n assert(j as int == Self::state_j(pushed@, popped@, n as int));\n if stack.len() == 0 {\n assert(j as int == n as int);\n }\n }\n stack.len() == 0\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc946/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc946/tests/mutated_testcases.jsonl"} {"problem": "lc978", "family": "leetcode", "description": "# Longest Turbulent Subarray\n\nGiven an integer array `arr`, return the length of a maximum-size turbulent subarray of `arr`.\n\nA subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.\n\nMore formally, a subarray `[arr[i], arr[i + 1], ..., arr[j]]` of `arr` is turbulent if and only if one of the following holds for every `i <= k < j`:\n- `arr[k] > arr[k + 1]` when `k` is odd, and `arr[k] < arr[k + 1]` when `k` is even.\n- `arr[k] > arr[k + 1]` when `k` is even, and `arr[k] < arr[k + 1]` when `k` is odd.\n\n## Example 1:\n\n> **Input:** arr = [9,4,2,10,7,8,8,1,9]\n> **Output:** 5\n> **Explanation:** `arr[1] > arr[2] < arr[3] > arr[4] < arr[5]`\n\n## Example 2:\n\n> **Input:** arr = [4,8,12,16]\n> **Output:** 2\n\n## Example 3:\n\n> **Input:** arr = [100]\n> **Output:** 1\n\n## Constraints:\n\n- $1 <= arr.length <= 4 * 10^4$\n- $0 <= arr[i] <= 10^9$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn max_turbulence_size(arr: Vec) -> i32 {\n \n }\n}\n```\n", "tags": "Medium\nStaff, Array, Dynamic Programming, Sliding Window, Weekly Contest 120\nAcceptance rate: 48.9%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn alt_at(arr: Seq, i: int) -> bool {\n 2 <= i < arr.len()\n && ((arr[i - 2] < arr[i - 1] && arr[i - 1] > arr[i])\n || (arr[i - 2] > arr[i - 1] && arr[i - 1] < arr[i]))\n }\n\n pub open spec fn is_turbulent(arr: Seq, start: int, end: int) -> bool {\n 0 <= start <= end < arr.len()\n && (forall |k: int| start < k <= end ==> #[trigger] arr[k - 1] != arr[k])\n && (forall |k: int| start + 2 <= k <= end ==> #[trigger] Self::alt_at(arr, k))\n }\n\n pub open spec fn turbulent_len(arr: Seq, start: int, len: int) -> bool {\n 1 <= len\n && start + len <= arr.len()\n && Self::is_turbulent(arr, start, start + len - 1)\n }\n\n pub open spec fn run_at(arr: Seq, i: int) -> nat\n decreases i\n {\n if i <= 0 {\n 1\n } else if i == 1 {\n if arr[0] != arr[1] { 2 } else { 1 }\n } else if Self::alt_at(arr, i) {\n Self::run_at(arr, i - 1) + 1\n } else if arr[i - 1] != arr[i] {\n 2\n } else {\n 1\n }\n }\n\n pub open spec fn max_run(arr: Seq, i: int) -> nat\n decreases i\n {\n if i <= 0 {\n 1\n } else {\n let prev = Self::max_run(arr, i - 1);\n let cur = Self::run_at(arr, i);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub fn max_turbulence_size(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 40_000,\n forall |i: int| 0 <= i < arr.len() ==> 0 <= #[trigger] arr[i] <= 1_000_000_000,\n ensures\n 1 <= result,\n result as int <= arr.len(),\n exists |start: int|\n 0 <= start && start + result <= arr.len()\n && #[trigger] Self::turbulent_len(arr@, start, result as int),\n forall |start: int, len: int|\n 0 <= start && 1 <= len && start + len <= arr.len() as int && len > result as int\n ==> !#[trigger] Self::turbulent_len(arr@, start, len),\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn max_turbulence_size(arr: Vec) -> i32 {\n let n = arr.len();\n let mut best: usize = 1;\n let mut cur: usize = 1;\n let mut i: usize = 1;\n\n while i < n {\n if i >= 2\n && ((arr[i - 2] < arr[i - 1] && arr[i - 1] > arr[i])\n || (arr[i - 2] > arr[i - 1] && arr[i - 1] < arr[i]))\n {\n cur = cur + 1;\n } else if arr[i - 1] != arr[i] {\n cur = 2;\n } else {\n cur = 1;\n }\n\n if cur > best {\n best = cur;\n }\n\n i = i + 1;\n }\n\n best as i32\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn alt_at(arr: Seq, i: int) -> bool {\n 2 <= i < arr.len()\n && ((arr[i - 2] < arr[i - 1] && arr[i - 1] > arr[i])\n || (arr[i - 2] > arr[i - 1] && arr[i - 1] < arr[i]))\n }\n\n pub open spec fn is_turbulent(arr: Seq, start: int, end: int) -> bool {\n 0 <= start <= end < arr.len()\n && (forall |k: int| start < k <= end ==> #[trigger] arr[k - 1] != arr[k])\n && (forall |k: int| start + 2 <= k <= end ==> #[trigger] Self::alt_at(arr, k))\n }\n\n pub open spec fn turbulent_len(arr: Seq, start: int, len: int) -> bool {\n 1 <= len\n && start + len <= arr.len()\n && Self::is_turbulent(arr, start, start + len - 1)\n }\n\n pub open spec fn run_at(arr: Seq, i: int) -> nat\n decreases i\n {\n if i <= 0 {\n 1\n } else if i == 1 {\n if arr[0] != arr[1] { 2 } else { 1 }\n } else if Self::alt_at(arr, i) {\n Self::run_at(arr, i - 1) + 1\n } else if arr[i - 1] != arr[i] {\n 2\n } else {\n 1\n }\n }\n\n pub open spec fn max_run(arr: Seq, i: int) -> nat\n decreases i\n {\n if i <= 0 {\n 1\n } else {\n let prev = Self::max_run(arr, i - 1);\n let cur = Self::run_at(arr, i);\n if cur > prev { cur } else { prev }\n }\n }\n\n pub fn max_turbulence_size(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 40_000,\n forall |i: int| 0 <= i < arr.len() ==> 0 <= #[trigger] arr[i] <= 1_000_000_000,\n ensures\n 1 <= result,\n result as int <= arr.len(),\n exists |start: int|\n 0 <= start && start + result <= arr.len()\n && #[trigger] Self::turbulent_len(arr@, start, result as int),\n forall |start: int, len: int|\n 0 <= start && 1 <= len && start + len <= arr.len() as int && len > result as int\n ==> !#[trigger] Self::turbulent_len(arr@, start, len),\n {\n let n = arr.len();\n let mut best: usize = 1;\n let mut cur: usize = 1;\n let mut i: usize = 1;\n\n while i < n {\n if i >= 2\n && ((arr[i - 2] < arr[i - 1] && arr[i - 1] > arr[i])\n || (arr[i - 2] > arr[i - 1] && arr[i - 1] < arr[i]))\n {\n cur = cur + 1;\n } else if arr[i - 1] != arr[i] {\n cur = 2;\n } else {\n cur = 1;\n }\n\n if cur > best {\n best = cur;\n }\n\n i = i + 1;\n }\n\n best as i32\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn alt_at(arr: Seq, i: int) -> bool {\n 2 <= i < arr.len()\n && ((arr[i - 2] < arr[i - 1] && arr[i - 1] > arr[i])\n || (arr[i - 2] > arr[i - 1] && arr[i - 1] < arr[i]))\n }\n\n pub open spec fn is_turbulent(arr: Seq, start: int, end: int) -> bool {\n 0 <= start <= end < arr.len()\n && (forall |k: int| start < k <= end ==> #[trigger] arr[k - 1] != arr[k])\n && (forall |k: int| start + 2 <= k <= end ==> #[trigger] Self::alt_at(arr, k))\n }\n\n pub open spec fn turbulent_len(arr: Seq, start: int, len: int) -> bool {\n 1 <= len\n && start + len <= arr.len()\n && Self::is_turbulent(arr, start, start + len - 1)\n }\n\n pub open spec fn run_at(arr: Seq, i: int) -> nat\n decreases i\n {\n if i <= 0 {\n 1\n } else if i == 1 {\n if arr[0] != arr[1] { 2 } else { 1 }\n } else if Self::alt_at(arr, i) {\n Self::run_at(arr, i - 1) + 1\n } else if arr[i - 1] != arr[i] {\n 2\n } else {\n 1\n }\n }\n\n pub open spec fn max_run(arr: Seq, i: int) -> nat\n decreases i\n {\n if i <= 0 {\n 1\n } else {\n let prev = Self::max_run(arr, i - 1);\n let cur = Self::run_at(arr, i);\n if cur > prev { cur } else { prev }\n }\n }\n\n proof fn lemma_singleton_turbulent(arr: Seq, i: int)\n requires\n 0 <= i < arr.len(),\n ensures\n Self::is_turbulent(arr, i, i),\n {\n }\n\n proof fn lemma_pair_turbulent(arr: Seq, i: int)\n requires\n 1 <= i < arr.len(),\n arr[i - 1] != arr[i],\n ensures\n Self::is_turbulent(arr, i - 1, i),\n {\n assert forall |k: int| i - 1 < k <= i implies #[trigger] arr[k - 1] != arr[k] by {\n assert(k == i);\n };\n }\n\n proof fn lemma_run_at_bounds(arr: Seq, i: int)\n requires\n 0 <= i < arr.len(),\n ensures\n 1 <= Self::run_at(arr, i) <= i + 1,\n decreases i,\n {\n if i > 1 && Self::alt_at(arr, i) {\n Self::lemma_run_at_bounds(arr, i - 1);\n }\n }\n\n proof fn lemma_unequal_implies_run_at_ge_two(arr: Seq, i: int)\n requires\n 1 <= i < arr.len(),\n arr[i - 1] != arr[i],\n ensures\n Self::run_at(arr, i) >= 2,\n decreases i,\n {\n if i > 1 && Self::alt_at(arr, i) {\n Self::lemma_run_at_bounds(arr, i - 1);\n }\n }\n\n proof fn lemma_turbulent_prefix(arr: Seq, start: int, end: int)\n requires\n Self::is_turbulent(arr, start, end),\n start < end,\n ensures\n Self::is_turbulent(arr, start, end - 1),\n {\n assert forall |k: int| start < k <= end - 1 implies #[trigger] arr[k - 1] != arr[k] by {\n assert(arr[k - 1] != arr[k]);\n };\n assert forall |k: int| start + 2 <= k <= end - 1 implies #[trigger] Self::alt_at(arr, k) by {\n assert(Self::alt_at(arr, k));\n };\n }\n\n proof fn lemma_extend_turbulent(arr: Seq, start: int, end: int)\n requires\n Self::is_turbulent(arr, start, end),\n start < end,\n end + 1 < arr.len(),\n Self::alt_at(arr, end + 1),\n ensures\n Self::is_turbulent(arr, start, end + 1),\n {\n assert forall |k: int| start < k <= end + 1 implies #[trigger] arr[k - 1] != arr[k] by {\n if k <= end {\n assert(arr[k - 1] != arr[k]);\n } else {\n assert(Self::alt_at(arr, end + 1));\n assert(arr[end] != arr[end + 1]);\n }\n };\n assert forall |k: int| start + 2 <= k <= end + 1 implies #[trigger] Self::alt_at(arr, k) by {\n if k <= end {\n assert(Self::alt_at(arr, k));\n } else {\n assert(Self::alt_at(arr, end + 1));\n }\n };\n }\n\n proof fn lemma_run_at_turbulent(arr: Seq, i: int)\n requires\n 0 <= i < arr.len(),\n ensures\n Self::is_turbulent(arr, i - Self::run_at(arr, i) as int + 1, i),\n decreases i,\n {\n if i == 0 {\n Self::lemma_singleton_turbulent(arr, 0);\n } else if i == 1 {\n if arr[0] != arr[1] {\n Self::lemma_pair_turbulent(arr, 1);\n } else {\n Self::lemma_singleton_turbulent(arr, 1);\n }\n } else if Self::alt_at(arr, i) {\n Self::lemma_unequal_implies_run_at_ge_two(arr, i - 1);\n Self::lemma_run_at_turbulent(arr, i - 1);\n let start = i - Self::run_at(arr, i) as int + 1;\n let prev_start = (i - 1) - Self::run_at(arr, i - 1) as int + 1;\n assert(Self::run_at(arr, i) == Self::run_at(arr, i - 1) + 1);\n assert(start == prev_start);\n assert(prev_start < i - 1);\n Self::lemma_extend_turbulent(arr, prev_start, i - 1);\n } else if arr[i - 1] != arr[i] {\n assert(i - Self::run_at(arr, i) as int + 1 == i - 1);\n Self::lemma_pair_turbulent(arr, i);\n } else {\n assert(i - Self::run_at(arr, i) as int + 1 == i);\n Self::lemma_singleton_turbulent(arr, i);\n }\n }\n\n proof fn lemma_turbulent_implies_run_at(arr: Seq, start: int, end: int)\n requires\n Self::is_turbulent(arr, start, end),\n ensures\n end - start + 1 <= Self::run_at(arr, end),\n decreases end - start,\n {\n if start == end {\n Self::lemma_run_at_bounds(arr, end);\n } else if start + 1 == end {\n Self::lemma_unequal_implies_run_at_ge_two(arr, end);\n } else {\n Self::lemma_turbulent_prefix(arr, start, end);\n Self::lemma_turbulent_implies_run_at(arr, start, end - 1);\n assert(Self::alt_at(arr, end));\n assert(Self::run_at(arr, end) == Self::run_at(arr, end - 1) + 1);\n }\n }\n\n proof fn lemma_max_run_ge(arr: Seq, i: int, j: int)\n requires\n 0 <= j <= i < arr.len(),\n ensures\n Self::max_run(arr, i) >= Self::run_at(arr, j),\n decreases i,\n {\n if i > j {\n Self::lemma_max_run_ge(arr, i - 1, j);\n }\n }\n\n pub fn max_turbulence_size(arr: Vec) -> (result: i32)\n requires\n 1 <= arr.len() <= 40_000,\n forall |i: int| 0 <= i < arr.len() ==> 0 <= #[trigger] arr[i] <= 1_000_000_000,\n ensures\n 1 <= result,\n result as int <= arr.len(),\n exists |start: int|\n 0 <= start && start + result <= arr.len()\n && #[trigger] Self::turbulent_len(arr@, start, result as int),\n forall |start: int, len: int|\n 0 <= start && 1 <= len && start + len <= arr.len() as int && len > result as int\n ==> !#[trigger] Self::turbulent_len(arr@, start, len),\n {\n let n = arr.len();\n let mut best: usize = 1;\n let mut cur: usize = 1;\n let mut i: usize = 1;\n let ghost mut best_end: int = 0;\n\n while i < n\n invariant\n n == arr.len(),\n 1 <= n <= 40_000,\n 1 <= i <= n,\n forall |k: int| 0 <= k < arr.len() ==> 0 <= #[trigger] arr[k] <= 1_000_000_000,\n cur as nat == Self::run_at(arr@, i as int - 1),\n best as nat == Self::max_run(arr@, i as int - 1),\n 1 <= cur <= i,\n 1 <= best <= i,\n 0 <= best_end < i as int,\n Self::run_at(arr@, best_end) == best as nat,\n decreases n - i,\n {\n if i >= 2\n && ((arr[i - 2] < arr[i - 1] && arr[i - 1] > arr[i])\n || (arr[i - 2] > arr[i - 1] && arr[i - 1] < arr[i]))\n {\n proof {\n Self::lemma_run_at_bounds(arr@, i as int - 1);\n assert(cur < usize::MAX);\n }\n cur = cur + 1;\n } else if arr[i - 1] != arr[i] {\n cur = 2;\n } else {\n cur = 1;\n }\n\n if cur > best {\n best = cur;\n proof {\n best_end = i as int;\n }\n }\n\n proof {\n if i >= 2 && ((arr@[(i - 2) as int] < arr@[(i - 1) as int] && arr@[(i - 1) as int] > arr@[i as int])\n || (arr@[(i - 2) as int] > arr@[(i - 1) as int] && arr@[(i - 1) as int] < arr@[i as int])) {\n assert(Self::alt_at(arr@, i as int));\n assert(Self::run_at(arr@, i as int) == Self::run_at(arr@, i as int - 1) + 1);\n } else if arr@[(i - 1) as int] != arr@[i as int] {\n if i == 1 {\n assert(Self::run_at(arr@, i as int) == 2);\n } else {\n assert(!Self::alt_at(arr@, i as int));\n assert(Self::run_at(arr@, i as int) == 2);\n }\n } else {\n if i == 1 {\n assert(Self::run_at(arr@, i as int) == 1);\n } else {\n assert(!Self::alt_at(arr@, i as int));\n assert(Self::run_at(arr@, i as int) == 1);\n }\n }\n assert(cur as nat == Self::run_at(arr@, i as int));\n assert(Self::max_run(arr@, i as int)\n == (if Self::run_at(arr@, i as int) > Self::max_run(arr@, i as int - 1)\n { Self::run_at(arr@, i as int) }\n else { Self::max_run(arr@, i as int - 1) }));\n assert(best as nat == Self::max_run(arr@, i as int));\n }\n\n i = i + 1;\n }\n\n proof {\n let end = best_end;\n let start = end - best as int + 1;\n\n Self::lemma_run_at_turbulent(arr@, end);\n Self::lemma_run_at_bounds(arr@, end);\n assert(Self::is_turbulent(arr@, start, end));\n assert(Self::turbulent_len(arr@, start, best as int));\n assert(start + best as int <= n as int);\n\n assert forall |s: int, len: int|\n 0 <= s && 1 <= len && s + len <= n as int && len > best as int\n implies !#[trigger] Self::turbulent_len(arr@, s, len) by {\n if Self::turbulent_len(arr@, s, len) {\n let end2 = s + len - 1;\n Self::lemma_turbulent_implies_run_at(arr@, s, end2);\n Self::lemma_max_run_ge(arr@, n as int - 1, end2);\n assert(Self::max_run(arr@, n as int - 1) >= len as nat);\n assert(Self::max_run(arr@, n as int - 1) == best as nat);\n assert(false);\n }\n };\n\n assert(best <= i32::MAX as usize);\n }\n\n best as i32\n }\n}\n\n}\n", "main": null, "checker": null, "testcases_path": "leetcode/lc978/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc978/tests/mutated_testcases.jsonl"} {"problem": "lc986", "family": "leetcode", "description": "# Interval List Intersections\n\nYou are given two lists of closed intervals, `firstList` and `secondList`, where `firstList[i] = [start_i, end_i]` and `secondList[j] = [start_j, end_j]`. Each list of intervals is pairwise **disjoint** and in **sorted order**.\n\nReturn the intersection of these two interval lists.\n\nA **closed interval** $[a, b]$ (with $a <= b$) denotes the set of real numbers $x$ with $a <= x <= b$.\n\nThe **intersection** of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].\n\n## Example 1:\n\n> **Input:** firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]\n> **Output:** [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]\n\n## Example 2:\n\n> **Input:** firstList = [[1,3],[5,9]], secondList = []\n> **Output:** []\n\n## Constraints:\n\n- $0 <= firstList.length, secondList.length <= 1000$\n- $firstList.length + secondList.length >= 1$\n- $0 <= start_i < end_i <= 10^9$\n- $end_i < start_{i+1}$\n- $0 <= start_j < end_j <= 10^9$\n- $end_j < start_{j+1}$\n\n## Starter Code\n\n```rust\nimpl Solution {\n pub fn interval_intersection(first_list: Vec>, second_list: Vec>) -> Vec> {\n \n }\n}\n```\n", "tags": "Medium\nArray, Two Pointers, Sweep Line\nAcceptance rate: 73.0%\n", "spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn interval_rows(list: Seq>) -> bool {\n forall |i: int| 0 <= i < list.len() ==> #[trigger] list[i].len() == 2\n }\n\n pub open spec fn valid_interval(list: Seq>, i: int) -> bool {\n 0 <= list[i][0] < list[i][1] <= 1_000_000_000\n }\n\n pub open spec fn disjoint_at(list: Seq>, i: int) -> bool {\n list[i][1] < list[i + 1][0]\n }\n\n pub open spec fn sorted_disjoint(list: Seq>) -> bool {\n (forall |i: int| 0 <= i < list.len() ==> Self::valid_interval(list, i))\n && (forall |i: int| 0 <= i < list.len() as int - 1 ==>\n #[trigger] Self::disjoint_at(list, i))\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn intersection_list(\n first: Seq>,\n second: Seq>,\n i: int,\n j: int,\n ) -> Seq>\n decreases\n (first.len() - i) + (second.len() - j),\n {\n if i >= first.len() as int || j >= second.len() as int {\n Seq::empty()\n } else {\n let a0 = first[i][0] as int;\n let a1 = first[i][1] as int;\n let b0 = second[j][0] as int;\n let b1 = second[j][1] as int;\n let lo = Self::max_int(a0, b0);\n let hi = Self::min_int(a1, b1);\n let (i2, j2) = if a1 < b1 { (i + 1, j) } else { (i, j + 1) };\n let rest = Self::intersection_list(first, second, i2, j2);\n if lo <= hi {\n let interval = Seq::new(2, |k: int| if k == 0 { lo as i32 } else { hi as i32 });\n let one = Seq::new(1, |k: int| interval);\n one.add(rest)\n } else {\n rest\n }\n }\n }\n\n pub fn interval_intersection(\n first_list: Vec>,\n second_list: Vec>,\n ) -> (result: Vec>)\n requires\n 0 <= first_list.len() <= 1000,\n 0 <= second_list.len() <= 1000,\n first_list.len() + second_list.len() >= 1,\n Self::interval_rows(first_list@),\n Self::interval_rows(second_list@),\n Self::sorted_disjoint(first_list@),\n Self::sorted_disjoint(second_list@),\n ensures\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k].len() == 2 && result@[k][0] <= result@[k][1],\n forall |k: int| 0 <= k < result@.len() as int - 1 ==>\n #[trigger] result@[k][0] <= result@[k + 1][0],\n result@.len() == Self::intersection_list(first_list@, second_list@, 0, 0).len(),\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k][0] == Self::intersection_list(first_list@, second_list@, 0, 0)[k][0]\n && result@[k][1] == Self::intersection_list(first_list@, second_list@, 0, 0)[k][1],\n {\n }\n}\n\n}\n", "code": "impl Solution {\n pub fn interval_intersection(\n first_list: Vec>,\n second_list: Vec>,\n ) -> Vec> {\n let mut result: Vec> = Vec::new();\n let mut i = 0usize;\n let mut j = 0usize;\n let n = first_list.len();\n let m = second_list.len();\n while i < n && j < m {\n let lo = first_list[i][0].max(second_list[j][0]);\n let hi = first_list[i][1].min(second_list[j][1]);\n if lo <= hi {\n result.push(vec![lo, hi]);\n }\n if first_list[i][1] < second_list[j][1] {\n i += 1;\n } else {\n j += 1;\n }\n }\n result\n }\n}\n", "code_spec": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn interval_rows(list: Seq>) -> bool {\n forall |i: int| 0 <= i < list.len() ==> #[trigger] list[i].len() == 2\n }\n\n pub open spec fn valid_interval(list: Seq>, i: int) -> bool {\n 0 <= list[i][0] < list[i][1] <= 1_000_000_000\n }\n\n pub open spec fn disjoint_at(list: Seq>, i: int) -> bool {\n list[i][1] < list[i + 1][0]\n }\n\n pub open spec fn sorted_disjoint(list: Seq>) -> bool {\n (forall |i: int| 0 <= i < list.len() ==> Self::valid_interval(list, i))\n && (forall |i: int| 0 <= i < list.len() as int - 1 ==>\n #[trigger] Self::disjoint_at(list, i))\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn intersection_list(\n first: Seq>,\n second: Seq>,\n i: int,\n j: int,\n ) -> Seq>\n decreases\n (first.len() - i) + (second.len() - j),\n {\n if i >= first.len() as int || j >= second.len() as int {\n Seq::empty()\n } else {\n let a0 = first[i][0] as int;\n let a1 = first[i][1] as int;\n let b0 = second[j][0] as int;\n let b1 = second[j][1] as int;\n let lo = Self::max_int(a0, b0);\n let hi = Self::min_int(a1, b1);\n let (i2, j2) = if a1 < b1 { (i + 1, j) } else { (i, j + 1) };\n let rest = Self::intersection_list(first, second, i2, j2);\n if lo <= hi {\n let interval = Seq::new(2, |k: int| if k == 0 { lo as i32 } else { hi as i32 });\n let one = Seq::new(1, |k: int| interval);\n one.add(rest)\n } else {\n rest\n }\n }\n }\n\n pub fn interval_intersection(\n first_list: Vec>,\n second_list: Vec>,\n ) -> (result: Vec>)\n requires\n 0 <= first_list.len() <= 1000,\n 0 <= second_list.len() <= 1000,\n first_list.len() + second_list.len() >= 1,\n Self::interval_rows(first_list@),\n Self::interval_rows(second_list@),\n Self::sorted_disjoint(first_list@),\n Self::sorted_disjoint(second_list@),\n ensures\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k].len() == 2 && result@[k][0] <= result@[k][1],\n forall |k: int| 0 <= k < result@.len() as int - 1 ==>\n #[trigger] result@[k][0] <= result@[k + 1][0],\n result@.len() == Self::intersection_list(first_list@, second_list@, 0, 0).len(),\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k][0] == Self::intersection_list(first_list@, second_list@, 0, 0)[k][0]\n && result@[k][1] == Self::intersection_list(first_list@, second_list@, 0, 0)[k][1],\n {\n let mut result: Vec> = Vec::new();\n let mut i = 0usize;\n let mut j = 0usize;\n let n = first_list.len();\n let m = second_list.len();\n while i < n && j < m {\n let lo = first_list[i][0].max(second_list[j][0]);\n let hi = first_list[i][1].min(second_list[j][1]);\n if lo <= hi {\n result.push(vec![lo, hi]);\n }\n if first_list[i][1] < second_list[j][1] {\n i += 1;\n } else {\n j += 1;\n }\n }\n result\n }\n}\n\n}\n", "verified": "use vstd::prelude::*;\n\nfn main() {}\n\nverus! {\n\npub struct Solution;\n\nimpl Solution {\n pub open spec fn interval_rows(list: Seq>) -> bool {\n forall |i: int| 0 <= i < list.len() ==> #[trigger] list[i].len() == 2\n }\n\n pub open spec fn valid_interval(list: Seq>, i: int) -> bool {\n 0 <= list[i][0] < list[i][1] <= 1_000_000_000\n }\n\n pub open spec fn disjoint_at(list: Seq>, i: int) -> bool {\n list[i][1] < list[i + 1][0]\n }\n\n pub open spec fn sorted_disjoint(list: Seq>) -> bool {\n (forall |i: int| 0 <= i < list.len() ==> Self::valid_interval(list, i))\n && (forall |i: int| 0 <= i < list.len() as int - 1 ==>\n #[trigger] Self::disjoint_at(list, i))\n }\n\n pub open spec fn max_int(a: int, b: int) -> int {\n if a >= b { a } else { b }\n }\n\n pub open spec fn min_int(a: int, b: int) -> int {\n if a <= b { a } else { b }\n }\n\n pub open spec fn intersection_list(\n first: Seq>,\n second: Seq>,\n i: int,\n j: int,\n ) -> Seq>\n decreases\n (first.len() - i) + (second.len() - j),\n {\n if i >= first.len() as int || j >= second.len() as int {\n Seq::empty()\n } else {\n let a0 = first[i][0] as int;\n let a1 = first[i][1] as int;\n let b0 = second[j][0] as int;\n let b1 = second[j][1] as int;\n let lo = Self::max_int(a0, b0);\n let hi = Self::min_int(a1, b1);\n let (i2, j2) = if a1 < b1 { (i + 1, j) } else { (i, j + 1) };\n let rest = Self::intersection_list(first, second, i2, j2);\n if lo <= hi {\n let interval = Seq::new(2, |k: int| if k == 0 { lo as i32 } else { hi as i32 });\n let one = Seq::new(1, |k: int| interval);\n one.add(rest)\n } else {\n rest\n }\n }\n }\n\n pub proof fn lemma_intersection_list_sorted(\n first: Seq>,\n second: Seq>,\n i: int,\n j: int,\n )\n requires\n 0 <= i <= first.len(),\n 0 <= j <= second.len(),\n Self::sorted_disjoint(first),\n Self::sorted_disjoint(second),\n ensures\n forall |k: int| 0 <= k < Self::intersection_list(first, second, i, j).len() as int - 1 ==>\n #[trigger] Self::intersection_list(first, second, i, j)[k][0]\n <= Self::intersection_list(first, second, i, j)[k + 1][0],\n decreases\n (first.len() - i) + (second.len() - j),\n {\n if i >= first.len() as int || j >= second.len() as int {\n } else {\n let (i2, j2) = (\n if (first[i][1] as int) < (second[j][1] as int) { i + 1 } else { i },\n if (first[i][1] as int) < (second[j][1] as int) { j } else { j + 1 },\n );\n Self::lemma_intersection_list_sorted(first, second, i2, j2);\n reveal_with_fuel(Solution::intersection_list, 1);\n let rest = Self::intersection_list(first, second, i2, j2);\n let a0 = first[i][0] as int;\n let a1 = first[i][1] as int;\n let b0 = second[j][0] as int;\n let b1 = second[j][1] as int;\n let lo = Self::max_int(a0, b0);\n let hi = Self::min_int(a1, b1);\n if rest.len() > 0 {\n Self::lemma_intersection_list_first_element_bounds(first, second, i2, j2);\n let next_start = rest[0][0] as int;\n assert(next_start >= first[i2][0] as int);\n assert(next_start >= second[j2][0] as int);\n if (first[i][1] as int) < (second[j][1] as int) {\n assert(i2 == i + 1);\n assert(j2 == j);\n assert(i as int + 1 < first.len());\n Self::lemma_sorted_disjoint_index(first, i);\n assert(first[i + 1][0] as int > a1);\n if lo <= hi {\n assert(hi <= a1);\n assert(lo <= a1);\n assert(lo <= next_start);\n }\n } else {\n assert(j2 == j + 1);\n assert(i2 == i);\n assert(j as int + 1 < second.len());\n Self::lemma_sorted_disjoint_index(second, j);\n assert(second[j + 1][0] as int > b1);\n if lo <= hi {\n assert(hi <= b1);\n assert(lo <= b1);\n assert(lo <= next_start);\n }\n }\n }\n }\n }\n\n pub proof fn lemma_sorted_disjoint_index(\n first: Seq>,\n i: int,\n )\n requires\n Self::sorted_disjoint(first),\n 0 <= i < first.len() as int - 1,\n ensures\n first[i][1] < first[i + 1][0],\n {\n assert(Self::disjoint_at(first, i));\n }\n\n pub proof fn lemma_intersection_list_first_element_bounds(\n first: Seq>,\n second: Seq>,\n i: int,\n j: int,\n )\n requires\n 0 <= i < first.len(),\n 0 <= j < second.len(),\n Self::sorted_disjoint(first),\n Self::sorted_disjoint(second),\n Self::intersection_list(first, second, i, j).len() > 0,\n ensures\n Self::intersection_list(first, second, i, j)[0][0] as int >= first[i][0] as int,\n Self::intersection_list(first, second, i, j)[0][0] as int >= second[j][0] as int,\n decreases\n (first.len() - i) + (second.len() - j),\n {\n reveal_with_fuel(Solution::intersection_list, 1);\n let a0 = first[i][0] as int;\n let a1 = first[i][1] as int;\n let b0 = second[j][0] as int;\n let b1 = second[j][1] as int;\n let lo = Self::max_int(a0, b0);\n let hi = Self::min_int(a1, b1);\n let (i2, j2) = (\n if (first[i][1] as int) < (second[j][1] as int) { i + 1 } else { i },\n if (first[i][1] as int) < (second[j][1] as int) { j } else { j + 1 },\n );\n let rest = Self::intersection_list(first, second, i2, j2);\n if lo <= hi {\n assert(Self::intersection_list(first, second, i, j)[0][0] == lo as i32);\n assert(lo >= a0);\n assert(lo >= b0);\n } else {\n assert(Self::intersection_list(first, second, i, j) =~= rest);\n assert(rest.len() > 0);\n Self::lemma_intersection_list_first_element_bounds(first, second, i2, j2);\n assert(rest[0][0] as int >= first[i2][0] as int);\n assert(rest[0][0] as int >= second[j2][0] as int);\n if (first[i][1] as int) < (second[j][1] as int) {\n assert(i2 == i + 1);\n assert(i as int + 1 < first.len());\n Self::lemma_sorted_disjoint_index(first, i);\n assert(first[i + 1][0] as int > a1);\n assert(Self::valid_interval(first, i));\n assert(a0 < a1);\n assert(first[i + 1][0] as int >= a0);\n } else {\n assert(j2 == j + 1);\n assert(j as int + 1 < second.len());\n Self::lemma_sorted_disjoint_index(second, j);\n assert(second[j + 1][0] as int > b1);\n assert(Self::valid_interval(second, j));\n assert(b0 < b1);\n assert(second[j + 1][0] as int >= b0);\n }\n }\n }\n\n pub proof fn lemma_intersection_list_step(\n first: Seq>,\n second: Seq>,\n i: int,\n j: int,\n )\n requires\n 0 <= i < first.len(),\n 0 <= j < second.len(),\n Self::max_int(first[i][0] as int, second[j][0] as int)\n <= Self::min_int(first[i][1] as int, second[j][1] as int),\n ensures\n Self::intersection_list(first, second, i, j).len() > 0,\n Self::intersection_list(first, second, i, j)[0][0]\n == Self::max_int(first[i][0] as int, second[j][0] as int) as i32,\n Self::intersection_list(first, second, i, j)[0][1]\n == Self::min_int(first[i][1] as int, second[j][1] as int) as i32,\n Self::intersection_list(first, second, i, j).subrange(\n 1,\n Self::intersection_list(first, second, i, j).len() as int,\n ) =~= Self::intersection_list(\n first,\n second,\n if (first[i][1] as int) < (second[j][1] as int) { i + 1 } else { i },\n if (first[i][1] as int) < (second[j][1] as int) { j } else { j + 1 },\n ),\n {\n reveal_with_fuel(Solution::intersection_list, 2);\n }\n\n pub fn interval_intersection(\n first_list: Vec>,\n second_list: Vec>,\n ) -> (result: Vec>)\n requires\n 0 <= first_list.len() <= 1000,\n 0 <= second_list.len() <= 1000,\n first_list.len() + second_list.len() >= 1,\n Self::interval_rows(first_list@),\n Self::interval_rows(second_list@),\n Self::sorted_disjoint(first_list@),\n Self::sorted_disjoint(second_list@),\n ensures\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k].len() == 2 && result@[k][0] <= result@[k][1],\n forall |k: int| 0 <= k < result@.len() as int - 1 ==>\n #[trigger] result@[k][0] <= result@[k + 1][0],\n result@.len() == Self::intersection_list(first_list@, second_list@, 0, 0).len(),\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k][0] == Self::intersection_list(first_list@, second_list@, 0, 0)[k][0]\n && result@[k][1] == Self::intersection_list(first_list@, second_list@, 0, 0)[k][1],\n {\n let mut result: Vec> = Vec::new();\n let mut i = 0usize;\n let mut j = 0usize;\n let n = first_list.len();\n let m = second_list.len();\n let ghost full = Self::intersection_list(first_list@, second_list@, 0, 0);\n proof {\n Self::lemma_intersection_list_sorted(first_list@, second_list@, 0, 0);\n }\n while i < n && j < m\n invariant\n 0 <= first_list.len() <= 1000,\n 0 <= second_list.len() <= 1000,\n n == first_list.len(),\n m == second_list.len(),\n Self::interval_rows(first_list@),\n Self::interval_rows(second_list@),\n i <= n,\n j <= m,\n result@.len() <= full.len(),\n forall |k: int| 0 <= k < full.len() as int - 1 ==> #[trigger] full[k][0] <= full[k + 1][0],\n result@.len() + Self::intersection_list(first_list@, second_list@, i as int, j as int).len()\n == full.len(),\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k][0] == full[k][0] && result@[k][1] == full[k][1],\n forall |k: int| 0 <= k < Self::intersection_list(first_list@, second_list@, i as int, j as int).len() ==>\n full[result@.len() as int + k][0] == Self::intersection_list(first_list@, second_list@, i as int, j as int)[k][0]\n && full[result@.len() as int + k][1] == Self::intersection_list(first_list@, second_list@, i as int, j as int)[k][1],\n forall |k: int| 0 <= k < result@.len() ==>\n result@[k].len() == 2 && result@[k][0] <= result@[k][1],\n forall |k: int| 0 <= k < result@.len() as int - 1 ==>\n #[trigger] result@[k][0] <= result@[k + 1][0],\n decreases\n (n - i) + (m - j),\n {\n proof {\n assert(i < n);\n assert(j < m);\n assert(Self::interval_rows(first_list@));\n assert(first_list@[i as int].len() == 2);\n assert(second_list@[j as int].len() == 2);\n }\n let ghost rest = Self::intersection_list(first_list@, second_list@, i as int, j as int);\n let lo = first_list[i][0].max(second_list[j][0]);\n let hi = first_list[i][1].min(second_list[j][1]);\n let ghost old_len = result@.len();\n let ghost i_old = i as int;\n let ghost j_old = j as int;\n if lo <= hi {\n result.push(vec![lo, hi]);\n }\n if first_list[i][1] < second_list[j][1] {\n i += 1;\n } else {\n j += 1;\n }\n proof {\n let rest_old = Self::intersection_list(first_list@, second_list@, i_old, j_old);\n let rest_new = Self::intersection_list(first_list@, second_list@, i as int, j as int);\n if lo <= hi {\n Self::lemma_intersection_list_step(first_list@, second_list@, i_old, j_old);\n assert(rest_old.subrange(1, rest_old.len() as int) =~= rest_new);\n assert forall |k: int| 0 <= k < rest_new.len() implies\n full[old_len as int + 1 + k][0] == rest_new[k][0]\n && full[old_len as int + 1 + k][1] == rest_new[k][1] by {\n assert(rest_old[1 + k][0] == rest_new[k][0]);\n assert(rest_old[1 + k][1] == rest_new[k][1]);\n assert(full[old_len as int + 1 + k][0] == rest_old[1 + k][0]);\n assert(full[old_len as int + 1 + k][1] == rest_old[1 + k][1]);\n };\n } else {\n assert(rest_old =~= rest_new);\n }\n assert forall |k: int| 0 <= k < result@.len() as int - 1 implies\n #[trigger] result@[k][0] <= result@[k + 1][0]\n by {\n assert(result@[k][0] == full[k][0]);\n assert(result@[k + 1][0] == full[k + 1][0]);\n };\n }\n }\n result\n }\n}\n\n}", "main": null, "checker": null, "testcases_path": "leetcode/lc986/tests/testcases.jsonl", "mutated_testcases_path": "leetcode/lc986/tests/mutated_testcases.jsonl"}