Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 51; map<int, int> mp; int n, kk, MOD, res, u, v, w, x; int invf[MAXN], pr[MAXN]; inline int read() { register int num = 0, neg = 1; register char ch = getchar(); while (!isdigit(ch) && ch != '-') { ch = getchar(); } if (ch == '-') { neg = -1; ch = getchar(); } while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch - '0'); ch = getchar(); } return num * neg; } inline void dfs(int l, int r, int depth) { if (depth >= kk || l == r) { return (void)mp[r - l + 1]++; } int mid = (l + r) >> 1; dfs(l, mid, depth + 1), dfs(mid + 1, r, depth + 1); } inline int calc(int x, int y) { int res = 0; for (register int i = 1; i <= x; i++) { res = (res + (pr[i + y] - pr[i] + MOD) % MOD * 2 % MOD) % MOD; } return (((long long int)x * y - res) % MOD + MOD) % MOD; } int main() { n = read(), kk = read(), MOD = read(), invf[1] = pr[1] = 1, dfs(1, n, 1); for (register int i = 2; i <= max(n, 2); i++) { invf[i] = MOD - (long long int)(MOD / i) * invf[MOD % i] % MOD, pr[i] = (pr[i - 1] + invf[i]) % MOD; } for (auto i : mp) { tie(u, v) = i, res = (res + (long long int)u * (u - 1) / 2 % MOD * v) % MOD; v >= 2 ? res = (res + (long long int)v * (v - 1) / 2 % MOD * calc(u, u)) % MOD : 1; } for (auto i : mp) { for (auto j : mp) { if (i.first < j.first) { tie(u, v) = i, tie(w, x) = j; res = (res + (long long int)calc(u, w) * v % MOD * x % MOD) % MOD; } } } printf("%d\n", (long long int)res * invf[2] % MOD); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, P; vector<int> pos; int cnt[N]; void solve(int l, int r, int k) { if (k <= 1 || l == r) { if (cnt[r - l + 1] == 0) pos.emplace_back(r - l + 1); cnt[r - l + 1]++; return; } int mid = (l + r) / 2; solve(l, mid, k - 1); solve(mid + 1, r, k - 1); return; } long long inv[N], sinv[N]; void init(int n = 100000) { inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = (P - P / i) * inv[P % i] % P; sinv[0] = 0; for (int i = 1; i <= n; i++) sinv[i] = (sinv[i - 1] + inv[i]) % P; return; } long long calc(int x, int y) { long long ans = 0; for (int i = 1; i <= x; i++) { long long res = inv[2] * y % P; res = (res - (sinv[i + y] - sinv[i] + P) % P + P) % P; ans = (ans + res) % P; } return ans; } int main() { scanf("%d%d%d", &n, &k, &P); init(); solve(1, n, k); int len = pos.size(); long long ans = 0; for (int i = 0; i < len; i++) ans = (ans + 1LL * pos[i] * (pos[i] - 1) % P * inv[4] % P * cnt[pos[i]] % P) % P; for (int i = 0; i < len; i++) for (int j = i + 1; j < len; j++) ans = (ans + calc(pos[i], pos[j]) * cnt[pos[i]] % P * cnt[pos[j]] % P) % P; for (int i = 0; i < len; i++) ans = (ans + calc(pos[i], pos[i]) * cnt[pos[i]] % P * (cnt[pos[i]] - 1) % P * inv[2] % P) % P; printf("%lld", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int mod, num1 = 0, num2 = 0, cnt1 = 0, cnt2 = 0, rev[N], sum[N]; void add(int &x, int y) { x += y; if (x >= mod) { x -= mod; } } void sub(int &x, int y) { x -= y; if (x < 0) { x += mod; } } int mul(int x, int y) { return (1ll * x * y) % mod; } int binpow(int x, int y) { int tich = 1; while (y) { if (y & 1) { tich = mul(tich, x); } x = mul(x, x); y >>= 1; } return tich; } void recur(int l, int r, int dep) { if (dep < 2 || l == r) { if (!num1 || r - l + 1 == num1) { num1 = r - l + 1; cnt1++; } else { num2 = r - l + 1; cnt2++; } return; } recur(l, (l + r) / 2, dep - 1); recur((l + r) / 2 + 1, r, dep - 1); } signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, i, j, k, l, dep, ans = 0; cin >> n >> dep >> mod; for (i = 1; i < N; i++) { rev[i] = binpow(i, mod - 2); sum[i] = sum[i - 1]; add(sum[i], rev[i]); } recur(1, n, dep); if (num1) { add(ans, mul(cnt1, mul(num1, mul(num1 - 1, rev[4])))); j = mul(mul(cnt1, cnt1 - 1), rev[2]); for (i = 1; i <= num1; i++) { add(ans, mul(mul(rev[2], num1), j)); sub(ans, mul((sum[i + num1] - sum[i] + mod) % mod, j)); } } if (num2) { add(ans, mul(cnt2, mul(num2, mul(num2 - 1, rev[4])))); j = mul(mul(cnt2, cnt2 - 1), rev[2]); for (i = 1; i <= num2; i++) { add(ans, mul(mul(rev[2], num2), j)); sub(ans, mul((sum[i + num2] - sum[i] + mod) % mod, j)); } } if (num1 && num2) { j = mul(cnt1, cnt2); for (i = 1; i <= num1; i++) { add(ans, mul(mul(rev[2], num2), j)); sub(ans, mul((sum[i + num2] - sum[i] + mod) % mod, j)); } } cout << ans; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int n, k, mod, cnt[MAXN], inv[MAXN] = {0, 1}, sum[MAXN]; int a, b, ans; inline int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x; } inline void Add(int &x, int y) { x += y, x >= mod && (x -= mod); } inline void prepare() { for (int i = 2; i <= n * 2; i++) inv[i] = mod - 1ll * mod / i * inv[mod % i] % mod; for (int i = 1; i <= n * 2; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; } void dfs_pre(int l, int r, int dep, int &len) { if (dep == k || l == r) return cnt[len = r - l + 1]++, void(); int mid = (l + r) >> 1; dfs_pre(l, mid, dep + 1, len); dfs_pre(mid + 1, r, dep + 1, len); } inline int calc(int x, int y) { int ans = 0; for (int i = 1; i <= x; i++) Add(ans, (mod + sum[i + y] - sum[i]) % mod); return ans; } int main() { n = read(), k = read(), mod = read(); if (k >= 19) { puts("0"); return 0; } dfs_pre(1, n, 1, a), prepare(); if (cnt[a - 1]) b = a - 1; else if (cnt[a + 1]) b = a + 1; assert((cnt[a - 1] & cnt[a + 1]) == 0); Add(ans, 1ll * calc(a, a) * cnt[a] % mod * (cnt[a] - 1) % mod * inv[2] % mod); Add(ans, 1ll * calc(b, b) * cnt[b] % mod * (cnt[b] - 1) % mod * inv[2] % mod); Add(ans, 1ll * calc(a, b) * cnt[a] % mod * cnt[b] % mod); printf("%lld\n", (1ll * n * (n - 1) % mod * inv[4] % mod - ans + mod) % mod); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; int n, k, p, sum, a[2], b[2], inv[N], res[N]; inline void adds(int u) { if (a[0] == 0) a[0] = u; else if (a[0] != u) a[1] = u; if (a[0] == u) b[0]++; if (a[1] == u) b[1]++; } inline void solve(int l, int r, int t) { if (t == 1 || l == r) { adds(r - l + 1); return; } int d = (l + r) >> 1; solve(l, d, t - 1), solve(d + 1, r, t - 1); } inline int getsum(int x, int y) { int ans = 0; memset(res, 0, sizeof(res)); for (int i = 1; i <= x; i++) res[i + 1]++, res[i + y + 1]--; for (int i = 1; i <= x + y; i++) res[i] += res[i - 1], ans = (ans + 1ll * res[i] % p * (-inv[i] + inv[2] + p)) % p; return ans; } int main() { cin >> n >> k >> p, solve(1, n, k), inv[0] = inv[1] = 1; for (int i = 2; i <= 123456; i++) inv[i] = 1ll * (p - p / i) * inv[p % i] % p; sum = (1ll * (a[0] - 1) * a[0] % p * inv[4] % p * b[0] % p + 1ll * (a[1] - 1) * a[1] % p * inv[4] % p * b[1] % p) % p; sum = (sum + 1ll * b[0] * b[1] % p * getsum(a[0], a[1])) % p; sum = (sum + 1ll * b[0] * (b[0] - 1) / 2 % p * getsum(a[0], a[0])) % p; sum = (sum + 1ll * b[1] * (b[1] - 1) / 2 % p * getsum(a[1], a[1])) % p; cout << sum << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<ll, ll>; using vi = vc<ll>; template <class t, class u> ostream& operator<<(ostream& os, const pair<t, u>& p) { return os << "{" << p.first << "," << p.second << "}"; } template <class t> ostream& operator<<(ostream& os, const vc<t>& v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } using uint = unsigned; using ull = unsigned long long; uint mod = 1; struct mint { uint v; mint(ll vv = 0) { s(vv % mod + mod); } mint& s(uint vv) { v = vv < mod ? vv : vv - mod; return *this; } mint operator-() const { return mint() - *this; } mint& operator+=(const mint& rhs) { return s(v + rhs.v); } mint& operator-=(const mint& rhs) { return s(v + mod - rhs.v); } mint& operator*=(const mint& rhs) { v = ull(v) * rhs.v % mod; return *this; } mint& operator/=(const mint& rhs) { return *this *= rhs.inv(); } mint operator+(const mint& rhs) const { return mint(*this) += rhs; } mint operator-(const mint& rhs) const { return mint(*this) -= rhs; } mint operator*(const mint& rhs) const { return mint(*this) *= rhs; } mint operator/(const mint& rhs) const { return mint(*this) /= rhs; } mint pow(ll n) const { mint res(1), x(*this); while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } mint inv() const { return pow(mod - 2); } friend ostream& operator<<(ostream& os, const mint& m) { return os << m.v; } }; const ll Vmax = (1 << 21) + 10; mint fact[Vmax], finv[Vmax], inv[Vmax]; void initfact() { fact[0] = 1; for (ll i = ll(1); i < ll(Vmax); i++) { fact[i] = fact[i - 1] * i; } finv[Vmax - 1] = fact[Vmax - 1].inv(); for (ll i = Vmax - 2; i >= 0; i--) { finv[i] = finv[i + 1] * (i + 1); } for (ll i = Vmax - 1; i >= 1; i--) { inv[i] = finv[i] * fact[i - 1]; } } mint choose(ll n, ll k) { return fact[n] * finv[n - k] * finv[k]; } mint binom(ll first, ll second) { return fact[first + second] * finv[first] * finv[second]; } mint catalan(ll n) { return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0); } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); ll n, h; cin >> n >> h; cin >> mod; initfact(); chmin(h, 20); ll s = min(ll(1) << (h - 1), n); ll sz[2], cnt[2]; sz[0] = n / s; sz[1] = sz[0] + 1; cnt[1] = n % s; cnt[0] = s - cnt[1]; mint ans = 0; for (ll i = ll(1); i < ll(n); i++) { ll k = n - i + 1; mint den = choose(n, k) * k * (k - 1); mint num = 0; for (ll first = ll(0); first < ll(2); first++) for (ll second = ll(0); second < ll(2); second++) { mint wab = cnt[first] * (cnt[second] - (first == second)); mint sum = 0; for (ll c = ll(0); c < ll(2); c++) for (ll d = ll(0); d < ll(2); d++) { mint x = 0; ll rem = n; if (c) rem -= sz[first]; if (d) rem -= sz[second]; if (rem >= k) x = choose(rem, k); if (c ^ d) sum -= x; else sum += x; } num += wab * sum; } ans += (mint(1) - num / den) / 2 * i; } cout << ans << endl; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int md; inline void add(int &a, int b) { a += b; if (a >= md) a -= md; } inline void sub(int &a, int b) { a -= b; if (a < 0) a += md; } inline int mul(int a, int b) { return (int)((long long)a * b % md); } inline int power(int a, long long b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } inline int inv(int a) { a %= md; if (a < 0) a += md; int b = md, u = 0, v = 1; while (a) { int t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += md; return u; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k >> md; if (k >= 20 || n <= (1 << (k - 1))) { cout << 0 << '\n'; return 0; } int bc = (1 << (k - 1)); int small_size = n / bc; int big_size = small_size + 1; int big_cnt = n % bc; int small_cnt = bc - big_cnt; vector<int> blocks(bc); for (int i = 0; i < n; i++) { blocks[i % (int)blocks.size()]++; } map<int, int> mp; for (int x : blocks) { mp[x]++; } vector<int> fact(n + 1), inv_fact(n + 1); fact[0] = inv_fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i); inv_fact[i] = inv(fact[i]); } int ans = 0; for (int b1id = 0; b1id < bc; b1id++) { int b = blocks[b1id]; add(ans, mul(mul(b, b - 1), inv(4))); } vector<int> sum_inv(n + 1); for (int i = 0; i < n; i++) { sum_inv[i + 1] = sum_inv[i]; add(sum_inv[i + 1], inv(i + 1)); } for (int b1id = 0; b1id < bc; b1id++) { int b1 = blocks[b1id]; if (b1 == small_size) small_cnt--; else big_cnt--; for (int x = 2; x <= b1; x++) { if (small_cnt > 0) { int aux = sum_inv[x + small_size]; sub(aux, sum_inv[x]); int prob = mul(x - 1, aux); add(ans, mul(small_cnt, mul(prob, inv(2)))); } if (big_cnt > 0) { int aux = sum_inv[x + big_size]; sub(aux, sum_inv[x]); int prob = mul(x - 1, aux); add(ans, mul(big_cnt, mul(prob, inv(2)))); } } if (b1 == small_size) small_cnt++; else big_cnt++; } cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> T gi() { T x = 0; bool f = 0; char c = getchar(); while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') f = 1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f ? -x : x; } const int N = 1e5 + 10; int Mod, inv[N]; unordered_map<int, int> t; void solve(int l, int r, int k) { if (k == 1 || l == r) return (void)t[r - l + 1]++; int mid = (l + r) >> 1; solve(l, mid, k - 1), solve(mid + 1, r, k - 1); } int C(int n) { return 1ll * n * (n - 1) / 2 % Mod; } int calc(int x, int y) { int res = 1ll * (Mod + 1) / 2 * x % Mod * y % Mod; for (int i = 1; i <= x; i++) res = (1ll * res + Mod - inv[i + y] + inv[i]) % Mod; return res; } int main() { int n = gi<int>(), k = gi<int>(), ans = 0; Mod = gi<int>(); inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod; for (int i = 2; i <= n; i++) (inv[i] += inv[i - 1]) %= Mod; solve(1, n, k); for (auto i : t) ans = (ans + 1ll * C(i.first) * i.second % Mod * ((Mod + 1) / 2)) % Mod, ans = (ans + 1ll * C(i.second) * calc(i.first, i.first)) % Mod; for (auto i : t) for (auto j : t) { if (j.first >= i.first) break; ans = (ans + 1ll * calc(j.first, i.first) * j.second % Mod * i.second) % Mod; } cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline void chkmin(T1 &x, T2 y) { if (y < x) x = y; } template <class T1, class T2> inline void chkmax(T1 &x, T2 y) { if (y > x) x = y; } const int BUF_SIZE = 1 << 20; char buf[BUF_SIZE], *P1 = buf, *P2 = buf, obuf[BUF_SIZE], *PO = obuf; inline char getc() { if (P1 == P2) P2 = (P1 = buf) + fread(buf, 1, BUF_SIZE, stdin); return P1 == P2 ? EOF : *P1++; } inline void read(int &x) { register char ch = getc(); x = 0; while (!isdigit(ch)) ch = getc(); while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getc(); } inline void flushO() { fwrite(obuf, PO - obuf, 1, stdout); PO = obuf; } inline void putc(char ch) { if (PO == obuf + (BUF_SIZE)) flushO(); *PO++ = ch; } inline void prints(char s[]) { for (char *ss = s; *ss != '\0'; ss++) putc(*ss); } inline void write(long long x) { if (x > 9) write(x / 10); putc(x % 10 ^ 48); } const int N = 100005; int MOD; inline int mo(int x) { return x >= MOD ? x - MOD : x; } struct mint { int x; mint() {} mint(int a) { x = a; } }; inline mint operator+(mint a, mint b) { return mo(a.x + b.x); } inline mint operator+=(mint &a, mint b) { return a = a + b; } inline mint operator-(mint a, mint b) { return mo(a.x + MOD - b.x); } inline mint operator-(mint a) { return mo(MOD - a.x); } inline mint operator-=(mint &a, mint b) { return a = a - b; } inline mint operator*(mint a, mint b) { return 1ll * a.x * b.x % MOD; } inline mint operator*=(mint &a, mint b) { return a = a * b; } inline mint operator^(mint a, int b) { mint res = mint{1}; for (; b; b >>= 1, a *= a) if (b & 1) res *= a; return res; } inline mint Inv(mint a) { return a ^ MOD - 2; } inline mint operator/(mint a, mint b) { return a * Inv(b); } inline mint operator/=(mint &a, mint b) { return a = a / b; } int n, k, la, lb, ca, cb; mint inv[N], sinv[N], ans; inline void math_init(int n) { inv[1] = 1; for (int i = 2; i <= (n); i++) inv[i] = -inv[MOD % i] * (MOD / i); for (int i = 1; i <= (n); i++) sinv[i] = sinv[i - 1] + inv[i]; } void solve(int l, int r, int h) { if (h <= 1 || l == r) { int len = r - l + 1; ans += inv[4] * len * (len - 1) + (ca * la + cb * lb) * inv[2] * len; for (int i = 1; i <= (len); i++) ans -= (sinv[la + i] - sinv[i]) * ca + (sinv[lb + i] - sinv[i]) * cb; if (!la) la = len; if (len != la && !lb) lb = len; if (len == la) ca++; else cb++; return; } int mid = l + r >> 1; solve(l, mid, h - 1); solve(mid + 1, r, h - 1); } int main() { scanf("%d%d%d", &n, &k, &MOD); math_init(max(n, 4)); solve(1, n, k); printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, mod, cnt[100005], s[100005], inv[100005], L = 100005, R, ans; void divide(int l, int r, int h) { if (h <= 1 || l == r) { cnt[r - l + 1]++, L = min(L, r - l + 1), R = max(R, r - l + 1); return; } int mid = (l + r) >> 1; divide(l, mid, h - 1), divide(mid + 1, r, h - 1); } int calc(int x, int y) { int ret = 1ll * x * y % mod * inv[2] % mod; for (int i = 1; i <= x; i++) ret = (ret - 1ll * (s[i + y] - s[i])) % mod; return (ret + mod) % mod; } int main() { scanf("%d%d%d", &n, &k, &mod); divide(1, n, k); inv[0] = inv[1] = s[1] = 1; for (int i = 2; i <= max(4, n); i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod, s[i] = (s[i - 1] + inv[i]) % mod; for (int i = L; i <= R; i++) { ans = (ans + 1ll * i * (i - 1) % mod * inv[4] % mod * cnt[i]) % mod; ans = (ans + 1ll * cnt[i] * (cnt[i] - 1) % mod * inv[2] % mod * calc(i, i)) % mod; for (int j = i + 1; j <= R; j++) ans = (ans + 1ll * cnt[i] * cnt[j] % mod * calc(i, j)) % mod; } printf("%d\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int MOD; int inv(long long int n) { long long int r = 1; int e = MOD - 2; while (e > 0) { if (e & 1) r *= n, r %= MOD; e >>= 1; n *= n, n %= MOD; } return r; } map<int, int> occ; vector<pair<int, int> > v; int findSizes(int s, int h) { if ((h == 1) || (s == 1)) occ[s]++; else findSizes(s / 2, h - 1), findSizes((s + 1) / 2, h - 1); return 0; } int main() { int n, k; cin >> n >> k >> MOD; findSizes(n, k); for (auto it = occ.begin(); it != occ.end(); it++) v.push_back(*it); int i, j; long long int ans = 0; for (i = 0; i < v.size(); i++) { long long int x = ((long long int)v[i].first * (v[i].first - 1)) % MOD; x *= inv(4), x %= MOD; ans += v[i].second * x, ans %= MOD; } for (i = 0; i < v.size(); i++) { for (j = i; j < v.size(); j++) { long long int o = ((long long int)v[i].second * v[j].second) % MOD; if (i == j) o = ((long long int)v[i].second * (v[i].second - 1) / 2) % MOD; for (k = 2; k <= v[i].first + v[j].first; k++) { long long int c = (inv(2) - inv(k) + MOD) % MOD; c *= o, c %= MOD; ans += c * (min(k - 1, v[i].first) - max(k - v[j].first, 1) + 1), ans %= MOD; } } } cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 1e5; int n, k, q, l1, l2, c1, c2; long long iv[mxN + 1], pi[mxN + 1], ans; void ms(int l = 0, int r = n - 1, int h = k) { if (h < 2 || l == r) { if (!l1 || r - l + 1 == l1) { l1 = r - l + 1; ++c1; } else { l2 = r - l + 1; ++c2; } return; } int m = (l + r) / 2; ms(l, m, h - 1); ms(m + 1, r, h - 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> q; iv[1] = 1; for (int i = 2; i <= n; ++i) iv[i] = (q - q / i) * iv[q % i] % q; for (int i = 1; i <= n; ++i) pi[i] = (pi[i - 1] + iv[i]) % q; ms(); if (c1 > 1) for (int i = 1; i <= l1; ++i) ans += (pi[l1 + i] - pi[i] + q) * c1 % q * (c1 - 1) % q; if (c2 > 1) for (int i = 1; i <= l2; ++i) ans += (pi[l2 + i] - pi[i] + q) * c2 % q * (c2 - 1) % q; for (int i = 1; i <= l1; ++i) ans += (pi[l2 + i] - pi[i] + q) * c1 % q * c2 % q * 2; cout << (iv[2] * n % q * (n - 1) % q - ans % q + q) * iv[2] % q; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void in(T &x) { x = 0; char c = getchar(); bool f = 0; while (!isdigit(c)) f |= (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar(); f ? x = -x : 0; } template <class T> inline void out(T x, const char c = '\n') { static short st[30]; short m = 0; if (x < 0) putchar('-'), x = -x; do st[++m] = x % 10, x /= 10; while (x); while (m) putchar(st[m--] | '0'); putchar(c); } template <class T> inline void err(const T &x, const char c = '\n') { cerr << x << c; } template <class T, class... Args> inline void in(T &x, Args &...args) { in(x); in(args...); } template <class T, class... Args> inline void out(const T &x, const Args &...args) { out(x, ' '); out(args...); } template <class T, class... Args> inline void err(const T &x, const Args &...args) { err(x, ' '); err(args...); } template <class T> inline void prt(T a[], int n) { for (register int i = 0; i < n; ++i) out(a[i], i == n - 1 ? '\n' : ' '); } template <class T> inline void clr(T a[], int n) { memset(a, 0, sizeof(T) * n); } template <class T> inline void clr(T *a, T *b) { memset(a, 0, sizeof(T) * (b - a)); } template <class T> inline bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class T> inline bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } namespace MOD_CALC { int md; inline int add(const int a, const int b) { return a + b >= md ? a + b - md : a + b; } inline int sub(const int a, const int b) { return a - b < 0 ? a - b + md : a - b; } inline int mul(const int a, const int b) { return (long long)a * b % md; } inline void inc(int &a, const int b) { (a += b) >= md ? a -= md : 0; } inline void dec(int &a, const int b) { (a -= b) < 0 ? a += md : 0; } inline int qpow(int a, int b) { int r = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) r = mul(r, a); return r; } inline int qpow(int a, int b, const int p) { int r = 1; for (; b; b >>= 1, a = (long long)a * a % p) if (b & 1) r = (long long)r * a % p; return r; } inline int mdinv(const int a) { return qpow(a, md - 2); } template <class... Args> inline int add(const int a, const int b, const Args &...args) { return add(add(a, b), args...); } template <class... Args> inline int mul(const int a, const int b, const Args &...args) { return mul(mul(a, b), args...); } } // namespace MOD_CALC using namespace MOD_CALC; namespace i207M { int iv[200005]; int inv2; void prework() { iv[1] = 1; for (register int i = 2; i < 200005; ++i) iv[i] = mul(md - md / i, iv[md % i]); for (register int i = 2; i < 200005; ++i) inc(iv[i], iv[i - 1]); } map<int, int> cnt; void divide(int l, int r, int h) { if (h <= 1 || l == r) { ++cnt[r - l + 1]; return; } int mid((l + r) / 2); divide(l, mid, h - 1); divide(mid + 1, r, h - 1); } int calc(int a, int b) { int res = 0; for (register int i = 1; i <= a; ++i) dec(res, sub(iv[i + b], iv[i])); inc(res, mul(inv2, mul(a, b))); return res; } void solve() { int ans = 0; for (const auto &it : cnt) { inc(ans, mul(it.first, it.first - 1, inv2, inv2, it.second)); inc(ans, mul((long long)it.second * (it.second - 1) / 2 % md, calc(it.first, it.first))); } for (const auto &it : cnt) for (const auto &is : cnt) { int x = it.first, y = is.first; if (x >= y) continue; inc(ans, mul(it.second, is.second, calc(x, y))); } out(ans); } int n, k; signed main() { in(n, k, md); inv2 = mdinv(2); prework(); divide(1, n, k); solve(); return 0; } } // namespace i207M signed main() { i207M::main(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int MOD; struct modnum { int v; modnum() : v(0) {} modnum(ll _v) : v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend ostream& operator<<(ostream& o, const modnum& n) { return o << n.v; } friend istream& operator<<(istream& i, modnum& n) { ll v; i >> v; n = modnum(v); return i; } friend bool operator==(const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator!=(const modnum& a, const modnum& b) { return a.v != b.v; } modnum& operator+=(const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator-=(const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator*=(const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum operator-() { modnum res; if (v) res.v = MOD - v; return res; } friend modnum operator+(const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator-(const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator*(const modnum& a, const modnum& b) { return modnum(a) *= b; } modnum pow(int e) const { if (e == 0) return 1; if (e & 1) return *this * this->pow(e - 1); return (*this * *this).pow(e / 2); } modnum& operator/=(const modnum& o) { return (*this) *= o.pow(MOD - 2); } friend modnum operator/(const modnum& a, const modnum& b) { return modnum(a) /= modnum(b); } }; using mn = modnum; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int N, K; cin >> N >> K >> MOD; vector<mn> recip(N + 1), pref(N + 1); for (int i = 1; i <= N; i++) { recip[i] = mn(1) / i; pref[i] = pref[i - 1] + recip[i]; } auto comb = [&](int a, int b) { mn res = 0; for (int i = 1; i <= a; i++) { res += mn(b) / 2 - (pref[i + b] - pref[i]); } return res; }; int szA = N, szB = N, ctA = 1, ctB = 0; for (int i = 0; i < K - 1; i++) { int nszA = szA / 2, nszB = (szB + 1) / 2; int nctA = 0, nctB = 0; if (szA & 1) { nctA += ctA; nctB += ctA; } else nctA += 2 * ctA; if (szB & 1) { nctA += ctB; nctB += ctB; } else nctB += 2 * ctB; szA = nszA, szB = nszB; ctA = nctA, ctB = nctB; } mn ans = 0; ans += mn(ctA) * szA * (szA - 1) / 4; if (ctA > 1) ans += mn(ctA) * (ctA - 1) / 2 * comb(szA, szA); ans += mn(ctB) * szB * (szB - 1) / 4; if (ctB > 1) ans += mn(ctB) * (ctB - 1) / 2 * comb(szB, szB); if (ctA > 0 && ctB > 0) ans += mn(ctA) * ctB * comb(szA, szB); cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, mo; int n1, n2, len, ans = 0; int Sum(int x, int y) { x += y; return (x >= mo) ? x - mo : x; } int Sub(int x, int y) { x -= y; return (x < 0) ? x + mo : x; } int Mul(int x, int y) { return (long long)x * y % mo; } int Pow(int x, int y = mo - 2) { int z = 1; while (y) { if (y & 1) z = Mul(z, x); y >>= 1; x = Mul(x, x); } return z; } int C(int x) { return Mul(x, Mul(x - 1, (mo + 1) / 2)); } namespace task1 { int Calc(int x) { return Mul((mo + 1) / 2, C(x)); } } // namespace task1 namespace task2 { int Calc(int x, int y) { int ans = Mul(x, y); for (int i = 2; i <= x + y; i++) { int l = max(1, i - y); int r = min(x, i - 1); if (l <= r) ans = Sub(ans, Mul(Pow(i), (r - l + 1) * 2)); } return Mul(ans, (mo + 1) / 2); } } // namespace task2 int main() { scanf("%d%d%d", &n, &k, &mo); if (k > 30) { len = 0; n2 = n; n1 = 0; } else { len = n / (1 << (k - 1)); n2 = n - len * (1 << (k - 1)); n1 = (1 << (k - 1)) - n2; } ans = Sum(ans, Mul(n1, task1::Calc(len))); ans = Sum(ans, Mul(n2, task1::Calc(len + 1))); ans = Sum(ans, Mul(C(n1), task2::Calc(len, len))); ans = Sum(ans, Mul(C(n2), task2::Calc(len + 1, len + 1))); ans = Sum(ans, Mul(Mul(n1, n2), task2::Calc(len, len + 1))); printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, q, inv[N], sum[N]; map<int, int> cnt; void divide(int l, int r, int k) { if (l == r || k <= 1) { ++cnt[r - l + 1]; return; } int mid = (l + r) >> 1; divide(l, mid, k - 1); divide(mid + 1, r, k - 1); } int ans; int calc(int x, int y) { int res = 1ll * x * y % q * inv[2] % q; for (int i = 1; i <= x; ++i) res = (res - sum[i + y] + sum[i]) % q; return res; } int main() { scanf("%d %d %d", &n, &k, &q); inv[1] = sum[1] = 1; for (int i = 2; i < N; ++i) inv[i] = 1ll * inv[q % i] * (q - q / i) % q, sum[i] = (sum[i - 1] + inv[i]) % q; divide(1, n, k); for (auto i : cnt) { int l = i.first, c = i.second; ans += 1ll * l * (l - 1) % q * inv[4] % q * c % q; ans %= q; ans += 1ll * c * (c - 1) % q * inv[2] % q * calc(l, l) % q; ans %= q; } for (auto i : cnt) for (auto j : cnt) { int x = i.first, y = j.first, nx = i.second, ny = j.second; if (x >= y) continue; ans += 1ll * nx * ny % q * calc(x, y) % q; ans %= q; } printf("%d\n", (ans + q) % q); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math") #pragma GCC target("popcnt") using namespace std; int n, k; long long inv[100010], sum[100010], p, ans = 0; map<int, int> M; template <class T> void read(T &x) { char ch = x = 0; bool fl = false; while (!isdigit(ch)) fl |= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x = fl ? -x : x; } long long calc(int l, int r) { return (sum[r] - sum[l - 1] + p) % p; } long long Calc(int A, int B) { long long ret = 1LL * A * B % p * inv[2] % p; for (int i = 1; i <= A; i++) { ret = (ret - calc(i + 1, i + B) + p) % p; } return ret; } void solve(int l, int r, int h) { if (h == 1 || l == r) return ans = (ans + 1LL * (r - l + 1) * (r - l) % p * inv[4]) % p, M[r - l + 1]++, void(); int mid = l + r >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } void init() { inv[1] = 1; for (int i = 2; i < 100010; i++) { inv[i] = (p - p / i) * inv[p % i] % p; } for (int i = 1; i < 100010; i++) { sum[i] = (sum[i - 1] + inv[i]) % p; } } int main() { read(n), read(k), read(p), init(); solve(1, n, k); for (auto &x : M) { ans = (ans + Calc(x.first, x.first) * x.second % p * (x.second - 1) % p * inv[2]) % p; for (auto &y : M) { if (x.first < y.first) ans = (ans + Calc(x.first, y.first) * x.second % p * y.second) % p; } } printf("%lld\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int MOD; struct modnum { int v; modnum() : v(0) {} modnum(ll _v) : v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend ostream& operator<<(ostream& o, const modnum& n) { return o << n.v; } friend istream& operator<<(istream& i, modnum& n) { ll v; i >> v; n = modnum(v); return i; } friend bool operator==(const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator!=(const modnum& a, const modnum& b) { return a.v != b.v; } modnum& operator+=(const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator-=(const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator*=(const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum operator-() { modnum res; if (v) res.v = MOD - v; return res; } friend modnum operator+(const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator-(const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator*(const modnum& a, const modnum& b) { return modnum(a) *= b; } modnum pow(int e) const { if (e == 0) return 1; if (e & 1) return *this * this->pow(e - 1); return (*this * *this).pow(e / 2); } modnum& operator/=(const modnum& o) { return (*this) *= o.pow(MOD - 2); } friend modnum operator/(const modnum& a, const modnum& b) { return modnum(a) /= modnum(b); } }; using mn = modnum; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int N, K; cin >> N >> K >> MOD; if (K >= 20 || (1 << (K - 1)) >= N) { cout << 0 << endl; return 0; } int ct = 1 << (K - 1); int ctB = N % ct; int ctA = ct - ctB; int szA = N / ct; int szB = szA + 1; vector<mn> recip(N + 1), pref(N + 1); for (int i = 1; i <= N; i++) { recip[i] = mn(1) / i; pref[i] = pref[i - 1] + recip[i]; } auto comb = [&](int a, int b) { mn res = 0; for (int i = 1; i <= a; i++) { res += mn(b) / 2 - (pref[i + b] - pref[i]); } return res; }; mn ans = 0; ans += mn(ctA) * szA * (szA - 1) / 4; if (ctA > 1) ans += mn(ctA) * (ctA - 1) / 2 * comb(szA, szA); ans += mn(ctB) * szB * (szB - 1) / 4; if (ctB > 1) ans += mn(ctB) * (ctB - 1) / 2 * comb(szB, szB); if (ctA > 0 && ctB > 0) ans += mn(ctA) * ctB * comb(szA, szB); cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline void proc_status() { ifstream t("/proc/self/status"); cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl; } template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline T read() { register T sum(0), fg(1); register char ch(getchar()); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fg = -1; for (; isdigit(ch); ch = getchar()) sum = sum * 10 - '0' + ch; return sum * fg; } const int MAXN = (int)1e5; int MOD; inline int fpm(int x, int y) { int res = 1; for (; y; y >>= 1, x = (long long)x * x % MOD) if (y & 1) res = (long long)res * x % MOD; return res; } int n, k; inline void input() { n = read<int>(), k = read<int>(), MOD = read<int>(); } map<int, int> cnt; inline void merge_sort(int l, int r, int dep) { if (l == r || dep == 1) { ++cnt[r - l + 1]; return; } int mid = (l + r) / 2; merge_sort(l, mid, dep - 1); merge_sort(mid + 1, r, dep - 1); } int pre[MAXN * 2 + 5]; inline int calc(int p, int q) { int sum = (long long)p * q % MOD * ((MOD + 1) / 2) % MOD; for (int i = 1; i <= p; ++i) (sum -= (pre[i + q] - pre[i]) % MOD) %= MOD; return sum; } inline void solve() { merge_sort(1, n, k); pre[0] = 0; for (int i = 1; i <= 2 * n; ++i) pre[i] = (pre[i - 1] + fpm(i, MOD - 2)) % MOD; int ans = 0; for (auto i : cnt) { (ans += (long long)i.first * (i.first - 1) / 2 % MOD * ((MOD + 1) / 2) % MOD * i.second % MOD) %= MOD; (ans += (long long)calc(i.first, i.first) * (i.second * (i.second - 1) / 2 % MOD) % MOD) %= MOD; } for (auto i : cnt) for (auto j : cnt) if (i.first < j.first) (ans += (long long)calc(i.first, j.first) * i.second % MOD * j.second % MOD) %= MOD; printf("%d\n", (ans + MOD) % MOD); } int main() { input(); solve(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; map<int, int> st; int n, h, mod, inv[maxn]; int qpow(int a, int b) { int ans = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) ans = 1ll * ans * a % mod; return ans; } void sol(int l, int r, int h) { if (r - l + 1 <= 1 || h <= 1) { st[r - l + 1]++; } else { int mid = l + r >> 1; sol(l, mid, h - 1); sol(mid + 1, r, h - 1); } } int cal(int x, int y) { int ret = 1ll * x * y % mod * (mod + 1) / 2 % mod; for (int i = 1; i <= x; ++i) ret -= inv[i + y] - inv[i], ret %= mod; return (ret % mod + mod) % mod; } int main() { scanf("%d%d%d", &n, &h, &mod); sol(1, n, h); inv[0] = inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= n; ++i) inv[i] = (inv[i - 1] + inv[i]) % mod; int ans = 0; for (auto c : st) ans = (ans + 1ll * (c.first) * (c.first - 1) % mod * qpow(4, mod - 2) % mod * c.second + 1ll * (c.second) * (c.second - 1) / 2 % mod * cal(c.first, c.first)) % mod; for (auto c : st) for (auto d : st) if (c.first < d.first) ans = (ans + 1ll * (c.second) * d.second % mod * cal(c.first, d.first)) % mod; printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> int q, I[200010]; long long inv(int a, int p) { return a == 1 ? 1 : (1 + p * (a - inv(p % a, a))) / a % p; } int f(int a, int b) { int s = 0; for (int i = 0; i < a + b - 1; i++) s = (s + ((i + 1 < a ? i + 1 : a) - (i - b + 1 > 0 ? i - b + 1 : 0)) * 1ll * i % q * I[i + 2]) % q; return (q + 1ll) / 2 * s % q; } int main() { int n, k; scanf("%d%d%d", &n, &k, &q); --k; if (k > 18) k = 18; int a = n >> k, b = a + 1, x, y; if (a) x = (b << k) - n, y = (1 << k) - x; else x = 0, y = n; for (int i = *I = 1; i <= b * 2; i++) I[i] = I[i - 1] * 1ll * i % q; long long P = inv(I[b * 2], q); for (int i = b * 2; i; i--) I[i] = I[i - 1] * P % q, P = P * i % q; int s = (x * (x - 1ll) / 2 % q * f(a, a) + 1ll * x * y % q * f(a, b) + y * (y - 1ll) / 2 % q * f(b, b) + a * (a - 1ll) / 2 % q * (q + 1 >> 1) % q * x + b * (b - 1ll) / 2 % q * (q + 1 >> 1) % q * y) % q; printf("%d\n", s); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 2e5 + 5; int n, k, mo, ans, inv[N], i, cnt[N], mn = N; void dfs(int l, int r, int h) { if (l == r || h <= 1) { ++cnt[r - l + 1]; mn = std::min(mn, r - l + 1); return; } int m = l + r >> 1; dfs(l, m, h - 1); dfs(m + 1, r, h - 1); } inline int calc(int x, int y) { int ans = 0; for (i = 2; i <= x + y; ++i) ans = (ans + 1ll * inv[i] * (std::min(i - 1, x) - std::max(i - y, 1) + 1)) % mo; return (1ll * x * y % mo * inv[2] + mo - ans) % mo; } int main() { scanf("%d%d%d", &n, &k, &mo); for (i = 2, inv[1] = 1; i < N; ++i) inv[i] = 1ll * (mo - mo / i) * inv[mo % i] % mo; dfs(1, n, k); ans = (1ll * mn * (mn - 1) % mo * cnt[mn] + 1ll * (mn + 1) * mn % mo * cnt[mn + 1]) % mo * inv[4] % mo; ans = (ans + 1ll * cnt[mn] * (cnt[mn] - 1) / 2 % mo * calc(mn, mn)) % mo; ans = (ans + 1ll * cnt[mn + 1] * (cnt[mn + 1] - 1) / 2 % mo * calc(mn + 1, mn + 1)) % mo; ans = (ans + 1ll * cnt[mn] * cnt[mn + 1] % mo * calc(mn, mn + 1)) % mo; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> char ch; bool fs; void re(int& x) { while (ch = getchar(), ch < 33) ; if (ch == '-') fs = 1, x = 0; else fs = 0, x = ch - 48; while (ch = getchar(), ch > 33) x = x * 10 + ch - 48; if (fs) x = -x; } using namespace std; int n, k, mod, ans, inv[200001], sum[200001]; map<int, int> s; void divide(int l, int r, int h) { if (h <= 1 || l == r) ++s[r - l + 1]; else divide(l, l + r >> 1, h - 1), divide((l + r >> 1) + 1, r, h - 1); } int calc(int x, int y) { int res = 1ll * inv[2] * x % mod * y % mod; for (int i = 1; i <= x; ++i) res = (res - (sum[i + y] - sum[i])) % mod; return (res + mod) % mod; } int main() { re(n), re(k), re(mod); divide(1, n, k); sum[1] = inv[1] = 1; for (int i = 2; i < 200001; ++i) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod, sum[i] = (sum[i - 1] + inv[i]) % mod; for (auto i : s) { ans = (ans + 1ll * inv[4] * i.first % mod * (i.first - 1) % mod * i.second) % mod; ans = (ans + 1ll * inv[2] * i.second % mod * (i.second - 1) % mod * calc(i.first, i.first)) % mod; } for (auto i : s) for (auto j : s) if (i.first < j.first) ans = (ans + 1ll * i.second * j.second % mod * calc(i.first, j.first)) % mod; printf("%d\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } int mod; struct modint { int x; modint(int o = 0) { x = o; } modint &operator=(int o) { return x = o, *this; } modint &operator+=(modint o) { return x = x + o.x >= mod ? x + o.x - mod : x + o.x, *this; } modint &operator-=(modint o) { return x = x - o.x < 0 ? x - o.x + mod : x - o.x, *this; } modint &operator*=(modint o) { return x = 1ll * x * o.x % mod, *this; } modint &operator^=(int b) { modint a = *this, c = 1; for (; b; b >>= 1, a *= a) if (b & 1) c *= a; return x = c.x, *this; } modint &operator/=(modint o) { return *this *= o ^= mod - 2; } modint &operator+=(int o) { return x = x + o >= mod ? x + o - mod : x + o, *this; } modint &operator-=(int o) { return x = x - o < 0 ? x - o + mod : x - o, *this; } modint &operator*=(int o) { return x = 1ll * x * o % mod, *this; } modint &operator/=(int o) { return *this *= ((modint(o)) ^= mod - 2); } template <class I> friend modint operator+(modint a, I b) { return a += b; } template <class I> friend modint operator-(modint a, I b) { return a -= b; } template <class I> friend modint operator*(modint a, I b) { return a *= b; } template <class I> friend modint operator/(modint a, I b) { return a /= b; } friend modint operator^(modint a, int b) { return a ^= b; } friend bool operator==(modint a, int b) { return a.x == b; } friend bool operator!=(modint a, int b) { return a.x != b; } bool operator!() { return !x; } modint operator-() { return x ? mod - x : 0; } }; inline modint qpow(modint a, int b) { return a ^ b; } int n, k; map<int, int> mp; modint iv[100005], sum[100005]; void div(int l, int r, int k) { if (k <= 1 || l == r) return mp[r - l + 1]++, void(); int mid = l + r >> 1; div(l, mid, k - 1), div(mid + 1, r, k - 1); } modint getprob(int x, int y) { modint res = modint(x) * y; for (register int i = (1); i <= (x); ++i) res -= 2 * (sum[i + y] - sum[i]); return res; } signed main() { n = read(), k = read(), mod = read(); iv[1] = 1; for (register int i = (2); i <= (max(4, n)); ++i) iv[i] = iv[mod % i] * (mod - mod / i); for (register int i = (1); i <= (n); ++i) sum[i] = sum[i - 1] + iv[i]; div(1, n, k); modint res = 0; for (auto it1 : mp) for (auto it2 : mp) { if (it1.first == it2.first) { int cnt = it1.second, len = it1.first; res += iv[2] * len * (len - 1) * cnt; res += modint(cnt) * (cnt - 1) * iv[2] * getprob(len, len); } else if (it1.first < it2.first) { int l1 = it1.first, l2 = it2.first, c1 = it1.second, c2 = it2.second; res += getprob(l1, l2) * c1 * c2; } } res *= iv[2]; cout << res.x; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200010; int moder; int inv[N]; int cnt[N]; int pre[N]; int ans; void init() { inv[1] = 1; for (int i = 2; i < N; ++i) { inv[i] = moder - 1ll * (moder / i) * inv[moder % i] % moder; } for (int i = 1; i < N; ++i) { pre[i] = (pre[i - 1] + inv[2] - inv[i]) % moder; pre[i] += pre[i] < 0 ? moder : 0; } } void mergesort(int l, int r, int h) { if (h <= 1) { if (r >= l) { ++cnt[r - l + 1]; ans = (ans + 1ll * (r - l + 1) * (r - l) % moder * inv[4]) % moder; } return; } int mid = (l + r) >> 1; mergesort(l, mid, h - 1); mergesort(mid + 1, r, h - 1); } int main() { int n, k; scanf("%d%d%d", &n, &k, &moder); init(); if (k >= 20) { puts("0"); return 0; } mergesort(1, n, k); for (int i = 0; i <= n; ++i) { if (!cnt[i]) continue; for (int j = i; j <= n; ++j) { if (!cnt[j]) continue; int coe = i == j ? 1ll * cnt[i] * (cnt[i] - 1) / 2 % moder : 1ll * cnt[i] * cnt[j] % moder; for (int k = 1; k <= i; ++k) { ans = (ans + 1ll * coe * (pre[k + j] - pre[k])) % moder; } } } ans += ans < 0 ? moder : 0; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } const int n = read(), m = read(), mod = read(); const int inv2 = (mod + 1) / 2; int ans, inv[200020], s[200020]; vector<pair<int, int> > vec; void Solve(int l, int r, int h) { if (h == 1 || l == r) { int len = (r - l + 1); ans = (ans + 1LL * len * (len - 1) / 2 % mod * inv2 % mod) % mod; for (int i = 0; i < (int)vec.size(); ++i) { if (vec[i].first == len) { ++vec[i].second; return; } } vec.push_back(make_pair(len, 1)); return; } int mid = (l + r) >> 1; Solve(l, mid, h - 1); Solve(mid + 1, r, h - 1); } int main() { inv[1] = s[1] = 1; for (int i = 2; i <= n << 1; ++i) { inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod; s[i] = (s[i - 1] + inv[i]) % mod; } Solve(1, n, m); for (int x = 0; x < (int)vec.size(); ++x) { for (int y = x; y < (int)vec.size(); ++y) { int A = vec[x].first, B = vec[y].first, cA = vec[x].second, cB = vec[y].second; int sum = 0; for (int i = 1; i <= A; ++i) { sum = (sum + (s[i + B] - s[i] + mod) % mod) % mod; } int tot = (x == y ? 1LL * cA * (cA - 1) / 2 % mod : 1LL * cA * cB % mod); ans = (ans + 1LL * tot * (1LL * A * B % mod * inv2 % mod - sum)) % mod; } } printf("%d\n", (ans + mod) % mod); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { char ch; x = 0; bool fl = false; while (!isdigit(ch = getchar())) (ch == '-') && (fl = true); for (x = (ch ^ '0'); isdigit(ch = getchar()); x = x * 10 + (ch ^ '0')) ; (fl == true) && (x = -x); } template <class T> inline void output(T x) { if (x / 10) output(x / 10); putchar(x % 10 + '0'); } template <class T> inline void ot(T x) { if (x < 0) putchar('-'), x = -x; output(x); putchar(' '); } template <class T> inline void prt(T a[], int st, int nd) { for (register int i = st; i <= nd; ++i) ot(a[i]); putchar('\n'); } namespace Modulo { int mod; inline int ad(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } inline int sub(int x, int y) { return ad(x, mod - y); } inline int mul(int x, int y) { return (long long)x * y % mod; } inline void inc(int &x, int y) { x = ad(x, y); } inline void inc2(int &x, int y) { x = mul(x, y); } inline int qm(int x, int y = mod - 2) { int ret = 1; while (y) { if (y & 1) ret = mul(x, ret); x = mul(x, x); y >>= 1; } return ret; } template <class... Args> inline int ad(const int a, const int b, const Args &...args) { return ad(ad(a, b), args...); } template <class... Args> inline int mul(const int a, const int b, const Args &...args) { return mul(mul(a, b), args...); } } // namespace Modulo using namespace Modulo; namespace Miracle { const int N = 1e5 + 5; int n, k; int iv[N], s[N]; int l1, l2, c1, c2; void divi(int l, int r, int d) { if (d == k || l == r) { if (!l1) { l1 = r - l + 1; ++c1; } else if (r - l + 1 == l1) ++c1; else if (!l2) l2 = r - l + 1, ++c2; else ++c2; return; } int mid = (l + r) >> 1; divi(l, mid, d + 1); divi(mid + 1, r, d + 1); } int calc(int l1, int l2) { if (!l1 || !l2) return 0; int ret = mul(l1, l2, qm(2)); for (register int i = 1; i <= l1; ++i) { ret = sub(ret, sub(s[i + l2], s[i])); } return ret; } int main() { rd(n); rd(k); rd(mod); iv[1] = 1; for (register int i = 2; i <= n; ++i) { iv[i] = mul(mod - mod / i, iv[mod % i]); } for (register int i = 1; i <= n; ++i) s[i] = ad(s[i - 1], iv[i]); divi(1, n, 1); int ans = ad(mul(c1, l1, (l1 - 1), qm(4)), mul(c2, l2, (l2 - 1), qm(4))); inc(ans, mul(c1, c1 - 1, qm(2), calc(l1, l1))); inc(ans, mul(c2, c2 - 1, qm(2), calc(l2, l2))); inc(ans, mul(c1, c2, calc(l1, l2))); ot(ans); return 0; } } // namespace Miracle signed main() { Miracle::main(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read() { int X = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar(); return X * w; } const int N = 100000 + 10; int mod, inv2, inv4; int qpow(int a, int b) { int c = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) c = 1ll * c * a % mod; return c; } int n, k; int inv[N], sum[N], o[N]; vector<int> l; void divide(int l, int r, int d) { if (d == k || l == r) { ++o[r - l + 1]; return; } int mid = (l + r) >> 1; divide(l, mid, d + 1), divide(mid + 1, r, d + 1); } int calc(int x, int y) { int res = 1ll * x * y % mod * inv2 % mod; for (int i = 1; i <= x; ++i) res = (res - (sum[i + y] - sum[i] + mod) % mod + mod) % mod; return res; } int main() { n = read(), k = read(), mod = read(), inv2 = qpow(2, mod - 2), inv4 = qpow(4, mod - 2); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = mod - 1ll * inv[mod % i] * (mod / i) % mod; for (int i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + inv[i]) % mod; divide(1, n, 1); for (int i = 1; i <= n; ++i) if (o[i]) l.emplace_back(i); int ans = 0; for (int i : l) ans = (ans + 1ll * i * (i - 1) % mod * inv4 % mod * o[i]) % mod; for (int i : l) ans = (ans + 1ll * o[i] * (o[i] - 1) / 2 % mod * calc(i, i)) % mod; if (l.size() > 1) ans = (ans + 1ll * o[l[0]] * o[l[1]] % mod * calc(l[0], l[1])) % mod; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, p, ans, inv4, cnt[N], inv[N]; void divide(int x, int k) { if (k == 1 || x == 1) return (void)(++cnt[x], ans = (ans + (long long)x * (x - 1) % p * inv4) % p); divide(x / 2, k - 1), divide(x - x / 2, k - 1); } inline int calc(int x, int y) { int ans = (long long)x * y % p * (p + 1) / 2 % p; for (int i = 2; i <= x + y; ++i) ans = (ans + (min(x, i - 1) - max(1, i - y) + 1ll) * (p - inv[i])) % p; return ans; } int main() { scanf("%d%d%d", &n, &k, &p), inv4 = (p + 1ll) * (p + 1) / 4 % p; inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (long long)(p - p / i) * inv[p % i] % p; divide(n, k); int x = 0; for (int i = 1; i <= n; ++i) if (cnt[i]) { x = i; break; } ans = (ans + (long long)cnt[x] * (cnt[x] - 1) / 2 % p * calc(x, x)) % p; if (cnt[x + 1]) ans = (ans + (long long)cnt[x] * cnt[x + 1] % p * calc(x, x + 1) + (long long)cnt[x + 1] * (cnt[x + 1] - 1) / 2 % p * calc(x + 1, x + 1)) % p; return printf("%d", ans), 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2E5 + 10; int P; template <typename T1, typename T2> void Inc(T1 &a, T2 b) { a += b; if (a >= P) a -= P; } template <typename T1, typename T2> void Dec(T1 &a, T2 b) { a -= b; if (a < 0) a += P; } template <typename T1, typename T2> T1 Add(T1 a, T2 b) { a += b; return a >= P ? a - P : a; } template <typename T1, typename T2> T1 Sub(T1 a, T2 b) { a -= b; return a < 0 ? a + P : a; } long long ksm(long long a, long long b) { long long ret = 1; for (; b; b >>= 1, (a *= a) %= P) if (b & 1) (ret *= a) %= P; return ret; } int n, k, inv2, inv4; long long Ans, sum[MAXN << 1]; map<int, int> M; long long calc(int len1, int len2) { long long ret = 1ll * len1 * len2 % P * inv2 % P; for (int i = 1; i <= len1; i++) Dec(ret, 1ll * Sub(sum[i + len2], sum[i]) % P); return ret; } void solve(int l, int r, int d) { if (l == r || d == k) { int len = r - l + 1; M[len]++; Inc(Ans, 1ll * len * (len - 1) % P * inv4 % P); return; } int mid = (l + r) >> 1; solve(l, mid, d + 1); solve(mid + 1, r, d + 1); } int main() { scanf("%d%d%d", &n, &k, &P); inv2 = ksm(2, P - 2); inv4 = ksm(4, P - 2); for (int i = 1; i <= 2 * n; i++) sum[i] = (sum[i - 1] + ksm(i, P - 2)) % P; solve(1, n, 1); for (auto p1 : M) { Inc(Ans, 1ll * p1.second * (p1.second - 1) % P * inv2 % P * calc(p1.first, p1.first) % P); for (auto p2 : M) if (p1.first < p2.first) Inc(Ans, 1ll * p1.second * p2.second % P * calc(p1.first, p2.first) % P); } printf("%lld\n", Ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; long long read() { long long s = 0; char c = getchar(), lc = '+'; while (c < '0' || '9' < c) lc = c, c = getchar(); while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar(); return lc == '-' ? -s : s; } void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x < 10) putchar(x + '0'); else { write(x / 10); putchar(x % 10 + '0'); } } void print(long long x, char c = '\n') { write(x); putchar(c); } long long sum[N], ans, p; long long power(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % p; a = a * a % p; b /= 2; } return ret; } long long calc(long long x, long long y) { long long ret = 0; for (long long i = 1; i <= x; i++) ret = (ret + sum[i + y] - sum[i] + p) % p; return ret; } long long tot[N][2]; void Count(long long l, long long r, long long h, long long Min) { if (r - l + 1 == Min) tot[h][0]++; else tot[h][1]++; if (h == 1) return void(ans = (ans + (r - l + 1) * (r - l) % p * power(4, p - 2) % p) % p); if (l == r) return; long long mid = (l + r) / 2; Count(l, mid, h - 1, Min / 2); Count(mid + 1, r, h - 1, Min / 2); } void merge_sort(long long h, long long Min) { if (h <= 0) return; if (h == 1) ans = (ans + tot[h][0] * (tot[h][0] - 1) / 2 % p * calc(Min, Min) % p + tot[h][1] * (tot[h][1] - 1) / 2 % p * calc(Min + 1, Min + 1) % p + tot[h][0] * tot[h][1] % p * calc(Min, Min + 1) % p) % p; merge_sort(h - 1, Min / 2); } signed main() { memset(tot, 0, sizeof(tot)); long long n = read(), k = read(); p = read(); for (long long i = 1; i <= n * 2; i++) sum[i] = (sum[i - 1] + power(2, p - 2) - power(i, p - 2) + p) % p; Count(1, n, k, n); merge_sort(k, n); print(ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") using namespace std; namespace ywy { unordered_map<int, int> mp; long long inv[200001], ans = 0; int p; inline void solve(int a, int b) { long long cjr = a; cjr *= b; cjr %= p; cjr *= mp[a]; cjr %= p; cjr *= mp[b]; cjr %= p; ans = (ans + cjr * inv[2]) % p; for (register int i = 2; i <= a + b; i++) { long long l = max(1, i - b), r = min(i - 1, a); if (l > r) continue; cjr = p - inv[i]; cjr *= (r - l + 1); cjr %= p; cjr *= mp[a]; cjr %= p; cjr *= mp[b]; ans = (ans + cjr) % p; } } void digui(int l, int r, int h) { if (h <= 1) { mp[r - l + 1]++; return; } if (l == r) return; int mid = (l + r) >> 1; digui(l, mid, h - 1); digui(mid + 1, r, h - 1); } void ywymain() { int n, k; cin >> n >> k >> p; inv[0] = inv[1] = 1; for (register int i = 2; i <= 100000; i++) inv[i] = (inv[p % i] * (p - p / i)) % p; digui(1, n, k); for (unordered_map<int, int>::iterator i = mp.begin(); i != mp.end(); i++) { long long cjr = i->first - 1; cjr *= i->first; cjr %= p; cjr *= i->second; cjr %= p; ans = (ans + cjr * inv[4]) % p; cjr = inv[2]; cjr *= (i->second - 1); cjr %= p; cjr *= i->second; cjr %= p; for (register int j = 2; j <= i->first * 2; j++) { long long l = max(1, j - i->first), r = min(j - 1, i->first); if (l > r) continue; long long h = p - inv[j]; h *= cjr; h %= p; h *= (r - l + 1); h %= p; ans = (ans + h) % p; } cjr *= inv[2]; cjr %= p; cjr *= i->first; cjr %= p; cjr *= i->first; ans = (ans + cjr) % p; } for (unordered_map<int, int>::iterator i = mp.begin(); i != mp.end(); i++) { unordered_map<int, int>::iterator j = i; j++; for (; j != mp.end(); j++) solve(i->first, j->first); } cout << ans << endl; } } // namespace ywy int main() { ywy::ywymain(); return (0); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } long long P; const int maxn = 110000; long long rec[maxn]; long long deg(long long x, long long d) { if (d < 0) d += P - 1; long long y = 1; while (d) { if (d & 1) (y *= x) %= P; (x *= x) %= P; d /= 2; } return y; } map<int, long long> cnt; void merge(int l, int k) { if (k <= 1 || l == 1) { ++cnt[l]; return; } merge(l / 2, k - 1); merge(l - l / 2, k - 1); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int n, k; cin >> n >> k >> P; merge(n, k); long long ans = 0; for (auto w1 : cnt) { int n = w1.first; long long q = w1.second; (ans += q * n % P * (n - 1) % P * deg(4, -1)) %= P; } for (auto w1 : cnt) for (auto w2 : cnt) { int n = w1.first, m = w2.first; if (m < n) continue; long long coef; if (n == m) coef = w1.second * (w1.second - 1) / 2 % P; else coef = w1.second * w2.second % P; long long R = 1LL * n * m % P * (P + 1) / 2 % P; for (int s = 1; s <= (int)(n + m); ++s) { int l = max(1, s - m), r = min(n, s - 1); (R -= 1LL * (r - l + 1) * deg(s, -1)) %= P; } (ans += coef * R) %= P; } if (ans < 0) ans += P; cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; const int N = 100005; int n, kk, P; ll ans, inv[N], sum[N]; map<int, int> cnt; ll qpow(ll a, ll b) { ll ret = 1; while (b) { if (b & 1) ret = ret * a % P; a = a * a % P; b >>= 1; } return ret; } void proc(int l, int r, int dep) { if (dep <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; proc(l, mid, dep - 1); proc(mid + 1, r, dep - 1); } int calc(int x, int y) { ll ret = ll(x) * y % P; for (int i = 1; i <= x; i++) ret = (ret + P - (sum[i + y] - sum[i]) * 2 % P) % P; return ret; } int main() { ios::sync_with_stdio(false); cin >> n >> kk >> P; for (int i = 1; i < N; i++) inv[i] = qpow(i, P - 2), sum[i] = (sum[i - 1] + inv[i]) % P; proc(1, n, kk); for (auto &i : cnt) { ll t = i.first, s = i.second; ans = (ans + t * (t - 1) % P * inv[2] % P * s % P + s * (s - 1) % P * inv[2] % P * calc(t, t) % P) % P; } for (auto &i : cnt) for (auto &j : cnt) if (i.first < j.first) ans = (ans + calc(i.first, j.first) * 1ll * i.second % P * j.second % P) % P; cout << ans * inv[2] % P << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; inline int read() { int x = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x; } int n, k, mod; long long inv[N], sum[N], ans; map<long long, long long> m; void solve(int l, int r, int dep) { if (l == r || !dep) { m[r - l + 1]++; return; } int mid = (l + r) >> 1; solve(l, mid, dep - 1), solve(mid + 1, r, dep - 1); } void init() { inv[0] = inv[1] = 1; for (register int i = 2; i <= n; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod, sum[i] = (sum[i - 1] + inv[i]) % mod; } long long cal(long long x, long long y) { long long res = x * y; for (register int i = 1; i <= x; i++) (res += mod - (sum[i + y] - sum[i] + mod) % mod * 2 % mod) %= mod; return res; } void work() { for (map<long long, long long>::iterator it = m.begin(); it != m.end(); it++) { long long len = it->first, cnt = it->second; (ans += len * (len - 1) % mod * inv[2] % mod * cnt % mod) %= mod; (ans += cnt * (cnt - 1) % mod * inv[2] % mod * cal(len, len)) %= mod; } for (map<long long, long long>::iterator it1 = m.begin(); it1 != m.end(); it1++) { for (map<long long, long long>::iterator it2 = m.begin(); it2 != m.end(); it2++) { long long x = it1->first, y = it2->first; if (x < y) (ans += cal(x, y) * it1->second % mod * it2->second % mod) %= mod; } } } int main() { n = read(), k = read() - 1, mod = read(); init(); solve(1, n, k); work(); printf("%lld", ans * inv[2] % mod); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod, inv2; vector<pair<long long, long long> > vec; long long inv[20000005], ans, sum; long long calc(long long len) { return len * (len - 1) % mod * inv2 % mod; } void merge(long long l, long long r, long long h) { if (h == 1 || l == r) { long long len = r - l + 1; ans += calc(len) * inv2 % mod; ans %= mod; for (long long i = 0; i < (long long)vec.size(); ++i) if (vec[i].first == len) { ++vec[i].second; return; } return vec.push_back(make_pair(len, 1)), void(0); } long long mid = (l + r) >> 1; merge(l, mid, h - 1); merge(mid + 1, r, h - 1); } void init(long long n) { inv[1] = 1; for (long long i = 2; i <= n; ++i) inv[i] = 1ll * (mod - (mod / i)) * inv[mod % i] % mod; for (long long i = 1; i <= n; ++i) inv[i] = (inv[i] + inv[i - 1]) % mod; } signed main() { long long n, K; cin >> n >> K >> mod; inv2 = (mod + 1) / 2; init(n + n); merge(1, n, K); for (long long i = 0; i < (long long)vec.size(); ++i) for (long long j = i; j < (long long)vec.size(); ++j) { long long lenA = vec[i].first, lenB = vec[j].first; long long cntA = vec[i].second, cntB = vec[j].second; sum = 0; for (long long k = 1; k <= lenA; ++k) sum = (sum + inv[k + lenB] - inv[k] + mod) % mod; long long tot = (i == j ? calc(cntA) : cntA * cntB) % mod; ans += tot * ((lenA * lenB % mod * inv2 % mod - sum + mod) % mod) % mod; ans %= mod; } cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> std::map<long long, long long> mp; long long p, inv[200005], sum[200005]; inline long long read() { register long long x = 0, f = 1; register char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline long long max(const long long &x, const long long &y) { return x > y ? x : y; } inline void simulate(long long l, long long r, long long k) { if (k == 1 || l == r) { std::map<long long, long long>::iterator it = mp.find(r - l + 1); if (it != mp.end()) ++it->second; else mp.insert(std::make_pair(r - l + 1, 1)); return; } long long mid = l + r >> 1; simulate(l, mid, k - 1); simulate(mid + 1, r, k - 1); } inline long long calc(long long len1, long long len2) { long long res = 0; for (register long long i = 1; i <= len1; ++i) { (res += (inv[2] * len2 % p - (sum[i + len2] - sum[i]) % p) % p) %= p; } return res; } signed main() { long long ans = 0; long long n = read(), k = read(); p = read(); inv[1] = 1; for (register long long i = 2; i <= max(n, 4); ++i) inv[i] = (p - (p / i)) * 1ll * inv[p % i] % p; for (register long long i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + inv[i]) % p; simulate(1, n, k); for (std::map<long long, long long>::iterator it = mp.begin(); it != mp.end(); ++it) { long long len = it->first, cnt = it->second; (ans += cnt * ((len - 1) * 1ll * len % p) % p * inv[4] % p) %= p; } for (std::map<long long, long long>::iterator it1 = mp.begin(); it1 != mp.end(); ++it1) { for (std::map<long long, long long>::iterator it2 = mp.begin(); it2 != mp.end(); ++it2) { if (it1->first == it2->first) { long long len = it1->first, cnt = (it1->second - 1) * 1ll * (it1->second) / 2 % p; (ans += cnt * 1ll * calc(len, len) % p) %= p; } else if (it1->first < it2->first) { long long cnt = (it2->second) * (it1->second) % p; (ans += cnt * 1ll * calc(it1->first, it2->first) % p) %= p; } } } printf("%lld\n", (ans + p) % p); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, mod, ans; int inv[N], sum[N]; map<int, int> cnt; map<int, int>::iterator it1, it2; int read() { int ret = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 0; c = getchar(); } while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return f ? ret : -ret; } void up(int &x, int y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } int qpow(int x, int y) { int res = 1; x %= mod; for (; y; y >>= 1, x = (long long)x * x % mod) if (y & 1) res = (long long)res * x % mod; return res; } void init() { n = read(); k = read(); mod = read(); for (int i = 1; i < N; ++i) sum[i] = inv[i] = qpow(i, mod - 2), up(sum[i], sum[i - 1]); } void divide(int l, int r, int dp) { if (dp <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; divide(l, mid, dp - 1); divide(mid + 1, r, dp - 1); } int calc(int x, int y) { int res = (long long)x * y % mod; for (int i = 1; i <= x; ++i) up(res, -(sum[i + y] - sum[i]) * 2 % mod); return res; } void solve() { for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) { int t = it1->first, s = it1->second; up(ans, (long long)t * (t - 1) % mod * inv[2] % mod * s % mod); up(ans, (long long)s * (s - 1) % mod * inv[2] % mod * calc(t, t) % mod); } for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) for (it2 = cnt.begin(); it2 != cnt.end(); ++it2) { int x = it1->first, y = it2->first; if (x >= y) continue; up(ans, (long long)calc(x, y) * it1->second % mod * it2->second % mod); } printf("%d\n", (long long)ans * inv[2] % mod); } int main() { init(); divide(1, n, k); solve(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 10; int mod = 1e9 + 7; long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } long long INV(long long a) { return power(a, mod - 2); } mt19937 Rand(123456); int Range(int l, int r) { return l + Rand() % (r - l + 1); } map<long long, long long> mp; void work(int l, int r, int k) { if (k == 1 || l == r) { ++mp[r - l + 1]; return; } int mid = (l + r) / 2; work(l, mid, k - 1); work(mid + 1, r, k - 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k >> mod; vector<long long> inv(2 * n), inv_sum; for (int i = 0; i <= 2 * n - 1; i++) inv[i] = INV(i); inv_sum = inv; for (int i = 1; i <= 2 * n - 1; i++) inv_sum[i] += inv_sum[i - 1]; for (auto& I : inv_sum) I %= mod; auto merge = [&](int l, int r, const vector<long long>& inv_sum) { long long res = (long long)l * r % mod * INV(2) % mod; for (int i = 1; i <= l; i++) res -= inv_sum[i + r] - inv_sum[i]; res %= mod; res += mod; return res % mod; }; work(1, n, k); long long res = 0; for (auto I : mp) res += (I.first * (I.first - 1) % mod * INV(4) % mod) * I.second % mod, res += I.second * (I.second - 1) % mod * INV(2) % mod * merge(I.first, I.first, inv_sum) % mod; for (auto I : mp) for (auto J : mp) if (I.first < J.first) res += merge(I.first, J.first, inv_sum) * I.second % mod * J.second % mod; res %= mod; res += mod; res %= mod; cout << res; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, mod, ans; int inv[N], sum[N]; map<int, int> cnt; map<int, int>::iterator it1, it2; int read() { int ret = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 0; c = getchar(); } while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return f ? ret : -ret; } void up(int &x, int y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } int Pow(int b, int k) { int r = 1; for (; k; k >>= 1, b = 1ll * b * b % mod) if (k & 1) r = 1ll * r * b % mod; return r; } void divide(int l, int r, int dp) { if (dp <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; divide(l, mid, dp - 1); divide(mid + 1, r, dp - 1); } int calc(int x, int y) { int res = (long long)x * y % mod; for (int i = 1; i <= x; ++i) up(res, -(sum[i + y] - sum[i]) * 2 % mod); return res; } int main() { n = read(); k = read(); mod = read(); for (int i = 1; i < N; ++i) sum[i] = inv[i] = Pow(i, mod - 2), up(sum[i], sum[i - 1]); divide(1, n, k); for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) { int t = it1->first, s = it1->second; up(ans, (long long)t * (t - 1) % mod * inv[2] % mod * s % mod); up(ans, (long long)s * (s - 1) % mod * inv[2] % mod * calc(t, t) % mod); } for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) for (it2 = cnt.begin(); it2 != cnt.end(); ++it2) { int x = it1->first, y = it2->first; if (x >= y) continue; up(ans, (long long)calc(x, y) * it1->second % mod * it2->second % mod); } printf("%d\n", (long long)ans * inv[2] % mod); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int MOD; int inv[200005]; void gen_inv(int maxa) { inv[1] = 1; for (int i = 2; i <= maxa; i++) inv[i] = MOD - 1LL * inv[MOD % i] * (MOD / i) % MOD; } int getans(int u, int v) { int ans = 0; for (int i = 2; i <= u + v; i++) ans = (ans + 1LL * (min(u, i - 1) - max(1, i - v) + 1) * inv[i]) % MOD; return ans; } int main() { int n, k; scanf("%d%d%d", &n, &k, &MOD); k--; gen_inv(2 * n); int ans = 1LL * n * (n - 1) / 2 % MOD * inv[2] % MOD; if (k <= 20 && (n >> k)) { int c0 = n >> k, c1 = c0 + 1; int tot1 = n & (1 << k) - 1, tot0 = (1 << k) - tot1; ans = (ans - 1LL * tot0 * (tot0 - 1) / 2 % MOD * getans(c0, c0) % MOD + MOD) % MOD; ans = (ans - 1LL * tot1 * (tot1 - 1) / 2 % MOD * getans(c1, c1) % MOD + MOD) % MOD; ans = (ans - 1LL * tot0 * tot1 % MOD * getans(c0, c1) % MOD + MOD) % MOD; } else ans = 0; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } template <typename F> inline void write(F x, char ed = '\n') { static short st[30]; short tp = 0; if (x < 0) putchar('-'), x = -x; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar('0' | st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } const int N = 500005; long long inv[N]; int n, k, P; long long L1, L2, C1, C2; long long calc(int L1, int L2) { long long ans = 0; for (int i = 1; i <= L1; i++) ans = ans + inv[i + L2] - inv[i]; return (ans % P + P) % P; } void solve(int l, int r, int k) { if (k <= 1 || l == r) { int len = r - l + 1; (!L1 || len == L1) ? L1 = len : !L2 && (L2 = len); len == L1 ? C1++ : C2++; return; } int mid = (l + r) >> 1; solve(l, mid, k - 1), solve(mid + 1, r, k - 1); } int main() { read(n), read(k), read(P); inv[0] = inv[1] = 1; long long inv2 = (P + 1) >> 1; for (int i = 2; i <= n; i++) inv[i] = (P - P / i) * inv[P % i] % P; for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] + inv[i] % P; solve(1, n, k); long long ans = (long long)n * (n - 1) % P * inv2 % P * inv2 % P; ans = (ans - C1 * (C1 - 1) / 2 % P * calc(L1, L1) - C2 * (C2 - 1) / 2 % P * calc(L2, L2) % P - C1 * C2 % P * calc(L1, L2)) % P; write((ans % P + P) % P); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, mod, ans, inv[N], s[N]; map<int, int> c; int ksm(int a, int b) { int r = 1; while (b) { if (b & 1) r = 1ll * r * a % mod; a = 1ll * a * a % mod; b >>= 1; } return r; } void pre(int l, int r, int k) { if (k <= 1 || l == r) { c[r - l + 1]++; return; } int mid = (l + r) >> 1; pre(l, mid, k - 1); pre(mid + 1, r, k - 1); } int clc(int x, int y) { int r = 1ll * x * y % mod; for (int i = 1; i <= x; i++) r = (r - 2ll * (s[i + y] - s[i]) % mod) % mod; return r; } int main() { scanf("%d%d%d", &n, &k, &mod); for (int i = 1; i <= 1e5; i++) inv[i] = ksm(i, mod - 2), s[i] = (s[i - 1] + inv[i]) % mod; pre(1, n, k); for (map<int, int>::iterator i = c.begin(); i != c.end(); i++) { ans = (ans + 1ll * i->first * (i->first - 1) % mod * inv[2] % mod * i->second % mod) % mod; ans = (ans + 1ll * i->second * (i->second - 1) % mod * inv[2] % mod * clc(i->first, i->first) % mod) % mod; } for (map<int, int>::iterator i = c.begin(); i != c.end(); i++) for (map<int, int>::iterator j = c.begin(); j != c.end(); j++) if (i->first < j->first) ans = (ans + 1ll * clc(i->first, j->first) * i->second % mod * j->second % mod) % mod; printf("%lld\n", (1ll * ans * inv[2] % mod + mod) % mod); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc; int ny; inline char gc() { return pp1 == pp2 && (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2) ? EOF : *pp1++; } inline int read() { int x = 0; for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;) if (nc == 45) ny = -1; if (nc < 0) return nc; for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF; x = (x << 3) + (x << 1) + (nc ^ 48)) ; return x * ny; } int n, k, Mod, Fac[MAXN], iFac[MAXN], ans, t[MAXN], bel[MAXN], cnt, len[MAXN], inv[MAXN], sum[MAXN]; inline int C(int n, int m) { return n < 0 || m < 0 || n < m ? 0 : 1ll * Fac[n] * iFac[m] % Mod * iFac[n - m] % Mod; } inline int Fp(int x, int k) { int ans = 1; for (; k; k >>= 1, x = 1ll * x * x % Mod) if (k & 1) ans = 1ll * ans * x % Mod; return ans; } int t1, t2; inline int calc(int len1, int len2) { int ans = 0; for (int i = (0); i <= (len1 - 1); i++) ans = (ans + 1ll * i * (sum[i + len2 + 1] - sum[i + 1] + Mod) % Mod) % Mod; return 1ll * ans * (Mod + 1 >> 1) % Mod; } inline void Div(int l, int r, int h) { if (h == k || l == r) { len[++cnt] = r - l + 1; return; } int mid = l + r >> 1; Div(l, mid, h + 1), Div(mid + 1, r, h + 1); } int main() { n = read(), k = read() - 1, Mod = read(), Div(1, n, 0); if (k > 20) return puts("0"), 0; Fac[0] = 1; for (int i = (1); i <= (n); i++) Fac[i] = 1ll * Fac[i - 1] * i % Mod; iFac[n] = Fp(Fac[n], Mod - 2); for (int i = (n); i >= (1); i--) iFac[i - 1] = 1ll * iFac[i] * i % Mod; for (int i = (1); i <= (n); i++) inv[i] = 1ll * iFac[i] * Fac[i - 1] % Mod, sum[i] = (sum[i - 1] + inv[i]) % Mod; if (k) { for (int i = (1); i <= (cnt); i++) if (len[i] == ((n >> k))) t1++; else t2++; ans = (calc(n >> k, n >> k) * (1ll * t1 * (t1 - 1) % Mod) + (calc(n >> k, (n >> k) + 1) + calc((n >> k) + 1, n >> k)) * (1ll * t1 * t2 % Mod) + calc((n >> k) + 1, (n >> k) + 1) * (1ll * t2 * (t2 - 1) % Mod)) % Mod; } for (int i = (1); i <= (cnt); i++) ans = (ans + (1ll * len[i] * (len[i] - 1) / 2 % Mod) * (Mod + 1 >> 1) % Mod) % Mod; cout << ans << "\n"; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; long long MOD, f[N]; long long qpow(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2) z = z * x % MOD; x = x * x % MOD; y /= 2; } return z; } map<int, int> ma; pair<int, int> a[10]; void dfs(int x, int k) { if (k == 1 || x == 1) { ma[x]++; return; } dfs((x + 1) / 2, k - 1); dfs(x / 2, k - 1); } int main() { int n, k; scanf("%d%d%lld", &n, &k, &MOD); dfs(n, k); for (int i = 1; i <= n * 2; i++) f[i] = (f[i - 1] - qpow(i, MOD - 2) + qpow(2, MOD - 2) + MOD) % MOD; int cnt = 0; for (auto p : ma) { a[++cnt] = p; } long long ans = 0; for (int i = 1; i <= cnt; i++) { ans += 1LL * a[i].second * a[i].first % MOD * (a[i].first - 1) % MOD * qpow(4, MOD - 2) % MOD; long long sum = 0; for (int j = 1; j <= a[i].first; j++) { sum = (sum + f[j + a[i].first] - f[j] + MOD) % MOD; } ans += sum * a[i].second % MOD * (a[i].second - 1) % MOD * qpow(2, MOD - 2) % MOD; } if (cnt == 2) { long long sum = 0; for (int j = 1; j <= a[1].first; j++) { sum = (sum + f[j + a[2].first] - f[j] + MOD) % MOD; } ans += sum * a[1].second % MOD * a[2].second % MOD; } printf("%lld", ans % MOD); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 220000; map<int, int> H; int inv[N]; void build(int st, int en, int k) { if (k == 1) { int len = en - st + 1; if (H.count(len)) H[len]++; else H[len] = 1; return; } if (st == en) return; int mid = st + en >> 1; build(st, mid, k - 1); build(mid + 1, en, k - 1); } int main() { ios_base::sync_with_stdio(0); int n, k, mod; cin >> n >> k >> mod; build(1, n, k); if (!H.size() || (H.size() == 1 && H.begin()->first == 1)) { cout << 0 << endl; return 0; } inv[1] = 1; for (int i = 2; i < N; i++) inv[i] = (long long)inv[mod % i] * (mod - mod / i) % mod; int ans = 0; if (H.size() == 1) { int len = H.begin()->first, cnt = H.begin()->second; ans = (long long)len * (len - 1) % mod * inv[4] % mod * cnt % mod; if (cnt > 1) { int now = 0, nn = 0; for (int sum = 2; sum <= len + len; sum++) { int st = max(1, sum - len), en = min(len, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt % mod * (cnt - 1) % mod * inv[2] % mod; (ans += now) >= mod && (ans -= mod); } } else { int len = H.begin()->first, cnt = H.begin()->second; ans = (long long)len * (len - 1) % mod * inv[4] % mod * cnt % mod; if (cnt > 1) { int now = 0, nn = 0; for (int sum = 2; sum <= len + len; sum++) { int st = max(1, sum - len), en = min(len, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt % mod * (cnt - 1) % mod * inv[2] % mod; (ans += now) >= mod && (ans -= mod); } map<int, int>::iterator it = H.begin(); it++; len = it->first, cnt = it->second; ans += (long long)len * (len - 1) % mod * inv[4] % mod * cnt % mod; if (ans >= mod) ans -= mod; if (cnt > 1) { int now = 0, nn = 0; for (int sum = 2; sum <= len + len; sum++) { int st = max(1, sum - len), en = min(len, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt % mod * (cnt - 1) % mod * inv[2] % mod; (ans += now) >= mod && (ans -= mod); } int len1 = H.begin()->first, len2 = it->first; int cnt1 = H.begin()->second, cnt2 = it->second; int now = 0, nn = 0; for (int sum = 2; sum <= len1 + len2; sum++) { int st = max(1, sum - len2), en = min(len1, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt1 % mod * cnt2 % mod; (ans += now) >= mod && (ans -= mod); } cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, mod, sum[200005], inv[200005], ans, s1, s2, num1, num2; void split(long long l, long long r, long long dep) { if (l > r) return; if (dep == 1 || l == r) { if (!s1) s1 = r - l + 1; if (s1 == r - l + 1) num1++; else { if (!s2) s2 = r - l + 1; if (s2 == r - l + 1) num2++; } ans = (ans + (r - l + 1) * (r - l) % mod * inv[4]) % mod; return; } long long mid = l + r >> 1; split(l, mid, dep - 1); split(mid + 1, r, dep - 1); } int main() { scanf("%lld%lld%lld", &n, &m, &mod); inv[0] = inv[1] = 1; for (long long i = 2; i <= n + 10; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (long long i = 1; i <= n + 10; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; split(1, n, m); if (s1 && s2) { long long nw = 0; for (long long i = 1; i <= s1; i++) { nw = (nw + inv[2] * s1 - sum[i + s1] + sum[i] + mod) % mod; } ans = (ans + num1 * (num1 - 1) % mod * inv[2] % mod * nw) % mod; nw = 0; for (long long i = 1; i <= s1; i++) { nw = (nw + inv[2] * s2 - sum[i + s2] + sum[i] + mod) % mod; } ans = (ans + num1 * num2 % mod * nw) % mod; nw = 0; for (long long i = 1; i <= s2; i++) { nw = (nw + inv[2] * s2 - sum[i + s2] + sum[i] + mod) % mod; } ans = (ans + num2 * (num2 - 1) % mod * inv[2] % mod * nw) % mod; } else { long long nw = 0; for (long long i = 1; i <= s1; i++) { nw = (nw + inv[2] * s1 - sum[i + s1] + sum[i] + mod) % mod; } ans = (ans + num1 * (num1 - 1) % mod * inv[2] % mod * nw) % mod; } printf("%lld", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200100; int mod, n, h; inline void Add(int &a, int b) { a = a + b >= mod ? a + b - mod : a + b; } int inv[maxn], sum[maxn]; inline int Qsum(int l, int r) { return (sum[r] - sum[l - 1] + mod) % mod; } int cnt[maxn], S, L; int ans; inline void getblock(int l, int r, int dep) { if (dep >= h || l == r) { int size = r - l + 1; cnt[size]++; if (L == 0) L = size; else if (L != size) S = size; if (S > L) swap(S, L); Add(ans, 1ll * size * (size - 1) % mod * inv[4] % mod); return; } int mid = (l + r) >> 1; getblock(l, mid, dep + 1); getblock(mid + 1, r, dep + 1); } inline int getans(int size1, int size2) { int ans = 1ll * size1 * size2 * inv[2] % mod; for (int i = 1; i <= size1; i++) Add(ans, mod - Qsum(i + 1, i + size2)); return ans; } int main() { scanf("%d%d%d", &n, &h, &mod); inv[1] = 1; for (int i = 2; i < maxn; i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i < maxn; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; getblock(1, n, 1); if (S == 0) S = L, L = 0; Add(ans, 1ll * getans(S, S) * cnt[S] % mod * (cnt[S] - 1) % mod * inv[2] % mod); if (L) Add(ans, 1ll * getans(L, L) * cnt[L] % mod * (cnt[L] - 1) % mod * inv[2] % mod); if (L) Add(ans, 1ll * getans(S, L) * cnt[S] % mod * cnt[L] % mod); printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; int inv[N], n, k, mo, ans; map<int, int> mp; void get(int n, int k) { if (k == 1 || n == 1) mp[n]++; else get(n / 2, k - 1), get(n - n / 2, k - 1); } int calc(int x, int y) { int ans = 1ll * x * y % mo * (mo + 1) / 2 % mo; for (int i = (int)(1); i <= (int)(y); i++) ans = (ans + mo + 1ll * inv[i] - inv[i + x]) % mo; return ans; } int main() { scanf("%d%d%d", &n, &k, &mo); inv[0] = inv[1] = 1; for (int i = (int)(2); i <= (int)(n); i++) inv[i] = 1ll * inv[mo % i] * (mo - mo / i) % mo; for (int i = (int)(1); i <= (int)(n); i++) inv[i] = (inv[i] + inv[i - 1]) % mo; get(n, k); for (auto a : mp) { ans = (ans + 1ll * a.first * (a.first - 1) / 2 % mo * (mo + 1) / 2 % mo * a.second) % mo; if (a.second >= 2) ans = (ans + 1ll * a.second * (a.second - 1) / 2 % mo * calc(a.first, a.first)) % mo; for (auto b : mp) if (a.first < b.first) ans = (ans + 1ll * calc(a.first, b.first) * a.second % mo * b.second) % mo; } printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAXN = (int)1e5 + 3; const int infint = (int)1e9 + 3; const long long inf = (long long)1e12; int n, k, q; vector<int> v; int add(int a, int b) { int c = a + b; if (c >= q) c -= q; if (c < 0) c += q; return c; } int mul(int a, int b) { int c = (1LL * a * b) % q; return c; } int pwr(int a, int b) { if (b == 0) return 1; int c = pwr(a, b >> 1); c = mul(c, c); if (b & 1) c = mul(c, a); return c; } int inv(int a) { return pwr(a, q - 2); } void solve(int n, int k) { if (k <= 1 || n == 1) { v.push_back(n); return; } solve(n / 2, k - 1); solve((n + 1) / 2, k - 1); } int div2; int inversion(int n) { return mul(n, mul(n - 1, inv(4))); } int sum(int a, int b) { if (a > b) return 0; a--; int s1 = mul(a, mul(a + 1, div2)), s2 = mul(b, mul(b + 1, div2)); s1 = add(s2, -s1); s1 = add(s1, a - b); return s1; } int ted[MAXN]; int f(int n1, int n2) { int cur = 0; for (int dem = 2; dem <= n1 + n2; dem++) { int num = sum(max(1, dem - n2), min(n1, dem - 1)); cur = add(cur, mul(num, inv(dem))); } return mul(cur, div2); } int debug(int n1, int n2) { int cur = 0; for (int i = 1; i <= n1; i++) for (int j = 1; j <= n2; j++) cur = add(cur, mul(i - 1, inv(i + j))); return mul(cur, inv(2)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k >> q; solve(n, k); sort(v.begin(), v.end()); div2 = inv(2); int ans = 0; for (auto u : v) ans = add(ans, inversion(u)), ted[u]++; vector<int> diff = v; diff.resize(unique(diff.begin(), diff.end()) - diff.begin()); for (int i = 0; i < diff.size(); i++) for (int j = 0; j < diff.size(); j++) { int zarib; if (i != j) zarib = mul(ted[diff[i]], ted[diff[j]]); else zarib = mul(ted[diff[i]], ted[diff[j]] - 1); ans = add(ans, mul(zarib, f(diff[i], diff[j]))); } cout << ans; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long MOD; long long powmod(long long a, long long n) { if (n == 0) return 1; if (n % 2) return (a * powmod(a, n - 1)) % MOD; long long c = powmod(a, n / 2); return (c * c) % MOD; } long long inv(long long a) { return powmod(a, MOD - 2); } long long fact[110000]; long long invfact[110000]; long long ncr(long long n, long long r) { if (r < 0 || n < 0) return 0; if (n < r) return 0; long long a = fact[n]; a = (a * invfact[r]) % MOD; a = (a * invfact[n - r]) % MOD; return a; } long long get(long long a, long long b) { long long ans = 0; for (long long q = 2; q <= a + b; q++) { long long f = min(a, q - 1) - max(1LL, q - b) + 1; ans += f * inv(q); ans %= MOD; } return ans; } int main() { long long n, k, q; cin >> n >> k >> q; MOD = q; map<long long, long long> r; r[n] = 1; for (int j = 1; j < k; j++) { map<long long, long long> rr; for (auto x : r) { if (x.first == 1) { rr[x.first] += x.second; } else { rr[x.first / 2] += x.second; rr[(x.first + 1) / 2] += x.second; } } r = rr; if (rr.count(1) && rr[1] == n) break; } long long avg = n * (n - 1) / 2; avg %= MOD; avg = (avg * inv(2)) % MOD; if (r.size() == 1) { auto x = *r.begin(); long long freq = x.second; long long g = x.first; avg -= (freq * (freq - 1) / 2) % MOD * get(g, g); avg %= MOD; } else if (r.size() == 2) { vector<pair<int, int> > x; for (auto b : r) x.push_back(b); long long freq0 = x[0].second; long long freq1 = x[1].second; avg -= (freq0 * (freq0 - 1) / 2) % MOD * get(x[0].first, x[0].first); avg %= MOD; avg -= (freq1 * (freq1 - 1) / 2) % MOD * get(x[1].first, x[1].first); avg %= MOD; avg -= (freq0 * freq1) % MOD * get(x[0].first, x[1].first); avg %= MOD; } avg %= MOD; if (avg < 0) avg += MOD; cout << avg << '\n'; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> template <typename T> inline void read(T &x) { x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); } using namespace std; int n, K, P; long long inv[201000], isum[201000]; inline void init() { inv[1] = 1; for (int i = 2; i <= max(n, 20); ++i) inv[i] = ((P - inv[P % i] * (P / i)) % P + P) % P; for (int i = 1; i <= max(n, 20); ++i) isum[i] = (isum[i - 1] + inv[i]) % P; } int tmp, cnt[201000]; void dfs_find(int L, int R, int nwk) { if (nwk == 1) { ++cnt[R - L + 1]; tmp = R - L + 1; return; } if (L == R) { ++cnt[1]; tmp = 1; return; } int mid = (L + R) >> 1; dfs_find(L, mid, nwk - 1); dfs_find(mid + 1, R, nwk - 1); } inline long long sol(int t, int tt) { long long res = 0; for (int i = 1; i <= t; ++i) { res += isum[i + tt] - isum[i]; } return (res % P + P) % P; } int main() { read(n), read(K), read(P); init(); if (K >= 30) { puts("0"); return 0; } dfs_find(1, n, K); int jzp, zzz; if (cnt[tmp - 1]) { jzp = tmp, zzz = tmp - 1; } else { jzp = tmp + 1, zzz = tmp; } int inv2 = inv[2]; long long ans = sol(jzp, jzp) * cnt[jzp] % P * (cnt[jzp] - 1) % P * inv2 % P; ans = (ans + sol(zzz, zzz) * cnt[zzz] % P * (cnt[zzz] - 1) % P * inv2 % P) % P; ans = (ans + sol(jzp, zzz) * cnt[jzp] % P * cnt[zzz] % P) % P; ans = 1ll * n * (n - 1) % P * inv[4] % P - ans; printf("%lld\n", (ans % P + P) % P); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k, mod, c[N], inv[N], s[N]; void dfs(int d, int l, int r) { if (d <= 1 || l == r) { ++c[r - l + 1]; return; } int m = (l + r) >> 1; dfs(d - 1, l, m); dfs(d - 1, m + 1, r); } int solve(int n, int m) { int res = (long long)inv[2] * n % mod * m % mod; for (int i = 1; i <= n; ++i) (res += s[i] - s[i + m]) %= mod; return (res + mod) % mod; } int main() { scanf("%d%d%d", &n, &k, &mod); inv[1] = 1; for (int i = 2; i <= max(4, n); ++i) inv[i] = (mod - mod / i) * (long long)inv[mod % i] % mod; for (int i = 1; i <= n; ++i) s[i] = (s[i - 1] + inv[i]) % mod; dfs(k, 1, n); int l = 0; for (int i = 1; i <= n; ++i) if (c[i]) { l = i; break; } int res = ((c[l] * (l - 1ll) + c[l + 1] * (l + 1ll)) % mod * l % mod * inv[4] + c[l] * (c[l] - 1ll) % mod * inv[2] % mod * solve(l, l) + (long long)c[l] * c[l + 1] % mod * solve(l, l + 1) + c[l + 1] * (c[l + 1] - 1ll) % mod * inv[2] % mod * solve(l + 1, l + 1)) % mod; printf("%d\n", res); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { long long x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(long long a) { if (a < 0) { a = -a; putchar('-'); } if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(long long a) { write(a); puts(""); } inline void wri(long long a) { write(a); putchar(' '); } const int N = 100005; int ycl[N], tong[N], mod, inv; void solve(int l, int r, int dep) { if (l == r || dep == 1) { tong[r - l + 1]++; return; } int mid = (l + r) >> 1; solve(l, mid, dep - 1); solve(mid + 1, r, dep - 1); } long long get(long long len) { return len * (len - 1) / 2 % mod * inv % mod; } inline long long ksm(long long a, int b) { int ans = 1; for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } int n, k; long long get(long long a, long long b) { if (a + b > n) return 0; long long ans = 0; for (int i = 1; i <= a; i++) { ans = (ans + inv * b - ycl[i + b] + ycl[i]) % mod; } return (ans + mod) % mod; } int main() { cin >> n >> k >> mod; inv = (mod + 1) / 2; for (int i = 1; i <= n; i++) ycl[i] = (ycl[i - 1] + ksm(i, mod - 2)) % mod; solve(1, n, k); for (int i = 1; i <= n; i++) if (tong[i]) { cout << (get(i) * tong[i] + get(i + 1) * tong[i + 1] + get(i, i + 1) * tong[i] % mod * tong[i + 1] + tong[i] * (tong[i] - 1) / 2 % mod * get(i, i) + tong[i + 1] * (tong[i + 1] - 1) / 2 % mod * get(i + 1, i + 1)) % mod << endl; return 0; } }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 100010; int mod = 998244353; void reduce(int& x) { x += x >> 31 & mod; } int mul(int a, int b) { return (long long)a * b % mod; } int pow(int a, int b, int res = 1) { for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; } void mma(int& x, int y, int z) { x = (x + (long long)y * z) % mod; } int remod(long long x) { x %= mod; return x + (x >> 63 & mod); } std::map<int, int> hav; void solve(int l, int r, int dep) { if (l == r || dep <= 1) return (void)(++hav[r - l + 1]); int mid = l + r >> 1; solve(l, mid, dep - 1); solve(mid + 1, r, dep - 1); } inline int calc1(int x) { return (long long)pow(4, mod - 2, x * (x - 1ll) % mod); } inline int coef(std::pair<int, int> x, std::pair<int, int> y) { int t = x.second; if (x.first == y.first) return (long long)t * (t - 1) / 2 % mod; return mul(t, y.second); } int n, K; const int MAXS = MAXN << 1; int inv[MAXS]; inline int calc2(int x, int y) { int res = pow(2, mod - 2, mul(x, y)); for (int i = 1; i <= x; ++i) reduce(res -= inv[i + y]), reduce(res += inv[i] - mod); return res; } int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> K >> mod; inv[0] = inv[1] = 1; for (int i = 2; i != MAXS; ++i) inv[i] = mul(inv[mod % i], mod - mod / i); for (int i = 1; i != MAXS; ++i) reduce(inv[i] += inv[i - 1] - mod); solve(1, n, K); int ans = 0; for (auto t : hav) mma(ans, t.second, calc1(t.first)); for (auto x : hav) for (auto y : hav) if (x.first <= y.first) mma(ans, coef(x, y), calc2(x.first, y.first)); std::cout << ans << std::endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, k, P, inv[N], ans, f[2], c[2]; inline void upd(int &x, int y) { ((x += y) > P) && (x -= P); } void sol(int l, int r, int k) { if (!k || l == r) { int len = r - l + 1; c[len & 1] = len; f[len & 1]++; return; } int mid = (l + r) >> 1; sol(l, mid, k - 1); sol(mid + 1, r, k - 1); } inline int C0(int n) { return 1ll * n * (n - 1) / 2 % P * inv[2] % P; } inline int C1(int n) { int ans = 1ll * n * (1ll * n * inv[2] % P - inv[n + 1] + P) % P; for (int i = (2); i <= (n); ++i) upd(ans, P - 1ll * (i - 1) * (inv[i] + inv[2 * n + 2 - i]) % P); return ans; } inline int C2(int x, int y) { if (x < y) swap(x, y); int ans = 1ll * x * y % P * inv[2] % P; for (int i = (2); i <= (x); ++i) upd(ans, P - 1ll * (i - 1) * (inv[i] + inv[x + y + 2 - i]) % P); return ans; } int main() { scanf("%d%d%d", &n, &k, &P); inv[1] = 1; for (int i = (2); i <= (2 * n); ++i) inv[i] = 1ll * (P - P / i) * inv[P % i] % P; sol(1, n, k - 1); for (int i = (0); i <= (1); ++i) upd(ans, 1ll * f[i] * C0(c[i]) % P), upd(ans, 1ll * f[i] * (f[i] - 1) / 2 % P * C1(c[i]) % P); upd(ans, 1ll * f[0] * f[1] % P * C2(c[0], c[1]) % P); printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, M, I2; map<int, int> cnt; long long inv[200050]; void divide(int m, int l, int r) { if (m == 1 || l == r) ++cnt[r - l + 1]; else { int mid = (l + r) >> 1; divide(m - 1, l, mid); divide(m - 1, mid + 1, r); } } long long pw(long long a, long long k) { long long ans = 1; for (; k; k >>= 1) { if (k & 1) ans = ans * a % M; a = a * a % M; } return ans; } void init() { scanf("%d%d%d", &n, &m, &M); divide(m, 1, n); I2 = (M + 1) >> 1; for (int i = 1; i < 200050; ++i) inv[i] = (inv[i - 1] + pw(i, M - 2)) % M; } long long C2(int x) { return 1LL * x * (x - 1) / 2 % M; } long long calc(int a, int b) { long long ans = 1LL * a * b % M * I2 % M; for (int i = 1; i <= a; ++i) ans += M - inv[i + b] + inv[i]; return ans % M; } void solve() { int ans = 0; for (auto p : cnt) ans += 1LL * C2(p.first) * I2 % M * p.second % M, ans %= M; for (auto p : cnt) { ans += calc(p.first, p.first) * C2(p.second) % M, ans %= M; for (auto q : cnt) if (p.first < q.first) ans += calc(p.first, q.first) * (p.second * q.second % M) % M, ans %= M; } printf("%d\n", ans); } int main() { init(); solve(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, k, q, A[N], num[2], Min, mod, ans, I4; namespace { int add(int x) { return (x >= mod) ? x - mod : x; } int sub(int x) { return (x < 0) ? x + mod : x; } void Add(int &x, int y) { x = add(x + y); } void Sub(int &x, int y) { x = sub(x - y); } int Pow(int x, int y = mod - 2) { int res = 1; for (; y; y >>= 1, x = 1LL * x * x % mod) { if (y & 1) res = 1LL * res * x % mod; } return res; } } // namespace vector<int> L; void solve(int l, int r, int k) { if (l > r) return; if (k <= 1 || l == r) { int Len = r - l + 1; Min = min(Min, Len); L.push_back(Len); Add(ans, 1LL * Len * (Len - 1) % mod * I4 % mod); return; } int mid = (l + r) >> 1; solve(l, mid, k - 1); solve(mid + 1, r, k - 1); } int inv[N]; int calc(int Sum, int x, int y) { int L = max(Sum - y, 1), R = min(Sum - 1, x); return max(R - L + 1, 0); } int main() { cin >> n >> k >> mod; Min = 1e9; I4 = Pow(4); inv[0] = 0; for (int i = 1; i <= n * 2; ++i) inv[i] = Pow(i); solve(1, n, k); for (int i = 0; i < (int)L.size(); ++i) ++num[L[i] - Min]; for (int i = 1; i <= 2 * (Min + 1); ++i) { int tot = 0; Add(tot, 1LL * num[0] * (num[0] - 1) % mod * inv[2] % mod * calc(i, Min, Min) % mod); Add(tot, 1LL * num[1] * (num[1] - 1) % mod * inv[2] % mod * calc(i, Min + 1, Min + 1) % mod); Add(tot, 1LL * num[0] * num[1] % mod * calc(i, Min + 1, Min) % mod); Add(ans, 1LL * tot * inv[2] % mod * sub(1 - 2LL * inv[i] % mod) % mod); } printf("%d\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> std::map<long long, long long> mp; long long p, inv[200005], sum[200005]; inline long long read() { register long long x = 0, f = 1; register char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline long long max(const long long &x, const long long &y) { return x > y ? x : y; } inline void simulate(long long l, long long r, long long k) { if (k == 1 || l == r) { std::map<long long, long long>::iterator it = mp.find(r - l + 1); if (it != mp.end()) ++it->second; else mp.insert(std::make_pair(r - l + 1, 1)); return; } long long mid = l + r >> 1; simulate(l, mid, k - 1); simulate(mid + 1, r, k - 1); } inline long long calc(long long len1, long long len2) { long long res = 0; for (register long long i = 1; i <= len1; ++i) { (res += (inv[2] * 1ll * len2 % p - (sum[i + len2] - sum[i]) % p) % p) %= p; } return res; } signed main() { long long ans = 0; long long n = read(), k = read(); p = read(); inv[1] = 1; for (register long long i = 2; i <= max(n, 4); ++i) inv[i] = (p - (p / i)) * 1ll * inv[p % i] % p; for (register long long i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + inv[i]) % p; simulate(1, n, k); for (std::map<long long, long long>::iterator it = mp.begin(); it != mp.end(); ++it) { long long len = it->first, cnt = it->second; (ans += cnt * ((len - 1) * 1ll * len % p) % p * inv[4] % p) %= p; } for (std::map<long long, long long>::iterator it1 = mp.begin(); it1 != mp.end(); ++it1) { for (std::map<long long, long long>::iterator it2 = mp.begin(); it2 != mp.end(); ++it2) { if (it1->first == it2->first) { long long len = it1->first, cnt = (it1->second - 1) * 1ll * (it1->second) / 2 % p; (ans += cnt * 1ll * calc(len, len) % p) %= p; } else if (it1->first < it2->first) { long long cnt = (it2->second) * 1ll * (it1->second) % p; (ans += cnt * 1ll * calc(it1->first, it2->first) % p) %= p; } } } printf("%lld\n", (ans + p) % p); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } uint MD; struct ModInt { using M = ModInt; uint v; ModInt() : v{0} {} ModInt(ll _v) { set_v(_v % MD + MD); } M& set_v(uint _v) { v = (_v < MD) ? _v : _v - MD; return *this; } explicit operator bool() const { return v != 0; } M operator-() const { return M(0) - *this; } M operator+(const M& r) const { return M().set_v(v + r.v); } M operator-(const M& r) const { return M().set_v(v + MD - r.v); } M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); } M operator/(const M& r) const { return *this * r.inv(); } M& operator+=(const M& r) { return *this = *this + r; } M& operator-=(const M& r) { return *this = *this - r; } M& operator*=(const M& r) { return *this = *this * r; } M& operator/=(const M& r) { return *this = *this / r; } M pow(ll n) const { M x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } M inv() const { return (*this).pow(MD - 2); } friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; } }; using Mint = ModInt; const int B = 200200; V<Mint> fact(B), iFac(B), inv(B), inv_sm(B); void first() { fact[0] = Mint(1); for (int i = 1; i < B; i++) fact[i] = fact[i - 1] * Mint(i); for (int i = 0; i < B; i++) iFac[i] = fact[i].inv(); for (int i = 1; i < B; i++) inv[i] = Mint(i).inv(); for (int i = 1; i < B; i++) inv_sm[i] = inv_sm[i - 1] + inv[i]; } Mint C(int n, int k) { if (n < k || k < 0) return 0; return fact[n] * iFac[k] * iFac[n - k]; } map<int, int> mp; void dfs(int l, int r, int h) { if (l == r) { mp[1]++; return; } if (h <= 1) { mp[r - l + 1]++; return; } int md = (l + r) / 2; dfs(l, md, h - 1); dfs(md + 1, r, h - 1); } int main() { int n, h; cin >> n >> h >> MD; first(); dfs(0, n - 1, h); auto two = [&](int l1, int l2) { Mint ans = 0; for (int i = 1; i <= l1; i++) { ans += Mint(i - 1) * (inv_sm[i + l2] - inv_sm[i]); } return ans / Mint(2); }; using P = pair<int, int>; V<P> ps; for (auto p : mp) { ps.push_back(p); } int k = int(ps.size()); Mint ans = 0; for (int i = 0; i < k; i++) { int c1, f1; tie(c1, f1) = ps[i]; ans += Mint(f1) * Mint(c1) * Mint(c1 - 1) / Mint(4); ans += Mint(f1) * Mint(f1 - 1) * two(c1, c1); for (int j = i + 1; j < k; j++) { int c2, f2; tie(c2, f2) = ps[j]; ans += Mint(f1) * Mint(f2) * (two(c1, c2) + two(c2, c1)); } } cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long Inf = 1e18; const int N = 1e5 + 10; int n, k, mod, inv[N], sinv[N], ans = 0; int gi() { int x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * o; } template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }; template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }; int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); } void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); } vector<int> seg; map<int, int> cnt; void solve(int l, int r, int d) { if (d <= 1 || l == r) { seg.push_back(r - l + 1); return; } int mid = (l + r) >> 1; solve(l, mid, d - 1), solve(mid + 1, r, d - 1); } int calc(int a, int b) { int ret = 1ll * a * b % mod; for (int i = 1; i <= a; i++) dec(ret, 2ll * sub(sinv[i + b], sinv[i]) % mod); return ret; } int main() { n = gi(), k = gi(), mod = gi(); inv[1] = 1; for (int i = 2; i <= max(n, 2); i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; sinv[1] = 1; for (int i = 2; i <= n; i++) sinv[i] = add(sinv[i - 1], inv[i]); solve(1, n, k); for (auto i : seg) inc(ans, (1ll * i * (i - 1) / 2) % mod), ++cnt[i]; for (auto i : cnt) if (i.second >= 2) inc(ans, 1ll * calc(i.first, i.first) * ((1ll * i.second * (i.second - 1) / 2) % mod) % mod); for (auto i : cnt) for (auto j : cnt) if (i.first < j.first) inc(ans, 1ll * calc(i.first, j.first) * i.second % mod * j.second % mod); ans = 1ll * ans * inv[2] % mod, cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int inv[100010], s[100010], num[100010], n, k, MOD, ans; void work(int l, int r, int h) { if (h <= 1 || l == r) { num[r - l + 1]++; return; } int mid = (l + r) >> 1; work(l, mid, h - 1); work(mid + 1, r, h - 1); } int basic(int x) { return 1ll * x * (x - 1) % MOD * inv[4] % MOD; } int cal(int x, int y) { int tans = 0; for (int i = 1; i <= x; i++) tans = (0ll + tans + s[i + y] - s[i] + MOD) % MOD; return (1ll * x * y % MOD * inv[2] - tans + MOD) % MOD; } int mypow(int x, int n) { int tans = 1; for (; n; n >>= 1, x = 1ll * x * x % MOD) if (n & 1) tans = 1ll * tans * x % MOD; return tans; } int main() { scanf("%d%d%d", &n, &k, &MOD); inv[4] = mypow(4, MOD - 2); for (int i = 1; i <= n; i++) inv[i] = mypow(i, MOD - 2); for (int i = 1; i <= n; i++) s[i] = (s[i - 1] + inv[i]) % MOD; work(1, n, k); for (int i = 1; i <= n; i++) if (num[i]) { ans = (ans + 1ll * num[i] * basic(i)) % MOD; ans = (ans + 1ll * num[i] * (num[i] - 1) % MOD * inv[2] % MOD * cal(i, i)) % MOD; if (num[i + 1]) { ans = (ans + 1ll * num[i + 1] * basic(i + 1)) % MOD; ans = (ans + 1ll * num[i + 1] * (num[i + 1] - 1) % MOD * inv[2] % MOD * cal(i + 1, i + 1)) % MOD; ans = (ans + 1ll * num[i] * num[i + 1] % MOD * cal(i, i + 1)) % MOD; } break; } printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 1e5 + 5; int n, k, q; map<int, int> cnt; long long ans; long long js[N], inv[N]; int mo; void build(int l, int r, int h) { if (l == r || h <= 1) { cnt[r - l + 1]++; return; } int mid = (l + r) / 2; build(l, mid, h - 1); build(mid + 1, r, h - 1); } long long calc(int la, int lb) { long long ret = inv[2] * la % mo * lb % mo; for (int i = 2; i <= la + lb; i++) { int l = 1, r = la; l = max(l, i - lb); r = min(r, i - 1); ret = (ret + mo - 1ll * (r - l + 1) * inv[i] % mo) % mo; } return ret; } int main() { n = get(); k = get(); mo = q = get(); build(1, n, k); inv[0] = inv[1] = 1; for (int i = 2; i <= 1e5; i++) inv[i] = 1ll * (q - q / i) * inv[q % i] % q; for (map<int, int>::iterator h = cnt.begin(); h != cnt.end(); h++) { int s = (*h).first, c = (*h).second; ans = (ans + 1ll * s * (s - 1) * inv[4] % q * c % q) % q; map<int, int>::iterator p = h; ans = (ans + inv[2] * c * (c - 1) % mo * calc(s, s) % mo) % mo; p++; for (; p != cnt.end(); p++) ans = (ans + 1ll * c * (*p).second % mo * calc(s, (*p).first)) % mo; } cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int n, k, q; void dfs(int l, int r, int dep) { if (dep <= 1 || l == r) { mp[r - l + 1]++; return; } int mid = (l + r) >> 1; dfs(l, mid, dep - 1); dfs(mid + 1, r, dep - 1); } int ADD(int x, int y) { return (x += y) >= q ? x - q : x; } int SUB(int x, int y) { return (x -= y) < 0 ? x + q : x; } int MUL(int x, int y) { return (long long)x * y % q; } int HALF(int x) { return x & 1 ? (x + q >> 1) : (x >> 1); } const int N = 100010; int inv[N], invs[N]; int calc(int x, int y) { int ret = MUL(x, y); for (int i = 1; i <= x; ++i) { ret = SUB(ret, MUL(SUB(invs[i + y], invs[i]), 2)); } return ret; } int main() { scanf("%d%d%d", &n, &k, &q); dfs(1, n, k); inv[0] = inv[1] = 1; invs[0] = invs[1] = 1; for (int i = 2; i <= n; ++i) { inv[i] = MUL(inv[q % i], q - q / i); invs[i] = ADD(invs[i - 1], inv[i]); } int ans = 0; for (auto i : mp) for (auto j : mp) if (i.first <= j.first) { if (i.first == j.first) { int ret = calc(i.first, i.first); ans = ADD(ans, MUL(ret, HALF(MUL(i.second, i.second - 1)))); ans = ADD(ans, MUL(i.second, HALF(MUL(i.first, i.first - 1)))); } else { int ret = calc(i.first, j.first); ans = ADD(ans, MUL(ret, MUL(i.second, j.second))); } } cout << HALF(ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 1e5 + 20; int n, k, M; int inv[MAXN], pre_inv[MAXN]; void math_pre() { inv[1] = 1; for (int i = 2; i <= ((n < 4) ? 4 : n); ++i) inv[i] = 1ll * (M - M / i) * inv[M % i] % M; for (int i = 1; i <= n; ++i) pre_inv[i] = (pre_inv[i - 1] + inv[i]) % M; } struct map { static const int MAXMap = 2; int tot; struct pad { int key, val; pad() {} pad(const int &KEY, const int &VAL) : key(KEY), val(VAL) {} } node[MAXMap + 1]; map() { tot = 0; } pad *find(const int &key) { pad *ret = node; while (ret - node < tot && ret->key != key) ++ret; return ret; } void insert(const pad &new_element) { node[tot++] = new_element; } pad *begin() { return &node[0]; } pad *end() { return &node[tot]; } } Map; void solve(const int &l, const int &r, const int &h) { if (l >= r || h <= 1) { int len = r - l + 1; map::pad *it = Map.find(len); if (it == Map.end()) Map.insert(map::pad(len, 1)); else ++it->val; return; } int mid = (l + r) >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } int calc(const int &len1, const int &len2) { int ret = 0; for (int i = 1; i <= len1; ++i) ret = ((ret + 1ll * inv[2] * len2 % M - (pre_inv[i + len2] - pre_inv[i + 1 - 1])) % M + M) % M; return ret; } int main() { scanf("%d%d%d", &n, &k, &M); math_pre(); solve(1, n, k); int ans = 0; for (map::pad *it = Map.begin(); it != Map.end(); ++it) { int len = it->key, cnt = it->val; ans = (ans + 1ll * cnt * len % M * (len - 1) % M * inv[4] % M) % M; } for (map::pad *it1 = Map.begin(); it1 != Map.end(); ++it1) for (map::pad *it2 = Map.begin(); it2 != Map.end(); ++it2) { if (it1 == it2) { int len = it1->key, cnt = 1ll * (0 + (it1->val - 1)) * it1->val / 2 % M; ans = (ans + 1ll * cnt * calc(len, len) % M) % M; } else if (it1->key < it2->key) { int len1 = it1->key, len2 = it2->key, cnt = 1ll * it1->val * it2->val % M; ans = (ans + 1ll * cnt * calc(len1, len2) % M) % M; } } printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k, p, inv[N], res[2][2]; vector<int> segs, tmp; void dfs(int l, int r, int k) { if (l == r || k == 1) { segs.emplace_back(r - l + 1); tmp.emplace_back(r - l + 1); return; } int mid = (l + r) >> 1; dfs(l, mid, k - 1); dfs(mid + 1, r, k - 1); } int calc(int a, int b) { int ans = 0; for (int i = 3; i <= a + b; ++i) { int inf = max(i - b, 1), suf = min(a, i - 1); assert(inf <= suf); ans = (ans + 1ll * (suf - inf + 1) * (i - 2) % p * inv[i]) % p; } return 1ll * ans * inv[2] % p; } int main() { scanf("%d%d%d", &n, &k, &p); inv[0] = inv[1] = 1; for (int i = 2; i < N; ++i) inv[i] = 1ll * (p - p / i) * inv[p % i] % p; dfs(1, n, k); sort(tmp.begin(), tmp.end()); tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end()); int ans = 0, m = tmp.size(); assert(m <= 2); for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) res[i][j] = calc(tmp[i], tmp[j]); int cnt[2] = {0, 0}; for (int i = int(segs.size()) - 1; ~i; --i) { int cur = segs[i], ind = (cur != tmp[0]) ? 1 : 0; ans = (ans + 1ll * cur * (cur - 1) % p * inv[4]) % p; for (int j = 0; j < m; ++j) ans = (ans + 1ll * res[ind][j] * cnt[j]) % p; ++cnt[ind]; } printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int Maxn = 100000; int n, k, Mod; int sum[Maxn + 5]; int inv[Maxn + 5]; void init() { inv[1] = 1; for (int i = 2; i <= Maxn; i++) { inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod; } for (int i = 1; i <= Maxn; i++) { sum[i] = (sum[i - 1] + inv[i]) % Mod; } } int cnt[Maxn + 5]; void merge_sort(int l, int r, int k) { if (k == 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; merge_sort(l, mid, k - 1); merge_sort(mid + 1, r, k - 1); } int calc(int len_1, int len_2) { int ans = 0; for (int i = 1; i <= len_1; i++) { ans = (ans + 1ll * len_2 * inv[2]) % Mod; int now = (sum[i + len_2] - sum[i] + Mod) % Mod; ans = (ans - now + Mod) % Mod; } return ans; } int lis[Maxn + 5], lis_len; int solve() { int ans = 0; for (int i = 1; i <= n; i++) { if (cnt[i]) { lis[++lis_len] = i; ans = (ans + 1ll * cnt[i] * i * (i - 1) % Mod * inv[4]) % Mod; if (cnt[i] > 1) { ans = (ans + 1ll * cnt[i] * (cnt[i] - 1) % Mod * inv[2] % Mod * calc(i, i)) % Mod; } } } for (int i = 1; i <= lis_len; i++) { for (int j = i + 1; j <= lis_len; j++) { ans = (ans + 1ll * cnt[lis[i]] * cnt[lis[j]] % Mod * calc(lis[i], lis[j])) % Mod; } } return ans; } int main() { scanf("%d%d%d", &n, &k, &Mod); init(); merge_sort(1, n, k); printf("%d\n", solve()); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T> void dprintln(const T &t) { cout << t << endl; } template <typename T, typename... Args> void dprintln(const T &t, const Args &...rest) { cout << t << ' '; dprintln(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { cout << t << ' '; println(rest...); } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { cout << t << ' '; print(rest...); } template <class T> void scan(T &t) { cin >> t; } template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long; using vl = vector<ll>; using vi = vector<int>; using pii = pair<int, int>; using vb = vector<bool>; using vpii = vector<pii>; auto bet = [](const ll x, const ll y, const ll i) { return x <= i && i <= y; }; template <typename T1, typename T2> T1 ceil(T1 x, T2 y) { return (x + y - 1) / y; } inline int h_bit(unsigned int x) { return 31 - __builtin_clz(x); } inline int h_bitll(unsigned long long x) { return 63 - __builtin_clzll(x); } template <typename T> struct bit { vector<T> a; explicit bit(int n, int v = 0) { a.resize(n + 1); if (v != 0) { for (int i = 1; i <= n; ++i) a[i] = v; } } T sum(T x) { T res = 0; while (x) { res += a[x]; x -= x & -x; } return res; } T sum(int l, int r) { if (l > r) return 0; return sum(r) - sum(l - 1); } void add(int x, T v) { while (x < a.size()) { a[x] += v; x += x & -x; } } void clear() { fill(a.begin(), a.end(), 0); } }; vi get_prime(int n) { vi minp(n + 1), p; for (int i = 2; i <= n; i++) { if (!minp[i]) { minp[i] = i; p.push_back(i); } for (auto &x : p) { if (x <= minp[i] && x * i <= n) minp[x * i] = x; else break; } } return p; } const int mod = 998244353; inline void add_mod(ll &x, const ll &y) { x += y; if (x >= mod) x -= mod; } inline ll submod(ll x, ll y) { return x >= y ? x - y : x - y + mod; } void sub_mod(ll &x, const ll y) { x -= y; if (x < 0) x += mod; } template <typename T> using vv = vector<vector<T>>; template <typename T1, typename T2 = T1> using vp = vector<pair<T1, T2>>; template <typename T, int n> using va = vector<array<T, n>>; struct UF { vi par; explicit UF(int n) { par.assign(n + 1, 0); for (int i = (1); i < (n + 1); ++i) par[i] = i; } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void unite(int x, int y) { par[find(x)] = find(y); } }; vi get_popcnt(int n) { vi res(n + 1); for (int i = (0); i < (n); ++i) { if (i * 2 <= n) res[i * 2] = res[i]; if ((i & 1) == 0) res[i + 1] = res[i] + 1; } return res; } const int N = 30, M = 1005; int l1 = 0, l2 = 0; int c1, c2; void dfs(int a, int h) { if (a == 0) return; if (h == 1 || a == 1) { if (l1 == 0) { l1 = a; ++c1; } else if (a == l1) ++c1; else { if (l2 == 0) l2 = a; ++c2; } return; } dfs(a / 2, h - 1), dfs(a - a / 2, h - 1); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k, q; scan(n, k, q); vl inv(max(4, n) + 1); inv[1] = 1; for (int i = (2); i < (max(4, n) + 1); ++i) inv[i] = q - ll(q / i) * inv[q % i] % q; dfs(n, k); ll ans = 0; if (c1 > 0) ans += 1LL * c1 * (l1 - 1) % q * l1 % q * inv[4] % q; if (c2 > 0) ans += 1LL * c2 * (l2 - 1) % q * l2 % q * inv[4] % q; if (c1 > 1) { ll sum = 0; for (int i = (2); i < (2 * l1 + 1); ++i) { int mi = max(1, i - l1); int ma = min(l1, i - 1); sum += 1LL * (ma - mi + 1) * (inv[2] - inv[i] + q) % q; } ans += 1LL * c1 * (c1 - 1) / 2 % q * sum % q; } if (c2 > 1) { ll sum = 0; for (int i = (2); i < (2 * l2 + 1); ++i) { int mi = max(1, i - l2); int ma = min(l2, i - 1); sum += 1LL * (ma - mi + 1) * (inv[2] - inv[i] + q) % q; } ans += 1LL * c2 * (c2 - 1) / 2 % q * sum % q; } if (c1 > 0 && c2 > 0) { ll sum = 0; for (int i = (2); i < (l1 + l2 + 1); ++i) { int mi = max(1, i - l2); int ma = min(l1, i - 1); sum += 1LL * (ma - mi + 1) * (inv[2] - inv[i] + q) % q; } ans += 1LL * c1 * c2 % q * sum % q; } println(ans % q); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int i, j, n, k, mo, rev[N], a[N], num, d[N], s[N], l, r; long long ans; void work(int l, int r, int k) { if (k <= 1 || l == r) { a[++num] = r - l + 1; return; } int m = (l + r) >> 1; work(l, m, k - 1), work(m + 1, r, k - 1); } int calc(int a, int b) { int i; long long S = (long long)a * b % mo; for (i = 1; i <= a; i++) S -= s[i + b] - s[i]; return S % mo; } int main() { scanf("%d%d%d", &n, &k, &mo); rev[0] = rev[1] = 1; for (i = 2; i <= n; i++) rev[i] = (long long)(mo - mo / i) * rev[mo % i] % mo; for (i = 1; i <= n; i++) s[i] = (s[i - 1] + rev[i]) % mo; for (i = 1; i <= n; i++) s[i] <<= 1; work(1, n, k); for (i = 1; i <= num; i++) d[a[i]]++; for (i = 1; i <= n; i++) if (d[i]) l = !l ? i : l, r = i; for (i = l; i <= r; i++) ans += ((long long)i * (i - 1) / 2 % mo * d[i] + (long long)d[i] * (d[i] - 1) / 2 % mo * calc(i, i)) % mo; if (l != r) ans += (long long)d[l] * d[r] % mo * calc(l, r) % mo; ans = ans % mo * (mo + 1) / 2 % mo; printf("%lld\n", (ans + mo) % mo); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, k, mod, q1, q2, ans; int inv[N], cnt[N]; void solve(int l, int r, int d) { if (d == 1 || l == r) { int len = r - l + 1; q1&& q1 != len ? q2 = len : q1 = len; ans = (ans + 1ll * len * (len - 1) / 2 % mod * (mod + 1) / 2) % mod; cnt[len]++; return; } int mid = (l + r) / 2; solve(l, mid, d - 1); solve(mid + 1, r, d - 1); } int main() { scanf("%d%d%d", &n, &k, &mod); inv[0] = inv[1] = 1; for (int i = (int)(2); i <= (int)(N - 1); i++) inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod; for (int i = (int)(1); i <= (int)(N - 1); i++) inv[i] = (inv[i] + inv[i - 1]) % mod; solve(1, n, k); for (int i = (int)(1); i <= (int)(q1); i++) { int val = 1ll * (i - 1) * cnt[q1] % mod * (mod + 1) / 2 % mod; ans = (ans + 1ll * (cnt[q1] - 1) * (inv[i + q1] + mod - inv[i]) % mod * val) % mod; ans = (ans + 1ll * (cnt[q2] - 0) * (inv[i + q2] + mod - inv[i]) % mod * val) % mod; } for (int i = (int)(1); i <= (int)(q2); i++) { int val = 1ll * (i - 1) * cnt[q2] % mod * (mod + 1) / 2 % mod; ans = (ans + 1ll * (cnt[q1] - 0) * (inv[i + q1] + mod - inv[i]) % mod * val) % mod; ans = (ans + 1ll * (cnt[q2] - 1) * (inv[i + q2] + mod - inv[i]) % mod * val) % mod; } printf("%d\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long n, k, gs, ans, mod, len[N], jc[N], inv[N], sum[N], ycl[2005][2005]; inline long long read() { long long res = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { res = res * 10 + c - 48; c = getchar(); } return res * f; } void build(long long l, long long r, long long h) { if (h <= 1 || l == r) { len[++gs] = r - l + 1; return; } long long mid = (l + r) / 2; build(l, mid, h - 1); build(mid + 1, r, h - 1); } long long C(long long n, long long m) { if (n < 0 || m < 0 || n < m) return 0; return jc[n] * inv[m] % mod * inv[n - m] % mod; } signed main() { n = read(); k = read(); mod = read(); jc[0] = jc[1] = inv[0] = inv[1] = 1; for (long long i = 2; i <= 100000; i++) jc[i] = jc[i - 1] * i % mod; for (long long i = 2; i <= 100000; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; build(1, n, k); for (long long i = 1; i <= gs; i++) { ans = (ans + len[i] * (len[i] - 1) % mod * inv[4] % mod) % mod; } sort(len + 1, len + gs + 1); long long x = 0; for (long long i = 1; i <= gs; i++) if (len[i] == len[i - 1]) sum[x]++; else { len[++x] = len[i]; sum[x] = 1; } gs = x; for (long long i = 1; i <= gs; i++) for (long long j = 1; j <= i; j++) { long long res = 0; res = len[i] * len[j] % mod * inv[2] % mod; for (long long a = 1; a <= len[i] + len[j]; a++) { long long l = max(1ll, a - len[j]), r = min(a - 1, len[i]); res = (res - inv[a] * (r - l + 1) % mod + mod) % mod; } if (i == j) ans = (ans + res * sum[i] % mod * (sum[i] - 1 + mod) % mod * inv[2] % mod) % mod; else ans = (ans + res * sum[i] % mod * sum[j] % mod) % mod; } cout << ans % mod; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> long long n, k, q, t1, t2, cnt1, cnt2, inv4, inv2, inv[200001]; long long ans; inline long long mul(const long long &a, const long long &b) { return 1ll * a * b % q; } inline long long qsm(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = mul(ans, a); a = mul(a, a); b >>= 1; } return ans; } void merge(long long l, long long r, long long k) { if (l < r) { if (k <= 1) { if (t1 == r - l + 1) ++cnt1; else if (t2 == r - l + 1) ++cnt2; else if (!t1) t1 = r - l + 1, cnt1 = 1; else t2 = r - l + 1, cnt2 = 1; (ans += mul(mul(r - l + 1, r - l), inv4)) %= q; return; } merge(l, (l + r) >> 1, k - 1); merge(((l + r) >> 1) + 1, r, k - 1); } else { { if (t1 == r - l + 1) ++cnt1; else if (t2 == r - l + 1) ++cnt2; else if (!t1) t1 = r - l + 1, cnt1 = 1; else t2 = r - l + 1, cnt2 = 1; (ans += mul(mul(r - l + 1, r - l), inv4)) %= q; return; } } } long long getans(long long n, long long m) { long long ans = 0; (ans += mul(inv2, mul(n, m))) %= q; for (long long i = 1; i <= n; i++) (ans += (inv[i] - inv[i + m] + q)) %= q; return ans; } signed main() { scanf("%I64d%I64d%I64d", &n, &k, &q); inv4 = qsm(4, q - 2); inv2 = (q + 1) >> 1; inv[0] = inv[1] = 1; for (long long i = 2; i <= n; i++) inv[i] = mul(q - q / i, inv[q % i]); for (long long i = 1; i <= n; i++) (inv[i] += inv[i - 1]) %= q; merge(1, n, k); (ans += mul(getans(t1, t1), mul(mul(cnt1, cnt1 - 1), inv2))) %= q; (ans += mul(getans(t2, t2), mul(mul(cnt2, cnt2 - 1), inv2))) %= q; (ans += mul(getans(t1, t2), mul(cnt1, cnt2))) %= q; printf("%I64d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( "title" ".in", "r", stdin); freopen( "title" ".out", "w", stdout); } int read() { int g = 0, f = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { g = g * 10 + ch - '0'; ch = getchar(); } return g * f; } const int N = 1e5 + 5; int n, k, mod, l1, l2, c1, c2, inv[N], s[N]; int ksm(int x, int y) { int re = 1; for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) re = 1ll * re * x % mod; return re; } void solve(int l, int r, int h) { if (h == k || l == r) { if (!l1) l1 = r - l + 1, c1++; else if (r - l + 1 == l1) c1++; else if (!l2) l2 = r - l + 1, c2++; else c2++; return void(); } int mid = l + r >> 1; solve(l, mid, h + 1), solve(mid + 1, r, h + 1); } int calc(int l1, int l2) { if (!l1 || !l2) return 0; int re = 1ll * l1 * l2 % mod * inv[2] % mod; for (int i = (1); i <= (l1); i++) re = (1ll * re + mod - (s[i + l2] + mod - s[i]) % mod) % mod; return re; } signed main() { n = read(), k = read(), mod = read(); inv[1] = 1; for (int i = (2); i <= ((int)1e5); i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = (1); i <= (n); i++) s[i] = (s[i - 1] + inv[i]) % mod; solve(1, n, 1); int ans = (1ll * c1 * l1 % mod * (l1 - 1) % mod * inv[4] % mod + 1ll * c2 * l2 % mod * (l2 - 1) % mod * inv[4] % mod) % mod; ans = (ans + 1ll * c1 * (c1 - 1) % mod * inv[2] % mod * calc(l1, l1) % mod) % mod; ans = (ans + 1ll * c2 * (c2 - 1) % mod * inv[2] % mod * calc(l2, l2) % mod) % mod; ans = (ans + 1ll * c1 * c2 % mod * calc(l1, l2) % mod) % mod; return cout << ans, signed(); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, K, P, inv[200003], cnt[200003], mn = 1e9, ans; void solve(int l, int r, int dep) { if (dep == 1 || l == r) { ++cnt[r - l + 1], mn = min(mn, r - l + 1); return; } int mid = (l + r) >> 1; solve(l, mid, dep - 1), solve(mid + 1, r, dep - 1); } int calc(int A, int B) { int res = 0; for (int i = 2; i <= A + B; ++i) res = (res + 1ll * (min(i - 1, A) - max(1, i - B) + 1) * (i - 2) % P * inv[i] % P) % P; return 1ll * res * inv[2] % P; } int main() { cin >> n >> K >> P; solve(1, n, K); inv[1] = 1; for (int i = 2; i <= 2 * n; ++i) inv[i] = P - 1ll * P / i * inv[P % i] % P; ans = 1ll * inv[2] * (1ll * (1ll * (mn) * ((mn)-1) / 2 % P) * cnt[mn] % P + 1ll * (1ll * (mn + 1) * ((mn + 1) - 1) / 2 % P) * cnt[mn + 1] % P) % P; ans = (ans + 1ll * (1ll * (cnt[mn]) * ((cnt[mn]) - 1) / 2 % P) * calc(mn, mn) % P) % P; ans = (ans + 1ll * (1ll * (cnt[mn + 1]) * ((cnt[mn + 1]) - 1) / 2 % P) * calc(mn + 1, mn + 1) % P) % P; ans = (ans + 1ll * cnt[mn] * cnt[mn + 1] % P * calc(mn, mn + 1) % P) % P; cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using namespace std; long long MOD; long long pw(long long a, long long b) { long long ans = 1; while (b) { while (!(b & 1)) b >>= 1, a = (a * a) % MOD; ans = (ans * a) % MOD, --b; } return ans; } map<int, int> mm; void run(int n, int k) { if (n <= 0) return; if (n == 1) { ++mm[1]; return; } if (k <= 1) { ++mm[n]; return; } int n2 = n / 2; run(n2, k - 1); run(n - n2, k - 1); } long long n, k; long long get(int a, int b) { if (a > b) swap(a, b); long long ans = 0; for (int i = 0; i <= (a - 1) + (b - 1); ++i) { long long cnt = 0; if (i < a) cnt = i + 1; else if (i < b) cnt = a; else cnt = i + 1 - (i + 1 - a) - (i + 1 - b); ans = (ans + cnt * pw(i + 2, MOD - 2)) % MOD; } return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k >> MOD; run(n, k); vector<pair<int, long long>> vv; for (auto x : mm) vv.push_back(x); long long ans = n * (n - 1) * pw(4, MOD - 2); long long sum = 0; for (int i = 0; i < vv.size(); ++i) { long long go = get(vv[i].first, vv[i].first); sum = (sum + go * vv[i].second % MOD * (vv[i].second - 1) % MOD * pw(2, MOD - 2)) % MOD; for (int j = i + 1; j < vv.size(); ++j) sum = (sum + get(vv[i].first, vv[j].first) * vv[i].second % MOD * vv[j].second) % MOD; } ans = (ans - sum + MOD) % MOD; cout << ans << "\n"; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void read(int &x) { char ch; int fu = 1; while ((ch = getchar()) <= 32) ; x = 0; if (ch == '-') fu = -1; else x = ch - 48; while ((ch = getchar()) > 32) x = x * 10 + ch - 48; x *= fu; } void read(long long &x) { char ch; int fu = 1; while ((ch = getchar()) <= 32) ; x = 0; if (ch == '-') fu = -1; else x = ch - 48; while ((ch = getchar()) > 32) x = x * 10 + ch - 48; x *= fu; } const double pi = acos(-1); void upmax(int &a, int b) { if (a < b) a = b; } void upmin(int &a, int b) { if (a > b) a = b; } const int N = 200220, inf = 1000000000; int n, k, p, ans; int a[N], inv[N], s[N]; void get(int n, int k) { if (k == 1 || n == 1) { a[n]++; return; } get(n / 2, k - 1); get((n + 1) / 2, k - 1); } int calc(int n, int m) { int res = 1LL * n * m % p * inv[2] % p; for (int i = 1; i <= m; i++) res = (0LL + res - s[n + i] + s[i]) % p; return res; } int main() { scanf("%d%d%d", &n, &k, &p); inv[1] = 1; for (int i = 2; i <= max(n * 2, 4); i++) inv[i] = -1LL * inv[p % i] * (p / i) % p; for (int i = 1; i <= n * 2; i++) s[i] = (s[i - 1] + inv[i]) % p; get(n, k); for (int i = 1; i <= n; i++) if (a[i]) { ans = (ans + 1LL * i * (i - 1) % p * inv[4] % p * a[i]) % p; } ans = (ans + p) % p; for (int i = 1; i <= n; i++) if (a[i]) ans = (ans + 1LL * a[i] * (a[i] - 1) % p * inv[2] % p * calc(i, i)) % p; for (int i = 1; i <= n; i++) if (a[i]) for (int j = i + 1; j <= n; j++) if (a[j]) ans = (ans + 1LL * a[i] * a[j] % p * calc(i, j)) % p; ans = (ans + p) % p; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, p, to[210000], pre[210000], ans, inv[2100000], inv2; vector<int> V; int Pow(int x, int y) { int ans = 1; for (int i = 1; i <= y; i *= 2, x = 1LL * x * x % p) if (i & y) ans = 1LL * ans * x % p; return ans; } void dfs(int l, int r, int h) { if (l < r) { if (h <= 1) { ans = (ans + 1LL * (r - l + 1) * (r - l) / 2 % p * Pow(2, p - 2)) % p; V.push_back(r - l + 1); } else { int m = (l + r) / 2; dfs(l, m, h - 1); dfs(m + 1, r, h - 1); } } else { V.push_back(1); } } int main() { scanf("%d%d%d", &n, &k, &p); dfs(1, n, k); map<int, int> M; for (auto y : V) { M[y] += 1; } inv2 = Pow(2, p - 2); for (int i = 1; i <= n; i++) inv[i] = Pow(i, p - 2); for (auto it : M) for (auto it2 : M) { int fac; if (it.first == it2.first) fac = 1LL * it.second * (it.second - 1) % p; else fac = 1LL * it.second * it2.second % p; int tmp = 0; for (int l = 2; l <= it.first + it2.first; l++) { int low = max(l - it2.first, 1) - 1; int hi = min(l - 1, it.first) - 1; tmp = (tmp + 1LL * (low + hi) * (hi - low + 1) / 2 % p * inv[l] % p * inv2) % p; } ans = (ans + 1LL * tmp * fac) % p; } printf("%d\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } template <typename F> inline void write(F x, char ed = '\n') { static short st[30]; short tp = 0; if (x < 0) putchar('-'), x = -x; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar('0' | st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } const int N = 500005; long long inv[N]; int n, k, P; long long L1, L2, C1, C2; long long calc(int L1, int L2) { long long ans = 0; for (int i = 1; i <= L1; i++) ans = ans + inv[i + L2] - inv[i]; return (ans % P + P) % P; } void solve(int l, int r, int k) { if (k <= 1 || l == r) { int len = r - l + 1; !L1 ? L1 = len : !L2 && (L2 = len); len == L1 ? C1++ : C2++; return; } int mid = (l + r) >> 1; solve(l, mid, k - 1), solve(mid + 1, r, k - 1); } int main() { read(n), read(k), read(P); inv[0] = inv[1] = 1; long long inv2 = (P + 1) >> 1; for (int i = 2; i <= n; i++) inv[i] = (P - P / i) * inv[P % i] % P; for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] + inv[i] % P; solve(1, n, k); long long ans = (long long)n * (n - 1) % P * inv2 % P * inv2 % P; ans = (ans - C1 * (C1 - 1) / 2 % P * calc(L1, L1) - C2 * (C2 - 1) / 2 % P * calc(L2, L2) % P - C1 * C2 % P * calc(L1, L2)) % P; write((ans % P + P) % P); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int n, k, mod, inv2; int ans = 0; inline void add(int& a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } inline int ksm(int a, int b) { int ans = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) ans = (long long)ans * a % mod; return ans; } inline void build(int l, int r, int h) { if (l < r) { if (h <= 1) { int len = r - l + 1; mp[len]++; add(ans, (long long)len * (len - 1) / 2ll % mod * inv2 % mod); } else { int mid = (l + r) >> 1; build(l, mid, h - 1); build(mid + 1, r, h - 1); } } else mp[1]++; } int main() { cin >> n >> k >> mod; inv2 = ksm(2, mod - 2); build(1, n, k); for (auto i = mp.begin(); i != mp.end(); ++i) { for (auto j = i; j != mp.end(); ++j) { int gs = (long long)i->second * (j->second - (i->first == j->first)) % mod; for (int l = 2; l <= i->first + j->first; ++l) { int minn = max(1, l - j->first); int maxx = min(i->first, l - 1); int tmp = (long long)gs * (maxx - minn + 1) % mod * (inv2 - ksm(l, mod - 2)) % mod; add(ans, tmp); } } } cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int cnt[maxn], mod; int Pow(int x, int p) { int r = 1; while (p) { if (p & 1) r = (long long)x * r % mod; p >>= 1; x = (long long)x * x % mod; } return r; } void dfs(int n, int k) { if (n == 1 || k == 1) { cnt[n]++; return; } dfs(n / 2, k - 1); dfs((n + 1) / 2, k - 1); } int calc(int x, int y) { int res = 0; for (int i = 2; i <= x + y; ++i) res = (res + (long long)min(x + y - i + 1, i - 1) * (i - 2) % mod * Pow(2 * i, mod - 2) % mod) % mod; return res; } int main() { int n, k; scanf("%d %d", &n, &k); scanf("%d", &mod); dfs(n, k); int ans = 0; for (int i = 1; i <= n; ++i) if (cnt[i]) { ans = (ans + (long long)cnt[i] * i % mod * (i - 1) % mod * Pow(4, mod - 2) % mod) % mod; ans = (ans + (long long)cnt[i] * (cnt[i] - 1) / 2 % mod * calc(i, i)) % mod; } for (int i = 1; i <= n; ++i) if (cnt[i]) for (int j = i + 1; j <= n; ++j) if (cnt[j]) ans = (ans + (long long)cnt[i] * cnt[j] % mod * calc(i, j)); cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( "title" ".in", "r", stdin); freopen( "title" ".out", "w", stdout); } int read() { int g = 0, f = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { g = g * 10 + ch - '0'; ch = getchar(); } return g * f; } const int N = 1e5 + 5; int n, k, mod, l1, l2, c1, c2, inv[N], s[N]; int ksm(int x, int y) { int re = 1; for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) re = 1ll * re * x % mod; return re; } void solve(int l, int r, int h) { if (h == k || l == r) { if (!l1) l1 = r - l + 1, c1++; else if (r - l + 1 == l1) c1++; else if (!l2) l2 = r - l + 1, c2++; else c2++; return void(); } int mid = l + r >> 1; solve(l, mid, h + 1), solve(mid + 1, r, h + 1); } int calc(int l1, int l2) { if (!l1 || !l2) return 0; int re = 1ll * l1 * l2 % mod * inv[2] % mod; for (int i = (1); i <= (l1); i++) re = (re + mod - (s[i + l2] + mod - s[i]) % mod) % mod; return re; } signed main() { n = read(), k = read(), mod = read(); inv[1] = 1; for (int i = (2); i <= ((int)1e5); i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (int i = (1); i <= (n); i++) s[i] = (s[i - 1] + inv[i]) % mod; solve(1, n, 1); int ans = (1ll * c1 * l1 % mod * (l1 - 1) % mod * inv[4] % mod + 1ll * c2 * l2 % mod * (l2 - 1) % mod * inv[4] % mod) % mod; ans = (ans + 1ll * c1 * (c1 - 1) % mod * inv[2] % mod * calc(l1, l1) % mod) % mod; ans = (ans + 1ll * c2 * (c2 - 1) % mod * inv[2] % mod * calc(l2, l2) % mod) % mod; ans = (ans + 1ll * c1 * c2 % mod * calc(l1, l2) % mod) % mod; return cout << ans, signed(); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, sgn = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn; } const int N = 1e5 + 10; int n, k, mod; long long sum[N], inv[N]; map<int, int> m; map<int, int>::iterator it, it1; long long ans; long long qp(long long x, int t) { long long res = 1; for (; t; t >>= 1, x = x * x % mod) if (t & 1) res = res * x % mod; return res; } void divide(int l, int r, int k) { if (l == r || k <= 1) { m[r - l + 1]++; return; } int mid = l + r >> 1; divide(l, mid, k - 1); divide(mid + 1, r, k - 1); } long long calc(int x, int y) { long long res = 1ll * x * y % mod; for (int i = 1; i <= x; i++) res = (res - 2 * sum[i + y] + 2 * sum[i] + mod) % mod; return (res + mod) % mod; } int main() { n = read(), k = read(), mod = read(); for (int i = 1; i < N; i++) inv[i] = qp(i, mod - 2); for (int i = 1; i < N; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; divide(1, n, k); for (it = m.begin(); it != m.end(); it++) { long long x = it->first, s = it->second; ans = (ans + x * (x - 1) % mod * inv[2] % mod * inv[2] % mod) % mod; ans = (ans + s * (s - 1) % mod * inv[2] % mod * inv[2] % mod * calc(x, x) % mod) % mod; } for (it = m.begin(); it != m.end(); it++) for (it1 = m.begin(); it1 != m.end(); it1++) { long long x1 = it->first, s1 = it->second, x2 = it1->first, s2 = it1->second; if (x1 >= x2) continue; ans = (ans + s1 * s2 % mod * inv[2] % mod * calc(x1, x2) % mod) % mod; } printf("%lld\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define For(i,x,y) for (register int i=(x);i<=(y);i++) #define FOR(i,x,y) for (register int i=(x);i<(y);i++) #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) #define Debug(v) for (auto i:v) printf("%lld ",i);puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define fil(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pa; typedef pair<ll,ll> PA; typedef vector<int> poly; inline ll read(){ ll x=0,f=1;char c=getchar(); while ((c<'0'||c>'9')&&(c!='-')) c=getchar(); if (c=='-') f=-1,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } namespace Fuckdfc{ const int mod = 998244353; inline int power(int a,int b){ int ret=1; for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) ret=1ll*ret*a%mod; return ret; } inline int Mod(int x){ return x>=mod?x-mod:x; } inline int Get(int x){ int ret=1; while (ret<x) ret<<=1; return ret; } poly pos; inline void DFT(poly &a,int n){ a.resize(n); FOR(i,0,n) if (i<pos[i]) swap(a[i],a[pos[i]]); for (int i=1;i<n;i<<=1){ int wn=power(3,(mod-1)/2/i); for (int j=0;j<n;j+=i<<1){ for (int w=1,k=0;k<i;k++,w=1ll*w*wn%mod){ int x=a[j+k],y=1ll*w*a[i+j+k]%mod; a[j+k]=Mod(x+y),a[i+j+k]=Mod(x+mod-y); } } } } inline void IDFT(poly &a,int n){ DFT(a,n),reverse(a.begin()+1,a.end()); int inv=power(n,mod-2); FOR(i,0,n) a[i]=1ll*a[i]*inv%mod; } inline poly Mul(poly a,poly b){ int siz=siz(a)+siz(b)-1,n=Get(siz); pos.resize(n); FOR(i,0,n) pos[i]=(pos[i>>1]>>1)|((i&1)*(n>>1)); DFT(a,n),DFT(b,n); FOR(i,0,n) a[i]=1ll*a[i]*b[i]%mod; return IDFT(a,n),a; } } const int N = 2e5+10, mod = 998244353; int n,k; int inv[N]; inline int power(int a,int b){ int ret=1; for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) ret=1ll*ret*a%mod; return ret; } inline void init(int n){ inv[0]=1; For(i,1,n) inv[i]=power(i,mod-2); } int cnt[N],tot,a[N],b[N]; inline void solve(int l,int r,int k){ if (k==1||l==r){ ++tot; For(i,l,r) a[i]=i-l+1,b[i]=tot; return cnt[r-l+1]++,void(0); } int mid=l+r>>1; solve(l,mid,k-1),solve(mid+1,r,k-1); } inline int calc(int i,int j){ return 1ll*inv[2]*inv[i+j]%mod*(i+j-2)%mod; } int main(){ n=read(),k=read(); solve(1,n,k),init(2*n); int ans=0; For(i,1,n) ans=(ans+1ll*i*(i-1)/2%mod*inv[2]%mod*cnt[i])%mod; poly f(n+1); For(i,1,n) f[a[i]]++; poly g=Fuckdfc::Mul(f,f); int ans2=0; FOR(i,2,siz(g)) ans2=(ans2+1ll*g[i]*inv[2]%mod*inv[i]%mod*(i-2))%mod; for (int l=1,r=1;l<=n;l=++r){ while (b[r+1]==b[l]) ++r; int y=0; For(i,2,2*(r-l+1)){ int x=1ll*inv[2]%mod*inv[i]%mod*(i-2)%mod; if (i<=r-l+2) ++y; else --y; ans2=(ans2+1ll*(mod-x)*y)%mod; } } printf("%d\n",(ans+1ll*ans2*inv[2])%mod); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 1e5; int n, k, q, l1, l2, c1, c2; long long iv[mxN + 1], pi[mxN + 1], ans; void ms(int l = 0, int r = n - 1, int h = k) { if (h < 2 || l == r) { if (!l1 || r - l + 1 == l1) { l1 = r - l + 1; ++c1; } else { l2 = r - l + 1; ++c2; } return; } int m = (l + r) / 2; ms(l, m, h - 1); ms(m + 1, r, h - 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> q; iv[1] = 1; for (int i = 2; i <= n; ++i) iv[i] = (q - q / i) * iv[q % i] % q; for (int i = 1; i <= n; ++i) pi[i] = (pi[i - 1] + iv[i]) % q; ms(); cout << l1 << " " << l2 << " " << c1 << " " << c2 << endl; if (c1 > 1) for (int i = 1; i <= l1; ++i) ans += (pi[l1 + i] - pi[i] + q) * c1 % q * (c1 - 1) % q; if (c2 > 1) for (int i = 1; i <= l2; ++i) ans += (pi[l2 + i] - pi[i] + q) * c2 % q * (c2 - 1) % q; for (int i = 1; i <= l1; ++i) ans += (pi[l2 + i] - pi[i] + q) * c1 % q * c2 % q * 2; cout << (iv[2] * n % q * (n - 1) % q - ans % q + q) * iv[2] % q; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using ld = long double; int mod; ll pw(ll a, int b) { if (!b) { return 1; } ll v = pw(a, b / 2); v = (v * v) % mod; if (b & 1) { v = (v * a) % mod; } return v; } ll ans = 0; ll norm(ll x) { x %= mod; if (x < 0) { x += mod; } return x; } const int N = 100228; ll invs[N]; void go(ll mul, int a, int b) { mul %= mod; ll cans = 0; for (int i = 2; i <= a + b; ++i) { int al = max(1, i - b); int ar = min(a, i); if (al <= ar) { ll c = ar - al + 1; cans += c * invs[i]; cans %= mod; } } ans = (ans + cans * mul) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20); int n, k; cin >> n >> k >> mod; for (int i = 1; i <= n; ++i) { invs[i] = pw(i, mod - 2); } int c = 1; for (int i = 1; i < k; ++i) { c *= 2; if (c >= n) { cout << 0 << "\n"; return 0; } } int sz = n / c; ll bc = n % c; go(bc * (bc - 1), sz + 1, sz + 1); go((c - bc) * (c - bc - 1), sz, sz); go(2ll * bc * (c - bc), sz, sz + 1); ll all = ll(n) * ll(n - 1) / 2; cout << norm((all - ans) * invs[2]) << "\n"; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 1e5 + 20; int n, k, M; int inv[MAXN], pre_inv[MAXN]; void math_pre() { inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = 1ll * (M - M / i) * inv[M % i] % M; for (int i = 1; i <= n; ++i) pre_inv[i] = (pre_inv[i - 1] + inv[i]) % M; } struct map { static const int MAXMap = 2; int tot; struct pad { int key, val; pad() {} pad(const int &KEY, const int &VAL) : key(KEY), val(VAL) {} } node[MAXMap + 1]; map() { tot = 0; } pad *find(const int &key) { pad *ret = node; while (ret - node < tot && ret->key != key) ++ret; return ret; } void insert(const pad &new_element) { node[tot++] = new_element; } pad *begin() { return &node[0]; } pad *end() { return &node[tot]; } } Map; void solve(const int &l, const int &r, const int &h) { if (l >= r || h <= 1) { int len = r - l + 1; map::pad *it = Map.find(len); if (it == Map.end()) Map.insert(map::pad(len, 1)); else ++it->val; return; } int mid = (l + r) >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } int calc(const int &len1, const int &len2) { int ret = 0; for (int i = 1; i <= len1; ++i) ret = ((ret + 1ll * inv[2] * len2 % M - (pre_inv[i + len2] - pre_inv[i + 1 - 1])) % M + M) % M; return ret; } int main() { scanf("%d%d%d", &n, &k, &M); math_pre(); solve(1, n, k); int ans = 0; for (map::pad *it = Map.begin(); it != Map.end(); ++it) { int len = it->key, cnt = it->val; ans = (ans + 1ll * cnt * len % M * (len - 1) % M * inv[4] % M) % M; } for (map::pad *it1 = Map.begin(); it1 != Map.end(); ++it1) for (map::pad *it2 = Map.begin(); it2 != Map.end(); ++it2) { if (it1 == it2) { int len = it1->key, cnt = 1ll * (0 + (it1->val - 1)) * it1->val / 2 % M; ans = (ans + 1ll * cnt * calc(len, len) % M) % M; } else if (it1->key < it2->key) { int len1 = it1->key, len2 = it2->key, cnt = 1ll * it1->val * it2->val % M; ans = (ans + 1ll * cnt * calc(len1, len2) % M) % M; } } printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int n, k, q, t1, t2, cnt1, cnt2, inv4, inv2, inv[100001]; long long ans; inline int mul(const int &a, const int &b) { return 1ll * a * b % q; } inline int qsm(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = mul(ans, a); a = mul(a, a); b >>= 1; } return ans; } void merge(int l, int r, int k) { if (l <= r) { if (k <= 1) { if (t1 == r - l + 1) ++cnt1; else if (t2 == r - l + 1) ++cnt2; else if (!t1) t1 = r - l + 1, cnt1 = 1; else t2 = r - l + 1, cnt2 = 1; (ans += mul(mul(r - l + 1, r - l), inv4)) %= q; return; } merge(l, (l + r) >> 1, k - 1); merge(((l + r) >> 1) + 1, r, k - 1); } } int getans(int n, int m) { int ans = 0; (ans += mul(inv2, mul(n, m))) %= q; for (int i = 1; i <= n; i++) (ans += (inv[i] - inv[i + m] + q)) %= q; return ans; } signed main() { scanf("%d%d%d", &n, &k, &q); inv4 = qsm(4, q - 2); inv2 = (q + 1) >> 1; inv[0] = inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = mul(q - q / i, inv[q % i]); for (int i = 1; i <= n; i++) (inv[i] += inv[i - 1]) %= q; merge(1, n, k); (ans += mul(getans(t1, t1), mul(mul(cnt1, cnt1 - 1), inv2))) %= q; (ans += mul(getans(t2, t2), mul(mul(cnt2, cnt2 - 1), inv2))) %= q; (ans += mul(getans(t1, t2), mul(cnt1, cnt2))) %= q; printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int q, I[200010]; long long inv(int a, int p) { return a == 1 ? 1 : (1 + p * (a - inv(p % a, a))) / a % p; } int f(int a, int b) { int s = 0; for (int i = 0; i < a + b - 1; i++) s = (s + ((i + 1 < a ? i + 1 : a) - (i - b + 1 > 0 ? i - b + 1 : 0)) * 1ll * i % q * I[i + 2]) % q; return (q + 1ll) / 2 * s % q; } int main() { int n, k; scanf("%d%d%d", &n, &k, &q); --k; int a = n >> k, b = a + 1, x, y; if (a) x = (b << k) - n, y = (1 << k) - x; else x = 0, y = n; for (int i = *I = 1; i <= a + b; i++) I[i] = I[i - 1] * 1ll * i % q; long long P = inv(I[a + b], q); for (int i = a + b; i; i--) I[i] = I[i - 1] * P % q, P = P * i % q; int s = (x * (x - 1ll) / 2 % q * f(a, a) + 1ll * x * y % q * f(a, b) + y * (y - 1ll) / 2 % q * f(b, b) + a * (a - 1ll) / 2 % q * (q + 1 >> 1) % q * x + b * (b - 1ll) / 2 % q * (q + 1 >> 1) % q * y) % q; printf("%d\n", s); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 100007; long long MOD; inline long long FST(long long base, int times) { long long ret = 1; while (times) { if (times & 1) ret = ret * base % MOD; times >>= 1; base = base * base % MOD; } return ret; } int seg[MAXN], tot_seg; long long inv[MAXN], invS[MAXN]; void getSeg(const int &l, const int &r, const int &h) { if (h <= 1 || l == r) { seg[++tot_seg] = r - l + 1; return; } const int &mid = (l + r) >> 1; getSeg(l, mid, h - 1); getSeg(mid + 1, r, h - 1); return; } inline long long calc(int max_i, int max_j) { long long ret = max_i * max_j % MOD; for (int i = 1; i <= max_i; ++i) ret = (ret - (invS[i + max_j] - invS[i]) * 2) % MOD; return ret; } long long buc[2][2]; int main() { int n, k; scanf("%d%d%I64d", &n, &k, &MOD); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = inv[i - 1] * i % MOD; inv[n] = FST(inv[n], MOD - 2); for (int i = n; i > 1; --i) { long long tmp_inv = inv[i]; inv[i] = inv[i - 1] * inv[i] % MOD; inv[i - 1] = tmp_inv * i % MOD; } for (int i = 1; i <= n; ++i) invS[i] = (invS[i - 1] + inv[i]) % MOD; inv[2] = FST(2, MOD - 2); long long ans = 0; getSeg(1, n, k); for (int i = 1; i <= tot_seg; ++i) { if (!buc[0][0]) buc[0][0] = seg[i]; if (seg[i] == buc[0][0]) ++buc[0][1]; else { if (!buc[1][0]) buc[1][0] = seg[i]; ++buc[1][1]; } ans = (ans + seg[i] * (seg[i] - 1) / 2 % MOD) % MOD; } for (int i = 0; i < 2; ++i) if (buc[i][1] >= 2) ans = (ans + calc(buc[i][0], buc[i][0]) * (buc[i][1] * (buc[i][1] - 1) / 2 % MOD) % MOD) % MOD; if (buc[0][0] && buc[0][1]) ans = (ans + calc(buc[0][0], buc[1][0]) * (buc[1][1] * buc[0][1] % MOD) % MOD) % MOD; ans = ans * inv[2] % MOD; printf("%I64d\n", (ans + MOD) % MOD); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long n, k, gs, ans, mod, len[N], jc[N], inv[N], sum[N], ycl[2005][2005]; inline long long read() { long long res = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { res = res * 10 + c - 48; c = getchar(); } return res * f; } void build(long long l, long long r, long long h) { if (h <= 1 || l == r) { len[++gs] = r - l + 1; return; } long long mid = (l + r) / 2; build(l, mid, h - 1); build(mid + 1, r, h - 1); } long long C(long long n, long long m) { if (n < 0 || m < 0 || n < m) return 0; return jc[n] * inv[m] % mod * inv[n - m] % mod; } signed main() { n = read(); k = read(); mod = read(); jc[0] = jc[1] = inv[0] = inv[1] = 1; for (long long i = 2; i <= n; i++) jc[i] = jc[i - 1] * i % mod; for (long long i = 2; i <= n; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; build(1, n, k); for (long long i = 1; i <= gs; i++) { ans = (ans + len[i] * (len[i] - 1) % mod * inv[4] % mod) % mod; } sort(len + 1, len + gs + 1); long long x = 0; for (long long i = 1; i <= gs; i++) if (len[i] == len[i - 1]) sum[x]++; else { len[++x] = len[i]; sum[x] = 1; } gs = x; for (long long i = 1; i <= gs; i++) for (long long j = 1; j <= i; j++) { long long res = 0; res = len[i] * len[j] % mod * inv[2] % mod; for (long long a = 1; a <= len[i] + len[j]; a++) { long long l = max(1ll, a - len[j]), r = min(a - 1, len[i]); res = (res - inv[a] * (r - l + 1) % mod + mod) % mod; } if (i == j) ans = (ans + res * sum[i] % mod * (sum[i] - 1 + mod) % mod * inv[2] % mod) % mod; else ans = (ans + res * sum[i] % mod * sum[j] % mod) % mod; } cout << ans % mod; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/priority_queue.hpp> #define priority_queue __gnu_pbds::priority_queue typedef long long LL; typedef long double LD; typedef unsigned int ui; typedef unsigned long long ul; #define ri register int #define pb push_back #define fi first #define se second #define mid ((l + r) >> 1) #define ls (i << 1) #define rs (i << 1 | 1) #define enum(i, j, k) for(int i = j; i <= k; i ++) #define FO(x) { freopen(#x".in", "r", stdin); freopen(#x".out", "w", stdout); } using namespace std; typedef pair<int, int> pii; typedef pair<LL, LL> pll; typedef pair<int, LL> pil; typedef pair<LL, int> pli; typedef vector<int> vi; #define ae(x) for(node *p = h[x]; p; p = p->nxt) #define G \ struct node { int v; node *nxt; } e[maxm], *h[maxn]; int z;\ void adde(int u, int v) { e[++z].v = v; e[z].nxt = h[u]; h[u] = &e[z]; }\ void addrev(int u, int v) { adde(u, v); adde(v, u); } #define W \ struct node { int v, w; node *nxt; } e[maxm], *h[maxn]; int z;\ void adde(int u, int v, int w) { e[++z].v = v; e[z].w = w; e[z].nxt = h[u]; h[u] = &e[z]; }\ void addrev(int u, int v, int w) { adde(u, v, w); adde(v, u, w); } inline int _() // ret >= 0 { int ret = 0; char ch = getchar(); while(ch < '0' || ch > '9') ch = getchar(); do { ret = ret * 10 + ch - '0'; ch = getchar(); } while('0' <= ch && ch <= '9'); return ret; } inline void _(int x) // x >= 0 { if(x < 10) { putchar(x + '0'); return ; } _(x / 10); putchar(x % 10 + '0'); } void What_are_you_doing_at_the_end_of_the_world_Are_you_busy_Can_you_save_us() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(16); } /* Default Code Ends Here. */ #define maxn 100100 LL mod; int n, k; LL fpow(LL x, LL a) { LL ret = 1; while(a) { if(a & 1) ret = ret * x % mod; a >>= 1; x = x * x % mod; } return ret; } LL ni(LL x) { return fpow(x, mod - 2); } int d1, d2; int t1, t2; void dfs(int x, int h) { if(h == 1) { if(x == d1) t1 ++; else if(x == d2) t2 ++; else exit(1); return ; } dfs(x / 2, h - 1); dfs(x - x / 2, h - 1); } LL sum[2 * maxn]; LL f(LL x, LL y) { LL ret = 0; enum(i, 1, x) { ret = (ret - (sum[i + y] - sum[i]) + mod) % mod; } ret = (ret + ni(2) * x % mod * y) % mod; return ret; } int main() { What_are_you_doing_at_the_end_of_the_world_Are_you_busy_Can_you_save_us(); cin >> n >> k >> mod; // cout << ni(2) << endl; // cout << 3 * ni(2) % mod << endl;; if(k - 1 > log2(n)) return cout << 0 << endl, 0; enum(i, 1, 200050) sum[i] = (sum[i - 1] + ni(i)) % mod; int x = pow(2, k - 1); d1 = n / x; d2 = d1 + 1; dfs(n, k); cerr << d1 << ' ' << t1 << ' ' << d2 << ' ' << t2 << endl; LL Ans = (t1 * (t1 - 1) / 2 % mod * f(d1, d1) + t2 * (t2 - 1) / 2 % mod * f(d2, d2) + t1 * t2 % mod * f(d1, d2)) % mod; Ans += (d1 * (d1 - 1) % mod * t1 + d2 * (d2 - 1) % mod * t2) % mod * ni(4); Ans %= mod; cout << Ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int MOD = 998244353; int n, k; vector<int> pos; int cnt[N]; void solve(int l, int r, int k) { if (k <= 1 || l == r) { if (cnt[r - l + 1] == 0) pos.push_back(r - l + 1); cnt[r - l + 1]++; return; } int mid = (l + r) / 2; solve(l, mid, k - 1); solve(mid + 1, r, k - 1); return; } long long inv[N], sinv[N]; void init(int n = 100000) { inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD; sinv[0] = 0; for (int i = 1; i <= n; i++) sinv[i] = (sinv[i - 1] + inv[i]) % MOD; return; } long long calc(int x, int y) { long long ans = 0; for (int i = 1; i <= x; i++) { long long res = inv[2] * y % MOD; res = (res - (sinv[i + y] - sinv[i] + MOD) % MOD + MOD) % MOD; ans = (ans + res) % MOD; } return ans; } int main() { scanf("%d%d", &n, &k); init(); solve(1, n, k); int len = pos.size(); long long ans = 0; for (int i = 0; i < len; i++) ans = (ans + 1LL * pos[i] * (pos[i] - 1) % MOD * inv[4] % MOD * cnt[pos[i]] % MOD) % MOD; for (int i = 0; i < len; i++) for (int j = i + 1; j < len; j++) ans = (ans + calc(pos[i], pos[j]) * cnt[pos[i]] % MOD * cnt[pos[j]] % MOD) % MOD; for (int i = 0; i < len; i++) ans = (ans + calc(pos[i], pos[i]) * cnt[pos[i]] % MOD * (cnt[pos[i]] - 1) % MOD * inv[2] % MOD) % MOD; printf("%lld", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 100010; int mod = 998244353; void reduce(int& x) { x += x >> 31 & mod; } int mul(int a, int b) { return (long long)a * b % mod; } int pow(int a, int b, int res = 1) { for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; } void mma(int& x, int y, int z) { x = (x + (long long)y * z) % mod; } int remod(long long x) { x %= mod; return x + (x >> 63 & mod); } std::map<int, int> hav; void solve(int l, int r, int dep) { if (l == r || dep <= 1) return (void)(++hav[r - l + 1]); int mid = l + r >> 1; solve(l, mid, dep - 1); solve(mid + 1, r, dep - 1); } inline int calc1(int x) { return (long long)pow(4, mod - 2, x * (x - 1ll) % mod); } inline int coef(std::pair<int, int> x, std::pair<int, int> y) { int t = x.second; if (x.first == y.first) return (long long)t * (t - 1) / 2 % mod; return mul(t, y.second); } int n, K; const int MAXS = MAXN << 1; int inv[MAXS]; inline int calc2(int x, int y) { int res = pow(2, mod - 2, mul(x, y)); for (int i = 1; i <= x; ++i) reduce(res -= inv[i + y]), reduce(res += inv[i] - mod); return res; } int main() { inv[0] = inv[1] = 1; for (int i = 2; i != MAXS; ++i) inv[i] = mul(inv[mod % i], mod - mod / i); for (int i = 1; i != MAXS; ++i) reduce(inv[i] += inv[i - 1] - mod); std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> K >> mod; solve(1, n, K); int ans = 0; for (auto t : hav) mma(ans, t.second, calc1(t.first)); for (auto x : hav) for (auto y : hav) if (x.first <= y.first) mma(ans, coef(x, y), calc2(x.first, y.first)); std::cout << ans << std::endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 100007; long long MOD; inline long long FST(long long base, int times) { long long ret = 1; while (times) { if (times & 1) ret = ret * base % MOD; times >>= 1; base = base * base % MOD; } return ret; } long long seg[MAXN], tot_seg; long long inv[MAXN], invS[MAXN]; void getSeg(const int &l, const int &r, const int &h) { if (h <= 1 || l == r) { seg[++tot_seg] = r - l + 1; return; } const int &mid = (l + r) >> 1; getSeg(l, mid, h - 1); getSeg(mid + 1, r, h - 1); return; } inline long long calc(int max_i, int max_j) { long long ret = max_i * max_j % MOD; for (int i = 1; i <= max_i; ++i) ret = (ret - (invS[i + max_j] - invS[i]) * 2) % MOD; return ret; } long long buc[2][2]; int main() { int n, k; scanf("%d%d%I64d", &n, &k, &MOD); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = inv[i - 1] * i % MOD; inv[n] = FST(inv[n], MOD - 2); for (int i = n; i > 1; --i) { long long tmp_inv = inv[i]; inv[i] = inv[i - 1] * inv[i] % MOD; inv[i - 1] = tmp_inv * i % MOD; } for (int i = 1; i <= n; ++i) invS[i] = (invS[i - 1] + inv[i]) % MOD; inv[2] = FST(2, MOD - 2); long long ans = 0; getSeg(1, n, k); for (int i = 1; i <= tot_seg; ++i) { if (!buc[0][0]) buc[0][0] = seg[i]; if (seg[i] == buc[0][0]) ++buc[0][1]; else { if (!buc[1][0]) buc[1][0] = seg[i]; ++buc[1][1]; } ans = (ans + seg[i] * (seg[i] - 1) / 2 % MOD) % MOD; } for (int i = 0; i < 2; ++i) if (buc[i][1] >= 2) ans = (ans + calc(buc[i][0], buc[i][0]) * (buc[i][1] * (buc[i][1] - 1) / 2 % MOD) % MOD) % MOD; if (buc[0][0] && buc[0][1]) ans = (ans + calc(buc[0][0], buc[1][0]) * (buc[1][1] * buc[0][1] % MOD) % MOD) % MOD; ans = ans * inv[2] % MOD; printf("%I64d\n", (ans + MOD) % MOD); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int n, k, mod, inv2; int ans = 0; inline void add(int& a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } inline int ksm(int a, int b) { int ans = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) ans = (long long)ans * a % mod; return ans; } inline void build(int l, int r, int h) { if (l < r) { if (h <= 1) { int len = r - l + 1; mp[len]++; add(ans, (long long)len * (len - 1) / 2ll % mod * inv2 % mod); } else { int mid = (l + r) >> 1; build(l, mid, h - 1); build(mid + 1, r, h - 1); } } else mp[1]++; } signed main() { cin >> n >> k >> mod; inv2 = ksm(2, mod - 2); build(1, n, k); for (auto i : mp) cout << i.first << ' ' << i.second << endl; for (auto i : mp) { for (auto j : mp) { int gs = (long long)i.second * (j.second - (i.first == j.first)) % mod; for (int l = 2; l <= i.second + j.second; ++l) { int minn = max(1, l - j.first); int maxx = min(i.first, l - 1); add(ans, (long long)gs * (maxx - minn + 1) % mod * (long long)(inv2 - ksm(l, mod - 2)) % mod * (long long)inv2 % mod); } } } cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MN = 200000 + 5; using namespace std; template <typename T> inline T& IN(T& in) { in = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) in = in * 10 + c - '0', c = getchar(); return in *= f; } int n, k, P; long long ans; map<int, int> len; long long inv[MN], s[MN]; long long qp(long long a, long long b) { long long c = 1; for (; b; b >>= 1, a = a * a % P) if (b & 1) c = c * a % P; return c; } void build(int l, int r, int k) { if (k == 1 || l == r) return len[r - l + 1]++, void(); int mid = l + r >> 1; build(l, mid, k - 1), build(mid + 1, r, k - 1); } long long calc(int x, int y) { long long res = x * y % P * inv[2] % P; for (int i = 1; i <= x; ++i) res = (res - (s[i + y] - s[i]) % P + P) % P; return res; } void input() { IN(n), IN(k), IN(P); int N = 200000; inv[1] = 1, s[1] = 1; for (int i = 2; i <= N; ++i) inv[i] = (P - P / i) * inv[P % i] % P, s[i] = (s[i - 1] + inv[i]) % P; build(1, n, k); for (auto it : len) { long long x = it.first, y = it.second; ans = (ans + x * (x - 1) % P * inv[4] % P * y % P + y * (y - 1) % P * inv[2] % P * calc(x, x) % P) % P; } for (auto x : len) for (auto y : len) if (x.first < y.first) ans = (ans + calc(x.first, y.first) * x.second % P * y.second % P) % P; printf("%lld\n", ans); } int main() { input(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int MOD; int inv[200005]; void gen_inv(int maxa) { inv[1] = 1; for (int i = 2; i <= maxa; i++) inv[i] = MOD - 1LL * inv[MOD % i] * (MOD / i) % MOD; } int getans(int u, int v) { int ans = 0; for (int i = 2; i <= u + v; i++) ans = (ans + 1LL * (min(u, i - 1) - max(1, i - v) + 1) * inv[i]) % MOD; return ans; } int main() { int n, k; scanf("%d%d%d", &n, &k, &MOD); k--; gen_inv(2 * n); int ans = 1LL * n * (n - 1) / 2 % MOD * inv[2] % MOD; if (n >> k) { int c0 = n >> k, c1 = c0 + 1; int tot1 = n & (1 << k) - 1, tot0 = (1 << k) - tot1; ans = (ans - 1LL * tot0 * (tot0 - 1) / 2 % MOD * getans(c0, c0) % MOD + MOD) % MOD; ans = (ans - 1LL * tot1 * (tot1 - 1) / 2 % MOD * getans(c1, c1) % MOD + MOD) % MOD; ans = (ans - 1LL * tot0 * tot1 % MOD * getans(c0, c1) % MOD + MOD) % MOD; } else ans = 0; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using std::cerr; using std::endl; const int N = 1e5 + 10; int n, K, P, inv[N], sum[N], ans; std::map<int, int> map; void divide(int l, int r, int dep) { if (l == r || dep == K) { return ++map[r - l + 1], void(); } int mid = (l + r) >> 1; divide(l, mid, dep + 1); divide(mid + 1, r, dep + 1); } inline long long calc(int x, int y) { int ret = 1ll * x * y % P * inv[2] % P; for (int i = 1; i <= x; ++i) ret = (ret - sum[i + y] + sum[i]) % P; ret = (ret % P + P) % P; return ret; } int main() { scanf("%d %d %d", &n, &K, &P); divide(1, n, 1); inv[1] = sum[1] = 1; for (int i = 2, lim = std::max(4, n); i <= lim; ++i) { inv[i] = P - 1ll * P / i * inv[P % i] % P; sum[i] = (sum[i - 1] + inv[i]) % P; } for (auto m : map) { ans = (ans + 1ll * m.first * (m.first - 1) % P * inv[4] % P * m.second) % P; ans = (ans + calc(m.first, m.first) * m.second % P * (m.second - 1)) % P; } for (auto m1 : map) for (auto m2 : map) if (m1.first < m2.first) ans = (ans + calc(m1.first, m2.first) * m1.second % P * m2.second) % P; std::cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; long long MOD, f[N]; long long qpow(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2) z = z * x % MOD; x = x * x % MOD; y /= 2; } return z; } map<int, int> ma; pair<int, int> a[10]; void dfs(int x, int k) { if (k == 1 || x == 1) { ma[x]++; return; } dfs((x + 1) / 2, k - 1); dfs(x / 2, k - 1); } int main() { int n, k; scanf("%d%d%lld", &n, &k, &MOD); dfs(n, k); for (int i = 1; i <= n * 2; i++) f[i] = (f[i - 1] - qpow(i, MOD - 2) + qpow(2, MOD - 2) + MOD) % MOD; int cnt = 0; for (auto p : ma) { a[++cnt] = p; } long long ans = 0; for (int i = 1; i <= cnt; i++) { ans += 1LL * a[i].second * a[i].first % MOD * (a[i].first - 1) % MOD * qpow(4, MOD - 2) % MOD; long long sum = 0; for (int j = 1; j <= a[i].first; j++) { sum = (sum + f[j + a[i].first] - f[j]) % MOD; } ans += sum * a[i].second % MOD * (a[i].second - 1) % MOD * qpow(2, MOD - 2) % MOD; } if (cnt == 2) { long long sum = 0; for (int j = 1; j <= a[1].first; j++) { sum = (sum + f[j + a[2].first] - f[j]) % MOD; } ans += sum * a[1].second % MOD * a[2].second % MOD; } printf("%lld", ans % MOD); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
IN-CORRECT
cpp
//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") //~ #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (1e6)+7; const int maxk = 20; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; // ***************************** CODE ***************************** // LL mod; LL pot(LL a, LL w) { if(w == 0) return 1; LL b = pot(a, w / 2); b = (b * b) % mod; if(w & 1) b = (b * a) % mod; return b; } LL odw(LL a) { return pot(a, mod - 2); } LL po2(LL a) { return ((a * (a - 1)) / 2LL); } LL dp[maxn]; LL licz(LL a, LL b) { if(a > b) swap(a, b); LL cnt = 0LL; for(int i = 0 ;i <= a + b;i++) dp[i] = 0; for(int i = 1;i <= b;i++) { dp[i + 1]++; dp[i + a + 1]--; } for(int i = 2;i <= a + b;i++) { dp[i] += dp[i - 1]; cnt += dp[i] * (odw(2) - odw(i)); cnt %= mod; } return cnt; } int main() { _upgrade int n, k; cin>>n>>k>>mod; k--; if(k > 20 || (1<<k) >= n) { cout<<"0"; return 0; } int dlu = n / (1<<k); int jed = (1<<k); int dwa = n - dlu * (1<<k); jed -= dwa; // dbg(jed, dwa, dlu); LL res = 0LL; res += ((((dlu * (dlu - 1) ) % mod * (odw(4))) % mod) * jed) % mod; res += ((((dlu * (dlu + 1) ) % mod * (odw(4))) % mod) * dwa) % mod; res += po2(jed) * licz(dlu, dlu); res += po2(dwa) * licz(dlu + 1, dlu + 1); res += ((jed * dwa) % mod) * licz(dlu, dlu + 1); res %= mod; res += mod; res %= mod; cout<<(res)% mod; return 0; }