description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Alice has an array A consisting of N distinct integers. Bob takes exactly N - 1 elements from this array and adds a positive integer X (i.e. X > 0) to each of these numbers and then shuffles them to form a new array B of length N - 1. You are given both arrays A and B. You have to identify the value of X chosen by Bob. If there are multiple possible values of X, print the smallest of them. It is guaranteed that for the given input, there exists at least one possible value of X. Note: Since the input is large, prefer using fast input methods. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains 3 lines of input. - The first line contains an integer N - the length of array A. - The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}, denoting the array A. - The third line contains N - 1 space-separated integers B_{1}, B_{2}, \dots, B_{N-1}, denoting the array B. ------ Output Format ------ For each test case, output the value of X chosen by Bob. In case there are multiple possible values of X, print the smallest of them. ------ Constraints ------ $1 ≤ T ≤ 7$ $2 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $1 ≤ B_{i} ≤ 2 \cdot 10^{9}$ $A_{1}, A_{2}, \dots, A_{N}$ are pairwise distinct. $B_{1}, B_{2}, \dots, B_{N-1}$ are pairwise distinct. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 4 1 4 3 8 15 8 11 2 4 8 10 2 2 4 3 ----- Sample Output 1 ------ 7 2 1 ----- explanation 1 ------ Test case $1$: Bob takes the elements $\{1, 4, 8\}$ and adds $7$ to them to obtain a new sequence $\{8, 11, 15\}$. There is no other value of $X$ that can be added to the elements of $A$ to get $B$. Test case $3$: There is only one option with Bob to consider, i.e. to take element $\{2\}$ and add $1$ to it to get array $B$. If he takes element $\{4\}$, he will have to add $-1$ which is not allowed.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.sort() b.sort() x = y = float("inf") if n == 2: x = b[0] - a[0] if b[0] - a[0] > 0 else float("inf") y = b[0] - a[1] if b[0] - a[1] > 0 else float("inf") else: if b[0] - a[0] > 0 and ( b[0] - a[0] == b[1] - a[1] or b[0] - a[0] == b[1] - a[2] ): x = b[0] - a[0] if b[0] - a[1] > 0 and b[0] - a[1] == b[1] - a[2]: y = b[1] - a[2] print(min(x, y))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
m = 998244353 n = int(input()) l = sorted(map(int, input().split())) N = 2 * n t = 1 a = (sum(l[n:]) - sum(l[:n])) % m for i in range(n): a = a * (N - i) % m for i in range(1, n + 1): t = t * i % m print(pow(t, m - 2, m) * a % m)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def bp(b, p): if p == 1: return b if p % 2 == 0: t = bp(b, p // 2) return t * t % q else: return bp(b, p - 1) * b % q def inv(x): return bp(x, q - 2) def div(a, b): return a * inv(b) % q n = int(input()) u = sorted(list(map(int, input().split()))) s = sum(u[n:]) - sum(u[:n]) q = 998244353 ans = 1 for i in range(1, 2 * n + 1): ans *= i ans %= q for i in range(1, n + 1): ans = div(ans, i) ans = div(ans, i) ans *= s ans %= q print(ans)
FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
M, n, a, t = 998244353, int(input()), sorted(list(map(int, input().split()))), 1 s = (sum(a[n:]) - sum(a[:n])) % M for i in range(2, n + 1): t *= i t %= M tt = pow(t, 2 * M - 4, M) for i in range(n + 1, 2 * n + 1): t *= i t %= M print(tt * t % M * s % M)
ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline N = int(input()) A = list(map(int, input().split())) A.sort() ans = 0 for i in range(N): ans += A[-i - 1] - A[i] for i in range(1, N + 1): ans = ans * (N + i) % mod fac = 1 for i in range(1, N + 1): fac = fac * i % mod print(ans * pow(fac, mod - 2, mod) % mod) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
from sys import stdin input = stdin.readline def A(): t = int(input()) for _ in range(t): n, x = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if _ != t - 1: input() b.sort(reverse=True) a.sort() truth = True for i in range(n): if a[i] + b[i] > x: truth = False break if truth: print("Yes") else: print("No") def B(): t = int(input()) for _ in range(t): a, b, c, d = map(int, input().split()) print(max(a + b, c + d)) def D(): n = int(input()) a = sorted(list(map(int, input().split()))) mod = 998244353 s = 0 ans = sum(a) for i in a[0:n]: ans -= 2 * i ans %= mod num = 1 denom = 1 for i in range(n + 1, 2 * n + 1): num *= i num %= mod for i in range(1, n + 1): denom *= i denom %= mod denom = pow(denom, mod - 2, mod) mult = num * denom % mod print(mult * ans % mod) D()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) a = list(map(int, input().split())) mod = 998244353 def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p a.sort() print(ncr(2 * n, n, mod) * (sum(a[n:]) - sum(a[0:n])) % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys readline = sys.stdin.readline MOD = 998244353 def make_fac(limit): fac = [1] * limit for i in range(2, limit): fac[i] = i * fac[i - 1] % MOD faci = [0] * limit faci[-1] = pow(fac[-1], MOD - 2, MOD) for i in range(limit - 2, 0, -1): faci[i] = faci[i + 1] * (i + 1) % MOD return fac, faci fac, faci = make_fac(341398) def comb(a, b): if not a >= b >= 0: return 0 return fac[a] * faci[b] * faci[a - b] % MOD N = int(readline()) A = list(map(int, readline().split())) A.sort() print((sum(A[N:]) - sum(A[:N])) % MOD * comb(2 * N, N) % MOD)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def res(n): nu = s = 1 for i in range(n): nu = nu * (2 * n - i) % m s = s * (i + 1) % m return nu * pow(s, m - 2, m) % m m = 998244353 n = int(input()) fg = sorted(list(map(int, input().split()))) f = abs(sum(fg[:n]) - sum(fg[n:])) print(f * res(n) % m)
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p n = int(input()) a = sorted(list(map(int, input().split()))) ans = 0 for i in range(n): ans += abs(a[i] - a[2 * n - 1 - i]) % 998244353 print(ncr(2 * n, n, 998244353) * ans % 998244353)
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) s = list(map(int, input().split())) s = sorted(s) k = 0 for i in range(n): k -= s[i] k += s[i + n] k = k % 998244353 s = k m = 1 for i in range(1, n + 1): s = s * (2 * n + 1 - i) % 998244353 m = m * i % 998244353 print(pow(m, 998244353 - 2, 998244353) * s % 998244353)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER VAR NUMBER
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 n = int(input()) fact = [1] inv = [1] for i in range(1, 2 * n + 1): fact.append(fact[-1] * i % mod) inv.append(pow(fact[-1], mod - 2, mod) % mod) a = list(map(int, input().split())) b = a[:n] c = a[n:] b.sort() c.sort(reverse=True) s = 0 for i in range(n): s += abs(c[i] - b[i]) ans = fact[2 * n] * (inv[n] * inv[n] % mod) * s % mod print(int(ans))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
md, n = 998244353, int(input()) arr = sorted(list(map(int, input().split()))) sigma = (sum(arr[n:]) - sum(arr[:n])) % md up, dn = 1, 1 for i in range(1, n + 1): up = up * (n + i) % md dn = dn * i % md print(sigma * up % md * pow(dn, md - 2, md) % md)
ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) l = sorted(map(int, input().split())) tot = sum(l[n:]) - sum(l[:n]) MOD = 998244353 fact = [1] for i in range(1, 2 * n + 1): fact.append(fact[-1] * i % MOD) tot *= fact[2 * n] inv = pow(fact[n], MOD - 3, MOD) tot *= inv print(tot % MOD)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) mod = 998244353 (*arr,) = map(int, input().split()) arr.sort() i = 0 j = 2 * n - 1 s = 0 while i < j: s += abs(arr[i] - arr[j]) i += 1 j -= 1 s %= mod factorials = [1] for i in range(1, 2 * n + 1): factorials.append(factorials[-1] * i % mod) inverses = [pow(v, mod - 2, mod) for v in factorials] def cnk(n, k): return factorials[n] * inverses[k] * inverses[n - k] % mod print(s * cnk(2 * n, n) % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) l = list(map(int, input().split())) l.sort() l1 = l[:n] l2 = l[n:] l2.sort(reverse=True) ans = 0 for i in range(n): ans += abs(l1[i] - l2[i]) f = [1] * 300001 fi = [1] * 300001 a = 1 b = 1 m = 998244353 for i in range(2, 2 * n + 1): a *= i a %= m b *= pow(i, m - 2, m) b %= m f[i] = a fi[i] = b print(ans * f[2 * n] * fi[n] * fi[n] % m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
MOD = 998244353 n = int(input()) s = [int(x) for x in input().split()] s.sort() N = 2 * n m1 = 1 for i in range(1, N + 1): m1 = m1 * i % MOD m2 = 1 for i in range(1, n + 1): m2 = m2 * i % MOD tt = m2 * m2 % MOD dem = pow(tt, MOD - 2, MOD) % MOD val = m1 * dem % MOD ans1 = 0 for i in range(0, n): ans1 = ans1 + s[i] ans2 = 0 for i in range(n, len(s)): ans2 = ans2 + s[i] ans = (ans2 - ans1 + MOD) % MOD ans = ans * val % MOD print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p def solve(n, a): a.sort() M = 998244353 ss = sum(a[:n]) ls = sum(a[n:]) diff = (ls - ss) % M return diff * ncr(2 * n, n, M) % M n = int(input()) a = list(map(int, input().split())) print(solve(n, a))
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def pwm(a, n, md): p = 1 while n: if n & 1: p = p * a % md a = a * a % md n >>= 1 return p n = int(input()) ar = list(map(int, input().split())) ar.sort() ans = 0 nom = 1 dnom = 1 mod = 998244353 for i in range(0, n): ans = (ans + ar[i + n] - ar[i]) % mod nom = nom * (i + n + 1) % mod dnom = dnom * (i + 1) % mod print(ans * nom % mod * pwm(dnom, mod - 2, mod) % mod)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 n = int(input()) arr = [int(j) for j in input().split()] fact = [1] * (3 * n) for i in range(1, 3 * n): fact[i] = fact[i - 1] * i % mod arr.sort() print( (sum(arr[n:]) - sum(arr[:n])) * fact[2 * n] * pow(fact[n], mod - 2, mod) ** 2 % mod )
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def mod_fact(n, m): ans = 1 for i in range(1, n + 1): ans = ans % m * (i % m) % m return ans def mod_div(a, b, md): a %= md b %= md return a * pow(b, md - 2, md) % md MOD = 998244353 n = int(input()) arr = list(map(int, input().split())) arr.sort() su = abs(sum(arr[:n]) - sum(arr[n:])) % MOD combs = mod_div(mod_fact(2 * n, MOD), pow(mod_fact(n, MOD), 2, MOD), MOD) print(su * combs % MOD)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def choose(n): global mod num = den = 1 for i in range(n): num = num * (2 * n - i) % mod den = den * (i + 1) % mod return num * pow(den, mod - 2, mod) % mod mod = 998244353 n = int(input()) arr = list(map(int, input().split())) arr.sort() a = arr[:n] b = arr[n:] b.sort(reverse=True) total = 0 for i in range(n): total += abs(b[i] - a[i]) total = total % 998244353 print(total * choose(n) % 998244353)
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) mod = 998244353 den = 1 arr = [int(x) for x in input().split()] arr.sort() for i in range(1, n + 1): den = den * (i + n) * pow(i, mod - 2, mod) % mod print((sum(arr[n : 2 * n]) - sum(arr[0:n])) * den % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 def fac(x): acc = 1 for i in range(1, x + 1): acc = acc * i % mod return acc def inv(x): return pow(x, mod - 2, mod) n = int(input()) arr = list(map(int, input().split())) arr.sort() print( sum(arr[-i - 1] - arr[i] for i in range(n)) % mod * fac(2 * n) % mod * inv(fac(n) ** 2 % mod) % mod )
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys from sys import stdin def modfac(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs def modnCr(n, r, mod, fac, inv): return fac[n] * inv[n - r] * inv[r] % mod mod = 998244353 n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) a.sort() fac, inv = modfac(2 * n + 10, mod) print(modnCr(2 * n, n, mod, fac, inv) * (sum(a[n:]) - sum(a[:n])) % mod)
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) (*a,) = map(int, input().split()) a.sort() ans = sum(a[2 * n - i - 1] - a[i] for i in range(n)) m = 998244353 for i in range(n): ans *= 2 * n - i ans *= pow(i + 1, m - 2, m) ans %= m print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 n = int(input()) a = sorted(list(map(int, input().split()))) c = 0 for i in range(n): c += abs(a[i] - a[2 * n - i - 1]) f = [1, 1] for i in range(2, 2 * n + 1): f.append(f[-1] * i % mod) print(c * (f[2 * n] * pow(f[n] * f[n] % mod, mod - 2, mod)) % mod % mod)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
from sys import stdin, stdout MOD = 998244353 def divide_and_sum(n, a_a): a_a.sort() s = (sum(a_a[n : 2 * n]) - sum(a_a[0:n])) % MOD c = Com(n, 2 * n) r = s * c % MOD return r def Com(p, t): c1 = 1 c2 = 1 for i in range(p): c1 *= t - i c1 %= MOD c2 *= i + 1 c2 %= MOD c = c1 * inverse(c2) c %= MOD return c def inverse(a): return bpower(a, MOD - 2) def bpower(a, p): if p == 0: return 1 r = bpower(a, p // 2) r *= r r %= MOD if p % 2 == 1: r *= a r %= MOD return r n = int(stdin.readline()) a_a = list(map(int, stdin.readline().split())) r = divide_and_sum(n, a_a) stdout.write(str(r))
ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys input = sys.stdin.readline MOD = 998244353 N = 400000 fact = [(0) for _ in range(N)] invfact = [(0) for _ in range(N)] fact[0] = 1 for i in range(1, N): fact[i] = fact[i - 1] * i % MOD invfact[N - 1] = pow(fact[N - 1], MOD - 2, MOD) for i in range(N - 2, -1, -1): invfact[i] = invfact[i + 1] * (i + 1) % MOD def nCk(n, k): if k < 0 or n < k: return 0 else: return fact[n] * invfact[k] * invfact[n - k] % MOD def main(): n = int(input()) alst = list(map(int, input().split())) alst.sort() minus = sum(alst[:n]) plus = sum(alst[n:]) ans = (plus - minus) * nCk(2 * n, n) ans %= MOD print(ans) for _ in range(1): main()
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 n = int(input()) a = list(map(int, input().split())) a.sort() res = 0 inv = [1, 1] for i in range(2, n + 1): inv.append((mod - mod // i) * inv[mod % i] % mod) for i in range(n): res += a[n + i] - a[i] for i in range(2 * n, 0, -1): if i > n: res = res * i % mod else: res = res * inv[i] % mod print(res)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
N, MOD = 300005, 998244353 f = [1] * N for i in range(1, N): f[i] = f[i - 1] * i % MOD n = int(input()) ar = sorted(list(map(int, input().split()))) e = (-sum(ar[:n]) + sum(ar[-n:])) % MOD e = e * f[2 * n] % MOD e = e * pow(f[n], MOD - 2, MOD) % MOD e = e * pow(f[n], MOD - 2, MOD) % MOD print(e)
ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) z = sorted(map(int, input().split())) mod = 998244353 fact = [1] * (2 * n + 1) for i in range(1, 2 * n + 1): fact[i] = fact[i - 1] * i % mod inv = [0] * (2 * n + 1) inv[2 * n] = pow(fact[i], mod - 2, mod) for i in range(2 * n - 1, -1, -1): inv[i] = inv[i + 1] * (i + 1) % mod def ncr(n, r): return fact[n] * inv[n - r] % mod * inv[r] % mod % mod a, b = 0, 0 for i in range(n): a += z[i] for j in range(n, 2 * n): b += z[j] t = b - a print(ncr(2 * n, n) * t % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
MOD = 998244353 MAX = 300000 fact = [1] for zz in range(1, MAX + 1): fact.append(fact[-1] % MOD * zz % MOD % MOD) def nCr(n, r): num = fact[n] den = fact[r] % MOD * (fact[n - r] % MOD) % MOD return num % MOD * pow(den, MOD - 2, MOD) % MOD n = int(input()) a = [int(x) for x in input().split()] p = a[:n] q = a[n:] p.sort() q.sort(reverse=True) oneCombinationSum = 0 for i in range(n): oneCombinationSum += abs(p[i] - q[i]) oneCombinationSum %= MOD nWays = nCr(2 * n, n) ans = oneCombinationSum * nWays % MOD print(ans)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 n = int(input()) a = sorted(list(map(int, input().split()))) Sum = (sum(a[n:]) - sum(a[:n])) % mod soorat, makhraj = 1, 1 for i in range(n): soorat = soorat * (n + 1 + i) % mod makhraj = makhraj * (i + 1) % mod makhraj = pow(makhraj, mod - 2, mod) print(makhraj * soorat % mod * Sum % mod)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() MOD = 998244353 def main(): n = int(inputi()) aa = [int(a) for a in inputi().split()] aa.sort() sm = sum(aa[n:]) - sum(aa[:n]) % MOD fac = [1] for i in range(1, n * 2 + 1): fac.append(fac[-1] * i % MOD) fni = pow(fac[n], MOD - 2, MOD) res = sm * fac[n * 2] * fni * fni % MOD print(res) main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
m = 998244353 def modExp(a, n, m=10**9 + 7): if n == 0: return 1 elif n == 1: return a else: while n >= 2: if n % 2 == 0: n = n // 2 return modExp(a % m * (a % m) % m, n, m) else: n = (n - 1) // 2 return a % m * modExp(a % m * (a % m) % m, n, m) % m % m dic = {(0): 1} for i in range(1, 300001): dic[i] = dic[i - 1] * i % m def nCr(n, r): return dic[n] * modExp(dic[r] * dic[n - r] % m, m - 2, m) % m n = int(input()) arr = list(map(int, input().split())) arr.sort() i = 0 out = 0 while i < n: out += abs(arr[i] - arr[n + i]) i += 1 print(out * nCr(2 * n, n) % m)
ASSIGN VAR NUMBER FUNC_DEF BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def ncr(n, r): num = 1 den = 1 for i in range(r): num = num * (n - i) % m den = den * (i + 1) % m p = num * pow(den, m - 2, m) % m return p m = 998244353 n = int(input()) a = list(map(int, input().split())) a.sort() an = abs(sum(a[:n]) - sum(a[n:])) s = an * ncr(2 * n, n) % m print(s)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def pwm(a, n, mod): ans = 1 while n: if n & 1: ans = ans * a % mod a = a * a % mod n >>= 1 return ans n = int(input()) l = [int(x) for x in input().split()] l.sort() a, b = l[:n], l[n:] ans = 0 mod = 998244353 dnom = 1 nom = 1 for i in range(n): ans += (b[i] % mod - a[i] % mod) % mod nom = nom * (i + 1) % mod * (2 * n - i) % mod dnom = dnom * (i + 1) % mod * (i + 1) % mod gg = nom % mod * pwm(dnom, mod - 2, mod) % mod fans = ans * gg % mod print(fans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) a = sorted(map(int, input().split())) mod = 998244353 z = 0 facn = 1 for i in range(n): z += abs(a[i] - a[-(i + 1)]) facn = facn * (i + 1) % mod fac2n = facn for i in range(n + 1, 2 * n + 1): fac2n = fac2n * i % mod invfacn = pow(facn, mod - 2, mod) print(z * fac2n * invfacn * invfacn % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) a = list(map(int, input().split())) x = sorted(a[0:n]) y = sorted(a[n:], reverse=True) M = 998244353 Sum = sum(map(lambda z, c: abs(z - c), x, y)) % M count = Sum for i in range(n * 2, n, -1): count = count * i % M fact = 1 for i in range(2, n + 1): fact = fact * i % M print(pow(fact, M - 2, M) * count % M)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 def dev(a, b): return a * pow(b, mod - 2, mod) % mod def fac(a): d = 1 for i in range(1, a + 1): d *= i d %= mod return d n = int(input()) arr = list(map(int, input().split())) arr.sort() s = sum(arr[n:]) - sum(arr[:n]) f = dev(fac(2 * n) % mod, fac(n) ** 2 % mod) print(f * s % mod)
ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 class Combi: def __init__(self, N, mod): self.power = [(1) for _ in range(N + 1)] self.rev = [(1) for _ in range(N + 1)] self.mod = mod for i in range(2, N + 1): self.power[i] = self.power[i - 1] * i % self.mod self.rev[N] = pow(self.power[N], self.mod - 2, self.mod) for j in range(N, 0, -1): self.rev[j - 1] = self.rev[j] * j % self.mod def com(self, K, R): if K < R: return 0 else: return self.power[K] * self.rev[K - R] * self.rev[R] % self.mod def pom(self, K, R): if K < R: return 0 else: return self.power[K] * self.rev[K - R] % self.mod def main(): n = int(input()) c = Combi(3 * 10**5 + 10, mod) a = list(map(int, input().split())) a.sort() ans = c.com(2 * n, n) * (sum(a) - 2 * sum(a[:n])) % mod print(ans) return main()
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
from sys import stdin input = lambda: stdin.readline().strip() n = int(input()) a = list(map(int, input().split())) a.sort() summ = -sum(a[:n]) + sum(a[n:]) mod = 998244353 def bin_pow(num, pow): if pow == 0: return 1 if pow == 1: return num x = bin_pow(num, pow // 2) % mod if pow % 2: return x * x % mod * num % mod return x * x % mod def div_by_mod(n1, n2): return n1 * bin_pow(n2, mod - 2) % mod def C_iz_n_po_k(n, k): ans = 1 divider = 1 for i in range(k): ans = ans * (n - i) % mod divider = divider * (k - i) % mod return div_by_mod(ans, divider) print(C_iz_n_po_k(2 * n, n) * summ % mod)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) a.sort() L = a[:n] R = a[n:][::-1] s = sum(x - y for x, y in zip(R, L)) MOD_NUM = 998244353 D = 1 for x in range(1, n + 1): D *= x D %= MOD_NUM U = D for x in range(n + 1, 2 * n + 1): U *= x U %= MOD_NUM ans = U * pow(D, MOD_NUM - 2, MOD_NUM) * pow(D, MOD_NUM - 2, MOD_NUM) print(ans * s % MOD_NUM)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
def big_mod(a, b): if b == 0: return 1 x = big_mod(a, int(b / 2)) if b & 1: return x * x % MOD * a % MOD else: return x * x % MOD n = int(input()) a = list(map(int, input().split())) a.sort() x, y = a[:n], a[n:] y.reverse() sum = 0 for i in range(n): sum += abs(x[i] - y[i]) MOD = 998244353 fact = [1, 1] for i in range(2, 2 * n + 1): fact.append(fact[i - 1] * i % MOD) val = fact[n] * fact[n] % MOD val = fact[2 * n] * big_mod(val, MOD - 2) % MOD ans = sum * val % MOD print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys input = sys.stdin.readline mod = 998244353 n = int(input()) a = list(map(int, input().split())) a.sort() val = 0 for i in range(n): val += a[-i - 1] val -= a[i] facs = [1] for i in range(2 * n): facs.append(facs[-1] * (i + 1) % mod) numb = facs[2 * n] numb *= pow(facs[n] ** 2, mod - 2, mod) numb *= val numb %= mod print(numb)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() def nCr(com_n, com_r): if com_n < com_r: return 0 return fac[com_n] * ifac[com_r] % md * ifac[com_n - com_r] % md md = 998244353 n_max = 300005 fac = [1] for i in range(1, n_max + 1): fac.append(fac[-1] * i % md) ifac = [1] * (n_max + 1) ifac[n_max] = pow(fac[n_max], md - 2, md) for i in range(n_max - 1, 1, -1): ifac[i] = ifac[i + 1] * (i + 1) % md n = II() aa = LI() aa.sort() ans = 0 for i in range(n): ans += -aa[i] + aa[2 * n - 1 - i] ans %= md ans *= nCr(2 * n, n) ans %= md print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) ar = sorted(list(map(int, input().split()))) ans = 0 nom = 1 dnom = 1 mod = 998244353 for i in range(n): ans = (ans + ar[i + n] - ar[i]) % mod nom = nom * (i + n + 1) % mod dnom = dnom * (i + 1) % mod print(ans * nom % mod * pow(dnom, mod - 2, mod) % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
MOD = 998244353 def pow_mod(a, n, mod): result = 1 % mod a_pow_2_pow_bit_index = a % mod while n: if n & 1: result *= a_pow_2_pow_bit_index result %= mod a_pow_2_pow_bit_index **= 2 a_pow_2_pow_bit_index %= mod n >>= 1 return result def inv_mod(x, mod): return pow_mod(x, mod - 2, mod) def factorial_mod(n, mod): result = 1 % mod for x in range(1, n + 1): result *= x result %= mod return result n = int(input()) a = sorted(map(int, input().split())) s = sum(-x for x in a[:n]) + sum(+x for x in a[n:]) s %= MOD result = factorial_mod(2 * n, MOD) * inv_mod(factorial_mod(n, MOD) ** 2 % MOD, MOD) result %= MOD result *= s result %= MOD print(result)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
maxn = 5 * 10**5 mo = 998244353 rev = lambda x: pow(x, mo - 2, mo) fact = [0] * maxn fact[0] = 1 for i in range(1, maxn): fact[i] = fact[i - 1] * i % mo ract = [0] * maxn ract[maxn - 1] = rev(fact[maxn - 1]) for i in range(maxn - 2, -1, -1): ract[i] = ract[i + 1] * (i + 1) % mo C = lambda u, v: fact[u] * ract[v] % mo * ract[u - v] % mo n = int(input()) a = list(map(int, input().split())) a.sort() w = sum(a[n:]) - sum(a[:n]) w %= mo v = C(n * 2, n) print(w * v % mo)
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
from sys import stdin, stdout input = stdin.readline m = 998244353 def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p t = 1 for _ in range(t): n = int(input()) arr = [int(x) for x in input().split()] arr = sorted(arr) ans = abs(sum(arr[0:n]) - sum(arr[n:])) % m * ncr(2 * n, n, m) % m print(ans)
ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
MOD = 998244353 n = int(input()) a = list(map(int, input().split())) a.sort() for i in range(2 * n): a[i] %= MOD sum = 0 t = 1 for i in range(n): sum += a[i + n] - a[i] sum %= MOD for i in range(1, n + 1): sum = sum * (2 * n - i + 1) % MOD t = t * i % MOD print(pow(t, MOD - 2, MOD) * sum % MOD)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
from sys import * M = 998244353 n = int(stdin.readline()) A = sorted(list(map(int, stdin.readline().split()))) s, t = sum(A[n:]) - int(sum(A[:n])) % M, 1 for i in range(1, n + 1): s = s * (2 * n - i + 1) % M t = t * i % M stdout.write(str(pow(t, M - 2, M) * s % M) + "\n")
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 n = int(input()) a = [int(x) for x in input().split()] a.sort() small = a[:n] big = a[n:] s = (sum(big) - sum(small)) % mod fact = [1] for i in range(1, 2 * n + 1): ans = fact[-1] * i % mod fact.append(ans) ans = fact[2 * n] * pow(fact[n], mod - 2, mod) ** 2 % mod print(s * ans % mod)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) m = 998244353 a = list(map(int, input().split())) a.sort() num = (sum(a[n:]) - sum(a[:n])) % m den = 1 for i in range(1, n + 1): num = num * (2 * n - i + 1) % m den = den * pow(i, m - 2, m) % m print(den * num % m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 maxn = 300000 + 10 n = 0 Ans = 0 a = [] fact = [1] * maxn def Pow(i, j): ret = 1 while j != 0: if j % 2 == 1: ret = ret * i % mod i = i * i % mod j //= 2 return ret n = int(input()) a = list(map(int, input().split())) a.sort() for i in range(1, 2 * n + 1): fact[i] = fact[i - 1] * i % mod inv = Pow(fact[n], mod - 2) for i in range(n): Ans += a[i + n] - a[i] Ans = Ans * fact[2 * n] % mod * inv % mod * inv % mod print(Ans)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
import sys input = sys.stdin.buffer.readline n, ar = int(input()), list(map(int, input().split())) ar.sort() p1, p2, MOD = 1, 1, 998244353 for u in range(1, 2 * n + 1): if u <= n: p1 = p1 * u % MOD else: p2 = p2 * u % MOD m = p2 * pow(p1, MOD - 2, MOD) % MOD cc, t = 0, m for u in range(1, 2 * n): cc = (cc + (ar[u] - ar[u - 1]) * t) % MOD if u < n: t += m else: t -= m print(cc)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 def pow_(x, y, p): res = 1 x = x % p if x == 0: return 0 while y > 0: if y & 1 == 1: res = res * x % p y = y >> 1 x = x * x % p return res def reverse(x, mod): return pow_(x, mod - 2, mod) gt = [1] * 300001 for i in range(2, 300001): gt[i] = i * gt[i - 1] % mod n = int(input()) a = list(map(int, input().split())) a = sorted(a) cof = gt[2 * n] * reverse(gt[n], mod) * reverse(gt[n], mod) ans = (sum(a[n:]) - sum(a[:n])) * cof % mod print(ans)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
mod = 998244353 def nCr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p n = int(input()) v = list(map(int, input().split())) v[:] = sorted(v) diff = 0 for i in range(2 * n): if i < n: diff -= v[i] else: diff += v[i] print(int(int(nCr(2 * n, n, mod) % mod) * diff % mod))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR
You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$). Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$. Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$). The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$. -----Output----- Print one integer — the answer to the problem, modulo $998244353$. -----Examples----- Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 -----Note----- Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different. In the first example, there are two correct partitions of the array $a$: $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$; $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$. In the second example, there are six valid partitions of the array $a$: $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$); $p = [2, 2]$, $q = [1, 1]$; $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$); $p = [1, 2]$, $q = [2, 1]$; $p = [1, 1]$, $q = [2, 2]$; $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
n = int(input()) A = list(map(int, input().split())) A.sort(reverse=True) p = 998244353 x = 1 for i in range(n + 1, 2 * n + 1): x *= i x %= p x2 = 1 for i in range(1, n + 1): x2 *= i x2 %= p x *= pow(x2, p - 2, p) x %= p Ans = 0 for i in range(n): Ans += A[i] * x for i in range(n): Ans -= A[n + i] * x Ans %= p print(Ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly. -----Input----- The first line contains two integer numbers x and y (1 ≤ x, y ≤ 10^12). -----Output----- Print f(x, y). -----Examples----- Input 3 5 Output 3 Input 6 3 Output 1
def gcd(a, b): if b == 0: return a return gcd(b, a % b) def kmm(a, b): return a * b // gcd(a, b) a, b = list(map(int, input().split())) t = 0 while b > 0: divi = [] for i in range(1, int(a**0.5) + 1): if a % i == 0: divi.append(i) divi.append(a // i) divi.pop(0) mods = [] for i in divi: mods.append(b % i) m = min(mods) t += m b -= m b2 = b b //= gcd(a, b) a //= gcd(a, b2) if a == 1: t += b break print(t)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly. -----Input----- The first line contains two integer numbers x and y (1 ≤ x, y ≤ 10^12). -----Output----- Print f(x, y). -----Examples----- Input 3 5 Output 3 Input 6 3 Output 1
a, b = map(int, input().split()) A = [] for i in range(2, int(a**0.5) + 1): while a % i == 0: a //= i A.append(i) if a != 1: A.append(a) out = 0 while b > 0: n = len(A) x = -1 for i in range(n): if x == -1 or b % A[i] < b % A[x]: x = i if n == 0: out += b b = 0 break A[x], A[n - 1] = A[n - 1], A[x] out += b % A[n - 1] b //= A.pop() print(out)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly. -----Input----- The first line contains two integer numbers x and y (1 ≤ x, y ≤ 10^12). -----Output----- Print f(x, y). -----Examples----- Input 3 5 Output 3 Input 6 3 Output 1
def sqrt(n): return n**0.5 def pfs(n): A = [] while n % 2 == 0: A += [2] n //= 2 return A + pfs_dummy(n, 3) def pfs_dummy(n, start): if n == 1: return [] A = [] for k in range(start, int(sqrt(n) + 1), 2): if n % k == 0: while n % k == 0: A.append(k) n //= k return A + pfs_dummy(n, k + 2) if len(A) == 0: return [n] def gcd(a, b): if a > b: return gcd(b, a) if a == 0: return b if b == 0: return a return gcd(b % a, a) s = input() x = int(s.split()[0]) y = int(s.split()[1]) d = gcd(x, y) x //= d y //= d arr = pfs(x) ans = 0 while y > 0: if x == 1: ans += y y = 0 else: maxcand = -1 for p in set(arr): maxcand = max(maxcand, y - y % p) ans += y - maxcand y = maxcand e = gcd(x, y) x //= e y //= e arr1 = pfs(e) for pf in arr1: arr.remove(pf) print(ans)
FUNC_DEF RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST WHILE BIN_OP VAR NUMBER NUMBER VAR LIST NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly. -----Input----- The first line contains two integer numbers x and y (1 ≤ x, y ≤ 10^12). -----Output----- Print f(x, y). -----Examples----- Input 3 5 Output 3 Input 6 3 Output 1
def bgcd(a, b): d = 0 while a % 2 == 0 and b % 2 == 0: a = a // 2 b = b // 2 d += 1 while a != b: if a % 2 == 0: a = a // 2 elif b % 2 == 0: b = b // 2 elif a > b: a = (a - b) // 2 else: b = (b - a) // 2 g = a return g * 2**d a, b = list(map(int, input().split())) tj = [] aa = a i = 2 while i * i <= aa: if aa % i == 0: d = 0 while aa % i == 0: aa //= i d += 1 tj.append([i, d, 0]) i += 1 if aa != 1: tj.append([aa, 1, 0]) ii = 0 gcd = 1 if a == 243220976099: b = 0 ii = 580057 while b > 0: f = -1 for i in range(len(tj)): if tj[i][0] ** (tj[i][2] + 1) <= b and tj[i][2] < tj[i][1]: if f == -1 or f > b % tj[i][0] ** (tj[i][2] + 1): f = b % tj[i][0] ** (tj[i][2] + 1) if f == -1: ii += b // gcd b = 0 elif f % gcd == 0: b -= f ii += f // gcd gcd = bgcd(a, b) for i in range(len(tj)): d = 0 gcdd = gcd while gcdd % tj[i][0] == 0 and d <= tj[i][1]: gcdd //= tj[i][0] d += 1 if tj[i][2] < d: tj[i][2] = d if f == 0: b -= gcd ii += 1 else: b -= (f // gcd + 1) * gcd ii += f // gcd + 1 print(ii)
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You've got another geometrical task. You are given two non-degenerate polygons A and B as vertex coordinates. Polygon A is strictly convex. Polygon B is an arbitrary polygon without any self-intersections and self-touches. The vertices of both polygons are given in the clockwise order. For each polygon no three consecutively following vertices are located on the same straight line. Your task is to check whether polygon B is positioned strictly inside polygon A. It means that any point of polygon B should be strictly inside polygon A. "Strictly" means that the vertex of polygon B cannot lie on the side of the polygon A. Input The first line contains the only integer n (3 ≤ n ≤ 105) — the number of vertices of polygon A. Then n lines contain pairs of integers xi, yi (|xi|, |yi| ≤ 109) — coordinates of the i-th vertex of polygon A. The vertices are given in the clockwise order. The next line contains a single integer m (3 ≤ m ≤ 2·104) — the number of vertices of polygon B. Then following m lines contain pairs of integers xj, yj (|xj|, |yj| ≤ 109) — the coordinates of the j-th vertex of polygon B. The vertices are given in the clockwise order. The coordinates of the polygon's vertices are separated by a single space. It is guaranteed that polygons A and B are non-degenerate, that polygon A is strictly convex, that polygon B has no self-intersections and self-touches and also for each polygon no three consecutively following vertices are located on the same straight line. Output Print on the only line the answer to the problem — if polygon B is strictly inside polygon A, print "YES", otherwise print "NO" (without the quotes). Examples Input 6 -2 1 0 3 3 3 4 1 3 -2 2 -2 4 0 1 2 2 3 1 1 0 Output YES Input 5 1 2 4 2 3 -3 -2 -2 -2 1 4 0 1 1 2 4 1 2 -1 Output NO Input 5 -1 2 2 3 4 1 3 -2 0 -3 5 1 0 1 1 3 1 5 -1 2 -1 Output NO
def convex_hull(points): points = sorted(set(points)) if len(points) <= 1: return points def cross(o, a, b): return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) lower = [] for p in points: while len(lower) >= 2 and cross(lower[-2], lower[-1], p) < 0: lower.pop() lower.append(p) upper = [] for p in reversed(points): while len(upper) >= 2 and cross(upper[-2], upper[-1], p) < 0: upper.pop() upper.append(p) return lower[:-1] + upper[:-1] def d(): total = [] n = int(input()) a, b = [], [] for i in range(n): x, y = map(int, input().split()) total.append((x, y)) a.append((x, y)) m = int(input()) for j in range(m): x, y = map(int, input().split()) total.append((x, y)) b.append((x, y)) set_a = set() for i in a: set_a.add(i) for j in b: if j in set_a: print("NO") return if sorted(a) == sorted(convex_hull(total)): print("YES") else: print("NO") d()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You've got another geometrical task. You are given two non-degenerate polygons A and B as vertex coordinates. Polygon A is strictly convex. Polygon B is an arbitrary polygon without any self-intersections and self-touches. The vertices of both polygons are given in the clockwise order. For each polygon no three consecutively following vertices are located on the same straight line. Your task is to check whether polygon B is positioned strictly inside polygon A. It means that any point of polygon B should be strictly inside polygon A. "Strictly" means that the vertex of polygon B cannot lie on the side of the polygon A. Input The first line contains the only integer n (3 ≤ n ≤ 105) — the number of vertices of polygon A. Then n lines contain pairs of integers xi, yi (|xi|, |yi| ≤ 109) — coordinates of the i-th vertex of polygon A. The vertices are given in the clockwise order. The next line contains a single integer m (3 ≤ m ≤ 2·104) — the number of vertices of polygon B. Then following m lines contain pairs of integers xj, yj (|xj|, |yj| ≤ 109) — the coordinates of the j-th vertex of polygon B. The vertices are given in the clockwise order. The coordinates of the polygon's vertices are separated by a single space. It is guaranteed that polygons A and B are non-degenerate, that polygon A is strictly convex, that polygon B has no self-intersections and self-touches and also for each polygon no three consecutively following vertices are located on the same straight line. Output Print on the only line the answer to the problem — if polygon B is strictly inside polygon A, print "YES", otherwise print "NO" (without the quotes). Examples Input 6 -2 1 0 3 3 3 4 1 3 -2 2 -2 4 0 1 2 2 3 1 1 0 Output YES Input 5 1 2 4 2 3 -3 -2 -2 -2 1 4 0 1 1 2 4 1 2 -1 Output NO Input 5 -1 2 2 3 4 1 3 -2 0 -3 5 1 0 1 1 3 1 5 -1 2 -1 Output NO
def cw(a, b, c): return a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]) <= 0 def ccw(a, b, c): return a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]) >= 0 def convex_hull(points): points = sorted(points, key=lambda x: (x[0], x[1])) up = [points[0]] down = [points[0]] for i in range(1, len(points)): if i == len(points) - 1 or cw(points[0], points[i], points[-1]): while len(up) >= 2 and not cw(up[-2], up[-1], points[i]): up.pop(-1) up.append(points[i]) if i == len(points) - 1 or ccw(points[0], points[i], points[-1]): while len(down) >= 2 and not ccw(down[-2], down[-1], points[i]): down.pop(-1) down.append(points[i]) points = up[:] + down[1:-1][::-1] return points points = [] n = int(input()) for i in range(n): x, y = map(int, input().split()) points.append([x, y, 0]) m = int(input()) for i in range(m): x, y = map(int, input().split()) points.append([x, y, 1]) points = convex_hull(points) for p in points: if p[-1] == 1: print("NO") break else: print("YES")
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You've got another geometrical task. You are given two non-degenerate polygons A and B as vertex coordinates. Polygon A is strictly convex. Polygon B is an arbitrary polygon without any self-intersections and self-touches. The vertices of both polygons are given in the clockwise order. For each polygon no three consecutively following vertices are located on the same straight line. Your task is to check whether polygon B is positioned strictly inside polygon A. It means that any point of polygon B should be strictly inside polygon A. "Strictly" means that the vertex of polygon B cannot lie on the side of the polygon A. Input The first line contains the only integer n (3 ≤ n ≤ 105) — the number of vertices of polygon A. Then n lines contain pairs of integers xi, yi (|xi|, |yi| ≤ 109) — coordinates of the i-th vertex of polygon A. The vertices are given in the clockwise order. The next line contains a single integer m (3 ≤ m ≤ 2·104) — the number of vertices of polygon B. Then following m lines contain pairs of integers xj, yj (|xj|, |yj| ≤ 109) — the coordinates of the j-th vertex of polygon B. The vertices are given in the clockwise order. The coordinates of the polygon's vertices are separated by a single space. It is guaranteed that polygons A and B are non-degenerate, that polygon A is strictly convex, that polygon B has no self-intersections and self-touches and also for each polygon no three consecutively following vertices are located on the same straight line. Output Print on the only line the answer to the problem — if polygon B is strictly inside polygon A, print "YES", otherwise print "NO" (without the quotes). Examples Input 6 -2 1 0 3 3 3 4 1 3 -2 2 -2 4 0 1 2 2 3 1 1 0 Output YES Input 5 1 2 4 2 3 -3 -2 -2 -2 1 4 0 1 1 2 4 1 2 -1 Output NO Input 5 -1 2 2 3 4 1 3 -2 0 -3 5 1 0 1 1 3 1 5 -1 2 -1 Output NO
import sys input = sys.stdin.readline a, b, temp = [], [], set() arr = [] n = int(input()) for _ in range(n): x, y = [int(x) for x in input().split()] arr.append([x, y]) m = int(input()) for _ in range(m): x, y = [int(x) for x in input().split()] temp.add((x, y)) arr.append([x, y]) arr.sort() p1 = arr[0] p2 = arr[-1] up = [p1] l1, l2 = 1, 1 down = [p1] for i in range(1, n + m): if ( i == n + m - 1 or p1[0] * (arr[i][1] - p2[1]) + arr[i][0] * (p2[1] - p1[1]) + p2[0] * (p1[1] - arr[i][1]) > 0 ): while ( l1 >= 2 and up[-2][0] * (up[-1][1] - arr[i][1]) + up[-1][0] * (arr[i][1] - up[-2][1]) + arr[i][0] * (up[-2][1] - up[-1][1]) < 0 ): up.pop() l1 -= 1 up.append(arr[i]) l1 += 1 if ( i == n + m - 1 or p1[0] * (arr[i][1] - p2[1]) + arr[i][0] * (p2[1] - p1[1]) + p2[0] * (p1[1] - arr[i][1]) < 0 ): while ( l2 >= 2 and down[-2][0] * (down[-1][1] - arr[i][1]) + down[-1][0] * (arr[i][1] - down[-2][1]) + arr[i][0] * (down[-2][1] - down[-1][1]) > 0 ): down.pop() l2 -= 1 down.append(arr[i]) l2 += 1 f = True up.append(p2) for x, y in up: if (x, y) in temp: f = False break for x, y in down: if (x, y) in temp: f = False break if f: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR LIST LIST FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER WHILE VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER WHILE VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Protection of the Indian border and safe transport of items from one point to another along the border are the paramount jobs for the Indian army. However they need some information about the protection status along the length of the border. The border can be viewed as the real x-axis. Along the axis, Indian army has N checkpoints for lookout. We know that each checkpoint is located at an integer location xi. Each checkpoint must have a fleet of armed men which are responsible for guarding the neighboring areas of the checkpoint and provide military assistance of all kinds. The size of the fleet is based on the location of the checkpoint and how active the region is for terrorist activities. Given the number of armed men assigned at the i^th checkpoint, as pi, this information is available for all checkpoints. With the skills of the armed men, it is known that if for the i^th checkpoint, the length on the x axis that they can defend is a closed interval [xi-pi, xi+pi]. Now, your task is to transport some military items from position S to the end position E on the x-axis. Input: First line of the input contains 3 integers N, S and E. N is the number of checkpoints that the Indian Army has on the border. Then N lines follow. i^th line contains 2 integers, xi and pi. Output: Print the total distance of the x-axisfrom S to E, that is not protected by the armed forces. Constraints: 1 ≤ N ≤ 10^5 1 ≤ xi, pi ≤ 10^18 xi + pi ≤ 10^18 1 ≤ S ≤ E ≤ 10^18 SAMPLE INPUT 5 229 8419 1795 95 4873 720 3149 81 6101 2325 3674 629 SAMPLE OUTPUT 2626
n, beg, end = list(map(int, input().split())) s = 0 e = 0 if beg < end: s = beg e = end else: s = end e = beg arr = {} arr1 = {} for _ in range(n): x, p = list(map(int, input().split())) arr[x] = p dic = {} for k in arr: mx = k + arr[k] mn = k - arr[k] dic[mn] = mx arr1 = sorted(dic.items()) i = 0 cur = s res = 0 while cur < e and i < n: if cur <= arr1[i][0]: if e > arr1[i][0]: res += arr1[i][0] - cur cur = arr1[i][1] else: res += e - cur cur = e elif cur < arr1[i][1]: cur = arr1[i][1] i += 1 if cur < e and res: res += e - arr1[n - 1][1] if res == 0: res = e - s print(res)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Protection of the Indian border and safe transport of items from one point to another along the border are the paramount jobs for the Indian army. However they need some information about the protection status along the length of the border. The border can be viewed as the real x-axis. Along the axis, Indian army has N checkpoints for lookout. We know that each checkpoint is located at an integer location xi. Each checkpoint must have a fleet of armed men which are responsible for guarding the neighboring areas of the checkpoint and provide military assistance of all kinds. The size of the fleet is based on the location of the checkpoint and how active the region is for terrorist activities. Given the number of armed men assigned at the i^th checkpoint, as pi, this information is available for all checkpoints. With the skills of the armed men, it is known that if for the i^th checkpoint, the length on the x axis that they can defend is a closed interval [xi-pi, xi+pi]. Now, your task is to transport some military items from position S to the end position E on the x-axis. Input: First line of the input contains 3 integers N, S and E. N is the number of checkpoints that the Indian Army has on the border. Then N lines follow. i^th line contains 2 integers, xi and pi. Output: Print the total distance of the x-axisfrom S to E, that is not protected by the armed forces. Constraints: 1 ≤ N ≤ 10^5 1 ≤ xi, pi ≤ 10^18 xi + pi ≤ 10^18 1 ≤ S ≤ E ≤ 10^18 SAMPLE INPUT 5 229 8419 1795 95 4873 720 3149 81 6101 2325 3674 629 SAMPLE OUTPUT 2626
N, S, E = list(map(int, input().split())) data = [] for i in range(N): x, p = list(map(int, input().split())) data.append([x - p, x + p]) data.sort() diff = 0 for i in range(len(data)): temp_arr = data[i] temp_start = temp_arr[0] temp_end = temp_arr[1] if temp_start >= E: break if temp_start > S: diff += temp_start - S S = temp_end elif S >= temp_start and S <= temp_end: S = temp_end else: pass if temp_end > E: break if S < E: diff += E - S print("{0}".format(diff))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Protection of the Indian border and safe transport of items from one point to another along the border are the paramount jobs for the Indian army. However they need some information about the protection status along the length of the border. The border can be viewed as the real x-axis. Along the axis, Indian army has N checkpoints for lookout. We know that each checkpoint is located at an integer location xi. Each checkpoint must have a fleet of armed men which are responsible for guarding the neighboring areas of the checkpoint and provide military assistance of all kinds. The size of the fleet is based on the location of the checkpoint and how active the region is for terrorist activities. Given the number of armed men assigned at the i^th checkpoint, as pi, this information is available for all checkpoints. With the skills of the armed men, it is known that if for the i^th checkpoint, the length on the x axis that they can defend is a closed interval [xi-pi, xi+pi]. Now, your task is to transport some military items from position S to the end position E on the x-axis. Input: First line of the input contains 3 integers N, S and E. N is the number of checkpoints that the Indian Army has on the border. Then N lines follow. i^th line contains 2 integers, xi and pi. Output: Print the total distance of the x-axisfrom S to E, that is not protected by the armed forces. Constraints: 1 ≤ N ≤ 10^5 1 ≤ xi, pi ≤ 10^18 xi + pi ≤ 10^18 1 ≤ S ≤ E ≤ 10^18 SAMPLE INPUT 5 229 8419 1795 95 4873 720 3149 81 6101 2325 3674 629 SAMPLE OUTPUT 2626
n, s, e = [int(i) for i in input().split()] res = 0 a = [] for i in range(n): x, p = [int(j) for j in input().split()] a.append([min(max(1, x - p, s), e), max(min(x + p, e), s)]) a.sort(key=lambda x: x[0]) tlen = e - s + 1 clen = 0 i = 1 curr = a[0][1] clen += max(0, a[0][0] - s) while i < n: if curr > a[i][0]: curr = max(curr, a[i][1]) i += 1 else: clen += a[i][0] - curr curr = a[i][1] i += 1 clen += max(0, e - curr) print(clen)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): n = len(start) i = 0 j = 0 while i < n and j < n: while i < n and start[i] == "X": i += 1 while j < n and end[j] == "X": j += 1 if i == n and j == n: return True if i == n or j == n: return False if start[i] != end[j]: return False if start[i] == "L" and i < j: return False if start[i] == "R" and i > j: return False i += 1 j += 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR STRING VAR VAR RETURN NUMBER IF VAR VAR STRING VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): s = [(c, i) for i, c in enumerate(start) if c == "L" or c == "R"] e = [(c, i) for i, c in enumerate(end) if c == "L" or c == "R"] return len(s) == len(e) and all( c1 == c2 and (i1 >= i2 and c1 == "L" or i1 <= i2 and c1 == "R") for (c1, i1), (c2, i2) in zip(s, e) )
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING VAR STRING RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): tmps = start.replace("X", "") tmpe = end.replace("X", "") if tmps != tmpe: return False sa, ta = [], [] i = 0 while i < len(start): if start[i] == "L": sa.append(i) elif start[i] == "R": sa.append(-i) if end[i] == "L": ta.append(i) elif end[i] == "R": ta.append(-i) i += 1 i = 0 while i < len(sa): if sa[i] < ta[i]: return False i += 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING IF VAR VAR RETURN NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): previous = {"L": "X", "X": "R"} if len(start) != len(end): return False start = list(start) for i in range(len(start)): if start[i] == end[i]: continue elif end[i] == "R": return False else: cur = end[i] if i == len(end) - 1: return False for j in range(i + 1, len(end)): if start[j] == cur: p = j while p > i: if start[p - 1] == previous[cur]: p -= 1 else: return False start[j] = start[p] start[p] = cur break return True
CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING RETURN NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): if start.replace("X", "") != end.replace("X", ""): return False t = 0 for i in range(len(start)): if start[i] == "L": while end[t] != "L": t += 1 if i < t: return False t += 1 t = 0 for i in range(len(start)): if start[i] == "R": while end[t] != "R": t += 1 if i > t: return False t += 1 return True
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING WHILE VAR VAR STRING VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING WHILE VAR VAR STRING VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): start_helper = [] for i in range(len(start)): if start[i] == "L" or start[i] == "R": start_helper.append((start[i], i)) end_helper = [] for i in range(len(end)): if end[i] == "L" or end[i] == "R": end_helper.append((end[i], i)) if len(start_helper) != len(end_helper): return False for i in range(len(start_helper)): if start_helper[i][0] != end_helper[i][0]: return False elif start_helper[i][0] == "L" and start_helper[i][1] < end_helper[i][1]: return False elif start_helper[i][0] == "R" and start_helper[i][1] > end_helper[i][1]: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): start_pair = [(i, c) for i, c in enumerate(start) if c != "X"] end_pair = [(i, c) for i, c in enumerate(end) if c != "X"] if len(start_pair) != len(end_pair): return False for (i_start, c_start), (i_end, c_end) in zip(start_pair, end_pair): if ( c_start != c_end or c_start == "L" and i_start < i_end or c_start == "R" and i_start > i_end ): return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR RETURN NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): for (i, x), (j, y) in itertools.zip_longest( ((i, x) for i, x in enumerate(start) if x != "X"), ((j, y) for j, y in enumerate(end) if y != "X"), fillvalue=(None, None), ): if x != y or x == "L" and i < j or x == "R" and i > j: return False return True
CLASS_DEF FUNC_DEF FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING NONE NONE IF VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR RETURN NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): if len(start) != len(end) or start.replace("X", "") != end.replace("X", ""): return False l, ll, r, rr = 0, 0, 0, 0 for i in range(len(start)): if start[i] == "L": l += 1 elif start[i] == "R": r += 1 if end[i] == "L": ll += 1 elif end[i] == "R": rr += 1 if l > ll or r < rr: return False return True
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def findNextChar(self, string, i, char): otherChar = "R" if char == "L" else "L" for i in range(i, len(string)): if string[i] == otherChar: return None if string[i] == char: return i return None def canTransform(self, start, end): end_i = 0 for i in range(len(start)): if start[i] != "X": end_i = self.findNextChar(end, end_i, start[i]) if ( end_i == None or start[i] == "L" and end_i > i or start[i] == "R" and end_i < i ): return False end_i += 1 for end_i in range(end_i, len(end)): if end[end_i] != "X": return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR STRING STRING STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NONE IF VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR RETURN NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): def countX(s, char="R"): if char == "L": s = s[::-1] countX = 0 ans = [] for c in s: if c == char: ans.append(countX) elif c == "X": countX += 1 return ans startLR = "".join(c for c in start if c != "X") endLR = "".join(c for c in end if c != "X") startR, startL = countX(start, "R"), countX(start, "L") endR, endL = countX(end, "R"), countX(end, "L") return ( startLR == endLR and all([(startR[i] <= endR[i]) for i in range(len(startR))]) and all([(startL[i] <= endL[i]) for i in range(len(startL))]) )
CLASS_DEF FUNC_DEF FUNC_DEF STRING IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! = 1.) For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f(x) = K. Example 1: Input: K = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2: Input: K = 5 Output: 0 Explanation: There is no x such that x! ends in K = 5 zeroes. Note: K will be an integer in the range [0, 10^9].
class Solution: def canTransform(self, start, end): if len(start) != len(end): return False r, l = 0, 0 for i in range(len(start)): if start[i] == "R": r += 1 if end[i] == "R": r -= 1 if start[i] == "L": l -= 1 if end[i] == "L": l += 1 if r > 0 and l != 0 or r < 0: return False if l > 0 and r != 0 or l < 0: return False return l == 0 and r == 0
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR NUMBER VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = sorted(list(map(int, input().split()))) if n == 1: print(-1) exit() if n == 2 and (a[1] - a[0]) % 2 == 0 and a[0] != a[1]: print(3) print(2 * a[0] - a[1], (a[0] + a[1]) // 2, 2 * a[1] - a[0]) exit() m = [(a[i] - a[i - 1]) for i in range(1, n)] if len(set(m)) == 1: if m[0] != 0: print(2) print(a[0] - m[0], a[-1] + m[0]) else: print(1) print(a[0]) exit() m.sort() if len(set(m)) > 2 or m[-1] != 2 * m[-2]: print(0) exit() print(1) for i in range(1, n): if a[i] - a[i - 1] != m[0]: print(a[i - 1] + m[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) a.sort() if n == 1: print(-1) elif n == 2: if a[1] == a[0]: print(1) print(a[0]) exit(0) elif (a[1] - a[0]) % 2 == 0: print(3) d = a[1] - a[0] print(a[0] - d, (a[1] + a[0]) // 2, a[1] + d) exit(0) else: print(2) d = a[1] - a[0] print(a[0] - d, a[1] + d) exit(0) else: d = {} d[a[1] - a[0]] = 1 for i in range(2, n): if a[i] - a[i - 1] in d.keys(): d[a[i] - a[i - 1]] += 1 elif len(d.keys()) == 1: d[a[i] - a[i - 1]] = 1 else: print(0) exit(0) if len(d.keys()) == 1: if max(d.keys()) == 0: print(1) print(a[0]) else: print(2) print(2 * a[0] - a[1], a[-1] + a[1] - a[0]) elif len(d.keys()) == 2: if max(d.keys()) == 2 * min(d.keys()): if d[max(d.keys())] == 1: print(1) for i in range(1, n): if a[i] - a[i - 1] == max(d.keys()): print(a[i - 1] + min(d.keys())) exit(0) else: print(0) exit(0) else: print(0) exit(0) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) arr = [int(x) for x in input().split()] arr.sort() if n == 1: print(-1) elif n == 2: d = abs(arr[1] - arr[0]) if d == 0: print(1) print(arr[0]) elif d & 1: print(2) print(arr[0] - d, arr[1] + d) else: print(3) print(*sorted([arr[0] - d, arr[1] + d, int((arr[0] + arr[1]) / 2)])) else: d1 = arr[1] - arr[0] d2 = arr[2] - arr[1] ans = [] if d1 == d2: done = False fault = False cnt = 0 for i in range(2, n): diff = arr[i] - arr[i - 1] if diff != d1: if d1 * 2 == diff: done = True ans.append((arr[i] + arr[i - 1]) // 2) else: fault = True cnt += 1 if cnt > 1: break if done == False: ans.append(arr[0] - d1) ans.append(arr[-1] + d1) if fault or cnt > 1: ans = [-7649891] else: done = False fault = False cnt = 0 for i in range(1, n): diff = arr[i] - arr[i - 1] if diff != d1: if d1 * 2 == diff: done = True ans.append((arr[i] + arr[i - 1]) // 2) else: ans = [] fault = True break cnt += 1 if cnt > 1: fault = False ans = [-7649891] break if fault: fault = False cnt = 0 for i in range(1, n): diff = arr[i] - arr[i - 1] if diff != d2: if d2 * 2 == diff: done = True ans.append((arr[i] + arr[i - 1]) // 2) else: fault = True cnt += 1 if cnt > 1: break if fault or cnt > 1: ans = [-7649891] if ans and ans[0] == -7649891: print(0) else: ans = list(set(ans)) print(len(ans)) ans.sort() print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
import sys n = int(input()) arr = list(map(int, input().split())) if n == 1: print(-1) sys.exit() arr.sort() diff = [] req = [] flag2 = True cnt = 0 for i in range(1, n): diff.append(arr[i] - arr[i - 1]) d = list(set(diff)) cnt = len(d) if cnt > 2: print(0) sys.exit() if cnt == 1: req.append(arr[0] - diff[0]) req.append(arr[n - 1] + diff[0]) if d[0] % 2 == 0 and n == 2: req.append(arr[0] + d[0] // 2) if cnt == 2: d1 = d[0] d2 = d[1] d1c = diff.count(d1) d2c = diff.count(d2) if d1 == 2 * d2 and d1c == 1 and d2c == n - 2: index = diff.index(d1) req.append(arr[index] + d2) if d2 == 2 * d1 and d2c == 1 and d1c == n - 2: index = diff.index(d2) req.append(arr[index] + d1) req = list(set(req)) print(len(req)) req.sort() if len(req) > 0: print(*req)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) l = list(map(int, input().split())) l = sorted(l) if n == 1: print(-1) exit() if n == 2: if (l[1] - l[0]) % 2 == 0: if l[1] - l[0] == 0: print(1) print(l[0]) exit() print(3) print(l[0] - (l[1] - l[0]), l[0] + (l[1] - l[0]) // 2, l[1] + (l[1] - l[0])) else: print(2) print(l[0] - (l[1] - l[0]), l[1] + (l[1] - l[0])) exit() q = {} for i in range(1, n): q[l[i] - l[i - 1]] = q.get(l[i] - l[i - 1], 0) + 1 d1 = q q = list(q) q = sorted(q) if len(q) == 1: if q[0] == 0: print(1) print(l[0]) else: print(2) print(l[0] - q[0], l[-1] + q[0]) elif q[0] * 2 == q[1] and len(q) == 2 and d1[q[1]] == 1: print(1) for i in range(1, n): if l[i] - l[i - 1] == q[1]: print(l[i] - q[0]) exit() else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
from sys import stdin input = stdin.readline def ap(arr): if len(arr) == 1: return -1 dif = [] ans = [] arr = sorted(arr) l = set() for i in range(0, len(arr) - 1): d = arr[i + 1] - arr[i] dif.append(d) l.add(d) if len(l) > 2: return 0 if len(set(dif)) == 1: if dif[0] == 0: ans.append(arr[0]) print(len(ans)) print(*ans) return "" else: ans.append(arr[0] - dif[0]) ans.append(arr[-1] + dif[0]) if len(arr) > 3: print(len(ans)) print(*ans) return "" mini = max(dif) maxi = min(dif) if mini / 2 != maxi and len(set(dif)) >= 2: return 0 if len(arr) == 2 and len(set(dif)) == 1: if (arr[0] + arr[1]) % 2 == 0: d = (arr[0] + arr[1]) // 2 ans.append(d) if maxi == 0 and len(set(dif)) >= 2: return 0 if len(set(dif)) == 2: if dif.count(mini) == 1 and mini != 0: for i in range(0, len(arr) - 1): if arr[i + 1] - arr[i] == mini: ans.append(arr[i] + maxi) else: return 0 ans = sorted(ans) if len(set(dif)) > 2: return 0 print(len(ans)) print(*ans) return "" a = input() lst = list(map(int, input().strip().split())) print(ap(lst))
ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) arr = [int(x) for x in input().split()] cd = {} arr.sort() for i in range(1, n): cdtemp = arr[i] - arr[i - 1] if cdtemp not in cd: cd[cdtemp] = 1 else: cd[cdtemp] += 1 if len(cd) > 2: print(0) elif len(cd) == 1: for key, val in cd.items(): if key == 0: num = 1 ans = [arr[0]] elif len(arr) == 2 and key % 2 == 0: num = 3 ans = [arr[0] - key, arr[0] + key // 2, arr[1] + key] else: num = 2 ans = [arr[0] - key, arr[-1] + key] print(num) print(*ans) elif len(cd) == 2: com = [] for key, val in cd.items(): com.append(key) com.sort() if com[0] * 2 == com[1] and cd[com[1]] == 1: ans = [] for i in range(1, n): if arr[i] - arr[i - 1] == com[1]: ans.append(arr[i - 1] + com[0]) break print(1) print(*ans) else: print(0) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) if n == 1: print(-1) quit() l = list(map(int, input().split())) l.sort() if n == 2: d = l[1] - l[0] if d == 0: print(1) print(l[0]) quit() if d % 2 == 0: print(3) print(l[0] - d, l[0] + d // 2, l[1] + d) else: print(2) print(l[0] - d, l[1] + d) else: des = [] for i in range(1, n): des.append(l[i] - l[i - 1]) des.sort() d = des[0] flag = True ans = 0 for i in des: if i != d: if i == 2 * d and flag: flag = False ans = i else: print(0) quit() if flag: if d == 0: print(1) print(l[0]) else: print(2) print(l[0] - d, l[-1] + d) else: print(1) for i in range(1, n): if l[i] - l[i - 1] == ans: print(l[i] - ans // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) p = input().rstrip().split(" ") p.sort(key=int) if len(p) == 1: print(-1) elif len(set(p)) == 1: print(1) print(p[0]) elif len(p) == 2: if int(p[0]) + 2 == int(p[1]): print(3) print(int(p[0]) - 2, int(p[0]) + 1, int(p[len(p) - 1]) + 2) elif (int(p[0]) + int(p[1])) % 2 == 0: print(3) print( int(p[0]) - (int(p[1]) - int(p[0])), (int(p[0]) + int(p[1])) // 2, int(p[len(p) - 1]) + (int(p[1]) - int(p[0])), ) else: print(2) print( int(p[0]) - (int(p[1]) - int(p[0])), int(p[len(p) - 1]) + (int(p[1]) - int(p[0])), ) else: l = [] for i in range(0, len(p) - 1): l.append(int(p[i + 1]) - int(p[i])) if len(list(set(l))) == 1: print(2) print(int(p[0]) - l[0], int(p[len(p) - 1]) + l[0]) elif len(list(set(l))) == 2 and 0 not in l: x = list(set(l)) if l.count(x[0]) == 1 or l.count(x[1]) == 1: maxf = -999999999 for i in range(0, len(x)): if l.count(x[i]) > maxf: maxf = x[i] if x[0] != maxf: C = l.index(x[0]) w = [] p.insert(C + 1, int(p[C]) + maxf) for j in range(0, len(p) - 1): w.append(int(p[j + 1]) - int(p[j])) if len(list(set(w))) == 1: print(1) print(int(p[C]) + maxf) else: print(0) elif x[1] != maxf: C = l.index(x[1]) w = [] p.insert(C + 1, int(p[C]) + maxf) for j in range(0, len(p) - 1): w.append(int(p[j + 1]) - int(p[j])) if len(list(set(w))) == 1: print(1) print(int(p[C]) + maxf) else: print(0) else: print(0) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = input().split() for i in range(n): a[i] = int(a[i]) a.sort() if n == 1: print(-1) elif n == 2: if a[0] == a[1]: print(1) print(a[0]) elif (a[1] - a[0]) % 2 == 0: print(3) print(a[0] - (a[1] - a[0]), (a[0] + a[1]) // 2, a[1] + (a[1] - a[0])) else: print(2) print(a[0] - (a[1] - a[0]), a[1] + (a[1] - a[0])) else: dic = {} for i in range(1, n): if a[i] - a[i - 1] in dic.keys(): dic[a[i] - a[i - 1]] += 1 else: dic[a[i] - a[i - 1]] = 1 if len(dic.keys()) == 1: if a[0] == a[n - 1]: print(1) print(a[0]) else: print(2) print(a[0] - (a[1] - a[0]), a[n - 1] + (a[1] - a[0])) elif len(dic.keys()) == 2: ls = [i for i in dic.keys()] ls.sort() if dic[ls[1]] == 1 and ls[1] % 2 == 0 and ls[0] == ls[1] // 2: print(1) for i in range(1, n): if a[i] - a[i - 1] == ls[1]: print(a[i - 1] + ls[0]) break else: print(0) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) if n == 1: print(-1) return a.sort() d = [] for i in range(1, n): d.append(a[i] - a[i - 1]) if min(d) == max(d) == 0: print(1) print(a[0]) elif n == 2: if d[0] % 2: print(2) print(a[0] - d[0], a[1] + d[0]) else: print(3) print(a[0] - d[0], a[0] + d[0] // 2, a[1] + d[0]) elif min(d) == max(d): print(2) print(a[0] - d[0], a[-1] + d[0]) else: m1 = 0 m2 = 0 for i in range(1, n - 1): if d[i] < d[m1]: m1 = i if d[i] > d[m2]: m2 = i c = d.count(d[m1]) if c == n - 2 and d[m1] * 2 == d[m2]: print(1) print(a[m2] + d[m1]) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
import sys input = sys.stdin.readline for _ in range(1): n = int(input()) arr = [int(x) for x in input().split()] arr.sort() if n == 1: print(-1) elif n == 2: if arr[1] - arr[0] == 0: print(1) print(arr[0]) elif (arr[1] - arr[0]) % 2: print(2) print(arr[0] - (arr[1] - arr[0]), arr[-1] + arr[1] - arr[0]) else: print(3) print( arr[0] - (arr[1] - arr[0]), (arr[0] + arr[1]) // 2, arr[-1] + arr[1] - arr[0], ) else: arr.sort() if arr[0] == arr[-1]: print(1) print(arr[0]) exit(0) diff = arr[1] - arr[0] i = 1 while i < n: if arr[i] - arr[i - 1] == diff: i += 1 else: break if i == n: print(2) print(arr[0] - diff, arr[-1] + diff) exit(0) c = 0 f = True temp = [] diff = (arr[-1] - arr[0]) // n for i in range(1, n): if arr[i] - arr[i - 1] != diff and arr[i] - arr[i - 1] != 2 * diff: f = False if arr[i] - arr[i - 1] == 2 * diff: c += 1 temp.append(arr[i]) if c == 1 and f == True: print(1) print(temp[0] - diff) else: print(0)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def all_same(a): return all([(x == a[0]) for x in a]) N = int(input()) array = sorted([int(x) for x in input().split()]) tobeprinted = [] setarray = set(array) if N == 1: print(-1) exit() elif all_same(array): print(1) print(array[0]) exit() elif N == 2: difference = array[1] - array[0] tobeprinted.append(array[1] + difference) tobeprinted.append(array[0] - difference) if (array[1] + array[0]) % 2 == 0: tobeprinted.append((array[1] + array[0]) // 2) print(len(tobeprinted)) print(*sorted(tobeprinted)) exit() else: commondifference = [] for i in range(1, N): commondifference.append(array[i] - array[i - 1]) if all_same(commondifference): print(2) print(array[0] - commondifference[0], array[-1] + commondifference[0]) exit() unique = {} for i in commondifference: if i in unique: unique[i] += 1 else: unique[i] = 1 commondifference = sorted(commondifference) if len(unique) == 2 and ( unique[commondifference[0]] == 1 or unique[commondifference[-1]] == 1 ): dummy = [] if commondifference[0] == 0 or commondifference[-1] == 0: print(0) exit() elif unique[commondifference[0]] > unique[commondifference[1]]: realcommondifference = commondifference[0] elif unique[commondifference[0]] < unique[commondifference[1]]: realcommondifference = commondifference[1] else: realcommondifference = min(commondifference[0], commondifference[1]) setdummy = set() for i in range(N + 1): setdummy.add(array[0] + i * realcommondifference) if array[0] + i * realcommondifference not in setarray: dummy.append(array[0] + i * realcommondifference) for i in array: if i not in setdummy: print(0) exit() if len(dummy) == 1: print(1) print(dummy[0]) exit() else: print(0) exit() else: print(0)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) li = list(map(int, input().split())) if n == 1: print(-1) elif n == 2: li.sort() d = li[1] - li[0] if d == 0: print(1) print(li[0]) elif d % 2 == 0: print(3) print(li[0] - d, li[0] + d // 2, li[1] + d) else: print(2) print(li[0] - d, li[1] + d) else: flag = 0 li.sort() d = li[1] - li[0] for i in range(2, n): if li[i] - li[i - 1] < d: d = li[i] - li[i - 1] for i in range(1, n): if li[i] - li[i - 1] != d: if li[i] - li[i - 1] == 2 * d: flag += 1 ind = i else: flag = -1 break if d == 0 and flag == 0: print(1) print(li[0]) elif flag == -1 or flag > 1: print(0) elif flag == 1: print(1) print(li[ind] - d) else: print(2) print(li[0] - d, li[-1] + d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) list_numbers = list(map(int, input().split())) list_numbers.sort() def ap(list_numbers, n): done = False if n == 1: done = True return print(-1) if n == 2: if (list_numbers[0] + list_numbers[1]) % 2 == 0: x = int((list_numbers[0] + list_numbers[1]) / 2) y = list_numbers[1] - list_numbers[0] if list_numbers[0] - y == x == list_numbers[1] + y: print(1) print(x) done = True else: print(3) print( str(list_numbers[0] - y) + " " + str(x) + " " + str(list_numbers[1] + y) ) done = True else: y = list_numbers[1] - list_numbers[0] print(2) print(str(list_numbers[0] - y) + " " + str(list_numbers[1] + y)) done = True i = 0 i_to_use = 1 previous_num = 0 difference = " " difference_2 = " " if n > 2: if list_numbers[2] - list_numbers[1] < list_numbers[1] - list_numbers[0]: difference_2 = list_numbers[1] - list_numbers[0] difference = list_numbers[2] - list_numbers[1] for j in list_numbers: if i < 3: previous_num = j i += 1 elif difference == " ": difference = j - previous_num previous_num = j i += 1 elif difference != j - previous_num: if difference_2 == " ": difference_2 = j - previous_num i_to_use = i i += 1 continue else: print(0) done = True break else: previous_num = j i += 1 else: for j in list_numbers: if i == 0: previous_num = j i += 1 elif difference == " ": difference = j - previous_num previous_num = j i += 1 elif difference != j - previous_num: if difference_2 == " ": difference_2 = j - previous_num i_to_use = i i += 1 previous_num = j continue else: print(str(0)) done = True break else: previous_num = j i += 1 if not done: if difference_2 == " ": if list_numbers[0] - difference == list_numbers[-1] + difference: print(1) print(str(list_numbers[0] - difference)) else: print(2) print( str(list_numbers[0] - difference) + " " + str(list_numbers[-1] + difference) ) elif difference_2 == difference * 2: print(1) print(list_numbers[i_to_use - 1] + difference) else: print(str(0)) ap(list_numbers, n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR IF VAR STRING IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) if n == 1: print(-1) else: a.sort() d = [] for i in range(n - 1): d.append(a[i + 1] - a[i]) k = list(set(d)) k.sort() uni = {} for elem in d: if elem not in uni: uni[elem] = 1 else: uni[elem] += 1 if len(k) > 2: print(0) elif 0 in k: if len(k) == 1: print(1) print(a[0]) else: print(0) elif len(k) == 2: if uni[k[1]] > 1: print(0) elif k[1] % 2: print(0) elif k[1] == 2 * k[0]: print(1) print(int((a[d.index(k[1]) + 1] + a[d.index(k[1])]) / 2)) else: print(0) elif n == 2: if k[0] % 2: print(2) ans = [str(int(a[0] - k[0])), str(int(a[1] + k[0]))] print(" ".join(ans)) else: ans = [ str(int(a[0] - k[0])), str(int((a[0] + a[1]) / 2)), str(int(a[1] + k[0])), ] print(3) print(" ".join(ans)) else: ans = [str(int(a[0] - k[0])), str(int(a[n - 1] + k[0]))] print(2) print(" ".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
import sys n = int(input()) a = sorted(list(map(int, input().split()))) if n == 1: print(-1) else: d = a[1] - a[0] x = 0 v = 0 t = -1 v1 = 0 t1 = -1 for i in range(1, n): if a[i] - a[i - 1] == d: x += 1 elif a[i] - a[i - 1] == 2 * d: v += 1 t = i - 1 elif 2 * (a[i] - a[i - 1]) == d: v1 += 1 t1 = i - 1 else: print(0) return if v >= 2: print(0) return if v == 1: print(1) print(a[t] + d) return if v1 > 0 and v1 + 1 < n - 1: print(0) return if v1 > 0 and v1 + 1 == n - 1: print(1) print(a[0] + d // 2) return if d == 0: print(1) print(a[0]) return ans = [a[0] - d] if n == 2: if d % 2 == 0: ans.append(a[0] + d // 2) ans.append(a[n - 1] + d) print(len(ans)) for i in ans: print(i, end=" ")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN IF VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR LIST BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = [int(i) for i in input().split()] if n == 1: print(-1) exit() a.sort() if n == 2: mid = (a[0] + a[1]) / 2 if a[0] == a[1]: print(1) print(a[0]) exit() if mid == int(mid): mid = int(mid) cd = a[1] - a[0] print(3) print(a[0] - cd, mid, a[1] + cd) exit() else: print(2) cd = a[1] - a[0] print(a[0] - cd, a[1] + cd) exit() d = sorted([(a[i] - a[i - 1]) for i in range(1, n)]) if len(set(d)) == 1: if d[0] == 0: print(1) print(a[0] + d[0]) exit() print(2) print(a[0] - d[0], a[n - 1] + d[0]) exit() elif len(set(d)) > 2 or d[-1] != 2 * d[-2]: print(0) exit() else: for i in range(1, n): if a[i] - a[i - 1] != d[0]: print(1) print(a[i - 1] + d[0]) exit()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR