Datasets:
File size: 235,817 Bytes
635444e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | {"cpid": 1539, "title": "Chip Exchange", "contest": "First Contest", "division": "Bronze", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1539", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie the cow has in her possession $A$ chips of type A and $B$ chips of type B ($0\\le A, B\\le 10^9$). She can perform the following operation as many times as she likes:\n\n> If you have at least $c_B$ chips of type B, exchange $c_B$ chips of type B for $c_A$ chips of type A ($1\\le c_A, c_B\\le 10^9$).\n\nDetermine the minimum non-negative integer $x$ such that the following holds: after receiving $x$ additional random chips, it is guaranteed that Bessie can end up with at least $f_A$ chips of type A ($0\\le f_A\\le 10^9$).\n\n## Input Format\n\nThe first line contains $T$, the number of independent test cases ($1\\le T\\le 10^4$).\n\nThen follow $T$ tests, each consisting of five integers $A, B, c_A, c_B, f_A$.\n\n## Output Format\n\nOutput the answer for each test on a separate line.\n\n**Note:** The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a `long long` in C/C++).\n\n## Examples\n\n**Input 1**\n\n```\n2\n2 3 1 1 6\n2 3 1 1 4\n```\n\n**Output 1**\n\n```\n1\n0\n```\n\n**Input 2**\n\n```\n5\n0 0 2 3 5\n0 1 2 3 5\n1 0 2 3 5\n10 10 2 3 5\n0 0 1 1000000000 1000000000\n```\n\n**Output 2**\n\n```\n9\n8\n7\n0\n1000000000000000000\n```\n\n## Note\n\nFor the first test of the second example, Bessie initially starts with no chips. If she receives any $9$ additional chips, she can perform the operation to end up with at least $5$ chips of type A. For example, if she receives $2$ chips of type A and $7$ chips of type B, she can perform the operation twice to end up with $6\\ge 5$ chips of type A. However, if she only receives $8$ chips of type B, she can only end up with $4 < 5$ chips of type A.\n\nFor the fourth test of the second example, she already has enough chips of type A from the start.\n\n## Scoring\n\n- Input 3: $c_A = c_B = 1$\n- Inputs 4-5: $x\\le 10$ for all cases\n- Inputs 6-7: $c_A = 2$, $c_B = 3$\n- Inputs 8-12: No additional constraints\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 2, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10000, \"T\");\n inf.readEoln();\n for (int tc = 1; tc <= T; tc++) {\n inf.readInt(0, 1000000000, \"A\"); inf.readSpace();\n inf.readInt(0, 1000000000, \"B\"); inf.readSpace();\n inf.readInt(1, 1000000000, \"c_A\"); inf.readSpace();\n inf.readInt(1, 1000000000, \"c_B\"); inf.readSpace();\n inf.readInt(0, 1000000000, \"f_A\");\n inf.readEoln();\n }\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nll solve(ll A, ll B, ll cA, ll cB, ll fA) {\n ll init = B / cB * cA + A;\n if (init >= fA) return 0;\n ll nA0 = fA - 1 - init;\n ll y = cB - 1 - B % cB;\n if (cA >= cB) y += nA0;\n else y += nA0 / cA * cB + nA0 % cA;\n return y + 1;\n}\n\nint main() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n ll A, B, cA, cB, fA;\n scanf(\"%lld %lld %lld %lld %lld\", &A, &B, &cA, &cB, &fA);\n printf(\"%lld\\n\", solve(A, B, cA, cB, fA));\n }\n return 0;\n}\n", "chk_cpp": null, "std_source_submission_id": 70933, "std_origin": "hand C++ translation (earliest root AC #70933 was Python3)"}
{"cpid": 1540, "title": "COW Splits", "contest": "First Contest", "division": "Bronze", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1540", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie is given a positive integer $N$ and a string $S$ of length $3N$ which is generated by concatenating $N$ strings of length $3$, each of which is a cyclic shift of \"COW\". In other words, each string will be \"COW\", \"OWC\", or \"WCO\".\n\nString $X$ is a *square string* if and only if there exists a string $Y$ such that $X = Y + Y$ where $+$ represents string concatenation. For example, \"COWCOW\" and \"CC\" are examples of square strings but \"COWO\" and \"OC\" are not.\n\nIn a single operation, Bessie can remove any *subsequence* $T$ from $S$ where $T$ is a square string. A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string.\n\nYour job is to help Bessie determine whether it is possible to transform $S$ into an empty string. Additionally, if it is possible, then you must provide a way to do so.\n\nBessie is also given a parameter $k$ which is either $0$ or $1$. Let $M$ be the number of operations in your construction.\n\n- If $k = 0$, then $M$ must equal the minimum possible number of operations.\n- If $k = 1$, then $M$ can be up to one plus the minimum possible number of operations.\n\n## Input Format\n\nThe first line contains $T$, the number of independent test cases ($1\\le T\\le 10^4$) and $k$ ($0\\le k\\le 1$).\n\nThe first line of each test case has $N$ ($1\\le N\\le 10^5$).\n\nThe second line of each test case has $S$.\n\nThe sum of $N$ across all test cases will not exceed $10^5$.\n\n## Output Format\n\nFor each test case, output either one or two lines using the following procedure.\n\nIf it is impossible to transform $S$ into an empty string, print $-1$ on a single line.\n\nOtherwise, on the first line print $M$ — the number of operations in your construction. On the second line, print $3N$ space-separated integers. The $i$-th integer $x$ indicates that the $i$-th letter of $S$ was deleted as part of the $x$-th subsequence ($1\\le x\\le M$).\n\n## Examples\n\n**Input 1**\n\n```\n3 1\n3\nCOWOWCWCO\n4\nWCOCOWWCOCOW\n6\nCOWCOWOWCOWCOWCOWC\n```\n\n**Output 1**\n\n```\n-1\n1\n1 1 1 1 1 1 1 1 1 1 1 1\n3\n3 3 2 3 3 2 1 1 1 1 1 1 1 1 1 1 1 1\n```\n\n**Input 2**\n\n```\n3 0\n3\nCOWOWCWCO\n4\nWCOCOWWCOCOW\n6\nCOWCOWOWCOWCOWCOWC\n```\n\n**Output 2**\n\n```\n-1\n1\n1 1 1 1 1 1 1 1 1 1 1 1\n2\n1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2\n```\n\n## Note\n\nFor the last test of the first example, the optimal number of operations is two, so any valid construction with either $M = 2$ or $M = 3$ would be accepted.\n\nFor $M = 3$, here is a possible construction:\n\n- In the first operation, remove the last twelve characters. Now we're left with `COWCOW`.\n- In the second operation, remove the subsequence `WW`. Now we're left with `COCO`.\n- In the last operation, remove all remaining characters.\n\n## Scoring\n\n- Inputs 3-4: $T\\le 10$, $N\\le 6$, $k = 0$\n- Inputs 5-6: $k = 1$\n- Inputs 7-14: $k = 0$\n\n**Problem credits:** Aakash Gokhale", "checker_kind": "custom (chk.cpp, testlib)", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 12, "n_sample_tests": 2, "point_scores": [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12], "val_cpp": "#include \"testlib.h\"\n#include <string>\nstatic bool isCyclicCOW(const std::string& g) {\n return g == \"COW\" || g == \"OWC\" || g == \"WCO\";\n}\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10000, \"T\");\n inf.readSpace();\n inf.readInt(0, 1, \"k\");\n inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 100000, \"N\");\n inf.readEoln();\n sumN += N;\n std::string s = inf.readLine();\n ensuref((int)s.size() == 3 * N,\n \"test %d: |S|=%d, expected 3N=%d\", tc, (int)s.size(), 3 * N);\n for (int i = 0; i < N; i++) {\n std::string g = s.substr(3 * i, 3);\n ensuref(isCyclicCOW(g),\n \"test %d: group %d '%s' is not a cyclic shift of COW\", tc, i + 1, g.c_str());\n }\n }\n ensuref(sumN <= 100000, \"sum N is %lld > 100000\", sumN);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios::sync_with_stdio(0); cin.tie(0);\n int t, x; cin >> t >> x;\n while (t--) {\n int n; cin >> n;\n string s; cin >> s;\n if (n & 1) {\n cout << -1 << endl;\n continue;\n }\n vector<int> ans(n * 3, 1);\n for (int i = 0; i < n / 2; i++) {\n string l = s.substr(i * 3, 3);\n string r = s.substr((i + n / 2) * 3, 3);\n if (l != r) {\n if (l.substr(0, 2) == r.substr(1, 2)) {\n ans[i * 3 + 2] = 2;\n ans[(i + n / 2) * 3] = 2;\n } else if (l.substr(1, 2) == r.substr(0, 2)) {\n ans[i * 3] = 2;\n ans[(i + n / 2) * 3 + 2] = 2;\n } \n }\n }\n cout << *max_element(ans.begin(), ans.end()) << endl;\n for (int i = 0; i < n * 3; i++) {\n cout << ans[i] << \" \\n\"[i == 3 * n - 1];\n }\n }\n}", "chk_cpp": "#include \"testlib.h\"\n#include <vector>\n#include <string>\nusing namespace std;\n\nint main(int argc, char *argv[]) {\n setName(\"USACO 2026 Bronze: COW Splits\");\n registerTestlibCmd(argc, argv);\n\n int T = inf.readInt();\n int K = inf.readInt();\n\n for (int t = 1; t <= T; t++) {\n int N = inf.readInt();\n string S = inf.readToken();\n if ((int)S.size() != 3 * N)\n quitf(_fail, \"judge input malformed at test %d\", t);\n\n // Read reference's M_ref (or -1 if impossible)\n int M_ref = ans.readInt();\n if (M_ref >= 1) {\n // consume the reference's 3N integers\n for (int i = 0; i < 3 * N; i++) ans.readInt();\n }\n\n // Read contestant's first integer: M, or -1\n int M = ouf.readInt();\n\n if (M_ref == -1) {\n if (M != -1)\n quitf(_wa, \"test %d: expected -1 (impossible), got M=%d\", t, M);\n continue;\n }\n\n // M_ref >= 1, so problem is solvable.\n if (M == -1)\n quitf(_wa, \"test %d: contestant claims impossible but reference shows M=%d feasible\",\n t, M_ref);\n\n if (M < 1)\n quitf(_wa, \"test %d: M must be >= 1, got %d\", t, M);\n\n // M bounds. The reference output always uses the exact minimum number\n // of operations, so M_ref == true minimum.\n // - K=0: M must equal the minimum, i.e. M == M_ref (M < M_ref is\n // impossible by definition of minimum).\n // - K=1: M may be up to minimum + 1, i.e. M <= M_ref + 1.\n int M_max = (K == 0) ? M_ref : (M_ref + 1);\n if (M > M_max)\n quitf(_wa, \"test %d: M=%d exceeds allowed max %d (M_ref=%d, K=%d)\",\n t, M, M_max, M_ref, K);\n\n // Read contestant's 3N assignments\n vector<int> a(3 * N);\n for (int i = 0; i < 3 * N; i++) a[i] = ouf.readInt(1, M, \"a_i\");\n\n // For each subsequence index s in [1..M], collect characters in order; verify square.\n for (int s = 1; s <= M; s++) {\n string sub;\n for (int i = 0; i < 3 * N; i++)\n if (a[i] == s) sub += S[i];\n int len = sub.size();\n if (len == 0)\n quitf(_wa, \"test %d: subsequence %d is empty\", t, s);\n if (len & 1)\n quitf(_wa, \"test %d: subsequence %d has odd length %d\", t, s, len);\n int half = len / 2;\n if (sub.substr(0, half) != sub.substr(half))\n quitf(_wa, \"test %d: subsequence %d is not a square string: '%s'\",\n t, s, sub.c_str());\n }\n }\n\n if (!ouf.seekEof())\n quitf(_wa, \"extra tokens in contestant output\");\n quitf(_ok, \"T=%d cases, K=%d\", T, K);\n}\n", "std_source_submission_id": 70934, "std_origin": "verbatim earliest root score=100 submission #70934"}
{"cpid": 1541, "title": "Photoshoot", "contest": "First Contest", "division": "Bronze", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1541", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John is looking at his cows in a magical field and wants to take pictures of subsets of his cows.\n\nThe field can be seen as a $N\\times N$ grid ($1\\le N\\le 500$), with a single stationary cow at each location. Farmer John's camera is capable of taking a picture of any $K\\times K$ square that is part of the field ($1\\le K\\le \\min(N, 25)$).\n\nAt all times, each cow has a beauty value between $0$ and $10^6$. The *attractiveness index* of a picture is the sum of the beauty values of the cows contained in the picture.\n\nThe beauty value for every cow starts out as $0$, so the attractiveness index of any picture in the beginning is $0$.\n\nAt $Q$ times ($1\\le Q\\le 3\\cdot 10^4$), the beauty of a single cow will increase by a positive integer due to eating the magical grass that is planted on Farmer John's field.\n\nFarmer John wants to know the maximum attractiveness index of a picture he can take after each of the $Q$ updates.\n\n## Input Format\n\nThe first line contains integers $N$ and $K$.\n\nThe following line contains an integer $Q$.\n\nEach of the following $Q$ lines contains three integers: $r$, $c$, and $v$, which are the row, column, and new beauty value, respectively ($1\\le r, c\\le N$, $1\\le v\\le 10^6$). It is guaranteed that the new beauty value is greater than the beauty value at that location before.\n\n## Output Format\n\nOutput $Q$ lines, corresponding to the maximum attractiveness index of a picture after each update.\n\n## Examples\n\n**Input 1**\n\n```\n4 2\n3\n2 2 11\n3 4 3\n3 1 100\n```\n\n**Output 1**\n\n```\n11\n11\n111\n```\n\n**Input 2**\n\n```\n3 1\n3\n2 2 3\n2 2 5\n2 2 7\n```\n\n**Output 2**\n\n```\n3\n5\n7\n```\n\n## Note\n\nFor the first example: after the first update, a picture with the maximum attractiveness index is the picture with upper-left corner $(2, 2)$ and lower-right corner $(3, 3)$, which has an attractiveness index of $11 + 0 + 0 + 0 = 11$. The second update does not affect the maximum attractiveness index. After the third update, the picture with the maximum attractiveness index changes to the picture with upper-left corner $(2, 1)$ and lower-right corner $(3, 2)$, which has an attractiveness index of $0 + 11 + 100 + 0 = 111$.\n\nFor the second example, there is only one cow with a positive beauty value, so the maximum attractiveness index will always include that cow.\n\n## Scoring\n\n- Inputs 3-6: $N\\le 50$, $Q\\le 100$\n- Inputs 7-10: $N\\le 50$\n- Inputs 11-18: No additional constraints\n\n**Problem credits:** Brian Law and Cici Liu", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 16, "n_sample_tests": 2, "point_scores": [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(1, 500, \"N\");\n inf.readSpace();\n int K = inf.readInt(1, std::min(N, 25), \"K\");\n inf.readEoln();\n int Q = inf.readInt(1, 30000, \"Q\");\n inf.readEoln();\n for (int i = 1; i <= Q; i++) {\n inf.readInt(1, N, \"r\"); inf.readSpace();\n inf.readInt(1, N, \"c\"); inf.readSpace();\n inf.readInt(1, 1000000, \"v\");\n inf.readEoln();\n }\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <algorithm>\n#include <ios>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid solve() {\n\n int k;\n int n;\n int q;\n\n cin >> n >> k >> q;\n\n int mx_sum = 0;\n vector sums(n, vector<int>(n));\n vector vals(n, vector<int>(n));\n\n for (int i = 0; i < q; ++i) {\n int c;\n int r;\n int v;\n cin >> r >> c >> v;\n --r;\n --c;\n const int diff = v - vals[r][c];\n vals[r][c] = v;\n for (int j = max(r - k + 1, 0); j <= min(r, n - k); ++j) {\n for (int l = max(c - k + 1, 0); l <= min(c, n - k); ++l) {\n sums[j][l] += diff;\n mx_sum = max(mx_sum, sums[j][l]);\n }\n }\n cout << mx_sum << '\\n';\n }\n\n}\n\nint main() {\n\n cin.tie(nullptr);\n\n ios_base::sync_with_stdio(false);\n\n solve();\n\n return 0;\n\n}", "chk_cpp": null, "std_source_submission_id": 70935, "std_origin": "verbatim earliest root score=100 submission #70935"}
{"cpid": 1542, "title": "Lineup Queries", "contest": "First Contest", "division": "Silver", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1542", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nThere is a line of cows, initially (i.e. at time $t = 0$) containing only cow $0$ at position $0$ (here, a cow is at position $k$ if there are $k$ cows in front of it). At time $t$ for $t = 1, 2, 3, \\dots$, the cow at position $0$ moves to position $\\lfloor t/2\\rfloor$, every cow in positions $1 \\dots \\lfloor t/2\\rfloor$ moves forward one position, and cow $t$ joins the line at the end of the line (position $t$).\n\nAnswer $Q$ ($1\\le Q\\le 10^5$) independent queries, each of one of the following types:\n\n1. At what position is cow $c$ immediately after time $t$ ($0\\le c\\le t\\le 10^{18}$)?\n2. Which cow is at position $x$ immediately after time $t$ ($0\\le x\\le t\\le 10^{18}$)?\n\n## Input Format\n\nThe first line contains $Q$, the number of queries.\n\nThe next $Q$ lines each contain three integers specifying a query either of the form \"$1\\ c\\ t$\" or \"$2\\ x\\ t$.\"\n\n## Output Format\n\nOutput the answer to each query on a separate line.\n\n## Examples\n\n**Input 1**\n\n```\n2\n1 4 9\n2 2 9\n```\n\n**Output 1**\n\n```\n2\n4\n```\n\n**Input 2**\n\n```\n22\n1 0 9\n1 1 9\n1 2 9\n1 3 9\n1 4 9\n1 5 9\n1 6 9\n1 7 9\n1 8 9\n1 9 9\n2 0 9\n2 1 9\n2 2 9\n2 3 9\n2 4 9\n2 5 9\n2 6 9\n2 7 9\n2 8 9\n2 9 9\n1 0 1000000000000000000\n2 0 1000000000000000000\n```\n\n**Output 2**\n\n```\n1\n3\n0\n4\n2\n5\n6\n7\n8\n9\n2\n0\n4\n1\n3\n5\n6\n7\n8\n9\n483992463350322770\n148148148148148148\n```\n\n## Note\n\nLineups immediately after various times:\n\n```\nt = 0 | 0\nt = 1 | 0 1\nt = 2 | 1 0 2\nt = 3 | 0 1 2 3\nt = 4 | 1 2 0 3 4\nt = 5 | 2 0 1 3 4 5\nt = 6 | 0 1 3 2 4 5 6\nt = 7 | 1 3 2 0 4 5 6 7\nt = 8 | 3 2 0 4 1 5 6 7 8\nt = 9 | 2 0 4 1 3 5 6 7 8 9\n```\n\nImmediately after $t = 9$, the location of cow $4$ is $2$, and the cow located at position $2$ is $4$.\n\n## Scoring\n\n- Input 3: $Q\\le 1000$, $t\\le 100$\n- Input 4: $t\\le 5000$\n- Inputs 5-8: All queries are of type 1\n- Inputs 9-12: All queries are of type 2\n\n**Problem credits:** Agastya Goel", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 2, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\nconst long long MAXT = 1000000000000000000LL;\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int Q = inf.readInt(1, 100000, \"Q\");\n inf.readEoln();\n for (int q = 1; q <= Q; q++) {\n int type = inf.readInt(1, 2, \"type\");\n inf.readSpace();\n long long a = inf.readLong(0, MAXT, \"arg\"); // c or x\n inf.readSpace();\n long long t = inf.readLong(0, MAXT, \"t\");\n inf.readEoln();\n ensuref(a <= t, \"query %d: need arg <= t (%lld > %lld)\", q, a, t);\n }\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\n// which cow is at position, given time (iterative to avoid deep recursion)\nll get_pos(ll i, ll t) {\n if (t < 2 * i) return i;\n ll cur_t = 2 * i - 1;\n t -= cur_t;\n ll pos = i;\n while (t >= 0) {\n if (t <= pos) return pos - t;\n cur_t += pos;\n t -= pos;\n pos = 0;\n cur_t += 1;\n t -= 1;\n pos = cur_t / 2;\n }\n return pos;\n}\n\n// position of cow p at time t (iterative form of the recursion)\nll at_pos(ll p, ll t) {\n while (true) {\n if (t == 0) return p;\n if (p < t / 2) {\n ll step = max((ll)1, (t - 2 * p) / 3);\n p += step;\n t -= step;\n } else if (p == t / 2) {\n p = 0;\n t = t - 1;\n } else {\n return p;\n }\n }\n}\n\nint main() {\n int q;\n scanf(\"%d\", &q);\n while (q--) {\n ll a, b, c;\n scanf(\"%lld %lld %lld\", &a, &b, &c);\n if (a == 1) printf(\"%lld\\n\", get_pos(b, c));\n else printf(\"%lld\\n\", at_pos(b, c));\n }\n return 0;\n}\n", "chk_cpp": null, "std_source_submission_id": 70930, "std_origin": "hand C++ translation (earliest root AC #70930 was Python3)"}
{"cpid": 1543, "title": "Mooclear Reactor", "contest": "First Contest", "division": "Silver", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1543", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie is designing a nuclear reactor to power Farmer John's lucrative new AI data center business, CowWeave!\n\nThe reactor core consists of $N$ ($1\\le N\\le 2\\cdot 10^5$) fuel rods, numbered $1$ through $N$. The $i$-th rod has a \"stable operating range\" $[l_i, r_i]$ ($-10^9\\le l_i\\le r_i\\le 10^9$), meaning it can only generate power if its energy $a_i$ (chosen by Bessie) satisfies $l_i\\le a_i\\le r_i$; otherwise, it sits idle and does not generate power. Moreover, $a_i$ must always be an integer. Note that $a_i$ can be any integer, not limited to $[-10^9, 10^9]$.\n\nHowever, quantum interactions between the rods mean that there are $M$ constraints of the form $(x, y, z)$ where Bessie must satisfy $a_x + a_y = z$ ($1\\le x, y\\le N$ and $-10^9\\le z\\le 10^9$) to prevent the reactor from melting down.\n\nHelp Bessie find the maximum number of power-generating rods she can achieve in her design without it melting down!\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 10$), the number of independent tests. Each test is specified in the following format:\n\n- The first line contains the two integers $N$ and $M$.\n- The second line contains the $N$ integers $l_1, \\dots, l_N$.\n- The third line contains the $N$ integers $r_1, \\dots, r_N$.\n- The next $M$ lines each contain three integers $x$, $y$, and $z$, each representing a constraint.\n\nIt is guaranteed that neither the sum of $N$ nor the sum of $M$ over all tests exceeds $4\\cdot 10^5$.\n\n## Output Format\n\nIf no choice of rod energies exists that satisfies all constraints, output $-1$. Otherwise, output the maximum number of power-generating rods Bessie can achieve.\n\n## Examples\n\n**Input 1**\n\n```\n2\n3 3\n1 2 3\n1 2 3\n1 1 2\n2 2 10\n1 1 4\n3 2\n1 2 3\n1 2 3\n1 1 2\n2 2 10\n```\n\n**Output 1**\n\n```\n-1\n2\n```\n\n**Input 2**\n\n```\n1\n3 2\n10 -10 10\n10 -10 10\n1 2 0\n2 3 0\n```\n\n**Output 2**\n\n```\n3\n```\n\n**Input 3**\n\n```\n5\n3 3\n1 -1 0\n2 1 2\n1 2 1\n1 3 4\n2 3 3\n1 1\n-100\n100\n1 1 3\n1 1\n-100\n100\n1 1 2\n1 2\n-100\n100\n1 1 2\n1 1 4\n1 2\n-100\n100\n1 1 2\n1 1 2\n```\n\n**Output 3**\n\n```\n2\n-1\n1\n-1\n1\n```\n\n## Note\n\nIn the second test of the first example, the constraints require that:\n\n- $a_1 + a_1 = 2$\n- $a_2 + a_2 = 10$\n\nChoosing energies $a = [1, 5, 3]$ results in $2$ power-generating rods because:\n\n- $l_1 = 1\\le a_1\\le 1 = r_1$\n- $l_3 = 3\\le a_3\\le 3 = r_3$\n\nand $a$ satisfies all required constraints.\n\nIn the second example, choosing rod energies $a = [10, -10, 10]$ results in $3$ power-generating rods.\n\n## Scoring\n\n- Input 4: $x = y$ for all constraints\n- Inputs 5-7: $|x - y| = 1$ for all constraints\n- Inputs 8-10: $|x - y|\\le 1$ for all constraints\n- Inputs 11-13: No additional conditions\n\n**Problem credits:** Akshaj Arora", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 3, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\n#include <vector>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 400000, \"T\");\n inf.readEoln();\n long long sumN = 0, sumM = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 400000, \"N\");\n inf.readSpace();\n int M = inf.readInt(0, 400000, \"M\");\n inf.readEoln();\n sumN += N; sumM += M;\n std::vector<int> l(N + 1);\n for (int i = 1; i <= N; i++) {\n l[i] = inf.readInt(-1000000000, 1000000000, \"l_i\");\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n for (int i = 1; i <= N; i++) {\n int r = inf.readInt(-1000000000, 1000000000, \"r_i\");\n ensuref(r >= l[i], \"test %d rod %d: need l<=r (%d > %d)\", tc, i, l[i], r);\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n for (int j = 1; j <= M; j++) {\n inf.readInt(1, N, \"x\");\n inf.readSpace();\n inf.readInt(1, N, \"y\");\n inf.readSpace();\n inf.readInt(-1000000000, 1000000000, \"z\");\n inf.readEoln();\n }\n }\n ensuref(sumN <= 400000, \"sum N is %lld > 400000\", sumN);\n ensuref(sumM <= 400000, \"sum M is %lld > 400000\", sumM);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n \ntypedef long long ll;\n \nvoid solve() {\n int n, m;\n cin >> n >> m;\n \n vector<int> l(n), r(n);\n for (int &i : l) cin >> i;\n for (int &i : r) cin >> i;\n \n vector<vector<pair<int, int>>> adj(n);\n while (m--) {\n int i, j, x;\n cin >> i >> j >> x, i--, j--;\n adj[i].push_back({j, x});\n adj[j].push_back({i, x});\n }\n \n bool imp = false;\n auto no = [&]() { imp = true; };\n \n vector<bool> done(n);\n vector<pair<int, ll>> set_v(n);\n int ans = 0;\n \n for (int i = 0; i < n; i++)\n if (!done[i]) {\n bool has_set = false;\n ll set_x;\n vector<pair<ll, int>> mod;\n \n auto dfs = [&](auto self, int i, pair<int, ll> v) -> void {\n if (done[i]) {\n if (set_v[i].first == v.first) {\n if (set_v[i].second != v.second) no();\n } else {\n ll x = set_v[i].first * set_v[i].second +\n v.first * v.second;\n x *= -1;\n if (x & 1) no();\n else {\n x /= 2;\n if (!has_set) has_set = true, set_x = x;\n else if (x != set_x) no();\n }\n }\n } else {\n if (v.first == 1) {\n mod.push_back({l[i] - v.second, 1});\n mod.push_back({1 + r[i] - v.second, -1});\n } else {\n mod.push_back({v.second - r[i], 1});\n mod.push_back({1 + v.second - l[i], -1});\n }\n done[i] = true;\n set_v[i] = v;\n for (auto [j, x] : adj[i])\n self(self, j, {-v.first, x - v.second});\n }\n };\n dfs(dfs, i, {1, 0});\n \n if (has_set) {\n for (auto [x, y] : mod)\n if (x <= set_x) ans += y;\n } else {\n sort(mod.begin(), mod.end());\n int cur = 0, opt = 0;\n for (auto [x, y] : mod) opt = max(opt, cur += y);\n ans += opt;\n }\n }\n if (imp) {\n cout << \"-1\\n\";\n } else {\n cout << ans << '\\n';\n }\n}\n \nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int T;\n cin >> T;\n while (T--) solve();\n}\n\n", "chk_cpp": null, "std_source_submission_id": 70931, "std_origin": "verbatim earliest root score=100 submission #70931"}
{"cpid": 1544, "title": "Sliding Window Summation", "contest": "First Contest", "division": "Silver", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1544", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie has a hidden binary string $b_1 b_2\\dots b_N$ ($1\\le N\\le 2\\cdot 10^5$).\n\nThe only information about $b$ you are given is a binary string $r_1 r_2\\dots r_{N-K+1}$ ($1\\le K\\le N$), where $r_i$ is the remainder when the number of ones in the length-$K$ window of $b$ with leftmost index $i$ is divided by two.\n\nOutput the minimum and maximum possible numbers of ones in Bessie's hidden binary string.\n\n## Input Format\n\nThere are $T$ ($1\\le T\\le 10^3$) independent test cases to be solved. Each test is specified by the following:\n\nThe first line contains $N$ and $K$.\n\nThe second line contains the binary string $r_1\\dots r_{N-K+1}$, where $r_i = \\sum_{j=i}^{i+K-1} b_j \\pmod{2}$.\n\nIt is guaranteed that the sum of $N$ over all tests does not exceed $10^6$.\n\n## Output Format\n\nFor each test case, output the minimum and maximum possible numbers of ones in Bessie's hidden binary string, separated by a single space.\n\n## Examples\n\n**Input 1**\n\n```\n7\n5 1\n10011\n5 2\n1001\n5 3\n100\n5 5\n0\n5 5\n1\n4 4\n1\n5 2\n0000\n```\n\n**Output 1**\n\n```\n3 3\n2 3\n1 4\n0 4\n1 5\n1 3\n0 5\n```\n\n## Note\n\nFor the first test case, $K = 1$ means that $r = b$, and the number of ones in $r$ is $3$.\n\nFor the second test case, there are two possibilities for $b$: $10001$ and $01110$, having $2$ and $3$ ones, respectively.\n\n## Scoring\n\n- Input 2: $N\\le 8$\n- Inputs 3-4: $K\\le 8$ and the sum of $N$ over all tests does not exceed $10^4$\n- Inputs 5-11: No additional constraints\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 1, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\n#include <string>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 1000, \"T\");\n inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 1000000, \"N\");\n inf.readSpace();\n int K = inf.readInt(1, N, \"K\");\n inf.readEoln();\n sumN += N;\n std::string s = inf.readLine();\n int expect = N - K + 1;\n ensuref((int)s.size() == expect,\n \"test %d: r has length %d, expected N-K+1=%d\", tc, (int)s.size(), expect);\n for (char c : s)\n ensuref(c == '0' || c == '1',\n \"test %d: r contains non-binary char '%c'\", tc, c);\n }\n ensuref(sumN <= 1000000, \"sum N is %lld > 1000000\", sumN);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n int N, K;\n scanf(\"%d %d\", &N, &K);\n static char buf[1000006];\n scanf(\"%s\", buf);\n string r(buf);\n\n ll min_sum = 0;\n int parity = 0;\n ll min_dif = N;\n for (int i = 0; i < K; i++) {\n // candidate sequence for b[i, i+K, i+2K, ...]\n ll cnt = 1;\n ll cur = 0; // cand[-1]\n ll cand_sum0 = 0; // sum of cand values\n // cand starts [0]\n cand_sum0 += cur;\n for (int j = i + K; j < N; j += K) {\n cur = cur ^ (r[j - K] - '0') ^ (r[j - K + 1] - '0');\n cand_sum0 += cur;\n cnt++;\n }\n ll cand_sum1 = cnt - cand_sum0;\n min_sum += min(cand_sum0, cand_sum1);\n if (cand_sum1 < cand_sum0) parity ^= 1;\n min_dif = min(min_dif, llabs(cand_sum0 - cand_sum1));\n }\n\n int r0 = r[0] - '0';\n ll get_min = min_sum + (ll)(parity != r0) * min_dif;\n ll get_max = N - min_sum - (ll)(((parity + K) & 1) != r0) * min_dif;\n printf(\"%lld %lld\\n\", get_min, get_max);\n }\n return 0;\n}\n", "chk_cpp": null, "std_source_submission_id": 70932, "std_origin": "hand C++ translation (earliest root AC #70932 was Python3)"}
{"cpid": 1545, "title": "COW Traversals", "contest": "First Contest", "division": "Gold", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1545", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nThere are $N$ ($1\\le N\\le 2\\cdot 10^5$) cows labeled $1\\dots N$ on Farmer John's farm, where each cow lives in its own barn. Each cow $i$ has a best friend $a_i$ ($1\\le a_i\\le N$). Cows can be best friends with themselves, and multiple cows can have the same best friend. The cows love to party, so they have decided to throw a party for $M$ ($1\\le M\\le 2\\cdot 10^5$) consecutive nights.\n\nOn night $i$, cow $c_i$ will decide to throw a party of type $t_i$ at its barn, where $t_i\\in \\texttt{\"COW\"}$. This party will exist for all future nights as well, until cow $c_i$ decides to throw a party of a different type.\n\nEvery night, each cow will attempt to go to a party. If a cow is not hosting a party, they will check their best friend's barn, and if there is no party there, will follow their best friend to wherever they are going (who might also follow their best friend and so on). It is possible that a cow might never find a party and will then give up for the night.\n\nCompute for each night, the number of cows that end up at a party of type $C$, $O$, and $W$ respectively.\n\n## Input Format\n\nThe first line contains $N$, the number of cows.\n\nThe second line contains $a_1,\\dots, a_N$, where $a_i$ is cow $i$'s best friend.\n\nThe third line contains $M$, the number of nights.\n\nThe next $M$ lines each contain an integer $c_i$ ($1\\le c_i\\le N$) and a character $v_i$, representing the cow that is throwing the party and the party type respectively.\n\n## Output Format\n\nOutput $M$ lines, where the $i$-th consists of $3$ space-separated integers, the number of cows going to parties of type $C$, $O$, and $W$ on the $i$-th night, respectively.\n\n## Examples\n\n**Input 1**\n\n```\n5\n2 3 4 5 4\n4\n2 C\n4 C\n4 W\n2 O\n```\n\n**Output 1**\n\n```\n2 0 0\n5 0 0\n2 0 3\n0 2 3\n```\n\n## Note\n\nOn night $1$, there is only one party of type $C$ at barn $2$, which only cows $1$ and $2$ attend.\n\nOn night $2$, there is a new party of type $C$ at barn $4$, which cows $3$, $4$, and $5$ can now reach.\n\nOn night $3$, the party at barn $4$ is changed to type $W$, affecting cows $3$, $4$ and $5$.\n\nOn night $4$, the party at barn $2$ is changed to type $O$, affecting cows $1$ and $2$.\n\n## Scoring\n\n- Input 2: $N, M\\le 100$\n- Inputs 3-4: $N, M\\le 4000$\n- Inputs 5-9: $\\{a_i\\}$ is a permutation of $\\{1,\\dots, N\\}$\n- Inputs 10-21: No additional constraints\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 1, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\n\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n\n int N = inf.readInt(1, 200000, \"N\");\n inf.readEoln();\n\n for (int i = 1; i <= N; i++) {\n inf.readInt(1, N, \"a_i\");\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n\n int M = inf.readInt(1, 200000, \"M\");\n inf.readEoln();\n\n for (int i = 1; i <= M; i++) {\n inf.readInt(1, N, \"c_i\");\n inf.readSpace();\n char v = inf.readChar();\n ensuref(v == 'C' || v == 'O' || v == 'W',\n \"night %d: party type must be C/O/W, got '%c'\", i, v);\n inf.readEoln();\n }\n\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> rt; // roots of each tree\nstruct dsu : vector<int> {\n dsu(int n) : vector(n, -1) {}\n int rep(int x) { return at(x) < 0 ? x : at(x) = rep(at(x)); }\n int sz(int x) { return -at(rep(x)); }\n bool same(int x, int y) { return rep(x) == rep(y); }\n bool join(int x, int y) {\n x = rep(x), y = rep(y);\n if (x == y) return 0;\n int r = rt[y];\n if (at(x) > at(y)) ::swap(x, y);\n at(x) += at(y);\n at(y) = x;\n rt[x] = r;\n return 1;\n }\n};\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n; cin >> n;\n rt.resize(n + 1);\n vector<int> nx(n + 1);\n for (int i = 1; i <= n; i++) {\n cin >> nx[i];\n rt[i] = i;\n }\n int q; cin >> q;\n vector<int> qs(q);\n // ts[i] stores the operations applied at node i, from earliest to latest\n vector<vector<int>> ts(n + 1);\n char c;\n for (int &idx : qs) {\n cin >> idx >> c;\n int t;\n if (c == 'C') t = 0;\n else if (c == 'O') t = 1;\n else t = 2;\n ts[idx].push_back(t);\n }\n reverse(qs.begin(), qs.end()); // process queries backward\n dsu uf(n + 1); // our union-find data structure\n\n // join all non-parties to their parents\n for (int i = 1; i <= n; i++) if (!ts[i].size()) uf.join(i, nx[i]);\n array<int, 3> ans = {0, 0, 0}; // ans[i] = # of cows attending a party of type i\n for (int i = 1; i <= n; i++) if (ts[i].size()) {\n ans[ts[i].back()] += uf.sz(i);\n }\n vector<array<int, 3>> res = {ans};\n for (int i : qs) {\n assert(ts[i].size());\n ans[ts[i].back()] -= uf.sz(i);\n ts[i].pop_back();\n if (ts[i].size()) { // switch the type of this party from one to another\n ans[ts[i].back()] += uf.sz(i);\n }\n else { // delete this party and merge i into nx[i]\n int sz = uf.sz(i);\n uf.join(i, nx[i]);\n int r = rt[uf.rep(i)];\n if (ts[r].size()) ans[ts[r].back()] += sz;\n }\n res.push_back(ans);\n }\n reverse(res.begin(), res.end());\n res.erase(res.begin());\n for (auto &[c, o, w] : res) cout << c << \" \" << o << \" \" << w << \"\\n\";\n}", "chk_cpp": null, "std_source_submission_id": 70927, "std_origin": "verbatim earliest root score=100 submission #70927"}
{"cpid": 1546, "title": "Milk Buckets", "contest": "First Contest", "division": "Gold", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1546", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie has challenged Farmer John to a game involving milk buckets! There are $N$ ($2 \\le N \\le 2\\cdot 10^5$) milk buckets lined up in a row. The $i$-th bucket from the left initially contains $a_i$ ($0 \\le a_i \\le 10^9$) gallons of milk.\n\nThe game consists of two phases:\n\n**Phase 1:** Farmer John may swap any two adjacent buckets. He may perform as many swaps as he likes, but each swap costs $1$ coin.\n\n**Phase 2:** After swapping, Farmer John performs the following operation until only one bucket is left: Choose two adjacent buckets with milk amounts $a_i$ and $a_{i+1}$, and replace both buckets with one bucket containing $\\frac{a_i + a_{i+1}}{2}$ gallons of milk in their place.\n\nYour goal is to determine the minimum number of coins Farmer John must spend in the swapping phase to maximize the amount of milk in the final bucket after all merges are complete.\n\n## Input Format\n\nThe first line contains one integer $T$ ($1 \\le T \\le 100$): the number of independent test cases.\n\nThen, for each test case, the first line contains an integer $N$: the number of milk buckets. The second line contains $N$ integers $a_1, a_2, \\dots, a_N$, separated by spaces: the number of gallons of milk in each bucket.\n\nIt is guaranteed that the sum of $N$ over all test cases does not exceed $5\\cdot 10^5$.\n\n## Output Format\n\nFor each test case, output the minimum number of coins Farmer John must spend to maximize the amount of milk in the final bucket.\n\n## Examples\n\n**Input 1**\n\n```\n2\n3\n0 0 1\n3\n0 1 0\n```\n\n**Output 1**\n\n```\n0\n1\n```\n\n**Input 2**\n\n```\n4\n4\n9 4 9 2\n6\n0 0 2 0 0 0\n3\n2 0 1\n9\n3 3 3 10 3 2 13 14 13\n```\n\n**Output 2**\n\n```\n1\n2\n0\n3\n```\n\n## Note\n\nFor the first test of the first example, we do not need to swap any milk buckets in the first phase. In the second phase, Farmer John can merge the first two buckets and then merge the only two buckets left to achieve a final amount of $0.5$. It can be shown that this final amount is maximal.\n\nFor the second test of the first example, we must perform a singular swap of the first two milk buckets in the first stage to achieve a final amount of $0.5$ in the second stage. It can be shown that we cannot achieve a final amount of $0.5$ without swaps in the first stage.\n\nFor the first test of the second example, Farmer John can swap the second and the third buckets in the first phase. Then, in the second phase, Farmer John can perform the following:\n\n$$[9, 4, 9, 2] \\to [9, 9, 4, 2] \\to [9, 9, 3] \\to [9, 6] \\to [7.5]$$\n\n(swap → merge buckets $3$ and $4$ → merge buckets $2$ and $3$ → merge buckets $1$ and $2$.)\n\nThe final amount of milk is $7.5$, which is the maximum possible. It can be shown that even with additional swaps, the final amount cannot exceed $7.5$, and that with fewer swaps, the final amount cannot reach $7.5$.\n\n## Scoring\n\n- Inputs 3-4: $a_i\\le 1$ and $N\\le 2000$ (sum of $N\\le 5000$)\n- Inputs 5-6: $a_i\\le 1$\n- Inputs 7-9: $N\\le 2000$ (sum of $N\\le 5000$)\n- Inputs 10-14: No additional constraints\n\n**Problem credits:** Charlie Yang", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 12, "n_sample_tests": 2, "point_scores": [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12], "val_cpp": "#include \"testlib.h\"\n\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n\n int T = inf.readInt(1, 100, \"T\");\n inf.readEoln();\n\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int n = inf.readInt(2, 200000, \"N\");\n inf.readEoln();\n sumN += n;\n for (int i = 1; i <= n; i++) {\n inf.readInt(0, 1000000000, \"a_i\");\n if (i < n) inf.readSpace();\n }\n inf.readEoln();\n }\n ensuref(sumN <= 500000, \"sum of N is %lld, must be <= 500000\", sumN);\n\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n \nstruct BIT {\n int n;\n vector<int> t;\n BIT(int _n) : n(_n), t(n + 1) {}\n \n void add(int k, int x) {\n while(k <= n) {\n t[k] += x;\n k += k&-k;\n }\n }\n \n int sum(int r) {\n int sm = 0;\n while(r > 0) {\n sm += t[r];\n r -= r&-r;\n }\n return sm;\n }\n};\n \nvoid tc() {\n int N; cin >> N;\n \n vector<int> a(N);\n for(int i = 0; i < N; i++) cin >> a[i];\n vector<int> o(N); iota(o.begin(), o.end(), 0);\n sort(o.begin(), o.end(), [&] (int x, int y) {\n return a[x] < a[y];\n });\n \n BIT bit(N);\n int last = -1;\n vector<int> upds;\n long long ans = 0;\n for(int i : o) {\n if(last != a[i]) {\n for(int u : upds) {\n bit.add(u, 1);\n }\n upds.clear();\n }\n ans += min(bit.sum(N) - bit.sum(i + 1), bit.sum(i + 1));\n last = a[i];\n upds.push_back(i + 1);\n }\n cout << ans << endl;\n}\n \nint main() {\n ios::sync_with_stdio(false), cin.tie(nullptr);\n int T; cin >> T;\n \n while(T--) {\n tc();\n }\n}\n", "chk_cpp": null, "std_source_submission_id": 70928, "std_origin": "verbatim earliest root score=100 submission #70928"}
{"cpid": 1547, "title": "Supervision", "contest": "First Contest", "division": "Gold", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1547", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nThere are $N$ ($1\\le N\\le 10^6$) cows in cow camp, labeled $1\\dots N$. Each cow is either a camper or a coach.\n\nA nonempty subset of the cows will be selected to attend a field trip. If the $i$-th cow is selected, the cow will move to position $p_i$ ($0\\le p_i\\le 10^9$) on a number line, where the array $p$ is strictly increasing.\n\nA nonempty subset of the cows is called \"good\" if for every selected camper, there is a selected coach within $D$ units to the left, inclusive ($0\\le D\\le 10^9$). How many good subsets are there, modulo $10^9 + 7$?\n\n## Input Format\n\nThe first line contains two integers $N$ and $D$.\n\nThe next $N$ lines each contain two integers $p_i$ and $o_i$. $p_i$ denotes the position the $i$-th cow will move to. $o_i = 1$ means the $i$-th cow is a coach, whereas $o_i = 0$ means the $i$-th cow is a camper.\n\nIt is guaranteed that the $p_i$ are given in strictly increasing order.\n\n## Output Format\n\nOutput the number of good subsets modulo $10^9 + 7$.\n\n## Examples\n\n**Input 1**\n\n```\n6 1\n3 1\n4 0\n6 1\n7 1\n9 0\n10 0\n```\n\n**Output 1**\n\n```\n11\n```\n\n**Input 2**\n\n```\n20 24\n3 0\n14 0\n17 1\n20 0\n21 0\n22 1\n28 0\n30 0\n32 0\n33 1\n38 0\n40 0\n52 0\n58 0\n73 0\n75 0\n77 1\n81 1\n84 1\n97 0\n```\n\n**Output 2**\n\n```\n13094\n```\n\n## Note\n\nFor the first example, the last two campers can never be selected. All other nonempty subsets work as long as if cow $2$ is selected, then cow $1$ is also selected.\n\n## Scoring\n\n- Input 3: $N = 20$\n- Input 4: $D = 0$\n- Inputs 5-8: $N\\le 5000$\n- Inputs 9-16: No additional constraints\n\n**Problem credits:** Agastya Goel, Eva Zhu, and Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 14, "n_sample_tests": 2, "point_scores": [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(1, 1000000, \"N\");\n inf.readSpace();\n inf.readInt(0, 1000000000, \"D\");\n inf.readEoln();\n long long prev = -1;\n for (int i = 1; i <= N; i++) {\n int p = inf.readInt(0, 1000000000, \"p_i\");\n inf.readSpace();\n inf.readInt(0, 1, \"o_i\");\n inf.readEoln();\n ensuref(p > prev, \"row %d: p must be strictly increasing (%d <= %lld)\", i, p, prev);\n prev = p;\n }\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\n#define ll long long\n#define rep(i, a, b) for(int i = (a); i <= (b); i++)\n#define per(i, b, a) for(int i = (b); i >= (a); i--)\nusing namespace std;\nconst int N = 1e6 + 5, MOD = 1e9 + 7;\nll p, D, inv2 = (MOD + 1) / 2, qp[N], qg[N];\nint main() {\n int n; scanf(\"%d%lld\", &n, &D);\n ll mul = 1, inv_mul = 1, fsum = 0, gsum = 0;\n int headp = 1, tailp = 0;\n int headg = 1, tailg = 0;\n rep(i, 1, n){\n int o; scanf(\"%lld%d\", &p, &o);\n while(headp <= tailp && qp[headp] < p-D){\n fsum = (fsum + (qg[headg] * mul)) % MOD;\n gsum = (gsum - qg[headg]) % MOD;\n headp++, headg++;\n }\n if(o == 0){\n mul = (mul * 2) % MOD;\n inv_mul = (inv_mul * inv2) % MOD;\n } \n else{\n ll total = (1 + fsum + gsum * mul % MOD) % MOD;\n ll x = (total * inv_mul) % MOD;\n qp[++tailp] = p, qg[++tailg] = x;\n gsum = (gsum + x) % MOD;\n }\n }\n ll ans = (fsum + (gsum * mul) % MOD) % MOD;\n printf(\"%lld\\n\", (ans + MOD) % MOD);\n return 0;\n}", "chk_cpp": null, "std_source_submission_id": 70929, "std_origin": "verbatim earliest root score=100 submission #70929"}
{"cpid": 1548, "title": "Hoof, Paper, Scissors Triples", "contest": "First Contest", "division": "Platinum", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1548", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nYou have probably heard of the game \"Rock, Paper, Scissors\". The cows like to play a similar game they call \"Hoof, Paper, Scissors\".\n\nThe rules of \"Hoof, Paper, Scissors\" are simple. Two cows play against each other. They both count to three and then each simultaneously makes a gesture that represents either a hoof, a piece of paper, or a pair of scissors. Hoof beats scissors (since a hoof can smash a pair of scissors), scissors beats paper (since scissors can cut paper), and paper beats hoof (since the hoof can get a papercut). For example, if the first cow makes a \"hoof\" gesture and the second a \"paper\" gesture, then the second cow wins. Of course, it is also possible to tie, if both cows make the same gesture.\n\nNow there are $N$ ($3\\le N\\le 2\\cdot 10^5$) cows who want to play hoof paper scissors, and they each independently have a strategy of drawing from some fixed distribution. In particular, the $i$-th cow's strategy is to play hoof, paper, or scissors with probabilities\n\n$$\\left(\\frac{h_i}{h_i+p_i+s_i},\\ \\frac{p_i}{h_i+p_i+s_i},\\ \\frac{s_i}{h_i+p_i+s_i}\\right),$$\n\nrespectively.\n\nHow many distinct triples of cows $(A, B, C)$ are there such that $A$ beats $B$ on average, $B$ beats $C$ on average, and $C$ beats $A$ on average? We consider two triples the same if one equals the other up to a cyclic shift.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 5\\cdot 10^4$), the number of independent tests. Each test is specified in the following format:\n\nThe first line contains $N$.\n\nThe next $N$ lines each contain three non-negative integers $h_i$, $p_i$, $s_i$ ($0\\le h_i, p_i, s_i\\le 10^9$, $h_i+p_i+s_i>0$).\n\nIt is guaranteed that the sum of $N$ over all tests does not exceed $3\\cdot 10^5$.\n\n## Output Format\n\nOutput the number of triples.\n\n**Note:** The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a `long long` in C/C++).\n\n## Examples\n\n**Input 1**\n\n```\n2\n4\n1 0 0\n1 0 0\n0 1 0\n0 0 1\n10\n20410069 21445597 257862632\n114108992 287498302 113278897\n607994331 143503714 631122722\n337497016 270153603 320256324\n633717786 631078144 493265815\n202783212 612643590 560838949\n713379081 42803063 58996167\n293262767 470686180 220651551\n656404313 408797935 345461691\n959196297 827681918 591519393\n```\n\n**Output 1**\n\n```\n2\n32\n```\n\n## Note\n\nFor the first test, there are two triples: $(1, 3, 4)$ and $(2, 3, 4)$.\n\n## Scoring\n\n- Inputs 2-3: $N\\le 10$\n- Inputs 4-9: $N\\le 7500$, the sum of $N$ over all tests does not exceed $10^4$\n- Inputs 10-21: No additional constraints", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 1, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\n\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n\n int T = inf.readInt(1, 50000, \"T\");\n inf.readEoln();\n\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int n = inf.readInt(3, 200000, \"N\");\n inf.readEoln();\n sumN += n;\n for (int i = 1; i <= n; i++) {\n int h = inf.readInt(0, 1000000000, \"h\");\n inf.readSpace();\n int p = inf.readInt(0, 1000000000, \"p\");\n inf.readSpace();\n int s = inf.readInt(0, 1000000000, \"s\");\n inf.readEoln();\n ensuref((long long)h + p + s > 0,\n \"test %d row %d: h+p+s must be > 0\", tc, i);\n }\n }\n ensuref(sumN <= 300000,\n \"sum of N over all tests is %lld, must be <= 300000\", sumN);\n\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Point {\n long long x, y;\n};\nlong long cross(Point a, Point b) {\n return a.x * b.y - a.y * b.x;\n}\nint half(Point p) {\n return p.y > 0 || (p.y == 0 && p.x > 0) ? 1 : -1;\n}\nbool angleCmp(Point a, Point b) {\n int h1 = half(a), h2 = half(b);\n return h1 == h2 ? cross(a, b) > 0 : h1 < h2;\n}\nlong long c3(long long n) {\n return n * (n - 1) * (n - 2) / 6;\n}\nlong long c2(long long n) {\n return n * (n - 1) / 2;\n}\nvoid solve() {\n int N;\n cin >> N;\n vector<Point> points;\n for (int i = 0; i < N; ++i) {\n int r, p, s;\n cin >> r >> p >> s;\n p -= s, r -= s;\n if (p || r) points.push_back({(long long)p, (long long)r});\n }\n sort(points.begin(), points.end(), angleCmp);\n vector<pair<Point, long long>> points2;\n for (const auto& t : points) {\n if (!points2.empty() && !angleCmp(points2.back().first, t) && !angleCmp(t, points2.back().first)) {\n points2.back().second++;\n } else {\n points2.push_back({t, 1});\n }\n }\n long long ans = c3(points.size());\n int r = -1;\n long long sum = 0;\n int M = points2.size();\n for (int l = 0; l < M; ++l) {\n if (r < l) {\n r = l;\n } else {\n sum -= points2[l].second;\n }\n while (cross(points2[l].first, points2[(r + 1) % M].first) > 0) {\n ++r;\n sum += points2[r % M].second;\n }\n ans -= (c3(sum + points2[l].second) - c3(sum));\n if ((r + 1) % M != l && cross(points2[l].first, points2[(r + 1) % M].first) == 0) {\n ans -= c2(points2[l].second) * points2[(r + 1) % M].second;\n ans -= points2[l].second * sum * points2[(r + 1) % M].second;\n }\n }\n cout << ans << \"\\n\";\n}\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int TC;\n cin >> TC;\n while (TC--) solve();\n return 0;\n}", "chk_cpp": null, "std_source_submission_id": 70924, "std_origin": "verbatim earliest root score=100 submission #70924"}
{"cpid": 1549, "title": "Lineup Counting Queries", "contest": "First Contest", "division": "Platinum", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1549", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nThere is a line of cows, initially (i.e. at time $t = 0$) containing only cow $0$ at position $0$ (here, a cow is at position $k$ if there are $k$ cows in front of it). At time $t$ for $t = 1, 2, 3, \\dots$, the cow at position $0$ moves to position $\\lfloor t/2\\rfloor$, every cow in positions $1 \\dots \\lfloor t/2\\rfloor$ moves forward one position, and cow $t$ joins the line at the end of the line (position $t$).\n\nAnswer $Q$ ($1\\le Q\\le 10^5$) independent queries each of the following form:\n\nOut of cows $l_1\\dots r_1$, how many are located at positions $l_2\\dots r_2$ immediately after time $t$?\n\n($0\\le l_1\\le r_1\\le t,\\ 0\\le l_2\\le r_2 \\le t,\\ t\\le 10^{18}$)\n\n## Input Format\n\nThe first line contains $Q$, the number of queries.\n\nThe next $Q$ lines each contain five integers specifying a query of the form \"$l_1\\ r_1\\ l_2\\ r_2\\ t$.\"\n\n## Output Format\n\nOutput the answer to each query on a separate line.\n\n## Examples\n\n**Input 1**\n\n```\n4\n0 9 0 9 9\n3 5 4 5 9\n4 5 3 5 9\n1 1 3 3 9\n```\n\n**Output 1**\n\n```\n10\n2\n1\n1\n```\n\n**Input 2**\n\n```\n1\n0 1000000000000000000 0 1000000000000000000 1000000000000000000\n```\n\n**Output 2**\n\n```\n1000000000000000001\n```\n\n## Note\n\nLineups at various times:\n\n```\nt = 0 | 0\nt = 1 | 0 1\nt = 2 | 1 0 2\nt = 3 | 0 1 2 3\nt = 4 | 1 2 0 3 4\nt = 5 | 2 0 1 3 4 5\nt = 6 | 0 1 3 2 4 5 6\nt = 7 | 1 3 2 0 4 5 6 7\nt = 8 | 3 2 0 4 1 5 6 7 8\nt = 9 | 2 0 4 1 3 5 6 7 8 9\n```\n\nAt $t = 9$ the cows from front to back are $[2, 0, 4, 1, 3, 5, 6, 7, 8, 9]$.\n\nTo answer the third query, the cows at positions $3\\dots 5$ are $[1, 3, 5]$, and only one of them is in the range $4\\dots 5$.\n\n## Scoring\n\n- Input 3: $Q\\le 1000$, $t\\le 100$\n- Inputs 4-7: $l_1 = r_1$ for all queries\n- Inputs 8-14: $r_1 \\le 2\\cdot l_1$ for all queries\n- Inputs 15-21: No additional constraints\n\n**Problem credits:** Agastya Goel and Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 19, "n_sample_tests": 2, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10], "val_cpp": "#include \"testlib.h\"\n\nconst long long MAXT = 1000000000000000000LL; // 1e18\n\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n\n int Q = inf.readInt(1, 100000, \"Q\");\n inf.readEoln();\n\n for (int q = 1; q <= Q; q++) {\n long long l1 = inf.readLong(0, MAXT, \"l1\");\n inf.readSpace();\n long long r1 = inf.readLong(0, MAXT, \"r1\");\n inf.readSpace();\n long long l2 = inf.readLong(0, MAXT, \"l2\");\n inf.readSpace();\n long long r2 = inf.readLong(0, MAXT, \"r2\");\n inf.readSpace();\n long long t = inf.readLong(0, MAXT, \"t\");\n inf.readEoln();\n\n ensuref(l1 <= r1, \"query %d: need l1 <= r1 (%lld > %lld)\", q, l1, r1);\n ensuref(r1 <= t, \"query %d: need r1 <= t (%lld > %lld)\", q, r1, t);\n ensuref(l2 <= r2, \"query %d: need l2 <= r2 (%lld > %lld)\", q, l2, r2);\n ensuref(r2 <= t, \"query %d: need r2 <= t (%lld > %lld)\", q, r2, t);\n }\n\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<long long> inv(long long n){\n vector<long long> ans;\n ans.push_back(4000000000000000000);\n for(int i=0;i<111;i++){\n ans.push_back(n);\n n = (2*n+4)/3-2;\n }\n return ans;\n}\n\nint main(){\n int q;\n cin >> q;\n while(q--){\n long long l1,r1,l2,r2,t;\n cin >> l1 >> r1 >> l2 >> r2 >> t;\n\n long long ans = 0;\n vector<long long> inv1 = inv(t-1);\n vector<long long> inv2 = inv(l2+t-1);\n vector<long long> inv3 = inv(r2+t);\n for(int j=1;j<111;j++){\n // l1 <= i <= min(t/2,r1)\n // 3*i-1 <= min(inv1[j-1],inv3[j])\n // 3*i-1 > inv2[j]\n long long low = max(l1,(inv2[j]+4)/3);\n long long high = min(min(t/2,r1),(min(inv1[j-1],inv3[j])+4)/3-1);\n ans += max(0LL,high-low+1);\n }\n\n // l1 <= i <= r1\n // l2 <= i <= r2\n // t <= 2*i-1\n ans += max(0LL,min(r1,r2)-max(max(l1,l2),t/2+1)+1);\n cout << ans << endl;\n }\n}", "chk_cpp": null, "std_source_submission_id": 70925, "std_origin": "verbatim earliest root score=100 submission #70925"}
{"cpid": 1550, "title": "Pluses and Minuses", "contest": "First Contest", "division": "Platinum", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1550", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John once painted a rectangular grid on the ground of his pasture. In each cell, he painted either a $+$ or a $-$ (representing $+1$ and $-1$, respectively).\n\nOver time, the paint faded, and Farmer John now remembers the values of only some cells. However, Farmer John does remember one important fact about the original painting:\n\n> In every row and every column, the sum of the values in any contiguous subsegment was always between $-1$ and $2$ (inclusive).\n\nAs an example, consider the row `+ - - +`. It does not satisfy the condition, since the subsegment `+ [ - - ] +` has sum $-2$.\n\nHowever, the row `- + + -` does satisfy the condition.\n\n```\n[ - ] + + - sum = -1\n[ - + ] + - sum = 0\n[ - + + ] - sum = +1\n[ - + + - ] sum = 0\n- [ + ] + - sum = +1\n- [ + + ] - sum = +2\n- [ + + - ] sum = +1\n- + [ + ] - sum = +1\n- + [ + - ] sum = 0\n- + + [ - ] sum = -1\n```\n\nCount the number of different grids consistent with Farmer John's memory.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 100$), the number of independent tests. Each test is specified as follows:\n\nThe first line contains $R$, $C$, and $X$ ($1\\le R, C\\le 5\\cdot 10^5$, $0\\le X\\le \\min(10^5, RC)$), meaning that the grid has dimensions $R\\times C$ and Farmer John remembers the values of $X$ different cells in the grid.\n\nThen following $X$ lines each contain a character $v\\in \\{+, -\\}$ followed by two integers $r$ and $c$ ($1\\le r\\le R$, $1\\le c\\le C$), meaning that the value at the $r$-th row and $c$-th column of the grid is $v$. It is guaranteed that no ordered pair $(r, c)$ appears more than once within a single test.\n\nAdditionally, it is guaranteed that neither the sum of $R$ nor the sum of $C$ over all tests exceeds $10^6$, and that the sum of $X$ over all tests does not exceed $2\\cdot 10^5$.\n\n## Output Format\n\nFor each test, output the number of grids on a separate line.\n\n## Examples\n\n**Input 1**\n\n```\n2\n1 3 3\n+ 1 3\n+ 1 1\n- 1 2\n1 3 3\n+ 1 1\n+ 1 3\n+ 1 2\n```\n\n**Output 1**\n\n```\n1\n0\n```\n\n**Input 2**\n\n```\n1\n2 2 0\n```\n\n**Output 2**\n\n```\n7\n```\n\n## Note\n\nFor the second example, here are the seven grids:\n\n```\n++ ++ ++ +- +- -+ -+\n++ +- -+ ++ -+ ++ +-\n```\n\n## Scoring\n\n- Inputs 3-4: $\\min(R, C) = 1$ for all tests\n- Inputs 5-6: $R, C\\le 10$ for all tests\n- Inputs 7-11: $\\sum \\max(R, C)^2 \\le 10^6$\n- Inputs 12-14: $\\sum RC \\le 10^6$\n- Inputs 15-22: No additional constraints\n\n**Problem credits:** Alex Chen", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 2, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\n#include <set>\n#include <utility>\n\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n\n int T = inf.readInt(1, 100, \"T\");\n inf.readEoln();\n\n long long sumR = 0, sumC = 0, sumX = 0;\n for (int tc = 1; tc <= T; tc++) {\n int R = inf.readInt(1, 500000, \"R\");\n inf.readSpace();\n int C = inf.readInt(1, 500000, \"C\");\n inf.readSpace();\n long long RC = (long long)R * C;\n long long xhi = RC < 100000 ? RC : 100000;\n int X = inf.readInt(0, (int)xhi, \"X\");\n inf.readEoln();\n\n sumR += R;\n sumC += C;\n sumX += X;\n\n std::set<std::pair<int,int>> seen;\n for (int i = 1; i <= X; i++) {\n char v = inf.readChar();\n ensuref(v == '+' || v == '-',\n \"test %d cell %d: value must be + or -, got '%c'\", tc, i, v);\n inf.readSpace();\n int r = inf.readInt(1, R, \"r\");\n inf.readSpace();\n int c = inf.readInt(1, C, \"c\");\n inf.readEoln();\n ensuref(seen.insert({r, c}).second,\n \"test %d: duplicate cell (%d, %d)\", tc, r, c);\n }\n }\n ensuref(sumR <= 1000000, \"sum of R is %lld, must be <= 1000000\", sumR);\n ensuref(sumC <= 1000000, \"sum of C is %lld, must be <= 1000000\", sumC);\n ensuref(sumX <= 200000, \"sum of X is %lld, must be <= 200000\", sumX);\n\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\n\nusing i64 = long long;\n\nstruct Cell {\n int v, r, c;\n};\n\nvoid tc() {\n int r, c, x;\n std::cin >> r >> c >> x;\n\n std::vector<Cell> cl(x);\n for (auto& [v, ri, ci] : cl) {\n char cc;\n std::cin >> cc >> ri >> ci;\n v = cc == '+';\n --ri;\n --ci;\n }\n\n if (r == 1 && c == 1) {\n if (x == 1)\n std::cout << 1 << '\\n';\n else\n std::cout << 2 << '\\n';\n return;\n }\n\n auto get_mi_rb = [&](int d) {\n int mx = std::max(r, c);\n if (mx % 2 == 0)\n return d + mx - 1;\n return d + mx;\n };\n\n auto comp = [&](int mi, int rb) {\n if (mi > rb)\n return 0;\n return (rb - mi) / 2 + 1;\n };\n\n i64 ans = 0;\n auto solve = [&](bool z) {\n std::vector vals(r + c - 1, -1);\n for (auto [v, ri, ci] : cl) {\n if (vals[ri + ci] == -1)\n vals[ri + ci] = v;\n else if (vals[ri + ci] != v)\n return;\n }\n bool ff = vals[0] == -1, fl = vals[r + c - 2] == -1;\n for (int fval : {0, 1}) {\n if (!ff && vals[0] != fval)\n continue;\n vals[0] = fval;\n for (int lval : {0, 1}) {\n if (!fl && vals[r + c - 2] != lval)\n continue;\n vals[r + c - 2] = lval;\n std::vector<int> d;\n for (int i = 0; i < r + c - 1; ++i) {\n if (vals[i] != -1)\n d.push_back(i);\n }\n std::pair i1{-1, -1}, i2{-1, -1};\n bool g = true;\n for (int i = 0; i < (int)d.size() - 1; ++i) {\n int lb = d[i], rb = d[i + 1];\n if ((vals[lb] ^ vals[rb]) != (rb - lb) % 2) {\n if (i1.first == -1)\n i1 = {lb, rb};\n else if (i2.first == -1)\n i2 = {lb, rb};\n else {\n g = false;\n break;\n }\n }\n }\n if (!g)\n continue;\n\n if (i1.first == -1) {\n if (z)\n ++ans;\n for (int i = 0; i < (int)d.size() - 1; ++i) {\n int L = d[i], R = d[i + 1];\n for (int lb = vals[L] == 1 ? L + 1 : L + 2; get_mi_rb(lb) <= R; lb += 2)\n ans += comp(get_mi_rb(lb), R);\n }\n } else if (i2.first == -1) {\n auto [L, R] = i1;\n if (vals[L] == 1)\n ans += comp(L + 1, R);\n else\n ans += comp(L + 2, R);\n } else {\n auto [l1, r1] = i1;\n auto [l2, r2] = i2;\n for (int lb = vals[l1] == 1 ? l1 + 1 : l1 + 2; lb <= r1; lb += 2) {\n int rb = std::max(get_mi_rb(lb), vals[l2] == 1 ? l2 + 1 : l2 + 2);\n ans += comp(rb, r2);\n }\n }\n }\n }\n };\n solve(true);\n if (std::min(r, c) > 1) {\n for (auto& [v, ri, ci] : cl)\n ci = c - 1 - ci;\n solve(false);\n }\n\n if (r == 2 && c == 2) {\n bool g = true;\n for (auto [v, ri, ci] : cl) {\n if (v == 0) {\n g = false;\n break;\n }\n }\n if (g)\n --ans;\n }\n\n std::cout << ans << '\\n';\n}\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n\n int t;\n std::cin >> t;\n while (t--)\n tc();\n}", "chk_cpp": null, "std_source_submission_id": 70926, "std_origin": "verbatim earliest root score=100 submission #70926"}
{"cpid": 1563, "title": "It's Mooin' Time IV", "contest": "Second Contest", "division": "Bronze", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1563", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie has a computer with a keyboard that only has two letters, `M` and `O`.\n\nBessie wants to type her favorite moo $S$ consisting of $N$ letters, each of which is either an `M` or an `O`. However, her computer has been hit with a virus. Every time she tries to type the letter `O`, every letter that she has typed so far flips, either from `M` to `O` or from `O` to `M`, before the `O` appears.\n\nIs it possible for Bessie to type out her favorite moo?\n\nAdditionally, Bessie is given a parameter $k$ which is either $0$ or $1$.\n\n- If $k = 0$, then Bessie only needs to determine whether it is possible to type out her favorite moo.\n- If $k = 1$, then Bessie also needs to give an example of a sequence of keystrokes to type so she can type out her favorite moo.\n\n## Input Format\n\nThe first line contains $T$, the number of independent test cases ($1\\le T\\le 10^4$) and $k$ ($0\\le k\\le 1$).\n\nThe first line of each test case has $N$ ($1\\le N\\le 2\\cdot 10^5$).\n\nThe second line of each test case has $S$. It is guaranteed that no characters will appear in $S$ besides $\\texttt{M}$ and $\\texttt{O}$.\n\nThe sum of $N$ across all test cases will not exceed $4\\cdot 10^5$.\n\n## Output Format\n\nFor each test case, output either one or two lines using the following procedure.\n\nIf it is impossible for Bessie to type out $S$, print $\\texttt{NO}$ on a single line.\n\nOtherwise, on the first line print $\\texttt{YES}$. Furthermore, if $k = 1$, on the second line, print a string of length $N$, the characters in order that Bessie needs to type in order to type out her favorite moo. If there are multiple such strings, any will be accepted.\n\n## Examples\n\n**Input 1**\n\n```\n2 0\n3\nMOO\n5\nOOMOO\n```\n\n**Output 1**\n\n```\nYES\nYES\n```\n\n**Input 2**\n\n```\n2 1\n3\nMOO\n5\nOOMOO\n```\n\n**Output 2**\n\n```\nYES\nOMO\nYES\nMOOMO\n```\n\n## Note\n\nAs Bessie types out $\\texttt{MOOMO}$, this is how the letters change:\n\n- Before typing the first $\\texttt{M}$, Bessie has an empty string. Afterwards, she has the string $\\texttt{M}$.\n- After typing the first $\\texttt{O}$, the $\\texttt{M}$ flips to $\\texttt{O}$, and then the $\\texttt{O}$ is appended to form $\\texttt{OO}$.\n- After typing the second $\\texttt{O}$, the $\\texttt{OO}$ flips to $\\texttt{MM}$, and then the $\\texttt{O}$ is appended to form $\\texttt{MMO}$.\n- After typing the second $\\texttt{M}$, Bessie has the string $\\texttt{MMOM}$.\n- After typing the last $\\texttt{O}$, the string $\\texttt{MMOM}$ flips to $\\texttt{OOMO}$, and then the $\\texttt{O}$ is appended to form $\\texttt{OOMOO}$, as desired.\n\n## Scoring\n\n- Inputs 3-4: $k = 0$\n- Inputs 5-6: $k = 1$, $T\\le 10^3$, $N\\le 10$\n- Inputs 7-9: $k = 1$, $T\\le 10$, $N\\le 1000$\n- Inputs 10-16: $k = 1$\n\n**Problem credits:** Nick Wu", "checker_kind": "custom (chk.cpp, testlib)", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 14, "n_sample_tests": 2, "point_scores": [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9], "val_cpp": "#include \"testlib.h\"\n#include <string>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10000, \"T\"); inf.readSpace();\n inf.readInt(0, 1, \"k\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 200000, \"N\"); inf.readEoln();\n sumN += N;\n std::string s = inf.readLine();\n ensuref((int)s.size() == N, \"test %d: |S|=%d expected %d\", tc, (int)s.size(), N);\n for (char c : s) ensuref(c == 'M' || c == 'O', \"test %d: char must be M/O got '%c'\", tc, c);\n }\n ensuref(sumN <= 400000, \"sum N %lld > 4e5\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <iostream>\n\nusing namespace std;\n\nint main() {\n int t, mode;\n cin >> t >> mode;\n while(t--) {\n int n;\n string s;\n cin >> n >> s;\n cout << \"YES\\n\";\n if(mode == 0) continue;\n for(int i = 0; i+1 < n; i++) {\n if(s[i] == s[i+1]) cout << 'M';\n else cout << 'O';\n }\n cout << s.back() << \"\\n\";\n }\n}", "chk_cpp": "#include \"testlib.h\"\n#include <string>\nusing namespace std;\n\nstatic string simulate(const string &keys) {\n int n = keys.size();\n string s(n, '?');\n int suffO = 0; // number of 'O' strictly after position i, computed right-to-left\n for (int i = n - 1; i >= 0; --i) {\n char base = keys[i];\n // 'O' at position i is appended at that step; subsequent O's flip it suffO times.\n // 'M' at position i is appended; subsequent O's flip it suffO times.\n bool flip = (suffO & 1);\n char out = base;\n if (flip) out = (out == 'M' ? 'O' : 'M');\n s[i] = out;\n if (keys[i] == 'O') suffO++;\n }\n return s;\n}\n\nint main(int argc, char *argv[]) {\n setName(\"USACO 2026 Bronze: Mooin' Time IV\");\n registerTestlibCmd(argc, argv);\n\n int T = inf.readInt();\n int K = inf.readInt();\n\n for (int t = 1; t <= T; t++) {\n int N = inf.readInt();\n string S = inf.readToken();\n if ((int)S.size() != N) quitf(_fail, \"judge input malformed at test %d\", t);\n\n string ansVerdict = ans.readToken(); // YES or NO\n string oufVerdict = ouf.readToken();\n if (ansVerdict != \"YES\" && ansVerdict != \"NO\")\n quitf(_fail, \"reference output malformed at test %d: %s\", t, ansVerdict.c_str());\n if (oufVerdict != \"YES\" && oufVerdict != \"NO\")\n quitf(_wa, \"test %d: contestant verdict must be YES or NO, got '%s'\", t, oufVerdict.c_str());\n if (ansVerdict != oufVerdict)\n quitf(_wa, \"test %d: verdict differs (expected %s, got %s)\", t, ansVerdict.c_str(), oufVerdict.c_str());\n\n if (K == 1 && ansVerdict == \"YES\") {\n string oufKeys = ouf.readToken();\n if ((int)oufKeys.size() != N)\n quitf(_wa, \"test %d: keystroke string length %d, expected %d\", t, (int)oufKeys.size(), N);\n for (char c : oufKeys)\n if (c != 'M' && c != 'O')\n quitf(_wa, \"test %d: keystroke string contains invalid char '%c'\", t, c);\n string produced = simulate(oufKeys);\n if (produced != S)\n quitf(_wa, \"test %d: keystrokes '%s' produce '%s', expected '%s'\",\n t, oufKeys.c_str(), produced.c_str(), S.c_str());\n\n // also consume reference's keystroke if present (it is, since reference says YES)\n if (!ans.seekEof()) ans.readToken();\n }\n }\n\n if (!ouf.seekEof()) quitf(_wa, \"extra tokens in contestant output\");\n quitf(_ok, \"T=%d cases, K=%d\", T, K);\n}\n", "std_source_submission_id": 70970, "std_origin": "verbatim earliest root score=100 submission #70970"}
{"cpid": 1564, "title": "Moo Hunt", "contest": "Second Contest", "division": "Bronze", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1564", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie is playing the popular game \"Moo Hunt\". In this game, there are $N$ ($3\\le N\\le 20$) cells in a line, numbered from $1$ to $N$. All cells either have the character $M$ or $O$ with the $i$-th cell having character $s_i$.\n\nBessie plans to perform $K$ ($1\\le K\\le 2\\cdot 10^5$) mooves. On her $i$-th moove, Bessie will tap $3$ different cells ($x_i, y_i, z_i$) ($1\\le x_i, y_i, z_i\\le N$). Bessie will earn a point if $s_{x_i} = M$ and $s_{y_i} = s_{z_i} = O$. In other words, Bessie will earn a point if she forms the string $MOO$ by tapping cells $x_i, y_i, z_i$ in that order.\n\nFarmer John wants to help Bessie get a new high score. He wants you to find the maximum possible score Bessie could get across all possible boards if she performs the $K$ mooves as well as the number of different boards that will allow Bessie to achieve this maximum possible score. Two boards are different if there exists a cell such that the corresponding characters at the cell are different.\n\n## Input Format\n\nThe first line contains $N$ and $K$, the number of cells and the number of mooves Bessie will perform.\n\nEach of the next $K$ lines contains $x_i, y_i, z_i$ describing Bessie's $i$-th move ($x_i, y_i, z_i$ are pairwise distinct).\n\n## Output Format\n\nOutput the maximum possible score Bessie could achieve, followed by the count of different boards that will allow Bessie to achieve this maximum score.\n\n## Examples\n\n**Input 1**\n\n```\n5 6\n1 2 3\n1 2 3\n1 3 5\n2 3 4\n5 3 2\n5 2 3\n```\n\n**Output 1**\n\n```\n4 2\n```\n\n**Input 2**\n\n```\n6 12\n2 4 3\n2 3 4\n3 5 2\n3 5 1\n3 1 5\n3 1 2\n6 1 5\n1 6 4\n2 3 6\n3 6 2\n4 1 6\n3 4 2\n```\n\n**Output 2**\n\n```\n6 3\n```\n\n## Note\n\nFor the first example, the boards $MOOOM$ and $MOOMM$ allow Bessie to achieve a maximum score of $4$. In both boards, Bessie will earn points on mooves $1, 2, 5, 6$. It can be shown that this is the maximum score Bessie can achieve, and those two boards are the only possible boards allowing Bessie to achieve a score of $4$.\n\nFor the second example, the boards that allow Bessie to achieve a maximum possible score of $6$ are $OOMOOO$, $OOMMOO$, and $OOMOOM$.\n\n## Scoring\n\n- Inputs 3-5: $N\\le 8$, $K\\le 10^4$\n- Inputs 6-12: There will be one test for each $N\\in \\{14, 15, 16, 17, 18, 19, 20\\}$ with no additional constraints on $K$\n\n**Problem credits:** Alex Liang", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 2, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(3, 20, \"N\"); inf.readSpace();\n int K = inf.readInt(1, 200000, \"K\"); inf.readEoln();\n for (int i = 1; i <= K; i++) {\n int x = inf.readInt(1, N, \"x\"); inf.readSpace();\n int y = inf.readInt(1, N, \"y\"); inf.readSpace();\n int z = inf.readInt(1, N, \"z\"); inf.readEoln();\n ensuref(x != y && y != z && x != z, \"moove %d: x,y,z must be pairwise distinct (%d %d %d)\", i, x, y, z);\n }\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std; \n \nint main(){\n int n, k;\n cin >> n >> k;\n \n vector<vector<vector<int>>> isAt(n, vector<vector<int>>(n, vector<int>(n, 0)));\n \n for (int i = 0; i < k; i++) {\n int x, y, z;\n cin >> x >> y >> z;\n x--; y--; z--;\n isAt[x][y][z]++;\n }\n \n vector<int> score((1 << n), 0);\n \n for (int msk = 1; msk < (1 << n); msk++) {\n int msb = 31 - __builtin_clz(msk);\n vector<int> mpos, opos;\n \n for (int i = 0; i < n; i++) {\n if ((1 << i) & msk)\n mpos.push_back(i);\n else\n opos.push_back(i);\n }\n \n // Previously\n score[msk] = score[msk ^ (1 << msb)];\n \n // Turned bad from flipping MSB\n for (int m : mpos) \n for (int o : opos)\n score[msk] -= isAt[m][msb][o] + isAt[m][o][msb];\n \n // Turned good from flipping MSB\n for (int o1 : opos)\n for (int o2 : opos)\n score[msk] += isAt[msb][o1][o2];\n }\n int bestVal = *max_element(score.begin(), score.end());\n int bestCount = count(score.begin(), score.end(), bestVal);\n \n cout << bestVal << \" \" << bestCount << \"\\n\";\n}", "chk_cpp": null, "std_source_submission_id": 70946, "std_origin": "verbatim earliest root score=100 submission #70946"}
{"cpid": 1565, "title": "Purchasing Milk", "contest": "Second Contest", "division": "Bronze", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1565", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nOn National Milk Day, Farmer John is offering exclusive prices on buckets of milk! He has $N$ ($1\\le N\\le 10^5$) deals numbered from $1$ to $N$. For the $i$-th deal, he is offering $2^{i-1}$ buckets of milk for $a_i$ ($1\\le a_i\\le 10^9$, $a_i < a_{i+1}$) moonies. The same deal may be taken any non-negative integer number of times.\n\nYou are thinking about $Q$ ($1\\le Q\\le 10^4$) independent queries. For each query, you have an integer $x$ ($1\\le x\\le 10^9$) in mind and wonder what is the minimum cost to purchase at least $x$ buckets of milk.\n\n## Input Format\n\nThe first line contains two integers $N$ and $Q$.\n\nThe following line contains $a_1, a_2, \\ldots, a_N$.\n\nEach of the following $Q$ lines contains an integer $x$, representing a query.\n\n## Output Format\n\nFor each query, output the minimum cost on a new line.\n\n**Note:** The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a `long long` in C/C++).\n\n## Examples\n\n**Input 1**\n\n```\n2 4\n10 15\n1\n2\n6\n7\n```\n\n**Output 1**\n\n```\n10\n15\n45\n55\n```\n\n**Input 2**\n\n```\n4 10\n10 25 30 70\n1\n2\n3\n4\n5\n6\n7\n8\n15\n101\n```\n\n**Output 2**\n\n```\n10\n20\n30\n30\n40\n50\n60\n60\n120\n760\n```\n\n## Note\n\nFor the first example, Farmer John is offering 2 deals: $1$ bucket of milk for $10$ moonies and $2$ buckets of milk for $15$ moonies.\n\nThe cheapest cost to buy $1$ bucket is just the cost of the $1$ bucket deal and the cheapest cost to buy $2$ buckets is just the cost of the $2$ bucket deal.\n\nTo get $6$ buckets, the cheapest way is to purchase $3$ of the $2$ bucket deal for a total of $45$ moonies.\n\nTo get $7$ buckets, the cheapest way is to purchase $3$ of the $2$ bucket deal and $1$ of the $1$ bucket deal for a total of $55$ moonies.\n\nFor the second example, Farmer John is offering a total of $4$ deals for $1, 2, 4$, and $8$ buckets. For each of the $10$ queries, the corresponding output indicates the minimum cost to purchase at least that amount of milk. Sometimes, it is cheaper to purchase more than the specified amount.\n\n## Scoring\n\n- Inputs 3-4: $N\\le 2$\n- Inputs 5-8: $N\\le 10$\n- Inputs 9-16: No additional constraints\n\n**Problem credits:** Chongtian Ma", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 14, "n_sample_tests": 2, "point_scores": [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(1, 100000, \"N\"); inf.readSpace();\n int Q = inf.readInt(1, 10000, \"Q\"); inf.readEoln();\n long long prev = 0;\n for (int i = 1; i <= N; i++) {\n int a = inf.readInt(1, 1000000000, \"a_i\");\n if (i > 1) ensuref(a > prev, \"a must be strictly increasing (idx %d: %d <= %lld)\", i, a, prev);\n prev = a;\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n for (int q = 1; q <= Q; q++) { inf.readInt(1, 1000000000, \"x\"); inf.readEoln(); }\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <stdio.h>\n#include <stdint.h>\n#include <limits.h>\n#include <algorithm>\nusing namespace std;\n\nvoid solve()\n{\n int N, K, Q;\n scanf(\"%d %d\", &N, &Q);\n \n int price[N];\n scanf(\"%d\", &price[0]);\n for (int idx = 1; idx < N; ++idx)\n {\n scanf(\"%d\", &price[idx]);\n price[idx] = min(price[idx], 2 * price[idx-1]);\n }\n \n for (int idx = 0; idx < Q; ++idx)\n {\n int x;\n scanf(\"%d\", &x);\n \n long long minCost = 1e18;\n long long curCost = 0;\n for (int jdx = min(30, N-1); jdx >= 0; --jdx)\n {\n int curBuckets = 1<<jdx;\n long long count = x / curBuckets;\n \n x -= count*curBuckets;\n curCost += count*price[jdx];\n \n if (x == 0)\n minCost = min(minCost, curCost);\n else\n minCost = min(minCost, curCost + price[jdx]);\n }\n \n printf(\"%lld\\n\", minCost);\n }\n}\n \nint main()\n{\n solve();\n \n return 0;\n}", "chk_cpp": null, "std_source_submission_id": 70947, "std_origin": "verbatim earliest root score=100 submission #70947"}
{"cpid": 1566, "title": "Cow-libi 2", "contest": "Second Contest", "division": "Silver", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1566", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John and Farmer Nhoj have taken their respective cows to sit around a campfire in hopes of settling their personal differences. In total, there are $N$ ($2\\le N\\le 10^5$) cows sitting in a circular formation. When the farmers are ready to take their cows back to their farms, they realize one crucial mistake: since all the cows look the same and are mixed up, they are unable to identify which cows belong to which farmer!\n\nThen the $N$ cows are organized into one straight line to be interrogated by the two farmers.\n\nBecause of the confusion, the order of the cows in the line, from $1$ to $N$, might not correspond to their circular order around the campfire.\n\nBut the cows want to play a game. Instead of answering directly which farmer they belong to, they say which farmer the cows *adjacent to them in the original circle* belong to. Additionally, it is known that Farmer Nhoj's cows always lie but Farmer John raised his cows well and they will always tell the truth.\n\nGiven the statements of the cows, is it possible to assign each cow to either Farmer John or Farmer Nhoj such that the statements of the cows assigned to Farmer John are all true, and the statements of the cows assigned to Farmer Nhoj are all false?\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 1000$), the number of independent test cases, and an integer $C\\in \\{0, 1\\}$ (whether to output a construction or not).\n\nThe first line of each test case contains $N$.\n\nThe following line contains a string of length $N$ containing characters `J` or `N`. The $i$-th character is `J` if cow $i$ claims the cow to their *left* in the circle belongs to Farmer John, or Farmer Nhoj otherwise.\n\nThe following line contains a string of length $N$ containing characters `J` or `N`. The $i$-th character is `J` if cow $i$ claims the cow to their *right* in the circle belongs to Farmer John, or Farmer Nhoj otherwise.\n\nIt is guaranteed that the sum of $N$ over all tests does not exceed $5\\cdot 10^5$.\n\n## Output Format\n\nFor each test case, output `YES` or `NO`.\n\nAdditionally, if $C = 1$ and the answer is `YES`, output two more lines describing your construction:\n\n- The first line should contain a permutation $p_1, p_2, \\dots, p_N$ of $1\\dots N$, representing the circular order of the cows around the campfire, where cow $p_i$ is to the left of cow $p_{i+1}$ for $i$ in $1\\dots N - 1$, and cow $p_N$ is to the left of cow $p_1$.\n- The second line should contain a string $b_1 b_2 \\dots b_N$ consisting only of `J`s and `N`s, meaning that cow $p_i$ belongs to Farmer John if $b_i$ is `J`, or Farmer Nhoj otherwise.\n\nAny valid construction will be accepted.\n\n## Examples\n\n**Input 1**\n\n```\n6 0\n3\nJJJ\nJJJ\n4\nJJNJ\nNJJJ\n6\nNJNJNJ\nJNNJNJ\n4\nNNNN\nNNNN\n3\nNNN\nNNN\n5\nJJNNJ\nNJNJJ\n```\n\n**Output 1**\n\n```\nYES\nNO\nNO\nYES\nNO\nYES\n```\n\n**Input 2**\n\n```\n6 1\n3\nJJJ\nJJJ\n4\nJJNJ\nNJJJ\n6\nNJNJNJ\nJNNJNJ\n4\nNNNN\nNNNN\n3\nNNN\nNNN\n5\nJJNNJ\nNJNJJ\n```\n\n**Output 2**\n\n```\nYES\n1 2 3\nJJJ\nNO\nNO\nYES\n1 2 3 4\nNJNJ\nNO\nYES\n4 5 2 1 3\nJJJJN\n```\n\n## Note\n\nConsider the output for the sixth test case. Cows $1, 2, 4, 5$ belong to Farmer John, and Cow $3$ belongs to Farmer Nhoj.\n\nThe cows will then behave as follows:\n\n- Cow $1$'s left and right neighbours are Cow $2$ and Cow $3$, respectively. Cow $1$ says that Cow $2$ belongs to Farmer John, and Cow $3$ belongs to Farmer Nhoj.\n- Cow $2$'s left and right neighbours are Cow $5$ and Cow $1$, respectively. Cow $2$ says that both cows belong to Farmer John.\n- Cow $3$'s left and right neighbours are Cow $1$ and Cow $4$, respectively. Cow $3$ (dishonestly) says that both cows belong to Farmer Nhoj.\n- Cow $4$'s left and right neighbours are Cow $3$ and Cow $5$, respectively. Cow $4$ says that Cow $3$ belongs to Farmer Nhoj, and Cow $5$ belongs to Farmer John.\n- Cow $5$'s left and right neighbours are Cow $4$ and Cow $2$, respectively. Cow $5$ says that both cows belong to Farmer John.\n\nAll these claims are consistent with the input.\n\n## Scoring\n\n- Input 3: $C = 0$ and $N\\le 10$\n- Input 4: $C = 1$ and $N\\le 10$\n- Inputs 5-8: $C = 0$\n- Inputs 9-12: $C = 1$\n\n**Problem credits:** Chongtian Ma", "checker_kind": "custom (chk.cpp, testlib)", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 2, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\n#include <string>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 1000, \"T\"); inf.readSpace();\n inf.readInt(0, 1, \"C\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(2, 100000, \"N\"); inf.readEoln();\n sumN += N;\n for (int line = 0; line < 2; line++) {\n std::string s = inf.readLine();\n ensuref((int)s.size() == N, \"test %d line %d: length %d expected %d\", tc, line+1, (int)s.size(), N);\n for (char c : s)\n ensuref(c == 'J' || c == 'N', \"test %d line %d: char must be J/N, got '%c'\", tc, line+1, c);\n }\n }\n ensuref(sumN <= 500000, \"sum N is %lld > 5e5\", sumN);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing vi = vector<int>;\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define all(x) x.begin(), x.end()\n#define sz(x) int(x.size())\n#define nl '\\n'\n \nint main() {\n ios::sync_with_stdio(0); cin.tie(0);\n int t, c; cin >> t >> c;\n while (t--) {\n int n; cin >> n;\n string a, b; cin >> a >> b;\n vector<vector<array<int, 2>>> g(2);\n int cnt[2][2] = {};\n FOR(i, 0, n) {\n int u = a[i] == 'N', v = b[i] == 'N';\n g[u].push_back({v, i});\n cnt[u][v]++;\n }\n bool ans = count(all(a), 'J') == count(all(b), 'J')\n and count(all(a), 'N') % 2 == 0\n and (!cnt[0][0] or !cnt[1][1] or cnt[0][1] or cnt[1][0]);\n if (ans) cout << \"YES\" << nl;\n else cout << \"NO\" << nl;\n if (!c or !ans) continue;\n int st = sz(g[0]) ? 0 : 1;\n vi p;\n auto dfs = [&] (auto dfs, int u) -> void {\n while (sz(g[u])) {\n auto [v, i] = g[u].back();\n g[u].pop_back();\n dfs(dfs, v);\n p.push_back(i);\n }\n };\n dfs(dfs, st);\n reverse(all(p));\n FOR(i, 0, n) cout << p[i] + 1 << \" \\n\"[i == n - 1];\n int l = 0;\n cout << \"J\";\n FOR(i, 1, n) {\n if (a[p[i]] == 'N') l = 1 - l;\n cout << \"JN\"[l];\n }\n cout << nl;\n }\n}", "chk_cpp": "#include \"testlib.h\"\n#include <string>\n#include <vector>\nusing namespace std;\n\nint main(int argc, char *argv[]) {\n setName(\"USACO 2026 Silver: Cow-libi 2\");\n registerTestlibCmd(argc, argv);\n\n int T = inf.readInt();\n int C = inf.readInt();\n\n for (int t = 1; t <= T; t++) {\n int N = inf.readInt();\n string L = inf.readToken();\n string R = inf.readToken();\n if ((int)L.size() != N || (int)R.size() != N)\n quitf(_fail, \"judge input malformed at test %d\", t);\n\n string ansVerdict = ans.readToken();\n string oufVerdict = ouf.readToken();\n if (ansVerdict != \"YES\" && ansVerdict != \"NO\")\n quitf(_fail, \"reference verdict bad: %s\", ansVerdict.c_str());\n if (oufVerdict != \"YES\" && oufVerdict != \"NO\")\n quitf(_wa, \"test %d: contestant verdict must be YES or NO, got '%s'\", t, oufVerdict.c_str());\n if (ansVerdict != oufVerdict)\n quitf(_wa, \"test %d: verdict differs (expected %s, got %s)\", t, ansVerdict.c_str(), oufVerdict.c_str());\n\n if (C == 1 && ansVerdict == \"YES\") {\n // Read reference's construction (just to keep ans in sync)\n for (int i = 0; i < N; i++) ans.readInt();\n ans.readToken();\n\n // Read contestant's permutation p[0..N-1] and assignment b\n vector<int> p(N);\n vector<char> seen(N + 1, 0);\n for (int i = 0; i < N; i++) {\n p[i] = ouf.readInt(1, N, \"p_i\");\n if (seen[p[i]])\n quitf(_wa, \"test %d: cow %d appears twice in permutation\", t, p[i]);\n seen[p[i]] = 1;\n }\n string b = ouf.readToken();\n if ((int)b.size() != N)\n quitf(_wa, \"test %d: assignment string length %d, expected %d\", t, (int)b.size(), N);\n for (char ch : b)\n if (ch != 'J' && ch != 'N')\n quitf(_wa, \"test %d: assignment string contains invalid char '%c'\", t, ch);\n\n for (int i = 0; i < N; i++) {\n int v = p[i];\n int li = (i - 1 + N) % N;\n int ri = (i + 1) % N;\n char self_f = b[i];\n char left_f = b[li];\n char right_f = b[ri];\n char claim_l = L[v - 1];\n char claim_r = R[v - 1];\n if (self_f == 'J') {\n if (claim_l != left_f)\n quitf(_wa, \"test %d: cow %d (J at pos %d) claims left=%c but left actual=%c\",\n t, v, i + 1, claim_l, left_f);\n if (claim_r != right_f)\n quitf(_wa, \"test %d: cow %d (J at pos %d) claims right=%c but right actual=%c\",\n t, v, i + 1, claim_r, right_f);\n } else { // 'N' lies\n if (claim_l == left_f)\n quitf(_wa, \"test %d: cow %d (N at pos %d) claims left=%c matches actual %c (must lie)\",\n t, v, i + 1, claim_l, left_f);\n if (claim_r == right_f)\n quitf(_wa, \"test %d: cow %d (N at pos %d) claims right=%c matches actual %c (must lie)\",\n t, v, i + 1, claim_r, right_f);\n }\n }\n }\n }\n\n if (!ouf.seekEof()) quitf(_wa, \"extra tokens in contestant output\");\n quitf(_ok, \"T=%d cases, C=%d\", T, C);\n}\n", "std_source_submission_id": 70971, "std_origin": "verbatim earliest root score=100 submission #70971"}
{"cpid": 1567, "title": "Declining Invitations", "contest": "Second Contest", "division": "Silver", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1567", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\n$N$ contestants participated in a contest, each placing a distinct rank from $1$ to $N$. There are $C$ criteria used to invite contestants to participate in the final round, and the $i$-th-ranked contestant satisfies a specified $n_i$ of them ($1\\le n_i\\le C$).\n\nThe invitation process runs as follows. First, the top $f_1$ students who satisfy the $1$st criteria will be invited. Then, out of all students who haven't yet been invited, the top $f_2$ (or all remaining if there are fewer than $f_2$ remaining) students who satisfy the $2$nd criterion will be invited. This process repeats, for each $i$ from $1$ to $C$ ($1\\le f_i\\le N$).\n\nHowever, some contestants will decline to participate in the final round, in which case they will be ignored when determining who to invite.\n\nYou are given a permutation $p_1, p_2, \\dots, p_N$ of $1\\dots N$. For each $i$ from $0$ to $N - 1$, determine the sum of the ranks of the contestants who will be invited if the participants with ranks given by the first $i$ elements of $p$ decline to attend.\n\n## Input Format\n\nThe first line contains $N$ and $C$ ($1\\le N, C\\le 10^5$).\n\nThe next line contains $f_1, f_2, \\dots, f_C$.\n\nThe next line contains $p_1, \\dots, p_N$.\n\nThe next $N$ lines each contain $n_i$ ($1\\le n_i\\le C$), followed by $n_i$ distinct integers in $[1, C]$, representing the criteria that the $i$-th-ranked contestant satisfies. It is guaranteed that $\\sum n_i\\le 10^6$.\n\n## Output Format\n\nOutput $N$ lines, the sum of ranks of invitees before each declination.\n\n## Examples\n\n**Input 1**\n\n```\n5 1\n3\n5 1 3 2 4\n1 1\n1 1\n1 1\n1 1\n1 1\n```\n\n**Output 1**\n\n```\n6\n6\n9\n6\n4\n```\n\n**Input 2**\n\n```\n5 4\n1 1 1 1\n1 2 3 4 5\n1 1\n2 1 2\n2 2 3\n2 3 4\n1 4\n```\n\n**Output 2**\n\n```\n10\n14\n12\n9\n5\n```\n\n**Input 3**\n\n```\n6 10\n5 6 4 1 3 3 3 6 5 3\n1 4 6 5 2 3\n1 9\n5 4 3 9 5 10\n10 6 2 10 1 7 8 3 9 4 5\n10 4 5 3 1 2 9 10 6 7 8\n2 3 1\n8 1 9 7 4 3 10 6 2\n```\n\n**Output 3**\n\n```\n21\n20\n16\n10\n5\n3\n```\n\n## Note\n\nFor the first example, there is only one criterion. The top three remaining contestants who have not declined will be invited.\n\nFor the second example, initially, the $i$-th contestant gets invited under criterion $i$ for all $1\\le i\\le 4$. After the first declination, the $(i+1)$-th contestant gets invited under criterion $i$ for all $1\\le i\\le 4$.\n\n## Scoring\n\n- Inputs 4-6: $N, C\\le 10^3$, $\\sum n_i\\le 10^4$\n- Inputs 7-8: $C = 1$\n- Inputs 9-10: $C = 2$\n- Inputs 11-16: No additional constraints\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 13, "n_sample_tests": 3, "point_scores": [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 16], "val_cpp": "#include \"testlib.h\"\n#include <set>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(1, 100000, \"N\"); inf.readSpace();\n int C = inf.readInt(1, 100000, \"C\"); inf.readEoln();\n for (int i = 1; i <= C; i++) { inf.readInt(1, N, \"f_i\"); if (i < C) inf.readSpace(); }\n inf.readEoln();\n // permutation p of 1..N\n std::set<int> seen;\n for (int i = 1; i <= N; i++) {\n int p = inf.readInt(1, N, \"p_i\");\n ensuref(seen.insert(p).second, \"p is not a permutation: %d repeats\", p);\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n long long sumn = 0;\n for (int i = 1; i <= N; i++) {\n int ni = inf.readInt(1, C, \"n_i\");\n sumn += ni;\n std::set<int> cs;\n for (int j = 1; j <= ni; j++) {\n inf.readSpace();\n int c = inf.readInt(1, C, \"criterion\");\n ensuref(cs.insert(c).second, \"row %d: criterion %d repeats\", i, c);\n }\n inf.readEoln();\n }\n ensuref(sumn <= 1000000, \"sum n_i is %lld > 1e6\", sumn);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <class T> using V = vector<T>;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int N, C;\n cin >> N >> C;\n V<int> f(C);\n for (int &t : f) cin >> t;\n V<int> p(N);\n for (int &t : p) {\n cin >> t;\n --t; // zero-index\n }\n V<queue<int>> q(C);\n V<int> invited_under(N, C);\n // zero-indexed criterion that each contestant is invited under\n // also, C = not invited, -1 = declined\n\n for (int i = 0; i < N; ++i) {\n int n;\n cin >> n;\n for (int j = 0; j < n; ++j) {\n int c;\n cin >> c;\n --c;\n q.at(c).push(i);\n }\n }\n\n // initial invitations\n int64_t ans = 0;\n for (int c = 0; c < C; ++c) {\n int invited_under_c = 0;\n while (size(q.at(c)) && invited_under_c < f.at(c)) {\n int x = q.at(c).front();\n q.at(c).pop();\n if (invited_under.at(x) < c) continue;\n invited_under.at(x) = c;\n ans += x + 1;\n ++invited_under_c;\n }\n }\n\n // process declinations one at a time\n for (int d : p) {\n cout << ans << \"\\n\";\n int c = invited_under.at(d);\n invited_under.at(d) = -1;\n if (c == C) continue;\n ans -= d + 1;\n while (size(q.at(c))) {\n int x = q.at(c).front();\n q.at(c).pop();\n if (invited_under.at(x) < c) continue;\n assert(invited_under.at(x) > c);\n swap(c, invited_under.at(x));\n if (c == C) {\n ans += x + 1;\n break;\n }\n }\n }\n}", "chk_cpp": null, "std_source_submission_id": 70943, "std_origin": "verbatim earliest root score=100 submission #70943"}
{"cpid": 1568, "title": "Farmer John Loves Rotations", "contest": "Second Contest", "division": "Silver", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1568", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John has an array $A$ containing $N$ integers ($1\\le N\\le 5\\cdot 10^5$, $1\\le A_i\\le N$). He picks his favorite index $j$ and takes out a sheet of paper with only $A_j$ written on it. He can then perform the following operation some number of times:\n\n> Cyclically shift all elements in $A$ one spot to the left or one spot to the right. Then, write down $A_j$ on a piece of paper.\n\nLet $S$ denote the set of distinct integers that occur in $A$. Farmer John wonders what the minimum number of operations he must perform is so that the paper contains all integers that appear in $S$.\n\nSince it is unclear what FJ's favorite index is, output the answer for all possible favorite indices $1\\le j\\le N$. Note for each index, $A$ is reset to its original form before performing any operations.\n\n## Input Format\n\nThe first line contains $N$.\n\nThe following line contains $A_1, A_2, \\ldots, A_N$.\n\n## Output Format\n\nOutput $N$ space-separated integers, where the $i$-th integer is the answer for his favorite index $j = i$.\n\n## Examples\n\n**Input 1**\n\n```\n6\n1 2 3 1 3 4\n```\n\n**Output 1**\n\n```\n4 3 3 4 3 3\n```\n\n**Input 2**\n\n```\n12\n1 1 2 1 1 3 1 1 4 1 1 1\n```\n\n**Output 2**\n\n```\n8 7 6 7 8 9 8 7 6 7 8 9\n```\n\n## Note\n\nFor the first example, the distinct numbers are $S = \\{1, 2, 3, 4\\}$. Suppose Farmer John's favorite index is $j = 1$. He starts off with $A_1 = 1$ written on a piece of paper. We can track the array $A$ after each cyclic shift Farmer John makes.\n\n- Cyclic shift right: FJ writes down $A_1 = 4$. Array: `4 1 2 3 1 3`.\n- Cyclic shift left: FJ writes down $A_1 = 1$ again. Array: `1 2 3 1 3 4`.\n- Cyclic shift left: FJ writes down $A_1 = 2$. Array: `2 3 1 3 4 1`.\n- Cyclic shift left: FJ writes down $A_1 = 3$. Array: `3 1 3 4 1 2`.\n\nAt this point, Farmer John has written down every number in $S$ using $4$ operations.\n\n## Scoring\n\n- Inputs 3-5: $N\\le 500$\n- Inputs 6-8: $N\\le 10^4$\n- Inputs 9-17: No additional constraints\n\n**Problem credits:** Chongtian Ma", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 15, "n_sample_tests": 2, "point_scores": [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 16], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(1, 500000, \"N\"); inf.readEoln();\n for (int i = 1; i <= N; i++) { inf.readInt(1, N, \"A_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <class T> using V = vector<T>;\n#define all(x) begin(x), end(x)\n\nvoid ckmin(int &a, int b) { a = min(a, b); }\nvoid ckmax(int &a, int b) { a = max(a, b); }\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int N;\n cin >> N;\n V<int> A(N);\n for (int &t : A) cin >> t;\n\n V<int> lst(N + 1, -1), nxt(N, -1);\n V<int> ans(N, N);\n auto add_ival = [&](int l, int r) {\n assert(l <= r);\n int len = r - l;\n ckmin(ans.at(l), len);\n ckmin(ans.at(r % N), len);\n };\n int r = 0;\n for (int i = 0; i < 2 * N; ++i) {\n int &ls = lst.at(A.at(i % N));\n if (ls == -1) {\n ckmax(r, i);\n } else if (ls < N) nxt.at(ls) = i;\n ls = i;\n }\n for (int i = 0; i < N; ++i) {\n add_ival(i, r);\n assert(nxt.at(i) != -1);\n ckmax(r, nxt.at(i));\n }\n for (int it = 0; it < 2; ++it) {\n for (int i = 0; i < N; ++i) ckmin(ans.at((i + 1) % N), ans.at(i) + 1);\n for (int i = N - 1; i >= 0; --i)\n ckmin(ans.at(i), ans.at((i + 1) % N) + 1);\n }\n for (int i = 0; i < N; ++i) {\n if (i) cout << \" \";\n cout << ans[i];\n }\n cout << \"\\n\";\n}", "chk_cpp": null, "std_source_submission_id": 70944, "std_origin": "verbatim earliest root score=100 submission #70944"}
{"cpid": 1569, "title": "Balancing the Barns", "contest": "Second Contest", "division": "Gold", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1569", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John has $N$ ($1\\le N\\le 5\\cdot 10^4$) barns arranged along a road. The $i$-th barn contains $a_i$ bales of hay and $b_i$ bags of feed ($0\\le a_i, b_i\\le 10^9$).\n\nBessie has been complaining about the inequality between barns. She defines the \"imbalance\" of the farm as the difference between the maximum hay in any barn and the minimum feed in any barn. Formally, the imbalance is $\\max(a) - \\min(b)$.\n\nTo address Bessie's concerns, Farmer John can perform exactly $K$ ($1\\le K\\le 10^{18}$) transfers. In each transfer, he selects a barn $i$, sells one of its haybales, and buys it a new bag of feed for the same barn. Note that there can be negative amounts in his farm (he is not afraid of debt). Formally, $K$ times, you choose an index $i\\in [1, N]$, decrement $a_i$, and increment $b_i$.\n\nHelp Farmer John determine the minimum possible imbalance after performing exactly $K$ transfers.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 10^3$), the number of independent test cases.\n\nThe first line of each test case contains $N$ and $K$.\n\nThe following line contains $a_1\\dots a_N$.\n\nThe following line contains $b_1\\dots b_N$.\n\nThe sum of $N$ over all test cases is at most $5\\cdot 10^4$.\n\n## Output Format\n\nFor each test case, output a single integer, the minimum possible value of $\\max(a) - \\min(b)$ after performing $K$ operations.\n\n## Examples\n\n**Input 1**\n\n```\n4\n1 10\n5\n3\n2 6\n100 96\n0 4\n3 3\n1 1 2\n0 0 1\n3 3\n1 2 2\n0 1 1\n```\n\n**Output 1**\n\n```\n-18\n90\n0\n0\n```\n\n## Note\n\nIn the first test case, Farmer John can transfer $10$ haybales from barn $1$ into bags of feed. This leaves $a = [-5]$ and $b = [13]$. The imbalance is $\\max(a) - \\min(b) = -5 - 13 = -18$.\n\nIn the second test case, Farmer John can transfer $5$ haybales from barn $1$ and $1$ haybale from barn $2$. This leaves $a = [95, 95]$ and $b = [5, 5]$. The imbalance is $95 - 5 = 90$. This is the minimum imbalance Farmer John can achieve.\n\n## Scoring\n\n- Inputs 2-4: $K\\le 500$, sum of $N$ over all test cases is $\\le 500$\n- Inputs 5-8: Sum of $N$ over all test cases is $\\le 500$\n- Inputs 9-13: No additional constraints\n\n**Problem credits:** Rohin Garg", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 12, "n_sample_tests": 1, "point_scores": [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 1000, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 50000, \"N\"); inf.readSpace();\n inf.readLong(1, 1000000000000000000LL, \"K\"); inf.readEoln();\n sumN += N;\n for (int i = 1; i <= N; i++) { inf.readInt(0, 1000000000, \"a_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n for (int i = 1; i <= N; i++) { inf.readInt(0, 1000000000, \"b_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n }\n ensuref(sumN <= 50000, \"sum N is %lld > 5e4\", sumN);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <algorithm>\n#include <array>\n#include <iomanip>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid solve() {\n int n;\n int64_t k;\n cin >> n >> k;\n vector<int64_t> a(n), b(n);\n for(auto& x: a) cin >> x;\n for(auto& x: b) cin >> x;\n auto can = [&](int64_t thresh) -> bool {\n vector<int64_t> na = a, nb = b;\n int64_t have = k;\n for(int i = 0; i < n; i++) {\n if(na[i] - nb[i] > thresh) {\n int64_t use = (na[i] - nb[i] - thresh + 1) / 2;\n if(use > have) return false;\n na[i] -= use;\n nb[i] += use;\n have -= use;\n }\n }\n if(have < 0) return false;\n int64_t lhs = *min_element(nb.begin(), nb.end());\n int64_t abound = lhs + thresh;\n __int128_t need = 0;\n vector<int64_t> xincs;\n int64_t lastx = lhs;\n int64_t inc = 0;\n for(int i = 0; i < n; i++) {\n xincs.push_back(nb[i]);\n if(na[i] > abound) {\n need += na[i] - abound;\n inc--;\n xincs.push_back(na[i]-thresh);\n }\n }\n if(need <= have) return true;\n sort(xincs.begin(), xincs.end());\n for(int i = 0; i < xincs.size() && inc < 0; i++) {\n auto currx = xincs[i];\n need += (currx - lastx) * __int128_t(inc);\n if(need <= have) return true;\n inc++;\n lastx = currx;\n }\n return false;\n };\n int64_t lhs = -2.1e18;\n int64_t rhs = 2.1e18;\n while(lhs < rhs) {\n int64_t mid = lhs + (rhs - lhs) / 2;\n if(can(mid)) rhs = mid;\n else lhs = mid + 1;\n }\n cout << lhs << \"\\n\";\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int t;\n cin >> t;\n while(t--) solve();\n}", "chk_cpp": null, "std_source_submission_id": 70939, "std_origin": "verbatim earliest root score=100 submission #70939"}
{"cpid": 1570, "title": "Lexicographically Smallest Path", "contest": "Second Contest", "division": "Gold", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1570", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie is given an undirected graph with $N$ ($1\\le N\\le 2\\cdot 10^5$) vertices labeled $1\\dots N$ and $M$ edges ($N - 1\\le M\\le 2\\cdot 10^5$). Each edge is described by two integers $u, v$ ($1\\le u, v\\le N$) describing an undirected edge between nodes $u$ and $v$ and a lowercase Latin letter $c$ in the range a..z that is the value on the edge. The graph given is guaranteed to be connected. There may be multiple edges or self-loops.\n\nDefine $f(a, b)$ as the lexicographically smallest concatenation of edge values over all paths starting from node $a$ and ending at node $b$. A path may contain the same edge more than once (i.e., cycles are allowed).\n\nFor each $i$ ($1\\le i\\le N$), help Bessie determine the length of $f(1, i)$. Output this length if it is finite, otherwise output $-1$.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 10$), the number of independent tests. Each test is specified in the following format:\n\nThe first line contains $N$ and $M$.\n\nThe next $M$ lines each contain two integers followed by a lowercase Latin character.\n\nIt is guaranteed that neither the sum of $N$ nor the sum of $M$ over all tests exceeds $4\\cdot 10^5$.\n\n## Output Format\n\nFor each test, output $N$ space-separated integers on a new line.\n\n## Examples\n\n**Input 1**\n\n```\n2\n1 0\n2 2\n1 1 a\n2 1 b\n```\n\n**Output 1**\n\n```\n0\n0 -1\n```\n\n**Input 2**\n\n```\n2\n7 7\n1 2 a\n1 3 a\n2 4 b\n3 5 a\n5 6 a\n6 7 a\n7 4 a\n4 3\n1 2 z\n2 3 x\n3 4 y\n```\n\n**Output 2**\n\n```\n0 1 1 5 2 3 4\n0 1 2 -1\n```\n\n## Note\n\nFor the first test case of the first example, node $1$ can be reached with an empty path, so the answer is $0$. In the second test case, node $2$ cannot have a lexicographically smallest path, since FJ can repeat the `'a'` self-loop any number of times before moving to node $2$, producing arbitrarily long strings that are still lexicographically minimal. Therefore, the answer for node $2$ is $-1$.\n\nFor the first test case of the second example, node $1$ has distance $0$. Nodes $2$ and $3$ are adjacent with node $1$, and they have distance $1$. For nodes $4, 5, 6$, and $7$, it can be proven that the lexicographically shortest path does not pass through the edge between node $2$ and $4$.\n\nFor the second test case of the second example, node $4$ again has no lexicographically smallest path, since the string can be extended indefinitely while remaining lexicographically minimal. Thus, its answer is $-1$.\n\n## Scoring\n\n- Inputs 3-4: Every character is `a`\n- Inputs 5-8: Every character is `a` or `b`\n- Inputs 9-14: $N, M\\le 5000$\n- Inputs 15-22: No additional constraints\n\n**Problem credits:** Daniel Zhu and Yash Belani", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 2, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10, \"T\"); inf.readEoln();\n long long sumN = 0, sumM = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 400000, \"N\"); inf.readSpace();\n int M = inf.readInt(std::max(0, N - 1), 400000, \"M\"); inf.readEoln();\n sumN += N; sumM += M;\n for (int e = 1; e <= M; e++) {\n inf.readInt(1, N, \"u\"); inf.readSpace();\n inf.readInt(1, N, \"v\"); inf.readSpace();\n char c = inf.readChar();\n ensuref(c >= 'a' && c <= 'z', \"test %d edge %d: value must be a-z, got '%c'\", tc, e, c);\n inf.readEoln();\n }\n }\n ensuref(sumN <= 400000, \"sum N is %lld > 4e5\", sumN);\n ensuref(sumM <= 400000, \"sum M is %lld > 4e5\", sumM);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include \"bits/stdc++.h\"\n#define endl '\\n'\n#define f first\n#define s second\nusing namespace std;\n \nconst int N = 2e5 + 5, A = 28;\nint n, m;\nvector<pair<int, int>> g[N];\nint ans[N];\n \nvoid ac() {\n cin >> n >> m;\n for (int i = 1; i <= n; i++) ans[i] = -1, g[i].clear();\n for (int i = 1; i <= m; i++) {\n int a, b;\n char x;\n cin >> a >> b >> x;\n int c = (x - 'a'); // converting characters to integers\n g[a].push_back({c, b});\n g[b].push_back({c, a});\n }\n ans[1] = 0;\n vector<int> active;\n active.push_back(1);\n while (active.size()) {\n int min_edge = A;\n for (int i : active) { // finding min edge to take\n for (auto j : g[i]) min_edge = min(min_edge, j.f);\n }\n vector<int> next_active;\n for (int i : active) {\n for (auto j : g[i]) {\n if (ans[j.s] == -1 && j.f == min_edge) { // extending string\n ans[j.s] = ans[i] + 1;\n next_active.push_back(j.s);\n }\n }\n }\n swap(next_active, active);\n }\n for (int i = 1; i <= n; i++) {\n if (i > 1) cout << \" \";\n cout << ans[i];\n }\n cout << \"\\n\";\n}\n \nint main() {\n int t;\n cin >> t;\n while (t--) ac();\n}", "chk_cpp": null, "std_source_submission_id": 70940, "std_origin": "verbatim earliest root score=100 submission #70940"}
{"cpid": 1571, "title": "The Chase", "contest": "Second Contest", "division": "Gold", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1571", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie is trying to evade the farmers. The farmers own $N$ ($2\\le N\\le 5\\cdot 10^5$) farms with a one-way road between the $i$-th farm and the $a_i$-th farm ($1\\le i\\le N$, $a_i\\ne i$). There are $F$ ($1\\le F\\le N$) farmers and the $i$-th farmer is initially stationed at farm $s_i$ ($1\\le s_i\\le N$, all $s_i$ unique). At each time step, every farmer takes the road at their current farm and moves to the next. Bessie gets caught if she is ever located at the same farm as any farmer.\n\nSuppose Bessie starts at some farm $b$. At each time step, she has two options: she can either choose to rest (stay at her current farm) or take the road and move to the next farm. If she chooses to move, she moves simultaneously with the farmers. Bessie moves so that she is never caught by any farmer in a finite number of timesteps.\n\nFor each starting farm $b$ ($1\\le b\\le N$), find the maximum number of times Bessie can choose the rest option if she starts at farm $b$.\n\n## Input Format\n\nThe first line contains $N$ and $F$, the number of farms and the number of farmers.\n\nThe second line contains $a_1\\ldots a_N$, the one-way roads going out of each farm.\n\nThe third line contains $s_1\\ldots s_F$, the starting locations of each farmer.\n\n## Output Format\n\nOutput $N$ lines, the $b$-th of which must consist of a single integer denoting the maximum number of times Bessie can choose the rest option if she starts at farm $b$. If there is no way for Bessie to ensure she is never caught after a finite number of timesteps, then output $-1$. If Bessie can rest an infinite number of times, then output $-2$.\n\n## Examples\n\n**Input 1**\n\n```\n4 1\n2 1 4 3\n1\n```\n\n**Output 1**\n\n```\n-1\n0\n-2\n-2\n```\n\n## Note\n\n- Farm 1: If Bessie starts at a farm with a farmer, then she is caught immediately, and you should output $-1$.\n- Farm 2: Bessie must choose to move at every timestep to avoid being caught by the farmer who starts at farm $1$.\n- Farms 3-4: Bessie can rest an infinite number of times without being caught.\n\n## Scoring\n\n- Input 2: $N\\le 50$\n- Inputs 3-10: $N\\le 2000$\n- Inputs 11-20: No additional constraints\n\n**Problem credits:** Alex Liang", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 1, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\n#include <set>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(2, 500000, \"N\"); inf.readSpace();\n int F = inf.readInt(1, N, \"F\"); inf.readEoln();\n for (int i = 1; i <= N; i++) {\n inf.readInt(1, N, \"a_i\");\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n std::set<int> seen;\n for (int i = 1; i <= F; i++) {\n int s = inf.readInt(1, N, \"s_i\");\n ensuref(seen.insert(s).second, \"farmer start %d duplicated\", s);\n if (i < F) inf.readSpace();\n }\n inf.readEoln();\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n \nint main() {\n ios_base::sync_with_stdio(0); cin.tie(0);\n int n, f;\n cin >> n >> f;\n \n vector<int> nxt(n + 1), hasFarmer(n + 1, 0);\n vector<vector<int>> radj(n + 1);\n \n for (int i = 1; i <= n; i++) {\n cin >> nxt[i];\n radj[nxt[i]].push_back(i);\n }\n for (int i = 1; i <= f; i++) {\n int s;\n cin >> s;\n hasFarmer[s] = 1;\n }\n \n vector<int> vis(n + 1, 0), close(n + 1, (int)1e9), ans(n + 1);\n \n for (int start = 1; start <= n; start++) {\n if (vis[start])\n continue;\n \n // Get cycle\n int cur = start;\n vector<int> cycle;\n \n while (vis[cur] != 2) {\n if (++vis[cur] == 2)\n cycle.push_back(cur);\n cur = nxt[cur];\n }\n \n // Get tree info\n int csz = cycle.size();\n vector<int> good(csz, 1), chop(csz, 1e9);\n vector<pair<int, int>> relevant;\n \n for (int i = 0; i < csz; i++) {\n function<void(int, int)> dfs = [&](int cur, int d){\n int pos = (i - d % csz + csz) % csz;\n vis[cur] = 1;\n relevant.push_back({cur, pos});\n \n if (hasFarmer[cur]) {\n good[pos] = 0;\n close[cur] = 0;\n }\n for (int to : radj[cur]) {\n if (cur == cycle[i] && to == cycle[(i - 1 + csz) % csz])\n continue;\n dfs(to, d + 1);\n close[cur] = min(close[cur], close[to] + 1);\n }\n };\n dfs(cycle[i], 0);\n }\n // Get distances to nearest good waiting spot\n for (int i = 2 * csz - 1; i >= 0; i--) \n chop[i % csz] = good[i % csz] ? 0 : chop[(i + 1) % csz] + 1;\n \n // Adjust close for cycle nodes\n for (int i = 0; i < 2 * csz - 1; i++)\n close[cycle[i % csz]] = min(close[cycle[i % csz]], close[cycle[(i - 1 + csz) % csz]] + 1);\n \n // Solve for each node\n for (auto [cur, st] : relevant) {\n if (close[cur] >= (int)1e8) {\n ans[cur] = -2;\n continue;\n }\n \n int ret = close[cur] - 1 - chop[(st - close[cur] % csz + 1 + csz) % csz];\n ans[cur] = ret >= 0 ? ret : -1;\n }\n }\n for (int i = 1; i <= n; i++)\n cout << ans[i] << \"\\n\";\n}", "chk_cpp": null, "std_source_submission_id": 70941, "std_origin": "verbatim earliest root score=100 submission #70941"}
{"cpid": 1572, "title": "Circle of Cows", "contest": "Second Contest", "division": "Platinum", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1572", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John has $N$ ($2\\le N\\le 1000$) cows at distinct locations $l_1, \\dots, l_N$ along a circle of circumference $C$ ($0\\le l_1 < l_2 < \\dots < l_N < C$, $N\\le C\\le 10^9$).\n\nFJ will select $k$ pairs of cows, where $1\\le k\\le \\lfloor N/2\\rfloor$, and no cow is selected more than once. He wants to select the pairs such that the minimum distance between any two cows in the same pair along the circumference of the circle is maximized.\n\nFor each value of $k$, help FJ determine the maximum possible minimum distance.\n\n## Input Format\n\nThe first line contains $N$ and $C$.\n\nThe second line contains $l_1 \\dots l_N$.\n\n## Output Format\n\nOutput a single line with $\\lfloor N/2\\rfloor$ space-separated integers, with the answers for $k = 1\\dots \\lfloor N/2\\rfloor$ in that order.\n\n## Examples\n\n**Input 1**\n\n```\n4 100\n0 25 50 75\n```\n\n**Output 1**\n\n```\n50 50\n```\n\n**Input 2**\n\n```\n4 100\n0 1 2 99\n```\n\n**Output 2**\n\n```\n3 2\n```\n\n## Note\n\nFor the first example:\n\n- For $k = 1$, cow $1$ can be paired to cow $3$, which is distance $50$ away along the circumference of the circle, making the answer $50$.\n- For $k = 2$, cow $1$ can be paired to cow $3$, and cow $2$ can be paired to cow $4$, which is distance $50$ away from it along the circumference of the circle, making the answer still $50$.\n\nFor the second example:\n\n- For $k = 1$, cow $3$ can be paired to cow $4$, which is distance $2 + 100 - 99 = 3$ away from it along the circumference of the circle, making the answer $3$.\n- For $k = 2$, cow $1$ can be paired to cow $3$ and cow $2$ can be paired to cow $4$. Each of these pairs contains two cows at a distance of $2$ from each other along the circumference of the circle, making the answer $2$.\n\n## Scoring\n\n- Inputs 3-4: $2 l_N\\le C$\n- Inputs 5-6: $N\\le 20$\n- Inputs 7-14: $N\\le 100$\n- Inputs 15-22: No additional constraints\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 2, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(2, 1000, \"N\");\n inf.readSpace();\n int C = inf.readInt(N, 1000000000, \"C\");\n inf.readEoln();\n long long prev = -1;\n for (int i = 1; i <= N; i++) {\n int l = inf.readInt(0, C - 1, \"l_i\");\n ensuref(l > prev, \"l must be strictly increasing (index %d: %d <= %lld)\", i, l, prev);\n prev = l;\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std; \n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0); \n\n int N, C; \n cin >> N >> C; \n vector<int> L(N);\n for (auto & x: L) {\n cin >> x; \n }\n\n vector<int> ans(N / 2 + 1);\n \n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n \n int i2 = (i + 1) % N, j2 = (j + 1) % N; \n int min_dist = min(L.at(j) - L.at(i), C - (L.at(j) - L.at(i))); \n int cnt = 1; \n\n while (i2 != j && j2 != i) {\n // distance from L[i2] to L[j2] in the positive direction\n int cur_dist = i2 < j2 ? L.at(j2) - L.at(i2) : C - (L.at(i2) - L.at(j2)); \n if (cur_dist < min_dist) {\n j2 = (j2 + 1 == N ? 0 : j2 + 1); \n } else if (C - cur_dist < min_dist) {\n i2 = (i2 + 1 == N ? 0 : i2 + 1); \n } else {\n cnt++; \n i2 = (i2 + 1 == N ? 0 : i2 + 1); \n j2 = (j2 + 1 == N ? 0 : j2 + 1); \n }\n }\n ans.at(cnt) = max(ans.at(cnt), min_dist); \n }\n }\n\n ans.erase(ans.begin()); \n for (int i = (int)ans.size() - 2; i >= 0; --i) {\n ans.at(i) = max(ans.at(i), ans.at(i + 1)); \n }\n for (int i = 0; i < N / 2; ++i) {\n cout << ans[i] << \" \\n\"[i + 1 == N / 2]; \n }\n}", "chk_cpp": null, "std_source_submission_id": 70936, "std_origin": "verbatim earliest root score=100 submission #70936"}
{"cpid": 1573, "title": "Cow Circle", "contest": "Second Contest", "division": "Platinum", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1573", "statement_md": "**Time limit:** 6 seconds **Memory limit:** 512 megabytes\n\n> **Note:** The time limit for this problem is 6s, thrice the default. The memory limit for this problem is 512 MB, twice the default.\n\n## Description\n\nFarmer John has $N$ ($1\\le N\\le 5000$) cows standing around a circular track divided into $M$ ($1\\le M\\le 10^6$) equally spaced positions, numbered $0$ to $M - 1$ clockwise. Cow $i$ is initially at position $x_i$, where $0 = x_1 < x_2 < \\dots < x_N < M$.\n\nFor each $1\\le i\\le N$, cow $i$ will independently and randomly choose either to face clockwise or counterclockwise with some probability specific to that cow. Once a cow has chosen her initial direction, she begins moving continuously in that direction at a constant speed of one position per minute. Whenever two cows meet (i.e., they occupy the same space), they bounce off of each other: immediately reversing their directions and continuing to move at the same speed in that direction.\n\nFarmer John is wondering where cow $1$ will end up. For each $0\\le i < M$, find the probability that cow $1$ is at position $i$ after $K$ ($1\\le K\\le 10^{18}$) minutes.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 100$), the number of independent test cases. Each test case is specified as follows:\n\nThe first line of each test case contains $N$ ($1\\le N\\le 5000$), $M$ ($1\\le M\\le 10^6$), and $K$ ($1\\le K\\le 10^{18}$).\n\nThe second line contains $N$ integers $p_1, \\dots, p_N$ ($0\\le p_i < 10^9 + 7$) where if $\\frac{a_i}{b_i}$ is the probability cow $i$ goes clockwise, then $p_i\\cdot b_i\\equiv a_i\\pmod{10^9 + 7}$.\n\nThe third and final line contains $N$ integers $x_1, x_2, \\dots, x_N$.\n\nIt is guaranteed that the sum of $N^2$ over all test cases is $\\le 5000^2$ and the sum of $M$ over all test cases is $\\le 10^6$.\n\n## Output Format\n\nOutput a new line for each test case. The line for each test case should be formatted as follows:\n\nFor every $0\\le i < M$, let $\\frac{p_i}{q_i}$ be the probability cow $1$ is in position $i$ at the end of $K$ minutes. Output $M$ space-separated integers $p_i q_i^{-1}\\pmod{10^9 + 7}$ (where $p_i q_i^{-1}\\cdot q_i\\equiv p_i\\pmod{10^9 + 7}$).\n\n## Examples\n\n**Input 1**\n\n```\n3\n2 2 1\n500000004 500000004\n0 1\n3 3 1\n500000004 500000004 500000004\n0 1 2\n5 10 13\n500000004 1 500000004 0 500000004\n0 3 4 7 9\n```\n\n**Output 1**\n\n```\n500000004 500000004\n500000004 250000002 250000002\n0 0 0 125000001 375000003 0 125000001 375000003 0 0\n```\n\n## Note\n\nFor the first test case, both cows have a $\\frac{1}{2}$ chance of going in either direction. If both pick the same direction, they will end up swapping positions (so cow $1$ ends up at $1$). Otherwise, they will bounce off in the middle and return to their original positions. Therefore, there is a $\\frac{1}{2}$ chance for cow $1$ to end up at $0$ and a $\\frac{1}{2}$ chance for cow $1$ to end up at $1$.\n\nFor the second test case, all cows again have a $\\frac{1}{2}$ chance of going in either direction. For each combination of directions, here is where cow $1$ ends up at:\n\n- CW, CW, CW: $1$\n- CW, CW, CCW: $1$\n- CCW, CCW, CCW: $2$\n- CCW, CW, CCW: $2$\n- CW, CCW, CW: $0$\n- CW, CCW, CCW: $0$\n- CCW, CW, CW: $0$\n- CCW, CCW, CW: $0$\n\n## Scoring\n\n- Input 2: $K\\le 100$, $N\\le 10$\n- Input 3: $N\\le 10$\n- Inputs 4-7: $\\sum N^3\\le 500^3$\n- Inputs 8-11: $K < \\frac{M}{2}$\n- Inputs 12-15: No additional constraints\n\n**Problem credits:** Sujay Konda", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 6.0, "memory_limit_mb": 512, "n_tests": 14, "n_sample_tests": 1, "point_scores": [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9], "val_cpp": "#include \"testlib.h\"\nconst long long MOD = 1000000007LL;\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100, \"T\");\n inf.readEoln();\n long long sumN2 = 0, sumM = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 5000, \"N\"); inf.readSpace();\n int M = inf.readInt(1, 1000000, \"M\"); inf.readSpace();\n inf.readLong(1, 1000000000000000000LL, \"K\");\n inf.readEoln();\n sumN2 += (long long)N * N; sumM += M;\n for (int i = 1; i <= N; i++) {\n inf.readInt(0, (int)(MOD - 1), \"p_i\");\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n long long prev = -1;\n for (int i = 1; i <= N; i++) {\n int x = inf.readInt(0, M - 1, \"x_i\");\n if (i == 1) ensuref(x == 0, \"test %d: x_1 must be 0, got %d\", tc, x);\n ensuref(x > prev, \"test %d: x must be strictly increasing (idx %d: %d <= %lld)\", tc, i, x, prev);\n prev = x;\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n }\n ensuref(sumN2 <= 25000000LL, \"sum N^2 is %lld > 5000^2\", sumN2);\n ensuref(sumM <= 1000000LL, \"sum M is %lld > 1e6\", sumM);\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nconst int MOD = 1e9 + 7;\n\nint bpow(int x, int y) {\n return (y == 0) ? 1 : ((ll)bpow((ll)x * x % MOD, y / 2) * ((y % 2) ? x : 1) % MOD);\n}\n\nvoid mulp(vector<int>& poly, int pr) {\n int npr = (MOD + 1 - pr) % MOD;\n if(npr == 0) return;\n poly.push_back(0);\n for(int i = poly.size() - 2; i >= 0; i--) {\n poly[i + 1] = (poly[i + 1] + (ll)npr * poly[i]) % MOD;\n poly[i] = ((ll)pr * poly[i]) % MOD;\n }\n}\nvoid divp(vector<int>& poly, int pr, int ipr) {\n int npr = (MOD + 1 - pr) % MOD;\n if(npr == 0) return;\n int v = poly.back();\n for(int i = poly.size() - 1; i >= 1; i--) {\n int nv = poly[i - 1];\n poly[i - 1] = (ll)v * ipr % MOD;\n v = (nv - (ll)poly[i - 1] * pr) % MOD;\n if(v < 0) v += MOD;\n }\n assert(v == 0);\n poly.pop_back();\n}\n\nvoid tc() {\n int N, M; cin >> N >> M;\n ll K; cin >> K;\n\n ll L = ((2 * K) / M) % N;\n ll E = (2 * K) % M;\n\n vector<int> p(N), x(N);\n for(int i = 0; i < N; i++) cin >> p[i];\n for(int i = 0; i < N; i++) cin >> x[i];\n\n auto get = [&] (int i) {\n if(i >= N) return x[i - N] + M;\n else return x[i];\n };\n\n auto solveR = [&] () {\n vector<int> ip(N);\n for(int i = 0; i < N; i++) ip[i] = bpow(MOD + 1 - p[i], MOD - 2);\n\n int j = 0;\n vector<int> ans(M);\n vector<int> cpoly(1, 1), opoly(1, 1);\n for(int i = 0; i < N; i++) {\n mulp(opoly, p[i]);\n }\n for(int i = 0; i < N; i++) {\n if(i > 0)\n mulp(opoly, p[i - 1]);\n while(get(j) - get(i) <= E) {\n mulp(cpoly, p[j % N]);\n divp(opoly, p[j % N], ip[j % N]);\n j++;\n }\n divp(cpoly, p[i], ip[i]);\n\n int cans = 0;\n int clc = (N - i) % N;\n for(int lc = 0; lc < N; lc++) {\n int olc = lc - clc;\n if(clc < cpoly.size() && olc >= 0 && olc < opoly.size()) {\n cans = (cans + (ll)cpoly[clc] * opoly[olc]) % MOD;\n }\n clc -= L;\n if(clc < 0) clc += N;\n }\n int endp = ((x[i] + K) % M + M) % M;\n ans[endp] = (ans[endp] + (ll)p[i] * cans) % MOD;\n } \n return ans;\n };\n\n vector<int> ans = solveR();\n\n for(int i = 0; i < N; i++) {\n x[i] = (M - x[i]) % M;\n p[i] = (1 - p[i] + MOD) % MOD;\n }\n reverse(x.begin() + 1, x.end());\n reverse(p.begin() + 1, p.end());\n\n vector<int> ans2 = solveR();\n\n for(int i = 0; i < M; i++) {\n ans[i] = (ans[i] + ans2[(M - i) % M]) % MOD;\n if(i > 0) cout << \" \";\n cout << ans[i];\n }\n cout << '\\n';\n}\n\nint main() {\n ios::sync_with_stdio(false), cin.tie(nullptr);\n int T; cin >> T;\n while(T--) tc();\n}", "chk_cpp": null, "std_source_submission_id": 70937, "std_origin": "verbatim earliest root score=100 submission #70937"}
{"cpid": 1574, "title": "Dynamic Instability", "contest": "Second Contest", "division": "Platinum", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1574", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer Nhoj has trapped Bessie on a rooted tree with $N$ ($2\\le N\\le 2\\cdot 10^5$) nodes, where node $1$ is the root. Scared and alone, Bessie makes the following move each second:\n\n- If Bessie's current node has no children, then she will move to a random ancestor of the current node (excluding the node itself).\n- Otherwise, Bessie will move to a random child of the current node.\n\nInitially, Bessie is at node $x$, and her only way out is the exit located at node $y$ ($1\\le x, y\\le N$). For $Q$ ($1\\le Q\\le 2\\cdot 10^5$) independent queries of $x$ and $y$, compute the expected number of seconds it would take Bessie to reach node $y$ for the first time if she started at node $x$, modulo $10^9 + 7$.\n\n## Input Format\n\nThe first line contains $N$ and $Q$.\n\nThe next line contains $N - 1$ integers $p_2, \\ldots, p_N$ describing the tree ($1\\le p_i < i$). For each $2\\le i\\le N$, there is an edge between nodes $i$ and $p_i$.\n\nEach of the next $Q$ lines contains integers $x$ and $y$ representing the nodes for that query.\n\n## Output Format\n\nFor each query, output the expected number of seconds for Bessie to reach node $y$ for the first time starting at node $x$, modulo $10^9 + 7$.\n\n## Examples\n\n**Input 1**\n\n```\n5 5\n1 2 2 1\n1 1\n2 1\n3 1\n4 1\n5 1\n```\n\n**Output 1**\n\n```\n0\n4\n3\n3\n1\n```\n\n**Input 2**\n\n```\n5 5\n1 2 2 1\n1 1\n1 2\n1 3\n1 4\n1 5\n```\n\n**Output 2**\n\n```\n0\n3\n500000011\n500000011\n6\n```\n\n**Input 3**\n\n```\n13 10\n1 2 2 4 3 1 5 6 4 7 8 10\n1 12\n10 6\n5 12\n1 13\n13 10\n6 4\n7 12\n3 1\n12 8\n2 1\n```\n\n**Output 3**\n\n```\n166666700\n21\n2\n166666701\n500000023\n18\n166666704\n750000018\n800000021\n500000018\n```\n\n## Note\n\nFor the first example:\n\n- In the 1st query, the expected time to reach node $1$ from itself is $0$.\n- In the 3rd query, after $1$ second, Bessie will be at node $1$ with probability $\\frac{1}{2}$ and at node $2$ with probability $\\frac{1}{2}$. Since the expected time to reach node $1$ from node $2$ is $4$, the expected time for Bessie to reach node $1$ starting at node $3$ is $1 + \\frac{1}{2}\\cdot 0 + \\frac{1}{2}\\cdot 4 = 3$.\n\nFor the second example: in the 3rd query, the expected time to reach node $3$ from node $1$ is $\\frac{15}{2}$.\n\n## Scoring\n\n- Inputs 4-8: For all queries, $y = 1$\n- Inputs 9-13: For all queries, $x = 1$\n- Inputs 14-18: For each $2\\le i\\le N$, $p_i$ is uniformly randomly chosen from the range $[1, i-1]$\n- Inputs 19-23: No additional constraints\n\n**Problem credits:** Avnith Vijayram", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 3, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(2, 200000, \"N\"); inf.readSpace();\n int Q = inf.readInt(1, 200000, \"Q\"); inf.readEoln();\n for (int i = 2; i <= N; i++) {\n inf.readInt(1, i - 1, \"p_i\"); // 1 <= p_i < i\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n for (int q = 1; q <= Q; q++) {\n inf.readInt(1, N, \"x\"); inf.readSpace();\n inf.readInt(1, N, \"y\"); inf.readEoln();\n }\n inf.readEof();\n return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst int mxn = 2e5+5;\nconst ll M = 1e9+7;\nll modpow(ll x, ll p) {\n ll a = 1;\n while (p) {\n if (p & 1) a = a * x % M;\n x = x * x % M;\n p /= 2;\n }\n return a;\n}\nll inv(ll x) {\n assert(x != 0);\n return modpow(x, M-2);\n}\n\nll par[mxn];\nvector<ll> childs[mxn];\n\nll d[mxn], up[mxn], e[mxn], esum[mxn], upd[mxn];\n\nll dfs1(int x, ll dep) {\n d[x] = dep;\n ll cx = childs[x].size();\n if (cx == 0) {\n up[x] = 1;\n return up[x];\n }\n \n ll upsum = 0;\n for (auto c : childs[x]) {\n upsum += dfs1(c, dep+1);\n }\n upsum %= M;\n\n if (dep > 0) {\n up[x] = (1 + inv(dep)) * (1 + inv(cx) * upsum % M) % M;\n }\n return up[x];\n}\n\nvoid dfs2(int x, ll cp, ll upcsum, ll ep, ll epsum, ll updsum) {\n ll downx = 0;\n if (x != 0) {\n downx = (cp + upcsum + (cp - 1) * (ep - inv(d[x]) * epsum % M)) % M;\n if (downx < 0) downx += M;\n }\n e[x] = (ep + downx) % M;\n esum[x] = (epsum + e[x]) % M;\n if (x > 0) updsum = (updsum + up[x] * inv(d[x]+1)) % M;\n upd[x] = updsum;\n\n ll cx = childs[x].size();\n ll upsum = 0;\n for (auto c : childs[x]) {\n upsum += up[c];\n }\n\n for (auto c : childs[x]) {\n dfs2(c, cx, (upsum + M - up[c]) % M, e[x], esum[x], upd[x]);\n }\n}\n\nint main() {\n int n, q; cin >> n >> q;\n for (int i = 1; i < n; i++) {\n int p; cin >> p; p--;\n childs[p].push_back(i);\n par[i] = p;\n }\n\n dfs1(0, 0);\n dfs2(0, 0, 0, 0, 0, 0);\n\n vector<vector<int>> jmp(18, vector<int>(n, -1));\n for (int i = 1; i < n; i++) jmp[0][i] = par[i];\n for (int j = 1; j < 18; j++) {\n for (int i = 0; i < n; i++) {\n int nx = jmp[j-1][i];\n if (nx != -1) nx = jmp[j-1][nx];\n jmp[j][i] = nx;\n }\n }\n\n auto jump = [&](int x, int l) -> int {\n for (int j = 17; j >= 0; j--) {\n if (x == -1) return x;\n if (l & (1 << j)) x = jmp[j][x];\n }\n return x;\n };\n\n while (q--) {\n int x, y; cin >> x >> y; x--; y--;\n \n int l;\n int nx = x, ny = y;\n if (d[nx] > d[ny]) nx = jump(nx, d[nx]-d[ny]);\n if (d[nx] < d[ny]) ny = jump(ny, d[ny]-d[nx]);\n if (nx == ny) l = nx;\n else {\n for (int j = 17; j >= 0; j--) {\n if (jmp[j][nx] != jmp[j][ny]) {\n nx = jmp[j][nx];\n ny = jmp[j][ny];\n }\n }\n assert(par[nx] == par[ny]);\n l = par[nx];\n }\n\n if (l == x) {\n cout << (e[y]-e[x]+M)%M << endl;\n } else {\n ll reachm = (up[x] + upd[par[x]] - upd[l] + M) % M;\n ll wtoy = (e[y] - (inv(d[l]+1) * esum[l] % M) + M) % M;\n cout << (reachm + wtoy) % M << endl;\n }\n }\n}\n", "chk_cpp": null, "std_source_submission_id": 70938, "std_origin": "verbatim earliest root score=100 submission #70938"}
{"cpid": 1587, "title": "Make All Distinct", "contest": "Third Contest", "division": "Bronze", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1587", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nYou have an integer array $a_1\\dots a_N$ with elements initially in the range $[1, N]$ ($1\\le N\\le 2\\cdot 10^5$), as well as a nonzero integer $K$ ($-N\\le K\\le N$, $K\\ne 0$).\n\nYou may perform the following operation as many times as you'd like (possibly zero): select an index $i$ and set $a_i = a_i + K$.\n\nFind the minimum number of operations to make all array elements distinct.\n\n## Input Format\n\nThe input consists of $T$ ($1\\le T\\le 10$) independent tests. Each test is described as follows:\n\nThe first line contains $N$ and $K$.\n\nThe second line contains $a_1\\dots a_N$.\n\nIt is guaranteed that the sum of $N$ over all tests does not exceed $10^6$.\n\n## Output Format\n\nFor each test, output a single line containing the minimum number of operations.\n\n**Note:** The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a `long long` in C/C++).\n\n## Examples\n\n**Input 1**\n\n```\n4\n4 1\n4 1 4 1\n4 -3\n4 1 4 1\n4 4\n4 1 4 1\n3 -1\n1 1 2\n```\n\n**Output 1**\n\n```\n2\n4\n2\n1\n```\n\n## Note\n\nFor the first test, here is a possible sequence of two operations that makes all elements distinct.\n\n```\n4 1 4 1\n5 1 4 1 (a_1 += 1)\n5 1 4 2 (a_4 += 1)\n```\n\n## Scoring\n\n- Inputs 2-4: $N\\le 50$\n- Inputs 5-7: $N\\le 2000$\n- Inputs 8-10: $K = 1$\n- Inputs 11-13: No additional constraints\n\n**Problem credits:** Akshaj Arora, Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 12, "n_sample_tests": 1, "point_scores": [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 200000, \"N\"); inf.readSpace();\n int K = inf.readInt(-N, N, \"K\"); inf.readEoln();\n ensuref(K != 0, \"test %d: K must be nonzero\", tc);\n sumN += N;\n for (int i = 1; i <= N; i++) { inf.readInt(1, N, \"a_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n }\n ensuref(sumN <= 1000000, \"sum N %lld > 1e6\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int T;\n cin >> T;\n for (int tc = 0; tc < T; ++tc) {\n int N, K;\n cin >> N >> K;\n vector<int> cnt(N);\n for (int i = 0; i < N; ++i) {\n int x;\n cin >> x;\n ++cnt.at(x - 1);\n }\n if (K < 0) {\n reverse(begin(cnt), end(cnt));\n K *= -1;\n }\n assert(0 < K && K <= N);\n int64_t ans = 0;\n\n for (int i = 0; i < K; ++i) { // process remainder i mod K\n int64_t cur_min = i;\n int cnt_min = 0;\n while (cur_min < N || cnt_min > 0) {\n if (cur_min < N) cnt_min += cnt.at(cur_min);\n if (cnt_min > 0) ans += cnt_min - 1;\n cur_min += K;\n if (cnt_min > 0) --cnt_min;\n }\n }\n\n cout << ans << \"\\n\";\n }\n}", "chk_cpp": null, "std_source_submission_id": 70958, "std_origin": "verbatim earliest root score=100 submission #70958"}
{"cpid": 1588, "title": "Strange Function", "contest": "Third Contest", "division": "Bronze", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1588", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFor all positive integers $x$, the function $f(x)$ is defined as follows:\n\n- If $x$ has any digits that aren't $0$ or $1$, for each digit of $x$, set it to $1$ if it is odd or $0$ otherwise, and return $x$.\n- Otherwise, return $x - 1$.\n\nGiven a value of $x$ ($1\\le x < 10^{2\\cdot 10^5}$), find how many times $f$ needs to be applied to $x$ until $x$ reaches $0$. As this number might be very large, output its remainder when divided by $10^9 + 7$.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 10^5$), the number of independent tests.\n\nThe next $T$ lines each contain a positive integer $x$ consisting solely of the digits $0$-$9$, with no leading zeros.\n\nIt is guaranteed that the total number of digits in all input integers does not exceed $10^6$.\n\n## Output Format\n\nFor each test case, output the remainder of the number of times when divided by $10^9 + 7$ on a separate line.\n\n## Examples\n\n**Input 1**\n\n```\n2\n24680\n210\n```\n\n**Output 1**\n\n```\n1\n4\n```\n\n**Input 2**\n\n```\n1\n1234567890123456789012345678901234567890\n```\n\n**Output 2**\n\n```\n511620083\n```\n\n## Note\n\nFor the first example:\n\n- First test: $x$ becomes zero after one operation.\n- Second test: $f(x) = 10$, $f^2(x) = 9$, $f^3(x) = 1$, $f^4(x) = 0$.\n\n## Scoring\n\n- Inputs 3-5: $T\\le 2000$, $x < 10^9$\n- Inputs 6-7: $x < 10^{18}$\n- Inputs 8-9: $x < 10^{60}$\n- Inputs 10-12: No additional constraints\n\n**Problem credits:** Aidan Bai", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 2, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\n#include <string>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100000, \"T\"); inf.readEoln();\n long long totDigits = 0;\n for (int tc = 1; tc <= T; tc++) {\n std::string x = inf.readToken();\n int len = (int)x.size();\n ensuref(len >= 1, \"test %d: empty number\", tc);\n for (char c : x) ensuref(c >= '0' && c <= '9', \"test %d: non-digit '%c'\", tc, c);\n // positive integer, no leading zeros\n ensuref(!(len > 1 && x[0] == '0'), \"test %d: leading zero\", tc);\n ensuref(!(len == 1 && x[0] == '0'), \"test %d: x must be positive\", tc);\n totDigits += len;\n inf.readEoln();\n }\n ensuref(totDigits <= 1000000, \"total digits %lld > 1e6\", totDigits);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include \"bits/extc++.h\"\n \nusing namespace std;\n \nusing ll = long long;\n \n#define sz(x) int(std::size(x))\n \nconstexpr ll mod = 1e9+7;\n \nvoid solve(){\n string s;\n cin>>s;\n ll moves=0;\n int n=sz(s);\n ll tpow[n]{}; // powers of 2\n tpow[0]=1;\n for(int i = 1; i<n; i++){\n tpow[i]=(tpow[i-1]*2)%mod;\n }\n {\n // first make all digits 0 or 1\n bool all01=1;\n for(auto u: s){\n if(u!='0' && u!='1'){\n all01=0;\n break;\n }\n }\n if(!all01){\n moves++;\n for(auto &u: s){\n u=char(((u-'0')%2)+'0');\n }\n }\n }\n reverse(s.begin(),s.end());\n for(int i = 0; i<n; i++){\n if(s[i]=='1'){\n if(i==0)moves++;\n else{\n moves=(moves+tpow[i]+tpow[i-1])%mod;\n }\n }\n }\n cout<<moves<<\"\\n\";\n}\n \nint main(){\n cin.tie(0)->sync_with_stdio(0);\n cin.exceptions(ios::failbit);\n int t;\n cin>>t;\n while(t--)solve();\n}", "chk_cpp": null, "std_source_submission_id": 70959, "std_origin": "verbatim earliest root score=100 submission #70959"}
{"cpid": 1589, "title": "Swap to Win", "contest": "Third Contest", "division": "Bronze", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1589", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John has a favorite string $t$ with $M$ characters. He also has $N$ strings $s_1, s_2, \\ldots, s_N$ each with $M$ characters ($1\\le N, M\\le 1000$).\n\nFJ can perform the following two types of operations:\n\n1. FJ chooses any string $s_x$ and two indices $p$ and $q$. Then, he swaps the $p$-th and $q$-th character of $s_x$ ($1\\le x\\le N$, $1\\le p, q\\le M$).\n2. FJ chooses two strings $s_x$ and $s_y$ and an index $k$. Then, he swaps the $k$-th characters of $s_x$ and $s_y$ ($1\\le x, y\\le N$, $1\\le k\\le M$).\n\nHis goal is to make $s_1$ equal to $t$. Find any series of operations that fulfills his goal. Because FJ is in a hurry, he only has time to perform a total of $2M$ operations. The inputs guarantee that it is possible to fulfill FJ's goal.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 10$), the number of independent tests. Each test is specified in the following format:\n\nThe first line contains $N$ and $M$.\n\nThe second line contains $t$.\n\nThen, $N$ lines follow, the $i$-th of which contains $s_i$.\n\nThe inputs will guarantee that it is possible to fulfill FJ's goal. All strings contain lowercase English letters (a-z).\n\n## Output Format\n\nThe output for each test should be as follows:\n\nOn the first line, output an integer $K$, the number of operations you will perform. $K$ must be a non-negative integer less than or equal to $2M$.\n\nThen, output $K$ lines, denoting the operations you will perform in sequential order. Each line should be one of the following:\n\n- `1 x p q`\n- `2 x y k`\n\n## Examples\n\n**Input 1**\n\n```\n3\n3 6\nbanana\nnabana\nbanana\nnnbaaa\n5 3\nabc\ndef\nbca\nghi\njkl\nmno\n3 5\nabcde\nabcde\nabcde\nzzzzz\n```\n\n**Output 1**\n\n```\n3\n2 1 2 1\n1 1 3 5\n2 1 2 5\n5\n1 2 1 3\n2 1 2 1\n1 2 2 3\n2 1 2 2\n2 1 2 3\n0\n```\n\n## Note\n\nHere is how $s$ changes according to the first test's output (with letters swapped in uppercase):\n\n```\nnabana Babana baNaBa banaNa\nbanana -> Nanana -> nanana -> nanaBa\nnnbaaa nnbaaa nnbaaa nnbaaa\n```\n\nAfter all three operations, $s_1 = t$.\n\n## Scoring\n\n- Inputs 2-6: $N, M\\le 100$\n- Inputs 7-12: No additional constraints\n\n**Problem credits:** Chongtian Ma", "checker_kind": "custom (chk.cpp, testlib)", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 11, "n_sample_tests": 1, "point_scores": [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10], "val_cpp": "#include \"testlib.h\"\n#include <string>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10, \"T\"); inf.readEoln();\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 1000, \"N\"); inf.readSpace();\n int M = inf.readInt(1, 1000, \"M\"); inf.readEoln();\n std::string t = inf.readLine();\n ensuref((int)t.size() == M, \"test %d: |t|=%d expected %d\", tc, (int)t.size(), M);\n for (char c : t) ensuref(c >= 'a' && c <= 'z', \"test %d t: non-lowercase '%c'\", tc, c);\n for (int i = 1; i <= N; i++) {\n std::string s = inf.readLine();\n ensuref((int)s.size() == M, \"test %d s_%d: len %d expected %d\", tc, i, (int)s.size(), M);\n for (char c : s) ensuref(c >= 'a' && c <= 'z', \"test %d s_%d: non-lowercase '%c'\", tc, i, c);\n }\n }\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n\n string t;\n cin >> t;\n\n vector<string> s(n);\n for (int i = 0; i < n; i++) cin >> s[i];\n\n vector<vector<int>> has(n, vector<int>(26, 0));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n has[i][s[i][j] - 'a']++;\n }\n }\n\n vector<array<int, 4>> ops;\n\n auto op = [&](int type, int a, int b, int c) {\n // store 1-indexed for printing\n ops.push_back({type, a + 1, b + 1, c + 1});\n\n if (type == 1) {\n swap(s[a][b], s[a][c]);\n } else {\n has[a][s[a][c] - 'a']--;\n has[b][s[b][c] - 'a']--;\n swap(s[a][c], s[b][c]);\n has[a][s[a][c] - 'a']++;\n has[b][s[b][c] - 'a']++;\n }\n };\n\n // Swap within s1 to fix as many positions as possible\n for (int i = 0; i < m; i++) {\n if (s[0][i] != t[i]) {\n for (int j = 0; j < m; j++) {\n if (s[0][j] == t[i] && s[0][j] != t[j]) {\n op(1, 0, i, j);\n break;\n }\n }\n }\n }\n\n // Import needed characters from other rows\n for (int i = 0; i < m; i++) {\n if (s[0][i] != t[i]) {\n for (int j = 1; j < n; j++) {\n if (has[j][t[i] - 'a']) {\n \t// find the column\n int idx = int(find(s[j].begin(), s[j].end(), t[i]) - s[j].begin());\n op(1, j, i, idx);\n op(2, 0, j, i);\n break;\n }\n }\n }\n }\n\n printf(\"%d\\n\", (int)ops.size());\n for (auto &v : ops) {\n printf(\"%d %d %d %d\\n\", v[0], v[1], v[2], v[3]);\n }\n}\n\nint main() {\n cin.tie(0) -> sync_with_stdio(0);\n\n int T;\n cin >> T;\n while (T--) solve();\n return 0;\n}", "chk_cpp": "#include \"testlib.h\"\n#include <vector>\n#include <string>\nusing namespace std;\n\nint main(int argc, char *argv[]) {\n setName(\"USACO 2026 Bronze: Swap to Win\");\n registerTestlibCmd(argc, argv);\n\n int T = inf.readInt();\n for (int t = 1; t <= T; t++) {\n int N = inf.readInt();\n int M = inf.readInt();\n string target = inf.readToken();\n if ((int)target.size() != M)\n quitf(_fail, \"judge input malformed at test %d (target len)\", t);\n vector<string> s(N + 1);\n for (int i = 1; i <= N; i++) {\n s[i] = inf.readToken();\n if ((int)s[i].size() != M)\n quitf(_fail, \"judge input malformed at test %d (s_%d len)\", t, i);\n }\n\n // also consume reference output's K and its K op lines (we don't need them for verification)\n int refK = ans.readInt();\n for (int j = 0; j < refK; j++) {\n int op = ans.readInt();\n if (op == 1) { ans.readInt(); ans.readInt(); ans.readInt(); }\n else if (op == 2) { ans.readInt(); ans.readInt(); ans.readInt(); }\n else quitf(_fail, \"reference output malformed at test %d\", t);\n }\n\n int K = ouf.readInt(0, 2 * M, \"K\");\n for (int j = 0; j < K; j++) {\n int op = ouf.readInt(1, 2, \"op type\");\n if (op == 1) {\n int x = ouf.readInt(1, N, \"x\");\n int p = ouf.readInt(1, M, \"p\");\n int q = ouf.readInt(1, M, \"q\");\n swap(s[x][p - 1], s[x][q - 1]);\n } else {\n int x = ouf.readInt(1, N, \"x\");\n int y = ouf.readInt(1, N, \"y\");\n int k = ouf.readInt(1, M, \"k\");\n swap(s[x][k - 1], s[y][k - 1]);\n }\n }\n\n if (s[1] != target)\n quitf(_wa, \"test %d: after %d operations s_1='%s' (expected '%s')\",\n t, K, s[1].c_str(), target.c_str());\n }\n\n if (!ouf.seekEof())\n quitf(_wa, \"extra tokens in contestant output\");\n quitf(_ok, \"T=%d cases\", T);\n}\n", "std_source_submission_id": 70973, "std_origin": "verbatim earliest root score=100 submission #70973"}
{"cpid": 1590, "title": "Clash!", "contest": "Third Contest", "division": "Silver", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1590", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John is playing a famous and strategic card game with his dear cow Bessie. FJ has $N$ ($2\\le N\\le 2\\cdot 10^5$) cards, conveniently numbered from $1$ to $N$. The $i$-th card costs $a_i$ ($1\\le a_i\\le 10^9$) moolixir if FJ wants to play it.\n\nHis hand always consists of $H$ cards at any given time ($1\\le H < N$). Initially, his hand consists of cards $1$ through $H$. The remaining cards are in a draw queue. Every time FJ plays a card in his hand, he will draw its replacement from the front of the draw queue to his hand. Then, FJ will put the card he just played to the back of the draw queue. Initially, cards $H + 1$ through $N$ are arranged from the front to the back of the draw queue in that order.\n\nIn this game, time is measured in integer seconds. Initially, the game starts at time $0$ with FJ having $0$ moolixir. Immediately before each of integer times $t = 1, 2, 3, \\dots$, moolixir increases by $1$. At each integer time, FJ may choose to play a card in his hand if its cost does not exceed FJ's current moolixir count, which subtracts FJ's current moolixir count by the card's cost.\n\nFJ marks a subset of his cards $s_1, s_2, \\ldots, s_k$ as win-conditions ($1\\le k\\le N$, $1\\le s_i\\le N$). If FJ has at least one win-condition in his hand, the next card he plays must be a win-condition.\n\nHe asks you $Q$ ($1\\le Q\\le 2\\cdot 10^5$) queries. Each query is of the following form: what is the maximum number of win-conditions he could have placed down within $t$ time ($1\\le t\\le 10^{18}$)?\n\n## Input Format\n\nThe first line contains $N$ and $H$.\n\nThe second line contains $N$ integers $a_1, a_2, \\ldots, a_N$.\n\nThe third line contains an integer $k$, the number of win-conditions.\n\nThe fourth line contains $k$ distinct integers $s_1, s_2, \\ldots, s_k$.\n\nThe fifth line contains an integer $Q$.\n\nThe following $Q$ lines each contain an integer $t$, the time to answer for each query.\n\n## Output Format\n\nFor each query, output the maximum number of win-conditions that FJ could've put down within $t$ time.\n\n## Examples\n\n**Input 1**\n\n```\n6 3\n2 4 3 5 7 6\n2\n1 4\n6\n1\n2\n3\n7\n10\n1000000000000000\n```\n\n**Output 1**\n\n```\n0\n1\n1\n2\n2\n142857142857143\n```\n\n## Note\n\nIn this case, you start with card $1$, a win condition on your hand. You can play it after you accumulate $2$ elixir in $2$ seconds. This means that just after $t = 1$ you can play no cards, but after $t = 2$ you can play your first card, which must be your win condition.\n\nAfter $t = 3$, it is still most optimal to play card $1$ and have $1$ elixir remaining, so the answer here is still $1$.\n\nYou then draw card $4$, which is also a win condition. You play it immediately after $t = 7$, so you have played $2$ win conditions at this time.\n\nYou then draw card $5$ and have no win conditions in your hand. After $t = 10$, even if you play card $3$ with the $3$ elixir you have, your number of win conditions does not change.\n\n## Scoring\n\n- Inputs 2-3: $N, Q\\le 100$\n- Inputs 4-5: $H = 1$\n- Inputs 6-11: No additional constraints\n\n**Problem credits:** Chongtian Ma", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 1, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\n#include <set>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(2, 200000, \"N\"); inf.readSpace();\n int H = inf.readInt(1, N - 1, \"H\"); inf.readEoln();\n for (int i = 1; i <= N; i++) { inf.readInt(1, 1000000000, \"a_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n int k = inf.readInt(1, N, \"k\"); inf.readEoln();\n std::set<int> seen;\n for (int i = 1; i <= k; i++) {\n int s = inf.readInt(1, N, \"s_i\");\n ensuref(seen.insert(s).second, \"win-condition %d repeats\", s);\n if (i < k) inf.readSpace();\n }\n inf.readEoln();\n int Q = inf.readInt(1, 200000, \"Q\"); inf.readEoln();\n for (int q = 1; q <= Q; q++) { inf.readLong(1, 1000000000000000000LL, \"t\"); inf.readEoln(); }\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <class T> using V = vector<T>;\n#define all(x) begin(x), end(x)\n\nusing ll = long long;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N, H;\n cin >> N >> H;\n\n using P = pair<int, int>;\n V<P> A(N);\n for (auto &p : A) cin >> p.second;\n\n int K;\n cin >> K;\n for (int i = 0; i < K; ++i) {\n int s;\n cin >> s;\n --s;\n A.at(s).first = -1;\n }\n\n priority_queue<P, V<P>, greater<P>> pq;\n\n for (int i = 0; i < H - 1; ++i) pq.push(A.at(i));\n V<ll> cycle; // time each win-condition in cycle is played\n ll cycle_len = 0;\n for (int i = 0; i <= N - H; ++i) {\n pq.push(A.at(i + H - 1));\n cycle_len += pq.top().second;\n if (pq.top().first == -1) cycle.push_back(cycle_len);\n pq.pop();\n }\n int Q;\n cin >> Q;\n for (int i = 0; i < Q; ++i) {\n ll t;\n cin >> t;\n ll ans = t / cycle_len * size(cycle);\n ans +=\n upper_bound(begin(cycle), end(cycle), t % cycle_len) - begin(cycle);\n cout << ans << \"\\n\";\n }\n}", "chk_cpp": null, "std_source_submission_id": 70955, "std_origin": "verbatim earliest root score=100 submission #70955"}
{"cpid": 1591, "title": "Milk Buckets", "contest": "Third Contest", "division": "Silver", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1591", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nThere are $N$ ($1\\le N\\le 2\\cdot 10^5$) buckets in a stack where the $i$-th bucket from the top has capacity $a_i$ gallons ($1\\le a_i\\le 10^9$). A tap above the top bucket sends one gallon of milk per second into the first bucket per second. There is also a pool below bucket $N$.\n\nWhen a bucket reaches its capacity after $t$ seconds, at the start of the $(t+1)$-th second it flips to dump its contents into either the bucket below if it is not the last bucket or the pool otherwise (it reverts back to filling at the end of the $(t+1)$-th second). A bucket cannot collect milk while it is flipped; any milk arriving at the bucket above during this second is lost. Additionally, any amount of milk exceeding the capacity of the bucket below is lost.\n\nHandle $Q$ ($1\\le Q\\le 3\\cdot 10^5$) queries, each specified by three integers $i$, $v$, and $t$:\n\n- First, set $a_i = v$ ($1\\le i\\le N$, $1\\le v\\le 10^9$).\n- Then answer the following question: Suppose that at time $0$, all buckets as well as the pool are empty. Determine the number of gallons of milk in the pool after $t$ seconds ($1\\le t\\le 10^{18}$).\n\nThe $a_i = v$ updates persist through later queries.\n\n## Input Format\n\nThe first line contains $N$.\n\nThe second line contains $a_1\\dots a_N$.\n\nThe next line contains $Q$.\n\nThen the following $Q$ lines contain three integers $i$, $v$, and $t$. This means that you should set $a_i = v$ and then answer the question for $t$.\n\n## Output Format\n\nOutput the answer to each question on a separate line.\n\n## Examples\n\n**Input 1**\n\n```\n3\n1 1 1\n30\n1 1 1\n1 1 2\n1 1 3\n1 1 4\n1 1 5\n1 1 6\n1 1 7\n1 1 8\n1 1 9\n1 1 10\n1 2 1\n1 2 2\n1 2 3\n1 2 4\n1 2 5\n1 2 6\n1 2 7\n1 2 8\n1 2 9\n1 2 10\n2 2 1\n2 2 2\n2 2 3\n2 2 4\n2 2 5\n2 2 6\n2 2 7\n2 2 8\n2 2 9\n2 2 10\n```\n\n**Output 1**\n\n```\n0\n0\n0\n1\n1\n2\n2\n3\n3\n4\n0\n0\n0\n0\n1\n1\n1\n2\n2\n2\n0\n0\n0\n0\n1\n1\n1\n2\n2\n2\n```\n\n**Input 2**\n\n```\n2\n1 2\n10\n1 1 1\n1 1 2\n1 1 3\n1 1 4\n1 1 5\n1 1 6\n1 1 7\n1 1 8\n1 1 9\n1 1 10\n```\n\n**Output 2**\n\n```\n0\n0\n0\n0\n2\n2\n2\n2\n4\n4\n```\n\n**Input 3**\n\n```\n3\n1 1 1\n1\n1 1 1000000000000000000\n```\n\n**Output 3**\n\n```\n499999999999999999\n```\n\n## Note\n\nWhen $a = [1, 1, 1]$:\n\n- Bucket $1$ flips at times $2, 4, 6, \\dots$\n- Bucket $2$ flips at times $3, 5, 7, \\dots$\n- Bucket $3$ flips at times $4, 6, 8, \\dots$\n\nWhen $a = [2, 1, 1]$:\n\n- Bucket $1$ flips at times $3, 6, 9, \\dots$\n- Bucket $2$ flips at times $4, 7, 10, \\dots$\n- Bucket $3$ flips at times $5, 8, 11, \\dots$\n\nWhen $a = [2, 2, 1]$:\n\n- Bucket $1$ flips at times $3, 6, 9, \\dots$\n- Bucket $2$ flips at times $4, 7, 10, \\dots$\n- Bucket $3$ flips at times $5, 8, 11, \\dots$\n\n## Scoring\n\n- Inputs 4-5: $N\\le 10$, $Q\\le 100$, and all $t\\le 10^4$\n- Inputs 6-11: $N\\le 10^3$, $Q\\le 10^4$\n- Inputs 12-23: No additional constraints\n\n**Problem credits:** Akshaj Arora", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 3, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int N = inf.readInt(1, 200000, \"N\"); inf.readEoln();\n for (int i = 1; i <= N; i++) { inf.readInt(1, 1000000000, \"a_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n int Q = inf.readInt(1, 300000, \"Q\"); inf.readEoln();\n for (int q = 1; q <= Q; q++) {\n inf.readInt(1, N, \"i\"); inf.readSpace();\n inf.readInt(1, 1000000000, \"v\"); inf.readSpace();\n inf.readLong(1, 1000000000000000000LL, \"t\"); inf.readEoln();\n }\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint cdiv(int a, int b) { return (a + b - 1) / b; }\n\nconst ll mx = 1e18;\n\nvoid solve() {\n int n;\n cin >> n;\n vector<int> a(n);\n for (auto &i : a) cin >> i;\n\n // precomp\n set<int> s;\n auto upd = [&](int i) {\n if (i != 0 && a[i] > a[i - 1]) s.insert(i);\n else s.erase(i);\n };\n for (int i = 1; i < n; i++) upd(i);\n\n // solve\n int q;\n cin >> q;\n while (q--) {\n ll i, v, t;\n cin >> i >> v >> t;\n a[--i] = v;\n upd(i);\n if (i < n - 1) upd(i + 1);\n\n ll ans = max(0LL, t - n + 1);\n ans /= a[0] + 1;\n for (int i : s) {\n ll r = cdiv(a[i], a[i - 1]);\n ans /= r;\n if (ans == 0) break;\n }\n ans *= a.back();\n cout << ans << '\\n';\n }\n}\n\nsigned main() {\n cin.tie(0)->sync_with_stdio(0);\n solve();\n}\n", "chk_cpp": null, "std_source_submission_id": 70956, "std_origin": "verbatim earliest root score=100 submission #70956"}
{"cpid": 1592, "title": "Point Elimination", "contest": "Third Contest", "division": "Silver", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1592", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nYou have $N$ ($2\\le N\\le 10^5$, $N$ is even) points $(x_i, y_i)$ ($1\\le x_i, y_i\\le 10^6$) on an infinite 2D-coordinate plane.\n\nYou can perform the following two types of operations any number of times:\n\n- Choose two points that are directly adjacent to each other (Manhattan distance of $1$), and remove both points.\n- Choose any two points and swap their $y$-coordinates. Formally, points $(a, b)$ and $(c, d)$ become $(a, d)$ and $(c, b)$ respectively.\n\nDetermine if it is possible to eliminate all points on the board. Note that it may be the case that two points may map to the same coordinate; they should still be treated as different points. You also may not directly delete points on the same coordinate, as they are technically not directly adjacent.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 5000$), the number of test cases.\n\nThe first line of each test case contains an integer $N$.\n\nThe following $N$ lines contain two integers $x_i$ and $y_i$.\n\nIt is guaranteed that the sum of $N$ over all test cases does not exceed $5\\cdot 10^5$.\n\n## Output Format\n\nFor each test case, output `YES` or `NO` on a new line.\n\n## Examples\n\n**Input 1**\n\n```\n4\n2\n1 1\n1 1\n4\n6 10\n7 11\n8 1\n8 1\n6\n1 2\n1 3\n1 4\n1 5\n10 10\n11 10\n6\n1 1\n1 1\n1 1\n1 1\n10 10\n11 11\n```\n\n**Output 1**\n\n```\nNO\nYES\nYES\nNO\n```\n\n## Note\n\nFor the first test, the only two points are equal, so no swaps will do anything. Thus, our answer is `NO`.\n\nIn the second test, we can swap the $y$-coordinates of $6$ and $7$ with $8$ and $8$. Then, we can remove the first two points (horizontal adjacency) and the last two (vertical).\n\nFor the third test, no swaps are needed. We can remove the first pair, second, and third.\n\nIn the last test, it can be shown that no matter how we swap the $y$-coordinates, we will never be able to remove all the points in adjacent pairs.\n\n## Scoring\n\n- Input 2: $T\\le 1000$, $N\\le 6$\n- Inputs 3-5: $N\\le 100$\n- Inputs 6-11: No additional constraints\n\n**Problem credits:** Alex Pylypenko, Chongtian Ma", "checker_kind": "UOJ builtin: wcmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 1, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 5000, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(2, 100000, \"N\"); inf.readEoln();\n ensuref(N % 2 == 0, \"test %d: N must be even, got %d\", tc, N);\n sumN += N;\n for (int i = 1; i <= N; i++) {\n inf.readInt(1, 1000000, \"x\"); inf.readSpace();\n inf.readInt(1, 1000000, \"y\"); inf.readEoln();\n }\n }\n ensuref(sumN <= 500000, \"sum N %lld > 5e5\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define int int64_t\nusing vi = vector<int>;\nusing pi = array<int, 2>;\n#define all(x) x.begin(), x.end()\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define nl '\\n'\n \nvoid solve() {\n int n; cin >> n;\n vi x(n), y(n);\n FOR(i, 0, n) cin >> x[i] >> y[i];\n sort(all(x));\n sort(all(y));\n for (int i = 0; i < n; i += 2) {\n if (x[i + 1] - x[i] >= 2\n or y[i + 1] - y[i] >= 2) {\n cout << \"NO\" << nl;\n return;\n }\n }\n auto get_range = [&] (vi v) -> pi {\n int lo = 0;\n for (int i = 0; i < n; i += 2) {\n lo += v[i] != v[i + 1];\n }\n int hi = 0;\n map<int, int> m;\n FOR(i, 0, n) m[v[i]]++;\n while (m.size()) {\n auto [i, c] = *m.begin();\n if (m.count(i + 1)) {\n int d = min(c, m[i + 1]);\n if ((m[i] - d) & 1) d--;\n m[i + 1] -= d;\n m[i] -= d;\n hi += d;\n }\n m.erase(i);\n }\n return {lo, hi};\n };\n auto [xl, xr] = get_range(x);\n auto [yl, yr] = get_range(y);\n for (int i = xl; i <= xr; i += 2) {\n if (clamp(n / 2 - i, yl, yr) == n / 2 - i) {\n if ((n / 2 - i) % 2 == yl % 2) {\n cout << \"YES\" << nl;\n return;\n }\n }\n }\n cout << \"NO\" << nl;\n}\n \nsigned main() {\n ios::sync_with_stdio(0); cin.tie(0);\n int t; cin >> t; while (t--) solve();\n}", "chk_cpp": null, "std_source_submission_id": 70967, "std_origin": "verbatim earliest root score=100 submission #70967"}
{"cpid": 1593, "title": "Good Cyclic Shifts", "contest": "Third Contest", "division": "Gold", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1593", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFor a permutation $p_1, p_2, \\dots, p_N$ of $1\\dots N$ ($1\\le N\\le 2\\cdot 10^5$), let $f(p) = \\sum_{i=1}^N \\frac{|p_i - i|}{2}$. A permutation is *good* if it can be turned into the identity permutation using at most $f(p)$ swaps of adjacent elements.\n\nGiven a permutation, find which cyclic shifts of it are good.\n\n## Input Format\n\nThe input consists of $T$ ($1\\le T\\le 10^5$) independent tests. Each test is specified as follows:\n\nThe first line contains $N$.\n\nThe second line contains $p_1, p_2, \\dots, p_N$ ($1\\le p_i\\le N$), which is guaranteed to be a permutation of $1\\dots N$.\n\nIt is guaranteed that the sum of $N$ over all tests does not exceed $10^6$.\n\n## Output Format\n\nFor each test, output two lines:\n\n- On the first line, output the number of good cyclic shifts $k$.\n- Then output a line with $k$ space-separated integers $s$ ($0\\le s < N$) in increasing order, meaning that $p$ is good when cyclically shifted to the right $s$ times.\n\n## Examples\n\n**Input 1**\n\n```\n3\n5\n5 4 3 2 1\n4\n1 2 4 3\n5\n1 2 3 4 5\n```\n\n**Output 1**\n\n```\n0\n\n2\n0 1\n5\n0 1 2 3 4\n```\n\n## Note\n\nConsider the second test case, where $p = [1, 2, 4, 3]$.\n\n$f(p) = (|1 - 1| + |2 - 2| + |4 - 3| + |3 - 4|) / 2 = 1$. Since $p$ can be turned into the identity permutation in one move by swapping $p_3$ and $p_4$, $p$ is good.\n\nCyclically shifting $p$ to the right $1$ time, we get $q = [3, 1, 2, 4]$. $f(q) = (|3 - 1| + |1 - 2| + |2 - 3| + |4 - 4|) / 2 = 2$. Since $q$ can be turned into the identity permutation in two moves by swapping $q_1$ two steps forward, $q$ is good.\n\nIt can be seen that the other two cyclic shifts are not good.\n\n## Scoring\n\n- Input 2: $N\\le 10$\n- Inputs 3-5: $T\\le 10$, $N\\le 2000$\n- Inputs 6-11: No additional constraints\n\n**Problem credits:** Akshaj Arora", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 1, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\n#include <set>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100000, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 200000, \"N\"); inf.readEoln();\n sumN += N;\n std::set<int> seen;\n for (int i = 1; i <= N; i++) {\n int p = inf.readInt(1, N, \"p_i\");\n ensuref(seen.insert(p).second, \"test %d: not a permutation, %d repeats\", tc, p);\n if (i < N) inf.readSpace();\n }\n inf.readEoln();\n }\n ensuref(sumN <= 1000000, \"sum N %lld > 1e6\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n \nvoid solve() {\n int n;\n cin >> n;\n vector<int> p(n);\n for (auto &i : p)\n cin >> i;\n \n // solve\n stack<int> s;\n vector<int> nl(n, -1);\n for (int j = 0; j < 2; j++) {\n for (int i = 0; i < n; i++) {\n while (s.size() && p[i] >= p[s.top()])\n s.pop();\n if (j == 1 && s.size())\n nl[i] = s.top();\n s.push(i);\n }\n }\n while (s.size())\n s.pop();\n vector<int> nr(n, -1);\n for (int j = 0; j < 2; j++) {\n for (int i = n - 1; i >= 0; i--) {\n while (s.size() && p[i] <= p[s.top()])\n s.pop();\n if (j == 1 && s.size())\n nr[i] = s.top();\n s.push(i);\n }\n }\n \n vector<int> ps(2 * n + 1);\n for (int i = 0; i < n; i++) {\n int l = nl[i], r = nr[i];\n if (min(l, r) == -1)\n continue;\n if ((l < i) ^ (i < r) ^ (l < r)) {\n if (l < r)\n l += n;\n ps[r + 1]++;\n ps[l + 1]--;\n }\n }\n for (int i = 1; i <= 2 * n; i++)\n ps[i] += ps[i - 1];\n vector<int> ans;\n for (int i = 1; i <= n; i++) {\n if (max(ps[i], ps[i + n]) == 0)\n ans.push_back(n - i);\n }\n reverse(ans.begin(), ans.end());\n \n // ans\n int m = size(ans);\n cout << m << '\\n';\n for (int i = 0; i < m; i++)\n cout << ans[i] << \" \\n\"[i == m - 1];\n if (m == 0)\n cout << \"\\n\";\n}\n \nsigned main() {\n cin.tie(0)->sync_with_stdio(0);\n int t = 1;\n cin >> t;\n while (t--)\n solve();\n}", "chk_cpp": null, "std_source_submission_id": 70951, "std_origin": "verbatim earliest root score=100 submission #70951"}
{"cpid": 1594, "title": "Picking Flowers", "contest": "Third Contest", "division": "Gold", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1594", "statement_md": "**Time limit:** 3 seconds **Memory limit:** 256 megabytes\n\n> **Note:** The time limit for this problem is 3s, 1.5x the default.\n\n## Description\n\nFarmer John's farm structure can be represented as a connected undirected graph with $N$ vertices and $M$ unweighted edges ($2\\le N\\le 2\\cdot 10^5$, $N - 1\\le M\\le 2\\cdot 10^5$). Initially, Farmer John is at his barn, represented by farm $1$.\n\nInitially, farms $s_1, s_2, \\ldots, s_K$ contain flower fields and farms $d_1, d_2, \\ldots, d_L$ are destination farms. FJ calls a path *pretty* if:\n\n- It starts at farm $1$.\n- It ends at some destination farm $x$.\n- There is no shorter path starting at farm $1$ and ending at farm $x$.\n- FJ visits all flower fields along the way.\n\nFJ can wave his magic wand and make up to one more farm contain a flower field (if it doesn't already). However, FJ isn't very decisive. For farms $f$ numbered $2$ through $N$, after FJ temporarily makes farm $f$ contain a flower field, determine if there exists a pretty path.\n\nNote that there are multiple test cases, and each case must be treated independently.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 100$), the number of independent test cases.\n\nThe first line of each test case contains $N$, $M$, $K$, and $L$ ($0\\le K\\le N-1$, $1\\le L\\le N-1$).\n\nThe following line contains $s_1, s_2, \\ldots, s_K$ ($2\\le s_i\\le N$, $s_i$ are all distinct).\n\nThe following line contains $d_1, d_2, \\ldots, d_L$ ($2\\le d_i\\le N$, $d_i$ are all distinct).\n\nThe following $M$ lines contain $u$ and $v$, denoting there is an undirected edge between farms $u$ and $v$. All edges are considered to have equal length. It is guaranteed that there aren't any multi-edges or self-loops.\n\nIt is guaranteed that both the sum of $N$ and the sum of $M$ do not exceed $10^6$ over all test cases.\n\n## Output Format\n\nFor each test case, output a binary string of length $N - 1$. The $i$-th character in the string should be $1$ if the answer holds true for the $(i+1)$-th farm.\n\n## Examples\n\n**Input 1**\n\n```\n1\n7 7 0 1\n\n5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n3 6\n```\n\n**Output 1**\n\n```\n111110\n```\n\n**Input 2**\n\n```\n1\n6 6 0 2\n\n5 3\n1 2\n2 3\n3 4\n4 5\n5 6\n2 5\n```\n\n**Output 2**\n\n```\n11010\n```\n\n**Input 3**\n\n```\n3\n4 3 2 1\n2 3\n4\n1 2\n2 3\n3 4\n4 4 2 1\n2 3\n4\n1 2\n1 3\n2 4\n3 4\n5 5 2 1\n2 4\n5\n1 2\n1 3\n2 4\n3 4\n4 5\n```\n\n**Output 3**\n\n```\n111\n000\n1011\n```\n\n## Note\n\nFor the first example, since $5$ is the only destination farm, the answer holds true if the $i$-th farm lies on any shortest path from $1$ to $5$. There are two shortest paths from $1$ to $5$: $1\\to 2\\to 3\\to 4\\to 5$ and $1\\to 2\\to 3\\to 6\\to 5$. Since there are no farms that already contain flower fields, the answer for farm $i$ holds true if farm $i$ lies on at least one of the two aforementioned paths.\n\nFor the second example, there are two destination farms: $5$ and $3$. Since there are no farms that already contain flower fields, the $i$-th farm must lie on a shortest path to either $5$ or $3$.\n\nFor the first test of the third example, the answer holds true for the $i$-th farm if FJ can pass through farm $i$, farm $2$, and farm $3$ (in no particular order) on some shortest path to farm $4$. It can be shown that the answer holds true for all farms.\n\n## Scoring\n\n- Inputs 4-6: $K = 0$ and $L = 1$\n- Inputs 7-9: $K = 0$\n- Inputs 10-23: No additional constraints\n\n**Problem credits:** Chongtian Ma", "checker_kind": "UOJ builtin: wcmp", "time_limit_s": 3.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 3, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\n#include <set>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100, \"T\"); inf.readEoln();\n long long sumN = 0, sumM = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(2, 200000, \"N\"); inf.readSpace();\n int M = inf.readInt(N - 1, 200000, \"M\"); inf.readSpace();\n int K = inf.readInt(0, N - 1, \"K\"); inf.readSpace();\n int L = inf.readInt(1, N - 1, \"L\"); inf.readEoln();\n sumN += N; sumM += M;\n std::set<int> sset;\n for (int i = 1; i <= K; i++) {\n int s = inf.readInt(2, N, \"s_i\");\n ensuref(sset.insert(s).second, \"test %d: s %d repeats\", tc, s);\n if (i < K) inf.readSpace();\n }\n inf.readEoln();\n std::set<int> dset;\n for (int i = 1; i <= L; i++) {\n int d = inf.readInt(2, N, \"d_i\");\n ensuref(dset.insert(d).second, \"test %d: d %d repeats\", tc, d);\n if (i < L) inf.readSpace();\n }\n inf.readEoln();\n for (int e = 1; e <= M; e++) {\n int u = inf.readInt(1, N, \"u\"); inf.readSpace();\n int v = inf.readInt(1, N, \"v\"); inf.readEoln();\n ensuref(u != v, \"test %d edge %d: self-loop not allowed\", tc, e);\n }\n }\n ensuref(sumN <= 1000000, \"sum N %lld > 1e6\", sumN);\n ensuref(sumM <= 1000000, \"sum M %lld > 1e6\", sumM);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define int int64_t\nusing vi = vector<int>;\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define all(x) x.begin(), x.end()\n#define nl '\\n'\n\nvoid solve() {\n int n, m, k, l; cin >> n >> m >> k >> l;\n vector<int> s(k); for (auto& i : s) cin >> i;\n vector<int> d(l); for (auto& i : d) cin >> i;\n vector<vi> g(n + 1);\n FOR(_, 0, m) {\n int u, v; cin >> u >> v;\n g[u].push_back(v); g[v].push_back(u);\n }\n vi layer[n];\n queue<int> q; q.push(1);\n vi dist(n + 1, -1); dist[1] = 0;\n while (q.size()) {\n int u = q.front(); q.pop();\n layer[dist[u]].push_back(u);\n for (int v : g[u]) if (dist[v] == -1) {\n dist[v] = dist[u] + 1;\n q.push(v);\n }\n }\n vi flower(n);\n int mx = 0;\n for (int i : s) {\n mx = max(mx, dist[i]);\n if (flower[dist[i]]) {\n cout << string(n - 1, '0') << nl;\n return;\n }\n flower[dist[i]] = i;\n }\n vi dp(n + 1);\n for (int i : d) {\n if (dist[i] > mx) dp[i] = 1;\n if (dist[i] == mx and flower[dist[i]]) dp[i] = 1;\n }\n for (int l = n - 2; l >= 0; l--) {\n for (int i : layer[l]) for (int j : g[i])\n if (flower[l + 1]) {\n if (j == flower[l + 1]) dp[i] |= dp[j];\n } else {\n if (dist[i] + 1 == dist[j]) dp[i] |= dp[j];\n }\n }\n vi dp2(n + 1);\n dp2[1] = 1;\n FOR(l, 0, n - 1) {\n for (int i : layer[l]) for (int j : g[i])\n if (flower[l + 1]) {\n if (j == flower[l + 1]) dp2[j] |= dp2[i];\n } else {\n if (dist[i] + 1 == dist[j]) dp2[j] |= dp2[i];\n }\n }\n FOR(i, 2, n + 1) cout << \"01\"[dp[i] and dp2[i]];\n cout << nl;\n}\n\nsigned main() {\n ios::sync_with_stdio(0); cin.tie(0);\n int t; cin >> t; while (t--) solve();\n}", "chk_cpp": null, "std_source_submission_id": 70986, "std_origin": "verbatim earliest root score=100 submission #70986"}
{"cpid": 1595, "title": "Random Tree Generation", "contest": "Third Contest", "division": "Gold", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1595", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nSuppose the function $\\text{randint}(l, r)$ returns an integer independently and uniformly at random from the range $[l, r]$.\n\nBessie generates a random labeled tree on $N$ vertices ($2\\le N\\le 2\\cdot 10^5$) using the following two-step process:\n\n1. Start with vertices labeled $1$ through $N$. For each $i$ from $2$ to $N$, add an edge between vertex $i$ and $\\text{randint}(1, i-1)$.\n2. Choose a permutation $p_1, p_2, \\dots, p_N$ of $\\{1, 2, \\ldots, N\\}$ uniformly at random. Relabel every vertex $v$ as $p_v$.\n\nNow, Farmer John is looking at the edge set of the final tree and wants to know the probability that the two-step process above produces a tree with exactly this edge set. Can you determine this probability modulo $10^9 + 7$?\n\n## Input Format\n\nThe input consists of $T$ ($1\\le T\\le 10$) independent inputs. Each input is specified as follows:\n\nThe first line contains $N$.\n\nThe next $N - 1$ lines contain the edges of the tree specified by two space-separated integers $u$ and $v$ ($1\\le u, v\\le N$). It is guaranteed that these edges induce a tree.\n\nIt is guaranteed that the sum of $N$ across all tests does not exceed $5\\cdot 10^5$.\n\n## Output Format\n\nFor each test, output the probability modulo $10^9 + 7$ on a new line (note that the output probability is a ratio of integers, so you will want to print the result of this division when working modulo $10^9 + 7$).\n\n## Examples\n\n**Input 1**\n\n```\n4\n2\n2 1\n3\n1 2\n2 3\n4\n1 2\n2 3\n2 4\n4\n1 2\n2 3\n3 4\n```\n\n**Output 1**\n\n```\n1\n333333336\n83333334\n55555556\n```\n\n## Note\n\nThe probabilities are $1$, $1/3$, $1/12$, $1/18$.\n\n- First test: There is only one tree on $N = 2$ vertices, so the probability of generating it is just $1$.\n- Second test: there are three trees on $N = 3$ vertices, and each of them is equally likely to have been generated by the process above. And $1/3\\equiv 333333336\\pmod{10^9 + 7}$.\n\n## Scoring\n\n- Inputs 2-3: $N\\le 8$\n- Inputs 4-9: $N\\le 2000$\n- Inputs 10-21: No additional constraints\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 1, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(2, 200000, \"N\"); inf.readEoln();\n sumN += N;\n for (int i = 1; i < N; i++) {\n int u = inf.readInt(1, N, \"u\"); inf.readSpace();\n int v = inf.readInt(1, N, \"v\"); inf.readEoln();\n ensuref(u != v, \"test %d edge %d: self-loop\", tc, i);\n }\n }\n ensuref(sumN <= 500000, \"sum N %lld > 5e5\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int MOD = 1e9 + 7;\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int t;\n cin >> t;\n while (t--) {\n int n;\n cin >> n;\n vector<vector<int>> adj(n);\n for (int i = 0; i < n - 1; i++) {\n int u, v;\n cin >> u >> v, u--, v--;\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n vector<int> inv(n + 1);\n inv[1] = 1;\n for (int i = 2; i <= n; i++)\n inv[i] = (ll)inv[MOD % i] * (MOD - MOD / i) % MOD;\n vector<int> sz(n), dp(n);\n auto dfs = [&](auto dfs, int p, int i) -> void {\n sz[i] = 1;\n dp[i] = 1;\n for (int j : adj[i])\n if (p != j) {\n dfs(dfs, i, j);\n sz[i] += sz[j];\n dp[i] = (ll)dp[i] * dp[j] % MOD;\n }\n dp[i] = (ll)dp[i] * inv[sz[i]] % MOD;\n };\n dfs(dfs, -1, 0); // dp[0] = inverse product for root\n auto make = [&](auto make, int p, int i) -> void {\n for (int j : adj[i])\n if (p != j) {\n dp[j] = (ll)dp[i] * sz[j] % MOD * inv[n - sz[j]] % MOD;\n make(make, i, j);\n }\n };\n make(make, -1, 0); // get inverse product (dp) for all other verts\n ll ways = 0;\n for (int i = 0; i < n; i++) ways += dp[i];\n ways %= MOD;\n for (int i = 1; i < n; i++)\n ways = ways * inv[i] % MOD; // divide by (n-1)!\n cout << ways << '\\n';\n }\n}", "chk_cpp": null, "std_source_submission_id": 70953, "std_origin": "verbatim earliest root score=100 submission #70953"}
{"cpid": 1596, "title": "All Pairs Shortest Paths", "contest": "Third Contest", "division": "Platinum", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1596", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nYou have a bunch of triangular regions that tessellate an infinite 2D plane. The tessellation is defined as follows (see the diagram for a better understanding):\n\n- Recall that Euler's formula states that $e^{ix} = \\cos(x) + i\\sin(x)$ for real $x$. First, draw a vertex at $x + y\\exp(\\pi i/3)$ on the complex plane for all integers $x, y$.\n- Then for every three vertices from the step above forming an equilateral triangle with side length $1$, draw the edges forming its border. Additionally, draw a vertex at the center of the triangle and edges from the center of the triangle to each of three outer vertices.\n\nYou are given $N$ ($2\\le N\\le 2\\cdot 10^5$) input points, each of which lies strictly within some region (i.e., not on any vertex or edge). For any pair of the input points, define the distance between them to be the smallest number of edges crossed when drawing a path from one point to the other that doesn't pass through any vertex.\n\nOutput the sum of all $N(N-1)/2$ pairwise distances between the input points.\n\n## Input Format\n\nThe first line contains $T$ ($T\\ge 1$), the number of independent tests. Each test is specified as follows:\n\nThe first line contains $N$.\n\nThe next $N$ lines each contain three integers $x$, $y$, and $z$ ($0\\le x, y < 10^6$, $0\\le z < 12$) representing a point at $x + y\\exp(\\pi i/3) + \\epsilon\\cdot \\exp((1 + 2z)\\pi i/12)$ on the complex plane ($\\epsilon$ being a small positive number).\n\nIt is guaranteed that the sum of $N$ over all tests doesn't exceed $2\\cdot 10^5$.\n\n## Output Format\n\nFor each test, output the sum of all $N(N-1)/2$ pairwise distances on a new line.\n\n## Examples\n\n**Input 1**\n\n```\n6\n2\n0 0 0\n0 0 0\n2\n0 0 0\n1 1 7\n2\n0 0 0\n0 0 6\n3\n0 0 1\n0 0 5\n0 0 9\n2\n0 2 11\n1 1 1\n2\n2 0 11\n1 1 1\n```\n\n**Output 1**\n\n```\n0\n3\n6\n12\n2\n6\n```\n\n## Note\n\nThe second test is illustrated by the following:\n\n- The vertex at $x + y\\exp(\\pi i/3)$ is labeled with $(x, y)$ for each $x\\in [-1, 2]$, $y\\in [-1, 2]$.\n- Dots are drawn at the vertices mentioned above as well as the vertices that are the centers of each equilateral triangle.\n- The triangular region containing $(x, y, z) = (0, 0, 0)$ is highlighted in green.\n- The triangular region containing $(x, y, z) = (1, 1, 7)$ is highlighted in blue. Note that $15\\pi/12 = 225°$.\n- An example path from the first region to the second crossing three edges is drawn.\n\n## Scoring\n\n- Inputs 2-5: $N\\le 10$, $0\\le x, y < 5$\n- Inputs 6-13: $N\\le 10$\n- Inputs 14-21: $T = 1$\n\n**Problem credits:** Benjamin Qi", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 20, "n_sample_tests": 1, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 2000000000, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 200000, \"N\"); inf.readEoln();\n sumN += N;\n for (int i = 1; i <= N; i++) {\n inf.readInt(0, 999999, \"x\"); inf.readSpace();\n inf.readInt(0, 999999, \"y\"); inf.readSpace();\n inf.readInt(0, 11, \"z\"); inf.readEoln();\n }\n }\n ensuref(sumN <= 200000, \"sum N %lld > 2e5\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve() {\n int n;\n cin >> n;\n vector<array<int, 3>> p(n);\n for (auto &[x, y, z] : p)\n cin >> x >> y >> z;\n\n // compute sum of pairwise abs difference given map of counts\n auto calc_dir = [&](map<int, int> &m) {\n ll ret = 0;\n int pos = m.begin()->first;\n int cnt = m.begin()->second;\n m.erase(m.begin());\n for (auto [k, v] : m) {\n ret += ll(k - pos) * cnt * (n - cnt);\n cnt += v;\n pos = k;\n }\n return ret;\n };\n\n // location in equilateral triangle grid\n // bool denotes inside downward-pointing triangle to the right\n vector<tuple<int, int, bool>> loc;\n // compute sum of pairwise distance in equilateral triangle grid\n auto calc = [&]() {\n ll ret = 0;\n\n map<int, int> m;\n for (auto [x, y, z] : loc)\n m[x]++;\n ret += calc_dir(m);\n\n m.clear();\n for (auto [x, y, z] : loc)\n m[y]++;\n ret += calc_dir(m);\n\n m.clear();\n for (auto [x, y, z] : loc)\n m[x + y + z]++;\n ret += calc_dir(m);\n\n return ret;\n };\n\n // equilateral triangle grid\n ll ans = 0;\n for (auto [x, y, z] : p) {\n loc.push_back({\n x - (z > 1 && z < 8),\n y - (z > 5),\n (z % 4) > 1,\n });\n }\n ans += calc();\n\n // upward hexagon grid\n loc.clear();\n for (auto [x, y, z] : p) {\n loc.push_back({\n x - (z > 2 && z < 7),\n y - (z > 6 && z != 11),\n 0,\n });\n }\n ans += calc() / 2;\n\n // downward hexagon grid\n loc.clear();\n for (auto [x, y, z] : p) {\n loc.push_back({\n x - (z != 0 && z < 9),\n y - (z == 0 || z > 4),\n 1,\n });\n }\n ans += calc() / 2;\n\n // count by rhombi by chain\n map<int, map<int, int>> m1, m2, m3;\n for (auto [x, y, z] : p) {\n int dir = z % 6;\n if (dir == 0 || dir == 5) // horizontal\n m1[y][x - (z == 5 || z == 6)]++;\n else if (dir == 1 || dir == 2) // up-right\n m2[x][y - (z == 7 || z == 8)]++;\n else // up-left\n m3[x + y][y - (z == 9 || z == 10)]++;\n }\n auto chains = [&](map<int, map<int, int>> &m) {\n for (auto [k, v] : m) {\n int total = 0;\n for (auto [k2, v2] : v) {\n total += v2;\n ans -= (ll)v2 * (v2 - 1) / 2;\n }\n ans += (ll)total * (total - 1) / 2;\n }\n };\n chains(m1);\n chains(m2);\n chains(m3);\n cout << ans << '\\n';\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int t;\n cin >> t;\n while (t--)\n solve();\n}", "chk_cpp": null, "std_source_submission_id": 70948, "std_origin": "verbatim earliest root score=100 submission #70948"}
{"cpid": 1597, "title": "Blast Damage", "contest": "Third Contest", "division": "Platinum", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1597", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nBessie is playing a video game where she needs to defeat a line of $N$ enemies with initial HPs given by the list $v_1\\dots v_N$ ($1\\le N\\le 2\\cdot 10^5$, $0\\le v_i\\le 10^9$). In one attack, she can perform the following sequence of steps:\n\n- Choose $i$ such that the $i$-th enemy is still alive (that is, $v_i > 0$).\n- Deal one damage to the $i$-th enemy and all enemies adjacent to it that are still alive. Specifically, for each $j\\in [\\max(i-1, 1), \\min(i+1, N)]$, if $v_j > 0$, subtract one from $v_j$.\n\nHelp Bessie determine the minimum number of attacks she needs to defeat all enemies (that is, reduce all $v_i$ to $0$).\n\nAdditionally, you are given a parameter $M$ ($0\\le M\\le 2$). If $M > 0$, please output a construction achieving the minimum number of attacks with a small number of *runs*, where a run consists of attacking the same enemy consecutively.\n\nLet $R$ be the number of runs in your construction. Your construction should be in the following format: output $R$ on its own line, followed by $R$ lines each containing two integers $i$ and $r$ ($1\\le i\\le N$, $0\\le r\\le 10^9$), meaning that Bessie attacks the $i$-th enemy $r$ times consecutively.\n\nDepending on the value of $M$, $R$ must satisfy one of the following constraints:\n\n- $M = 1$: $R\\le 2N$ (it can be proven that a construction always exists).\n- $M = 2$: $R\\le f(N)$, where $f(N)$ is the maximum minimum number of runs over all lists of length $N$.\n\n## Input Format\n\nEach input consists of $T$ ($1\\le T\\le 10^5$) independent tests. The first line contains $T$ and $M$.\n\nEach test is specified as follows:\n\nThe first line contains $N$.\n\nThe second line contains $v_1\\dots v_N$.\n\nIt is guaranteed that the sum of $N$ over all tests does not exceed $10^6$.\n\n## Output Format\n\nFor each test, output the minimum number of attacks on the first line.\n\nThen if $M > 0$, output $R + 1$ additional lines as specified above. Any valid construction will be accepted.\n\n## Examples\n\n**Input 1**\n\n```\n2 0\n1\n10\n3\n6 1 7\n```\n\n**Output 1**\n\n```\n10\n12\n```\n\n**Input 2**\n\n```\n2 1\n1\n10\n3\n6 1 7\n```\n\n**Output 2**\n\n```\n10\n2\n1 0\n1 10\n12\n4\n2 1\n1 5\n3 2\n3 4\n```\n\n**Input 3**\n\n```\n2 2\n1\n10\n3\n6 1 7\n```\n\n**Output 3**\n\n```\n10\n1\n1 10\n12\n3\n2 1\n3 6\n1 5\n```\n\n## Note\n\nFor the second test of the first example, you can first perform one attack on the middle enemy. Then, in any order after that, perform five attacks on the first enemy and six attacks on the last enemy.\n\nThe output of the second example receives credit because $R = 2\\le 2$ for test $1$ and $R = 4\\le 6$ for test $2$.\n\nThe output of the third example receives credit because $R = 1\\le f(1)$ for test $1$ and $R = 3\\le f(3)$ for test $2$.\n\n## Scoring\n\n- Inputs 4-7: $M = 0$\n- Inputs 8-11: $M = 1$\n- Inputs 12-13: $M = 2$\n\n**Problem credits:** Benjamin Qi", "checker_kind": "custom (chk.cpp, testlib)", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 10, "n_sample_tests": 3, "point_scores": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100000, \"T\"); inf.readSpace();\n inf.readInt(0, 2, \"M\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 200000, \"N\"); inf.readEoln();\n sumN += N;\n for (int i = 1; i <= N; i++) { inf.readInt(0, 1000000000, \"v_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n }\n ensuref(sumN <= 1000000, \"sum N %lld > 1e6\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <class T> using V = vector<T>;\n#define all(x) begin(x), end(x)\n\nusing ll = long long;\n\nint M;\n\npair<ll, vector<pair<int, ll>>> get_ops(const vector<ll> &v) {\n int N = size(v);\n\n // get op count\n vector<int> a(N);\n for (int i = 0; i < N; ++i) { // satisfy i-th type 2 constraint by increasing a_{i+1} and a_i\n ll remaining = v.at(i) - a.at(i);\n if (i > 0) remaining -= a.at(i - 1);\n remaining = max(remaining, 0LL);\n if (i + 1 < N) {\n a.at(i + 1) = min(remaining, v.at(i + 1));\n remaining -= a.at(i + 1);\n }\n a.at(i) += remaining;\n }\n ll op_count = accumulate(begin(a), end(a), 0LL);\n\n // construction\n auto sensitive = [&](int i) {\n assert(0 <= i && i < N);\n return ((i == 0 ? 0 : a.at(i - 1)) + a.at(i) +\n (i + 1 == N ? 0 : a.at(i + 1)) >\n v.at(i)) &&\n a.at(i);\n };\n vector<pair<int, ll>> runs;\n for (int i = 0; i < N; ++i)\n if (sensitive(i)) {\n if (i) assert(!sensitive(i - 1));\n // sanity check: no two consecutive sensitive indices\n runs.push_back({i, a.at(i)});\n }\n for (int i = 0; i < N; ++i)\n if (!sensitive(i) && a.at(i)) { runs.push_back({i, a.at(i)}); }\n return {op_count, runs};\n}\n\nvoid solve() {\n int N;\n cin >> N;\n vector<ll> v(N);\n for (auto &t : v) cin >> t;\n auto ans = get_ops(v);\n bool rev = false;\n if (N % 2 == 0 && size(ans.second) == N) { // for M=2\n rev = true;\n reverse(all(v));\n ans = get_ops(v);\n assert(size(ans.second) < N);\n }\n const auto &[op_count, runs] = ans;\n cout << op_count << \"\\n\";\n if (M) {\n cout << size(runs) << \"\\n\";\n for (auto [i, r] : runs)\n cout << (rev ? N - i : i + 1) << \" \" << r << \"\\n\";\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int T;\n cin >> T >> M;\n while (T--) solve();\n}", "chk_cpp": "#include \"testlib.h\"\n#include <vector>\nusing namespace std;\ntypedef long long ll;\n\nint main(int argc, char *argv[]) {\n setName(\"USACO 2026 Platinum: Blast Damage\");\n registerTestlibCmd(argc, argv);\n\n int T = inf.readInt();\n int M = inf.readInt();\n\n for (int t = 1; t <= T; t++) {\n int N = inf.readInt();\n vector<ll> v(N + 2, 0);\n for (int i = 1; i <= N; i++) v[i] = inf.readLong();\n\n ll cnt_ref = ans.readLong();\n ll cnt = ouf.readLong();\n if (cnt != cnt_ref)\n quitf(_wa, \"test %d: minimum attack count differs (expected %lld, got %lld)\",\n t, cnt_ref, cnt);\n\n if (M > 0) {\n // Read reference's R + R lines (consume only)\n int R_ref = ans.readInt();\n for (int j = 0; j < R_ref; j++) { ans.readInt(); ans.readLong(); }\n\n int R = ouf.readInt(0, (int)2e9, \"R\");\n\n // Bound check\n if (M == 1) {\n if (R > 2 * N)\n quitf(_wa, \"test %d: R=%d exceeds 2N=%d (M=1)\", t, R, 2 * N);\n } else { // M == 2\n // f(N) = max over all length-N lists of the minimum number of\n // runs = N if N is odd, else N-1 (see editorial). R_ref (the\n // reference's run count on THIS list) is only a lower bound on\n // f(N), so bounding by R_ref would wrongly reject valid\n // constructions that use up to f(N) runs.\n int fN = (N % 2 == 1) ? N : N - 1;\n if (R > fN)\n quitf(_wa, \"test %d: R=%d exceeds f(N)=%d (M=2)\",\n t, R, fN);\n }\n\n ll total_r = 0;\n for (int j = 0; j < R; j++) {\n int idx = ouf.readInt(1, N, \"i\");\n ll r = ouf.readLong();\n if (r < 0 || r > (ll)1e9)\n quitf(_wa, \"test %d: run #%d r=%lld out of [0, 1e9]\", t, j + 1, r);\n if (r == 0) continue;\n if (v[idx] <= 0)\n quitf(_wa, \"test %d: run #%d attacks dead enemy %d\", t, j + 1, idx);\n if (r > v[idx])\n quitf(_wa, \"test %d: run #%d r=%lld exceeds v[%d]=%lld (would attack dead)\",\n t, j + 1, r, idx, v[idx]);\n ll a = (idx > 1) ? v[idx - 1] : 0;\n ll c = (idx < N) ? v[idx + 1] : 0;\n v[idx] -= r;\n if (idx > 1) v[idx - 1] = max((ll)0, a - r);\n if (idx < N) v[idx + 1] = max((ll)0, c - r);\n total_r += r;\n }\n\n if (total_r != cnt)\n quitf(_wa, \"test %d: sum of run lengths = %lld != minimum attacks = %lld\",\n t, total_r, cnt);\n\n for (int i = 1; i <= N; i++)\n if (v[i] != 0)\n quitf(_wa, \"test %d: enemy %d still has HP %lld after construction\",\n t, i, v[i]);\n }\n }\n\n if (!ouf.seekEof())\n quitf(_wa, \"extra tokens in contestant output\");\n quitf(_ok, \"T=%d cases, M=%d\", T, M);\n}\n", "std_source_submission_id": 70949, "std_origin": "verbatim earliest root score=100 submission #70949"}
{"cpid": 1598, "title": "Min Max Subarrays II", "contest": "Third Contest", "division": "Platinum", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1598", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nYou are given integers $N, Q$ ($1\\le N, Q\\le 2\\cdot 10^5$) and $Q$ constraints represented by four integers $t_i, l_i, r_i, k_i$ ($1\\le t_i\\le 2$, $1\\le l_i\\le r_i\\le N$, $0\\le k_i\\le 10^9$, all $k_i$ are distinct).\n\nConstruct an array $a$ consisting of $N$ integers between $0$ and $10^9$ such that for all $1\\le i\\le Q$, $\\min a[l_i\\ldots r_i] = k_i$ if $t_i = 1$ and $\\max a[l_i\\ldots r_i] = k_i$ if $t_i = 2$. If multiple valid arrays exist, print any. If no valid array exists, print $-1$.\n\n## Input Format\n\nThe first line contains an integer $T$ ($1\\le T\\le 10^4$) representing the number of independent test cases.\n\nFor each test case, the first line contains two integers $N, Q$.\n\nThe next $Q$ lines each contain $4$ integers $t_i$ $l_i$ $r_i$ $k_i$.\n\nIt is guaranteed that neither the sum of $N$ nor the sum of $Q$ over all test cases exceed $2\\cdot 10^5$.\n\n## Output Format\n\nFor each test case, if a valid array exists, output $N$ space-separated integers $a_1\\dots a_N$ on a new line. Otherwise, print $-1$.\n\n## Examples\n\n**Input 1**\n\n```\n3\n2 2\n1 1 2 1\n1 1 2 2\n2 2\n1 1 2 1\n1 2 2 2\n4 1\n2 2 4 3\n```\n\n**Output 1**\n\n```\n-1\n1 2\n0 3 0 0\n```\n\n**Input 2**\n\n```\n4\n2 2\n1 1 2 1\n2 1 2 2\n3 2\n1 1 2 3\n2 2 3 1\n5 2\n1 1 2 3\n1 4 5 2\n4 4\n1 1 4 1\n1 2 3 2\n2 1 2 5\n2 3 4 6\n```\n\n**Output 2**\n\n```\n1 2\n-1\n3 3 0 2 2\n1 5 2 6\n```\n\n## Note\n\nFor the first example: in the first test case, the answer is $-1$ because the minimum value of the array cannot be both $1$ and $2$ at the same time. In the second test case, $a[1\\ldots 2]$ has a minimum of $1$ at $a[1]$ in the sample output, satisfying the first constraint. Since $a[2] = 2$, the second constraint is also satisfied. In the third test case, there are multiple solutions. For instance, the array $[4, 3, 2, 1]$ would also be accepted.\n\nFor the second example: in the second test case, the array $[3, 5, 1]$ satisfies the first constraint but not the second constraint. Contrarily, the array $[3, 1, 1]$ satisfies the second constraint but not the first constraint. It can be proven that no array can satisfy both constraints at the same time, hence the answer is $-1$. For all other test cases, it can be proven that the constructed array satisfies all $Q$ constraints.\n\n## Scoring\n\n- Inputs 3-4: $N, Q\\le 100$ and all $t_i$ within the same test case are equal\n- Inputs 5-6: All $t_i$ within the same test case are equal\n- Inputs 7-10: $N, Q\\le 100$\n- Inputs 11-14: No additional constraints\n\n**Problem credits:** Charlie Yang", "checker_kind": "custom (chk.cpp, testlib)", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 12, "n_sample_tests": 2, "point_scores": [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12], "val_cpp": "#include \"testlib.h\"\n#include <set>\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 10000, \"T\"); inf.readEoln();\n long long sumN = 0, sumQ = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 200000, \"N\"); inf.readSpace();\n int Q = inf.readInt(1, 200000, \"Q\"); inf.readEoln();\n sumN += N; sumQ += Q;\n std::set<int> ks;\n for (int i = 1; i <= Q; i++) {\n inf.readInt(1, 2, \"t\"); inf.readSpace();\n int l = inf.readInt(1, N, \"l\"); inf.readSpace();\n int r = inf.readInt(l, N, \"r\"); inf.readSpace();\n int k = inf.readInt(0, 1000000000, \"k\"); inf.readEoln();\n ensuref(ks.insert(k).second, \"test %d: k value %d repeats (all k distinct)\", tc, k);\n }\n }\n ensuref(sumN <= 200000, \"sum N %lld > 2e5\", sumN);\n ensuref(sumQ <= 200000, \"sum Q %lld > 2e5\", sumQ);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <set>\n\nusing namespace std;\n\nconst int N = 200000;\nconst int M = 200000;\n\nint tt[M], rr[M], aa[M];\nvector<int> hh[N];\nset<pair<int, int>> qul, qur;\nvector<int> ei[M];\nint hhl[N], hhr[N], bb[N];\nint visited[M], i_;\n\nvoid dfs(int p, int h) {\n\tif (visited[h]) {\n\t\ti_ = p;\n\t\treturn;\n\t}\n\tvisited[h] = 1;\n\tfor (int i : ei[h])\n\t\tif (i != p)\n\t\t\tdfs(i, h ^ hhl[i] ^ hhr[i]);\n}\n\nvoid dfs_(int h) {\n\tif (visited[h] == 2)\n\t\treturn;\n\tvisited[h] = 2;\n\tfor (int i : ei[h])\n\t\tif (bb[i] == -1) {\n\t\t\tint g = h ^ hhl[i] ^ hhr[i];\n\t\t\tbb[i] = aa[g];\n\t\t\tdfs_(g);\n\t\t}\n}\n\nint main() {\n\tios_base::sync_with_stdio(false), cin.tie(NULL);\n\tint tc; cin >> tc;\n\twhile (tc--) {\n\t\tint n, m; cin >> n >> m;\n\t\tfor (int i = 0; i < n; i++)\n\t\t\thh[i].clear();\n\t\tfor (int h = 0; h < m; h++) {\n\t\t\tint t, l, r, a; cin >> t >> l >> r >> a, l--;\n\t\t\ttt[h] = t, rr[h] = r, aa[h] = a;\n\t\t\thh[l].push_back(h << 1 ^ 0);\n\t\t\tif (r < n)\n\t\t\t\thh[r].push_back(h << 1 ^ 1);\n\t\t}\n\t\tbool yes = true;\n\t\tqul.clear(), qur.clear();\n\t\tfor (int h = 0; h < m; h++)\n\t\t\tei[h].clear();\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int h_ : hh[i]) {\n\t\t\t\tint h = h_ >> 1;\n\t\t\t\tif (!(h_ & 1)) {\n\t\t\t\t\tif (tt[h] == 1)\n\t\t\t\t\t\tqul.insert({ aa[h], h });\n\t\t\t\t\telse\n\t\t\t\t\t\tqur.insert({ aa[h], h });\n\t\t\t\t} else {\n\t\t\t\t\tif (tt[h] == 1)\n\t\t\t\t\t\tqul.erase({ aa[h], h });\n\t\t\t\t\telse\n\t\t\t\t\t\tqur.erase({ aa[h], h });\n\t\t\t\t}\n\t\t\t}\n\t\t\tint hl = qul.empty() ? -1 : (*prev(qul.end())).second;\n\t\t\tint hr = qur.empty() ? -1 : (*qur.begin()).second;\n\t\t\tbb[i] = -1;\n\t\t\tif (hl != -1 && hr != -1) {\n\t\t\t\tif (aa[hl] > aa[hr]) {\n\t\t\t\t\tyes = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tei[hhl[i] = hl].push_back(i);\n\t\t\t\tei[hhr[i] = hr].push_back(i);\n\t\t\t} else if (hl != -1) {\n\t\t\t\tei[hhl[i] = hl].push_back(i);\n\t\t\t\tei[hhr[i] = hl].push_back(i);\n\t\t\t} else if (hr != -1) {\n\t\t\t\tei[hhl[i] = hr].push_back(i);\n\t\t\t\tei[hhr[i] = hr].push_back(i);\n\t\t\t}\n\t\t}\n\t\tif (!yes) {\n\t\t\tcout << \"-1\\n\";\n\t\t\tcontinue;\n\t\t}\n\t\tfor (int h = 0; h < m; h++)\n\t\t\tvisited[h] = 0;\n\t\tfor (int h_ = 0; h_ < m; h_++)\n\t\t\tif (!visited[h_]) {\n\t\t\t\ti_ = -1, dfs(-1, h_);\n\t\t\t\tif (i_ == -1) {\n\t\t\t\t\tyes = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tint hl = hhl[i_];\n\t\t\t\tbb[i_] = aa[hl];\n\t\t\t\tdfs_(hl);\n\t\t\t}\n\t\tif (!yes) {\n\t\t\tcout << \"-1\\n\";\n\t\t\tcontinue;\n\t\t}\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tcout << (bb[i] != -1 ? bb[i] : 0) << \" \\n\"[i + 1 == n];\n\t}\n\treturn 0;\n}", "chk_cpp": "#include \"testlib.h\"\n#include <vector>\nusing namespace std;\ntypedef long long ll;\n\nint main(int argc, char *argv[]) {\n setName(\"USACO 2026 Platinum: Min Max Subarrays II\");\n registerTestlibCmd(argc, argv);\n\n int T = inf.readInt();\n for (int t = 1; t <= T; t++) {\n int N = inf.readInt();\n int Q = inf.readInt();\n vector<int> tt(Q), ll_(Q), rr(Q);\n vector<ll> kk(Q);\n for (int i = 0; i < Q; i++) {\n tt[i] = inf.readInt();\n ll_[i] = inf.readInt();\n rr[i] = inf.readInt();\n kk[i] = inf.readLong();\n }\n\n // Reference output: -1 or N integers.\n // Use the first token to detect.\n string firstAns = ans.readToken();\n bool refImpossible = (firstAns == \"-1\");\n if (!refImpossible) {\n // already consumed first integer; consume remaining N-1\n for (int i = 1; i < N; i++) ans.readLong();\n }\n\n string firstOuf = ouf.readToken();\n if (firstOuf == \"-1\") {\n if (!refImpossible)\n quitf(_wa, \"test %d: contestant says impossible but reference shows feasible\", t);\n continue;\n }\n\n if (refImpossible)\n quitf(_wa, \"test %d: contestant claims feasible but reference says impossible\", t);\n\n // Parse contestant's N values: firstOuf is the first integer.\n vector<ll> a(N);\n try {\n a[0] = stoll(firstOuf);\n } catch (...) {\n quitf(_wa, \"test %d: first token '%s' is not an integer\", t, firstOuf.c_str());\n }\n for (int i = 1; i < N; i++) a[i] = ouf.readLong();\n\n for (int i = 0; i < N; i++)\n if (a[i] < 0 || a[i] > (ll)1e9)\n quitf(_wa, \"test %d: a[%d]=%lld out of [0, 1e9]\", t, i + 1, a[i]);\n\n // Build sparse tables for min and max for O(1) range query.\n int LOG = 1;\n while ((1 << LOG) <= N) LOG++;\n vector<vector<ll>> mn(LOG, vector<ll>(N)), mx(LOG, vector<ll>(N));\n for (int i = 0; i < N; i++) mn[0][i] = mx[0][i] = a[i];\n for (int k = 1; k < LOG; k++) {\n int len = 1 << k;\n for (int i = 0; i + len <= N; i++) {\n mn[k][i] = min(mn[k - 1][i], mn[k - 1][i + (len >> 1)]);\n mx[k][i] = max(mx[k - 1][i], mx[k - 1][i + (len >> 1)]);\n }\n }\n auto qmin = [&](int l, int r) {\n int k = __lg(r - l + 1);\n return min(mn[k][l], mn[k][r - (1 << k) + 1]);\n };\n auto qmax = [&](int l, int r) {\n int k = __lg(r - l + 1);\n return max(mx[k][l], mx[k][r - (1 << k) + 1]);\n };\n\n for (int i = 0; i < Q; i++) {\n ll val = (tt[i] == 1) ? qmin(ll_[i] - 1, rr[i] - 1) : qmax(ll_[i] - 1, rr[i] - 1);\n if (val != kk[i])\n quitf(_wa, \"test %d: constraint %d (t=%d l=%d r=%d k=%lld) violated; computed value=%lld\",\n t, i + 1, tt[i], ll_[i], rr[i], kk[i], val);\n }\n }\n\n if (!ouf.seekEof())\n quitf(_wa, \"extra tokens in contestant output\");\n quitf(_ok, \"T=%d cases\", T);\n}\n", "std_source_submission_id": 70983, "std_origin": "verbatim earliest root score=100 submission #70983"}
{"cpid": 1602, "title": "Arranging Cows", "contest": "US Open", "division": "Platinum-ish (US Open)", "problem_number": 1, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1602", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nYou are given a length-$N$ bitstring $s_{1\\ldots N}$ ($2\\le N\\le 10^9$). In one operation, you can reverse a range $s_{l\\ldots r}$ if the following conditions are true:\n\n- The size of the range is even.\n- The first half of the range consists of one character (either $0$ or $1$), and the second half contains the opposite character.\n- Either $l=1$ or $s_{l-1}\\ne s_l$.\n- Either $r=N$ or $s_{r+1}\\ne s_r$.\n\nFind the minimum number of operations to move all of the $1$s to the front, or report that it is impossible. If it is possible to do so, also output the number of sequences of operations achieving this minimum, modulo $10^9+7$.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 2026$), the number of independent tests. Each test is specified in the following format:\n\nThe bitstring is given in a compressed format. The first line contains $R$, the number of runs in the string ($2\\le R\\le 800$), and the first character of the string (either `0` or `1`).\n\nThe next line contains $R$ space-separated integers $l_1, l_2, l_3, \\ldots, l_R$ ($0 < l_i < 10^9$), the lengths of maximal consecutive blocks of equal characters in $s$. It is guaranteed that $N = \\sum_{i=1}^{R} l_i \\le 10^9$.\n\nAdditionally, it is guaranteed that the sum of $R^2$ over all tests does not exceed $1.5 \\cdot 10^6$.\n\n## Output Format\n\nFor each test case, print the minimum number of operations to move all of the $1$s to the front or $-1$ if it is impossible, as well as the number of sequences of operations achieving this minimum modulo $10^9+7$.\n\n## Examples\n\n**Input 1**\n\n```\n9\n2 0\n1 1\n2 1\n1 1\n2 1\n2 1\n2 0\n1 2\n5 0\n1 1 1 2 1\n3 0\n1 2 1\n8 0\n1 1 2 1 1 2 1 1\n6 0\n3 3 1 2 2 1\n7 0\n5 1 1 3 2 1 1\n```\n\n**Output 1**\n\n```\n1 1\n0 1\n0 1\n-1 0\n2 1\n-1 0\n4 7\n3 1\n4 1\n```\n\nHere is the sequence of two operations for the fifth testcase: $010110 \\to 100110 \\to 111000$.\n\n**Input 2**\n\n```\n5\n2 1\n1 1\n4 1\n1 1 1 1\n6 1\n1 1 1 1 1 1\n8 1\n1 1 1 1 1 1 1 1\n10 1\n1 1 1 1 1 1 1 1 1 1\n```\n\n**Output 2**\n\n```\n0 1\n1 1\n2 1\n3 3\n4 9\n```\n\nIn all of these test cases, the minimum number of operations equals $R/2-1$.\n\nHere are all three possible sequences of three operations for the fourth test case:\n\n```\n(1)\n 10101010\n-> 11001010\n-> 11001100\n-> 11110000\n\n(2)\n 10101010\n-> 10110010\n-> 10001110\n-> 11110000\n\n(3)\n 10101010\n-> 10101100\n-> 11001100\n-> 11110000\n```\n\n## Scoring\n\n- Input 3: $N\\le 10$, all tests are distinct.\n- Input 4: $R\\le 10$.\n- Inputs 5-8: $R\\le 100$, the sum of $R^2$ over all tests does not exceed $10^5$, the minimum number of operations is guaranteed to equal $R/2-1$.\n- Inputs 9-12: $R\\le 100$, the sum of $R^2$ over all tests does not exceed $10^5$.\n- Inputs 13-16: No additional constraints.\n\n**Problem credits:** Sujay Konda", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 256, "n_tests": 14, "n_sample_tests": 2, "point_scores": [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 2026, \"T\"); inf.readEoln();\n long long sumR2 = 0;\n for (int tc = 1; tc <= T; tc++) {\n int R = inf.readInt(2, 800, \"R\"); inf.readSpace();\n int first = inf.readInt(0, 1, \"first_char\"); inf.readEoln();\n (void)first;\n sumR2 += (long long)R * R;\n long long N = 0;\n for (int i = 1; i <= R; i++) {\n int l = inf.readInt(1, 999999999, \"l_i\"); // 0 < l_i < 1e9\n N += l;\n if (i < R) inf.readSpace();\n }\n inf.readEoln();\n ensuref(N <= 1000000000LL, \"test %d: N=%lld > 1e9\", tc, N);\n }\n ensuref(sumR2 <= 1500000LL, \"sum R^2 %lld > 1.5e6\", sumR2);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int LGN = 30;\nconst int INF = 1e9;\nconst int MOD = 1e9 + 7;\nusing ll = long long;\n\nconst int MXM = 1000;\n\nint f[MXM + 1], invf[MXM + 1];\n\nint mulm(int x, int y) {\n return (ll)x * y % MOD;\n}\n\nint bpow(int x, int y) {\n return (y == 0 ? 1 : mulm(bpow(mulm(x, x), y / 2), (y % 2 ? x : 1)));\n}\n\nint choose(int n, int k) {\n if(k > n) return 0;\n if(k < 0) return 0;\n assert(n >= 0);\n return mulm(mulm(f[n], invf[k]), invf[n - k]);\n}\n\nvoid tc() {\n int M; cin >> M; char c; cin >> c;\n vector<int> a;\n for(int i = 0; i < LGN; i++)\n a.push_back(0);\n for(int i = 0; i < M; i++) {\n int ai; cin >> ai;\n a.push_back(ai);\n }\n for(int i = 0; i < LGN; i++)\n a.push_back(0);\n\n vector<int> p(M + 2 * LGN);\n for(int i = 2; i < M + 2 * LGN; i++) {\n p[i] = a[i] + p[i - 2];\n }\n\n vector<vector<bool>> dp(M + 2 * LGN, vector<bool>(M + 2 * LGN));\n vector<vector<int>> dp2(M + 2 * LGN, vector<int>(M + 2 * LGN));\n\n map<int, vector<pair<int, int>>> trans;\n\n // add possible transitions from l, r\n auto add_trans = [&] (int l, int r) {\n if(r < LGN || l >= M + LGN) return;\n\n trans[p[r - 1] + p[l - 1]].push_back({l, r});\n };\n \n for(int i = 0; i < M + 2 * LGN - 1; i++) {\n dp[i][i + 1] = true;\n dp2[i][i + 1] = 1;\n add_trans(i, i + 1);\n }\n\n for(int sz = 4; sz <= M + 2 * LGN; sz += 2) {\n for(int l = 1; l + sz - 1 < M + 2 * LGN; l++) {\n int r = l + sz - 1;\n for(auto [u, v] : trans[p[r - 1] + p[l - 1]]) {\n if(l < u && v < r && dp[l][u] && dp[v][r]) {\n dp[l][r] = true;\n int ways = mulm(f[(r - l) / 2 - 1], mulm(invf[(r - v) / 2], mulm(invf[(v - u) / 2], invf[(u - l) / 2])));\n dp2[l][r] += mulm(ways, mulm(dp2[u][v], mulm(dp2[l][u], dp2[v][r])));\n dp2[l][r] %= MOD;\n } \n }\n if(dp[l][r])\n add_trans(l, r);\n }\n }\n int ans = INF;\n int ans2 = 0;\n for(int l = 0; l <= LGN; l++) {\n for(int r = M + LGN - 1; r < M + 2 * LGN; r++) {\n if(dp[l][r] && (LGN - l) % 2 == (c == '0')) {\n if ((r - l) / 2 < ans) {\n ans = (r - l) / 2;\n ans2 = 0;\n }\n if((r - l) / 2 == ans) {\n ans2 += dp2[l][r];\n ans2 %= MOD;\n }\n }\n }\n }\n cout << ((ans == INF) ? -1 : ans) << \" \";\n cout << ans2 << endl;\n}\n\nint main() {\n\n f[0] = 1;\n for(int i = 1; i <= MXM; i++) {\n f[i] = mulm(f[i - 1], i);\n }\n invf[MXM] = bpow(f[MXM], MOD - 2);\n for(int i = MXM; i >= 1; i--) {\n invf[i - 1] = mulm(invf[i], i);\n }\n\n ios::sync_with_stdio(false), cin.tie(nullptr);\n int T; cin >> T;\n while(T--) tc();\n}", "chk_cpp": null, "std_source_submission_id": 169142, "std_origin": "verbatim earliest root score=100 submission #169142"}
{"cpid": 1603, "title": "Haybale Stacks", "contest": "US Open", "division": "Platinum-ish (US Open)", "problem_number": 2, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1603", "statement_md": "**Time limit:** 2.5 seconds **Memory limit:** 256 megabytes\n\n## Description\n\nFarmer John has $N$ stacks of haybales ($1\\le N\\le 5\\cdot 10^5$), where the $i$-th stack contains $a_i$ haybales ($1\\le a_i\\le 10^9$). He wants to remove all of these haybales and has $M$ ($1\\le M\\le 2500$) cows available to help him. If hired, the $i$-th cow will repeat the following $s_i$ times ($1\\le s_i\\le 100$) for a cost of $c_i$ ($1\\le c_i\\le 10^9$):\n\n- If the stack contains at least $p_i$ haybales ($1\\le p_i\\le 10^9$), then the cow will remove one haybale.\n- If the stack contains less than $p_i$ haybales, the cow does nothing.\n\nFor each stack, FJ wants to remove all of the haybales in it. He will do this by hiring cows in sequence (possibly the same cow more than once) until the stack becomes empty. Help FJ determine for each stack the minimum cost to empty it.\n\n## Input Format\n\nThe first line contains $T$ ($1\\le T\\le 100$), the number of independent tests. Each test is formatted as follows:\n\nThe first line contains an integer $N$. The second line contains $N$ integers, $a_1, a_2, \\ldots, a_N$.\n\nThe third line contains an integer $M$. Then the next $M$ lines will contain $p_i, s_i, c_i$.\n\nIt is guaranteed that the cows will be able to remove all the haybales in every stack. Additionally, it is guaranteed that the sum of $N$ over all tests does not exceed $5\\cdot 10^5$, and the sum of $M$ over all tests does not exceed $2500$.\n\n## Output Format\n\nFor each test, print $N$ space-separated integers, the $i$-th integer being the cost of removing all the haybales in the $i$-th stack.\n\n## Examples\n\n**Input 1**\n\n```\n2\n3\n15 100 10\n4\n101 1 1\n1 4 8\n9 3 5\n15 2 3\n3\n15 100 10\n4\n101 1 1\n1 1 5\n9 1 8\n15 1 3\n```\n\n**Output 1**\n\n```\n29 155 21\n73 328 50\n```\n\n## Note\n\n**First test:** For the last stack of initial size $10$, we can hire cow $3$ once, which costs $5$ and will remove haybales twice (not thrice because the number of haybales turns to $8$ after the second one is removed). Then we can hire cow $2$ twice, removing the $8$ haybales, resulting in no haybales left. The total cost is $5+8+8=21$.\n\n**Second test:** This satisfies $\\max(s)=1$.\n\n## Scoring\n\n- Inputs 2-3: $a_i\\le 100$.\n- Inputs 4-5: $\\max(s)=1$.\n- Inputs 6-9: $\\max(s)\\le 4$.\n- Inputs 10-15: $\\max(s)\\le 20$.\n- Inputs 16-21: No additional constraints.\n\n**Problem credits:** Sujay Konda", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.5, "memory_limit_mb": 256, "n_tests": 19, "n_sample_tests": 2, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100, \"T\"); inf.readEoln();\n long long sumN = 0, sumM = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 500000, \"N\"); inf.readEoln();\n sumN += N;\n for (int i = 1; i <= N; i++) { inf.readInt(1, 1000000000, \"a_i\"); if (i < N) inf.readSpace(); }\n inf.readEoln();\n int M = inf.readInt(1, 2500, \"M\"); inf.readEoln();\n sumM += M;\n for (int i = 1; i <= M; i++) {\n inf.readInt(1, 1000000000, \"p_i\"); inf.readSpace();\n inf.readInt(1, 100, \"s_i\"); inf.readSpace();\n inf.readInt(1, 1000000000, \"c_i\"); inf.readEoln();\n }\n }\n ensuref(sumN <= 500000, \"sum N %lld > 5e5\", sumN);\n ensuref(sumM <= 2500, \"sum M %lld > 2500\", sumM);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1e18;\n \nconst int S = 100;\n \nvoid upd(ll& a, ll b) { a = min(a, b); }\n \nint main() {\n ios::sync_with_stdio(false), cin.tie(nullptr);\n int t; cin >> t;\n while(t--) {\n ll n; cin >> n;\n vector<ll> a(n), oa(n);\n for(int i = 0; i < n; i++) {\n cin >> a[i];\n oa[i] = i;\n }\n sort(oa.begin(), oa.end(), [&] (ll x, ll y) { return a[x] < a[y]; });\n ll m; cin >> m;\n vector<ll> l(m), s(m), v(m), o(m);\n for(int i = 0; i < m; i++) {\n cin >> l[i] >> s[i] >> v[i];\n l[i]--;\n o[i] = i;\n }\n sort(o.begin(), o.end(), [&] (ll x, ll y) { return l[x] < l[y]; });\n int pl = 0;\n vector<ll> dp(S * S, INF); dp[0] = 0;\n vector<ll> best(S + 1, 1e15);\n \n // computes expdp which is used to query\n vector<ll> expdp;\n int bsi = 1;\n auto compute = [&] (int nsi) {\n for(int si = 1; si <= S; si++) {\n if(best[bsi] * si > bsi * best[si]) {\n bsi = si;\n }\n }\n expdp = dp;\n for(int i = 0; i < expdp.size(); i++) {\n if(i + nsi < expdp.size()) {\n upd(expdp[i + nsi], expdp[i] + best[nsi]);\n }\n }\n };\n // uses expdp to query\n auto query = [&] (ll x) {\n ll d = x - pl;\n ll t = max((d - (ll)expdp.size()) / bsi + 1, 0ll);\n return t * best[bsi] + expdp[d - bsi * t];\n };\n int k = 0;\n int lasts = 0;\n vector<ll> ans(n);\n for(int i : o) {\n compute(lasts);\n while(k < n && a[oa[k]] < l[i]) ans[oa[k]] = query(a[oa[k]]), k++;\n vector<ll> ndp(S * S, INF);\n for(int j = 0; j < S * S; j++) {\n ndp[j] = query(l[i] + j);\n }\n for(int j = 1; j < s[i]; j++) upd(ndp[j], ndp[0] + v[i]);\n upd(best[s[i]], v[i]);\n lasts = s[i];\n dp = ndp;\n pl = l[i];\n }\n compute(lasts);\n while(k < n) ans[oa[k]] = query(a[oa[k]]), k++;\n for(ll i : ans) cout << i << \" \";\n cout << endl;\n }\n}\n", "chk_cpp": null, "std_source_submission_id": 169143, "std_origin": "verbatim earliest root score=100 submission #169143"}
{"cpid": 1604, "title": "Perfect Binary Trees", "contest": "US Open", "division": "Platinum-ish (US Open)", "problem_number": 3, "source_url": "https://usaco.org/index.php?page=viewproblem2&cpid=1604", "statement_md": "**Time limit:** 2 seconds **Memory limit:** 512 megabytes\n\n## Description\n\nA *perfect binary tree* is a rooted tree where every non-leaf node has exactly two children and all leaf nodes are at an equal distance from the root.\n\nAn *unrooted perfect binary tree* is an unrooted tree that is a perfect binary tree when rooted at one of its nodes.\n\nBessie has a tree with $N$ ($1\\le N\\le 10^5$) nodes. Determine the number of ways to remove a subset of edges from the tree so that the resulting forest is a collection of unrooted perfect binary trees. As the answer may be very large, output the result modulo $10^9+7$.\n\n## Input Format\n\nThe first line contains an integer $T$ ($1\\le T\\le 100$), the number of independent test cases.\n\nThe first line of each test case contains an integer $N$.\n\nEach of the next $N-1$ lines of each test case contains two integers $u_i$ and $v_i$ ($1\\le u_i, v_i\\le N$) indicating an edge between nodes $u_i$ and $v_i$.\n\nIt is guaranteed that for each test case, the given edges form a tree with $N$ nodes.\n\nAdditionally, the sum of $N$ over all test cases does not exceed $2\\cdot 10^5$.\n\n## Output Format\n\nFor each test case, output a single integer: the number of subsets of edges that, when removed, result in a forest that is a collection of unrooted perfect binary trees, modulo $10^9+7$.\n\n## Examples\n\n**Input 1**\n\n```\n3\n6\n1 2\n3 2\n4 6\n5 6\n6 2\n3\n1 2\n3 2\n7\n2 1\n2 3\n1 6\n1 7\n3 4\n3 5\n```\n\n**Output 1**\n\n```\n8\n2\n14\n```\n\n## Note\n\nIn the first test case, Bessie can remove any of the following subsets of edges to get a forest of perfect binary trees:\n\n- $(2,6)$\n- $(1,2)$, $(2,3)$, $(2,6)$\n- $(1,2)$, $(2,3)$, $(4,6)$\n- $(1,2)$, $(2,3)$, $(5,6)$\n- $(1,2)$, $(4,6)$, $(5,6)$\n- $(2,6)$, $(4,6)$, $(5,6)$\n- $(2,3)$, $(4,6)$, $(5,6)$\n- $(1,2)$, $(2,3)$, $(2,6)$, $(4,6)$, $(5,6)$\n\nThe first subset results in two subtrees of height $1$, the last subset results in six subtrees of height $0$, and the other subsets result in three subtrees of height $0$ and one subtree of height $1$.\n\n## Scoring\n\n- Inputs 2-3: $N\\le 15$.\n- Inputs 4-5: No node is adjacent to more than two other nodes.\n- Inputs 6-9: $N\\le 1000$, the sum of $N$ does not exceed $2000$, and no node is adjacent to more than three other nodes.\n- Inputs 10-13: No node is adjacent to more than three other nodes.\n- Inputs 14-21: No additional constraints.\n\n**Problem credits:** Avnith Vijayram", "checker_kind": "UOJ builtin: ncmp", "time_limit_s": 2.0, "memory_limit_mb": 512, "n_tests": 19, "n_sample_tests": 2, "point_scores": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10], "val_cpp": "#include \"testlib.h\"\nint main(int argc, char *argv[]) {\n registerValidation(argc, argv);\n int T = inf.readInt(1, 100, \"T\"); inf.readEoln();\n long long sumN = 0;\n for (int tc = 1; tc <= T; tc++) {\n int N = inf.readInt(1, 100000, \"N\"); inf.readEoln();\n sumN += N;\n for (int i = 1; i < N; i++) {\n int u = inf.readInt(1, N, \"u\"); inf.readSpace();\n int v = inf.readInt(1, N, \"v\"); inf.readEoln();\n ensuref(u != v, \"test %d edge %d: self-loop\", tc, i);\n }\n }\n ensuref(sumN <= 200000, \"sum N %lld > 2e5\", sumN);\n inf.readEof(); return 0;\n}\n", "std_cpp": "#include <bits/stdc++.h>\nusing namespace std;\n \nusing ll = long long;\ntemplate<class T> using V = vector<T>;\nconst ll M = 1e9+7;\nconst int LGN = 17;\n \nvoid solve() {\n int n; cin >> n;\n V<V<int>> adj(n);\n for (int i = 0; i < n-1; i++) {\n int u, v; cin >> u >> v; u--; v--;\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n V<ll> ans(n, 0);\n V<V<ll>> dp(LGN, V<ll>(n, 0)), pd(LGN, V<ll>(n, 0));\n \n auto dfs = [&](auto&& dfs, int x, int p) -> void {\n ll sums[LGN][3][2];\n memset(sums, 0, sizeof(sums));\n for (int i = 0; i < LGN; i++) sums[i][0][0] = 1;\n \n for (auto y : adj[x]) {\n if (y == p) continue;\n dfs(dfs, y, x);\n \n for (int i = 0; i < LGN; i++) {\n for (int j = 2; j >= 0; j--) {\n for (int k = 1; k >= 0; k--) {\n if (j < 2) {\n sums[i][j+1][k] += (i > 0 ? dp[i-1][y] : 0LL) * sums[i][j][k] % M;\n sums[i][j+1][k] %= M;\n } \n if (k < 1) {\n sums[i][j][k+1] += pd[i][y] * sums[i][j][k] % M;\n sums[i][j][k+1] %= M;\n }\n sums[i][j][k] *= ans[y];\n sums[i][j][k] %= M;\n }\n }\n }\n }\n \n dp[0][x] += sums[0][0][0];\n for (int i = 0; i < LGN; i++) {\n if (i < LGN-1) dp[i+1][x] += sums[i+1][2][0];\n \n if (i < LGN-1) pd[i][x] += sums[i+1][1][0];\n if (i > 0) pd[i-1][x] += sums[i][1][1];\n \n ans[x] += dp[i][x];\n if (i == 0) ans[x] += sums[i][0][1];\n else ans[x] += sums[i][2][1];\n }\n for (int i = 0; i < LGN; i++) {\n dp[i][x] %= M;\n pd[i][x] %= M; \n }\n ans[x] %= M;\n };\n \n dfs(dfs, 0, -1);\n cout << ans[0] << endl;\n}\n \nsigned main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n \n int t; cin >> t;\n while (t--) solve();\n}", "chk_cpp": null, "std_source_submission_id": 169144, "std_origin": "verbatim earliest root score=100 submission #169144"}
|