static void DFS3(int now, int parent, List <int>[] graph, long[] res, int[] childs, long[] pats, CaseCalculator calculator, long mask) { long pat = 1; if (parent == -1) { pat = pats[now]; } else { long parentVal = (res[parent] * calculator.Inverse( calculator.Permutation(childs.Length - 1, childs[now] + 1))) % mask; parentVal = (parentVal * calculator.Permutation( childs[now] + 1, childs[now] + 1)) % mask; parentVal = (parentVal * calculator.Inverse(pats[now])) % mask; parentVal = (parentVal * calculator.Combination( childs.Length - 1, childs[now])) % mask; pat = (parentVal * pats[now]) % mask; } res[now] = pat; for (int i = 0; i < graph[now].Count; i++) { int to = graph[now][i]; if (to == parent) { continue; } DFS3(to, now, graph, res, childs, pats, calculator, mask); } }
static void Method(string[] args) { int n = ReadInt(); List <int>[] graph = new List <int> [n]; for (int i = 0; i < n; i++) { graph[i] = new List <int>(); } for (int i = 0; i < n - 1; i++) { int[] ab = ReadInts(); int a = ab[0] - 1; int b = ab[1] - 1; graph[a].Add(b); graph[b].Add(a); } int[] childs = new int[n]; DFS1(0, -1, graph, childs); long mask = 1000000000 + 7; CaseCalculator calculator = new CaseCalculator(mask, n); long[] pats = new long[n]; DFS2(0, -1, graph, calculator, pats, childs, mask); long[] res = new long[n]; DFS3(0, -1, graph, res, childs, pats, calculator, mask); for (int i = 0; i < n; i++) { WriteLine(res[i]); } }
static void Method(string[] args) { int k = ReadInt(); string s = Read(); long mask = 1000000000 + 7; long[] divs = new long[1000000 + 100]; long[] divs2 = new long[1000000 + 100]; divs[0] = 1; divs2[0] = 1; for (int i = 1; i < divs.Length; i++) { divs[i] = divs[i - 1] * 25; divs[i] %= mask; divs2[i] = divs2[i - 1] * 26; divs2[i] %= mask; } var calculator = new CaseCalculator(mask, s.Length + k + 100); long res = 0; for (int i = 0; i <= k; i++) { long val = calculator.Combination(s.Length - 1 + i, i) * divs2[k - i]; val %= mask; val *= divs[i]; val %= mask; res += val; res %= mask; } WriteLine(res); }
static void Method(string[] args) { int q = ReadInt(); long mask = 1000003; var calculator = new CaseCalculator(mask, mask + 100); for (int i = 0; i < q; i++) { long[] xdn = ReadLongs(); long x = xdn[0]; long d = xdn[1]; long n = xdn[2]; if (d == 0) { WriteLine(calculator.Pow(x, n)); } else { long baseVal = calculator.Multi(x, calculator.Inverse(d)); if (baseVal + n - 1 >= mask) { WriteLine(0); } else { WriteLine(calculator.Multi( calculator.Permutation(baseVal + n - 1, n), calculator.Pow(d, n))); } } } }
static void Method(string[] args) { int[] nm = ReadInts(); int n = nm[0]; int m = nm[1]; long mask = 1000000000 + 7; List <int[]> factors = PrimeFactors(Abs(n)); var calculator = new CaseCalculator(mask, m + 100); long res = 1; for (int i = 0; i < factors.Count; i++) { res *= calculator.Combination(m + factors[i][1] - 1, m - 1); res %= mask; } long minuses = 0; for (int i = n < 0 ? 1 : 0; i <= m; i += 2) { minuses += calculator.Combination(m, i); minuses %= mask; } WriteLine(res * minuses % mask); }
static void Method(string[] args) { long n = ReadLong(); long[] xs = ReadLongs(); long mask = 1000000000 + 7; CaseCalculator calculator = new CaseCalculator(mask, n); long[] divs = new long[n]; for (int i = 0; i < n; i++) { divs[i] = calculator.Reverse(i + 1); } long[] sums = new long[n]; sums[0] = divs[0]; for (int i = 1; i < n; i++) { sums[i] = (sums[i - 1] + divs[i]) % mask; } long res = 0; for (int i = 0; i < n - 1; i++) { long dist = xs[i + 1] - xs[i]; res += dist * sums[i]; res %= mask; } for (int i = 1; i < n; i++) { res = (res * i) % mask; } WriteLine(res); }
static void Method(string[] args) { int[] nm = ReadInts(); long n = nm[0]; long m = nm[1]; long[] array = ReadLongs(); long sum = array.Sum(); if (m < sum) { WriteLine(0); return; } long mask = 1000000000 + 7; var calculator = new CaseCalculator(mask, sum + n); long res = 1; for (long i = m + n; i > m + n - (sum + n); i--) { res *= i; res %= mask; } res *= calculator.Inverse(calculator.Permutation(sum + n, sum + n)); res %= mask; WriteLine(res); }
static void Method(string[] args) { int n = ReadInt(); long[] array = ReadLongs(); Array.Sort(array); long mask = 998244353; long res = 0; var calculator = new CaseCalculator(mask, 2 * n); for (int i = 0; i < 2 * n; i++) { long tmp = calculator.Combination(2 * n - 1, n); tmp *= array[i]; tmp %= mask; if (i < n) { res += mask - tmp; } else { res += tmp; } res %= mask; } WriteLine(res * 2 % mask); }
static void Method(string[] args) { long[] kn = ReadLongs(); long k = kn[0]; long n = kn[1]; long mask = 998244353; CaseCalculator calculator = new CaseCalculator(mask, n + k - 1); long allPat = calculator.Combination(n + k - 1, n); long[] res = new long[2 * k - 1]; long prevPats = 0; for (long val = 2; val <= k + 1; val++) { long pats = Min(val - 2, 2 * k - val) / 2 + 1; if (pats == prevPats) { res[val - 2] = res[val - 3]; res[2 * k - val] = res[val - 3]; continue; } long seqCnt = Min(pats, n / 2); long resTmp = allPat; for (long i = 1; i <= seqCnt; i++) { long tmp = calculator.Combination(pats, i); tmp = calculator.Multi(tmp, calculator.Combination(k - 1 + n - i * 2, k - 1)); if (i % 2 == 1) { resTmp += mask - tmp; } else { resTmp += tmp; } resTmp %= mask; } res[val - 2] = resTmp; res[2 * k - val] = resTmp; prevPats = pats; } for (int i = 0; i < 2 * k - 1; i++) { WriteLine(res[i]); } }
static void Method(string[] args) { long[] nab = ReadLongs(); long n = nab[0]; long a = nab[1]; long b = nab[2]; long mask = 1000000000 + 7; long[] pows = new long[40]; pows[0] = 2; for (int i = 1; i < pows.Length; i++) { pows[i] = pows[i - 1] * pows[i - 1]; pows[i] %= mask; } long all = 1; long tmp = 1; for (int i = 0; i < 40; i++) { if ((n / tmp) % 2 == 1) { all *= pows[i]; all %= mask; } tmp *= 2; } all = (all - 1 + mask) % mask; CaseCalculator calculator = new CaseCalculator(mask, Max(a, b)); long nA = 1; long nB = 1; for (long i = n; i >= n - a + 1; i--) { nA *= i; nA %= mask; } for (long i = n; i >= n - b + 1; i--) { nB *= i; nB %= mask; } all = (all - (nA * calculator.Inverse(calculator.Permutation(a, a))) % mask + mask) % mask; all = (all - (nB * calculator.Inverse(calculator.Permutation(b, b))) % mask + mask) % mask; WriteLine(all); }
static void Method(string[] args) { int p = ReadInt(); int[] array = ReadInts(); CaseCalculator caseCalculator = new CaseCalculator(p, p - 1); long[] combinations = new long[p]; for (int i = 0; i < p; i++) { combinations[i] = caseCalculator.Combination(p - 1, i); } long[] res = new long[p]; for (int i = 0; i < p; i++) { if (array[i] == 0) { continue; } res[p - 1]++; long tmp = 1; bool minus = false; for (int j = 0; j < p; j++) { long delta = (combinations[j] * tmp) % p; if (minus) { delta *= -1; } res[j] += p - delta; res[j] %= p; tmp *= i; tmp %= p; minus = !minus; } } for (int i = p - 1; i >= 0; i--) { Write(res[i]); if (i > 0) { Write(" "); } } }
static void Method(string[] args) { int s = ReadInt(); long res = 0; long mask = 1000000000 + 7; var calculator = new CaseCalculator(mask, 5000); for (int i = 1; i * 3 <= s; i++) { long remain = s - i * 3; res += calculator.Combination(remain + i - 1, remain); res %= mask; } WriteLine(res); }
static void Method(string[] args) { int n = ReadInt(); long mask = 1000000000 + 7; var calculator = new CaseCalculator(mask, n); long res = calculator.Permutation(n, n); long minus = 0; for (int i = 1; i <= n; i++) { minus += calculator.Combination(n - 1, i - 1); minus %= mask; } res += mask - minus; res %= mask; WriteLine(res); }
static void Method(string[] args) { int t = ReadInt(); long mask = 1000000000 + 7; CaseCalculator calculator = new CaseCalculator(mask, 1000000); for (int i = 0; i < t; i++) { int[] nmk = ReadInts(); int n = nmk[0]; int m = nmk[1]; int k = nmk[2]; int[] array = ReadInts(); Array.Sort(array); long res = 0; for (int j = 0; j + m - 1 < n; j++) { if (array[j + m - 1] - array[j] > k) { continue; } int bottom = j + m - 1; int top = n; while (bottom + 1 < top) { int mid = (bottom + top) / 2; if (array[mid] - array[j] > k) { top = mid; } else { bottom = mid; } } long len = bottom - j; res += calculator.Combination(len, m - 1); res %= mask; } WriteLine(res); } }
static void Method(string[] args) { long[] nk = ReadLongs(); long n = nk[0]; long k = nk[1]; long mask = 1000000000 + 7; CaseCalculator calculator = new CaseCalculator(mask, n * 2); long res = 1; for (long i = 1; i <= Min(n, k); i++) { long tmp = calculator.Combination(n, i); tmp = calculator.Multi(tmp, calculator.Combination(n - 1, i)); res += tmp; res %= mask; } WriteLine(res); }
static void Method(string[] args) { int[] nxp = ReadInts(); int n = nxp[0]; int x = nxp[1]; int p = nxp[2]; int bottom = 0; int top = n; int upperCnt = 0; int lowerCnt = 0; while (bottom < top) { int mid = (bottom + top) / 2; if (mid <= p) { bottom = mid + 1; if (mid < p) { lowerCnt++; } } else { top = mid; upperCnt++; } } long mask = 1000000000 + 7; var calculator = new CaseCalculator(mask, n); int lowers = x - 1; int uppers = n - x; long res = calculator.Permutation(lowers, lowerCnt); res *= calculator.Permutation(uppers, upperCnt); res %= mask; res *= calculator.Permutation(n - 1 - lowerCnt - upperCnt, n - 1 - lowerCnt - upperCnt); res %= mask; WriteLine(res); }
static void Method(string[] args) { int[] rcs = ReadInts(); long r1 = rcs[0]; long c1 = rcs[1]; long r2 = rcs[2]; long c2 = rcs[3]; long mask = 1000000000 + 7; CaseCalculator calculator = new CaseCalculator(mask, r2 + c2 + 5); long res = 0; for (long i = r1 + 1; i <= r2 + 1; i++) { res += calculator.Combination(i + c2, i); res += mask - calculator.Combination(i + c1 - 1, i); res %= mask; } WriteLine(res); }
static void Method(string[] args) { long[] xy = ReadLongs(); long x = xy[0]; long y = xy[1]; if (2 * y - x < 0 || (2 * y - x) % 3 != 0 || 2 * x - y < 0 || (2 * x - y) % 3 != 0) { WriteLine(0); return; } long a = (2 * y - x) / 3; long b = (2 * x - y) / 3; long mask = 1000000000 + 7; CaseCalculator calculator = new CaseCalculator(mask, a + b); WriteLine(calculator.Combination(a + b, a)); }
static void Method(string[] args) { int[] nk = ReadInts(); int n = nk[0]; int k = nk[1]; int[][] lrs = new int[n][]; for (int i = 0; i < n; i++) { lrs[i] = ReadInts(); } Array.Sort(lrs, (a, b) => a[0] - b[0]); long mask = 998244353; var calculator = new CaseCalculator(mask, n); var que = new PriorityQueue <bool>(); int now = 0; long res = 0; for (int i = 0; i < n; i++) { int l = lrs[i][0]; while (que.Count > 0 && que.Top().Key < l) { que.Dequeue(); now--; } now++; que.Enqueue(lrs[i][1], true); if (now == k) { res++; } if (now > k) { res += calculator.Combination(now - 1, k - 1); } res %= mask; } WriteLine(res); }
static void Method(string[] args) { long[] nk = ReadLongs(); long n = nk[0]; long k = nk[1]; long[] array = ReadLongs(); Array.Sort(array); long mask = 1000000000 + 7; for (int i = 0; i < n; i++) { array[i] = mask * 10 + array[i]; array[i] %= mask; } if (k == 1) { WriteLine(0); return; } CaseCalculator calculator = new CaseCalculator(mask, n); long res = 0; for (long i = k - 1; i < n; i++) { long tmp = array[i] * calculator.Combination(i, k - 1); tmp %= mask; res += tmp; res %= mask; } for (long i = 0; i < n - k + 1; i++) { long tmp = array[i] * calculator.Combination(n - i - 1, k - 1); tmp %= mask; res += mask - tmp; res %= mask; } WriteLine(res); }
static void Method(string[] args) { long[] nmk = ReadLongs(); long n = nmk[0]; long m = nmk[1]; long k = nmk[2]; if (n == 1) { WriteLine(m); return; } long mask = 998244353; CaseCalculator calculator = new CaseCalculator(mask, n); long res = 0; long[] pows = new long[n]; pows[0] = 1; for (int i = 1; i < n; i++) { pows[i] = pows[i - 1] * (m - 1); pows[i] %= mask; } for (long i = n - 1; n - 1 - i <= k; i--) { long tmp = m * pows[i]; tmp %= mask; tmp *= calculator.Combination(n - 1, i); tmp %= mask; res += tmp; res %= mask; } WriteLine(res); }
static void DFS2(int now, int parent, List <int>[] graph, CaseCalculator calculator, long[] pats, int[] childs, long mask) { long val = 1; int childCnt = childs[now]; for (int i = 0; i < graph[now].Count; i++) { int to = graph[now][i]; if (to == parent) { continue; } long tmp = calculator.Combination(childCnt, childs[to] + 1); DFS2(to, now, graph, calculator, pats, childs, mask); tmp = (tmp * pats[to]) % mask; childCnt -= childs[to] + 1; val *= tmp; val %= mask; } pats[now] = val; }
static void Method(string[] args) { long[] nk = ReadLongs(); long n = nk[0]; long k = nk[1]; string t = Read(); long equals = 0; long notEquals = 0; for (int i = 0; i < t.Length / 2; i++) { if (t[i] == t[(int)n - i - 1]) equals++; else notEquals++; } long mask = 1000000000 + 7; long[] equalPats = new long[equals+1]; long[] notEqualPats = new long[notEquals+1]; CaseCalculator calculator = new CaseCalculator(mask, n); long pow25 = 1; for(long i = 0; i <= equals; i++) { equalPats[i] = calculator.Combination(equals, i); equalPats[i] = calculator.Multi(equalPats[i], pow25); pow25 *= 25; pow25 %= mask; if (i > 0) { equalPats[i] += equalPats[i - 1]; equalPats[i] %= mask; } } long[] twoPows = new long[notEquals + 1]; twoPows[0] = 1; for(long i = 1; i <= notEquals; i++) { twoPows[i] = twoPows[i - 1] * 2; twoPows[i] %= mask; } long pow24 = 1; for(long i = 0; i <= notEquals; i++) { notEqualPats[i] = calculator.Multi( calculator.Combination(notEquals, i), twoPows[notEquals - i]); notEqualPats[i] = calculator.Multi(notEqualPats[i], pow24); pow24 *= 24; pow24 %= mask; } /* long[] notEqualSums = new long[notEquals + 1]; for (long i = 0; i <= notEquals; i++) { notEqualSums[i] = notEqualPats[i]; if (i > 0) notEqualSums[i] += notEqualSums[i - 1]; notEqualSums[i] %= mask; } */ long res = 0; bool haveCenter = n % 2 == 1; for (long i = 0; i <= notEquals; i++) { long remain = k - notEquals - i; if (remain < 0) continue; if (!(notEquals == 0 && remain == 1)) { res += calculator.Multi(notEqualPats[i], equalPats[Min(equals, remain / 2)]); } if (remain > 0 && haveCenter) { long tmp = calculator.Multi(notEqualPats[i], equalPats[Min(equals,(remain - 1) / 2)]); tmp = calculator.Multi(tmp, 25); res += tmp; } res %= mask; } WriteLine(res); }
static void Method(string[] args) { int[] nk = ReadInts(); int n = nk[0]; int k = nk[1]; int[,] vals = new int[n, n]; for (int i = 0; i < n; i++) { int[] row = ReadInts(); for (int j = 0; j < n; j++) { vals[i, j] = row[j]; } } UnionFind rowSet = new UnionFind(n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { bool ok = true; for (int l = 0; l < n; l++) { if (vals[i, l] + vals[j, l] > k) { ok = false; break; } } if (ok) { rowSet.Unite(i, j); } } } UnionFind colSet = new UnionFind(n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { bool ok = true; for (int l = 0; l < n; l++) { if (vals[l, i] + vals[l, j] > k) { ok = false; break; } } if (ok) { colSet.Unite(i, j); } } } long mask = 998244353; var calculator = new CaseCalculator(mask, n); long res = 1; for (int i = 0; i < n; i++) { if (rowSet.Root(i) == i) { int size = rowSet.GetSize(i); res *= calculator.Permutation(size, size); res %= mask; } } for (int i = 0; i < n; i++) { if (colSet.Root(i) == i) { int size = colSet.GetSize(i); res *= calculator.Permutation(size, size); res %= mask; } } WriteLine(res); }
static void Method(string[] args) { long[] nm = ReadLongs(); long n = nm[0]; long m = nm[1]; long mask = 1000000000 + 7; var calculator = new CaseCalculator(mask, m + 100); long[] evenRevSums = new long[n + 10]; long[] oddRevSums = new long[n + 10]; for (int i = 0; i < n + 10; i++) { if (i % 2 == 0) { evenRevSums[i] = calculator.allInverses[i]; } else { oddRevSums[i] = calculator.allInverses[i]; } if (i > 0) { evenRevSums[i] += evenRevSums[i - 1]; evenRevSums[i] %= mask; oddRevSums[i] += oddRevSums[i - 1]; oddRevSums[i] %= mask; } } long res = 0; for (int i = 0; i <= n; i++) { if (i % 2 == 0) { res += (calculator.Combination(n, i) * calculator.Permutation(m - i, n - i)) % mask; res %= mask; } else { res += mask - (calculator.Combination(n, i) * calculator.Permutation(m - i, n - i)) % mask; res %= mask; } } res *= calculator.Permutation(m, n); res %= mask; /* * for(long i = n - (m - n); i <= n; i++) * { * long tmp = calculator.Permutation(n, n - i); * long selects = calculator.allPermutations[i]; * if (i % 2 == 0) * { * selects *= (mask + evenRevSums[i] - oddRevSums[i]) % mask; * * } * else * { * selects *= (mask + oddRevSums[i] - evenRevSums[i]) % mask; * } * selects %= mask; * tmp *= selects; * tmp %= mask; * res += tmp; * } * * res *= calculator.Permutation(m, n); * res %= mask; */ WriteLine(res); }
static void Method(string[] args) { int[] nk = ReadInts(); int n = nk[0]; int k = nk[1]; long[] array = ReadLongs(); long mask = 998244353; var calculator = new CaseCalculator(mask, k + 5); long[,] pows = new long[k + 1, n]; long[] pow2s = new long[k + 1]; for (int i = 0; i <= k; i++) { for (int j = 0; j < n; j++) { if (i == 0) { pows[i, j] = 1; } else { pows[i, j] = pows[i - 1, j] * array[j]; pows[i, j] %= mask; } } if (i == 0) { pow2s[i] = 1; } else { pow2s[i] = pow2s[i - 1] * 2; pow2s[i] %= mask; } } long[] sums = new long[k + 1]; for (int i = 0; i <= k; i++) { for (int j = 0; j < n; j++) { sums[i] += pows[i, j]; sums[i] %= mask; } } for (int i = 1; i <= k; i++) { long val = 0; for (int j = 0; j <= i; j++) { long left = sums[j] * calculator.Inverse(calculator.Permutation(j, j)); long right = sums[i - j] * calculator.Inverse(calculator.Permutation(i - j, i - j)); left %= mask; right %= mask; val += left * right; val %= mask; } val *= calculator.Permutation(i, i); val %= mask; long sames = pow2s[i] * sums[i]; sames %= mask; val += mask - sames; val %= mask; val *= calculator.Inverse(2); val %= mask; WriteLine(val); } }